Source code structure of FFmpeg, source code structure of ffmpeg

Source: Internet
Author: User
Tags coding standards

Source code structure of FFmpeg, source code structure of ffmpeg

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

FFmpeg library function source code analysis article list:

[General]

Simple Analysis of FFmpeg source code: av_register_all ()

Simple Analysis of FFmpeg source code: avcodec_register_all ()

Simple Analysis of FFmpeg source code: Memory Allocation and release (av_malloc (), av_free (), etc)

Simple Analysis of FFmpeg source code: initialization and destruction of common struct (AVFormatContext, AVFrame, etc)

Simple Analysis of FFmpeg source code: avio_open2 ()

Simple Analysis of FFmpeg source code: av_find_decoder () and av_find_encoder ()

Simple Analysis of FFmpeg source code: avcodec_open2 ()

Simple Analysis of FFmpeg source code: avcodec_close ()

[Decoding]

Illustration: The function avformat_open_input of FFMPEG to open the media

Simple Analysis of FFmpeg source code: avformat_open_input ()

Simple Analysis of FFmpeg source code: avformat_find_stream_info ()

Simple Analysis of FFmpeg source code: av_read_frame ()

Simple Analysis of FFmpeg source code: avcodec_decode_video2 ()

Simple Analysis of FFmpeg source code: avformat_close_input ()

[Encoding]

Simple Analysis of FFmpeg source code: avformat_alloc_output_context2 ()

Simple Analysis of FFmpeg source code: avformat_write_header ()

Simple Analysis of FFmpeg source code: avcodec_encode_video ()

Simple Analysis of FFmpeg source code: av_write_frame ()

Simple Analysis of FFmpeg source code: av_write_trailer ()

[Architecture diagram]

FFmpeg source code structure-decoding

FFmpeg source code structure-Encoding

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

The previous article deeply analyzed the internal source code of core APIs in the FFmpeg decoding process. This article continues to analyze the internal source code of core APIs in the FFmpeg encoding process. The encoding process in this article can be referred to the program "the simplest FFmpeg-Based Video Encoder".



Function call Relationship Diagram

First, the analysis result is displayed ,. The size of this image is large (greater than 4000x4000). Therefore, you must open the image link and save the image as a local file before you can view it. It indicates the function call process of FFmpeg when encoding a video. To ensure a clear structure, only the most critical functions are listed, removing other functions that are not particularly important.


Click to view clearer images


The meaning of key mark in section 1 is explained below.


The function background color function is displayed in a box in the graph. Different Background colors indicate different functions:
Pink background function: FFmpeg API function.
White Background function: internal function of FFmpeg.
Functions with a yellow background: Functions in the URLProtocol struct, including the ability to read and write various protocols.
Green background function: the function in the AVOutputFormat struct, which contains the function of reading and writing various encapsulation formats.
Blue background function: a function in the AVCodec struct, including the codec function.

The entire region relationship diagram can be divided into the following areas:
Left area-Architecture Function Area: these functions are not applicable to a specific video format.
Yellow area in the upper right corner-protocol processing function area: different protocols (RTP, RTMP, FILE) Call different protocol processing functions.
Green area in the middle of the right -- Encapsulation Format processing function area: Different encapsulation formats (MKV, FLV, MPEG2TS, AVI) Call different Encapsulation Format processing functions.
The blue area at the bottom of the right -- codec function area: different coding standards (HEVC, H.264, MPEG2) Call different codec functions.


In order to make the call relationship more explicit, the arrow lines in the figure also use different colors:
Red Arrow line: indicates the encoding process.
Arrow lines of other colors: Indicate the call relationship between functions. Where:
Call the functions in the URLProtocol struct to identify them with yellow arrows;
Call the function in the AVOutputFormat struct to identify it with a green arrow;

Call the function in the AVCodec struct to identify it with a blue arrow.


The file where the function is located. Each function identifies the file path where the function is located.



The following section briefly describes the call relationships between functions in several regions (the call relationships between functions are represented in indentation ). For detailed function analysis, see related articles in FFmpeg source code analysis.

Left area (Architecture Function)

 

1. av_register_all () [Simple Function 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_output_context2 () [Simple Function Analysis]

1) avformat_alloc_context ()

(A) av_malloc (sizeof (AVFormatContext ))

(B) avformat_get_context_defaults ()

A) av_opt_set_defaults ()

2) av_guess_format ()

(A) av_oformat_next ()

(B) av_match_name ()

(C) av_match_ext ()

3. avio_open2 () [Simple Function Analysis]

1) ffurl_open ()

(A) ffurl_alloc ()

A) url_find_protocol ()

B) url_alloc_for_protocol ()

(B) ffurl_connect ()

A) URLProtocol-> url_open ()

2) ffio_fdopen ()

(A) av_malloc (buffer_size)

(B) avio_alloc_context ()

A) av_mallocz (sizeof (AVIOContext ))

B) ffio_init_context ()

4. avformat_new_stream () [Simple Function Analysis]

1) av_mallocz (sizeof (AVStream ))

2) avcodec_alloc_context3 ()

(A) av_malloc (sizeof (AVCodecContext ))

(B) avcodec_get_context_defaults3 ()

5. avcodec_find_encoder () [Simple Function Analysis]

1) find_encdec ()

6. avcodec_open2 () [Simple Function Analysis]

1) AVCodec-> init ()

7. avformat_write_header () [Simple Function Analysis]

1) init_muxer ()

2) AVOutputFormat-> write_header ()

3) init_pts ()

8. avcodec_encode_video2 () [Simple Function Analysis]

1) AVCodec-> encode2 ()

9. av_write_frame () [Simple Function Analysis]

1) check_packet ()

2) compute_pkt_fields2 ()

3) write_packet ()

(A) AVOutputFormat-> write_packet ()

10. av_write_trailer () [Simple Function Analysis]

1) write_packet ()

2) AVOutputFormat-> write_trailer ()

11. avcodec_close () [Simple Function Analysis]

1) AVCodec-> close ()

12. avformat_free_context () [Simple Function Analysis]

1) ff_free_stream ()

13. avio_close () [Simple Function Analysis]

1) avio_flush ()

(A) flush_buffer ()

2) ffurl_close ()

(A) ffurl_closep ()

A) URLProtocol-> url_close ()

 

The URLProtocol struct in the upper right contains the following protocol handler pointer:
Url_open (): Open
Url_read (): Read
Url_write (): Write
Url_seek (): Adjust the progress
Url_close (): Close

[Example] different protocols correspond to different implementation functions of the above interfaces. For example:

The URLProtocol structure ff_file_protocol corresponding to the File protocol (File:
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 URLProtocol structure ff_librtmp_protocol corresponding to the RTMP protocol (libRTMP:
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 URLProtocol struct ff_udp_protocol corresponding to the UDP protocol:
Url_open ()-> udp_open ()
Url_read ()-> udp_read ()
Url_write ()-> udp_write ()
Url_seek ()-> udp_close ()
Url_close ()-> udp_close ()

AVOutputFormat (AVOutputFormat Encapsulation Format handler) AVOutputFormat contains the following Encapsulation Format handler pointer:
Write_header (): Write the file header
Write_packet (): Write a frame of data.
Write_trailer (): End of the file

[Example] Different encapsulation formats correspond to different implementation functions of the above interfaces. For example:

The AVOutputFormat struct ff_flv_muxer corresponding to the FLV Encapsulation Format:
Write_header ()-> flv_write_header ()
Write_packet ()-> flv_write_packet ()

Write_trailer ()-> flv_write_trailer ()

The AVOutputFormat struct ff_matroska_muxer corresponding to the MKV Encapsulation Format:
Write_header ()-> mkv_write_header ()
Write_packet ()-> mkv_write_flush_packet ()

Write_trailer ()-> mkv_write_trailer ()

The AVOutputFormat struct ff_mpegts_muxer corresponding to the MPEG2TS Encapsulation Format:
Write_header ()-> mpegts_write_header ()
Write_packet ()-> mpegts_write_packet ()

Write_trailer ()-> mpegts_write_end ()

The AVOutputFormat struct ff_avi_muxer corresponding to the AVI Encapsulation Format:
Write_header ()-> avi_write_header ()
Write_packet ()-> avi_write_packet ()
Write_trailer ()-> avi_write_trailer ()

The lower-right area (AVCodec codec function) AVCodec contains the following codec function pointer:
Init (): initialization
Encode2 (): encode a frame of data
Close (): close

[Example] Different codecs correspond to different implementation functions of the above interfaces. For example:

The AVCodec struct ff_libx265_encoder corresponding to the HEVC Encoder:
Init ()-> libx265_encode_init ()-> x265_param_alloc (), x265_param_default_preset (), x265_encoder_open ()
Encode2 ()-> libx265_encode_frame ()-> x265_encoder_encode ()

Close ()-> libx265_encode_close ()-> x265_param_free (), x265_encoder_close ()

The AVCodec struct ff_libx1__encoder corresponding to the H.264 Encoder:
Init ()-> x1__init ()-> x1__param_default (), x1__encoder_open (), x1__encoder_headers ()
Encode2 ()-> x1__frame ()-> x1__encoder_encode ()

Close ()-> x1__close ()-> x1__encoder_close ()

The AVCodec struct ff_libvpx_vp8_encoder corresponding to the VP8 encoder (libVPX:
Init ()-> vpx_init ()-> vpx_codec_enc_config_default ()
Encode2 ()-> vp8_encode ()-> vpx_codec_enc_init (), vpx_codec_encode ()

Close ()-> vp8_free ()-> vpx_codec_destroy ()

The AVCodec struct ff_mpeg2video_encoder corresponding to MPEG2 Encoder:
Init ()-> encode_init ()
Encode2 ()-> ff_mpv_encode_picture ()
Close ()-> ff_mpv_encode_end ()



Lei Xiaohua
Leixiaohua1020@126.com
Http://blog.csdn.net/leixiaohua1020


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.