1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include <assert.h>
13 #include <float.h>
14 #include <limits.h>
15 #include <math.h>
16
17 #include "config/aom_scale_rtcd.h"
18 #include "config/av1_rtcd.h"
19
20 #include "aom_dsp/aom_dsp_common.h"
21 #include "aom_dsp/binary_codes_writer.h"
22 #include "aom_dsp/psnr.h"
23 #include "aom_mem/aom_mem.h"
24 #include "aom_ports/mem.h"
25 #include "aom_ports/system_state.h"
26 #include "av1/common/onyxc_int.h"
27 #include "av1/common/quant_common.h"
28 #include "av1/common/restoration.h"
29
30 #include "av1/encoder/av1_quantize.h"
31 #include "av1/encoder/encoder.h"
32 #include "av1/encoder/mathutils.h"
33 #include "av1/encoder/picklpf.h"
34 #include "av1/encoder/pickrst.h"
35
36 // When set to RESTORE_WIENER or RESTORE_SGRPROJ only those are allowed.
37 // When set to RESTORE_TYPES we allow switchable.
38 static const RestorationType force_restore_type = RESTORE_TYPES;
39
40 // Number of Wiener iterations
41 #define NUM_WIENER_ITERS 5
42
43 // Penalty factor for use of dual sgr
44 #define DUAL_SGR_PENALTY_MULT 0.01
45
46 // Working precision for Wiener filter coefficients
47 #define WIENER_TAP_SCALE_FACTOR ((int64_t)1 << 16)
48
49 const int frame_level_restore_bits[RESTORE_TYPES] = { 2, 2, 2, 2 };
50
51 typedef int64_t (*sse_extractor_type)(const YV12_BUFFER_CONFIG *a,
52 const YV12_BUFFER_CONFIG *b);
53 typedef int64_t (*sse_part_extractor_type)(const YV12_BUFFER_CONFIG *a,
54 const YV12_BUFFER_CONFIG *b,
55 int hstart, int width, int vstart,
56 int height);
57
58 #define NUM_EXTRACTORS (3 * (1 + 1))
59
60 static const sse_part_extractor_type sse_part_extractors[NUM_EXTRACTORS] = {
61 aom_get_y_sse_part, aom_get_u_sse_part,
62 aom_get_v_sse_part, aom_highbd_get_y_sse_part,
63 aom_highbd_get_u_sse_part, aom_highbd_get_v_sse_part,
64 };
65
sse_restoration_unit(const RestorationTileLimits * limits,const YV12_BUFFER_CONFIG * src,const YV12_BUFFER_CONFIG * dst,int plane,int highbd)66 static int64_t sse_restoration_unit(const RestorationTileLimits *limits,
67 const YV12_BUFFER_CONFIG *src,
68 const YV12_BUFFER_CONFIG *dst, int plane,
69 int highbd) {
70 return sse_part_extractors[3 * highbd + plane](
71 src, dst, limits->h_start, limits->h_end - limits->h_start,
72 limits->v_start, limits->v_end - limits->v_start);
73 }
74
75 typedef struct {
76 // The best coefficients for Wiener or Sgrproj restoration
77 WienerInfo wiener;
78 SgrprojInfo sgrproj;
79
80 // The sum of squared errors for this rtype.
81 int64_t sse[RESTORE_SWITCHABLE_TYPES];
82
83 // The rtype to use for this unit given a frame rtype as
84 // index. Indices: WIENER, SGRPROJ, SWITCHABLE.
85 RestorationType best_rtype[RESTORE_TYPES - 1];
86 } RestUnitSearchInfo;
87
88 typedef struct {
89 const YV12_BUFFER_CONFIG *src;
90 YV12_BUFFER_CONFIG *dst;
91
92 const AV1_COMMON *cm;
93 const MACROBLOCK *x;
94 int plane;
95 int plane_width;
96 int plane_height;
97 RestUnitSearchInfo *rusi;
98
99 // Speed features
100 const SPEED_FEATURES *sf;
101
102 uint8_t *dgd_buffer;
103 int dgd_stride;
104 const uint8_t *src_buffer;
105 int src_stride;
106
107 // sse and bits are initialised by reset_rsc in search_rest_type
108 int64_t sse;
109 int64_t bits;
110 int tile_y0, tile_stripe0;
111
112 // sgrproj and wiener are initialised by rsc_on_tile when starting the first
113 // tile in the frame.
114 SgrprojInfo sgrproj;
115 WienerInfo wiener;
116 AV1PixelRect tile_rect;
117 } RestSearchCtxt;
118
rsc_on_tile(void * priv)119 static void rsc_on_tile(void *priv) {
120 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
121 set_default_sgrproj(&rsc->sgrproj);
122 set_default_wiener(&rsc->wiener);
123 rsc->tile_stripe0 = 0;
124 }
125
reset_rsc(RestSearchCtxt * rsc)126 static void reset_rsc(RestSearchCtxt *rsc) {
127 rsc->sse = 0;
128 rsc->bits = 0;
129 }
130
init_rsc(const YV12_BUFFER_CONFIG * src,const AV1_COMMON * cm,const MACROBLOCK * x,const SPEED_FEATURES * sf,int plane,RestUnitSearchInfo * rusi,YV12_BUFFER_CONFIG * dst,RestSearchCtxt * rsc)131 static void init_rsc(const YV12_BUFFER_CONFIG *src, const AV1_COMMON *cm,
132 const MACROBLOCK *x, const SPEED_FEATURES *sf, int plane,
133 RestUnitSearchInfo *rusi, YV12_BUFFER_CONFIG *dst,
134 RestSearchCtxt *rsc) {
135 rsc->src = src;
136 rsc->dst = dst;
137 rsc->cm = cm;
138 rsc->x = x;
139 rsc->plane = plane;
140 rsc->rusi = rusi;
141 rsc->sf = sf;
142
143 const YV12_BUFFER_CONFIG *dgd = &cm->cur_frame->buf;
144 const int is_uv = plane != AOM_PLANE_Y;
145 rsc->plane_width = src->crop_widths[is_uv];
146 rsc->plane_height = src->crop_heights[is_uv];
147 rsc->src_buffer = src->buffers[plane];
148 rsc->src_stride = src->strides[is_uv];
149 rsc->dgd_buffer = dgd->buffers[plane];
150 rsc->dgd_stride = dgd->strides[is_uv];
151 rsc->tile_rect = av1_whole_frame_rect(cm, is_uv);
152 assert(src->crop_widths[is_uv] == dgd->crop_widths[is_uv]);
153 assert(src->crop_heights[is_uv] == dgd->crop_heights[is_uv]);
154 }
155
try_restoration_unit(const RestSearchCtxt * rsc,const RestorationTileLimits * limits,const AV1PixelRect * tile_rect,const RestorationUnitInfo * rui)156 static int64_t try_restoration_unit(const RestSearchCtxt *rsc,
157 const RestorationTileLimits *limits,
158 const AV1PixelRect *tile_rect,
159 const RestorationUnitInfo *rui) {
160 const AV1_COMMON *const cm = rsc->cm;
161 const int plane = rsc->plane;
162 const int is_uv = plane > 0;
163 const RestorationInfo *rsi = &cm->rst_info[plane];
164 RestorationLineBuffers rlbs;
165 const int bit_depth = cm->seq_params.bit_depth;
166 const int highbd = cm->seq_params.use_highbitdepth;
167
168 const YV12_BUFFER_CONFIG *fts = &cm->cur_frame->buf;
169 // TODO(yunqing): For now, only use optimized LR filter in decoder. Can be
170 // also used in encoder.
171 const int optimized_lr = 0;
172
173 av1_loop_restoration_filter_unit(
174 limits, rui, &rsi->boundaries, &rlbs, tile_rect, rsc->tile_stripe0,
175 is_uv && cm->seq_params.subsampling_x,
176 is_uv && cm->seq_params.subsampling_y, highbd, bit_depth,
177 fts->buffers[plane], fts->strides[is_uv], rsc->dst->buffers[plane],
178 rsc->dst->strides[is_uv], cm->rst_tmpbuf, optimized_lr);
179
180 return sse_restoration_unit(limits, rsc->src, rsc->dst, plane, highbd);
181 }
182
av1_lowbd_pixel_proj_error_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int xq[2],const sgr_params_type * params)183 int64_t av1_lowbd_pixel_proj_error_c(const uint8_t *src8, int width, int height,
184 int src_stride, const uint8_t *dat8,
185 int dat_stride, int32_t *flt0,
186 int flt0_stride, int32_t *flt1,
187 int flt1_stride, int xq[2],
188 const sgr_params_type *params) {
189 int i, j;
190 const uint8_t *src = src8;
191 const uint8_t *dat = dat8;
192 int64_t err = 0;
193 if (params->r[0] > 0 && params->r[1] > 0) {
194 for (i = 0; i < height; ++i) {
195 for (j = 0; j < width; ++j) {
196 assert(flt1[j] < (1 << 15) && flt1[j] > -(1 << 15));
197 assert(flt0[j] < (1 << 15) && flt0[j] > -(1 << 15));
198 const int32_t u = (int32_t)(dat[j] << SGRPROJ_RST_BITS);
199 int32_t v = u << SGRPROJ_PRJ_BITS;
200 v += xq[0] * (flt0[j] - u) + xq[1] * (flt1[j] - u);
201 const int32_t e =
202 ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) - src[j];
203 err += ((int64_t)e * e);
204 }
205 dat += dat_stride;
206 src += src_stride;
207 flt0 += flt0_stride;
208 flt1 += flt1_stride;
209 }
210 } else if (params->r[0] > 0) {
211 for (i = 0; i < height; ++i) {
212 for (j = 0; j < width; ++j) {
213 assert(flt0[j] < (1 << 15) && flt0[j] > -(1 << 15));
214 const int32_t u = (int32_t)(dat[j] << SGRPROJ_RST_BITS);
215 int32_t v = u << SGRPROJ_PRJ_BITS;
216 v += xq[0] * (flt0[j] - u);
217 const int32_t e =
218 ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) - src[j];
219 err += ((int64_t)e * e);
220 }
221 dat += dat_stride;
222 src += src_stride;
223 flt0 += flt0_stride;
224 }
225 } else if (params->r[1] > 0) {
226 for (i = 0; i < height; ++i) {
227 for (j = 0; j < width; ++j) {
228 assert(flt1[j] < (1 << 15) && flt1[j] > -(1 << 15));
229 const int32_t u = (int32_t)(dat[j] << SGRPROJ_RST_BITS);
230 int32_t v = u << SGRPROJ_PRJ_BITS;
231 v += xq[1] * (flt1[j] - u);
232 const int32_t e =
233 ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) - src[j];
234 err += ((int64_t)e * e);
235 }
236 dat += dat_stride;
237 src += src_stride;
238 flt1 += flt1_stride;
239 }
240 } else {
241 for (i = 0; i < height; ++i) {
242 for (j = 0; j < width; ++j) {
243 const int32_t e = (int32_t)(dat[j]) - src[j];
244 err += ((int64_t)e * e);
245 }
246 dat += dat_stride;
247 src += src_stride;
248 }
249 }
250
251 return err;
252 }
253
av1_highbd_pixel_proj_error_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int xq[2],const sgr_params_type * params)254 int64_t av1_highbd_pixel_proj_error_c(const uint8_t *src8, int width,
255 int height, int src_stride,
256 const uint8_t *dat8, int dat_stride,
257 int32_t *flt0, int flt0_stride,
258 int32_t *flt1, int flt1_stride, int xq[2],
259 const sgr_params_type *params) {
260 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
261 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
262 int i, j;
263 int64_t err = 0;
264 const int32_t half = 1 << (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS - 1);
265 if (params->r[0] > 0 && params->r[1] > 0) {
266 int xq0 = xq[0];
267 int xq1 = xq[1];
268 for (i = 0; i < height; ++i) {
269 for (j = 0; j < width; ++j) {
270 const int32_t d = dat[j];
271 const int32_t s = src[j];
272 const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
273 int32_t v0 = flt0[j] - u;
274 int32_t v1 = flt1[j] - u;
275 int32_t v = half;
276 v += xq0 * v0;
277 v += xq1 * v1;
278 const int32_t e = (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
279 err += ((int64_t)e * e);
280 }
281 dat += dat_stride;
282 flt0 += flt0_stride;
283 flt1 += flt1_stride;
284 src += src_stride;
285 }
286 } else if (params->r[0] > 0 || params->r[1] > 0) {
287 int exq;
288 int32_t *flt;
289 int flt_stride;
290 if (params->r[0] > 0) {
291 exq = xq[0];
292 flt = flt0;
293 flt_stride = flt0_stride;
294 } else {
295 exq = xq[1];
296 flt = flt1;
297 flt_stride = flt1_stride;
298 }
299 for (i = 0; i < height; ++i) {
300 for (j = 0; j < width; ++j) {
301 const int32_t d = dat[j];
302 const int32_t s = src[j];
303 const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
304 int32_t v = half;
305 v += exq * (flt[j] - u);
306 const int32_t e = (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
307 err += ((int64_t)e * e);
308 }
309 dat += dat_stride;
310 flt += flt_stride;
311 src += src_stride;
312 }
313 } else {
314 for (i = 0; i < height; ++i) {
315 for (j = 0; j < width; ++j) {
316 const int32_t d = dat[j];
317 const int32_t s = src[j];
318 const int32_t e = d - s;
319 err += ((int64_t)e * e);
320 }
321 dat += dat_stride;
322 src += src_stride;
323 }
324 }
325 return err;
326 }
327
get_pixel_proj_error(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int use_highbitdepth,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int * xqd,const sgr_params_type * params)328 static int64_t get_pixel_proj_error(const uint8_t *src8, int width, int height,
329 int src_stride, const uint8_t *dat8,
330 int dat_stride, int use_highbitdepth,
331 int32_t *flt0, int flt0_stride,
332 int32_t *flt1, int flt1_stride, int *xqd,
333 const sgr_params_type *params) {
334 int xq[2];
335 decode_xq(xqd, xq, params);
336 if (!use_highbitdepth) {
337 return av1_lowbd_pixel_proj_error(src8, width, height, src_stride, dat8,
338 dat_stride, flt0, flt0_stride, flt1,
339 flt1_stride, xq, params);
340 } else {
341 return av1_highbd_pixel_proj_error(src8, width, height, src_stride, dat8,
342 dat_stride, flt0, flt0_stride, flt1,
343 flt1_stride, xq, params);
344 }
345 }
346
347 #define USE_SGRPROJ_REFINEMENT_SEARCH 1
finer_search_pixel_proj_error(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int use_highbitdepth,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int start_step,int * xqd,const sgr_params_type * params)348 static int64_t finer_search_pixel_proj_error(
349 const uint8_t *src8, int width, int height, int src_stride,
350 const uint8_t *dat8, int dat_stride, int use_highbitdepth, int32_t *flt0,
351 int flt0_stride, int32_t *flt1, int flt1_stride, int start_step, int *xqd,
352 const sgr_params_type *params) {
353 int64_t err = get_pixel_proj_error(
354 src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth, flt0,
355 flt0_stride, flt1, flt1_stride, xqd, params);
356 (void)start_step;
357 #if USE_SGRPROJ_REFINEMENT_SEARCH
358 int64_t err2;
359 int tap_min[] = { SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MIN1 };
360 int tap_max[] = { SGRPROJ_PRJ_MAX0, SGRPROJ_PRJ_MAX1 };
361 for (int s = start_step; s >= 1; s >>= 1) {
362 for (int p = 0; p < 2; ++p) {
363 if ((params->r[0] == 0 && p == 0) || (params->r[1] == 0 && p == 1)) {
364 continue;
365 }
366 int skip = 0;
367 do {
368 if (xqd[p] - s >= tap_min[p]) {
369 xqd[p] -= s;
370 err2 =
371 get_pixel_proj_error(src8, width, height, src_stride, dat8,
372 dat_stride, use_highbitdepth, flt0,
373 flt0_stride, flt1, flt1_stride, xqd, params);
374 if (err2 > err) {
375 xqd[p] += s;
376 } else {
377 err = err2;
378 skip = 1;
379 // At the highest step size continue moving in the same direction
380 if (s == start_step) continue;
381 }
382 }
383 break;
384 } while (1);
385 if (skip) break;
386 do {
387 if (xqd[p] + s <= tap_max[p]) {
388 xqd[p] += s;
389 err2 =
390 get_pixel_proj_error(src8, width, height, src_stride, dat8,
391 dat_stride, use_highbitdepth, flt0,
392 flt0_stride, flt1, flt1_stride, xqd, params);
393 if (err2 > err) {
394 xqd[p] -= s;
395 } else {
396 err = err2;
397 // At the highest step size continue moving in the same direction
398 if (s == start_step) continue;
399 }
400 }
401 break;
402 } while (1);
403 }
404 }
405 #endif // USE_SGRPROJ_REFINEMENT_SEARCH
406 return err;
407 }
408
signed_rounded_divide(int64_t dividend,int64_t divisor)409 static int64_t signed_rounded_divide(int64_t dividend, int64_t divisor) {
410 if (dividend < 0)
411 return (dividend - divisor / 2) / divisor;
412 else
413 return (dividend + divisor / 2) / divisor;
414 }
415
get_proj_subspace(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int use_highbitdepth,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int * xq,const sgr_params_type * params)416 static void get_proj_subspace(const uint8_t *src8, int width, int height,
417 int src_stride, const uint8_t *dat8,
418 int dat_stride, int use_highbitdepth,
419 int32_t *flt0, int flt0_stride, int32_t *flt1,
420 int flt1_stride, int *xq,
421 const sgr_params_type *params) {
422 int i, j;
423 int64_t H[2][2] = { { 0, 0 }, { 0, 0 } };
424 int64_t C[2] = { 0, 0 };
425 const int size = width * height;
426
427 // Default values to be returned if the problem becomes ill-posed
428 xq[0] = 0;
429 xq[1] = 0;
430
431 if (!use_highbitdepth) {
432 const uint8_t *src = src8;
433 const uint8_t *dat = dat8;
434 for (i = 0; i < height; ++i) {
435 for (j = 0; j < width; ++j) {
436 const int32_t u =
437 (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
438 const int32_t s =
439 (int32_t)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
440 const int32_t f1 =
441 (params->r[0] > 0) ? (int32_t)flt0[i * flt0_stride + j] - u : 0;
442 const int32_t f2 =
443 (params->r[1] > 0) ? (int32_t)flt1[i * flt1_stride + j] - u : 0;
444 H[0][0] += (int64_t)f1 * f1;
445 H[1][1] += (int64_t)f2 * f2;
446 H[0][1] += (int64_t)f1 * f2;
447 C[0] += (int64_t)f1 * s;
448 C[1] += (int64_t)f2 * s;
449 }
450 }
451 } else {
452 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
453 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
454 for (i = 0; i < height; ++i) {
455 for (j = 0; j < width; ++j) {
456 const int32_t u =
457 (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
458 const int32_t s =
459 (int32_t)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
460 const int32_t f1 =
461 (params->r[0] > 0) ? (int32_t)flt0[i * flt0_stride + j] - u : 0;
462 const int32_t f2 =
463 (params->r[1] > 0) ? (int32_t)flt1[i * flt1_stride + j] - u : 0;
464 H[0][0] += (int64_t)f1 * f1;
465 H[1][1] += (int64_t)f2 * f2;
466 H[0][1] += (int64_t)f1 * f2;
467 C[0] += (int64_t)f1 * s;
468 C[1] += (int64_t)f2 * s;
469 }
470 }
471 }
472 H[0][0] /= size;
473 H[0][1] /= size;
474 H[1][1] /= size;
475 H[1][0] = H[0][1];
476 C[0] /= size;
477 C[1] /= size;
478 if (params->r[0] == 0) {
479 // H matrix is now only the scalar H[1][1]
480 // C vector is now only the scalar C[1]
481 const int64_t Det = H[1][1];
482 if (Det == 0) return; // ill-posed, return default values
483 xq[0] = 0;
484 xq[1] = (int)signed_rounded_divide(C[1] * (1 << SGRPROJ_PRJ_BITS), Det);
485 } else if (params->r[1] == 0) {
486 // H matrix is now only the scalar H[0][0]
487 // C vector is now only the scalar C[0]
488 const int64_t Det = H[0][0];
489 if (Det == 0) return; // ill-posed, return default values
490 xq[0] = (int)signed_rounded_divide(C[0] * (1 << SGRPROJ_PRJ_BITS), Det);
491 xq[1] = 0;
492 } else {
493 const int64_t Det = H[0][0] * H[1][1] - H[0][1] * H[1][0];
494 if (Det == 0) return; // ill-posed, return default values
495
496 // If scaling up dividend would overflow, instead scale down the divisor
497 const int64_t div1 = H[1][1] * C[0] - H[0][1] * C[1];
498 if ((div1 > 0 && INT64_MAX / (1 << SGRPROJ_PRJ_BITS) < div1) ||
499 (div1 < 0 && INT64_MIN / (1 << SGRPROJ_PRJ_BITS) > div1))
500 xq[0] = (int)signed_rounded_divide(div1, Det / (1 << SGRPROJ_PRJ_BITS));
501 else
502 xq[0] = (int)signed_rounded_divide(div1 * (1 << SGRPROJ_PRJ_BITS), Det);
503
504 const int64_t div2 = H[0][0] * C[1] - H[1][0] * C[0];
505 if ((div2 > 0 && INT64_MAX / (1 << SGRPROJ_PRJ_BITS) < div2) ||
506 (div2 < 0 && INT64_MIN / (1 << SGRPROJ_PRJ_BITS) > div2))
507 xq[1] = (int)signed_rounded_divide(div2, Det / (1 << SGRPROJ_PRJ_BITS));
508 else
509 xq[1] = (int)signed_rounded_divide(div2 * (1 << SGRPROJ_PRJ_BITS), Det);
510 }
511 }
512
encode_xq(int * xq,int * xqd,const sgr_params_type * params)513 static void encode_xq(int *xq, int *xqd, const sgr_params_type *params) {
514 if (params->r[0] == 0) {
515 xqd[0] = 0;
516 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xq[1], SGRPROJ_PRJ_MIN1,
517 SGRPROJ_PRJ_MAX1);
518 } else if (params->r[1] == 0) {
519 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
520 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0], SGRPROJ_PRJ_MIN1,
521 SGRPROJ_PRJ_MAX1);
522 } else {
523 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
524 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0] - xq[1], SGRPROJ_PRJ_MIN1,
525 SGRPROJ_PRJ_MAX1);
526 }
527 }
528
529 // Apply the self-guided filter across an entire restoration unit.
apply_sgr(int sgr_params_idx,const uint8_t * dat8,int width,int height,int dat_stride,int use_highbd,int bit_depth,int pu_width,int pu_height,int32_t * flt0,int32_t * flt1,int flt_stride)530 static void apply_sgr(int sgr_params_idx, const uint8_t *dat8, int width,
531 int height, int dat_stride, int use_highbd, int bit_depth,
532 int pu_width, int pu_height, int32_t *flt0, int32_t *flt1,
533 int flt_stride) {
534 for (int i = 0; i < height; i += pu_height) {
535 const int h = AOMMIN(pu_height, height - i);
536 int32_t *flt0_row = flt0 + i * flt_stride;
537 int32_t *flt1_row = flt1 + i * flt_stride;
538 const uint8_t *dat8_row = dat8 + i * dat_stride;
539
540 // Iterate over the stripe in blocks of width pu_width
541 for (int j = 0; j < width; j += pu_width) {
542 const int w = AOMMIN(pu_width, width - j);
543 const int ret = av1_selfguided_restoration(
544 dat8_row + j, w, h, dat_stride, flt0_row + j, flt1_row + j,
545 flt_stride, sgr_params_idx, bit_depth, use_highbd);
546 (void)ret;
547 assert(!ret);
548 }
549 }
550 }
551
search_selfguided_restoration(const uint8_t * dat8,int width,int height,int dat_stride,const uint8_t * src8,int src_stride,int use_highbitdepth,int bit_depth,int pu_width,int pu_height,int32_t * rstbuf)552 static SgrprojInfo search_selfguided_restoration(
553 const uint8_t *dat8, int width, int height, int dat_stride,
554 const uint8_t *src8, int src_stride, int use_highbitdepth, int bit_depth,
555 int pu_width, int pu_height, int32_t *rstbuf) {
556 int32_t *flt0 = rstbuf;
557 int32_t *flt1 = flt0 + RESTORATION_UNITPELS_MAX;
558 int ep, bestep = 0;
559 int64_t besterr = -1;
560 int exqd[2], bestxqd[2] = { 0, 0 };
561 int flt_stride = ((width + 7) & ~7) + 8;
562 assert(pu_width == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
563 pu_width == RESTORATION_PROC_UNIT_SIZE);
564 assert(pu_height == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
565 pu_height == RESTORATION_PROC_UNIT_SIZE);
566
567 for (ep = 0; ep < SGRPROJ_PARAMS; ep++) {
568 int exq[2];
569 apply_sgr(ep, dat8, width, height, dat_stride, use_highbitdepth, bit_depth,
570 pu_width, pu_height, flt0, flt1, flt_stride);
571 aom_clear_system_state();
572 const sgr_params_type *const params = &sgr_params[ep];
573 get_proj_subspace(src8, width, height, src_stride, dat8, dat_stride,
574 use_highbitdepth, flt0, flt_stride, flt1, flt_stride, exq,
575 params);
576 aom_clear_system_state();
577 encode_xq(exq, exqd, params);
578 int64_t err = finer_search_pixel_proj_error(
579 src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth,
580 flt0, flt_stride, flt1, flt_stride, 2, exqd, params);
581 if (besterr == -1 || err < besterr) {
582 bestep = ep;
583 besterr = err;
584 bestxqd[0] = exqd[0];
585 bestxqd[1] = exqd[1];
586 }
587 }
588
589 SgrprojInfo ret;
590 ret.ep = bestep;
591 ret.xqd[0] = bestxqd[0];
592 ret.xqd[1] = bestxqd[1];
593 return ret;
594 }
595
count_sgrproj_bits(SgrprojInfo * sgrproj_info,SgrprojInfo * ref_sgrproj_info)596 static int count_sgrproj_bits(SgrprojInfo *sgrproj_info,
597 SgrprojInfo *ref_sgrproj_info) {
598 int bits = SGRPROJ_PARAMS_BITS;
599 const sgr_params_type *params = &sgr_params[sgrproj_info->ep];
600 if (params->r[0] > 0)
601 bits += aom_count_primitive_refsubexpfin(
602 SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
603 ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0,
604 sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0);
605 if (params->r[1] > 0)
606 bits += aom_count_primitive_refsubexpfin(
607 SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
608 ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1,
609 sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1);
610 return bits;
611 }
612
search_sgrproj(const RestorationTileLimits * limits,const AV1PixelRect * tile,int rest_unit_idx,void * priv,int32_t * tmpbuf,RestorationLineBuffers * rlbs)613 static void search_sgrproj(const RestorationTileLimits *limits,
614 const AV1PixelRect *tile, int rest_unit_idx,
615 void *priv, int32_t *tmpbuf,
616 RestorationLineBuffers *rlbs) {
617 (void)rlbs;
618 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
619 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
620
621 const MACROBLOCK *const x = rsc->x;
622 const AV1_COMMON *const cm = rsc->cm;
623 const int highbd = cm->seq_params.use_highbitdepth;
624 const int bit_depth = cm->seq_params.bit_depth;
625
626 uint8_t *dgd_start =
627 rsc->dgd_buffer + limits->v_start * rsc->dgd_stride + limits->h_start;
628 const uint8_t *src_start =
629 rsc->src_buffer + limits->v_start * rsc->src_stride + limits->h_start;
630
631 const int is_uv = rsc->plane > 0;
632 const int ss_x = is_uv && cm->seq_params.subsampling_x;
633 const int ss_y = is_uv && cm->seq_params.subsampling_y;
634 const int procunit_width = RESTORATION_PROC_UNIT_SIZE >> ss_x;
635 const int procunit_height = RESTORATION_PROC_UNIT_SIZE >> ss_y;
636
637 rusi->sgrproj = search_selfguided_restoration(
638 dgd_start, limits->h_end - limits->h_start,
639 limits->v_end - limits->v_start, rsc->dgd_stride, src_start,
640 rsc->src_stride, highbd, bit_depth, procunit_width, procunit_height,
641 tmpbuf);
642
643 RestorationUnitInfo rui;
644 rui.restoration_type = RESTORE_SGRPROJ;
645 rui.sgrproj_info = rusi->sgrproj;
646
647 rusi->sse[RESTORE_SGRPROJ] = try_restoration_unit(rsc, limits, tile, &rui);
648
649 const int64_t bits_none = x->sgrproj_restore_cost[0];
650 const int64_t bits_sgr = x->sgrproj_restore_cost[1] +
651 (count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj)
652 << AV1_PROB_COST_SHIFT);
653
654 double cost_none =
655 RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
656 double cost_sgr =
657 RDCOST_DBL(x->rdmult, bits_sgr >> 4, rusi->sse[RESTORE_SGRPROJ]);
658 if (rusi->sgrproj.ep < 10)
659 cost_sgr *= (1 + DUAL_SGR_PENALTY_MULT * rsc->sf->dual_sgr_penalty_level);
660
661 RestorationType rtype =
662 (cost_sgr < cost_none) ? RESTORE_SGRPROJ : RESTORE_NONE;
663 rusi->best_rtype[RESTORE_SGRPROJ - 1] = rtype;
664
665 rsc->sse += rusi->sse[rtype];
666 rsc->bits += (cost_sgr < cost_none) ? bits_sgr : bits_none;
667 if (cost_sgr < cost_none) rsc->sgrproj = rusi->sgrproj;
668 }
669
av1_compute_stats_c(int wiener_win,const uint8_t * dgd,const uint8_t * src,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H)670 void av1_compute_stats_c(int wiener_win, const uint8_t *dgd, const uint8_t *src,
671 int h_start, int h_end, int v_start, int v_end,
672 int dgd_stride, int src_stride, int64_t *M,
673 int64_t *H) {
674 int i, j, k, l;
675 int16_t Y[WIENER_WIN2];
676 const int wiener_win2 = wiener_win * wiener_win;
677 const int wiener_halfwin = (wiener_win >> 1);
678 uint8_t avg = find_average(dgd, h_start, h_end, v_start, v_end, dgd_stride);
679
680 memset(M, 0, sizeof(*M) * wiener_win2);
681 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
682 for (i = v_start; i < v_end; i++) {
683 for (j = h_start; j < h_end; j++) {
684 const int16_t X = (int16_t)src[i * src_stride + j] - (int16_t)avg;
685 int idx = 0;
686 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
687 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
688 Y[idx] = (int16_t)dgd[(i + l) * dgd_stride + (j + k)] - (int16_t)avg;
689 idx++;
690 }
691 }
692 assert(idx == wiener_win2);
693 for (k = 0; k < wiener_win2; ++k) {
694 M[k] += (int32_t)Y[k] * X;
695 for (l = k; l < wiener_win2; ++l) {
696 // H is a symmetric matrix, so we only need to fill out the upper
697 // triangle here. We can copy it down to the lower triangle outside
698 // the (i, j) loops.
699 H[k * wiener_win2 + l] += (int32_t)Y[k] * Y[l];
700 }
701 }
702 }
703 }
704 for (k = 0; k < wiener_win2; ++k) {
705 for (l = k + 1; l < wiener_win2; ++l) {
706 H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
707 }
708 }
709 }
710
av1_compute_stats_highbd_c(int wiener_win,const uint8_t * dgd8,const uint8_t * src8,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,aom_bit_depth_t bit_depth)711 void av1_compute_stats_highbd_c(int wiener_win, const uint8_t *dgd8,
712 const uint8_t *src8, int h_start, int h_end,
713 int v_start, int v_end, int dgd_stride,
714 int src_stride, int64_t *M, int64_t *H,
715 aom_bit_depth_t bit_depth) {
716 int i, j, k, l;
717 int32_t Y[WIENER_WIN2];
718 const int wiener_win2 = wiener_win * wiener_win;
719 const int wiener_halfwin = (wiener_win >> 1);
720 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
721 const uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
722 uint16_t avg =
723 find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
724
725 uint8_t bit_depth_divider = 1;
726 if (bit_depth == AOM_BITS_12)
727 bit_depth_divider = 16;
728 else if (bit_depth == AOM_BITS_10)
729 bit_depth_divider = 4;
730
731 memset(M, 0, sizeof(*M) * wiener_win2);
732 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
733 for (i = v_start; i < v_end; i++) {
734 for (j = h_start; j < h_end; j++) {
735 const int32_t X = (int32_t)src[i * src_stride + j] - (int32_t)avg;
736 int idx = 0;
737 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
738 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
739 Y[idx] = (int32_t)dgd[(i + l) * dgd_stride + (j + k)] - (int32_t)avg;
740 idx++;
741 }
742 }
743 assert(idx == wiener_win2);
744 for (k = 0; k < wiener_win2; ++k) {
745 M[k] += (int64_t)Y[k] * X;
746 for (l = k; l < wiener_win2; ++l) {
747 // H is a symmetric matrix, so we only need to fill out the upper
748 // triangle here. We can copy it down to the lower triangle outside
749 // the (i, j) loops.
750 H[k * wiener_win2 + l] += (int64_t)Y[k] * Y[l];
751 }
752 }
753 }
754 }
755 for (k = 0; k < wiener_win2; ++k) {
756 M[k] /= bit_depth_divider;
757 H[k * wiener_win2 + k] /= bit_depth_divider;
758 for (l = k + 1; l < wiener_win2; ++l) {
759 H[k * wiener_win2 + l] /= bit_depth_divider;
760 H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
761 }
762 }
763 }
764
wrap_index(int i,int wiener_win)765 static INLINE int wrap_index(int i, int wiener_win) {
766 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
767 return (i >= wiener_halfwin1 ? wiener_win - 1 - i : i);
768 }
769
770 // Solve linear equations to find Wiener filter tap values
771 // Taps are output scaled by WIENER_FILT_STEP
linsolve_wiener(int n,int64_t * A,int stride,int64_t * b,int32_t * x)772 static int linsolve_wiener(int n, int64_t *A, int stride, int64_t *b,
773 int32_t *x) {
774 for (int k = 0; k < n - 1; k++) {
775 // Partial pivoting: bring the row with the largest pivot to the top
776 for (int i = n - 1; i > k; i--) {
777 // If row i has a better (bigger) pivot than row (i-1), swap them
778 if (llabs(A[(i - 1) * stride + k]) < llabs(A[i * stride + k])) {
779 for (int j = 0; j < n; j++) {
780 const int64_t c = A[i * stride + j];
781 A[i * stride + j] = A[(i - 1) * stride + j];
782 A[(i - 1) * stride + j] = c;
783 }
784 const int64_t c = b[i];
785 b[i] = b[i - 1];
786 b[i - 1] = c;
787 }
788 }
789 // Forward elimination (convert A to row-echelon form)
790 for (int i = k; i < n - 1; i++) {
791 if (A[k * stride + k] == 0) return 0;
792 const int64_t c = A[(i + 1) * stride + k];
793 const int64_t cd = A[k * stride + k];
794 for (int j = 0; j < n; j++) {
795 A[(i + 1) * stride + j] -= c / 256 * A[k * stride + j] / cd * 256;
796 }
797 b[i + 1] -= c * b[k] / cd;
798 }
799 }
800 // Back-substitution
801 for (int i = n - 1; i >= 0; i--) {
802 if (A[i * stride + i] == 0) return 0;
803 int64_t c = 0;
804 for (int j = i + 1; j <= n - 1; j++) {
805 c += A[i * stride + j] * x[j] / WIENER_TAP_SCALE_FACTOR;
806 }
807 // Store filter taps x in scaled form.
808 x[i] = (int32_t)(WIENER_TAP_SCALE_FACTOR * (b[i] - c) / A[i * stride + i]);
809 }
810
811 return 1;
812 }
813
814 // Fix vector b, update vector a
update_a_sep_sym(int wiener_win,int64_t ** Mc,int64_t ** Hc,int32_t * a,int32_t * b)815 static void update_a_sep_sym(int wiener_win, int64_t **Mc, int64_t **Hc,
816 int32_t *a, int32_t *b) {
817 int i, j;
818 int32_t S[WIENER_WIN];
819 int64_t A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
820 const int wiener_win2 = wiener_win * wiener_win;
821 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
822 memset(A, 0, sizeof(A));
823 memset(B, 0, sizeof(B));
824 for (i = 0; i < wiener_win; i++) {
825 for (j = 0; j < wiener_win; ++j) {
826 const int jj = wrap_index(j, wiener_win);
827 A[jj] += Mc[i][j] * b[i] / WIENER_TAP_SCALE_FACTOR;
828 }
829 }
830 for (i = 0; i < wiener_win; i++) {
831 for (j = 0; j < wiener_win; j++) {
832 int k, l;
833 for (k = 0; k < wiener_win; ++k) {
834 for (l = 0; l < wiener_win; ++l) {
835 const int kk = wrap_index(k, wiener_win);
836 const int ll = wrap_index(l, wiener_win);
837 B[ll * wiener_halfwin1 + kk] +=
838 Hc[j * wiener_win + i][k * wiener_win2 + l] * b[i] /
839 WIENER_TAP_SCALE_FACTOR * b[j] / WIENER_TAP_SCALE_FACTOR;
840 }
841 }
842 }
843 }
844 // Normalization enforcement in the system of equations itself
845 for (i = 0; i < wiener_halfwin1 - 1; ++i) {
846 A[i] -=
847 A[wiener_halfwin1 - 1] * 2 +
848 B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
849 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
850 }
851 for (i = 0; i < wiener_halfwin1 - 1; ++i) {
852 for (j = 0; j < wiener_halfwin1 - 1; ++j) {
853 B[i * wiener_halfwin1 + j] -=
854 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
855 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
856 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
857 (wiener_halfwin1 - 1)]);
858 }
859 }
860 if (linsolve_wiener(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
861 S[wiener_halfwin1 - 1] = WIENER_TAP_SCALE_FACTOR;
862 for (i = wiener_halfwin1; i < wiener_win; ++i) {
863 S[i] = S[wiener_win - 1 - i];
864 S[wiener_halfwin1 - 1] -= 2 * S[i];
865 }
866 memcpy(a, S, wiener_win * sizeof(*a));
867 }
868 }
869
870 // Fix vector a, update vector b
update_b_sep_sym(int wiener_win,int64_t ** Mc,int64_t ** Hc,int32_t * a,int32_t * b)871 static void update_b_sep_sym(int wiener_win, int64_t **Mc, int64_t **Hc,
872 int32_t *a, int32_t *b) {
873 int i, j;
874 int32_t S[WIENER_WIN];
875 int64_t A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
876 const int wiener_win2 = wiener_win * wiener_win;
877 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
878 memset(A, 0, sizeof(A));
879 memset(B, 0, sizeof(B));
880 for (i = 0; i < wiener_win; i++) {
881 const int ii = wrap_index(i, wiener_win);
882 for (j = 0; j < wiener_win; j++) {
883 A[ii] += Mc[i][j] * a[j] / WIENER_TAP_SCALE_FACTOR;
884 }
885 }
886
887 for (i = 0; i < wiener_win; i++) {
888 for (j = 0; j < wiener_win; j++) {
889 const int ii = wrap_index(i, wiener_win);
890 const int jj = wrap_index(j, wiener_win);
891 int k, l;
892 for (k = 0; k < wiener_win; ++k) {
893 for (l = 0; l < wiener_win; ++l) {
894 B[jj * wiener_halfwin1 + ii] +=
895 Hc[i * wiener_win + j][k * wiener_win2 + l] * a[k] /
896 WIENER_TAP_SCALE_FACTOR * a[l] / WIENER_TAP_SCALE_FACTOR;
897 }
898 }
899 }
900 }
901 // Normalization enforcement in the system of equations itself
902 for (i = 0; i < wiener_halfwin1 - 1; ++i) {
903 A[i] -=
904 A[wiener_halfwin1 - 1] * 2 +
905 B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
906 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
907 }
908 for (i = 0; i < wiener_halfwin1 - 1; ++i) {
909 for (j = 0; j < wiener_halfwin1 - 1; ++j) {
910 B[i * wiener_halfwin1 + j] -=
911 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
912 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
913 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
914 (wiener_halfwin1 - 1)]);
915 }
916 }
917 if (linsolve_wiener(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
918 S[wiener_halfwin1 - 1] = WIENER_TAP_SCALE_FACTOR;
919 for (i = wiener_halfwin1; i < wiener_win; ++i) {
920 S[i] = S[wiener_win - 1 - i];
921 S[wiener_halfwin1 - 1] -= 2 * S[i];
922 }
923 memcpy(b, S, wiener_win * sizeof(*b));
924 }
925 }
926
wiener_decompose_sep_sym(int wiener_win,int64_t * M,int64_t * H,int32_t * a,int32_t * b)927 static int wiener_decompose_sep_sym(int wiener_win, int64_t *M, int64_t *H,
928 int32_t *a, int32_t *b) {
929 static const int32_t init_filt[WIENER_WIN] = {
930 WIENER_FILT_TAP0_MIDV, WIENER_FILT_TAP1_MIDV, WIENER_FILT_TAP2_MIDV,
931 WIENER_FILT_TAP3_MIDV, WIENER_FILT_TAP2_MIDV, WIENER_FILT_TAP1_MIDV,
932 WIENER_FILT_TAP0_MIDV,
933 };
934 int64_t *Hc[WIENER_WIN2];
935 int64_t *Mc[WIENER_WIN];
936 int i, j, iter;
937 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
938 const int wiener_win2 = wiener_win * wiener_win;
939 for (i = 0; i < wiener_win; i++) {
940 a[i] = b[i] =
941 WIENER_TAP_SCALE_FACTOR / WIENER_FILT_STEP * init_filt[i + plane_off];
942 }
943 for (i = 0; i < wiener_win; i++) {
944 Mc[i] = M + i * wiener_win;
945 for (j = 0; j < wiener_win; j++) {
946 Hc[i * wiener_win + j] =
947 H + i * wiener_win * wiener_win2 + j * wiener_win;
948 }
949 }
950
951 iter = 1;
952 while (iter < NUM_WIENER_ITERS) {
953 update_a_sep_sym(wiener_win, Mc, Hc, a, b);
954 update_b_sep_sym(wiener_win, Mc, Hc, a, b);
955 iter++;
956 }
957 return 1;
958 }
959
960 // Computes the function x'*H*x - x'*M for the learned 2D filter x, and compares
961 // against identity filters; Final score is defined as the difference between
962 // the function values
compute_score(int wiener_win,int64_t * M,int64_t * H,InterpKernel vfilt,InterpKernel hfilt)963 static int64_t compute_score(int wiener_win, int64_t *M, int64_t *H,
964 InterpKernel vfilt, InterpKernel hfilt) {
965 int32_t ab[WIENER_WIN * WIENER_WIN];
966 int16_t a[WIENER_WIN], b[WIENER_WIN];
967 int64_t P = 0, Q = 0;
968 int64_t iP = 0, iQ = 0;
969 int64_t Score, iScore;
970 int i, k, l;
971 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
972 const int wiener_win2 = wiener_win * wiener_win;
973
974 aom_clear_system_state();
975
976 a[WIENER_HALFWIN] = b[WIENER_HALFWIN] = WIENER_FILT_STEP;
977 for (i = 0; i < WIENER_HALFWIN; ++i) {
978 a[i] = a[WIENER_WIN - i - 1] = vfilt[i];
979 b[i] = b[WIENER_WIN - i - 1] = hfilt[i];
980 a[WIENER_HALFWIN] -= 2 * a[i];
981 b[WIENER_HALFWIN] -= 2 * b[i];
982 }
983 memset(ab, 0, sizeof(ab));
984 for (k = 0; k < wiener_win; ++k) {
985 for (l = 0; l < wiener_win; ++l)
986 ab[k * wiener_win + l] = a[l + plane_off] * b[k + plane_off];
987 }
988 for (k = 0; k < wiener_win2; ++k) {
989 P += ab[k] * M[k] / WIENER_FILT_STEP / WIENER_FILT_STEP;
990 for (l = 0; l < wiener_win2; ++l) {
991 Q += ab[k] * H[k * wiener_win2 + l] * ab[l] / WIENER_FILT_STEP /
992 WIENER_FILT_STEP / WIENER_FILT_STEP / WIENER_FILT_STEP;
993 }
994 }
995 Score = Q - 2 * P;
996
997 iP = M[wiener_win2 >> 1];
998 iQ = H[(wiener_win2 >> 1) * wiener_win2 + (wiener_win2 >> 1)];
999 iScore = iQ - 2 * iP;
1000
1001 return Score - iScore;
1002 }
1003
finalize_sym_filter(int wiener_win,int32_t * f,InterpKernel fi)1004 static void finalize_sym_filter(int wiener_win, int32_t *f, InterpKernel fi) {
1005 int i;
1006 const int wiener_halfwin = (wiener_win >> 1);
1007
1008 for (i = 0; i < wiener_halfwin; ++i) {
1009 const int64_t dividend = f[i] * WIENER_FILT_STEP;
1010 const int64_t divisor = WIENER_TAP_SCALE_FACTOR;
1011 // Perform this division with proper rounding rather than truncation
1012 if (dividend < 0) {
1013 fi[i] = (int16_t)((dividend - (divisor / 2)) / divisor);
1014 } else {
1015 fi[i] = (int16_t)((dividend + (divisor / 2)) / divisor);
1016 }
1017 }
1018 // Specialize for 7-tap filter
1019 if (wiener_win == WIENER_WIN) {
1020 fi[0] = CLIP(fi[0], WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP0_MAXV);
1021 fi[1] = CLIP(fi[1], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
1022 fi[2] = CLIP(fi[2], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
1023 } else {
1024 fi[2] = CLIP(fi[1], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
1025 fi[1] = CLIP(fi[0], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
1026 fi[0] = 0;
1027 }
1028 // Satisfy filter constraints
1029 fi[WIENER_WIN - 1] = fi[0];
1030 fi[WIENER_WIN - 2] = fi[1];
1031 fi[WIENER_WIN - 3] = fi[2];
1032 // The central element has an implicit +WIENER_FILT_STEP
1033 fi[3] = -2 * (fi[0] + fi[1] + fi[2]);
1034 }
1035
count_wiener_bits(int wiener_win,WienerInfo * wiener_info,WienerInfo * ref_wiener_info)1036 static int count_wiener_bits(int wiener_win, WienerInfo *wiener_info,
1037 WienerInfo *ref_wiener_info) {
1038 int bits = 0;
1039 if (wiener_win == WIENER_WIN)
1040 bits += aom_count_primitive_refsubexpfin(
1041 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1042 WIENER_FILT_TAP0_SUBEXP_K,
1043 ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV,
1044 wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV);
1045 bits += aom_count_primitive_refsubexpfin(
1046 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1047 WIENER_FILT_TAP1_SUBEXP_K,
1048 ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV,
1049 wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV);
1050 bits += aom_count_primitive_refsubexpfin(
1051 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1052 WIENER_FILT_TAP2_SUBEXP_K,
1053 ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV,
1054 wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV);
1055 if (wiener_win == WIENER_WIN)
1056 bits += aom_count_primitive_refsubexpfin(
1057 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1058 WIENER_FILT_TAP0_SUBEXP_K,
1059 ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV,
1060 wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV);
1061 bits += aom_count_primitive_refsubexpfin(
1062 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1063 WIENER_FILT_TAP1_SUBEXP_K,
1064 ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV,
1065 wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV);
1066 bits += aom_count_primitive_refsubexpfin(
1067 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1068 WIENER_FILT_TAP2_SUBEXP_K,
1069 ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV,
1070 wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV);
1071 return bits;
1072 }
1073
1074 #define USE_WIENER_REFINEMENT_SEARCH 1
finer_tile_search_wiener(const RestSearchCtxt * rsc,const RestorationTileLimits * limits,const AV1PixelRect * tile,RestorationUnitInfo * rui,int wiener_win)1075 static int64_t finer_tile_search_wiener(const RestSearchCtxt *rsc,
1076 const RestorationTileLimits *limits,
1077 const AV1PixelRect *tile,
1078 RestorationUnitInfo *rui,
1079 int wiener_win) {
1080 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
1081 int64_t err = try_restoration_unit(rsc, limits, tile, rui);
1082 #if USE_WIENER_REFINEMENT_SEARCH
1083 int64_t err2;
1084 int tap_min[] = { WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP1_MINV,
1085 WIENER_FILT_TAP2_MINV };
1086 int tap_max[] = { WIENER_FILT_TAP0_MAXV, WIENER_FILT_TAP1_MAXV,
1087 WIENER_FILT_TAP2_MAXV };
1088
1089 WienerInfo *plane_wiener = &rui->wiener_info;
1090
1091 // printf("err pre = %"PRId64"\n", err);
1092 const int start_step = 4;
1093 for (int s = start_step; s >= 1; s >>= 1) {
1094 for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
1095 int skip = 0;
1096 do {
1097 if (plane_wiener->hfilter[p] - s >= tap_min[p]) {
1098 plane_wiener->hfilter[p] -= s;
1099 plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
1100 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
1101 err2 = try_restoration_unit(rsc, limits, tile, rui);
1102 if (err2 > err) {
1103 plane_wiener->hfilter[p] += s;
1104 plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
1105 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
1106 } else {
1107 err = err2;
1108 skip = 1;
1109 // At the highest step size continue moving in the same direction
1110 if (s == start_step) continue;
1111 }
1112 }
1113 break;
1114 } while (1);
1115 if (skip) break;
1116 do {
1117 if (plane_wiener->hfilter[p] + s <= tap_max[p]) {
1118 plane_wiener->hfilter[p] += s;
1119 plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
1120 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
1121 err2 = try_restoration_unit(rsc, limits, tile, rui);
1122 if (err2 > err) {
1123 plane_wiener->hfilter[p] -= s;
1124 plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
1125 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
1126 } else {
1127 err = err2;
1128 // At the highest step size continue moving in the same direction
1129 if (s == start_step) continue;
1130 }
1131 }
1132 break;
1133 } while (1);
1134 }
1135 for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
1136 int skip = 0;
1137 do {
1138 if (plane_wiener->vfilter[p] - s >= tap_min[p]) {
1139 plane_wiener->vfilter[p] -= s;
1140 plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
1141 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
1142 err2 = try_restoration_unit(rsc, limits, tile, rui);
1143 if (err2 > err) {
1144 plane_wiener->vfilter[p] += s;
1145 plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
1146 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
1147 } else {
1148 err = err2;
1149 skip = 1;
1150 // At the highest step size continue moving in the same direction
1151 if (s == start_step) continue;
1152 }
1153 }
1154 break;
1155 } while (1);
1156 if (skip) break;
1157 do {
1158 if (plane_wiener->vfilter[p] + s <= tap_max[p]) {
1159 plane_wiener->vfilter[p] += s;
1160 plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
1161 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
1162 err2 = try_restoration_unit(rsc, limits, tile, rui);
1163 if (err2 > err) {
1164 plane_wiener->vfilter[p] -= s;
1165 plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
1166 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
1167 } else {
1168 err = err2;
1169 // At the highest step size continue moving in the same direction
1170 if (s == start_step) continue;
1171 }
1172 }
1173 break;
1174 } while (1);
1175 }
1176 }
1177 // printf("err post = %"PRId64"\n", err);
1178 #endif // USE_WIENER_REFINEMENT_SEARCH
1179 return err;
1180 }
1181
search_wiener(const RestorationTileLimits * limits,const AV1PixelRect * tile_rect,int rest_unit_idx,void * priv,int32_t * tmpbuf,RestorationLineBuffers * rlbs)1182 static void search_wiener(const RestorationTileLimits *limits,
1183 const AV1PixelRect *tile_rect, int rest_unit_idx,
1184 void *priv, int32_t *tmpbuf,
1185 RestorationLineBuffers *rlbs) {
1186 (void)tmpbuf;
1187 (void)rlbs;
1188 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1189 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1190
1191 const int wiener_win =
1192 (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
1193
1194 int64_t M[WIENER_WIN2];
1195 int64_t H[WIENER_WIN2 * WIENER_WIN2];
1196 int32_t vfilter[WIENER_WIN], hfilter[WIENER_WIN];
1197
1198 const AV1_COMMON *const cm = rsc->cm;
1199 if (cm->seq_params.use_highbitdepth) {
1200 av1_compute_stats_highbd(wiener_win, rsc->dgd_buffer, rsc->src_buffer,
1201 limits->h_start, limits->h_end, limits->v_start,
1202 limits->v_end, rsc->dgd_stride, rsc->src_stride, M,
1203 H, cm->seq_params.bit_depth);
1204 } else {
1205 av1_compute_stats(wiener_win, rsc->dgd_buffer, rsc->src_buffer,
1206 limits->h_start, limits->h_end, limits->v_start,
1207 limits->v_end, rsc->dgd_stride, rsc->src_stride, M, H);
1208 }
1209
1210 const MACROBLOCK *const x = rsc->x;
1211 const int64_t bits_none = x->wiener_restore_cost[0];
1212
1213 if (!wiener_decompose_sep_sym(wiener_win, M, H, vfilter, hfilter)) {
1214 rsc->bits += bits_none;
1215 rsc->sse += rusi->sse[RESTORE_NONE];
1216 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1217 rusi->sse[RESTORE_WIENER] = INT64_MAX;
1218 return;
1219 }
1220
1221 RestorationUnitInfo rui;
1222 memset(&rui, 0, sizeof(rui));
1223 rui.restoration_type = RESTORE_WIENER;
1224 finalize_sym_filter(wiener_win, vfilter, rui.wiener_info.vfilter);
1225 finalize_sym_filter(wiener_win, hfilter, rui.wiener_info.hfilter);
1226
1227 // Filter score computes the value of the function x'*A*x - x'*b for the
1228 // learned filter and compares it against identity filer. If there is no
1229 // reduction in the function, the filter is reverted back to identity
1230 if (compute_score(wiener_win, M, H, rui.wiener_info.vfilter,
1231 rui.wiener_info.hfilter) > 0) {
1232 rsc->bits += bits_none;
1233 rsc->sse += rusi->sse[RESTORE_NONE];
1234 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1235 rusi->sse[RESTORE_WIENER] = INT64_MAX;
1236 return;
1237 }
1238
1239 aom_clear_system_state();
1240
1241 rusi->sse[RESTORE_WIENER] =
1242 finer_tile_search_wiener(rsc, limits, tile_rect, &rui, wiener_win);
1243 rusi->wiener = rui.wiener_info;
1244
1245 if (wiener_win != WIENER_WIN) {
1246 assert(rui.wiener_info.vfilter[0] == 0 &&
1247 rui.wiener_info.vfilter[WIENER_WIN - 1] == 0);
1248 assert(rui.wiener_info.hfilter[0] == 0 &&
1249 rui.wiener_info.hfilter[WIENER_WIN - 1] == 0);
1250 }
1251
1252 const int64_t bits_wiener =
1253 x->wiener_restore_cost[1] +
1254 (count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener)
1255 << AV1_PROB_COST_SHIFT);
1256
1257 double cost_none =
1258 RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
1259 double cost_wiener =
1260 RDCOST_DBL(x->rdmult, bits_wiener >> 4, rusi->sse[RESTORE_WIENER]);
1261
1262 RestorationType rtype =
1263 (cost_wiener < cost_none) ? RESTORE_WIENER : RESTORE_NONE;
1264 rusi->best_rtype[RESTORE_WIENER - 1] = rtype;
1265
1266 rsc->sse += rusi->sse[rtype];
1267 rsc->bits += (cost_wiener < cost_none) ? bits_wiener : bits_none;
1268 if (cost_wiener < cost_none) rsc->wiener = rusi->wiener;
1269 }
1270
search_norestore(const RestorationTileLimits * limits,const AV1PixelRect * tile_rect,int rest_unit_idx,void * priv,int32_t * tmpbuf,RestorationLineBuffers * rlbs)1271 static void search_norestore(const RestorationTileLimits *limits,
1272 const AV1PixelRect *tile_rect, int rest_unit_idx,
1273 void *priv, int32_t *tmpbuf,
1274 RestorationLineBuffers *rlbs) {
1275 (void)tile_rect;
1276 (void)tmpbuf;
1277 (void)rlbs;
1278
1279 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1280 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1281
1282 const int highbd = rsc->cm->seq_params.use_highbitdepth;
1283 rusi->sse[RESTORE_NONE] = sse_restoration_unit(
1284 limits, rsc->src, &rsc->cm->cur_frame->buf, rsc->plane, highbd);
1285
1286 rsc->sse += rusi->sse[RESTORE_NONE];
1287 }
1288
search_switchable(const RestorationTileLimits * limits,const AV1PixelRect * tile_rect,int rest_unit_idx,void * priv,int32_t * tmpbuf,RestorationLineBuffers * rlbs)1289 static void search_switchable(const RestorationTileLimits *limits,
1290 const AV1PixelRect *tile_rect, int rest_unit_idx,
1291 void *priv, int32_t *tmpbuf,
1292 RestorationLineBuffers *rlbs) {
1293 (void)limits;
1294 (void)tile_rect;
1295 (void)tmpbuf;
1296 (void)rlbs;
1297 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1298 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1299
1300 const MACROBLOCK *const x = rsc->x;
1301
1302 const int wiener_win =
1303 (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
1304
1305 double best_cost = 0;
1306 int64_t best_bits = 0;
1307 RestorationType best_rtype = RESTORE_NONE;
1308
1309 for (RestorationType r = 0; r < RESTORE_SWITCHABLE_TYPES; ++r) {
1310 // Check for the condition that wiener or sgrproj search could not
1311 // find a solution or the solution was worse than RESTORE_NONE.
1312 // In either case the best_rtype will be set as RESTORE_NONE. These
1313 // should be skipped from the test below.
1314 if (r > RESTORE_NONE) {
1315 if (rusi->best_rtype[r - 1] == RESTORE_NONE) continue;
1316 }
1317
1318 const int64_t sse = rusi->sse[r];
1319 int64_t coeff_pcost = 0;
1320 switch (r) {
1321 case RESTORE_NONE: coeff_pcost = 0; break;
1322 case RESTORE_WIENER:
1323 coeff_pcost =
1324 count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener);
1325 break;
1326 case RESTORE_SGRPROJ:
1327 coeff_pcost = count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj);
1328 break;
1329 default: assert(0); break;
1330 }
1331 const int64_t coeff_bits = coeff_pcost << AV1_PROB_COST_SHIFT;
1332 const int64_t bits = x->switchable_restore_cost[r] + coeff_bits;
1333 double cost = RDCOST_DBL(x->rdmult, bits >> 4, sse);
1334 if (r == RESTORE_SGRPROJ && rusi->sgrproj.ep < 10)
1335 cost *= (1 + DUAL_SGR_PENALTY_MULT * rsc->sf->dual_sgr_penalty_level);
1336 if (r == 0 || cost < best_cost) {
1337 best_cost = cost;
1338 best_bits = bits;
1339 best_rtype = r;
1340 }
1341 }
1342
1343 rusi->best_rtype[RESTORE_SWITCHABLE - 1] = best_rtype;
1344
1345 rsc->sse += rusi->sse[best_rtype];
1346 rsc->bits += best_bits;
1347 if (best_rtype == RESTORE_WIENER) rsc->wiener = rusi->wiener;
1348 if (best_rtype == RESTORE_SGRPROJ) rsc->sgrproj = rusi->sgrproj;
1349 }
1350
copy_unit_info(RestorationType frame_rtype,const RestUnitSearchInfo * rusi,RestorationUnitInfo * rui)1351 static void copy_unit_info(RestorationType frame_rtype,
1352 const RestUnitSearchInfo *rusi,
1353 RestorationUnitInfo *rui) {
1354 assert(frame_rtype > 0);
1355 rui->restoration_type = rusi->best_rtype[frame_rtype - 1];
1356 if (rui->restoration_type == RESTORE_WIENER)
1357 rui->wiener_info = rusi->wiener;
1358 else
1359 rui->sgrproj_info = rusi->sgrproj;
1360 }
1361
search_rest_type(RestSearchCtxt * rsc,RestorationType rtype)1362 static double search_rest_type(RestSearchCtxt *rsc, RestorationType rtype) {
1363 static const rest_unit_visitor_t funs[RESTORE_TYPES] = {
1364 search_norestore, search_wiener, search_sgrproj, search_switchable
1365 };
1366
1367 reset_rsc(rsc);
1368 rsc_on_tile(rsc);
1369
1370 av1_foreach_rest_unit_in_plane(rsc->cm, rsc->plane, funs[rtype], rsc,
1371 &rsc->tile_rect, rsc->cm->rst_tmpbuf, NULL);
1372 return RDCOST_DBL(rsc->x->rdmult, rsc->bits >> 4, rsc->sse);
1373 }
1374
rest_tiles_in_plane(const AV1_COMMON * cm,int plane)1375 static int rest_tiles_in_plane(const AV1_COMMON *cm, int plane) {
1376 const RestorationInfo *rsi = &cm->rst_info[plane];
1377 return rsi->units_per_tile;
1378 }
1379
av1_pick_filter_restoration(const YV12_BUFFER_CONFIG * src,AV1_COMP * cpi)1380 void av1_pick_filter_restoration(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi) {
1381 AV1_COMMON *const cm = &cpi->common;
1382 const int num_planes = av1_num_planes(cm);
1383 assert(!cm->all_lossless);
1384
1385 int ntiles[2];
1386 for (int is_uv = 0; is_uv < 2; ++is_uv)
1387 ntiles[is_uv] = rest_tiles_in_plane(cm, is_uv);
1388
1389 assert(ntiles[1] <= ntiles[0]);
1390 RestUnitSearchInfo *rusi =
1391 (RestUnitSearchInfo *)aom_memalign(16, sizeof(*rusi) * ntiles[0]);
1392
1393 // If the restoration unit dimensions are not multiples of
1394 // rsi->restoration_unit_size then some elements of the rusi array may be
1395 // left uninitialised when we reach copy_unit_info(...). This is not a
1396 // problem, as these elements are ignored later, but in order to quiet
1397 // Valgrind's warnings we initialise the array below.
1398 memset(rusi, 0, sizeof(*rusi) * ntiles[0]);
1399 cpi->td.mb.rdmult = cpi->rd.RDMULT;
1400
1401 RestSearchCtxt rsc;
1402 const int plane_start = AOM_PLANE_Y;
1403 const int plane_end = num_planes > 1 ? AOM_PLANE_V : AOM_PLANE_Y;
1404 for (int plane = plane_start; plane <= plane_end; ++plane) {
1405 init_rsc(src, &cpi->common, &cpi->td.mb, &cpi->sf, plane, rusi,
1406 &cpi->trial_frame_rst, &rsc);
1407
1408 const int plane_ntiles = ntiles[plane > 0];
1409 const RestorationType num_rtypes =
1410 (plane_ntiles > 1) ? RESTORE_TYPES : RESTORE_SWITCHABLE_TYPES;
1411
1412 double best_cost = 0;
1413 RestorationType best_rtype = RESTORE_NONE;
1414
1415 const int highbd = rsc.cm->seq_params.use_highbitdepth;
1416 if (!cpi->sf.disable_loop_restoration_chroma || !plane) {
1417 extend_frame(rsc.dgd_buffer, rsc.plane_width, rsc.plane_height,
1418 rsc.dgd_stride, RESTORATION_BORDER, RESTORATION_BORDER,
1419 highbd);
1420
1421 for (RestorationType r = 0; r < num_rtypes; ++r) {
1422 if ((force_restore_type != RESTORE_TYPES) && (r != RESTORE_NONE) &&
1423 (r != force_restore_type))
1424 continue;
1425
1426 double cost = search_rest_type(&rsc, r);
1427
1428 if (r == 0 || cost < best_cost) {
1429 best_cost = cost;
1430 best_rtype = r;
1431 }
1432 }
1433 }
1434
1435 cm->rst_info[plane].frame_restoration_type = best_rtype;
1436 if (force_restore_type != RESTORE_TYPES)
1437 assert(best_rtype == force_restore_type || best_rtype == RESTORE_NONE);
1438
1439 if (best_rtype != RESTORE_NONE) {
1440 for (int u = 0; u < plane_ntiles; ++u) {
1441 copy_unit_info(best_rtype, &rusi[u], &cm->rst_info[plane].unit_info[u]);
1442 }
1443 }
1444 }
1445
1446 aom_free(rusi);
1447 }
1448