FFMPEG:在解码视频时,是否可以将结果生成到用户提供的缓冲区中?

cbe*_*bel 7 c++ directx video ffmpeg

在FFMPEG解码视频场景中,H264例如,通常我们分配一个AVFrame和解码的压缩数据,然后我们从构件的结果datalinesizeAVFrame。如下代码:

// input setting: data and size are a H264 data.
AVPacket avpkt;
av_init_packet(&avpkt);
avpkt.data = const_cast<uint8_t*>(data);
avpkt.size = size;

// decode video: H264 ---> YUV420
AVFrame *picture = avcodec_alloc_frame();
int len = avcodec_decode_video2(context, picture, &got_picture, &avpkt);
Run Code Online (Sandbox Code Playgroud)

我们可能会使用结果来做一些其他的任务,例如,使用 DirectX9 进行渲染。即准备缓冲区(DirectX9 Textures),并从解码结果中复制。

D3DLOCKED_RECT lrY;
D3DLOCKED_RECT lrU;
D3DLOCKED_RECT lrV;
textureY->LockRect(0, &lrY, NULL, 0);
textureU->LockRect(0, &lrU, NULL, 0);
textureV->LockRect(0, &lrV, NULL, 0);

// copy YUV420: picture->data ---> lr.pBits.
my_copy_image_function(picture->data[0], picture->linesize[0], lrY.pBits, lrY.Pitch, width, height);
my_copy_image_function(picture->data[1], picture->linesize[1], lrU.pBits, lrU.Pitch, width / 2, height / 2);
my_copy_image_function(picture->data[2], picture->linesize[2], lrV.pBits, lrV.Pitch, width / 2, height / 2);
Run Code Online (Sandbox Code Playgroud)

这个过程被认为发生了2次复制(ffmpeg复制结果到图片->数据,然后将图片->数据复制到DirectX9 Texture)。

我的问题是:是否可以将流程改进为只有1 个副本?另一方面,我们是否可以提供缓冲区(pBitsDirectX9 纹理的缓冲区)给 ffmpeg,并将函数结果解码到 DirectX9 纹理的缓冲区,而不是AVFrame?

cbe*_*bel 7

我找到了出路。

有一个公共成员AVCodecContextget_buffer2,这是一个回调函数。调用时avcodec_decode_video2会调用该回调函数,该回调函数负责将缓冲区和一些信息委托给AVFrame,然后avcodec_decode_video2将结果生成到 的缓冲区中AVFrame

回调函数get_buffer2被设置avcodec_default_get_buffer2为默认值。但是,我们可以将其覆盖为我们的 privided 函数。例如:

void our_buffer_default_free(void *opaque, uint8_t *data)
{
    // empty
}
int our_get_buffer(struct AVCodecContext *c, AVFrame *pic, int flags)
{
    assert(c->codec_type == AVMEDIA_TYPE_VIDEO);
    pic->data[0] = lrY.pBits;
    pic->data[1] = lrU.pBits;
    pic->data[2] = lrV.pBits;
    picture->linesize[0] = lrY.Pitch;
    picture->linesize[1] = lrU.Pitch;
    picture->linesize[2] = lrV.Pitch;
    pic->buf[0] = av_buffer_create(pic->data[0], pic->linesize[0] * pic->height, our_buffer_default_free, NULL, 0);
    pic->buf[1] = av_buffer_create(pic->data[1], pic->linesize[1] * pic->height / 2, our_buffer_default_free, NULL, 0);
    pic->buf[2] = av_buffer_create(pic->data[2], pic->linesize[2] * pic->height / 2, our_buffer_default_free, NULL, 0);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在解码之前,我们重写回调函数:

context->get_buffer2 = our_get_buffer;
Run Code Online (Sandbox Code Playgroud)

然后avcodec_decode_video2将结果生成到我们提供的缓冲区。

顺便说一句,对于经常在类中实现这些过程的C++程序,我们可以先记录这个指针:

context->opaque = this;
Run Code Online (Sandbox Code Playgroud)

并将覆盖的回调函数定义为静态成员:

static int myclass::my_get_buffer(struct AVCodecContext *c, AVFrame *pic, int flags)
{
    auto this_pointer = static_cast<decode_context*>(c->opaque);
    return this_pointer->my_get_buffer_real(c, pic, flags);
}
int myclass::my_get_buffer_real(struct AVCodecContext *c, AVFrame *pic, int flags)
{
    // ditto with above our_get_buffer.
    // ...
}
Run Code Online (Sandbox Code Playgroud)