blob: 31669d4aa03fe03bced20605969f8889bf43ce63 [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;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700160
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000161 av1_loop_restoration_filter_unit(
Debargha Mukherjee5105f7a2018-01-29 16:05:54 -0800162 limits, rui, &rsi->boundaries, &rlbs, tile_rect, rsc->tile_stripe0,
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000163 is_uv && cm->subsampling_x, is_uv && cm->subsampling_y, highbd, bit_depth,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000164 fts->buffers[plane], fts->strides[is_uv], rsc->dst->buffers[plane],
165 rsc->dst->strides[is_uv], cm->rst_tmpbuf);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700166
Urvang Joshi813186b2018-03-08 15:38:46 -0800167 return sse_restoration_unit(limits, rsc->src, rsc->dst, plane, highbd);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700168}
169
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100170static int64_t get_pixel_proj_error(const uint8_t *src8, int width, int height,
171 int src_stride, const uint8_t *dat8,
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100172 int dat_stride, int use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000173 int32_t *flt0, int flt0_stride,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700174 int32_t *flt1, int flt1_stride, int *xqd,
175 const sgr_params_type *params) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700176 int i, j;
177 int64_t err = 0;
178 int xq[2];
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000179 decode_xq(xqd, xq, params);
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100180 if (!use_highbitdepth) {
David Barker3a0df182016-12-21 10:44:52 +0000181 const uint8_t *src = src8;
182 const uint8_t *dat = dat8;
183 for (i = 0; i < height; ++i) {
184 for (j = 0; j < width; ++j) {
185 const int32_t u =
186 (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000187 int32_t v = u << SGRPROJ_PRJ_BITS;
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000188 if (params->r0 > 0) v += xq[0] * (flt0[i * flt0_stride + j] - u);
189 if (params->r1 > 0) v += xq[1] * (flt1[i * flt1_stride + j] - u);
David Barker3a0df182016-12-21 10:44:52 +0000190 const int32_t e =
191 ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) -
192 src[i * src_stride + j];
193 err += e * e;
194 }
195 }
196 } else {
197 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
198 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
199 for (i = 0; i < height; ++i) {
200 for (j = 0; j < width; ++j) {
201 const int32_t u =
202 (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000203 int32_t v = u << SGRPROJ_PRJ_BITS;
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000204 if (params->r0 > 0) v += xq[0] * (flt0[i * flt0_stride + j] - u);
205 if (params->r1 > 0) v += xq[1] * (flt1[i * flt1_stride + j] - u);
David Barker3a0df182016-12-21 10:44:52 +0000206 const int32_t e =
207 ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) -
208 src[i * src_stride + j];
209 err += e * e;
210 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700211 }
212 }
213 return err;
214}
215
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700216#define USE_SGRPROJ_REFINEMENT_SEARCH 1
217static int64_t finer_search_pixel_proj_error(
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100218 const uint8_t *src8, int width, int height, int src_stride,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000219 const uint8_t *dat8, int dat_stride, int use_highbitdepth, int32_t *flt0,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700220 int flt0_stride, int32_t *flt1, int flt1_stride, int start_step, int *xqd,
221 const sgr_params_type *params) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000222 int64_t err = get_pixel_proj_error(
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000223 src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth, flt0,
224 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700225 (void)start_step;
226#if USE_SGRPROJ_REFINEMENT_SEARCH
227 int64_t err2;
228 int tap_min[] = { SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MIN1 };
229 int tap_max[] = { SGRPROJ_PRJ_MAX0, SGRPROJ_PRJ_MAX1 };
230 for (int s = start_step; s >= 1; s >>= 1) {
231 for (int p = 0; p < 2; ++p) {
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000232 if ((params->r0 == 0 && p == 0) || (params->r1 == 0 && p == 1)) continue;
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700233 int skip = 0;
234 do {
235 if (xqd[p] - s >= tap_min[p]) {
236 xqd[p] -= s;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000237 err2 =
238 get_pixel_proj_error(src8, width, height, src_stride, dat8,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000239 dat_stride, use_highbitdepth, flt0,
240 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700241 if (err2 > err) {
242 xqd[p] += s;
243 } else {
244 err = err2;
245 skip = 1;
246 // At the highest step size continue moving in the same direction
247 if (s == start_step) continue;
248 }
249 }
250 break;
251 } while (1);
252 if (skip) break;
253 do {
254 if (xqd[p] + s <= tap_max[p]) {
255 xqd[p] += s;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000256 err2 =
257 get_pixel_proj_error(src8, width, height, src_stride, dat8,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000258 dat_stride, use_highbitdepth, flt0,
259 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700260 if (err2 > err) {
261 xqd[p] -= s;
262 } else {
263 err = err2;
264 // At the highest step size continue moving in the same direction
265 if (s == start_step) continue;
266 }
267 }
268 break;
269 } while (1);
270 }
271 }
272#endif // USE_SGRPROJ_REFINEMENT_SEARCH
273 return err;
274}
275
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100276static void get_proj_subspace(const uint8_t *src8, int width, int height,
David Barkerbfbd8b32017-11-01 12:34:23 +0000277 int src_stride, const uint8_t *dat8,
278 int dat_stride, int use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000279 int32_t *flt0, int flt0_stride, int32_t *flt1,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700280 int flt1_stride, int *xq,
281 const sgr_params_type *params) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700282 int i, j;
283 double H[2][2] = { { 0, 0 }, { 0, 0 } };
284 double C[2] = { 0, 0 };
285 double Det;
286 double x[2];
287 const int size = width * height;
288
Jingning Han041c67b2017-04-14 21:39:26 -0700289 aom_clear_system_state();
290
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800291 // Default
292 xq[0] = 0;
293 xq[1] = 0;
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100294 if (!use_highbitdepth) {
David Barker3a0df182016-12-21 10:44:52 +0000295 const uint8_t *src = src8;
296 const uint8_t *dat = dat8;
297 for (i = 0; i < height; ++i) {
298 for (j = 0; j < width; ++j) {
299 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
300 const double s =
301 (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000302 const double f1 =
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000303 (params->r0 > 0) ? (double)flt0[i * flt0_stride + j] - u : 0;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000304 const double f2 =
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000305 (params->r1 > 0) ? (double)flt1[i * flt1_stride + j] - u : 0;
David Barker3a0df182016-12-21 10:44:52 +0000306 H[0][0] += f1 * f1;
307 H[1][1] += f2 * f2;
308 H[0][1] += f1 * f2;
309 C[0] += f1 * s;
310 C[1] += f2 * s;
311 }
312 }
313 } else {
314 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
315 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
316 for (i = 0; i < height; ++i) {
317 for (j = 0; j < width; ++j) {
318 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
319 const double s =
320 (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000321 const double f1 =
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000322 (params->r0 > 0) ? (double)flt0[i * flt0_stride + j] - u : 0;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000323 const double f2 =
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000324 (params->r1 > 0) ? (double)flt1[i * flt1_stride + j] - u : 0;
David Barker3a0df182016-12-21 10:44:52 +0000325 H[0][0] += f1 * f1;
326 H[1][1] += f2 * f2;
327 H[0][1] += f1 * f2;
328 C[0] += f1 * s;
329 C[1] += f2 * s;
330 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700331 }
332 }
333 H[0][0] /= size;
334 H[0][1] /= size;
335 H[1][1] /= size;
336 H[1][0] = H[0][1];
337 C[0] /= size;
338 C[1] /= size;
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000339 if (params->r0 == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000340 // H matrix is now only the scalar H[1][1]
341 // C vector is now only the scalar C[1]
342 Det = H[1][1];
343 if (Det < 1e-8) return; // ill-posed, return default values
344 x[0] = 0;
345 x[1] = C[1] / Det;
346
347 xq[0] = 0;
348 xq[1] = (int)rint(x[1] * (1 << SGRPROJ_PRJ_BITS));
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000349 } else if (params->r1 == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000350 // H matrix is now only the scalar H[0][0]
351 // C vector is now only the scalar C[0]
352 Det = H[0][0];
353 if (Det < 1e-8) return; // ill-posed, return default values
354 x[0] = C[0] / Det;
355 x[1] = 0;
356
357 xq[0] = (int)rint(x[0] * (1 << SGRPROJ_PRJ_BITS));
358 xq[1] = 0;
359 } else {
360 Det = (H[0][0] * H[1][1] - H[0][1] * H[1][0]);
361 if (Det < 1e-8) return; // ill-posed, return default values
362 x[0] = (H[1][1] * C[0] - H[0][1] * C[1]) / Det;
363 x[1] = (H[0][0] * C[1] - H[1][0] * C[0]) / Det;
364
365 xq[0] = (int)rint(x[0] * (1 << SGRPROJ_PRJ_BITS));
366 xq[1] = (int)rint(x[1] * (1 << SGRPROJ_PRJ_BITS));
367 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700368}
369
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000370void encode_xq(int *xq, int *xqd, const sgr_params_type *params) {
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000371 if (params->r0 == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000372 xqd[0] = 0;
373 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xq[1], SGRPROJ_PRJ_MIN1,
374 SGRPROJ_PRJ_MAX1);
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000375 } else if (params->r1 == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000376 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
Debargha Mukherjee114575d2018-02-23 11:18:37 -0800377 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0], SGRPROJ_PRJ_MIN1,
378 SGRPROJ_PRJ_MAX1);
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000379 } else {
380 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
381 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0] - xq[1], SGRPROJ_PRJ_MIN1,
382 SGRPROJ_PRJ_MAX1);
383 }
384}
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700385
David Barkerbfbd8b32017-11-01 12:34:23 +0000386// Apply the self-guided filter across an entire restoration unit.
387static void apply_sgr(const sgr_params_type *params, const uint8_t *dat8,
388 int width, int height, int dat_stride, int use_highbd,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000389 int bit_depth, int pu_width, int pu_height, int32_t *flt0,
390 int32_t *flt1, int flt_stride) {
David Barkerbfbd8b32017-11-01 12:34:23 +0000391 for (int i = 0; i < height; i += pu_height) {
392 const int h = AOMMIN(pu_height, height - i);
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000393 int32_t *flt0_row = flt0 + i * flt_stride;
David Barkerbfbd8b32017-11-01 12:34:23 +0000394 int32_t *flt1_row = flt1 + i * flt_stride;
David Barkerbfbd8b32017-11-01 12:34:23 +0000395 const uint8_t *dat8_row = dat8 + i * dat_stride;
396
397 // Iterate over the stripe in blocks of width pu_width
398 for (int j = 0; j < width; j += pu_width) {
399 const int w = AOMMIN(pu_width, width - j);
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000400 av1_selfguided_restoration(dat8_row + j, w, h, dat_stride, flt0_row + j,
401 flt1_row + j, flt_stride, params, bit_depth,
Debargha Mukherjee1a709942018-02-02 08:08:46 -0800402 use_highbd);
David Barkerbfbd8b32017-11-01 12:34:23 +0000403 }
404 }
405}
406
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100407static SgrprojInfo search_selfguided_restoration(
David Barkerbfbd8b32017-11-01 12:34:23 +0000408 const uint8_t *dat8, int width, int height, int dat_stride,
409 const uint8_t *src8, int src_stride, int use_highbitdepth, int bit_depth,
410 int pu_width, int pu_height, int32_t *rstbuf) {
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000411 int32_t *flt0 = rstbuf;
Urvang Joshi813186b2018-03-08 15:38:46 -0800412 int32_t *flt1 = flt0 + RESTORATION_UNITPELS_MAX;
David Barker506eb722017-03-08 13:35:49 +0000413 int ep, bestep = 0;
David Barkerbfbd8b32017-11-01 12:34:23 +0000414 int64_t besterr = -1;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700415 int exqd[2], bestxqd[2] = { 0, 0 };
David Barkerbfbd8b32017-11-01 12:34:23 +0000416 int flt_stride = ((width + 7) & ~7) + 8;
Debargha Mukherjee7a5587a2017-08-31 07:41:30 -0700417 assert(pu_width == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
418 pu_width == RESTORATION_PROC_UNIT_SIZE);
419 assert(pu_height == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
420 pu_height == RESTORATION_PROC_UNIT_SIZE);
David Barker3a0df182016-12-21 10:44:52 +0000421
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700422 for (ep = 0; ep < SGRPROJ_PARAMS; ep++) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000423 const sgr_params_type *params = &sgr_params[ep];
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700424 int exq[2];
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000425
426 apply_sgr(params, dat8, width, height, dat_stride, use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000427 bit_depth, pu_width, pu_height, flt0, flt1, flt_stride);
Debargha Mukherjee7ae7aea2017-05-04 15:17:17 -0700428 aom_clear_system_state();
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000429 get_proj_subspace(src8, width, height, src_stride, dat8, dat_stride,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000430 use_highbitdepth, flt0, flt_stride, flt1, flt_stride, exq,
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000431 params);
Debargha Mukherjee7ae7aea2017-05-04 15:17:17 -0700432 aom_clear_system_state();
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000433 encode_xq(exq, exqd, params);
434 int64_t err = finer_search_pixel_proj_error(
435 src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000436 flt0, flt_stride, flt1, flt_stride, 2, exqd, params);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700437 if (besterr == -1 || err < besterr) {
438 bestep = ep;
439 besterr = err;
440 bestxqd[0] = exqd[0];
441 bestxqd[1] = exqd[1];
442 }
443 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100444
445 SgrprojInfo ret;
446 ret.ep = bestep;
447 ret.xqd[0] = bestxqd[0];
448 ret.xqd[1] = bestxqd[1];
449 return ret;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700450}
451
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700452static int count_sgrproj_bits(SgrprojInfo *sgrproj_info,
453 SgrprojInfo *ref_sgrproj_info) {
454 int bits = SGRPROJ_PARAMS_BITS;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000455 const sgr_params_type *params = &sgr_params[sgrproj_info->ep];
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000456 if (params->r0 > 0)
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000457 bits += aom_count_primitive_refsubexpfin(
458 SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
459 ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0,
460 sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0);
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000461 if (params->r1 > 0)
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000462 bits += aom_count_primitive_refsubexpfin(
463 SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
464 ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1,
465 sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700466 return bits;
467}
468
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100469static void search_sgrproj(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000470 const AV1PixelRect *tile, int rest_unit_idx,
471 void *priv) {
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100472 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
473 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100474
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100475 const MACROBLOCK *const x = rsc->x;
476 const AV1_COMMON *const cm = rsc->cm;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100477 const int highbd = cm->use_highbitdepth;
478 const int bit_depth = cm->bit_depth;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100479
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +0100480 uint8_t *dgd_start =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100481 rsc->dgd_buffer + limits->v_start * rsc->dgd_stride + limits->h_start;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100482 const uint8_t *src_start =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100483 rsc->src_buffer + limits->v_start * rsc->src_stride + limits->h_start;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100484
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000485 const int is_uv = rsc->plane > 0;
486 const int ss_x = is_uv && cm->subsampling_x;
487 const int ss_y = is_uv && cm->subsampling_y;
488 const int procunit_width = RESTORATION_PROC_UNIT_SIZE >> ss_x;
489 const int procunit_height = RESTORATION_PROC_UNIT_SIZE >> ss_y;
490
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100491 rusi->sgrproj = search_selfguided_restoration(
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +0100492 dgd_start, limits->h_end - limits->h_start,
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100493 limits->v_end - limits->v_start, rsc->dgd_stride, src_start,
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000494 rsc->src_stride, highbd, bit_depth, procunit_width, procunit_height,
495 cm->rst_tmpbuf);
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100496
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100497 RestorationUnitInfo rui;
498 rui.restoration_type = RESTORE_SGRPROJ;
499 rui.sgrproj_info = rusi->sgrproj;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100500
Urvang Joshi813186b2018-03-08 15:38:46 -0800501 rusi->sse[RESTORE_SGRPROJ] = try_restoration_unit(rsc, limits, tile, &rui);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100502
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100503 const int64_t bits_none = x->sgrproj_restore_cost[0];
504 const int64_t bits_sgr = x->sgrproj_restore_cost[1] +
505 (count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj)
506 << AV1_PROB_COST_SHIFT);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100507
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100508 double cost_none =
509 RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
510 double cost_sgr =
511 RDCOST_DBL(x->rdmult, bits_sgr >> 4, rusi->sse[RESTORE_SGRPROJ]);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100512
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100513 RestorationType rtype =
514 (cost_sgr < cost_none) ? RESTORE_SGRPROJ : RESTORE_NONE;
515 rusi->best_rtype[RESTORE_SGRPROJ - 1] = rtype;
Debargha Mukherjeee168a782017-08-31 12:30:10 -0700516
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100517 rsc->sse += rusi->sse[rtype];
518 rsc->bits += (cost_sgr < cost_none) ? bits_sgr : bits_none;
519 if (cost_sgr < cost_none) rsc->sgrproj = rusi->sgrproj;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700520}
521
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100522static double find_average(const uint8_t *src, int h_start, int h_end,
523 int v_start, int v_end, int stride) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700524 uint64_t sum = 0;
525 double avg = 0;
526 int i, j;
Jingning Han041c67b2017-04-14 21:39:26 -0700527 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700528 for (i = v_start; i < v_end; i++)
529 for (j = h_start; j < h_end; j++) sum += src[i * stride + j];
530 avg = (double)sum / ((v_end - v_start) * (h_end - h_start));
531 return avg;
532}
533
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100534static void compute_stats(int wiener_win, const uint8_t *dgd,
535 const uint8_t *src, int h_start, int h_end,
536 int v_start, int v_end, int dgd_stride,
537 int src_stride, double *M, double *H) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538 int i, j, k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800539 double Y[WIENER_WIN2];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700540 const int wiener_win2 = wiener_win * wiener_win;
541 const int wiener_halfwin = (wiener_win >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700542 const double avg =
543 find_average(dgd, h_start, h_end, v_start, v_end, dgd_stride);
544
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700545 memset(M, 0, sizeof(*M) * wiener_win2);
546 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547 for (i = v_start; i < v_end; i++) {
548 for (j = h_start; j < h_end; j++) {
549 const double X = (double)src[i * src_stride + j] - avg;
550 int idx = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700551 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
552 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700553 Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg;
554 idx++;
555 }
556 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700557 assert(idx == wiener_win2);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700558 for (k = 0; k < wiener_win2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700559 M[k] += Y[k] * X;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700560 H[k * wiener_win2 + k] += Y[k] * Y[k];
561 for (l = k + 1; l < wiener_win2; ++l) {
David Barker33f3bfd2017-01-06 15:34:50 +0000562 // H is a symmetric matrix, so we only need to fill out the upper
563 // triangle here. We can copy it down to the lower triangle outside
564 // the (i, j) loops.
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700565 H[k * wiener_win2 + l] += Y[k] * Y[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700566 }
567 }
568 }
569 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700570 for (k = 0; k < wiener_win2; ++k) {
571 for (l = k + 1; l < wiener_win2; ++l) {
572 H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
David Barker33f3bfd2017-01-06 15:34:50 +0000573 }
574 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700575}
576
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100577static double find_average_highbd(const uint16_t *src, int h_start, int h_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700578 int v_start, int v_end, int stride) {
579 uint64_t sum = 0;
580 double avg = 0;
581 int i, j;
Jingning Han041c67b2017-04-14 21:39:26 -0700582 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700583 for (i = v_start; i < v_end; i++)
584 for (j = h_start; j < h_end; j++) sum += src[i * stride + j];
585 avg = (double)sum / ((v_end - v_start) * (h_end - h_start));
586 return avg;
587}
588
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100589static void compute_stats_highbd(int wiener_win, const uint8_t *dgd8,
590 const uint8_t *src8, int h_start, int h_end,
591 int v_start, int v_end, int dgd_stride,
592 int src_stride, double *M, double *H) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700593 int i, j, k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800594 double Y[WIENER_WIN2];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700595 const int wiener_win2 = wiener_win * wiener_win;
596 const int wiener_halfwin = (wiener_win >> 1);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100597 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
598 const uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700599 const double avg =
600 find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
601
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700602 memset(M, 0, sizeof(*M) * wiener_win2);
603 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604 for (i = v_start; i < v_end; i++) {
605 for (j = h_start; j < h_end; j++) {
606 const double X = (double)src[i * src_stride + j] - avg;
607 int idx = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700608 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
609 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610 Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg;
611 idx++;
612 }
613 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700614 assert(idx == wiener_win2);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700615 for (k = 0; k < wiener_win2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700616 M[k] += Y[k] * X;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700617 H[k * wiener_win2 + k] += Y[k] * Y[k];
618 for (l = k + 1; l < wiener_win2; ++l) {
David Barker33f3bfd2017-01-06 15:34:50 +0000619 // H is a symmetric matrix, so we only need to fill out the upper
620 // triangle here. We can copy it down to the lower triangle outside
621 // the (i, j) loops.
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700622 H[k * wiener_win2 + l] += Y[k] * Y[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623 }
624 }
625 }
626 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700627 for (k = 0; k < wiener_win2; ++k) {
628 for (l = k + 1; l < wiener_win2; ++l) {
629 H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
David Barker33f3bfd2017-01-06 15:34:50 +0000630 }
631 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700633
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700634static INLINE int wrap_index(int i, int wiener_win) {
635 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
636 return (i >= wiener_halfwin1 ? wiener_win - 1 - i : i);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637}
638
639// Fix vector b, update vector a
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700640static void update_a_sep_sym(int wiener_win, double **Mc, double **Hc,
641 double *a, double *b) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700642 int i, j;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800643 double S[WIENER_WIN];
Debargha Mukherjee6ae588f2017-04-14 00:40:02 -0700644 double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700645 const int wiener_win2 = wiener_win * wiener_win;
646 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700647 memset(A, 0, sizeof(A));
648 memset(B, 0, sizeof(B));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700649 for (i = 0; i < wiener_win; i++) {
650 for (j = 0; j < wiener_win; ++j) {
651 const int jj = wrap_index(j, wiener_win);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700652 A[jj] += Mc[i][j] * b[i];
653 }
654 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700655 for (i = 0; i < wiener_win; i++) {
656 for (j = 0; j < wiener_win; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657 int k, l;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700658 for (k = 0; k < wiener_win; ++k)
659 for (l = 0; l < wiener_win; ++l) {
660 const int kk = wrap_index(k, wiener_win);
661 const int ll = wrap_index(l, wiener_win);
662 B[ll * wiener_halfwin1 + kk] +=
663 Hc[j * wiener_win + i][k * wiener_win2 + l] * b[i] * b[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664 }
665 }
666 }
667 // Normalization enforcement in the system of equations itself
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700668 for (i = 0; i < wiener_halfwin1 - 1; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700669 A[i] -=
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700670 A[wiener_halfwin1 - 1] * 2 +
671 B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
672 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
673 for (i = 0; i < wiener_halfwin1 - 1; ++i)
674 for (j = 0; j < wiener_halfwin1 - 1; ++j)
675 B[i * wiener_halfwin1 + j] -=
676 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
677 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
678 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
679 (wiener_halfwin1 - 1)]);
680 if (linsolve(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
681 S[wiener_halfwin1 - 1] = 1.0;
682 for (i = wiener_halfwin1; i < wiener_win; ++i) {
683 S[i] = S[wiener_win - 1 - i];
684 S[wiener_halfwin1 - 1] -= 2 * S[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700685 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700686 memcpy(a, S, wiener_win * sizeof(*a));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700687 }
688}
689
690// Fix vector a, update vector b
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700691static void update_b_sep_sym(int wiener_win, double **Mc, double **Hc,
692 double *a, double *b) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693 int i, j;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800694 double S[WIENER_WIN];
Debargha Mukherjee6ae588f2017-04-14 00:40:02 -0700695 double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700696 const int wiener_win2 = wiener_win * wiener_win;
697 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700698 memset(A, 0, sizeof(A));
699 memset(B, 0, sizeof(B));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700700 for (i = 0; i < wiener_win; i++) {
701 const int ii = wrap_index(i, wiener_win);
702 for (j = 0; j < wiener_win; j++) A[ii] += Mc[i][j] * a[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703 }
704
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 ii = wrap_index(i, wiener_win);
708 const int jj = wrap_index(j, wiener_win);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700709 int k, l;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700710 for (k = 0; k < wiener_win; ++k)
711 for (l = 0; l < wiener_win; ++l)
712 B[jj * wiener_halfwin1 + ii] +=
713 Hc[i * wiener_win + j][k * wiener_win2 + l] * a[k] * a[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700714 }
715 }
716 // Normalization enforcement in the system of equations itself
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700717 for (i = 0; i < wiener_halfwin1 - 1; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718 A[i] -=
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700719 A[wiener_halfwin1 - 1] * 2 +
720 B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
721 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
722 for (i = 0; i < wiener_halfwin1 - 1; ++i)
723 for (j = 0; j < wiener_halfwin1 - 1; ++j)
724 B[i * wiener_halfwin1 + j] -=
725 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
726 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
727 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
728 (wiener_halfwin1 - 1)]);
729 if (linsolve(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
730 S[wiener_halfwin1 - 1] = 1.0;
731 for (i = wiener_halfwin1; i < wiener_win; ++i) {
732 S[i] = S[wiener_win - 1 - i];
733 S[wiener_halfwin1 - 1] -= 2 * S[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700734 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700735 memcpy(b, S, wiener_win * sizeof(*b));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700736 }
737}
738
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700739static int wiener_decompose_sep_sym(int wiener_win, double *M, double *H,
740 double *a, double *b) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700741 static const int init_filt[WIENER_WIN] = {
742 WIENER_FILT_TAP0_MIDV, WIENER_FILT_TAP1_MIDV, WIENER_FILT_TAP2_MIDV,
743 WIENER_FILT_TAP3_MIDV, WIENER_FILT_TAP2_MIDV, WIENER_FILT_TAP1_MIDV,
744 WIENER_FILT_TAP0_MIDV,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700745 };
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800746 double *Hc[WIENER_WIN2];
747 double *Mc[WIENER_WIN];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700748 int i, j, iter;
749 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
750 const int wiener_win2 = wiener_win * wiener_win;
751 for (i = 0; i < wiener_win; i++) {
752 a[i] = b[i] = (double)init_filt[i + plane_off] / WIENER_FILT_STEP;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700753 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700754 for (i = 0; i < wiener_win; i++) {
755 Mc[i] = M + i * wiener_win;
756 for (j = 0; j < wiener_win; j++) {
757 Hc[i * wiener_win + j] =
758 H + i * wiener_win * wiener_win2 + j * wiener_win;
759 }
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700760 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700761
762 iter = 1;
Debargha Mukherjee1b3dbf02017-03-13 14:47:21 -0700763 while (iter < NUM_WIENER_ITERS) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700764 update_a_sep_sym(wiener_win, Mc, Hc, a, b);
765 update_b_sep_sym(wiener_win, Mc, Hc, a, b);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700766 iter++;
767 }
768 return 1;
769}
770
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800771// Computes the function x'*H*x - x'*M for the learned 2D filter x, and compares
Yaowu Xuc27fc142016-08-22 16:08:15 -0700772// against identity filters; Final score is defined as the difference between
773// the function values
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700774static double compute_score(int wiener_win, double *M, double *H,
775 InterpKernel vfilt, InterpKernel hfilt) {
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800776 double ab[WIENER_WIN * WIENER_WIN];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700777 int i, k, l;
778 double P = 0, Q = 0;
779 double iP = 0, iQ = 0;
780 double Score, iScore;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800781 double a[WIENER_WIN], b[WIENER_WIN];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700782 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
783 const int wiener_win2 = wiener_win * wiener_win;
Jingning Han041c67b2017-04-14 21:39:26 -0700784
785 aom_clear_system_state();
786
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800787 a[WIENER_HALFWIN] = b[WIENER_HALFWIN] = 1.0;
788 for (i = 0; i < WIENER_HALFWIN; ++i) {
789 a[i] = a[WIENER_WIN - i - 1] = (double)vfilt[i] / WIENER_FILT_STEP;
790 b[i] = b[WIENER_WIN - i - 1] = (double)hfilt[i] / WIENER_FILT_STEP;
791 a[WIENER_HALFWIN] -= 2 * a[i];
792 b[WIENER_HALFWIN] -= 2 * b[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700794 memset(ab, 0, sizeof(ab));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700795 for (k = 0; k < wiener_win; ++k) {
796 for (l = 0; l < wiener_win; ++l)
797 ab[k * wiener_win + l] = a[l + plane_off] * b[k + plane_off];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700798 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700799 for (k = 0; k < wiener_win2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 P += ab[k] * M[k];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700801 for (l = 0; l < wiener_win2; ++l)
802 Q += ab[k] * H[k * wiener_win2 + l] * ab[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803 }
804 Score = Q - 2 * P;
805
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700806 iP = M[wiener_win2 >> 1];
807 iQ = H[(wiener_win2 >> 1) * wiener_win2 + (wiener_win2 >> 1)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808 iScore = iQ - 2 * iP;
809
810 return Score - iScore;
811}
812
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700813static void quantize_sym_filter(int wiener_win, double *f, InterpKernel fi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700814 int i;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700815 const int wiener_halfwin = (wiener_win >> 1);
816 for (i = 0; i < wiener_halfwin; ++i) {
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800817 fi[i] = RINT(f[i] * WIENER_FILT_STEP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818 }
819 // Specialize for 7-tap filter
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700820 if (wiener_win == WIENER_WIN) {
821 fi[0] = CLIP(fi[0], WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP0_MAXV);
822 fi[1] = CLIP(fi[1], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
823 fi[2] = CLIP(fi[2], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
824 } else {
825 fi[2] = CLIP(fi[1], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
826 fi[1] = CLIP(fi[0], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
827 fi[0] = 0;
828 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800829 // Satisfy filter constraints
830 fi[WIENER_WIN - 1] = fi[0];
831 fi[WIENER_WIN - 2] = fi[1];
832 fi[WIENER_WIN - 3] = fi[2];
David Barker1e8e6b92017-01-13 13:45:51 +0000833 // The central element has an implicit +WIENER_FILT_STEP
834 fi[3] = -2 * (fi[0] + fi[1] + fi[2]);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800835}
836
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700837static int count_wiener_bits(int wiener_win, WienerInfo *wiener_info,
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700838 WienerInfo *ref_wiener_info) {
839 int bits = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700840 if (wiener_win == WIENER_WIN)
841 bits += aom_count_primitive_refsubexpfin(
842 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
843 WIENER_FILT_TAP0_SUBEXP_K,
844 ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV,
845 wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700846 bits += aom_count_primitive_refsubexpfin(
847 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
848 WIENER_FILT_TAP1_SUBEXP_K,
849 ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV,
850 wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV);
851 bits += aom_count_primitive_refsubexpfin(
852 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
853 WIENER_FILT_TAP2_SUBEXP_K,
854 ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV,
855 wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700856 if (wiener_win == WIENER_WIN)
857 bits += aom_count_primitive_refsubexpfin(
858 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
859 WIENER_FILT_TAP0_SUBEXP_K,
860 ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV,
861 wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700862 bits += aom_count_primitive_refsubexpfin(
863 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
864 WIENER_FILT_TAP1_SUBEXP_K,
865 ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV,
866 wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV);
867 bits += aom_count_primitive_refsubexpfin(
868 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
869 WIENER_FILT_TAP2_SUBEXP_K,
870 ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV,
871 wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV);
872 return bits;
873}
874
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700875#define USE_WIENER_REFINEMENT_SEARCH 1
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000876static int64_t finer_tile_search_wiener(const RestSearchCtxt *rsc,
877 const RestorationTileLimits *limits,
878 const AV1PixelRect *tile,
879 RestorationUnitInfo *rui,
880 int wiener_win) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700881 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
Urvang Joshi813186b2018-03-08 15:38:46 -0800882 int64_t err = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700883#if USE_WIENER_REFINEMENT_SEARCH
884 int64_t err2;
885 int tap_min[] = { WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP1_MINV,
886 WIENER_FILT_TAP2_MINV };
887 int tap_max[] = { WIENER_FILT_TAP0_MAXV, WIENER_FILT_TAP1_MAXV,
888 WIENER_FILT_TAP2_MAXV };
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100889
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100890 WienerInfo *plane_wiener = &rui->wiener_info;
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100891
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700892 // printf("err pre = %"PRId64"\n", err);
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000893 const int start_step = 4;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700894 for (int s = start_step; s >= 1; s >>= 1) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700895 for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700896 int skip = 0;
897 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100898 if (plane_wiener->hfilter[p] - s >= tap_min[p]) {
899 plane_wiener->hfilter[p] -= s;
900 plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
901 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -0800902 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700903 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100904 plane_wiener->hfilter[p] += s;
905 plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
906 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700907 } else {
908 err = err2;
909 skip = 1;
910 // At the highest step size continue moving in the same direction
911 if (s == start_step) continue;
912 }
913 }
914 break;
915 } while (1);
916 if (skip) break;
917 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100918 if (plane_wiener->hfilter[p] + s <= tap_max[p]) {
919 plane_wiener->hfilter[p] += s;
920 plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
921 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -0800922 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700923 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100924 plane_wiener->hfilter[p] -= s;
925 plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
926 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700927 } else {
928 err = err2;
929 // At the highest step size continue moving in the same direction
930 if (s == start_step) continue;
931 }
932 }
933 break;
934 } while (1);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700935 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700936 for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700937 int skip = 0;
938 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100939 if (plane_wiener->vfilter[p] - s >= tap_min[p]) {
940 plane_wiener->vfilter[p] -= s;
941 plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
942 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -0800943 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700944 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100945 plane_wiener->vfilter[p] += s;
946 plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
947 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700948 } else {
949 err = err2;
950 skip = 1;
951 // At the highest step size continue moving in the same direction
952 if (s == start_step) continue;
953 }
954 }
955 break;
956 } while (1);
957 if (skip) break;
958 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100959 if (plane_wiener->vfilter[p] + s <= tap_max[p]) {
960 plane_wiener->vfilter[p] += s;
961 plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
962 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -0800963 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700964 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100965 plane_wiener->vfilter[p] -= s;
966 plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
967 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700968 } else {
969 err = err2;
970 // At the highest step size continue moving in the same direction
971 if (s == start_step) continue;
972 }
973 }
974 break;
975 } while (1);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700976 }
977 }
978// printf("err post = %"PRId64"\n", err);
979#endif // USE_WIENER_REFINEMENT_SEARCH
980 return err;
981}
982
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100983static void search_wiener(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000984 const AV1PixelRect *tile_rect, int rest_unit_idx,
985 void *priv) {
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100986 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
987 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
988
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100989 const int wiener_win =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100990 (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100991
992 double M[WIENER_WIN2];
993 double H[WIENER_WIN2 * WIENER_WIN2];
994 double vfilterd[WIENER_WIN], hfilterd[WIENER_WIN];
995
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100996 const AV1_COMMON *const cm = rsc->cm;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100997 if (cm->use_highbitdepth)
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100998 compute_stats_highbd(wiener_win, rsc->dgd_buffer, rsc->src_buffer,
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +0100999 limits->h_start, limits->h_end, limits->v_start,
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001000 limits->v_end, rsc->dgd_stride, rsc->src_stride, M, H);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001001 else
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001002 compute_stats(wiener_win, rsc->dgd_buffer, rsc->src_buffer, limits->h_start,
1003 limits->h_end, limits->v_start, limits->v_end,
1004 rsc->dgd_stride, rsc->src_stride, M, H);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001005
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001006 const MACROBLOCK *const x = rsc->x;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001007 const int64_t bits_none = x->wiener_restore_cost[0];
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001008
1009 if (!wiener_decompose_sep_sym(wiener_win, M, H, vfilterd, hfilterd)) {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001010 rsc->bits += bits_none;
1011 rsc->sse += rusi->sse[RESTORE_NONE];
1012 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1013 rusi->sse[RESTORE_WIENER] = INT64_MAX;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001014 return;
1015 }
1016
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001017 RestorationUnitInfo rui;
1018 memset(&rui, 0, sizeof(rui));
1019 rui.restoration_type = RESTORE_WIENER;
1020 quantize_sym_filter(wiener_win, vfilterd, rui.wiener_info.vfilter);
1021 quantize_sym_filter(wiener_win, hfilterd, rui.wiener_info.hfilter);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001022
1023 // Filter score computes the value of the function x'*A*x - x'*b for the
1024 // learned filter and compares it against identity filer. If there is no
1025 // reduction in the function, the filter is reverted back to identity
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001026 if (compute_score(wiener_win, M, H, rui.wiener_info.vfilter,
1027 rui.wiener_info.hfilter) > 0) {
1028 rsc->bits += bits_none;
1029 rsc->sse += rusi->sse[RESTORE_NONE];
1030 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1031 rusi->sse[RESTORE_WIENER] = INT64_MAX;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001032 return;
1033 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001034
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001035 aom_clear_system_state();
1036
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001037 rusi->sse[RESTORE_WIENER] =
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001038 finer_tile_search_wiener(rsc, limits, tile_rect, &rui, wiener_win);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001039 rusi->wiener = rui.wiener_info;
1040
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001041 if (wiener_win != WIENER_WIN) {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001042 assert(rui.wiener_info.vfilter[0] == 0 &&
1043 rui.wiener_info.vfilter[WIENER_WIN - 1] == 0);
1044 assert(rui.wiener_info.hfilter[0] == 0 &&
1045 rui.wiener_info.hfilter[WIENER_WIN - 1] == 0);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001046 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001047
1048 const int64_t bits_wiener =
1049 x->wiener_restore_cost[1] +
1050 (count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener)
1051 << AV1_PROB_COST_SHIFT);
1052
1053 double cost_none =
1054 RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
1055 double cost_wiener =
1056 RDCOST_DBL(x->rdmult, bits_wiener >> 4, rusi->sse[RESTORE_WIENER]);
1057
1058 RestorationType rtype =
1059 (cost_wiener < cost_none) ? RESTORE_WIENER : RESTORE_NONE;
1060 rusi->best_rtype[RESTORE_WIENER - 1] = rtype;
1061
1062 rsc->sse += rusi->sse[rtype];
1063 rsc->bits += (cost_wiener < cost_none) ? bits_wiener : bits_none;
1064 if (cost_wiener < cost_none) rsc->wiener = rusi->wiener;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001065}
1066
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001067static void search_norestore(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001068 const AV1PixelRect *tile_rect, int rest_unit_idx,
1069 void *priv) {
1070 (void)tile_rect;
1071
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001072 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1073 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1074
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001075 const int highbd = rsc->cm->use_highbitdepth;
Urvang Joshi813186b2018-03-08 15:38:46 -08001076 rusi->sse[RESTORE_NONE] = sse_restoration_unit(
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001077 limits, rsc->src, rsc->cm->frame_to_show, rsc->plane, highbd);
Rupert Swarbrick64b8bbd2017-10-16 15:53:07 +01001078
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001079 rsc->sse += rusi->sse[RESTORE_NONE];
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001080}
1081
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001082static void search_switchable(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001083 const AV1PixelRect *tile_rect, int rest_unit_idx,
1084 void *priv) {
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +01001085 (void)limits;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001086 (void)tile_rect;
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001087 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1088 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001089
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001090 const MACROBLOCK *const x = rsc->x;
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001091
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001092 const int wiener_win =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001093 (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001094
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001095 double best_cost = 0;
1096 int64_t best_bits = 0;
1097 RestorationType best_rtype = RESTORE_NONE;
1098
1099 for (RestorationType r = 0; r < RESTORE_SWITCHABLE_TYPES; ++r) {
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001100 // Check for the condition that wiener or sgrproj search could not
1101 // find a solution or the solution was worse than RESTORE_NONE.
1102 // In either case the best_rtype will be set as RESTORE_NONE. These
1103 // should be skipped from the test below.
1104 if (r > RESTORE_NONE) {
1105 if (rusi->best_rtype[r - 1] == RESTORE_NONE) continue;
1106 }
1107
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001108 const int64_t sse = rusi->sse[r];
1109 int64_t coeff_pcost = 0;
1110 switch (r) {
1111 case RESTORE_NONE: coeff_pcost = 0; break;
1112 case RESTORE_WIENER:
1113 coeff_pcost =
1114 count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener);
1115 break;
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001116 case RESTORE_SGRPROJ:
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001117 coeff_pcost = count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj);
1118 break;
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001119 default: assert(0); break;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001120 }
1121 const int64_t coeff_bits = coeff_pcost << AV1_PROB_COST_SHIFT;
1122 const int64_t bits = x->switchable_restore_cost[r] + coeff_bits;
1123 double cost = RDCOST_DBL(x->rdmult, bits >> 4, sse);
1124 if (r == 0 || cost < best_cost) {
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001125 best_cost = cost;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001126 best_bits = bits;
1127 best_rtype = r;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001128 }
1129 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001130
1131 rusi->best_rtype[RESTORE_SWITCHABLE - 1] = best_rtype;
1132
1133 rsc->sse += rusi->sse[best_rtype];
1134 rsc->bits += best_bits;
1135 if (best_rtype == RESTORE_WIENER) rsc->wiener = rusi->wiener;
1136 if (best_rtype == RESTORE_SGRPROJ) rsc->sgrproj = rusi->sgrproj;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001137}
1138
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001139static void copy_unit_info(RestorationType frame_rtype,
1140 const RestUnitSearchInfo *rusi,
1141 RestorationUnitInfo *rui) {
1142 assert(frame_rtype > 0);
1143 rui->restoration_type = rusi->best_rtype[frame_rtype - 1];
1144 if (rui->restoration_type == RESTORE_WIENER)
1145 rui->wiener_info = rusi->wiener;
1146 else
1147 rui->sgrproj_info = rusi->sgrproj;
1148}
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001149
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001150static double search_rest_type(RestSearchCtxt *rsc, RestorationType rtype) {
1151 static const rest_unit_visitor_t funs[RESTORE_TYPES] = {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001152 search_norestore, search_wiener, search_sgrproj, search_switchable
1153 };
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001154
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001155 reset_rsc(rsc);
1156 av1_foreach_rest_unit_in_frame(rsc->cm, rsc->plane, rsc_on_tile, funs[rtype],
1157 rsc);
1158 return RDCOST_DBL(rsc->x->rdmult, rsc->bits >> 4, rsc->sse);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001159}
1160
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001161static int rest_tiles_in_plane(const AV1_COMMON *cm, int plane) {
1162 const RestorationInfo *rsi = &cm->rst_info[plane];
David Barkeree0ae202018-03-28 16:58:57 +01001163#if CONFIG_SIMPLE_LRUNIT
1164 return rsi->units_per_tile;
1165#else
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001166 return cm->tile_rows * cm->tile_cols * rsi->units_per_tile;
David Barkeree0ae202018-03-28 16:58:57 +01001167#endif // CONFIG_SIMPLE_LRUNIT
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001168}
1169
Rupert Swarbrick146a0602017-10-17 16:52:20 +01001170void av1_pick_filter_restoration(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001171 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001172 const int num_planes = av1_num_planes(cm);
Urvang Joshi14072aa2018-03-21 17:43:36 -07001173 assert(!cm->all_lossless);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001174
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001175 int ntiles[2];
1176 for (int is_uv = 0; is_uv < 2; ++is_uv)
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001177 ntiles[is_uv] = rest_tiles_in_plane(cm, is_uv);
Debargha Mukherjeed48f5732017-05-19 14:58:07 -07001178
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001179 assert(ntiles[1] <= ntiles[0]);
1180 RestUnitSearchInfo *rusi =
Yaowu Xu43e30f42017-11-27 14:26:00 -08001181 (RestUnitSearchInfo *)aom_memalign(16, sizeof(*rusi) * ntiles[0]);
Debargha Mukherjeed48f5732017-05-19 14:58:07 -07001182
Imdad Sardharwallab08544d2018-01-23 12:45:31 +00001183 // If the restoration unit dimensions are not multiples of
1184 // rsi->restoration_unit_size then some elements of the rusi array may be
1185 // left uninitialised when we reach copy_unit_info(...). This is not a
1186 // problem, as these elements are ignored later, but in order to quiet
1187 // Valgrind's warnings we initialise the array below.
1188 memset(rusi, 0, sizeof(*rusi) * ntiles[0]);
1189
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001190 RestSearchCtxt rsc;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001191 const int plane_start = AOM_PLANE_Y;
1192 const int plane_end = num_planes > 1 ? AOM_PLANE_V : AOM_PLANE_Y;
1193 for (int plane = plane_start; plane <= plane_end; ++plane) {
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001194 init_rsc(src, &cpi->common, &cpi->td.mb, plane, rusi, &cpi->trial_frame_rst,
1195 &rsc);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001196
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001197 const int plane_ntiles = ntiles[plane > 0];
1198 const RestorationType num_rtypes =
1199 (plane_ntiles > 1) ? RESTORE_TYPES : RESTORE_SWITCHABLE_TYPES;
1200
1201 double best_cost = 0;
1202 RestorationType best_rtype = RESTORE_NONE;
1203
Rupert Swarbrick5b401362017-10-31 10:56:27 +00001204 const int highbd = rsc.cm->use_highbitdepth;
Rupert Swarbrick5b401362017-10-31 10:56:27 +00001205 extend_frame(rsc.dgd_buffer, rsc.plane_width, rsc.plane_height,
1206 rsc.dgd_stride, RESTORATION_BORDER, RESTORATION_BORDER,
1207 highbd);
1208
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001209 for (RestorationType r = 0; r < num_rtypes; ++r) {
1210 if ((force_restore_type != RESTORE_TYPES) && (r != RESTORE_NONE) &&
1211 (r != force_restore_type))
1212 continue;
1213
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001214 double cost = search_rest_type(&rsc, r);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001215
1216 if (r == 0 || cost < best_cost) {
1217 best_cost = cost;
1218 best_rtype = r;
1219 }
1220 }
1221
1222 cm->rst_info[plane].frame_restoration_type = best_rtype;
1223 if (force_restore_type != RESTORE_TYPES)
1224 assert(best_rtype == force_restore_type || best_rtype == RESTORE_NONE);
1225
1226 if (best_rtype != RESTORE_NONE) {
1227 for (int u = 0; u < plane_ntiles; ++u) {
1228 copy_unit_info(best_rtype, &rusi[u], &cm->rst_info[plane].unit_info[u]);
1229 }
1230 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001231 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001232
1233 aom_free(rusi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001234}