DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

VFrame可以被解釋為一個AVPicture來填充 data 和 linesize 欄位。 填充這些欄位最簡單的方法是使用 avpicture_fill 函數。

要填充AVFrame和V 緩衝區的Y,它取決於輸入數據和想要使用框架( 要寫入 AVFrame ) 。 或者保留副本) 。

如果緩衝區足夠大( 至少為 linesize[0] * height 數據,則為), 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);



如果需要操作輸入數據,則需要計算所需的緩衝區大小,並在其中複製輸入數據。


//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 分配的任何緩衝區) 釋放它。

posted on 2021-07-23 11:49  DoubleLi  阅读(210)  评论(0编辑  收藏  举报