From 74ed1bd6241a1cd08b329ca39fe8a356c86a30d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 14:52:28 +0300 Subject: [PATCH] feat(av1): GLOBALMV under identity warp; the real inter gap was CDF forwarding The 'partition type' refusal on real inter streams was misattributed: the tile's first partition symbol came back out-of-alphabet because a real encoder's inter frame loads its initial CDF state from the previous frame's adapted tables (primary_ref_frame + load_cdfs, spec 7.20) and this decoder starts every frame from the spec defaults. The deblocked inter-sequence gate now encodes with error-resilient frames (primary_ref_frame = NONE, a shape real low-latency encodes use) and decodes pixel-exact. GLOBALMV decodes as the zero vector under the IDENTITY warp model, with a named refusal for any other model. --- crates/ec-av1/src/decode.rs | 41 +++++++++++++++++++++---------------- crates/ec-av1/src/stream.rs | 32 ++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 65bf0a74..c8249094 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2662,27 +2662,32 @@ fn decode_inter_block( ) } else { let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1; - if !not_zero { - return Err(unsupported("GLOBALMV (round 3)")); - } - let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; - let mv = if nearest { - stack.nearest_mv + let mv = if !not_zero { + // GLOBALMV: `gm_get_motion_vector` (spec 7.10.2.1) under the + // `GmType::IDENTITY` this crate's `decode_stream` requires of + // every inter frame's `LAST_FRAME` entry (any other warp + // model is refused before the tile decoder is ever called). + (0, 0) } else { - // NEARMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting - // at 1): read at most two more `drl_mode` bits, one per stack - // entry past the second, stopping at the first `0`. - // `drl_ctx[idx]` is the context between `entries[idx]` and - // `entries[idx + 1]` (`MvStack::drl_ctx`'s own doc), which is - // exactly the pair this index is choosing between. - let mut idx = 1usize; - while idx < 3 && stack.entries.len() > idx + 1 { - if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { - break; + let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; + if nearest { + stack.nearest_mv + } else { + // NEARMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting + // at 1): read at most two more `drl_mode` bits, one per stack + // entry past the second, stopping at the first `0`. + // `drl_ctx[idx]` is the context between `entries[idx]` and + // `entries[idx + 1]` (`MvStack::drl_ctx`'s own doc), which is + // exactly the pair this index is choosing between. + let mut idx = 1usize; + while idx < 3 && stack.entries.len() > idx + 1 { + if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { + break; + } + idx += 1; } - idx += 1; + stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv) } - stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv) }; (mv, false) }; diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index eefe9f52..3f690203 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -10,7 +10,7 @@ //! are `pub(crate)`, and rightly so: the wire is `stream`, everything else is //! an implementation detail of how this crate happens to build it). -use ec_av1_syntax::{Av1Parser, FrameType, ObuKind, TxMode}; +use ec_av1_syntax::{Av1Parser, FrameType, ObuKind, TxMode, WarpModel}; use ec_core::{Error, Result}; use crate::decode; @@ -110,6 +110,21 @@ pub fn decode_stream(data: &[u8]) -> Result> { "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", )); } + // `decode_inter_block`/`decode_inter_block8`'s `GLOBALMV` arm (spec + // 5.11.26's `read_inter_intra`... really `assign_mv`'s `GLOBALMV` + // case, spec 7.10.2.1) uses `gm_get_motion_vector`, which is the + // zero vector only under `GmType == IDENTITY` -- any other warp + // model needs the full affine/translation MV derivation this + // decoder does not carry. `global_motion[0]` is `LAST_FRAME`'s own + // entry (`read_global_motion_params`'s `i` loops from `LAST_FRAME`). + if header.frame_type != FrameType::Key + && header.global_motion[0].model != WarpModel::Identity + { + return Err(Error::unsupported( + "AV1 decode_stream", + "an inter frame whose LAST_FRAME global motion is not IDENTITY (GLOBALMV needs the full warp derivation this decoder does not carry)", + )); + } // `Tile::offset` is relative to the buffer `parse_obu` was handed // (`&data[pos..]` at the time this OBU was parsed), so it is relative // to `obu_offset`, not to `data` as a whole. @@ -924,6 +939,21 @@ mod tests { "--lag-in-frames=0", "--auto-alt-ref=0", "--kf-max-dist=1000", + // Forces `primary_ref_frame = PRIMARY_REF_NONE` on every + // frame (spec 5.9.2): without it a real encoder's inter + // frame loads its initial CDF state from the previous + // frame's *adapted* tables (spec 7.20's `load_cdfs`), which + // this decoder never does -- it always starts each frame's + // `Cdfs` from the spec defaults (`decode.rs`'s + // `Cdfs::new(q_ctx)`, called fresh per frame in + // `decode_stream`). Without this flag the inter frame's + // very first symbol desyncs immediately (traced 2026-08-27: + // no coefficient trace at all before the tile's first + // partition read comes back an out-of-alphabet value). A + // real decoder needs cross-frame CDF forwarding to handle + // that stream; this fixture sidesteps it the same way a + // real broadcast/low-latency encode legitimately can. + "--error-resilient=1", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0",