Merge branch 'lane-perf4' (486d8fd8): loop-restoration source planes cloned only when LR is active, intra DC sums the edge without materialising it, film-grain chroma and MC reconstruction on row slices; byte-exact, 4K 64.9 s to 58.4 s (conflict with r29's superres LR tail resolved: the optional deblocked planes are upscaled when present)
Browse files at this commit3 changed files +118 -70
- crates/ec-av1/src/decode.rs +54 -24
- crates/ec-av1/src/film_grain.rs +45 -29
- crates/ec-av1/src/intra.rs +19 -17
crates/ec-av1/src/decode.rsmodified+54 -24
| @@ -18853,7 +18853,17 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 18853 | 18853 | frame_width as usize, |
| 18854 | 18854 | frame_height as usize, |
| 18855 | 18855 | ); |
| 18856 | - let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone()); | |
| 18856 | + // lane-perf4: loop restoration is the only reader of the pre-CDEF planes, | |
| 18857 | + // and a frame that restores nothing copied every plane twice for nothing | |
| 18858 | + // (three `PlaneBuf` clones here plus the `cdef.to_vec()` inside | |
| 18859 | + // [`crate::restoration::apply_loop_restoration_plane`], which returns its | |
| 18860 | + // input unchanged when the plane's type is `None`). Clone only when some | |
| 18861 | + // plane actually restores. | |
| 18862 | + let lr_active = (0..3).any(|p| { | |
| 18863 | + lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None | |
| 18864 | + && lr.loop_restoration_size[p] != 0 | |
| 18865 | + }); | |
| 18866 | + let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone())); | |
| 18857 | 18867 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own |
| 18858 | 18868 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter |
| 18859 | 18869 | // dump above these bisect WHICH filter stage introduced a mismatch. |
| @@ -18877,8 +18887,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 18877 | 18887 | // boundaries are upscaled (`save_deblock_boundary_lines` runs |
| 18878 | 18888 | // `av1_upscale_normative_rows` over its saved rows when the frame is |
| 18879 | 18889 | // scaled), so every LR coordinate from here on is in upscaled space. |
| 18880 | - let (mut deblocked_y, mut deblocked_u, mut deblocked_v) = | |
| 18881 | - (deblocked_y, deblocked_u, deblocked_v); | |
| 18890 | + let mut deblocked = deblocked; | |
| 18882 | 18891 | let mut lr_w = frame_width as usize; |
| 18883 | 18892 | if let Some((upscaled_w, _)) = superres() { |
| 18884 | 18893 | let bd = u32::from(bit_depth()); |
| @@ -18900,16 +18909,18 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 18900 | 18909 | y = up(&y, fw, fh, uw); |
| 18901 | 18910 | u = up(&u, cw, ch, cuw); |
| 18902 | 18911 | v = up(&v, cw, ch, cuw); |
| 18903 | - deblocked_y = up(&deblocked_y, fw, fh, uw); | |
| 18904 | - deblocked_u = up(&deblocked_u, cw, ch, cuw); | |
| 18905 | - deblocked_v = up(&deblocked_v, cw, ch, cuw); | |
| 18912 | + if let Some((dy, du, dv)) = deblocked.take() { | |
| 18913 | + deblocked = Some((up(&dy, fw, fh, uw), up(&du, cw, ch, cuw), up(&dv, cw, ch, cuw))); | |
| 18914 | + } | |
| 18906 | 18915 | lr_w = uw; |
| 18907 | 18916 | crate::superres::note_upscaled_picture(); |
| 18908 | 18917 | } |
| 18909 | - apply_loop_restoration( | |
| 18910 | - &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid, | |
| 18911 | - (lr_w, frame_height as usize), | |
| 18912 | - ); | |
| 18918 | + if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked { | |
| 18919 | + apply_loop_restoration( | |
| 18920 | + &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid, | |
| 18921 | + (lr_w, frame_height as usize), | |
| 18922 | + ); | |
| 18923 | + } | |
| 18913 | 18924 | |
| 18914 | 18925 | let (fw, fh) = (lr_w, frame_height as usize); |
| 18915 | 18926 | // An upscaled frame is already at its final size with no mi-aligned |
| @@ -19290,11 +19301,19 @@ impl PlaneBuf { | ||
| 19290 | 19301 | // 768..832). libaom reconstructs those rows into the frame buffer's |
| 19291 | 19302 | // border; nothing reads them back, so they are simply dropped here. |
| 19292 | 19303 | let (h, w) = (h.min(self.height.saturating_sub(y)), w.min(self.width.saturating_sub(x))); |
| 19304 | + // lane-perf4: one row of each operand at a time. `side` is the | |
| 19305 | + // prediction/residual stride and `self.width` the plane's, so the | |
| 19306 | + // three row bases are computed once per row instead of a | |
| 19307 | + // multiply-add per sample (the samples themselves are unchanged). | |
| 19308 | + let max = crate::decode::sample_max(); | |
| 19293 | 19309 | for row in 0..h { |
| 19294 | - for col in 0..w { | |
| 19295 | - let sample = (i32::from(prediction[row * side + col]) + residual[row * side + col]) | |
| 19296 | - .clamp(0, crate::decode::sample_max()) as u16; | |
| 19297 | - self.data[(y + row) * self.width + x + col] = sample; | |
| 19310 | + let src = row * side; | |
| 19311 | + let dst = (y + row) * self.width + x; | |
| 19312 | + let pred = &prediction[src..src + w]; | |
| 19313 | + let res = &residual[src..src + w]; | |
| 19314 | + let out = &mut self.data[dst..dst + w]; | |
| 19315 | + for ((o, &p), &r) in out.iter_mut().zip(pred).zip(res) { | |
| 19316 | + *o = (i32::from(p) + r).clamp(0, max) as u16; | |
| 19298 | 19317 | } |
| 19299 | 19318 | } |
| 19300 | 19319 | } |
| @@ -30862,7 +30881,17 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 30862 | 30881 | frame_width as usize, |
| 30863 | 30882 | frame_height as usize, |
| 30864 | 30883 | ); |
| 30865 | - let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone()); | |
| 30884 | + // lane-perf4: loop restoration is the only reader of the pre-CDEF planes, | |
| 30885 | + // and a frame that restores nothing copied every plane twice for nothing | |
| 30886 | + // (three `PlaneBuf` clones here plus the `cdef.to_vec()` inside | |
| 30887 | + // [`crate::restoration::apply_loop_restoration_plane`], which returns its | |
| 30888 | + // input unchanged when the plane's type is `None`). Clone only when some | |
| 30889 | + // plane actually restores. | |
| 30890 | + let lr_active = (0..3).any(|p| { | |
| 30891 | + lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None | |
| 30892 | + && lr.loop_restoration_size[p] != 0 | |
| 30893 | + }); | |
| 30894 | + let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone())); | |
| 30866 | 30895 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own |
| 30867 | 30896 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter |
| 30868 | 30897 | // dump above these bisect WHICH filter stage introduced a mismatch. |
| @@ -30886,8 +30915,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 30886 | 30915 | // boundaries are upscaled (`save_deblock_boundary_lines` runs |
| 30887 | 30916 | // `av1_upscale_normative_rows` over its saved rows when the frame is |
| 30888 | 30917 | // scaled), so every LR coordinate from here on is in upscaled space. |
| 30889 | - let (mut deblocked_y, mut deblocked_u, mut deblocked_v) = | |
| 30890 | - (deblocked_y, deblocked_u, deblocked_v); | |
| 30918 | + let mut deblocked = deblocked; | |
| 30891 | 30919 | let mut lr_w = frame_width as usize; |
| 30892 | 30920 | if let Some((upscaled_w, _)) = superres() { |
| 30893 | 30921 | let bd = u32::from(bit_depth()); |
| @@ -30909,16 +30937,18 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 30909 | 30937 | y = up(&y, fw, fh, uw); |
| 30910 | 30938 | u = up(&u, cw, ch, cuw); |
| 30911 | 30939 | v = up(&v, cw, ch, cuw); |
| 30912 | - deblocked_y = up(&deblocked_y, fw, fh, uw); | |
| 30913 | - deblocked_u = up(&deblocked_u, cw, ch, cuw); | |
| 30914 | - deblocked_v = up(&deblocked_v, cw, ch, cuw); | |
| 30940 | + if let Some((dy, du, dv)) = deblocked.take() { | |
| 30941 | + deblocked = Some((up(&dy, fw, fh, uw), up(&du, cw, ch, cuw), up(&dv, cw, ch, cuw))); | |
| 30942 | + } | |
| 30915 | 30943 | lr_w = uw; |
| 30916 | 30944 | crate::superres::note_upscaled_picture(); |
| 30917 | 30945 | } |
| 30918 | - apply_loop_restoration( | |
| 30919 | - &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid, | |
| 30920 | - (lr_w, frame_height as usize), | |
| 30921 | - ); | |
| 30946 | + if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked { | |
| 30947 | + apply_loop_restoration( | |
| 30948 | + &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid, | |
| 30949 | + (lr_w, frame_height as usize), | |
| 30950 | + ); | |
| 30951 | + } | |
| 30922 | 30952 | |
| 30923 | 30953 | let motion_field = build_motion_field( |
| 30924 | 30954 | &grid, |
crates/ec-av1/src/film_grain.rsmodified+45 -29
| @@ -387,35 +387,51 @@ fn add_noise_to_block( | ||
| 387 | 387 | |
| 388 | 388 | let cy0 = py0 / 2; |
| 389 | 389 | let cx0 = px0 / 2; |
| 390 | - for i in 0..half_h { | |
| 391 | - for j in 0..half_w { | |
| 392 | - let ly = py0 + i * 2; | |
| 393 | - let lx = px0 + j * 2; | |
| 394 | - let l0 = (ly * y_stride + lx) as usize; | |
| 395 | - let average_luma = (picture.y[l0] as i32 + picture.y[l0 + 1] as i32 + 1) >> 1; | |
| 396 | - let cidx = ((cy0 + i) * c_stride + (cx0 + j)) as usize; | |
| 397 | - let gidx = ((cg_row0 + i) * cg_stride + (cg_col0 + j)) as usize; | |
| 398 | - if apply_cb { | |
| 399 | - let scaled = (((average_luma * cb_luma_mult + cb_mult * picture.u[cidx] as i32) | |
| 400 | - >> 6) | |
| 401 | - + cb_offset) | |
| 402 | - .clamp(0, index_max); | |
| 403 | - let delta = | |
| 404 | - (scaling_cb[scaled as usize] * cb_grain[gidx] + rounding_offset) | |
| 405 | - >> scaling_shift; | |
| 406 | - picture.u[cidx] = | |
| 407 | - ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16; | |
| 408 | - } | |
| 409 | - if apply_cr { | |
| 410 | - let scaled = (((average_luma * cr_luma_mult + cr_mult * picture.v[cidx] as i32) | |
| 411 | - >> 6) | |
| 412 | - + cr_offset) | |
| 413 | - .clamp(0, index_max); | |
| 414 | - let delta = | |
| 415 | - (scaling_cr[scaled as usize] * cr_grain[gidx] + rounding_offset) | |
| 416 | - >> scaling_shift; | |
| 417 | - picture.v[cidx] = | |
| 418 | - ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16; | |
| 390 | + if apply_cb || apply_cr { | |
| 391 | + // lane-perf4: one row of each operand at a time. The 4:2:0 luma | |
| 392 | + // average (spec 7.18.3.5) reads a horizontally adjacent PAIR of the | |
| 393 | + // block's own luma row, so `chunks_exact(2)` walks exactly the pairs | |
| 394 | + // the per-sample `(ly * y_stride + lx)` form used to re-derive, and | |
| 395 | + // the U/V/grain rows advance in lockstep with it -- same integers, | |
| 396 | + // same rounding, no per-sample index arithmetic or bounds check. | |
| 397 | + let cw = half_w as usize; | |
| 398 | + // U and V are distinct fields, so a disjoint borrow of each is | |
| 399 | + // needed alongside the immutable read of Y. | |
| 400 | + let Picture { y: ref luma_plane, u: ref mut u_plane, v: ref mut v_plane, .. } = | |
| 401 | + *picture; | |
| 402 | + for i in 0..half_h { | |
| 403 | + let lrow = ((py0 + i * 2) * y_stride + px0) as usize; | |
| 404 | + let crow = ((cy0 + i) * c_stride + cx0) as usize; | |
| 405 | + let grow = ((cg_row0 + i) * cg_stride + cg_col0) as usize; | |
| 406 | + let luma = &luma_plane[lrow..lrow + cw * 2]; | |
| 407 | + let gcb = &cb_grain[grow..grow + cw]; | |
| 408 | + let gcr = &cr_grain[grow..grow + cw]; | |
| 409 | + let urow = &mut u_plane[crow..crow + cw]; | |
| 410 | + let vrow = &mut v_plane[crow..crow + cw]; | |
| 411 | + for ((((lpair, u), v), &gb), &gr) in luma | |
| 412 | + .chunks_exact(2) | |
| 413 | + .zip(urow.iter_mut()) | |
| 414 | + .zip(vrow.iter_mut()) | |
| 415 | + .zip(gcb.iter()) | |
| 416 | + .zip(gcr.iter()) | |
| 417 | + { | |
| 418 | + let average_luma = (lpair[0] as i32 + lpair[1] as i32 + 1) >> 1; | |
| 419 | + if apply_cb { | |
| 420 | + let scaled = (((average_luma * cb_luma_mult + cb_mult * *u as i32) >> 6) | |
| 421 | + + cb_offset) | |
| 422 | + .clamp(0, index_max); | |
| 423 | + let delta = | |
| 424 | + (scaling_cb[scaled as usize] * gb + rounding_offset) >> scaling_shift; | |
| 425 | + *u = ((*u as i32 + delta).clamp(min_chroma, max_chroma)) as u16; | |
| 426 | + } | |
| 427 | + if apply_cr { | |
| 428 | + let scaled = (((average_luma * cr_luma_mult + cr_mult * *v as i32) >> 6) | |
| 429 | + + cr_offset) | |
| 430 | + .clamp(0, index_max); | |
| 431 | + let delta = | |
| 432 | + (scaling_cr[scaled as usize] * gr + rounding_offset) >> scaling_shift; | |
| 433 | + *v = ((*v as i32 + delta).clamp(min_chroma, max_chroma)) as u16; | |
| 434 | + } | |
| 419 | 435 | } |
| 420 | 436 | } |
| 421 | 437 | } |
crates/ec-av1/src/intra.rsmodified+19 -17
| @@ -623,31 +623,33 @@ fn directional( | ||
| 623 | 623 | /// frame edge must be extended by repetition here too, not averaged over its |
| 624 | 624 | /// own shorter length. |
| 625 | 625 | fn dc(above: Option<&[u16]>, left: Option<&[u16]>, bw: usize, bh: usize) -> i32 { |
| 626 | - let extend = |samples: &[u16], want: usize| -> Vec<u16> { | |
| 627 | - if samples.len() >= want { | |
| 628 | - samples[..want].to_vec() | |
| 626 | + // lane-perf4: the replication is only ever *summed*, never indexed, so | |
| 627 | + // the extended edge is a sum rather than the `Vec<u16>` it used to be | |
| 628 | + // built as (two allocations per intra block, 0.9% of a 1080p decode's | |
| 629 | + // self time between them). Same integers, same order of magnitude, no | |
| 630 | + // rounding difference: `sum(samples[..n]) + last * (want - n)`. | |
| 631 | + let extend = |samples: &[u16], want: usize| -> u32 { | |
| 632 | + let n = samples.len().min(want); | |
| 633 | + let sum: u32 = samples[..n].iter().map(|&s| u32::from(s)).sum(); | |
| 634 | + if n < want { | |
| 635 | + let last = u32::from(*samples.last().expect("an edge that exists has samples")); | |
| 636 | + sum + last * (want - n) as u32 | |
| 629 | 637 | } else { |
| 630 | - let last = *samples.last().expect("an edge that exists has samples"); | |
| 631 | - let mut v = samples.to_vec(); | |
| 632 | - v.resize(want, last); | |
| 633 | - v | |
| 638 | + sum | |
| 634 | 639 | } |
| 635 | 640 | }; |
| 636 | 641 | let above = above.map(|a| extend(a, bw)); |
| 637 | 642 | let left = left.map(|l| extend(l, bh)); |
| 638 | - let average = |samples: &[u16]| { | |
| 639 | - let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum(); | |
| 640 | - ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32 | |
| 641 | - }; | |
| 642 | - match (&above, &left) { | |
| 643 | + let average = |sum: u32, count: u32| ((sum + (count >> 1)) / count) as i32; | |
| 644 | + match (above, left) { | |
| 643 | 645 | (None, None) => 1i32 << (crate::decode::bit_depth() - 1), |
| 644 | - (Some(a), None) => average(a), | |
| 645 | - (None, Some(l)) => average(l), | |
| 646 | + (Some(a), None) => average(a, bw as u32), | |
| 647 | + (None, Some(l)) => average(l, bh as u32), | |
| 646 | 648 | (Some(a), Some(l)) if bw == bh => { |
| 647 | 649 | // `dc_predictor` (libaom `intrapred.c`): exact division, the |
| 648 | 650 | // square path this whole lane must leave bit-identical. |
| 649 | - let sum: u32 = a.iter().chain(l).map(|&s| u32::from(s)).sum(); | |
| 650 | - let count = (a.len() + l.len()) as u32; | |
| 651 | + let sum: u32 = a + l; | |
| 652 | + let count = (bw + bh) as u32; | |
| 651 | 653 | ((sum + (count >> 1)) / count) as i32 |
| 652 | 654 | } |
| 653 | 655 | (Some(a), Some(l)) => { |
| @@ -657,7 +659,7 @@ fn dc(above: Option<&[u16]>, left: Option<&[u16]>, bw: usize, bh: usize) -> i32 | ||
| 657 | 659 | // every 1:2 ratio, `d == 5` for every 1:4 one; libaom comments |
| 658 | 660 | // this exact derivation in `intrapred.c` rather than tabulating |
| 659 | 661 | // it, so this ports the derivation, not a lookup). |
| 660 | - let sum: u32 = a.iter().chain(l).map(|&s| u32::from(s)).sum(); | |
| 662 | + let sum: u32 = a + l; | |
| 661 | 663 | let (shift1, multiplier) = dc_rect_multiplier(bw, bh); |
| 662 | 664 | let num = u64::from(sum + ((bw + bh) as u32 >> 1)); |
| 663 | 665 | (((num >> shift1) * multiplier) >> 16) as i32 |