1. 程式人生 > >ffmpeg最簡單的解碼保存YUV數據 <轉>

ffmpeg最簡單的解碼保存YUV數據 <轉>

context 陣列 log fop content const www. += fopen

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; }

http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=24922718&id=4584541

ffmpeg最簡單的解碼保存YUV數據 <轉>