我想通过将颜色格式设置为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和华为P7在调用getInputImage时会为空。
文档说图像不包含原始数据。
但它也提到自API 21起就支持COLOR_FormatYUV420Flexible,因此我应该如何解决。

最佳答案

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的不同编解码器
  • 08-18 04:09