blob: 55fa8e95bcd621244732ae0ec6bd568e6d22c44d [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xubde4ac82016-11-28 15:26:06 -08002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xubde4ac82016-11-28 15:26:06 -08004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13#include <float.h>
14#include <limits.h>
15#include <math.h>
16
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070018
Yaowu Xuf883b422016-08-30 14:01:10 -070019#include "aom_dsp/aom_dsp_common.h"
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -070020#include "aom_dsp/binary_codes_writer.h"
21#include "aom_dsp/psnr.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070022#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070023#include "aom_ports/mem.h"
Jingning Han041c67b2017-04-14 21:39:26 -070024#include "aom_ports/system_state.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025
26#include "av1/common/onyxc_int.h"
27#include "av1/common/quant_common.h"
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -080028#include "av1/common/restoration.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029
Tom Finegan17ce8b12017-02-08 12:46:31 -080030#include "av1/encoder/av1_quantize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070031#include "av1/encoder/encoder.h"
Debargha Mukherjee1330dfd2017-09-03 22:22:27 -070032#include "av1/encoder/mathutils.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033#include "av1/encoder/picklpf.h"
34#include "av1/encoder/pickrst.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070035
Debargha Mukherjeefdbe3f72017-04-06 12:09:19 -070036// When set to RESTORE_WIENER or RESTORE_SGRPROJ only those are allowed.
Tom Finegan8af64ae2017-09-07 08:13:06 -070037// When set to RESTORE_TYPES we allow switchable.
Tom Finegan50c62ee2017-09-07 12:44:16 -070038static const RestorationType force_restore_type = RESTORE_TYPES;
Debargha Mukherjee1b3dbf02017-03-13 14:47:21 -070039
40// Number of Wiener iterations
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -070041#define NUM_WIENER_ITERS 5
Debargha Mukherjee1b3dbf02017-03-13 14:47:21 -070042
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -080043const int frame_level_restore_bits[RESTORE_TYPES] = { 2, 2, 2, 2 };
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -070044
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +010045typedef int64_t (*sse_extractor_type)(const YV12_BUFFER_CONFIG *a,
46 const YV12_BUFFER_CONFIG *b);
47typedef int64_t (*sse_part_extractor_type)(const YV12_BUFFER_CONFIG *a,
48 const YV12_BUFFER_CONFIG *b,
49 int hstart, int width, int vstart,
50 int height);
51
Yaowu Xud3e7c682017-12-21 14:08:25 -080052#define NUM_EXTRACTORS (3 * (1 + 1))
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +010053
54static const sse_part_extractor_type sse_part_extractors[NUM_EXTRACTORS] = {
55 aom_get_y_sse_part, aom_get_u_sse_part,
Yaowu Xud3e7c682017-12-21 14:08:25 -080056 aom_get_v_sse_part, aom_highbd_get_y_sse_part,
57 aom_highbd_get_u_sse_part, aom_highbd_get_v_sse_part,
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +010058};
59
Urvang Joshi813186b2018-03-08 15:38:46 -080060static int64_t sse_restoration_unit(const RestorationTileLimits *limits,
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +010061 const YV12_BUFFER_CONFIG *src,
62 const YV12_BUFFER_CONFIG *dst, int plane,
63 int highbd) {
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +010064 return sse_part_extractors[3 * highbd + plane](
65 src, dst, limits->h_start, limits->h_end - limits->h_start,
66 limits->v_start, limits->v_end - limits->v_start);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -070067}
68
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +000069typedef struct {
70 // The best coefficients for Wiener or Sgrproj restoration
71 WienerInfo wiener;
72 SgrprojInfo sgrproj;
73
74 // The sum of squared errors for this rtype.
75 int64_t sse[RESTORE_SWITCHABLE_TYPES];
76
77 // The rtype to use for this unit given a frame rtype as
78 // index. Indices: WIENER, SGRPROJ, SWITCHABLE.
79 RestorationType best_rtype[RESTORE_TYPES - 1];
80} RestUnitSearchInfo;
81
82typedef struct {
83 const YV12_BUFFER_CONFIG *src;
84 YV12_BUFFER_CONFIG *dst;
85
86 const AV1_COMMON *cm;
87 const MACROBLOCK *x;
88 int plane;
89 int plane_width;
90 int plane_height;
91 RestUnitSearchInfo *rusi;
92
93 uint8_t *dgd_buffer;
94 int dgd_stride;
95 const uint8_t *src_buffer;
96 int src_stride;
97
98 // sse and bits are initialised by reset_rsc in search_rest_type
99 int64_t sse;
100 int64_t bits;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000101 int tile_y0, tile_stripe0;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000102
103 // sgrproj and wiener are initialised by rsc_on_tile when starting the first
104 // tile in the frame.
105 SgrprojInfo sgrproj;
106 WienerInfo wiener;
107} RestSearchCtxt;
108
109static void rsc_on_tile(int tile_row, int tile_col, void *priv) {
110 (void)tile_col;
111
112 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
113 set_default_sgrproj(&rsc->sgrproj);
114 set_default_wiener(&rsc->wiener);
115
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000116 rsc->tile_stripe0 =
117 (tile_row == 0) ? 0 : rsc->cm->rst_end_stripe[tile_row - 1];
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000118}
119
120static void reset_rsc(RestSearchCtxt *rsc) {
121 rsc->sse = 0;
122 rsc->bits = 0;
123}
124
125static void init_rsc(const YV12_BUFFER_CONFIG *src, const AV1_COMMON *cm,
126 const MACROBLOCK *x, int plane, RestUnitSearchInfo *rusi,
127 YV12_BUFFER_CONFIG *dst, RestSearchCtxt *rsc) {
128 rsc->src = src;
129 rsc->dst = dst;
130 rsc->cm = cm;
131 rsc->x = x;
132 rsc->plane = plane;
133 rsc->rusi = rusi;
134
135 const YV12_BUFFER_CONFIG *dgd = cm->frame_to_show;
136 const int is_uv = plane != AOM_PLANE_Y;
137 rsc->plane_width = src->crop_widths[is_uv];
David Barker21f43072017-12-14 14:21:45 +0000138 rsc->plane_height = src->crop_heights[is_uv];
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000139 rsc->src_buffer = src->buffers[plane];
140 rsc->src_stride = src->strides[is_uv];
141 rsc->dgd_buffer = dgd->buffers[plane];
142 rsc->dgd_stride = dgd->strides[is_uv];
143 assert(src->crop_widths[is_uv] == dgd->crop_widths[is_uv]);
144 assert(src->crop_heights[is_uv] == dgd->crop_heights[is_uv]);
145}
146
Urvang Joshi813186b2018-03-08 15:38:46 -0800147static int64_t try_restoration_unit(const RestSearchCtxt *rsc,
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100148 const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000149 const AV1PixelRect *tile_rect,
150 const RestorationUnitInfo *rui) {
151 const AV1_COMMON *const cm = rsc->cm;
152 const int plane = rsc->plane;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100153 const int is_uv = plane > 0;
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000154 const RestorationInfo *rsi = &cm->rst_info[plane];
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100155 RestorationLineBuffers rlbs;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100156 const int bit_depth = cm->bit_depth;
157 const int highbd = cm->use_highbitdepth;
Rupert Swarbrick64b8bbd2017-10-16 15:53:07 +0100158
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100159 const YV12_BUFFER_CONFIG *fts = cm->frame_to_show;
Yunqing Wange8025032018-04-10 18:50:29 -0700160 // TODO(yunqing): For now, only use optimized LR filter in decoder. Can be
161 // also used in encoder.
Yunqing Wang2ff71af2018-04-24 15:10:10 -0700162 const int optimized_lr = 0;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700163
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000164 av1_loop_restoration_filter_unit(
Debargha Mukherjee5105f7a2018-01-29 16:05:54 -0800165 limits, rui, &rsi->boundaries, &rlbs, tile_rect, rsc->tile_stripe0,
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000166 is_uv && cm->subsampling_x, is_uv && cm->subsampling_y, highbd, bit_depth,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000167 fts->buffers[plane], fts->strides[is_uv], rsc->dst->buffers[plane],
Yunqing Wang2ff71af2018-04-24 15:10:10 -0700168 rsc->dst->strides[is_uv], cm->rst_tmpbuf, optimized_lr);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700169
Urvang Joshi813186b2018-03-08 15:38:46 -0800170 return sse_restoration_unit(limits, rsc->src, rsc->dst, plane, highbd);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700171}
172
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100173static int64_t get_pixel_proj_error(const uint8_t *src8, int width, int height,
174 int src_stride, const uint8_t *dat8,
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100175 int dat_stride, int use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000176 int32_t *flt0, int flt0_stride,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700177 int32_t *flt1, int flt1_stride, int *xqd,
178 const sgr_params_type *params) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700179 int i, j;
180 int64_t err = 0;
181 int xq[2];
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000182 decode_xq(xqd, xq, params);
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100183 if (!use_highbitdepth) {
David Barker3a0df182016-12-21 10:44:52 +0000184 const uint8_t *src = src8;
185 const uint8_t *dat = dat8;
186 for (i = 0; i < height; ++i) {
187 for (j = 0; j < width; ++j) {
188 const int32_t u =
189 (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000190 int32_t v = u << SGRPROJ_PRJ_BITS;
Urvang Joshi3715b882018-05-14 20:05:25 -0400191 if (params->r[0] > 0) v += xq[0] * (flt0[i * flt0_stride + j] - u);
192 if (params->r[1] > 0) v += xq[1] * (flt1[i * flt1_stride + j] - u);
David Barker3a0df182016-12-21 10:44:52 +0000193 const int32_t e =
194 ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) -
195 src[i * src_stride + j];
196 err += e * e;
197 }
198 }
199 } else {
200 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
201 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900202 const int32_t half = 1 << (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS - 1);
Urvang Joshi3715b882018-05-14 20:05:25 -0400203 if (params->r[0] > 0 && params->r[1] > 0) {
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900204 int xq0 = xq[0];
205 int xq1 = xq[1];
206 for (i = 0; i < height; ++i) {
207 for (j = 0; j < width; ++j) {
208 const int32_t d = dat[j];
209 const int32_t s = src[j];
210 const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
211 int32_t v0 = flt0[j] - u;
212 int32_t v1 = flt1[j] - u;
213 int32_t v = half;
214 v += xq0 * v0;
215 v += xq1 * v1;
216 const int32_t e =
217 (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
218 err += e * e;
219 }
220 dat += dat_stride;
221 flt0 += flt0_stride;
222 flt1 += flt1_stride;
223 src += src_stride;
224 }
Urvang Joshi3715b882018-05-14 20:05:25 -0400225 } else if (params->r[0] > 0 || params->r[1] > 0) {
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900226 int exq;
227 int32_t *flt;
228 int flt_stride;
Urvang Joshi3715b882018-05-14 20:05:25 -0400229 if (params->r[0] > 0) {
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900230 exq = xq[0];
231 flt = flt0;
232 flt_stride = flt0_stride;
233 } else {
234 exq = xq[1];
235 flt = flt1;
236 flt_stride = flt1_stride;
237 }
238 for (i = 0; i < height; ++i) {
239 for (j = 0; j < width; ++j) {
240 const int32_t d = dat[j];
241 const int32_t s = src[j];
242 const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
243 int32_t v = half;
244 v += exq * (flt[j] - u);
245 const int32_t e =
246 (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
247 err += e * e;
248 }
249 dat += dat_stride;
250 flt += flt_stride;
251 src += src_stride;
252 }
253 } else {
254 for (i = 0; i < height; ++i) {
255 for (j = 0; j < width; ++j) {
256 const int32_t d = dat[j];
257 const int32_t s = src[j];
258 const int32_t e = d - s;
259 err += e * e;
260 }
261 dat += dat_stride;
262 src += src_stride;
David Barker3a0df182016-12-21 10:44:52 +0000263 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700264 }
265 }
266 return err;
267}
268
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700269#define USE_SGRPROJ_REFINEMENT_SEARCH 1
270static int64_t finer_search_pixel_proj_error(
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100271 const uint8_t *src8, int width, int height, int src_stride,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000272 const uint8_t *dat8, int dat_stride, int use_highbitdepth, int32_t *flt0,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700273 int flt0_stride, int32_t *flt1, int flt1_stride, int start_step, int *xqd,
274 const sgr_params_type *params) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000275 int64_t err = get_pixel_proj_error(
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000276 src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth, flt0,
277 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700278 (void)start_step;
279#if USE_SGRPROJ_REFINEMENT_SEARCH
280 int64_t err2;
281 int tap_min[] = { SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MIN1 };
282 int tap_max[] = { SGRPROJ_PRJ_MAX0, SGRPROJ_PRJ_MAX1 };
283 for (int s = start_step; s >= 1; s >>= 1) {
284 for (int p = 0; p < 2; ++p) {
Urvang Joshi3715b882018-05-14 20:05:25 -0400285 if ((params->r[0] == 0 && p == 0) || (params->r[1] == 0 && p == 1)) {
286 continue;
287 }
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700288 int skip = 0;
289 do {
290 if (xqd[p] - s >= tap_min[p]) {
291 xqd[p] -= s;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000292 err2 =
293 get_pixel_proj_error(src8, width, height, src_stride, dat8,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000294 dat_stride, use_highbitdepth, flt0,
295 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700296 if (err2 > err) {
297 xqd[p] += s;
298 } else {
299 err = err2;
300 skip = 1;
301 // At the highest step size continue moving in the same direction
302 if (s == start_step) continue;
303 }
304 }
305 break;
306 } while (1);
307 if (skip) break;
308 do {
309 if (xqd[p] + s <= tap_max[p]) {
310 xqd[p] += s;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000311 err2 =
312 get_pixel_proj_error(src8, width, height, src_stride, dat8,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000313 dat_stride, use_highbitdepth, flt0,
314 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700315 if (err2 > err) {
316 xqd[p] -= s;
317 } else {
318 err = err2;
319 // At the highest step size continue moving in the same direction
320 if (s == start_step) continue;
321 }
322 }
323 break;
324 } while (1);
325 }
326 }
327#endif // USE_SGRPROJ_REFINEMENT_SEARCH
328 return err;
329}
330
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100331static void get_proj_subspace(const uint8_t *src8, int width, int height,
David Barkerbfbd8b32017-11-01 12:34:23 +0000332 int src_stride, const uint8_t *dat8,
333 int dat_stride, int use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000334 int32_t *flt0, int flt0_stride, int32_t *flt1,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700335 int flt1_stride, int *xq,
336 const sgr_params_type *params) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700337 int i, j;
338 double H[2][2] = { { 0, 0 }, { 0, 0 } };
339 double C[2] = { 0, 0 };
340 double Det;
341 double x[2];
342 const int size = width * height;
343
Jingning Han041c67b2017-04-14 21:39:26 -0700344 aom_clear_system_state();
345
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800346 // Default
347 xq[0] = 0;
348 xq[1] = 0;
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100349 if (!use_highbitdepth) {
David Barker3a0df182016-12-21 10:44:52 +0000350 const uint8_t *src = src8;
351 const uint8_t *dat = dat8;
352 for (i = 0; i < height; ++i) {
353 for (j = 0; j < width; ++j) {
354 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
355 const double s =
356 (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000357 const double f1 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400358 (params->r[0] > 0) ? (double)flt0[i * flt0_stride + j] - u : 0;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000359 const double f2 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400360 (params->r[1] > 0) ? (double)flt1[i * flt1_stride + j] - u : 0;
David Barker3a0df182016-12-21 10:44:52 +0000361 H[0][0] += f1 * f1;
362 H[1][1] += f2 * f2;
363 H[0][1] += f1 * f2;
364 C[0] += f1 * s;
365 C[1] += f2 * s;
366 }
367 }
368 } else {
369 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
370 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
371 for (i = 0; i < height; ++i) {
372 for (j = 0; j < width; ++j) {
373 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
374 const double s =
375 (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000376 const double f1 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400377 (params->r[0] > 0) ? (double)flt0[i * flt0_stride + j] - u : 0;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000378 const double f2 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400379 (params->r[1] > 0) ? (double)flt1[i * flt1_stride + j] - u : 0;
David Barker3a0df182016-12-21 10:44:52 +0000380 H[0][0] += f1 * f1;
381 H[1][1] += f2 * f2;
382 H[0][1] += f1 * f2;
383 C[0] += f1 * s;
384 C[1] += f2 * s;
385 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700386 }
387 }
388 H[0][0] /= size;
389 H[0][1] /= size;
390 H[1][1] /= size;
391 H[1][0] = H[0][1];
392 C[0] /= size;
393 C[1] /= size;
Urvang Joshi3715b882018-05-14 20:05:25 -0400394 if (params->r[0] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000395 // H matrix is now only the scalar H[1][1]
396 // C vector is now only the scalar C[1]
397 Det = H[1][1];
398 if (Det < 1e-8) return; // ill-posed, return default values
399 x[0] = 0;
400 x[1] = C[1] / Det;
401
402 xq[0] = 0;
403 xq[1] = (int)rint(x[1] * (1 << SGRPROJ_PRJ_BITS));
Urvang Joshi3715b882018-05-14 20:05:25 -0400404 } else if (params->r[1] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000405 // H matrix is now only the scalar H[0][0]
406 // C vector is now only the scalar C[0]
407 Det = H[0][0];
408 if (Det < 1e-8) return; // ill-posed, return default values
409 x[0] = C[0] / Det;
410 x[1] = 0;
411
412 xq[0] = (int)rint(x[0] * (1 << SGRPROJ_PRJ_BITS));
413 xq[1] = 0;
414 } else {
415 Det = (H[0][0] * H[1][1] - H[0][1] * H[1][0]);
416 if (Det < 1e-8) return; // ill-posed, return default values
417 x[0] = (H[1][1] * C[0] - H[0][1] * C[1]) / Det;
418 x[1] = (H[0][0] * C[1] - H[1][0] * C[0]) / Det;
419
420 xq[0] = (int)rint(x[0] * (1 << SGRPROJ_PRJ_BITS));
421 xq[1] = (int)rint(x[1] * (1 << SGRPROJ_PRJ_BITS));
422 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700423}
424
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000425void encode_xq(int *xq, int *xqd, const sgr_params_type *params) {
Urvang Joshi3715b882018-05-14 20:05:25 -0400426 if (params->r[0] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000427 xqd[0] = 0;
428 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xq[1], SGRPROJ_PRJ_MIN1,
429 SGRPROJ_PRJ_MAX1);
Urvang Joshi3715b882018-05-14 20:05:25 -0400430 } else if (params->r[1] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000431 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
Debargha Mukherjee114575d2018-02-23 11:18:37 -0800432 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0], SGRPROJ_PRJ_MIN1,
433 SGRPROJ_PRJ_MAX1);
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000434 } else {
435 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
436 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0] - xq[1], SGRPROJ_PRJ_MIN1,
437 SGRPROJ_PRJ_MAX1);
438 }
439}
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700440
David Barkerbfbd8b32017-11-01 12:34:23 +0000441// Apply the self-guided filter across an entire restoration unit.
Urvang Joshic079f7a2018-05-11 16:13:56 -0700442static void apply_sgr(int sgr_params_idx, const uint8_t *dat8, int width,
443 int height, int dat_stride, int use_highbd, int bit_depth,
444 int pu_width, int pu_height, int32_t *flt0, int32_t *flt1,
445 int flt_stride) {
David Barkerbfbd8b32017-11-01 12:34:23 +0000446 for (int i = 0; i < height; i += pu_height) {
447 const int h = AOMMIN(pu_height, height - i);
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000448 int32_t *flt0_row = flt0 + i * flt_stride;
David Barkerbfbd8b32017-11-01 12:34:23 +0000449 int32_t *flt1_row = flt1 + i * flt_stride;
David Barkerbfbd8b32017-11-01 12:34:23 +0000450 const uint8_t *dat8_row = dat8 + i * dat_stride;
451
452 // Iterate over the stripe in blocks of width pu_width
453 for (int j = 0; j < width; j += pu_width) {
454 const int w = AOMMIN(pu_width, width - j);
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000455 av1_selfguided_restoration(dat8_row + j, w, h, dat_stride, flt0_row + j,
Urvang Joshic079f7a2018-05-11 16:13:56 -0700456 flt1_row + j, flt_stride, sgr_params_idx,
457 bit_depth, use_highbd);
David Barkerbfbd8b32017-11-01 12:34:23 +0000458 }
459 }
460}
461
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100462static SgrprojInfo search_selfguided_restoration(
David Barkerbfbd8b32017-11-01 12:34:23 +0000463 const uint8_t *dat8, int width, int height, int dat_stride,
464 const uint8_t *src8, int src_stride, int use_highbitdepth, int bit_depth,
465 int pu_width, int pu_height, int32_t *rstbuf) {
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000466 int32_t *flt0 = rstbuf;
Urvang Joshi813186b2018-03-08 15:38:46 -0800467 int32_t *flt1 = flt0 + RESTORATION_UNITPELS_MAX;
David Barker506eb722017-03-08 13:35:49 +0000468 int ep, bestep = 0;
David Barkerbfbd8b32017-11-01 12:34:23 +0000469 int64_t besterr = -1;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700470 int exqd[2], bestxqd[2] = { 0, 0 };
David Barkerbfbd8b32017-11-01 12:34:23 +0000471 int flt_stride = ((width + 7) & ~7) + 8;
Debargha Mukherjee7a5587a2017-08-31 07:41:30 -0700472 assert(pu_width == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
473 pu_width == RESTORATION_PROC_UNIT_SIZE);
474 assert(pu_height == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
475 pu_height == RESTORATION_PROC_UNIT_SIZE);
David Barker3a0df182016-12-21 10:44:52 +0000476
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700477 for (ep = 0; ep < SGRPROJ_PARAMS; ep++) {
478 int exq[2];
Urvang Joshic079f7a2018-05-11 16:13:56 -0700479 apply_sgr(ep, dat8, width, height, dat_stride, use_highbitdepth, bit_depth,
480 pu_width, pu_height, flt0, flt1, flt_stride);
Debargha Mukherjee7ae7aea2017-05-04 15:17:17 -0700481 aom_clear_system_state();
Urvang Joshic079f7a2018-05-11 16:13:56 -0700482 const sgr_params_type *const params = &sgr_params[ep];
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000483 get_proj_subspace(src8, width, height, src_stride, dat8, dat_stride,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000484 use_highbitdepth, flt0, flt_stride, flt1, flt_stride, exq,
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000485 params);
Debargha Mukherjee7ae7aea2017-05-04 15:17:17 -0700486 aom_clear_system_state();
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000487 encode_xq(exq, exqd, params);
488 int64_t err = finer_search_pixel_proj_error(
489 src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000490 flt0, flt_stride, flt1, flt_stride, 2, exqd, params);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700491 if (besterr == -1 || err < besterr) {
492 bestep = ep;
493 besterr = err;
494 bestxqd[0] = exqd[0];
495 bestxqd[1] = exqd[1];
496 }
497 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100498
499 SgrprojInfo ret;
500 ret.ep = bestep;
501 ret.xqd[0] = bestxqd[0];
502 ret.xqd[1] = bestxqd[1];
503 return ret;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700504}
505
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700506static int count_sgrproj_bits(SgrprojInfo *sgrproj_info,
507 SgrprojInfo *ref_sgrproj_info) {
508 int bits = SGRPROJ_PARAMS_BITS;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000509 const sgr_params_type *params = &sgr_params[sgrproj_info->ep];
Urvang Joshi3715b882018-05-14 20:05:25 -0400510 if (params->r[0] > 0)
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000511 bits += aom_count_primitive_refsubexpfin(
512 SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
513 ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0,
514 sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0);
Urvang Joshi3715b882018-05-14 20:05:25 -0400515 if (params->r[1] > 0)
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000516 bits += aom_count_primitive_refsubexpfin(
517 SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
518 ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1,
519 sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700520 return bits;
521}
522
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100523static void search_sgrproj(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000524 const AV1PixelRect *tile, int rest_unit_idx,
525 void *priv) {
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100526 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
527 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100528
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100529 const MACROBLOCK *const x = rsc->x;
530 const AV1_COMMON *const cm = rsc->cm;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100531 const int highbd = cm->use_highbitdepth;
532 const int bit_depth = cm->bit_depth;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100533
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +0100534 uint8_t *dgd_start =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100535 rsc->dgd_buffer + limits->v_start * rsc->dgd_stride + limits->h_start;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100536 const uint8_t *src_start =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100537 rsc->src_buffer + limits->v_start * rsc->src_stride + limits->h_start;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100538
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000539 const int is_uv = rsc->plane > 0;
540 const int ss_x = is_uv && cm->subsampling_x;
541 const int ss_y = is_uv && cm->subsampling_y;
542 const int procunit_width = RESTORATION_PROC_UNIT_SIZE >> ss_x;
543 const int procunit_height = RESTORATION_PROC_UNIT_SIZE >> ss_y;
544
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100545 rusi->sgrproj = search_selfguided_restoration(
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +0100546 dgd_start, limits->h_end - limits->h_start,
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100547 limits->v_end - limits->v_start, rsc->dgd_stride, src_start,
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000548 rsc->src_stride, highbd, bit_depth, procunit_width, procunit_height,
549 cm->rst_tmpbuf);
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100550
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100551 RestorationUnitInfo rui;
552 rui.restoration_type = RESTORE_SGRPROJ;
553 rui.sgrproj_info = rusi->sgrproj;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100554
Urvang Joshi813186b2018-03-08 15:38:46 -0800555 rusi->sse[RESTORE_SGRPROJ] = try_restoration_unit(rsc, limits, tile, &rui);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100556
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100557 const int64_t bits_none = x->sgrproj_restore_cost[0];
558 const int64_t bits_sgr = x->sgrproj_restore_cost[1] +
559 (count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj)
560 << AV1_PROB_COST_SHIFT);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100561
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100562 double cost_none =
563 RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
564 double cost_sgr =
565 RDCOST_DBL(x->rdmult, bits_sgr >> 4, rusi->sse[RESTORE_SGRPROJ]);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100566
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100567 RestorationType rtype =
568 (cost_sgr < cost_none) ? RESTORE_SGRPROJ : RESTORE_NONE;
569 rusi->best_rtype[RESTORE_SGRPROJ - 1] = rtype;
Debargha Mukherjeee168a782017-08-31 12:30:10 -0700570
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100571 rsc->sse += rusi->sse[rtype];
572 rsc->bits += (cost_sgr < cost_none) ? bits_sgr : bits_none;
573 if (cost_sgr < cost_none) rsc->sgrproj = rusi->sgrproj;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700574}
575
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100576static double find_average(const uint8_t *src, int h_start, int h_end,
577 int v_start, int v_end, int stride) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700578 uint64_t sum = 0;
579 double avg = 0;
580 int i, j;
Jingning Han041c67b2017-04-14 21:39:26 -0700581 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582 for (i = v_start; i < v_end; i++)
583 for (j = h_start; j < h_end; j++) sum += src[i * stride + j];
584 avg = (double)sum / ((v_end - v_start) * (h_end - h_start));
585 return avg;
586}
587
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100588static void compute_stats(int wiener_win, const uint8_t *dgd,
589 const uint8_t *src, int h_start, int h_end,
590 int v_start, int v_end, int dgd_stride,
591 int src_stride, double *M, double *H) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700592 int i, j, k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800593 double Y[WIENER_WIN2];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700594 const int wiener_win2 = wiener_win * wiener_win;
595 const int wiener_halfwin = (wiener_win >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596 const double avg =
597 find_average(dgd, h_start, h_end, v_start, v_end, dgd_stride);
598
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700599 memset(M, 0, sizeof(*M) * wiener_win2);
600 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601 for (i = v_start; i < v_end; i++) {
602 for (j = h_start; j < h_end; j++) {
603 const double X = (double)src[i * src_stride + j] - avg;
604 int idx = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700605 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
606 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607 Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg;
608 idx++;
609 }
610 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700611 assert(idx == wiener_win2);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700612 for (k = 0; k < wiener_win2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700613 M[k] += Y[k] * X;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700614 H[k * wiener_win2 + k] += Y[k] * Y[k];
615 for (l = k + 1; l < wiener_win2; ++l) {
David Barker33f3bfd2017-01-06 15:34:50 +0000616 // H is a symmetric matrix, so we only need to fill out the upper
617 // triangle here. We can copy it down to the lower triangle outside
618 // the (i, j) loops.
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700619 H[k * wiener_win2 + l] += Y[k] * Y[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700620 }
621 }
622 }
623 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700624 for (k = 0; k < wiener_win2; ++k) {
625 for (l = k + 1; l < wiener_win2; ++l) {
626 H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
David Barker33f3bfd2017-01-06 15:34:50 +0000627 }
628 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700629}
630
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100631static double find_average_highbd(const uint16_t *src, int h_start, int h_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632 int v_start, int v_end, int stride) {
633 uint64_t sum = 0;
634 double avg = 0;
635 int i, j;
Jingning Han041c67b2017-04-14 21:39:26 -0700636 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637 for (i = v_start; i < v_end; i++)
638 for (j = h_start; j < h_end; j++) sum += src[i * stride + j];
639 avg = (double)sum / ((v_end - v_start) * (h_end - h_start));
640 return avg;
641}
642
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +0900643static AOM_FORCE_INLINE void compute_stats_highbd(
644 int wiener_win, const uint8_t *dgd8, const uint8_t *src8, int h_start,
645 int h_end, int v_start, int v_end, int dgd_stride, int src_stride,
646 double *M, double *H) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700647 int i, j, k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800648 double Y[WIENER_WIN2];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700649 const int wiener_win2 = wiener_win * wiener_win;
650 const int wiener_halfwin = (wiener_win >> 1);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100651 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
652 const uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700653 const double avg =
654 find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
655
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700656 memset(M, 0, sizeof(*M) * wiener_win2);
657 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700658 for (i = v_start; i < v_end; i++) {
659 for (j = h_start; j < h_end; j++) {
660 const double X = (double)src[i * src_stride + j] - avg;
661 int idx = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700662 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
663 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664 Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg;
665 idx++;
666 }
667 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700668 assert(idx == wiener_win2);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700669 for (k = 0; k < wiener_win2; ++k) {
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +0900670 double Yk = Y[k];
671 M[k] += Yk * X;
672 double *H2 = &H[k * wiener_win2];
673 H2[k] += Yk * Yk;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700674 for (l = k + 1; l < wiener_win2; ++l) {
David Barker33f3bfd2017-01-06 15:34:50 +0000675 // H is a symmetric matrix, so we only need to fill out the upper
676 // triangle here. We can copy it down to the lower triangle outside
677 // the (i, j) loops.
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +0900678 H2[l] += Yk * Y[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700679 }
680 }
681 }
682 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700683 for (k = 0; k < wiener_win2; ++k) {
684 for (l = k + 1; l < wiener_win2; ++l) {
685 H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
David Barker33f3bfd2017-01-06 15:34:50 +0000686 }
687 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700688}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700690static INLINE int wrap_index(int i, int wiener_win) {
691 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
692 return (i >= wiener_halfwin1 ? wiener_win - 1 - i : i);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693}
694
695// Fix vector b, update vector a
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700696static void update_a_sep_sym(int wiener_win, double **Mc, double **Hc,
697 double *a, double *b) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700698 int i, j;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800699 double S[WIENER_WIN];
Debargha Mukherjee6ae588f2017-04-14 00:40:02 -0700700 double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700701 const int wiener_win2 = wiener_win * wiener_win;
702 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703 memset(A, 0, sizeof(A));
704 memset(B, 0, sizeof(B));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700705 for (i = 0; i < wiener_win; i++) {
706 for (j = 0; j < wiener_win; ++j) {
707 const int jj = wrap_index(j, wiener_win);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700708 A[jj] += Mc[i][j] * b[i];
709 }
710 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700711 for (i = 0; i < wiener_win; i++) {
712 for (j = 0; j < wiener_win; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700713 int k, l;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700714 for (k = 0; k < wiener_win; ++k)
715 for (l = 0; l < wiener_win; ++l) {
716 const int kk = wrap_index(k, wiener_win);
717 const int ll = wrap_index(l, wiener_win);
718 B[ll * wiener_halfwin1 + kk] +=
719 Hc[j * wiener_win + i][k * wiener_win2 + l] * b[i] * b[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700720 }
721 }
722 }
723 // Normalization enforcement in the system of equations itself
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700724 for (i = 0; i < wiener_halfwin1 - 1; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700725 A[i] -=
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700726 A[wiener_halfwin1 - 1] * 2 +
727 B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
728 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
729 for (i = 0; i < wiener_halfwin1 - 1; ++i)
730 for (j = 0; j < wiener_halfwin1 - 1; ++j)
731 B[i * wiener_halfwin1 + j] -=
732 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
733 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
734 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
735 (wiener_halfwin1 - 1)]);
736 if (linsolve(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
737 S[wiener_halfwin1 - 1] = 1.0;
738 for (i = wiener_halfwin1; i < wiener_win; ++i) {
739 S[i] = S[wiener_win - 1 - i];
740 S[wiener_halfwin1 - 1] -= 2 * S[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700742 memcpy(a, S, wiener_win * sizeof(*a));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743 }
744}
745
746// Fix vector a, update vector b
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700747static void update_b_sep_sym(int wiener_win, double **Mc, double **Hc,
748 double *a, double *b) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700749 int i, j;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800750 double S[WIENER_WIN];
Debargha Mukherjee6ae588f2017-04-14 00:40:02 -0700751 double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700752 const int wiener_win2 = wiener_win * wiener_win;
753 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700754 memset(A, 0, sizeof(A));
755 memset(B, 0, sizeof(B));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700756 for (i = 0; i < wiener_win; i++) {
757 const int ii = wrap_index(i, wiener_win);
758 for (j = 0; j < wiener_win; j++) A[ii] += Mc[i][j] * a[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700759 }
760
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700761 for (i = 0; i < wiener_win; i++) {
762 for (j = 0; j < wiener_win; j++) {
763 const int ii = wrap_index(i, wiener_win);
764 const int jj = wrap_index(j, wiener_win);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765 int k, l;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700766 for (k = 0; k < wiener_win; ++k)
767 for (l = 0; l < wiener_win; ++l)
768 B[jj * wiener_halfwin1 + ii] +=
769 Hc[i * wiener_win + j][k * wiener_win2 + l] * a[k] * a[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700770 }
771 }
772 // Normalization enforcement in the system of equations itself
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700773 for (i = 0; i < wiener_halfwin1 - 1; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774 A[i] -=
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700775 A[wiener_halfwin1 - 1] * 2 +
776 B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
777 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
778 for (i = 0; i < wiener_halfwin1 - 1; ++i)
779 for (j = 0; j < wiener_halfwin1 - 1; ++j)
780 B[i * wiener_halfwin1 + j] -=
781 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
782 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
783 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
784 (wiener_halfwin1 - 1)]);
785 if (linsolve(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
786 S[wiener_halfwin1 - 1] = 1.0;
787 for (i = wiener_halfwin1; i < wiener_win; ++i) {
788 S[i] = S[wiener_win - 1 - i];
789 S[wiener_halfwin1 - 1] -= 2 * S[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700791 memcpy(b, S, wiener_win * sizeof(*b));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700792 }
793}
794
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700795static int wiener_decompose_sep_sym(int wiener_win, double *M, double *H,
796 double *a, double *b) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700797 static const int init_filt[WIENER_WIN] = {
798 WIENER_FILT_TAP0_MIDV, WIENER_FILT_TAP1_MIDV, WIENER_FILT_TAP2_MIDV,
799 WIENER_FILT_TAP3_MIDV, WIENER_FILT_TAP2_MIDV, WIENER_FILT_TAP1_MIDV,
800 WIENER_FILT_TAP0_MIDV,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700801 };
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800802 double *Hc[WIENER_WIN2];
803 double *Mc[WIENER_WIN];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700804 int i, j, iter;
805 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
806 const int wiener_win2 = wiener_win * wiener_win;
807 for (i = 0; i < wiener_win; i++) {
808 a[i] = b[i] = (double)init_filt[i + plane_off] / WIENER_FILT_STEP;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700809 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700810 for (i = 0; i < wiener_win; i++) {
811 Mc[i] = M + i * wiener_win;
812 for (j = 0; j < wiener_win; j++) {
813 Hc[i * wiener_win + j] =
814 H + i * wiener_win * wiener_win2 + j * wiener_win;
815 }
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700816 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817
818 iter = 1;
Debargha Mukherjee1b3dbf02017-03-13 14:47:21 -0700819 while (iter < NUM_WIENER_ITERS) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700820 update_a_sep_sym(wiener_win, Mc, Hc, a, b);
821 update_b_sep_sym(wiener_win, Mc, Hc, a, b);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700822 iter++;
823 }
824 return 1;
825}
826
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800827// Computes the function x'*H*x - x'*M for the learned 2D filter x, and compares
Yaowu Xuc27fc142016-08-22 16:08:15 -0700828// against identity filters; Final score is defined as the difference between
829// the function values
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700830static double compute_score(int wiener_win, double *M, double *H,
831 InterpKernel vfilt, InterpKernel hfilt) {
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800832 double ab[WIENER_WIN * WIENER_WIN];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700833 int i, k, l;
834 double P = 0, Q = 0;
835 double iP = 0, iQ = 0;
836 double Score, iScore;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800837 double a[WIENER_WIN], b[WIENER_WIN];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700838 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
839 const int wiener_win2 = wiener_win * wiener_win;
Jingning Han041c67b2017-04-14 21:39:26 -0700840
841 aom_clear_system_state();
842
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800843 a[WIENER_HALFWIN] = b[WIENER_HALFWIN] = 1.0;
844 for (i = 0; i < WIENER_HALFWIN; ++i) {
845 a[i] = a[WIENER_WIN - i - 1] = (double)vfilt[i] / WIENER_FILT_STEP;
846 b[i] = b[WIENER_WIN - i - 1] = (double)hfilt[i] / WIENER_FILT_STEP;
847 a[WIENER_HALFWIN] -= 2 * a[i];
848 b[WIENER_HALFWIN] -= 2 * b[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700850 memset(ab, 0, sizeof(ab));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700851 for (k = 0; k < wiener_win; ++k) {
852 for (l = 0; l < wiener_win; ++l)
853 ab[k * wiener_win + l] = a[l + plane_off] * b[k + plane_off];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700854 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700855 for (k = 0; k < wiener_win2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700856 P += ab[k] * M[k];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700857 for (l = 0; l < wiener_win2; ++l)
858 Q += ab[k] * H[k * wiener_win2 + l] * ab[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859 }
860 Score = Q - 2 * P;
861
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700862 iP = M[wiener_win2 >> 1];
863 iQ = H[(wiener_win2 >> 1) * wiener_win2 + (wiener_win2 >> 1)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700864 iScore = iQ - 2 * iP;
865
866 return Score - iScore;
867}
868
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700869static void quantize_sym_filter(int wiener_win, double *f, InterpKernel fi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870 int i;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700871 const int wiener_halfwin = (wiener_win >> 1);
872 for (i = 0; i < wiener_halfwin; ++i) {
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800873 fi[i] = RINT(f[i] * WIENER_FILT_STEP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874 }
875 // Specialize for 7-tap filter
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700876 if (wiener_win == WIENER_WIN) {
877 fi[0] = CLIP(fi[0], WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP0_MAXV);
878 fi[1] = CLIP(fi[1], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
879 fi[2] = CLIP(fi[2], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
880 } else {
881 fi[2] = CLIP(fi[1], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
882 fi[1] = CLIP(fi[0], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
883 fi[0] = 0;
884 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800885 // Satisfy filter constraints
886 fi[WIENER_WIN - 1] = fi[0];
887 fi[WIENER_WIN - 2] = fi[1];
888 fi[WIENER_WIN - 3] = fi[2];
David Barker1e8e6b92017-01-13 13:45:51 +0000889 // The central element has an implicit +WIENER_FILT_STEP
890 fi[3] = -2 * (fi[0] + fi[1] + fi[2]);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800891}
892
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700893static int count_wiener_bits(int wiener_win, WienerInfo *wiener_info,
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700894 WienerInfo *ref_wiener_info) {
895 int bits = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700896 if (wiener_win == WIENER_WIN)
897 bits += aom_count_primitive_refsubexpfin(
898 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
899 WIENER_FILT_TAP0_SUBEXP_K,
900 ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV,
901 wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700902 bits += aom_count_primitive_refsubexpfin(
903 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
904 WIENER_FILT_TAP1_SUBEXP_K,
905 ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV,
906 wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV);
907 bits += aom_count_primitive_refsubexpfin(
908 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
909 WIENER_FILT_TAP2_SUBEXP_K,
910 ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV,
911 wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700912 if (wiener_win == WIENER_WIN)
913 bits += aom_count_primitive_refsubexpfin(
914 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
915 WIENER_FILT_TAP0_SUBEXP_K,
916 ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV,
917 wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700918 bits += aom_count_primitive_refsubexpfin(
919 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
920 WIENER_FILT_TAP1_SUBEXP_K,
921 ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV,
922 wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV);
923 bits += aom_count_primitive_refsubexpfin(
924 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
925 WIENER_FILT_TAP2_SUBEXP_K,
926 ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV,
927 wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV);
928 return bits;
929}
930
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700931#define USE_WIENER_REFINEMENT_SEARCH 1
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000932static int64_t finer_tile_search_wiener(const RestSearchCtxt *rsc,
933 const RestorationTileLimits *limits,
934 const AV1PixelRect *tile,
935 RestorationUnitInfo *rui,
936 int wiener_win) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700937 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
Urvang Joshi813186b2018-03-08 15:38:46 -0800938 int64_t err = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700939#if USE_WIENER_REFINEMENT_SEARCH
940 int64_t err2;
941 int tap_min[] = { WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP1_MINV,
942 WIENER_FILT_TAP2_MINV };
943 int tap_max[] = { WIENER_FILT_TAP0_MAXV, WIENER_FILT_TAP1_MAXV,
944 WIENER_FILT_TAP2_MAXV };
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100945
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100946 WienerInfo *plane_wiener = &rui->wiener_info;
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100947
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700948 // printf("err pre = %"PRId64"\n", err);
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000949 const int start_step = 4;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700950 for (int s = start_step; s >= 1; s >>= 1) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700951 for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700952 int skip = 0;
953 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100954 if (plane_wiener->hfilter[p] - s >= tap_min[p]) {
955 plane_wiener->hfilter[p] -= s;
956 plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
957 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -0800958 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700959 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100960 plane_wiener->hfilter[p] += s;
961 plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
962 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700963 } else {
964 err = err2;
965 skip = 1;
966 // At the highest step size continue moving in the same direction
967 if (s == start_step) continue;
968 }
969 }
970 break;
971 } while (1);
972 if (skip) break;
973 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100974 if (plane_wiener->hfilter[p] + s <= tap_max[p]) {
975 plane_wiener->hfilter[p] += s;
976 plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
977 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -0800978 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700979 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100980 plane_wiener->hfilter[p] -= s;
981 plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
982 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700983 } else {
984 err = err2;
985 // At the highest step size continue moving in the same direction
986 if (s == start_step) continue;
987 }
988 }
989 break;
990 } while (1);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700991 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700992 for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700993 int skip = 0;
994 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100995 if (plane_wiener->vfilter[p] - s >= tap_min[p]) {
996 plane_wiener->vfilter[p] -= s;
997 plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
998 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -0800999 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001000 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001001 plane_wiener->vfilter[p] += s;
1002 plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
1003 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001004 } else {
1005 err = err2;
1006 skip = 1;
1007 // At the highest step size continue moving in the same direction
1008 if (s == start_step) continue;
1009 }
1010 }
1011 break;
1012 } while (1);
1013 if (skip) break;
1014 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001015 if (plane_wiener->vfilter[p] + s <= tap_max[p]) {
1016 plane_wiener->vfilter[p] += s;
1017 plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
1018 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -08001019 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001020 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001021 plane_wiener->vfilter[p] -= s;
1022 plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
1023 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001024 } else {
1025 err = err2;
1026 // At the highest step size continue moving in the same direction
1027 if (s == start_step) continue;
1028 }
1029 }
1030 break;
1031 } while (1);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -07001032 }
1033 }
1034// printf("err post = %"PRId64"\n", err);
1035#endif // USE_WIENER_REFINEMENT_SEARCH
1036 return err;
1037}
1038
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001039static void search_wiener(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001040 const AV1PixelRect *tile_rect, int rest_unit_idx,
1041 void *priv) {
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001042 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1043 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1044
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001045 const int wiener_win =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001046 (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001047
1048 double M[WIENER_WIN2];
1049 double H[WIENER_WIN2 * WIENER_WIN2];
1050 double vfilterd[WIENER_WIN], hfilterd[WIENER_WIN];
1051
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001052 const AV1_COMMON *const cm = rsc->cm;
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +09001053 if (cm->use_highbitdepth) {
1054 if (rsc->plane == AOM_PLANE_Y) {
1055 compute_stats_highbd(WIENER_WIN, rsc->dgd_buffer, rsc->src_buffer,
1056 limits->h_start, limits->h_end, limits->v_start,
1057 limits->v_end, rsc->dgd_stride, rsc->src_stride, M,
1058 H);
1059 } else {
1060 compute_stats_highbd(WIENER_WIN_CHROMA, rsc->dgd_buffer, rsc->src_buffer,
1061 limits->h_start, limits->h_end, limits->v_start,
1062 limits->v_end, rsc->dgd_stride, rsc->src_stride, M,
1063 H);
1064 }
1065 } else {
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001066 compute_stats(wiener_win, rsc->dgd_buffer, rsc->src_buffer, limits->h_start,
1067 limits->h_end, limits->v_start, limits->v_end,
1068 rsc->dgd_stride, rsc->src_stride, M, H);
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +09001069 }
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001070
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001071 const MACROBLOCK *const x = rsc->x;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001072 const int64_t bits_none = x->wiener_restore_cost[0];
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001073
1074 if (!wiener_decompose_sep_sym(wiener_win, M, H, vfilterd, hfilterd)) {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001075 rsc->bits += bits_none;
1076 rsc->sse += rusi->sse[RESTORE_NONE];
1077 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1078 rusi->sse[RESTORE_WIENER] = INT64_MAX;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001079 return;
1080 }
1081
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001082 RestorationUnitInfo rui;
1083 memset(&rui, 0, sizeof(rui));
1084 rui.restoration_type = RESTORE_WIENER;
1085 quantize_sym_filter(wiener_win, vfilterd, rui.wiener_info.vfilter);
1086 quantize_sym_filter(wiener_win, hfilterd, rui.wiener_info.hfilter);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001087
1088 // Filter score computes the value of the function x'*A*x - x'*b for the
1089 // learned filter and compares it against identity filer. If there is no
1090 // reduction in the function, the filter is reverted back to identity
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001091 if (compute_score(wiener_win, M, H, rui.wiener_info.vfilter,
1092 rui.wiener_info.hfilter) > 0) {
1093 rsc->bits += bits_none;
1094 rsc->sse += rusi->sse[RESTORE_NONE];
1095 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1096 rusi->sse[RESTORE_WIENER] = INT64_MAX;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001097 return;
1098 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001099
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001100 aom_clear_system_state();
1101
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001102 rusi->sse[RESTORE_WIENER] =
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001103 finer_tile_search_wiener(rsc, limits, tile_rect, &rui, wiener_win);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001104 rusi->wiener = rui.wiener_info;
1105
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001106 if (wiener_win != WIENER_WIN) {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001107 assert(rui.wiener_info.vfilter[0] == 0 &&
1108 rui.wiener_info.vfilter[WIENER_WIN - 1] == 0);
1109 assert(rui.wiener_info.hfilter[0] == 0 &&
1110 rui.wiener_info.hfilter[WIENER_WIN - 1] == 0);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001111 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001112
1113 const int64_t bits_wiener =
1114 x->wiener_restore_cost[1] +
1115 (count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener)
1116 << AV1_PROB_COST_SHIFT);
1117
1118 double cost_none =
1119 RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
1120 double cost_wiener =
1121 RDCOST_DBL(x->rdmult, bits_wiener >> 4, rusi->sse[RESTORE_WIENER]);
1122
1123 RestorationType rtype =
1124 (cost_wiener < cost_none) ? RESTORE_WIENER : RESTORE_NONE;
1125 rusi->best_rtype[RESTORE_WIENER - 1] = rtype;
1126
1127 rsc->sse += rusi->sse[rtype];
1128 rsc->bits += (cost_wiener < cost_none) ? bits_wiener : bits_none;
1129 if (cost_wiener < cost_none) rsc->wiener = rusi->wiener;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001130}
1131
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001132static void search_norestore(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001133 const AV1PixelRect *tile_rect, int rest_unit_idx,
1134 void *priv) {
1135 (void)tile_rect;
1136
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001137 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1138 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1139
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001140 const int highbd = rsc->cm->use_highbitdepth;
Urvang Joshi813186b2018-03-08 15:38:46 -08001141 rusi->sse[RESTORE_NONE] = sse_restoration_unit(
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001142 limits, rsc->src, rsc->cm->frame_to_show, rsc->plane, highbd);
Rupert Swarbrick64b8bbd2017-10-16 15:53:07 +01001143
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001144 rsc->sse += rusi->sse[RESTORE_NONE];
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001145}
1146
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001147static void search_switchable(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001148 const AV1PixelRect *tile_rect, int rest_unit_idx,
1149 void *priv) {
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +01001150 (void)limits;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001151 (void)tile_rect;
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001152 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1153 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001154
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001155 const MACROBLOCK *const x = rsc->x;
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001156
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001157 const int wiener_win =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001158 (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001159
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001160 double best_cost = 0;
1161 int64_t best_bits = 0;
1162 RestorationType best_rtype = RESTORE_NONE;
1163
1164 for (RestorationType r = 0; r < RESTORE_SWITCHABLE_TYPES; ++r) {
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001165 // Check for the condition that wiener or sgrproj search could not
1166 // find a solution or the solution was worse than RESTORE_NONE.
1167 // In either case the best_rtype will be set as RESTORE_NONE. These
1168 // should be skipped from the test below.
1169 if (r > RESTORE_NONE) {
1170 if (rusi->best_rtype[r - 1] == RESTORE_NONE) continue;
1171 }
1172
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001173 const int64_t sse = rusi->sse[r];
1174 int64_t coeff_pcost = 0;
1175 switch (r) {
1176 case RESTORE_NONE: coeff_pcost = 0; break;
1177 case RESTORE_WIENER:
1178 coeff_pcost =
1179 count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener);
1180 break;
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001181 case RESTORE_SGRPROJ:
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001182 coeff_pcost = count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj);
1183 break;
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001184 default: assert(0); break;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001185 }
1186 const int64_t coeff_bits = coeff_pcost << AV1_PROB_COST_SHIFT;
1187 const int64_t bits = x->switchable_restore_cost[r] + coeff_bits;
1188 double cost = RDCOST_DBL(x->rdmult, bits >> 4, sse);
1189 if (r == 0 || cost < best_cost) {
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001190 best_cost = cost;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001191 best_bits = bits;
1192 best_rtype = r;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001193 }
1194 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001195
1196 rusi->best_rtype[RESTORE_SWITCHABLE - 1] = best_rtype;
1197
1198 rsc->sse += rusi->sse[best_rtype];
1199 rsc->bits += best_bits;
1200 if (best_rtype == RESTORE_WIENER) rsc->wiener = rusi->wiener;
1201 if (best_rtype == RESTORE_SGRPROJ) rsc->sgrproj = rusi->sgrproj;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001202}
1203
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001204static void copy_unit_info(RestorationType frame_rtype,
1205 const RestUnitSearchInfo *rusi,
1206 RestorationUnitInfo *rui) {
1207 assert(frame_rtype > 0);
1208 rui->restoration_type = rusi->best_rtype[frame_rtype - 1];
1209 if (rui->restoration_type == RESTORE_WIENER)
1210 rui->wiener_info = rusi->wiener;
1211 else
1212 rui->sgrproj_info = rusi->sgrproj;
1213}
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001214
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001215static double search_rest_type(RestSearchCtxt *rsc, RestorationType rtype) {
1216 static const rest_unit_visitor_t funs[RESTORE_TYPES] = {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001217 search_norestore, search_wiener, search_sgrproj, search_switchable
1218 };
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001219
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001220 reset_rsc(rsc);
1221 av1_foreach_rest_unit_in_frame(rsc->cm, rsc->plane, rsc_on_tile, funs[rtype],
1222 rsc);
1223 return RDCOST_DBL(rsc->x->rdmult, rsc->bits >> 4, rsc->sse);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001224}
1225
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001226static int rest_tiles_in_plane(const AV1_COMMON *cm, int plane) {
1227 const RestorationInfo *rsi = &cm->rst_info[plane];
David Barkeree0ae202018-03-28 16:58:57 +01001228 return rsi->units_per_tile;
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001229}
1230
Rupert Swarbrick146a0602017-10-17 16:52:20 +01001231void av1_pick_filter_restoration(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001232 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001233 const int num_planes = av1_num_planes(cm);
Urvang Joshi14072aa2018-03-21 17:43:36 -07001234 assert(!cm->all_lossless);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001235
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001236 int ntiles[2];
1237 for (int is_uv = 0; is_uv < 2; ++is_uv)
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001238 ntiles[is_uv] = rest_tiles_in_plane(cm, is_uv);
Debargha Mukherjeed48f5732017-05-19 14:58:07 -07001239
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001240 assert(ntiles[1] <= ntiles[0]);
1241 RestUnitSearchInfo *rusi =
Yaowu Xu43e30f42017-11-27 14:26:00 -08001242 (RestUnitSearchInfo *)aom_memalign(16, sizeof(*rusi) * ntiles[0]);
Debargha Mukherjeed48f5732017-05-19 14:58:07 -07001243
Imdad Sardharwallab08544d2018-01-23 12:45:31 +00001244 // If the restoration unit dimensions are not multiples of
1245 // rsi->restoration_unit_size then some elements of the rusi array may be
1246 // left uninitialised when we reach copy_unit_info(...). This is not a
1247 // problem, as these elements are ignored later, but in order to quiet
1248 // Valgrind's warnings we initialise the array below.
1249 memset(rusi, 0, sizeof(*rusi) * ntiles[0]);
1250
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001251 RestSearchCtxt rsc;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001252 const int plane_start = AOM_PLANE_Y;
1253 const int plane_end = num_planes > 1 ? AOM_PLANE_V : AOM_PLANE_Y;
1254 for (int plane = plane_start; plane <= plane_end; ++plane) {
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001255 init_rsc(src, &cpi->common, &cpi->td.mb, plane, rusi, &cpi->trial_frame_rst,
1256 &rsc);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001257
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001258 const int plane_ntiles = ntiles[plane > 0];
1259 const RestorationType num_rtypes =
1260 (plane_ntiles > 1) ? RESTORE_TYPES : RESTORE_SWITCHABLE_TYPES;
1261
1262 double best_cost = 0;
1263 RestorationType best_rtype = RESTORE_NONE;
1264
Rupert Swarbrick5b401362017-10-31 10:56:27 +00001265 const int highbd = rsc.cm->use_highbitdepth;
Rupert Swarbrick5b401362017-10-31 10:56:27 +00001266 extend_frame(rsc.dgd_buffer, rsc.plane_width, rsc.plane_height,
1267 rsc.dgd_stride, RESTORATION_BORDER, RESTORATION_BORDER,
1268 highbd);
1269
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001270 for (RestorationType r = 0; r < num_rtypes; ++r) {
1271 if ((force_restore_type != RESTORE_TYPES) && (r != RESTORE_NONE) &&
1272 (r != force_restore_type))
1273 continue;
1274
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001275 double cost = search_rest_type(&rsc, r);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001276
1277 if (r == 0 || cost < best_cost) {
1278 best_cost = cost;
1279 best_rtype = r;
1280 }
1281 }
1282
1283 cm->rst_info[plane].frame_restoration_type = best_rtype;
1284 if (force_restore_type != RESTORE_TYPES)
1285 assert(best_rtype == force_restore_type || best_rtype == RESTORE_NONE);
1286
1287 if (best_rtype != RESTORE_NONE) {
1288 for (int u = 0; u < plane_ntiles; ++u) {
1289 copy_unit_info(best_rtype, &rusi[u], &cm->rst_info[plane].unit_info[u]);
1290 }
1291 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001292 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001293
1294 aom_free(rusi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001295}