Compound-reference blocks -- comp_ref/compound_mode syntax and averaged two-reference prediction
Browse files at this commit3cac2171b37a29191d35fc0bd73b618fd2f49ce5Parent: b22678d- feat
- compound-reference blocks -- comp_ref/compound_mode syntax and averaged two-reference prediction
3 changed files +40 -11
crates/ec-av1/src/decode.rsmodified+9 -0
| @@ -25002,6 +25002,15 @@ fn read_comp_mode( | |||
| 25002 | ) -> bool { | 25002 | ) -> bool { |
| 25003 | let ctx = reference_mode_ctx(above, left); | 25003 | let ctx = reference_mode_ctx(above, left); |
| 25004 | let compound = dec.symbol(&mut cdfs.comp_mode[ctx]) == 1; | 25004 | let compound = dec.symbol(&mut cdfs.comp_mode[ctx]) == 1; |
| 25005 | + if crate::envflags::env_flag!("EC_TRACE_MODE") { | ||
| 25006 | + eprintln!( | ||
| 25007 | + "EC_RCM ctx={ctx} val={} a={:?} l={:?} rng={}", | ||
| 25008 | + usize::from(compound), | ||
| 25009 | + above.map(|n| (n.is_inter, n.ref0, n.ref1)), | ||
| 25010 | + left.map(|n| (n.is_inter, n.ref0, n.ref1)), | ||
| 25011 | + dec.debug_state().0 | ||
| 25012 | + ); | ||
| 25013 | + } | ||
| 25005 | if compound { | 25014 | if compound { |
| 25006 | hit!(COMP_MODE_HITS); | 25015 | hit!(COMP_MODE_HITS); |
| 25007 | } | 25016 | } |
crates/ec-av1/src/encode.rsmodified+1 -2
| @@ -4352,7 +4352,6 @@ fn search_inter_block( | |||
| 4352 | let mode_bits_of = | 4352 | let mode_bits_of = |
| 4353 | |mode: usize| pair_bits + symbol_bits(&cdf::INTER_COMPOUND_MODE[mode_ctx], mode); | 4353 | |mode: usize| pair_bits + symbol_bits(&cdf::INTER_COMPOUND_MODE[mode_ctx], mode); |
| 4354 | let mut ccands: Vec<(((i32, i32), (i32, i32)), f64, InterInfo)> = vec![ | 4354 | let mut ccands: Vec<(((i32, i32), (i32, i32)), f64, InterInfo)> = vec![ |
| 4355 | - #[cfg(any())] | ||
| 4356 | ( | 4355 | ( |
| 4357 | cstack.nearest_mv, | 4356 | cstack.nearest_mv, |
| 4358 | mode_bits_of(0), | 4357 | mode_bits_of(0), |
| @@ -4380,7 +4379,7 @@ fn search_inter_block( | |||
| 4380 | ]; | 4379 | ]; |
| 4381 | // `NEW_NEWMV` seeded from the two single-reference searches this | 4380 | // `NEW_NEWMV` seeded from the two single-reference searches this |
| 4382 | // block already ran -- no joint search of its own. | 4381 | // block already ran -- no joint search of its own. |
| 4383 | - if false && let Some(&(_, mv1)) = extra_new_mvs.iter().find(|(r, _)| *r == ref1) { | 4382 | + if let Some(&(_, mv1)) = extra_new_mvs.iter().find(|(r, _)| *r == ref1) { |
| 4384 | let base = cstack | 4383 | let base = cstack |
| 4385 | .entries | 4384 | .entries |
| 4386 | .first() | 4385 | .first() |
crates/ec-av1/src/tile.rsmodified+30 -9
| @@ -374,6 +374,12 @@ fn write_compound_block( | |||
| 374 | None, | 374 | None, |
| 375 | ); | 375 | ); |
| 376 | let ctx = crate::cdf::COMPOUND_MODE_CTX_MAP[stack.ref_mv_ctx >> 1][stack.new_mv_ctx.min(4)]; | 376 | let ctx = crate::cdf::COMPOUND_MODE_CTX_MAP[stack.ref_mv_ctx >> 1][stack.new_mv_ctx.min(4)]; |
| 377 | + if crate::envflags::env_flag!("EC_TRACE_MODE") { | ||
| 378 | + eprintln!( | ||
| 379 | + "EC_WCOMP mi_row={mi_row} mi_col={mi_col} mode={mode} ref0={} ref1={ref1} ctx={ctx} new_mv_ctx={} ref_mv_ctx={} stack={}", | ||
| 380 | + info.ref_frame, stack.new_mv_ctx, stack.ref_mv_ctx, stack.entries.len() | ||
| 381 | + ); | ||
| 382 | + } | ||
| 377 | enc.symbol(mode, &mut cdfs.inter_compound_mode[ctx]); | 383 | enc.symbol(mode, &mut cdfs.inter_compound_mode[ctx]); |
| 378 | // `assign_compound_mv`'s own DRL half: `NEW_NEWMV` walks from index 0, | 384 | // `assign_compound_mv`'s own DRL half: `NEW_NEWMV` walks from index 0, |
| 379 | // the two derived modes code no index at all. | 385 | // the two derived modes code no index at all. |
| @@ -437,6 +443,14 @@ fn write_comp_mode( | |||
| 437 | let above = has_above.then(|| neighbours.above_nbr(mi_c)); | 443 | let above = has_above.then(|| neighbours.above_nbr(mi_c)); |
| 438 | let left = has_left.then(|| neighbours.left_nbr(mi_r)); | 444 | let left = has_left.then(|| neighbours.left_nbr(mi_r)); |
| 439 | let ctx = crate::mvstack::reference_mode_ctx(above, left); | 445 | let ctx = crate::mvstack::reference_mode_ctx(above, left); |
| 446 | + if crate::envflags::env_flag!("EC_TRACE_MODE") { | ||
| 447 | + eprintln!( | ||
| 448 | + "EC_WCM mi=({mi_r},{mi_c}) ctx={ctx} val={} a={:?} l={:?}", | ||
| 449 | + usize::from(compound), | ||
| 450 | + above.map(|n| (n.is_inter, n.ref0, n.ref1)), | ||
| 451 | + left.map(|n| (n.is_inter, n.ref0, n.ref1)) | ||
| 452 | + ); | ||
| 453 | + } | ||
| 440 | enc.symbol(usize::from(compound), &mut cdfs.comp_mode[ctx]); | 454 | enc.symbol(usize::from(compound), &mut cdfs.comp_mode[ctx]); |
| 441 | } | 455 | } |
| 442 | 456 | ||
| @@ -3334,7 +3348,9 @@ fn write_single_ref( | |||
| 3334 | cdfs: &mut Cdfs, | 3348 | cdfs: &mut Cdfs, |
| 3335 | ref_frame: i8, | 3349 | ref_frame: i8, |
| 3336 | above_ref: i8, | 3350 | above_ref: i8, |
| 3351 | + above_ref1: Option<i8>, | ||
| 3337 | left_ref: i8, | 3352 | left_ref: i8, |
| 3353 | + left_ref1: Option<i8>, | ||
| 3338 | ) { | 3354 | ) { |
| 3339 | REF_HITS[(ref_frame.max(1) - 1) as usize % 7].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 3355 | REF_HITS[(ref_frame.max(1) - 1) as usize % 7].fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 3340 | use crate::mvstack::{ | 3356 | use crate::mvstack::{ |
| @@ -3344,21 +3360,26 @@ fn write_single_ref( | |||
| 3344 | }; | 3360 | }; |
| 3345 | let above = (above_ref > 0).then_some(above_ref); | 3361 | let above = (above_ref > 0).then_some(above_ref); |
| 3346 | let left = (left_ref > 0).then_some(left_ref); | 3362 | let left = (left_ref > 0).then_some(left_ref); |
| 3363 | + // libaom `av1_collect_neighbors_ref_counts` counts BOTH of a compound | ||
| 3364 | + // neighbour's references (decode.rs `read_single_ref`'s own | ||
| 3365 | + // `above_ref1`/`left_ref1`): dropping the second one reads a different | ||
| 3366 | + // context row for every block sitting under a compound neighbour, and so | ||
| 3367 | + // decodes a different reference NAME. | ||
| 3347 | let backward = ref_frame >= BWDREF_FRAME; | 3368 | let backward = ref_frame >= BWDREF_FRAME; |
| 3348 | enc.symbol( | 3369 | enc.symbol( |
| 3349 | usize::from(backward), | 3370 | usize::from(backward), |
| 3350 | - &mut cdfs.single_ref[single_ref_p1_ctx(above, None, left, None)][0], | 3371 | + &mut cdfs.single_ref[single_ref_p1_ctx(above, above_ref1, left, left_ref1)][0], |
| 3351 | ); | 3372 | ); |
| 3352 | if backward { | 3373 | if backward { |
| 3353 | let is_altref = ref_frame == ALTREF_FRAME; | 3374 | let is_altref = ref_frame == ALTREF_FRAME; |
| 3354 | enc.symbol( | 3375 | enc.symbol( |
| 3355 | usize::from(is_altref), | 3376 | usize::from(is_altref), |
| 3356 | - &mut cdfs.single_ref[single_ref_p2_ctx(above, None, left, None)][1], | 3377 | + &mut cdfs.single_ref[single_ref_p2_ctx(above, above_ref1, left, left_ref1)][1], |
| 3357 | ); | 3378 | ); |
| 3358 | if !is_altref { | 3379 | if !is_altref { |
| 3359 | enc.symbol( | 3380 | enc.symbol( |
| 3360 | usize::from(ref_frame == ALTREF2_FRAME), | 3381 | usize::from(ref_frame == ALTREF2_FRAME), |
| 3361 | - &mut cdfs.single_ref[single_ref_p6_ctx(above, None, left, None)][5], | 3382 | + &mut cdfs.single_ref[single_ref_p6_ctx(above, above_ref1, left, left_ref1)][5], |
| 3362 | ); | 3383 | ); |
| 3363 | } | 3384 | } |
| 3364 | return; | 3385 | return; |
| @@ -3366,17 +3387,17 @@ fn write_single_ref( | |||
| 3366 | let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME; | 3387 | let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME; |
| 3367 | enc.symbol( | 3388 | enc.symbol( |
| 3368 | usize::from(far), | 3389 | usize::from(far), |
| 3369 | - &mut cdfs.single_ref[single_ref_p3_ctx(above, None, left, None)][2], | 3390 | + &mut cdfs.single_ref[single_ref_p3_ctx(above, above_ref1, left, left_ref1)][2], |
| 3370 | ); | 3391 | ); |
| 3371 | if far { | 3392 | if far { |
| 3372 | enc.symbol( | 3393 | enc.symbol( |
| 3373 | usize::from(ref_frame == GOLDEN_FRAME), | 3394 | usize::from(ref_frame == GOLDEN_FRAME), |
| 3374 | - &mut cdfs.single_ref[single_ref_p5_ctx(above, None, left, None)][4], | 3395 | + &mut cdfs.single_ref[single_ref_p5_ctx(above, above_ref1, left, left_ref1)][4], |
| 3375 | ); | 3396 | ); |
| 3376 | } else { | 3397 | } else { |
| 3377 | enc.symbol( | 3398 | enc.symbol( |
| 3378 | usize::from(ref_frame == LAST2_FRAME), | 3399 | usize::from(ref_frame == LAST2_FRAME), |
| 3379 | - &mut cdfs.single_ref[single_ref_p4_ctx(above, None, left, None)][3], | 3400 | + &mut cdfs.single_ref[single_ref_p4_ctx(above, above_ref1, left, left_ref1)][3], |
| 3380 | ); | 3401 | ); |
| 3381 | } | 3402 | } |
| 3382 | } | 3403 | } |
| @@ -3946,7 +3967,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 3946 | } | 3967 | } |
| 3947 | mode_for_tx = 0; | 3968 | mode_for_tx = 0; |
| 3948 | } else { | 3969 | } else { |
| 3949 | - write_single_ref(&mut enc, &mut cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]); | 3970 | + write_single_ref(&mut enc, &mut cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.above_ref1[mi_c], neighbours.left_ref[mi_r], neighbours.left_ref1[mi_r]); |
| 3950 | 3971 | ||
| 3951 | let stack = find_mv_stack( | 3972 | let stack = find_mv_stack( |
| 3952 | &grid, | 3973 | &grid, |
| @@ -4178,7 +4199,7 @@ fn write_inter_frame_leaf( | |||
| 4178 | } | 4199 | } |
| 4179 | mode_for_tx = 0; | 4200 | mode_for_tx = 0; |
| 4180 | } else { | 4201 | } else { |
| 4181 | - write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]); | 4202 | + write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.above_ref1[mi_c], neighbours.left_ref[mi_r], neighbours.left_ref1[mi_r]); |
| 4182 | 4203 | ||
| 4183 | let stack = find_mv_stack( | 4204 | let stack = find_mv_stack( |
| 4184 | grid, | 4205 | grid, |
| @@ -4397,7 +4418,7 @@ fn write_inter_frame_leaf8( | |||
| 4397 | } | 4418 | } |
| 4398 | mode_for_tx = 0; | 4419 | mode_for_tx = 0; |
| 4399 | } else { | 4420 | } else { |
| 4400 | - write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[leaf_mi.1], neighbours.left_ref[leaf_mi.0]); | 4421 | + write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[leaf_mi.1], neighbours.above_ref1[leaf_mi.1], neighbours.left_ref[leaf_mi.0], neighbours.left_ref1[leaf_mi.0]); |
| 4401 | 4422 | ||
| 4402 | let stack = find_mv_stack( | 4423 | let stack = find_mv_stack( |
| 4403 | grid, | 4424 | grid, |