轉(zhuǎn)碼的四個(gè)案例

總結(jié)
1轴或,獲取一幀視頻所占內(nèi)存大小(byte):avpicture_get_size(outPix,witd,height);
2仿贬,獲取一個(gè)像素點(diǎn)所占內(nèi)存大小(bit):av_get_bits_per_pixel(av_pix_fmt_desc_get(src_pix));
      根據(jù)以上的數(shù)據(jù)計(jì)算一幀視頻所占的內(nèi)存大胁严:uint8_t *src_buffer = (uint8_t *)malloc(src_w * src_h * src_bpp / 8);等價(jià)于 uint8_t *src_buffer = (uint8_t *)malloc(src_w * src_h * 3 /2);
3龙屉,初始化AVFrame結(jié)構(gòu)體變量:
        給AVFrame結(jié)構(gòu)體的data妻坝、linesize分配內(nèi)存空間:avpicture_fill((AVPicture *)outFrame,outBuffer,outPix,witd,height);
        分配內(nèi)存空間并給data賦值:avpicture_fill((AVPicture *)inFrame, inbuffer, inPix, witd, height);
4市俊,不用AVFrame結(jié)構(gòu)體,自定義data透典、linesize
    uint8_t *src_data[4];//聲明長度為4的數(shù)組(yuv420p晴楔,數(shù)組src_data第一個(gè)元素存著y的數(shù)據(jù),第二個(gè)元素存著u的數(shù)據(jù)峭咒,第三個(gè)元素存著v的數(shù)據(jù))
    int src_linesize[4];//跟src_data對應(yīng)
    uint8_t *dst_data[4];
    int dst_linesiz[4];
    av_image_alloc(src_data,src_linesize,src_w,src_h,src_pix,1);//根據(jù)像素格式分配相應(yīng)的內(nèi)存大小
    av_image_alloc(dst_data,dst_linesiz,dst_w,dst_h,dst_pix,1);//根據(jù)像素格式分配相應(yīng)的內(nèi)存大小
    memcpy(src_data[0], src_buffer, src_w*src_h );//給src_data的第一個(gè)元素賦值y的數(shù)據(jù)
    memcpy(src_data[1], src_buffer + src_w*src_h, src_w*src_h / 4);//給src_data的第二個(gè)元素賦值u的數(shù)據(jù)
    memcpy(src_data[2], src_buffer + src_w*src_h + src_w*src_h/4, src_w*src_h/4);//給src_data的第三個(gè)元素賦值v的數(shù)據(jù)
5, SwsContext:提供一幀視頻數(shù)據(jù)像素格式轉(zhuǎn)換的上下文
      1税弃,初始化
              1.1 sws_getContext初始化
        SwsContext *sws_ctx = sws_getContext(
        src_w,src_h,src_pix,
        dst_w,dst_h,dst_pix,
        SWS_BICUBIC, NULL, NULL, NULL
        );
              1.2 復(fù)雜但是更靈活的初始化方法
        struct SwsContext *img_convert_ctx;
       img_convert_ctx =sws_alloc_context();
    //Show AVOption
    av_opt_show2(img_convert_ctx,stdout,AV_OPT_FLAG_VIDEO_PARAM,0);
    //Set Value
    av_opt_set_int(img_convert_ctx,"sws_flags",SWS_BICUBIC|SWS_PRINT_INFO,0);
    av_opt_set_int(img_convert_ctx,"srcw",src_w,0);
    av_opt_set_int(img_convert_ctx,"srch",src_h,0);
    av_opt_set_int(img_convert_ctx,"src_format",src_pixfmt,0);
    //'0' for MPEG (Y:0-235);'1' for JPEG (Y:0-255)
    av_opt_set_int(img_convert_ctx,"src_range",1,0);
    av_opt_set_int(img_convert_ctx,"dstw",dst_w,0);
    av_opt_set_int(img_convert_ctx,"dsth",dst_h,0);
    av_opt_set_int(img_convert_ctx,"dst_format",dst_pixfmt,0);
    av_opt_set_int(img_convert_ctx,"dst_range",1,0);
    sws_init_context(img_convert_ctx,NULL,NULL);
      2,一幀數(shù)據(jù)轉(zhuǎn)換函數(shù):sws_scale(根據(jù)提供的像素格式來轉(zhuǎn)化為相應(yīng)的數(shù)據(jù))
                sws_scale(sws_ctx,inFrame->data,inFrame->linesize,0,height,outFrame->data,outFrame->linesize);//借助AVFrame結(jié)構(gòu)體的變量來保存數(shù)據(jù)
                sws_scale(sws_ctx,src_data,src_linesize,0,src_h,dst_data,dst_linesiz);//自定義變量來保存數(shù)據(jù)
      3凑队,關(guān)閉上下文: sws_freeContext(sws_ctx);
6则果,像素格式:AVPixelFormat
      AV_PIX_FMT_YUV420P //平面類型
      AV_PIX_FMT_RGB24。//打包類型
7,圖像拉伸
      SWS_POINT:鄰域插值,1個(gè)點(diǎn)確定插值的點(diǎn)
      SWS_BILINEAR:雙線性插值,4個(gè)點(diǎn)確定插值的點(diǎn)
      SWS_BICUBIC:雙三次插值,16個(gè)點(diǎn)確定插值的點(diǎn)
8,yuv取值范圍
      Y的取值范圍是16-235漩氨,U西壮、V的取值范圍是16-240。FFmpeg中稱之為“mpeg”范圍叫惊。(0)
       以JPEG為代表的標(biāo)準(zhǔn)中款青,Y、U霍狰、V的取值范圍都是0-255抡草。FFmpeg中稱之為“jpeg” 范圍。(1)
       src_range
       dst_range

一蔗坯,swcale實(shí)現(xiàn)rgb24轉(zhuǎn)yuv420p

int main(int argc, char **argv){
    av_register_all();

    FILE *infile = fopen("sucai.bmp","rb+");
    int  witd = 400;
    int height = 400;
    uint8_t *inbuffer = (uint8_t *)malloc(3 * witd * height);
    printf("%x---%x", inbuffer, &inbuffer);
    fread(inbuffer, witd*height * 3, 1, infile);
    fclose(infile);

    enum AVPixelFormat outPix = AV_PIX_FMT_YUV420P, inPix = AV_PIX_FMT_BGR24;
    AVFrame *outFrame = av_frame_alloc();
    uint8_t *outBuffer = new uint8_t[avpicture_get_size(outPix,witd,height)];
    avpicture_fill((AVPicture *)outFrame,outBuffer,outPix,witd,height);

    AVFrame *inFrame = av_frame_alloc();
    avpicture_fill((AVPicture *)inFrame, inbuffer, inPix, witd, height);

    SwsContext *sws_ctx = sws_getContext(
        witd,height,inPix,
        witd,height,outPix,
        SWS_BILINEAR,NULL,NULL,NULL
        );
    sws_scale(sws_ctx,inFrame->data,inFrame->linesize,0,height,outFrame->data,outFrame->linesize);
    sws_freeContext(sws_ctx);

    FILE *outFile = fopen("output444.yuv","wb+");
    fwrite(outFrame->data[0],witd*height,1,outFile);
    fwrite(outFrame->data[1], witd*height / 4, 1, outFile);
    fwrite(outFrame->data[2], witd*height / 4, 1, outFile);
    fclose(outFile);
    return 0;
}

二康震,swcale實(shí)現(xiàn)YUV轉(zhuǎn)RGB

版本一
int main(int argc, char **argv){
    av_register_all();

    AVPixelFormat src_pix = AV_PIX_FMT_YUV420P, dst_pix = AV_PIX_FMT_RGB24;

    FILE *src_file = fopen("sintel_480x272_yuv420p.yuv","rb+");
    int src_w = 480, src_h = 272;
    uint8_t *src_buffer = (uint8_t *)malloc(src_w * src_h * 3 /2);
    

    FILE *dst_file = fopen("sintel_1280x720_rgb42-6.rgb","wb+");
    int dst_w = 1280, dst_h = 720;
    //uint8_t *dst_buffer = (uint8_t *)malloc(dst_w * dst_h * 3);
    uint8_t *dst_buffer = new uint8_t[avpicture_get_size(dst_pix, dst_w, dst_h)];//可以使用api提供的方法來設(shè)置dst_buffer所占內(nèi)存的大小

    AVFrame *src_frame = av_frame_alloc();
    AVFrame *dst_frame = av_frame_alloc();
    avpicture_fill((AVPicture *)src_frame, src_buffer, src_pix, src_w, src_h);
    avpicture_fill((AVPicture *)dst_frame, dst_buffer, dst_pix, dst_w, dst_h);

    SwsContext *sws_ctx = sws_getContext(
        src_w,src_h,src_pix,
        dst_w,dst_h,dst_pix,
        SWS_BICUBIC, NULL, NULL, NULL
        );
    int i = 0;
    while (1)
    {
        
        if (fread(src_buffer, 1, src_w*src_h * 3 / 2, src_file) != src_w*src_h * 3 / 2){
            break;
        }
        sws_scale(sws_ctx,src_frame->data,src_frame->linesize,0,src_h,dst_frame->data,dst_frame->linesize);
        printf("%d------%d\n", i, sizeof(src_buffer));
        fwrite(dst_frame->data[0], 1, dst_w * dst_h * 3, dst_file);
        i++;
    }
    sws_freeContext(sws_ctx);
    fclose(src_file);
    fclose(dst_file);
    return getchar();
}
版本二
int main(int argc, char **argv){
    av_register_all();

    AVPixelFormat src_pix = AV_PIX_FMT_YUV420P, dst_pix = AV_PIX_FMT_RGB24;

    FILE *src_file = fopen("sintel_480x272_yuv420p.yuv","rb+");
    int src_w = 480, src_h = 272;
    int src_bpp = av_get_bits_per_pixel(av_pix_fmt_desc_get(src_pix));
    uint8_t *src_buffer = (uint8_t *)malloc(src_w * src_h * src_bpp / 8);
    
    FILE *dst_file = fopen("sintel_1280x720_rgb42-9.rgb","wb+");
    int dst_w = 1280, dst_h = 720;
    int dst_bpp = av_get_bits_per_pixel(av_pix_fmt_desc_get(dst_pix));

    uint8_t *src_data[4];
    int src_linesize[4];
    uint8_t *dst_data[4];
    int dst_linesiz[4];
    av_image_alloc(src_data,src_linesize,src_w,src_h,src_pix,1);
    av_image_alloc(dst_data,dst_linesiz,dst_w,dst_h,dst_pix,1);
    SwsContext *sws_ctx = sws_getContext(
        src_w,src_h,src_pix,
        dst_w,dst_h,dst_pix,
        SWS_BICUBIC, NULL, NULL, NULL
        );
    int i = 0;
    while (1)
    {
        if (fread(src_buffer, 1, src_w*src_h * src_bpp / 8, src_file) != src_w*src_h * src_bpp / 8){
            break;
        }
        memcpy(src_data[0], src_buffer, src_w*src_h );
        memcpy(src_data[1], src_buffer + src_w*src_h, src_w*src_h / 4);
        memcpy(src_data[2], src_buffer + src_w*src_h + src_w*src_h/4, src_w*src_h/4);
        sws_scale(sws_ctx,src_data,src_linesize,0,src_h,dst_data,dst_linesiz);
        printf("%d------%d\n", i, sizeof(src_buffer));
        fwrite(dst_data[0], 1, dst_w * dst_h * dst_bpp / 8, dst_file);
        i++;
    }
    sws_freeContext(sws_ctx);
    fclose(src_file);
    fclose(dst_file);
    return getchar();
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市宾濒,隨后出現(xiàn)的幾起案子腿短,更是在濱河造成了極大的恐慌,老刑警劉巖绘梦,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件橘忱,死亡現(xiàn)場離奇詭異,居然都是意外死亡谚咬,警方通過查閱死者的電腦和手機(jī)鹦付,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進(jìn)店門尚粘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來择卦,“玉大人,你說我怎么就攤上這事”蹋” “怎么了祈噪?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長尚辑。 經(jīng)常有香客問我辑鲤,道長,這世上最難降的妖魔是什么杠茬? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任月褥,我火速辦了婚禮,結(jié)果婚禮上瓢喉,老公的妹妹穿的比我還像新娘宁赤。我一直安慰自己,他們只是感情好栓票,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布决左。 她就那樣靜靜地躺著,像睡著了一般走贪。 火紅的嫁衣襯著肌膚如雪佛猛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天坠狡,我揣著相機(jī)與錄音继找,去河邊找鬼。 笑死逃沿,一個(gè)胖子當(dāng)著我的面吹牛码荔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播感挥,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼缩搅,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了触幼?” 一聲冷哼從身側(cè)響起硼瓣,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎置谦,沒想到半個(gè)月后堂鲤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡媒峡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年瘟栖,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谅阿。...
    茶點(diǎn)故事閱讀 40,030評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡半哟,死狀恐怖酬滤,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情寓涨,我是刑警寧澤盯串,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站戒良,受9級(jí)特大地震影響体捏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜糯崎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一几缭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧沃呢,春花似錦奏司、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至黄锤,卻和暖如春搪缨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背鸵熟。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工副编, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人流强。 一個(gè)月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓痹届,卻偏偏與公主長得像,于是被迫代替她去往敵國和親打月。 傳聞我的和親對象是個(gè)殘疾皇子队腐,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容