One mini-GOP of lookahead behind a knob, and a future half for the ARF filter window
Browse files at this commita1a8777e23387b1387cb6c50408cc1d7b798372fParent: 53e1991- feat
- one mini-GOP of lookahead behind a knob, and a future half for the ARF filter window
3 changed files +271 -9
- crates/ec-av1/src/encode.rs +2 -1
- crates/ec-av1/src/encoder.rs +253 -8
- crates/ec-av1/src/speed.rs +16 -0
crates/ec-av1/src/encode.rsmodified+2 -1
| @@ -11706,12 +11706,13 @@ pub(crate) struct PyramidFrame { | |||
| 11706 | /// one's (film A 5.9%) with the two past neighbours at strength 30, and the | 11706 | /// one's (film A 5.9%) with the two past neighbours at strength 30, and the |
| 11707 | /// filtered picture is 50.1 dB from the source it replaces -- 4 dB above the | 11707 | /// filtered picture is 50.1 dB from the source it replaces -- 4 dB above the |
| 11708 | /// quality the ARF is coded at, so the substitution itself costs little. | 11708 | /// quality the ARF is coded at, so the substitution itself costs little. |
| 11709 | +// lane-lookahead: the CALLER sizes `window` (`ARF_TF_WIN` past neighbours and | ||
| 11710 | +// `ARF_TF_WIN_FUT` future ones); this takes every picture in it. | ||
| 11709 | pub(crate) fn arf_temporal_filter(src: &Picture, window: &[Picture], strength: f64) -> Picture { | 11711 | pub(crate) fn arf_temporal_filter(src: &Picture, window: &[Picture], strength: f64) -> Picture { |
| 11710 | const SIDE: usize = 16; | 11712 | const SIDE: usize = 16; |
| 11711 | let neighbours: Vec<&Picture> = window | 11713 | let neighbours: Vec<&Picture> = window |
| 11712 | .iter() | 11714 | .iter() |
| 11713 | .filter(|p| p.width == src.width && p.height == src.height) | 11715 | .filter(|p| p.width == src.width && p.height == src.height) |
| 11714 | - .take(crate::encoder::arf_tf_window()) | ||
| 11715 | .collect(); | 11716 | .collect(); |
| 11716 | if neighbours.is_empty() || strength <= 0.0 { | 11717 | if neighbours.is_empty() || strength <= 0.0 { |
| 11717 | return src.clone(); | 11718 | return src.clone(); |
crates/ec-av1/src/encoder.rsmodified+253 -8
| @@ -1238,6 +1238,58 @@ pub(crate) fn arf_tf_window() -> usize { | |||
| 1238 | .unwrap_or_else(|| crate::speed::at(&crate::speed::ARF_TF_WIN)) | 1238 | .unwrap_or_else(|| crate::speed::at(&crate::speed::ARF_TF_WIN)) |
| 1239 | } | 1239 | } |
| 1240 | 1240 | ||
| 1241 | +/// lane-lookahead: how many DISPLAY-FUTURE neighbours the top ARF's filter | ||
| 1242 | +/// takes out of the buffered next group ([`crate::speed::ARF_TF_WIN_FUT`], | ||
| 1243 | +/// `EC_AV1_ARF_TF_WIN_FUT=<n>`). Inert without [`lookahead`]. | ||
| 1244 | +pub(crate) fn arf_tf_window_future() -> usize { | ||
| 1245 | + match ARF_TF_FUT_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed) { | ||
| 1246 | + usize::MAX => {} | ||
| 1247 | + n => return n, | ||
| 1248 | + } | ||
| 1249 | + std::env::var("EC_AV1_ARF_TF_WIN_FUT") | ||
| 1250 | + .ok() | ||
| 1251 | + .and_then(|v| v.parse().ok()) | ||
| 1252 | + .unwrap_or_else(|| crate::speed::at(&crate::speed::ARF_TF_WIN_FUT)) | ||
| 1253 | +} | ||
| 1254 | + | ||
| 1255 | +/// PROCESS-GLOBAL overrides of the two lane-lookahead knobs, the shape | ||
| 1256 | +/// [`set_last2`] has (this crate's tests never call `set_var`); `u8::MAX` / | ||
| 1257 | +/// `usize::MAX` is unset, and a witness holds [`crate::speed::knob_write`] | ||
| 1258 | +/// while it uses them. | ||
| 1259 | +static LOOKAHEAD_OVERRIDE: std::sync::atomic::AtomicU8 = | ||
| 1260 | + std::sync::atomic::AtomicU8::new(u8::MAX); | ||
| 1261 | +static ARF_TF_FUT_OVERRIDE: std::sync::atomic::AtomicUsize = | ||
| 1262 | + std::sync::atomic::AtomicUsize::new(usize::MAX); | ||
| 1263 | + | ||
| 1264 | +/// Sets [`LOOKAHEAD_OVERRIDE`]; `None` clears it. | ||
| 1265 | +#[allow(dead_code)] // set only from the `#[cfg(test)]` witness | ||
| 1266 | +pub(crate) fn set_lookahead(on: Option<bool>) { | ||
| 1267 | + LOOKAHEAD_OVERRIDE.store( | ||
| 1268 | + on.map_or(u8::MAX, u8::from), | ||
| 1269 | + std::sync::atomic::Ordering::Relaxed, | ||
| 1270 | + ); | ||
| 1271 | +} | ||
| 1272 | + | ||
| 1273 | +/// Sets [`ARF_TF_FUT_OVERRIDE`]; `None` clears it. | ||
| 1274 | +#[allow(dead_code)] // set only from the `#[cfg(test)]` witness | ||
| 1275 | +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); | ||
| 1277 | +} | ||
| 1278 | + | ||
| 1279 | +/// lane-lookahead: whether the pyramid path holds one group of lookahead | ||
| 1280 | +/// ([`crate::speed::LOOKAHEAD`], `EC_AV1_LOOKAHEAD=0|1`). | ||
| 1281 | +fn lookahead() -> bool { | ||
| 1282 | + match LOOKAHEAD_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed) { | ||
| 1283 | + 0 => return false, | ||
| 1284 | + 1 => return true, | ||
| 1285 | + _ => {} | ||
| 1286 | + } | ||
| 1287 | + match std::env::var("EC_AV1_LOOKAHEAD").ok() { | ||
| 1288 | + Some(v) => v != "0", | ||
| 1289 | + None => crate::speed::LOOKAHEAD, | ||
| 1290 | + } | ||
| 1291 | +} | ||
| 1292 | + | ||
| 1241 | fn tpl_pyramid() -> bool { | 1293 | fn tpl_pyramid() -> bool { |
| 1242 | match std::env::var("EC_AV1_TPL_PYRAMID").ok() { | 1294 | match std::env::var("EC_AV1_TPL_PYRAMID").ok() { |
| 1243 | Some(v) => v != "0", | 1295 | Some(v) => v != "0", |
| @@ -1315,6 +1367,11 @@ pub struct Av1Encoder { | |||
| 1315 | dpb: [Option<DpbSlot>; 8], | 1367 | dpb: [Option<DpbSlot>; 8], |
| 1316 | /// Pictures held back waiting for their mini-GOP's hidden frame. | 1368 | /// Pictures held back waiting for their mini-GOP's hidden frame. |
| 1317 | pending: Vec<(u64, Picture)>, | 1369 | pending: Vec<(u64, Picture)>, |
| 1370 | + /// lane-lookahead: the CLOSED group waiting for the next group's sources | ||
| 1371 | + /// before it is coded, so its top ARF -- the group's last picture in | ||
| 1372 | + /// display order -- has display-future neighbours available. Always empty | ||
| 1373 | + /// with [`lookahead`] off. | ||
| 1374 | + ready: Vec<(u64, Picture)>, | ||
| 1318 | /// The flat path's delay queue: pictures whose packets later calls | 1375 | /// The flat path's delay queue: pictures whose packets later calls |
| 1319 | /// return, each coded with the ones behind it as its lookahead window | 1376 | /// return, each coded with the ones behind it as its lookahead window |
| 1320 | /// (lane-av1facade, deepened to [`crate::encode::tpl_depth`] - 1 on | 1377 | /// (lane-av1facade, deepened to [`crate::encode::tpl_depth`] - 1 on |
| @@ -1415,6 +1472,7 @@ impl Av1Encoder { | |||
| 1415 | pyramid: None, | 1472 | pyramid: None, |
| 1416 | dpb: [const { None }; 8], | 1473 | dpb: [const { None }; 8], |
| 1417 | pending: Vec::new(), | 1474 | pending: Vec::new(), |
| 1475 | + ready: Vec::new(), | ||
| 1418 | held: std::collections::VecDeque::new(), | 1476 | held: std::collections::VecDeque::new(), |
| 1419 | anchor: 0, | 1477 | anchor: 0, |
| 1420 | leaf_hist: Vec::new(), | 1478 | leaf_hist: Vec::new(), |
| @@ -1618,7 +1676,7 @@ impl Av1Encoder { | |||
| 1618 | // A key frame closes whatever group is open (there is nothing for | 1676 | // A key frame closes whatever group is open (there is nothing for |
| 1619 | // its leaves to point forward at across the boundary) and then | 1677 | // its leaves to point forward at across the boundary) and then |
| 1620 | // restarts the pyramid from the key itself. | 1678 | // restarts the pyramid from the key itself. |
| 1621 | - let mut packets = self.drain_pending()?; | 1679 | + let mut packets = self.drain_all(Some(picture))?; |
| 1622 | packets.push(self.encode_key(order, picture)?); | 1680 | packets.push(self.encode_key(order, picture)?); |
| 1623 | return Ok(packets); | 1681 | return Ok(packets); |
| 1624 | } | 1682 | } |
| @@ -1655,7 +1713,7 @@ impl Av1Encoder { | |||
| 1655 | } | 1713 | } |
| 1656 | return Ok(packets); | 1714 | return Ok(packets); |
| 1657 | } | 1715 | } |
| 1658 | - self.drain_pending() | 1716 | + self.drain_all(None) |
| 1659 | } | 1717 | } |
| 1660 | 1718 | ||
| 1661 | /// The flat path: one picture and its lookahead (the next source picture, | 1719 | /// The flat path: one picture and its lookahead (the next source picture, |
| @@ -1885,6 +1943,11 @@ impl Av1Encoder { | |||
| 1885 | // from, already padded (lane-arfcen). Empty codes the frame at one | 1943 | // from, already padded (lane-arfcen). Empty codes the frame at one |
| 1886 | // lambda, which is what the whole pyramid path used to do. | 1944 | // lambda, which is what the whole pyramid path used to do. |
| 1887 | lookahead: &[Picture], | 1945 | lookahead: &[Picture], |
| 1946 | + // lane-lookahead: DISPLAY-FUTURE pictures for the SOURCE temporal | ||
| 1947 | + // filter only (the top ARF's, out of the buffered next group), never | ||
| 1948 | + // for the temporal lambda map -- keeping the filter's window and the | ||
| 1949 | + // tpl window separate levers. Empty everywhere else. | ||
| 1950 | + future: &[Picture], | ||
| 1888 | ) -> Result<Packet> { | 1951 | ) -> Result<Packet> { |
| 1889 | let render = (self.config.width, self.config.height); | 1952 | let render = (self.config.width, self.config.height); |
| 1890 | let order_hint = (order & 0x7f) as u32; | 1953 | let order_hint = (order & 0x7f) as u32; |
| @@ -1973,7 +2036,18 @@ impl Av1Encoder { | |||
| 1973 | false => 0.0, | 2036 | false => 0.0, |
| 1974 | }; | 2037 | }; |
| 1975 | let source = match tf > 0.0 { | 2038 | let source = match tf > 0.0 { |
| 1976 | - true => crate::encode::arf_temporal_filter(&source, lookahead, tf), | 2039 | + true => { |
| 2040 | + // lane-lookahead: the filter's window is sized HERE -- up to | ||
| 2041 | + // `ARF_TF_WIN` display-past neighbours (nearest first) and | ||
| 2042 | + // then the display-future ones the caller buffered. | ||
| 2043 | + let window: Vec<Picture> = lookahead | ||
| 2044 | + .iter() | ||
| 2045 | + .take(arf_tf_window()) | ||
| 2046 | + .chain(future.iter()) | ||
| 2047 | + .cloned() | ||
| 2048 | + .collect(); | ||
| 2049 | + crate::encode::arf_temporal_filter(&source, &window, tf) | ||
| 2050 | + } | ||
| 1977 | false => source, | 2051 | false => source, |
| 1978 | }; | 2052 | }; |
| 1979 | let encoded = || -> Result<Encoded> { encode_inter_frame( | 2053 | let encoded = || -> Result<Encoded> { encode_inter_frame( |
| @@ -2029,14 +2103,52 @@ impl Av1Encoder { | |||
| 2029 | Ok(self.packet(cropped.stream, false, order, level)) | 2103 | Ok(self.packet(cropped.stream, false, order, level)) |
| 2030 | } | 2104 | } |
| 2031 | 2105 | ||
| 2032 | - /// Codes the open mini-GOP: its hidden frame first, then its leaves, then | 2106 | + /// Closes the open mini-GOP. With the lookahead off that codes it |
| 2033 | - /// the `show_existing_frame` header that puts the hidden frame back in | 2107 | + /// straight away, which is byte for byte what this encoder did before |
| 2034 | - /// display order. | 2108 | + /// lane-lookahead; with it on the open group takes the `ready` slot and |
| 2109 | + /// the group ALREADY there is coded, with the open one's sources as its | ||
| 2110 | + /// display-future window. | ||
| 2035 | fn drain_pending(&mut self) -> Result<Vec<Packet>> { | 2111 | fn drain_pending(&mut self) -> Result<Vec<Packet>> { |
| 2036 | - if self.pending.is_empty() { | 2112 | + if !lookahead() { |
| 2113 | + let group = std::mem::take(&mut self.pending); | ||
| 2114 | + return self.code_group(group, &[]); | ||
| 2115 | + } | ||
| 2116 | + let group = std::mem::take(&mut self.ready); | ||
| 2117 | + let next = std::mem::take(&mut self.pending); | ||
| 2118 | + let packets = self.code_group(group, &next); | ||
| 2119 | + self.ready = next; | ||
| 2120 | + packets | ||
| 2121 | + } | ||
| 2122 | + | ||
| 2123 | + /// Codes every group still buffered, in coding order -- what a key frame | ||
| 2124 | + /// (`next` is the key picture, the last buffered group's own | ||
| 2125 | + /// display-future) and the end of stream (`None`) both need. The tail | ||
| 2126 | + /// group can be any length, down to the single picture `code_group` | ||
| 2127 | + /// codes as one shown leaf. | ||
| 2128 | + fn drain_all(&mut self, next: Option<&Picture>) -> Result<Vec<Packet>> { | ||
| 2129 | + let tail: Vec<(u64, Picture)> = next | ||
| 2130 | + .filter(|_| lookahead()) | ||
| 2131 | + .map(|p| vec![(self.next_index, p.clone())]) | ||
| 2132 | + .unwrap_or_default(); | ||
| 2133 | + let ready = std::mem::take(&mut self.ready); | ||
| 2134 | + let pending = std::mem::take(&mut self.pending); | ||
| 2135 | + let mut packets = self.code_group(ready, &pending)?; | ||
| 2136 | + packets.extend(self.code_group(pending, &tail)?); | ||
| 2137 | + Ok(packets) | ||
| 2138 | + } | ||
| 2139 | + | ||
| 2140 | + /// Codes one mini-GOP: its hidden frame first, then its leaves, then | ||
| 2141 | + /// the `show_existing_frame` header that puts the hidden frame back in | ||
| 2142 | + /// display order. `future` is the display-future sources the caller has | ||
| 2143 | + /// buffered past this group (lane-lookahead), empty without a lookahead. | ||
| 2144 | + fn code_group( | ||
| 2145 | + &mut self, | ||
| 2146 | + group: Vec<(u64, Picture)>, | ||
| 2147 | + future: &[(u64, Picture)], | ||
| 2148 | + ) -> Result<Vec<Packet>> { | ||
| 2149 | + if group.is_empty() { | ||
| 2037 | return Ok(Vec::new()); | 2150 | return Ok(Vec::new()); |
| 2038 | } | 2151 | } |
| 2039 | - let group: Vec<(u64, Picture)> = std::mem::take(&mut self.pending); | ||
| 2040 | let mut packets = Vec::with_capacity(group.len() + 1); | 2152 | let mut packets = Vec::with_capacity(group.len() + 1); |
| 2041 | // The lookahead window of the group's picture at index `pos`: the | 2153 | // The lookahead window of the group's picture at index `pos`: the |
| 2042 | // display-order successors inside this group, or -- for the group's | 2154 | // display-order successors inside this group, or -- for the group's |
| @@ -2080,10 +2192,21 @@ impl Av1Encoder { | |||
| 2080 | Level::Leaf, | 2192 | Level::Leaf, |
| 2081 | pyramid.leaf_q_offset, | 2193 | pyramid.leaf_q_offset, |
| 2082 | &tpl_window(0, false), | 2194 | &tpl_window(0, false), |
| 2195 | + &[], | ||
| 2083 | )?); | 2196 | )?); |
| 2084 | self.anchor = 1 - self.anchor; | 2197 | self.anchor = 1 - self.anchor; |
| 2085 | return Ok(packets); | 2198 | return Ok(packets); |
| 2086 | } | 2199 | } |
| 2200 | + // lane-lookahead: the display-future half of the top ARF's temporal | ||
| 2201 | + // filter window, padded like the frame itself. Empty when the | ||
| 2202 | + // lookahead is off (`future` is then always empty) or when the filter | ||
| 2203 | + // asks for no future neighbours, which is what keeps the default | ||
| 2204 | + // stream byte-identical. | ||
| 2205 | + let arf_future: Vec<Picture> = future | ||
| 2206 | + .iter() | ||
| 2207 | + .take(arf_tf_window_future()) | ||
| 2208 | + .map(|(_, p)| p.padded_to(SUPERBLOCK)) | ||
| 2209 | + .collect(); | ||
| 2087 | // The group's top level: the last picture, hidden, off the anchor. | 2210 | // The group's top level: the last picture, hidden, off the anchor. |
| 2088 | packets.push(self.encode_pyramid_inter( | 2211 | packets.push(self.encode_pyramid_inter( |
| 2089 | arf_order, | 2212 | arf_order, |
| @@ -2106,6 +2229,10 @@ impl Av1Encoder { | |||
| 2106 | Level::Arf, | 2229 | Level::Arf, |
| 2107 | pyramid.arf_q_offset, | 2230 | pyramid.arf_q_offset, |
| 2108 | &tpl_window(group.len() - 1, true), | 2231 | &tpl_window(group.len() - 1, true), |
| 2232 | + // lane-lookahead: the group's top ARF is its LAST picture in | ||
| 2233 | + // display order, so every picture of the next group is a | ||
| 2234 | + // display-future neighbour of it -- nearest first. | ||
| 2235 | + &arf_future, | ||
| 2109 | )?); | 2236 | )?); |
| 2110 | let leaves = group.len() - 1; | 2237 | let leaves = group.len() - 1; |
| 2111 | // THE THIRD LEVEL: a second hidden frame at the middle of the leaf | 2238 | // THE THIRD LEVEL: a second hidden frame at the middle of the leaf |
| @@ -2140,6 +2267,7 @@ impl Av1Encoder { | |||
| 2140 | Level::Arf, | 2267 | Level::Arf, |
| 2141 | offset, | 2268 | offset, |
| 2142 | &tpl_window(at, false), | 2269 | &tpl_window(at, false), |
| 2270 | + &[], | ||
| 2143 | )?); | 2271 | )?); |
| 2144 | } | 2272 | } |
| 2145 | // THE FOURTH LEVEL: one hidden frame at the middle of each half of | 2273 | // THE FOURTH LEVEL: one hidden frame at the middle of each half of |
| @@ -2177,6 +2305,7 @@ impl Av1Encoder { | |||
| 2177 | Level::Arf, | 2305 | Level::Arf, |
| 2178 | offset, | 2306 | offset, |
| 2179 | &tpl_window(pos, false), | 2307 | &tpl_window(pos, false), |
| 2308 | + &[], | ||
| 2180 | )?); | 2309 | )?); |
| 2181 | } | 2310 | } |
| 2182 | } | 2311 | } |
| @@ -2253,6 +2382,7 @@ impl Av1Encoder { | |||
| 2253 | Level::Leaf, | 2382 | Level::Leaf, |
| 2254 | pyramid.leaf_q_offset, | 2383 | pyramid.leaf_q_offset, |
| 2255 | &tpl_window(i, false), | 2384 | &tpl_window(i, false), |
| 2385 | + &[], | ||
| 2256 | )?); | 2386 | )?); |
| 2257 | } | 2387 | } |
| 2258 | let packet = show_existing(self, next_anchor_slot, arf_order)?; | 2388 | let packet = show_existing(self, next_anchor_slot, arf_order)?; |
| @@ -3515,6 +3645,121 @@ mod tests { | |||
| 3515 | assert_eq!(shown, arfs, "one show_existing_frame per hidden frame"); | 3645 | assert_eq!(shown, arfs, "one show_existing_frame per hidden frame"); |
| 3516 | } | 3646 | } |
| 3517 | 3647 | ||
| 3648 | + /// A picture whose content is a static gradient plus a per-picture | ||
| 3649 | + /// pseudo-random +-2 of noise: motion search matches the neighbours | ||
| 3650 | + /// almost exactly, so the ARF temporal filter's weights are near 1 and | ||
| 3651 | + /// the filter actually changes the source (a moving test card gives every | ||
| 3652 | + /// neighbour a large MSE, weight ~0, and the filter is a no-op that would | ||
| 3653 | + /// make the future-window witness below pass blind). | ||
| 3654 | + fn noisy_card(width: usize, height: usize, t: usize) -> Picture { | ||
| 3655 | + let mut picture = Picture::grey(width, height); | ||
| 3656 | + let mut seed = (t as u64 + 1).wrapping_mul(0x9e37_79b9_7f4a_7c15); | ||
| 3657 | + for y in 0..height { | ||
| 3658 | + for x in 0..width { | ||
| 3659 | + seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); | ||
| 3660 | + let noise = ((seed >> 33) % 5) as i32 - 2; | ||
| 3661 | + let base = 40 + (x + y) as i32 / 2; | ||
| 3662 | + picture.y[y * width + x] = base.saturating_add(noise).clamp(0, 255) as u16; | ||
| 3663 | + } | ||
| 3664 | + } | ||
| 3665 | + for i in 0..(width / 2) * (height / 2) { | ||
| 3666 | + picture.u[i] = 110; | ||
| 3667 | + picture.v[i] = 140; | ||
| 3668 | + } | ||
| 3669 | + picture | ||
| 3670 | + } | ||
| 3671 | + | ||
| 3672 | + /// lane-lookahead's correctness witness. One group of lookahead | ||
| 3673 | + /// (`Av1Encoder::drain_pending` hands the closed group to `ready` and | ||
| 3674 | + /// codes the group before it) must not change WHAT is coded: | ||
| 3675 | + /// | ||
| 3676 | + /// * with the future half of the filter window empty the stream is byte | ||
| 3677 | + /// for byte the one the encoder coded without a lookahead, at every | ||
| 3678 | + /// run length -- 1 picture (no hidden frame at all), 7 (a short group), | ||
| 3679 | + /// 8 (exactly `mini_gop`), 9 (a group plus a tail) and 17 -- and at a | ||
| 3680 | + /// GOP short enough (8) that key frames drain the buffer mid-run; | ||
| 3681 | + /// * every run decodes to exactly its picture count through OUR decoder | ||
| 3682 | + /// and through ffmpeg, sample-exact between the two, so the flush and | ||
| 3683 | + /// the `show_existing_frame` order survive the delay; | ||
| 3684 | + /// * and with ONE future neighbour the top ARF's source filter actually | ||
| 3685 | + /// reads it -- the stream MOVES (class gate-blind-to-feature: a | ||
| 3686 | + /// buffered picture nothing consumes would pass every check above). | ||
| 3687 | + #[test] | ||
| 3688 | + fn the_lookahead_holds_one_group_and_still_codes_every_picture() { | ||
| 3689 | + let _knobs = crate::speed::knob_write(); | ||
| 3690 | + crate::encode::force_screen(Some(false)); | ||
| 3691 | + let (width, height) = (128usize, 128usize); | ||
| 3692 | + let run = |n: usize, gop: usize, on: bool, fut: usize| -> (Vec<u8>, Vec<Packet>) { | ||
| 3693 | + set_lookahead(Some(on)); | ||
| 3694 | + set_arf_tf_future(Some(fut)); | ||
| 3695 | + let config = EncoderConfig { | ||
| 3696 | + width, | ||
| 3697 | + height, | ||
| 3698 | + base_q_idx: 120, | ||
| 3699 | + gop, | ||
| 3700 | + colour: Colour::Bt709Limited, | ||
| 3701 | + tile_cols_log2: 0, | ||
| 3702 | + tile_rows_log2: 0, | ||
| 3703 | + }; | ||
| 3704 | + let mut enc = Av1Encoder::with_pyramid(config, Pyramid::default()).unwrap(); | ||
| 3705 | + let sources: Vec<Picture> = (0..n).map(|t| noisy_card(width, height, t)).collect(); | ||
| 3706 | + let packets = encode_all(&mut enc, &sources); | ||
| 3707 | + (concat(&packets), packets) | ||
| 3708 | + }; | ||
| 3709 | + for gop in [32usize, 8] { | ||
| 3710 | + for n in [1usize, 7, 8, 9, 17] { | ||
| 3711 | + let (flat, flat_packets) = run(n, gop, false, 0); | ||
| 3712 | + let (held, packets) = run(n, gop, true, 0); | ||
| 3713 | + assert_eq!( | ||
| 3714 | + flat.len(), | ||
| 3715 | + held.len(), | ||
| 3716 | + "{n} pictures at gop {gop}: the lookahead changed the stream LENGTH" | ||
| 3717 | + ); | ||
| 3718 | + assert!(flat == held, "{n} pictures at gop {gop}: the stream is not byte-identical"); | ||
| 3719 | + assert_eq!( | ||
| 3720 | + packets.iter().map(|p| (p.order, p.key, p.level)).collect::<Vec<_>>(), | ||
| 3721 | + flat_packets.iter().map(|p| (p.order, p.key, p.level)).collect::<Vec<_>>(), | ||
| 3722 | + "{n} pictures at gop {gop}: coding order/levels moved" | ||
| 3723 | + ); | ||
| 3724 | + let ours = crate::stream::decode_stream(&held).expect("our decoder"); | ||
| 3725 | + assert_eq!(ours.len(), n, "{n} pictures at gop {gop}: our decoder's count"); | ||
| 3726 | + if !have_ffmpeg() { | ||
| 3727 | + continue; | ||
| 3728 | + } | ||
| 3729 | + let theirs = ffmpeg_decode_luma(&held, width, height); | ||
| 3730 | + assert_eq!(theirs.len(), n, "{n} pictures at gop {gop}: ffmpeg's count"); | ||
| 3731 | + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { | ||
| 3732 | + let got: Vec<u8> = a.y.iter().map(|&v| v as u8).collect(); | ||
| 3733 | + assert!( | ||
| 3734 | + got == *b, | ||
| 3735 | + "{n} pictures at gop {gop}, display frame {i}: ours differs from ffmpeg" | ||
| 3736 | + ); | ||
| 3737 | + } | ||
| 3738 | + } | ||
| 3739 | + } | ||
| 3740 | + // The future half REACHES the filter, and what it produces still | ||
| 3741 | + // decodes exactly. | ||
| 3742 | + let (base, _) = run(17, 32, true, 0); | ||
| 3743 | + let (symmetric, _) = run(17, 32, true, 1); | ||
| 3744 | + set_lookahead(None); | ||
| 3745 | + set_arf_tf_future(None); | ||
| 3746 | + assert!( | ||
| 3747 | + base != symmetric, | ||
| 3748 | + "one display-future neighbour left the top ARF's filter unchanged ({} B both)", | ||
| 3749 | + base.len() | ||
| 3750 | + ); | ||
| 3751 | + let ours = crate::stream::decode_stream(&symmetric).expect("our decoder"); | ||
| 3752 | + assert_eq!(ours.len(), 17, "the symmetric arm's display-order count"); | ||
| 3753 | + if have_ffmpeg() { | ||
| 3754 | + let theirs = ffmpeg_decode_luma(&symmetric, width, height); | ||
| 3755 | + assert_eq!(theirs.len(), 17, "the symmetric arm's ffmpeg count"); | ||
| 3756 | + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { | ||
| 3757 | + let got: Vec<u8> = a.y.iter().map(|&v| v as u8).collect(); | ||
| 3758 | + assert!(got == *b, "symmetric arm, display frame {i}: ours differs from ffmpeg"); | ||
| 3759 | + } | ||
| 3760 | + } | ||
| 3761 | + } | ||
| 3762 | + | ||
| 3518 | fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) { | 3763 | fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) { |
| 3519 | let (width, height) = (128usize, 128usize); | 3764 | let (width, height) = (128usize, 128usize); |
| 3520 | let config = EncoderConfig { | 3765 | let config = EncoderConfig { |
crates/ec-av1/src/speed.rsmodified+16 -0
| @@ -435,6 +435,22 @@ pub(crate) const ARF_TF_MID: [f64; 11] = [0.0; 11]; | |||
| 435 | /// the leaves it has to predict. | 435 | /// the leaves it has to predict. |
| 436 | pub(crate) const ARF_TF_WIN: [usize; 11] = [2; 11]; | 436 | pub(crate) const ARF_TF_WIN: [usize; 11] = [2; 11]; |
| 437 | 437 | ||
| 438 | +/// lane-lookahead: how many DISPLAY-FUTURE neighbours the top ARF's temporal | ||
| 439 | +/// filter averages in, on top of [`ARF_TF_WIN`] past ones. Non-zero needs | ||
| 440 | +/// [`LOOKAHEAD`] -- the future pictures are the next group's sources, which | ||
| 441 | +/// the encoder buffers only with the lookahead on. | ||
| 442 | +/// `EC_AV1_ARF_TF_WIN_FUT=<n>` overrides it. | ||
| 443 | +pub(crate) const ARF_TF_WIN_FUT: [usize; 11] = [0; 11]; | ||
| 444 | + | ||
| 445 | +/// lane-lookahead: whether [`crate::encoder::Av1Encoder`] holds one whole | ||
| 446 | +/// mini-GOP of lookahead -- the next group's sources are buffered before the | ||
| 447 | +/// current group's top ARF is coded, so that ARF has display-FUTURE pictures | ||
| 448 | +/// available. Costs one group of latency (`mini_gop` pictures, 8 by default: | ||
| 449 | +/// ~99 MB of buffered source at 4K 8-bit 4:2:0) and nothing else: with | ||
| 450 | +/// [`ARF_TF_WIN_FUT`] at 0 nothing reads the future half and the streams are | ||
| 451 | +/// byte-identical. `EC_AV1_LOOKAHEAD=0|1` overrides it. | ||
| 452 | +pub(crate) const LOOKAHEAD: bool = false; | ||
| 453 | + | ||
| 438 | /// lane-arfpred: the qindex below which the ARF temporal filter is OFF -- | 454 | /// lane-arfpred: the qindex below which the ARF temporal filter is OFF -- |
| 439 | /// libaom scales `arnr` strength with the quantizer and stops filtering at | 455 | /// libaom scales `arnr` strength with the quantizer and stops filtering at |
| 440 | /// the high-quality end for the same reason: a picture we are about to | 456 | /// the high-quality end for the same reason: a picture we are about to |