Encode_sequence codes the content-gated pyramid by default
Browse files at this commit534fad01c640fc0c67f54d6a9444c1fe075a2872Parent: 5d7858a- feat
- encode_sequence codes the content-gated pyramid by default
3 changed files +268 -94
- crates/ec-av1/src/encode.rs +73 -43
- crates/ec-av1/src/encoder.rs +188 -38
- tools/ec-bench/src/main.rs +7 -13
crates/ec-av1/src/encode.rsmodified+73 -43
| @@ -960,6 +960,23 @@ thread_local! { | |||
| 960 | static SCREEN_FORCE: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) }; | 960 | static SCREEN_FORCE: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) }; |
| 961 | } | 961 | } |
| 962 | 962 | ||
| 963 | +thread_local! { | ||
| 964 | + /// What the last [`encode_sequence_with_ctx`] call on this thread coded | ||
| 965 | + /// under, AFTER the content gate: the pyramid, or `None` for a stream | ||
| 966 | + /// that stayed flat. Read by the BD gates, which print the requested and | ||
| 967 | + /// the effective pyramid side by side -- a screen clip requests one and | ||
| 968 | + /// codes flat, and that difference is the gate (class | ||
| 969 | + /// `gate-blind-to-feature`). | ||
| 970 | + static LAST_SEQ_PYRAMID: std::cell::Cell<Option<crate::encoder::Pyramid>> = | ||
| 971 | + const { std::cell::Cell::new(None) }; | ||
| 972 | +} | ||
| 973 | + | ||
| 974 | +/// The pyramid the last sequence coded on this thread was EFFECTIVELY under. | ||
| 975 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | ||
| 976 | +pub(crate) fn last_sequence_pyramid() -> Option<crate::encoder::Pyramid> { | ||
| 977 | + LAST_SEQ_PYRAMID.with(std::cell::Cell::get) | ||
| 978 | +} | ||
| 979 | + | ||
| 963 | #[cfg(test)] | 980 | #[cfg(test)] |
| 964 | pub(crate) fn force_screen(value: Option<bool>) { | 981 | pub(crate) fn force_screen(value: Option<bool>) { |
| 965 | SCREEN_FORCE.with(|c| c.set(value)); | 982 | SCREEN_FORCE.with(|c| c.set(value)); |
| @@ -9012,6 +9029,42 @@ pub(crate) fn encode_sequence_with_ctx( | |||
| 9012 | }; | 9029 | }; |
| 9013 | first.check_even()?; | 9030 | first.check_even()?; |
| 9014 | let render = (first.width, first.height); | 9031 | let render = (first.width, first.height); |
| 9032 | + // THE CODING PYRAMID (lane-av1pyrdef): a sequence of camera material is | ||
| 9033 | + // coded as mini-GOPs -- a hidden ALTREF ahead of its display position, | ||
| 9034 | + // the leaves that predict backward off it, and a `show_existing_frame` | ||
| 9035 | + // header that re-outputs it -- unless `EC_AV1_PYRAMID` turns it off for | ||
| 9036 | + // an A/B. Screen content stays flat (the content gate in | ||
| 9037 | + // `Av1Encoder::encode_frames`, whose detector this asks first so the | ||
| 9038 | + // whole sequence path is decided in one place), and so does a run that | ||
| 9039 | + // asks for a quantizer rounding offset the facade does not carry: the | ||
| 9040 | + // facade codes at `encoder::DEADZONE` (0.5), so any other deadzone -- | ||
| 9041 | + // the ablations' -- must keep the flat path rather than be silently | ||
| 9042 | + // ignored. | ||
| 9043 | + // | ||
| 9044 | + // The reordering itself is NOT reimplemented here: it is the facade's | ||
| 9045 | + // own driver ([`crate::encoder::Av1Encoder::encode_sequence_pyramid`]), | ||
| 9046 | + // called with this sequence's shape, so the two paths cannot drift. | ||
| 9047 | + let pyramid = crate::encoder::Pyramid::from_env() | ||
| 9048 | + .filter(|_| !rest.is_empty() && (deadzone - 0.5).abs() < f64::EPSILON) | ||
| 9049 | + .filter(|_| !picture_is_screen(first)); | ||
| 9050 | + LAST_SEQ_PYRAMID.with(|c| c.set(pyramid)); | ||
| 9051 | + if let Some(pyramid) = pyramid { | ||
| 9052 | + for picture in rest { | ||
| 9053 | + picture.check_even()?; | ||
| 9054 | + if (picture.width, picture.height) != render { | ||
| 9055 | + return Err(Error::unsupported( | ||
| 9056 | + "AV1 encode", | ||
| 9057 | + "every picture in a sequence must be the same size as the first", | ||
| 9058 | + )); | ||
| 9059 | + } | ||
| 9060 | + } | ||
| 9061 | + return crate::encoder::Av1Encoder::encode_sequence_pyramid( | ||
| 9062 | + pictures, | ||
| 9063 | + base_q_idx, | ||
| 9064 | + pyramid, | ||
| 9065 | + armed_tiles(), | ||
| 9066 | + ); | ||
| 9067 | + } | ||
| 9015 | // The key frame and every inter frame after it are coded at the same | 9068 | // The key frame and every inter frame after it are coded at the same |
| 9016 | // padded size, so the reference each inter frame predicts from -- the | 9069 | // padded size, so the reference each inter frame predicts from -- the |
| 9017 | // previous frame's own uncropped reconstruction, at the size a decoder's | 9070 | // previous frame's own uncropped reconstruction, at the size a decoder's |
| @@ -12326,33 +12379,19 @@ mod tests { | |||
| 12326 | / source.len() as f64; | 12379 | / source.len() as f64; |
| 12327 | ladder.push((mean, (encoded.stream.len() as f64).log10())); | 12380 | ladder.push((mean, (encoded.stream.len() as f64).log10())); |
| 12328 | } | 12381 | } |
| 12382 | + // What this clip asked for and what the content gate left it coding | ||
| 12383 | + // under: a screen clip requests a pyramid and codes flat, and that | ||
| 12384 | + // difference is what keeps its row identical to the flat baseline | ||
| 12385 | + // (class `gate-blind-to-feature`). | ||
| 12386 | + eprintln!( | ||
| 12387 | + "{name}: sequence, pyramid requested {:?} effective {:?}", | ||
| 12388 | + crate::encoder::Pyramid::from_env(), | ||
| 12389 | + last_sequence_pyramid(), | ||
| 12390 | + ); | ||
| 12329 | ladder.sort_by(|a, b| a.0.total_cmp(&b.0)); | 12391 | ladder.sort_by(|a, b| a.0.total_cmp(&b.0)); |
| 12330 | (ladder, wall) | 12392 | (ladder, wall) |
| 12331 | } | 12393 | } |
| 12332 | 12394 | ||
| 12333 | - /// The [`crate::encoder::Pyramid`] this run's ladder is coded under, from | ||
| 12334 | - /// `EC_AV1_PYRAMID=<mini_gop>[:<arf_q_offset>:<leaf_q_offset>]`. Unset | ||
| 12335 | - /// keeps the flat one-key-then-all-inter ladder the baseline was measured | ||
| 12336 | - /// on, so the gate's default recipe is unchanged. | ||
| 12337 | - /// | ||
| 12338 | - /// RE-SWEPT on lane-av1pyrgate under the content gate (see | ||
| 12339 | - /// [`crate::encoder::Pyramid`]'s own table): with the screen capture | ||
| 12340 | - /// coding flat by construction, `4:-16:8` takes film A +62.8/+32.5 -> | ||
| 12341 | - /// +54.1/+24.2 and film B +86.3/+50.5 -> +82.6/+47.4, and the screen row | ||
| 12342 | - /// is unchanged to the byte. The gate ladder still defaults to flat -- | ||
| 12343 | - /// the sequence path this arm is pinned against codes no pyramid. | ||
| 12344 | - fn pyramid_from_env() -> Option<crate::encoder::Pyramid> { | ||
| 12345 | - let spec = std::env::var("EC_AV1_PYRAMID").ok()?; | ||
| 12346 | - let mut f = spec.split(':'); | ||
| 12347 | - let mini_gop = f.next()?.parse().ok()?; | ||
| 12348 | - let d = crate::encoder::Pyramid::default(); | ||
| 12349 | - Some(crate::encoder::Pyramid { | ||
| 12350 | - mini_gop, | ||
| 12351 | - arf_q_offset: f.next().and_then(|v| v.parse().ok()).unwrap_or(d.arf_q_offset), | ||
| 12352 | - leaf_q_offset: f.next().and_then(|v| v.parse().ok()).unwrap_or(d.leaf_q_offset), | ||
| 12353 | - }) | ||
| 12354 | - } | ||
| 12355 | - | ||
| 12356 | /// [`our_ladder`] through the streaming facade — the surface the editor's | 12395 | /// [`our_ladder`] through the streaming facade — the surface the editor's |
| 12357 | /// export drives (`EC_AV1_GATE_FACADE=1`). With a [`crate::encoder::Pyramid`] | 12396 | /// export drives (`EC_AV1_GATE_FACADE=1`). With a [`crate::encoder::Pyramid`] |
| 12358 | /// each stream is also reordered (hidden `ALTREF` first, | 12397 | /// each stream is also reordered (hidden `ALTREF` first, |
| @@ -12778,14 +12817,16 @@ mod tests { | |||
| 12778 | let _ = crate::tile::take_compound_size_hits(); | 12817 | let _ = crate::tile::take_compound_size_hits(); |
| 12779 | let _ = crate::tile::take_compound_leaf_mode_hits(); | 12818 | let _ = crate::tile::take_compound_leaf_mode_hits(); |
| 12780 | let _ = crate::motion::take_census(); | 12819 | let _ = crate::motion::take_census(); |
| 12781 | - let pyramid_env = pyramid_from_env(); | ||
| 12782 | // The entry-surface arm: run the SAME ladder through the | 12820 | // The entry-surface arm: run the SAME ladder through the |
| 12783 | // streaming facade the editor's export calls, which must print | 12821 | // streaming facade the editor's export calls, which must print |
| 12784 | - // the same BD numbers as the sequence path. | 12822 | + // the same BD numbers as the sequence path -- byte for byte the |
| 12823 | + // same stream now that both drive one mini-GOP driver | ||
| 12824 | + // (lane-av1pyrdef). | ||
| 12785 | let facade = std::env::var_os("EC_AV1_GATE_FACADE").is_some(); | 12825 | let facade = std::env::var_os("EC_AV1_GATE_FACADE").is_some(); |
| 12786 | - let (ours, ours_wall) = match (facade, pyramid_env) { | 12826 | + let (ours, ours_wall) = match facade { |
| 12787 | - (false, None) => our_ladder(name, &source, width, height, fctx), | 12827 | + false => our_ladder(name, &source, width, height, fctx), |
| 12788 | - (_, pyramid) => { | 12828 | + true => { |
| 12829 | + let pyramid = crate::encoder::Pyramid::from_env(); | ||
| 12789 | let (ladder, wall, counts, bytes, effective) = | 12830 | let (ladder, wall, counts, bytes, effective) = |
| 12790 | our_ladder_facade(name, &source, width, height, pyramid); | 12831 | our_ladder_facade(name, &source, width, height, pyramid); |
| 12791 | eprintln!( | 12832 | eprintln!( |
| @@ -13293,21 +13334,10 @@ mod tests { | |||
| 13293 | let _ = crate::tile::take_palette_uv_hits(); | 13334 | let _ = crate::tile::take_palette_uv_hits(); |
| 13294 | let _ = crate::tile::take_intrabc_hits(); | 13335 | let _ = crate::tile::take_intrabc_hits(); |
| 13295 | let _ = take_intrabc_search(); | 13336 | let _ = take_intrabc_search(); |
| 13296 | - let (ours, ours_wall) = match pyramid_from_env() { | 13337 | + // The sequence path itself codes the pyramid now |
| 13297 | - None => our_ladder(name, &source, cw, ch, fctx), | 13338 | + // (lane-av1pyrdef), and prints what it requested and what the |
| 13298 | - Some(pyramid) => { | 13339 | + // content gate left effective. |
| 13299 | - let (ladder, wall, counts, bytes, effective) = | 13340 | + let (ours, ours_wall) = our_ladder(name, &source, cw, ch, fctx); |
| 13300 | - our_ladder_facade(name, &source, cw, ch, Some(pyramid)); | ||
| 13301 | - eprintln!( | ||
| 13302 | - "{name}: pyramid requested {pyramid:?} effective {effective:?} \ | ||
| 13303 | - -- frames key {} arf {} leaf {} \ | ||
| 13304 | - show_existing {}; bytes key {} arf {} leaf {} show_existing {}", | ||
| 13305 | - counts[0], counts[1], counts[2], counts[3], | ||
| 13306 | - bytes[0], bytes[1], bytes[2], bytes[3], | ||
| 13307 | - ); | ||
| 13308 | - (ladder, wall) | ||
| 13309 | - } | ||
| 13310 | - }; | ||
| 13311 | // gate-blind-to-feature: what the screen tools actually did at | 13341 | // gate-blind-to-feature: what the screen tools actually did at |
| 13312 | // native resolution, over this clip's four encodes. | 13342 | // native resolution, over this clip's four encodes. |
| 13313 | let screen_on = SCREEN_FRAMES[1].load(std::sync::atomic::Ordering::Relaxed); | 13343 | let screen_on = SCREEN_FRAMES[1].load(std::sync::atomic::Ordering::Relaxed); |
crates/ec-av1/src/encoder.rsmodified+188 -38
| @@ -356,6 +356,33 @@ pub struct Pyramid { | |||
| 356 | pub leaf_q_offset: i16, | 356 | pub leaf_q_offset: i16, |
| 357 | } | 357 | } |
| 358 | 358 | ||
| 359 | +impl Pyramid { | ||
| 360 | + /// The pyramid a run asks for through the environment: | ||
| 361 | + /// `EC_AV1_PYRAMID=<mini_gop>[:<arf_q_offset>:<leaf_q_offset>]`, with | ||
| 362 | + /// `0` (or anything that is not a mini-GOP of at least 2, e.g. `flat`) | ||
| 363 | + /// meaning the flat one-picture-one-frame path. UNSET is | ||
| 364 | + /// [`Pyramid::default`]: the pyramid is what | ||
| 365 | + /// [`crate::encode::encode_sequence`], the BD gate and `ec-bench` code | ||
| 366 | + /// under unless a run turns it off for an A/B. | ||
| 367 | + #[must_use] | ||
| 368 | + pub fn from_env() -> Option<Self> { | ||
| 369 | + let d = Self::default(); | ||
| 370 | + let Some(spec) = std::env::var("EC_AV1_PYRAMID").ok() else { | ||
| 371 | + return Some(d); | ||
| 372 | + }; | ||
| 373 | + let mut f = spec.split(':'); | ||
| 374 | + let mini_gop: usize = f.next().and_then(|v| v.parse().ok()).unwrap_or(0); | ||
| 375 | + if mini_gop < 2 { | ||
| 376 | + return None; | ||
| 377 | + } | ||
| 378 | + Some(Self { | ||
| 379 | + mini_gop, | ||
| 380 | + arf_q_offset: f.next().and_then(|v| v.parse().ok()).unwrap_or(d.arf_q_offset), | ||
| 381 | + leaf_q_offset: f.next().and_then(|v| v.parse().ok()).unwrap_or(d.leaf_q_offset), | ||
| 382 | + }) | ||
| 383 | + } | ||
| 384 | +} | ||
| 385 | + | ||
| 359 | impl Default for Pyramid { | 386 | impl Default for Pyramid { |
| 360 | /// The best point of the offset sweep run through the BD gate's own | 387 | /// The best point of the offset sweep run through the BD gate's own |
| 361 | /// `EC_AV1_PYRAMID=<mini_gop>[:<arf_q_offset>:<leaf_q_offset>]` knob | 388 | /// `EC_AV1_PYRAMID=<mini_gop>[:<arf_q_offset>:<leaf_q_offset>]` knob |
| @@ -580,6 +607,11 @@ pub struct Av1Encoder { | |||
| 580 | anchor: usize, | 607 | anchor: usize, |
| 581 | /// Coding-order position of the next packet — a packet's `dts`. | 608 | /// Coding-order position of the next packet — a packet's `dts`. |
| 582 | next_dts: u64, | 609 | next_dts: u64, |
| 610 | + /// Set by [`Av1Encoder::encode_sequence_pyramid`]: every coded frame's | ||
| 611 | + /// own cropped [`Encoded`] (its display position and its reconstruction), | ||
| 612 | + /// which is what [`crate::encode::EncodedSequence`] carries and a packet | ||
| 613 | + /// does not. `None` for every other caller, which pays nothing for it. | ||
| 614 | + collected: Option<Vec<(u64, Encoded)>>, | ||
| 583 | } | 615 | } |
| 584 | 616 | ||
| 585 | /// One decoded-picture-buffer slot: what a `refresh_frame_flags` bit stores | 617 | /// One decoded-picture-buffer slot: what a `refresh_frame_flags` bit stores |
| @@ -645,6 +677,7 @@ impl Av1Encoder { | |||
| 645 | held: std::collections::VecDeque::new(), | 677 | held: std::collections::VecDeque::new(), |
| 646 | anchor: 0, | 678 | anchor: 0, |
| 647 | next_dts: 0, | 679 | next_dts: 0, |
| 680 | + collected: None, | ||
| 648 | }) | 681 | }) |
| 649 | } | 682 | } |
| 650 | 683 | ||
| @@ -931,6 +964,65 @@ impl Av1Encoder { | |||
| 931 | Ok(self.packet(cropped.stream, is_key, order, level)) | 964 | Ok(self.packet(cropped.stream, is_key, order, level)) |
| 932 | } | 965 | } |
| 933 | 966 | ||
| 967 | + /// THE SHARED MINI-GOP DRIVER: codes a whole sequence — one key frame | ||
| 968 | + /// then all inter, which is [`crate::encode::encode_sequence`]'s only | ||
| 969 | + /// shape — through this very facade, under `pyramid`, and hands back what | ||
| 970 | + /// that path returns. The two are byte-identical BY CONSTRUCTION rather | ||
| 971 | + /// than by a mirrored second implementation: there is one reordering, | ||
| 972 | + /// one DPB/slot policy and one set of per-level quantizer offsets, here | ||
| 973 | + /// (`encoder::tests::the_facade_codes_the_same_bytes_as_encode_sequence` | ||
| 974 | + /// still pins it, now over a pyramid sequence too). | ||
| 975 | + /// | ||
| 976 | + /// The content gate lives in [`Av1Encoder::encode_frames`] and applies | ||
| 977 | + /// unchanged, so a screen-content clip handed in here codes flat. | ||
| 978 | + /// | ||
| 979 | + /// `EncodedSequence::frames` comes back in DISPLAY order, one entry per | ||
| 980 | + /// source picture — the order the flat path's coding order happens to be, | ||
| 981 | + /// and the order every reader of it (the BD gate's sample-exactness | ||
| 982 | + /// assertions against a decoder's output) needs. `stream` stays in coding | ||
| 983 | + /// order, which is the order a decoder reads. | ||
| 984 | + /// | ||
| 985 | + /// # Errors | ||
| 986 | + /// As [`Av1Encoder::encode_frames`]. | ||
| 987 | + pub(crate) fn encode_sequence_pyramid( | ||
| 988 | + pictures: &[Picture], | ||
| 989 | + base_q_idx: u8, | ||
| 990 | + pyramid: Pyramid, | ||
| 991 | + tiles: (u32, u32), | ||
| 992 | + ) -> Result<crate::encode::EncodedSequence> { | ||
| 993 | + let first = pictures.first().ok_or_else(|| { | ||
| 994 | + Error::unsupported("AV1 encode", "a sequence needs at least one picture") | ||
| 995 | + })?; | ||
| 996 | + let config = EncoderConfig { | ||
| 997 | + width: first.width, | ||
| 998 | + height: first.height, | ||
| 999 | + base_q_idx, | ||
| 1000 | + // One key frame, at picture 0, and inter for the rest. | ||
| 1001 | + gop: pictures.len().max(1), | ||
| 1002 | + // `encode_sequence` writes an unspecified colour config. | ||
| 1003 | + colour: Colour::Unspecified, | ||
| 1004 | + tile_cols_log2: tiles.0, | ||
| 1005 | + tile_rows_log2: tiles.1, | ||
| 1006 | + }; | ||
| 1007 | + let mut encoder = Self::with_pyramid(config, pyramid)?; | ||
| 1008 | + encoder.collected = Some(Vec::with_capacity(pictures.len())); | ||
| 1009 | + let mut stream = Vec::new(); | ||
| 1010 | + for picture in pictures { | ||
| 1011 | + for packet in encoder.encode_frames(picture)? { | ||
| 1012 | + stream.extend_from_slice(&packet.data); | ||
| 1013 | + } | ||
| 1014 | + } | ||
| 1015 | + for packet in encoder.flush()? { | ||
| 1016 | + stream.extend_from_slice(&packet.data); | ||
| 1017 | + } | ||
| 1018 | + let mut collected = encoder.collected.take().expect("collecting"); | ||
| 1019 | + collected.sort_by_key(|(order, _)| *order); | ||
| 1020 | + Ok(crate::encode::EncodedSequence { | ||
| 1021 | + stream, | ||
| 1022 | + frames: collected.into_iter().map(|(_, encoded)| encoded).collect(), | ||
| 1023 | + }) | ||
| 1024 | + } | ||
| 1025 | + | ||
| 934 | /// Stamps a packet with its display and coding positions and advances the | 1026 | /// Stamps a packet with its display and coding positions and advances the |
| 935 | /// coding-order clock. | 1027 | /// coding-order clock. |
| 936 | fn packet(&mut self, data: Vec<u8>, key: bool, order: u64, level: Level) -> Packet { | 1028 | fn packet(&mut self, data: Vec<u8>, key: bool, order: u64, level: Level) -> Packet { |
| @@ -983,6 +1075,9 @@ impl Av1Encoder { | |||
| 983 | if let Some(rate_loop) = self.rate_loop.as_mut() { | 1075 | if let Some(rate_loop) = self.rate_loop.as_mut() { |
| 984 | rate_loop.update(Level::Key, cropped.stream.len()); | 1076 | rate_loop.update(Level::Key, cropped.stream.len()); |
| 985 | } | 1077 | } |
| 1078 | + if let Some(collected) = self.collected.as_mut() { | ||
| 1079 | + collected.push((order, cropped.clone())); | ||
| 1080 | + } | ||
| 986 | Ok(self.packet(cropped.stream, true, order, Level::Key)) | 1081 | Ok(self.packet(cropped.stream, true, order, Level::Key)) |
| 987 | } | 1082 | } |
| 988 | 1083 | ||
| @@ -1091,6 +1186,9 @@ impl Av1Encoder { | |||
| 1091 | if let Some(rate_loop) = self.rate_loop.as_mut() { | 1186 | if let Some(rate_loop) = self.rate_loop.as_mut() { |
| 1092 | rate_loop.update(level, cropped.stream.len()); | 1187 | rate_loop.update(level, cropped.stream.len()); |
| 1093 | } | 1188 | } |
| 1189 | + if let Some(collected) = self.collected.as_mut() { | ||
| 1190 | + collected.push((order, cropped.clone())); | ||
| 1191 | + } | ||
| 1094 | Ok(self.packet(cropped.stream, false, order, level)) | 1192 | Ok(self.packet(cropped.stream, false, order, level)) |
| 1095 | } | 1193 | } |
| 1096 | 1194 | ||
| @@ -1263,54 +1361,106 @@ mod tests { | |||
| 1263 | assert!(enc.flush().unwrap().is_empty(), "nothing is held twice"); | 1361 | assert!(enc.flush().unwrap().is_empty(), "nothing is held twice"); |
| 1264 | } | 1362 | } |
| 1265 | 1363 | ||
| 1266 | - /// THE ENTRY-SURFACE GATE (lane-av1facade): the streaming facade — what | 1364 | + /// A screen-like card: hard-edged 4-pixel bands of two flat luma values |
| 1267 | - /// the editor's export drives — codes byte for byte what | 1365 | + /// with a moving block over the lower half — few colours per 16x16 block |
| 1268 | - /// [`crate::encode::encode_sequence`] codes, which is the path the BD | 1366 | + /// at high variance, which is exactly what |
| 1269 | - /// gate measures. It did not before: the facade passed no lookahead, so | 1367 | + /// [`crate::encode::picture_is_screen`] separates. Used unforced, so the |
| 1270 | - /// every inter frame missed the per-superblock temporal lambda map | 1368 | + /// gate below runs the real detector rather than an override. |
| 1271 | - /// (lane-av1tpl) the gate's numbers were measured with. | 1369 | + fn screen_card(width: usize, height: usize, shift: usize) -> Picture { |
| 1370 | + let mut picture = Picture::grey(width, height); | ||
| 1371 | + for y in 0..height { | ||
| 1372 | + for x in 0..width { | ||
| 1373 | + let band = if (x / 4).is_multiple_of(2) { 40 } else { 200 }; | ||
| 1374 | + let over = y >= height / 2 && (x + shift) % width < width / 3; | ||
| 1375 | + picture.y[y * width + x] = if over { 128 } else { band }; | ||
| 1376 | + } | ||
| 1377 | + } | ||
| 1378 | + picture | ||
| 1379 | + } | ||
| 1380 | + | ||
| 1381 | + /// THE ENTRY-SURFACE GATE (lane-av1facade, extended to the pyramid on | ||
| 1382 | + /// lane-av1pyrdef): the streaming facade — what the editor's export | ||
| 1383 | + /// drives — codes byte for byte what [`crate::encode::encode_sequence`] | ||
| 1384 | + /// codes, which is the path the BD gate measures. Both now drive ONE | ||
| 1385 | + /// mini-GOP driver ([`Av1Encoder::encode_sequence_pyramid`]), so the | ||
| 1386 | + /// identity is by construction; this pins it anyway, over both content | ||
| 1387 | + /// classes the content gate splits: | ||
| 1388 | + /// | ||
| 1389 | + /// * film-like (the gate's own clip when the fixture is there, a | ||
| 1390 | + /// multi-tone moving card otherwise): the pyramid is EFFECTIVE, the | ||
| 1391 | + /// stream is reordered (a hidden `ALTREF` per mini-GOP), and the two | ||
| 1392 | + /// paths' whole streams are identical; | ||
| 1393 | + /// * screen-like: the content gate drops BOTH paths back to flat, one | ||
| 1394 | + /// packet per picture, identical frame by frame as well as whole. | ||
| 1272 | /// | 1395 | /// |
| 1273 | - /// Runs on the gate's own clip at the gate's own size when the fixture is | 1396 | + /// Never skipped, since a skipped identity gate is the defect it is meant |
| 1274 | - /// there, and on a synthetic card otherwise — never skipped, since a | 1397 | + /// to catch. |
| 1275 | - /// skipped identity gate is the defect it is meant to catch. | ||
| 1276 | #[test] | 1398 | #[test] |
| 1277 | fn the_facade_codes_the_same_bytes_as_encode_sequence() { | 1399 | fn the_facade_codes_the_same_bytes_as_encode_sequence() { |
| 1278 | let frames = 12usize; | 1400 | let frames = 12usize; |
| 1279 | - let pictures = h264_clip_frames(640, 384, frames).unwrap_or_else(|| { | 1401 | + let film = h264_clip_frames(640, 384, frames).unwrap_or_else(|| { |
| 1280 | eprintln!("no h264 clip: the facade identity gate runs on a synthetic card"); | 1402 | eprintln!("no h264 clip: the facade identity gate runs on a synthetic card"); |
| 1281 | (0..frames).map(|t| test_card(64, 64, t)).collect() | 1403 | (0..frames).map(|t| test_card(64, 64, t)).collect() |
| 1282 | }); | 1404 | }); |
| 1283 | - let (width, height) = (pictures[0].width, pictures[0].height); | 1405 | + let screen: Vec<Picture> = (0..frames).map(|t| screen_card(64, 64, t)).collect(); |
| 1284 | - for q in [150u8, 60] { | 1406 | + let pyramid = Pyramid::from_env().expect("the pyramid is this build's default"); |
| 1285 | - let config = EncoderConfig { | 1407 | + for (content, pictures, is_screen) in |
| 1286 | - width, | 1408 | + [("film", &film, false), ("screen", &screen, true)] |
| 1287 | - height, | 1409 | + { |
| 1288 | - base_q_idx: q, | 1410 | + let (width, height) = (pictures[0].width, pictures[0].height); |
| 1289 | - // One key frame then all inter, which is `encode_sequence`'s | 1411 | + for q in [150u8, 60] { |
| 1290 | - // only shape, and the gate's recipe. | 1412 | + let config = EncoderConfig { |
| 1291 | - gop: frames, | 1413 | + width, |
| 1292 | - // `encode_sequence` writes an unspecified colour config. | 1414 | + height, |
| 1293 | - colour: Colour::Unspecified, | 1415 | + base_q_idx: q, |
| 1294 | - tile_cols_log2: 0, | 1416 | + // One key frame then all inter, which is |
| 1295 | - tile_rows_log2: 0, | 1417 | + // `encode_sequence`'s only shape, and the gate's recipe. |
| 1296 | - }; | 1418 | + gop: frames, |
| 1297 | - let mut enc = Av1Encoder::new(config).unwrap(); | 1419 | + // `encode_sequence` writes an unspecified colour config. |
| 1298 | - let packets = encode_all(&mut enc, &pictures); | 1420 | + colour: Colour::Unspecified, |
| 1299 | - let sequence = crate::encode::encode_sequence(&pictures, q, DEADZONE).unwrap(); | 1421 | + tile_cols_log2: 0, |
| 1300 | - assert_eq!(packets.len(), frames, "q={q}: one packet per picture"); | 1422 | + tile_rows_log2: 0, |
| 1301 | - for (i, (packet, coded)) in packets.iter().zip(&sequence.frames).enumerate() { | 1423 | + }; |
| 1302 | - let cropped = crate::encode::crop_encoded(coded, width, height); | 1424 | + let mut enc = Av1Encoder::with_pyramid(config, pyramid).unwrap(); |
| 1425 | + let mut packets = Vec::new(); | ||
| 1426 | + for picture in pictures { | ||
| 1427 | + packets.extend(enc.encode_frames(picture).unwrap()); | ||
| 1428 | + } | ||
| 1429 | + packets.extend(enc.flush().unwrap()); | ||
| 1430 | + assert_eq!( | ||
| 1431 | + enc.pyramid().is_none(), | ||
| 1432 | + is_screen, | ||
| 1433 | + "{content} q={q}: the content gate read this clip the other way round" | ||
| 1434 | + ); | ||
| 1435 | + let sequence = crate::encode::encode_sequence(pictures, q, DEADZONE).unwrap(); | ||
| 1436 | + assert_eq!( | ||
| 1437 | + (packets.iter().map(|p| p.data.len()).sum::<usize>(), concat(&packets)), | ||
| 1438 | + (sequence.stream.len(), sequence.stream.clone()), | ||
| 1439 | + "{content} q={q}: the facade's stream is not the sequence path's" | ||
| 1440 | + ); | ||
| 1303 | assert_eq!( | 1441 | assert_eq!( |
| 1304 | - (packet.data.len(), packet.data.clone()), | 1442 | + sequence.frames.len(), |
| 1305 | - (cropped.stream.len(), cropped.stream.clone()), | 1443 | + frames, |
| 1306 | - "q={q} frame {i}: the facade's bytes are not the sequence path's" | 1444 | + "{content} q={q}: one entry per picture, in display order" |
| 1307 | ); | 1445 | ); |
| 1446 | + if is_screen { | ||
| 1447 | + assert_eq!(packets.len(), frames, "{content} q={q}: one packet per picture"); | ||
| 1448 | + for (i, (packet, coded)) in packets.iter().zip(&sequence.frames).enumerate() { | ||
| 1449 | + let cropped = crate::encode::crop_encoded(coded, width, height); | ||
| 1450 | + assert_eq!( | ||
| 1451 | + (packet.data.len(), packet.data.clone()), | ||
| 1452 | + (cropped.stream.len(), cropped.stream.clone()), | ||
| 1453 | + "{content} q={q} frame {i}: the facade's bytes are not the \ | ||
| 1454 | + sequence path's" | ||
| 1455 | + ); | ||
| 1456 | + } | ||
| 1457 | + } else { | ||
| 1458 | + assert!( | ||
| 1459 | + packets.iter().any(|p| p.level == Level::Arf), | ||
| 1460 | + "{content} q={q}: a non-screen stream coded no hidden frame" | ||
| 1461 | + ); | ||
| 1462 | + } | ||
| 1308 | } | 1463 | } |
| 1309 | - assert_eq!( | ||
| 1310 | - concat(&packets), | ||
| 1311 | - sequence.stream, | ||
| 1312 | - "q={q}: whole stream" | ||
| 1313 | - ); | ||
| 1314 | } | 1464 | } |
| 1315 | } | 1465 | } |
| 1316 | 1466 | ||
tools/ec-bench/src/main.rsmodified+7 -13
| @@ -368,19 +368,13 @@ fn bench_av1_encode(rows: &mut Vec<Row>) { | |||
| 368 | tile_cols_log2, | 368 | tile_cols_log2, |
| 369 | tile_rows_log2, | 369 | tile_rows_log2, |
| 370 | }; | 370 | }; |
| 371 | - // `EC_AV1_PYRAMID=<mini_gop>[:<arf_q_offset>:<leaf_q_offset>]` benches the | 371 | + // The encoder's coding pyramid (hidden ALTREF + show_existing_frame) is |
| 372 | - // encoder's coding pyramid (hidden ALTREF + show_existing_frame) instead | 372 | + // the default, as it is in `encode_sequence`; `EC_AV1_PYRAMID=0` benches |
| 373 | - // of the flat one-picture-one-packet stream, the same knob shape as | 373 | + // the flat one-picture-one-packet stream instead, and |
| 374 | - // `EC_H265_RDOQ_ESTIMATE`. Unset keeps the flat default. | 374 | + // `EC_AV1_PYRAMID=<mini_gop>[:<arf_q_offset>:<leaf_q_offset>]` another |
| 375 | - let pyramid = std::env::var("EC_AV1_PYRAMID").ok().and_then(|spec| { | 375 | + // shape of it. The `hidden` column below is how many frames of the row |
| 376 | - let mut f = spec.split(':'); | 376 | + // were coded ahead of their display position. |
| 377 | - let d = Pyramid::default(); | 377 | + let pyramid = Pyramid::from_env(); |
| 378 | - Some(Pyramid { | ||
| 379 | - mini_gop: f.next()?.parse().ok()?, | ||
| 380 | - arf_q_offset: f.next().and_then(|v| v.parse().ok()).unwrap_or(d.arf_q_offset), | ||
| 381 | - leaf_q_offset: f.next().and_then(|v| v.parse().ok()).unwrap_or(d.leaf_q_offset), | ||
| 382 | - }) | ||
| 383 | - }); | ||
| 384 | let mut enc = match pyramid { | 378 | let mut enc = match pyramid { |
| 385 | None => Av1Encoder::new(cfg).expect("av1 encoder"), | 379 | None => Av1Encoder::new(cfg).expect("av1 encoder"), |
| 386 | Some(p) => Av1Encoder::with_pyramid(cfg, p).expect("av1 encoder"), | 380 | Some(p) => Av1Encoder::with_pyramid(cfg, p).expect("av1 encoder"), |