Detailed description of h264 Encoding

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_6c7b28cd0100tupj.html

Original article address:H264 Code details (transfer) Author:Sleepless night

(1)     X1__param_default (x1__param_t * PARAM) 

Purpose: set the parameters of the encoder.

                

CQM: quantize table information

                CSP:           

                Memset (param-> cqm_4iy, 16, 16) in quantified table information );

                                    Memset (param-> cqm_4ic,
16, 16 );

                                    Memset (param-> cqm_4py,
16, 16 );

                                    Memset (param-> cqm_4pc,
16, 16 );

                                    Memset (param-> cqm_8iy,
16, 64 );

                                    Memset (param-> cqm_8py,
16, 64 );

(2) staticint Parse (INT argc, char ** argv, x1__param_t * Param, cli_opt_t * OPT)Initialization

1.   Getopt_long (nargc, nargv, options, long_options, idx)Obtain the vector and method selection of the entry address.

2.   Getopt_internal (nargc, nargv, options)   Resolution entry address Vector

(3)
Staticint Encode (x264_param_t * Param, cli_opt_t * OPT)

H-> param = Param

The VUI information mainly includes frame rate, image size, and other information.

 

X264_sps_init (H-> SPS, 0, & H-> param );

X__pps_init (H-> PPS, 0, & H-> param, H-> SPS );

    Initialize and open up frame Space

  Save the information of the previous macro block because it is initialized. as a reference for the first macro block, there will be x1__macroblock_cache_load (H, I _mb_x, I _mb_y ); it is the value of the macro block around the macro block to be encoded. To get the predicted value of the current block, you must first know the predicted value on the left.

 

  Initialize CPU parameter settings for various chunks

 

        1. X1__t * x1__encoder_open (X1__param_t * PARAM)This function is used to modify incorrect parameters and initialize the struct parameters and cabac encoding and prediction parameters.

        2. p_read_frame (& pic, opt-> Hin, I _frame + opt-> I _seek, param-> I _width, param-> I _height)

 Read a frame and set this frame to Prev

3. I _file + = encode_frame (H, opt-> Hout, & pic); enter the core code Layer

General flowchart of the core coding layer: (x264.c)

1.       X1__encoder_encode (H, & Nal, & I _nal, PIC, & pic_out) encodes frames.

2.       I _size = x1__nal_encode (data, & I _data, 1, & nal [I])

Network packaging code

3.       I _file + = p_write_nalu (Hout, Data, I _size)

Write the network package to the output file.

4.       Returns and encodes the next frame.

  The following is a detailed flowchart:

I. Detailed flowchart in the frame:

1.         X1__encoder_encode (H, & Nal, & I _nal, PIC, & pic_out) encodes frames.

1.   

    X1__frame_t * fenc = x1__frame_get (H-> frames. Unused);

X1__frame_copy_picture (H, fenc, pic_in );

Fenc-> I _frame = H-> frames. I _input ++;

X1__frame_put (H-> frames. Next, fenc );

X1__frame_init_lowres (H-> param. CPU, fenc); // It contains low-pixel extensions, many for loops, which should be the extension of head computing and semi-precision pixels.

2.           Pai_slicetype_decide (h); the determination of the slice type should also look at it

3.           While (is_x1__type_ B (H-> frames. Next [bframes]-> I _type ))

Bframes ++;

X1__frame_put (H-> frames. current, x1__frame_get (& H-> frames. next [bframes]). This is mainly because frame B must be encoded only after the end of the non-B-frame encoding. Therefore, a series of frames B that are not edited are stored in the queue, encoding is performed only after non-B frames are taken out, and then the previous B frame encoding is performed.

Do_encode:

4.           Create list0 & list1.

      X264_reference_build_list (H, H-> fdec-> I _poc, I _slice_type );

      Bit Rate Control Initialization

X264_ratecontrol_start (H, I _slice_type, H-> fenc-> I _qpplus1 );

  

5. Create slice header data

X264_slice_init (H, I _nal_type, I _slice_type, I _global_qp );

6   I _frame_size = x264_slices_write (h); this is the key to encoding.

1.     X264_slice_header_write (& H-> out. BS, & H-> Sh, H-> I _nal_ref_idc );

2.     Some initialization work

3.     For (mb_xy = H-> Sh. I _first_mb, I _skip = 0; mb_xy X264_slices_write () function

4.     X1__macroblock_cache_load (H, I _mb_x, I _mb_y); it is the value of the macro block around the macro block to be encoded. To get the predicted value of the current block, you must first know it, predicted value on the left!

5.     * ***** X264_macroblock_analyse (h); key points. I used a series of sad to calculate the optimization scheme. For example, I split the 16x16 macro block into 16 4x4 to calculate the sad and compare it with the original 16x16 sad, perform detailed analysis at the following layer.

A. X264_mb_analyse_intra (H, & analysis, cost_max); I feel like a 16 × 16 sad, 4 8 × 8 sad and, 16 4× 4sad and select the optimal mode. Maybe I do not understand it. x1__mb_encode_i4x4 (H, idx, A-> I _qp) IN THE x1__mb_encode_i4x4 ); I can't find any problem with tracking several functions in i8 × 8. I need to take a closer look (now I can follow it again)

It seems that if the DC component is used here, quantitative Zigzag scanning will be performed. You don't have to wait until x1__macroblock_encode (h) completes.

B. X264_analyse_update_cache (H, & analysis); you have calculated the selected color block mode. It seems that there are also updated information for the next prediction as a reference.

6.     X264_macroblock_encode (h );

A. Judge the type of Macro Block

B. Perform DCT, quantization, and zigzag based on the judgment type, and record the current mode as the next encoding Macro Block (sub-Macro Block) for reference.

The implementation of zigzag is not clear (the original zigzag has a macro definition, and now I understand it). The process of anti-quantization and IDCT cannot be followed. It should be a compilation! The function is as follows: (x1__mb_encode_i4x4 (H, I, I _qp );)

X264_mb_dequant_4x4 (dct4x4, H-> dequant4_mf [cqm_4iy], I _qscale );

H-> dctf. add4x4_idct (p_dst, I _stride, dct4x4 );

Also, this function cannot be tracked. It should be the reverse transformation of the reconstructed image.

H-> dctf. add4x4_idct (p_dst, I _stride, dct4x4 );

H-> MB. cache. intra4x4_pred_mode [x1__scan8 [I] = x1__mb_pred_mode4x4_fix (I _mode); how does this value change according to the previous pattern? It may be because the above two functions failed to be updated, so fuzzy

C.   Encode the color block. The QP is limited to 0-51 and the prediction mode is selected (the total value of DC is 128)

X1__mb_encode_8x8_chroma (H ,! Is_intra (H-> MB. I _type), I _qp); the two color signals are encoded separately, which is similar to the Brightness Signal.

D. We do not know how to obtain the white light and color! It is clear now that each bit indicates whether all the sub-blocks are 0, but not all of them are clear yet. 0x02 indicates that the sub-blocks in the color block are AC, and dc0x01 indicates that only DC is available,

E. Determine if Skip is required or not by using the white list. It also involves vector prediction. It will take a good look tomorrow. Where

H-> MB. QP [H-> MB. I _mb_xy] = H-> MB. I _last_qp; this is the next read   Save the QP. Otherwise, the decoder cannot read the next one. Qp,

There are still some problems with the understanding of the service. The meaning of the 8-bit representation of the service is not very clear. It is about the encoding of the DC and AC. 185 page introduction (New Generation of video compression standard Bi Houjie)

7.     Select cabac or cavlc

The principle of cabac is not carefully understood.

8.   X1__macroblock_cache_save (h); save it as a reference for the next prediction.

9.   Some final work to prepare for the next Macro Block (look rough)

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.