forge
Explore
Log in

Don't you dare go Hollow

Replicate truncated true-edge samples before DC averages them

Browse files at this commit

ad1509f56f370ab67f4e686e370268451f231f4eAhmet Kaan GümüşParent: dc2d88c
  • fix
    • replicate truncated true-edge samples before DC averages them
Show full message
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.rsmodified+19 -4
@@ -306,15 +306,30 @@ fn directional(angle: u16, edges: &Edges, side: usize, dst: &mut [u8]) {
306 /// `dc_predict` (spec 7.11.2.5): the average of whichever neighbours exist.306 /// `dc_predict` (spec 7.11.2.5): the average of whichever neighbours exist.
307 ///307 ///
308 /// Only the block's own `side` samples of each edge count: what a directional308 /// Only the block's own `side` samples of each edge count: what a directional
309-/// mode reaches past them is no part of the average.309+/// mode reaches past them is no part of the average. But `side` samples it
310+/// always is: libaom's `build_intra_predictors` (`av1/common/reconintra.c`)
311+/// replicates the last real sample out to the full transform width/height
312+/// before `dc_predictor` averages, so a slice truncated short by the true
313+/// frame edge must be extended by repetition here too, not averaged over its
314+/// own shorter length.
310 fn dc(above: Option<&[u8]>, left: Option<&[u8]>, side: usize) -> i32 {315 fn dc(above: Option<&[u8]>, left: Option<&[u8]>, side: usize) -> i32 {
311- let above = above.map(|a| &a[..side.min(a.len())]);316+ let extend = |samples: &[u8]| -> Vec<u8> {
312- let left = left.map(|l| &l[..side.min(l.len())]);317+ if samples.len() >= side {
318+ samples[..side].to_vec()
319+ } else {
320+ let last = *samples.last().expect("an edge that exists has samples");
321+ let mut v = samples.to_vec();
322+ v.resize(side, last);
323+ v
324+ }
325+ };
326+ let above = above.map(extend);
327+ let left = left.map(extend);
313 let average = |samples: &[u8]| {328 let average = |samples: &[u8]| {
314 let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum();329 let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum();
315 ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32330 ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32
316 };331 };
317- match (above, left) {332+ match (&above, &left) {
318 (None, None) => 128,333 (None, None) => 128,
319 (Some(a), None) => average(a),334 (Some(a), None) => average(a),
320 (None, Some(l)) => average(l),335 (None, Some(l)) => average(l),