Per-speed rdo-lookahead-frames optimization
Tags: rav1e, tuning, opensource
Published: 2021-03-03 08:21:42

This is the third story about how we made rav1e up to 2x fast in 3 weeks. For the first one go [here](https://dev.to/barrbrain/video-encoder-rollback-optimization-in-rav1e-4d5k) for the second go [here](https://dev.to/luzero/temporal-rdo-update-optimization-2pf1).


## Speed levels

**rav1e** have 11 speed levels, 0 to 10, each tuned to give reasonable quality to speed trade-off for each use-case.

Previously, by default, all speed levels had `--rdo-lookahed-frames` set to 40, which at the time was a reasonable decision as it gave the encoder plenty of lookahead to make coding decision on.

In recent benchmarking of encoder settings, we discovered that the high and constant value of lookahead is excessive for higher speeds. Relative time spends on rdo lookahead increase with increasing encoding speed preset and gains decrease.

## Speed, Quality, BD-rate

In changing encoder presets we need to balance 2 things: Speed Gain and Quality Decrease.

As speed gain, we took the average difference of encoding time between baseline and different `--rdo-lookahed-frames` settings, in a range from 30 to 10.

The quality difference was measured by BD-rate (Bjontegaard rate). It allows measurement of bitrate difference between codecs or features of codec at the same quality, for used metric. The rate change is computed as the average percent difference in rate over a range of qualities.

In simple terms, how much more bits encoder needs to achieve the same quality, on average. 
- **Negative** values of BD-rate: bit-rate saves, **better**
- **Positive** values do BD-rate: bit-rate expenses, **worse**

## Per-speed level rdo lookahead

This graph shows what average speed difference is expected by changing `--rdo-lookahed-frames` from default 40.
![image alt](https://camo.githubusercontent.com/332e03cd46b1bd8676ec57b1b523f835f8cc6153d522397dc061bc85e21dff13/68747470733a2f2f63646e2e646973636f72646170702e636f6d2f6174746163686d656e74732f3639363834393937343636363938353439342f3831313633393235363037373639373037342f323032315f30325f31375f31385f34345f33302e77656270)
As we can see, the biggest change occurs at speed 10, around 12% for `--rdo-lookahed-frames` 10, after that speed up is steady for speeds 9 to 5 and after that decrease for speeds 4 to 1.

Now, let's look at the quality trade-off.

![image alt](https://camo.githubusercontent.com/0dadf99588bb6d5e34986a2a3317ff6b9c047ea826336433a7a216a25e617c6e/68747470733a2f2f63646e2e646973636f72646170702e636f6d2f6174746163686d656e74732f3639363834393937343636363938353439342f3831313633383537373333303931333332302f323032315f30325f31375f31375f32335f30302e77656270)

As we can see, speeds 10 and 9 have the lowest impact on quality, for speed 9 data even show that `--rdo-lookahed-frames 30` have a negative BD-rate, which is of course just noise of data. Given this, it's reasonable to set `--rdo-lookahed-frames 10` for speeds 10 and 9.

After speed 9 BD-rate difference increase, especially for the lowest value of 10, and ~1% of BD-rate difference is a sensible trade-off, we found it reasonable to set `--rdo-lookahed-frames 20` for speeds 8,7,6. Speeds lower than 6 are high-quality presets and it's important to make them faster but also make them good, so we decided to set `--rdo-lookahed-frames 30` for speeds 5,4,3 as it gives small speed up with the marginal difference in BD-rate, and left the highest quality presets unchanged.

Final values:
For speeds 10,9  -- `--rdo-lookahed-frames 10`
For speeds 8,7,6 - `--rdo-lookahed-frames 20`
For speeds 5,4,3 - `--rdo-lookahed-frames 30`
For speeds 2,1,0 - `--rdo-lookahed-frames 40`