x264 rate control

Craig picture Craig · Aug 31, 2012 · Viewed 7.1k times · Source

We are using the x264 encoder in a video conferencing project, we have the basic streaming video working, however, we are having trouble understanding how the various rate control settings determine the final bitrate. We set the following params:

x264_param_t params;
x264_param_default_preset(&params, "ultrafast", "zerolatency");
params.i_threads = 1;
params.i_width = width;
params.i_height = height;
params.i_fps_num = fps;
params.i_keyint_max = fps;
params.b_intra_refresh = 1;
params.b_repeat_headers = 1;
params.b_annexb = 1;

//Set rate control stuff here

x264_param_apply_profile(&params, "baseline");

If we only set the params.rc.i_bitrate param, the encoder seems to massively overshoot the bitrate. If we set the i_vbv_max_bitrate & i_vbv_buffer_size params we see a bitrate which peaks (and sometimes overshoots) the i_vbv_max_bitrate setting. Obviously having tight control over the biterate is important for video conferencing, but the documentation is kind of opaque. Is anybody else using x264 for video conferencing? How are you setting the encoder? Any help appreciated, thanks in advance.

Answer

av501 picture av501 · Aug 31, 2012

If you are in low delay video conferencing mode for minimal jitter and reliable performance you need to be in CBR mode and not VBR mode. It is the VBR mode that gives the shoot ups and peaks that a live low latency system cannot deal with.

There are specialized vbr algorithms for low latency video communication / surviellance applications but x264 does not have that. So don't use its vbr. its vbr is meant for storage.

There is a --nal-hrd cbr to enable cbr

There is no way you can guarantee that bitrate won't shoot up beyond a point because things are statistical. However you can keep it under control for 99% of the time as long as some assumptions are met.

Your vbv buffer size needs to be the smallest size you can deal from a quality perspective with for two purposes 1. Not allowing too much variation in bitrate 2. Reducing end to end delay This is the buffering the encoder assumes is available at the decoder side. The smaller it is worse the quality. Find the smallest value you can deal with.

The vbv max bitrate needs to be set to the cbr target bitrate value. This is the guideline to rc that this is the max instantaneous bitrate you are allowed. Set it to the target bitrate. Remember its a guideline. Because of statistical nature of video, it can be overshot.

Ensure you have intra refresh and therefore I pictures are turned off.

Ultra fast is probably not a good preset to use without overriding some of the settings. Set your reference pictures, b-pictures, profile explicitly. Set your me and subme to higher levels. [highest level if I were you]. This is one reason you are getting a shoot up. The encoder is not able to do a good job because of this. When encoder has better me and subme it requires lesser bits to encode the hard parts where the shootup happens! Generally people who don't understand video will not get this. They keep asking why does me/subme matter for bitrate and shootups. Trust me it does. How much shoot up you get when the hard parts come for encoding is totally dependent on the algorithms of the encoder and me is one of the critical ones.

See what profile you need to work at. Cabac gives upto 20% improvement. So if you can do main profile, do it. use only 1 reference picture and no B pictures. But there is absolutely no harm in using cabac for the bitrate advantage if you can use it.

EDIT: In general, there is no magic pill. The above is advice gained from years of working in multimedia and understanding video. However you still have to play around a bit to find optimal settings between quality/latency and other constraints that are specific to your system. Ensure you understand the terms before you tweak them else you will be running around in circles forever. Also remember video is statistical. Nothing will be perfect 100% all the time.