forge
Explore
Log in

Don't you dare go Hollow

Fuse the weighted bi-prediction combining pass into motion compensation

Browse files at this commit

928abb0381634bb745e8f4db0c2e77a70ee1c700Ahmet Kaan GümüşParent: f3d387e
  • perf
    • fuse the weighted bi-prediction combining pass into motion compensation
Show full message
perf(h264): fuse the weighted bi-prediction combining pass into motion compensation

x264's default streams carry weighted_bipred_idc=2 (implicit), so most
B partitions took the weighted combining path: two temporaries, a scalar
per-sample combine, and a copy back. The combining formula of 8.4.2.3.2
is per-sample, so list 0 now interpolates straight into the picture and
list 1 combines on top of it (Put::AvgW) with 8.4.2.3.2's exact
integers — same tree, same rounding, on the same bytes. Non-fusable
weights keep the temporary path.

Byte-exact A/B vs d27a904d on the five fixture classes: all
display-order frame hashes identical. ec-h264 suite green (79 lib + 23
conformance + 12 encode + 1 decode-first).
crates/ec-h264/src/decoder.rsmodified+64 -0
@@ -2264,6 +2264,20 @@ fn compensate(
22642264 };
22652265 let stride = pic.y.stride;
22662266 let origin = pic.y.at(x0 as usize, y0 as usize);
2267+ // Weighted bi-prediction fuses the same way: the combining formula is
2268+ // per-sample, so list 0 lands in the picture and list 1 combines on top
2269+ // of it (Put::AvgW) with 8.4.2.3.2's exact integers.
2270+ let weighted_put = if !direct && use0 && use1 {
2271+ Some(crate::inter::Put::AvgW {
2272+ w0: wt.w[0],
2273+ w1: wt.w[1],
2274+ shift: (wt.log_wd + 1) as u32,
2275+ round: 1 << wt.log_wd,
2276+ off: (wt.o[0] + wt.o[1] + 1) >> 1,
2277+ })
2278+ } else {
2279+ None
2280+ };
22672281 if direct && use0 && use1 {
22682282 let plane = luma_ref(0)?;
22692283 mc_luma(
@@ -2305,6 +2319,31 @@ fn compensate(
23052319 &mut pic.y.data[origin..],
23062320 crate::inter::Put::Set,
23072321 );
2322+ } else if let Some(put) = weighted_put {
2323+ let plane = luma_ref(0)?;
2324+ mc_luma(
2325+ &plane,
2326+ x0,
2327+ y0,
2328+ mv[0],
2329+ w,
2330+ h,
2331+ stride,
2332+ &mut pic.y.data[origin..],
2333+ crate::inter::Put::Set,
2334+ );
2335+ let plane = luma_ref(1)?;
2336+ mc_luma(
2337+ &plane,
2338+ x0,
2339+ y0,
2340+ mv[1],
2341+ w,
2342+ h,
2343+ stride,
2344+ &mut pic.y.data[origin..],
2345+ put,
2346+ );
23082347 } else {
23092348 let mut part = [[0u8; 256]; 2];
23102349 for list in 0..2 {
@@ -2376,6 +2415,31 @@ fn compensate(
23762415 &mut plane_out.data[origin..],
23772416 crate::inter::Put::Avg,
23782417 );
2418+ } else if let Some(put) = weighted_put {
2419+ let plane = chroma_ref(0);
2420+ mc_chroma(
2421+ &plane,
2422+ cx0,
2423+ cy0,
2424+ mv[0],
2425+ cw,
2426+ ch,
2427+ stride,
2428+ &mut plane_out.data[origin..],
2429+ crate::inter::Put::Set,
2430+ );
2431+ let plane = chroma_ref(1);
2432+ mc_chroma(
2433+ &plane,
2434+ cx0,
2435+ cy0,
2436+ mv[1],
2437+ cw,
2438+ ch,
2439+ stride,
2440+ &mut plane_out.data[origin..],
2441+ put,
2442+ );
23792443 } else if direct {
23802444 let list = usize::from(!use0);
23812445 let plane = chroma_ref(list);
crates/ec-h264/src/inter.rsmodified+263 -36
@@ -128,8 +128,28 @@ fn store_half8(dst: &mut [u8], v: i16x8, put: Put) {
128128 let c = ((v + i16x8::splat(16)) >> 5u32)
129129 .max(i16x8::ZERO)
130130 .min(i16x8::splat(255));
131- for (o, &s) in dst.iter_mut().zip(c.as_array()) {
132- store_u8(o, s as u8, put);
131+ match put {
132+ Put::Set => {
133+ for (o, &s) in dst.iter_mut().zip(c.as_array()) {
134+ *o = s as u8;
135+ }
136+ }
137+ Put::Avg => {
138+ for (o, &s) in dst.iter_mut().zip(c.as_array()) {
139+ *o = avg(*o, s as u8);
140+ }
141+ }
142+ Put::AvgW {
143+ w0,
144+ w1,
145+ shift,
146+ round,
147+ off,
148+ } => {
149+ for (o, &s) in dst.iter_mut().zip(c.as_array()) {
150+ store_weighted(o, s as u8, w0, w1, shift, round, off);
151+ }
152+ }
133153 }
134154 }
135155
@@ -139,8 +159,28 @@ fn store_half16(dst: &mut [u8], v: i16x16, put: Put) {
139159 let c = ((v + i16x16::splat(16)) >> 5u32)
140160 .max(i16x16::ZERO)
141161 .min(i16x16::splat(255));
142- for (o, &s) in dst.iter_mut().zip(c.as_array()) {
143- store_u8(o, s as u8, put);
162+ match put {
163+ Put::Set => {
164+ for (o, &s) in dst.iter_mut().zip(c.as_array()) {
165+ *o = s as u8;
166+ }
167+ }
168+ Put::Avg => {
169+ for (o, &s) in dst.iter_mut().zip(c.as_array()) {
170+ *o = avg(*o, s as u8);
171+ }
172+ }
173+ Put::AvgW {
174+ w0,
175+ w1,
176+ shift,
177+ round,
178+ off,
179+ } => {
180+ for (o, &s) in dst.iter_mut().zip(c.as_array()) {
181+ store_weighted(o, s as u8, w0, w1, shift, round, off);
182+ }
183+ }
144184 }
145185 }
146186 /// One 6-tap half-sample pass over a `w` x `h` block whose top-left sample is
@@ -206,8 +246,28 @@ fn store_centre(dst: &mut [u8], v: i32x8, put: Put) {
206246 let c = ((v + i32x8::splat(512)) >> 10u32)
207247 .max(i32x8::ZERO)
208248 .min(i32x8::splat(255));
209- for (o, &s) in dst.iter_mut().zip(c.as_array()) {
210- store_u8(o, s as u8, put);
249+ match put {
250+ Put::Set => {
251+ for (o, &s) in dst.iter_mut().zip(c.as_array()) {
252+ *o = s as u8;
253+ }
254+ }
255+ Put::Avg => {
256+ for (o, &s) in dst.iter_mut().zip(c.as_array()) {
257+ *o = avg(*o, s as u8);
258+ }
259+ }
260+ Put::AvgW {
261+ w0,
262+ w1,
263+ shift,
264+ round,
265+ off,
266+ } => {
267+ for (o, &s) in dst.iter_mut().zip(c.as_array()) {
268+ store_weighted(o, s as u8, w0, w1, shift, round, off);
269+ }
270+ }
211271 }
212272 }
213273
@@ -253,6 +313,13 @@ fn avg_rows_to_contig(out: &mut [u8], a: &[u8], b: &[u8], n: usize, put: Put) {
253313 out[i] = avg(avg(a[i], b[i]), out[i]);
254314 }
255315 }
316+ Put::AvgW {
317+ w0,
318+ w1,
319+ shift,
320+ round,
321+ off,
322+ } => avg_rows_weighted(out, a, b, n, w0, w1, shift, round, off),
256323 }
257324 }
258325
@@ -270,6 +337,13 @@ fn copy_rows(out: &mut [u8], os: usize, src: &[u8], w: usize, h: usize, put: Put
270337 *d = avg(*d, s);
271338 }
272339 }
340+ Put::AvgW {
341+ w0,
342+ w1,
343+ shift,
344+ round,
345+ off,
346+ } => copy_rows_weighted(dst, src, w0, w1, shift, round, off),
273347 }
274348 }
275349 }
@@ -328,18 +402,41 @@ pub(crate) fn mc_luma(
328402
329403 // Full sample: a straight copy.
330404 if fx == 0 && fy == 0 {
331- for row in 0..h {
332- let src = base + row * stride;
333- let dst = &mut out[row * os..row * os + w];
334- let s = &r.data[src..src + w];
335- match put {
336- Put::Set => dst.copy_from_slice(s),
337- Put::Avg => {
338- for (d, &v) in dst.iter_mut().zip(s) {
405+ match put {
406+ Put::Set => {
407+ for row in 0..h {
408+ let src = base + row * stride;
409+ out[row * os..row * os + w].copy_from_slice(&r.data[src..src + w]);
410+ }
411+ }
412+ Put::Avg => {
413+ for row in 0..h {
414+ let src = base + row * stride;
415+ let dst = &mut out[row * os..row * os + w];
416+ for (d, &v) in dst.iter_mut().zip(&r.data[src..src + w]) {
339417 *d = avg(*d, v);
340418 }
341419 }
342420 }
421+ Put::AvgW {
422+ w0,
423+ w1,
424+ shift,
425+ round,
426+ off,
427+ } => copy_weighted(
428+ out,
429+ os,
430+ &r.data[base..],
431+ stride,
432+ w,
433+ h,
434+ w0,
435+ w1,
436+ shift,
437+ round,
438+ off,
439+ ),
343440 }
344441 return;
345442 }
@@ -412,8 +509,36 @@ pub(crate) fn mc_luma(
412509 let s = &r.data[src..src + w];
413510 let hl = &half[row * w..row * w + w];
414511 let dst = &mut out[row * os..row * os + w];
415- for col in 0..w {
416- store_u8(&mut dst[col], avg(s[col], hl[col]), put);
512+ match put {
513+ Put::Set => {
514+ for col in 0..w {
515+ dst[col] = avg(s[col], hl[col]);
516+ }
517+ }
518+ Put::Avg => {
519+ for col in 0..w {
520+ dst[col] = avg(dst[col], avg(s[col], hl[col]));
521+ }
522+ }
523+ Put::AvgW {
524+ w0,
525+ w1,
526+ shift,
527+ round,
528+ off,
529+ } => {
530+ for col in 0..w {
531+ store_weighted(
532+ &mut dst[col],
533+ avg(s[col], hl[col]),
534+ w0,
535+ w1,
536+ shift,
537+ round,
538+ off,
539+ );
540+ }
541+ }
417542 }
418543 }
419544 }
@@ -451,7 +576,17 @@ fn chroma_rows<const W: usize>(
451576 + u16::from(q[c]) * wt[2]
452577 + u16::from(q[c + 1]) * wt[3]
453578 + 32;
454- store_u8(&mut dst[c], (v >> 6) as u8, put);
579+ match put {
580+ Put::Set => dst[c] = (v >> 6) as u8,
581+ Put::Avg => dst[c] = avg(dst[c], (v >> 6) as u8),
582+ Put::AvgW {
583+ w0,
584+ w1,
585+ shift,
586+ round,
587+ off,
588+ } => store_weighted(&mut dst[c], (v >> 6) as u8, w0, w1, shift, round, off),
589+ }
455590 }
456591 }
457592 }
@@ -478,18 +613,41 @@ pub(crate) fn mc_chroma(
478613 // Full sample: a straight copy. A zero motion vector is the most common
479614 // vector in real content, and it lands here.
480615 if fx == 0 && fy == 0 {
481- for row in 0..h {
482- let src = base + row * stride;
483- let dst = &mut out[row * os..row * os + w];
484- let s = &r.data[src..src + w];
485- match put {
486- Put::Set => dst.copy_from_slice(s),
487- Put::Avg => {
488- for (d, &v) in dst.iter_mut().zip(s) {
616+ match put {
617+ Put::Set => {
618+ for row in 0..h {
619+ let src = base + row * stride;
620+ out[row * os..row * os + w].copy_from_slice(&r.data[src..src + w]);
621+ }
622+ }
623+ Put::Avg => {
624+ for row in 0..h {
625+ let src = base + row * stride;
626+ let dst = &mut out[row * os..row * os + w];
627+ for (d, &v) in dst.iter_mut().zip(&r.data[src..src + w]) {
489628 *d = avg(*d, v);
490629 }
491630 }
492631 }
632+ Put::AvgW {
633+ w0,
634+ w1,
635+ shift,
636+ round,
637+ off,
638+ } => copy_weighted(
639+ out,
640+ os,
641+ &r.data[base..],
642+ stride,
643+ w,
644+ h,
645+ w0,
646+ w1,
647+ shift,
648+ round,
649+ off,
650+ ),
493651 }
494652 return;
495653 }
@@ -533,26 +691,95 @@ impl Weights {
533691 /// How an interpolation result lands in `out`: written outright, or rounded
534692 /// into what is already there.
535693 ///
536-/// `Avg` is the second list of a default-weighted bi-prediction (8.4.2.3.1):
537-/// the combined prediction is the rounded average of the two lists' *clipped
538-/// byte* predictions, so the first list interpolates straight into the
539-/// picture and the second averages on top of it. The average reads the same
540-/// bytes whether the first one arrived through a temporary or not — no
541-/// temporary, no combining pass, no copy back.
694+/// The weighted second pass of 8.4.2.3.2 is a fusion mode as well: the first
695+/// prediction sits in `out`, and the second list's sample `v` combines with
696+/// it as `clip((out*w0 + v*w1 + round) >> shift) + off` — the same integer
697+/// formula, on the same bytes, as running the combining pass over the two
698+/// clipped predictions.
542699 #[derive(Clone, Copy, PartialEq, Eq)]
543700 pub(crate) enum Put {
544701 /// Write the prediction.
545702 Set,
546703 /// Average the prediction into the bytes already in `out`.
547704 Avg,
705+ /// Weighted-average the prediction into the bytes already in `out`.
706+ AvgW {
707+ w0: i32,
708+ w1: i32,
709+ shift: u32,
710+ round: i32,
711+ off: i32,
712+ },
548713 }
549714
715+/// The 8.4.2.3.2 combining step for one sample: list 0's prediction is the
716+/// byte already in `dst`, list 1's is `v`.
550717 #[inline]
551-fn store_u8(dst: &mut u8, v: u8, put: Put) {
552- if put == Put::Avg {
553- *dst = avg(*dst, v);
554- } else {
555- *dst = v;
718+fn store_weighted(dst: &mut u8, v: u8, w0: i32, w1: i32, shift: u32, round: i32, off: i32) {
719+ let p0 = i32::from(*dst);
720+ *dst = clip8((((p0 * w0 + i32::from(v) * w1 + round) >> shift) + off) as i32);
721+}
722+/// The weighted combining pass over a whole block: `h` rows of `w` samples
723+/// from a `stride`-pitch source into an `os`-pitch destination.
724+#[inline(never)]
725+#[allow(clippy::too_many_arguments)]
726+fn copy_weighted(
727+ out: &mut [u8],
728+ os: usize,
729+ src: &[u8],
730+ stride: usize,
731+ w: usize,
732+ h: usize,
733+ w0: i32,
734+ w1: i32,
735+ shift: u32,
736+ round: i32,
737+ off: i32,
738+) {
739+ for row in 0..h {
740+ let dst = &mut out[row * os..row * os + w];
741+ let s = &src[row * stride..row * stride + w];
742+ for (d, &v) in dst.iter_mut().zip(s) {
743+ store_weighted(d, v, w0, w1, shift, round, off);
744+ }
745+ }
746+}
747+
748+/// The weighted combining pass over contiguous equal-length slices, where
749+/// list 1's sample is the rounded average of `a` and `b`.
750+#[inline(never)]
751+#[allow(clippy::too_many_arguments)]
752+fn avg_rows_weighted(
753+ out: &mut [u8],
754+ a: &[u8],
755+ b: &[u8],
756+ n: usize,
757+ w0: i32,
758+ w1: i32,
759+ shift: u32,
760+ round: i32,
761+ off: i32,
762+) {
763+ let (out, a, b) = (&mut out[..n], &a[..n], &b[..n]);
764+ for i in 0..n {
765+ store_weighted(&mut out[i], avg(a[i], b[i]), w0, w1, shift, round, off);
766+ }
767+}
768+
769+/// The weighted combining pass over a whole block from a `w`-pitch source.
770+#[inline(never)]
771+#[allow(clippy::too_many_arguments)]
772+fn copy_rows_weighted(
773+ out: &mut [u8],
774+ src: &[u8],
775+ w0: i32,
776+ w1: i32,
777+ shift: u32,
778+ round: i32,
779+ off: i32,
780+) {
781+ for (d, &s) in out.iter_mut().zip(src) {
782+ store_weighted(d, s, w0, w1, shift, round, off);
556783 }
557784 }
558785