如何理解MediaCodec的用户界面来提升编解码性能?

How to understand MediaCodec's user surface to improve codec performance?

mediacodec 的 official document 表示:

Data Types Codecs operate on three kinds of data: compressed data, raw audio data and raw video data. All three kinds of data can be processed using ByteBuffers, but you should use a Surface for raw video data to improve codec performance. Surface uses native video buffers without mapping or copying them to ByteBuffers; thus, it is much more efficient. You normally cannot access the raw video data when using a Surface, but you can use the ImageReader class to access unsecured decoded (raw) video frames. This may still be more efficient than using ByteBuffers, as some native buffers may be mapped into direct ByteBuffers. When using ByteBuffer mode, you can access raw video frames using the Image class and getInput/OutputImage(int).

这个怎么理解? 您应该为原始视频数据使用 Surface 以提高编解码器性能,什么是本机视频缓冲区?如何使用 Surface 提高编码或解码性能?

您可以使用编解码器的输入表面对视频帧进行编码,您可以使用 createInputSurface() 获得该表面然后(如果您不使用 NDK)您可以从 canvas表面并在其上绘制帧,或者您可以使用 NDK 并将帧数据复制到表面缓冲区,结果中的这两种方法都会为您提供编码的帧数据。

至于解码,您可以在 UI 中创建一些表面,然后使用解码器的 configure() it will allow the decoder to render a decoded frame into the surface so you won't need to copy decoded data from output buffers of decoder the only thing you should do is to pass true as "render" argument to releaseOutputBuffer() 将其传递给解码器。

当您使用表面时,您不必将字节缓冲区明确地提供给编码器。编码器将直接从表面抓取输入数据。下面是一些使用它的例子。 BigFlake BigFlake is the best site for starting with. EncodeAndMuxTest 希望这能帮助您理解这个概念。