问题
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.
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/15305241/how-do-i-feed-h-264-nal-units-to-android-mediacodec-for-decoding