ffmpeg最简单的解码保存YUV数据

来源:互联网 发布:淘宝股东是日本人 编辑:程序博客网 时间:2024/06/06 18:09

文章转载自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&id=4584541&uid=24922718

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数据了,废话不多说直接上代码

const char* SRC_FILE = "1.mp4";int main(){    FILE *yuv_file = fopen("yuv_file","ab");    if (!yuv_file)        return 0;    av_register_all();    AVFormatContext* pFormat = NULL;    if (avformat_open_input(&pFormat, SRC_FILE, NULL, NULL) < 0)    {        return 0;    }    AVCodecContext* video_dec_ctx = NULL;    AVCodec* video_dec = NULL;    if (avformat_find_stream_info(pFormat, NULL) < 0)    {        return 0;    }    av_dump_format(pFormat,0,SRC_FILE,0);    video_dec_ctx = pFormat->streams[0]->codec;    video_dec = avcodec_find_decoder(video_dec_ctx->codec_id);    if (avcodec_open2(video_dec_ctx, video_dec, NULL) < 0)    {        return 0;    }    AVPacket *pkt = new AVPacket();    av_init_packet(pkt);    while (1)    {        if (av_read_frame(pFormat, pkt) < 0)        {            fclose(yuv_file);            delete pkt;            return 0;        }        if (pkt->stream_index == 0)        {            AVFrame *pFrame = avcodec_alloc_frame();            int got_picture = 0,ret = 0;            ret = avcodec_decode_video2(video_dec_ctx, pFrame, &got_picture, pkt);            if (ret < 0)            {                delete pkt;                return 0;            }            if (got_picture)            {                char* buf = new char[video_dec_ctx->height * video_dec_ctx->width * 3 / 2];                memset(buf, 0, video_dec_ctx->height * video_dec_ctx->width * 3 / 2);                int height = video_dec_ctx->height;                int width = video_dec_ctx->width;                printf("decode video ok\n");                int a = 0, i;                for (i = 0; i<height; i++)                {                    memcpy(buf + a, pFrame->data[0] + i * pFrame->linesize[0], width);                    a += width;                }                for (i = 0; i<height / 2; i++)                {                    memcpy(buf + a, pFrame->data[1] + i * pFrame->linesize[1], width / 2);                    a += width / 2;                }                for (i = 0; i<height / 2; i++)                {                    memcpy(buf + a, pFrame->data[2] + i * pFrame->linesize[2], width / 2);                    a += width / 2;                }                fwrite(buf, 1, video_dec_ctx->height * video_dec_ctx->width * 3 / 2, yuv_file);                delete buf;                buf = NULL;            }            avcodec_free_frame(&pFrame);                                }    }    return 0;}


原创粉丝点击