forge
Explore
Log in

Don't you dare go Hollow

The encoder writes multi-tile frames, sample-exact at every layout

Browse files at this commit

a01d142597e40d984e6941a72743b18a8ef086ffAhmet Kaan GümüşParent: 252b2d1
  • feat
    • the encoder writes multi-tile frames, sample-exact at every layout
Show full message
feat(av1): the encoder writes multi-tile frames, sample-exact at every layout

Uniform tile spacing (spec 5.9.15), one tile group OBU with per-tile sizes,
context_update_tile_id on the largest tile, and per-tile CDF / LR-reference /
neighbour-band / MV-grid resets mirroring the decoder's own tile loop. The
writer's availability answers are tile-relative now (the intra_inter and
tx-size contexts asked mi_row > 0 and coded a tile's first row against a
neighbour the decoder does not have).

EncoderConfig gains tile_cols_log2/tile_rows_log2 (default 0/0, byte-identical
to the one-tile stream); ec-bench takes EC_AV1_TILES=<cols>:<rows>.
crates/ec-av1/src/decode.rsmodified+0 -7
@@ -23882,7 +23882,6 @@ impl<'p> RefPix<'p> {
23882 /// (lane-defer8).23882 /// (lane-defer8).
23883 fn ref_dims(ref_frame: i8, refpix: &RefPix<'_>) -> Result<(usize, usize)> {23883 fn ref_dims(ref_frame: i8, refpix: &RefPix<'_>) -> Result<(usize, usize)> {
23884 refpix.dims(ref_frame).ok_or_else(|| {23884 refpix.dims(ref_frame).ok_or_else(|| {
23885- eprintln!("TILEDBG missing ref_frame={ref_frame}");
23886 unsupported(23885 unsupported(
23887 "a reference frame selected with no picture at this frame's own \23886 "a reference frame selected with no picture at this frame's own \
23888 ref_frame_idx slot for it",23887 ref_frame_idx slot for it",
@@ -25415,9 +25414,6 @@ fn decode_inter_block(
25415 // combinations that don't implement scaled MC yet.25414 // combinations that don't implement scaled MC yet.
25416 frame_width: usize, fctx: &crate::decode::FrameCtx,25415 frame_width: usize, fctx: &crate::decode::FrameCtx,
25417 ) -> Result<()> {25416 ) -> Result<()> {
25418- if std::env::var_os("TILEDBG").is_some() {
25419- eprintln!("TILEDBG dec block at={at:?} side={side} tell={}", dec.debug_bitpos());
25420- }
25421 25417
25422 // lane-inter4 r1: `at` is in MI units (4 px) so a 32-level 1:4 strip can25418 // lane-inter4 r1: `at` is in MI units (4 px) so a 32-level 1:4 strip can
25423 // name an 8-px offset; `(r, c)` stays the enclosing 16-px cell for the25419 // name an 8-px offset; `(r, c)` stays the enclosing 16-px cell for the
@@ -33446,9 +33442,6 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
33446 let waving = fctx.wave.with(|w| w.borrow().is_some());33442 let waving = fctx.wave.with(|w| w.borrow().is_some());
33447 let mut wave_job = (0usize, 0usize);33443 let mut wave_job = (0usize, 0usize);
33448 for (sb_r, sb_c, row_start, sb_start) in sb_visit_order(sb_r0, sb_r1, sb_c0, sb_c1, sb128) {33444 for (sb_r, sb_c, row_start, sb_start) in sb_visit_order(sb_r0, sb_r1, sb_c0, sb_c1, sb128) {
33449- if std::env::var_os("TILEDBG").is_some() {
33450- eprintln!("TILEDBG dec tile={tile_idx} sb_r={sb_r} sb_c={sb_c} tell={}", dec.debug_bitpos());
33451- }
33452 // lane-wave1: the previous superblock's recorded writes -- handed to33445 // lane-wave1: the previous superblock's recorded writes -- handed to
33453 // the wavefront workers, or replayed here at one recon thread (where33446 // the wavefront workers, or replayed here at one recon thread (where
33454 // the queue is empty, every write having run at its push site).33447 // the queue is empty, every write having run at its push site).
crates/ec-av1/src/encode.rsmodified+0 -6
@@ -4983,12 +4983,6 @@ pub(crate) fn encode_inter_frame(
4983 |bits, sb_cols, grid, units| code_tiles(bits, sb_cols, grid, units),4983 |bits, sb_cols, grid, units| code_tiles(bits, sb_cols, grid, units),
4984 |lf, cdef, h, tiles: &[Vec<u8>], lr| {4984 |lf, cdef, h, tiles: &[Vec<u8>], lr| {
4985 let payloads: Vec<&[u8]> = tiles.iter().map(Vec::as_slice).collect();4985 let payloads: Vec<&[u8]> = tiles.iter().map(Vec::as_slice).collect();
4986- eprintln!(
4987- "TILEDBG inter decode tiles={:?} cdef_bits={} lr={}",
4988- tiles.iter().map(Vec::len).collect::<Vec<_>>(),
4989- cdef.bits,
4990- lr.uses_lr
4991- );
4992 crate::decode::decode_inter_frame_tiles_lr(4986 crate::decode::decode_inter_frame_tiles_lr(
4993 &payloads,4987 &payloads,
4994 &header_tile_info,4988 &header_tile_info,
crates/ec-av1/src/encoder.rsmodified+56 -3
@@ -1650,10 +1650,10 @@ mod tests {
1650 /// spec does not, and the parser half catches the reverse.1650 /// spec does not, and the parser half catches the reverse.
1651 #[test]1651 #[test]
1652 fn every_tile_layout_decodes_sample_exact_through_both_decoders() {1652 fn every_tile_layout_decodes_sample_exact_through_both_decoders() {
1653- let (width, height) = (128usize, 128usize);1653+ let (width, height) = (640usize, 384usize);
1654- let sources: Vec<Picture> = (0..2).map(|t| test_card(width, height, t * 3)).collect();1654+ let sources: Vec<Picture> = (0..4).map(|t| test_card(width, height, t * 3)).collect();
1655 let mut sizes = Vec::new();1655 let mut sizes = Vec::new();
1656- for (cols_log2, rows_log2) in [(0u32, 1u32)] {1656+ for (cols_log2, rows_log2) in [(0u32, 0u32), (1, 0), (0, 1), (1, 1), (2, 1)] {
1657 let config = EncoderConfig {1657 let config = EncoderConfig {
1658 width,1658 width,
1659 height,1659 height,
@@ -1731,6 +1731,59 @@ mod tests {
1731 }1731 }
1732 }1732 }
1733 1733
1734+ /// The same round trip at a real 1920x1080 crop of the gate's own clip
1735+ /// -- the size the editor's export actually runs at, where a tile grid
1736+ /// is worth having. Ignored by default only for its wall (a 1080p
1737+ /// encode of three pictures), not for any weakness in the check.
1738+ #[test]
1739+ #[ignore = "1080p encode: minutes, run it with --ignored"]
1740+ fn a_1080p_multi_tile_stream_decodes_sample_exact_through_both_decoders() {
1741+ let (width, height) = (1920usize, 1080usize);
1742+ let Some(sources) = h264_clip_frames(width, height, 3) else {
1743+ eprintln!("SKIP the 1080p tile round trip: no fixture");
1744+ return;
1745+ };
1746+ for (cols_log2, rows_log2) in [(1u32, 0u32), (1, 1), (2, 1)] {
1747+ let config = EncoderConfig {
1748+ width,
1749+ height,
1750+ base_q_idx: 120,
1751+ gop: 3,
1752+ colour: Colour::Bt709Limited,
1753+ tile_cols_log2: cols_log2,
1754+ tile_rows_log2: rows_log2,
1755+ };
1756+ let layout = format!("{}x{} tiles", 1 << cols_log2, 1 << rows_log2);
1757+ let mut enc = Av1Encoder::new(config).unwrap();
1758+ let mut stream = Vec::new();
1759+ for (i, picture) in sources.iter().enumerate() {
1760+ let packet = enc
1761+ .encode(picture)
1762+ .unwrap_or_else(|e| panic!("{layout} frame {i}: {e}"));
1763+ stream.extend_from_slice(&packet.data);
1764+ }
1765+ let ours = crate::stream::decode_stream(&stream).expect("our decoder");
1766+ assert_eq!(ours.len(), sources.len(), "{layout}: our decoder's frames");
1767+ if !have_ffmpeg() {
1768+ eprintln!("SKIP the ffmpeg half of 1080p {layout}: no ffmpeg");
1769+ continue;
1770+ }
1771+ let theirs = ffmpeg_decode_luma(&stream, width, height);
1772+ assert_eq!(theirs.len(), sources.len(), "{layout}: ffmpeg's frames");
1773+ for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() {
1774+ let got: Vec<u8> = a.y.iter().map(|&v| v as u8).collect();
1775+ if let Some(at) = got.iter().zip(b).position(|(x, y)| x != y) {
1776+ panic!(
1777+ "1080p {layout} frame {i}: luma differs first at ({}, {})",
1778+ at % width,
1779+ at / width
1780+ );
1781+ }
1782+ }
1783+ eprintln!("1080p {layout}: {} bytes, sample-exact", stream.len());
1784+ }
1785+ }
1786+
1734 /// [`RateTarget::Quality`] is monotone: a higher quality dial never1787 /// [`RateTarget::Quality`] is monotone: a higher quality dial never
1735 /// produces a smaller stream or a worse mean PSNR than a lower one, on1788 /// produces a smaller stream or a worse mean PSNR than a lower one, on
1736 /// the same real clip.1789 /// the same real clip.
crates/ec-av1/src/tile.rsmodified+36 -24
@@ -952,6 +952,14 @@ impl Quadrant {
952 /// What the blocks above and to the left of the one being coded left behind,952 /// What the blocks above and to the left of the one being coded left behind,
953 /// kept on the 16x16 grid the smallest block sits on.953 /// kept on the 16x16 grid the smallest block sits on.
954 struct Neighbours {954 struct Neighbours {
955+ /// This tile's own top-left mode-info origin (spec 5.11.1's `MiRowStart`/
956+ /// `MiColStart`): every "is there a neighbour above/left of me" answer is
957+ /// TILE-relative, not frame-relative -- the decoder's own
958+ /// `Neighbours::start_tile` sets the same pair, and a writer that asked
959+ /// `mi_row > 0` instead coded a tile's own first row against a neighbour
960+ /// the decoder does not have (class: new map ignores tile edge).
961+ tile_row0_mi: usize,
962+ tile_col0_mi: usize,
955 /// Per plane, whether the neighbour coded anything and the sign of its DC.963 /// Per plane, whether the neighbour coded anything and the sign of its DC.
956 above: Vec<[Neighbour; 3]>,964 above: Vec<[Neighbour; 3]>,
957 /// The same, down the left edge.965 /// The same, down the left edge.
@@ -1044,6 +1052,8 @@ impl Neighbours {
1044 /// mode-info units.1052 /// mode-info units.
1045 fn new(cols: usize, rows: usize, mi_cols: usize, mi_rows: usize) -> Self {1053 fn new(cols: usize, rows: usize, mi_cols: usize, mi_rows: usize) -> Self {
1046 Self {1054 Self {
1055+ tile_row0_mi: 0,
1056+ tile_col0_mi: 0,
1047 above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)],1057 above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)],
1048 left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)],1058 left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)],
1049 above_mode: vec![DC_PRED; cols],1059 above_mode: vec![DC_PRED; cols],
@@ -1068,6 +1078,23 @@ impl Neighbours {
1068 }1078 }
1069 1079
1070 /// Clears the left edge, which a tile starts every superblock row with.1080 /// Clears the left edge, which a tile starts every superblock row with.
1081+ /// Points this writer's availability answers at one tile
1082+ /// ([`Self::tile_row0_mi`]).
1083+ fn set_tile_origin(&mut self, row0_mi: usize, col0_mi: usize) {
1084+ self.tile_row0_mi = row0_mi;
1085+ self.tile_col0_mi = col0_mi;
1086+ }
1087+
1088+ /// Whether the block at mode-info row `mi_r` has a neighbour above it
1089+ /// INSIDE its own tile, and the same to the left.
1090+ fn has_above(&self, mi_r: usize) -> bool {
1091+ mi_r > self.tile_row0_mi
1092+ }
1093+
1094+ fn has_left(&self, mi_c: usize) -> bool {
1095+ mi_c > self.tile_col0_mi
1096+ }
1097+
1071 fn start_row(&mut self) {1098 fn start_row(&mut self) {
1072 self.left.iter_mut().for_each(|l| *l = Default::default());1099 self.left.iter_mut().for_each(|l| *l = Default::default());
1073 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);1100 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);
@@ -1109,7 +1136,7 @@ impl Neighbours {
1109 /// off a CDF row one higher than the decoder's, which is the first1136 /// off a CDF row one higher than the decoder's, which is the first
1110 /// divergence of the inter `TxMode::Select` stream.1137 /// divergence of the inter `TxMode::Select` stream.
1111 fn tx_size_ctx_txfm(&self, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize {1138 fn tx_size_ctx_txfm(&self, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize {
1112- let (has_above, has_left) = (mi_r > 0, mi_c > 0);1139+ let (has_above, has_left) = (self.has_above(mi_r), self.has_left(mi_c));
1113 let mut above = usize::from(self.above_txfm[mi_c]) >= own_side;1140 let mut above = usize::from(self.above_txfm[mi_c]) >= own_side;
1114 let mut left = usize::from(self.left_txfm[mi_r]) >= own_side;1141 let mut left = usize::from(self.left_txfm[mi_r]) >= own_side;
1115 if has_above && self.above_inter[mi_c] {1142 if has_above && self.above_inter[mi_c] {
@@ -1126,8 +1153,8 @@ impl Neighbours {
1126 /// largest transform, plus whether the one to the left is at least as1153 /// largest transform, plus whether the one to the left is at least as
1127 /// tall. A neighbour outside the tile contributes nothing.1154 /// tall. A neighbour outside the tile contributes nothing.
1128 fn tx_size_ctx(&self, (mi_r, mi_c): (usize, usize), max_tx: usize) -> usize {1155 fn tx_size_ctx(&self, (mi_r, mi_c): (usize, usize), max_tx: usize) -> usize {
1129- usize::from(mi_r > 0 && usize::from(self.above_tx[mi_c]) >= max_tx)1156+ usize::from(self.has_above(mi_r) && usize::from(self.above_tx[mi_c]) >= max_tx)
1130- + usize::from(mi_c > 0 && usize::from(self.left_tx[mi_r]) >= max_tx)1157+ + usize::from(self.has_left(mi_c) && usize::from(self.left_tx[mi_r]) >= max_tx)
1131 }1158 }
1132 1159
1133 /// Publishes one block's resolved transform side over every 4x4 unit it1160 /// Publishes one block's resolved transform side over every 4x4 unit it
@@ -1472,6 +1499,7 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs(
1472 mi_cols as usize,1499 mi_cols as usize,
1473 mi_rows as usize,1500 mi_rows as usize,
1474 );1501 );
1502+ neighbours.set_tile_origin(tile.mi_row0 as usize, tile.mi_col0 as usize);
1475 1503
1476 // The tile adapts every non-literal CDF it writes, exactly as the decoder1504 // The tile adapts every non-literal CDF it writes, exactly as the decoder
1477 // adapts the ones it reads, so the frame header leaves `disable_cdf_update`1505 // adapts the ones it reads, so the frame header leaves `disable_cdf_update`
@@ -3396,6 +3424,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
3396 mi_cols as usize,3424 mi_cols as usize,
3397 mi_rows as usize,3425 mi_rows as usize,
3398 );3426 );
3427+ neighbours.set_tile_origin(tile.mi_row0 as usize, tile.mi_col0 as usize);
3399 // The MV grid spans the FRAME and carries every tile written before this3428 // The MV grid spans the FRAME and carries every tile written before this
3400 // one, exactly as a decoder's does (`decode_inter_frame_tile_with_cdfs`3429 // one, exactly as a decoder's does (`decode_inter_frame_tile_with_cdfs`
3401 // keeps one grid for the whole frame and narrows its read window per3430 // keeps one grid for the whole frame and narrows its read window per
@@ -3443,14 +3472,6 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
3443 write_lr(&mut enc, &mut cdfs, sb_r * SB_MI_W, sb_c * SB_MI_W);3472 write_lr(&mut enc, &mut cdfs, sb_r * SB_MI_W, sb_c * SB_MI_W);
3444 let sb_at = (sb_r as usize * 4, sb_c as usize * 4);3473 let sb_at = (sb_r as usize * 4, sb_c as usize * 4);
3445 let sb_ctx = neighbours.partition_ctx(sb_at, SB);3474 let sb_ctx = neighbours.partition_ctx(sb_at, SB);
3446- if std::env::var_os("TILEDBG").is_some() {
3447- eprintln!(
3448- "TILEDBG enc sb at=({}, {}) ctx={sb_ctx} tell={}",
3449- sb_at.0 * 4,
3450- sb_at.1 * 4,
3451- enc.tell()
3452- );
3453- }
3454 // spec `decode_partition`'s hasRows/hasCols (5.11.4): a superblock3475 // spec `decode_partition`'s hasRows/hasCols (5.11.4): a superblock
3455 // whose bottom or right half falls outside the true frame cannot3476 // whose bottom or right half falls outside the true frame cannot
3456 // be left whole, but this writer only ever splits a superblock3477 // be left whole, but this writer only ever splits a superblock
@@ -3484,15 +3505,6 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
3484 }3505 }
3485 let site = &blocks[(r32 * cols + c32) as usize];3506 let site = &blocks[(r32 * cols + c32) as usize];
3486 let at = (r32 as usize * 2, c32 as usize * 2);3507 let at = (r32 as usize * 2, c32 as usize * 2);
3487- if std::env::var_os("TILEDBG").is_some() {
3488- eprintln!(
3489- "TILEDBG enc block at=({}, {}) whole={} tell={}",
3490- at.0 * 4,
3491- at.1 * 4,
3492- matches!(site, Quadrant::Whole(_)),
3493- enc.tell()
3494- );
3495- }
3496 let ctx32 = neighbours.partition_ctx(at, BLOCK);3508 let ctx32 = neighbours.partition_ctx(at, BLOCK);
3497 // spec `decode_partition`'s hasRows/hasCols recomputed at this3509 // spec `decode_partition`'s hasRows/hasCols recomputed at this
3498 // quadrant's own half: a whole 32x32 block cannot be left3510 // quadrant's own half: a whole 32x32 block cannot be left
@@ -3699,9 +3711,9 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
3699 };3711 };
3700 3712
3701 let (r, c) = at;3713 let (r, c) = at;
3702- let has_above = r32 > 0;
3703- let has_left = c32 > 0;
3704 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));3714 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));
3715+ let has_above = neighbours.has_above(mi_r);
3716+ let has_left = neighbours.has_left(mi_c);
3705 let skip_ctx = usize::from(neighbours.above_skip[mi_c])3717 let skip_ctx = usize::from(neighbours.above_skip[mi_c])
3706 + usize::from(neighbours.left_skip[mi_r]);3718 + usize::from(neighbours.left_skip[mi_r]);
3707 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);3719 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);
@@ -3912,7 +3924,7 @@ fn write_inter_frame_leaf(
3912 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);3924 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);
3913 write_cdef_idx(enc, (mi_r, mi_c), block.skip);3925 write_cdef_idx(enc, (mi_r, mi_c), block.skip);
3914 3926
3915- let (has_above, has_left) = (r > 0, c > 0);3927+ let (has_above, has_left) = (neighbours.has_above(mi_r), neighbours.has_left(mi_c));
3916 let (above_inter, left_inter) = (neighbours.above_inter[mi_c], neighbours.left_inter[mi_r]);3928 let (above_inter, left_inter) = (neighbours.above_inter[mi_c], neighbours.left_inter[mi_r]);
3917 let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);3929 let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
3918 let is_inter = block.inter.is_some();3930 let is_inter = block.inter.is_some();
@@ -4106,7 +4118,7 @@ fn write_inter_frame_leaf8(
4106 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);4118 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);
4107 write_cdef_idx(enc, leaf_mi, block.skip);4119 write_cdef_idx(enc, leaf_mi, block.skip);
4108 4120
4109- let (has_above, has_left) = (leaf_mi.0 > 0, leaf_mi.1 > 0);4121+ let (has_above, has_left) = (neighbours.has_above(leaf_mi.0), neighbours.has_left(leaf_mi.1));
4110 let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);4122 let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
4111 let is_inter = block.inter.is_some();4123 let is_inter = block.inter.is_some();
4112 enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]);4124 enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]);