一点点摸透MediaCodec的心

MediaCodecAndroid L之后提供的底层多媒体基础框架中,负责编解码的重要的类。

Q:MediaCodec是否存在实例数量的限制?

A:答案是肯定的。
如果创建的时候超过限制:

  1. 通过createDecoderByType/createEncoderByType创建,系统会自动选用下一个可用的Codec来实例化,如Google内置的软解码器OMX.google.h264.decoder
    异常时会有系统日志:
E/ACodec: Allocating component 'OMX.qcom.video.encoder.avc' failed, try next one.
E/OMX: FAILED to allocate omx component 'OMX.qcom.video.encoder.avc' err=InsufficientResources(0x80001000)
  1. 通过createByCodecName创建,则会抛出如下类似异常。
Caused by: android.media.MediaCodec$CodecException: Failed to initialize OMX.qcom.video.encoder.avc, error 0xfffffff4
        at android.media.MediaCodec.native_setup(Native Method)
        at android.media.MediaCodec.<init>(MediaCodec.java:1776)
        at android.media.MediaCodec.createByCodecName(MediaCodec.java:1757)

Q:如有限制,限制是多少?

A:手头的机器:坚果Pro,目标类型video/avc

Codec Name类型Max
OMX.qcom.video.encoder.avc编码器16
OMX.google.h264.encoder编码器100+
OMX.qcom.video.decoder.avc解码器16
OMX.qcom.video.decoder.avc.secure解码器16
OMX.google.h264.decoder解码器100+

内置的软编解码器可以认为是没有实例数量限制。而高通的硬件编解码器,实例上限是16个。

successfully create 16 times for OMX.qcom.video.encoder.avc
successfully create 100 times for OMX.google.h264.encoder
successfully create 16 times for OMX.qcom.video.decoder.avc
successfully create 16 times for OMX.qcom.video.decoder.avc.secure
successfully create 100 times for OMX.google.h264.decoder

另外,需要注意的是,这个实例上限应该是共享的(需要更进一步的信息佐证),如果一直创建而没有释放的话,比如如下场景:

  1. 创建了16个硬件解码器OMX.qcom.video.decoder.avc但不做释放。
  2. 接下来创建OMX.qcom.video.decoder.avc.secure硬件解码器,0个实例创建成功。
  3. 同样的创建OMX.qcom.video.encoder.avc硬件编码器,0个实例创建成功。
successfully create 16 times for OMX.qcom.video.decoder.avc
successfully create 0 times for OMX.qcom.video.decoder.avc.secure
successfully create 100 times for OMX.google.h264.decoder
successfully create 0 times for OMX.qcom.video.encoder.avc
successfully create 100 times for OMX.google.h264.encoder

附其他机型的结果:

华为P9

格式类型Codec NameMax
AAC解码器OMX.google.aac.decoder100+
AVC编码器OMX.IMG.TOPAZ.VIDEO.Encoder8
AVC编码器OMX.google.h264.encoder100+
AVC解码器OMX.IMG.MSVDX.Decoder.AVC100+
AVC解码器OMX.google.h264.decoder100+

华为P9的硬件解码器能创建超过100个实例,有点窎喔。

successfully create 100 times for OMX.google.aac.decoder
successfully create 8 times for OMX.IMG.TOPAZ.VIDEO.Encoder
successfully create 100 times for OMX.google.h264.encoder
successfully create 100 times for OMX.IMG.MSVDX.Decoder.AVC
successfully create 100 times for OMX.google.h264.decoder

Google Pixel XL

格式类型Codec NameMax
AAC解码器OMX.google.aac.decoder100+
AVC编码器OMX.qcom.video.encoder.avc16
AVC编码器OMX.google.h264.encoder100+
AVC解码器OMX.qcom.video.decoder.avc16
AVC解码器OMX.qcom.video.decoder.avc.secure6
AVC解码器OMX.google.h264.decoder100+
successfully create 100 times for OMX.google.aac.decoder
successfully create 16 times for OMX.qcom.video.encoder.avc
successfully create 100 times for OMX.google.h264.encoder
successfully create 16 times for OMX.qcom.video.decoder.avc
successfully create 6 times for OMX.qcom.video.decoder.avc.secure
successfully create 100 times for OMX.google.h264.decoder

Oppo R9s

格式类型Codec NameMax
AAC解码器OMX.google.aac.decoder100+
AVC编码器OMX.qcom.video.encoder.avc16
AVC编码器OMX.google.h264.encoder100+
AVC解码器OMX.qcom.video.decoder.avc16
AVC解码器OMX.qcom.video.decoder.avc.secure16
AVC解码器OMX.google.h264.decoder100+
successfully create 100 times for OMX.google.aac.decoder
successfully create 16 times for OMX.qcom.video.decoder.avc
successfully create 16 times for OMX.qcom.video.decoder.avc.secure
successfully create 100 times for OMX.google.h264.decoder
successfully create 16 times for OMX.qcom.video.encoder.avc
successfully create 100 times for OMX.google.h264.encoder

Meizu M5 Note

格式类型Codec NameMax
AAC解码器OMX.google.aac.decoder100+
AAC解码器OMX.ffmpeg.aac.decoder100+
AVC编码器OMX.MTK.VIDEO.ENCODER.AVC16
AVC编码器OMX.google.h264.encoder100+
AVC解码器OMX.MTK.VIDEO.DECODER.AVC16
AVC解码器OMX.google.h264.decoder100+

没想到魅族这款机器竟然还有ffmpeg实现的aac解码器

successfully create 100 times for OMX.google.aac.decoder
successfully create 100 times for OMX.ffmpeg.aac.decoder
successfully create 16 times for OMX.MTK.VIDEO.DECODER.AVC
successfully create 100 times for OMX.google.h264.decoder
successfully create 16 times for OMX.MTK.VIDEO.ENCODER.AVC
successfully create 100 times for OMX.google.h264.encoder

Q: 硬编/解码器和软编/解码器的性能差距有多大?

A:性能肯定有差距。上面提到,当系统无法分配硬件解码器的时候,会分配默认的软解码器。在有的机器上,会出现卡成PPT的机器(需补充具体的机器),而在有的机器上,则属于可以接受(没有明显卡顿的情况,如坚果Pro,Oppo R9s)

这里要补充性能测试数据

Q: 硬编码会有什么坑

  1. MediaMuxer 输出文件不支持流式播放
  2. 华为P9 机器硬编码生成的视频在iOS上播放会有一半绿屏
    原文作者:罗力
    原文地址: https://www.jianshu.com/p/ec7ff19ddbff
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞