x264 Source Code Simple analysis: Overview

Source: Internet
Author: User

Recently, we are studying the encoding of H. A and HEVC, so we analyze the source code of the most common H.--x264 Encoder. This article simply combs its structure. X264 source code is large and involves a lot of algorithms, there are a lot of things do not understand, so there is no guarantee that the analysis is completely correct. At present, I intend to put the understanding of the part of the first to make a memo.


Function call graph

The function call diagram for X264 is shown below.


Click to see a larger image
The following explains the meaning of a key marker.

The function background color function is shown as a box in the diagram. Different background colors mark the different functions of the function:
White background functions: non-distinguishing ordinary intrinsic functions.
Light red background function: interface function (API) of the Libx264 class library.
Pink background function: Filtering function (filter). For loop filtering, half-pixel interpolation, Ssim/psnr calculation.
Yellow background function: analytic function (analysis). Used in intra-frame prediction mode, or inter-frame prediction mode judgment.
function of the green background: Macro block encoding function (Encode). The macro block is encoded by DCT transform and quantization of residual error.
function of the purple background: Entropy coding function (Entropy Coding). Cabac or CAVLC entropy encoding the data after the macro block is encoded.
Blue background function: assembly function (Assembly). Functions that have been optimized for assembly. The figure shows the C language versions of these functions, plus the MMX version, SSE version, neon version, and so on.
light blue background function: bitrate control function. A function that controls the rate of the code. The specific methods include ABR, CBR, CRF and so on.

The entire diagram of a region can be divided into the following areas:
the leftmost area--x264 the command-line program function area.
The
left middle area--libx264 the inner function area.
upper right Pink area--filter module. These include loop filtering, half-pixel interpolation, and Ssim/psnr calculations.
Yellow area on top right--analysis module. It includes intra-frame prediction mode analysis and inter-frame motion estimation.
right Middle green area--macro block encoding module. It includes DCT transform, quantization, Hadamard transform for coded frames, and DCT inverse transform, inverse quantization, Hadamard inverse transformation for reconstructed frames.
Lower
right Purple area--Entropy coding module. It contains CABAC or CAVLC entropy coding.

Arrow Line

The Arrow line flags the function's call relationship:

Black Arrow Line: an indiscriminate call relationship.
Pink Arrow Line: The call relationship between the filter function (filter).
Yellow Arrow Line: The call relationship between analytic functions (analysis).
Green Arrow Line: The call relationship between the macro block encoding function (Encode).
Purple Arrow Line: The call relationship between the entropy-coded function (Entropy Coding).
The file where the function is located each function identifies the file path where it resides.



Several key sections below briefly record several key sections of the diagram.

x264 Command-line programThe x264 command-line program refers to the console program provided by the x264 project. Through this program can call libx264 encoded YUV to H. The entry function for the program is main (). The main () function first calls parse () to parse the input parameters and then calls encode () to encode the YUV data.
Parse () first calls X264_param_default () to assign a default value to the x264_param_t struct that holds the parameter, and then resolves the parameters stored in argv[] through the command line through Getopt_long () in a cycle. and make the corresponding setup work; finally call Select_input () and Select_output () to complete the setup of input file format (yuv,y4m, etc.) and output file format (bare stream, mp4,mkv,flv, etc.).
Encode () first calls X264_encoder_open () to open the encoder, and then calls X264_encoder_parameters () to pass the parameters stored in the parameter structure x264_param_t to the encoder And then repeatedly call Encode_frame () in a loop to encode one frame at a frames, and finally call X264_encoder_close () to close the encoder after the encoding is complete.
Encode_frame () calls X264_encoder_encode () to encode the x264_picture_t that stores the YUV data as the x264_nal_t that stores the data in H.

interface of the libx264 class libraryIn a x264 encoding process, you need to call at least the following API functions (refer to the article "simplest video encoder: Based on libx264 (encoded YUV)"):
X264_param_default (): Sets the default value of the parameter set structure body x264_param_t.
X264_picture_alloc (): Allocates memory for the image structure body x264_picture_t.
X264_encoder_open (): Open the encoder.
X264_encoder_encode (): Encodes a frame image.
X264_encoder_close (): Turn off the encoder.
X264_picture_clean (): Releases the resources requested by X264_picture_alloc ().

The libx264 intrinsic function libx264 intrinsic function refers to the function that precedes the API function, X264_slice_write (). This part of the function is more, temporarily not detailed analysis, just give a few examples to list their functions.
X264_encoder_open () calls the following function:
X264_validate_parameters (): Check the input parameters (for example, if the width of the input image is a positive number).
X264_predict_16x16_init (): Initializes the intra16x16 intra-frame predictive assembler function.
X264_predict_4x4_init (): Initializes the intra4x4 intra-frame predictive assembler function.
X264_pixel_init (): Initializes the pixel values to calculate the associated assembler functions (including sad, SATD, SSD, etc.).
X264_dct_init (): Initializes the assembly functions related to DCT transform and DCT inverse transformation.
X264_mc_init (): Initializes the assembly function associated with the motion compensation.
X264_quant_init (): initializes the compilation functions related to quantization and inverse quantization.
X264_deblock_init (): Initializes the assembly function associated with the block-effect filter.
X264_lookahead_init (): Initializes lookahead related variables.
X264_ratecontrol_new (): Initializes the bitrate control module.

x264_encoder_headers ()The following function is called:
x264_sps_write (): Output SPS
X264_pps_write (): Output pps
X264_sei_version_write (): Output sei

X264_encoder_encode ()The following function is called:
X264_frame_pop_unused (): Gets the 1 x264_frame_t type structural body fenc. If the frames.unused[] queue is not empty, call X264_frame_pop () from the unused[] queue to fetch 1 off-the-shelf, otherwise call x264_frame_new () to create a new one.
X264_frame_copy_picture (): Copies the input image data to the Fenc.
X264_lookahead_put_frame (): Puts Fenc into lookahead.next.list[] queue, waiting to determine the frame type.
X264_lookahead_get_frames (): Parses the frame type by lookahead. The function calls X264_slicetype_decide (); X264_slicetype_decide () also calls X264_slicetype_analyse () and X264_slicetype_frame_cost (). After some column analysis, the frame type information is finalized and the frame is placed in the frames.current[] queue.
X264_frame_shift (): Remove a frame from the frames.current[] queue for encoding.
X264_reference_update (): Updates the reference frame queue.
X264_reference_reset (): If it is an IDR frame, call the function to empty the reference frame list.
X264_reference_hierarchy_reset (): If it is I (non-IDR frame), p-frame, B-frame (which can be used as a reference frame), call the function (not yet studied).
X264_reference_build_list (): Create reference frame list list0 and List1.
X264_ratecontrol_start (): Turn on bit rate control.
X264_slice_init (): Create Slice Header.
X264_slices_write (): Encode data (the most critical step). which calls X264_slice_write () completes the work of the encoding (note that "x264_slices_write ()" and "X264_slice_write ()" Have a "s" name difference).
X264_encoder_frame_end (): After encoding, do some post-processing, such as releasing some intermediate variables and printing out some statistical information. where x264_frame_push_unused () is called, the Fenc is re-placed back into the frames.unused[] queue, and X264_ratecontrol_end () is called to turn off the bitrate control.

X264_slice_write () X264_slice_write () is used to encode slice. The function contains a very long for () loop. The loop encodes a macro block each time it is executed. The following functions are more important in x264_slice_write ():
X264_nal_start (): Start writing a nalu.
X264_macroblock_thread_init (): Initializes the rebuild data cache for the stored macro block fdec_buf[] and the encoded data cache fenc_buf[].
X264_slice_header_write (): Output slice header.
X264_fdec_filter_row (): Filter module. The module includes loop filtering, half-pixel interpolation, and Ssim/psnr calculation.
X264_macroblock_cache_load (): The information about the macro block that will be encoded around the macro block is read in.
X264_macroblock_analyse (): Analysis module. The module includes intra-frame prediction mode analysis and inter-frame motion estimation.
X264_macroblock_encode (): Macro block encoding module. The module encodes the macro block by means of DCT transform and quantization of residual error.
X264_macroblock_write_cabac (): Cabac Entropy coding module.
X264_MACROBLOCK_WRITE_CAVLC (): CAVLC entropy coding module.
X264_macroblock_cache_save (): Saves information about the current macro block.
X264_RATECONTROL_MB (): Bit rate control.
X264_nal_end (): End Write a nalu.

Filter ModuleThe function corresponding to the filter module is X264_fdec_filter_row (). This function completes the function of loop filtering, half pixel interpolation, Ssim/psnr calculation. The function calls the following and some of the more important functions:
X264_frame_deblock_row (): de-block effect filter.
X264_frame_filter (): Half-pixel interpolation.
X264_pixel_ssd_wxh (): Psnr calculation.
X264_pixel_ssim_wxh (): Ssim calculation.

Analysis ModuleThe function corresponding to the Analysis module is X264_macroblock_analyse (). The function includes intra-frame prediction mode analysis and inter-frame motion estimation. The function calls the following more important functions (only a few representative functions are listed):
X264_mb_analyse_init (): Analysis module initialization.
X264_mb_analyse_intra (): I macro block intra-frame predictive mode analysis.
X264_macroblock_probe_pskip (): Analyzes if it is skip mode.
x264_mb_analyse_inter_p16x16 (): P16X16 macro block inter-frame prediction mode analysis.
x264_mb_analyse_inter_p8x8 (): P8X8 macro block inter-frame prediction mode analysis.
x264_mb_analyse_inter_p16x8 (): P16X8 macro block inter-frame prediction mode analysis.
x264_mb_analyse_inter_b16x16 (): B16X16 macro block inter-frame prediction mode analysis.
x264_mb_analyse_inter_b8x8 (): B8X8 macro block inter-frame prediction mode analysis.
x264_mb_analyse_inter_b16x8 (): B16X8 macro block inter-frame prediction mode analysis.

macro block encoding moduleThe function corresponding to the macro block encoding module is X264_macroblock_encode (). The module encodes the macro block by means of DCT transform and quantization of residual error. For INTRA16X16 macro blocks, call x264_mb_encode_i16x16 () to encode, and for intra4x4, call x264_mb_encode_i4x4 () to encode. For inter type macro blocks are encoded directly inside the function body.

Entropy Coding ModuleCabac entropy encoding corresponds to the function is X264_macroblock_write_cabac (). CAVLC entropy encoding corresponds to the function is X264_MACROBLOCK_WRITE_CAVLC (). X264_MACROBLOCK_WRITE_CAVLC () calls the following several more important functions:
X264_cavlc_mb_header_i (): Writes the I macro block MB header data. Includes intra-frame prediction mode, etc.
X264_cavlc_mb_header_p (): write P macro block MB header data. Includes MVD, reference frame ordinal, and so on.
X264_cavlc_mb_header_b (): Write B macro block MB header data. Includes MVD, reference frame ordinal, and so on.
X264_cavlc_qp_delta (): Write to QP.
X264_cavlc_block_residual (): Writes the residuals data.

bit Rate Control ModuleThe code-rate control module functions are distributed in different x264 source code, including the following several important functions:
X264_ratecontrol_new () in X264_encoder_open (): Creates a bitrate control.
X264_ratecontrol_start () in X264_encoder_encode (): Start bitrate control.
X264_RATECONTROL_MB () in X264_slice_write (): Bit rate control algorithm.
X264_ratecontrol_end () in X264_encoder_encode (): End code rate control.
X264_ratecontrol_summary () in X264_encoder_close (): bitrate control information.
X264_ratecontrol_delete () in X264_encoder_close (): Releases the bitrate control.


At this point, x264 's source code overview is basically complete, following a few articles in detail to record their internal sources.



Lei Huawei
[Email protected]
http://blog.csdn.net/leixiaohua1020




x264 Source Code Simple analysis: Overview

Related Article

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.