forge
Explore
Log in

Don't you dare go Hollow

An inter block's chroma inherits its luma transform type

Browse files at this commit

25e26f3f4595952486bf16a98efcef020c38cc4aAhmet Kaan GümüşParent: 888d58d
  • fix
    • an inter block's chroma inherits its luma transform type
Show full message
fix(av1): an inter block's chroma inherits its luma transform type

The decoder derives an inter block's chroma tx_type from the luma type at
the block's chroma-reference position (spec 5.11.40, libaom
av1_get_tx_type / reduce_inherited_chroma_tx_type); the encoder quantised
chroma under a DCT_DCT basis whatever the luma type search chose, so every
non-DCT luma winner had its chroma levels inverse-transformed on a basis
they were never produced for.

Instrumented on the bars 1080p gate row at q=60, unconditional inter type
search: luma 53.66 -> 53.70 dB (the search's own win, real), U 51.97 ->
49.15, V 52.52 -> 45.53. Pre-filter frame luma sse is lower on every frame
in the arm, so neither the RDOQ distortion model, the type rate term
(EC_AV1_TXRD_LAMBDA=0 reproduces the collapse) nor the typed forward gain
(measured 0.98-1.03 of unity for every type and size) was the cause.

Adds EC_AV1_TXRD_LAMBDA / EC_AV1_TXRD_CENSUS and the per-frame /
per-plane / pre- and post-filter gate instruments the diagnosis needed.
crates/ec-av1/src/decode.rsmodified+1 -1
@@ -18428,7 +18428,7 @@ fn read_inter_luma8(
1842818428 /// both `TX_CLASS_2D`, so that mistake left the entropy stream perfectly in
1842918429 /// sync and only corrupted the inverse transform (one 64x64 superblock of
1843018430 /// 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 {
1843218432 use TxType::*;
1843318433 let sqr_up = w.max(h);
1843418434 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 {
39613961 + 4.0 * symbol_bits(&cdf::TXFM_PARTITION[ctx(max_tx / 2)], 0)
39623962 }
39633963
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+
39644010 /// The inter block's own var-tx decision, once its motion vector is settled:
39654011 /// its residual as ONE transform over the whole block, or as the four
39664012 /// transforms of half the side the tree's first halving names. The prediction
@@ -6214,6 +6260,30 @@ fn code_square_inter(
62146260 search, &luma_new, skip_new, second, fctx,
62156261 )
62166262 };
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+ };
62176287 chroma[0].commit(x / 2, y / 2, side / 2, &u_new);
62186288 chroma[1].commit(x / 2, y / 2, side / 2, &v_new);
62196289 return (
@@ -6252,6 +6322,20 @@ fn code_square_inter(
62526322 let (levels, tx_depth, dcost, luma_tx_types) = commit_inter_luma(
62536323 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,
62546324 );
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+ };
62556339 chroma[0].commit(x / 2, y / 2, side / 2, &u);
62566340 chroma[1].commit(x / 2, y / 2, side / 2, &v);
62576341 (BlockCoeffs {
@@ -9559,6 +9643,38 @@ fn search_inter_block(
95599643 (best.luma.levels.clone(), 0, 0.0, Vec::new())
95609644 }
95619645 };
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 };
95629678 chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u);
95639679 chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v);
95649680 (
@@ -14966,6 +15082,19 @@ mod tests {
1496615082 if crate::envflags::env_flag!("EC_AV1_FRAME_PSNR") {
1496715083 let row: Vec<String> = per_frame.iter().map(|p| format!("{p:.2}")).collect();
1496815084 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));
1496915098 }
1497015099 let mean: f64 = per_frame.iter().sum::<f64>() / source.len() as f64;
1497115100 ladder.push((mean, (encoded.stream.len() as f64).log10()));