多媒体格式之YUV420P
1.介绍
video的raw data一般都是YUV420p的格式,简单的记录下这个格式的细节,如有不对希望大家能指出。
YUV图像通常有两种格式,一种是packet 还有一种是planar
从字面上就能理解packet的意思就是所有的yuv数据都是一股脑的放在一起,当然 内部的数据还是按照格式要求的,只是从外部来讲是一整个包包含了所有的yuv数据,
最长见的YUV格式就是planar格式了。这个格式是讲yuv三个分量分别放在三个数组里。
如下图是个420p的格式图:
2.详解
YUV420格式是指,每个像素都保留一个Y(亮度)分量,而在水平方向上,不是每行都取U和V分量,而是一行只取U分量,则其接着一行就只取V分量,以此重复(即4:2:0, 4:0:2, 4:2:0, 4:0:2 …….),所以420不是指没有V,而是指一行采样只取U,另一行采样只取V。在取U和V时,每两个Y之间取一个U或V。但从4×4矩阵列来看,每4个矩阵点Y区域中,只有一个U和V,所以它们的比值是4:1。所以对于一个像素,RGB需要8 * 3 = 24位,即占3个字节;而YUV420P,8 + 8/4 + 8/4 = 12位,即占2个字节,其中8指Y分量,8/4指U和V分量。
3.在用ffmpeg中的avcodec_decode_video2解码后yuv数据
avc.yuv_file = fopen("/home/yang/video.yuv", "ab");
if (!avc.yuv_file)
return 0;
while (1)
{
if (av_read_frame(avc.fc, avc.packet) < 0)
{
printf("read frame err\n");
fclose(avc.yuv_file);
break;
}
打开文件后
int got = 0;
avcodec_decode_video2(avc.vcc, avc.video_frame, &got, avc.packet);
if (got <= 0)
{
printf("video got err %d\n", got);
return 1;
}
printf("video decodec ok,%d\n", got);
确认解码OK
if (got)
{
char* buf = malloc(avc.des_height * avc.des_width * 3 / 2);
memset(buf, 0, avc.des_height * avc.des_width * 3 / 2);
int height = avc.des_height;
int width = avc.des_width;
printf("decode video ok\n");
int a = 0, i;
for (i = 0; i < height; i++)
{
memcpy(buf + a, avc.video_frame->data[0] + i * avc.video_frame->linesize[0], width);
a += width;
}
for (i = 0; i < height / 2; i++)
{
memcpy(buf + a, avc.video_frame->data[1] + i * avc.video_frame->linesize[1], width / 2);
a += width / 2;
}
for (i = 0; i < height / 2; i++)
{
memcpy(buf + a, avc.video_frame->data[2] + i * avc.video_frame->linesize[2], width / 2);
a += width / 2;
}
fwrite(buf, 1, avc.des_height * avc.des_width * 3 / 2, avc.yuv_file);
free(buf);
buf = NULL;
}
存入文件OK了;