Skip to main content

encode_decode/
encode_decode.rs

1//! Software round trip for AV1 and VP9: `cargo run --release --example encode_decode`.
2//!
3//! Generates screen-like BGRA pictures, converts them to I420, encodes them with the software
4//! encoder, decodes the stream again and checks that every picture comes back. It then shows
5//! [`open_best_encoder`] with the default order and with a custom order. Needs no GPU.
6
7use hwcodec::{
8    Codec, EncoderConfig, EncoderKind, HardwareEncoder, I420Frame, convert, default_order,
9    open_best_encoder, open_software_decoder,
10};
11
12const WIDTH: u32 = 640;
13const HEIGHT: u32 = 360;
14const FPS: u32 = 30;
15const BITRATE_KBPS: u32 = 800;
16const FRAMES: u32 = 60;
17/// Below this the decoded picture no longer looks like the source.
18const MIN_PSNR_DB: f64 = 30.0;
19
20type AnyResult<T> = Result<T, Box<dyn std::error::Error>>;
21
22fn main() -> AnyResult<()> {
23    let config = EncoderConfig::new(WIDTH, HEIGHT, FPS, BITRATE_KBPS);
24    for (name, codec) in [("AV1", Codec::Av1), ("VP9", Codec::Vp9)] {
25        round_trip(name, codec, &config)?;
26    }
27
28    println!();
29    for (name, codec) in [("AV1", Codec::Av1), ("VP9", Codec::Vp9)] {
30        // The default order: the hardware encoders of this operating system, then software.
31        let order: Vec<String> = default_order(codec)
32            .iter()
33            .map(ToString::to_string)
34            .collect();
35        let (kind, _encoder) = open_best_encoder(codec, &config, None)?;
36        println!("{name} default order [{}] opened: {kind}", order.join(", "));
37
38        // A custom order: software first, so hardware is never tried.
39        let software_first = [
40            EncoderKind::Software,
41            EncoderKind::Hardware(HardwareEncoder::Vaapi),
42            EncoderKind::Hardware(HardwareEncoder::Nvenc),
43        ];
44        let (kind, _encoder) = open_best_encoder(codec, &config, Some(&software_first))?;
45        println!("{name} software-first order opened: {kind}");
46    }
47    Ok(())
48}
49
50/// Encodes [`FRAMES`] pictures in software, decodes them and compares them with the input.
51fn round_trip(name: &str, codec: Codec, config: &EncoderConfig) -> AnyResult<()> {
52    let (_, mut encoder) = open_best_encoder(codec, config, Some(&[EncoderKind::Software]))?;
53    let mut decoder = open_software_decoder(codec)?;
54    let mut bytes = 0;
55    let mut decoded_frames = 0;
56    let mut worst_psnr = f64::INFINITY;
57    for index in 0..FRAMES {
58        let bgra = screen_like_bgra(WIDTH, HEIGHT, index);
59        let frame = convert::bgra_to_i420(&bgra, WIDTH, HEIGHT, WIDTH, HEIGHT)
60            .ok_or("the picture does not fit the encoder size")?;
61        let pts = i64::from(index * 1000 / FPS);
62        for packet in encoder.encode(&frame, pts, false)? {
63            bytes += packet.data.len();
64            let Some(decoded) = decoder.decode(&packet.data)? else {
65                continue;
66            };
67            if (decoded.width(), decoded.height()) != (frame.width(), frame.height()) {
68                return Err(format!(
69                    "{name}: decoded {}x{} instead of {}x{}",
70                    decoded.width(),
71                    decoded.height(),
72                    frame.width(),
73                    frame.height()
74                )
75                .into());
76            }
77            decoded_frames += 1;
78            // The first pictures are still sharpening while the rate control settles.
79            if index >= FRAMES / 2 {
80                worst_psnr = worst_psnr.min(luma_psnr(&frame, &decoded));
81            }
82        }
83    }
84    if decoded_frames != FRAMES {
85        return Err(format!("{name}: {decoded_frames} of {FRAMES} pictures came back").into());
86    }
87    if worst_psnr < MIN_PSNR_DB {
88        return Err(format!("{name}: picture quality too low ({worst_psnr:.1} dB)").into());
89    }
90    println!(
91        "{name}: {FRAMES} pictures {WIDTH}x{HEIGHT} encoded to {} kB and decoded again, \
92         luma PSNR at least {worst_psnr:.1} dB – ok",
93        bytes / 1000
94    );
95    Ok(())
96}
97
98/// A packed BGRA picture: a colour gradient with a dark block moving across it.
99fn screen_like_bgra(width: u32, height: u32, index: u32) -> Vec<u8> {
100    let (w, h) = (width as usize, height as usize);
101    let block_x = (index as usize * 6) % w;
102    let mut bgra = Vec::with_capacity(w * h * 4);
103    for y in 0..h {
104        for x in 0..w {
105            let in_block = x >= block_x && x < block_x + 80 && y >= h / 3 && y < h / 3 + 60;
106            if in_block {
107                bgra.extend_from_slice(&[30, 30, 30, 255]);
108            } else {
109                let blue = (x * 255 / w) as u8;
110                let green = (y * 255 / h) as u8;
111                bgra.extend_from_slice(&[blue, green, 160, 255]);
112            }
113        }
114    }
115    bgra
116}
117
118/// Peak signal-to-noise ratio of the brightness plane, in dB; higher is closer to the source.
119fn luma_psnr(source: &I420Frame, decoded: &I420Frame) -> f64 {
120    let squared_error: f64 = source
121        .y()
122        .iter()
123        .zip(decoded.y())
124        .map(|(a, b)| (f64::from(*a) - f64::from(*b)).powi(2))
125        .sum();
126    let mean = squared_error / source.y().len() as f64;
127    if mean == 0.0 {
128        return f64::INFINITY;
129    }
130    10.0 * (255.0f64.powi(2) / mean).log10()
131}