Simplest FFmpeg-based encoder-pure version (not including Libavformat)

Source: Internet
Author: User

=====================================================

The simplest ffmpeg-based video encoder article list:

The simplest video encoder based on FFmpeg (YUV encoded as H.

Simplest ffmpeg-based video encoder-Updated (YUV encoded as HEVC (h.265))

Simplest FFmpeg-based encoder-pure version (not including Libavformat)

=====================================================


This document records a more "pure" ffmpeg-based video encoder. A video encoder based on FFmpeg has been recorded previously:

The simplest ffmpeg-based video encoder-Updated (YUV encoded as HEVC (h.265))

This video encoder calls the FFmpeg in the Libavformat and libavcodec two libraries to complete the video coding work. But this is not a "pure" encoder. The two libraries above Libavformat complete the encapsulation format processing, while LIBAVCODEC completes the coding work. A "pure" encoder, in theory, only need to use libavcodec is enough, do not need to use Libavformat. The encoder recorded in this paper is such a "pure" encoder, it only by calling Libavcodec to encode YUV data into the format of H.264/HEVC and other compressed video stream.


Flow chart

The process of encoding video using only Libavcodec (not using Libavformat) is shown.


The functions of the key functions in the flowchart are listed as follows:

Avcodec_register_all (): Registers all codecs.
Avcodec_find_encoder (): Find the encoder.
AVCODEC_ALLOC_CONTEXT3 (): Allocates memory for Avcodeccontext.
Avcodec_open2 (): Open the encoder.
Avcodec_encode_video2 (): Encodes a frame of data.

The two structures that store data are listed below:
Avframe: Stores one frame of non-encoded pixel data.
Avpacket: Stores one frame of compressed encoded data.

Contrast

Simply record the difference between this "pure version" video encoder using LIBAVCODEC and the video encoder using the Libavcodec+libavformat.

PS: Using Libavcodec+libavformat Encoder reference article "simplest ffmpeg-based video encoder-Updated version (YUV code for HEVC (h.265))"

(1)The following functions related to Libavformat do not exist in the "pure version" video encoder.
Av_register_all (): Register all codecs, multiplexing/demultiplexer components, etc. where Avcodec_register_all () is called to register all codec-related components.
Avformat_alloc_context (): Creates the Avformatcontext struct body.
AVFORMAT_ALLOC_OUTPUT_CONTEXT2 (): Initializes an output stream.
Avio_open (): Open the output file.
Avformat_new_stream (): Creates the Avstream struct body. Avformat_new_stream () is called AVCODEC_ALLOC_CONTEXT3 () to create the Avcodeccontext struct.
Avformat_write_header (): Writes the file header.
Av_write_frame (): Writes the encoded file frame.
Av_write_trailer (): Write the end of the file.
(2)Several new functions are added.
Avcodec_register_all (): Registers only the components of the codec.

AVCODEC_ALLOC_CONTEXT3 (): Creates the Avcodeccontext struct body.

As you can see, this pure encoder function call is simpler, less functional, and relatively more "lightweight" than the "complete" encoder.


Source

/** * Simplest FFmpeg based video encoder (pure version) * Simplest FFmpeg video Encoder pure * * Lei hua Lei Xiaohua * [email protected] * Communication University/ Digital TV Technology * Communication University of China/digital TV Technology * HTTP://BLOG.CSDN.NET/LEIXIAOHUA1020 * * This program implements the YUV pixel number It is encoded as video stream (H264,MPEG2,VP8, etc.). * It only uses libavcodec (without using Libavformat). * Is the simplest tutorial for ffmpeg video coding. * Learn This example to learn about the FFmpeg coding process. * This software encode yuv420p data to video Bitstream * (Such as H. h.265, VP8, MPEG2 etc). * It only uses libavcodec to encode video (without Libavformat) * It's the simplest video encoding software based on Ffmpe G. * Suitable for beginner of FFmpeg */#include <stdio.h>extern "C" {#include "libavutil\opt.h" #include "libavcodec \avcodec.h "#include" libavutil\imgutils.h "};//test different codec#define test_h264 0#define TEST_HEVC 1int main (int ar    GC, char* argv[]) {Avcodec *pcodec;    Avcodeccontext *pcodecctx= NULL;    int I, ret, x, Y, got_output; FILE *fp_in;    FILE *fp_out;    Avframe *pframe; Avpacket Pkt;int Y_sizE;int Framecnt=0;char filename_in[]= ". /ds_480x272.yuv ", #if test_hevcavcodecid codec_id=av_codec_id_hevc;char filename_out[]=" DS.HEVC "; #elseAVCodecID Codec_id=av_codec_id_h264;char filename_out[]= "ds.h264"; #endifint in_w=480,in_h=272;int framenum=100;avcodec_    Register_all ();    Pcodec = Avcodec_find_encoder (codec_id);        if (!pcodec) {printf ("Codec not found\n");    return-1;    } Pcodecctx = Avcodec_alloc_context3 (PCODEC);        if (!pcodecctx) {printf ("Could not allocate video codec context\n");    return-1;    } pcodecctx->bit_rate = 400000;    Pcodecctx->width = In_w;    Pcodecctx->height = In_h;    pcodecctx->time_base.num=1;pcodecctx->time_base.den=25;    Pcodecctx->gop_size = 10;    Pcodecctx->max_b_frames = 1;    PCODECCTX-&GT;PIX_FMT = av_pix_fmt_yuv420p;     if (codec_id = = av_codec_id_h264) av_opt_set (pcodecctx->priv_data, "preset", "slow", 0); if (Avcodec_open2 (Pcodecctx, Pcodec, NULL) < 0) {printf ("CoUld not open codec\n ");    return-1;    } pframe = Av_frame_alloc ();        if (!pframe) {printf ("Could not allocate video frame\n");    return-1;    } Pframe->format = pcodecctx->pix_fmt;    Pframe->width = pcodecctx->width;    Pframe->height = pcodecctx->height;                         ret = Av_image_alloc (Pframe->data, Pframe->linesize, Pcodecctx->width, Pcodecctx->height,    PCODECCTX-&GT;PIX_FMT, 16);        if (Ret < 0) {printf ("Could not allocate raw picture buffer\n");    return-1; }//input Raw datafp_in = fopen (filename_in, "RB"), if (!fp_in) {printf ("Could not open%s\n", filename_in); return-1;} Output bitstreamfp_out = fopen (Filename_out, "WB"), if (!fp_out) {printf ("Could not open%s\n", filename_out); return-1;    }y_size = Pcodecctx->width * pcodecctx->height;        Encode for (i = 0; i < Framenum; i++) {av_init_packet (&AMP;PKT);    Pkt.data = NULL; Packet data would be allocated By the encoder pkt.size = 0;//read raw YUV dataif (fread (pframe->data[0],1,y_size,fp_in) < 0| | Yfread (pframe->data[1],1,y_size/4,fp_in) < 0| | U fread (pframe->data[2],1,y_size/4,fp_in) < 0) {//Vreturn-1;}        else if (feof (fp_in)) {break;}        pframe->pts = i;        /* Encode the image */ret = Avcodec_encode_video2 (Pcodecctx, &pkt, Pframe, &got_output);            if (Ret < 0) {printf ("Error encoding frame\n");        return-1;            } if (Got_output) {printf ("Succeed to encode frame:%5d\tsize:%5d\n", framecnt,pkt.size); framecnt++;            Fwrite (Pkt.data, 1, pkt.size, fp_out);        Av_free_packet (&AMP;PKT); }}//flush Encoder for (got_output = 1; got_output; i++) {ret = Avcodec_encode_video2 (Pcodecctx, &AMP;PK        T, NULL, &got_output);            if (Ret < 0) {printf ("Error encoding frame\n");        return-1; } if (got_output) {priNTF ("Flush encoder:succeed to encode 1 frame!\tsize:%5d\n", pkt.size);            Fwrite (Pkt.data, 1, pkt.size, fp_out);        Av_free_packet (&AMP;PKT);    }} fclose (Fp_out);    Avcodec_close (PCODECCTX);    Av_free (PCODECCTX);    Av_freep (&pframe->data[0]); Av_frame_free (&pframe); return 0;}


Run results

Determine which encoder to use by setting the macro that defines the start of the program.

Test different codec#define test_h264  0#define TEST_HEVC  1

When the test_h264 is set to 1, encode h: "ds.h264".
When TEST_HEVC is set to 1, the Hevc file "Ds.hevc" is decoded.
The input file is "DS_480X272.YUV".

The program runs as shown below.


Enter the YUV file as shown in.


The output of the HEVC file is as shown in.


Download

The simplest FFmpeg encoder pure project was added as a sub-project to the simplest FFmpeg Video encoder project. The information for the new simplest FFmpeg video encoder is as follows.


simplest ffmpeg video encoder

SourceForge home: https://sourceforge.net/projects/simplestffmpegvideoencoder/
csdn:http://download.csdn.net/detail/leixiaohua1020/8322003


This program realizes the YUV pixel data encoded as video stream (H.265,H264,MPEG2,VP8, etc.).

Is the simplest tutorial on ffmpeg video coding.

It consists of the following two sub-items:

Simplest_ffmpeg_video_encoder: The simplest ffmpeg-based video encoder. Encode and encapsulate the video using Libavcodec and Libavformat.
simplest_ffmpeg_video_encoder_pure: The simplest ffmpeg-based video encoder-pure version. Use only LIBAVCODEC encoded video and do not use Libavformat.


Simplest FFmpeg-based encoder-pure version (not including Libavformat)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.