Transferred from: http://blog.csdn.net/leixiaohua1020/article/details/44220151
FFmpeg library function source code analysis article list:
"Architecture Diagram"
FFmpeg source code structure diagram-decoding
FFmpeg source code structure Map-Encoding
General
FFmpeg Source code Simple analysis: Av_register_all ()
FFmpeg Source code Simple analysis: Avcodec_register_all ()
FFmpeg Source code Simple analysis: allocation and release of memory (Av_malloc (), Av_free (), etc.)
Simple analysis of FFmpeg source code: Initialization and destruction of common structures (avformatcontext,avframe, etc.)
FFmpeg Source code Simple analysis: Avio_open2 ()
FFmpeg Source code Simple analysis: Av_find_decoder () and Av_find_encoder ()
FFmpeg Source code Simple analysis: Avcodec_open2 ()
FFmpeg Source code Simple analysis: Avcodec_close ()
Decoding
Diagram FFmpeg The function that opens the media avformat_open_input
FFmpeg Source code Simple analysis: Avformat_open_input ()
FFmpeg Source code Simple analysis: Avformat_find_stream_info ()
FFmpeg Source code Simple analysis: Av_read_frame ()
FFmpeg Source code Simple analysis: Avcodec_decode_video2 ()
FFmpeg Source code Simple analysis: Avformat_close_input ()
Encoding
FFmpeg Source code Simple analysis: Avformat_alloc_output_context2 ()
FFmpeg Source code Simple analysis: Avformat_write_header ()
FFmpeg Source code Simple analysis: Avcodec_encode_video ()
FFmpeg Source code Simple analysis: Av_write_frame ()
FFmpeg Source code Simple analysis: Av_write_trailer ()
Other
FFmpeg source code Simple analysis: Log output system (Av_log (), etc.)
FFmpeg source code Simple analysis: Structure member management System-avclass
FFmpeg source code Simple analysis: Structure member management System-avoption
FFmpeg source code Simple analysis: Libswscale's Sws_getcontext ()
FFmpeg source code Simple analysis: Libswscale's Sws_scale ()
FFmpeg source code Simple analysis: Libavdevice's Avdevice_register_all ()
FFmpeg source code Simple analysis: Libavdevice's Gdigrab
Script
FFmpeg source code Simple analysis: Makefile
FFmpeg source Code Simple analysis: Configure
"H."
Simple analysis of the source code of the H. FFmpeg decoder: An overview
=====================================================
Recently, we have studied the source code of FFmpeg inside. Previous research on FFmpeg was primarily applied, so many of the sample programs produced were called FFmpeg APIs. But always feel like this to ffmpeg understanding is relatively shallow, so intends to analyze its source code, the rationale for its internal structure of the "context." But there is a very difficult question: FFmpeg comes with three projects: Ffplay, FFmpeg, Ffprobe code is very large, which contains hundreds of APIs, and these APIs contain a large number of ffmpeg intrinsic functions. In this way, it is almost impossible to clarify the relationship between them. After some thought, I intend to choose the most core API for the FFmpeg encoding and decoding process to analyze. In the process of encoding or decoding, the core of the API is not many, altogether about 10, so that you can analyze its internal source code.
- FFMPEG decoding process Core API can refer to: "The simplest ffmpeg+sdl-based video player"
- The coding process Core API can be consulted: "The simplest video encoder based on FFmpeg"
FFmpeg source code structure diagram-decoding
First, the analyzed FFmpeg source code structure diagram is presented. The size of this map is very large, the size is about 4000x4000, a bit like a map (so it is best to select "See more Clear picture", right-save the picture to the local before viewing). It shows the function call flow of ffmpeg when decoding a video. To ensure a clear structure, only the most critical functions are listed, eliminating other functions that are not particularly important.
Click to see a clearer picture
The following explains the meaning of a key marker.
function background Color
The function is shown as a box in the diagram. Different background colors mark the different functions of the function:
- Pink background function: FFmpeg's API function.
- White background function: FFmpeg's intrinsic function.
- Functions of the yellow background: functions in the URLPROTOCOL structure, including the function of the Processing protocol (PROTOCOL).
- Functions in the green background: functions in the AVINPUTFORMAT structure, including the ability to handle encapsulation formats (format).
- Functions in the blue background: functions in the AVCODEC structure, including the function of the codec (CODEC).
Ps:urlprotocol,avinputformat,avcodec is connected to a list of links after FFmpeg starts running and the components are registered. So there are actually a lot of urlprotocol,avinputformat,avcodec. The picture shows that decoding an input protocol is "file" (which is actually opening a file.) "File" is also considered as a generalized protocol), the encapsulation format is FLV, the video encoding format is the function call relationship of the data of H.
Regional
The entire architecture diagram can be divided into the following areas:
- Left area--schema function area: These functions are not intended for a particular video format.
- Yellow area in upper right--Protocol processing function area: Different protocol (RTP,RTMP,FILE) calls different protocol handlers.
- Right middle Green Zone-encapsulates the format processing function area: Different encapsulation formats (Mkv,flv,mpegts,avi) call different encapsulation format handlers.
- Lower right blue area-codec function area: different encoding standards (HEVC,H.264,MPEG2) call different codec functions.
Arrow Line
To make the call relationship more obvious, the arrow lines in the diagram also use different colors:
Black Arrow Line: Flags the call relationship between functions.
Red Arrow Line: Marks the process of decoding.
Arrow lines for other colors: flags the call relationship between functions. which
Calling the function in the URLPROTOCOL structure is identified by a yellow arrow line;
Calls the function in the AVINPUTFORMAT structure with the green Arrow line identification;
calls to functions in the AVCODEC structure are identified with blue arrow lines.
The file where the function resides
The path to the file where it is located is identified next to each function.
Also, it is important to note that some API functions also invoke other API functions inside. In other words, the API function does not necessarily call FFmpeg's intrinsic function, and it is possible for him to invoke other API functions. As you can see, Avformat_close_input () calls Avformat_free_context () and Avio_close (). These API functions that are called in internal code are also marked pink.
Function call Relationship
The following is a simple list of call relationships between functions in several regions (the invocation relationship between functions is represented by indentation). Detailed function analysis can refer to the relevant "FFmpeg Source code Analysis" series of articles.
Left area (ffmpeg schema function)
1. Av_register_all () "Function Simple Analysis"
1) Avcodec_register_all ()
(a) Register_hwaccel ()
(b) Register_encoder ()
(c) Register_decoder ()
(d) Register_parser ()
(e) REGISTER_BSF ()
2) Register_muxer ()
3) Register_demuxer ()
4) Register_protocol ()
2. Avformat_alloc_context () "Function Simple Analysis"
1) av_malloc (sizeof (Avformatcontext))
2) Avformat_get_context_defaults ()
(a) Av_opt_set_defaults ()
3. Avformat_open_input () "Function Simple Analysis"
1) init_input ()
(a) Avio_open2 ()"Simple analysis of Functions"
a) ffurl_open ()
i. ffurl_alloc ()
l URL_FIND_PR Otocol ()
l url_alloc_for_protocol ()
ii. ffurl_connect ()
Urlprotocol->url_open ()
b) Ffio_fdopen ()
I. Av_malloc (BUFFER_SIZE)
II. Avio_alloc_context ()
L Av_mallocz (sizeof (aviocontext))
L ffio_init_context ()
(b) Av_probe_input_buffer2 ()
A) Avio_read ()
I. Avinputformat->read_packet ()
b) Av_probe_input_format2 ()
c) AV_PROBE_INPUT_FORMAT3 ()
I. Av_iformat_next ()
II. Av_match_name ()
Iii. Av_match_ext ()
Iv. Avinputformat->read_probe ()
2) Avinputformat->read_header ()
4. Avformat_find_stream_info () "Function Simple Analysis"
1) Find_decoder ()
(a) Avcodec_find_decoder ()
2) Avcodec_open2 ()
3) read_frame_internal ()
4) Try_decode_frame ()
(a) Avcodec_decode_video2 ()
5) Avcodec_close ()
6) Estimate_timings ()
(a) estimate_timings_from_pts ()
(b) Estimate_timings_from_bit_rate ()
(c) update_stream_timings ()
5. Avcodec_find_decoder () "Function Simple Analysis"
1) Find_encdec ()
6. Avcodec_open2 () "Function Simple Analysis"
1) avcodec->init ()
7. Av_read_frame () "Function Simple Analysis"
1) Read_from_packet_buffer ()
2) read_frame_internal ()
(a) Ff_read_packet ()
A) Avinputformat->read_packet ()
(b) Parse_packet ()
A) Av_parser_parse2 ()
8. Avcodec_decode_video2 () "Function Simple Analysis"
1) av_packet_split_side_data ()
2) avcodec-> decode ()
3) Av_frame_set_pkt_pos ()
4) Av_frame_set_best_effort_timestamp ()
9. Avcodec_close () "Function Simple Analysis"
1) avcodec->close ()
Avformat_close_input () "Function Simple Analysis"
1) avinputformat->read_close ()
2) Avformat_free_context ()
(a) Ff_free_stream ()
3) Avio_close ()
(a) Avio_flush ()
A) Flush_buffer ()
(b) Ffurl_close ()
A) ffurl_closep ()
Urlprotocol->url_close ()
Upper right area (Urlprotocol protocol processing function)
The URLPROTOCOL structure contains the following protocol handler function pointers:
Url_open (): Open
Url_read (): Read
Url_write (): Write
Url_seek (): Adjust progress
url_close (): Off
"Example" different protocols correspond to the above interfaces have different implementation functions, give a few examples:
The file protocol (i.e. files) corresponds to the URLPROTOCOL structure body Ff_file_protocol:
Url_open (), File_open (),
open ()
Url_read (), File_read (), read ()
Url_write (), File_write (), write ()
Url_seek (), File_seek (), Lseek ()
Url_close (
), File_close (), Close ()
The RTMP Protocol (LIBRTMP) corresponds to the URLPROTOCOL structure Ff_librtmp_protocol:
Url_open (), Rtmp_open (), Rtmp_init (), Rtmp_setupurl (), Rtmp_connect (), Rtmp_connectstream ()
Url_read (), Rtmp_read (), Rtmp_read ()
Url_write (), Rtmp_write (), Rtmp_write ()
Url_seek (), Rtmp_read_seek (), Rtmp_sendseek ()
Url_close (
), Rtmp_close (), Rtmp_close ()
The UDP protocol corresponds to the URLPROTOCOL structure body Ff_udp_protocol:
Url_open (), Udp_open ()
Url_read (), Udp_read ()
Url_write (), Udp_write ()
Url_seek (), Udp_close ()
Url_close (), Udp_close ()
Right-Middle Area (Avinputformat package format handler function)
The Avinputformat contains the following encapsulation format handler pointers:
read_probe (): Check format
Read_header (): Read file header
Read_packet (): reads a frame of data
Read_seek (): Adjust progress
Read_close (): Off
"Example" different packaging format corresponds to the above interface has different implementation functions, for example:
The Avinputformat structure body ff_flv_demuxer for the FLV package format:
read_probe (), Flv_probe () –> probe ()
Read_header (), Flv_read_header (), Create_stream (), Avformat_new_stream ()
Read_packet (), Flv_read_packet ()
Read_seek (), Flv_read_seek ()
Read_close (), Flv_read_close (
)
MKV Package format corresponds to the AVINPUTFORMAT structure ff_matroska_demuxer:
read_probe (), Matroska_probe ()
Read_header (), Matroska_read_header ()
Read_packet (), Matroska_read_packet ()
Read_seek (), Matroska_read_seek ()
Read_close (), Matroska_read_close (
)
The MPEG2TS package format corresponds to the AVINPUTFORMAT structure ff_mpegts_demuxer:
Read_probe (), Mpegts_probe ()
Read_header (), Mpegts_read_header ()
Read_packet (), Mpegts_read_packet ()
Read_close (), Mpegts_read_close (
)
AVI package format corresponding to the AVINPUTFORMAT structure ff_avi_demuxer:
read_probe (), Avi_probe ()
Read_header (), Avi_read_header ()
Read_packet (), Avi_read_packet ()
Read_seek (), Avi_read_seek ()
Read_close (), Avi_read_close (
)
Lower right area (Avcodec codec function)
The AVCODEC contains the following codec function pointers:
init (): Initialize
Decode (): Decoding a frame of data
Close (): Off
"Example" different codecs correspond to the above interfaces have different implementation functions, for example:
HEVC decoding corresponds to the Avcodec structural body Ff_hevc_decoder:
Init (), Hevc_decode_init ()
Decode (), Hevc_decode_frame (), Decode_nal_units ()
Close (), Hevc_decode_free ()
The decoding corresponding AVCODEC structure of the Ff_h264_decoder:
init (), Ff_h264_decode_init ()
Decode (), H264_decode_frame (), Decode_nal_units ()
Close (), H264_decode_end ()
VP8 decoding (LIBVPX) corresponds to the AVCODEC structure Ff_libvpx_vp8_decoder:
Vpx_init () Vpx_codec_dec_init (),
init ()
Decode (), Vp8_decode (), Vpx_codec_decode (), Vpx_codec_get_frame ()
Close () Vp8_free ()-Vpx_codec_destroy (
)
MPEG2 decoding the corresponding AVCODEC structure Ff_mpeg2video_decoder:
init (), Mpeg_decode_init ()
Decode (), Mpeg_decode_frame ()
Close (), Mpeg_decode_end ()
Rai (Lei Xiaohua)
[Email protected]
http://blog.csdn.net/leixiaohua1020
FFmpeg source code structure diagram