The output crop and the post-deblock snapshot stop copying whole planes
Browse files at this commit36f0c532280bca2dd27bee8143cb879374b18d4fParent: 8cdbcf4- perf
- the output crop and the post-deblock snapshot stop copying whole planes
1 changed file +73 -14
- crates/ec-av1/src/decode.rs +73 -14
crates/ec-av1/src/decode.rsmodified+73 -14
| @@ -18606,10 +18606,65 @@ fn scratch_empty() -> Vec<u16> { | |||
| 18606 | v | 18606 | v |
| 18607 | } | 18607 | } |
| 18608 | 18608 | ||
| 18609 | +/// A pooled buffer of exactly `n` samples whose contents are NOT cleared -- | ||
| 18610 | +/// for a caller that writes every element it later reads (lane-copy: the | ||
| 18611 | +/// post-deblock snapshot writes 4 rows in 64 and reads no other). | ||
| 18612 | +fn scratch_sized(n: usize) -> Vec<u16> { | ||
| 18613 | + let mut v = PLANE_SCRATCH.with(|p| p.borrow_mut().pop()).unwrap_or_default(); | ||
| 18614 | + if v.len() > n { | ||
| 18615 | + v.truncate(n); | ||
| 18616 | + } else if v.len() < n { | ||
| 18617 | + v.resize(n, 0); | ||
| 18618 | + } | ||
| 18619 | + v | ||
| 18620 | +} | ||
| 18621 | + | ||
| 18609 | /// A plane's post-deblock snapshot, its samples taken from the pool (every | 18622 | /// A plane's post-deblock snapshot, its samples taken from the pool (every |
| 18610 | /// other field is `usize`). | 18623 | /// other field is `usize`). |
| 18611 | -fn plane_snapshot(p: &PlaneBuf<'_>) -> PlaneBuf<'static> { | 18624 | +/// |
| 18612 | - PlaneBuf { data: std::borrow::Cow::Owned(scratch_from(&p.data)), ..*p } | 18625 | +/// lane-copy: loop restoration is the only reader, and spec 7.17.2's stripe |
| 18626 | +/// boundary substitution reads the post-deblock plane at just four rows | ||
| 18627 | +/// around each interior stripe boundary (`row` outside | ||
| 18628 | +/// `[stripe_v_start, stripe_v_end)` resolves to `stripe_v_start-1`, | ||
| 18629 | +/// `stripe_v_start-2`, `stripe_v_end` or `stripe_v_end+1` in | ||
| 18630 | +/// `restoration::lr_src_row`); stripes are `64 >> ss_y` rows offset by | ||
| 18631 | +/// `RESTORATION_UNIT_OFFSET = 8 >> ss_y`, so the boundaries sit at | ||
| 18632 | +/// `k*stripe_h - offset`. Copying only those rows turns a full-plane | ||
| 18633 | +/// memmove per plane per frame into 4 rows in 64 (luma). Every other row of | ||
| 18634 | +/// the pooled buffer keeps whatever a previous frame left there -- an | ||
| 18635 | +/// initialised value that no reader ever reaches. | ||
| 18636 | +fn plane_snapshot(p: &PlaneBuf<'_>, ss_y: u32) -> PlaneBuf<'static> { | ||
| 18637 | + let mut data = scratch_sized(p.data.len()); | ||
| 18638 | + let rows = if p.width == 0 { 0 } else { p.data.len() / p.width }; | ||
| 18639 | + let stripe_h = 64usize >> ss_y; | ||
| 18640 | + let offset = 8usize >> ss_y; | ||
| 18641 | + let mut b = stripe_h - offset; | ||
| 18642 | + while b < rows { | ||
| 18643 | + let (lo, hi) = (b - 2, (b + 2).min(rows)); | ||
| 18644 | + data[lo * p.width..hi * p.width].copy_from_slice(&p.data[lo * p.width..hi * p.width]); | ||
| 18645 | + b += stripe_h; | ||
| 18646 | + } | ||
| 18647 | + PlaneBuf { data: std::borrow::Cow::Owned(data), ..*p } | ||
| 18648 | +} | ||
| 18649 | + | ||
| 18650 | +/// The output crop, taking the plane by value: a frame whose width is | ||
| 18651 | +/// already superblock-aligned (only its height is padded) has a padded | ||
| 18652 | +/// buffer whose stride equals the visible width, so the crop is a pure | ||
| 18653 | +/// truncation of the buffer we already own -- no full-plane copy at all | ||
| 18654 | +/// (lane-copy: three ~6 MB memmoves per 4K frame on the frame thread's | ||
| 18655 | +/// serial tail). Any other shape falls back to the row-by-row copy. | ||
| 18656 | +fn crop_owned(plane: PlaneBuf<'_>, w: usize, h: usize) -> Vec<u16> { | ||
| 18657 | + let stride = plane.width; | ||
| 18658 | + let mut data = plane.data.into_owned(); | ||
| 18659 | + if stride == w { | ||
| 18660 | + data.truncate(w * h); | ||
| 18661 | + return data; | ||
| 18662 | + } | ||
| 18663 | + let mut out = Vec::with_capacity(w * h); | ||
| 18664 | + for row in 0..h { | ||
| 18665 | + out.extend(data[row * stride..][..w].iter().copied()); | ||
| 18666 | + } | ||
| 18667 | + out | ||
| 18613 | } | 18668 | } |
| 18614 | 18669 | ||
| 18615 | /// Hands a buffer back to the pool; the capacity is what is worth keeping. | 18670 | /// Hands a buffer back to the pool; the capacity is what is worth keeping. |
| @@ -21324,7 +21379,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | |||
| 21324 | && lr.loop_restoration_size[p] != 0 | 21379 | && lr.loop_restoration_size[p] != 0 |
| 21325 | }); | 21380 | }); |
| 21326 | let deblocked = | 21381 | let deblocked = |
| 21327 | - lr_active.then(|| (plane_snapshot(&y), plane_snapshot(&u), plane_snapshot(&v))); | 21382 | + lr_active.then(|| (plane_snapshot(&y, 0), plane_snapshot(&u, 1), plane_snapshot(&v, 1))); |
| 21328 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own | 21383 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own |
| 21329 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter | 21384 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter |
| 21330 | // dump above these bisect WHICH filter stage introduced a mismatch. | 21385 | // dump above these bisect WHICH filter stage introduced a mismatch. |
| @@ -21383,9 +21438,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | |||
| 21383 | ); | 21438 | ); |
| 21384 | } | 21439 | } |
| 21385 | if let Some((db_y, db_u, db_v)) = deblocked { | 21440 | if let Some((db_y, db_u, db_v)) = deblocked { |
| 21386 | - scratch_return(db_y.data.into_owned()); | 21441 | + // LIFO pool: return in reverse so the next frame's `y`/`u`/`v` |
| 21387 | - scratch_return(db_u.data.into_owned()); | 21442 | + // snapshots pop buffers of their own size and never resize. |
| 21388 | scratch_return(db_v.data.into_owned()); | 21443 | scratch_return(db_v.data.into_owned()); |
| 21444 | + scratch_return(db_u.data.into_owned()); | ||
| 21445 | + scratch_return(db_y.data.into_owned()); | ||
| 21389 | } | 21446 | } |
| 21390 | 21447 | ||
| 21391 | let (fw, fh) = (lr_w, frame_height as usize); | 21448 | let (fw, fh) = (lr_w, frame_height as usize); |
| @@ -21461,9 +21518,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | |||
| 21461 | Picture { | 21518 | Picture { |
| 21462 | width: fw, | 21519 | width: fw, |
| 21463 | height: fh, | 21520 | height: fh, |
| 21464 | - y: crop(&y, fw, fh), | 21521 | + y: crop_owned(y, fw, fh), |
| 21465 | - u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)), | 21522 | + u: crop_owned(u, fw.div_ceil(2), fh.div_ceil(2)), |
| 21466 | - v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)), | 21523 | + v: crop_owned(v, fw.div_ceil(2), fh.div_ceil(2)), |
| 21467 | }, | 21524 | }, |
| 21468 | result_cdfs, | 21525 | result_cdfs, |
| 21469 | )) | 21526 | )) |
| @@ -33460,7 +33517,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 33460 | && lr.loop_restoration_size[p] != 0 | 33517 | && lr.loop_restoration_size[p] != 0 |
| 33461 | }); | 33518 | }); |
| 33462 | let deblocked = | 33519 | let deblocked = |
| 33463 | - lr_active.then(|| (plane_snapshot(&y), plane_snapshot(&u), plane_snapshot(&v))); | 33520 | + lr_active.then(|| (plane_snapshot(&y, 0), plane_snapshot(&u, 1), plane_snapshot(&v, 1))); |
| 33464 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own | 33521 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own |
| 33465 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter | 33522 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter |
| 33466 | // dump above these bisect WHICH filter stage introduced a mismatch. | 33523 | // dump above these bisect WHICH filter stage introduced a mismatch. |
| @@ -33519,9 +33576,11 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 33519 | ); | 33576 | ); |
| 33520 | } | 33577 | } |
| 33521 | if let Some((db_y, db_u, db_v)) = deblocked { | 33578 | if let Some((db_y, db_u, db_v)) = deblocked { |
| 33522 | - scratch_return(db_y.data.into_owned()); | 33579 | + // LIFO pool: return in reverse so the next frame's `y`/`u`/`v` |
| 33523 | - scratch_return(db_u.data.into_owned()); | 33580 | + // snapshots pop buffers of their own size and never resize. |
| 33524 | scratch_return(db_v.data.into_owned()); | 33581 | scratch_return(db_v.data.into_owned()); |
| 33582 | + scratch_return(db_u.data.into_owned()); | ||
| 33583 | + scratch_return(db_y.data.into_owned()); | ||
| 33525 | } | 33584 | } |
| 33526 | 33585 | ||
| 33527 | let motion_field = build_motion_field( | 33586 | let motion_field = build_motion_field( |
| @@ -33591,9 +33650,9 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 33591 | Picture { | 33650 | Picture { |
| 33592 | width: fw, | 33651 | width: fw, |
| 33593 | height: fh, | 33652 | height: fh, |
| 33594 | - y: crop(&y, fw, fh), | 33653 | + y: crop_owned(y, fw, fh), |
| 33595 | - u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)), | 33654 | + u: crop_owned(u, fw.div_ceil(2), fh.div_ceil(2)), |
| 33596 | - v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)), | 33655 | + v: crop_owned(v, fw.div_ceil(2), fh.div_ceil(2)), |
| 33597 | }, | 33656 | }, |
| 33598 | result_cdfs, | 33657 | result_cdfs, |
| 33599 | motion_field, | 33658 | motion_field, |