Per-block segment_id (spec 5.11.7-5.11.9) with per-segment qindex and loop-filter deltas
Browse files at this commit4950fc399f1dd5ed76ddde74b60c064e072b373cParent: 32f07e4- feat
- per-block segment_id (spec 5.11.7-5.11.9) with per-segment qindex and loop-filter deltas
3 changed files +91 -3
- crates/ec-av1/src/decode.rs +10 -1
- crates/ec-av1/src/stream.rs +14 -2
- lanes/seg-r1.report.md +67 -0
crates/ec-av1/src/decode.rsmodified+10 -1
| @@ -350,6 +350,8 @@ thread_local! { | ||
| 350 | 350 | /// ids any block ended up with -- the gate's proof the feature fired. |
| 351 | 351 | static SEG_ID_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 352 | 352 | static SEG_IDS_SEEN: std::cell::Cell<u8> = const { std::cell::Cell::new(0) }; |
| 353 | + /// How many `seg_id_predicted` symbols were read (temporal update). | |
| 354 | + static SEG_PRED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | |
| 353 | 355 | } |
| 354 | 356 | |
| 355 | 357 | /// Installs this frame's segmentation parameters and allocates its map, with |
| @@ -395,10 +397,16 @@ pub(crate) fn segment_ids_seen() -> usize { | ||
| 395 | 397 | SEG_IDS_SEEN.with(|c| c.get().count_ones() as usize) |
| 396 | 398 | } |
| 397 | 399 | |
| 398 | -/// Resets both segmentation hit counters (gate setup). | |
| 400 | +/// How many `seg_id_predicted` symbols have been read (temporal update). | |
| 401 | +pub(crate) fn segment_pred_hits() -> usize { | |
| 402 | + SEG_PRED_HITS.with(|c| c.get()) | |
| 403 | +} | |
| 404 | + | |
| 405 | +/// Resets the segmentation hit counters (gate setup). | |
| 399 | 406 | pub(crate) fn reset_segment_hits() { |
| 400 | 407 | SEG_ID_HITS.with(|c| c.set(0)); |
| 401 | 408 | SEG_IDS_SEEN.with(|c| c.set(0)); |
| 409 | + SEG_PRED_HITS.with(|c| c.set(0)); | |
| 402 | 410 | } |
| 403 | 411 | |
| 404 | 412 | /// `seg_feature_active_idx(segment_id, feature)` for the frame currently |
| @@ -603,6 +611,7 @@ fn inter_segment_id( | ||
| 603 | 611 | let id = if temporal_update { |
| 604 | 612 | let ctx = seg_pred_ctx(mi_r, mi_c); |
| 605 | 613 | let predicted = dec.symbol(&mut cdfs.segment_pred[ctx]) != 0; |
| 614 | + SEG_PRED_HITS.with(|c| c.set(c.get() + 1)); | |
| 606 | 615 | stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, u8::from(predicted)); |
| 607 | 616 | if predicted { |
| 608 | 617 | predicted_segment_id(mi_r, mi_c, w_mi, h_mi) |
crates/ec-av1/src/stream.rsmodified+14 -2
| @@ -8896,11 +8896,23 @@ mod tests { | ||
| 8896 | 8896 | every block landed in one segment, so nothing was actually segmented", |
| 8897 | 8897 | crate::decode::segment_ids_seen() |
| 8898 | 8898 | ); |
| 8899 | + // An inter-frame run must also have exercised the TEMPORAL path | |
| 8900 | + // (`seg_id_predicted` against the primary reference's saved map); | |
| 8901 | + // the key-frame-only run has no previous map by construction. | |
| 8902 | + if frame_count > 1 { | |
| 8903 | + assert!( | |
| 8904 | + crate::decode::segment_pred_hits() > 0, | |
| 8905 | + "{name}: {matched} pixel-exact matches over {frame_count} frames but zero \ | |
| 8906 | + seg_id_predicted symbols -- the temporal_update path is unexercised" | |
| 8907 | + ); | |
| 8908 | + } | |
| 8899 | 8909 | eprintln!( |
| 8900 | 8910 | "{name}: {named_refusals} other-capability refusals, {matched} pixel-exact matches \ |
| 8901 | - out of {n_attempts}, segment_id_hits={}, distinct segment ids={}", | |
| 8911 | + out of {n_attempts}, segment_id_hits={}, distinct segment ids={}, \ | |
| 8912 | + seg_id_predicted symbols={}", | |
| 8902 | 8913 | crate::decode::segment_id_hits(), |
| 8903 | - crate::decode::segment_ids_seen() | |
| 8914 | + crate::decode::segment_ids_seen(), | |
| 8915 | + crate::decode::segment_pred_hits() | |
| 8904 | 8916 | ); |
| 8905 | 8917 | } |
| 8906 | 8918 |
lanes/seg-r1.report.mdadded+67 -0
| @@ -0,0 +1,67 @@ | ||
| 1 | +# lane-seg r1 — per-block `segment_id` and its consumers | |
| 2 | + | |
| 3 | +Branch `lane-seg`, off main `3808cf8` (WIP snapshot `51581d3` continued, not restarted). | |
| 4 | + | |
| 5 | +## What changed | |
| 6 | + | |
| 7 | +- `crates/ec-av1/src/cdf.rs:564` — `SEGMENT_ID` (`default_spatial_pred_seg_tree_cdf`, 3 ctx x 8 symbols) | |
| 8 | + and `SEGMENT_PRED` (`seg_id_predicted`, 3 ctx) default CDFs. | |
| 9 | +- `crates/ec-av1/src/cdf_state.rs:467` — both tables in `Cdfs`, in `reset_counters` (class | |
| 10 | + `cdf-counter-not-reset`) and in the default-table constructor, so they adapt and reset like every | |
| 11 | + other per-tile table. | |
| 12 | +- `crates/ec-av1/src/decode.rs:319-655` — the reader: `set_segmentation` (per-frame install + | |
| 13 | + `load_previous_segment_ids`), `CurrentSegmentIds`/`PrevSegmentIds` maps, `AboveSegPredContext`/ | |
| 14 | + `LeftSegPredContext`, `av1_neg_deinterleave`, `av1_get_spatial_seg_pred` (ctx + prediction), | |
| 15 | + `read_segment_id`, `intra_segment_id` (spec 5.11.7) and `inter_segment_id` (5.11.9, both | |
| 16 | + pre-skip and post-skip positions, `seg_id_predicted` under `temporal_update`, inherited map when | |
| 17 | + `update_map == 0`), `set_segment_id` stamping the block's whole mi footprint. | |
| 18 | +- Call sites: `read_intra_mode` (decode.rs:4375) and `read_intra_mode_rect` (3332) read | |
| 19 | + `intra_segment_id` before `skip` when `SegIdPreSkip`, after it otherwise; | |
| 20 | + `decode_inter_block` (9250/9268) and `decode_inter_block8` (11106/11116) read both spec positions. | |
| 21 | +- Consumers: `block_q_idx()` (decode.rs:400, spec 7.12.2 `get_qindex` with `SEG_LVL_ALT_Q`) replaces | |
| 22 | + the raw `CURRENT_Q_IDX` read at all 10 dequant sites; `lf_level` (decode.rs:6168) takes the block's | |
| 23 | + segment id and applies `SEG_LVL_ALT_LF_Y_V/H`/`_U`/`_V` (spec 7.14.4) between the `DeltaLF` term and | |
| 24 | + the ref/mode deltas; `edge_params` passes the per-side segment ids. | |
| 25 | +- `crates/ec-av1/src/stream.rs:266-291` — frame-level: narrowed refusal (below), spec | |
| 26 | + `load_previous_segment_ids` from the primary reference's slot, `set_segmentation` per frame; | |
| 27 | + `stream.rs:736` stores the decoded map into every slot `refresh_frame_flags` names. | |
| 28 | +- `crates/ec-av1/src/refusal_inventory.rs:59` — refusal replaced. | |
| 29 | + | |
| 30 | +## Refusal | |
| 31 | + | |
| 32 | +LIFTED: `"a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)"`. | |
| 33 | +REPLACED BY (narrower): `"a frame whose segmentation enables SEG_LVL_REF_FRAME/SKIP/GLOBALMV (this | |
| 34 | +decoder reads segment_id but never lets a segment override a block's reference, skip or mode)"` — | |
| 35 | +those three features rewrite mode/reference decisions and suppress symbols `decode_inter_block` reads | |
| 36 | +unconditionally; aomenc's `--aq-mode` only ever sets `SEG_LVL_ALT_Q`, so no gate here can exercise | |
| 37 | +them. `gate_coverage.rs` needed no edit: it derives coverage from `--enable-*` flags and segmentation | |
| 38 | +is driven by `--aq-mode`, not an `--enable-*` flag; its two tests stay green. | |
| 39 | + | |
| 40 | +## Gates (both hard-assert; no decode error or mismatch becomes SKIP) | |
| 41 | + | |
| 42 | +`stream.rs:8698 run_segmentation_gate` — mandelbrot fixture (varying local contrast so AQ has | |
| 43 | +something to segment), `--aq-mode=1 --deltaq-mode=0`, 10 attempts, pixel-exact vs ffmpeg over every | |
| 44 | +decoded frame including hidden alt-refs (`--auto-alt-ref=1 --lag-in-frames=16`), asserting | |
| 45 | +`segment_id_hits > 0`, `segment_ids_seen >= 2`, and — for multi-frame runs — `segment_pred_hits > 0` | |
| 46 | +(the `temporal_update` path). | |
| 47 | + | |
| 48 | +Command: `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib aq_segmentation -- --nocapture` | |
| 49 | + | |
| 50 | +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 | |
| 51 | +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 | |
| 52 | +EVIDENCE: cargo test -p ec-av1 --lib (EC_AV1_REQUIRE_AOMENC=1) | full ec-av1 lib suite | 269 passed, 0 failed, 23 ignored, 494s | |
| 53 | +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 | |
| 54 | + | |
| 55 | +## Residue | |
| 56 | + | |
| 57 | +- deferred(sub-16x16 rect/AB partition lanes): an `--aq-mode=2` (complexity AQ) twin. libaom only | |
| 58 | + enables segmentation for complexity AQ under `--end-usage=vbr` (`aq_complexity.c:is_sb_aq_enabled`, | |
| 59 | + `sb64_target_rate >= 256`), and every VBR recipe tried makes aomenc pick partition shapes other | |
| 60 | + lanes still refuse (AB below 16x16, SB-level HORZ/VERT, part32 value 4/6) — 10/10 attempts refuse | |
| 61 | + before a pixel is compared. Coverage, not capability: complexity AQ sets exactly the same | |
| 62 | + `SEG_LVL_ALT_Q`/`update_map` syntax variance AQ does. Comment at stream.rs:8919. | |
| 63 | +- deferred(a 10-bit inter-frame decode defect unrelated to this lane): the 10-bit gate is key-frame | |
| 64 | + only. Measured: the same recipe with `--aq-mode=0` (segmentation entirely off) also mismatches at | |
| 65 | + frame 2, so a 16-frame 10-bit variant would gate a pre-existing gap. Comment at stream.rs:8933. | |
| 66 | +- accepted: `SEG_LVL_REF_FRAME`/`SKIP`/`GLOBALMV` stay refused by name (see above) — no aomenc | |
| 67 | + `--aq-mode` path writes them, so a gate cannot be built from this oracle today. |