The top ARF can take its temporal lambda window out of the buffered next group
Browse files at this commitc295244dfd64b86b3a62c947d8ce36c9cc5ae510Parent: 323c803- feat
- the top ARF can take its temporal lambda window out of the buffered next group
2 changed files +175 -1
- crates/ec-av1/src/encoder.rs +153 -1
- crates/ec-av1/src/speed.rs +22 -0
crates/ec-av1/src/encoder.rsmodified+153 -1
| @@ -1260,6 +1260,8 @@ static LOOKAHEAD_OVERRIDE: std::sync::atomic::AtomicU8 = | |||
| 1260 | std::sync::atomic::AtomicU8::new(u8::MAX); | 1260 | std::sync::atomic::AtomicU8::new(u8::MAX); |
| 1261 | static ARF_TF_FUT_OVERRIDE: std::sync::atomic::AtomicUsize = | 1261 | static ARF_TF_FUT_OVERRIDE: std::sync::atomic::AtomicUsize = |
| 1262 | std::sync::atomic::AtomicUsize::new(usize::MAX); | 1262 | std::sync::atomic::AtomicUsize::new(usize::MAX); |
| 1263 | +static TPL_FUT_OVERRIDE: std::sync::atomic::AtomicUsize = | ||
| 1264 | + std::sync::atomic::AtomicUsize::new(usize::MAX); | ||
| 1263 | 1265 | ||
| 1264 | /// Sets [`LOOKAHEAD_OVERRIDE`]; `None` clears it. | 1266 | /// Sets [`LOOKAHEAD_OVERRIDE`]; `None` clears it. |
| 1265 | #[allow(dead_code)] // set only from the `#[cfg(test)]` witness | 1267 | #[allow(dead_code)] // set only from the `#[cfg(test)]` witness |
| @@ -1276,6 +1278,26 @@ pub(crate) fn set_arf_tf_future(n: Option<usize>) { | |||
| 1276 | ARF_TF_FUT_OVERRIDE.store(n.unwrap_or(usize::MAX), std::sync::atomic::Ordering::Relaxed); | 1278 | ARF_TF_FUT_OVERRIDE.store(n.unwrap_or(usize::MAX), std::sync::atomic::Ordering::Relaxed); |
| 1277 | } | 1279 | } |
| 1278 | 1280 | ||
| 1281 | +/// Sets [`TPL_FUT_OVERRIDE`]; `None` clears it. | ||
| 1282 | +#[allow(dead_code)] // set only from the `#[cfg(test)]` witness | ||
| 1283 | +pub(crate) fn set_tpl_fut(n: Option<usize>) { | ||
| 1284 | + TPL_FUT_OVERRIDE.store(n.unwrap_or(usize::MAX), std::sync::atomic::Ordering::Relaxed); | ||
| 1285 | +} | ||
| 1286 | + | ||
| 1287 | +/// lane-tplfut: which window the TOP ARF's temporal lambda map propagates | ||
| 1288 | +/// back through ([`crate::speed::TPL_FUT`], `EC_AV1_TPL_FUT=<n>`). Inert | ||
| 1289 | +/// without [`lookahead`] -- mode 0 is the shipped past window. | ||
| 1290 | +pub(crate) fn tpl_fut() -> usize { | ||
| 1291 | + match TPL_FUT_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed) { | ||
| 1292 | + usize::MAX => {} | ||
| 1293 | + n => return n, | ||
| 1294 | + } | ||
| 1295 | + std::env::var("EC_AV1_TPL_FUT") | ||
| 1296 | + .ok() | ||
| 1297 | + .and_then(|v| v.parse().ok()) | ||
| 1298 | + .unwrap_or_else(|| crate::speed::at(&crate::speed::TPL_FUT)) | ||
| 1299 | +} | ||
| 1300 | + | ||
| 1279 | /// lane-lookahead: whether the pyramid path holds one group of lookahead | 1301 | /// lane-lookahead: whether the pyramid path holds one group of lookahead |
| 1280 | /// ([`crate::speed::LOOKAHEAD`], `EC_AV1_LOOKAHEAD=0|1`). | 1302 | /// ([`crate::speed::LOOKAHEAD`], `EC_AV1_LOOKAHEAD=0|1`). |
| 1281 | fn lookahead() -> bool { | 1303 | fn lookahead() -> bool { |
| @@ -2207,6 +2229,41 @@ impl Av1Encoder { | |||
| 2207 | .take(arf_tf_window_future()) | 2229 | .take(arf_tf_window_future()) |
| 2208 | .map(|(_, p)| p.padded_to(SUPERBLOCK)) | 2230 | .map(|(_, p)| p.padded_to(SUPERBLOCK)) |
| 2209 | .collect(); | 2231 | .collect(); |
| 2232 | + // lane-tplfut: the top ARF's TEMPORAL LAMBDA window. Mode 0 (and any | ||
| 2233 | + // mode with no buffered future picture) keeps the group's own | ||
| 2234 | + // display-PAST sources, reversed -- the only window that exists | ||
| 2235 | + // without a lookahead, and the one every measurement so far was | ||
| 2236 | + // taken with. The other modes read the next group's sources, which | ||
| 2237 | + // is the direction libaom's tpl looks at an ARF. | ||
| 2238 | + // `n` of the buffered display-future sources, nearest first, padded | ||
| 2239 | + // like the frame being coded. | ||
| 2240 | + let arf_tpl_future = |n: usize| -> Vec<Picture> { | ||
| 2241 | + future.iter().take(n).map(|(_, p)| p.padded_to(SUPERBLOCK)).collect() | ||
| 2242 | + }; | ||
| 2243 | + let arf_tpl: Vec<Picture> = { | ||
| 2244 | + let depth = crate::encode::tpl_depth(); | ||
| 2245 | + let past = || tpl_window(group.len() - 1, true); | ||
| 2246 | + match (tpl_fut(), future.is_empty(), tpl_pyramid() && depth >= 2) { | ||
| 2247 | + (_, _, false) => Vec::new(), | ||
| 2248 | + (0, _, _) | (_, true, _) => past(), | ||
| 2249 | + (1, _, _) => arf_tpl_future(depth - 1), | ||
| 2250 | + // Half the budget each side (`EC_AV1_TPL_FUT_HALF=<n>` | ||
| 2251 | + // moves the split), past first: the chain then has | ||
| 2252 | + // ONE discontinuity in the middle (the coarse pass between | ||
| 2253 | + // the nearest past and the nearest future neighbour spans the | ||
| 2254 | + // whole group), which is the arm's known cost. | ||
| 2255 | + (2, _, _) => { | ||
| 2256 | + let half = std::env::var("EC_AV1_TPL_FUT_HALF").ok().and_then(|v| v.parse().ok()).unwrap_or((depth - 1) / 2); | ||
| 2257 | + let mut w = past(); | ||
| 2258 | + w.truncate(half.max(1)); | ||
| 2259 | + let room = (depth - 1).saturating_sub(w.len()); | ||
| 2260 | + w.extend(arf_tpl_future(room)); | ||
| 2261 | + w | ||
| 2262 | + } | ||
| 2263 | + // Mode 3 and anything above: the WHOLE buffered next group. | ||
| 2264 | + (_, _, _) => arf_tpl_future(future.len()), | ||
| 2265 | + } | ||
| 2266 | + }; | ||
| 2210 | // The group's top level: the last picture, hidden, off the anchor. | 2267 | // The group's top level: the last picture, hidden, off the anchor. |
| 2211 | packets.push(self.encode_pyramid_inter( | 2268 | packets.push(self.encode_pyramid_inter( |
| 2212 | arf_order, | 2269 | arf_order, |
| @@ -2228,7 +2285,7 @@ impl Av1Encoder { | |||
| 2228 | false, | 2285 | false, |
| 2229 | Level::Arf, | 2286 | Level::Arf, |
| 2230 | pyramid.arf_q_offset, | 2287 | pyramid.arf_q_offset, |
| 2231 | - &tpl_window(group.len() - 1, true), | 2288 | + &arf_tpl, |
| 2232 | // lane-lookahead: the group's top ARF is its LAST picture in | 2289 | // lane-lookahead: the group's top ARF is its LAST picture in |
| 2233 | // display order, so every picture of the next group is a | 2290 | // display order, so every picture of the next group is a |
| 2234 | // display-future neighbour of it -- nearest first. | 2291 | // display-future neighbour of it -- nearest first. |
| @@ -3788,6 +3845,101 @@ mod tests { | |||
| 3788 | crate::speed::set_speed(was); | 3845 | crate::speed::set_speed(was); |
| 3789 | } | 3846 | } |
| 3790 | 3847 | ||
| 3848 | + /// lane-tplfut's correctness witness, the sibling of | ||
| 3849 | + /// [`the_lookahead_holds_one_group_and_still_codes_every_picture`]: with | ||
| 3850 | + /// the top ARF's TEMPORAL LAMBDA window taken out of the buffered next | ||
| 3851 | + /// group (`speed::TPL_FUT`), every picture is still coded, the streams | ||
| 3852 | + /// decode exactly through our decoder and ffmpeg, and the stream MOVES | ||
| 3853 | + /// against the knob off wherever a next group exists (class | ||
| 3854 | + /// gate-blind-to-feature). Coded at q=60, not 120: at a coarse | ||
| 3855 | + /// quantizer this fixture's ARF is almost pure skip and a different | ||
| 3856 | + /// lambda map re-quantises to identical bytes. | ||
| 3857 | + #[test] | ||
| 3858 | + fn the_future_tpl_window_still_codes_every_picture_and_moves_the_stream() { | ||
| 3859 | + let _knobs = crate::speed::knob_write(); | ||
| 3860 | + let was = crate::speed::speed(); | ||
| 3861 | + crate::speed::set_speed(0); | ||
| 3862 | + crate::encode::force_screen(Some(false)); | ||
| 3863 | + let (width, height) = (128usize, 128usize); | ||
| 3864 | + let run = |n: usize, gop: usize, fut: usize| -> (Vec<u8>, Vec<Packet>) { | ||
| 3865 | + set_lookahead(Some(true)); | ||
| 3866 | + set_arf_tf_future(Some(0)); | ||
| 3867 | + set_tpl_fut(Some(fut)); | ||
| 3868 | + let config = EncoderConfig { | ||
| 3869 | + base_q_idx: 60, | ||
| 3870 | + width, | ||
| 3871 | + height, | ||
| 3872 | + gop, | ||
| 3873 | + colour: Colour::Bt709Limited, | ||
| 3874 | + tile_cols_log2: 0, | ||
| 3875 | + tile_rows_log2: 0, | ||
| 3876 | + }; | ||
| 3877 | + let mut enc = Av1Encoder::with_pyramid(config, Pyramid::default()).unwrap(); | ||
| 3878 | + let sources: Vec<Picture> = (0..n).map(|t| noisy_card(width, height, t)).collect(); | ||
| 3879 | + let packets = encode_all(&mut enc, &sources); | ||
| 3880 | + assert!( | ||
| 3881 | + enc.pyramid().is_some(), | ||
| 3882 | + "{n} pictures at gop {gop}: the content gate dropped the pyramid (speed {})", | ||
| 3883 | + crate::speed::speed(), | ||
| 3884 | + ); | ||
| 3885 | + (concat(&packets), packets) | ||
| 3886 | + }; | ||
| 3887 | + for gop in [32usize, 8] { | ||
| 3888 | + for n in [1usize, 7, 8, 9, 17] { | ||
| 3889 | + let (off, off_packets) = run(n, gop, 0); | ||
| 3890 | + for mode in [1usize, 2, 3] { | ||
| 3891 | + let (on, packets) = run(n, gop, mode); | ||
| 3892 | + assert_eq!( | ||
| 3893 | + packets.iter().map(|p| (p.order, p.key, p.level)).collect::<Vec<_>>(), | ||
| 3894 | + off_packets.iter().map(|p| (p.order, p.key, p.level)).collect::<Vec<_>>(), | ||
| 3895 | + "{n} pictures at gop {gop} mode {mode}: coding order/levels moved" | ||
| 3896 | + ); | ||
| 3897 | + let ours = crate::stream::decode_stream(&on).expect("our decoder"); | ||
| 3898 | + assert_eq!(ours.len(), n, "{n} pictures at gop {gop} mode {mode}: our count"); | ||
| 3899 | + if !have_ffmpeg() { | ||
| 3900 | + continue; | ||
| 3901 | + } | ||
| 3902 | + let theirs = ffmpeg_decode_luma(&on, width, height); | ||
| 3903 | + assert_eq!(theirs.len(), n, "{n} pictures at gop {gop} mode {mode}: ffmpeg count"); | ||
| 3904 | + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { | ||
| 3905 | + let got: Vec<u8> = a.y.iter().map(|&v| v as u8).collect(); | ||
| 3906 | + assert!( | ||
| 3907 | + got == *b, | ||
| 3908 | + "{n} pictures at gop {gop} mode {mode}, frame {i}: ours differs from ffmpeg" | ||
| 3909 | + ); | ||
| 3910 | + } | ||
| 3911 | + } | ||
| 3912 | + let _ = &off; | ||
| 3913 | + } | ||
| 3914 | + } | ||
| 3915 | + // The future window REACHES the lambda map: 17 pictures at gop 32 is | ||
| 3916 | + // three groups, so the first two top ARFs each have a next group | ||
| 3917 | + // buffered and their windows really are display-future. | ||
| 3918 | + let (off, _) = run(17, 32, 0); | ||
| 3919 | + // Mode 2 is NOT in this list: at its default split it keeps the three | ||
| 3920 | + // NEAREST past neighbours, and those dominate the propagation -- on | ||
| 3921 | + // this 128x128 fixture the map moves too little to flip an RD | ||
| 3922 | + // decision and the stream comes back byte-identical to mode 0. Not a | ||
| 3923 | + // dead knob: the same run with `EC_AV1_TPL_FUT_HALF=1` (one past | ||
| 3924 | + // neighbour, six future) does move the stream, and the gate below | ||
| 3925 | + // measures mode 2 on real content. | ||
| 3926 | + for mode in [1usize, 3] { | ||
| 3927 | + let (on, _) = run(17, 32, mode); | ||
| 3928 | + assert!( | ||
| 3929 | + off != on, | ||
| 3930 | + "mode {mode} left the top ARF's temporal lambda map unchanged \ | ||
| 3931 | + ({} B vs {} B, preset {})", | ||
| 3932 | + off.len(), | ||
| 3933 | + on.len(), | ||
| 3934 | + crate::speed::speed() | ||
| 3935 | + ); | ||
| 3936 | + } | ||
| 3937 | + set_lookahead(None); | ||
| 3938 | + set_arf_tf_future(None); | ||
| 3939 | + set_tpl_fut(None); | ||
| 3940 | + crate::speed::set_speed(was); | ||
| 3941 | + } | ||
| 3942 | + | ||
| 3791 | fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) { | 3943 | fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) { |
| 3792 | let (width, height) = (128usize, 128usize); | 3944 | let (width, height) = (128usize, 128usize); |
| 3793 | let config = EncoderConfig { | 3945 | let config = EncoderConfig { |
crates/ec-av1/src/speed.rsmodified+22 -0
| @@ -475,6 +475,28 @@ pub(crate) const ARF_TF_WIN_FUT: [usize; 11] = [3; 11]; | |||
| 475 | /// two-pass-like allocation would need. | 475 | /// two-pass-like allocation would need. |
| 476 | pub(crate) const LOOKAHEAD: bool = true; | 476 | pub(crate) const LOOKAHEAD: bool = true; |
| 477 | 477 | ||
| 478 | +/// lane-tplfut: which pictures the TOP ARF's temporal lambda map | ||
| 479 | +/// ([`crate::encode::tpl_lambda_factors`]) propagates back through. The top | ||
| 480 | +/// ARF is its group's LAST picture in display order, so the group's own | ||
| 481 | +/// sources -- what [`crate::encoder::Av1Encoder::code_group`] hands it today | ||
| 482 | +/// -- are all display-PAST, i.e. the window is time-REVERSED against every | ||
| 483 | +/// other frame's (and against the flat path's, which always looks forward). | ||
| 484 | +/// libaom's tpl at an ARF looks FORWARD, over the next group. With | ||
| 485 | +/// [`LOOKAHEAD`] on those pictures are buffered, so the window is a choice: | ||
| 486 | +/// | ||
| 487 | +/// * `0` -- the past window (time-reversed), the shipped behaviour. | ||
| 488 | +/// * `1` -- the display-FUTURE pictures only, nearest first, up to | ||
| 489 | +/// `tpl_depth() - 1` of them. | ||
| 490 | +/// * `2` -- past then future, concatenated nearest-first. NOTE the chain has | ||
| 491 | +/// a discontinuity in the middle (the coarse pass between the last past | ||
| 492 | +/// neighbour and the nearest future one spans the whole group). | ||
| 493 | +/// * `3` -- future-only over the WHOLE next group, ignoring `tpl_depth()`. | ||
| 494 | +/// | ||
| 495 | +/// Falls back to `0`'s window whenever no future picture is buffered (the | ||
| 496 | +/// lookahead off, or the end of the stream). `EC_AV1_TPL_FUT=<n>` overrides | ||
| 497 | +/// it. | ||
| 498 | +pub(crate) const TPL_FUT: [usize; 11] = [0; 11]; | ||
| 499 | + | ||
| 478 | /// lane-arfpred: the qindex below which the ARF temporal filter is OFF -- | 500 | /// lane-arfpred: the qindex below which the ARF temporal filter is OFF -- |
| 479 | /// libaom scales `arnr` strength with the quantizer and stops filtering at | 501 | /// libaom scales `arnr` strength with the quantizer and stops filtering at |
| 480 | /// the high-quality end for the same reason: a picture we are about to | 502 | /// the high-quality end for the same reason: a picture we are about to |