ffmpeg_sample解讀_vaapi_encode


title: ffmpeg_sample解讀_vaapi_encode
date: 2020-10-28 10:15:02
tags: [讀書筆記]
typora-copy-images-to: ./imgs
typora-root-url: ./imgs


總結(jié)

把yuv數(shù)據(jù)編碼成h264數(shù)據(jù),使用了 硬件編碼,同時把硬件編碼綁定到編解碼器上下文上.
* 同時創(chuàng)建了一個硬件編碼幀. 綁定硬件編碼上下文后.使用就和之前的編解碼器上下文一樣了.

流程圖

graph TB
fo[fopen]
-->ahcc[av_hwdevice_ctx_create]
-->afebn[avcodec_find_encoder_by_name]
-->aac[avcodec_alloc_context3]
-->shc[set_hwframe_ctx]
-->ahca[av_hwframe_ctx_alloc]
-->ahci[av_hwframe_ctx_init]
-->abr[av_buffer_ref]
-->aco[avcodec_open2]
-->afa[av_frame_alloc]
-->afgb[av_frame_get_buffer]
-->ahgb[av_hwframe_get_buffer]
-->ahtd[av_hwframe_transfer_data]
-->asf[avcodec_send_frame]
-->arp[avcodec_receive_packet]
-->fwite[fwrite]
-->release[release]
image-20201116175041786

代碼



/**
 * @file
 * Intel VAAPI-accelerated encoding example.
 *
 * @example vaapi_encode.c
 * This example shows how to do VAAPI-accelerated encoding. now only support NV12
 * raw file, usage like: vaapi_encode 1920 1080 input.yuv output.h264
 *
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <libavcodec/avcodec.h>
#include <libavutil/pixdesc.h>
#include <libavutil/hwcontext.h>

static int width, height;
static AVBufferRef *hw_device_ctx = NULL;

static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
{
    AVBufferRef *hw_frames_ref;
    AVHWFramesContext *frames_ctx = NULL;
    int err = 0;
    //創(chuàng)建另一個引用
    if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {

        fprintf(stderr, "Failed to create VAAPI frame context.\n");
        return -1;
    }
    //設(shè)置對應(yīng)參數(shù)
    frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
    frames_ctx->format    = AV_PIX_FMT_VAAPI;
    frames_ctx->sw_format = AV_PIX_FMT_NV12;
    frames_ctx->width     = width;
    frames_ctx->height    = height;
    frames_ctx->initial_pool_size = 20;
    //進行初始化
    if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) {
        fprintf(stderr, "Failed to initialize VAAPI frame context."
                "Error code: %s\n",av_err2str(err));
        av_buffer_unref(&hw_frames_ref);
        return err;
    }
    //把這個buffer 和 編碼上下文聯(lián)系起來,額外拷貝一個引用.之后在使用編解碼器的時候,就直接使用硬件編解碼器了
    ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
    if (!ctx->hw_frames_ctx)
        err = AVERROR(ENOMEM);

    av_buffer_unref(&hw_frames_ref);
    return err;
}

static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
{
    int ret = 0;
    AVPacket enc_pkt;

    av_init_packet(&enc_pkt);
    enc_pkt.data = NULL;
    enc_pkt.size = 0;
//數(shù)據(jù)送入編碼器,取出packet.寫入到文件中
    if ((ret = avcodec_send_frame(avctx, frame)) < 0) {
        fprintf(stderr, "Error code: %s\n", av_err2str(ret));
        goto end;
    }
    while (1) {
        ret = avcodec_receive_packet(avctx, &enc_pkt);
        if (ret)
            break;

        enc_pkt.stream_index = 0;
        ret = fwrite(enc_pkt.data, enc_pkt.size, 1, fout);
        av_packet_unref(&enc_pkt);
    }

end:
    ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);
    return ret;
}

/**
 * 把yuv數(shù)據(jù)編碼成h264數(shù)據(jù),使用了 硬件編碼,
 * vaapi_encode 1920 1080 input.yuv output.h264
 * @param argc
 * @param argv
 * @return
 */
int vaapi_encode_main(int argc, char *argv[])
{
    int size, err;
    FILE *fin = NULL, *fout = NULL;
    AVFrame *sw_frame = NULL, *hw_frame = NULL;
    AVCodecContext *avctx = NULL;
    AVCodec *codec = NULL;
    const char *enc_name = "h264_vaapi";

    if (argc < 5) {
        fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]);
        return -1;
    }
//string轉(zhuǎn) int
    width  = atoi(argv[1]);
    height = atoi(argv[2]);
    size   = width * height;
//讀取輸入文件
    if (!(fin = fopen(argv[3], "r"))) {
        fprintf(stderr, "Fail to open input file : %s\n", strerror(errno));
        return -1;
    }
    //打開輸出文件 w+ 在文件末尾寫入不管文件指針在那  b 二進制形式打開文件
    if (!(fout = fopen(argv[4], "w+b"))) {
        fprintf(stderr, "Fail to open output file : %s\n", strerror(errno));
        err = -1;
        goto close;
    }
//創(chuàng)建硬件驅(qū)動,初始化驅(qū)動上下文
    err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
                                 NULL, NULL, 0);
    if (err < 0) {
        fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(err));
        goto close;
    }
//找到編碼器
    if (!(codec = avcodec_find_encoder_by_name(enc_name))) {
        fprintf(stderr, "Could not find encoder.\n");
        err = -1;
        goto close;
    }
//分配編碼器上下文 空間
    if (!(avctx = avcodec_alloc_context3(codec))) {
        err = AVERROR(ENOMEM);
        goto close;
    }
//設(shè)置編碼器上下文的參數(shù)
    avctx->width     = width;
    avctx->height    = height;
    //時間基 和幀率
    avctx->time_base = (AVRational){1, 25};
    avctx->framerate = (AVRational){25, 1};//
    //樣板寬高比 寬/高
    avctx->sample_aspect_ratio = (AVRational){1, 1};
    avctx->pix_fmt   = AV_PIX_FMT_VAAPI;

    /* set hw_frames_ctx for encoder's AVCodecContext */
    //設(shè)置硬件buffer上下文
    if ((err = set_hwframe_ctx(avctx, hw_device_ctx)) < 0) {
        fprintf(stderr, "Failed to set hwframe context.\n");
        goto close;
    }
//打開編解碼上下文
    if ((err = avcodec_open2(avctx, codec, NULL)) < 0) {
        fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err));
        goto close;
    }

    while (1) {
        if (!(sw_frame = av_frame_alloc())) {
            err = AVERROR(ENOMEM);
            goto close;
        }
        /* read data into software frame, and transfer them into hw frame */
        sw_frame->width  = width;
        sw_frame->height = height;
        sw_frame->format = AV_PIX_FMT_NV12;
        //   填充幀的 AVFrame.data and AVFrame.buf arrays
        if ((err = av_frame_get_buffer(sw_frame, 0)) < 0)

            goto close;
        //size此時是圖片寬*高
        //從文件中讀取1個 size大小數(shù)據(jù)到幀的data[0]中
        if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0)
            break;
        //從文件中讀取1 個 size/2大小數(shù)據(jù)到幀的data[0]中
        if ((err = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0)
            break;

        if (!(hw_frame = av_frame_alloc())) {
            err = AVERROR(ENOMEM);
            goto close;
        }
        //把新分配的hw_frame 和hw_frames_ctx 綁定, 硬件幀和硬件上下文綁定.此時硬件幀里沒數(shù)據(jù)
        if ((err = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frame, 0)) < 0) {
            fprintf(stderr, "Error code: %s.\n", av_err2str(err));
            goto close;
        }
        if (!hw_frame->hw_frames_ctx) {
            err = AVERROR(ENOMEM);
            goto close;
        }
             //把原始數(shù)據(jù)拷貝到硬件buffer中  從sw_frame到hw_frame ,此時硬件幀里有了數(shù)據(jù)
        if ((err = av_hwframe_transfer_data(hw_frame, sw_frame, 0)) < 0) {
            fprintf(stderr, "Error while transferring frame data to surface."
                    "Error code: %s.\n", av_err2str(err));
            goto close;
        }
//編碼并寫出到文件,注意這里使用的是硬件幀
        if ((err = (encode_write(avctx, hw_frame, fout))) < 0) {
            fprintf(stderr, "Failed to encode.\n");
            goto close;
        }
        av_frame_free(&hw_frame);
        av_frame_free(&sw_frame);
    }

    /* flush encoder */
    //刷新緩沖區(qū)數(shù)據(jù)
    err = encode_write(avctx, NULL, fout);
    if (err == AVERROR_EOF)
        err = 0;

close:
    if (fin)
        fclose(fin);
    if (fout)
        fclose(fout);
    av_frame_free(&sw_frame);
    av_frame_free(&hw_frame);
    avcodec_free_context(&avctx);
    av_buffer_unref(&hw_device_ctx);

    return err;
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末所踊,一起剝皮案震驚了整個濱河市蠢络,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異瞳秽,居然都是意外死亡,警方通過查閱死者的電腦和手機率翅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門练俐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人冕臭,你說我怎么就攤上這事腺晾。” “怎么了辜贵?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵悯蝉,是天一觀的道長。 經(jīng)常有香客問我念颈,道長泉粉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮嗡靡,結(jié)果婚禮上跺撼,老公的妹妹穿的比我還像新娘。我一直安慰自己讨彼,他們只是感情好歉井,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著哈误,像睡著了一般哩至。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蜜自,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天菩貌,我揣著相機與錄音,去河邊找鬼重荠。 笑死箭阶,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的戈鲁。 我是一名探鬼主播仇参,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼婆殿!你這毒婦竟也來了诈乒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤婆芦,失蹤者是張志新(化名)和其女友劉穎怕磨,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體寞缝,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡癌压,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了荆陆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片滩届。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖被啼,靈堂內(nèi)的尸體忽然破棺而出帜消,到底是詐尸還是另有隱情,我是刑警寧澤浓体,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布泡挺,位于F島的核電站,受9級特大地震影響命浴,放射性物質(zhì)發(fā)生泄漏娄猫。R本人自食惡果不足惜贱除,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望媳溺。 院中可真熱鬧月幌,春花似錦、人聲如沸悬蔽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蝎困。三九已至录语,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間禾乘,已是汗流浹背澎埠。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留盖袭,地道東北人失暂。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像鳄虱,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子凭峡,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

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