Home Ask Login Register

Developers Planet

Your answer is one click away!

Sami Kuhmonen February 2016

MediaCodec decoding h264 wrong on one device

I'm trying to decode h.264 stream using Android's MediaCodec interface. Everything is working just fine on my test devices, but on one customer device that I dont' have access to (Samsung Tab S) there are strange issues.

When I decode the stream I don't send any SPS/PPS NALs or an initial frame. I just start pushing the data from the live stream, chopped into blocks ending with a 0x09 NAL and the decoder will synchronize itself nicely without problems quite quickly.

The issue with at least this one device is that when I get a BufferInfo from the decoder it will claim it decoded 1413120 bytes of data, but the buffer size is only 1382400! So of course if I even try to get that much data out of the buffer it will crash.

The video is 1280x720 and is decoded into NV12, so the buffer size is just fine. The reported decoded output size isn't. If I force the size as 1382400 and convert the NV12 into RGB I get almost correct picture. The first 32 lines have a strong green color and the blue channel is shifted by quite a lot. This means that the UV block is decoded partially wrong on this device.

Has anyone run into this kind of an issue before? I have recorded the raw h264 stream from that specific device and it plays just fine with no green blocks or color shifts.

Should I really set up SPS/PPS and an initial frame before starting the streaming? The stream seems to contain everything needed since the decoder realizes the resolution to be correct, sets up buffers and decodes on every other device I've tested except this one. So I'm just wondering if Samsung has something special going on.

Another application decoding the same stream shows it without issues, but as far as I know they use ffmpeg internally, not MediaCodec. I would rather use built-in system codecs if possible.

Here is an example of the result. Don't have an image of just the stream, do note that the frame is rotated. The Y component in the green area i


Dean Wild February 2016

I had similar issues in the past (specifically on Samsung devices) and if I remember correctly it was due to the missing SPS/PPS data. You must feed in the SPS/PPS data if you want consistent results.

Not a direct solution to your issue but a possible workaround is to use an alternative decoder (if present) when running on that particular device.

I'm not sure how you are instantiating your decoder but often people use the mime type like so:

decoder = MediaCodec.createDecoderByType("video/avc");

The device will then choose the preferred decoder (probably hardware).

You can alternatively instantiate a specific decoder like so:

decoder = MediaCodec.createByCodecName("OMX.google.h264.decoder");

// OR

decoder = MediaCodec.createByCodecName("OMX.qcom.video.decoder.avc");

In my experience, most devices have at least 2 different H264 decoders available and you may find that an alternative decoder on this device performs without faults.

You can list all the available codecs using the following code:

static MediaCodecInfo[] getCodecs() {

        MediaCodecList mediaCodecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
        return mediaCodecList.getCodecInfos();
    } else {
        int numCodecs = MediaCodecList.getCodecCount();
        MediaCodecInfo[] mediaCodecInfo = new MediaCodecInfo[numCodecs];

        for (int i = 0; i < numCodecs; i++) {
            MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
            mediaCodecInfo[i] = codecInfo;

        return mediaCodecInfo;

Post Status

Asked in February 2016
Viewed 1,216 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life