The simplest analysis of +FFMPGA code based on FFmpeg transcoding program (go + summarize)

Source: Internet
Author: User

Module:

Libavcodec-Codec
Libavdevice-Input support
Libavfilter-Video Audio filter Support
Libavformat-Visual Audio format parsing
Libavutil-Tools Library
Libpostproc-Post effect processing
Libswscale-Image Color, size conversion

1. Brief analysis of FFMPGA code

1.1 Av_log ()

Av_log () is a function of the output log in FFmpeg. Open a FFmpeg source code file, you will find the Av_log () function. In general, the FFmpeg class Library's source code is not allowed to use printf () such a function, all output is used Av_log ().                        The declaration of Av_log () is located in Libavutil\log.h, as shown below. void Av_log (void *AVCL, int level, const char *fmt, ...) Av_printf_format (3, 4);

The declaration of this function is more special in two places:

(1) The last parameter of the function is "...".
In the C language, use "..." to represent parameters in the case of an indeterminate number of function arguments.  For example, the prototype of printf () is defined as follows: int printf (const char*, ...);

(2) Its declaration is followed by a Av_printf_format (3, 4). There has been no in-depth study of this place, and the online material says its role is to check the format of Av_log () in the format of printf ().

Av_log () Each field has the following meanings:
AVCL: Specifies a struct that contains avclass.
Level of Level:log
FMT: As with printf ().

Thus, the difference between av_log () and printf () is mainly due to the fact that there are two more parameters ahead. The first parameter specifies the struct to which the log belongs, such as Avformatcontext, Avcodeccontext, and so on. The second parameter specifies the level of LOG, and the following levels are defined in the source code: Av_log_panic,av_log_fatal,av_log_error,av_log_warning,av_log_info,av_log_verbose, Av_log_debug. The values defined for each level represent the severity, and the smaller the number represents the more severe. The default level is Av_log_info. In addition, there is one level that does not output any information, namely Av_log_quiet.

There is a "log level" in the current system. All log information that is more severe than this level will be output. For example, the current LOG level is av_log_warning, which outputs information at the av_log_panic,av_log_fatal,av_log_error,av_log_warning level without outputting av_log_info level information. You can get the current log level through Av_log_get_level (), and another function Av_log_set_level () to set the current log level.

1.1 int _tmain (int argc, _tchar* argv[])

People who have used C know that every C program will have a main (), but sometimes it is written by someone else to find that the main function is not an int main (), but an int _tmain (), and the header file is not <iostream.h> but <stdafx.h Will you be confused? First of all, this _tmain () is to support Unicode used by the main an alias, since it is an alias, there should be a macro defined, where is defined? In the <stdafx.h> that confused you, there are two lines
#include <stdio.h>
#include <tchar.h>
We can find the macro definition of _tmain in the header file <tchar.h>
#define _tmain Main
So, after pre-compiling, _tmain becomes main.

The _TCHAR type is a wide-character string, unlike the string we commonly use, which is a type used in 32-bit or higher operating systems.

1.2 Source code--General part

(1). Av_register_all (), Avcodec_register_all ()

Avcodec_register_all (): Register Hwaccel,encoder,decoder,parser,bitstream
Av_register_all (): Registers Muxer,demuxer,protocol, which is almost the first called in all FFmpeg-based applications. Only the function can be called to use a multiplexer, encoder, etc.
Avfilter_register_all (): Register filter Filter

(2). Memory allocation: Av_malloc (), Av_realloc (), Av_mallocz (), Av_calloc (), Av_free (), Av_freep ()

Common functions for memory operations are located in Libavutil\mem.:

Av_malloc ()--simply encapsulates the system's malloc () and does the error checking work;

Av_realloc ()--simply encapsulates the system's ReAlloc (), which is used to adjust the requested memory size;

After Av_malloc () is called in Av_mallocz ()--av_mallocz (), and memset () is called, the allocated memory is set to 0

Av_calloc ()--Simple encapsulation of AV_MALLOCZ ();

Av_freep ()--Release the requested memory;

Av_freep ()--simply encapsulates the Av_free () and sets the target pointer to null after releasing the memory.

(3). FFmpeg Structural Body

A) Solution protocol (HTTP,RTSP,RTMP,MMS)

Aviocontext,urlprotocol,urlcontext primarily stores the type and status of the protocol used by AV. The Urlprotocol stores the encapsulation format used by the input video audio. Each of these protocols corresponds to a URLPROTOCOL structure. (Note: Files in FFmpeg are also treated as a protocol "file")

b) Solution Encapsulation (FLV,AVI,RMVB,MP4)

The Avformatcontext mainly stores the information contained in the video-Audio encapsulation format, and the Avinputformat stores the encapsulation format used by the input video audio. Each video and audio package format corresponds to a AVINPUTFORMAT structure.

c) decoding (H264,mpeg2,aac,mp3)

Each Avstream stores data related to a video/audio stream, and each avstream corresponds to a avcodeccontext that stores the data that the video/audio stream uses for decoding; a avcodec in each avcodeccontext that contains the video /audio corresponding to the decoder. Each decoder corresponds to a AVCODEC structure.

d) Storage data

Video, each structure is usually saved one frame; audio may have several frames

Pre-decoded data: Avpacket

Decoded data: Avframe

Avformatcontext: The basic structure that governs the whole world. Mainly used for processing encapsulation formats (FLV/MKV/RMVB, etc.)

Aviocontext: input and output corresponding structure for input and output (read/write files, rtmp protocol, etc.).

Avstream,avcodeccontext: The structure of the AV stream, which is used to encode and decode audio and video.

Avframe: Storage of uncompressed data (video corresponds to RGB/YUV pixel data, audio corresponds to PCM sampled data)

Avpacket: Storage of compressed data (video corresponding to the stream data, audio corresponding to the Aac/mp3 stream data)

(4). Avio_open2 ()

This function is used to open the input and output file of FFmpeg.  The declaration of Avio_open2 () is located in the libavformat\avio.h file int avio_open2 (aviocontext **s, const char *url, int flags, const AVIOINTERRUPTCB *INT_CB, avdictionary **options)

S: Aviocontext structure created after a successful function call.
URL: The address of the input and output protocol (the file is also a "generalized" protocol, for the file is the path of the file).
Flags: How to open the address. Avio_flag_read: Read only; Avio_flag_write: Write only; avio_flag_read_write: Read and write.                                                 

(5). Avcodec_find_encoder () and Avcodec_find_decoder ()

Finding encoders and decoders is essentially traversing the Avcodec list and getting the element that meets the criteria, declared in libavcodec\avcodec.h:

Avcodec *avcodec_find_encoder (enum avcodecid ID);

Avcodec *avcodec_find_decoder (enum avcodecid ID);

(6). Avcodec_open2 ()

Initializes a avcodeccontext of a video-audio codec. The declaration of Avcodec_open2 () is located in Libavcodec\avcodec.h

int Avcodec_open2 (Avcodeccontext *avctx, const AVCODEC *CODEC, Avdictionary **options);

Avctx: Avcodeccontext that needs to be initialized;
Codec: input of Avcodec;
Options: some choices. For example, when using libx264 encoding, "preset", "tune", etc. can be set by this parameter

Avcodec_open2 The main work of the () function:

1) allocating memory for various structures (via various av_malloc ())
2) Set the option of the input avdictionary form to Avcodeccontext
3) Other piecemeal checks, such as checking if the codec is in the "experimental" phase
4) If the encoder, check whether the input parameters meet the requirements of the encoder
5) Call Avcodec init () to initialize the specific decoder.

(7). avcodec_close ()

This function is used to close the encoder. The declaration of the Avcodec_close () function is located in libavcodec\avcodec.h

int Avcodec_close (Avcodeccontext *avctx);

1.3 Source code-decoding part

(1). Avformat_open_input ()

The process of opening the media begins at avformat_open_input (), Complete:

1). Initialization of the input/output structure body aviocontext;

2). Identification of the protocol (e.g. rtmp, or file) of the input data (via a set of scoring mechanisms):

Determine the suffix of the file name + read the data of the header

Using the Urlprotocol of the file protocol that gets the highest score, through the way of the function pointer, with the

FFmpeg Connection (non-professional words);

3). All that is left is to call the Urlprotocol function to do open,read and so on.

(2). Avformat_close_input ()

Used to open a avformatcontext, which is normally used in pairs with avformat_open_input ();

The declaration of Avformat_close_input () is located in libavformat\avformat.h: void Avformat_close_input (Avformatcontext **s);

function function:

1) Call Avinputformat's Read_close () method to close the input stream
2) call Avformat_free_context () to release Avformatcontext
3) Call Avio_close () to close and release Aviocontext

(3). Avformat_find_stream_info ()

This function can read some of the AV data and obtain some relevant information. The declaration of Avformat_find_stream_info () is located in Libavformat\avformat.h:

int Avformat_find_stream_info (Avformatcontext *ic, avdictionary **options); The return value is greater than or equal to 0 after normal function execution;

C: Avformatcontext of the input
Options: Extras

Function:

This function is primarily used to assign values to the Avstream structure of each media stream (audio/video). In fact, it has realized the decoder of the search, decoder open, depending on the audio frame reading, video audio frame decoding and so on. In other words, the function has actually "walked through" the entire process of decoding. Let's take a look at some of the key processes of the function in addition to the member variable assignment:

1). Find decoder: Find_decoder ()
2). Open decoder: Avcodec_open2 ()
3). Read the full frame of compressed encoded data: read_frame_internal () Note: Av_read_frame () is actually called read_frame_internal ().
4). Decode Some compressed encoded data: Try_decode_frame ()

(4). Av_read_frame ()

Reads the audio from the stream in several frames or a video frame. For example, when decoding a video, each decoding a video frame, you need to call Av_read_frame () to obtain a frame of video compression data before the data can be decoded (for example, the compressed data in a frame of H. A is usually a nal).

reference others to Av_read_ first FRAME () of the explanation, do a reference: through Av_read_packet (* * *), to read a package, it is necessary to note that this function must be a case that contains an integer frame, does not exist half frame, take the TS stream as an example, is to read a complete PES package (a complete PES package contains several video or audio es packages), after reading through AV_PARSER_PARSE2 (* * *) Analyze the video frame (or several frames of audio), return, the next time you enter the loop, if the last data is not completely finished, then St = s->cur_st; will not be null, that is, then enter the AV_PARSER_PARSE2 (* * *) process, not the following av_read _packet (* *) process, so that if the read contains n-frame video data (in the case of video), then call Av_read_frame (* * *) n times will not read the data, but instead return the first read data, until all parsing is complete.

 Note :Av_read_frame-The new version of FFmpeg with Av_read_frame, and the old version is Av_read_packet, the difference is av_read_packet read out is the package, It may be half-frame or multi-frame and does not guarantee the integrity of the frame. The Av_read_frame Av_read_packet is encapsulated so that the read-out data is always a full frame   

The declaration of Av_read_frame () is located in libavformat\avformat.h: int av_read_frame (Avformatcontext *s, Avpacket *pkt);

S: Input of Avformatcontext

PKT: Output of Avpacket/*

 

The simplest analysis of +FFMPGA code based on FFmpeg transcoding program (go + summarize)

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.