ffmpeg_sample interpretation_vaapi_encode

Encode yuv data into h264 data, use hardware encoding, and bind the hardware encoding to the codec context. * At the same time, a hardware encoding frame is created. After binding the hardware encoding context, the use is the same as the previous codec The context is the same.

ffmpeg_sample interpretation_vaapi_encode – Nuggets

/**
 * @file
 * Intel VAAPI-accelerated encoding example.
 *
 * @example vaapi_encode.c
 * This example shows how to do VAAPI-accelerated encoding. now only supports 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;
    //Create another reference
    if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {

        fprintf(stderr, "Failed to create VAAPI frame context.\
");
        return -1;
    }
    //Set corresponding parameters
    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;
    //Initialize
    if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) {
        fprintf(stderr, "Failed to initialize VAAPI frame context."
                "Error code: %s\
",av_err2str(err));
        av_buffer_unref( & amp;hw_frames_ref);
        return err;
    }
    //Associate this buffer with the encoding context and copy an additional reference. When using the codec later, use the hardware codec directly.
    ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
    if (!ctx->hw_frames_ctx)
        err = AVERROR(ENOMEM);

    av_buffer_unref( & amp;hw_frames_ref);
    return err;
}

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

    av_init_packet( & amp;enc_pkt);
    enc_pkt.data = NULL;
    enc_pkt.size = 0;
//The data is sent to the encoder, the packet is taken out and written to the file.
    if ((ret = avcodec_send_frame(avctx, frame)) < 0) {
        fprintf(stderr, "Error code: %s\
", av_err2str(ret));
        goto end;
    }
    while (1) {
        ret = avcodec_receive_packet(avctx, & amp;enc_pkt);
        if (ret)
            break;

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

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

/**
 * Encode yuv data into h264 data, using hardware encoding,
 * 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>\
", argv[0]);
        return -1;
    }
//string to int
    width = atoi(argv[1]);
    height = atoi(argv[2]);
    size = width * height;
//Read input file
    if (!(fin = fopen(argv[3], "r"))) {
        fprintf(stderr, "Fail to open input file : %s\
", strerror(errno));
        return -1;
    }
    //Open the output file w + Write at the end of the file regardless of where the file pointer is b Open the file in binary form
    if (!(fout = fopen(argv[4], "w + b"))) {
        fprintf(stderr, "Fail to open output file : %s\
", strerror(errno));
        err = -1;
        goto close;
    }
//Create hardware driver and initialize driver context
    err = av_hwdevice_ctx_create( & amp;hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
                                 NULL, NULL, 0);
    if (err < 0) {
        fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\
", av_err2str(err));
        goto close;
    }
//find the encoder
    if (!(codec = avcodec_find_encoder_by_name(enc_name))) {
        fprintf(stderr, "Could not find encoder.\
");
        err = -1;
        goto close;
    }
//Allocate encoder context space
    if (!(avctx = avcodec_alloc_context3(codec))) {
        err = AVERROR(ENOMEM);
        goto close;
    }
//Set the parameters of the encoder context
    avctx->width = width;
    avctx->height = height;
    //Time base and frame rate
    avctx->time_base = (AVRational){1, 25};
    avctx->framerate = (AVRational){25, 1};//
    //template aspect ratio width/height
    avctx->sample_aspect_ratio = (AVRational){1, 1};
    avctx->pix_fmt = AV_PIX_FMT_VAAPI;

    /* set hw_frames_ctx for encoder's AVCodecContext */
    //Set hardware buffer context
    if ((err = set_hwframe_ctx(avctx, hw_device_ctx)) < 0) {
        fprintf(stderr, "Failed to set hwframe context.\
");
        goto close;
    }
//Open the encoding and decoding context
    if ((err = avcodec_open2(avctx, codec, NULL)) < 0) {
        fprintf(stderr, "Cannot open video encoder codec. Error code: %s\
", 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;
        // Fill the AVFrame.data and AVFrame.buf arrays of the frame
        if ((err = av_frame_get_buffer(sw_frame, 0)) < 0)

            goto close;
        //size is the image width*height at this time
        //Read 1 size data from the file into data[0] of the frame
        if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0)
            break;
        //Read 1 size/2 size data from the file into data[0] of the frame
        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;
        }
        //Bind the newly allocated hw_frame to hw_frames_ctx, and bind the hardware frame to the hardware context. There is no data in the hardware frame at this time
        if ((err = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frame, 0)) < 0) {
            fprintf(stderr, "Error code: %s.\
", av_err2str(err));
            goto close;
        }
        if (!hw_frame->hw_frames_ctx) {
            err = AVERROR(ENOMEM);
            goto close;
        }
             //Copy the original data to the hardware buffer from sw_frame to hw_frame. At this time, there is data in the hardware 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.\
", av_err2str(err));
            goto close;
        }
//Encode and write out to file. Note that hardware frames are used here.
        if ((err = (encode_write(avctx, hw_frame, fout))) < 0) {
            fprintf(stderr, "Failed to encode.\
");
            goto close;
        }
        av_frame_free( & amp;hw_frame);
        av_frame_free( & amp;sw_frame);
    }

    /* flush encoder */
    //Refresh buffer data
    err = encode_write(avctx, NULL, fout);
    if (err == AVERROR_EOF)
        err = 0;

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

    return err;
}


Author: wonderful world
Link: https://juejin.cn/post/6895657345419608078
Source: Rare Earth Nuggets
Copyright belongs to the author. For commercial reprinting, please contact the author for authorization. For non-commercial reprinting, please indicate the source.