Re-encoding h.264 content with a different bit rate using Android MediaCodec

后端 未结 1 1822
陌清茗
陌清茗 2021-01-01 04:12

I\'m attempting to re-encode a h.264 mp4 file with a different bit rate using the Android MediaCodec API\'s introduced in 4.2.

I am able decode and play the content

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 04:51

    In your program, there is a design issue. In the loop while (!sawOutputEOS) { where you are trying to re-encode,

    int inputBufIndex = encoder.dequeueInputBuffer(kTimeOutUs);
    

    Dequeues one buffer from the input / input port of the encoder

     ByteBuffer dstBuf = encoderInputBuffers[inputBufIndex];
    

    Buffer pointer to the dequeued buffer

    int sampleSize = extractor.readSampleData(dstBuf, 0 /* offset */);
    

    The dequeued buffer is filled with the data from extractor. The output of an extractor is a compressed bitstream. This is not a YUV uncompressed frame.

    encoder.queueInputBuffer(inputBufIndex,....)
    

    Here, the compressed bitstream is encoded. Since this is not a YUV frame, the encoder will try to perform a compression to the best of it's ability and hence, you observe a green illegible frame at the output of the encoder. I presume you are observing this on the screen due to the following portion of code where you decode the same content. This would have been observed even if the encoder's output was written into a file and played through a different player or on PC.

    From the program, I presume your intended design is Decode ==> Encode ==> Live Decode for which your graph should be

    MediaExtractor ==> MediaCodec (Decoder) ==> MediaCodec (Encoder) ==> MediaCodec (Decoder) ==> Display
    

    P.S: Did you observe any memory violations when you run this program?

    0 讨论(0)
提交回复
热议问题