forge
Explore
Log in

Don't you dare go Hollow

wip(av1): per-symbol EC_RNG trace through write_coeffs/write_eob

Browse files at this commit

dcd164478ea94a7a10eb130f30a4d262da79565aAhmet Kaan GümüşParent: 24b23ce
Every coefficient-path symbol (txb_skip, tx type, eob_pt/extra/bits,
base, base-range, DC sign, golomb tail) now prints under EC_RNG in the
same tag format the debug aomdec build emits, so the two traces diff
line-for-line. A plane: Option<usize> argument gates the prints to the
real tile writer -- the rate estimator calls write_coeffs thousands of
times per frame and drowned the trace without it.

The trace names the true-size desync: at mi(24,8), the first whole
32x32 quadrant straddling the true frame height, the writer's DC-sign
context is 0 where the reference decoder computes 2 at the same tell.
Root cause still open on this lane.
crates/ec-av1/src/tile.rsmodified+86 -11
@@ -1130,6 +1130,7 @@ fn write_block_planes(
11301130 scan,
11311131 skip_ctx,
11321132 dc_sign_ctx(around[plane].dc_vote),
1133+ Some(plane),
11331134 );
11341135 ec_rng_trace(|| {
11351136 format!(
@@ -1279,7 +1280,7 @@ pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet) -> f64 {
12791280 };
12801281 let mut enc = SymbolEncoder::new();
12811282 enc.reset_bits();
1282- write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0);
1283+ write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None);
12831284 enc.bits()
12841285 }
12851286
@@ -1312,6 +1313,7 @@ pub(crate) fn partition_bits(side: usize, split: bool) -> f64 {
13121313 /// levels the contexts below are read from are the levels of
13131314 /// coefficients later in the scan, which a decoder walking the scan backwards
13141315 /// already has.
1316+#[allow(clippy::too_many_arguments)]
13151317 fn write_coeffs(
13161318 enc: &mut SymbolEncoder,
13171319 coding: &mut TxbTables,
@@ -1319,13 +1321,26 @@ fn write_coeffs(
13191321 scan: &[u16],
13201322 skip_ctx: usize,
13211323 sign_ctx: usize,
1324+ plane: Option<usize>,
13221325 ) {
1326+ if let Some(plane) = plane {
1327+ ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell()));
1328+ }
13231329 let side = coding.side;
13241330 let eob = scan
13251331 .iter()
13261332 .rposition(|&pos| grid[pos as usize] != 0)
13271333 .map_or(0, |i| i + 1);
13281334 enc.symbol(usize::from(eob == 0), &mut coding.txb_skip[skip_ctx]);
1335+ if plane.is_some() {
1336+ ec_rng_trace(|| {
1337+ format!(
1338+ "EC_TXBSKIP ctx={skip_ctx} eob0={} tell={}",
1339+ usize::from(eob == 0),
1340+ enc.tell()
1341+ )
1342+ });
1343+ }
13291344 if eob == 0 {
13301345 return;
13311346 }
@@ -1334,9 +1349,12 @@ fn write_coeffs(
13341349 // uses DCT_DCT, which is index one of `Tx_Type_Intra_Inv_Set2`.
13351350 if let Some(tx_type) = coding.tx_type.as_deref_mut() {
13361351 enc.symbol(TX_TYPE_DCT_DCT_SET2, tx_type);
1352+ if plane.is_some() {
1353+ ec_rng_trace(|| format!("EC_TXTYPE tell={}", enc.tell()));
1354+ }
13371355 }
13381356
1339- write_eob(enc, coding, eob);
1357+ write_eob(enc, coding, eob, plane);
13401358
13411359 for scan_idx in (0..eob).rev() {
13421360 let pos = scan[scan_idx] as usize;
@@ -1344,16 +1362,29 @@ fn write_coeffs(
13441362 let level = grid[pos].abs();
13451363 if scan_idx == eob - 1 {
13461364 let ctx = eob_coeff_ctx(scan_idx, side * side);
1347- enc.symbol(
1348- (level.min(NUM_BASE_LEVELS + 1) - 1) as usize,
1349- &mut coding.base_eob[ctx],
1350- );
1365+ let sym = (level.min(NUM_BASE_LEVELS + 1) - 1) as usize;
1366+ enc.symbol(sym, &mut coding.base_eob[ctx]);
1367+ if plane.is_some() {
1368+ ec_rng_trace(|| {
1369+ format!(
1370+ "EC_BASEEOB scan_idx={scan_idx} ctx={ctx} level={} tell={}",
1371+ sym + 1,
1372+ enc.tell()
1373+ )
1374+ });
1375+ }
13511376 } else {
13521377 let ctx = base_ctx(grid, side, row, col);
1353- enc.symbol(
1354- level.min(NUM_BASE_LEVELS + 1) as usize,
1355- &mut coding.base[ctx],
1356- );
1378+ let sym = level.min(NUM_BASE_LEVELS + 1) as usize;
1379+ enc.symbol(sym, &mut coding.base[ctx]);
1380+ if plane.is_some() {
1381+ ec_rng_trace(|| {
1382+ format!(
1383+ "EC_BASE scan_idx={scan_idx} ctx={ctx} level={sym} tell={}",
1384+ enc.tell()
1385+ )
1386+ });
1387+ }
13571388 }
13581389 if level > NUM_BASE_LEVELS {
13591390 let ctx = br_ctx(grid, side, row, col);
@@ -1362,6 +1393,14 @@ fn write_coeffs(
13621393 while sent < COEFF_BASE_RANGE {
13631394 let k = remaining.min(BR_STEP);
13641395 enc.symbol(k as usize, &mut coding.br[ctx]);
1396+ if plane.is_some() {
1397+ ec_rng_trace(|| {
1398+ format!(
1399+ "EC_BR scan_idx={scan_idx} ctx={ctx} k={k} tell={}",
1400+ enc.tell()
1401+ )
1402+ });
1403+ }
13651404 if k < BR_STEP {
13661405 break;
13671406 }
@@ -1380,6 +1419,15 @@ fn write_coeffs(
13801419 }
13811420 if pos == 0 {
13821421 enc.symbol(usize::from(level < 0), &mut coding.dc_sign[sign_ctx]);
1422+ if plane.is_some() {
1423+ ec_rng_trace(|| {
1424+ format!(
1425+ "EC_DCSIGN ctx={sign_ctx} neg={} tell={}",
1426+ usize::from(level < 0),
1427+ enc.tell()
1428+ )
1429+ });
1430+ }
13831431 } else {
13841432 enc.literal(u32::from(level < 0), 1);
13851433 }
@@ -1387,14 +1435,20 @@ fn write_coeffs(
13871435 // of itself here, after its own sign (spec 5.11.39).
13881436 if level.abs() > MAX_BR_LEVEL {
13891437 write_golomb(enc, (level.abs() - MAX_BR_LEVEL - 1) as u32);
1438+ if plane.is_some() {
1439+ ec_rng_trace(|| format!("EC_GOLOMB tell={}", enc.tell()));
1440+ }
13901441 }
13911442 }
1443+ if let Some(plane) = plane {
1444+ ec_rng_trace(|| format!("EC_PLANE plane={plane} eob={eob} tell_after={}", enc.tell()));
1445+ }
13921446 }
13931447
13941448 /// The end-of-block position (spec 5.11.39): which group of scan positions the
13951449 /// last coded coefficient falls in, then its offset inside that group — the
13961450 /// offset's top bit from a CDF and the rest as raw bits.
1397-fn write_eob(enc: &mut SymbolEncoder, coding: &mut TxbTables, eob: usize) {
1451+fn write_eob(enc: &mut SymbolEncoder, coding: &mut TxbTables, eob: usize, plane: Option<usize>) {
13981452 /// `Eob_Group_Start` (spec 5.11.39): the first scan position each group of
13991453 /// end-of-block positions covers, indexed by the group's own number.
14001454 const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513];
@@ -1408,14 +1462,34 @@ fn write_eob(enc: &mut SymbolEncoder, coding: &mut TxbTables, eob: usize) {
14081462 // The groups are numbered from one, and how many of them the transform
14091463 // reaches is the size of its own end-of-block alphabet.
14101464 enc.symbol(group - 1, coding.eob_pt);
1465+ if let Some(plane) = plane {
1466+ ec_rng_trace(|| format!("EC_EOBPT plane={plane} eob_pt={group} tell={}", enc.tell()));
1467+ }
14111468
14121469 let bits = OFFSET_BITS[group];
14131470 if bits > 0 {
14141471 let offset = (eob - GROUP_START[group]) as u32;
14151472 let top = (offset >> (bits - 1)) & 1;
14161473 enc.symbol(top as usize, &mut coding.eob_extra[group - 3]);
1474+ if let Some(plane) = plane {
1475+ ec_rng_trace(|| {
1476+ format!(
1477+ "EC_EOBEXTRA plane={plane} eob_ctx={} top={top} tell={}",
1478+ group - 3,
1479+ enc.tell()
1480+ )
1481+ });
1482+ }
14171483 if bits > 1 {
14181484 enc.literal(offset & ((1 << (bits - 1)) - 1), bits - 1);
1485+ if let Some(plane) = plane {
1486+ ec_rng_trace(|| {
1487+ format!(
1488+ "EC_EOBBITS plane={plane} eob_extra={offset} tell={}",
1489+ enc.tell()
1490+ )
1491+ });
1492+ }
14191493 }
14201494 }
14211495 }
@@ -2622,6 +2696,7 @@ mod tests {
26222696 &scan,
26232697 0,
26242698 0,
2699+ None,
26252700 );
26262701 priced += luma_32_coeff_bits(grid);
26272702 }