Media Extractor: Decoder gives wrong Width on Android 4.2

1.1k views Asked by At

I'm writing a plugin for Unity that decodes and takes the frames from a video file using the Media Extractor and re-encodes to a new video file. However the frames are being decoded into an array of the wrong size (on android 4.2.2) because the codec thinks the height is 736 when it is actually 720.

for (int i = 0; i < numTracks; ++i) 
    {
        MediaFormat format = extractor.getTrackFormat(i);

        String mime = format .getString(MediaFormat.KEY_MIME);  

        if(mime.startsWith("video/"))
        {
            extractor.selectTrack(i);
            //Decoder
            decoder = MediaCodec.createDecoderByType(mime);
            decoder.configure(format, null, null, 0);

            break;
        }
     }

The output buffer index returns INFO_OUTPUT_BUFFERS_CHANGED and then INFO_OUTPUT_FORMAT_CHANGED. Logging this informs me that decoder thinks there is a height of 736 instead of the correct 720.

            decoder.queueInputBuffer(inputBufIndex, 0, sampleSize, extractor.getSampleTime(), 0);               
            //Get Outputbuffer Index
            int outIndex = decoder.dequeueOutputBuffer(info, 10000);

This works fine on a device running 4.4, the problem is only present on an older 4.2 device. Anyone have any thoughts?

1

There are 1 answers

0
mstorsjo On

Keep in mind that you need to check the crop fields in MediaFormat as well, the height field is the full height of the output buffer including potential padding. See e.g. the checkFrame function in https://android.googlesource.com/platform/cts/+/jb-mr2-release/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java - you'll get the actual content height as format.getInteger("crop-bottom") - format.getInteger("crop-top") + 1.