From 4950fc399f1dd5ed76ddde74b60c064e072b373c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Sep 2026 21:46:18 +0300 Subject: [PATCH] feat(av1): per-block segment_id (spec 5.11.7-5.11.9) with per-segment qindex and loop-filter deltas Reads intra_segment_id (pre/post-skip per SegIdPreSkip) and inter_segment_id at both spec positions, incl. seg_id_predicted against the primary reference's saved map (load_previous_segment_ids); the decoded map is carried into every reference slot refresh_frame_flags names. Consumers: get_qindex SEG_LVL_ALT_Q at all dequant sites, SEG_LVL_ALT_LF_* in lf_level. Both seg CDFs adapt and sit in the per-frame counter reset. The blanket segmentation refusal is replaced by a narrow SEG_LVL_REF_FRAME/SKIP/GLOBALMV one. Gates: aomenc --aq-mode=1 --deltaq-mode=0 8-bit 16-frame (10/10 pixel-exact incl. hidden frames, 223 segment_id symbols, 6 distinct ids, 35 seg_id_predicted) and 10-bit key frame (10/10, 80 symbols, 5 ids). --- crates/ec-av1/src/decode.rs | 11 +++++- crates/ec-av1/src/stream.rs | 16 +++++++-- lanes/seg-r1.report.md | 67 +++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 lanes/seg-r1.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 4f0e7cfb..654ee4ee 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -350,6 +350,8 @@ thread_local! { /// ids any block ended up with -- the gate's proof the feature fired. static SEG_ID_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; static SEG_IDS_SEEN: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// How many `seg_id_predicted` symbols were read (temporal update). + static SEG_PRED_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// Installs this frame's segmentation parameters and allocates its map, with @@ -395,10 +397,16 @@ pub(crate) fn segment_ids_seen() -> usize { SEG_IDS_SEEN.with(|c| c.get().count_ones() as usize) } -/// Resets both segmentation hit counters (gate setup). +/// How many `seg_id_predicted` symbols have been read (temporal update). +pub(crate) fn segment_pred_hits() -> usize { + SEG_PRED_HITS.with(|c| c.get()) +} + +/// Resets the segmentation hit counters (gate setup). pub(crate) fn reset_segment_hits() { SEG_ID_HITS.with(|c| c.set(0)); SEG_IDS_SEEN.with(|c| c.set(0)); + SEG_PRED_HITS.with(|c| c.set(0)); } /// `seg_feature_active_idx(segment_id, feature)` for the frame currently @@ -603,6 +611,7 @@ fn inter_segment_id( let id = if temporal_update { let ctx = seg_pred_ctx(mi_r, mi_c); let predicted = dec.symbol(&mut cdfs.segment_pred[ctx]) != 0; + SEG_PRED_HITS.with(|c| c.set(c.get() + 1)); stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, u8::from(predicted)); if predicted { predicted_segment_id(mi_r, mi_c, w_mi, h_mi) diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 6f068eb4..c5210163 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -8896,11 +8896,23 @@ mod tests { every block landed in one segment, so nothing was actually segmented", crate::decode::segment_ids_seen() ); + // An inter-frame run must also have exercised the TEMPORAL path + // (`seg_id_predicted` against the primary reference's saved map); + // the key-frame-only run has no previous map by construction. + if frame_count > 1 { + assert!( + crate::decode::segment_pred_hits() > 0, + "{name}: {matched} pixel-exact matches over {frame_count} frames but zero \ + seg_id_predicted symbols -- the temporal_update path is unexercised" + ); + } eprintln!( "{name}: {named_refusals} other-capability refusals, {matched} pixel-exact matches \ - out of {n_attempts}, segment_id_hits={}, distinct segment ids={}", + out of {n_attempts}, segment_id_hits={}, distinct segment ids={}, \ + seg_id_predicted symbols={}", crate::decode::segment_id_hits(), - crate::decode::segment_ids_seen() + crate::decode::segment_ids_seen(), + crate::decode::segment_pred_hits() ); } diff --git a/lanes/seg-r1.report.md b/lanes/seg-r1.report.md new file mode 100644 index 00000000..c0a2991c --- /dev/null +++ b/lanes/seg-r1.report.md @@ -0,0 +1,67 @@ +# lane-seg r1 — per-block `segment_id` and its consumers + +Branch `lane-seg`, off main `3808cf8` (WIP snapshot `51581d3` continued, not restarted). + +## What changed + +- `crates/ec-av1/src/cdf.rs:564` — `SEGMENT_ID` (`default_spatial_pred_seg_tree_cdf`, 3 ctx x 8 symbols) + and `SEGMENT_PRED` (`seg_id_predicted`, 3 ctx) default CDFs. +- `crates/ec-av1/src/cdf_state.rs:467` — both tables in `Cdfs`, in `reset_counters` (class + `cdf-counter-not-reset`) and in the default-table constructor, so they adapt and reset like every + other per-tile table. +- `crates/ec-av1/src/decode.rs:319-655` — the reader: `set_segmentation` (per-frame install + + `load_previous_segment_ids`), `CurrentSegmentIds`/`PrevSegmentIds` maps, `AboveSegPredContext`/ + `LeftSegPredContext`, `av1_neg_deinterleave`, `av1_get_spatial_seg_pred` (ctx + prediction), + `read_segment_id`, `intra_segment_id` (spec 5.11.7) and `inter_segment_id` (5.11.9, both + pre-skip and post-skip positions, `seg_id_predicted` under `temporal_update`, inherited map when + `update_map == 0`), `set_segment_id` stamping the block's whole mi footprint. +- Call sites: `read_intra_mode` (decode.rs:4375) and `read_intra_mode_rect` (3332) read + `intra_segment_id` before `skip` when `SegIdPreSkip`, after it otherwise; + `decode_inter_block` (9250/9268) and `decode_inter_block8` (11106/11116) read both spec positions. +- Consumers: `block_q_idx()` (decode.rs:400, spec 7.12.2 `get_qindex` with `SEG_LVL_ALT_Q`) replaces + the raw `CURRENT_Q_IDX` read at all 10 dequant sites; `lf_level` (decode.rs:6168) takes the block's + segment id and applies `SEG_LVL_ALT_LF_Y_V/H`/`_U`/`_V` (spec 7.14.4) between the `DeltaLF` term and + the ref/mode deltas; `edge_params` passes the per-side segment ids. +- `crates/ec-av1/src/stream.rs:266-291` — frame-level: narrowed refusal (below), spec + `load_previous_segment_ids` from the primary reference's slot, `set_segmentation` per frame; + `stream.rs:736` stores the decoded map into every slot `refresh_frame_flags` names. +- `crates/ec-av1/src/refusal_inventory.rs:59` — refusal replaced. + +## Refusal + +LIFTED: `"a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)"`. +REPLACED BY (narrower): `"a frame whose segmentation enables SEG_LVL_REF_FRAME/SKIP/GLOBALMV (this +decoder reads segment_id but never lets a segment override a block's reference, skip or mode)"` — +those three features rewrite mode/reference decisions and suppress symbols `decode_inter_block` reads +unconditionally; aomenc's `--aq-mode` only ever sets `SEG_LVL_ALT_Q`, so no gate here can exercise +them. `gate_coverage.rs` needed no edit: it derives coverage from `--enable-*` flags and segmentation +is driven by `--aq-mode`, not an `--enable-*` flag; its two tests stay green. + +## Gates (both hard-assert; no decode error or mismatch becomes SKIP) + +`stream.rs:8698 run_segmentation_gate` — mandelbrot fixture (varying local contrast so AQ has +something to segment), `--aq-mode=1 --deltaq-mode=0`, 10 attempts, pixel-exact vs ffmpeg over every +decoded frame including hidden alt-refs (`--auto-alt-ref=1 --lag-in-frames=16`), asserting +`segment_id_hits > 0`, `segment_ids_seen >= 2`, and — for multi-frame runs — `segment_pred_hits > 0` +(the `temporal_update` path). + +Command: `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib aq_segmentation -- --nocapture` + +EVIDENCE: crates/ec-av1/src/stream.rs:8908 a_real_aomenc_stream_with_variance_aq_segmentation_decodes_pixel_exact | aomenc --aq-mode=1 --deltaq-mode=0, 16 frames 128x64 8-bit, 10 seeds, decode_stream vs ffmpeg per plane | 10/10 pixel-exact, segment_id_hits=223, distinct segment ids=6, seg_id_predicted symbols=35 +EVIDENCE: crates/ec-av1/src/stream.rs:8941 a_real_aomenc_10bit_stream_with_aq_segmentation_decodes_pixel_exact | same recipe, yuv420p10le, key frame only, 10 seeds | 10/10 pixel-exact, segment_id_hits=80, distinct segment ids=5 +EVIDENCE: cargo test -p ec-av1 --lib (EC_AV1_REQUIRE_AOMENC=1) | full ec-av1 lib suite | 269 passed, 0 failed, 23 ignored, 494s +EVIDENCE: scratchpad reelc.obu / hg.obu (ffmpeg -t 0.4 -c:v copy -f obu) | cargo run -p ec-av1 --example decode_probe | ReelC stops at "a 32x32 partition type this decoder does not code (value=4)", ReelB at "a partition below 8x8" — neither stops on segmentation + +## Residue + +- deferred(sub-16x16 rect/AB partition lanes): an `--aq-mode=2` (complexity AQ) twin. libaom only + enables segmentation for complexity AQ under `--end-usage=vbr` (`aq_complexity.c:is_sb_aq_enabled`, + `sb64_target_rate >= 256`), and every VBR recipe tried makes aomenc pick partition shapes other + lanes still refuse (AB below 16x16, SB-level HORZ/VERT, part32 value 4/6) — 10/10 attempts refuse + before a pixel is compared. Coverage, not capability: complexity AQ sets exactly the same + `SEG_LVL_ALT_Q`/`update_map` syntax variance AQ does. Comment at stream.rs:8919. +- deferred(a 10-bit inter-frame decode defect unrelated to this lane): the 10-bit gate is key-frame + only. Measured: the same recipe with `--aq-mode=0` (segmentation entirely off) also mismatches at + frame 2, so a 16-frame 10-bit variant would gate a pre-existing gap. Comment at stream.rs:8933. +- accepted: `SEG_LVL_REF_FRAME`/`SKIP`/`GLOBALMV` stay refused by name (see above) — no aomenc + `--aq-mode` path writes them, so a gate cannot be built from this oracle today.