1

我在这里关注 FFmpeg 视频编码示例,但它制作了一些虚拟 YUV420P 帧,并且我已经从相机捕获了 BGR 图像。

我不知道如何使用frame->data[]frame->linesize[]用我的 BGR 图像填充它们,所以我可以编码 H264 视频。


编辑:

在罗纳德的回答之后,我有以下代码(相机发送的每张新照片都会调用它):

    .............
    AVFrame *bgrFrame = av_frame_alloc();

    bgrFrame->width = originalBGRImage.cols;
    bgrFrame->height = originalBGRImage.rows;

    ret = av_image_alloc(bgrFrame->data, bgrFrame->linesize, bgrFrame->width, bgrFrame->height, AV_PIX_FMT_BGR24, 32);

    /////////////////////////////////////
    // This works and prevents memory leak....if I remove it, it consumes all the RAM....but I can't free this memory here, since I will use it later...
    av_freep(&bgrFrame->data[0]);
    av_frame_free(&bgrFrame);
    return;
    /////////////////////////////////////

    ret = av_image_fill_pointers(bgrFrame->data, AV_PIX_FMT_BGR24, bgrFrame->height, originalBGRImage.data, bgrFrame->linesize);

    /////////////////////////////////////
    // Here is where I am done using the memory so I will want to free it...but this same code crashes the program.
    av_freep(&bgrFrame->data[0]);
    av_frame_free(&bgrFrame);
    return;
    /////////////////////////////////////

因此,如果我删除了av_freep(&bgrFrame->data[0]);代码末尾的

4

1 回答 1

1

使用av_image_fill_linesizes()填充线条大小(除非它们被填充,在这种情况下您应该手动指定它们),然后使用av_image_fill_pointers()填充数据指针。作为 pix_fmt,使用AV_PIX_FMT_BGR24

现在,这给了你一张 BGR24 图片。您可以使用libx264rgb编码器将其编码为 RGB H264 图像。如果要编码 YUV H264,首先需要将 RGB 图像转换为 YUV,这可以使用 libswscale 完成。谷歌“libswscale 示例”以查找有关如何执行此操作的示例代码。

于 2016-02-02T12:30:28.613 回答