forge
Explore
Log in

Don't you dare go Hollow

wip(av1): lane-realworld r4 delta_q/delta_lf at the 75-turn cap (orchestrator commit, unverified)

Browse files at this commit

d1bc3781ac590156b98db405e69401e0646688a2Ahmet Kaan GümüşParent: bf8d564
crates/ec-av1/src/cdf.rsmodified+8 -0
@@ -420,6 +420,14 @@ pub const PALETTE_UV_MODE: [[u16; 3]; 2] = [[32461, 32768, 0], [21488, 32768, 0]
420 /// intra frame with `allow_intrabc` set.420 /// intra frame with `allow_intrabc` set.
421 pub const INTRABC: [u16; 3] = [30531, 32768, 0];421 pub const INTRABC: [u16; 3] = [30531, 32768, 0];
422 422
423+/// `default_delta_q_cdf` (entropymode.c:840-852, lane-realworld r4): the
424+/// 4-symbol `delta_q_abs` alphabet read once per superblock when
425+/// `delta_q_present` is set. `default_delta_lf_cdf`/`default_delta_lf_multi_cdf`
426+/// share this same value (verified against libaom's real source).
427+pub const DELTA_Q: [u16; 5] = [28160, 32120, 32677, 32768, 0];
428+/// `default_delta_lf_cdf` -- see [`DELTA_Q`]'s doc.
429+pub const DELTA_LF: [u16; 5] = DELTA_Q;
430+
423 /// `default_tx_size_cdf[0]` (entropymode.c): an 8x8 block's `tx_depth` flag431 /// `default_tx_size_cdf[0]` (entropymode.c): an 8x8 block's `tx_depth` flag
424 /// (`TX8` vs `TX4`), indexed by `tx_size_context` (0..=2).432 /// (`TX8` vs `TX4`), indexed by `tx_size_context` (0..=2).
425 pub const TX_SIZE_CAT0: [[u16; 3]; 3] = [[19968, 32768, 0], [19968, 32768, 0], [24320, 32768, 0]];433 pub const TX_SIZE_CAT0: [[u16; 3]; 3] = [[19968, 32768, 0], [19968, 32768, 0], [24320, 32768, 0]];
crates/ec-av1/src/cdf_state.rsmodified+16 -0
@@ -433,6 +433,16 @@ pub(crate) struct Cdfs {
433 pub palette_uv_size: [[u16; 8]; 7],433 pub palette_uv_size: [[u16; 8]; 7],
434 /// The `use_intrabc` flag -- see [`cdf::INTRABC`].434 /// The `use_intrabc` flag -- see [`cdf::INTRABC`].
435 pub intrabc: [u16; 3],435 pub intrabc: [u16; 3],
436+ /// `delta_q_abs` (lane-realworld r4, spec 5.11.10) -- see [`cdf::DELTA_Q`].
437+ pub delta_q: [u16; 5],
438+ /// `delta_lf_abs`, the single-value form (spec 5.11.11) -- see
439+ /// [`cdf::DELTA_LF`]. Currently unread: `delta_lf_present` still refuses
440+ /// by name in `stream.rs` (the deblocker has no per-block carrier for
441+ /// the result yet), kept here so the struct is ready for that round.
442+ pub delta_lf: [u16; 5],
443+ /// `delta_lf_abs[i]`, the `delta_lf_multi` per-plane/direction form
444+ /// (`FRAME_LF_COUNT = 4`) -- same unread status as [`Self::delta_lf`].
445+ pub delta_lf_multi: [[u16; 5]; 4],
436 }446 }
437 447
438 /// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`,448 /// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`,
@@ -654,6 +664,9 @@ impl Cdfs {
654 reset2(&mut self.palette_uv_mode);664 reset2(&mut self.palette_uv_mode);
655 reset2(&mut self.palette_uv_size);665 reset2(&mut self.palette_uv_size);
656 reset1(&mut self.intrabc);666 reset1(&mut self.intrabc);
667+ reset1(&mut self.delta_q);
668+ reset1(&mut self.delta_lf);
669+ reset2(&mut self.delta_lf_multi);
657 }670 }
658 671
659 /// The defaults a key frame starts from (spec 8.4, `init_coeff_cdfs` and672 /// The defaults a key frame starts from (spec 8.4, `init_coeff_cdfs` and
@@ -1114,6 +1127,9 @@ impl Cdfs {
1114 palette_uv_mode: cdf::PALETTE_UV_MODE,1127 palette_uv_mode: cdf::PALETTE_UV_MODE,
1115 palette_uv_size: cdf::PALETTE_UV_SIZE,1128 palette_uv_size: cdf::PALETTE_UV_SIZE,
1116 intrabc: cdf::INTRABC,1129 intrabc: cdf::INTRABC,
1130+ delta_q: cdf::DELTA_Q,
1131+ delta_lf: cdf::DELTA_LF,
1132+ delta_lf_multi: [cdf::DELTA_LF; 4],
1117 }1133 }
1118 }1134 }
1119 1135
crates/ec-av1/src/decode.rsmodified+89 -6
@@ -21,7 +21,7 @@
21 //! too). Anything outside that refuses with [`Error::unsupported`] rather than21 //! too). Anything outside that refuses with [`Error::unsupported`] rather than
22 //! silently miscoding.22 //! silently miscoding.
23 23
24-use ec_av1_syntax::{CdefParams, LoopFilterParams, TileInfo};24+use ec_av1_syntax::{CdefParams, DeltaParams, LoopFilterParams, TileInfo};
25 use ec_core::{Error, Result};25 use ec_core::{Error, Result};
26 26
27 use crate::cdf;27 use crate::cdf;
@@ -106,6 +106,68 @@ pub(crate) fn cdef_idx_hits() -> usize {
106 CDEF_IDX_HITS.with(|c| c.get())106 CDEF_IDX_HITS.with(|c| c.get())
107 }107 }
108 108
109+// lane-realworld r4: this frame's `delta.q_present` (spec 5.9.17) and its
110+// actual step (`1 << delta.q_res`, already the real multiplier, not the raw
111+// 2-bit field). `false`/`1` (the defaults) make [`maybe_read_delta_q`] a
112+// true no-op, matching every existing gate stream's `delta_q_present == 0`
113+// behaviour exactly, the same corner-cut [`CDEF_BITS`] takes above.
114+thread_local! {
115+ static DELTA_Q_PRESENT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
116+ static DELTA_Q_RES: std::cell::Cell<i32> = const { std::cell::Cell::new(1) };
117+}
118+// The running quantizer index (spec `CurrentQIndex`, libaom
119+// `xd->current_base_qindex`) -- reset to the frame's own `base_q_idx` at the
120+// top of every tile (spec `decode_tile`), carried across superblocks within
121+// that tile, and mutated only by [`maybe_read_delta_q`]. Read back at the
122+// dequantization call sites instead of the frame-constant `base_q_idx`.
123+thread_local! {
124+ static CURRENT_Q_IDX: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
125+}
126+// How many real `delta_q_present` symbol groups [`maybe_read_delta_q`] has
127+// read, across every call on the current thread -- the gate's proof that a
128+// `delta_q_present` stream actually reached the new reader.
129+thread_local! {
130+ static DELTA_Q_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
131+}
132+
133+/// Current value of [`DELTA_Q_HITS`].
134+pub(crate) fn delta_q_hits() -> usize {
135+ DELTA_Q_HITS.with(|c| c.get())
136+}
137+
138+/// `read_delta_qindex`/`read_delta_q_params` (spec 5.11.10, libaom
139+/// `decodemv.c:85-146,735-770`), called right after [`maybe_read_cdef_idx`]
140+/// at every block-decode call site (spec order `skip -> cdef -> delta_q`).
141+/// A no-op when `delta_q_present` is unset, or when this block is not at the
142+/// superblock's own top-left MI position (`b_row == 0 && b_col == 0` in
143+/// libaom, collapsed here to a position check since a partition tree's
144+/// top-left leaf is the only block that can ever land there), or when that
145+/// leaf both *is* the whole superblock and is `skip` (the one case libaom
146+/// itself skips the read, state carrying over unchanged).
147+fn maybe_read_delta_q(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, is_whole_sb: bool, skip: bool) {
148+ if !DELTA_Q_PRESENT.with(|c| c.get()) {
149+ return;
150+ }
151+ if mi_r % SB_MI as usize != 0 || mi_c % SB_MI as usize != 0 {
152+ return;
153+ }
154+ if is_whole_sb && skip {
155+ return;
156+ }
157+ let mut abs = dec.symbol(&mut cdfs.delta_q) as i32;
158+ const DELTA_Q_SMALL: i32 = 3;
159+ if abs >= DELTA_Q_SMALL {
160+ let rem_bits = dec.literal(3) + 1;
161+ let thr = (1i32 << rem_bits) + 1;
162+ abs = dec.literal(rem_bits) as i32 + thr;
163+ }
164+ let sign_negative = if abs != 0 { dec.literal(1) != 0 } else { true };
165+ let reduced = if sign_negative { -abs } else { abs };
166+ DELTA_Q_HITS.with(|c| c.set(c.get() + 1));
167+ let res = DELTA_Q_RES.with(|c| c.get());
168+ CURRENT_Q_IDX.with(|c| c.set((c.get() + reduced * res).clamp(1, 255)));
169+}
170+
109 /// `read_cdef` (spec 5.11.56, libaom `decodemv.c:read_cdef`), called right171 /// `read_cdef` (spec 5.11.56, libaom `decodemv.c:read_cdef`), called right
110 /// after `skip` is known at every block-decode call site. A no-op at172 /// after `skip` is known at every block-decode call site. A no-op at
111 /// `cdef.bits == 0` (0-bit literal, nothing to consume) -- and173 /// `cdef.bits == 0` (0-bit literal, nothing to consume) -- and
@@ -2139,6 +2201,10 @@ fn read_intra_mode_rect(
2139 }2201 }
2140 let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0;2202 let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0;
2141 maybe_read_cdef_idx(dec, mi_r, mi_c, skip);2203 maybe_read_cdef_idx(dec, mi_r, mi_c, skip);
2204+ // A HORZ/VERT rect strip is never the whole superblock (`bw`/`bh` never
2205+ // both 64 here -- see this fn's own doc), so `is_whole_sb` is always
2206+ // `false`.
2207+ maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip);
2142 let above_ctx = INTRA_MODE_CTX[above_mode];2208 let above_ctx = INTRA_MODE_CTX[above_mode];
2143 let left_ctx = INTRA_MODE_CTX[left_mode];2209 let left_ctx = INTRA_MODE_CTX[left_mode];
2144 let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]);2210 let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]);
@@ -2466,7 +2532,7 @@ fn decode_block_rect(
2466 bw,2532 bw,
2467 bh,2533 bh,
2468 8,2534 8,
2469- i32::from(base_q_idx),2535+ CURRENT_Q_IDX.with(|c| c.get()),
2470 luma_tx_type,2536 luma_tx_type,
2471 );2537 );
2472 y.reconstruct_rect(2538 y.reconstruct_rect(
@@ -2505,7 +2571,7 @@ fn decode_block_rect(
2505 chroma_w,2571 chroma_w,
2506 chroma_h,2572 chroma_h,
2507 8,2573 8,
2508- i32::from(base_q_idx),2574+ CURRENT_Q_IDX.with(|c| c.get()),
2509 u_tx_type,2575 u_tx_type,
2510 );2576 );
2511 u.reconstruct_rect(2577 u.reconstruct_rect(
@@ -2543,7 +2609,7 @@ fn decode_block_rect(
2543 chroma_w,2609 chroma_w,
2544 chroma_h,2610 chroma_h,
2545 8,2611 8,
2546- i32::from(base_q_idx),2612+ CURRENT_Q_IDX.with(|c| c.get()),
2547 v_tx_type,2613 v_tx_type,
2548 );2614 );
2549 v.reconstruct_rect(2615 v.reconstruct_rect(
@@ -2612,6 +2678,7 @@ fn read_intra_mode(
2612 // spec order (see the comment below on `read_intrabc_info`): `skip`,2678 // spec order (see the comment below on `read_intrabc_info`): `skip`,
2613 // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here.2679 // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here.
2614 maybe_read_cdef_idx(dec, mi_r, mi_c, skip);2680 maybe_read_cdef_idx(dec, mi_r, mi_c, skip);
2681+ maybe_read_delta_q(dec, cdfs, mi_r, mi_c, side == 64, skip);
2615 // `read_intrabc_info` (spec 5.11.13, libaom decodemv.c:693, called at2682 // `read_intrabc_info` (spec 5.11.13, libaom decodemv.c:693, called at
2616 // :811 right after `skip_txfm`/`segment_id`/`cdef`/`delta_q` and before2683 // :811 right after `skip_txfm`/`segment_id`/`cdef`/`delta_q` and before
2617 // `mbmi->mode` is ever read): a `use_intrabc` symbol only present on an2684 // `mbmi->mode` is ever read): a `use_intrabc` symbol only present on an
@@ -3148,7 +3215,7 @@ fn read_plane(
3148 }3215 }
3149 full3216 full
3150 };3217 };
3151- let residual = dequant_and_inverse_typed(&levels, side, 8, i32::from(base_q_idx), tx_type);3218+ let residual = dequant_and_inverse_typed(&levels, side, 8, CURRENT_Q_IDX.with(|c| c.get()), tx_type);
3152 if std::env::var_os("EC_AV1_TRACE").is_some() {3219 if std::env::var_os("EC_AV1_TRACE").is_some() {
3153 eprintln!(3220 eprintln!(
3154 "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}",3221 "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}",
@@ -4746,6 +4813,7 @@ pub fn decode_key_frame_tile(
4746 allow_screen_content_tools: bool,4813 allow_screen_content_tools: bool,
4747 allow_intrabc: bool,4814 allow_intrabc: bool,
4748 ) -> Result<Picture> {4815 ) -> Result<Picture> {
4816+ let delta = DeltaParams::default();
4749 let single_tile = TileInfo {4817 let single_tile = TileInfo {
4750 mi_col_starts: vec![0, mi_cols],4818 mi_col_starts: vec![0, mi_cols],
4751 mi_row_starts: vec![0, mi_rows],4819 mi_row_starts: vec![0, mi_rows],
@@ -4768,6 +4836,7 @@ pub fn decode_key_frame_tile(
4768 reduced_tx_set,4836 reduced_tx_set,
4769 allow_screen_content_tools,4837 allow_screen_content_tools,
4770 allow_intrabc,4838 allow_intrabc,
4839+ delta,
4771 )4840 )
4772 .map(|(picture, _)| picture)4841 .map(|(picture, _)| picture)
4773 }4842 }
@@ -4797,6 +4866,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
4797 reduced_tx_set: bool,4866 reduced_tx_set: bool,
4798 allow_screen_content_tools: bool,4867 allow_screen_content_tools: bool,
4799 allow_intrabc: bool,4868 allow_intrabc: bool,
4869+ // lane-realworld r4: this frame header's own `delta` (spec 5.9.17/5.9.18)
4870+ // -- only `q_present`/`q_res` are actually read ([`maybe_read_delta_q`]);
4871+ // `stream.rs` still refuses a frame with `lf_present` set before this
4872+ // function is ever called.
4873+ delta: DeltaParams,
4800 ) -> Result<(Picture, Cdfs)> {4874 ) -> Result<(Picture, Cdfs)> {
4801 if mi_cols == 0 || mi_rows == 0 {4875 if mi_cols == 0 || mi_rows == 0 {
4802 return Err(unsupported("a frame with no mode-info grid"));4876 return Err(unsupported("a frame with no mode-info grid"));
@@ -4806,6 +4880,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
4806 CDEF_BITS.with(|c| c.set(cdef.bits));4880 CDEF_BITS.with(|c| c.set(cdef.bits));
4807 CDEF_SB_COLS.with(|c| c.set(sb_cols as usize));4881 CDEF_SB_COLS.with(|c| c.set(sb_cols as usize));
4808 CDEF_IDX_GRID.with(|g| *g.borrow_mut() = vec![0u8; sb_cols as usize * sb_rows as usize]);4882 CDEF_IDX_GRID.with(|g| *g.borrow_mut() = vec![0u8; sb_cols as usize * sb_rows as usize]);
4883+ DELTA_Q_PRESENT.with(|c| c.set(delta.q_present));
4884+ DELTA_Q_RES.with(|c| c.set(1i32 << delta.q_res));
4809 let (cols32, rows32) = block_grid(mi_cols, mi_rows);4885 let (cols32, rows32) = block_grid(mi_cols, mi_rows);
4810 let (true_width, true_height) = ((mi_cols * 4) as usize, (mi_rows * 4) as usize);4886 let (true_width, true_height) = ((mi_cols * 4) as usize, (mi_rows * 4) as usize);
4811 let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK);4887 let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK);
@@ -4879,6 +4955,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
4879 let (sb_c0, sb_c1) = ((mi_col0 / SB_MI).min(sb_cols), mi_col1.div_ceil(SB_MI).min(sb_cols));4955 let (sb_c0, sb_c1) = ((mi_col0 / SB_MI).min(sb_cols), mi_col1.div_ceil(SB_MI).min(sb_cols));
4880 4956
4881 let mut cdfs = base_cdfs.clone();4957 let mut cdfs = base_cdfs.clone();
4958+ // spec `decode_tile`: `CurrentQIndex` resets to the frame's own
4959+ // `base_q_idx` at the top of every tile, not once per frame.
4960+ CURRENT_Q_IDX.with(|c| c.set(i32::from(base_q_idx)));
4882 if std::env::var_os("EC_AV1_TRACE").is_some() {4961 if std::env::var_os("EC_AV1_TRACE").is_some() {
4883 eprintln!(4962 eprintln!(
4884 "TRACE key_tile_bytes tile={tile_num} len={} first8={:02x?} base_q_idx={base_q_idx} mi_cols={mi_cols} mi_rows={mi_rows}",4963 "TRACE key_tile_bytes tile={tile_num} len={} first8={:02x?} base_q_idx={base_q_idx} mi_cols={mi_cols} mi_rows={mi_rows}",
@@ -5643,7 +5722,7 @@ fn read_inter_plane(
5643 dc_sign_ctx(around.2),5722 dc_sign_ctx(around.2),
5644 default_tx_type,5723 default_tx_type,
5645 )?;5724 )?;
5646- let residual = dequant_and_inverse_typed(&grid, side, 8, i32::from(base_q_idx), tx_type);5725+ let residual = dequant_and_inverse_typed(&grid, side, 8, CURRENT_Q_IDX.with(|c| c.get()), tx_type);
5647 plane.reconstruct_mc(x, y, side, prediction, &residual);5726 plane.reconstruct_mc(x, y, side, prediction, &residual);
5648 Ok((grid, tx_type))5727 Ok((grid, tx_type))
5649 }5728 }
@@ -6734,6 +6813,7 @@ fn decode_inter_block(
6734 let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]);6813 let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]);
6735 let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1;6814 let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1;
6736 maybe_read_cdef_idx(dec, r * SUB_MI as usize, c * SUB_MI as usize, skip);6815 maybe_read_cdef_idx(dec, r * SUB_MI as usize, c * SUB_MI as usize, skip);
6816+ maybe_read_delta_q(dec, cdfs, r * SUB_MI as usize, c * SUB_MI as usize, side == 64, skip);
6737 // lane-warp r5: see `reject_residual` -- a square-block decode of a6817 // lane-warp r5: see `reject_residual` -- a square-block decode of a
6738 // HORZ_B strip is symbol-exact only for `skip` (no residual, no6818 // HORZ_B strip is symbol-exact only for `skip` (no residual, no
6739 // motion_mode/warp symbol), so gate here before any further read.6819 // motion_mode/warp symbol), so gate here before any further read.
@@ -8504,6 +8584,8 @@ fn decode_inter_block8(
8504 let skip_ctx = usize::from(above_skip) + usize::from(left_skip);8584 let skip_ctx = usize::from(above_skip) + usize::from(left_skip);
8505 let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1;8585 let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1;
8506 maybe_read_cdef_idx(dec, leaf_mi.0, leaf_mi.1, skip);8586 maybe_read_cdef_idx(dec, leaf_mi.0, leaf_mi.1, skip);
8587+ // Always a `BLOCK_8X8` leaf here, never the whole superblock.
8588+ maybe_read_delta_q(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip);
8507 8589
8508 let (has_above, has_left) = (8590 let (has_above, has_left) = (
8509 leaf_mi.0 > neighbours.tile_row0_mi,8591 leaf_mi.0 > neighbours.tile_row0_mi,
@@ -9484,6 +9566,7 @@ pub fn decode_inter_frame_tile(
9484 false,9566 false,
9485 false,9567 false,
9486 false,9568 false,
9569+ DeltaParams::default(),
9487 )9570 )
9488 .map(|(picture, _, _)| picture)9571 .map(|(picture, _, _)| picture)
9489 }9572 }