Merge branch 'lane-av1-signctx': per-superblock DC levels and sign context
Browse files at this commit1 changed file +175 -22
- crates/ec-av1/src/tile.rs +175 -22
crates/ec-av1/src/tile.rsmodified+175 -22
| @@ -105,10 +105,7 @@ pub fn flat_key_frame_tile(mi_cols: u32, mi_rows: u32) -> Result<Vec<u8>> { | ||
| 105 | 105 | /// picks the coefficient CDFs. |
| 106 | 106 | /// |
| 107 | 107 | /// # Errors |
| 108 | -/// Returns an error when the frame is not a whole number of 64x64 superblocks, | |
| 109 | -/// when `dc_level` is outside the range the base and base-range syntax carry | |
| 110 | -/// (`-14..=14` without zero), or when `base_q_idx` is outside the q-context | |
| 111 | -/// band whose default CDFs this crate carries. | |
| 108 | +/// As [`dc_key_frame_tile_levels`], which this is the one-level case of. | |
| 112 | 109 | pub fn dc_key_frame_tile( |
| 113 | 110 | mi_cols: u32, |
| 114 | 111 | mi_rows: u32, |
| @@ -116,7 +113,41 @@ pub fn dc_key_frame_tile( | ||
| 116 | 113 | dc_level: i32, |
| 117 | 114 | ) -> Result<Vec<u8>> { |
| 118 | 115 | check_superblocks(mi_cols, mi_rows)?; |
| 119 | - if dc_level == 0 || dc_level.abs() > MAX_LEVEL { | |
| 116 | + let blocks = ((mi_cols / SB_MI) * (mi_rows / SB_MI)) as usize; | |
| 117 | + dc_key_frame_tile_levels(mi_cols, mi_rows, base_q_idx, &vec![dc_level; blocks]) | |
| 118 | +} | |
| 119 | + | |
| 120 | +/// Writes the payload of a one-tile key frame carrying one luma DC coefficient | |
| 121 | +/// per superblock, `levels` giving them in the raster order the superblocks are | |
| 122 | +/// coded in. | |
| 123 | +/// | |
| 124 | +/// Each superblock decodes to a flat block of its own grey, so a frame written | |
| 125 | +/// here is a grid of greys the caller chooses — which is what makes the sign | |
| 126 | +/// context observable: a block's sign context is read off its coded neighbours, | |
| 127 | +/// and a frame whose levels differ in sign exercises the three-way split that a | |
| 128 | +/// single-sign frame cannot reach. | |
| 129 | +/// | |
| 130 | +/// # Errors | |
| 131 | +/// Returns an error when the frame is not a whole number of 64x64 superblocks, | |
| 132 | +/// when `levels` does not carry exactly one level per superblock, when a level | |
| 133 | +/// is outside the range the base and base-range syntax carry (`-14..=14` | |
| 134 | +/// without zero), or when `base_q_idx` is outside the q-context band whose | |
| 135 | +/// default CDFs this crate carries. | |
| 136 | +pub fn dc_key_frame_tile_levels( | |
| 137 | + mi_cols: u32, | |
| 138 | + mi_rows: u32, | |
| 139 | + base_q_idx: u8, | |
| 140 | + levels: &[i32], | |
| 141 | +) -> Result<Vec<u8>> { | |
| 142 | + check_superblocks(mi_cols, mi_rows)?; | |
| 143 | + let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI); | |
| 144 | + if levels.len() != (sb_cols * sb_rows) as usize { | |
| 145 | + return Err(Error::unsupported( | |
| 146 | + "AV1 tile", | |
| 147 | + "a DC-only key frame needs one level per 64x64 superblock", | |
| 148 | + )); | |
| 149 | + } | |
| 150 | + if levels.iter().any(|&l| l == 0 || l.abs() > MAX_LEVEL) { | |
| 120 | 151 | return Err(Error::unsupported( |
| 121 | 152 | "AV1 tile", |
| 122 | 153 | "a DC-only key frame is written for levels -14..=14 without zero; \ |
| @@ -130,13 +161,20 @@ pub fn dc_key_frame_tile( | ||
| 130 | 161 | base_q_idx must be 61..=120", |
| 131 | 162 | )); |
| 132 | 163 | } |
| 133 | - let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI); | |
| 134 | - let level = dc_level.abs(); | |
| 135 | - let negative = dc_level < 0; | |
| 136 | 164 | |
| 165 | + // The sign of the DC each coded block left behind, for the two neighbours | |
| 166 | + // the sign context is read from: one row of them above, and the block to | |
| 167 | + // the left, which is dropped at the start of every superblock row the way a | |
| 168 | + // decoder clears its left context there. | |
| 169 | + let mut above: Vec<Option<bool>> = vec![None; sb_cols as usize]; | |
| 137 | 170 | let mut enc = SymbolEncoder::new(); |
| 138 | 171 | for r in 0..sb_rows { |
| 172 | + let mut left: Option<bool> = None; | |
| 139 | 173 | for c in 0..sb_cols { |
| 174 | + let dc_level = levels[(r * sb_cols + c) as usize]; | |
| 175 | + let level = dc_level.abs(); | |
| 176 | + let negative = dc_level < 0; | |
| 177 | + | |
| 140 | 178 | enc.symbol_fixed(PARTITION_NONE, &cdf::PARTITION_W64[0]); |
| 141 | 179 | |
| 142 | 180 | // Nothing is skipped now, so every neighbour's skip flag is 0 and |
| @@ -160,22 +198,22 @@ pub fn dc_key_frame_tile( | ||
| 160 | 198 | // The base-range tail. Every neighbour of the DC is zero, so its |
| 161 | 199 | // magnitude context is 0 throughout. |
| 162 | 200 | if level > NUM_BASE_LEVELS { |
| 163 | - let mut left = level - (NUM_BASE_LEVELS + 1); | |
| 201 | + let mut remaining = level - (NUM_BASE_LEVELS + 1); | |
| 164 | 202 | let mut sent = 0; |
| 165 | 203 | while sent < COEFF_BASE_RANGE { |
| 166 | - let k = left.min(BR_STEP); | |
| 204 | + let k = remaining.min(BR_STEP); | |
| 167 | 205 | enc.symbol_fixed(k as usize, &cdf::COEFF_BR_LUMA); |
| 168 | 206 | if k < BR_STEP { |
| 169 | 207 | break; |
| 170 | 208 | } |
| 171 | - left -= k; | |
| 209 | + remaining -= k; | |
| 172 | 210 | sent += BR_STEP; |
| 173 | 211 | } |
| 174 | 212 | } |
| 175 | 213 | // The signs come after the levels, DC first (spec 5.11.39). |
| 176 | 214 | enc.symbol_fixed( |
| 177 | 215 | usize::from(negative), |
| 178 | - &cdf::DC_SIGN_LUMA[dc_sign_ctx(r, c, negative)], | |
| 216 | + &cdf::DC_SIGN_LUMA[dc_sign_ctx(above[c as usize], left)], | |
| 179 | 217 | ); |
| 180 | 218 | |
| 181 | 219 | // Both chroma transform blocks are all-zero. Their planes carry no |
| @@ -184,22 +222,31 @@ pub fn dc_key_frame_tile( | ||
| 184 | 222 | // that covers its whole plane block is left: context 7. |
| 185 | 223 | enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_32); |
| 186 | 224 | enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_32); |
| 225 | + | |
| 226 | + above[c as usize] = Some(negative); | |
| 227 | + left = Some(negative); | |
| 187 | 228 | } |
| 188 | 229 | } |
| 189 | 230 | Ok(enc.finish()) |
| 190 | 231 | } |
| 191 | 232 | |
| 192 | -/// `Dc_Sign_Contexts` (spec 8.3.2) for a frame every block of which carries a | |
| 193 | -/// DC of the same sign: each of the sixteen 4x4 units above and left of the | |
| 194 | -/// block votes its neighbour's sign, so the count is 0, +-16 or +-32 and the | |
| 195 | -/// table's three-way split reduces to "no coded neighbour, or which way they | |
| 196 | -/// lean". The left column is cleared at the start of every superblock row. | |
| 197 | -fn dc_sign_ctx(sb_row: u32, sb_col: u32, negative: bool) -> usize { | |
| 198 | - let neighbours = i32::from(sb_row > 0) + i32::from(sb_col > 0); | |
| 199 | - if neighbours == 0 { | |
| 200 | - return 0; | |
| 233 | +/// `Dc_Sign_Contexts` (spec 8.3.2): every 4x4 unit above and left of the block | |
| 234 | +/// votes the sign of the DC its own block carries — plus one for a positive | |
| 235 | +/// one, minus one for a negative one, nothing for a unit with no coded | |
| 236 | +/// coefficient — and the sum picks one of three contexts. Every block here is a | |
| 237 | +/// whole superblock carrying one DC, so all sixteen units of a neighbour vote | |
| 238 | +/// together and the sum can only lean up, lean down, or cancel. | |
| 239 | +fn dc_sign_ctx(above: Option<bool>, left: Option<bool>) -> usize { | |
| 240 | + let vote = |n: Option<bool>| match n { | |
| 241 | + None => 0i32, | |
| 242 | + Some(true) => -1, | |
| 243 | + Some(false) => 1, | |
| 244 | + }; | |
| 245 | + match (vote(above) + vote(left)).signum() { | |
| 246 | + 0 => 0, | |
| 247 | + -1 => 1, | |
| 248 | + _ => 2, | |
| 201 | 249 | } |
| 202 | - if negative { 1 } else { 2 } | |
| 203 | 250 | } |
| 204 | 251 | |
| 205 | 252 | #[cfg(test)] |
| @@ -449,6 +496,112 @@ mod tests { | ||
| 449 | 496 | ); |
| 450 | 497 | } |
| 451 | 498 | |
| 499 | + /// Encode a grid of 64x64 superblocks, each carrying its own DC level, and | |
| 500 | + /// hand back the decoded planes. | |
| 501 | + fn decode_level_grid(levels: &[i32], sb_cols: u32, sb_rows: u32) -> Vec<u8> { | |
| 502 | + let (w, h) = (64 * sb_cols, 64 * sb_rows); | |
| 503 | + let mut seq = sequence_64(); | |
| 504 | + seq.max_frame_width = w; | |
| 505 | + seq.max_frame_height = h; | |
| 506 | + let mut header = flat_key_frame(); | |
| 507 | + header.frame_width = w; | |
| 508 | + header.frame_height = h; | |
| 509 | + header.upscaled_width = w; | |
| 510 | + header.render_width = w; | |
| 511 | + header.render_height = h; | |
| 512 | + header.mi_cols = sb_cols * SB_MI; | |
| 513 | + header.mi_rows = sb_rows * SB_MI; | |
| 514 | + header.tile_info.mi_col_starts = vec![0, header.mi_cols]; | |
| 515 | + header.tile_info.mi_row_starts = vec![0, header.mi_rows]; | |
| 516 | + header.quantization.base_q_idx = Q_IDX; | |
| 517 | + let tile = dc_key_frame_tile_levels(header.mi_cols, header.mi_rows, Q_IDX, levels).unwrap(); | |
| 518 | + let mut stream = temporal_delimiter(); | |
| 519 | + stream.extend_from_slice(&sequence_header_obu(&seq).unwrap()); | |
| 520 | + stream.extend_from_slice(&frame_obu(&seq, &header, &tile).unwrap()); | |
| 521 | + ffmpeg_decode(&stream, w as usize, h as usize) | |
| 522 | + } | |
| 523 | + | |
| 524 | + fn decoded_value(level: i32) -> u8 { | |
| 525 | + DECODED_AT_Q100 | |
| 526 | + .iter() | |
| 527 | + .find(|&&(l, _)| l == level) | |
| 528 | + .map(|&(_, v)| v) | |
| 529 | + .expect("the level is in the pinned table") | |
| 530 | + } | |
| 531 | + | |
| 532 | + /// `dc_predict` (spec 7.11.2) for the flat case: a block whose neighbours | |
| 533 | + /// are themselves flat predicts their average, and predicts mid-grey with | |
| 534 | + /// no neighbour at all. Every block here is a whole 64x64 superblock, so | |
| 535 | + /// the above row and the left column weigh the same. | |
| 536 | + fn dc_prediction(above: Option<u8>, left: Option<u8>) -> u8 { | |
| 537 | + match (above, left) { | |
| 538 | + (None, None) => 128, | |
| 539 | + (Some(a), None) => a, | |
| 540 | + (None, Some(l)) => l, | |
| 541 | + (Some(a), Some(l)) => ((u32::from(a) * 64 + u32::from(l) * 64 + 64) >> 7) as u8, | |
| 542 | + } | |
| 543 | + } | |
| 544 | + | |
| 545 | + /// What a DC level adds to the prediction: the pinned table is that sum | |
| 546 | + /// against a mid-grey prediction, and the residual does not depend on what | |
| 547 | + /// it is added to. | |
| 548 | + fn dc_residual(level: i32) -> i32 { | |
| 549 | + i32::from(decoded_value(level)) - 128 | |
| 550 | + } | |
| 551 | + | |
| 552 | + /// Every block reads its DC sign context off the coded blocks above and to | |
| 553 | + /// its left, and the three ways that can land — no coded neighbour, the | |
| 554 | + /// neighbours leaning one way, the neighbours cancelling — are only | |
| 555 | + /// reachable in a frame whose levels differ in sign. Getting the context | |
| 556 | + /// wrong desyncs the arithmetic decoder, so the check is that every block | |
| 557 | + /// still decodes to the grey its own level asks for. | |
| 558 | + #[test] | |
| 559 | + fn each_superblock_decodes_to_the_grey_its_own_level_asks_for() { | |
| 560 | + if !have_ffmpeg() { | |
| 561 | + eprintln!("SKIP each_superblock_decodes_to_the_grey_its_own_level_asks_for: no ffmpeg"); | |
| 562 | + return; | |
| 563 | + } | |
| 564 | + // Read in raster order the two grids put a positive, a negative and a | |
| 565 | + // cancelling sign context in front of the bottom-right block, and a | |
| 566 | + // leaning-down one in front of the second grid's right and bottom | |
| 567 | + // blocks. | |
| 568 | + for levels in [[14, 3, -3, -14], [-14, -3, 3, 14]] { | |
| 569 | + let planes = decode_level_grid(&levels, 2, 2); | |
| 570 | + let (luma, chroma) = planes.split_at(128 * 128); | |
| 571 | + // The blocks are not independent: a DC prediction reads the | |
| 572 | + // reconstructed neighbours, so each block's grey is its | |
| 573 | + // neighbours' average plus its own residual. | |
| 574 | + let mut recon = [0u8; 4]; | |
| 575 | + for (block, level) in levels.iter().enumerate() { | |
| 576 | + let (br, bc) = (block / 2, block % 2); | |
| 577 | + let above = (br > 0).then(|| recon[block - 2]); | |
| 578 | + let left = (bc > 0).then(|| recon[block - 1]); | |
| 579 | + let want = (i32::from(dc_prediction(above, left)) + dc_residual(*level)) | |
| 580 | + .clamp(0, 255) as u8; | |
| 581 | + recon[block] = want; | |
| 582 | + for y in 0..64 { | |
| 583 | + for x in 0..64 { | |
| 584 | + let i = (br * 64 + y) * 128 + bc * 64 + x; | |
| 585 | + assert_eq!( | |
| 586 | + luma[i], want, | |
| 587 | + "luma at ({x}, {y}) of the block carrying level {level} in {levels:?}" | |
| 588 | + ); | |
| 589 | + } | |
| 590 | + } | |
| 591 | + } | |
| 592 | + for (i, &sample) in chroma.iter().enumerate() { | |
| 593 | + assert_eq!(sample, 128, "chroma sample {i} of {levels:?}"); | |
| 594 | + } | |
| 595 | + } | |
| 596 | + } | |
| 597 | + | |
| 598 | + #[test] | |
| 599 | + fn a_level_grid_that_does_not_cover_the_frame_is_refused() { | |
| 600 | + assert!(dc_key_frame_tile_levels(32, 32, 100, &[3, 3]).is_err()); | |
| 601 | + assert!(dc_key_frame_tile_levels(32, 32, 100, &[3, 3, 3, 3, 3]).is_err()); | |
| 602 | + assert!(dc_key_frame_tile_levels(32, 32, 100, &[3, 3, 0, 3]).is_err()); | |
| 603 | + } | |
| 604 | + | |
| 452 | 605 | #[test] |
| 453 | 606 | fn dc_levels_the_base_syntax_cannot_carry_are_refused() { |
| 454 | 607 | assert!(dc_key_frame_tile(16, 16, 100, 0).is_err()); |