forge
Explore
Log in

Don't you dare go Hollow

First frame that a real decoder decodes

Browse files at this commit

304ec6566eb92795c6606db07e3089c8995d5d44Ahmet Kaan GümüşParent: 3c7b63f
  • feat
    • first frame that a real decoder decodes
Show full message
feat(av1): first frame that a real decoder decodes

A tile payload writer for the one block coding the encoder can produce so
far: a key frame whose every superblock is one skipped 64x64 DC-predicted
block. Four symbols per superblock — the partition, the skip flag and the
two intra modes — with the frame header turning off everything that would
add syntax the writer does not code yet (CDEF, loop restoration, filter
intra, screen content tools, transform size selection, CDF adaptation).

The default CDF tables of spec section 9.4 arrive with it, in the same
orientation the symbol coder reads: the partition, skip, key-frame luma
mode and CFL-free chroma mode tables, which is what this syntax touches.

Gated end to end rather than by roundtrip: the stream is handed to ffmpeg
and every sample of the decoded frame must be 128, which is what a DC
prediction with no neighbours and no residual produces. Any desync between
our arithmetic coder and dav1d's shows up as a decode failure or a sample
that is not mid-grey, with no metric in between to hide it.

Frames that are not a whole number of superblocks are refused by name:
a partial superblock forces the partition syntax down the block tree.
crates/ec-av1/src/cdf.rsadded+198 -0
@@ -0,0 +1,198 @@
1+//! The default CDF tables of spec section 9.4, in the orientation
2+//! [`crate::msac`] reads: cumulative probabilities scaled to `1 << 15`, with
3+//! the alphabet's total as the second-to-last entry and the adaptation counter
4+//! last.
5+//!
6+//! Only the syntax elements the tile writer codes today are here; the rest
7+//! arrive with the syntax that needs them.
8+
9+/// `Default_Partition_W64_Cdf` (spec 9.4): the ten partition types of a
10+/// 64x64 block, indexed by the context the blocks above and left give.
11+pub const PARTITION_W64: [[u16; 11]; 4] = [
12+ [
13+ 20137, 21547, 23078, 29566, 29837, 30261, 30524, 30892, 31724, 32768, 0,
14+ ],
15+ [
16+ 6732, 7490, 9497, 27944, 28250, 28515, 28969, 29630, 30104, 32768, 0,
17+ ],
18+ [
19+ 5945, 7663, 8348, 28683, 29117, 29749, 30064, 30298, 32238, 32768, 0,
20+ ],
21+ [
22+ 870, 1212, 1487, 31198, 31394, 31574, 31743, 31881, 32332, 32768, 0,
23+ ],
24+];
25+
26+/// `Default_Skip_Cdf` (spec 9.4): indexed by how many of the blocks above
27+/// and left were skipped.
28+pub const SKIP: [[u16; 3]; 3] = [[31671, 32768, 0], [16515, 32768, 0], [4576, 32768, 0]];
29+
30+/// `Default_Intra_Frame_Y_Mode_Cdf` (spec 9.4): the thirteen luma intra
31+/// modes of a key frame, indexed by the mode context of the block above and
32+/// then of the block to the left.
33+pub const KF_Y_MODE: [[[u16; 14]; 5]; 5] = [
34+ [
35+ [
36+ 15588, 17027, 19338, 20218, 20682, 21110, 21825, 23244, 24189, 28165, 29093, 30466,
37+ 32768, 0,
38+ ],
39+ [
40+ 12016, 18066, 19516, 20303, 20719, 21444, 21888, 23032, 24434, 28658, 30172, 31409,
41+ 32768, 0,
42+ ],
43+ [
44+ 10052, 10771, 22296, 22788, 23055, 23239, 24133, 25620, 26160, 29336, 29929, 31567,
45+ 32768, 0,
46+ ],
47+ [
48+ 14091, 15406, 16442, 18808, 19136, 19546, 19998, 22096, 24746, 29585, 30958, 32462,
49+ 32768, 0,
50+ ],
51+ [
52+ 12122, 13265, 15603, 16501, 18609, 20033, 22391, 25583, 26437, 30261, 31073, 32475,
53+ 32768, 0,
54+ ],
55+ ],
56+ [
57+ [
58+ 10023, 19585, 20848, 21440, 21832, 22760, 23089, 24023, 25381, 29014, 30482, 31436,
59+ 32768, 0,
60+ ],
61+ [
62+ 5983, 24099, 24560, 24886, 25066, 25795, 25913, 26423, 27610, 29905, 31276, 31794,
63+ 32768, 0,
64+ ],
65+ [
66+ 7444, 12781, 20177, 20728, 21077, 21607, 22170, 23405, 24469, 27915, 29090, 30492,
67+ 32768, 0,
68+ ],
69+ [
70+ 8537, 14689, 15432, 17087, 17408, 18172, 18408, 19825, 24649, 29153, 31096, 32210,
71+ 32768, 0,
72+ ],
73+ [
74+ 7543, 14231, 15496, 16195, 17905, 20717, 21984, 24516, 26001, 29675, 30981, 31994,
75+ 32768, 0,
76+ ],
77+ ],
78+ [
79+ [
80+ 12613, 13591, 21383, 22004, 22312, 22577, 23401, 25055, 25729, 29538, 30305, 32077,
81+ 32768, 0,
82+ ],
83+ [
84+ 9687, 13470, 18506, 19230, 19604, 20147, 20695, 22062, 23219, 27743, 29211, 30907,
85+ 32768, 0,
86+ ],
87+ [
88+ 6183, 6505, 26024, 26252, 26366, 26434, 27082, 28354, 28555, 30467, 30794, 32086,
89+ 32768, 0,
90+ ],
91+ [
92+ 10718, 11734, 14954, 17224, 17565, 17924, 18561, 21523, 23878, 28975, 30287, 32252,
93+ 32768, 0,
94+ ],
95+ [
96+ 9194, 9858, 16501, 17263, 18424, 19171, 21563, 25961, 26561, 30072, 30737, 32463,
97+ 32768, 0,
98+ ],
99+ ],
100+ [
101+ [
102+ 12602, 14399, 15488, 18381, 18778, 19315, 19724, 21419, 25060, 29696, 30917, 32409,
103+ 32768, 0,
104+ ],
105+ [
106+ 8203, 13821, 14524, 17105, 17439, 18131, 18404, 19468, 25225, 29485, 31158, 32342,
107+ 32768, 0,
108+ ],
109+ [
110+ 8451, 9731, 15004, 17643, 18012, 18425, 19070, 21538, 24605, 29118, 30078, 32018,
111+ 32768, 0,
112+ ],
113+ [
114+ 7714, 9048, 9516, 16667, 16817, 16994, 17153, 18767, 26743, 30389, 31536, 32528, 32768,
115+ 0,
116+ ],
117+ [
118+ 8843, 10280, 11496, 15317, 16652, 17943, 19108, 22718, 25769, 29953, 30983, 32485,
119+ 32768, 0,
120+ ],
121+ ],
122+ [
123+ [
124+ 12578, 13671, 15979, 16834, 19075, 20913, 22989, 25449, 26219, 30214, 31150, 32477,
125+ 32768, 0,
126+ ],
127+ [
128+ 9563, 13626, 15080, 15892, 17756, 20863, 22207, 24236, 25380, 29653, 31143, 32277,
129+ 32768, 0,
130+ ],
131+ [
132+ 8356, 8901, 17616, 18256, 19350, 20106, 22598, 25947, 26466, 29900, 30523, 32261,
133+ 32768, 0,
134+ ],
135+ [
136+ 10835, 11815, 13124, 16042, 17018, 18039, 18947, 22753, 24615, 29489, 30883, 32482,
137+ 32768, 0,
138+ ],
139+ [
140+ 7618, 8288, 9859, 10509, 15386, 18657, 22903, 28776, 29180, 31355, 31802, 32593, 32768,
141+ 0,
142+ ],
143+ ],
144+];
145+
146+/// `Default_Uv_Mode_Cdf` (spec 9.4) for blocks where chroma from luma is not
147+/// allowed: the thirteen chroma intra modes, indexed by the luma mode.
148+pub const UV_MODE_NO_CFL: [[u16; 14]; 13] = [
149+ [
150+ 22631, 24152, 25378, 25661, 25986, 26520, 27055, 27923, 28244, 30059, 30941, 31961, 32768,
151+ 0,
152+ ],
153+ [
154+ 9513, 26881, 26973, 27046, 27118, 27664, 27739, 27824, 28359, 29505, 29800, 31796, 32768, 0,
155+ ],
156+ [
157+ 9845, 9915, 28663, 28704, 28757, 28780, 29198, 29822, 29854, 30764, 31777, 32029, 32768, 0,
158+ ],
159+ [
160+ 13639, 13897, 14171, 25331, 25606, 25727, 25953, 27148, 28577, 30612, 31355, 32493, 32768,
161+ 0,
162+ ],
163+ [
164+ 9764, 9835, 9930, 9954, 25386, 27053, 27958, 28148, 28243, 31101, 31744, 32363, 32768, 0,
165+ ],
166+ [
167+ 11825, 13589, 13677, 13720, 15048, 29213, 29301, 29458, 29711, 31161, 31441, 32550, 32768,
168+ 0,
169+ ],
170+ [
171+ 14175, 14399, 16608, 16821, 17718, 17775, 28551, 30200, 30245, 31837, 32342, 32667, 32768,
172+ 0,
173+ ],
174+ [
175+ 12885, 13038, 14978, 15590, 15673, 15748, 16176, 29128, 29267, 30643, 31961, 32461, 32768,
176+ 0,
177+ ],
178+ [
179+ 12026, 13661, 13874, 15305, 15490, 15726, 15995, 16273, 28443, 30388, 30767, 32416, 32768,
180+ 0,
181+ ],
182+ [
183+ 19052, 19840, 20579, 20916, 21150, 21467, 21885, 22719, 23174, 28861, 30379, 32175, 32768,
184+ 0,
185+ ],
186+ [
187+ 18627, 19649, 20974, 21219, 21492, 21816, 22199, 23119, 23527, 27053, 31397, 32148, 32768,
188+ 0,
189+ ],
190+ [
191+ 17026, 19004, 19997, 20339, 20586, 21103, 21349, 21907, 22482, 25896, 26541, 31819, 32768,
192+ 0,
193+ ],
194+ [
195+ 12124, 13759, 14959, 14992, 15007, 15051, 15078, 15166, 15255, 15753, 16039, 16606, 32768,
196+ 0,
197+ ],
198+];
crates/ec-av1/src/lib.rsmodified+2 -0
@@ -13,7 +13,9 @@
13 #![warn(missing_docs)]13 #![warn(missing_docs)]
14 14
15 pub mod bits;15 pub mod bits;
16+pub mod cdf;
16 pub mod frame;17 pub mod frame;
17 pub mod msac;18 pub mod msac;
18 pub mod obu;19 pub mod obu;
19 pub mod sequence;20 pub mod sequence;
21+pub mod tile;
crates/ec-av1/src/tile.rsadded+223 -0
@@ -0,0 +1,223 @@
1+//! Tile payload writer (spec 5.11), for the one block coding the encoder can
2+//! produce today: a key frame whose every superblock is a single 64x64
3+//! DC-predicted block with no residual.
4+//!
5+//! That frame decodes to a flat mid-grey picture — every sample is the value a
6+//! DC prediction with no neighbours produces — which is what makes it a usable
7+//! gate: any desync between this writer and a real decoder shows up as a
8+//! decode failure or a sample that is not mid-grey, with no metric in the way.
9+//! It is the skeleton the block modes, transform sizes and coefficients hang
10+//! off as they arrive.
11+
12+use ec_core::{Error, Result};
13+
14+use crate::cdf;
15+use crate::msac::SymbolEncoder;
16+
17+/// `PARTITION_NONE` (spec 6.10.4): the whole block, undivided.
18+const PARTITION_NONE: usize = 0;
19+/// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode.
20+const DC_PRED: usize = 0;
21+/// Side of a superblock in 4x4 mode-info units when 128x128 superblocks are off.
22+const SB_MI: u32 = 16;
23+
24+/// Writes the payload of a one-tile key frame in which every superblock is a
25+/// skipped 64x64 DC-predicted block.
26+///
27+/// `mi_cols` and `mi_rows` are the frame's dimensions in 4x4 mode-info units,
28+/// as the frame header carries them.
29+///
30+/// # Errors
31+/// Returns an error when the frame is not a whole number of 64x64 superblocks:
32+/// a partial superblock forces the partition syntax down the block tree, which
33+/// this writer does not code yet.
34+pub fn flat_key_frame_tile(mi_cols: u32, mi_rows: u32) -> Result<Vec<u8>> {
35+ if mi_cols == 0
36+ || mi_rows == 0
37+ || !mi_cols.is_multiple_of(SB_MI)
38+ || !mi_rows.is_multiple_of(SB_MI)
39+ {
40+ return Err(Error::unsupported(
41+ "AV1 tile",
42+ "a flat key frame is written only for frames that are a whole \
43+ number of 64x64 superblocks",
44+ ));
45+ }
46+ let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI);
47+
48+ let mut enc = SymbolEncoder::new();
49+ for r in 0..sb_rows {
50+ for c in 0..sb_cols {
51+ // decode_partition (spec 5.11.4). Every neighbour is a 64x64 block,
52+ // whose stored partition context has a zero bit at this block
53+ // size, so the context is 0 wherever the block sits.
54+ enc.symbol_fixed(PARTITION_NONE, &cdf::PARTITION_W64[0]);
55+
56+ // intra_frame_mode_info (spec 5.11.16). Segmentation, delta q,
57+ // delta lf, palette, filter intra and intrabc are all off in the
58+ // frame header, and a skipped block codes no CDEF index, so the
59+ // block is three symbols: the skip flag and the two modes.
60+ let skip_ctx = usize::from(r > 0) + usize::from(c > 0);
61+ enc.symbol_fixed(1, &cdf::SKIP[skip_ctx]);
62+ // Both neighbours are DC-predicted, and an unavailable neighbour
63+ // counts as DC too, so both mode contexts are 0.
64+ enc.symbol_fixed(DC_PRED, &cdf::KF_Y_MODE[0][0]);
65+ // Chroma from luma is only offered up to 32x32, so the CFL-free
66+ // table is the one a 64x64 block reads.
67+ enc.symbol_fixed(DC_PRED, &cdf::UV_MODE_NO_CFL[DC_PRED]);
68+
69+ // read_block_tx_size codes nothing while the frame's tx_mode is
70+ // TX_MODE_LARGEST, and a skipped block has no residual, so the
71+ // block ends here.
72+ }
73+ }
74+ Ok(enc.finish())
75+}
76+
77+#[cfg(test)]
78+mod tests {
79+ use super::*;
80+ use crate::frame::frame_obu;
81+ use crate::obu::temporal_delimiter;
82+ use crate::sequence::sequence_header_obu;
83+ use ec_av1_syntax::sequence::SequenceHeader;
84+ use ec_av1_syntax::{
85+ FrameHeader, FrameType, LoopFilterParams, PRIMARY_REF_NONE, QuantizationParams, TileInfo,
86+ TxMode,
87+ };
88+ use std::io::Write;
89+ use std::process::{Command, Stdio};
90+
91+ /// A 64x64 sequence with every tool this writer does not code turned off:
92+ /// 64x64 superblocks, no CDEF, no loop restoration, no superres, no filter
93+ /// intra, and no screen content tools (which is what keeps intra block copy
94+ /// and palette out of the block syntax).
95+ fn sequence_64() -> SequenceHeader {
96+ let mut seq = crate::sequence::tests::sample_1080p();
97+ seq.frame_width_bits = 7;
98+ seq.frame_height_bits = 7;
99+ seq.max_frame_width = 64;
100+ seq.max_frame_height = 64;
101+ seq.use_128x128_superblock = false;
102+ seq.enable_filter_intra = false;
103+ seq.enable_cdef = false;
104+ seq.enable_restoration = false;
105+ seq.enable_superres = false;
106+ seq.seq_force_screen_content_tools = 0;
107+ seq.seq_force_integer_mv = 0;
108+ seq
109+ }
110+
111+ /// The key frame the tile above belongs to: one tile, quantised so nothing
112+ /// is lossless, one transform size per block, no in-loop filtering, and no
113+ /// CDF adaptation (the writer codes against the defaults).
114+ fn flat_key_frame() -> FrameHeader {
115+ FrameHeader {
116+ frame_type: FrameType::Key,
117+ frame_is_intra: true,
118+ show_frame: true,
119+ error_resilient_mode: true,
120+ disable_cdf_update: true,
121+ allow_screen_content_tools: false,
122+ force_integer_mv: true,
123+ refresh_frame_flags: 0xFF,
124+ primary_ref_frame: PRIMARY_REF_NONE,
125+ frame_width: 64,
126+ frame_height: 64,
127+ upscaled_width: 64,
128+ render_width: 64,
129+ render_height: 64,
130+ mi_cols: 16,
131+ mi_rows: 16,
132+ tile_info: TileInfo {
133+ uniform_spacing: true,
134+ cols: 1,
135+ rows: 1,
136+ cols_log2: 0,
137+ rows_log2: 0,
138+ mi_col_starts: vec![0, 16],
139+ mi_row_starts: vec![0, 16],
140+ context_update_tile_id: 0,
141+ tile_size_bytes: 1,
142+ },
143+ quantization: QuantizationParams {
144+ base_q_idx: 100,
145+ ..QuantizationParams::default()
146+ },
147+ loop_filter: LoopFilterParams::default(),
148+ tx_mode: TxMode::Largest,
149+ reduced_tx_set: false,
150+ ..FrameHeader::default()
151+ }
152+ }
153+
154+ fn have_ffmpeg() -> bool {
155+ Command::new("ffmpeg")
156+ .arg("-version")
157+ .stdout(Stdio::null())
158+ .stderr(Stdio::null())
159+ .status()
160+ .is_ok_and(|s| s.success())
161+ }
162+
163+ /// Decodes an AV1 OBU stream with ffmpeg and hands back the planes.
164+ fn ffmpeg_decode(stream: &[u8], w: usize, h: usize) -> Vec<u8> {
165+ let mut child = Command::new("ffmpeg")
166+ .args([
167+ "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-",
168+ ])
169+ .stdin(Stdio::piped())
170+ .stdout(Stdio::piped())
171+ .stderr(Stdio::piped())
172+ .spawn()
173+ .expect("ffmpeg failed to start");
174+ child
175+ .stdin
176+ .take()
177+ .expect("ffmpeg stdin")
178+ .write_all(stream)
179+ .expect("writing the stream to ffmpeg");
180+ let out = child.wait_with_output().expect("ffmpeg failed to run");
181+ assert!(
182+ out.status.success(),
183+ "ffmpeg refused the stream: {}",
184+ String::from_utf8_lossy(&out.stderr)
185+ );
186+ assert_eq!(
187+ out.stdout.len(),
188+ w * h * 3 / 2,
189+ "expected one 4:2:0 frame, ffmpeg said: {}",
190+ String::from_utf8_lossy(&out.stderr)
191+ );
192+ out.stdout
193+ }
194+
195+ #[test]
196+ fn flat_key_frame_decodes_to_mid_grey() {
197+ if !have_ffmpeg() {
198+ eprintln!("SKIP flat_key_frame_decodes_to_mid_grey: no ffmpeg on PATH");
199+ return;
200+ }
201+ let seq = sequence_64();
202+ let header = flat_key_frame();
203+ let tile = flat_key_frame_tile(header.mi_cols, header.mi_rows).unwrap();
204+
205+ let mut stream = temporal_delimiter();
206+ stream.extend_from_slice(&sequence_header_obu(&seq).unwrap());
207+ stream.extend_from_slice(&frame_obu(&seq, &header, &tile).unwrap());
208+
209+ let planes = ffmpeg_decode(&stream, 64, 64);
210+ // A DC prediction with no neighbour to average is the middle of the
211+ // range, and a skipped block adds no residual, so every sample of every
212+ // plane is 128 — one wrong symbol anywhere in the tile shows up here.
213+ for (i, &s) in planes.iter().enumerate() {
214+ assert_eq!(s, 128, "sample {i} of the decoded frame");
215+ }
216+ }
217+
218+ #[test]
219+ fn partial_superblocks_are_refused() {
220+ assert!(flat_key_frame_tile(16, 20).is_err());
221+ assert!(flat_key_frame_tile(0, 16).is_err());
222+ }
223+}