Study on x264 encoding latency

Source: Internet
Author: User
The latency of x264 encoding is studied.
The method is to add log in x264.c
Static int encode (x264_param_t * Param, cli_opt_t * OPT)
{
...
I _frame_size = encode_frame (H, opt-> Hout, & pic, & last_dts );
If (I _frame_size = 0) // delay Frames
Fprintf (stderr, "output zero % d \ n", I _frame );
...
}

After statistics, we found that the number of x264 encoded delayed frames conforms to the following formula.
H-> frames. I _delay =
Param-> I _sync_lookahead + // number of forward considering Frames
Max (param-> I _bframe, // number of B frames
Param-> RC. I _lookahead) + // Bit Rate Control forward considering the number of frames
Param-> I _threads-1. // number of parallel encoding Frames

There are two types of latency:
1. Pre-encoding latency (the current frame is not encoded and more buffer frames are required before encoding starts ).
This type of delay exit is in the encoder. C, x1__encoder_encode Function
...
If (H-> frames. I _input <= H-> frames. I _delay + 1-H-> I _thread_frames)
{
/* Nothing yet to encode, waiting for filling of buffers */
Pic_out-> I _type = x264_type_auto;
Return 0;
}
...
I _sync_lookahead, I _bframe, and RC. I _lookahead all affect the delay.

2. latency After encoding (the current frame has been encoded, but the subsequent frame has not been encoded, so you have to exit first ).
This type of delay exit is in the encoder. C, x1__encoder_frame_end Function
If (! H-> out. I _nal)
{
Pic_out-> I _type = x264_type_auto;
Return 0;
}
This delay is caused by x264 parallel frame encoding.
X264 parallel frame encoding processes a frame group (I _threads parallel frame processing) and then processes the next frame group.

According to the formula, we can see the method to reduce the frame latency, that is, (zerolatency setting)
Param-> RC. I _lookahead = 0;
Param-> I _sync_lookahead = 0;
Param-> I _bframe = 0;
Param-> B _sliced_threads = 1;
Param-> B _vfr_input = 0;
Param-> RC. B _mb_tree = 0;

Set H-> frames. I _delay = 0. However, the setting of param-> B _sliced_threads = 1 is doubtful.
When B _sliced_threads = 1, x264 discard frame parallel encoding, which will inevitably affect the encoding speed.
You can set B _sliced_threads to 0, and set zerolatency for others.
In this case, H-> frames. I _delay = param-> I _threads-1.
X264 automatically calculates I _threads Based on CPU, which is generally 6/8. The frame group latency is about 1/3-1/2 seconds.
Check whether the requirements of the specific system are met.

In this way, the write of x264 parallel frame encoding is not very adaptive.
If I _threads can increase with the increase of the number of input frames in the initial encoding phase, the encoding group latency can be completely solved.
Your personal thoughts are not verified. please correct me.

Study on x264 encoding latency

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.