Configure Constant Quality Mode in librav1e

Constant Quality (CQ) encoding in the AV1 encoder librav1e allows you to maintain a consistent level of visual fidelity throughout a video by dynamically adjusting the bitrate. This article provides a straightforward guide on how to configure constant quality encoding in librav1e using both its command-line interface (rav1e) and its native programming API.

Using the Command Line Interface (rav1e)

To configure constant quality encoding in the command-line tool rav1e, you use the --quantizer flag (or the shorthand -q).

The quantizer parameter accepts an integer value ranging from 0 to 255: * 0: Lossless quality (largest file size). * 255: Lowest quality (smallest file size).

A lower quantizer value results in higher visual quality and a higher bitrate, while a higher value increases compression at the cost of visual fidelity.

Example Command

rav1e input.y4m -o output.ivf --quantizer 100

In this example, the quantizer is set to 100, which is a common sweet spot for balancing visual quality and file size in high-definition video.

Configuring Constant Quality in the Rust API (librav1e)

If you are integrating librav1e directly into a Rust application, you configure constant quality via the Config struct.

The quantizer field resides inside the encoder configuration (enc). You can programmatically set the quantizer using the following approach:

use rav1e::prelude::*;

fn main() {
    // Create a default configuration
    let mut cfg = Config::default();

    // Set the width, height, and timebase of your video
    cfg.enc.width = 1920;
    cfg.enc.height = 1080;
    cfg.enc.time_base = Rational::new(1, 30);

    // Configure the constant quality level (quantizer)
    cfg.enc.quantizer = 100; 

    // Build the encoder context
    let mut ctx: Context<u16> = cfg.new_context().unwrap();
}

By manually defining cfg.enc.quantizer, you instruct the encoder to lock into Constant Quantizer mode, which bypasses target bitrate constraints to focus entirely on maintaining visual consistency.