-
Notifications
You must be signed in to change notification settings - Fork 253
/
lrf.rs
1541 lines (1419 loc) · 47.6 KB
/
lrf.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017-2019, The rav1e contributors. All rights reserved
//
// This source code is subject to the terms of the BSD 2 Clause License and
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
// was not distributed with this source code in the LICENSE file, you can
// obtain it at www.aomedia.org/license/software. If the Alliance for Open
// Media Patent License 1.0 was not distributed with this source code in the
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
cfg_if::cfg_if! {
if #[cfg(nasm_x86_64)] {
use crate::asm::x86::lrf::*;
} else {
use self::rust::*;
}
}
use crate::context::PLANES;
use crate::context::SB_SIZE;
use crate::encoder::FrameInvariants;
use crate::frame::AsRegion;
use crate::frame::Frame;
use crate::frame::Plane;
use crate::frame::PlaneConfig;
use crate::frame::PlaneOffset;
use crate::frame::PlaneSlice;
use crate::hawktracer::*;
use crate::tiling::{Area, PlaneRegionMut, Rect};
use crate::util::clamp;
use crate::util::CastFromPrimitive;
use crate::util::ILog;
use crate::util::Pixel;
use crate::api::SGRComplexityLevel;
use std::cmp;
use std::iter::FusedIterator;
use std::ops::{Index, IndexMut};
pub const RESTORATION_TILESIZE_MAX_LOG2: usize = 8;
pub const RESTORE_NONE: u8 = 0;
pub const RESTORE_SWITCHABLE: u8 = 1;
pub const RESTORE_WIENER: u8 = 2;
pub const RESTORE_SGRPROJ: u8 = 3;
pub const WIENER_TAPS_MIN: [i8; 3] = [-5, -23, -17];
pub const WIENER_TAPS_MID: [i8; 3] = [3, -7, 15];
pub const WIENER_TAPS_MAX: [i8; 3] = [10, 8, 46];
#[allow(unused)]
pub const WIENER_TAPS_K: [i8; 3] = [1, 2, 3];
pub const WIENER_BITS: usize = 7;
pub const SGRPROJ_XQD_MIN: [i8; 2] = [-96, -32];
pub const SGRPROJ_XQD_MID: [i8; 2] = [-32, 31];
pub const SGRPROJ_XQD_MAX: [i8; 2] = [31, 95];
pub const SGRPROJ_PRJ_SUBEXP_K: u8 = 4;
pub const SGRPROJ_PRJ_BITS: u8 = 7;
pub const SGRPROJ_PARAMS_BITS: u8 = 4;
pub const SGRPROJ_MTABLE_BITS: u8 = 20;
pub const SGRPROJ_SGR_BITS: u8 = 8;
pub const SGRPROJ_RECIP_BITS: u8 = 12;
pub const SGRPROJ_RST_BITS: u8 = 4;
pub const SGRPROJ_PARAMS_S: [[u32; 2]; 1 << SGRPROJ_PARAMS_BITS] = [
[140, 3236],
[112, 2158],
[93, 1618],
[80, 1438],
[70, 1295],
[58, 1177],
[47, 1079],
[37, 996],
[30, 925],
[25, 863],
[0, 2589],
[0, 1618],
[0, 1177],
[0, 925],
[56, 0],
[22, 0],
];
// List of indices to SGRPROJ_PARAMS_S values that at a given complexity level.
// SGRPROJ_ALL_SETS contains every possible index
const SGRPROJ_ALL_SETS: &[u8] =
&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
// SGRPROJ_REDUCED_SETS has half of the values. Using only these values gives
// most of the gains from sgr. The decision of which values to use is somewhat
// arbitrary. The sgr parameters has 3 discontinuous groups. The first has both
// parameters as non-zero. The other two are distinguishable by which of the
// two parameters is zero. There are an even number of each of these groups and
// the non-zero parameters grow as the indices increase. This array uses the
// 1nd, 3rd, ... smallest params of each group.
const SGRPROJ_REDUCED_SETS: &[u8] = &[1, 3, 5, 7, 9, 11, 13, 15];
pub fn get_sgr_sets(complexity: SGRComplexityLevel) -> &'static [u8] {
match complexity {
SGRComplexityLevel::Full => SGRPROJ_ALL_SETS,
SGRComplexityLevel::Reduced => SGRPROJ_REDUCED_SETS,
}
}
pub const SOLVE_IMAGE_MAX: usize = 1 << RESTORATION_TILESIZE_MAX_LOG2;
pub const SOLVE_IMAGE_STRIDE: usize = SOLVE_IMAGE_MAX + 6 + 2;
pub const SOLVE_IMAGE_HEIGHT: usize = SOLVE_IMAGE_STRIDE;
pub const SOLVE_IMAGE_SIZE: usize = SOLVE_IMAGE_STRIDE * SOLVE_IMAGE_HEIGHT;
pub const STRIPE_IMAGE_MAX: usize = (1 << RESTORATION_TILESIZE_MAX_LOG2)
+ (1 << (RESTORATION_TILESIZE_MAX_LOG2 - 1));
pub const STRIPE_IMAGE_STRIDE: usize = STRIPE_IMAGE_MAX + 6 + 2;
pub const STRIPE_IMAGE_HEIGHT: usize = 64 + 6 + 2;
pub const STRIPE_IMAGE_SIZE: usize = STRIPE_IMAGE_STRIDE * STRIPE_IMAGE_HEIGHT;
pub const IMAGE_WIDTH_MAX: usize = [STRIPE_IMAGE_MAX, SOLVE_IMAGE_MAX]
[(STRIPE_IMAGE_MAX < SOLVE_IMAGE_MAX) as usize];
/// The buffer used in `sgrproj_stripe_filter()` and `sgrproj_solve()`.
#[derive(Debug)]
pub struct IntegralImageBuffer {
pub integral_image: Vec<u32>,
pub sq_integral_image: Vec<u32>,
}
impl IntegralImageBuffer {
/// Creates a new buffer with the given size, filled with zeros.
#[inline]
pub fn zeroed(size: usize) -> Self {
Self { integral_image: vec![0; size], sq_integral_image: vec![0; size] }
}
}
#[allow(unused)] // Wiener coming soon!
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum RestorationFilter {
None,
Wiener { coeffs: [[i8; 3]; 2] },
Sgrproj { set: u8, xqd: [i8; 2] },
}
impl Default for RestorationFilter {
fn default() -> RestorationFilter {
RestorationFilter::None
}
}
impl RestorationFilter {
pub fn notequal(self, cmp: RestorationFilter) -> bool {
match self {
RestorationFilter::None {} => {
if let RestorationFilter::None {} = cmp {
false
} else {
true
}
}
RestorationFilter::Sgrproj { set, xqd } => {
if let RestorationFilter::Sgrproj { set: set2, xqd: xqd2 } = cmp {
!(set == set2 && xqd[0] == xqd2[0] && xqd[1] == xqd2[1])
} else {
true
}
}
RestorationFilter::Wiener { coeffs } => {
if let RestorationFilter::Wiener { coeffs: coeffs2 } = cmp {
!(coeffs[0][0] == coeffs2[0][0]
&& coeffs[0][1] == coeffs2[0][1]
&& coeffs[0][2] == coeffs2[0][2]
&& coeffs[1][0] == coeffs2[1][0]
&& coeffs[1][1] == coeffs2[1][1]
&& coeffs[1][2] == coeffs2[1][2])
} else {
true
}
}
}
}
}
pub(crate) mod rust {
use crate::cpu_features::CpuFeatureLevel;
use crate::frame::PlaneSlice;
use crate::lrf::{
get_integral_square, sgrproj_sum_finish, SGRPROJ_RST_BITS,
SGRPROJ_SGR_BITS,
};
use crate::util::CastFromPrimitive;
use crate::Pixel;
#[inline(always)]
pub(crate) fn sgrproj_box_ab_internal(
r: usize, af: &mut [u32], bf: &mut [u32], iimg: &[u32], iimg_sq: &[u32],
iimg_stride: usize, start_x: usize, y: usize, stripe_w: usize, s: u32,
bdm8: usize,
) {
let d: usize = r * 2 + 1;
let n: usize = d * d;
let one_over_n = if r == 1 { 455 } else { 164 };
for x in start_x..stripe_w + 2 {
let sum = get_integral_square(iimg, iimg_stride, x, y, d);
let ssq = get_integral_square(iimg_sq, iimg_stride, x, y, d);
let (reta, retb) =
sgrproj_sum_finish(ssq, sum, n as u32, one_over_n, s, bdm8);
af[x] = reta;
bf[x] = retb;
}
}
// computes an intermediate (ab) row for stripe_w + 2 columns at row y
pub(crate) fn sgrproj_box_ab_r1(
af: &mut [u32], bf: &mut [u32], iimg: &[u32], iimg_sq: &[u32],
iimg_stride: usize, y: usize, stripe_w: usize, s: u32, bdm8: usize,
_cpu: CpuFeatureLevel,
) {
sgrproj_box_ab_internal(
1,
af,
bf,
iimg,
iimg_sq,
iimg_stride,
0,
y,
stripe_w,
s,
bdm8,
);
}
// computes an intermediate (ab) row for stripe_w + 2 columns at row y
pub(crate) fn sgrproj_box_ab_r2(
af: &mut [u32], bf: &mut [u32], iimg: &[u32], iimg_sq: &[u32],
iimg_stride: usize, y: usize, stripe_w: usize, s: u32, bdm8: usize,
_cpu: CpuFeatureLevel,
) {
sgrproj_box_ab_internal(
2,
af,
bf,
iimg,
iimg_sq,
iimg_stride,
0,
y,
stripe_w,
s,
bdm8,
);
}
pub(crate) fn sgrproj_box_f_r0<T: Pixel>(
f: &mut [u32], y: usize, w: usize, cdeffed: &PlaneSlice<T>,
_cpu: CpuFeatureLevel,
) {
sgrproj_box_f_r0_internal(f, 0, y, w, cdeffed);
}
#[inline(always)]
pub(crate) fn sgrproj_box_f_r0_internal<T: Pixel>(
f: &mut [u32], start_x: usize, y: usize, w: usize, cdeffed: &PlaneSlice<T>,
) {
for x in start_x..w {
f[x] = (u32::cast_from(cdeffed.p(x, y))) << SGRPROJ_RST_BITS;
}
}
pub(crate) fn sgrproj_box_f_r1<T: Pixel>(
af: &[&[u32]; 3], bf: &[&[u32]; 3], f: &mut [u32], y: usize, w: usize,
cdeffed: &PlaneSlice<T>, _cpu: CpuFeatureLevel,
) {
sgrproj_box_f_r1_internal(af, bf, f, 0, y, w, cdeffed);
}
#[inline(always)]
pub(crate) fn sgrproj_box_f_r1_internal<T: Pixel>(
af: &[&[u32]; 3], bf: &[&[u32]; 3], f: &mut [u32], start_x: usize,
y: usize, w: usize, cdeffed: &PlaneSlice<T>,
) {
let shift = 5 + SGRPROJ_SGR_BITS - SGRPROJ_RST_BITS;
for x in start_x..w {
let a = 3 * (af[0][x] + af[2][x] + af[0][x + 2] + af[2][x + 2])
+ 4
* (af[1][x]
+ af[0][x + 1]
+ af[1][x + 1]
+ af[2][x + 1]
+ af[1][x + 2]);
let b = 3 * (bf[0][x] + bf[2][x] + bf[0][x + 2] + bf[2][x + 2])
+ 4
* (bf[1][x]
+ bf[0][x + 1]
+ bf[1][x + 1]
+ bf[2][x + 1]
+ bf[1][x + 2]);
let v = a * u32::cast_from(cdeffed.p(x, y)) + b;
f[x] = (v + (1 << shift >> 1)) >> shift;
}
}
pub(crate) fn sgrproj_box_f_r2<T: Pixel>(
af: &[&[u32]; 2], bf: &[&[u32]; 2], f0: &mut [u32], f1: &mut [u32],
y: usize, w: usize, cdeffed: &PlaneSlice<T>, _cpu: CpuFeatureLevel,
) {
sgrproj_box_f_r2_internal(af, bf, f0, f1, 0, y, w, cdeffed);
}
#[inline(always)]
pub(crate) fn sgrproj_box_f_r2_internal<T: Pixel>(
af: &[&[u32]; 2], bf: &[&[u32]; 2], f0: &mut [u32], f1: &mut [u32],
start_x: usize, y: usize, w: usize, cdeffed: &PlaneSlice<T>,
) {
let shift = 5 + SGRPROJ_SGR_BITS - SGRPROJ_RST_BITS;
let shifto = 4 + SGRPROJ_SGR_BITS - SGRPROJ_RST_BITS;
for x in start_x..w {
let a = 5 * (af[0][x] + af[0][x + 2]) + 6 * (af[0][x + 1]);
let b = 5 * (bf[0][x] + bf[0][x + 2]) + 6 * (bf[0][x + 1]);
let ao = 5 * (af[1][x] + af[1][x + 2]) + 6 * (af[1][x + 1]);
let bo = 5 * (bf[1][x] + bf[1][x + 2]) + 6 * (bf[1][x + 1]);
let v = (a + ao) * u32::cast_from(cdeffed.p(x, y)) + b + bo;
f0[x] = (v + (1 << shift >> 1)) >> shift;
let vo = ao * u32::cast_from(cdeffed.p(x, y + 1)) + bo;
f1[x] = (vo + (1 << shifto >> 1)) >> shifto;
}
}
}
#[inline(always)]
fn sgrproj_sum_finish(
ssq: u32, sum: u32, n: u32, one_over_n: u32, s: u32, bdm8: usize,
) -> (u32, u32) {
let scaled_ssq = (ssq + (1 << (2 * bdm8) >> 1)) >> (2 * bdm8);
let scaled_sum = (sum + (1 << bdm8 >> 1)) >> bdm8;
let p =
cmp::max(0, (scaled_ssq * n) as i32 - (scaled_sum * scaled_sum) as i32)
as u32;
let z = (p * s + (1 << SGRPROJ_MTABLE_BITS >> 1)) >> SGRPROJ_MTABLE_BITS;
let a = if z >= 255 {
256
} else if z == 0 {
1
} else {
((z << SGRPROJ_SGR_BITS) + z / 2) / (z + 1)
};
let b = ((1 << SGRPROJ_SGR_BITS) - a) * sum * one_over_n;
(a, (b + (1 << SGRPROJ_RECIP_BITS >> 1)) >> SGRPROJ_RECIP_BITS)
}
// Using an integral image, compute the sum of a square region
fn get_integral_square(
iimg: &[u32], stride: usize, x: usize, y: usize, size: usize,
) -> u32 {
// Cancel out overflow in iimg by using wrapping arithmetic
iimg[y * stride + x]
.wrapping_add(iimg[(y + size) * stride + x + size])
.wrapping_sub(iimg[(y + size) * stride + x])
.wrapping_sub(iimg[y * stride + x + size])
}
struct VertPaddedIter<'a, T: Pixel> {
// The two sources that can be selected when clipping
deblocked: &'a Plane<T>,
cdeffed: &'a Plane<T>,
// x index to choice where on the row to start
x: isize,
// y index that will be mutated
y: isize,
// The index at which to terminate. Can be larger than the slice length.
end: isize,
// Used for source buffer choice/clipping. May (and regularly will)
// be negative.
stripe_begin: isize,
// Also used for source buffer choice/clipping. May specify a stripe boundary
// less than, equal to, or larger than the buffers we're accessing.
stripe_end: isize,
// Active area cropping is done by specifying a value smaller than the height
// of the plane.
crop: isize,
}
impl<'a, 'b, T: Pixel> VertPaddedIter<'a, T> {
fn new(
cdeffed: &PlaneSlice<'a, T>, deblocked: &PlaneSlice<'a, T>,
stripe_h: usize, crop: usize,
) -> VertPaddedIter<'a, T> {
// cdeffed and deblocked must start at the same coordinates from their
// underlying planes. Since cropping is provided via a separate params, the
// height of the underlying planes do not need to match.
assert_eq!(cdeffed.x, deblocked.x);
assert_eq!(cdeffed.y, deblocked.y);
// To share integral images, always use the max box filter radius of 2
let r = 2;
// The number of rows outside the stripe are needed
let rows_above = r + 2;
let rows_below = 2;
// Offset crop and stripe_h so they are relative to the underlying plane
// and not the plane slice.
let crop = crop as isize + deblocked.y;
let stripe_end = stripe_h as isize + deblocked.y;
// Move y up the number rows above.
// If y is negative we repeat the first row
let y = deblocked.y - rows_above as isize;
VertPaddedIter {
deblocked: deblocked.plane,
cdeffed: cdeffed.plane,
x: deblocked.x,
y,
end: (rows_above + stripe_h + rows_below) as isize + y,
stripe_begin: deblocked.y,
stripe_end,
crop,
}
}
}
impl<'a, T: Pixel> Iterator for VertPaddedIter<'a, T> {
type Item = &'a [T];
#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
if self.end > self.y {
// clamp before deciding the source
// clamp vertically to storage at top and passed-in height at bottom
let cropped_y = clamp(self.y, 0, self.crop - 1);
// clamp vertically to stripe limits
let ly = clamp(cropped_y, self.stripe_begin - 2, self.stripe_end + 1);
// decide if we're vertically inside or outside the strip
let src_plane =
if ly >= self.stripe_begin && ly < self.stripe_end as isize {
self.cdeffed
} else {
self.deblocked
};
// cannot directly return self.ps.row(row) due to lifetime issue
let range = src_plane.row_range(self.x, ly);
self.y += 1;
Some(&src_plane.data[range])
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.end - self.y;
debug_assert!(remaining >= 0);
let remaining = remaining as usize;
(remaining, Some(remaining))
}
}
impl<T: Pixel> ExactSizeIterator for VertPaddedIter<'_, T> {}
impl<T: Pixel> FusedIterator for VertPaddedIter<'_, T> {}
struct HorzPaddedIter<'a, T: Pixel> {
// Active area cropping is done using the length of the slice
slice: &'a [T],
// x index of the iterator
// When less than 0, repeat the first element. When greater than end, repeat
// the last element
index: isize,
// The index at which to terminate. Can be larger than the slice length.
end: usize,
}
impl<'a, T: Pixel> HorzPaddedIter<'a, T> {
fn new(
slice: &'a [T], start_index: isize, width: usize,
) -> HorzPaddedIter<'a, T> {
HorzPaddedIter {
slice,
index: start_index,
end: (width as isize + start_index) as usize,
}
}
}
impl<'a, T: Pixel> Iterator for HorzPaddedIter<'a, T> {
type Item = &'a T;
#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.end as isize {
// clamp to the edges of the frame
let x = clamp(self.index, 0, self.slice.len() as isize - 1) as usize;
self.index += 1;
Some(&self.slice[x])
} else {
None
}
}
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
let size: usize = (self.end as isize - self.index) as usize;
(size, Some(size))
}
}
impl<T: Pixel> ExactSizeIterator for HorzPaddedIter<'_, T> {}
impl<T: Pixel> FusedIterator for HorzPaddedIter<'_, T> {}
pub fn setup_integral_image<T: Pixel>(
integral_image_buffer: &mut IntegralImageBuffer,
integral_image_stride: usize, crop_w: usize, crop_h: usize, stripe_w: usize,
stripe_h: usize, cdeffed: &PlaneSlice<T>, deblocked: &PlaneSlice<T>,
) {
let integral_image = &mut integral_image_buffer.integral_image;
let sq_integral_image = &mut integral_image_buffer.sq_integral_image;
// Number of elements outside the stripe
let left_w = 4; // max radius of 2 + 2 padding
let right_w = 3; // max radius of 2 + 1 padding
assert_eq!(cdeffed.x, deblocked.x);
// Find how many unique elements to use to the left and right
let left_uniques = if cdeffed.x == 0 { 0 } else { left_w };
let right_uniques = right_w.min(crop_w - stripe_w);
// Find the total number of unique elements used
let row_uniques = left_uniques + stripe_w + right_uniques;
// Negative start indices result in repeating the first element of the row
let start_index_x = if cdeffed.x == 0 { -(left_w as isize) } else { 0 };
let mut rows_iter = VertPaddedIter::new(
// Move left to encompass all the used data
&cdeffed.go_left(left_uniques),
&deblocked.go_left(left_uniques),
// since r2 uses every other row, we need an extra row if stripe_h is odd
stripe_h + (stripe_h & 1),
crop_h,
)
.map(|row: &[T]| {
HorzPaddedIter::new(
// Limit how many unique elements we use
&row[..row_uniques],
start_index_x,
left_w + stripe_w + right_w,
)
});
// Setup the first row
{
let mut sum: u32 = 0;
let mut sq_sum: u32 = 0;
// Remove the first row and use it outside of the main loop
let row = rows_iter.next().unwrap();
for (src, (integral, sq_integral)) in
row.zip(integral_image.iter_mut().zip(sq_integral_image.iter_mut()))
{
let current = u32::cast_from(*src);
// Wrap adds to prevent undefined behaviour on overflow. Overflow is
// cancelled out when calculating the sum of a region.
sum = sum.wrapping_add(current);
*integral = sum;
sq_sum = sq_sum.wrapping_add(current * current);
*sq_integral = sq_sum;
}
}
// Calculate all other rows
let mut integral_slice = &mut integral_image[..];
let mut sq_integral_slice = &mut sq_integral_image[..];
for row in rows_iter {
let mut sum: u32 = 0;
let mut sq_sum: u32 = 0;
// Split the data between the previous row and future rows.
// This allows us to mutate the current row while accessing the
// previous row.
let (integral_row_prev, integral_row) =
integral_slice.split_at_mut(integral_image_stride);
let (sq_integral_row_prev, sq_integral_row) =
sq_integral_slice.split_at_mut(integral_image_stride);
for (
src,
((integral_above, sq_integral_above), (integral, sq_integral)),
) in row.zip(
integral_row_prev
.iter()
.zip(sq_integral_row_prev.iter())
.zip(integral_row.iter_mut().zip(sq_integral_row.iter_mut())),
) {
let current = u32::cast_from(*src);
// Wrap adds to prevent undefined behaviour on overflow. Overflow is
// cancelled out when calculating the sum of a region.
sum = sum.wrapping_add(current);
*integral = sum.wrapping_add(*integral_above);
sq_sum = sq_sum.wrapping_add(current * current);
*sq_integral = sq_sum.wrapping_add(*sq_integral_above);
}
// The current row also contains all future rows. Replacing the slice with
// it moves down a row.
integral_slice = integral_row;
sq_integral_slice = sq_integral_row;
}
}
pub fn sgrproj_stripe_filter<T: Pixel, U: Pixel>(
set: u8, xqd: [i8; 2], fi: &FrameInvariants<T>,
integral_image_buffer: &IntegralImageBuffer, integral_image_stride: usize,
cdeffed: &PlaneSlice<U>, out: &mut PlaneRegionMut<U>,
) {
let &Rect { width: stripe_w, height: stripe_h, .. } = out.rect();
let bdm8 = fi.sequence.bit_depth - 8;
let mut a_r2: [[u32; IMAGE_WIDTH_MAX + 2]; 2] =
[[0; IMAGE_WIDTH_MAX + 2]; 2];
let mut b_r2: [[u32; IMAGE_WIDTH_MAX + 2]; 2] =
[[0; IMAGE_WIDTH_MAX + 2]; 2];
let mut f_r2_0: [u32; IMAGE_WIDTH_MAX] = [0; IMAGE_WIDTH_MAX];
let mut f_r2_1: [u32; IMAGE_WIDTH_MAX] = [0; IMAGE_WIDTH_MAX];
let mut a_r1: [[u32; IMAGE_WIDTH_MAX + 2]; 3] =
[[0; IMAGE_WIDTH_MAX + 2]; 3];
let mut b_r1: [[u32; IMAGE_WIDTH_MAX + 2]; 3] =
[[0; IMAGE_WIDTH_MAX + 2]; 3];
let mut f_r1: [u32; IMAGE_WIDTH_MAX] = [0; IMAGE_WIDTH_MAX];
let s_r2: u32 = SGRPROJ_PARAMS_S[set as usize][0];
let s_r1: u32 = SGRPROJ_PARAMS_S[set as usize][1];
/* prime the intermediate arrays */
// One oddness about the radius=2 intermediate array computations that
// the spec doesn't make clear: Although the spec defines computation
// of every row (of a, b and f), only half of the rows (every-other
// row) are actually used.
let integral_image = &integral_image_buffer.integral_image;
let sq_integral_image = &integral_image_buffer.sq_integral_image;
if s_r2 > 0 {
sgrproj_box_ab_r2(
&mut a_r2[0],
&mut b_r2[0],
integral_image,
sq_integral_image,
integral_image_stride,
0,
stripe_w,
s_r2,
bdm8,
fi.cpu_feature_level,
);
}
if s_r1 > 0 {
let integral_image_offset = integral_image_stride + 1;
sgrproj_box_ab_r1(
&mut a_r1[0],
&mut b_r1[0],
&integral_image[integral_image_offset..],
&sq_integral_image[integral_image_offset..],
integral_image_stride,
0,
stripe_w,
s_r1,
bdm8,
fi.cpu_feature_level,
);
sgrproj_box_ab_r1(
&mut a_r1[1],
&mut b_r1[1],
&integral_image[integral_image_offset..],
&sq_integral_image[integral_image_offset..],
integral_image_stride,
1,
stripe_w,
s_r1,
bdm8,
fi.cpu_feature_level,
);
}
/* iterate by row */
// Increment by two to handle the use of even rows by r=2 and run a nested
// loop to handle increments of one.
for y in (0..stripe_h).step_by(2) {
// get results to use y and y+1
let f_r2_ab: [&[u32]; 2] = if s_r2 > 0 {
sgrproj_box_ab_r2(
&mut a_r2[(y / 2 + 1) % 2],
&mut b_r2[(y / 2 + 1) % 2],
integral_image,
sq_integral_image,
integral_image_stride,
y + 2,
stripe_w,
s_r2,
bdm8,
fi.cpu_feature_level,
);
let ap0: [&[u32]; 2] = [&a_r2[(y / 2) % 2], &a_r2[(y / 2 + 1) % 2]];
let bp0: [&[u32]; 2] = [&b_r2[(y / 2) % 2], &b_r2[(y / 2 + 1) % 2]];
sgrproj_box_f_r2(
&ap0,
&bp0,
&mut f_r2_0,
&mut f_r2_1,
y,
stripe_w,
cdeffed,
fi.cpu_feature_level,
);
[&f_r2_0, &f_r2_1]
} else {
sgrproj_box_f_r0(
&mut f_r2_0,
y,
stripe_w,
cdeffed,
fi.cpu_feature_level,
);
// share results for both rows
[&f_r2_0, &f_r2_0]
};
for dy in 0..(2.min(stripe_h - y)) {
let y = y + dy;
if s_r1 > 0 {
let integral_image_offset = integral_image_stride + 1;
sgrproj_box_ab_r1(
&mut a_r1[(y + 2) % 3],
&mut b_r1[(y + 2) % 3],
&integral_image[integral_image_offset..],
&sq_integral_image[integral_image_offset..],
integral_image_stride,
y + 2,
stripe_w,
s_r1,
bdm8,
fi.cpu_feature_level,
);
let ap1: [&[u32]; 3] =
[&a_r1[y % 3], &a_r1[(y + 1) % 3], &a_r1[(y + 2) % 3]];
let bp1: [&[u32]; 3] =
[&b_r1[y % 3], &b_r1[(y + 1) % 3], &b_r1[(y + 2) % 3]];
sgrproj_box_f_r1(
&ap1,
&bp1,
&mut f_r1,
y,
stripe_w,
cdeffed,
fi.cpu_feature_level,
);
} else {
sgrproj_box_f_r0(
&mut f_r1,
y,
stripe_w,
cdeffed,
fi.cpu_feature_level,
);
}
/* apply filter */
let w0 = xqd[0] as i32;
let w1 = xqd[1] as i32;
let w2 = (1 << SGRPROJ_PRJ_BITS) - w0 - w1;
let line = &cdeffed[y];
#[inline(always)]
fn apply_filter<U: Pixel>(
out: &mut [U], line: &[U], f_r1: &[u32], f_r2_ab: &[u32],
stripe_w: usize, bit_depth: usize, w0: i32, w1: i32, w2: i32,
) {
let line_it = line[..stripe_w].iter();
let f_r2_ab_it = f_r2_ab[..stripe_w].iter();
let f_r1_it = f_r1[..stripe_w].iter();
let out_it = out[..stripe_w].iter_mut();
for ((o, &u), (&f_r2_ab, &f_r1)) in
out_it.zip(line_it).zip(f_r2_ab_it.zip(f_r1_it))
{
let u = i32::cast_from(u) << SGRPROJ_RST_BITS;
let v = w0 * f_r2_ab as i32 + w1 * u + w2 * f_r1 as i32;
let s = (v + (1 << (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) >> 1))
>> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS);
*o = U::cast_from(clamp(s, 0, (1 << bit_depth) - 1));
}
}
apply_filter(
&mut out[y],
line,
&f_r1,
f_r2_ab[dy],
stripe_w,
fi.sequence.bit_depth,
w0,
w1,
w2,
);
}
}
}
// Frame inputs below aren't all equal, and will change as work
// continues. There's no deblocked reconstruction available at this
// point of RDO, so we use the non-deblocked reconstruction, cdef and
// input. The input can be a full-sized frame. Cdef input is a partial
// frame constructed specifically for RDO.
// For simplicity, this ignores stripe segmentation (it's possible the
// extra complexity isn't worth it and we'll ignore stripes
// permanently during RDO, but that's not been tested yet). Data
// access inside the cdef frame is monolithic and clipped to the cdef
// borders.
// Input params follow the same rules as sgrproj_stripe_filter.
// Inputs are relative to the colocated slice views.
pub fn sgrproj_solve<T: Pixel>(
set: u8, fi: &FrameInvariants<T>,
integral_image_buffer: &IntegralImageBuffer, input: &PlaneSlice<u16>,
cdeffed: &PlaneSlice<u16>, cdef_w: usize, cdef_h: usize,
) -> (i8, i8) {
let bdm8 = fi.sequence.bit_depth - 8;
let mut a_r2: [[u32; IMAGE_WIDTH_MAX + 2]; 2] =
[[0; IMAGE_WIDTH_MAX + 2]; 2];
let mut b_r2: [[u32; IMAGE_WIDTH_MAX + 2]; 2] =
[[0; IMAGE_WIDTH_MAX + 2]; 2];
let mut f_r2_0: [u32; IMAGE_WIDTH_MAX] = [0; IMAGE_WIDTH_MAX];
let mut f_r2_1: [u32; IMAGE_WIDTH_MAX] = [0; IMAGE_WIDTH_MAX];
let mut a_r1: [[u32; IMAGE_WIDTH_MAX + 2]; 3] =
[[0; IMAGE_WIDTH_MAX + 2]; 3];
let mut b_r1: [[u32; IMAGE_WIDTH_MAX + 2]; 3] =
[[0; IMAGE_WIDTH_MAX + 2]; 3];
let mut f_r1: [u32; IMAGE_WIDTH_MAX] = [0; IMAGE_WIDTH_MAX];
let s_r2: u32 = SGRPROJ_PARAMS_S[set as usize][0];
let s_r1: u32 = SGRPROJ_PARAMS_S[set as usize][1];
let mut h: [[f64; 2]; 2] = [[0., 0.], [0., 0.]];
let mut c: [f64; 2] = [0., 0.];
/* prime the intermediate arrays */
// One oddness about the radius=2 intermediate array computations that
// the spec doesn't make clear: Although the spec defines computation
// of every row (of a, b and f), only half of the rows (every-other
// row) are actually used.
let integral_image = &integral_image_buffer.integral_image;
let sq_integral_image = &integral_image_buffer.sq_integral_image;
if s_r2 > 0 {
sgrproj_box_ab_r2(
&mut a_r2[0],
&mut b_r2[0],
integral_image,
sq_integral_image,
SOLVE_IMAGE_STRIDE,
0,
cdef_w,
s_r2,
bdm8,
fi.cpu_feature_level,
);
}
if s_r1 > 0 {
let integral_image_offset = SOLVE_IMAGE_STRIDE + 1;
sgrproj_box_ab_r1(
&mut a_r1[0],
&mut b_r1[0],
&integral_image[integral_image_offset..],
&sq_integral_image[integral_image_offset..],
SOLVE_IMAGE_STRIDE,
0,
cdef_w,
s_r1,
bdm8,
fi.cpu_feature_level,
);
sgrproj_box_ab_r1(
&mut a_r1[1],
&mut b_r1[1],
&integral_image[integral_image_offset..],
&sq_integral_image[integral_image_offset..],
SOLVE_IMAGE_STRIDE,
1,
cdef_w,
s_r1,
bdm8,
fi.cpu_feature_level,
);
}
/* iterate by row */
// Increment by two to handle the use of even rows by r=2 and run a nested
// loop to handle increments of one.
for y in (0..cdef_h).step_by(2) {
// get results to use y and y+1
let f_r2_01: [&[u32]; 2] = if s_r2 > 0 {
sgrproj_box_ab_r2(
&mut a_r2[(y / 2 + 1) % 2],
&mut b_r2[(y / 2 + 1) % 2],
integral_image,
sq_integral_image,
SOLVE_IMAGE_STRIDE,
y + 2,
cdef_w,
s_r2,
bdm8,
fi.cpu_feature_level,
);
let ap0: [&[u32]; 2] = [&a_r2[(y / 2) % 2], &a_r2[(y / 2 + 1) % 2]];
let bp0: [&[u32]; 2] = [&b_r2[(y / 2) % 2], &b_r2[(y / 2 + 1) % 2]];
sgrproj_box_f_r2(
&ap0,
&bp0,
&mut f_r2_0,
&mut f_r2_1,
y,
cdef_w,
cdeffed,
fi.cpu_feature_level,
);
[&f_r2_0, &f_r2_1]
} else {
sgrproj_box_f_r0(&mut f_r2_0, y, cdef_w, cdeffed, fi.cpu_feature_level);
// share results for both rows
[&f_r2_0, &f_r2_0]
};
for dy in 0..(2.min(cdef_h - y)) {
let y = y + dy;
if s_r1 > 0 {
let integral_image_offset = SOLVE_IMAGE_STRIDE + 1;
sgrproj_box_ab_r1(
&mut a_r1[(y + 2) % 3],
&mut b_r1[(y + 2) % 3],
&integral_image[integral_image_offset..],
&sq_integral_image[integral_image_offset..],
SOLVE_IMAGE_STRIDE,
y + 2,
cdef_w,
s_r1,
bdm8,
fi.cpu_feature_level,
);
let ap1: [&[u32]; 3] =
[&a_r1[y % 3], &a_r1[(y + 1) % 3], &a_r1[(y + 2) % 3]];
let bp1: [&[u32]; 3] =
[&b_r1[y % 3], &b_r1[(y + 1) % 3], &b_r1[(y + 2) % 3]];
sgrproj_box_f_r1(
&ap1,
&bp1,
&mut f_r1,
y,
cdef_w,
cdeffed,
fi.cpu_feature_level,
);
} else {
sgrproj_box_f_r0(&mut f_r1, y, cdef_w, cdeffed, fi.cpu_feature_level);
}
#[inline(always)]
fn process_line<T: Pixel>(
h: &mut [[f64; 2]; 2], c: &mut [f64; 2], cdeffed: &[T], input: &[T],
f_r1: &[u32], f_r2_ab: &[u32], cdef_w: usize,
) {
let cdeffed_it = cdeffed[..cdef_w].iter();
let input_it = input[..cdef_w].iter();
let f_r2_ab_it = f_r2_ab[..cdef_w].iter();
let f_r1_it = f_r1[..cdef_w].iter();
#[derive(Debug, Copy, Clone)]
struct Sums {
h: [[i64; 2]; 2],
c: [i64; 2],
}
let sums: Sums = cdeffed_it
.zip(input_it)
.zip(f_r2_ab_it.zip(f_r1_it))
.map(|((&u, &i), (&f2, &f1))| {
let u = i32::cast_from(u) << SGRPROJ_RST_BITS;
let s = (i32::cast_from(i) << SGRPROJ_RST_BITS) - u;
let f2 = f2 as i32 - u;
let f1 = f1 as i32 - u;
(s as i64, f1 as i64, f2 as i64)
})
.fold(Sums { h: [[0; 2]; 2], c: [0; 2] }, |sums, (s, f1, f2)| {
let mut ret: Sums = sums;
ret.h[0][0] += f2 * f2;
ret.h[1][1] += f1 * f1;
ret.h[0][1] += f1 * f2;
ret.c[0] += f2 * s;
ret.c[1] += f1 * s;
ret
});
h[0][0] += sums.h[0][0] as f64;
h[1][1] += sums.h[1][1] as f64;
h[0][1] += sums.h[0][1] as f64;
c[0] += sums.c[0] as f64;
c[1] += sums.c[1] as f64;
}