lrf.rs 36.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
// Copyright (c) 2017-2018, 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.

#![allow(safe_extern_statics)]

Luca Barbato's avatar
Luca Barbato committed
12
use crate::frame::Frame;
Raphaël Zumer's avatar
Raphaël Zumer committed
13
14
15
use crate::encoder::FrameInvariants;
use crate::context::PLANES;
use crate::context::MAX_SB_SIZE;
16
17
18
19
20
use crate::frame::Plane;
use crate::frame::PlaneSlice;
use crate::frame::PlaneMutSlice;
use crate::frame::PlaneOffset;
use crate::frame::PlaneConfig;
21
use std::cmp;
Raphaël Zumer's avatar
Raphaël Zumer committed
22
use crate::util::clamp;
23
24
use crate::util::CastFromPrimitive;
use crate::util::Pixel;
25

26
27
use std::ops::{Index, IndexMut};

28
pub const RESTORATION_TILESIZE_MAX_LOG2: usize = 8;
29

30
31
32
33
34
pub const RESTORE_NONE: u8 = 0;
pub const RESTORE_SWITCHABLE: u8 = 1;
pub const RESTORE_WIENER: u8 = 2;
pub const RESTORE_SGRPROJ: u8 = 3;

35
36
37
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 ];
38
#[allow(unused)]
39
pub const WIENER_TAPS_K:   [i8; 3] = [ 1, 2, 3 ];
Monty's avatar
Monty committed
40
pub const WIENER_BITS: usize = 7;
41
42
43
44
45
46
47

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;
Monty's avatar
Monty committed
48
49
50
51
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;
Monty's avatar
Monty committed
52
pub const SGRPROJ_PARAMS_S: [[u32; 2]; 1 << SGRPROJ_PARAMS_BITS] = [
53
54
55
56
  [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]
57
58
];

Romain Vimont's avatar
Romain Vimont committed
59
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
60
61
62
pub enum RestorationFilter {
  None,
  Wiener  { coeffs: [[i8; 3]; 2] },
Monty's avatar
Monty committed
63
  Sgrproj { set: u8,
64
65
66
67
68
            xqd: [i8; 2] },
}

impl RestorationFilter {
  pub fn default() -> RestorationFilter {
69
    RestorationFilter::None{}
Monty's avatar
Monty committed
70
71
72
  }
}

73
#[inline(always)]
Monty's avatar
Monty committed
74
fn sgrproj_sum_finish(ssq: u32, sum: u32, n: u32, one_over_n: u32, s: u32, bdm8: usize) -> (u32, u32) {
75
76
  let scaled_ssq = (ssq + (1 << (2 * bdm8) >> 1)) >> (2 * bdm8);
  let scaled_sum = (sum + (1 << bdm8 >> 1)) >> bdm8;
Monty's avatar
Monty committed
77
78
79
  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 {
80
81
82
83
    256
  } else if z == 0 {
    1
  } else {
Monty's avatar
Monty committed
84
    (((z << SGRPROJ_SGR_BITS) + z/2) / (z+1))
85
  };
86
87
  let b = ((1 << SGRPROJ_SGR_BITS) - a) * sum * one_over_n;
  (a, (b + (1 << SGRPROJ_RECIP_BITS >> 1)) >> SGRPROJ_RECIP_BITS)
88
}
Monty's avatar
Monty committed
89

90
91
92
93
94
95
96
97
98
// 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])
99
100
}

101
102
// computes an intermediate (ab) column for stripe_h + 2 rows at
// column stripe_x.
103
// r=1 case computes every row as every row is used (see r2 version below)
104
105
106
107
108
109
110
111
112
113
114
115
fn sgrproj_box_ab_r1(
  af: &mut [u32; 64 + 2], bf: &mut [u32; 64 + 2], iimg: &[u32],
  iimg_sq: &[u32], iimg_stride: usize, stripe_x: isize, stripe_h: usize,
  s: u32, bdm8: usize
) {
  for y in 0..stripe_h + 2 {
    let sum = get_integral_square(iimg, iimg_stride, stripe_x as usize, y, 3);
    let ssq =
      get_integral_square(iimg_sq, iimg_stride, stripe_x as usize, y, 3);
    let (reta, retb) = sgrproj_sum_finish(ssq, sum, 9, 455, s, bdm8);
    af[y] = reta;
    bf[y] = retb;
Monty's avatar
Monty committed
116
  }
Monty's avatar
Monty committed
117
118
}

119
120
121
122
123
124
125
// 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.  We use the full-size array here but only
// compute the even rows.  This is not so much optimization as trying
// to illustrate what this convoluted filter is actually doing
// (ie not as much as it may appear).
126
127
128
129
130
131
132
133
134
135
136
137
fn sgrproj_box_ab_r2(
  af: &mut [u32; 64 + 2], bf: &mut [u32; 64 + 2], iimg: &[u32],
  iimg_sq: &[u32], iimg_stride: usize, stripe_x: isize, stripe_h: usize,
  s: u32, bdm8: usize
) {
  for y in (0..stripe_h + 2).step_by(2) {
    let sum = get_integral_square(iimg, iimg_stride, stripe_x as usize, y, 5);
    let ssq =
      get_integral_square(iimg_sq, iimg_stride, stripe_x as usize, y, 5);
    let (reta, retb) = sgrproj_sum_finish(ssq, sum, 25, 164, s, bdm8);
    af[y] = reta;
    bf[y] = retb;
138
139
140
  }
}

Monty's avatar
Monty committed
141
fn sgrproj_box_f_r0<T: Pixel>(f: &mut[u32; 64], x: usize, y: isize, h: usize, cdeffed: &PlaneSlice<T>) {
142
  for i in cmp::max(0, -y) as usize..h {
Monty's avatar
Monty committed
143
    f[i as usize] = (u32::cast_from(cdeffed.p(x, (y + i as isize) as usize))) << SGRPROJ_RST_BITS;
144
145
146
  }
}

Monty's avatar
Monty committed
147
fn sgrproj_box_f_r1<T: Pixel>(af: &[&[u32; 64+2]; 3], bf: &[&[u32; 64+2]; 3], f: &mut[u32; 64],
Monty's avatar
Monty committed
148
                              x: usize, y: isize, h: usize, cdeffed: &PlaneSlice<T>) {
149
150
151
  let shift = 5 + SGRPROJ_SGR_BITS - SGRPROJ_RST_BITS;
  for i in cmp::max(0, -y) as usize..h {
    let a =
152
153
      3 * (af[0][i] + af[2][i]   + af[0][i+2] + af[2][i+2]) +
      4 * (af[1][i] + af[0][i+1] + af[1][i+1] + af[2][i+1] + af[1][i+2]);
154
    let b =
155
156
      3 * (bf[0][i] + bf[2][i]   + bf[0][i+2] + bf[2][i+2]) +
      4 * (bf[1][i] + bf[0][i+1] + bf[1][i+1] + bf[2][i+1] + bf[1][i+2]);
Monty's avatar
Monty committed
157
    let v = a * u32::cast_from(cdeffed.p(x, (y + i as isize) as usize)) + b;
158
    f[i as usize] = (v + (1 << shift >> 1)) >> shift;
159
160
161
  }
}

Monty's avatar
Monty committed
162
fn sgrproj_box_f_r2<T: Pixel>(af: &[&[u32; 64+2]; 3], bf: &[&[u32; 64+2]; 3], f: &mut[u32; 64],
Monty's avatar
Monty committed
163
                              x: usize, y: isize, h: usize, cdeffed: &PlaneSlice<T>) {
164
165
166
167
  let shift = 5 + SGRPROJ_SGR_BITS - SGRPROJ_RST_BITS;
  let shifto = 4 + SGRPROJ_SGR_BITS - SGRPROJ_RST_BITS;
  for i in (cmp::max(0, -y) as usize..h).step_by(2) {
    let a =
168
169
      5 * (af[0][i] + af[2][i]) +
      6 * (af[1][i]);
170
    let b =
171
172
      5 * (bf[0][i] + bf[2][i]) +
      6 * (bf[1][i]);
173
    let ao =
174
      5 * (af[0][i+2] + af[2][i+2]) +
175
176
      6 * (af[1][i+2]);
    let bo =
177
      5 * (bf[0][i+2] + bf[2][i+2]) +
178
      6 * (bf[1][i+2]);
Monty's avatar
Monty committed
179
    let v = (a + ao) * u32::cast_from(cdeffed.p(x, (y+i as isize) as usize)) + b + bo;
180
    f[i as usize] = (v + (1 << shift >> 1)) >> shift;
Monty's avatar
Monty committed
181
    let vo = ao * u32::cast_from(cdeffed.p(x, (y + i as isize) as usize + 1)) + bo;
182
    f[i as usize + 1] = (vo + (1 << shifto >> 1)) >> shifto;
Monty's avatar
Monty committed
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
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, r: 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);

    // 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 {
      // decide if we're vertically inside or outside the strip
      let src_plane =
        if self.y >= self.stripe_begin && self.y < self.stripe_end as isize {
          self.cdeffed
        } else {
          self.deblocked
        };
      // 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);
      // 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> {}

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> {}

fn setup_integral_image<'a, T: Pixel>(
  rows_iter: &mut impl Iterator<Item = impl Iterator<Item = &'a T>>,
  integral_image: &mut [u32], sq_integral_image: &mut [u32],
  integral_image_stride: usize
) {
  // 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;
  }
}

391
392
393
394
395
396
pub fn sgrproj_stripe_filter<T: Pixel>(set: u8, xqd: [i8; 2], fi: &FrameInvariants<T>,
                                       crop_w: usize, crop_h: usize,
                                       stripe_w: usize, stripe_h: usize,
                                       cdeffed: &PlaneSlice<T>,
                                       deblocked: &PlaneSlice<T>,
                                       out: &mut PlaneMutSlice<T>) {
397
  const WIDTH_MAX: usize = (1 << RESTORATION_TILESIZE_MAX_LOG2) * 2;
Monty's avatar
Monty committed
398
  assert!(stripe_h <= 64);
399
400
401
402
403
404
405
406
407
408
409
  assert!(stripe_w <= WIDTH_MAX);

  const INTEGRAL_IMAGE_STRIDE: usize = WIDTH_MAX + 6 + 2;
  const INTEGRAL_IMAGE_HEIGHT: usize = 64 + 6 + 2;
  const INTEGRAL_IMAGE_SIZE: usize =
    INTEGRAL_IMAGE_STRIDE * INTEGRAL_IMAGE_HEIGHT;
  let mut integral_image: [u32; INTEGRAL_IMAGE_SIZE] =
    [0; INTEGRAL_IMAGE_SIZE];
  let mut sq_integral_image: [u32; INTEGRAL_IMAGE_SIZE] =
    [0; INTEGRAL_IMAGE_SIZE];

Monty's avatar
Monty committed
410
  let bdm8 = fi.sequence.bit_depth - 8;
Monty's avatar
Monty committed
411
412
413
414
415
416
  let mut a_r2: [[u32; 64+2]; 3] = [[0; 64+2]; 3];
  let mut b_r2: [[u32; 64+2]; 3] = [[0; 64+2]; 3];
  let mut f_r2: [u32; 64] = [0; 64];
  let mut a_r1: [[u32; 64+2]; 3] = [[0; 64+2]; 3];
  let mut b_r1: [[u32; 64+2]; 3] = [[0; 64+2]; 3];
  let mut f_r1: [u32; 64] = [0; 64];
417

Monty's avatar
Monty committed
418
419
  let s_r2: u32 = SGRPROJ_PARAMS_S[set as usize][0];
  let s_r1: u32 = SGRPROJ_PARAMS_S[set as usize][1];
Monty's avatar
Monty committed
420

421
  let outstart = cmp::max(0, cmp::max(-cdeffed.y, -out.y)) as usize;
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
  let max_r: usize = if s_r2 > 0 {
    2
  } else if s_r1 > 0 {
    1
  } else {
    0
  };
  {
    // Number of elements outside the stripe
    let left_w = max_r + 2;
    let right_w = max_r + 1;

    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),
      stripe_h,
      crop_h,
      max_r
    )
    .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_integral_image(
      &mut rows_iter,
      &mut integral_image,
      &mut sq_integral_image,
      INTEGRAL_IMAGE_STRIDE
    );
  }
Monty's avatar
Monty committed
471
  /* prime the intermediate arrays */
472
  if s_r2 > 0 {
Monty's avatar
Monty committed
473
    sgrproj_box_ab_r2(&mut a_r2[0], &mut b_r2[0],
474
475
476
                      &integral_image, &sq_integral_image,
                      INTEGRAL_IMAGE_STRIDE,
                      0, stripe_h, s_r2, bdm8);
Monty's avatar
Monty committed
477
    sgrproj_box_ab_r2(&mut a_r2[1], &mut b_r2[1],
478
479
480
                      &integral_image, &sq_integral_image,
                      INTEGRAL_IMAGE_STRIDE,
                      1, stripe_h, s_r2, bdm8);
481
  }
482
  if s_r1 > 0 {
483
484
    let r_diff = max_r - 1;
    let integral_image_offset = r_diff + r_diff * INTEGRAL_IMAGE_STRIDE;
Monty's avatar
Monty committed
485
    sgrproj_box_ab_r1(&mut a_r1[0], &mut b_r1[0],
486
487
488
489
                      &integral_image[integral_image_offset..],
                      &sq_integral_image[integral_image_offset..],
                      INTEGRAL_IMAGE_STRIDE,
                      0, stripe_h, s_r1, bdm8);
Monty's avatar
Monty committed
490
    sgrproj_box_ab_r1(&mut a_r1[1], &mut b_r1[1],
491
492
493
494
                      &integral_image[integral_image_offset..],
                      &sq_integral_image[integral_image_offset..],
                      INTEGRAL_IMAGE_STRIDE,
                      1, stripe_h, s_r1, bdm8);
495
  }
Raphaël Zumer's avatar
Raphaël Zumer committed
496

Monty's avatar
Monty committed
497
  /* iterate by column */
Monty's avatar
Monty committed
498
  for xi in 0..stripe_w {
499
    /* build intermediate array columns */
500
    if s_r2 > 0 {
Monty's avatar
Monty committed
501
      sgrproj_box_ab_r2(&mut a_r2[(xi+2)%3], &mut b_r2[(xi+2)%3],
502
503
504
                        &integral_image, &sq_integral_image,
                        INTEGRAL_IMAGE_STRIDE,
                        xi as isize + 2, stripe_h, s_r2, bdm8);
Monty's avatar
Monty committed
505
506
      let ap0: [&[u32; 64+2]; 3] = [&a_r2[xi%3], &a_r2[(xi+1)%3], &a_r2[(xi+2)%3]];
      let bp0: [&[u32; 64+2]; 3] = [&b_r2[xi%3], &b_r2[(xi+1)%3], &b_r2[(xi+2)%3]];
507
      sgrproj_box_f_r2(&ap0, &bp0, &mut f_r2, xi, 0, stripe_h as usize, &cdeffed);
508
    } else {
509
      sgrproj_box_f_r0(&mut f_r2, xi, 0, stripe_h as usize, &cdeffed);
510
    }
511
    if s_r1 > 0 {
512
513
      let r_diff = max_r - 1;
      let integral_image_offset = r_diff + r_diff * INTEGRAL_IMAGE_STRIDE;
Monty's avatar
Monty committed
514
      sgrproj_box_ab_r1(&mut a_r1[(xi+2)%3], &mut b_r1[(xi+2)%3],
515
516
517
518
                        &integral_image[integral_image_offset..],
                        &sq_integral_image[integral_image_offset..],
                        INTEGRAL_IMAGE_STRIDE,
                        xi as isize + 2, stripe_h, s_r1, bdm8);
Monty's avatar
Monty committed
519
520
      let ap1: [&[u32; 64+2]; 3] = [&a_r1[xi%3], &a_r1[(xi+1)%3], &a_r1[(xi+2)%3]];
      let bp1: [&[u32; 64+2]; 3] = [&b_r1[xi%3], &b_r1[(xi+1)%3], &b_r1[(xi+2)%3]];
521
      sgrproj_box_f_r1(&ap1, &bp1, &mut f_r1, xi, 0, stripe_h as usize, &cdeffed);
522
    } else {
523
      sgrproj_box_f_r0(&mut f_r1, xi, 0, stripe_h as usize, &cdeffed);
Monty's avatar
Monty committed
524
    }
525
526

    /* apply filter */
527
    let bit_depth = fi.sequence.bit_depth;
528
529
530
    let w0 = xqd[0] as i32;
    let w1 = xqd[1] as i32;
    let w2 = (1 << SGRPROJ_PRJ_BITS) - w0 - w1;
531
532
    for yi in outstart..stripe_h as usize {
      let u = i32::cast_from(cdeffed.p(xi, yi)) << SGRPROJ_RST_BITS;
Monty's avatar
Monty committed
533
      let v = w0*f_r2[yi] as i32 + w1*u + w2*f_r1[yi] as i32;
534
      let s = (v + (1 << (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) >> 1)) >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS);
535
      out[yi][xi] = T::cast_from(clamp(s, 0, (1 << bit_depth) - 1));
Monty's avatar
Monty committed
536
537
538
539
    }
  }
}

540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// 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>,
                               input: &PlaneSlice<T>,
                               cdeffed: &PlaneSlice<T>,
                               cdef_w: usize, cdef_h: usize) -> (i8, i8) {
  assert!(cdef_h <= 64);
559
560
561
562
563
564
565
566
567
  assert!(cdef_w <= 64);
  const INTEGRAL_IMAGE_STRIDE: usize = 64 + 6 + 2;
  let mut integral_image: [u32;
    INTEGRAL_IMAGE_STRIDE * INTEGRAL_IMAGE_STRIDE] =
    [0; INTEGRAL_IMAGE_STRIDE * INTEGRAL_IMAGE_STRIDE];
  let mut sq_integral_image: [u32;
    INTEGRAL_IMAGE_STRIDE * INTEGRAL_IMAGE_STRIDE] =
    [0; INTEGRAL_IMAGE_STRIDE * INTEGRAL_IMAGE_STRIDE];

568
  let bdm8 = fi.sequence.bit_depth - 8;
569

Monty's avatar
Monty committed
570
571
572
573
574
575
  let mut a_r2: [[u32; 64+2]; 3] = [[0; 64+2]; 3];
  let mut b_r2: [[u32; 64+2]; 3] = [[0; 64+2]; 3];
  let mut f_r2: [u32; 64] = [0; 64];
  let mut a_r1: [[u32; 64+2]; 3] = [[0; 64+2]; 3];
  let mut b_r1: [[u32; 64+2]; 3] = [[0; 64+2]; 3];
  let mut f_r1: [u32; 64] = [0; 64];
576

Monty's avatar
Monty committed
577
578
  let s_r2: u32 = SGRPROJ_PARAMS_S[set as usize][0];
  let s_r1: u32 = SGRPROJ_PARAMS_S[set as usize][1];
579
580
581
582

  let mut h:[[f64; 2]; 2] = [[0.,0.],[0.,0.]];
  let mut c:[f64; 2] = [0., 0.];

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
  let max_r = if s_r2 > 0 { 2 } else if s_r1 > 0 { 1 } else { 0 };
  {
    let mut rows_iter = VertPaddedIter::new(
      cdeffed,
      cdeffed,
      cdef_h,
      cdef_h,
      max_r)
    .map(|row: &[T]| {
      let left_w = max_r + 2;
      let right_w = max_r + 1;
      HorzPaddedIter::new(
        &row[..cdef_w],
        -(left_w as isize),
        left_w + cdef_w + right_w
      )
    });
    setup_integral_image(
      &mut rows_iter,
      &mut integral_image,
      &mut sq_integral_image,
      INTEGRAL_IMAGE_STRIDE
    );
  }

608
609
610
  /* prime the intermediate arrays */
  if s_r2 > 0 {
    sgrproj_box_ab_r2(&mut a_r2[0], &mut b_r2[0],
611
612
613
                      &integral_image, &sq_integral_image,
                      INTEGRAL_IMAGE_STRIDE,
                      0, cdef_h, s_r2, bdm8);
614
    sgrproj_box_ab_r2(&mut a_r2[1], &mut b_r2[1],
615
616
617
                      &integral_image, &sq_integral_image,
                      INTEGRAL_IMAGE_STRIDE,
                      1, cdef_h, s_r2, bdm8);
618
619
  }
  if s_r1 > 0 {
620
621
    let r_diff = max_r - 1;
    let integral_image_offset = r_diff + r_diff * INTEGRAL_IMAGE_STRIDE;
622
    sgrproj_box_ab_r1(&mut a_r1[0], &mut b_r1[0],
623
624
625
626
                      &integral_image[integral_image_offset..],
                      &sq_integral_image[integral_image_offset..],
                      INTEGRAL_IMAGE_STRIDE,
                      0, cdef_h, s_r1, bdm8);
627
    sgrproj_box_ab_r1(&mut a_r1[1], &mut b_r1[1],
628
629
630
631
                      &integral_image[integral_image_offset..],
                      &sq_integral_image[integral_image_offset..],
                      INTEGRAL_IMAGE_STRIDE,
                      1, cdef_h, s_r1, bdm8);
632
  }
633

634
635
636
637
638
  /* iterate by column */
  for xi in 0..cdef_w {
    /* build intermediate array columns */
    if s_r2 > 0 {
      sgrproj_box_ab_r2(&mut a_r2[(xi+2)%3], &mut b_r2[(xi+2)%3],
639
640
641
                        &integral_image, &sq_integral_image,
                        INTEGRAL_IMAGE_STRIDE,
                        xi as isize + 2, cdef_h, s_r2, bdm8);
Monty's avatar
Monty committed
642
643
      let ap0: [&[u32; 64+2]; 3] = [&a_r2[xi%3], &a_r2[(xi+1)%3], &a_r2[(xi+2)%3]];
      let bp0: [&[u32; 64+2]; 3] = [&b_r2[xi%3], &b_r2[(xi+1)%3], &b_r2[(xi+2)%3]];
644
645
646
647
648
      sgrproj_box_f_r2(&ap0, &bp0, &mut f_r2, xi, 0, cdef_h as usize, &cdeffed);
    } else {
      sgrproj_box_f_r0(&mut f_r2, xi, 0, cdef_h as usize, &cdeffed);
    }
    if s_r1 > 0 {
649
650
      let r_diff = max_r - 1;
      let integral_image_offset = r_diff + r_diff * INTEGRAL_IMAGE_STRIDE;
651
      sgrproj_box_ab_r1(&mut a_r1[(xi+2)%3], &mut b_r1[(xi+2)%3],
652
653
654
655
                        &integral_image[integral_image_offset..],
                        &sq_integral_image[integral_image_offset..],
                        INTEGRAL_IMAGE_STRIDE,
                        xi as isize + 2, cdef_h, s_r1, bdm8);
Monty's avatar
Monty committed
656
657
      let ap1: [&[u32; 64+2]; 3] = [&a_r1[xi%3], &a_r1[(xi+1)%3], &a_r1[(xi+2)%3]];
      let bp1: [&[u32; 64+2]; 3] = [&b_r1[xi%3], &b_r1[(xi+1)%3], &b_r1[(xi+2)%3]];
658
659
660
661
662
663
664
665
      sgrproj_box_f_r1(&ap1, &bp1, &mut f_r1, xi, 0, cdef_h as usize, &cdeffed);
    } else {
      sgrproj_box_f_r0(&mut f_r1, xi, 0, cdef_h as usize, &cdeffed);
    }

    for yi in 0..cdef_h {
      let u = i32::cast_from(cdeffed.p(yi,xi)) << SGRPROJ_RST_BITS;
      let s = i32::cast_from(input.p(yi,xi)) << SGRPROJ_RST_BITS;
Monty's avatar
Monty committed
666
667
      let f2 = f_r2[yi] as i32 - u;
      let f1 = f_r1[yi] as i32 - u;
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
      h[0][0] += f2 as f64 * f2 as f64;
      h[1][1] += f1 as f64 * f1 as f64;
      h[0][1] += f1 as f64 * f2 as f64;
      c[0] += f2 as f64 * s as f64;
      c[1] += f1 as f64 * s as f64;
    }
  }

  // this is lifted almost in-tact from libaom
  let n = cdef_w as f64 * cdef_h as f64;
  h[0][0] /= n;
  h[0][1] /= n;
  h[1][1] /= n;
  h[1][0] = h[0][1];
  c[0] /= n;
  c[1] /= n;
  let (xq0, xq1) = if s_r2 == 0 {
    // H matrix is now only the scalar h[1][1]
    // C vector is now only the scalar c[1]
    if h[1][1] == 0. {
      (0, 0)
    } else {
      (0, (c[1] / h[1][1]).round() as i32)
    }
  } else if s_r1 == 0 {
    // H matrix is now only the scalar h[0][0]
    // C vector is now only the scalar c[0]
    if h[0][0] == 0. {
      (0, 0)
    } else {
      ((c[0] / h[0][0]).round() as i32, 0)
    }
  } else {
    let det = h[0][0] * h[1][1] - h[0][1] * h[1][0];
    if det == 0. {
      (0, 0)
    } else {
      // If scaling up dividend would overflow, instead scale down the divisor
      let div1 = (h[1][1] * c[0] - h[0][1] * c[1]) * (1 << SGRPROJ_PRJ_BITS) as f64;
      let div2 = (h[0][0] * c[1] - h[1][0] * c[0]) * (1 << SGRPROJ_PRJ_BITS) as f64;

      ((div1 / det).round() as i32, (div2 / det).round() as i32)
    }
  };
  (clamp(xq0, SGRPROJ_XQD_MIN[0] as i32, SGRPROJ_XQD_MAX[0] as i32) as i8,
   clamp(xq1, SGRPROJ_XQD_MIN[1] as i32, SGRPROJ_XQD_MAX[1] as i32) as i8)
}

716
fn wiener_stripe_filter<T: Pixel>(coeffs: [[i8; 3]; 2], fi: &FrameInvariants<T>,
717
718
719
720
                                  crop_w: usize, crop_h: usize,
                                  stripe_w: usize, stripe_h: usize,
                                  stripe_x: usize, stripe_y: isize,
                                  cdeffed: &Plane<T>, deblocked: &Plane<T>, out: &mut Plane<T>) {
721
  let bit_depth = fi.sequence.bit_depth;
Monty's avatar
Monty committed
722
723
  let round_h = if bit_depth == 12 {5} else {3};
  let round_v = if bit_depth == 12 {9} else {11};
724
725
  let offset = 1 << (bit_depth + WIENER_BITS - round_h - 1);
  let limit = (1 << (bit_depth + 1 + WIENER_BITS - round_h)) - 1;
Raphaël Zumer's avatar
Raphaël Zumer committed
726

Monty's avatar
Monty committed
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
  let mut work: [i32; MAX_SB_SIZE+7] = [0; MAX_SB_SIZE+7];
  let vfilter: [i32; 7] = [ coeffs[0][0] as i32,
                            coeffs[0][1] as i32,
                            coeffs[0][2] as i32,
                            128 - 2 * (coeffs[0][0] as i32 +
                                       coeffs[0][1] as i32 +
                                       coeffs[0][2] as i32 ),
                            coeffs[0][2] as i32,
                            coeffs[0][1] as i32,
                            coeffs[0][0] as i32];
  let hfilter: [i32; 7] = [ coeffs[1][0] as i32,
                            coeffs[1][1] as i32,
                            coeffs[1][2] as i32,
                            128 - 2 * (coeffs[1][0] as i32 +
                                       coeffs[1][1] as i32 +
                                       coeffs[1][2] as i32),
                            coeffs[1][2] as i32,
                            coeffs[1][1] as i32,
                            coeffs[1][0] as i32];

  // unlike x, our y can be negative to start as the first stripe
  // starts off the top of the frame by 8 pixels, and can also run off the end of the frame
Monty's avatar
Monty committed
749
750
  let start_wi = if stripe_y < 0 {-stripe_y} else {0} as usize;
  let start_yi = if stripe_y < 0 {0} else {stripe_y} as usize;
751
  let end_i = cmp::max(0, if stripe_h as isize + stripe_y > crop_h as isize {
Monty's avatar
Monty committed
752
    crop_h as isize - stripe_y - start_wi as isize
Monty's avatar
Monty committed
753
  } else {
754
    stripe_h as isize - start_wi as isize
Monty's avatar
Monty committed
755
  }) as usize;
Raphaël Zumer's avatar
Raphaël Zumer committed
756

Romain Vimont's avatar
Romain Vimont committed
757
  let mut out_slice = out.mut_slice(PlaneOffset{x: 0, y: start_yi as isize});
Monty's avatar
Monty committed
758

Monty's avatar
Monty committed
759
760
  for xi in stripe_x..stripe_x+stripe_w {
    let n = cmp::min(7, crop_w as isize + 3 - xi as isize);
761
    for yi in stripe_y - 3..stripe_y + stripe_h as isize + 4 {
762
      let src_plane: &Plane<T>;
Monty's avatar
Monty committed
763
764
      let mut acc = 0;
      let ly;
Monty's avatar
Monty committed
765
766
      if yi < stripe_y {
        ly = cmp::max(clamp(yi, 0, crop_h as isize - 1), stripe_y - 2) as usize;
Monty's avatar
Monty committed
767
        src_plane = deblocked;
768
      } else if yi < stripe_y+stripe_h as isize {
Monty's avatar
Monty committed
769
        ly = clamp(yi, 0, crop_h as isize - 1) as usize;
Monty's avatar
Monty committed
770
771
        src_plane = cdeffed;
      } else {
772
        ly = cmp::min(clamp(yi, 0, crop_h as isize - 1), stripe_y + stripe_h as isize + 1) as usize;
Monty's avatar
Monty committed
773
774
        src_plane = deblocked;
      }
Raphaël Zumer's avatar
Raphaël Zumer committed
775

Monty's avatar
Monty committed
776
      for i in 0..3 - xi as isize {
777
        acc += hfilter[i as usize] * i32::cast_from(src_plane.p(0, ly));
Monty's avatar
Monty committed
778
779
      }
      for i in cmp::max(0,3 - (xi as isize))..n {
780
        acc += hfilter[i as usize] * i32::cast_from(src_plane.p((xi as isize + i - 3) as usize, ly));
Monty's avatar
Monty committed
781
782
      }
      for i in n..7 {
783
        acc += hfilter[i as usize] * i32::cast_from(src_plane.p(crop_w - 1, ly));
Monty's avatar
Monty committed
784
      }
Raphaël Zumer's avatar
Raphaël Zumer committed
785

786
      acc = (acc + (1 << round_h >> 1)) >> round_h;
Monty's avatar
Monty committed
787
      work[(yi-stripe_y+3) as usize] = clamp(acc, -offset, limit-offset);
Monty's avatar
Monty committed
788
789
    }

790
    for (wi, dst) in (start_wi..start_wi+end_i).zip(out_slice.rows_iter_mut().map(|row| &mut row[xi]).take(end_i)) {
Monty's avatar
Monty committed
791
792
793
794
      let mut acc = 0;
      for (i,src) in (0..7).zip(work[wi..wi+7].iter_mut()) {
        acc += vfilter[i] * *src;
      }
795
      *dst = T::cast_from(clamp((acc + (1 << round_v >> 1)) >> round_v, 0, (1 << bit_depth) - 1));
Monty's avatar
Monty committed
796
    }
797
798
799
  }
}

800
#[derive(Copy, Clone, Debug)]
801
802
803
804
805
806
807
808
809
810
811
812
pub struct RestorationUnit {
  pub filter: RestorationFilter,
}

impl RestorationUnit {
  pub fn default() -> RestorationUnit {
    RestorationUnit {
      filter: RestorationFilter::default(),
    }
  }
}

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
#[derive(Clone, Debug)]
pub struct FrameRestorationUnits {
  units: Box<[RestorationUnit]>,
  pub cols: usize,
  pub rows: usize,
}

impl FrameRestorationUnits {
  pub fn new(cols: usize, rows: usize) -> Self {
    Self {
      units: vec![RestorationUnit::default(); cols * rows].into_boxed_slice(),
      cols,
      rows,
    }
  }
}

impl Index<usize> for FrameRestorationUnits {
  type Output = [RestorationUnit];
  #[inline(always)]
  fn index(&self, index: usize) -> &Self::Output {
    &self.units[index * self.cols..(index + 1) * self.cols]
  }
}

impl IndexMut<usize> for FrameRestorationUnits {
  #[inline(always)]
  fn index_mut(&mut self, index: usize) -> &mut Self::Output {
    &mut self.units[index * self.cols..(index + 1) * self.cols]
  }
}

845
#[derive(Clone, Debug)]
846
pub struct RestorationPlaneConfig {
847
848
  pub lrf_type: u8,
  pub unit_size: usize,
Romain Vimont's avatar
Romain Vimont committed
849
850
  // (1 << sb_shift) gives the number of superblocks both horizontally and
  // vertically in a restoration unit, not accounting for RU stretching
851
  pub sb_shift: usize,
Monty's avatar
Monty committed
852
853
854
  // stripe height is 64 in all cases except 4:2:0 chroma planes where
  // it is 32.  This is independent of all other setup parameters
  pub stripe_height: usize,
855
856
  pub cols: usize,
  pub rows: usize,
857
858
859
860
861
}

#[derive(Clone, Debug)]
pub struct RestorationPlane {
  pub cfg: RestorationPlaneConfig,
862
  pub units: FrameRestorationUnits,
863
864
865
866
867
868
869
870
871
}

#[derive(Clone, Default)]
pub struct RestorationPlaneOffset {
  pub row: usize,
  pub col: usize
}

impl RestorationPlane {
Monty's avatar
Monty committed
872
  pub fn new(lrf_type: u8, unit_size: usize, sb_shift: usize, stripe_decimate: usize,
873
             cols: usize, rows: usize) -> RestorationPlane {
Monty's avatar
Monty committed
874
    let stripe_height = if stripe_decimate != 0 {32} else {64};
875
    RestorationPlane {
876
877
878
879
880
881
882
883
      cfg: RestorationPlaneConfig {
        lrf_type,
        unit_size,
        sb_shift,
        stripe_height,
        cols,
        rows,
      },
884
      units: FrameRestorationUnits::new(cols, rows),
885
886
887
    }
  }

888
889
890
  // Stripes are always 64 pixels high in a non-subsampled
  // frame, and decimated from 64 pixels in chroma.  When
  // filtering, they are not co-located on Y with superblocks.
Monty's avatar
Monty committed
891
  fn restoration_unit_index_by_stripe(&self, stripenum: usize, rux: usize) -> (usize, usize) {
892
    (
893
894
      cmp::min(rux, self.cfg.cols - 1),
      cmp::min(stripenum * self.cfg.stripe_height / self.cfg.unit_size, self.cfg.rows - 1),
895
896
897
    )
  }

Monty's avatar
Monty committed
898
899
  pub fn restoration_unit_by_stripe(&self, stripenum: usize, rux: usize) -> &RestorationUnit {
    let (x, y) = self.restoration_unit_index_by_stripe(stripenum, rux);
900
    &self.units[y][x]
901
  }
902
903
}

904
#[derive(Clone, Debug)]
905
pub struct RestorationState {
Romain Vimont's avatar
Romain Vimont committed
906
  pub planes: [RestorationPlane; PLANES]
907
908
909
}

impl RestorationState {
910
  pub fn new<T: Pixel>(fi: &FrameInvariants<T>, input: &Frame<T>) -> Self {
911
    let PlaneConfig { xdec, ydec, .. } = input.planes[1].cfg;
Monty's avatar
Monty committed
912
    let stripe_uv_decimate = if xdec>0 && ydec>0 {1} else {0};
913
914
915
916
917
918
    // Currrently opt for smallest possible restoration unit size (1
    // superblock) This is *temporary*.  Counting on it will break
    // very shortly; the 1-superblock hardwiring is only until the
    // upper level encoder is capable of dealing with the delayed
    // writes that RU size > SB size will require.
    let lrf_y_shift = if fi.sequence.use_128x128_superblock {1} else {2};
Monty's avatar
Monty committed
919
    let lrf_uv_shift = lrf_y_shift + stripe_uv_decimate;
920
921
922
923
924
925
926

    // derive the rest
    let y_unit_log2 = RESTORATION_TILESIZE_MAX_LOG2 - lrf_y_shift;
    let uv_unit_log2 = RESTORATION_TILESIZE_MAX_LOG2 - lrf_uv_shift;
    let y_unit_size = 1 << y_unit_log2;
    let uv_unit_size = 1 << uv_unit_log2;
    let y_sb_log2 = if fi.sequence.use_128x128_superblock {7} else {6};
Monty's avatar
Monty committed
927
    let uv_sb_log2 = y_sb_log2 - stripe_uv_decimate;
928
929
    let cols = ((fi.width + (y_unit_size >> 1)) / y_unit_size).max(1);
    let rows = ((fi.height + (y_unit_size >> 1)) / y_unit_size).max(1);
930

931
    RestorationState {
Romain Vimont's avatar
Romain Vimont committed
932
933
934
935
936
937
938
939
      planes: [
        RestorationPlane::new(RESTORE_SWITCHABLE, y_unit_size, y_unit_log2 - y_sb_log2,
                              0, cols, rows),
        RestorationPlane::new(RESTORE_SWITCHABLE, uv_unit_size, uv_unit_log2 - uv_sb_log2,
                              stripe_uv_decimate, cols, rows),
        RestorationPlane::new(RESTORE_SWITCHABLE, uv_unit_size, uv_unit_log2 - uv_sb_log2,
                              stripe_uv_decimate, cols, rows)
      ],
940
941
    }
  }
942

943
944
  pub fn lrf_filter_frame<T: Pixel>(&mut self, out: &mut Frame<T>, pre_cdef: &Frame<T>,
                                    fi: &FrameInvariants<T>) {
945
    let cdeffed = out.clone();
Raphaël Zumer's avatar
Raphaël Zumer committed
946

947
948
949
950
951
    // unlike the other loop filters that operate over the padded
    // frame dimensions, restoration filtering and source pixel
    // accesses are clipped to the original frame dimensions
    // that's why we use fi.width and fi.height instead of PlaneConfig fields

Monty's avatar
Monty committed
952
    // number of stripes (counted according to colocated Y luma position)
953
    let stripe_n = (fi.height + 7) / 64 + 1;
Raphaël Zumer's avatar
Raphaël Zumer committed
954

Monty's avatar
Monty committed
955
    for pli in 0..PLANES {
Romain Vimont's avatar
Romain Vimont committed
956
      let rp = &self.planes[pli];
Monty's avatar
Monty committed
957
      let xdec = out.planes[pli].cfg.xdec;
958
      let ydec = out.planes[pli].cfg.ydec;
959
960
      let crop_w = (fi.width + (1 << xdec >> 1)) >> xdec;
      let crop_h = (fi.height + (1 << ydec >> 1)) >> ydec;
Raphaël Zumer's avatar
Raphaël Zumer committed
961

Monty's avatar
Monty committed
962
963
      for si in 0..stripe_n {
        // stripe y pixel locations must be able to overspan the frame
964
        let stripe_start_y = (si as isize * 64 - 8) >> ydec;
Monty's avatar
Monty committed
965
        let stripe_size = 64 >> ydec; // one past, unlike spec
Raphaël Zumer's avatar
Raphaël Zumer committed
966

Monty's avatar
Monty committed
967
        // horizontally, go rdu-by-rdu
968
        for rux in 0..rp.cfg.cols {
Monty's avatar
Monty committed
969
          // stripe x pixel locations must be clipped to frame, last may need to stretch
970
971
          let x = rux * rp.cfg.unit_size;
          let size = if rux == rp.cfg.cols - 1 {
972
            crop_w - x
Monty's avatar
Monty committed
973
          } else {
974
            rp.cfg.unit_size
Monty's avatar
Monty committed
975
          };
Monty's avatar
Monty committed
976
          let ru = rp.restoration_unit_by_stripe(si, rux);
Monty's avatar
Monty committed
977
          match ru.filter {
978
            RestorationFilter::Wiener{coeffs} => {
979
980
              wiener_stripe_filter(coeffs, fi,
                                   crop_w, crop_h,
981
982
                                   size, stripe_size,
                                   x, stripe_start_y,
983
984
                                   &cdeffed.planes[pli], &pre_cdef.planes[pli],
                                   &mut out.planes[pli]);
Monty's avatar
Monty committed
985
986
            },
            RestorationFilter::Sgrproj{set, xqd} => {
987
              sgrproj_stripe_filter(set, xqd, fi,
988
989
990
                                    crop_w - x,
                                    (crop_h as isize - stripe_start_y) as usize,
                                    size, stripe_size,
Romain Vimont's avatar
Romain Vimont committed
991
                                    &cdeffed.planes[pli].slice(PlaneOffset{x: x as isize,
992
                                                                           y: stripe_start_y}),
Romain Vimont's avatar
Romain Vimont committed
993
                                    &pre_cdef.planes[pli].slice(PlaneOffset{x: x as isize,
994
                                                                            y: stripe_start_y}),
Romain Vimont's avatar
Romain Vimont committed
995
                                    &mut out.planes[pli].mut_slice(PlaneOffset{x: x as isize,
996
                                                                               y: stripe_start_y}));
Monty's avatar
Monty committed
997
998
999
1000
1001
1002
1003
            },
            RestorationFilter::None => {
              // do nothing
            }
          }
        }
      }
Raphaël Zumer's avatar
Raphaël Zumer committed
1004
    }
1005
1006
  }
}