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(
2264 };2264 };
2265 let stride = pic.y.stride;2265 let stride = pic.y.stride;
2266 let origin = pic.y.at(x0 as usize, y0 as usize);2266 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+ };
2267 if direct && use0 && use1 {2281 if direct && use0 && use1 {
2268 let plane = luma_ref(0)?;2282 let plane = luma_ref(0)?;
2269 mc_luma(2283 mc_luma(
@@ -2305,6 +2319,31 @@ fn compensate(
2305 &mut pic.y.data[origin..],2319 &mut pic.y.data[origin..],
2306 crate::inter::Put::Set,2320 crate::inter::Put::Set,
2307 );2321 );
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+ );
2308 } else {2347 } else {
2309 let mut part = [[0u8; 256]; 2];2348 let mut part = [[0u8; 256]; 2];
2310 for list in 0..2 {2349 for list in 0..2 {
@@ -2376,6 +2415,31 @@ fn compensate(
2376 &mut plane_out.data[origin..],2415 &mut plane_out.data[origin..],
2377 crate::inter::Put::Avg,2416 crate::inter::Put::Avg,
2378 );2417 );
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+ );
2379 } else if direct {2443 } else if direct {
2380 let list = usize::from(!use0);2444 let list = usize::from(!use0);
2381 let plane = chroma_ref(list);2445 let plane = chroma_ref(list);
crates/ec-h264/src/inter.rsmodified+253 -26
@@ -128,8 +128,28 @@ fn store_half8(dst: &mut [u8], v: i16x8, put: Put) {
128 let c = ((v + i16x8::splat(16)) >> 5u32)128 let c = ((v + i16x8::splat(16)) >> 5u32)
129 .max(i16x8::ZERO)129 .max(i16x8::ZERO)
130 .min(i16x8::splat(255));130 .min(i16x8::splat(255));
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+ } => {
131 for (o, &s) in dst.iter_mut().zip(c.as_array()) {149 for (o, &s) in dst.iter_mut().zip(c.as_array()) {
132- store_u8(o, s as u8, put);150+ store_weighted(o, s as u8, w0, w1, shift, round, off);
151+ }
152+ }
133 }153 }
134 }154 }
135 155
@@ -139,8 +159,28 @@ fn store_half16(dst: &mut [u8], v: i16x16, put: Put) {
139 let c = ((v + i16x16::splat(16)) >> 5u32)159 let c = ((v + i16x16::splat(16)) >> 5u32)
140 .max(i16x16::ZERO)160 .max(i16x16::ZERO)
141 .min(i16x16::splat(255));161 .min(i16x16::splat(255));
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+ } => {
142 for (o, &s) in dst.iter_mut().zip(c.as_array()) {180 for (o, &s) in dst.iter_mut().zip(c.as_array()) {
143- store_u8(o, s as u8, put);181+ store_weighted(o, s as u8, w0, w1, shift, round, off);
182+ }
183+ }
144 }184 }
145 }185 }
146 /// One 6-tap half-sample pass over a `w` x `h` block whose top-left sample is186 /// 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) {
206 let c = ((v + i32x8::splat(512)) >> 10u32)246 let c = ((v + i32x8::splat(512)) >> 10u32)
207 .max(i32x8::ZERO)247 .max(i32x8::ZERO)
208 .min(i32x8::splat(255));248 .min(i32x8::splat(255));
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+ } => {
209 for (o, &s) in dst.iter_mut().zip(c.as_array()) {267 for (o, &s) in dst.iter_mut().zip(c.as_array()) {
210- store_u8(o, s as u8, put);268+ store_weighted(o, s as u8, w0, w1, shift, round, off);
269+ }
270+ }
211 }271 }
212 }272 }
213 273
@@ -253,6 +313,13 @@ fn avg_rows_to_contig(out: &mut [u8], a: &[u8], b: &[u8], n: usize, put: Put) {
253 out[i] = avg(avg(a[i], b[i]), out[i]);313 out[i] = avg(avg(a[i], b[i]), out[i]);
254 }314 }
255 }315 }
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),
256 }323 }
257 }324 }
258 325
@@ -270,6 +337,13 @@ fn copy_rows(out: &mut [u8], os: usize, src: &[u8], w: usize, h: usize, put: Put
270 *d = avg(*d, s);337 *d = avg(*d, s);
271 }338 }
272 }339 }
340+ Put::AvgW {
341+ w0,
342+ w1,
343+ shift,
344+ round,
345+ off,
346+ } => copy_rows_weighted(dst, src, w0, w1, shift, round, off),
273 }347 }
274 }348 }
275 }349 }
@@ -328,18 +402,41 @@ pub(crate) fn mc_luma(
328 402
329 // Full sample: a straight copy.403 // Full sample: a straight copy.
330 if fx == 0 && fy == 0 {404 if fx == 0 && fy == 0 {
405+ match put {
406+ Put::Set => {
331 for row in 0..h {407 for row in 0..h {
332 let src = base + row * stride;408 let src = base + row * stride;
333- let dst = &mut out[row * os..row * os + w];409+ out[row * os..row * os + w].copy_from_slice(&r.data[src..src + w]);
334- let s = &r.data[src..src + w];410+ }
335- match put {411+ }
336- Put::Set => dst.copy_from_slice(s),
337 Put::Avg => {412 Put::Avg => {
338- for (d, &v) in dst.iter_mut().zip(s) {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]) {
339 *d = avg(*d, v);417 *d = avg(*d, v);
340 }418 }
341 }419 }
342 }420 }
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+ ),
343 }440 }
344 return;441 return;
345 }442 }
@@ -412,8 +509,36 @@ pub(crate) fn mc_luma(
412 let s = &r.data[src..src + w];509 let s = &r.data[src..src + w];
413 let hl = &half[row * w..row * w + w];510 let hl = &half[row * w..row * w + w];
414 let dst = &mut out[row * os..row * os + w];511 let dst = &mut out[row * os..row * os + w];
512+ match put {
513+ Put::Set => {
415 for col in 0..w {514 for col in 0..w {
416- store_u8(&mut dst[col], avg(s[col], hl[col]), put);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+ }
417 }542 }
418 }543 }
419 }544 }
@@ -451,7 +576,17 @@ fn chroma_rows<const W: usize>(
451 + u16::from(q[c]) * wt[2]576 + u16::from(q[c]) * wt[2]
452 + u16::from(q[c + 1]) * wt[3]577 + u16::from(q[c + 1]) * wt[3]
453 + 32;578 + 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+ }
455 }590 }
456 }591 }
457 }592 }
@@ -478,18 +613,41 @@ pub(crate) fn mc_chroma(
478 // Full sample: a straight copy. A zero motion vector is the most common613 // Full sample: a straight copy. A zero motion vector is the most common
479 // vector in real content, and it lands here.614 // vector in real content, and it lands here.
480 if fx == 0 && fy == 0 {615 if fx == 0 && fy == 0 {
616+ match put {
617+ Put::Set => {
481 for row in 0..h {618 for row in 0..h {
482 let src = base + row * stride;619 let src = base + row * stride;
483- let dst = &mut out[row * os..row * os + w];620+ out[row * os..row * os + w].copy_from_slice(&r.data[src..src + w]);
484- let s = &r.data[src..src + w];621+ }
485- match put {622+ }
486- Put::Set => dst.copy_from_slice(s),
487 Put::Avg => {623 Put::Avg => {
488- for (d, &v) in dst.iter_mut().zip(s) {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]) {
489 *d = avg(*d, v);628 *d = avg(*d, v);
490 }629 }
491 }630 }
492 }631 }
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+ ),
493 }651 }
494 return;652 return;
495 }653 }
@@ -533,26 +691,95 @@ impl Weights {
533 /// How an interpolation result lands in `out`: written outright, or rounded691 /// How an interpolation result lands in `out`: written outright, or rounded
534 /// into what is already there.692 /// into what is already there.
535 ///693 ///
536-/// `Avg` is the second list of a default-weighted bi-prediction (8.4.2.3.1):694+/// The weighted second pass of 8.4.2.3.2 is a fusion mode as well: the first
537-/// the combined prediction is the rounded average of the two lists' *clipped695+/// prediction sits in `out`, and the second list's sample `v` combines with
538-/// byte* predictions, so the first list interpolates straight into the696+/// it as `clip((out*w0 + v*w1 + round) >> shift) + off` — the same integer
539-/// picture and the second averages on top of it. The average reads the same697+/// formula, on the same bytes, as running the combining pass over the two
540-/// bytes whether the first one arrived through a temporary or not — no698+/// clipped predictions.
541-/// temporary, no combining pass, no copy back.
542 #[derive(Clone, Copy, PartialEq, Eq)]699 #[derive(Clone, Copy, PartialEq, Eq)]
543 pub(crate) enum Put {700 pub(crate) enum Put {
544 /// Write the prediction.701 /// Write the prediction.
545 Set,702 Set,
546 /// Average the prediction into the bytes already in `out`.703 /// Average the prediction into the bytes already in `out`.
547 Avg,704 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+ },
548 }713 }
549 714
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`.
550 #[inline]717 #[inline]
551-fn store_u8(dst: &mut u8, v: u8, put: Put) {718+fn store_weighted(dst: &mut u8, v: u8, w0: i32, w1: i32, shift: u32, round: i32, off: i32) {
552- if put == Put::Avg {719+ let p0 = i32::from(*dst);
553- *dst = avg(*dst, v);720+ *dst = clip8((((p0 * w0 + i32::from(v) * w1 + round) >> shift) + off) as i32);
554- } else {721+}
555- *dst = v;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);
556 }783 }
557 }784 }
558 785