使用 FFmpeg 编码时处理原始数据的正确方法

Correct way for working with raw data when encoding using FFmpeg

我正在通过 ffmpeg 为 encoding/decoding 原始数据开发 android 库。我发现的每个示例都使用文件,它要么读取文件,要么写入文件。但是,我使用代表 RGBA 图像的原始字节数组作为编码器输入,使用字节数组作为编码器输出。让我们关注这个问题的编码部分。

我的函数如下所示:

int encodeRGBA(uint8_t *image, int imageSize, int presentationTimestamp,
 uint8_t *result, int resultSize)

其中 image 是包含原始 rgba 图像数据的字节数组,imageSize 是该数组的长度,presentationTimestamp 只是 AVFrame 用于设置 ptsresult 是预先分配的字节数组,具有一些定义的长度(当前大小匹配宽度 x 高度),resultSize 是字节数组长度(宽度 x 高度)。返回的 int 值表示实际使用的预分配数组的长度。我知道这不是将数据发送回 java 的最佳方法,这也是问题的一部分。有没有更好的返回结果的方法?

找到示例 here for encoding, directly writes byte data to the frame->data[0] (different approach for different formats, RGBA or YUV). But google search for "ffmpeg read from memory" results in examples like this, this or this。他们都建议使用 AVIOContext

我很困惑如何使用 AVFormatContext 和 AVCodecContext 进行编码?

目前我的编码器使用第一种方法工作,并且我成功地返回了所描述的结果(使用预分配的字节数组)。我想知道这是否是错误的做法?我应该使用 AVIOContext 来处理字节数组吗?

Should I be using AVIOContext for handling byte arrays?

没有。 AVIOContext 如果用于处理内存中的文件和/或容器。在这种情况下,需要 avformat 才能从字节数组中读取编码帧。您直接使用原始帧,不需要使用 avformat。