X264 encoded h264

Source: Internet
Author: User

All content in this article is original. For more information, see the source!

Preface

Some time ago, we have been engaged in video coding and decoding and transmission. Now, basically the whole system can implement the function of real-time server encoding packaging and transmission to the client, and the client can be decoded and displayed in real time, so I will record the work in the previous stage and use x264 for H. 264 the process of encoding, the process of transmitting h264 data packets using jrtplib, and the process of decoding using FFMPEG are recorded. On the one hand, I will make a summary, on the other hand, I want to help those who do the same job with me get familiar with the content faster.

Next, I will first introduce the H.264 video encoding for this library. In my blog, I have already introduced howCompile the x264 library in WindowsSo I will not describe it here. In fact, I personally think it is meaningless to compile this library in windows, but it is a waste of time, because even if a library can be compiled in windows, I still cannot check the execution of the program in the database (or I may not be able to find this method, but I still cannot follow up the method in my blog ). If you can directly find a compiled library on the Internet for direct use, it is the best, so I will upload my compiled library to my GitHub, please stamp the requiredHere(It also contains the compiled FFMPEG library ).

When I used this library for video encoding, I found that there were very few resources available on the Internet, and x264 did not provide the corresponding API documentation, the x264 study notes I found on the Internet also made me very depressed. After a long time, I did not know what to write. For me, I only need to know how to use this library, so I compiled x264 in Linux and followed up on x264.c. Finally, I understood the x264 encoding process, although some of the parameters are completely unknown.

H264 basic knowledge

You want to use x264 for H. 264 of the video encoding, first need to H. I have some knowledge about encoding 264. Next I will first introduce H. some basic knowledge about encoding 264. For more information, see Bi Houjie's new generation video compression coding standard.

First, we must know that h264 supports three different grades:

1. Basic grade: it is mainly used for "video sessions", such as conference and television, videophone, telemedicine, and remote teaching;

2. Scalability: it is mainly used for network video streams, such as on-demand videos;

3. Major grades: it is mainly used for consumer electronic applications, such as digital television broadcasting and digital video storage.

These three different levels lead to differences in the encoding functions used during the encoding process, specifically as follows:

1) basic Grade: I-and p-slices support intra-and inter-frame encoding and context-based adaptive variable-length encoding (cavlc ). It is mainly used for real-time video communication, such as videophone, conference TV, and wireless communication;

2) Major grades: supports interlace videos, B-bit inter-frame encoding and weighted prediction in-frame encoding, and context-based adaptive arithmetic coding (cabac ). It is mainly used for digital broadcast and television and digital video storage;

3) extended grade: supports effective switching between code streams (SP and Si slices) and improved error code performance (data segmentation), but does not support line-by-line video and cabac.

The definitions of I, P, B, SP, AND Si mentioned above are as follows: an encoded image is usually divided into several macro blocks, A macro block consists of a 16x16 brightness pixel and an 8x8 CB appended with an 8x8 Cr Color Pixel block. In each image, several macro blocks are arranged into slices.

The I-part only contains the I macro block. The P-part can contain the P and I macro blocks, and the B-part can contain the B and I macro blocks.

I Macro Block uses decoded pixels from the current video as a reference for intra-Frame Prediction (decoded pixels in other films cannot be used as a reference for intra-Frame Prediction ).

P Macro Block uses the previously encoded image as the reference image for intra-Frame Prediction. The macro block encoded in one frame can be further divided into macro blocks: that is, 16x16, 16x8, 8x16, or 8x8 brightness pixel blocks (and included color pixels). If you select an 8x8 sub-macro block, it can be further divided into various sub-macro blocks. The size is 8x8, 8x4, 4x8, or 4x4 brightness pixel blocks (and included color pixels ).

The B Macro Block uses two-way reference images (current and future encoded image frames) for intra-frame prediction.

Now we can easily understand the encoding functions available for each grade. For example, in the video session system, we can only get the current frame or the frame before the current frame, so we can only use the basic level.

Then, we need to know the data format of h264 encoding:

First, we must know that the input of h264 encoding is in yuv420 format.Here). For rgb32 images, we must first convert them to the yuv420 format.

Then, we need to know that the h264 function is divided into two layers: the video encoding layer (VCL, video coding layer) and the network extraction layer (Nal, Network encoding action Layer ). VCL data is the output of encoding. It indicates the video data sequence after compression and encoding. Before VCL data is transmitted or stored, the encoded VCL data is first mapped or encapsulated into the nal unit. Each nal unit includes an original byte sequence load (rbsp) and a set of NAL header information corresponding to the video encoding data. The structure of the nal unit sequence is shown in Figure 1.

Figure 1. nal unit Sequence Structure

The Nal header is 0x0000 00 01 or 0x00 00 01.

Knowing this, we can start to use x264 for h264 encoding.

 

Video with x264 Encoding

 

As for how to use vs to create a project and contain library files, I will not mention it here. After searching, there will be a lot of files. The following describes the Encoding Process for using this library. The main steps are described in the annotations.

Int main (INT argc, char ** argv) {int iresult = 0; x1__t * px1_handle = NULL; x1__param_t * px1_param = new x1__param_t; Assert (px1_param ); // * configure the parameter // * use the default parameter. Here, due to my real-time network transmission, I use the zerolatency option. After using this option, no delayed_frames exists, if this is not the case, you also need to get the cached Encoding Frame x1__param_default_preset (px1_param, "veryfast", "zerolatency") after the encoding is completed "); // * cpuflags px1_param-> I _threads = x1__sync_lookahead_auto; // * obtain the empty buffer and continue to use it. Lock guarantee. // * video options px1_param-> I _width = 352; // * the image width to be encoded. px1_param-> I _height = 288; // * the height of the image to be encoded px1_param-> I _frame_total = 0; // * The total number of frames encoded. do not know to use 0. px1_param-> I _keyint_max = 10; // * stream parameter px1_param-> I _bframe = 5; px1_param-> B _open_gop = 0; px1_param-> scheme = 0; px1_param-> scheme = cost; // * log parameter. If you do not need to print the encoding information, comment out px1_param-> I _log_level = x1__log_debug ;//* Speed control parameters px1_param-> RC. I _bitrate = 1024*10; // * bit rate (bit rate, unit: Kbps) // * muxing parameters px1_param-> I _fps_den = 1; // * Frame Rate denominator px1_param-> bytes = 10; // * Frame Rate molecule px1_param-> I _timebase_den = px1_param-> I _fps_num; px1_param-> I _timebase_num = px1_param-> bytes; // * set the profile. use baseline profile x264_param_apply_profile (px1_param, x1__profile_names [0]); // * encode the required auxiliary variable inal = 0; pnals = NULL; x264_pi Cture_t * ppicin = new bytes; x264_picture_t * ppicout = new bytes; x264_picture_init (ppicout); bytes (ppicin, bytes, px1_param-> I _width, px1_param-> I _height ); ppicin-> IMG. I _csp = x1__csp_i420; ppicin-> IMG. I _plane = 3; // * Open the encoder handle and use x1__encoder_parameters to obtain the parameter set to x264. update px1_handle = x1__encoder_open (px1_param); Assert (PX 264 handle); // * create a file for storing the encoded data file * pfile = fopen ("test.264", "WB"); Assert (pfile ); // set the y4m file parameter y4m_input_t * y4m_hnd = (y4m_input_t *) malloc (sizeof (bytes); // open the y4m file iresult = open_file_y4m ("benchmark. y4m ", (hnd_t **) & y4m_hnd, px1_param); If (iresult <0) {printf (" failed to open file! \ N "); Return 0;} // get the total number of frames of the file. Int nframes =: get_frame_total_y4m (hnd_t *) y4m_hnd); // start encoding for (INT I = 0; I <nframes; I ++) {// read a frame of read_frame_y4m (ppicin, (hnd_t *) y4m_hnd, I); if (I = 0) ppicin-> I _pts = I; elseppicin-> I _pts = I-1; // encode int frame_size = x1__encoder_encode (px1_handle, & pnals, & Inal, ppicin, ppicout ); if (frame_size> 0) {for (INT I = 0; I <inal; ++ I) {// write the encoded data to the file. fwrite (pnals [I]. p_payload, 1, pnals [I]. I _payload, pfile) ;}}// * clear the image area x264_picture_clean (ppicin); // * close the encoder_close (px1_handle); px1_handle = NULL; Delete ppicin; ppicin = NULL; Delete ppicout; ppicout = NULL; Delete px1_param; px1_param = NULL; return 0 ;}

For the benchmark. y4m and y4m. H files used here, stampHereDownload. In this way, the stored test.264 is the encoded file. You can use FFMPEG to decode it into MP4 and MKV so that it can be played by mainstream players.

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.