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


总结

把yuv数据编码成h264数据,使用了 硬件编码,同时把硬件编码绑定到编解码器上下文上.
* 同时创建了一个硬件编码帧. 绑定硬件编码上下文后.使用就和之前的编解码器上下文一样了.

流程图

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 
#include 
#include 

#include 
#include 
#include 

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;
    //创建另一个引用
    if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {

        fprintf(stderr, "Failed to create VAAPI frame context.\n");
        return -1;
    }
    //设置对应参数
    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 和 编码上下文联系起来,额外拷贝一个引用.之后在使用编解码器的时候,就直接使用硬件编解码器了
    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;
//数据送入编码器,取出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数据编码成h264数据,使用了 硬件编码,
 * 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    \n", argv[0]);
        return -1;
    }
//string转 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;
    }
//创建硬件驱动,初始化驱动上下文
    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;
    }
//设置编码器上下文的参数
    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 */
    //设置硬件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大小数据到帧的data[0]中
        if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0)
            break;
        //从文件中读取1 个 size/2大小数据到帧的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 绑定, 硬件帧和硬件上下文绑定.此时硬件帧里没数据
        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;
        }
             //把原始数据拷贝到硬件buffer中  从sw_frame到hw_frame ,此时硬件帧里有了数据
        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 */
    //刷新缓冲区数据
    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;
}

你可能感兴趣的:(ffmpeg_sample解读_vaapi_encode)