MediaCodec getInputImage在某些设备上返回null

ene*_*ene 23 android encode mediacodec

我想通过设置颜色格式使用MediaCodec进行编码COLOR_FormatYUV420Flexible.我的输入缓冲区是yuv420p.当我像这样输入缓冲区时:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

但有些设备颜色错误.所以我试试这个:

    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()};
Run Code Online (Sandbox Code Playgroud)

我将缓冲区填充到YUV通道.它可以在某些设备上运行.但是当调用getInputImage时,moto X pro和huawei P7变为null.文档说图像不包含原始数据.但它也提到COLOR_FormatYUV420Flexible自API 21以来的支持.所以我应该如何解决这个问题.

Pne*_*nic 1

getInputImage文档说:

     * @return the input image, or null if the index is not a
     * dequeued input buffer, or not a ByteBuffer that contains a
     * raw image.
Run Code Online (Sandbox Code Playgroud)

或者不是包含原始图像的 ByteBuffer。可能意味着图像不支持颜色格式。仅仅因为COLOR_FormatYUV420Flexible自 21 起可用,并不意味着所有编解码器都支持此格式。

如果您绝对必须使用 getInputImage,那么也许可以尝试:

  • COLOR_FormatYUV420Planar
  • COLOR_FormatYUV420SemiPlanar
  • 可以处理的不同编解码器COLOR_FormatYUV420Flexible