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