I see a few problems...
(1) You're attempting to replace a buffer in the input buffer array. MediaCodec
doesn't work like this -- the framework provides the buffers, and you copy the data into them. The idea is that, by allowing the framework to do the allocation, it can potentially avoid copying the data later on.
You need to get the array of input buffers from decoder.getInputBuffers()
, and use those. Make sure to clear()
the ByteBuffer
to reset the position and limit each time.
(2) You're writing a single packet of data and expecting a frame of output data. In practice, you may need to supply multiple buffers of data before the first frame is generated. See this post for an example. In some profiles the encoder is allowed to reorder frames, so even after the decoder starts going you can't just feed a frame and wait for decoded data to pop out the other side.
(3) The AVC decoder needs the SPS/PPS data, which you can provide via a buffer with the BUFFER_FLAG_CODEC_CONFIG
flag set, or by adding the data with "csd-0" / "csd-1" keys to the MediaFormat
using MediaFormat#setByteBuffer()
. Examples of both approaches can be found in EncodeDecodeTest.
There are a number of AVC decoding examples on bigflake, but the data source is the MediaCodec
encoder, so they generally get point #3 for free.
This posting may be useful for you.
For displaying the frames, you can see different approaches in Grafika (which generally works with .mp4 files, so the encode/decode implementation there isn't as relevant).