Merge branch 'lane-av1tx2' (e67d4eda): inter TxMode::Select on by default -- the writer counted TXFM_CONTEXT bands of neighbours outside the tile (decoder drops the term), var-tx depth-1 RD search (whole block vs four halves), own-decoder sequence round trip test naming the first differing sample; BD-rate vs rav1e 156/172/72, vs libaom 213/226/167; ffmpeg decodes every stream sample-exact
Browse files at this commit2 changed files +258 -32
- crates/ec-av1/src/encode.rs +195 -17
- crates/ec-av1/src/tile.rs +63 -15
crates/ec-av1/src/encode.rsmodified+195 -17
| @@ -124,19 +124,22 @@ const SPLIT_INTER_8: bool = true; | |||
| 124 | /// Whether a key frame codes `tx_mode == TxMode::Select` and searches each | 124 | /// Whether a key frame codes `tx_mode == TxMode::Select` and searches each |
| 125 | /// block's transform depth (lane-av1tx). On by default; `EC_AV1_TX_SELECT=0` | 125 | /// block's transform depth (lane-av1tx). On by default; `EC_AV1_TX_SELECT=0` |
| 126 | /// turns it off for an A/B on one build. | 126 | /// turns it off for an A/B on one build. |
| 127 | -/// Whether an INTER frame also carries `TxMode::Select` (lane-av1tx step 2: | 127 | +/// Whether an INTER frame also carries `TxMode::Select`: an inter block's |
| 128 | -/// one `txfm_split` flag per inter block, a searched `tx_depth` on every | 128 | +/// residual as one transform over the whole block or as the four transforms |
| 129 | -/// intra block inside the inter frame). OFF by default and opt-in through | 129 | +/// of half the side ([`commit_inter_luma`]), a searched `tx_depth` on every |
| 130 | -/// `EC_AV1_TX_SELECT_INTER=1`: the syntax is written but a stream carrying it | 130 | +/// intra block inside the inter frame. ON since lane-av1tx2, which found why |
| 131 | -/// is refused by dav1d and by this crate's own decoder, and the first | 131 | +/// the streams desynced -- the writer counted the [`crate::decode:: |
| 132 | -/// diverging symbol was not located inside the lane's budget. Key frames are | 132 | +/// TXFM_CTX_INIT`] band value of a neighbour outside the tile, where the |
| 133 | -/// unaffected -- they are byte-exact with [`tx_select`] on. | 133 | +/// reader drops the term -- and measured the split search: BD-rate vs libaom |
| 134 | +/// 233.5 -> 226.1 and 170.9 -> 167.4 on two clips, 212.8 -> 213.2 on the | ||
| 135 | +/// third, for 13% encode wall. `EC_AV1_TX_SELECT_INTER=0` turns it off for an | ||
| 136 | +/// A/B on one build. | ||
| 134 | fn tx_select_inter() -> bool { | 137 | fn tx_select_inter() -> bool { |
| 135 | static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); | 138 | static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); |
| 136 | *ON.get_or_init(|| { | 139 | *ON.get_or_init(|| { |
| 137 | - matches!( | 140 | + !matches!( |
| 138 | std::env::var("EC_AV1_TX_SELECT_INTER").as_deref(), | 141 | std::env::var("EC_AV1_TX_SELECT_INTER").as_deref(), |
| 139 | - Ok("1") | Ok("on") | 142 | + Ok("0") | Ok("off") |
| 140 | ) | 143 | ) |
| 141 | }) | 144 | }) |
| 142 | } | 145 | } |
| @@ -1798,6 +1801,111 @@ pub(crate) fn take_tx_depth_hits() -> [usize; 3] { | |||
| 1798 | std::array::from_fn(|d| TX_DEPTH_HITS[d].swap(0, std::sync::atomic::Ordering::Relaxed)) | 1801 | std::array::from_fn(|d| TX_DEPTH_HITS[d].swap(0, std::sync::atomic::Ordering::Relaxed)) |
| 1799 | } | 1802 | } |
| 1800 | 1803 | ||
| 1804 | +/// How many inter blocks resolved to each var-tx depth (0 = one transform | ||
| 1805 | +/// over the whole block, 1 = its four halves), the fire count a gate prints | ||
| 1806 | +/// to see the split search reach its candidates at all. | ||
| 1807 | +static INTER_TX_SPLIT_HITS: [std::sync::atomic::AtomicUsize; 2] = | ||
| 1808 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 2]; | ||
| 1809 | + | ||
| 1810 | +/// Reads [`INTER_TX_SPLIT_HITS`] and zeroes it. | ||
| 1811 | +#[cfg(test)] | ||
| 1812 | +pub(crate) fn take_inter_tx_split_hits() -> [usize; 2] { | ||
| 1813 | + std::array::from_fn(|d| INTER_TX_SPLIT_HITS[d].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 1814 | +} | ||
| 1815 | + | ||
| 1816 | +/// What the `txfm_split` symbols of one inter block cost at `depth`, priced | ||
| 1817 | +/// off the neighbour-free context row the way [`tx_depth_bits`] is: depth 0 | ||
| 1818 | +/// is one flag, depth 1 is a split root plus one unsplit flag per child | ||
| 1819 | +/// (decode.rs `read_var_tx_size` stops symbol-free only at `MAX_VARTX_DEPTH` | ||
| 1820 | +/// or a 4x4 sub-size). | ||
| 1821 | +fn txfm_split_bits(side: usize, depth: usize) -> f64 { | ||
| 1822 | + let max_tx = side.min(64); | ||
| 1823 | + let ctx = |tx: usize| { | ||
| 1824 | + crate::decode::txfm_partition_ctx_rect( | ||
| 1825 | + crate::decode::TXFM_CTX_INIT, | ||
| 1826 | + crate::decode::TXFM_CTX_INIT, | ||
| 1827 | + side, | ||
| 1828 | + tx, | ||
| 1829 | + tx, | ||
| 1830 | + ) | ||
| 1831 | + }; | ||
| 1832 | + if depth == 0 { | ||
| 1833 | + return symbol_bits(&cdf::TXFM_PARTITION[ctx(max_tx)], 0); | ||
| 1834 | + } | ||
| 1835 | + symbol_bits(&cdf::TXFM_PARTITION[ctx(max_tx)], 1) | ||
| 1836 | + + 4.0 * symbol_bits(&cdf::TXFM_PARTITION[ctx(max_tx / 2)], 0) | ||
| 1837 | +} | ||
| 1838 | + | ||
| 1839 | +/// The inter block's own var-tx decision, once its motion vector is settled: | ||
| 1840 | +/// its residual as ONE transform over the whole block, or as the four | ||
| 1841 | +/// transforms of half the side the tree's first halving names. The prediction | ||
| 1842 | +/// is the same motion compensation either way, so the split trial | ||
| 1843 | +/// re-transforms the identical residual in four pieces -- it wins where four | ||
| 1844 | +/// small transforms carry a locally varying residual cheaper than one big | ||
| 1845 | +/// one. Commits the winner into `luma` and hands back its block-coordinate | ||
| 1846 | +/// levels, the depth the writer codes and the cost the winner adds to (or | ||
| 1847 | +/// takes off) the caller's already-computed flat cost. | ||
| 1848 | +/// | ||
| 1849 | +/// Only for a block wholly inside the frame: past the true edge the reader's | ||
| 1850 | +/// var-tx recursion skips the units that fall outside | ||
| 1851 | +/// (decode.rs `read_var_tx_size`'s `max_w_mi`/`max_h_mi` guard), which would | ||
| 1852 | +/// leave the residual units this writes unpaired. | ||
| 1853 | +#[allow(clippy::too_many_arguments)] | ||
| 1854 | +fn commit_inter_luma( | ||
| 1855 | + luma: &mut Plane, | ||
| 1856 | + (x, y): (usize, usize), | ||
| 1857 | + side: usize, | ||
| 1858 | + mv: (i32, i32), | ||
| 1859 | + reference: (&[u16], usize, usize, usize), | ||
| 1860 | + search: &Search, | ||
| 1861 | + flat: &Trial, | ||
| 1862 | + skip: bool, fctx: &crate::decode::FrameCtx, | ||
| 1863 | +) -> (Vec<i32>, u8, f64) { | ||
| 1864 | + let eligible = tx_select() | ||
| 1865 | + && tx_select_inter() | ||
| 1866 | + && !skip | ||
| 1867 | + && side >= 16 | ||
| 1868 | + && x + side <= luma.true_width | ||
| 1869 | + && y + side <= luma.true_height; | ||
| 1870 | + if !eligible { | ||
| 1871 | + luma.commit(x, y, side, flat); | ||
| 1872 | + return (flat.levels.clone(), 0, 0.0); | ||
| 1873 | + } | ||
| 1874 | + let tx = side / 2; | ||
| 1875 | + let set = if tx >= 16 { TxbSet::Luma16Inter } else { TxbSet::Luma8Inter }; | ||
| 1876 | + let mut levels = vec![0i32; side * side]; | ||
| 1877 | + let (mut sse, mut bits) = (0.0, 0.0); | ||
| 1878 | + let mut units = Vec::with_capacity(4); | ||
| 1879 | + for tu_row in 0..2 { | ||
| 1880 | + for tu_col in 0..2 { | ||
| 1881 | + let (tu_x, tu_y) = (x + tu_col * tx, y + tu_row * tx); | ||
| 1882 | + let trial = mc_trial( | ||
| 1883 | + luma, tu_x, tu_y, tx, mv, true, reference.0, reference.1, reference.2, | ||
| 1884 | + reference.3, false, search.base_q_idx, search.deadzone, set, fctx, | ||
| 1885 | + ); | ||
| 1886 | + sse += trial.sse; | ||
| 1887 | + bits += trial.bits; | ||
| 1888 | + for row in 0..tx { | ||
| 1889 | + levels[(tu_row * tx + row) * side + tu_col * tx..][..tx] | ||
| 1890 | + .copy_from_slice(&trial.levels[row * tx..][..tx]); | ||
| 1891 | + } | ||
| 1892 | + units.push((tu_x, tu_y, trial)); | ||
| 1893 | + } | ||
| 1894 | + } | ||
| 1895 | + let split_cost = sse + search.lambda * (bits + txfm_split_bits(side, 1)); | ||
| 1896 | + let flat_cost = flat.sse + search.lambda * (flat.bits + txfm_split_bits(side, 0)); | ||
| 1897 | + if split_cost < flat_cost { | ||
| 1898 | + for (tu_x, tu_y, trial) in &units { | ||
| 1899 | + luma.commit(*tu_x, *tu_y, tx, trial); | ||
| 1900 | + } | ||
| 1901 | + INTER_TX_SPLIT_HITS[1].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 1902 | + return (levels, 1, split_cost - flat_cost); | ||
| 1903 | + } | ||
| 1904 | + INTER_TX_SPLIT_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 1905 | + luma.commit(x, y, side, flat); | ||
| 1906 | + (flat.levels.clone(), 0, 0.0) | ||
| 1907 | +} | ||
| 1908 | + | ||
| 1801 | #[allow(clippy::too_many_arguments)] | 1909 | #[allow(clippy::too_many_arguments)] |
| 1802 | fn code_square( | 1910 | fn code_square( |
| 1803 | luma: &mut Plane, | 1911 | luma: &mut Plane, |
| @@ -2195,47 +2303,51 @@ fn code_square_inter( | |||
| 2195 | } | 2303 | } |
| 2196 | if let (cost, Some((luma_new, u_new, v_new, skip_new, new_mv))) = best { | 2304 | if let (cost, Some((luma_new, u_new, v_new, skip_new, new_mv))) = best { |
| 2197 | if cost < intra_cost { | 2305 | if cost < intra_cost { |
| 2198 | - luma.commit(x, y, side, &luma_new); | 2306 | + let (levels, tx_depth, dcost) = commit_inter_luma( |
| 2307 | + luma, (x, y), side, new_mv, (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), search, &luma_new, skip_new, fctx, | ||
| 2308 | + ); | ||
| 2199 | chroma[0].commit(x / 2, y / 2, side / 2, &u_new); | 2309 | chroma[0].commit(x / 2, y / 2, side / 2, &u_new); |
| 2200 | chroma[1].commit(x / 2, y / 2, side / 2, &v_new); | 2310 | chroma[1].commit(x / 2, y / 2, side / 2, &v_new); |
| 2201 | return ( | 2311 | return ( |
| 2202 | BlockCoeffs { | 2312 | BlockCoeffs { |
| 2203 | - luma: coeffs(&luma_new.levels, side), | 2313 | + luma: coeffs(&levels, side), |
| 2204 | u: coeffs(&u_new.levels, side / 2), | 2314 | u: coeffs(&u_new.levels, side / 2), |
| 2205 | v: coeffs(&v_new.levels, side / 2), | 2315 | v: coeffs(&v_new.levels, side / 2), |
| 2206 | mode: DC_PRED as u8, | 2316 | mode: DC_PRED as u8, |
| 2207 | uv_mode: DC_PRED, | 2317 | uv_mode: DC_PRED, |
| 2208 | skip: skip_new, | 2318 | skip: skip_new, |
| 2209 | eight: None, | 2319 | eight: None, |
| 2210 | - tx_depth: 0, | 2320 | + tx_depth, |
| 2211 | inter: Some(InterInfo { | 2321 | inter: Some(InterInfo { |
| 2212 | mode: InterMode::NewMv, | 2322 | mode: InterMode::NewMv, |
| 2213 | mv: new_mv, | 2323 | mv: new_mv, |
| 2214 | }), | 2324 | }), |
| 2215 | }, | 2325 | }, |
| 2216 | - cost, | 2326 | + cost + dcost, |
| 2217 | ); | 2327 | ); |
| 2218 | } | 2328 | } |
| 2219 | } | 2329 | } |
| 2220 | 2330 | ||
| 2221 | if inter_cost < intra_cost { | 2331 | if inter_cost < intra_cost { |
| 2222 | - luma.commit(x, y, side, &luma_trial); | 2332 | + let (levels, tx_depth, dcost) = commit_inter_luma( |
| 2333 | + luma, (x, y), side, mv, (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), search, &luma_trial, skip, fctx, | ||
| 2334 | + ); | ||
| 2223 | chroma[0].commit(x / 2, y / 2, side / 2, &u); | 2335 | chroma[0].commit(x / 2, y / 2, side / 2, &u); |
| 2224 | chroma[1].commit(x / 2, y / 2, side / 2, &v); | 2336 | chroma[1].commit(x / 2, y / 2, side / 2, &v); |
| 2225 | (BlockCoeffs { | 2337 | (BlockCoeffs { |
| 2226 | - luma: coeffs(&luma_trial.levels, side), | 2338 | + luma: coeffs(&levels, side), |
| 2227 | u: coeffs(&u.levels, side / 2), | 2339 | u: coeffs(&u.levels, side / 2), |
| 2228 | v: coeffs(&v.levels, side / 2), | 2340 | v: coeffs(&v.levels, side / 2), |
| 2229 | mode: DC_PRED as u8, | 2341 | mode: DC_PRED as u8, |
| 2230 | uv_mode: DC_PRED, | 2342 | uv_mode: DC_PRED, |
| 2231 | skip, | 2343 | skip, |
| 2232 | eight: None, | 2344 | eight: None, |
| 2233 | - tx_depth: 0, | 2345 | + tx_depth, |
| 2234 | inter: Some(InterInfo { | 2346 | inter: Some(InterInfo { |
| 2235 | mode: InterMode::NearestMv, | 2347 | mode: InterMode::NearestMv, |
| 2236 | mv, | 2348 | mv, |
| 2237 | }), | 2349 | }), |
| 2238 | - }, inter_cost) | 2350 | + }, inter_cost + dcost) |
| 2239 | } else { | 2351 | } else { |
| 2240 | (intra_block, intra_cost) | 2352 | (intra_block, intra_cost) |
| 2241 | } | 2353 | } |
| @@ -5902,6 +6014,55 @@ mod tests { | |||
| 5902 | } | 6014 | } |
| 5903 | } | 6015 | } |
| 5904 | 6016 | ||
| 6017 | + /// The same sequence through OUR OWN decoder, sample-exact against the | ||
| 6018 | + /// encoder's reconstruction. ffmpeg stays the bitstream oracle | ||
| 6019 | + /// ([[shared-oracle-blindness]]) -- but a stream ffmpeg REFUSES yields no | ||
| 6020 | + /// divergence point at all, while our decoder can be traced symbol by | ||
| 6021 | + /// symbol (`EC_TRACE_MODE_STEP`) on the very stream that broke. This is | ||
| 6022 | + /// the instrument the inter `TxMode::Select` hunt lacked; on failure it | ||
| 6023 | + /// names the first differing frame, plane and position. | ||
| 6024 | + #[test] | ||
| 6025 | + fn every_frame_of_a_sequence_decodes_through_our_own_decoder() { | ||
| 6026 | + let fctx = &crate::decode::FrameCtx::new(); | ||
| 6027 | + let (width, height) = (128usize, 64usize); | ||
| 6028 | + let pictures: Vec<Picture> = | ||
| 6029 | + (0..5).map(|i| panned_test_card(width, height, i * 3)).collect(); | ||
| 6030 | + let _ = take_inter_tx_split_hits(); | ||
| 6031 | + let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap(); | ||
| 6032 | + let split = take_inter_tx_split_hits(); | ||
| 6033 | + eprintln!("inter var-tx: {} blocks flat, {} split", split[0], split[1]); | ||
| 6034 | + // [[gate-blind-to-feature]]: with inter `TxMode::Select` on, this | ||
| 6035 | + // content splits -- a green round trip in which the split never fires | ||
| 6036 | + // would prove only the flat path. | ||
| 6037 | + if tx_select() && tx_select_inter() { | ||
| 6038 | + assert!(split[1] > 0, "the inter var-tx split never fired"); | ||
| 6039 | + } | ||
| 6040 | + let decoded = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); | ||
| 6041 | + assert_eq!(decoded.len(), encoded.frames.len(), "frame count"); | ||
| 6042 | + for (i, (frame, dec)) in encoded.frames.iter().zip(&decoded).enumerate() { | ||
| 6043 | + let recon = &frame.reconstruction; | ||
| 6044 | + for (plane, got, want, stride) in [ | ||
| 6045 | + ("luma", &dec.y, &recon.y, width), | ||
| 6046 | + ("U", &dec.u, &recon.u, width / 2), | ||
| 6047 | + ("V", &dec.v, &recon.v, width / 2), | ||
| 6048 | + ] { | ||
| 6049 | + assert_eq!(got.len(), want.len(), "frame {i}: {plane} size"); | ||
| 6050 | + if let Some(at) = got.iter().zip(want).position(|(a, b)| a != b) { | ||
| 6051 | + panic!( | ||
| 6052 | + "frame {i}: {plane} differs first at ({}, {}): decoded {} vs \ | ||
| 6053 | + reconstruction {} ({} of {} samples differ)", | ||
| 6054 | + at % stride, | ||
| 6055 | + at / stride, | ||
| 6056 | + got[at], | ||
| 6057 | + want[at], | ||
| 6058 | + got.iter().zip(want).filter(|(a, b)| a != b).count(), | ||
| 6059 | + got.len(), | ||
| 6060 | + ); | ||
| 6061 | + } | ||
| 6062 | + } | ||
| 6063 | + } | ||
| 6064 | + } | ||
| 6065 | + | ||
| 5905 | /// The rate term the mode search ranks every decision by has to be the | 6066 | /// The rate term the mode search ranks every decision by has to be the |
| 5906 | /// rate the writer then spends. The search prices a block's coefficients | 6067 | /// rate the writer then spends. The search prices a block's coefficients |
| 5907 | /// through `tile::coeff_bits` -- the writer's own symbol chain, but | 6068 | /// through `tile::coeff_bits` -- the writer's own symbol chain, but |
| @@ -6866,6 +7027,23 @@ mod tests { | |||
| 6866 | .collect::<Vec<_>>() | 7027 | .collect::<Vec<_>>() |
| 6867 | .join(" ") | 7028 | .join(" ") |
| 6868 | ); | 7029 | ); |
| 7030 | + // And which var-tx depth this clip's INTER blocks resolved to | ||
| 7031 | + // (lane-av1tx2): 0 is one transform over the whole block, 1 its | ||
| 7032 | + // four halves. | ||
| 7033 | + let splits = take_inter_tx_split_hits(); | ||
| 7034 | + let split_total: usize = splits.iter().sum(); | ||
| 7035 | + eprintln!( | ||
| 7036 | + "{name}: inter var-tx depths {}", | ||
| 7037 | + splits | ||
| 7038 | + .iter() | ||
| 7039 | + .enumerate() | ||
| 7040 | + .map(|(d, &n)| format!( | ||
| 7041 | + "{d}={n} ({:.1}%)", | ||
| 7042 | + 100.0 * n as f64 / split_total.max(1) as f64 | ||
| 7043 | + )) | ||
| 7044 | + .collect::<Vec<_>>() | ||
| 7045 | + .join(" ") | ||
| 7046 | + ); | ||
| 6869 | let (aom, aom_wall) = | 7047 | let (aom, aom_wall) = |
| 6870 | external_ladder(&source, width, height, "libaom-av1", &aom_points); | 7048 | external_ladder(&source, width, height, "libaom-av1", &aom_points); |
| 6871 | let (rav1e, rav1e_wall) = | 7049 | let (rav1e, rav1e_wall) = |
crates/ec-av1/src/tile.rsmodified+63 -15
| @@ -759,16 +759,25 @@ impl Neighbours { | |||
| 759 | /// `get_tx_size_context` as libaom writes it for a block inside an INTER | 759 | /// `get_tx_size_context` as libaom writes it for a block inside an INTER |
| 760 | /// frame (decode.rs `tx_size_context_txfm_rect`): the `TXFM_CONTEXT` | 760 | /// frame (decode.rs `tx_size_context_txfm_rect`): the `TXFM_CONTEXT` |
| 761 | /// bands, except that an *inter* neighbour contributes its own BLOCK size. | 761 | /// bands, except that an *inter* neighbour contributes its own BLOCK size. |
| 762 | + /// | ||
| 763 | + /// lane-av1tx2 r1: a neighbour OUTSIDE the tile contributes nothing at all | ||
| 764 | + /// -- the decoder's `tx_size_context_txfm` drops the whole term when | ||
| 765 | + /// `has_above`/`has_left` is false, where this counted the band's | ||
| 766 | + /// [`TXFM_CTX_INIT`] (64, the widest transform) as a real neighbour. Every | ||
| 767 | + /// intra block on a tile's top row or left column then took its `tx_depth` | ||
| 768 | + /// off a CDF row one higher than the decoder's, which is the first | ||
| 769 | + /// divergence of the inter `TxMode::Select` stream. | ||
| 762 | fn tx_size_ctx_txfm(&self, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize { | 770 | fn tx_size_ctx_txfm(&self, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize { |
| 771 | + let (has_above, has_left) = (mi_r > 0, mi_c > 0); | ||
| 763 | let mut above = usize::from(self.above_txfm[mi_c]) >= own_side; | 772 | let mut above = usize::from(self.above_txfm[mi_c]) >= own_side; |
| 764 | let mut left = usize::from(self.left_txfm[mi_r]) >= own_side; | 773 | let mut left = usize::from(self.left_txfm[mi_r]) >= own_side; |
| 765 | - if mi_r > 0 && self.above_inter[mi_c] { | 774 | + if has_above && self.above_inter[mi_c] { |
| 766 | above = self.above_side_mi[mi_c] >= own_side; | 775 | above = self.above_side_mi[mi_c] >= own_side; |
| 767 | } | 776 | } |
| 768 | - if mi_c > 0 && self.left_inter[mi_r] { | 777 | + if has_left && self.left_inter[mi_r] { |
| 769 | left = self.left_side_mi[mi_r] >= own_side; | 778 | left = self.left_side_mi[mi_r] >= own_side; |
| 770 | } | 779 | } |
| 771 | - usize::from(above) + usize::from(left) | 780 | + usize::from(has_above && above) + usize::from(has_left && left) |
| 772 | } | 781 | } |
| 773 | 782 | ||
| 774 | /// `get_tx_size_context` (decode.rs [`crate::decode::tx_size_context`]): | 783 | /// `get_tx_size_context` (decode.rs [`crate::decode::tx_size_context`]): |
| @@ -1708,6 +1717,9 @@ fn write_luma_tus( | |||
| 1708 | grid: &[i32], | 1717 | grid: &[i32], |
| 1709 | mode: usize, | 1718 | mode: usize, |
| 1710 | scans: &[Vec<u16>; 4], | 1719 | scans: &[Vec<u16>; 4], |
| 1720 | + // lane-av1tx2 r2: an INTER block's split units read the inter coefficient | ||
| 1721 | + // tables, not the intra ones (`TxbSet::Luma*Inter`). | ||
| 1722 | + inter: bool, | ||
| 1711 | ) -> Result<()> { | 1723 | ) -> Result<()> { |
| 1712 | if tx < side && side > 32 { | 1724 | if tx < side && side > 32 { |
| 1713 | // A 64x64 block's own levels reach the writer as the 32x32 corner a | 1725 | // A 64x64 block's own levels reach the writer as the 32x32 corner a |
| @@ -1720,11 +1732,15 @@ fn write_luma_tus( | |||
| 1720 | } | 1732 | } |
| 1721 | let coeff_side = tx.min(32); | 1733 | let coeff_side = tx.min(32); |
| 1722 | let n = side / tx; | 1734 | let n = side / tx; |
| 1723 | - let set = match tx { | 1735 | + let set = match (tx, inter) { |
| 1724 | - 32 => TxbSet::Luma32, | 1736 | + (32, false) => TxbSet::Luma32, |
| 1725 | - 16 => TxbSet::Luma16, | 1737 | + (16, false) => TxbSet::Luma16, |
| 1726 | - 8 => TxbSet::Luma8, | 1738 | + (8, false) => TxbSet::Luma8, |
| 1727 | - 4 => TxbSet::Luma4, | 1739 | + (4, false) => TxbSet::Luma4, |
| 1740 | + (32, true) => TxbSet::Luma32Inter, | ||
| 1741 | + (16, true) => TxbSet::Luma16Inter, | ||
| 1742 | + (8, true) => TxbSet::Luma8Inter, | ||
| 1743 | + (4, true) => TxbSet::Luma4Inter, | ||
| 1728 | _ => TxbSet::Luma64, | 1744 | _ => TxbSet::Luma64, |
| 1729 | }; | 1745 | }; |
| 1730 | let scan = match coeff_side { | 1746 | let scan = match coeff_side { |
| @@ -1810,7 +1826,7 @@ fn write_luma_select( | |||
| 1810 | } | 1826 | } |
| 1811 | } | 1827 | } |
| 1812 | let tx = max_tx >> depth; | 1828 | let tx = max_tx >> depth; |
| 1813 | - write_luma_tus(enc, cdfs, neighbours, at_mi, side, tx, grid, mode, scans)?; | 1829 | + write_luma_tus(enc, cdfs, neighbours, at_mi, side, tx, grid, mode, scans, false)?; |
| 1814 | neighbours.record_tx(at_mi, side, tx); | 1830 | neighbours.record_tx(at_mi, side, tx); |
| 1815 | Ok(tx) | 1831 | Ok(tx) |
| 1816 | } | 1832 | } |
| @@ -1851,11 +1867,42 @@ fn write_tx_syntax_inter( | |||
| 1851 | max_tx, | 1867 | max_tx, |
| 1852 | max_tx, | 1868 | max_tx, |
| 1853 | ); | 1869 | ); |
| 1854 | - enc.symbol(0, &mut cdfs.txfm_partition[ctx]); | 1870 | + enc.symbol(usize::from(depth != 0), &mut cdfs.txfm_partition[ctx]); |
| 1855 | - neighbours.record_txfm(at_mi, max_tx, max_tx, max_tx); | 1871 | + if depth == 0 { |
| 1856 | - } else { | 1872 | + neighbours.record_txfm(at_mi, max_tx, max_tx, max_tx); |
| 1857 | - neighbours.record_txfm(at_mi, side, side, side); | 1873 | + return max_tx; |
| 1874 | + } | ||
| 1875 | + // lane-av1tx2 r2: one halving of the var-tx tree. Each child of a | ||
| 1876 | + // split root codes its own `txfm_split` (decode.rs | ||
| 1877 | + // `read_var_tx_size` only stops symbol-free at `MAX_VARTX_DEPTH` | ||
| 1878 | + // or at a 4x4 sub-size), off the bands the children before it | ||
| 1879 | + // published -- so the context read and the update interleave per | ||
| 1880 | + // unit, exactly as the reader's recursion does. | ||
| 1881 | + let tx = max_tx / 2; | ||
| 1882 | + for row in (0..max_tx / MI).step_by(tx / MI) { | ||
| 1883 | + for col in (0..max_tx / MI).step_by(tx / MI) { | ||
| 1884 | + let unit = (mi_r + row, mi_c + col); | ||
| 1885 | + // The encoder only picks depth 1 for a block wholly | ||
| 1886 | + // inside the frame; the reader skips a unit past the true | ||
| 1887 | + // edge, which would leave the residual units unpaired. | ||
| 1888 | + debug_assert!( | ||
| 1889 | + unit.0 < neighbours.mi_rows && unit.1 < neighbours.mi_cols, | ||
| 1890 | + "a split inter transform unit past the frame edge" | ||
| 1891 | + ); | ||
| 1892 | + let ctx = txfm_partition_ctx_rect( | ||
| 1893 | + neighbours.above_txfm[unit.1], | ||
| 1894 | + neighbours.left_txfm[unit.0], | ||
| 1895 | + side, | ||
| 1896 | + tx, | ||
| 1897 | + tx, | ||
| 1898 | + ); | ||
| 1899 | + enc.symbol(0, &mut cdfs.txfm_partition[ctx]); | ||
| 1900 | + neighbours.record_txfm(unit, tx, tx, tx); | ||
| 1901 | + } | ||
| 1902 | + } | ||
| 1903 | + return tx; | ||
| 1858 | } | 1904 | } |
| 1905 | + neighbours.record_txfm(at_mi, side, side, side); | ||
| 1859 | return max_tx; | 1906 | return max_tx; |
| 1860 | } | 1907 | } |
| 1861 | let ctx = neighbours.tx_size_ctx_txfm(at_mi, max_tx); | 1908 | let ctx = neighbours.tx_size_ctx_txfm(at_mi, max_tx); |
| @@ -3119,6 +3166,7 @@ pub fn sb_coeff_inter_frame_tile_tx( | |||
| 3119 | &grids[0], | 3166 | &grids[0], |
| 3120 | mode_for_tx, | 3167 | mode_for_tx, |
| 3121 | &all_scans, | 3168 | &all_scans, |
| 3169 | + is_inter, | ||
| 3122 | )?; | 3170 | )?; |
| 3123 | } | 3171 | } |
| 3124 | write_block_planes( | 3172 | write_block_planes( |
| @@ -3327,7 +3375,7 @@ fn write_inter_frame_leaf( | |||
| 3327 | ]; | 3375 | ]; |
| 3328 | if split { | 3376 | if split { |
| 3329 | write_luma_tus( | 3377 | write_luma_tus( |
| 3330 | - enc, cdfs, neighbours, at_mi, SUB, tx, &grids[0], mode_for_tx, all_scans, | 3378 | + enc, cdfs, neighbours, at_mi, SUB, tx, &grids[0], mode_for_tx, all_scans, is_inter, |
| 3331 | )?; | 3379 | )?; |
| 3332 | } | 3380 | } |
| 3333 | write_block_planes( | 3381 | write_block_planes( |
| @@ -3552,7 +3600,7 @@ fn write_inter_frame_leaf8( | |||
| 3552 | ]; | 3600 | ]; |
| 3553 | if split { | 3601 | if split { |
| 3554 | write_luma_tus( | 3602 | write_luma_tus( |
| 3555 | - enc, cdfs, neighbours, leaf_mi, 8, tx, &grids[0], mode_for_tx, all_scans, | 3603 | + enc, cdfs, neighbours, leaf_mi, 8, tx, &grids[0], mode_for_tx, all_scans, is_inter, |
| 3556 | )?; | 3604 | )?; |
| 3557 | } | 3605 | } |
| 3558 | write_block_planes( | 3606 | write_block_planes( |