FFMPEG:将YUV数据转储为AVFrame结构

Zax*_*Zax 6 ffmpeg yuv

我正在尝试将YUV420数据转储到AVFrameFFMPEG 的结构中.从以下链接:

http://ffmpeg.org/doxygen/trunk/structAVFrame.html,我可以推导出我需要将我的数据输入

data[AV_NUM_DATA_POINTERS] 
Run Code Online (Sandbox Code Playgroud)

运用

linesize [AV_NUM_DATA_POINTERS].
Run Code Online (Sandbox Code Playgroud)

我试图转储的YUV数据是YUV420,图片大小是416x240.如此我如何将这个yuv数据转储/映射到AVFrame结构变量?我知道lineize表示步幅,即我想我的图片的宽度,我尝试过一些组合,但没有得到输出.我请求你帮我映射缓冲区.提前致谢.

小智 21

AVFrame可以解释为AVPicture来填充datalinesize字段.填充这些字段的最简单方法是使用avpicture_fill函数.

要填充AVFrame的YU和V缓冲区,它取决于您的输入数据以及您想要对帧执行的操作(是否要写入AVFrame并删除初始数据?或保留副本).

如果缓冲区足够大(至少linesize[0] * height对于Y数据,linesize[1 or 2] * height/2对于U/V数据),您可以直接使用输入缓冲区:

// Initialize the AVFrame
AVFrame* frame = avcodec_alloc_frame();
frame->width = width;
frame->height = height;
frame->format = AV_PIX_FMT_YUV420P;

// Initialize frame->linesize
avpicture_fill((AVPicture*)frame, NULL, frame->format, frame->width, frame->height);

// Set frame->data pointers manually
frame->data[0] = inputBufferY;
frame->data[1] = inputBufferU;
frame->data[2] = inputBufferV;

// Or if your Y, U, V buffers are contiguous and have the correct size, simply use:
// avpicture_fill((AVPicture*)frame, inputBufferYUV, frame->format, frame->width, frame->height);
Run Code Online (Sandbox Code Playgroud)

如果您需要/需要操作输入数据的副本,则需要计算所需的缓冲区大小,并在其中复制输入数据.

// Initialize the AVFrame
AVFrame* frame = avcodec_alloc_frame();
frame->width = width;
frame->height = height;
frame->format = AV_PIX_FMT_YUV420P;

// Allocate a buffer large enough for all data
int size = avpicture_get_size(frame->format, frame->width, frame->height);
uint8_t* buffer = (uint8_t*)av_malloc(size);

// Initialize frame->linesize and frame->data pointers
avpicture_fill((AVPicture*)frame, buffer, frame->format, frame->width, frame->height);

// Copy data from the 3 input buffers
memcpy(frame->data[0], inputBufferY, frame->linesize[0] * frame->height);
memcpy(frame->data[1], inputBufferU, frame->linesize[1] * frame->height / 2);
memcpy(frame->data[2], inputBufferV, frame->linesize[2] * frame->height / 2);
Run Code Online (Sandbox Code Playgroud)

完成AVFrame后,不要忘记将其释放av_frame_free(以及分配的任何缓冲区av_malloc).