【问题标题】:MediaCodec getInputImage return null on Some DevicesMediaCodec getInputImage 在某些设备上返回 null
【发布时间】:2016-10-18 03:51:59
【问题描述】:

我想通过将颜色格式设置为COLOR_FormatYUV420Flexible 来使用 MediaCodec 进行编码。 我的输入缓冲区是 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;
    }

但有些设备的颜色不对。 所以我试试这个:

    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()};

我将缓冲区填充到 YUV 通道。它适用于某些设备。但是moto X pro和huawei P7在调用getInputImage时会得到null。 文档说图像不包含原始数据。 但它也提到自 API 21 起支持COLOR_FormatYUV420Flexible。那么我应该如何解决这个问题。

【问题讨论】:

  • 我在很多 Galaxy Note3 和 Galaxy Tab 4 10.1 设备上都遇到了这个问题,还有其他人提供的信息吗?
  • @FTLRalph 你能发布完整的代码示例吗?从问题中不清楚如何将 YUV 数据复制到输入缓冲区。另外,您能否发布显示问题的编码视频示例?对我来说,错误的颜色听起来像是步幅或填充问题,但需要更多信息才能调试。
  • 您检查是否支持 COLOR_FormatYUV420Flexible?根据此链接:stackoverflow.com/questions/30857610/…

标签: android encode android-mediacodec


【解决方案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.

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

如果你绝对必须使用 getInputImage,那么不妨试试:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 2013-01-10
    • 1970-01-01
    相关资源
    最近更新 更多