如何预先分配LibavCodec的内存来编写解码的帧数据

How do I pre-allocate the memory for libavcodec to write decoded frame data?

本文关键字:解码 数据 内存 何预先 分配 LibavCodec      更新时间:2023-10-16

我试图通过遵循演示代码来解码视频:shere

我需要能够控制pFrame->data[0]中的帧数据存储位置。我尝试将pFrame->data设置为自己的缓冲区,如下所示:

// Determine required buffer size and allocate buffer 
int numBytes = av_image_get_buffer_size(pixFmt, width, height, 1); 
(uint8_t*) dataBuff = (uint8_t*) malloc (numBytes * sizeof(uint8_t)); 
// Assign buffer to image planes in pFrame
av_image_fill_arrays(frame->data, frame->linesize, dataBuff, pixFmt, width,
height, 1);

虽然这确实将pFrame->data设置为dataBuff(如果我打印其地址,则它们是相同的),但此调用ret = avcodec_receive_frame(pCodecContext, pFrame)接收解码的数据始终将数据写入其他地址。它似乎可以在基础API中的某个地方管理自己的内存,而忽略了我之前分配给pFramedataBuff

所以我被困了 - 我可以告诉libav将解码的帧数据写入我预先分配的内存吗?我已经看到人们在网上和Libav论坛上都提出类似的问题,但没有找到答案。

非常感谢〜

我发现正确的方法是通过回调函数 get_buffer2创建自己的自定义分配器,因为此答案证明:

ffmpeg:在解码视频时,可以为用户提供的缓冲区生成结果?

进一步的文档在这里!