An inter block's chroma inherits its luma transform type
Browse files at this commit25e26f3f4595952486bf16a98efcef020c38cc4aParent: 888d58d- fix
- an inter block's chroma inherits its luma transform type
2 changed files +130 -1
- crates/ec-av1/src/decode.rs +1 -1
- crates/ec-av1/src/encode.rs +129 -0
crates/ec-av1/src/decode.rsmodified+1 -1
| @@ -18428,7 +18428,7 @@ fn read_inter_luma8( | |||
| 18428 | /// both `TX_CLASS_2D`, so that mistake left the entropy stream perfectly in | 18428 | /// both `TX_CLASS_2D`, so that mistake left the entropy stream perfectly in |
| 18429 | /// sync and only corrupted the inverse transform (one 64x64 superblock of | 18429 | /// sync and only corrupted the inverse transform (one 64x64 superblock of |
| 18430 | /// high-frequency chroma noise, luma bit-exact). | 18430 | /// high-frequency chroma noise, luma bit-exact). |
| 18431 | -fn reduce_inherited_chroma_tx_type(t: TxType, w: usize, h: usize, fctx: &crate::decode::FrameCtx) -> TxType { | 18431 | +pub(crate) fn reduce_inherited_chroma_tx_type(t: TxType, w: usize, h: usize, fctx: &crate::decode::FrameCtx) -> TxType { |
| 18432 | use TxType::*; | 18432 | use TxType::*; |
| 18433 | let sqr_up = w.max(h); | 18433 | let sqr_up = w.max(h); |
| 18434 | let sqr = w.min(h); | 18434 | let sqr = w.min(h); |
crates/ec-av1/src/encode.rsmodified+129 -0
| @@ -3961,6 +3961,52 @@ fn txfm_split_bits(side: usize, depth: usize) -> f64 { | |||
| 3961 | + 4.0 * symbol_bits(&cdf::TXFM_PARTITION[ctx(max_tx / 2)], 0) | 3961 | + 4.0 * symbol_bits(&cdf::TXFM_PARTITION[ctx(max_tx / 2)], 0) |
| 3962 | } | 3962 | } |
| 3963 | 3963 | ||
| 3964 | +/// spec 5.11.40 / libaom `av1_get_tx_type`: an INTER block's chroma transform | ||
| 3965 | +/// type is never coded -- the decoder takes the LUMA type of the block's | ||
| 3966 | +/// chroma-reference position and reduces it to the set the chroma size allows | ||
| 3967 | +/// ([`crate::decode::reduce_inherited_chroma_tx_type`]). This encoder | ||
| 3968 | +/// quantised chroma under a `DCT_DCT` basis whatever luma chose, so every | ||
| 3969 | +/// non-`DCT_DCT` luma winner had its chroma levels inverse-transformed on a | ||
| 3970 | +/// basis they were never produced for: on the bars gate row the luma of the | ||
| 3971 | +/// unconditional tx-type arm came out BETTER on every frame (53.66 -> 53.70 | ||
| 3972 | +/// dB) while V lost seven decibels (52.52 -> 45.53) -- the whole 20-point BD | ||
| 3973 | +/// collapse two lanes attributed to the type PRICE (class | ||
| 3974 | +/// `parsed then discarded` / `override slot on one arm`: a decoder-derived | ||
| 3975 | +/// field the encoder never mirrored). | ||
| 3976 | +/// | ||
| 3977 | +/// Re-codes the two chroma planes of a committed inter block at the type the | ||
| 3978 | +/// decoder will use, `None` when the inherited type reduces to `DCT_DCT` and | ||
| 3979 | +/// the trials the caller already holds are right. | ||
| 3980 | +#[allow(clippy::too_many_arguments)] | ||
| 3981 | +fn recode_inter_chroma( | ||
| 3982 | + chroma: &[Plane; 2], | ||
| 3983 | + (x, y): (usize, usize), | ||
| 3984 | + side: usize, | ||
| 3985 | + mv: (i32, i32), | ||
| 3986 | + refs: [(&[u16], usize, usize, usize); 2], | ||
| 3987 | + second: Option<((i32, i32), [(&[u16], usize, usize, usize); 2])>, | ||
| 3988 | + search: &Search, | ||
| 3989 | + set: TxbSet, | ||
| 3990 | + luma_tx_type: TxType, | ||
| 3991 | + fctx: &crate::decode::FrameCtx, | ||
| 3992 | +) -> Option<[Trial; 2]> { | ||
| 3993 | + let c = side / 2; | ||
| 3994 | + let tx = crate::decode::reduce_inherited_chroma_tx_type(luma_tx_type, c.min(32), c.min(32), fctx); | ||
| 3995 | + if tx == TxType::DctDct { | ||
| 3996 | + return None; | ||
| 3997 | + } | ||
| 3998 | + Some([0usize, 1].map(|p| match second { | ||
| 3999 | + Some((mv1, r1)) => mc_trial_compound_typed( | ||
| 4000 | + &chroma[p], x / 2, y / 2, c, (mv, mv1), false, refs[p], r1[p], search.base_q_idx, | ||
| 4001 | + search.deadzone, set, tx, fctx, | ||
| 4002 | + ), | ||
| 4003 | + None => mc_trial_typed( | ||
| 4004 | + &chroma[p], x / 2, y / 2, c, mv, false, refs[p].0, refs[p].1, refs[p].2, refs[p].3, | ||
| 4005 | + false, search.base_q_idx, search.deadzone, set, tx, fctx, | ||
| 4006 | + ), | ||
| 4007 | + })) | ||
| 4008 | +} | ||
| 4009 | + | ||
| 3964 | /// The inter block's own var-tx decision, once its motion vector is settled: | 4010 | /// The inter block's own var-tx decision, once its motion vector is settled: |
| 3965 | /// its residual as ONE transform over the whole block, or as the four | 4011 | /// its residual as ONE transform over the whole block, or as the four |
| 3966 | /// transforms of half the side the tree's first halving names. The prediction | 4012 | /// transforms of half the side the tree's first halving names. The prediction |
| @@ -6214,6 +6260,30 @@ fn code_square_inter( | |||
| 6214 | search, &luma_new, skip_new, second, fctx, | 6260 | search, &luma_new, skip_new, second, fctx, |
| 6215 | ) | 6261 | ) |
| 6216 | }; | 6262 | }; |
| 6263 | + // lane-txrd: chroma follows the luma type the search just chose. | ||
| 6264 | + let [u_new, v_new] = match luma_tx_types.first().copied() { | ||
| 6265 | + Some(t) => recode_inter_chroma( | ||
| 6266 | + chroma, (x, y), side, info.mv, | ||
| 6267 | + [ | ||
| 6268 | + (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), | ||
| 6269 | + (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), | ||
| 6270 | + ], | ||
| 6271 | + info.ref1.filter(|_| motion_won == 0).and_then(|r| { | ||
| 6272 | + refs[r as usize].map(|g| { | ||
| 6273 | + ( | ||
| 6274 | + info.mv1, | ||
| 6275 | + [ | ||
| 6276 | + (g.u.as_slice(), g.width / 2, chroma[0].true_width, chroma[0].true_height), | ||
| 6277 | + (g.v.as_slice(), g.width / 2, chroma[1].true_width, chroma[1].true_height), | ||
| 6278 | + ], | ||
| 6279 | + ) | ||
| 6280 | + }) | ||
| 6281 | + }), | ||
| 6282 | + search, chroma_set, t, fctx, | ||
| 6283 | + ) | ||
| 6284 | + .unwrap_or([u_new, v_new]), | ||
| 6285 | + None => [u_new, v_new], | ||
| 6286 | + }; | ||
| 6217 | chroma[0].commit(x / 2, y / 2, side / 2, &u_new); | 6287 | chroma[0].commit(x / 2, y / 2, side / 2, &u_new); |
| 6218 | chroma[1].commit(x / 2, y / 2, side / 2, &v_new); | 6288 | chroma[1].commit(x / 2, y / 2, side / 2, &v_new); |
| 6219 | return ( | 6289 | return ( |
| @@ -6252,6 +6322,20 @@ fn code_square_inter( | |||
| 6252 | let (levels, tx_depth, dcost, luma_tx_types) = commit_inter_luma( | 6322 | let (levels, tx_depth, dcost, luma_tx_types) = commit_inter_luma( |
| 6253 | luma, (x, y), side, mv, (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), search, &luma_trial, skip, None, fctx, | 6323 | luma, (x, y), side, mv, (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), search, &luma_trial, skip, None, fctx, |
| 6254 | ); | 6324 | ); |
| 6325 | + // lane-txrd: chroma follows the luma type (this tail's block is | ||
| 6326 | + // single-reference `NEARESTMV`, so no compound second half). | ||
| 6327 | + let [u, v] = match luma_tx_types.first().copied() { | ||
| 6328 | + Some(t) => recode_inter_chroma( | ||
| 6329 | + chroma, (x, y), side, mv, | ||
| 6330 | + [ | ||
| 6331 | + (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), | ||
| 6332 | + (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), | ||
| 6333 | + ], | ||
| 6334 | + None, search, chroma_set, t, fctx, | ||
| 6335 | + ) | ||
| 6336 | + .unwrap_or([u, v]), | ||
| 6337 | + None => [u, v], | ||
| 6338 | + }; | ||
| 6255 | chroma[0].commit(x / 2, y / 2, side / 2, &u); | 6339 | chroma[0].commit(x / 2, y / 2, side / 2, &u); |
| 6256 | chroma[1].commit(x / 2, y / 2, side / 2, &v); | 6340 | chroma[1].commit(x / 2, y / 2, side / 2, &v); |
| 6257 | (BlockCoeffs { | 6341 | (BlockCoeffs { |
| @@ -9559,6 +9643,38 @@ fn search_inter_block( | |||
| 9559 | (best.luma.levels.clone(), 0, 0.0, Vec::new()) | 9643 | (best.luma.levels.clone(), 0, 0.0, Vec::new()) |
| 9560 | } | 9644 | } |
| 9561 | }; | 9645 | }; |
| 9646 | + // lane-txrd: chroma follows the luma type the var-tx/type search chose. | ||
| 9647 | + let [bu, bv] = match (best.inter.filter(|_| motion_won == 0), luma_tx_types.first().copied()) { | ||
| 9648 | + (Some(info), Some(t)) => { | ||
| 9649 | + let (ou, ov) = match extra.iter().find(|(r, _, _)| *r == info.ref_frame) { | ||
| 9650 | + Some(&(_, g, _)) => ( | ||
| 9651 | + (g.u.as_slice(), g.width / 2, chroma[0].true_width, chroma[0].true_height), | ||
| 9652 | + (g.v.as_slice(), g.width / 2, chroma[1].true_width, chroma[1].true_height), | ||
| 9653 | + ), | ||
| 9654 | + None => ( | ||
| 9655 | + (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), | ||
| 9656 | + (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), | ||
| 9657 | + ), | ||
| 9658 | + }; | ||
| 9659 | + let sec = info.ref1.and_then(|r| { | ||
| 9660 | + compound.iter().find(|(c, _, _)| *c == r).map(|&(_, g, _)| { | ||
| 9661 | + ( | ||
| 9662 | + info.mv1, | ||
| 9663 | + [ | ||
| 9664 | + (g.u.as_slice(), g.width / 2, chroma[0].true_width, chroma[0].true_height), | ||
| 9665 | + (g.v.as_slice(), g.width / 2, chroma[1].true_width, chroma[1].true_height), | ||
| 9666 | + ], | ||
| 9667 | + ) | ||
| 9668 | + }) | ||
| 9669 | + }); | ||
| 9670 | + recode_inter_chroma( | ||
| 9671 | + chroma, (x, y), BLOCK, info.mv, [ou, ov], sec, search, chroma_set, t, fctx, | ||
| 9672 | + ) | ||
| 9673 | + .unwrap_or([best.u, best.v]) | ||
| 9674 | + } | ||
| 9675 | + _ => [best.u, best.v], | ||
| 9676 | + }; | ||
| 9677 | + let best = Candidate { u: bu, v: bv, ..best }; | ||
| 9562 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); | 9678 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); |
| 9563 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); | 9679 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); |
| 9564 | ( | 9680 | ( |
| @@ -14966,6 +15082,19 @@ mod tests { | |||
| 14966 | if crate::envflags::env_flag!("EC_AV1_FRAME_PSNR") { | 15082 | if crate::envflags::env_flag!("EC_AV1_FRAME_PSNR") { |
| 14967 | let row: Vec<String> = per_frame.iter().map(|p| format!("{p:.2}")).collect(); | 15083 | let row: Vec<String> = per_frame.iter().map(|p| format!("{p:.2}")).collect(); |
| 14968 | eprintln!("{name}: q={q} per-frame PSNR {}", row.join(" ")); | 15084 | eprintln!("{name}: q={q} per-frame PSNR {}", row.join(" ")); |
| 15085 | + // lane-txrd: per PLANE, because an all-plane mean hides a | ||
| 15086 | + // collapse in one of them (class `metric blind to a plane`). | ||
| 15087 | + let plane = |pick: fn(&Picture) -> &Vec<u16>| -> String { | ||
| 15088 | + let v: Vec<String> = decoded | ||
| 15089 | + .iter() | ||
| 15090 | + .zip(source) | ||
| 15091 | + .map(|(d, s)| format!("{:.2}", psnr(pick(d), pick(s)))) | ||
| 15092 | + .collect(); | ||
| 15093 | + v.join(" ") | ||
| 15094 | + }; | ||
| 15095 | + eprintln!("{name}: q={q} Y {}", plane(|p| &p.y)); | ||
| 15096 | + eprintln!("{name}: q={q} U {}", plane(|p| &p.u)); | ||
| 15097 | + eprintln!("{name}: q={q} V {}", plane(|p| &p.v)); | ||
| 14969 | } | 15098 | } |
| 14970 | let mean: f64 = per_frame.iter().sum::<f64>() / source.len() as f64; | 15099 | let mean: f64 = per_frame.iter().sum::<f64>() / source.len() as f64; |
| 14971 | ladder.push((mean, (encoded.stream.len() as f64).log10())); | 15100 | ladder.push((mean, (encoded.stream.len() as f64).log10())); |