[Original] key functions and data structures in FFMPEG of zero-basic learning video decoding

Source: Internet
Author: User

Before you start decoding, you must first understand the important functions and data structures in FFMPEG.

1. Data Structure:

(1) avformatcontext

Avformatcontext is a persistent data structure. Many functions use it as a parameter. The comment on this data structure in FFMPEG code is: Format I/O context. This structure contains the format content of a video stream. Avinputformat (or avoutputformat can exist only one of them in avformatcontext at the same time), important data structures such as avstream and avpacket, and other related information such as title, author, copyright. There are also some information that may be used in codec, such as duration, file_size, bit_rate, and so on.

Initialization: Because the avformatconext structure contains a lot of information, the initialization process is completed step by step, and some variables are not initialized if no value is available. However, since all declarations use pointers, a memory allocation process is indispensable:

  

AVFormatContext *pFormatCtx;pFormatCtx = avformat_alloc_context();

 

 

(2) avoutputformat

Specifies the CODEC to be used by the decoder ".

AVOutputFormat *fmt; fmt = guess_format(NULL, filename, NULL);

 

 

Determine the file format based on filename and initialize the encoder. Of course, if avinputformat * FMT is used, it is the fix decoder. (Specify the output sequence-> fix encoder, specify the input sequence-> fix decoder)

 

(3) avinputformat 

Obtain avinputformat from avformatcontext

AVInputFormat *inputFormat;inputFormat = pFormatCtx->iformat;

(4) avcodeccontext

Note in the ffmpeg sdk that the importance of the main external API structure is evident. In addition, every member variable of avcodec is described in detail in its definition. Avcodeccontext Initialization is the most important part in codec usage, and avcodeccontext is a member structure of avstream.

 

(5) avcodec

In structure avcodec, there are few member variables and member functions, but they are very important. It contains the codecid, which is the codec and pixel format used. There are also five functions (init, encode, close, decoder, flush) mentioned above ). The use of avcodec after Initialization is attached to avcodeccontex. The former is a member of the latter. After avcodeccontext is initialized, avcodec can also be well initialized.

 

 

(6) avframe

  Avframe is used as a description of "Original Image" (that is, YUV or RGB... Is there anything else ?) Its first two member data, uint8_t

* Data [4], int linesize [4]. The first one Stores y, CB, and Cr (YUV format). What is linesize? The two pieces of data can also extract another data structure: avpicture.

In addition, avframe also contains other member data, such. Key_frame, coded_picture_number, reference, and macro block type * mb_type.

 

The initialization of avframe is not as simple as its structure. Because avframe also has a task for carrying image data (data [4]), you should be careful when allocating memory to it.

 

(7) avpacket

  Avpacket exists as the basic unit for writing files. We may think that writing the encoded bit stream to a file is not enough. Why bother setting up an avpacket structure. In my opinion, this encoding setting is very necessary, especially for real-time video transmission. The synchronization and boundary problems can be solved through avpacket. Avpacket's member data includes two timestamps, data (usually encoded data), size and so on (see avformat. h 48 rows ). I have to mention the codec function when talking about avpacket usage, because some information about avpacket can only be known after codec.

(8) avpicture 

Avpicture has the following reasons: avpicture extracts the concept of picture from the frame and only uses the information of the picture (image), brightness, color, and row size. Frame also has information such as whether it is a key frame. Such classification makes the entire concept clearer. Avpacket is the basic data unit for writing encoded data into a file. Its unit size and data are all from avpacket.

 

(9) avstream

There is a reason why avstream is the second throughout structure after avformatcontext. The member data includes avcodeccontext, which is basically used to set the video codec parameters (including bit rate, resolution and other important information ). Also, as "stream", it contains "stream"

Some data in this concept, such as: frame rate (r_frame_rate), Basic time measurement unit (time_base), (the first frame location (start_time), and duration (duration) number of frames (nb_frames) and some IP information. Of course, some of the following information does not have to be initialized, but avcodeccontex must be initialized and is the most important part of initializing avstream.

 

The above is an important data structure in FFMPEG. The following train of thought for generating a link: (-> derived)

Avformatcontext-> avstream-> avcodeccontext-> avcodec

|

Avoutputformat or avinputformat

Avframe-> avpicture...> Avpacket

2. Functions in FFMPEG:

  The ffmpeg sdk provides many initialization and encoding functions. What we need to do is to initialize the primary data structure correctly, and correctly use the corresponding codec functions and read/write (I/O) operation functions. As an integrated code SDK, FFMPEG has its own standardized use process. For example, the av_register_all () function is a "register function" that should be called at the very beginning. It initializes libavcodec and registers all codec and video file formats ).

  (1). av_register_all ();

Usage: Initialize ibavcoded, and register all codecs and formats

Functions that must be called by each project that uses the ffmpeg sdk. Codec and format registration before use.

(2). avformatcontext * avformat_alloc_context (void );

Usage: allocate the output media context. It is actually avclass for initializing avformatcontext:

  (3 ).Void av_dump_format (avformatcontext * IC, int index, const char * URL, int is_output ); 

Usage: This step fills up the streamams field of avformatcontext with valid information. As a debugable diagnosis, we will output all this information to the standard error output, but you do not use this in an application product:

(4). Int avformat_open_input (avformatcontext ** ps, const char * filename, avinputformat * FMT, avdictionary ** options );

    Usage: open a video file.

  (5). Int avformat_find_stream_info (avformatcontext * IC, avdictionary ** options );

    Usage: obtains the stream information of a video file.

  (6). avcodec * avcodec_find_decoder (Enum avcodecid ID );

    Usage: get the video encoding format

  (7). Int avcodec_open2 (avcodeccontext * avctx, const avcodec * codec, avdictionary ** options );

    Usage: open an encoding file in an encoding format.

  (8). Int av_read_frame (avformatcontext * s, avpacket * Pkt );

    Usage: Read packet from Frame

  (9 ).Int avcodec_decode_audio4 (avcodeccontext * avctx, avframe * frame, int * got_frame_ptr, const avpacket * avpkt );

    Usage: Decoding sound

 

The decoding process is as follows:

  The decoding process is as follows: register all formats-> initialize avformatcontext-> open a video file-> get the stream information of the video file-> get the initial video stream-> get the video stream encoding content-> get the audio stream encoding content -> get video encoding format-> get audio encoding format-> open an encoding file in one encoding format-> Read packet from frame-> decode video-> decode audio-> release packet-> disable decoder-> disable avformatcontext

 

  Decoding process function: av_register_all ()-> avformat_alloc_context ()-> avformat_open_input ()-> avformat_find_stream_info ()-> parse ()-> avcodec_open2 ()-> av_new_packet () -> av_read_frame ()-> avcodec_decode_video2 () --> avcodec_decode_audio4 ()-> av_free_packet ()-> avcodec_close ()-> avformat_close_input ()

    

 

 

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.