A key-frame quantizer offset for the coding pyramid
Browse files at this commitf7ddd74bba0f816255a240f37fa606c164c78826Parent: 5c50b7f- feat
- a key-frame quantizer offset for the coding pyramid
5 changed files +72 -3
- crates/ec-av1/src/encoder.rs +47 -3
- lanes/keyq-arm.sh +9 -0
- lanes/keyq.sweep.txt +4 -0
- lanes/keyq/ctl.log +6 -0
- lanes/keyq/k32.log +6 -0
crates/ec-av1/src/encoder.rsmodified+47 -3
| @@ -388,6 +388,13 @@ pub struct Pyramid { | |||
| 388 | /// nearest good reference. Ignored by a group of fewer than four | 388 | /// nearest good reference. Ignored by a group of fewer than four |
| 389 | /// pictures, where there is no leaf on both sides of a midpoint. | 389 | /// pictures, where there is no leaf on both sides of a midpoint. |
| 390 | pub mid_q_offset: Option<i16>, | 390 | pub mid_q_offset: Option<i16>, |
| 391 | + /// Added to `base_q_idx` for the GOP's KEY FRAME — negative, since every | ||
| 392 | + /// frame of the run reads it, directly or through an ARF. The 48-frame | ||
| 393 | + /// census (`lanes/census-longgop.md`) found ours coded at base q while | ||
| 394 | + /// its own ARFs sat 32 steps finer, where rav1e (key 100 / arf 120 / | ||
| 395 | + /// leaf 153) and libaom (63 / 95 / 163) both make the key the best frame | ||
| 396 | + /// of the run and spend 22--37% of the stream on it against our 10--11%. | ||
| 397 | + pub key_q_offset: i16, | ||
| 391 | } | 398 | } |
| 392 | 399 | ||
| 393 | /// How many pictures the mini-GOP now being collected takes, given how many | 400 | /// How many pictures the mini-GOP now being collected takes, given how many |
| @@ -425,7 +432,7 @@ fn absorb_tail() -> bool { | |||
| 425 | 432 | ||
| 426 | impl Pyramid { | 433 | impl Pyramid { |
| 427 | /// The pyramid a run asks for through the environment: | 434 | /// The pyramid a run asks for through the environment: |
| 428 | - /// `EC_AV1_PYRAMID=<mini_gop>[:<arf_q_offset>:<leaf_q_offset>[:<mid>]]`, | 435 | + /// `EC_AV1_PYRAMID=<mini_gop>[:<arf_q_offset>:<leaf_q_offset>[:<mid>[:<key>]]]`, |
| 429 | /// where `<mid>` is the third level's offset or `off` for none, with | 436 | /// where `<mid>` is the third level's offset or `off` for none, with |
| 430 | /// `0` (or anything that is not a mini-GOP of at least 2, e.g. `flat`) | 437 | /// `0` (or anything that is not a mini-GOP of at least 2, e.g. `flat`) |
| 431 | /// meaning the flat one-picture-one-frame path. UNSET is | 438 | /// meaning the flat one-picture-one-frame path. UNSET is |
| @@ -455,6 +462,8 @@ impl Pyramid { | |||
| 455 | Some("off") => None, | 462 | Some("off") => None, |
| 456 | Some(v) => v.parse().ok().or(d.mid_q_offset), | 463 | Some(v) => v.parse().ok().or(d.mid_q_offset), |
| 457 | }, | 464 | }, |
| 465 | + // A fifth field is the key frame's own offset. | ||
| 466 | + key_q_offset: f.next().and_then(|v| v.parse().ok()).unwrap_or(d.key_q_offset), | ||
| 458 | }) | 467 | }) |
| 459 | } | 468 | } |
| 460 | } | 469 | } |
| @@ -714,6 +723,7 @@ impl Default for Pyramid { | |||
| 714 | arf_q_offset: -32, | 723 | arf_q_offset: -32, |
| 715 | leaf_q_offset: 16, | 724 | leaf_q_offset: 16, |
| 716 | mid_q_offset: MID_DEFAULT, | 725 | mid_q_offset: MID_DEFAULT, |
| 726 | + key_q_offset: KEY_DEFAULT, | ||
| 717 | } | 727 | } |
| 718 | } | 728 | } |
| 719 | } | 729 | } |
| @@ -756,6 +766,9 @@ const MID_SLOT: u8 = 2; | |||
| 756 | /// gap) by 6.0 / 2.4 BD points. | 766 | /// gap) by 6.0 / 2.4 BD points. |
| 757 | const MID_DEFAULT: Option<i16> = Some(-8); | 767 | const MID_DEFAULT: Option<i16> = Some(-8); |
| 758 | 768 | ||
| 769 | +/// [`Pyramid::default`]'s key-frame offset (`lanes/keyq.sweep.txt`). | ||
| 770 | +const KEY_DEFAULT: i16 = 0; | ||
| 771 | + | ||
| 759 | /// The AV1 software encoder: [`EncoderConfig`] in, one [`Packet`] out per | 772 | /// The AV1 software encoder: [`EncoderConfig`] in, one [`Packet`] out per |
| 760 | /// [`Av1Encoder::encode`] call — or, with a [`Pyramid`] configured, a | 773 | /// [`Av1Encoder::encode`] call — or, with a [`Pyramid`] configured, a |
| 761 | /// coding-order burst of them per [`Av1Encoder::encode_frames`] call plus a | 774 | /// coding-order burst of them per [`Av1Encoder::encode_frames`] call plus a |
| @@ -1290,10 +1303,12 @@ impl Av1Encoder { | |||
| 1290 | /// whole GOP at once. | 1303 | /// whole GOP at once. |
| 1291 | fn encode_key(&mut self, order: u64, picture: &Picture) -> Result<Packet> { | 1304 | fn encode_key(&mut self, order: u64, picture: &Picture) -> Result<Packet> { |
| 1292 | let render = (self.config.width, self.config.height); | 1305 | let render = (self.config.width, self.config.height); |
| 1293 | - let base_q_idx = self | 1306 | + let base = self |
| 1294 | .rate_loop | 1307 | .rate_loop |
| 1295 | .as_ref() | 1308 | .as_ref() |
| 1296 | - .map_or(self.config.base_q_idx, |r| r.q_idx(Level::Key)); | 1309 | + .map_or(i16::from(self.config.base_q_idx), |r| i16::from(r.q_idx(Level::Key))); |
| 1310 | + let offset = self.pyramid.map_or(0, |p| p.key_q_offset); | ||
| 1311 | + let base_q_idx = (base + offset).clamp(1, 255) as u8; | ||
| 1297 | let encoded = encode_key_frame_inner( | 1312 | let encoded = encode_key_frame_inner( |
| 1298 | &picture.padded_to(SUPERBLOCK), | 1313 | &picture.padded_to(SUPERBLOCK), |
| 1299 | base_q_idx, | 1314 | base_q_idx, |
| @@ -2378,6 +2393,35 @@ mod tests { | |||
| 2378 | ); | 2393 | ); |
| 2379 | } | 2394 | } |
| 2380 | 2395 | ||
| 2396 | + /// [`Pyramid::key_q_offset`] reaches the key frame: the whole run reads | ||
| 2397 | + /// the key, so an offset that never arrived would be invisible in the BD | ||
| 2398 | + /// table except as "no change" (class symbol-consumption-gap). A finer | ||
| 2399 | + /// key must cost strictly more bytes than the same picture at base q. | ||
| 2400 | + #[test] | ||
| 2401 | + fn the_key_q_offset_reaches_the_key_frame() { | ||
| 2402 | + let _knobs = crate::speed::knob_read(); | ||
| 2403 | + crate::encode::force_screen(Some(false)); | ||
| 2404 | + let key_bytes = |offset: i16| { | ||
| 2405 | + let config = EncoderConfig { | ||
| 2406 | + width: 128, | ||
| 2407 | + height: 128, | ||
| 2408 | + base_q_idx: 120, | ||
| 2409 | + gop: 32, | ||
| 2410 | + colour: Colour::Bt709Limited, | ||
| 2411 | + tile_cols_log2: 0, | ||
| 2412 | + tile_rows_log2: 0, | ||
| 2413 | + }; | ||
| 2414 | + let pyramid = Pyramid { key_q_offset: offset, ..Pyramid::default() }; | ||
| 2415 | + let mut enc = Av1Encoder::with_pyramid(config, pyramid).unwrap(); | ||
| 2416 | + let packets = enc.encode_frames(&test_card(128, 128, 0)).unwrap(); | ||
| 2417 | + let key = packets.iter().find(|p| p.level == Level::Key).expect("key frame"); | ||
| 2418 | + key.data.len() | ||
| 2419 | + }; | ||
| 2420 | + let (base, finer) = (key_bytes(0), key_bytes(-48)); | ||
| 2421 | + eprintln!("key at base q {base} bytes, at base q - 48 {finer} bytes"); | ||
| 2422 | + assert!(finer > base, "key_q_offset -48 did not reach the key frame ({finer} <= {base})"); | ||
| 2423 | + } | ||
| 2424 | + | ||
| 2381 | fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) { | 2425 | fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) { |
| 2382 | let (width, height) = (128usize, 128usize); | 2426 | let (width, height) = (128usize, 128usize); |
| 2383 | let config = EncoderConfig { | 2427 | let config = EncoderConfig { |
lanes/keyq-arm.shadded+9 -0
| @@ -0,0 +1,9 @@ | |||
| 1 | +#!/usr/bin/env bash | ||
| 2 | +# keyq lane: one long-GOP (or 12-frame) BD arm on the prebuilt release binary. | ||
| 3 | +# usage: keyq-arm.sh <unit> <log> <test> [<pyramid spec>|control] | ||
| 4 | +unit=$1; log=$2; test=$3; spec=$4 | ||
| 5 | +bin=$HOME/.cache/cargo-target-keyq/release/deps/ec_av1-8fef0196ab10338d | ||
| 6 | +env="" | ||
| 7 | +[ "$spec" = control ] || env="EC_AV1_PYRAMID=$spec" | ||
| 8 | +systemd-run --user --unit "$unit" -p MemoryMax=8G --collect bash -c \ | ||
| 9 | + "cd $HOME/Documents/Code/Rust/edith_codecs-keyq; $env $bin --ignored --nocapture --test-threads 1 --exact $test > $log 2>&1; echo RC=\$? >> $log" | ||
lanes/keyq.sweep.txtadded+4 -0
| @@ -0,0 +1,4 @@ | |||
| 1 | +# lane-keyq -- key-frame quantizer offset (Pyramid::key_q_offset, 5th env field) | ||
| 2 | +# gate: encode::tests::bd_rate_film_long_gop (48 pictures, gop 48), release lib binary, | ||
| 3 | +# --test-threads 1, <= 2 arms in parallel. Rows keyed by the log's own header. | ||
| 4 | +# spec = EC_AV1_PYRAMID=<mini_gop>:<arf>:<leaf>:<mid>:<key>; control = unset (default 8:-32:16:-8:0). | ||
lanes/keyq/ctl.logadded+6 -0
| @@ -0,0 +1,6 @@ | |||
| 1 | + | ||
| 2 | +running 1 test | ||
| 3 | +test encode::tests::bd_rate_film_long_gop ... | ||
| 4 | +native crop, 48 frames, gop=48; tiles (0, 0), tile threads 1, intrabc false, palette maxcolors 128 | ||
| 5 | +| clip | ours PSNR/bytes per point | BD-rate vs libaom | BD-rate vs rav1e | wall ours:libaom:rav1e (noisy) | | ||
| 6 | +|---|---|---|---|---| | ||
lanes/keyq/k32.logadded+6 -0
| @@ -0,0 +1,6 @@ | |||
| 1 | + | ||
| 2 | +running 1 test | ||
| 3 | +test encode::tests::bd_rate_film_long_gop ... | ||
| 4 | +native crop, 48 frames, gop=48; tiles (0, 0), tile threads 1, intrabc false, palette maxcolors 128 | ||
| 5 | +| clip | ours PSNR/bytes per point | BD-rate vs libaom | BD-rate vs rav1e | wall ours:libaom:rav1e (noisy) | | ||
| 6 | +|---|---|---|---|---| | ||