Video encoding process YUV data is encoded into H264 data

1. Video encoding process

Insert image description here

2. Practical demo

#ifndef MAINBACK_C
#define MAINBACK_C
#endif // MAINBACK_C
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <libavcodec/avcodec.h>
#include <libavutil/common.h>
#include <libavutil/frame.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <time.h>
int encode(AVCodecContext* codec_ctx,AVFrame* frame,AVPacket* pkt,FILE* out)
{
    
    
    int ret=0;
    ret = avcodec_send_frame(codec_ctx,frame);
    printf("send data\n");
    if(ret<0){
    
    
        printf("avcodec_send_frame failed\n");
        return ret;
    }

    while(1)
    {
    
    
        ret=avcodec_receive_packet(codec_ctx,pkt);
        if(ret==AVERROR(EAGAIN)||ret==AVERROR(AVERROR_EOF))
            break;
        if(ret<0)return ret;
        printf("write data\n");
        fwrite(pkt->data,1,pkt->size,out);

    }

}
//编码流程  先初始化编码器,读取原始文件,send,recvice,写入
int main(int argc,char* argv[])
{
    
    
    if(argv<3)
    {
    
    
        printf("argv<3\n");
        return -1;
    }
    char* yuv_path=argv[1];
    char* out_path=argv[2];
    FILE* yuv_file=fopen(yuv_path,"rb");
    FILE* out_file=fopen(out_path,"wb");
    AVCodec* codec=avcodec_find_encoder(AV_CODEC_ID_H264);
    AVCodecContext* codec_ctx=avcodec_alloc_context3(codec);

    codec_ctx->bit_rate=1024*512;
    codec_ctx->width=768;
    codec_ctx->height=320;
    codec_ctx->pix_fmt=AV_PIX_FMT_YUV420P;
    codec_ctx->max_b_frames=0;
    codec_ctx->gop_size=25;
    codec_ctx->framerate=(AVRational){
    
    25,1};
    codec_ctx->time_base=(AVRational){
    
    1,25};
    av_opt_set(codec_ctx->priv_data,"preset","veryslow",0);
    av_opt_set(codec_ctx->priv_data,"profile","main",0);
    av_opt_set(codec_ctx->priv_data,"tune","zerolatency",0);
    avcodec_open2(codec_ctx,codec,NULL);
    AVFrame* frame=av_frame_alloc();
    AVPacket* pkt=av_packet_alloc();
    frame->format=codec_ctx->pix_fmt;
    frame->width=codec_ctx->width;
    frame->height=codec_ctx->height;

    av_frame_get_buffer(frame,0);//分配frame的buff
    int frame_bytes=av_image_get_buffer_size(frame->format,frame->width,frame->height,1);
    uint8_t* yuv_buff=av_malloc(frame_bytes);
    memset(yuv_buff,0,frame_bytes);
    int pts=0;
    int ret=0;
    uint64_t begin=time(0);
    while(1)
    {
    
    
        int rsize=fread(yuv_buff,1,frame_bytes,yuv_file);
        ret = av_frame_make_writable(frame);
        if(ret != 0)
            printf("av_frame_make_writable failed, ret = %d\n", ret);

        int need_size=av_image_fill_arrays(frame->data,frame->linesize,yuv_buff,frame->format,frame->width,frame->height,1);
        if(rsize!=need_size)break;
        frame->pts=pts;
        pts+=1;

        encode(codec_ctx,frame,pkt,out_file);
    }
    encode(codec_ctx,NULL,pkt,out_file);
    printf("time:%lld",time(0)-begin);
    return 0;
}

3. Explanation of relevant coding knowledge points

1. Parameter setting issues:

When encoding video, set the encoder context parameters: code rate, width, height, format, frame rate, etc.
Frame settings: width, height, format.
Then calculate the data of a frame: width * height * the number of bytes occupied by the format.
Usually use av_image_get_buffer_size to calculate

2. About av_opt_set

av_opt_set is used to set parameters

  • preset
    The preset parameter is a parameter that weighs the encoding speed and compression rate. The slower the encoding speed, the higher the compression rate.
    Parameters:
    ultrafast
    superfast
    veryfast
    faster
    fast
    medium – default preset The default is medium
    slow
    slower
    veryslow
  • tune
    The tune parameter is used for visual emphasis, that is to say, what should be compressed during compression and what should be retained...
    Parameters:
    film: movie type, use this option when the quality of the video is very strict
    animation: animation, compressed video Use this option when it is an animation.
    grain: The particles are very heavy. This option is suitable for videos with heavy graininess.
    stillimage: Still images. This option is mainly used for videos with more static images.
    psnr: Improve psnr. The The psnr ratio of the video encoded by this option is higher
    ssim: improve ssim, the video ssim encoded by this option is higher
    fastdecode: fast decoding, this option is beneficial to fast decoding
    zerolatency: zero delay, this option is mainly used for video live broadcast
  • profile
  1. baseline profile: basic image quality. Supports I/P frames, only supports Progressive and CAVLC;
  2. extended profile: advanced image quality. Supports I/P/B/SP/SI frames, only supports Progressive and CAVLC;
  3. main profile: mainstream image quality. Provides I/P/B frames, supports Progressive and Interlaced, and also supports CAVLC and CABAC;
  4. high profile: high-level image quality. Based on the main Profile, 8x8 internal prediction, custom quantization, lossless video encoding and more YUV formats are added;

3. About code stream settings

Insert image description here

Guess you like

Origin blog.csdn.net/m0_60565784/article/details/131614914