How to use MediaCodec without MediaExtractor for H264

Solution 1:

I'm assuming you're reading a raw H.264 elementary stream and not an MP4 file.

It looks like you're feeding fixed-size blocks of data to the decoder. That doesn't work. You need to put a single access unit into each buffer.

Solution 2:

To your last question i.e. how can you get SPS and PPS values, you need to have a mechanism to read the same from the file.

If you are having an elementary stream file, then you would need to parse the file, identify NALU headers and extract the content.

If you have container format, you will need to have a mechanism to read the file format of the container type and extract the information.

If you have a streaming input, then you can receive the content from the incoming SDP information.

As for your current code, I would recommend concatenating both SPS and PPS into one buffer and provide the same to the underlying codec as shown below

byte[] csd_info = { 0, 0, 0, 1, 103, 100, 0, 40, -84, 52, -59, 1, -32, 17, 31, 120, 11, 80, 16, 16, 31, 0, 0, 3, 3, -23, 0, 0, -22, 96, -108, 0, 0, 0, 1, 104, -18, 60, -128 };
format.setByteBuffer("csd-0", ByteBuffer.wrap(csd_info));
format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 1920 * 1080);
format.setInteger("durationUs", 63446722);