Merge branch 'lane-wave1' (22e1ab44): dequantisation and inverse transform run on the reconstruction workers from recorded coefficients; Mc ops carry only their transform-unit window; byte-exact at recon 1, 4 and the composed setting; 4K 21.3 fps at (6,2,4)
Browse files at this commit1 changed file +160 -32
- crates/ec-av1/src/decode.rs +160 -32
crates/ec-av1/src/decode.rsmodified+160 -32
| @@ -204,7 +204,7 @@ use crate::mvstack::{ | |||
| 204 | single_ref_p6_ctx, uni_comp_ref_p1_ctx, | 204 | single_ref_p6_ctx, uni_comp_ref_p1_ctx, |
| 205 | }; | 205 | }; |
| 206 | use crate::tile::{INTRA_MODE_CTX, block_grid, has_half}; | 206 | use crate::tile::{INTRA_MODE_CTX, block_grid, has_half}; |
| 207 | -use crate::transform::{TxType, dequant_and_inverse_typed, dequant_and_inverse_typed_wh}; | 207 | +use crate::transform::{TxType, dequant_and_inverse_typed_wh}; |
| 208 | 208 | ||
| 209 | const PARTITION_NONE: usize = 0; | 209 | const PARTITION_NONE: usize = 0; |
| 210 | const PARTITION_HORZ: usize = 1; | 210 | const PARTITION_HORZ: usize = 1; |
| @@ -2057,6 +2057,61 @@ fn cfl_src_rect(px: usize, py: usize, bw: usize, bh: usize) -> CflSrc { | |||
| 2057 | /// entropy parsing no longer touches the frame's own planes (except the two | 2057 | /// entropy parsing no longer touches the frame's own planes (except the two |
| 2058 | /// sites named in [`flush_recon`]'s doc), which is what lets step (ii) run | 2058 | /// sites named in [`flush_recon`]'s doc), which is what lets step (ii) run |
| 2059 | /// the replay on a superblock-row wavefront worker. | 2059 | /// the replay on a superblock-row wavefront worker. |
| 2060 | +/// The dequantization + inverse transform of one transform unit, kept as its | ||
| 2061 | +/// INPUTS so that a recorded write carries the work instead of its result | ||
| 2062 | +/// (lane-wave3): at `EC_AV1_RECON_THREADS > 1` the transform then runs on the | ||
| 2063 | +/// recon worker, not on the parsing thread. `stride == 0` means the dense | ||
| 2064 | +/// `w x h` residual; otherwise it is re-laid into the `stride x stride` grid | ||
| 2065 | +/// `reconstruct_mc_rect` reads both of its buffers at. | ||
| 2066 | +#[derive(Clone, Copy)] | ||
| 2067 | +pub(crate) struct TxParams { | ||
| 2068 | + pub(crate) w: usize, | ||
| 2069 | + pub(crate) h: usize, | ||
| 2070 | + pub(crate) bit_depth: u8, | ||
| 2071 | + pub(crate) q_idx: i32, | ||
| 2072 | + pub(crate) dc_delta: i32, | ||
| 2073 | + pub(crate) ac_delta: i32, | ||
| 2074 | + pub(crate) tx_type: TxType, | ||
| 2075 | + pub(crate) stride: usize, | ||
| 2076 | +} | ||
| 2077 | + | ||
| 2078 | +impl TxParams { | ||
| 2079 | + /// The one copy of the produce-the-residual code: the inline path calls it | ||
| 2080 | + /// on the parse thread, the wavefront worker calls it on its own, so both | ||
| 2081 | + /// are the same function on the same inputs and byte-identical. | ||
| 2082 | + fn run(self, grid: &[i32]) -> Vec<i32> { | ||
| 2083 | + let residual = dequant_and_inverse_typed_wh( | ||
| 2084 | + grid, self.w, self.h, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta, | ||
| 2085 | + self.tx_type, | ||
| 2086 | + ); | ||
| 2087 | + if self.stride == 0 { | ||
| 2088 | + return residual; | ||
| 2089 | + } | ||
| 2090 | + let mut strided = vec![0i32; self.stride * self.stride]; | ||
| 2091 | + for row in 0..self.h { | ||
| 2092 | + strided[row * self.stride..][..self.w] | ||
| 2093 | + .copy_from_slice(&residual[row * self.w..][..self.w]); | ||
| 2094 | + } | ||
| 2095 | + strided | ||
| 2096 | + } | ||
| 2097 | +} | ||
| 2098 | + | ||
| 2099 | +/// A recorded write's residual: already transformed, or the coefficients and | ||
| 2100 | +/// [`TxParams`] the worker transforms itself. | ||
| 2101 | +pub(crate) enum Residual { | ||
| 2102 | + Done(Vec<i32>), | ||
| 2103 | + Coeffs(TxParams, Vec<i32>), | ||
| 2104 | +} | ||
| 2105 | + | ||
| 2106 | +impl Residual { | ||
| 2107 | + fn resolve(self) -> Vec<i32> { | ||
| 2108 | + match self { | ||
| 2109 | + Self::Done(v) => v, | ||
| 2110 | + Self::Coeffs(p, grid) => p.run(&grid), | ||
| 2111 | + } | ||
| 2112 | + } | ||
| 2113 | +} | ||
| 2114 | + | ||
| 2060 | pub(crate) enum ReconOp { | 2115 | pub(crate) enum ReconOp { |
| 2061 | Intra { | 2116 | Intra { |
| 2062 | plane: usize, | 2117 | plane: usize, |
| @@ -2070,7 +2125,7 @@ pub(crate) enum ReconOp { | |||
| 2070 | mode: usize, | 2125 | mode: usize, |
| 2071 | angle_delta: i32, | 2126 | angle_delta: i32, |
| 2072 | reach: Reach, | 2127 | reach: Reach, |
| 2073 | - residual: Vec<i32>, | 2128 | + residual: Residual, |
| 2074 | cfl: Option<(i32, CflSrc)>, | 2129 | cfl: Option<(i32, CflSrc)>, |
| 2075 | filter_intra: Option<usize>, | 2130 | filter_intra: Option<usize>, |
| 2076 | smooth_neighbor: bool, | 2131 | smooth_neighbor: bool, |
| @@ -2087,7 +2142,7 @@ pub(crate) enum ReconOp { | |||
| 2087 | w: usize, | 2142 | w: usize, |
| 2088 | h: usize, | 2143 | h: usize, |
| 2089 | prediction: Vec<u16>, | 2144 | prediction: Vec<u16>, |
| 2090 | - residual: Vec<i32>, | 2145 | + residual: Residual, |
| 2091 | }, | 2146 | }, |
| 2092 | } | 2147 | } |
| 2093 | 2148 | ||
| @@ -2423,7 +2478,7 @@ fn push_intra( | |||
| 2423 | let pred = take_palette_pred(fctx); | 2478 | let pred = take_palette_pred(fctx); |
| 2424 | push_op(fctx, ReconOp::Intra { | 2479 | push_op(fctx, ReconOp::Intra { |
| 2425 | plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach, | 2480 | plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach, |
| 2426 | - residual: residual.to_vec(), cfl, filter_intra, smooth_neighbor, pred, | 2481 | + residual: Residual::Done(residual.to_vec()), cfl, filter_intra, smooth_neighbor, pred, |
| 2427 | }); | 2482 | }); |
| 2428 | } | 2483 | } |
| 2429 | 2484 | ||
| @@ -2452,7 +2507,7 @@ fn push_intra_owned( | |||
| 2452 | let pred = take_palette_pred(fctx); | 2507 | let pred = take_palette_pred(fctx); |
| 2453 | push_op(fctx, ReconOp::Intra { | 2508 | push_op(fctx, ReconOp::Intra { |
| 2454 | plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach, | 2509 | plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach, |
| 2455 | - residual, cfl, filter_intra, smooth_neighbor, pred, | 2510 | + residual: Residual::Done(residual), cfl, filter_intra, smooth_neighbor, pred, |
| 2456 | }); | 2511 | }); |
| 2457 | } | 2512 | } |
| 2458 | 2513 | ||
| @@ -2472,7 +2527,8 @@ fn push_mc_rect_owned( | |||
| 2472 | return; | 2527 | return; |
| 2473 | } | 2528 | } |
| 2474 | push_op(fctx, ReconOp::Mc { | 2529 | push_op(fctx, ReconOp::Mc { |
| 2475 | - plane, x, y, stride, w, h, prediction: prediction.to_vec(), residual, | 2530 | + plane, x, y, stride, w, h, prediction: prediction.to_vec(), |
| 2531 | + residual: Residual::Done(residual), | ||
| 2476 | }); | 2532 | }); |
| 2477 | } | 2533 | } |
| 2478 | 2534 | ||
| @@ -2501,7 +2557,7 @@ fn push_intra_rect( | |||
| 2501 | let pred = take_palette_pred(fctx); | 2557 | let pred = take_palette_pred(fctx); |
| 2502 | push_op(fctx, ReconOp::Intra { | 2558 | push_op(fctx, ReconOp::Intra { |
| 2503 | plane, x, y, bw, bh, rect: true, mode, angle_delta, reach, | 2559 | plane, x, y, bw, bh, rect: true, mode, angle_delta, reach, |
| 2504 | - residual: residual.to_vec(), cfl, filter_intra, smooth_neighbor, pred, | 2560 | + residual: Residual::Done(residual.to_vec()), cfl, filter_intra, smooth_neighbor, pred, |
| 2505 | }); | 2561 | }); |
| 2506 | } | 2562 | } |
| 2507 | 2563 | ||
| @@ -2534,7 +2590,78 @@ fn push_mc_rect( | |||
| 2534 | } | 2590 | } |
| 2535 | push_op(fctx, ReconOp::Mc { | 2591 | push_op(fctx, ReconOp::Mc { |
| 2536 | plane, x, y, stride, w, h, | 2592 | plane, x, y, stride, w, h, |
| 2537 | - prediction: prediction.to_vec(), residual: residual.to_vec(), | 2593 | + prediction: prediction.to_vec(), residual: Residual::Done(residual.to_vec()), |
| 2594 | + }); | ||
| 2595 | +} | ||
| 2596 | + | ||
| 2597 | +/// [`push_intra_rect`] carrying the transform's INPUTS: at one reconstruction | ||
| 2598 | +/// thread the transform runs here, exactly as before; on the wavefront it | ||
| 2599 | +/// travels with the op and runs on the worker (lane-wave3). | ||
| 2600 | +#[allow(clippy::too_many_arguments)] | ||
| 2601 | +fn push_intra_tx( | ||
| 2602 | + plane: usize, | ||
| 2603 | + x: usize, | ||
| 2604 | + y: usize, | ||
| 2605 | + bw: usize, | ||
| 2606 | + bh: usize, | ||
| 2607 | + rect: bool, | ||
| 2608 | + mode: usize, | ||
| 2609 | + angle_delta: i32, | ||
| 2610 | + reach: Reach, | ||
| 2611 | + tx: TxParams, | ||
| 2612 | + grid: &[i32], | ||
| 2613 | + cfl: Option<(i32, CflSrc)>, | ||
| 2614 | + filter_intra: Option<usize>, | ||
| 2615 | + smooth_neighbor: bool, | ||
| 2616 | + fctx: &crate::decode::FrameCtx, | ||
| 2617 | +) { | ||
| 2618 | + if fctx.recon_planes.with(std::cell::Cell::get).is_some() { | ||
| 2619 | + inline_intra( | ||
| 2620 | + plane, x, y, bw, bh, rect, mode, angle_delta, reach, &tx.run(grid), cfl, filter_intra, | ||
| 2621 | + smooth_neighbor, fctx, | ||
| 2622 | + ); | ||
| 2623 | + return; | ||
| 2624 | + } | ||
| 2625 | + let pred = take_palette_pred(fctx); | ||
| 2626 | + push_op(fctx, ReconOp::Intra { | ||
| 2627 | + plane, x, y, bw, bh, rect, mode, angle_delta, reach, | ||
| 2628 | + residual: Residual::Coeffs(tx, grid.to_vec()), cfl, filter_intra, smooth_neighbor, pred, | ||
| 2629 | + }); | ||
| 2630 | +} | ||
| 2631 | + | ||
| 2632 | +/// [`push_mc_rect`] carrying the transform's inputs -- see [`push_intra_tx`]. | ||
| 2633 | +#[allow(clippy::too_many_arguments)] | ||
| 2634 | +fn push_mc_rect_tx( | ||
| 2635 | + plane: usize, | ||
| 2636 | + x: usize, | ||
| 2637 | + y: usize, | ||
| 2638 | + stride: usize, | ||
| 2639 | + w: usize, | ||
| 2640 | + h: usize, | ||
| 2641 | + prediction: &[u16], | ||
| 2642 | + tx: TxParams, | ||
| 2643 | + grid: &[i32], | ||
| 2644 | + fctx: &crate::decode::FrameCtx, | ||
| 2645 | +) { | ||
| 2646 | + if fctx.recon_planes.with(std::cell::Cell::get).is_some() { | ||
| 2647 | + inline_mc(plane, x, y, stride, w, h, prediction, &tx.run(grid), fctx); | ||
| 2648 | + return; | ||
| 2649 | + } | ||
| 2650 | + // `reconstruct_mc_rect` reads exactly rows `0..h`, columns `0..w` of the | ||
| 2651 | + // two buffers at `stride`, so the record carries that window densely | ||
| 2652 | + // rather than the whole block's prediction: a 4x4 unit of a 64x64 block | ||
| 2653 | + // copied 64*64 samples on the PARSING thread before. The residual is then | ||
| 2654 | + // produced dense too (`TxParams::stride = 0`), which is the same numbers | ||
| 2655 | + // in the same places at stride `w`. | ||
| 2656 | + let mut window = Vec::with_capacity(w * h); | ||
| 2657 | + for row in 0..h { | ||
| 2658 | + window.extend_from_slice(&prediction[row * stride..][..w]); | ||
| 2659 | + } | ||
| 2660 | + let mut tx = tx; | ||
| 2661 | + tx.stride = 0; | ||
| 2662 | + push_op(fctx, ReconOp::Mc { | ||
| 2663 | + plane, x, y, stride: w, w, h, prediction: window, | ||
| 2664 | + residual: Residual::Coeffs(tx, grid.to_vec()), | ||
| 2538 | }); | 2665 | }); |
| 2539 | } | 2666 | } |
| 2540 | 2667 | ||
| @@ -2577,11 +2704,11 @@ fn exec_op( | |||
| 2577 | plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl, | 2704 | plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl, |
| 2578 | filter_intra, smooth_neighbor, pred, | 2705 | filter_intra, smooth_neighbor, pred, |
| 2579 | } => exec_intra( | 2706 | } => exec_intra( |
| 2580 | - y, u, v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, &residual, cfl, | 2707 | + y, u, v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, &residual.resolve(), |
| 2581 | - filter_intra, smooth_neighbor, pred, fctx, | 2708 | + cfl, filter_intra, smooth_neighbor, pred, fctx, |
| 2582 | ), | 2709 | ), |
| 2583 | ReconOp::Mc { plane, x, y: oy, stride, w, h, prediction, residual } => { | 2710 | ReconOp::Mc { plane, x, y: oy, stride, w, h, prediction, residual } => { |
| 2584 | - exec_mc(y, u, v, plane, x, oy, stride, w, h, &prediction, &residual, fctx); | 2711 | + exec_mc(y, u, v, plane, x, oy, stride, w, h, &prediction, &residual.resolve(), fctx); |
| 2585 | } | 2712 | } |
| 2586 | } | 2713 | } |
| 2587 | } | 2714 | } |
| @@ -12672,21 +12799,28 @@ fn read_plane( | |||
| 12672 | full | 12799 | full |
| 12673 | }; | 12800 | }; |
| 12674 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); | 12801 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); |
| 12675 | - let residual = dequant_and_inverse_typed(&levels, side, bit_depth(fctx), block_q_idx(fctx), dc_delta, ac_delta, tx_type); | 12802 | + let tx = TxParams { |
| 12803 | + w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, | ||
| 12804 | + ac_delta, tx_type, stride: 0, | ||
| 12805 | + }; | ||
| 12676 | if crate::envflags::env_flag!("EC_AV1_TRACE") { | 12806 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 12807 | + let residual = tx.run(&levels); | ||
| 12677 | eprintln!( | 12808 | eprintln!( |
| 12678 | "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}", | 12809 | "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}", |
| 12679 | ); | 12810 | ); |
| 12680 | } | 12811 | } |
| 12681 | - push_intra_owned( | 12812 | + push_intra_tx( |
| 12682 | plane_idx, | 12813 | plane_idx, |
| 12683 | x, | 12814 | x, |
| 12684 | y, | 12815 | y, |
| 12685 | side, | 12816 | side, |
| 12817 | + side, | ||
| 12818 | + false, | ||
| 12686 | predict_mode, | 12819 | predict_mode, |
| 12687 | angle_delta, | 12820 | angle_delta, |
| 12688 | reach, | 12821 | reach, |
| 12689 | - residual, | 12822 | + tx, |
| 12823 | + &levels, | ||
| 12690 | cfl, | 12824 | cfl, |
| 12691 | filter_intra, | 12825 | filter_intra, |
| 12692 | smooth_neighbor, fctx, | 12826 | smooth_neighbor, fctx, |
| @@ -16877,26 +17011,17 @@ fn read_inter_plane_rect( | |||
| 16877 | full | 17011 | full |
| 16878 | }; | 17012 | }; |
| 16879 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); | 17013 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); |
| 16880 | - let residual = dequant_and_inverse_typed_wh( | ||
| 16881 | - &grid, | ||
| 16882 | - w, | ||
| 16883 | - h, | ||
| 16884 | - bit_depth(fctx), | ||
| 16885 | - block_q_idx(fctx), | ||
| 16886 | - dc_delta, | ||
| 16887 | - ac_delta, | ||
| 16888 | - tx_type, | ||
| 16889 | - ); | ||
| 16890 | if crate::envflags::env_flag!("EC_TRACE_TXTYPE") { | 17014 | if crate::envflags::env_flag!("EC_TRACE_TXTYPE") { |
| 16891 | eprintln!("OUR_TXTYPE plane={plane_idx} x={x} y={y} w={w} h={h} tx_type={tx_type:?} inh={inherited_luma_tx_type:?}"); | 17015 | eprintln!("OUR_TXTYPE plane={plane_idx} x={x} y={y} w={w} h={h} tx_type={tx_type:?} inh={inherited_luma_tx_type:?}"); |
| 16892 | } | 17016 | } |
| 16893 | // `reconstruct_mc_rect` reads both buffers at the square prediction | 17017 | // `reconstruct_mc_rect` reads both buffers at the square prediction |
| 16894 | - // stride, so the dense `w x h` residual is re-laid at that stride first. | 17018 | + // stride, so the dense `w x h` residual is re-laid at that stride |
| 16895 | - let mut strided = vec![0i32; stride * stride]; | 17019 | + // (`TxParams::stride`) after the inverse transform. |
| 16896 | - for row in 0..h { | 17020 | + let tx = TxParams { |
| 16897 | - strided[row * stride..][..w].copy_from_slice(&residual[row * w..][..w]); | 17021 | + w, h, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, ac_delta, tx_type, |
| 16898 | - } | 17022 | + stride, |
| 16899 | - push_mc_rect_owned(plane_idx, x, y, stride, w, h, prediction, strided, fctx); | 17023 | + }; |
| 17024 | + push_mc_rect_tx(plane_idx, x, y, stride, w, h, prediction, tx, &grid, fctx); | ||
| 16900 | Ok((grid, tx_type)) | 17025 | Ok((grid, tx_type)) |
| 16901 | } | 17026 | } |
| 16902 | 17027 | ||
| @@ -21465,8 +21590,11 @@ fn read_inter_plane( | |||
| 21465 | full | 21590 | full |
| 21466 | }; | 21591 | }; |
| 21467 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); | 21592 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); |
| 21468 | - let residual = dequant_and_inverse_typed(&grid, side, bit_depth(fctx), block_q_idx(fctx), dc_delta, ac_delta, tx_type); | 21593 | + let tx = TxParams { |
| 21469 | - push_mc_rect_owned(plane_idx, x, y, side, side, side, prediction, residual, fctx); | 21594 | + w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, |
| 21595 | + ac_delta, tx_type, stride: 0, | ||
| 21596 | + }; | ||
| 21597 | + push_mc_rect_tx(plane_idx, x, y, side, side, side, prediction, tx, &grid, fctx); | ||
| 21470 | Ok((grid, tx_type)) | 21598 | Ok((grid, tx_type)) |
| 21471 | } | 21599 | } |
| 21472 | 21600 | ||