Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 242
unbrokenrabbit Avatar asked Mar 08 '13 23:03

unbrokenrabbit


1 Answers

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.

like image 106
Robert Ende Avatar answered Sep 29 '22 12:09

Robert Ende