Chinaunix首页 | 论坛 | 博客
  • 博客访问: 13369
  • 博文数量: 3
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 12
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-15 20:07
文章分类
文章存档

2015年(3)

我的朋友
最近访客

分类: C/C++

2015-03-23 20:26:14

    video的raw data一般都是YUV420p的格式,简单的记录下这个格式的细节,如有不对希望大家能指出。
   YUV图像通常有两种格式,一种是packet 还有一种是planar
    从字面上就能理解packet的意思就是所有的yuv数据都是一股脑的放在一起,当然 内部的数据还是按照格式要求的,只是从外部来讲是一整个包包含了所有的yuv数据。最长见的YUV格式就是planar格式了。这个格式是讲yuv三个分量分别放在三个数组里。
   如下图是个420p的格式图:

   
 

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分量。

所以从上可以知道,一般一个视频如果是yuv420p的raw data, 则他的每帧图像的大小计算公式:width*height*3/2

   ffmpeg中是如何管理这个yuv的数据的呢?
    核心就是AVFrame这个结构体,成员data是个指针数组,每个成员所指向的就是yuv三个分量的实体数据了,成员linesize是指对应于每一行的大小,为什么需要这个变量,是因为在YUV格式和RGB格式时,每行的大小不一定等于图像的宽度。所以很容易想象yuv的布局格式。如下图
   
事实上绝大多数的情况都是这样布局的,所以可以看出 数据时从左往右填充,但是不一定能填满。
        好了,知道了这些就很容易解码保存yuv数据了,废话不多说直接上代码

点击(此处)折叠或打开

  1. const char* SRC_FILE = "1.mp4";

  2. int main()
  3. {
  4.     FILE *yuv_file = fopen("yuv_file","ab");
  5.     if (!yuv_file)
  6.         return 0;
  7.     av_register_all();
  8.     AVFormatContext* pFormat = NULL;
  9.     if (avformat_open_input(&pFormat, SRC_FILE, NULL, NULL) < 0)
  10.     {
  11.         return 0;
  12.     }
  13.     AVCodecContext* video_dec_ctx = NULL;
  14.     AVCodec* video_dec = NULL;
  15.     if (avformat_find_stream_info(pFormat, NULL) < 0)
  16.     {
  17.         return 0;
  18.     }
  19.     av_dump_format(pFormat,0,SRC_FILE,0);
  20.     video_dec_ctx = pFormat->streams[0]->codec;
  21.     video_dec = avcodec_find_decoder(video_dec_ctx->codec_id);
  22.     if (avcodec_open2(video_dec_ctx, video_dec, NULL) < 0)
  23.     {
  24.         return 0;
  25.     }
  26.     AVPacket *pkt = new AVPacket();
  27.     av_init_packet(pkt);
  28.     while (1)
  29.     {
  30.         if (av_read_frame(pFormat, pkt) < 0)
  31.         {
  32.             fclose(yuv_file);
  33.             delete pkt;
  34.             return 0;
  35.         }
  36.         if (pkt->stream_index == 0)
  37.         {
  38.             AVFrame *pFrame = avcodec_alloc_frame();
  39.             int got_picture = 0,ret = 0;
  40.             ret = avcodec_decode_video2(video_dec_ctx, pFrame, &got_picture, pkt);
  41.             if (ret < 0)
  42.             {
  43.                 delete pkt;
  44.                 return 0;
  45.             }
  46.             if (got_picture)
  47.             {
  48.                 char* buf = new char[video_dec_ctx->height * video_dec_ctx->width * 3 / 2];
  49.                 memset(buf, 0, video_dec_ctx->height * video_dec_ctx->width * 3 / 2);
  50.                 int height = video_dec_ctx->height;
  51.                 int width = video_dec_ctx->width;
  52.                 printf("decode video ok\n");
  53.                 int a = 0, i;
  54.                 for (i = 0; i<height; i++)
  55.                 {
  56.                     memcpy(buf + a, pFrame->data[0] + i * pFrame->linesize[0], width);
  57.                     a += width;
  58.                 }
  59.                 for (i = 0; i<height / 2; i++)
  60.                 {
  61.                     memcpy(buf + a, pFrame->data[1] + i * pFrame->linesize[1], width / 2);
  62.                     a += width / 2;
  63.                 }
  64.                 for (i = 0; i<height / 2; i++)
  65.                 {
  66.                     memcpy(buf + a, pFrame->data[2] + i * pFrame->linesize[2], width / 2);
  67.                     a += width / 2;
  68.                 }
  69.                 fwrite(buf, 1, video_dec_ctx->height * video_dec_ctx->width * 3 / 2, yuv_file);
  70.                 delete buf;
  71.                 buf = NULL;
  72.             }
  73.             avcodec_free_frame(&pFrame);
  74.             
  75.             
  76.         }
  77.     }

  78.     return 0;
  79. }




阅读(1570) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:从ffmpeg中提取出YUV数据

给主人留下些什么吧!~~