FFMPEG:将YUV数据转储到AVFrame结构中 [英] FFMPEG: Dumping YUV data into AVFrame structure

查看:305
本文介绍了FFMPEG:将YUV数据转储到AVFrame结构中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

http://ffmpeg.org/doxygen/trunk/structAVFrame.html ,我可以得出我需要将数据放入

的信息

data[AV_NUM_DATA_POINTERS] 

使用

linesize [AV_NUM_DATA_POINTERS].

我要转储的YUV数据是YUV420,图片大小是416x240.那么如何将yuv数据转储/映射到AVFrame结构变量?知道linesize代表了步幅,即我想知道图片的宽度,我尝试了一些组合但没有得到输出,请您帮我映射缓冲区.预先感谢.

解决方案

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

要填充AVFrame的YU和V缓冲区,这取决于您的输入数据以及对帧的处理方式(是否要写入AVFrame并擦除初始数据?还是保留副本). /p>

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

// 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);

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

// 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);

使用完AVFrame后,请不要忘记使用av_frame_free(以及av_malloc分配的任何缓冲区)释放它.

I'm trying to dump a YUV420 data into the AVFrame structure of FFMPEG. From the below link:

http://ffmpeg.org/doxygen/trunk/structAVFrame.html, i can derive that i need to put my data into

data[AV_NUM_DATA_POINTERS] 

using

linesize [AV_NUM_DATA_POINTERS].

The YUV data i'm trying to dump is YUV420 and the picture size is 416x240. So how do i dump/map this yuv data to AVFrame structures variable? Iknow that linesize represents the stride i.e. i suppose the width of my picture, I have tried with some combinations but do not get the output.I kindly request you to help me map the buffer. Thanks in advance.

解决方案

AVFrame can be interpreted as an AVPicture to fill the data and linesize fields. The easiest way to fill these field is to the use the avpicture_fill function.

To fill in the AVFrame's Y U and V buffers, it depends on your input data and what you want to do with the frame (do you want to write into the AVFrame and erase the initial data? or keep a copy).

If the buffer is large enough (at least linesize[0] * height for Y data, linesize[1 or 2] * height/2 for U/V data), you can directly use input buffers:

// 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);

If you want/need to manipulate a copy of input data, you need to compute the needed buffer size, and copy input data in it.

// 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);

Once you are done with the AVFrame, do not forget to free it with av_frame_free (and any buffer allocated by av_malloc).

这篇关于FFMPEG:将YUV数据转储到AVFrame结构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆