From ad1509f56f370ab67f4e686e370268451f231f4e 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 02:47:06 +0300 Subject: [PATCH] fix(av1): replicate truncated true-edge samples before DC averages them dc_predict averaged only the real samples a true-edge-clamped above/left slice held, undercounting against libaom's build_intra_predictors, which replicates the last real sample out to the full side length before dc_predictor runs. That produced a uniform DC offset across whole blocks straddling the true edge (854x480, first bad block mi(8,208): -3 luma across the entire 32x32, repeated in blocks below). Extend both edges to `side` by repetition, matching Edges::build's existing behavior for every other mode, before averaging. --- crates/ec-av1/src/intra.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/ec-av1/src/intra.rs b/crates/ec-av1/src/intra.rs index f3056b89..73978ea3 100644 --- a/crates/ec-av1/src/intra.rs +++ b/crates/ec-av1/src/intra.rs @@ -306,15 +306,30 @@ fn directional(angle: u16, edges: &Edges, side: usize, dst: &mut [u8]) { /// `dc_predict` (spec 7.11.2.5): the average of whichever neighbours exist. /// /// Only the block's own `side` samples of each edge count: what a directional -/// mode reaches past them is no part of the average. +/// mode reaches past them is no part of the average. But `side` samples it +/// always is: libaom's `build_intra_predictors` (`av1/common/reconintra.c`) +/// replicates the last real sample out to the full transform width/height +/// before `dc_predictor` averages, so a slice truncated short by the true +/// frame edge must be extended by repetition here too, not averaged over its +/// own shorter length. fn dc(above: Option<&[u8]>, left: Option<&[u8]>, side: usize) -> i32 { - let above = above.map(|a| &a[..side.min(a.len())]); - let left = left.map(|l| &l[..side.min(l.len())]); + let extend = |samples: &[u8]| -> Vec { + if samples.len() >= side { + samples[..side].to_vec() + } else { + let last = *samples.last().expect("an edge that exists has samples"); + let mut v = samples.to_vec(); + v.resize(side, last); + v + } + }; + let above = above.map(extend); + let left = left.map(extend); let average = |samples: &[u8]| { let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum(); ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32 }; - match (above, left) { + match (&above, &left) { (None, None) => 128, (Some(a), None) => average(a), (None, Some(l)) => average(l),