一點點摸透MediaCodec的心

MediaCodecAndroid L之后提供的底層多媒體基礎(chǔ)框架中,負(fù)責(zé)編解碼的重要的類。

Q:MediaCodec是否存在實例數(shù)量的限制?

A:答案是肯定的。
如果創(chuàng)建的時候超過限制:

  1. 通過createDecoderByType/createEncoderByType創(chuàng)建,系統(tǒng)會自動選用下一個可用的Codec來實例化,如Google內(nèi)置的軟解碼器OMX.google.h264.decoder
    異常時會有系統(tǒng)日志:
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創(chuàng)建,則會拋出如下類似異常。
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,目標(biāo)類型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+

內(nèi)置的軟編解碼器可以認(rèn)為是沒有實例數(shù)量限制。而高通的硬件編解碼器,實例上限是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

另外,需要注意的是,這個實例上限應(yīng)該是共享的(需要更進(jìn)一步的信息佐證),如果一直創(chuàng)建而沒有釋放的話,比如如下場景:

  1. 創(chuàng)建了16個硬件解碼器OMX.qcom.video.decoder.avc但不做釋放。
  2. 接下來創(chuàng)建OMX.qcom.video.decoder.avc.secure硬件解碼器,0個實例創(chuàng)建成功。
  3. 同樣的創(chuàng)建OMX.qcom.video.encoder.avc硬件編碼器,0個實例創(chuàng)建成功。
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

附其他機型的結(jié)果:

華為P9

格式 類型 Codec Name Max
AAC 解碼器 OMX.google.aac.decoder 100+
AVC 編碼器 OMX.IMG.TOPAZ.VIDEO.Encoder 8
AVC 編碼器 OMX.google.h264.encoder 100+
AVC 解碼器 OMX.IMG.MSVDX.Decoder.AVC 100+
AVC 解碼器 OMX.google.h264.decoder 100+

華為P9的硬件解碼器能創(chuàng)建超過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 Name Max
AAC 解碼器 OMX.google.aac.decoder 100+
AVC 編碼器 OMX.qcom.video.encoder.avc 16
AVC 編碼器 OMX.google.h264.encoder 100+
AVC 解碼器 OMX.qcom.video.decoder.avc 16
AVC 解碼器 OMX.qcom.video.decoder.avc.secure 6
AVC 解碼器 OMX.google.h264.decoder 100+
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 Name Max
AAC 解碼器 OMX.google.aac.decoder 100+
AVC 編碼器 OMX.qcom.video.encoder.avc 16
AVC 編碼器 OMX.google.h264.encoder 100+
AVC 解碼器 OMX.qcom.video.decoder.avc 16
AVC 解碼器 OMX.qcom.video.decoder.avc.secure 16
AVC 解碼器 OMX.google.h264.decoder 100+
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 Name Max
AAC 解碼器 OMX.google.aac.decoder 100+
AAC 解碼器 OMX.ffmpeg.aac.decoder 100+
AVC 編碼器 OMX.MTK.VIDEO.ENCODER.AVC 16
AVC 編碼器 OMX.google.h264.encoder 100+
AVC 解碼器 OMX.MTK.VIDEO.DECODER.AVC 16
AVC 解碼器 OMX.google.h264.decoder 100+

沒想到魅族這款機器竟然還有ffmpeg實現(xiàn)的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:性能肯定有差距。上面提到,當(dāng)系統(tǒng)無法分配硬件解碼器的時候,會分配默認(rèn)的軟解碼器。在有的機器上,會出現(xiàn)卡成PPT的機器(需補充具體的機器),而在有的機器上,則屬于可以接受(沒有明顯卡頓的情況,如堅果Pro,Oppo R9s)

這里要補充性能測試數(shù)據(jù)

Q: 硬編碼會有什么坑

  1. MediaMuxer 輸出文件不支持流式播放
  2. 華為P9 機器硬編碼生成的視頻在iOS上播放會有一半綠屏
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容