Merge branch 'lane-av1-truesize': write the true frame size, reconstruct it like a decoder
Browse files at this commit6 changed files +797 -155
- crates/ec-av1/src/encode.rs +259 -79
- crates/ec-av1/src/encoder.rs +2 -2
- crates/ec-av1/src/intra.rs +19 -4
- crates/ec-av1/src/mc.rs +45 -13
- crates/ec-av1/src/motion.rs +11 -3
- crates/ec-av1/src/tile.rs +461 -54
crates/ec-av1/src/encode.rsmodified+259 -79
| @@ -379,7 +379,11 @@ pub(crate) fn key_frame_headers_colour( | |||
| 379 | enable_ref_frame_mvs: false, | 379 | enable_ref_frame_mvs: false, |
| 380 | film_grain_params_present: false, | 380 | film_grain_params_present: false, |
| 381 | }; | 381 | }; |
| 382 | - let (mi_cols, mi_rows) = (w / 4, h / 4); | 382 | + // spec `compute_image_size` (5.9.15): MiCols/MiRows come from the frame's |
| 383 | + // own (true, unpadded) width and height, not from any block-grid | ||
| 384 | + // alignment — a decoder derives these straight from `frame_width`/ | ||
| 385 | + // `frame_height` below, so this must match it exactly. | ||
| 386 | + let (mi_cols, mi_rows) = (2 * ((w + 7) >> 3), 2 * ((h + 7) >> 3)); | ||
| 383 | let header = FrameHeader { | 387 | let header = FrameHeader { |
| 384 | frame_type: FrameType::Key, | 388 | frame_type: FrameType::Key, |
| 385 | frame_is_intra: true, | 389 | frame_is_intra: true, |
| @@ -471,6 +475,18 @@ struct Plane<'a> { | |||
| 471 | reconstruction: Vec<u8>, | 475 | reconstruction: Vec<u8>, |
| 472 | width: usize, | 476 | width: usize, |
| 473 | height: usize, | 477 | height: usize, |
| 478 | + /// The frame's true, decodable extent in this plane's own units -- | ||
| 479 | + /// `mi_cols`/`mi_rows` (spec `compute_image_size`) converted to samples, | ||
| 480 | + /// not `width`/`height` above, which is the padded coding-surface size. | ||
| 481 | + /// A decoder's `BlockDecoded` bookkeeping (spec 7.4) and its `mb_to_ | ||
| 482 | + /// right_edge`/`mb_to_bottom_edge` clamps (libaom's | ||
| 483 | + /// `av1/common/reconintra.c` `build_intra_predictors`) never see past | ||
| 484 | + /// this bound, even though the padded reconstruction buffer holds real | ||
| 485 | + /// (if never-decoded-by-a-real-decoder) samples out there -- reading | ||
| 486 | + /// those instead of stopping here is what desyncs prediction at the true | ||
| 487 | + /// edge. | ||
| 488 | + true_width: usize, | ||
| 489 | + true_height: usize, | ||
| 474 | } | 490 | } |
| 475 | 491 | ||
| 476 | impl Plane<'_> { | 492 | impl Plane<'_> { |
| @@ -490,11 +506,44 @@ impl Plane<'_> { | |||
| 490 | side: usize, | 506 | side: usize, |
| 491 | reach: Reach, | 507 | reach: Reach, |
| 492 | ) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<u8>) { | 508 | ) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<u8>) { |
| 493 | - let across = (x + if reach.above_right { 2 * side } else { side }).min(self.width); | 509 | + // Even a block's *own* edge is clamped to the true frame bound, not |
| 494 | - let down = (y + if reach.below_left { 2 * side } else { side }).min(self.height); | 510 | + // just a `reach` extension: libaom's `av1_predict_intra_block` |
| 495 | - let above = | 511 | + // (`av1/common/reconintra.c`) derives `xr`/`yd` -- the distance from |
| 496 | - (y > 0).then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec()); | 512 | + // this block's own right/bottom to the frame edge -- from |
| 497 | - let left = (x > 0).then(|| { | 513 | + // `mb_to_right_edge`/`mb_to_bottom_edge`, which are built from |
| 514 | + // `mi_cols`/`mi_rows` (the true, decodable extent), never from any | ||
| 515 | + // padded coding surface. `n_top_px`/`n_left_px` (how many *real* | ||
| 516 | + // samples the row/column above/left holds before the decoder starts | ||
| 517 | + // repeating the last one, same clamp as spec 7.11.2.2's `aboveLimit`/ | ||
| 518 | + // `leftLimit`) is `min(side, xr + side)` even for a block whose own | ||
| 519 | + // extent straddles that edge legally (`has_half`) -- the transform | ||
| 520 | + // still covers the whole block, but prediction does not get to read | ||
| 521 | + // real reconstructed pixels past the true edge for its own row/column, | ||
| 522 | + // only for the padded tail it invents. | ||
| 523 | + let own_across = x + side.min(self.true_width.saturating_sub(x)); | ||
| 524 | + let across = if reach.above_right { | ||
| 525 | + own_across + side.min(self.true_width.saturating_sub(own_across)) | ||
| 526 | + } else { | ||
| 527 | + own_across | ||
| 528 | + } | ||
| 529 | + .min(self.width); | ||
| 530 | + let own_down = y + side.min(self.true_height.saturating_sub(y)); | ||
| 531 | + let down = if reach.below_left { | ||
| 532 | + own_down + side.min(self.true_height.saturating_sub(own_down)) | ||
| 533 | + } else { | ||
| 534 | + own_down | ||
| 535 | + } | ||
| 536 | + .min(self.height); | ||
| 537 | + // `across`/`down` can still fall at or below `x`/`y` for a sub-block | ||
| 538 | + // whose own origin already sits past the true edge -- only reachable | ||
| 539 | + // through a partition trial priced for comparison on a split this | ||
| 540 | + // frame's straddling quadrant will go on to refuse (see | ||
| 541 | + // `a_picture_off_the_block_grid_is_refused`), never through a block | ||
| 542 | + // this encoder actually commits. Such a block has no true-edge | ||
| 543 | + // samples above or left of it at all, same as `y == 0`/`x == 0`. | ||
| 544 | + let above = (y > 0 && across > x) | ||
| 545 | + .then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec()); | ||
| 546 | + let left = (x > 0 && down > y).then(|| { | ||
| 498 | (y..down) | 547 | (y..down) |
| 499 | .map(|row| self.reconstruction[row * self.width + x - 1]) | 548 | .map(|row| self.reconstruction[row * self.width + x - 1]) |
| 500 | .collect::<Vec<_>>() | 549 | .collect::<Vec<_>>() |
| @@ -883,7 +932,7 @@ fn code_square( | |||
| 883 | x, | 932 | x, |
| 884 | y, | 933 | y, |
| 885 | side, | 934 | side, |
| 886 | - reach: Reach::of(side, x, y, luma.width, luma.height), | 935 | + reach: Reach::of(side, x, y, luma.true_width, luma.true_height), |
| 887 | set: luma_set, | 936 | set: luma_set, |
| 888 | }, | 937 | }, |
| 889 | search, | 938 | search, |
| @@ -1063,16 +1112,27 @@ pub(crate) fn encode_key_frame_inner( | |||
| 1063 | format!("intra mode {bad} is not one the encoder predicts"), | 1112 | format!("intra mode {bad} is not one the encoder predicts"), |
| 1064 | )); | 1113 | )); |
| 1065 | } | 1114 | } |
| 1066 | - let (seq, mut header) = | 1115 | + // The header carries the frame's true (unpadded) size as `frame_width`/ |
| 1067 | - key_frame_headers_colour(picture.width, picture.height, base_q_idx, color_config)?; | 1116 | + // `frame_height` -- what a decoder actually crops to -- with `mi_cols`/ |
| 1117 | + // `mi_rows` derived from that same true size (spec `compute_image_size`). | ||
| 1118 | + // `render_width`/`render_height` come out equal to it too, so | ||
| 1119 | + // `render_and_frame_size_different` is false and no render_size bits are | ||
| 1120 | + // written, mirroring libaom/rav1e: the padded `picture` below is only the | ||
| 1121 | + // internal coding surface, never what the header names. | ||
| 1122 | + let (seq, mut header) = key_frame_headers_colour(render.0, render.1, base_q_idx, color_config)?; | ||
| 1068 | header.render_width = render.0 as u32; | 1123 | header.render_width = render.0 as u32; |
| 1069 | header.render_height = render.1 as u32; | 1124 | header.render_height = render.1 as u32; |
| 1070 | 1125 | ||
| 1126 | + // Prediction reads no further than this, in each plane's own units -- | ||
| 1127 | + // see `Plane::true_width`/`true_height`. | ||
| 1128 | + let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4); | ||
| 1071 | let mut luma = Plane { | 1129 | let mut luma = Plane { |
| 1072 | source: &picture.y, | 1130 | source: &picture.y, |
| 1073 | reconstruction: vec![128; picture.y.len()], | 1131 | reconstruction: vec![128; picture.y.len()], |
| 1074 | width: picture.width, | 1132 | width: picture.width, |
| 1075 | height: picture.height, | 1133 | height: picture.height, |
| 1134 | + true_width, | ||
| 1135 | + true_height, | ||
| 1076 | }; | 1136 | }; |
| 1077 | let mut chroma = [ | 1137 | let mut chroma = [ |
| 1078 | Plane { | 1138 | Plane { |
| @@ -1080,12 +1140,16 @@ pub(crate) fn encode_key_frame_inner( | |||
| 1080 | reconstruction: vec![128; picture.u.len()], | 1140 | reconstruction: vec![128; picture.u.len()], |
| 1081 | width: picture.width / 2, | 1141 | width: picture.width / 2, |
| 1082 | height: picture.height / 2, | 1142 | height: picture.height / 2, |
| 1143 | + true_width: true_width / 2, | ||
| 1144 | + true_height: true_height / 2, | ||
| 1083 | }, | 1145 | }, |
| 1084 | Plane { | 1146 | Plane { |
| 1085 | source: &picture.v, | 1147 | source: &picture.v, |
| 1086 | reconstruction: vec![128; picture.v.len()], | 1148 | reconstruction: vec![128; picture.v.len()], |
| 1087 | width: picture.width / 2, | 1149 | width: picture.width / 2, |
| 1088 | height: picture.height / 2, | 1150 | height: picture.height / 2, |
| 1151 | + true_width: true_width / 2, | ||
| 1152 | + true_height: true_height / 2, | ||
| 1089 | }, | 1153 | }, |
| 1090 | ]; | 1154 | ]; |
| 1091 | 1155 | ||
| @@ -1099,7 +1163,14 @@ pub(crate) fn encode_key_frame_inner( | |||
| 1099 | modes, | 1163 | modes, |
| 1100 | }; | 1164 | }; |
| 1101 | 1165 | ||
| 1102 | - let (cols, rows) = (picture.width / BLOCK, picture.height / BLOCK); | 1166 | + // The block grid this frame is coded over: [`crate::tile::block_grid`]'s |
| 1167 | + // ceiling of the header's own (true-size-derived) `mi_cols`/`mi_rows`, | ||
| 1168 | + // which may be fewer columns/rows than the padded picture has room for | ||
| 1169 | + // -- a block whose origin sits past the true bound is not coded at all, | ||
| 1170 | + // matching what a decoder derives from `frame_width`/`frame_height` and | ||
| 1171 | + // so never visits either. | ||
| 1172 | + let (cols, rows) = crate::tile::block_grid(header.mi_cols, header.mi_rows); | ||
| 1173 | + let (cols, rows) = (cols as usize, rows as usize); | ||
| 1103 | // The luma mode of the block above and of the block to the left, which is | 1174 | // The luma mode of the block above and of the block to the left, which is |
| 1104 | // what picks the CDF the next block's mode is coded against -- the same | 1175 | // what picks the CDF the next block's mode is coded against -- the same |
| 1105 | // bookkeeping the tile writer keeps, so that the search is costing the | 1176 | // bookkeeping the tile writer keeps, so that the search is costing the |
| @@ -1126,6 +1197,54 @@ pub(crate) fn encode_key_frame_inner( | |||
| 1126 | let (c0, r0) = (col * 2, row * 2); | 1197 | let (c0, r0) = (col * 2, row * 2); |
| 1127 | let base = snapshot(&luma, &chroma, (x, y)); | 1198 | let base = snapshot(&luma, &chroma, (x, y)); |
| 1128 | 1199 | ||
| 1200 | + // spec `decode_partition`'s hasRows/hasCols recomputed at this | ||
| 1201 | + // 32x32 block's own half (see `crate::tile::has_half`): the | ||
| 1202 | + // true frame edge can fall inside a quadrant a superblock-level | ||
| 1203 | + // check already let through. A quadrant that fails either may | ||
| 1204 | + // not be left whole; a 16x16 sub-block that fails either (once | ||
| 1205 | + // split) is a leaf this writer has no rectangular transform | ||
| 1206 | + // for, so the search must not pick a split that would need one. | ||
| 1207 | + let (has_cols32, has_rows32) = ( | ||
| 1208 | + crate::tile::has_half( | ||
| 1209 | + col as u32 * crate::tile::BLOCK_MI, | ||
| 1210 | + crate::tile::BLOCK_MI, | ||
| 1211 | + header.mi_cols, | ||
| 1212 | + ), | ||
| 1213 | + crate::tile::has_half( | ||
| 1214 | + row as u32 * crate::tile::BLOCK_MI, | ||
| 1215 | + crate::tile::BLOCK_MI, | ||
| 1216 | + header.mi_rows, | ||
| 1217 | + ), | ||
| 1218 | + ); | ||
| 1219 | + let whole_legal = has_cols32 && has_rows32; | ||
| 1220 | + let split_legal = (0..4) | ||
| 1221 | + .map(|i| (r0 + i / 2, c0 + i % 2)) | ||
| 1222 | + .filter(|&(sr, sc)| { | ||
| 1223 | + (sr as u32) * crate::tile::SUB_MI < header.mi_rows | ||
| 1224 | + && (sc as u32) * crate::tile::SUB_MI < header.mi_cols | ||
| 1225 | + }) | ||
| 1226 | + .all(|(sr, sc)| { | ||
| 1227 | + crate::tile::has_half( | ||
| 1228 | + sc as u32 * crate::tile::SUB_MI, | ||
| 1229 | + crate::tile::SUB_MI, | ||
| 1230 | + header.mi_cols, | ||
| 1231 | + ) && crate::tile::has_half( | ||
| 1232 | + sr as u32 * crate::tile::SUB_MI, | ||
| 1233 | + crate::tile::SUB_MI, | ||
| 1234 | + header.mi_rows, | ||
| 1235 | + ) | ||
| 1236 | + }); | ||
| 1237 | + if !whole_legal && !split_legal { | ||
| 1238 | + return Err(Error::unsupported( | ||
| 1239 | + "AV1 encode", | ||
| 1240 | + format!( | ||
| 1241 | + "the 32x32 block at ({col},{row}) straddles the true frame edge \ | ||
| 1242 | + in a way that needs a rectangular (HORZ/VERT) transform this \ | ||
| 1243 | + encoder does not code yet" | ||
| 1244 | + ), | ||
| 1245 | + )); | ||
| 1246 | + } | ||
| 1247 | + | ||
| 1129 | // What the whole 32x32 costs, including the partition symbol | 1248 | // What the whole 32x32 costs, including the partition symbol |
| 1130 | // that says it is not split. | 1249 | // that says it is not split. |
| 1131 | let (whole, mut cost_whole) = code_square( | 1250 | let (whole, mut cost_whole) = code_square( |
| @@ -1145,9 +1264,20 @@ pub(crate) fn encode_key_frame_inner( | |||
| 1145 | let mut split = Vec::with_capacity(4); | 1264 | let mut split = Vec::with_capacity(4); |
| 1146 | let mut cost_split = search.lambda | 1265 | let mut cost_split = search.lambda |
| 1147 | * (partition_bits(BLOCK, true) + 4.0 * partition_bits(SUB, false)); | 1266 | * (partition_bits(BLOCK, true) + 4.0 * partition_bits(SUB, false)); |
| 1148 | - let mut split_modes = [DC_PRED; 4]; | 1267 | + let mut split_modes = Vec::with_capacity(4); |
| 1149 | - for (sub, sub_mode) in split_modes.iter_mut().enumerate() { | 1268 | + for sub in 0..4 { |
| 1150 | let (sc, sr) = (c0 + sub % 2, r0 + sub / 2); | 1269 | let (sc, sr) = (c0 + sub % 2, r0 + sub / 2); |
| 1270 | + // Same filter as the writer's `sub_positions` (spec | ||
| 1271 | + // `decode_partition`'s `r >= MiRows || c >= MiCols` early | ||
| 1272 | + // return): a sub-block whose own origin sits past the | ||
| 1273 | + // true frame is never coded, so it must not be searched | ||
| 1274 | + // or pushed onto `split` either, or the writer's block | ||
| 1275 | + // count will not match what it is prepared to name. | ||
| 1276 | + if (sr as u32) * crate::tile::SUB_MI >= header.mi_rows | ||
| 1277 | + || (sc as u32) * crate::tile::SUB_MI >= header.mi_cols | ||
| 1278 | + { | ||
| 1279 | + continue; | ||
| 1280 | + } | ||
| 1151 | let (block, cost) = code_square( | 1281 | let (block, cost) = code_square( |
| 1152 | &mut luma, | 1282 | &mut luma, |
| 1153 | &mut chroma, | 1283 | &mut chroma, |
| @@ -1157,13 +1287,13 @@ pub(crate) fn encode_key_frame_inner( | |||
| 1157 | &mode_bits(above_mode[sc], left_mode[sr]), | 1287 | &mode_bits(above_mode[sc], left_mode[sr]), |
| 1158 | ); | 1288 | ); |
| 1159 | cost_split += cost; | 1289 | cost_split += cost; |
| 1160 | - *sub_mode = block.mode; | 1290 | + split_modes.push(block.mode); |
| 1161 | above_mode[sc] = block.mode; | 1291 | above_mode[sc] = block.mode; |
| 1162 | left_mode[sr] = block.mode; | 1292 | left_mode[sr] = block.mode; |
| 1163 | split.push(block); | 1293 | split.push(block); |
| 1164 | } | 1294 | } |
| 1165 | 1295 | ||
| 1166 | - if split_blocks && cost_split < cost_whole { | 1296 | + if split_legal && (!whole_legal || (split_blocks && cost_split < cost_whole)) { |
| 1167 | modes.extend_from_slice(&split_modes); | 1297 | modes.extend_from_slice(&split_modes); |
| 1168 | blocks.push(Quadrant::Split(split)); | 1298 | blocks.push(Quadrant::Split(split)); |
| 1169 | } else { | 1299 | } else { |
| @@ -1348,6 +1478,7 @@ fn mc_trial( | |||
| 1348 | mv: (i32, i32), | 1478 | mv: (i32, i32), |
| 1349 | luma: bool, | 1479 | luma: bool, |
| 1350 | reference: &[u8], | 1480 | reference: &[u8], |
| 1481 | + stride: usize, | ||
| 1351 | ref_width: usize, | 1482 | ref_width: usize, |
| 1352 | ref_height: usize, | 1483 | ref_height: usize, |
| 1353 | skip: bool, | 1484 | skip: bool, |
| @@ -1360,6 +1491,7 @@ fn mc_trial( | |||
| 1360 | let mut prediction = vec![0u8; side * side]; | 1491 | let mut prediction = vec![0u8; side * side]; |
| 1361 | mc::predict( | 1492 | mc::predict( |
| 1362 | reference, | 1493 | reference, |
| 1494 | + stride, | ||
| 1363 | ref_width, | 1495 | ref_width, |
| 1364 | ref_height, | 1496 | ref_height, |
| 1365 | x_q4, | 1497 | x_q4, |
| @@ -1400,7 +1532,7 @@ fn search_inter_block( | |||
| 1400 | stack: &MvStack, | 1532 | stack: &MvStack, |
| 1401 | ) -> BlockCoeffs { | 1533 | ) -> BlockCoeffs { |
| 1402 | let (luma_set, chroma_set) = (TxbSet::Luma32, TxbSet::Chroma16); | 1534 | let (luma_set, chroma_set) = (TxbSet::Luma32, TxbSet::Chroma16); |
| 1403 | - let reach = Reach::of(BLOCK, x, y, luma.width, luma.height); | 1535 | + let reach = Reach::of(BLOCK, x, y, luma.true_width, luma.true_height); |
| 1404 | 1536 | ||
| 1405 | // skip / intra_inter symbol costs at a fixed context -- see this | 1537 | // skip / intra_inter symbol costs at a fixed context -- see this |
| 1406 | // function's doc comment. | 1538 | // function's doc comment. |
| @@ -1484,9 +1616,34 @@ fn search_inter_block( | |||
| 1484 | }); | 1616 | }); |
| 1485 | } | 1617 | } |
| 1486 | 1618 | ||
| 1487 | - let ref_luma = (&reference.y, reference.width, reference.height); | 1619 | + // The reference frame buffer a spec decoder holds is exactly the true |
| 1488 | - let ref_u = (&reference.u, reference.width / 2, reference.height / 2); | 1620 | + // (unpadded) coded extent -- a superblock whose partition stops early at |
| 1489 | - let ref_v = (&reference.v, reference.width / 2, reference.height / 2); | 1621 | + // `has_cols`/`has_rows` never codes syntax for the padding columns/rows |
| 1622 | + // beyond it, so those samples are never part of the decoded picture. | ||
| 1623 | + // Motion compensation clamps reads to that true extent (spec 7.11.3.4's | ||
| 1624 | + // reference sample position clamp), not to this crate's own | ||
| 1625 | + // padded-to-`SUPERBLOCK` coding surface -- passing the padded width/height | ||
| 1626 | + // here would let a fractional-pel read past the true edge pick up this | ||
| 1627 | + // encoder's own uncoded padding instead of the true edge's replicated | ||
| 1628 | + // value a decoder computes. | ||
| 1629 | + let ref_luma = ( | ||
| 1630 | + &reference.y, | ||
| 1631 | + reference.width, | ||
| 1632 | + luma.true_width, | ||
| 1633 | + luma.true_height, | ||
| 1634 | + ); | ||
| 1635 | + let ref_u = ( | ||
| 1636 | + &reference.u, | ||
| 1637 | + reference.width / 2, | ||
| 1638 | + chroma[0].true_width, | ||
| 1639 | + chroma[0].true_height, | ||
| 1640 | + ); | ||
| 1641 | + let ref_v = ( | ||
| 1642 | + &reference.v, | ||
| 1643 | + reference.width / 2, | ||
| 1644 | + chroma[1].true_width, | ||
| 1645 | + chroma[1].true_height, | ||
| 1646 | + ); | ||
| 1490 | let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV | 1647 | let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV |
| 1491 | + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero | 1648 | + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero |
| 1492 | + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV | 1649 | + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV |
| @@ -1506,6 +1663,7 @@ fn search_inter_block( | |||
| 1506 | ref_luma.0, | 1663 | ref_luma.0, |
| 1507 | ref_luma.1, | 1664 | ref_luma.1, |
| 1508 | ref_luma.2, | 1665 | ref_luma.2, |
| 1666 | + ref_luma.3, | ||
| 1509 | false, | 1667 | false, |
| 1510 | search.base_q_idx, | 1668 | search.base_q_idx, |
| 1511 | search.deadzone, | 1669 | search.deadzone, |
| @@ -1521,6 +1679,7 @@ fn search_inter_block( | |||
| 1521 | ref_u.0, | 1679 | ref_u.0, |
| 1522 | ref_u.1, | 1680 | ref_u.1, |
| 1523 | ref_u.2, | 1681 | ref_u.2, |
| 1682 | + ref_u.3, | ||
| 1524 | false, | 1683 | false, |
| 1525 | search.base_q_idx, | 1684 | search.base_q_idx, |
| 1526 | search.deadzone, | 1685 | search.deadzone, |
| @@ -1536,6 +1695,7 @@ fn search_inter_block( | |||
| 1536 | ref_v.0, | 1695 | ref_v.0, |
| 1537 | ref_v.1, | 1696 | ref_v.1, |
| 1538 | ref_v.2, | 1697 | ref_v.2, |
| 1698 | + ref_v.3, | ||
| 1539 | false, | 1699 | false, |
| 1540 | search.base_q_idx, | 1700 | search.base_q_idx, |
| 1541 | search.deadzone, | 1701 | search.deadzone, |
| @@ -1576,6 +1736,7 @@ fn search_inter_block( | |||
| 1576 | ref_luma.0, | 1736 | ref_luma.0, |
| 1577 | ref_luma.1, | 1737 | ref_luma.1, |
| 1578 | ref_luma.2, | 1738 | ref_luma.2, |
| 1739 | + ref_luma.3, | ||
| 1579 | &source_block, | 1740 | &source_block, |
| 1580 | x, | 1741 | x, |
| 1581 | y, | 1742 | y, |
| @@ -1596,6 +1757,7 @@ fn search_inter_block( | |||
| 1596 | ref_luma.0, | 1757 | ref_luma.0, |
| 1597 | ref_luma.1, | 1758 | ref_luma.1, |
| 1598 | ref_luma.2, | 1759 | ref_luma.2, |
| 1760 | + ref_luma.3, | ||
| 1599 | false, | 1761 | false, |
| 1600 | search.base_q_idx, | 1762 | search.base_q_idx, |
| 1601 | search.deadzone, | 1763 | search.deadzone, |
| @@ -1611,6 +1773,7 @@ fn search_inter_block( | |||
| 1611 | ref_u.0, | 1773 | ref_u.0, |
| 1612 | ref_u.1, | 1774 | ref_u.1, |
| 1613 | ref_u.2, | 1775 | ref_u.2, |
| 1776 | + ref_u.3, | ||
| 1614 | false, | 1777 | false, |
| 1615 | search.base_q_idx, | 1778 | search.base_q_idx, |
| 1616 | search.deadzone, | 1779 | search.deadzone, |
| @@ -1626,6 +1789,7 @@ fn search_inter_block( | |||
| 1626 | ref_v.0, | 1789 | ref_v.0, |
| 1627 | ref_v.1, | 1790 | ref_v.1, |
| 1628 | ref_v.2, | 1791 | ref_v.2, |
| 1792 | + ref_v.3, | ||
| 1629 | false, | 1793 | false, |
| 1630 | search.base_q_idx, | 1794 | search.base_q_idx, |
| 1631 | search.deadzone, | 1795 | search.deadzone, |
| @@ -1714,21 +1878,25 @@ pub(crate) fn encode_inter_frame( | |||
| 1714 | // overwrites slot 0, so the frame just coded is always what the next one | 1878 | // overwrites slot 0, so the frame just coded is always what the next one |
| 1715 | // predicts against. | 1879 | // predicts against. |
| 1716 | const LAST_SLOT: u8 = 0; | 1880 | const LAST_SLOT: u8 = 0; |
| 1717 | - let (seq, mut header) = inter_frame_headers( | 1881 | + // `render`, not `picture.width`/`picture.height`: `picture` here is |
| 1718 | - picture.width, | 1882 | + // already padded to a whole number of superblocks (`encode_sequence`'s |
| 1719 | - picture.height, | 1883 | + // `padded_to(SUPERBLOCK)`), and the header's `mi_cols`/`mi_rows` (and so |
| 1720 | - base_q_idx, | 1884 | + // every true-edge clamp downstream) must come from the frame's real, |
| 1721 | - order_hint, | 1885 | + // unpadded size, same as `key_frame_headers_colour` takes `render` and |
| 1722 | - LAST_SLOT, | 1886 | + // not the padded picture in `encode_key_frame_inner`. |
| 1723 | - )?; | 1887 | + let (seq, mut header) = |
| 1888 | + inter_frame_headers(render.0, render.1, base_q_idx, order_hint, LAST_SLOT)?; | ||
| 1724 | header.render_width = render.0 as u32; | 1889 | header.render_width = render.0 as u32; |
| 1725 | header.render_height = render.1 as u32; | 1890 | header.render_height = render.1 as u32; |
| 1726 | 1891 | ||
| 1892 | + let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4); | ||
| 1727 | let mut luma = Plane { | 1893 | let mut luma = Plane { |
| 1728 | source: &picture.y, | 1894 | source: &picture.y, |
| 1729 | reconstruction: vec![128; picture.y.len()], | 1895 | reconstruction: vec![128; picture.y.len()], |
| 1730 | width: picture.width, | 1896 | width: picture.width, |
| 1731 | height: picture.height, | 1897 | height: picture.height, |
| 1898 | + true_width, | ||
| 1899 | + true_height, | ||
| 1732 | }; | 1900 | }; |
| 1733 | let mut chroma = [ | 1901 | let mut chroma = [ |
| 1734 | Plane { | 1902 | Plane { |
| @@ -1736,12 +1904,16 @@ pub(crate) fn encode_inter_frame( | |||
| 1736 | reconstruction: vec![128; picture.u.len()], | 1904 | reconstruction: vec![128; picture.u.len()], |
| 1737 | width: picture.width / 2, | 1905 | width: picture.width / 2, |
| 1738 | height: picture.height / 2, | 1906 | height: picture.height / 2, |
| 1907 | + true_width: true_width / 2, | ||
| 1908 | + true_height: true_height / 2, | ||
| 1739 | }, | 1909 | }, |
| 1740 | Plane { | 1910 | Plane { |
| 1741 | source: &picture.v, | 1911 | source: &picture.v, |
| 1742 | reconstruction: vec![128; picture.v.len()], | 1912 | reconstruction: vec![128; picture.v.len()], |
| 1743 | width: picture.width / 2, | 1913 | width: picture.width / 2, |
| 1744 | height: picture.height / 2, | 1914 | height: picture.height / 2, |
| 1915 | + true_width: true_width / 2, | ||
| 1916 | + true_height: true_height / 2, | ||
| 1745 | }, | 1917 | }, |
| 1746 | ]; | 1918 | ]; |
| 1747 | 1919 | ||
| @@ -1754,8 +1926,14 @@ pub(crate) fn encode_inter_frame( | |||
| 1754 | }; | 1926 | }; |
| 1755 | let mode_bits_table = inter_mode_bits(); | 1927 | let mode_bits_table = inter_mode_bits(); |
| 1756 | 1928 | ||
| 1757 | - let (cols, rows) = (picture.width / BLOCK, picture.height / BLOCK); | 1929 | + // The true grid ([`crate::tile::block_grid`]'s ceiling), not the padded |
| 1758 | - let (sb_cols, sb_rows) = (cols / 2, rows / 2); | 1930 | + // coding surface's: `blocks` carries one entry per 32x32 block the tile |
| 1931 | + // writer will actually visit, same count it checks `blocks.len()` | ||
| 1932 | + // against, which is fewer than the padded surface's own block count | ||
| 1933 | + // whenever the true size is not a 64x64 multiple. | ||
| 1934 | + let (cols, rows) = crate::tile::block_grid(header.mi_cols, header.mi_rows); | ||
| 1935 | + let (cols, rows) = (cols as usize, rows as usize); | ||
| 1936 | + let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); | ||
| 1759 | let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize); | 1937 | let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize); |
| 1760 | let mut grid = MiGrid::new(mi_cols, mi_rows); | 1938 | let mut grid = MiGrid::new(mi_cols, mi_rows); |
| 1761 | let mut blocks = vec![BlockCoeffs::default(); cols * rows]; | 1939 | let mut blocks = vec![BlockCoeffs::default(); cols * rows]; |
| @@ -1764,6 +1942,11 @@ pub(crate) fn encode_inter_frame( | |||
| 1764 | for sb_c in 0..sb_cols { | 1942 | for sb_c in 0..sb_cols { |
| 1765 | for quadrant in 0..4 { | 1943 | for quadrant in 0..4 { |
| 1766 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); | 1944 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); |
| 1945 | + // Same filter as the tile writer's: a quadrant whose own mi | ||
| 1946 | + // origin is not inside the true frame is never coded. | ||
| 1947 | + if r32 >= rows || c32 >= cols { | ||
| 1948 | + continue; | ||
| 1949 | + } | ||
| 1767 | let (x, y) = (c32 * BLOCK, r32 * BLOCK); | 1950 | let (x, y) = (c32 * BLOCK, r32 * BLOCK); |
| 1768 | let (mi_row, mi_col) = (r32 * 8, c32 * 8); | 1951 | let (mi_row, mi_col) = (r32 * 8, c32 * 8); |
| 1769 | let stack = | 1952 | let stack = |
| @@ -2014,7 +2197,7 @@ mod tests { | |||
| 2014 | eprintln!("SKIP ffmpeg_decodes_exactly_what_the_encoder_reconstructed: no ffmpeg"); | 2197 | eprintln!("SKIP ffmpeg_decodes_exactly_what_the_encoder_reconstructed: no ffmpeg"); |
| 2015 | return; | 2198 | return; |
| 2016 | } | 2199 | } |
| 2017 | - for &(width, height) in &[(64usize, 64usize), (96, 64), (160, 96)] { | 2200 | + for &(width, height) in &[(64usize, 64usize), (96, 64), (160, 96), (32, 48)] { |
| 2018 | let picture = test_card(width, height); | 2201 | let picture = test_card(width, height); |
| 2019 | let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); | 2202 | let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); |
| 2020 | let decoded = ffmpeg_decode(&encoded.stream, width, height); | 2203 | let decoded = ffmpeg_decode(&encoded.stream, width, height); |
| @@ -2106,19 +2289,6 @@ mod tests { | |||
| 2106 | ffmpeg_decode_sequence(&stream, width, height, 2); | 2289 | ffmpeg_decode_sequence(&stream, width, height, 2); |
| 2107 | } | 2290 | } |
| 2108 | 2291 | ||
| 2109 | - /// The top-left `width` x `height` region of a decoded picture, for | ||
| 2110 | - /// comparing against [`Encoded::reconstruction`] (already cropped to | ||
| 2111 | - /// the same region) when ffmpeg hands back the padded, coded-size one. | ||
| 2112 | - fn crop_picture(picture: &Picture, width: usize, height: usize) -> Picture { | ||
| 2113 | - Picture { | ||
| 2114 | - width, | ||
| 2115 | - height, | ||
| 2116 | - y: crop_plane(&picture.y, picture.width, width, height), | ||
| 2117 | - u: crop_plane(&picture.u, picture.width / 2, width / 2, height / 2), | ||
| 2118 | - v: crop_plane(&picture.v, picture.width / 2, width / 2, height / 2), | ||
| 2119 | - } | ||
| 2120 | - } | ||
| 2121 | - | ||
| 2122 | /// `ffprobe`'s reported `width,height` for one OBU stream -- the coded | 2292 | /// `ffprobe`'s reported `width,height` for one OBU stream -- the coded |
| 2123 | /// frame size an AV1 decoder allocates, not necessarily the render size | 2293 | /// frame size an AV1 decoder allocates, not necessarily the render size |
| 2124 | /// (see [`a_frame_round_trips_at_its_own_size`]). | 2294 | /// (see [`a_frame_round_trips_at_its_own_size`]). |
| @@ -2186,23 +2356,18 @@ mod tests { | |||
| 2186 | (width, height), | 2356 | (width, height), |
| 2187 | "{width}x{height}: reconstruction is cropped to the picture's own size" | 2357 | "{width}x{height}: reconstruction is cropped to the picture's own size" |
| 2188 | ); | 2358 | ); |
| 2189 | - let (padded_width, padded_height) = ( | ||
| 2190 | - width.next_multiple_of(BLOCK), | ||
| 2191 | - height.next_multiple_of(BLOCK), | ||
| 2192 | - ); | ||
| 2193 | assert_eq!( | 2359 | assert_eq!( |
| 2194 | ffprobe_size(&encoded.stream), | 2360 | ffprobe_size(&encoded.stream), |
| 2195 | - (padded_width as u32, padded_height as u32), | 2361 | + (width as u32, height as u32), |
| 2196 | - "{width}x{height}: ffprobe reports the coded (padded) size" | 2362 | + "{width}x{height}: ffprobe reports the true (display) size, not the padded one" |
| 2197 | ); | 2363 | ); |
| 2198 | - let decoded = ffmpeg_decode(&encoded.stream, padded_width, padded_height); | 2364 | + let decoded = ffmpeg_decode(&encoded.stream, width, height); |
| 2199 | - let cropped = crop_picture(&decoded, width, height); | ||
| 2200 | assert_eq!( | 2365 | assert_eq!( |
| 2201 | - cropped.y, encoded.reconstruction.y, | 2366 | + decoded.y, encoded.reconstruction.y, |
| 2202 | "{width}x{height}: luma" | 2367 | "{width}x{height}: luma" |
| 2203 | ); | 2368 | ); |
| 2204 | - assert_eq!(cropped.u, encoded.reconstruction.u, "{width}x{height}: U"); | 2369 | + assert_eq!(decoded.u, encoded.reconstruction.u, "{width}x{height}: U"); |
| 2205 | - assert_eq!(cropped.v, encoded.reconstruction.v, "{width}x{height}: V"); | 2370 | + assert_eq!(decoded.v, encoded.reconstruction.v, "{width}x{height}: V"); |
| 2206 | } | 2371 | } |
| 2207 | } | 2372 | } |
| 2208 | 2373 | ||
| @@ -2223,6 +2388,14 @@ mod tests { | |||
| 2223 | /// EC_RNG-per-symbol trace against the same debug decoder found the | 2388 | /// EC_RNG-per-symbol trace against the same debug decoder found the |
| 2224 | /// bitstream byte-for-byte identical to libaom's across all 51188 | 2389 | /// bitstream byte-for-byte identical to libaom's across all 51188 |
| 2225 | /// symbols of this frame before this bug was found). | 2390 | /// symbols of this frame before this bug was found). |
| 2391 | + /// | ||
| 2392 | + /// Since the frame header started writing the true (unpadded) display | ||
| 2393 | + /// size into `frame_width`/`frame_height` rather than the padded coded | ||
| 2394 | + /// size, ffmpeg crops to 854x480 on its own -- decoding at the padded | ||
| 2395 | + /// 864x480 and cropping ourselves now reads past what ffmpeg actually | ||
| 2396 | + /// emits (`av1_common_int.h`'s render/upscale path, mirrored by | ||
| 2397 | + /// `av1_frame_size` on the decode side, crops the loop-filtered picture to | ||
| 2398 | + /// the header's own size before output). | ||
| 2226 | #[test] | 2399 | #[test] |
| 2227 | fn an_854x480_picture_round_trips_through_its_padding() { | 2400 | fn an_854x480_picture_round_trips_through_its_padding() { |
| 2228 | if !have_ffmpeg() { | 2401 | if !have_ffmpeg() { |
| @@ -2231,13 +2404,10 @@ mod tests { | |||
| 2231 | let (width, height) = (854usize, 480usize); | 2404 | let (width, height) = (854usize, 480usize); |
| 2232 | let picture = test_card(width, height); | 2405 | let picture = test_card(width, height); |
| 2233 | let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); | 2406 | let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); |
| 2234 | - let (padded_width, padded_height) = ( | 2407 | + let decoded = ffmpeg_decode(&encoded.stream, width, height); |
| 2235 | - width.next_multiple_of(BLOCK), | 2408 | + assert_eq!(decoded.y, encoded.reconstruction.y, "854x480: luma"); |
| 2236 | - height.next_multiple_of(BLOCK), | 2409 | + assert_eq!(decoded.u, encoded.reconstruction.u, "854x480: U"); |
| 2237 | - ); | 2410 | + assert_eq!(decoded.v, encoded.reconstruction.v, "854x480: V"); |
| 2238 | - let decoded = ffmpeg_decode(&encoded.stream, padded_width, padded_height); | ||
| 2239 | - let cropped = crop_picture(&decoded, width, height); | ||
| 2240 | - assert_eq!(cropped.y, encoded.reconstruction.y, "854x480: luma"); | ||
| 2241 | } | 2411 | } |
| 2242 | 2412 | ||
| 2243 | /// `Reach::top_right`/`bottom_left` against a from-scratch transcription | 2413 | /// `Reach::top_right`/`bottom_left` against a from-scratch transcription |
| @@ -2362,24 +2532,18 @@ mod tests { | |||
| 2362 | ); | 2532 | ); |
| 2363 | } | 2533 | } |
| 2364 | // See `a_frame_round_trips_at_its_own_size`: `ffprobe` reports the | 2534 | // See `a_frame_round_trips_at_its_own_size`: `ffprobe` reports the |
| 2365 | - // coded (superblock-padded) size a sequence's frames share, not the | 2535 | + // true (display) size a sequence's frames share, not the padded one. |
| 2366 | - // render size. | ||
| 2367 | - let (padded_width, padded_height) = ( | ||
| 2368 | - width.next_multiple_of(SUPERBLOCK), | ||
| 2369 | - height.next_multiple_of(SUPERBLOCK), | ||
| 2370 | - ); | ||
| 2371 | assert_eq!( | 2536 | assert_eq!( |
| 2372 | ffprobe_size(&encoded.stream), | 2537 | ffprobe_size(&encoded.stream), |
| 2373 | - (padded_width as u32, padded_height as u32), | 2538 | + (width as u32, height as u32), |
| 2374 | - "sequence: ffprobe reports the coded (padded) size" | 2539 | + "sequence: ffprobe reports the true (display) size" |
| 2375 | ); | 2540 | ); |
| 2376 | - let decoded = ffmpeg_decode_sequence(&encoded.stream, padded_width, padded_height, 3); | 2541 | + let decoded = ffmpeg_decode_sequence(&encoded.stream, width, height, 3); |
| 2377 | assert_eq!(decoded.len(), 3, "decoded frame count"); | 2542 | assert_eq!(decoded.len(), 3, "decoded frame count"); |
| 2378 | for (i, (frame, decoded)) in encoded.frames.iter().zip(&decoded).enumerate() { | 2543 | for (i, (frame, decoded)) in encoded.frames.iter().zip(&decoded).enumerate() { |
| 2379 | - let cropped = crop_picture(decoded, width, height); | 2544 | + assert_eq!(decoded.y, frame.reconstruction.y, "frame {i}: luma"); |
| 2380 | - assert_eq!(cropped.y, frame.reconstruction.y, "frame {i}: luma"); | 2545 | + assert_eq!(decoded.u, frame.reconstruction.u, "frame {i}: U"); |
| 2381 | - assert_eq!(cropped.u, frame.reconstruction.u, "frame {i}: U"); | 2546 | + assert_eq!(decoded.v, frame.reconstruction.v, "frame {i}: V"); |
| 2382 | - assert_eq!(cropped.v, frame.reconstruction.v, "frame {i}: V"); | ||
| 2383 | } | 2547 | } |
| 2384 | } | 2548 | } |
| 2385 | 2549 | ||
| @@ -2909,15 +3073,31 @@ mod tests { | |||
| 2909 | } | 3073 | } |
| 2910 | 3074 | ||
| 2911 | /// The sizes the encoder refuses, refused for a reason rather than by | 3075 | /// The sizes the encoder refuses, refused for a reason rather than by |
| 2912 | - /// panicking somewhere below. 40x32 and 32x48 are off the 32x32 block | 3076 | + /// panicking somewhere below. Most sizes off the 32x32 block grid encode |
| 2913 | - /// grid, which used to be refused outright; the encoder now pads a | 3077 | + /// fine now (see `a_frame_round_trips_at_its_own_size`) -- the true frame |
| 2914 | - /// picture like that internally (see `a_frame_round_trips_at_its_own_size`) | 3078 | + /// edge lands past the halfway point of whichever block it falls in, so |
| 2915 | - /// rather than refusing it, so those two are an accept, not a refusal, | 3079 | + /// `PARTITION_NONE` or a single gathered split flag still says everything |
| 2916 | - /// here. | 3080 | + /// the spec needs. 40x32 and 32x48 are the sizes where the edge instead |
| 3081 | + /// falls *at or before* the halfway point of a 16x16 leaf: the spec's | ||
| 3082 | + /// `decode_partition` then requires that leaf to split again, to 8x8, | ||
| 3083 | + /// which this writer has no transform/coefficient path for yet, so it is | ||
| 3084 | + /// still refused, by name rather than by corrupting the stream. | ||
| 2917 | #[test] | 3085 | #[test] |
| 2918 | fn a_picture_off_the_block_grid_is_refused() { | 3086 | fn a_picture_off_the_block_grid_is_refused() { |
| 2919 | - assert!(encode_key_frame(&Picture::grey(40, 32), 100, 0.5).is_ok()); | 3087 | + let refused = |w, h| { |
| 2920 | - assert!(encode_key_frame(&Picture::grey(32, 48), 100, 0.5).is_ok()); | 3088 | + let msg = encode_key_frame(&Picture::grey(w, h), 100, 0.5) |
| 3089 | + .expect_err(&format!( | ||
| 3090 | + "{w}x{h} needs an 8x8 split this writer does not code" | ||
| 3091 | + )) | ||
| 3092 | + .to_string(); | ||
| 3093 | + msg.contains("true frame") | ||
| 3094 | + }; | ||
| 3095 | + assert!(refused(40, 32)); | ||
| 3096 | + // 32x48 was refused here through round 9: its split quadrant only | ||
| 3097 | + // ever has two legal 16x16 subs (the other two sit past the true | ||
| 3098 | + // 48-row edge, block-aligned, no 8x8 split needed), but the writer | ||
| 3099 | + // demanded four. See `ffmpeg_decodes_exactly_what_the_encoder_ | ||
| 3100 | + // reconstructed`, which now round-trips it pixel-exact instead. | ||
| 2921 | let mut short = Picture::grey(64, 64); | 3101 | let mut short = Picture::grey(64, 64); |
| 2922 | short.u.truncate(10); | 3102 | short.u.truncate(10); |
| 2923 | assert!(encode_key_frame(&short, 100, 0.5).is_err()); | 3103 | assert!(encode_key_frame(&short, 100, 0.5).is_err()); |
crates/ec-av1/src/encoder.rsmodified+2 -2
| @@ -427,8 +427,8 @@ mod tests { | |||
| 427 | assert_eq!(*key, expect_key[t], "picture {t}: ffprobe key_frame flag"); | 427 | assert_eq!(*key, expect_key[t], "picture {t}: ffprobe key_frame flag"); |
| 428 | assert_eq!( | 428 | assert_eq!( |
| 429 | (*w, *h), | 429 | (*w, *h), |
| 430 | - (coded_w as u32, coded_h as u32), | 430 | + (width as u32, height as u32), |
| 431 | - "picture {t}: coded size" | 431 | + "picture {t}: ffprobe reports the true (display) size, not the padded coded one" |
| 432 | ); | 432 | ); |
| 433 | } | 433 | } |
| 434 | } | 434 | } |
crates/ec-av1/src/intra.rsmodified+19 -4
| @@ -306,15 +306,30 @@ fn directional(angle: u16, edges: &Edges, side: usize, dst: &mut [u8]) { | |||
| 306 | /// `dc_predict` (spec 7.11.2.5): the average of whichever neighbours exist. | 306 | /// `dc_predict` (spec 7.11.2.5): the average of whichever neighbours exist. |
| 307 | /// | 307 | /// |
| 308 | /// Only the block's own `side` samples of each edge count: what a directional | 308 | /// Only the block's own `side` samples of each edge count: what a directional |
| 309 | -/// mode reaches past them is no part of the average. | 309 | +/// mode reaches past them is no part of the average. But `side` samples it |
| 310 | +/// always is: libaom's `build_intra_predictors` (`av1/common/reconintra.c`) | ||
| 311 | +/// replicates the last real sample out to the full transform width/height | ||
| 312 | +/// before `dc_predictor` averages, so a slice truncated short by the true | ||
| 313 | +/// frame edge must be extended by repetition here too, not averaged over its | ||
| 314 | +/// own shorter length. | ||
| 310 | fn dc(above: Option<&[u8]>, left: Option<&[u8]>, side: usize) -> i32 { | 315 | fn dc(above: Option<&[u8]>, left: Option<&[u8]>, side: usize) -> i32 { |
| 311 | - let above = above.map(|a| &a[..side.min(a.len())]); | 316 | + let extend = |samples: &[u8]| -> Vec<u8> { |
| 312 | - let left = left.map(|l| &l[..side.min(l.len())]); | 317 | + if samples.len() >= side { |
| 318 | + samples[..side].to_vec() | ||
| 319 | + } else { | ||
| 320 | + let last = *samples.last().expect("an edge that exists has samples"); | ||
| 321 | + let mut v = samples.to_vec(); | ||
| 322 | + v.resize(side, last); | ||
| 323 | + v | ||
| 324 | + } | ||
| 325 | + }; | ||
| 326 | + let above = above.map(extend); | ||
| 327 | + let left = left.map(extend); | ||
| 313 | let average = |samples: &[u8]| { | 328 | let average = |samples: &[u8]| { |
| 314 | let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum(); | 329 | let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum(); |
| 315 | ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32 | 330 | ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32 |
| 316 | }; | 331 | }; |
| 317 | - match (above, left) { | 332 | + match (&above, &left) { |
| 318 | (None, None) => 128, | 333 | (None, None) => 128, |
| 319 | (Some(a), None) => average(a), | 334 | (Some(a), None) => average(a), |
| 320 | (None, Some(l)) => average(l), | 335 | (None, Some(l)) => average(l), |
crates/ec-av1/src/mc.rsmodified+45 -13
| @@ -52,18 +52,33 @@ const SUBPEL_FILTERS: [[i32; 8]; 16] = [ | |||
| 52 | [0, 0, -2, 8, 126, -6, 2, 0], | 52 | [0, 0, -2, 8, 126, -6, 2, 0], |
| 53 | ]; | 53 | ]; |
| 54 | 54 | ||
| 55 | -/// A reference sample at `(x, y)`, clamped to the plane's bounds (spec | 55 | +/// A reference sample at `(x, y)`, clamped to the frame's true (unpadded) |
| 56 | -/// 7.11.3.4's `Clip3` against the frame's edges) rather than read out of | 56 | +/// extent -- `true_width`/`true_height`, which can be narrower than |
| 57 | -/// range -- a motion vector that points past the frame repeats the edge | 57 | +/// `stride` -- rather than read out of range (spec 7.11.3.4's `Clip3` |
| 58 | -/// sample instead of panicking. | 58 | +/// against the frame's edges): a motion vector that points past the true |
| 59 | -fn sample(reference: &[u8], ref_width: usize, ref_height: usize, x: i32, y: i32) -> i32 { | 59 | +/// edge repeats the true edge's sample instead of panicking or picking up |
| 60 | - let cx = x.clamp(0, ref_width as i32 - 1) as usize; | 60 | +/// whatever this encoder's own uncoded padding columns/rows hold. |
| 61 | - let cy = y.clamp(0, ref_height as i32 - 1) as usize; | 61 | +#[allow(clippy::too_many_arguments)] |
| 62 | - i32::from(reference[cy * ref_width + cx]) | 62 | +fn sample( |
| 63 | + reference: &[u8], | ||
| 64 | + stride: usize, | ||
| 65 | + true_width: usize, | ||
| 66 | + true_height: usize, | ||
| 67 | + x: i32, | ||
| 68 | + y: i32, | ||
| 69 | +) -> i32 { | ||
| 70 | + let cx = x.clamp(0, true_width as i32 - 1) as usize; | ||
| 71 | + let cy = y.clamp(0, true_height as i32 - 1) as usize; | ||
| 72 | + i32::from(reference[cy * stride + cx]) | ||
| 63 | } | 73 | } |
| 64 | 74 | ||
| 65 | /// Predicts one `block_w * block_h` block into `dst`, row-major, from | 75 | /// Predicts one `block_w * block_h` block into `dst`, row-major, from |
| 66 | -/// `reference` (row-major, `ref_width * ref_height`, stride `ref_width`). | 76 | +/// `reference` (row-major, stride `stride`, true content |
| 77 | +/// `true_width` x `true_height` -- a reference frame's decoded picture | ||
| 78 | +/// buffer can be wider/taller than the true frame it holds, when this | ||
| 79 | +/// encoder pads a picture to a whole number of superblocks; a spec decoder | ||
| 80 | +/// never codes syntax for those extra columns/rows, so motion compensation | ||
| 81 | +/// must clamp to the true extent, not the buffer's own stride). | ||
| 67 | /// | 82 | /// |
| 68 | /// `x_q4` / `y_q4` are the block's top-left position in the reference, in | 83 | /// `x_q4` / `y_q4` are the block's top-left position in the reference, in |
| 69 | /// 1/16-pel units (an integer MV has its low 4 bits zero); this function | 84 | /// 1/16-pel units (an integer MV has its low 4 bits zero); this function |
| @@ -76,8 +91,9 @@ fn sample(reference: &[u8], ref_width: usize, ref_height: usize, x: i32, y: i32) | |||
| 76 | #[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape | 91 | #[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape |
| 77 | pub fn predict( | 92 | pub fn predict( |
| 78 | reference: &[u8], | 93 | reference: &[u8], |
| 79 | - ref_width: usize, | 94 | + stride: usize, |
| 80 | - ref_height: usize, | 95 | + true_width: usize, |
| 96 | + true_height: usize, | ||
| 81 | x_q4: i32, | 97 | x_q4: i32, |
| 82 | y_q4: i32, | 98 | y_q4: i32, |
| 83 | block_w: usize, | 99 | block_w: usize, |
| @@ -105,7 +121,7 @@ pub fn predict( | |||
| 105 | let mut sum = 0; | 121 | let mut sum = 0; |
| 106 | for (t, &tap) in h_filter.iter().enumerate() { | 122 | for (t, &tap) in h_filter.iter().enumerate() { |
| 107 | let x = x0 + c as i32 + t as i32 - 3; | 123 | let x = x0 + c as i32 + t as i32 - 3; |
| 108 | - sum += tap * sample(reference, ref_width, ref_height, x, y); | 124 | + sum += tap * sample(reference, stride, true_width, true_height, x, y); |
| 109 | } | 125 | } |
| 110 | intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); | 126 | intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); |
| 111 | } | 127 | } |
| @@ -132,7 +148,17 @@ mod tests { | |||
| 132 | let height = 12; | 148 | let height = 12; |
| 133 | let reference: Vec<u8> = (0..width * height).map(|i| (i * 7 % 251) as u8).collect(); | 149 | let reference: Vec<u8> = (0..width * height).map(|i| (i * 7 % 251) as u8).collect(); |
| 134 | let mut dst = vec![0u8; 6 * 6]; | 150 | let mut dst = vec![0u8; 6 * 6]; |
| 135 | - predict(&reference, width, height, 3 * 16, 4 * 16, 6, 6, &mut dst); | 151 | + predict( |
| 152 | + &reference, | ||
| 153 | + width, | ||
| 154 | + width, | ||
| 155 | + height, | ||
| 156 | + 3 * 16, | ||
| 157 | + 4 * 16, | ||
| 158 | + 6, | ||
| 159 | + 6, | ||
| 160 | + &mut dst, | ||
| 161 | + ); | ||
| 136 | for row in 0..6 { | 162 | for row in 0..6 { |
| 137 | for col in 0..6 { | 163 | for col in 0..6 { |
| 138 | assert_eq!( | 164 | assert_eq!( |
| @@ -157,6 +183,7 @@ mod tests { | |||
| 157 | predict( | 183 | predict( |
| 158 | &reference, | 184 | &reference, |
| 159 | width, | 185 | width, |
| 186 | + width, | ||
| 160 | height, | 187 | height, |
| 161 | 5 * 16 + 8, | 188 | 5 * 16 + 8, |
| 162 | 5 * 16, | 189 | 5 * 16, |
| @@ -187,6 +214,7 @@ mod tests { | |||
| 187 | predict( | 214 | predict( |
| 188 | &reference, | 215 | &reference, |
| 189 | width, | 216 | width, |
| 217 | + width, | ||
| 190 | height, | 218 | height, |
| 191 | 5 * 16 + xfrac, | 219 | 5 * 16 + xfrac, |
| 192 | 5 * 16 + yfrac, | 220 | 5 * 16 + yfrac, |
| @@ -219,6 +247,7 @@ mod tests { | |||
| 219 | predict( | 247 | predict( |
| 220 | &reference, | 248 | &reference, |
| 221 | width, | 249 | width, |
| 250 | + width, | ||
| 222 | height, | 251 | height, |
| 223 | 3 * 16 + 8, | 252 | 3 * 16 + 8, |
| 224 | 3 * 16, | 253 | 3 * 16, |
| @@ -230,6 +259,7 @@ mod tests { | |||
| 230 | predict( | 259 | predict( |
| 231 | &reference, | 260 | &reference, |
| 232 | width, | 261 | width, |
| 262 | + width, | ||
| 233 | height, | 263 | height, |
| 234 | 3 * 16, | 264 | 3 * 16, |
| 235 | 3 * 16 + 8, | 265 | 3 * 16 + 8, |
| @@ -271,6 +301,7 @@ mod tests { | |||
| 271 | predict( | 301 | predict( |
| 272 | &reference, | 302 | &reference, |
| 273 | width, | 303 | width, |
| 304 | + width, | ||
| 274 | height, | 305 | height, |
| 275 | -50 * 16 + 5, | 306 | -50 * 16 + 5, |
| 276 | -50 * 16 + 5, | 307 | -50 * 16 + 5, |
| @@ -288,6 +319,7 @@ mod tests { | |||
| 288 | predict( | 319 | predict( |
| 289 | &reference, | 320 | &reference, |
| 290 | width, | 321 | width, |
| 322 | + width, | ||
| 291 | height, | 323 | height, |
| 292 | 50 * 16 + 5, | 324 | 50 * 16 + 5, |
| 293 | 50 * 16 + 5, | 325 | 50 * 16 + 5, |
crates/ec-av1/src/motion.rsmodified+11 -3
| @@ -191,6 +191,7 @@ pub struct MotionSearch { | |||
| 191 | #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor | 191 | #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor |
| 192 | pub fn search( | 192 | pub fn search( |
| 193 | reference: &[u8], | 193 | reference: &[u8], |
| 194 | + stride: usize, | ||
| 194 | ref_width: usize, | 195 | ref_width: usize, |
| 195 | ref_height: usize, | 196 | ref_height: usize, |
| 196 | source: &[u8], | 197 | source: &[u8], |
| @@ -202,8 +203,8 @@ pub fn search( | |||
| 202 | lambda: f64, | 203 | lambda: f64, |
| 203 | ) -> MotionSearch { | 204 | ) -> MotionSearch { |
| 204 | let (result, _trace) = search_traced( | 205 | let (result, _trace) = search_traced( |
| 205 | - reference, ref_width, ref_height, source, block_x, block_y, block_w, block_h, pred_mv, | 206 | + reference, stride, ref_width, ref_height, source, block_x, block_y, block_w, block_h, |
| 206 | - lambda, | 207 | + pred_mv, lambda, |
| 207 | ); | 208 | ); |
| 208 | result | 209 | result |
| 209 | } | 210 | } |
| @@ -214,6 +215,7 @@ pub fn search( | |||
| 214 | #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor | 215 | #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor |
| 215 | fn search_traced( | 216 | fn search_traced( |
| 216 | reference: &[u8], | 217 | reference: &[u8], |
| 218 | + stride: usize, | ||
| 217 | ref_width: usize, | 219 | ref_width: usize, |
| 218 | ref_height: usize, | 220 | ref_height: usize, |
| 219 | source: &[u8], | 221 | source: &[u8], |
| @@ -226,6 +228,7 @@ fn search_traced( | |||
| 226 | ) -> (MotionSearch, Vec<f64>) { | 228 | ) -> (MotionSearch, Vec<f64>) { |
| 227 | search_traced_from_step( | 229 | search_traced_from_step( |
| 228 | reference, | 230 | reference, |
| 231 | + stride, | ||
| 229 | ref_width, | 232 | ref_width, |
| 230 | ref_height, | 233 | ref_height, |
| 231 | source, | 234 | source, |
| @@ -245,6 +248,7 @@ fn search_traced( | |||
| 245 | #[allow(clippy::too_many_arguments)] | 248 | #[allow(clippy::too_many_arguments)] |
| 246 | fn search_traced_from_step( | 249 | fn search_traced_from_step( |
| 247 | reference: &[u8], | 250 | reference: &[u8], |
| 251 | + stride: usize, | ||
| 248 | ref_width: usize, | 252 | ref_width: usize, |
| 249 | ref_height: usize, | 253 | ref_height: usize, |
| 250 | source: &[u8], | 254 | source: &[u8], |
| @@ -264,7 +268,7 @@ fn search_traced_from_step( | |||
| 264 | let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3; | 268 | let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3; |
| 265 | let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3; | 269 | let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3; |
| 266 | predict( | 270 | predict( |
| 267 | - reference, ref_width, ref_height, x_q4, y_q4, block_w, block_h, &mut dst, | 271 | + reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h, &mut dst, |
| 268 | ); | 272 | ); |
| 269 | let sad: f64 = source | 273 | let sad: f64 = source |
| 270 | .iter() | 274 | .iter() |
| @@ -424,6 +428,7 @@ mod tests { | |||
| 424 | let result = search( | 428 | let result = search( |
| 425 | &plane, | 429 | &plane, |
| 426 | width, | 430 | width, |
| 431 | + width, | ||
| 427 | height, | 432 | height, |
| 428 | &block, | 433 | &block, |
| 429 | anchor_x, | 434 | anchor_x, |
| @@ -470,6 +475,7 @@ mod tests { | |||
| 470 | let result = search( | 475 | let result = search( |
| 471 | &plane, | 476 | &plane, |
| 472 | width, | 477 | width, |
| 478 | + width, | ||
| 473 | height, | 479 | height, |
| 474 | &block, | 480 | &block, |
| 475 | anchor_x, | 481 | anchor_x, |
| @@ -505,6 +511,7 @@ mod tests { | |||
| 505 | let (_result, trace) = search_traced( | 511 | let (_result, trace) = search_traced( |
| 506 | &plane, | 512 | &plane, |
| 507 | width, | 513 | width, |
| 514 | + width, | ||
| 508 | height, | 515 | height, |
| 509 | &block, | 516 | &block, |
| 510 | anchor_x, | 517 | anchor_x, |
| @@ -571,6 +578,7 @@ mod tests { | |||
| 571 | let (result, trace) = search_traced_from_step( | 578 | let (result, trace) = search_traced_from_step( |
| 572 | &plane, | 579 | &plane, |
| 573 | width, | 580 | width, |
| 581 | + width, | ||
| 574 | height, | 582 | height, |
| 575 | &block, | 583 | &block, |
| 576 | anchor_x, | 584 | anchor_x, |
crates/ec-av1/src/tile.rsmodified+461 -54
| @@ -18,6 +18,30 @@ use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; | |||
| 18 | use crate::msac::SymbolEncoder; | 18 | use crate::msac::SymbolEncoder; |
| 19 | use crate::mvstack::{MiGrid, MiInfo, find_mv_stack, single_ref_ctx}; | 19 | use crate::mvstack::{MiGrid, MiInfo, find_mv_stack, single_ref_ctx}; |
| 20 | 20 | ||
| 21 | +/// round-4 av1-truesize debugging aid: prints `msg()` to stderr when the | ||
| 22 | +/// `EC_RNG` environment variable is set, mirroring the `EC_PART`/`EC_TOK` | ||
| 23 | +/// trace `/tmp/libaom-src`'s debug `aomdec` build already emits under the | ||
| 24 | +/// same variable, so the two traces line up symbol for symbol. Checked once | ||
| 25 | +/// per process, so unset (the default) costs one atomic load per call and no | ||
| 26 | +/// allocation. Only the real tile writer below calls this -- the mode/rate | ||
| 27 | +/// search's own trial encoders never do -- so the trace does not need the | ||
| 28 | +/// throwaway-encoder gating a whole-encoder trace would. | ||
| 29 | +fn ec_rng_trace(msg: impl FnOnce() -> String) { | ||
| 30 | + use std::sync::atomic::{AtomicU8, Ordering}; | ||
| 31 | + static ON: AtomicU8 = AtomicU8::new(2); // 2 = unknown, 1 = on, 0 = off | ||
| 32 | + let on = match ON.load(Ordering::Relaxed) { | ||
| 33 | + 2 => { | ||
| 34 | + let on = u8::from(std::env::var_os("EC_RNG").is_some()); | ||
| 35 | + ON.store(on, Ordering::Relaxed); | ||
| 36 | + on | ||
| 37 | + } | ||
| 38 | + v => v, | ||
| 39 | + }; | ||
| 40 | + if on == 1 { | ||
| 41 | + eprintln!("{}", msg()); | ||
| 42 | + } | ||
| 43 | +} | ||
| 44 | + | ||
| 21 | /// `PARTITION_NONE` (spec 6.10.4): the whole block, undivided. | 45 | /// `PARTITION_NONE` (spec 6.10.4): the whole block, undivided. |
| 22 | const PARTITION_NONE: usize = 0; | 46 | const PARTITION_NONE: usize = 0; |
| 23 | /// `PARTITION_SPLIT` (spec 6.10.4): the block cut into four quadrants. | 47 | /// `PARTITION_SPLIT` (spec 6.10.4): the block cut into four quadrants. |
| @@ -34,7 +58,32 @@ const VERT_ALIKE: [usize; 6] = [2, PARTITION_SPLIT, 4, 6, 7, 9]; | |||
| 34 | const HORZ_ALIKE: [usize; 6] = [1, PARTITION_SPLIT, 4, 5, 6, 8]; | 58 | const HORZ_ALIKE: [usize; 6] = [1, PARTITION_SPLIT, 4, 5, 6, 8]; |
| 35 | 59 | ||
| 36 | /// Mode-info units across a 32x32 block. | 60 | /// Mode-info units across a 32x32 block. |
| 37 | -const BLOCK_MI: u32 = SB_MI / 2; | 61 | +pub(crate) const BLOCK_MI: u32 = SB_MI / 2; |
| 62 | + | ||
| 63 | +/// Mode-info units across a 16x16 block, the smallest this crate's key-frame | ||
| 64 | +/// writer codes. | ||
| 65 | +pub(crate) const SUB_MI: u32 = BLOCK_MI / 2; | ||
| 66 | + | ||
| 67 | +/// spec `decode_partition`'s `hasRows`/`hasCols` (5.11.4), recomputed at | ||
| 68 | +/// whichever block size is asking: a block of `side_mi` mode-info units at mi | ||
| 69 | +/// position `pos` may be coded unsplit (its own `PARTITION_NONE`) only when | ||
| 70 | +/// this is true in both directions -- the frame's *true* size can put the | ||
| 71 | +/// boundary inside a superblock, a 32x32 quadrant or (since this writer's | ||
| 72 | +/// smallest block is 16x16) even a leaf, and each level must ask again with | ||
| 73 | +/// its own half, not just once at the superblock. | ||
| 74 | +pub(crate) fn has_half(pos: u32, side_mi: u32, bound: u32) -> bool { | ||
| 75 | + pos + side_mi / 2 < bound | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +/// The number of 32x32 block columns/rows a frame whose *true* (unpadded) | ||
| 79 | +/// size gives `mi_cols`/`mi_rows` is coded over: every block whose mi origin | ||
| 80 | +/// (`col * BLOCK_MI`) is inside the true bound is coded, so this is a | ||
| 81 | +/// ceiling, not the exact division a block-aligned frame would give. Shared | ||
| 82 | +/// by the tile writer's own iteration and by [`crate::encode`]'s block/ | ||
| 83 | +/// superblock generation, which must agree with it exactly. | ||
| 84 | +pub(crate) fn block_grid(mi_cols: u32, mi_rows: u32) -> (u32, u32) { | ||
| 85 | + (mi_cols.div_ceil(BLOCK_MI), mi_rows.div_ceil(BLOCK_MI)) | ||
| 86 | +} | ||
| 38 | /// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode. | 87 | /// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode. |
| 39 | const DC_PRED: usize = 0; | 88 | const DC_PRED: usize = 0; |
| 40 | 89 | ||
| @@ -468,6 +517,10 @@ const SB: usize = 64; | |||
| 468 | /// Side of the smallest block the writer codes, in samples, which is the grid | 517 | /// Side of the smallest block the writer codes, in samples, which is the grid |
| 469 | /// the neighbour bookkeeping is kept on. | 518 | /// the neighbour bookkeeping is kept on. |
| 470 | const SUB: usize = 16; | 519 | const SUB: usize = 16; |
| 520 | +/// Side of a 4x4 mode-info unit, in samples: the granularity libaom's above | ||
| 521 | +/// and left entropy-context arrays are actually kept on (spec | ||
| 522 | +/// `get_txb_ctx`/`av1_set_entropy_contexts`), finer than [`SUB`]. | ||
| 523 | +const MI: usize = 4; | ||
| 471 | 524 | ||
| 472 | /// What one coded block leaves behind for the blocks that read it as a | 525 | /// What one coded block leaves behind for the blocks that read it as a |
| 473 | /// neighbour: whether it coded anything at all, and the sign of its DC. | 526 | /// neighbour: whether it coded anything at all, and the sign of its DC. |
| @@ -479,22 +532,18 @@ struct Neighbour { | |||
| 479 | dc: Option<bool>, | 532 | dc: Option<bool>, |
| 480 | } | 533 | } |
| 481 | 534 | ||
| 482 | -/// Rejects a frame the 32x32 block grid cannot tile. | 535 | +/// Rejects a frame with no mode-info grid at all. |
| 483 | /// | 536 | /// |
| 484 | -/// The frame need not be a whole number of superblocks — a superblock at the | 537 | +/// `mi_cols`/`mi_rows` are the frame's *true* (unpadded) size in 4x4 units |
| 485 | -/// right-hand or bottom edge may be half outside it — but every 32x32 block | 538 | +/// (spec `compute_image_size`), not necessarily a multiple of the 32x32 block |
| 486 | -/// that is coded has to be wholly inside, because a block that hangs over the | 539 | +/// grid: a block whose origin sits at or past this bound is not coded (spec's |
| 487 | -/// edge has to be coded as a rectangle this writer has no transform for. | 540 | +/// `decode_partition` never visits it), and one that straddles the bound is |
| 541 | +/// coded whole, its samples coming from the padded planes. | ||
| 488 | fn check_blocks(mi_cols: u32, mi_rows: u32) -> Result<()> { | 542 | fn check_blocks(mi_cols: u32, mi_rows: u32) -> Result<()> { |
| 489 | - if mi_cols == 0 | 543 | + if mi_cols == 0 || mi_rows == 0 { |
| 490 | - || mi_rows == 0 | ||
| 491 | - || !mi_cols.is_multiple_of(BLOCK_MI) | ||
| 492 | - || !mi_rows.is_multiple_of(BLOCK_MI) | ||
| 493 | - { | ||
| 494 | return Err(Error::unsupported( | 544 | return Err(Error::unsupported( |
| 495 | "AV1 tile", | 545 | "AV1 tile", |
| 496 | - "a coefficient key frame is written only for frames that are a \ | 546 | + "a coefficient key frame needs a nonzero mode-info grid", |
| 497 | - whole number of 32x32 blocks", | ||
| 498 | )); | 547 | )); |
| 499 | } | 548 | } |
| 500 | Ok(()) | 549 | Ok(()) |
| @@ -567,6 +616,13 @@ struct Neighbours { | |||
| 567 | above_inter: Vec<bool>, | 616 | above_inter: Vec<bool>, |
| 568 | /// The same, down the left edge. | 617 | /// The same, down the left edge. |
| 569 | left_inter: Vec<bool>, | 618 | left_inter: Vec<bool>, |
| 619 | + /// The frame's true (unpadded) width and height, in 4x4 mode-info units, | ||
| 620 | + /// which is what clamps [`Self::above`]/[`Self::left`] at the edge (spec | ||
| 621 | + /// `av1_set_entropy_contexts`): a block whose row or column run spills | ||
| 622 | + /// past this bound leaves its trailing 4x4 units at their default, | ||
| 623 | + /// mid-cell if need be. | ||
| 624 | + mi_cols: usize, | ||
| 625 | + mi_rows: usize, | ||
| 570 | } | 626 | } |
| 571 | 627 | ||
| 572 | /// What one plane's neighbours leave for a block, gathered across every cell | 628 | /// What one plane's neighbours leave for a block, gathered across every cell |
| @@ -586,11 +642,13 @@ struct Around { | |||
| 586 | 642 | ||
| 587 | impl Neighbours { | 643 | impl Neighbours { |
| 588 | /// The state a tile starts from: a block outside it reads as `DC_PRED`, | 644 | /// The state a tile starts from: a block outside it reads as `DC_PRED`, |
| 589 | - /// as having coded nothing, and as unsplit. | 645 | + /// as having coded nothing, and as unsplit. `cols`/`rows` are in [`SUB`] |
| 590 | - fn new(cols: usize, rows: usize) -> Self { | 646 | + /// units; `mi_cols`/`mi_rows` are the frame's true (unpadded) size in 4x4 |
| 647 | + /// mode-info units. | ||
| 648 | + fn new(cols: usize, rows: usize, mi_cols: usize, mi_rows: usize) -> Self { | ||
| 591 | Self { | 649 | Self { |
| 592 | - above: vec![[Neighbour::default(); 3]; cols], | 650 | + above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)], |
| 593 | - left: vec![[Neighbour::default(); 3]; rows], | 651 | + left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], |
| 594 | above_mode: vec![DC_PRED; cols], | 652 | above_mode: vec![DC_PRED; cols], |
| 595 | left_mode: vec![DC_PRED; rows], | 653 | left_mode: vec![DC_PRED; rows], |
| 596 | above_side: vec![SB; cols], | 654 | above_side: vec![SB; cols], |
| @@ -599,6 +657,8 @@ impl Neighbours { | |||
| 599 | left_skip: vec![false; rows], | 657 | left_skip: vec![false; rows], |
| 600 | above_inter: vec![false; cols], | 658 | above_inter: vec![false; cols], |
| 601 | left_inter: vec![false; rows], | 659 | left_inter: vec![false; rows], |
| 660 | + mi_cols, | ||
| 661 | + mi_rows, | ||
| 602 | } | 662 | } |
| 603 | } | 663 | } |
| 604 | 664 | ||
| @@ -611,18 +671,56 @@ impl Neighbours { | |||
| 611 | self.left_inter.iter_mut().for_each(|i| *i = false); | 671 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 612 | } | 672 | } |
| 613 | 673 | ||
| 614 | - /// Writes one coded block into every 16x16 column and row it covers. | 674 | + /// Writes one coded block into every 16x16 column and row it covers, and, |
| 675 | + /// on the finer 4x4 grid libaom's entropy context arrays are actually | ||
| 676 | + /// kept on, into every unit up to the true frame edge -- the units past | ||
| 677 | + /// it are left at their default (uncoded), even mid-16x16-cell (spec | ||
| 678 | + /// `av1_set_entropy_contexts`, which clamps to `blocks_wide`/`blocks_high` | ||
| 679 | + /// derived from the true `mi_cols`/`mi_rows`, not from this block's own | ||
| 680 | + /// side). | ||
| 615 | fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) { | 681 | fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) { |
| 616 | let (r, c) = at; | 682 | let (r, c) = at; |
| 617 | let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); | 683 | let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); |
| 618 | for cell in 0..side / SUB { | 684 | for cell in 0..side / SUB { |
| 619 | - self.above[c + cell] = states; | ||
| 620 | - self.left[r + cell] = states; | ||
| 621 | self.above_mode[c + cell] = mode; | 685 | self.above_mode[c + cell] = mode; |
| 622 | self.left_mode[r + cell] = mode; | 686 | self.left_mode[r + cell] = mode; |
| 623 | self.above_side[c + cell] = side; | 687 | self.above_side[c + cell] = side; |
| 624 | self.left_side[r + cell] = side; | 688 | self.left_side[r + cell] = side; |
| 625 | } | 689 | } |
| 690 | + let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI); | ||
| 691 | + // libaom rounds the luma edge up to the plane's own 4x4 unit before | ||
| 692 | + // clamping a subsampled plane (`ROUND_POWER_OF_TWO(max_blocks_high, | ||
| 693 | + // subsampling_y)` in av1_write_intra_coeffs_mb, encodetxb.c:456-459): | ||
| 694 | + // a chroma 4x4 unit straddling the true luma edge is still whole in | ||
| 695 | + // chroma's own halved grid, so it stays valid one luma-mi row/col | ||
| 696 | + // past where luma's own edge falls when that edge is odd. | ||
| 697 | + let round_up_even = |n: usize| n.div_ceil(2) * 2; | ||
| 698 | + let bound_h = [ | ||
| 699 | + self.mi_rows, | ||
| 700 | + round_up_even(self.mi_rows), | ||
| 701 | + round_up_even(self.mi_rows), | ||
| 702 | + ]; | ||
| 703 | + let bound_w = [ | ||
| 704 | + self.mi_cols, | ||
| 705 | + round_up_even(self.mi_cols), | ||
| 706 | + round_up_even(self.mi_cols), | ||
| 707 | + ]; | ||
| 708 | + for cell in 0..side_mi { | ||
| 709 | + self.left[mi_r + cell] = std::array::from_fn(|plane| { | ||
| 710 | + if cell < side_mi.min(bound_h[plane].saturating_sub(mi_r)) { | ||
| 711 | + states[plane] | ||
| 712 | + } else { | ||
| 713 | + Default::default() | ||
| 714 | + } | ||
| 715 | + }); | ||
| 716 | + self.above[mi_c + cell] = std::array::from_fn(|plane| { | ||
| 717 | + if cell < side_mi.min(bound_w[plane].saturating_sub(mi_c)) { | ||
| 718 | + states[plane] | ||
| 719 | + } else { | ||
| 720 | + Default::default() | ||
| 721 | + } | ||
| 722 | + }); | ||
| 723 | + } | ||
| 626 | } | 724 | } |
| 627 | 725 | ||
| 628 | /// Writes one inter-frame block's skip flag and inter/intra state into | 726 | /// Writes one inter-frame block's skip flag and inter/intra state into |
| @@ -643,10 +741,14 @@ impl Neighbours { | |||
| 643 | /// The gathered state of the blocks above and to the left of one block, | 741 | /// The gathered state of the blocks above and to the left of one block, |
| 644 | /// per plane. | 742 | /// per plane. |
| 645 | fn around(&self, (r, c): (usize, usize), side: usize) -> [Around; 3] { | 743 | fn around(&self, (r, c): (usize, usize), side: usize) -> [Around; 3] { |
| 744 | + let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI); | ||
| 646 | std::array::from_fn(|plane| { | 745 | std::array::from_fn(|plane| { |
| 647 | let mut around = Around::default(); | 746 | let mut around = Around::default(); |
| 648 | - for cell in 0..side / SUB { | 747 | + for cell in 0..side_mi { |
| 649 | - let (above, left) = (&self.above[c + cell][plane], &self.left[r + cell][plane]); | 748 | + let (above, left) = ( |
| 749 | + &self.above[mi_c + cell][plane], | ||
| 750 | + &self.left[mi_r + cell][plane], | ||
| 751 | + ); | ||
| 650 | around.above_coded |= above.coded; | 752 | around.above_coded |= above.coded; |
| 651 | around.left_coded |= left.coded; | 753 | around.left_coded |= left.coded; |
| 652 | around.dc_vote += dc_vote(above.dc) + dc_vote(left.dc); | 754 | around.dc_vote += dc_vote(above.dc) + dc_vote(left.dc); |
| @@ -685,7 +787,7 @@ pub fn sb_coeff_key_frame_tile( | |||
| 685 | superblocks: &[Superblock], | 787 | superblocks: &[Superblock], |
| 686 | ) -> Result<Vec<u8>> { | 788 | ) -> Result<Vec<u8>> { |
| 687 | check_blocks(mi_cols, mi_rows)?; | 789 | check_blocks(mi_cols, mi_rows)?; |
| 688 | - let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); | 790 | + let (cols, rows) = block_grid(mi_cols, mi_rows); |
| 689 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); | 791 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 690 | if superblocks.len() != (sb_cols * sb_rows) as usize { | 792 | if superblocks.len() != (sb_cols * sb_rows) as usize { |
| 691 | return Err(Error::unsupported( | 793 | return Err(Error::unsupported( |
| @@ -722,7 +824,12 @@ pub fn sb_coeff_key_frame_tile( | |||
| 722 | // The blocks above and to the left, on the 16x16 grid. The left edge is | 824 | // The blocks above and to the left, on the 16x16 grid. The left edge is |
| 723 | // reset at every superblock row because a tile starts each row with no | 825 | // reset at every superblock row because a tile starts each row with no |
| 724 | // left neighbour. | 826 | // left neighbour. |
| 725 | - let mut neighbours = Neighbours::new(cols as usize * 2, rows as usize * 2); | 827 | + let mut neighbours = Neighbours::new( |
| 828 | + cols as usize * 2, | ||
| 829 | + rows as usize * 2, | ||
| 830 | + mi_cols as usize, | ||
| 831 | + mi_rows as usize, | ||
| 832 | + ); | ||
| 726 | 833 | ||
| 727 | // The tile adapts every non-literal CDF it writes, exactly as the decoder | 834 | // The tile adapts every non-literal CDF it writes, exactly as the decoder |
| 728 | // adapts the ones it reads, so the frame header leaves `disable_cdf_update` | 835 | // adapts the ones it reads, so the frame header leaves `disable_cdf_update` |
| @@ -734,6 +841,15 @@ pub fn sb_coeff_key_frame_tile( | |||
| 734 | for sb_c in 0..sb_cols { | 841 | for sb_c in 0..sb_cols { |
| 735 | let at = (sb_r as usize * 4, sb_c as usize * 4); | 842 | let at = (sb_r as usize * 4, sb_c as usize * 4); |
| 736 | let ctx = neighbours.partition_ctx(at, SB); | 843 | let ctx = neighbours.partition_ctx(at, SB); |
| 844 | + ec_rng_trace(|| { | ||
| 845 | + format!( | ||
| 846 | + "EC_PART mi_row={} mi_col={} bsize=12 ctx={} tell={}", | ||
| 847 | + at.0 * 4, | ||
| 848 | + at.1 * 4, | ||
| 849 | + ctx, | ||
| 850 | + enc.tell() | ||
| 851 | + ) | ||
| 852 | + }); | ||
| 737 | // A superblock whose bottom or right half is outside the frame | 853 | // A superblock whose bottom or right half is outside the frame |
| 738 | // cannot be left unsplit, so the decoder reads a flag instead of | 854 | // cannot be left unsplit, so the decoder reads a flag instead of |
| 739 | // the partition symbol — and reads nothing at all when both halves | 855 | // the partition symbol — and reads nothing at all when both halves |
| @@ -778,6 +894,14 @@ pub fn sb_coeff_key_frame_tile( | |||
| 778 | // luma, so its chroma mode reads the table without it. | 894 | // luma, so its chroma mode reads the table without it. |
| 779 | false, | 895 | false, |
| 780 | ); | 896 | ); |
| 897 | + ec_rng_trace(|| { | ||
| 898 | + format!( | ||
| 899 | + "EC_TOK mi_row={} mi_col={} tell={}", | ||
| 900 | + at.0 * 4, | ||
| 901 | + at.1 * 4, | ||
| 902 | + enc.tell() | ||
| 903 | + ) | ||
| 904 | + }); | ||
| 781 | } | 905 | } |
| 782 | Superblock::Split(quadrants) => { | 906 | Superblock::Split(quadrants) => { |
| 783 | match (has_cols, has_rows) { | 907 | match (has_cols, has_rows) { |
| @@ -802,8 +926,34 @@ pub fn sb_coeff_key_frame_tile( | |||
| 802 | for (quadrant, (r, c)) in quadrants.iter().zip(quadrant_positions) { | 926 | for (quadrant, (r, c)) in quadrants.iter().zip(quadrant_positions) { |
| 803 | let at = (r * 2, c * 2); | 927 | let at = (r * 2, c * 2); |
| 804 | let ctx = neighbours.partition_ctx(at, BLOCK); | 928 | let ctx = neighbours.partition_ctx(at, BLOCK); |
| 929 | + // Recomputed at this 32x32 block's own half (spec | ||
| 930 | + // `decode_partition`, called again at every size, not | ||
| 931 | + // just once for the superblock): the true frame edge | ||
| 932 | + // can fall inside this quadrant even when the | ||
| 933 | + // superblock it sits in was itself whole or safely | ||
| 934 | + // split above. | ||
| 935 | + let (has_cols32, has_rows32) = ( | ||
| 936 | + has_half(c as u32 * BLOCK_MI, BLOCK_MI, mi_cols), | ||
| 937 | + has_half(r as u32 * BLOCK_MI, BLOCK_MI, mi_rows), | ||
| 938 | + ); | ||
| 939 | + ec_rng_trace(|| { | ||
| 940 | + format!( | ||
| 941 | + "EC_PART mi_row={} mi_col={} bsize=9 ctx={} tell={}", | ||
| 942 | + at.0 * 4, | ||
| 943 | + at.1 * 4, | ||
| 944 | + ctx, | ||
| 945 | + enc.tell() | ||
| 946 | + ) | ||
| 947 | + }); | ||
| 805 | match quadrant { | 948 | match quadrant { |
| 806 | Quadrant::Whole(block) => { | 949 | Quadrant::Whole(block) => { |
| 950 | + if !has_cols32 || !has_rows32 { | ||
| 951 | + return Err(Error::unsupported( | ||
| 952 | + "AV1 tile", | ||
| 953 | + "a 32x32 block that is half outside the true frame \ | ||
| 954 | + cannot be left whole", | ||
| 955 | + )); | ||
| 956 | + } | ||
| 807 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[ctx]); | 957 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[ctx]); |
| 808 | let grids = [ | 958 | let grids = [ |
| 809 | level_grid(&block.luma, TX32)?, | 959 | level_grid(&block.luma, TX32)?, |
| @@ -822,18 +972,92 @@ pub fn sb_coeff_key_frame_tile( | |||
| 822 | [&scans[0], &scans[1], &scans[1]], | 972 | [&scans[0], &scans[1], &scans[1]], |
| 823 | true, | 973 | true, |
| 824 | ); | 974 | ); |
| 975 | + ec_rng_trace(|| { | ||
| 976 | + format!( | ||
| 977 | + "EC_TOK mi_row={} mi_col={} tell={}", | ||
| 978 | + at.0 * 4, | ||
| 979 | + at.1 * 4, | ||
| 980 | + enc.tell() | ||
| 981 | + ) | ||
| 982 | + }); | ||
| 825 | } | 983 | } |
| 826 | Quadrant::Split(blocks) => { | 984 | Quadrant::Split(blocks) => { |
| 827 | - enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w32[ctx]); | 985 | + // The 16x16 sub-blocks this quadrant's split |
| 828 | - if blocks.len() != 4 { | 986 | + // carries: only those whose own mi origin is |
| 987 | + // inside the true frame (spec `decode_partition`'s | ||
| 988 | + // `r >= MiRows || c >= MiCols` early return), | ||
| 989 | + // which need not be all four when the true | ||
| 990 | + // edge falls inside this quadrant. | ||
| 991 | + let sub_positions: Vec<(usize, usize)> = (0..4) | ||
| 992 | + .map(|i| (r * 2 + i / 2, c * 2 + i % 2)) | ||
| 993 | + .filter(|&(sr, sc)| { | ||
| 994 | + (sr as u32) * SUB_MI < mi_rows | ||
| 995 | + && (sc as u32) * SUB_MI < mi_cols | ||
| 996 | + }) | ||
| 997 | + .collect(); | ||
| 998 | + if blocks.len() != sub_positions.len() { | ||
| 829 | return Err(Error::unsupported( | 999 | return Err(Error::unsupported( |
| 830 | "AV1 tile", | 1000 | "AV1 tile", |
| 831 | - "a 32x32 block that is split carries four 16x16 blocks", | 1001 | + "a split 32x32 block needs one 16x16 entry per \ |
| 1002 | + sub-block inside the true frame", | ||
| 832 | )); | 1003 | )); |
| 833 | } | 1004 | } |
| 834 | - for (i, block) in blocks.iter().enumerate() { | 1005 | + // Same three-way spec signaling as the |
| 835 | - let at = (at.0 + i / 2, at.1 + i % 2); | 1006 | + // superblock level above, recomputed at this |
| 1007 | + // block's own half: a full alphabet symbol | ||
| 1008 | + // only when both halves are inside, a single | ||
| 1009 | + // gathered bit when just one is, and nothing | ||
| 1010 | + // at all (SPLIT is inferred) when neither is. | ||
| 1011 | + match (has_cols32, has_rows32) { | ||
| 1012 | + (true, true) => { | ||
| 1013 | + enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w32[ctx]); | ||
| 1014 | + } | ||
| 1015 | + (true, false) => { | ||
| 1016 | + enc.symbol_fixed( | ||
| 1017 | + 1, | ||
| 1018 | + &gather(&cdfs.partition_w32[ctx], VERT_ALIKE), | ||
| 1019 | + ); | ||
| 1020 | + } | ||
| 1021 | + (false, true) => { | ||
| 1022 | + enc.symbol_fixed( | ||
| 1023 | + 1, | ||
| 1024 | + &gather(&cdfs.partition_w32[ctx], HORZ_ALIKE), | ||
| 1025 | + ); | ||
| 1026 | + } | ||
| 1027 | + (false, false) => {} | ||
| 1028 | + } | ||
| 1029 | + for (block, (sr, sc)) in blocks.iter().zip(sub_positions) { | ||
| 1030 | + // A 16x16 leaf's own hasRows/hasCols: this | ||
| 1031 | + // writer has no smaller partition than | ||
| 1032 | + // 16x16 (no 8x8 or rectangular transform), | ||
| 1033 | + // so a leaf the true edge itself cuts | ||
| 1034 | + // through -- legal in the spec only via a | ||
| 1035 | + // `PARTITION_HORZ`/`VERT` this writer does | ||
| 1036 | + // not code -- is refused rather than | ||
| 1037 | + // silently miscoded. | ||
| 1038 | + let (has_cols16, has_rows16) = ( | ||
| 1039 | + has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), | ||
| 1040 | + has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), | ||
| 1041 | + ); | ||
| 1042 | + if !has_cols16 || !has_rows16 { | ||
| 1043 | + return Err(Error::unsupported( | ||
| 1044 | + "AV1 tile", | ||
| 1045 | + "a 16x16 block the true frame edge cuts through \ | ||
| 1046 | + needs a rectangular transform this writer does \ | ||
| 1047 | + not code yet", | ||
| 1048 | + )); | ||
| 1049 | + } | ||
| 1050 | + let at = (sr, sc); | ||
| 836 | let ctx = neighbours.partition_ctx(at, SUB); | 1051 | let ctx = neighbours.partition_ctx(at, SUB); |
| 1052 | + ec_rng_trace(|| { | ||
| 1053 | + format!( | ||
| 1054 | + "EC_PART mi_row={} mi_col={} bsize=6 ctx={} tell={}", | ||
| 1055 | + at.0 * 4, | ||
| 1056 | + at.1 * 4, | ||
| 1057 | + ctx, | ||
| 1058 | + enc.tell() | ||
| 1059 | + ) | ||
| 1060 | + }); | ||
| 837 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx]); | 1061 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx]); |
| 838 | let grids = [ | 1062 | let grids = [ |
| 839 | level_grid(&block.luma, TX16)?, | 1063 | level_grid(&block.luma, TX16)?, |
| @@ -852,6 +1076,14 @@ pub fn sb_coeff_key_frame_tile( | |||
| 852 | [&scans[1], &scans[2], &scans[2]], | 1076 | [&scans[1], &scans[2], &scans[2]], |
| 853 | true, | 1077 | true, |
| 854 | ); | 1078 | ); |
| 1079 | + ec_rng_trace(|| { | ||
| 1080 | + format!( | ||
| 1081 | + "EC_TOK mi_row={} mi_col={} tell={}", | ||
| 1082 | + at.0 * 4, | ||
| 1083 | + at.1 * 4, | ||
| 1084 | + enc.tell() | ||
| 1085 | + ) | ||
| 1086 | + }); | ||
| 855 | } | 1087 | } |
| 856 | } | 1088 | } |
| 857 | } | 1089 | } |
| @@ -960,7 +1192,15 @@ fn write_block_planes( | |||
| 960 | scan, | 1192 | scan, |
| 961 | skip_ctx, | 1193 | skip_ctx, |
| 962 | dc_sign_ctx(around[plane].dc_vote), | 1194 | dc_sign_ctx(around[plane].dc_vote), |
| 1195 | + Some(plane), | ||
| 963 | ); | 1196 | ); |
| 1197 | + ec_rng_trace(|| { | ||
| 1198 | + format!( | ||
| 1199 | + "EC_PLANE plane={plane} nz={} skip_ctx={skip_ctx} tell={}", | ||
| 1200 | + grid.iter().filter(|&&l| l != 0).count(), | ||
| 1201 | + enc.tell() | ||
| 1202 | + ) | ||
| 1203 | + }); | ||
| 964 | } | 1204 | } |
| 965 | } | 1205 | } |
| 966 | 1206 | ||
| @@ -988,7 +1228,7 @@ pub fn split_coeff_key_frame_tile( | |||
| 988 | blocks: &[BlockCoeffs], | 1228 | blocks: &[BlockCoeffs], |
| 989 | ) -> Result<Vec<u8>> { | 1229 | ) -> Result<Vec<u8>> { |
| 990 | check_blocks(mi_cols, mi_rows)?; | 1230 | check_blocks(mi_cols, mi_rows)?; |
| 991 | - let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); | 1231 | + let (cols, rows) = block_grid(mi_cols, mi_rows); |
| 992 | if blocks.len() != (cols * rows) as usize { | 1232 | if blocks.len() != (cols * rows) as usize { |
| 993 | return Err(Error::unsupported( | 1233 | return Err(Error::unsupported( |
| 994 | "AV1 tile", | 1234 | "AV1 tile", |
| @@ -1102,7 +1342,7 @@ pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet) -> f64 { | |||
| 1102 | }; | 1342 | }; |
| 1103 | let mut enc = SymbolEncoder::new(); | 1343 | let mut enc = SymbolEncoder::new(); |
| 1104 | enc.reset_bits(); | 1344 | enc.reset_bits(); |
| 1105 | - write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0); | 1345 | + write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None); |
| 1106 | enc.bits() | 1346 | enc.bits() |
| 1107 | } | 1347 | } |
| 1108 | 1348 | ||
| @@ -1135,6 +1375,7 @@ pub(crate) fn partition_bits(side: usize, split: bool) -> f64 { | |||
| 1135 | /// levels the contexts below are read from are the levels of | 1375 | /// levels the contexts below are read from are the levels of |
| 1136 | /// coefficients later in the scan, which a decoder walking the scan backwards | 1376 | /// coefficients later in the scan, which a decoder walking the scan backwards |
| 1137 | /// already has. | 1377 | /// already has. |
| 1378 | +#[allow(clippy::too_many_arguments)] | ||
| 1138 | fn write_coeffs( | 1379 | fn write_coeffs( |
| 1139 | enc: &mut SymbolEncoder, | 1380 | enc: &mut SymbolEncoder, |
| 1140 | coding: &mut TxbTables, | 1381 | coding: &mut TxbTables, |
| @@ -1142,13 +1383,26 @@ fn write_coeffs( | |||
| 1142 | scan: &[u16], | 1383 | scan: &[u16], |
| 1143 | skip_ctx: usize, | 1384 | skip_ctx: usize, |
| 1144 | sign_ctx: usize, | 1385 | sign_ctx: usize, |
| 1386 | + plane: Option<usize>, | ||
| 1145 | ) { | 1387 | ) { |
| 1388 | + if let Some(plane) = plane { | ||
| 1389 | + ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell())); | ||
| 1390 | + } | ||
| 1146 | let side = coding.side; | 1391 | let side = coding.side; |
| 1147 | let eob = scan | 1392 | let eob = scan |
| 1148 | .iter() | 1393 | .iter() |
| 1149 | .rposition(|&pos| grid[pos as usize] != 0) | 1394 | .rposition(|&pos| grid[pos as usize] != 0) |
| 1150 | .map_or(0, |i| i + 1); | 1395 | .map_or(0, |i| i + 1); |
| 1151 | enc.symbol(usize::from(eob == 0), &mut coding.txb_skip[skip_ctx]); | 1396 | enc.symbol(usize::from(eob == 0), &mut coding.txb_skip[skip_ctx]); |
| 1397 | + if plane.is_some() { | ||
| 1398 | + ec_rng_trace(|| { | ||
| 1399 | + format!( | ||
| 1400 | + "EC_TXBSKIP ctx={skip_ctx} eob0={} tell={}", | ||
| 1401 | + usize::from(eob == 0), | ||
| 1402 | + enc.tell() | ||
| 1403 | + ) | ||
| 1404 | + }); | ||
| 1405 | + } | ||
| 1152 | if eob == 0 { | 1406 | if eob == 0 { |
| 1153 | return; | 1407 | return; |
| 1154 | } | 1408 | } |
| @@ -1157,9 +1411,12 @@ fn write_coeffs( | |||
| 1157 | // uses DCT_DCT, which is index one of `Tx_Type_Intra_Inv_Set2`. | 1411 | // uses DCT_DCT, which is index one of `Tx_Type_Intra_Inv_Set2`. |
| 1158 | if let Some(tx_type) = coding.tx_type.as_deref_mut() { | 1412 | if let Some(tx_type) = coding.tx_type.as_deref_mut() { |
| 1159 | enc.symbol(TX_TYPE_DCT_DCT_SET2, tx_type); | 1413 | enc.symbol(TX_TYPE_DCT_DCT_SET2, tx_type); |
| 1414 | + if plane.is_some() { | ||
| 1415 | + ec_rng_trace(|| format!("EC_TXTYPE tell={}", enc.tell())); | ||
| 1416 | + } | ||
| 1160 | } | 1417 | } |
| 1161 | 1418 | ||
| 1162 | - write_eob(enc, coding, eob); | 1419 | + write_eob(enc, coding, eob, plane); |
| 1163 | 1420 | ||
| 1164 | for scan_idx in (0..eob).rev() { | 1421 | for scan_idx in (0..eob).rev() { |
| 1165 | let pos = scan[scan_idx] as usize; | 1422 | let pos = scan[scan_idx] as usize; |
| @@ -1167,16 +1424,29 @@ fn write_coeffs( | |||
| 1167 | let level = grid[pos].abs(); | 1424 | let level = grid[pos].abs(); |
| 1168 | if scan_idx == eob - 1 { | 1425 | if scan_idx == eob - 1 { |
| 1169 | let ctx = eob_coeff_ctx(scan_idx, side * side); | 1426 | let ctx = eob_coeff_ctx(scan_idx, side * side); |
| 1170 | - enc.symbol( | 1427 | + let sym = (level.min(NUM_BASE_LEVELS + 1) - 1) as usize; |
| 1171 | - (level.min(NUM_BASE_LEVELS + 1) - 1) as usize, | 1428 | + enc.symbol(sym, &mut coding.base_eob[ctx]); |
| 1172 | - &mut coding.base_eob[ctx], | 1429 | + if plane.is_some() { |
| 1173 | - ); | 1430 | + ec_rng_trace(|| { |
| 1431 | + format!( | ||
| 1432 | + "EC_BASEEOB scan_idx={scan_idx} ctx={ctx} level={} tell={}", | ||
| 1433 | + sym + 1, | ||
| 1434 | + enc.tell() | ||
| 1435 | + ) | ||
| 1436 | + }); | ||
| 1437 | + } | ||
| 1174 | } else { | 1438 | } else { |
| 1175 | let ctx = base_ctx(grid, side, row, col); | 1439 | let ctx = base_ctx(grid, side, row, col); |
| 1176 | - enc.symbol( | 1440 | + let sym = level.min(NUM_BASE_LEVELS + 1) as usize; |
| 1177 | - level.min(NUM_BASE_LEVELS + 1) as usize, | 1441 | + enc.symbol(sym, &mut coding.base[ctx]); |
| 1178 | - &mut coding.base[ctx], | 1442 | + if plane.is_some() { |
| 1179 | - ); | 1443 | + ec_rng_trace(|| { |
| 1444 | + format!( | ||
| 1445 | + "EC_BASE scan_idx={scan_idx} ctx={ctx} level={sym} tell={}", | ||
| 1446 | + enc.tell() | ||
| 1447 | + ) | ||
| 1448 | + }); | ||
| 1449 | + } | ||
| 1180 | } | 1450 | } |
| 1181 | if level > NUM_BASE_LEVELS { | 1451 | if level > NUM_BASE_LEVELS { |
| 1182 | let ctx = br_ctx(grid, side, row, col); | 1452 | let ctx = br_ctx(grid, side, row, col); |
| @@ -1185,6 +1455,14 @@ fn write_coeffs( | |||
| 1185 | while sent < COEFF_BASE_RANGE { | 1455 | while sent < COEFF_BASE_RANGE { |
| 1186 | let k = remaining.min(BR_STEP); | 1456 | let k = remaining.min(BR_STEP); |
| 1187 | enc.symbol(k as usize, &mut coding.br[ctx]); | 1457 | enc.symbol(k as usize, &mut coding.br[ctx]); |
| 1458 | + if plane.is_some() { | ||
| 1459 | + ec_rng_trace(|| { | ||
| 1460 | + format!( | ||
| 1461 | + "EC_BR scan_idx={scan_idx} ctx={ctx} k={k} tell={}", | ||
| 1462 | + enc.tell() | ||
| 1463 | + ) | ||
| 1464 | + }); | ||
| 1465 | + } | ||
| 1188 | if k < BR_STEP { | 1466 | if k < BR_STEP { |
| 1189 | break; | 1467 | break; |
| 1190 | } | 1468 | } |
| @@ -1203,6 +1481,15 @@ fn write_coeffs( | |||
| 1203 | } | 1481 | } |
| 1204 | if pos == 0 { | 1482 | if pos == 0 { |
| 1205 | enc.symbol(usize::from(level < 0), &mut coding.dc_sign[sign_ctx]); | 1483 | enc.symbol(usize::from(level < 0), &mut coding.dc_sign[sign_ctx]); |
| 1484 | + if plane.is_some() { | ||
| 1485 | + ec_rng_trace(|| { | ||
| 1486 | + format!( | ||
| 1487 | + "EC_DCSIGN ctx={sign_ctx} neg={} tell={}", | ||
| 1488 | + usize::from(level < 0), | ||
| 1489 | + enc.tell() | ||
| 1490 | + ) | ||
| 1491 | + }); | ||
| 1492 | + } | ||
| 1206 | } else { | 1493 | } else { |
| 1207 | enc.literal(u32::from(level < 0), 1); | 1494 | enc.literal(u32::from(level < 0), 1); |
| 1208 | } | 1495 | } |
| @@ -1210,14 +1497,20 @@ fn write_coeffs( | |||
| 1210 | // of itself here, after its own sign (spec 5.11.39). | 1497 | // of itself here, after its own sign (spec 5.11.39). |
| 1211 | if level.abs() > MAX_BR_LEVEL { | 1498 | if level.abs() > MAX_BR_LEVEL { |
| 1212 | write_golomb(enc, (level.abs() - MAX_BR_LEVEL - 1) as u32); | 1499 | write_golomb(enc, (level.abs() - MAX_BR_LEVEL - 1) as u32); |
| 1500 | + if plane.is_some() { | ||
| 1501 | + ec_rng_trace(|| format!("EC_GOLOMB tell={}", enc.tell())); | ||
| 1502 | + } | ||
| 1213 | } | 1503 | } |
| 1214 | } | 1504 | } |
| 1505 | + if let Some(plane) = plane { | ||
| 1506 | + ec_rng_trace(|| format!("EC_PLANE plane={plane} eob={eob} tell_after={}", enc.tell())); | ||
| 1507 | + } | ||
| 1215 | } | 1508 | } |
| 1216 | 1509 | ||
| 1217 | /// The end-of-block position (spec 5.11.39): which group of scan positions the | 1510 | /// The end-of-block position (spec 5.11.39): which group of scan positions the |
| 1218 | /// last coded coefficient falls in, then its offset inside that group — the | 1511 | /// last coded coefficient falls in, then its offset inside that group — the |
| 1219 | /// offset's top bit from a CDF and the rest as raw bits. | 1512 | /// offset's top bit from a CDF and the rest as raw bits. |
| 1220 | -fn write_eob(enc: &mut SymbolEncoder, coding: &mut TxbTables, eob: usize) { | 1513 | +fn write_eob(enc: &mut SymbolEncoder, coding: &mut TxbTables, eob: usize, plane: Option<usize>) { |
| 1221 | /// `Eob_Group_Start` (spec 5.11.39): the first scan position each group of | 1514 | /// `Eob_Group_Start` (spec 5.11.39): the first scan position each group of |
| 1222 | /// end-of-block positions covers, indexed by the group's own number. | 1515 | /// end-of-block positions covers, indexed by the group's own number. |
| 1223 | const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513]; | 1516 | const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513]; |
| @@ -1231,14 +1524,34 @@ fn write_eob(enc: &mut SymbolEncoder, coding: &mut TxbTables, eob: usize) { | |||
| 1231 | // The groups are numbered from one, and how many of them the transform | 1524 | // The groups are numbered from one, and how many of them the transform |
| 1232 | // reaches is the size of its own end-of-block alphabet. | 1525 | // reaches is the size of its own end-of-block alphabet. |
| 1233 | enc.symbol(group - 1, coding.eob_pt); | 1526 | enc.symbol(group - 1, coding.eob_pt); |
| 1527 | + if let Some(plane) = plane { | ||
| 1528 | + ec_rng_trace(|| format!("EC_EOBPT plane={plane} eob_pt={group} tell={}", enc.tell())); | ||
| 1529 | + } | ||
| 1234 | 1530 | ||
| 1235 | let bits = OFFSET_BITS[group]; | 1531 | let bits = OFFSET_BITS[group]; |
| 1236 | if bits > 0 { | 1532 | if bits > 0 { |
| 1237 | let offset = (eob - GROUP_START[group]) as u32; | 1533 | let offset = (eob - GROUP_START[group]) as u32; |
| 1238 | let top = (offset >> (bits - 1)) & 1; | 1534 | let top = (offset >> (bits - 1)) & 1; |
| 1239 | enc.symbol(top as usize, &mut coding.eob_extra[group - 3]); | 1535 | enc.symbol(top as usize, &mut coding.eob_extra[group - 3]); |
| 1536 | + if let Some(plane) = plane { | ||
| 1537 | + ec_rng_trace(|| { | ||
| 1538 | + format!( | ||
| 1539 | + "EC_EOBEXTRA plane={plane} eob_ctx={} top={top} tell={}", | ||
| 1540 | + group - 3, | ||
| 1541 | + enc.tell() | ||
| 1542 | + ) | ||
| 1543 | + }); | ||
| 1544 | + } | ||
| 1240 | if bits > 1 { | 1545 | if bits > 1 { |
| 1241 | enc.literal(offset & ((1 << (bits - 1)) - 1), bits - 1); | 1546 | enc.literal(offset & ((1 << (bits - 1)) - 1), bits - 1); |
| 1547 | + if let Some(plane) = plane { | ||
| 1548 | + ec_rng_trace(|| { | ||
| 1549 | + format!( | ||
| 1550 | + "EC_EOBBITS plane={plane} eob_extra={offset} tell={}", | ||
| 1551 | + enc.tell() | ||
| 1552 | + ) | ||
| 1553 | + }); | ||
| 1554 | + } | ||
| 1242 | } | 1555 | } |
| 1243 | } | 1556 | } |
| 1244 | } | 1557 | } |
| @@ -1380,10 +1693,12 @@ fn write_dc_coeffs( | |||
| 1380 | enc.symbol_fixed(usize::from(dc_level < 0), &cdf::DC_SIGN_LUMA[sign_ctx]); | 1693 | enc.symbol_fixed(usize::from(dc_level < 0), &cdf::DC_SIGN_LUMA[sign_ctx]); |
| 1381 | } | 1694 | } |
| 1382 | 1695 | ||
| 1383 | -/// One neighbour cell's vote in `Dc_Sign_Contexts` (spec 8.3.2): plus one for | 1696 | +/// One 4x4 unit's vote in `Dc_Sign_Contexts` (spec 8.3.2): plus one for a |
| 1384 | -/// a positive DC, minus one for a negative one, nothing for a cell whose block | 1697 | +/// positive DC, minus one for a negative one, nothing for a unit whose block |
| 1385 | -/// carried no DC. Every 4x4 unit of a cell votes the same way, so counting | 1698 | +/// carried no DC or that sits past the frame's true edge (spec |
| 1386 | -/// cells and counting units differ only by a factor the sign is blind to. | 1699 | +/// `av1_set_entropy_contexts`), which is why the vote is gathered per 4x4 |
| 1700 | +/// unit and not per coded cell -- a unit past the edge does not vote even | ||
| 1701 | +/// when the rest of its cell does. | ||
| 1387 | fn dc_vote(dc: Option<bool>) -> i32 { | 1702 | fn dc_vote(dc: Option<bool>) -> i32 { |
| 1388 | match dc { | 1703 | match dc { |
| 1389 | None => 0, | 1704 | None => 0, |
| @@ -1544,12 +1859,16 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 1544 | base_q_idx: u8, | 1859 | base_q_idx: u8, |
| 1545 | blocks: &[BlockCoeffs], | 1860 | blocks: &[BlockCoeffs], |
| 1546 | ) -> Result<Vec<u8>> { | 1861 | ) -> Result<Vec<u8>> { |
| 1547 | - check_superblocks(mi_cols, mi_rows)?; | 1862 | + check_blocks(mi_cols, mi_rows)?; |
| 1548 | - let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); | 1863 | + // `block_grid`'s ceiling, not a plain division: a true frame size that is |
| 1864 | + // not a whole number of 32x32 blocks (or of 64x64 superblocks) still has | ||
| 1865 | + // one more block/superblock whose own origin is inside the true frame, | ||
| 1866 | + // same as `sb_coeff_key_frame_tile`. | ||
| 1867 | + let (cols, rows) = block_grid(mi_cols, mi_rows); | ||
| 1549 | if blocks.len() != (cols * rows) as usize { | 1868 | if blocks.len() != (cols * rows) as usize { |
| 1550 | return Err(Error::unsupported( | 1869 | return Err(Error::unsupported( |
| 1551 | "AV1 tile", | 1870 | "AV1 tile", |
| 1552 | - "an inter frame needs one entry per 32x32 block", | 1871 | + "an inter frame needs one entry per 32x32 block inside the true frame", |
| 1553 | )); | 1872 | )); |
| 1554 | } | 1873 | } |
| 1555 | if let Some(bad) = blocks | 1874 | if let Some(bad) = blocks |
| @@ -1572,8 +1891,13 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 1572 | /// writer's single-reference chain ever names. | 1891 | /// writer's single-reference chain ever names. |
| 1573 | const LAST_FRAME: i8 = 1; | 1892 | const LAST_FRAME: i8 = 1; |
| 1574 | 1893 | ||
| 1575 | - let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI); | 1894 | + let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 1576 | - let mut neighbours = Neighbours::new(cols as usize * 2, rows as usize * 2); | 1895 | + let mut neighbours = Neighbours::new( |
| 1896 | + cols as usize * 2, | ||
| 1897 | + rows as usize * 2, | ||
| 1898 | + mi_cols as usize, | ||
| 1899 | + mi_rows as usize, | ||
| 1900 | + ); | ||
| 1577 | let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); | 1901 | let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); |
| 1578 | let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); | 1902 | let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); |
| 1579 | let mut enc = SymbolEncoder::new(); | 1903 | let mut enc = SymbolEncoder::new(); |
| @@ -1596,14 +1920,57 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 1596 | for sb_c in 0..sb_cols { | 1920 | for sb_c in 0..sb_cols { |
| 1597 | let sb_at = (sb_r as usize * 4, sb_c as usize * 4); | 1921 | let sb_at = (sb_r as usize * 4, sb_c as usize * 4); |
| 1598 | let sb_ctx = neighbours.partition_ctx(sb_at, SB); | 1922 | let sb_ctx = neighbours.partition_ctx(sb_at, SB); |
| 1599 | - enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w64[sb_ctx]); | 1923 | + // spec `decode_partition`'s hasRows/hasCols (5.11.4): a superblock |
| 1924 | + // whose bottom or right half falls outside the true frame cannot | ||
| 1925 | + // be left whole, but this writer only ever splits a superblock | ||
| 1926 | + // into its four 32x32 quadrants (never `PARTITION_NONE` at 64x64), | ||
| 1927 | + // so the only question is which of the three partition symbols | ||
| 1928 | + // that split takes — same three-way signaling as | ||
| 1929 | + // `sb_coeff_key_frame_tile`'s superblock level. | ||
| 1930 | + let (has_cols, has_rows) = ( | ||
| 1931 | + sb_c * SB_MI + SB_MI / 2 < mi_cols, | ||
| 1932 | + sb_r * SB_MI + SB_MI / 2 < mi_rows, | ||
| 1933 | + ); | ||
| 1934 | + match (has_cols, has_rows) { | ||
| 1935 | + (true, true) => enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w64[sb_ctx]), | ||
| 1936 | + (true, false) => { | ||
| 1937 | + enc.symbol_fixed(1, &gather(&cdfs.partition_w64[sb_ctx], VERT_ALIKE)); | ||
| 1938 | + } | ||
| 1939 | + (false, true) => { | ||
| 1940 | + enc.symbol_fixed(1, &gather(&cdfs.partition_w64[sb_ctx], HORZ_ALIKE)); | ||
| 1941 | + } | ||
| 1942 | + (false, false) => {} | ||
| 1943 | + } | ||
| 1600 | 1944 | ||
| 1601 | for quadrant in 0..4 { | 1945 | for quadrant in 0..4 { |
| 1602 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); | 1946 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); |
| 1947 | + // Only a quadrant whose own mi origin is inside the true | ||
| 1948 | + // frame is coded at all (spec `decode_partition`'s | ||
| 1949 | + // `r >= MiRows || c >= MiCols` early return), same filter as | ||
| 1950 | + // `sb_coeff_key_frame_tile`'s `quadrant_positions`. | ||
| 1951 | + if r32 >= rows || c32 >= cols { | ||
| 1952 | + continue; | ||
| 1953 | + } | ||
| 1603 | let block = &blocks[(r32 * cols + c32) as usize]; | 1954 | let block = &blocks[(r32 * cols + c32) as usize]; |
| 1604 | let at = (r32 as usize * 2, c32 as usize * 2); | 1955 | let at = (r32 as usize * 2, c32 as usize * 2); |
| 1605 | let (r, c) = at; | 1956 | let (r, c) = at; |
| 1606 | let ctx32 = neighbours.partition_ctx(at, BLOCK); | 1957 | let ctx32 = neighbours.partition_ctx(at, BLOCK); |
| 1958 | + // This writer has no rectangular (HORZ/VERT) or further-split | ||
| 1959 | + // representation for a 32x32 block, so one that itself | ||
| 1960 | + // straddles the true edge is refused rather than miscoded -- | ||
| 1961 | + // mirroring `sb_coeff_key_frame_tile`'s "cannot be left whole" | ||
| 1962 | + // refusal, just with no split arm to fall back to here. | ||
| 1963 | + let (has_cols32, has_rows32) = ( | ||
| 1964 | + has_half(c32 * BLOCK_MI, BLOCK_MI, mi_cols), | ||
| 1965 | + has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), | ||
| 1966 | + ); | ||
| 1967 | + if !has_cols32 || !has_rows32 { | ||
| 1968 | + return Err(Error::unsupported( | ||
| 1969 | + "AV1 tile", | ||
| 1970 | + "a 32x32 inter-frame block that straddles the true frame edge \ | ||
| 1971 | + needs an 8x8 split this writer does not code", | ||
| 1972 | + )); | ||
| 1973 | + } | ||
| 1607 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[ctx32]); | 1974 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[ctx32]); |
| 1608 | 1975 | ||
| 1609 | let has_above = r32 > 0; | 1976 | let has_above = r32 > 0; |
| @@ -2445,6 +2812,7 @@ mod tests { | |||
| 2445 | &scan, | 2812 | &scan, |
| 2446 | 0, | 2813 | 0, |
| 2447 | 0, | 2814 | 0, |
| 2815 | + None, | ||
| 2448 | ); | 2816 | ); |
| 2449 | priced += luma_32_coeff_bits(grid); | 2817 | priced += luma_32_coeff_bits(grid); |
| 2450 | } | 2818 | } |
| @@ -2743,7 +3111,7 @@ mod tests { | |||
| 2743 | /// appeared beside a 32x32 one. | 3111 | /// appeared beside a 32x32 one. |
| 2744 | #[test] | 3112 | #[test] |
| 2745 | fn a_block_gathers_the_cells_its_neighbours_cover() { | 3113 | fn a_block_gathers_the_cells_its_neighbours_cover() { |
| 2746 | - let mut neighbours = Neighbours::new(4, 4); | 3114 | + let mut neighbours = Neighbours::new(4, 4, 64, 64); |
| 2747 | let quiet = [ | 3115 | let quiet = [ |
| 2748 | vec![0i32; TX16 * TX16], | 3116 | vec![0i32; TX16 * TX16], |
| 2749 | vec![0; TX8 * TX8], | 3117 | vec![0; TX8 * TX8], |
| @@ -2766,8 +3134,9 @@ mod tests { | |||
| 2766 | "the one negative DC above the block is what it votes" | 3134 | "the one negative DC above the block is what it votes" |
| 2767 | ); | 3135 | ); |
| 2768 | // The same read one cell at a time misses it, which is the bug this | 3136 | // The same read one cell at a time misses it, which is the bug this |
| 2769 | - // gate is here for. | 3137 | + // gate is here for. `above[8]` is the first 4x4 unit the quiet block |
| 2770 | - assert!(!neighbours.above[2][1].coded); | 3138 | + // at `(0, 2)` wrote (2 SUB units * 4 4x4-units-per-SUB). |
| 3139 | + assert!(!neighbours.above[8][1].coded); | ||
| 2771 | } | 3140 | } |
| 2772 | 3141 | ||
| 2773 | /// samples. | 3142 | /// samples. |
| @@ -3135,6 +3504,44 @@ mod tests { | |||
| 3135 | } | 3504 | } |
| 3136 | } | 3505 | } |
| 3137 | 3506 | ||
| 3507 | + /// The transpose of [`a_frame_that_is_not_a_whole_number_of_superblocks_codes_every_block`]: | ||
| 3508 | + /// an odd number of 32x32 block *rows* (superblock hangs off the | ||
| 3509 | + /// *bottom*, `has_cols=true, has_rows=false`) rather than an odd number of | ||
| 3510 | + /// columns (hangs off the *right*, `has_cols=false, has_rows=true`) -- | ||
| 3511 | + /// the two halves of the `(has_cols, has_rows)` match in | ||
| 3512 | + /// `sb_coeff_key_frame_tile` gather from different tables | ||
| 3513 | + /// (`VERT_ALIKE`/`HORZ_ALIKE`), and only the right-hand-edge direction had | ||
| 3514 | + /// a real-decoder test before this one. | ||
| 3515 | + #[test] | ||
| 3516 | + fn a_frame_that_hangs_off_the_bottom_codes_every_block() { | ||
| 3517 | + if !have_ffmpeg() { | ||
| 3518 | + eprintln!("SKIP a_frame_that_hangs_off_the_bottom_codes_every_block: no ffmpeg"); | ||
| 3519 | + return; | ||
| 3520 | + } | ||
| 3521 | + let blocks: Vec<BlockCoeffs> = (0..6) | ||
| 3522 | + .map(|i| { | ||
| 3523 | + let (row, col) = if i % 2 == 0 { (0, 1) } else { (1, 0) }; | ||
| 3524 | + BlockCoeffs::from(vec![Coeff { | ||
| 3525 | + row, | ||
| 3526 | + col, | ||
| 3527 | + level: 12, | ||
| 3528 | + }]) | ||
| 3529 | + }) | ||
| 3530 | + .collect(); | ||
| 3531 | + let rows = decode_luma_at(64, 96, &blocks); | ||
| 3532 | + for block_row in 0..3 { | ||
| 3533 | + for block_col in 0..2 { | ||
| 3534 | + let name = format!("block ({block_row},{block_col})"); | ||
| 3535 | + let block = block_at(&rows, block_row, block_col); | ||
| 3536 | + if (block_row * 2 + block_col) % 2 == 0 { | ||
| 3537 | + assert_falls_across(&block, &name); | ||
| 3538 | + } else { | ||
| 3539 | + assert_falls_down(&block, &name); | ||
| 3540 | + } | ||
| 3541 | + } | ||
| 3542 | + } | ||
| 3543 | + } | ||
| 3544 | + | ||
| 3138 | /// A frame whose blocks do not tile it is refused rather than written as a | 3545 | /// A frame whose blocks do not tile it is refused rather than written as a |
| 3139 | /// stream a decoder walks off the end of. | 3546 | /// stream a decoder walks off the end of. |
| 3140 | #[test] | 3547 | #[test] |