forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

replay.rs613 lines, 26.0 KiBHistoryRawDownload
1//! Filter replay and CDEF band helpers: the replay_* family that re-runs the loop filters over a captured picture, and cdef_band. Moved out of `decode/mod.rs` (av1split D11); the items are unchanged apart from visibility.
2
3use super::*;
4
5/// The filter half of a captured decode, under new parameters: exactly the
6/// [`apply_deblock`] / [`apply_cdef`] pair the decoder's own tail runs, on a
7/// copy of the captured planes. `None` when nothing is captured (the caller
8/// then decodes for real).
9pub(crate) fn replay_filters(
10 loop_filter: &LoopFilterParams,
11 cdef: &CdefParams,
12 fctx: &FrameCtx,
13) -> Option<Picture> {
14 #[cfg(test)]
15 if REPLAY_DISABLED.load(std::sync::atomic::Ordering::Relaxed) {
16 return None;
17 }
18 replay_inner(loop_filter, cdef, None, fctx).map(|(picture, _)| picture)
19}
20
21/// lane-av1cap: the filter stage's LAST replay -- the WINNING parameters,
22/// under the per-64x64 `cdef_idx` grid the re-coded tile carries. The stage
23/// used to reach this picture by decoding that re-coded tile a second time
24/// (10% of a 4K frame's encode), only because the loop-restoration search
25/// needs the decoder's own post-CDEF plane
26/// ([`FrameCtx::capture_stages`]). It is the same two kernel calls
27/// ([`apply_deblock`], [`apply_cdef`]) on the same captured pre-filter
28/// planes in the same order, so it is the same bytes -- pinned by
29/// `encoder::tests::filter_replay_final_matches_the_capture_decode`, which
30/// runs the decode as well and holds picture and stages against it.
31///
32/// `want_stages` mirrors `capture_stages`' own `deblocked = None` arm: that
33/// decode's header says `RESTORE_NONE`, so the decoder took no pre-CDEF
34/// snapshot and handed the post-CDEF plane over for both stages.
35pub(crate) fn replay_final(
36 loop_filter: &LoopFilterParams,
37 cdef: &CdefParams,
38 idx_grid: &[u8],
39 want_stages: bool,
40 fctx: &FrameCtx,
41) -> Option<(Picture, Option<FilterStages>)> {
42 #[cfg(test)]
43 if REPLAY_DISABLED.load(std::sync::atomic::Ordering::Relaxed) {
44 return None;
45 }
46 replay_inner(loop_filter, cdef, Some((idx_grid, want_stages)), fctx)
47}
48
49fn replay_inner(
50 loop_filter: &LoopFilterParams,
51 cdef: &CdefParams,
52 // `Some((cdef_idx grid, capture the filter stages))` for the final
53 // replay; `None` for a scoring candidate, which always codes `bits == 0`.
54 final_pass: Option<(&[u8], bool)>,
55 fctx: &FrameCtx,
56) -> Option<(Picture, Option<FilterStages>)> {
57 REPLAY.with_borrow_mut(|slot| {
58 let r = slot.as_mut()?;
59 let hit = matches!(&r.deblocked, Some((lf, ..)) if lf == loop_filter);
60 // Set when the working planes ARE the deblocked planes (see below):
61 // they go into the cache at the end, and the picture is cropped out
62 // of them rather than taking their buffers.
63 let mut share = false;
64 let cpt = crate::par::timer(crate::par::S_COPY);
65 let unit_cols = r.neighbours.mi_cols.div_ceil(2);
66 let unit_rows = r.neighbours.mi_rows.div_ceil(2);
67 let (mut y, mut u, mut v) = if hit {
68 let work = r.work.take();
69 let wrote = std::mem::take(&mut r.wrote);
70 // `cdef_adjust_strength` is monotone in the strength it is given,
71 // so a candidate whose primary strength is no lower and whose
72 // secondary is zero only if the last one's was writes every unit
73 // the last one wrote -- and writes it from the same post-deblock
74 // source, so the samples under it need not be put back first.
75 // Only while both candidates carry ONE strength pair (`bits ==
76 // 0`, every scoring candidate) and read the same `cdef_idx` grid.
77 let superset =
78 |cur: (u8, u8), old: (u8, u8)| cur.0 >= old.0 && (cur.1 != 0 || old.1 == 0);
79 let (skip_y, skip_uv) = match (final_pass, &r.work_cdef) {
80 (None, Some(old)) if old.bits == 0 && cdef.bits == 0 => (
81 superset(
82 (cdef.y_pri_strength[0], cdef.y_sec_strength[0]),
83 (old.y_pri_strength[0], old.y_sec_strength[0]),
84 ),
85 superset(
86 (cdef.uv_pri_strength[0], cdef.uv_sec_strength[0]),
87 (old.uv_pri_strength[0], old.uv_sec_strength[0]),
88 ),
89 ),
90 _ => (false, false),
91 };
92 let (deblocked, scratch) = (&r.deblocked, &mut r.scratch);
93 let (_, dy, du, dv) = deblocked.as_ref().expect("just matched");
94 match work {
95 Some((mut wy, mut wu, mut wv)) => {
96 if !skip_y {
97 restore_written(&mut wy, dy, &wrote, 1, unit_cols, 8);
98 }
99 if !skip_uv {
100 restore_written(&mut wu, du, &wrote, 2, unit_cols, 4);
101 restore_written(&mut wv, dv, &wrote, 2, unit_cols, 4);
102 }
103 (wy, wu, wv)
104 }
105 None => (
106 pooled(dy, scratch),
107 pooled(du, scratch),
108 pooled(dv, scratch),
109 ),
110 }
111 } else {
112 // The levels moved, so the cached deblocked planes are dead:
113 // their buffers are this candidate's working copies.
114 for plane in r.work.take().into_iter().flat_map(|(a, b, c)| [a, b, c]) {
115 r.scratch.push(plane.data.into_owned());
116 }
117 r.work_cdef = None;
118 if let Some((_, dy, du, dv)) = r.deblocked.take() {
119 r.scratch.push(dy.data.into_owned());
120 r.scratch.push(du.data.into_owned());
121 r.scratch.push(dv.data.into_owned());
122 }
123 let (cap_y, cap_u, cap_v, scratch) = (&r.y, &r.u, &r.v, &mut r.scratch);
124 let (mut y, mut u, mut v) = (
125 pooled(cap_y, scratch),
126 pooled(cap_u, scratch),
127 pooled(cap_v, scratch),
128 );
129 let _t = crate::par::timer(crate::par::S_DEBLOCK);
130 apply_deblock(
131 &mut y,
132 &mut u,
133 &mut v,
134 loop_filter,
135 &r.neighbours,
136 r.frame_width,
137 r.frame_height,
138 fctx,
139 );
140 // lane-av1cap2: an all-zero-strength CDEF writes no sample at
141 // all ([`cdef_all_zero`]'s own early return), so THESE planes
142 // are already this candidate's output -- they become the
143 // deblock cache at the end instead of being copied into it
144 // here, which is three ~6 MB copies less per candidate that
145 // moves the levels (the whole deblocking half of the search:
146 // its CDEF strengths are still (0,0)).
147 share = cdef_all_zero(cdef);
148 if !share {
149 let scratch = &mut r.scratch;
150 r.deblocked = Some((
151 *loop_filter,
152 pooled(&y, scratch),
153 pooled(&u, scratch),
154 pooled(&v, scratch),
155 ));
156 }
157 (y, u, v)
158 };
159 drop(cpt);
160 // The winner's `cdef_idx` literals live in the re-coded tile, which
161 // nothing parses on this path -- hand the grid the encoder already
162 // chose straight to the filter (`bits == 0` leaves the grid the last
163 // parse sized, which is what every scoring candidate reads).
164 if let Some((grid, _)) = final_pass.filter(|(g, _)| !g.is_empty()) {
165 fctx.cdef_idx_grid.with(|g| *g.borrow_mut() = grid.to_vec());
166 }
167 let ct = crate::par::timer(crate::par::S_CDEF);
168 let mut wrote = std::mem::take(&mut r.wrote);
169 wrote.clear();
170 wrote.resize(unit_cols * unit_rows, 0);
171 r.work_cdef = Some(*cdef);
172 let pre = r
173 .deblocked
174 .as_ref()
175 .map(|(_, dy, du, dv)| (dy.data.as_ref(), du.data.as_ref(), dv.data.as_ref()));
176 apply_cdef(
177 &mut y,
178 &mut u,
179 &mut v,
180 cdef,
181 &r.neighbours,
182 (r.frame_width, r.frame_height),
183 fctx,
184 pre,
185 Some(&mut wrote),
186 );
187 drop(ct);
188 if final_pass.is_some_and(|(_, want)| want) {
189 // The loop-restoration search runs on the stages above (where
190 // post-CDEF stands in for post-deblock, as the capture decode
191 // had it); the FILTER this frame ends up applying reads the
192 // real post-deblock rows, which is what the decoder does.
193 let snapshot = if share {
194 // Nothing filtered these planes after the deblock, so they
195 // are the post-deblock picture themselves.
196 Some(plane_snapshot(&y, 0))
197 } else {
198 r.deblocked
199 .as_ref()
200 .map(|(_, dy, ..)| plane_snapshot(dy, 0))
201 };
202 let scratch = &mut r.scratch;
203 r.final_luma = snapshot.map(|d| (pooled(&y, scratch), d));
204 }
205 let stages = final_pass.and_then(|(_, want)| {
206 want.then(|| FilterStages {
207 deblocked: [y.data.to_vec(), u.data.to_vec(), v.data.to_vec()],
208 cdefed: [y.data.to_vec(), u.data.to_vec(), v.data.to_vec()],
209 stride: [y.width, u.width, v.width],
210 })
211 });
212 // The capture only happens on the shape whose tail is the two
213 // filter calls and the crop (no superres, no loop restoration), so
214 // what is left is that crop -- byte for byte the cropped return path
215 // of the decode this replaces, wide-margin stash included.
216 //
217 // lane-av1fpar: the crop is why this arm exists at all. The capture
218 // used to demand `frame == plane` dimensions, which no editor size
219 // meets (1080 and 1608 both pad to a superblock row), so every
220 // candidate of every frame at those sizes fell through to a full
221 // re-decode -- 57% of a 1080p frame's wall.
222 let (fw, fh) = (r.frame_width, r.frame_height);
223 if fw == r.width && fh == r.height {
224 fctx.last_frame_wide_margin.with(|m| *m.borrow_mut() = None);
225 r.wrote = Vec::new();
226 r.work_cdef = None;
227 if share {
228 let picture = Picture {
229 width: r.width,
230 height: r.height,
231 y: y.data.to_vec(),
232 u: u.data.to_vec(),
233 v: v.data.to_vec(),
234 };
235 r.deblocked = Some((*loop_filter, y, u, v));
236 return Some((picture, stages));
237 }
238 return Some((
239 Picture {
240 width: r.width,
241 height: r.height,
242 y: y.data.into_owned(),
243 u: u.data.into_owned(),
244 v: v.data.into_owned(),
245 },
246 stages,
247 ));
248 }
249 let crop = |plane: &PlaneBuf<'_>, w: usize, h: usize| -> Vec<u16> {
250 let mut out = Vec::with_capacity(w * h);
251 for row in 0..h {
252 out.extend(plane.data[row * plane.width..][..w].iter().copied());
253 }
254 out
255 };
256 let (tw, th) = (r.true_width, r.true_height);
257 fctx.last_frame_wide_margin.with(|m| {
258 *m.borrow_mut() = (tw > fw || th > fh).then(|| Picture {
259 width: tw,
260 height: th,
261 y: crop(&y, tw, th),
262 u: crop(&u, round_ss(tw, ss_x(fctx)), round_ss(th, ss_y(fctx))),
263 v: crop(&v, round_ss(tw, ss_x(fctx)), round_ss(th, ss_y(fctx))),
264 });
265 });
266 let out = Picture {
267 width: fw,
268 height: fh,
269 y: crop(&y, fw, fh),
270 u: crop(&u, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))),
271 v: crop(&v, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))),
272 };
273 // The working planes outlive the crop: either they ARE the
274 // deblocked planes this candidate's successors reuse, or they go
275 // back to the pool.
276 if share {
277 r.wrote = Vec::new();
278 r.work_cdef = None;
279 r.deblocked = Some((*loop_filter, y, u, v));
280 } else {
281 // lane-av1speed3: kept whole (post-CDEF samples and all) for the
282 // next candidate, together with the map of what CDEF wrote.
283 r.wrote = wrote;
284 r.work = Some((y, u, v));
285 }
286 Some((out, stages))
287 })
288}
289
290/// lane-av1cap2: the loop-restoration half of the final replay. Once the
291/// units are chosen the tile is coded a second time to carry them, and that
292/// re-coded tile used to be DECODED to reach the restored reference frame
293/// (5.6% of a 4K frame's encode). Loop restoration reads nothing of that
294/// tile but the per-unit filters the encoder just picked: it is the
295/// decoder's own [`crate::restoration::apply_loop_restoration_plane`] on
296/// the post-CDEF plane the final replay produced, with the TRUE
297/// post-deblock stripe-border rows -- the same call the decoder's
298/// `apply_loop_restoration` makes, on the same input. Only luma moves: the
299/// encoder never writes a chroma restoration type other than
300/// `RESTORE_NONE`, whose kernel hands its input straight back.
301///
302/// Returns the cropped luma plane and refreshes the wide-margin stash's own
303/// luma (restoration writes strictly inside `frame_width x frame_height`,
304/// so the margin columns past it stand). `None` when nothing was captured
305/// -- the caller then decodes for real.
306pub(crate) fn replay_restoration(
307 lr: &LoopRestorationParams,
308 units: &[Option<crate::restoration::WienerInfo>],
309 fctx: &FrameCtx,
310) -> Option<Vec<u16>> {
311 #[cfg(test)]
312 if REPLAY_DISABLED.load(std::sync::atomic::Ordering::Relaxed) {
313 return None;
314 }
315 REPLAY.with_borrow_mut(|slot| {
316 let r = slot.as_mut()?;
317 let (cdefed, deblocked) = r.final_luma.as_ref()?;
318 let (fw, fh) = (r.frame_width, r.frame_height);
319 let mut grid = crate::restoration::RestorationGrid::with_ss(
320 lr,
321 fw as u32,
322 fh as u32,
323 fctx.subsampling_x.get(),
324 fctx.subsampling_y.get(),
325 );
326 let (horz, vert) = (grid.horz_units[0], grid.vert_units[0]);
327 if horz == 0 || units.len() != horz * vert {
328 return None;
329 }
330 for (i, filter) in units.iter().enumerate() {
331 if let Some(info) = filter {
332 grid.set(
333 0,
334 i / horz,
335 i % horz,
336 crate::restoration::UnitFilter::Wiener(*info),
337 );
338 }
339 }
340 let stride = cdefed.width;
341 let mut out = scratch_empty();
342 crate::restoration::apply_loop_restoration_plane(
343 &cdefed.data,
344 &deblocked.data,
345 stride,
346 fw,
347 fh,
348 0,
349 lr.frame_restoration_type[0],
350 lr.loop_restoration_size[0],
351 &grid,
352 0,
353 &mut out,
354 None,
355 fctx,
356 );
357 fctx.last_frame_wide_margin.with(|m| {
358 if let Some(p) = m.borrow_mut().as_mut() {
359 let tw = p.width;
360 for row in 0..p.height.min(fh) {
361 let n = tw.min(fw);
362 p.y[row * tw..][..n].copy_from_slice(&out[row * stride..][..n]);
363 }
364 }
365 });
366 let mut y = Vec::with_capacity(fw * fh);
367 for row in 0..fh {
368 y.extend(out[row * stride..][..fw].iter().copied());
369 }
370 scratch_return(out);
371 Some(y)
372 })
373}
374
375/// The output crop, taking the plane by value: a frame whose width is
376/// already superblock-aligned (only its height is padded) has a padded
377/// buffer whose stride equals the visible width, so the crop is a pure
378/// truncation of the buffer we already own -- no full-plane copy at all
379/// (lane-copy: three ~6 MB memmoves per 4K frame on the frame thread's
380/// serial tail). Any other shape falls back to the row-by-row copy.
381pub(super) fn crop_owned(plane: PlaneBuf<'_>, w: usize, h: usize) -> Vec<u16> {
382 let stride = plane.width;
383 let mut data = plane.data.into_owned();
384 if stride == w {
385 data.truncate(w * h);
386 return data;
387 }
388 let mut out = Vec::with_capacity(w * h);
389 for row in 0..h {
390 out.extend(data[row * stride..][..w].iter().copied());
391 }
392 out
393}
394
395/// Hands a buffer back to the pool; the capacity is what is worth keeping.
396/// Over [`SCRATCH_CAP`] the buffer is dropped (freed) instead -- the cap is
397/// what keeps a pool shared by every thread from being an unbounded stash.
398pub(super) fn scratch_return(v: Vec<u16>) {
399 let mut p = PLANE_SCRATCH.lock().unwrap();
400 if p.len() < SCRATCH_CAP {
401 p.push(v);
402 }
403}
404
405/// The geometry one CDEF plane needs: its row stride and the frame's true
406/// (decodable) extent in that plane's own units.
407#[derive(Clone, Copy)]
408pub(super) struct CdefPlaneGeom {
409 pub(super) stride: usize,
410 pub(super) true_w: usize,
411 pub(super) true_h: usize,
412}
413
414/// One CDEF band's writable state: each plane's own row slice (the rows the
415/// band's mi rows cover, carved disjointly by `cdef_rows`), the plane row
416/// that slice starts at, and the band's slice of the `wrote` map with the
417/// unit row it starts at. A band writes nothing outside these slices.
418pub(super) struct CdefBandDst<'a> {
419 pub(super) data: [&'a mut [u16]; 3],
420 pub(super) row0: [usize; 3],
421 // lane-av1speed3: one byte per 8x8 CDEF unit, set for every unit this band
422 // actually filters. Bit 0 = this unit's LUMA square was filtered, bit 1 =
423 // its chroma ones. `cdef_adjust_strength` zeroes a unit's primary strength
424 // off its own variance, so which units a candidate writes is not a function
425 // of its strengths alone -- the encoder's filter replay reuses one
426 // destination plane across candidates and needs the map to restore it.
427 pub(super) wrote: Option<&'a mut [u8]>,
428 pub(super) wrote_row0: usize,
429}
430
431/// One CDEF mi-row band. The band owns its rows of the destination planes
432/// through `dst` (no shared `&mut` of the whole plane), and reads only the
433/// PRE-CDEF snapshots `src_*`, indexed with absolute coordinates. Reference
434/// parameters restore `noalias` (lane-perf11 (a): -x% instructions at one
435/// filter thread).
436#[allow(clippy::too_many_arguments)]
437pub(super) fn cdef_band(
438 r_lo: usize,
439 r_hi: usize,
440 mut dst: CdefBandDst<'_>,
441 geom: [CdefPlaneGeom; 3],
442 src_y: &[u16],
443 src_u: &[u16],
444 src_v: &[u16],
445 cdef: &CdefParams,
446 skip_grid: &Neighbours,
447 idx_grid: &[u8],
448 sb_cols: usize,
449 coeff_shift: i32,
450 damping: i32,
451 damping_uv: i32,
452 (frame_w, frame_h): (usize, usize),
453 fctx: &crate::decode::FrameCtx,
454) {
455 let unit_cols = skip_grid.mi_cols.div_ceil(2);
456 let strength_idx = |mi_r: usize, mi_c: usize| -> usize {
457 if sb_cols == 0 {
458 return 0;
459 }
460 let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize);
461 idx_grid.get(sb_r * sb_cols + sb_c).copied().unwrap_or(0) as usize
462 };
463 // One scratch window per plane kind, reused by every unit (8x8 luma and
464 // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring).
465 // 256 entries, not the (bw + 4) x (bh + 4) samples themselves: the
466 // AVX2 kernel taps the window in place with two-row loads that reach
467 // past the last sample, and the tail stays zero for every unit.
468 let mut win_y = [0i16; 256];
469 let mut win_uv = [0i16; 256];
470 let mut mi_r = r_lo;
471 while mi_r < r_hi {
472 let mut mi_c = 0usize;
473 while mi_c < skip_grid.mi_cols {
474 if skip_grid.is_skip_txfm(mi_r, mi_c) {
475 hit!(CDEF_SKIPPED_UNITS);
476 }
477 if !skip_grid.is_skip_txfm(mi_r, mi_c) {
478 let sidx = strength_idx(mi_r, mi_c);
479 let (ox, oy) = (mi_c * 4, mi_r * 4);
480 // Nothing to do for this unit at all: the direction search is
481 // only ever consumed through a strength, so a unit whose four
482 // strengths are zero never needs its 8x8 window gathered.
483 if cdef.y_pri_strength[sidx] == 0
484 && cdef.y_sec_strength[sidx] == 0
485 && cdef.uv_pri_strength[sidx] == 0
486 && cdef.uv_sec_strength[sidx] == 0
487 {
488 mi_c += 2;
489 continue;
490 }
491 let g = geom[0];
492 cdef_gather(
493 src_y, g.stride, g.true_w, g.true_h, ox, oy, 8, 8, &mut win_y,
494 );
495 // lane-cdef r1: the direction search reads the SAME padded
496 // block the taps do -- libaom runs `cdef_find_dir` on the
497 // prepared filter block (`cdef_block.c:296-320` over
498 // `cdef_prepare_fb`'s buffer, `cdef.c:249-256`), whose columns
499 // and rows past the mi-rounded frame extent are CDEF_VERY_LARGE.
500 // Clamping to the last real sample instead made every straddling
501 // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence
502 // a different `adjust_strength`, hence a +-1 band at the crop edge.
503 let (dir, var) = cdef_find_dir(&win_y, 8 + 2 * CDEF_BORDER, coeff_shift);
504
505 // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0`
506 // -- each plane zeroes the shared direction when *its own*
507 // frame-level primary strength is 0, independent of the
508 // per-block adjusted `t` and of the other plane's strength.
509 let y_pri_strength = i32::from(cdef.y_pri_strength[sidx]) << coeff_shift;
510 let y_sec_strength = i32::from(cdef.y_sec_strength[sidx]) << coeff_shift;
511 let y_dir = if y_pri_strength != 0 { dir } else { 0 };
512 let t = cdef_adjust_strength(y_pri_strength, var);
513 // lane-cdef r1 rung: one line per filtered 8x8 luma CDEF unit.
514 if crate::envflags::env_flag!("EC_AV1_CDEF_DBG") {
515 eprintln!(
516 "EC_CDEF mi_r={mi_r} mi_c={mi_c} x={ox} y={oy} sidx={sidx} dir={dir} var={var} t={t} pri={y_pri_strength} sec={y_sec_strength} damp={damping}"
517 );
518 }
519 let enable_primary = t != 0;
520 let enable_secondary = y_sec_strength != 0;
521 if (ox + 8 > frame_w || oy + 8 > frame_h)
522 && (enable_primary
523 || enable_secondary
524 || cdef.uv_pri_strength[sidx] != 0
525 || cdef.uv_sec_strength[sidx] != 0)
526 {
527 hit!(CDEF_STRADDLE_UNITS);
528 }
529 if enable_primary || enable_secondary {
530 cdef_filter_block(
531 &win_y,
532 &mut *dst.data[0],
533 g.stride,
534 ox,
535 oy - dst.row0[0],
536 8,
537 8,
538 t,
539 y_sec_strength,
540 y_dir,
541 damping,
542 damping,
543 enable_primary,
544 enable_secondary,
545 coeff_shift,
546 fctx,
547 );
548 }
549
550 let t_uv = i32::from(cdef.uv_pri_strength[sidx]) << coeff_shift;
551 let uv_sec_strength = i32::from(cdef.uv_sec_strength[sidx]) << coeff_shift;
552 // lane-av1422filter: the chroma direction is the luma one
553 // remapped through `conv422`/`conv440` under asymmetric
554 // subsampling (see [`CDEF_DIR_CONV422`]); identical to the
555 // raw luma direction for 4:2:0 and 4:4:4.
556 let uv_dir = if t_uv != 0 {
557 match (ss_x(fctx), ss_y(fctx)) {
558 (1, 0) => CDEF_DIR_CONV422[dir],
559 (0, 1) => CDEF_DIR_CONV440[dir],
560 _ => dir,
561 }
562 } else {
563 0
564 };
565 let enable_primary_uv = t_uv != 0;
566 let enable_secondary_uv = uv_sec_strength != 0;
567 if let Some(w) = dst.wrote.as_deref_mut() {
568 let mark = u8::from(enable_primary || enable_secondary)
569 | (u8::from(enable_primary_uv || enable_secondary_uv) << 1);
570 w[(mi_r / 2 - dst.wrote_row0) * unit_cols + mi_c / 2] = mark;
571 }
572 if enable_primary_uv || enable_secondary_uv {
573 let (cox, coy) = (ox >> ss_x(fctx), oy >> ss_y(fctx));
574 let (csize_x, csize_y) = (8 >> ss_x(fctx), 8 >> ss_y(fctx));
575 for (p, src_p) in [(1usize, src_u), (2, src_v)] {
576 let gp = geom[p];
577 cdef_gather(
578 src_p,
579 gp.stride,
580 gp.true_w,
581 gp.true_h,
582 cox,
583 coy,
584 csize_x,
585 csize_y,
586 &mut win_uv,
587 );
588 cdef_filter_block(
589 &win_uv,
590 &mut *dst.data[p],
591 gp.stride,
592 cox,
593 coy - dst.row0[p],
594 csize_x,
595 csize_y,
596 t_uv,
597 uv_sec_strength,
598 uv_dir,
599 damping_uv,
600 damping_uv,
601 enable_primary_uv,
602 enable_secondary_uv,
603 coeff_shift,
604 fctx,
605 );
606 }
607 }
608 }
609 mi_c += 2;
610 }
611 mi_r += 2;
612 }
613}