FFmpeg decoding H264 Source code Analysis

Source: Internet
Author: User
Tags versions

http://blog.csdn.net/leixiaohua1020/article/details/44864509

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

A list of source code analysis articles:

"Encode-x264"

x264 Source Code Simple analysis: Overview

x264 Source Code Simple analysis: x264 command line tool (X264.exe)

x264 Source Code Simple analysis: Encoder backbone Part-1

x264 Source Code Simple analysis: Encoder backbone Part-2

x264 Source Code Simple analysis: X264_slice_write ()

x264 Source Code Simple analysis: Filtering (Filter) part

x264 Source Code Simple analysis: Macro block Analysis (analyze) section-intra-frame macro block (Intra)

x264 Source Code Simple analysis: Macro block Analysis (analyze) section-Inter-frame macro block (Inter)

x264 Source Code Simple analysis: Macro block Encoding (ENCODE) section

x264 Source Code Simple analysis: Entropy coding (Entropy Encoding) part

Simple analysis of ffmpeg and libx264 interface source code

"Decode-libavcodec H. A decoder"

Simple analysis of the source code of the H. FFmpeg decoder: An overview

Simple analysis of the source code of the H. FFmpeg Decoder: Parser (Parser) section

Simple analysis of the source code of the H. FFmpeg decoder: The main part of the decoder

Simple analysis of the source code of the H. FFmpeg decoder: Entropy decoding (entropydecoding) part

Simple analysis of the source code of the H. FFmpeg Decoder: Macro block decoding (Decode) part-intra-frame macro block (Intra)

Simple analysis of the source code of the H. FFmpeg Decoder: Macro block Decoding (Decode) section-Inter-frame macro block (Inter)

Simple analysis of the source code of the H. FFmpeg Decoder: Loop Filter section

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

This paper simply records the source code of the Libavcodec (H. Decoder) in FFmpeg. This decoder is very important, it can be said that the FFmpeg project today can almost "monopoly" video audio codec technology, a large part of the contribution from the H. This decoder is powerful, stable performance, on the other hand, the source code is also more complex, difficult to delve into. This article intends to comb the source code structure of this codec to facilitate further study of the use of H. PS: This part of the code is very complex, there are many places are still relatively vague, but also need to learn slowly ...

function Call Graph

The function call diagram of the H. + decoder is shown below.


Click to see a larger image

The meanings of the key markers in the diagram are explained below.


the struct as an interface The structure of the interface between the FFmpeg and the H. 2 decoder is as follows: Ff_h264_parser: The avcodecparser structure used to parse the code stream of H. x. Ff_h264_decoder: The AVCODEC structure used to decode the H-code stream.
The function background color function is shown as a box in the diagram. Different background colors mark the functions of the function: white background functions: ordinary intrinsics. Pink background function: analytic function (Parser). These functions are used to parse information such as SPS, PPS, and so on. function of the purple background: Entropy decoding function (Entropy decoding). These functions read the bitstream data and perform CABAC or CAVLC entropy decoding. Function of the green background: decoding function (Decode). These functions decode compressed data through intra-frame prediction, inter-frame prediction, and DCT inverse transformation. function of the yellow background: loop filter function (loops filter). These functions filter the decoded data to remove the block effect. Blue background function: assembly function (Assembly). These functions are 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.
the arrow line arrow line marks the function's call relationship: Black Arrow Line: an indiscriminate call relationship. Pink Arrow Line: The call relationship between analytic functions (Parser). Purple Arrow Line: The call relationship between the Entropy decoding function (Entropy decoding). Green Arrow Line: The call relationship between decoding functions (Decode).   Yellow Arrow Line: The call relationship between loop filter function (loop filter). the file where the function resides

Each function identifies the file path in which it resides.



several key parts

A few key sections are briefly documented below.


the structure of the interface between the FFmpeg and the H.

The structure of the interface between the FFmpeg and the H. 2 decoder is as follows: Ff_h264_parser and Ff_h264_decoder.Ff_h264_parser
The Ff_h264_parser is a avcodecparser structure used to parse the code stream of H. Several important function pointers are included in the Avcodecparser:
Parser_init (): initializes the parser.
Parser_parse (): Parse.
Parser_close (): Close the parser. In the Ff_h264_parser structure, several of these function pointers point to the following implementation functions, respectively:
Init (): Initializes the H. A parser.
H264_parse (): Parses the H-code stream.
Close (): Closes the H + parser.Ff_h264_decoder
The Ff_h264_decoder is a AVCODEC structure used to decode the H-code stream. Several important function pointers are included in the AVCODEC:
Init (): The initial dissolve code device.
Decode (): Decode.
Close (): Turns off the decoder. In the Ff_h264_decoder structure, several of these function pointers point to the following implementation functions, respectively:
Ff_h264_decode_init (): Initializes the H. A decoder.
H264_decode_frame (): Decodes a stream of H.
H264_decode_end (): Turn off the H + decoder.
Common intrinsic functionsThe normal intrinsic function refers to a function that has not yet been classified in the H. Here are a few examples.
initialization function called by Ff_h264_decode_init () in Ff_h264_decoder:
Ff_h264dsp_init (): Initializes the DSP-related function. Includes IDCT, loop filter function and so on.
Ff_h264qpel_init (): Initializes a One-fourth-pixel motion compensation-related function.
Ff_h264_pred_init (): Initializes intra-frame prediction-related functions.
Ff_h264_decode_extradata (): Resolves extradata in Avcodeccontext. Ff_h264_decoder h264_decode_frame () The function that is called by the layer and decodes slice-related functions:
Decode_nal_units (), Ff_h264_execute_decode_slices (), Decode_slice () and so on. Cleanup function called by H264_decode_end () in Ff_h264_decoder:
Ff_h264_remove_all_refs (): Removes all reference frames.
Ff_h264_free_context (): Frees the allocated memory when initializing the H. A decoder.
Ff_h264_parser h264_parse () Layer-wise and parse-slice-related functions:
H264_find_frame_end (): Finds the end of the Nalu.
Parse_nal_units (): Resolves a nalu.
analytic functions (Parser)The Analytic function (Parser) is used to parse some of the information in the code stream (e.g., SPS, PPS, Slice header, etc.). These parsing functions are called in both parse_nal_units () and Decode_nal_units () to complete the parsing. Here are some examples of analytic functions.
Ff_h264_decode_nal (): Parse Nalu. This function is the precondition of the latter several analytic functions.
Ff_h264_decode_slice_header (): resolves slice header.
Ff_h264_decode_sei (): Parse sei.
Ff_h264_decode_seq_parameter_set (): resolves SPs.
Ff_h264_decode_picture_parameter_set (): Parses pps.
entropy decoding function (Entropy decoding)The Entropy decoding function (Entropy decoding) reads the bitstream data and decodes Cabac or CAVLC entropy. The Cabac decoding function is Ff_h264_decode_mb_cabac (), and the CAVLC decoding function is FF_H264_DECODE_MB_CAVLC (). The entropy decoding function contains a number of functions that read the exponential Columbus encoded data, such as Get_ue_golomb_long (), Get_ue_golomb (), Get_se_golomb (), get_ue_golomb_31 (), and so on.
Cavlc/cabac decoding is required to obtain the residual data. For example, when decoding CAVLC, the decode_residual () function is called, and Decode_residual () invokes the GET_VLC2 () function, GET_VLC2 () calls Open_reader (), Update_cache ( ), GET_VLC (), Close_reader () several functions read data in CAVLC format.
In addition, when acquiring motion vectors, pred_motion () and several similar functions are called to get information about motion vectors.

decoding function (Decode)decoding function (Decode) decodes compressed data through intra-frame prediction, inter-frame prediction, and DCT inverse transformation. The decoding function is FF_H264_HL_DECODE_MB (). Unlike the macro block type, several different functions are called, the most common being called hl_decode_mb_simple_8 ().
The definition of hl_decode_mb_simple_8 () cannot be found directly in the source code because the function name of the actual code is written in the form of a macro (which is later analyzed in detail). The source code for Hl_decode_mb_simple_8 () is actually the HL_DECODE_MB () function of the Func ().
Func (HL_DECODE_MB) () is handled differently depending on the type of macro block: If the macro block type is intra, Hl_decode_mb_predict_luma () is called for intra-frame prediction, and if the macro block type is not intra, Func is called ( hl_motion_422) () or Func (hl_motion_420) () for One-fourth pixel motion compensation.
then Func (HL_DECODE_MB) () calls Hl_decode_mb_idct_luma () and other functions to work on the DCT inverse of the data.

Loop Filter function (loops filter)The Loop filter function filters the decoded data to remove the block effect. The Loop filter function is Loop_filter (). FF_H264_FILTER_MB () and Ff_h264_filter_mb_fast () are called. H264_filter_mb_fast_internal () is also called in Ff_h264_filter_mb_fast (). In H264_filter_mb_fast_internal (), the following functions are called for filtering:
Filter_mb_edgeh (): Brightness level filtering
Filter_mb_edgev (): Brightness vertical Filter
Filter_mb_edgech (): Chroma Horizontal Filter

FILTER_MB_EDGECV (): Chroma Vertical Filter


The Assembler function (Assembly) assembler function (Assembly) is a function of assembly optimization. In order to improve efficiency, the entire H. A decoder (mainly in the decoding section and the Loop Filter section) contains a large number of assembly functions. In the actual decoding process, FFmpeg will invoke the corresponding assembler function (not the C language function) according to the system's characteristics to improve the efficiency of decoding. If the system does not support assembly optimization, FFmpeg will invoke the C language version of the function. For example, when predicting within a frame, there are several versions of the function for the 16x16 Brightness DC mode:
C language version of Pred16x16_dc_8_c ()
Neon version of Ff_pred16x16_dc_neon ()
Mmxext version of Ff_pred16x16_dc_8_mmxext ()
SSE2 version of Ff_pred16x16_dc_8_sse2 ()


Appendix

Find a picture on the Internet (unknown source), the analysis of the ffmpeg of the decoder for each function of the time-consuming situation, compared to the reference value, attached here.


Click to see a clearer picture


As can be seen from the figure, entropy decoding, macro block decoding, loop filtering time-consuming ratios are: 23.64%, 51.85%, 22.22%.



At this point the structure of the FFmpeg decoder is roughly sorted out.

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.