MediaCodec getInputImage return null on Some Devices
Asked Answered
W

1

24

I want to encode using MediaCodec by setting color format to COLOR_FormatYUV420Flexible. My Input buffer's is yuv420p.When I input buffer like this :

    int inputBufferIndex = mEncoder.dequeueInputBuffer(-1);
    mCurrentBufferIndex = inputBufferIndex;
    if (inputBufferIndex >= 0) {
        ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
        //if(VERBOSE)
            Log.i(TAG,"pos:"+inputBuffer.position()+"\tlimit:"+inputBuffer.limit());
        inputBuffer.clear();
        return inputBuffer;
    }

But some devices get wrong color. So I try this :

    int inputBufferIndex = mEncoder.dequeueInputBuffer(-1);
    mCurrentBufferIndex = inputBufferIndex;
    if (inputBufferIndex >= 0) {
        Image img = mEncoder.getInputImage(inputBufferIndex);
        if(img==null)
            return null;
        //mCurrentInputPlanes = img.getPlanes();
        ByteBuffer buffers[]={img.getPlanes()[0].getBuffer(),
                img.getPlanes()[1].getBuffer(),
                img.getPlanes()[2].getBuffer()};

I fill the buffer to YUV channels .It work on some devices. But moto X pro and huawei P7 get null when calling getInputImage. The documentation say the image doesn't contains raw data. But it also mentions COLOR_FormatYUV420Flexible is supported since API 21.So how should I fix this.

Welldefined answered 16/6, 2016 at 14:42 Comment(3)
I'm getting this on a lot of Galaxy Note3 and Galaxy Tab 4 10.1 devices, any further information from anyone?Shebashebang
@FTLRalph Could you post complete code example? From the question it is not clear how the YUV data is copied to the inputbuffer. Also, could you post example of the encoded video which shows the problem? Wrong color sounds like stride or padding issue to me, but need more info to debug.Franky
Did you check was supported COLOR_FormatYUV420Flexible? according to this link : #30858110Chapnick
T
1

getInputImage documentation says:

     * @return the input image, or null if the index is not a
     * dequeued input buffer, or not a ByteBuffer that contains a
     * raw image.

or not a ByteBuffer that contains a raw image. could mean that the image does not support the color format. Just because COLOR_FormatYUV420Flexible is available since 21, does not mean that all codec support this format.

If you absolutely have to use getInputImage, then maybe try:

  • COLOR_FormatYUV420Planar
  • COLOR_FormatYUV420SemiPlanar
  • a different codec that can handle COLOR_FormatYUV420Flexible
Thai answered 19/2, 2018 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.