How do I feed H.264 NAL units to Android MediaCodec for decoding?

I'm trying to figure out how to use Android's MediaCodec class to decode H.264 video. To start, I'm trying to manually parse the NAL units out of an H.264 file and feed them to MediaCodec for decoding. I believe I'm parsing the NAL units out of the file correctly (searching for 0x00 0x00 0x01 sequence in file, indicates the start of a NAL unit), but MediaCodec always times out and returns -1 each time I make a call to dequeueOutputBuffer(). Does anyone know the specifics of how to feed H.264 NAL units to MediaCodec for decoding? Am I supposed to strip off the 0x00 0x00 0x01 sequence before I send it a NAL unit? Do I need to ignore/drop certain NAL unit types? Do I need to prepend the extracted SPS and PPS info (with or without the 0x00 0x00 0x01 sequence) before each NAL unit? I'm playing around with a lot of ideas here and none of them are panning out. Any guidance in this realm would be very much appreciated.


As far as what I have experienced, the decoder will return -1 on dequeueOutputBuffer() as long as you don't feed it the sps/pps info. When you do that, the decoder should return with the INFO_OUTPUT_FORMAT_CHANGED flag (-2) on dequeueOutputBuffer(), and after that will properly return decoded frames.

If the decoder still returns -1, try adding the sps/pps info to the decoder with the flag BUFFER_FLAG_CODEC_CONFIG.


AFAIK you need to take the header out as well as feed it with SPS/PSS as first buffers, But getting -1 is not a big deal it will be a normal behaivour. Some devices will crash if they are not feeded by SPS/PPS before the other NALs

链接地址: http://www.djcxy.com/p/69914.html

上一篇: 将unicode字符串写入Excel 2007

下一篇: 如何将H.264 NAL单元提供给Android MediaCodec进行解码?