wip(av1): per-symbol EC_RNG trace through write_coeffs/write_eob
Browse files at this commitdcd164478ea94a7a10eb130f30a4d262da79565aParent: 24b23ce1 changed file +86 -11
- crates/ec-av1/src/tile.rs +86 -11
crates/ec-av1/src/tile.rsmodified+86 -11
| @@ -1130,6 +1130,7 @@ fn write_block_planes( | |||
| 1130 | scan, | 1130 | scan, |
| 1131 | skip_ctx, | 1131 | skip_ctx, |
| 1132 | dc_sign_ctx(around[plane].dc_vote), | 1132 | dc_sign_ctx(around[plane].dc_vote), |
| 1133 | + Some(plane), | ||
| 1133 | ); | 1134 | ); |
| 1134 | ec_rng_trace(|| { | 1135 | ec_rng_trace(|| { |
| 1135 | format!( | 1136 | format!( |
| @@ -1279,7 +1280,7 @@ pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet) -> f64 { | |||
| 1279 | }; | 1280 | }; |
| 1280 | let mut enc = SymbolEncoder::new(); | 1281 | let mut enc = SymbolEncoder::new(); |
| 1281 | enc.reset_bits(); | 1282 | 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); |
| 1283 | enc.bits() | 1284 | enc.bits() |
| 1284 | } | 1285 | } |
| 1285 | 1286 | ||
| @@ -1312,6 +1313,7 @@ pub(crate) fn partition_bits(side: usize, split: bool) -> f64 { | |||
| 1312 | /// levels the contexts below are read from are the levels of | 1313 | /// levels the contexts below are read from are the levels of |
| 1313 | /// coefficients later in the scan, which a decoder walking the scan backwards | 1314 | /// coefficients later in the scan, which a decoder walking the scan backwards |
| 1314 | /// already has. | 1315 | /// already has. |
| 1316 | +#[allow(clippy::too_many_arguments)] | ||
| 1315 | fn write_coeffs( | 1317 | fn write_coeffs( |
| 1316 | enc: &mut SymbolEncoder, | 1318 | enc: &mut SymbolEncoder, |
| 1317 | coding: &mut TxbTables, | 1319 | coding: &mut TxbTables, |
| @@ -1319,13 +1321,26 @@ fn write_coeffs( | |||
| 1319 | scan: &[u16], | 1321 | scan: &[u16], |
| 1320 | skip_ctx: usize, | 1322 | skip_ctx: usize, |
| 1321 | sign_ctx: usize, | 1323 | sign_ctx: usize, |
| 1324 | + plane: Option<usize>, | ||
| 1322 | ) { | 1325 | ) { |
| 1326 | + if let Some(plane) = plane { | ||
| 1327 | + ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell())); | ||
| 1328 | + } | ||
| 1323 | let side = coding.side; | 1329 | let side = coding.side; |
| 1324 | let eob = scan | 1330 | let eob = scan |
| 1325 | .iter() | 1331 | .iter() |
| 1326 | .rposition(|&pos| grid[pos as usize] != 0) | 1332 | .rposition(|&pos| grid[pos as usize] != 0) |
| 1327 | .map_or(0, |i| i + 1); | 1333 | .map_or(0, |i| i + 1); |
| 1328 | enc.symbol(usize::from(eob == 0), &mut coding.txb_skip[skip_ctx]); | 1334 | 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 | + } | ||
| 1329 | if eob == 0 { | 1344 | if eob == 0 { |
| 1330 | return; | 1345 | return; |
| 1331 | } | 1346 | } |
| @@ -1334,9 +1349,12 @@ fn write_coeffs( | |||
| 1334 | // uses DCT_DCT, which is index one of `Tx_Type_Intra_Inv_Set2`. | 1349 | // uses DCT_DCT, which is index one of `Tx_Type_Intra_Inv_Set2`. |
| 1335 | if let Some(tx_type) = coding.tx_type.as_deref_mut() { | 1350 | if let Some(tx_type) = coding.tx_type.as_deref_mut() { |
| 1336 | enc.symbol(TX_TYPE_DCT_DCT_SET2, tx_type); | 1351 | 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 | + } | ||
| 1337 | } | 1355 | } |
| 1338 | 1356 | ||
| 1339 | - write_eob(enc, coding, eob); | 1357 | + write_eob(enc, coding, eob, plane); |
| 1340 | 1358 | ||
| 1341 | for scan_idx in (0..eob).rev() { | 1359 | for scan_idx in (0..eob).rev() { |
| 1342 | let pos = scan[scan_idx] as usize; | 1360 | let pos = scan[scan_idx] as usize; |
| @@ -1344,16 +1362,29 @@ fn write_coeffs( | |||
| 1344 | let level = grid[pos].abs(); | 1362 | let level = grid[pos].abs(); |
| 1345 | if scan_idx == eob - 1 { | 1363 | if scan_idx == eob - 1 { |
| 1346 | let ctx = eob_coeff_ctx(scan_idx, side * side); | 1364 | let ctx = eob_coeff_ctx(scan_idx, side * side); |
| 1347 | - enc.symbol( | 1365 | + let sym = (level.min(NUM_BASE_LEVELS + 1) - 1) as usize; |
| 1348 | - (level.min(NUM_BASE_LEVELS + 1) - 1) as usize, | 1366 | + enc.symbol(sym, &mut coding.base_eob[ctx]); |
| 1349 | - &mut coding.base_eob[ctx], | 1367 | + if plane.is_some() { |
| 1350 | - ); | 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 | + } | ||
| 1351 | } else { | 1376 | } else { |
| 1352 | let ctx = base_ctx(grid, side, row, col); | 1377 | let ctx = base_ctx(grid, side, row, col); |
| 1353 | - enc.symbol( | 1378 | + let sym = level.min(NUM_BASE_LEVELS + 1) as usize; |
| 1354 | - level.min(NUM_BASE_LEVELS + 1) as usize, | 1379 | + enc.symbol(sym, &mut coding.base[ctx]); |
| 1355 | - &mut coding.base[ctx], | 1380 | + if plane.is_some() { |
| 1356 | - ); | 1381 | + ec_rng_trace(|| { |
| 1382 | + format!( | ||
| 1383 | + "EC_BASE scan_idx={scan_idx} ctx={ctx} level={sym} tell={}", | ||
| 1384 | + enc.tell() | ||
| 1385 | + ) | ||
| 1386 | + }); | ||
| 1387 | + } | ||
| 1357 | } | 1388 | } |
| 1358 | if level > NUM_BASE_LEVELS { | 1389 | if level > NUM_BASE_LEVELS { |
| 1359 | let ctx = br_ctx(grid, side, row, col); | 1390 | let ctx = br_ctx(grid, side, row, col); |
| @@ -1362,6 +1393,14 @@ fn write_coeffs( | |||
| 1362 | while sent < COEFF_BASE_RANGE { | 1393 | while sent < COEFF_BASE_RANGE { |
| 1363 | let k = remaining.min(BR_STEP); | 1394 | let k = remaining.min(BR_STEP); |
| 1364 | enc.symbol(k as usize, &mut coding.br[ctx]); | 1395 | 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 | + } | ||
| 1365 | if k < BR_STEP { | 1404 | if k < BR_STEP { |
| 1366 | break; | 1405 | break; |
| 1367 | } | 1406 | } |
| @@ -1380,6 +1419,15 @@ fn write_coeffs( | |||
| 1380 | } | 1419 | } |
| 1381 | if pos == 0 { | 1420 | if pos == 0 { |
| 1382 | enc.symbol(usize::from(level < 0), &mut coding.dc_sign[sign_ctx]); | 1421 | 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 | + } | ||
| 1383 | } else { | 1431 | } else { |
| 1384 | enc.literal(u32::from(level < 0), 1); | 1432 | enc.literal(u32::from(level < 0), 1); |
| 1385 | } | 1433 | } |
| @@ -1387,14 +1435,20 @@ fn write_coeffs( | |||
| 1387 | // of itself here, after its own sign (spec 5.11.39). | 1435 | // of itself here, after its own sign (spec 5.11.39). |
| 1388 | if level.abs() > MAX_BR_LEVEL { | 1436 | if level.abs() > MAX_BR_LEVEL { |
| 1389 | write_golomb(enc, (level.abs() - MAX_BR_LEVEL - 1) as u32); | 1437 | 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 | + } | ||
| 1441 | + } | ||
| 1390 | } | 1442 | } |
| 1443 | + if let Some(plane) = plane { | ||
| 1444 | + ec_rng_trace(|| format!("EC_PLANE plane={plane} eob={eob} tell_after={}", enc.tell())); | ||
| 1391 | } | 1445 | } |
| 1392 | } | 1446 | } |
| 1393 | 1447 | ||
| 1394 | /// The end-of-block position (spec 5.11.39): which group of scan positions the | 1448 | /// The end-of-block position (spec 5.11.39): which group of scan positions the |
| 1395 | /// last coded coefficient falls in, then its offset inside that group — the | 1449 | /// last coded coefficient falls in, then its offset inside that group — the |
| 1396 | /// offset's top bit from a CDF and the rest as raw bits. | 1450 | /// 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>) { |
| 1398 | /// `Eob_Group_Start` (spec 5.11.39): the first scan position each group of | 1452 | /// `Eob_Group_Start` (spec 5.11.39): the first scan position each group of |
| 1399 | /// end-of-block positions covers, indexed by the group's own number. | 1453 | /// end-of-block positions covers, indexed by the group's own number. |
| 1400 | const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513]; | 1454 | 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) { | |||
| 1408 | // The groups are numbered from one, and how many of them the transform | 1462 | // The groups are numbered from one, and how many of them the transform |
| 1409 | // reaches is the size of its own end-of-block alphabet. | 1463 | // reaches is the size of its own end-of-block alphabet. |
| 1410 | enc.symbol(group - 1, coding.eob_pt); | 1464 | 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 | + } | ||
| 1411 | 1468 | ||
| 1412 | let bits = OFFSET_BITS[group]; | 1469 | let bits = OFFSET_BITS[group]; |
| 1413 | if bits > 0 { | 1470 | if bits > 0 { |
| 1414 | let offset = (eob - GROUP_START[group]) as u32; | 1471 | let offset = (eob - GROUP_START[group]) as u32; |
| 1415 | let top = (offset >> (bits - 1)) & 1; | 1472 | let top = (offset >> (bits - 1)) & 1; |
| 1416 | enc.symbol(top as usize, &mut coding.eob_extra[group - 3]); | 1473 | 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 | + } | ||
| 1417 | if bits > 1 { | 1483 | if bits > 1 { |
| 1418 | enc.literal(offset & ((1 << (bits - 1)) - 1), bits - 1); | 1484 | 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 | + } | ||
| 1419 | } | 1493 | } |
| 1420 | } | 1494 | } |
| 1421 | } | 1495 | } |
| @@ -2622,6 +2696,7 @@ mod tests { | |||
| 2622 | &scan, | 2696 | &scan, |
| 2623 | 0, | 2697 | 0, |
| 2624 | 0, | 2698 | 0, |
| 2699 | + None, | ||
| 2625 | ); | 2700 | ); |
| 2626 | priced += luma_32_coeff_bits(grid); | 2701 | priced += luma_32_coeff_bits(grid); |
| 2627 | } | 2702 | } |