yuv420P数据对AVFrame的赋值,
AVFrame *pict = new AVFrame;
FILE * fyuv = fopen("G:\\352x288.yuv","rb");
int nfilelen = ( width * height * 3 )/2;
BYTE * pbuf = new BYTE[nfilelen];
fread(pbuf,1,nfilelen,fyuv);
fclose(fyuv);
int size = width * height;
pict->data[0] = pbuf;
pict->data[1] = pict->data[0] + size;
pict->data[2] = pict->data[1] + size / 4;
pict->linesize[0] = width;
pict->linesize[1] = width / 2;
pict->linesize[2] = width / 2;
AVFrame * alloc_picture(enum PixelFormat pix_fmt, int width, int height)
{
AVFrame *picture;
uint8_t *picture_buf;
int size;
picture = avcodec_alloc_frame(); if (!picture){ return NULL;}
size = avpicture_get_size(pix_fmt, width, height);
picture_buf = (uint8_t *)av_malloc(size); if (!picture_buf) {av_free(picture); return NULL;}
avpicture_fill((AVPicture *)picture, picture_buf, pix_fmt, width, height);
return picture;
}
从AVFrame中取值yuv
上面看到yuv对avFrame的复制,如果取值呢、
如果: pict->linesize[0] = =width; 相等;
但是如果不相等,一般 pict->linesize[0] 大于 width 16 或者一些别的,以为为了内存效率会按照固定倍数给值;
所以,一般取值的方法:
那么:
unsingd char * pbufY = new unsignd char * [ pict->linesize[0] * pframe->height ];
memcpy( pbufY, pict->data[0] , pict->linesize[0] * pframe->height );
同理:
u 和 v 的计算一样的方法;
然后关于显示:
设置 yuv 播放器 显示宽度为 pict->linesize[0] , 而不是 width;
当然,这是你看到图像最右边可能会有绿色的书线条,有多余的宽度值决定;
如果需要完美过去图像,你可以每行数据按照 width copy 就可以了;