blob: 115f66f543ec708f19d0d570c37bf551a12c334a [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 Mukherjeeda394fb2018-05-18 09:05:16 -070043// Penalty factor for use of dual sgr
44#define DUAL_SGR_PENALTY_MULT 0.01
45
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -080046const int frame_level_restore_bits[RESTORE_TYPES] = { 2, 2, 2, 2 };
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -070047
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +010048typedef int64_t (*sse_extractor_type)(const YV12_BUFFER_CONFIG *a,
49 const YV12_BUFFER_CONFIG *b);
50typedef int64_t (*sse_part_extractor_type)(const YV12_BUFFER_CONFIG *a,
51 const YV12_BUFFER_CONFIG *b,
52 int hstart, int width, int vstart,
53 int height);
54
Yaowu Xud3e7c682017-12-21 14:08:25 -080055#define NUM_EXTRACTORS (3 * (1 + 1))
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +010056
57static const sse_part_extractor_type sse_part_extractors[NUM_EXTRACTORS] = {
58 aom_get_y_sse_part, aom_get_u_sse_part,
Yaowu Xud3e7c682017-12-21 14:08:25 -080059 aom_get_v_sse_part, aom_highbd_get_y_sse_part,
60 aom_highbd_get_u_sse_part, aom_highbd_get_v_sse_part,
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +010061};
62
Urvang Joshi813186b2018-03-08 15:38:46 -080063static int64_t sse_restoration_unit(const RestorationTileLimits *limits,
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +010064 const YV12_BUFFER_CONFIG *src,
65 const YV12_BUFFER_CONFIG *dst, int plane,
66 int highbd) {
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +010067 return sse_part_extractors[3 * highbd + plane](
68 src, dst, limits->h_start, limits->h_end - limits->h_start,
69 limits->v_start, limits->v_end - limits->v_start);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -070070}
71
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +000072typedef struct {
73 // The best coefficients for Wiener or Sgrproj restoration
74 WienerInfo wiener;
75 SgrprojInfo sgrproj;
76
77 // The sum of squared errors for this rtype.
78 int64_t sse[RESTORE_SWITCHABLE_TYPES];
79
80 // The rtype to use for this unit given a frame rtype as
81 // index. Indices: WIENER, SGRPROJ, SWITCHABLE.
82 RestorationType best_rtype[RESTORE_TYPES - 1];
83} RestUnitSearchInfo;
84
85typedef struct {
86 const YV12_BUFFER_CONFIG *src;
87 YV12_BUFFER_CONFIG *dst;
88
89 const AV1_COMMON *cm;
90 const MACROBLOCK *x;
91 int plane;
92 int plane_width;
93 int plane_height;
94 RestUnitSearchInfo *rusi;
95
Debargha Mukherjeeda394fb2018-05-18 09:05:16 -070096 // Speed features
97 const SPEED_FEATURES *sf;
98
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +000099 uint8_t *dgd_buffer;
100 int dgd_stride;
101 const uint8_t *src_buffer;
102 int src_stride;
103
104 // sse and bits are initialised by reset_rsc in search_rest_type
105 int64_t sse;
106 int64_t bits;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000107 int tile_y0, tile_stripe0;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000108
109 // sgrproj and wiener are initialised by rsc_on_tile when starting the first
110 // tile in the frame.
111 SgrprojInfo sgrproj;
112 WienerInfo wiener;
113} RestSearchCtxt;
114
115static void rsc_on_tile(int tile_row, int tile_col, void *priv) {
116 (void)tile_col;
117
118 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
119 set_default_sgrproj(&rsc->sgrproj);
120 set_default_wiener(&rsc->wiener);
121
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000122 rsc->tile_stripe0 =
123 (tile_row == 0) ? 0 : rsc->cm->rst_end_stripe[tile_row - 1];
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000124}
125
126static void reset_rsc(RestSearchCtxt *rsc) {
127 rsc->sse = 0;
128 rsc->bits = 0;
129}
130
131static void init_rsc(const YV12_BUFFER_CONFIG *src, const AV1_COMMON *cm,
Debargha Mukherjeeda394fb2018-05-18 09:05:16 -0700132 const MACROBLOCK *x, const SPEED_FEATURES *sf, int plane,
133 RestUnitSearchInfo *rusi, YV12_BUFFER_CONFIG *dst,
134 RestSearchCtxt *rsc) {
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000135 rsc->src = src;
136 rsc->dst = dst;
137 rsc->cm = cm;
138 rsc->x = x;
139 rsc->plane = plane;
140 rsc->rusi = rusi;
Debargha Mukherjeeda394fb2018-05-18 09:05:16 -0700141 rsc->sf = sf;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000142
143 const YV12_BUFFER_CONFIG *dgd = cm->frame_to_show;
144 const int is_uv = plane != AOM_PLANE_Y;
145 rsc->plane_width = src->crop_widths[is_uv];
David Barker21f43072017-12-14 14:21:45 +0000146 rsc->plane_height = src->crop_heights[is_uv];
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000147 rsc->src_buffer = src->buffers[plane];
148 rsc->src_stride = src->strides[is_uv];
149 rsc->dgd_buffer = dgd->buffers[plane];
150 rsc->dgd_stride = dgd->strides[is_uv];
151 assert(src->crop_widths[is_uv] == dgd->crop_widths[is_uv]);
152 assert(src->crop_heights[is_uv] == dgd->crop_heights[is_uv]);
153}
154
Urvang Joshi813186b2018-03-08 15:38:46 -0800155static int64_t try_restoration_unit(const RestSearchCtxt *rsc,
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100156 const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000157 const AV1PixelRect *tile_rect,
158 const RestorationUnitInfo *rui) {
159 const AV1_COMMON *const cm = rsc->cm;
160 const int plane = rsc->plane;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100161 const int is_uv = plane > 0;
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000162 const RestorationInfo *rsi = &cm->rst_info[plane];
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100163 RestorationLineBuffers rlbs;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100164 const int bit_depth = cm->bit_depth;
165 const int highbd = cm->use_highbitdepth;
Rupert Swarbrick64b8bbd2017-10-16 15:53:07 +0100166
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100167 const YV12_BUFFER_CONFIG *fts = cm->frame_to_show;
Yunqing Wange8025032018-04-10 18:50:29 -0700168 // TODO(yunqing): For now, only use optimized LR filter in decoder. Can be
169 // also used in encoder.
Yunqing Wang2ff71af2018-04-24 15:10:10 -0700170 const int optimized_lr = 0;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700171
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000172 av1_loop_restoration_filter_unit(
Debargha Mukherjee5105f7a2018-01-29 16:05:54 -0800173 limits, rui, &rsi->boundaries, &rlbs, tile_rect, rsc->tile_stripe0,
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000174 is_uv && cm->subsampling_x, is_uv && cm->subsampling_y, highbd, bit_depth,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000175 fts->buffers[plane], fts->strides[is_uv], rsc->dst->buffers[plane],
Yunqing Wang2ff71af2018-04-24 15:10:10 -0700176 rsc->dst->strides[is_uv], cm->rst_tmpbuf, optimized_lr);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700177
Urvang Joshi813186b2018-03-08 15:38:46 -0800178 return sse_restoration_unit(limits, rsc->src, rsc->dst, plane, highbd);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700179}
180
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100181static int64_t get_pixel_proj_error(const uint8_t *src8, int width, int height,
182 int src_stride, const uint8_t *dat8,
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100183 int dat_stride, int use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000184 int32_t *flt0, int flt0_stride,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700185 int32_t *flt1, int flt1_stride, int *xqd,
186 const sgr_params_type *params) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700187 int i, j;
188 int64_t err = 0;
189 int xq[2];
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000190 decode_xq(xqd, xq, params);
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100191 if (!use_highbitdepth) {
David Barker3a0df182016-12-21 10:44:52 +0000192 const uint8_t *src = src8;
193 const uint8_t *dat = dat8;
194 for (i = 0; i < height; ++i) {
195 for (j = 0; j < width; ++j) {
196 const int32_t u =
197 (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000198 int32_t v = u << SGRPROJ_PRJ_BITS;
Urvang Joshi3715b882018-05-14 20:05:25 -0400199 if (params->r[0] > 0) v += xq[0] * (flt0[i * flt0_stride + j] - u);
200 if (params->r[1] > 0) v += xq[1] * (flt1[i * flt1_stride + j] - u);
David Barker3a0df182016-12-21 10:44:52 +0000201 const int32_t e =
202 ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) -
203 src[i * src_stride + j];
204 err += e * e;
205 }
206 }
207 } else {
208 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
209 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900210 const int32_t half = 1 << (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS - 1);
Urvang Joshi3715b882018-05-14 20:05:25 -0400211 if (params->r[0] > 0 && params->r[1] > 0) {
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900212 int xq0 = xq[0];
213 int xq1 = xq[1];
214 for (i = 0; i < height; ++i) {
215 for (j = 0; j < width; ++j) {
216 const int32_t d = dat[j];
217 const int32_t s = src[j];
218 const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
219 int32_t v0 = flt0[j] - u;
220 int32_t v1 = flt1[j] - u;
221 int32_t v = half;
222 v += xq0 * v0;
223 v += xq1 * v1;
224 const int32_t e =
225 (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
226 err += e * e;
227 }
228 dat += dat_stride;
229 flt0 += flt0_stride;
230 flt1 += flt1_stride;
231 src += src_stride;
232 }
Urvang Joshi3715b882018-05-14 20:05:25 -0400233 } else if (params->r[0] > 0 || params->r[1] > 0) {
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900234 int exq;
235 int32_t *flt;
236 int flt_stride;
Urvang Joshi3715b882018-05-14 20:05:25 -0400237 if (params->r[0] > 0) {
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900238 exq = xq[0];
239 flt = flt0;
240 flt_stride = flt0_stride;
241 } else {
242 exq = xq[1];
243 flt = flt1;
244 flt_stride = flt1_stride;
245 }
246 for (i = 0; i < height; ++i) {
247 for (j = 0; j < width; ++j) {
248 const int32_t d = dat[j];
249 const int32_t s = src[j];
250 const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
251 int32_t v = half;
252 v += exq * (flt[j] - u);
253 const int32_t e =
254 (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
255 err += e * e;
256 }
257 dat += dat_stride;
258 flt += flt_stride;
259 src += src_stride;
260 }
261 } else {
262 for (i = 0; i < height; ++i) {
263 for (j = 0; j < width; ++j) {
264 const int32_t d = dat[j];
265 const int32_t s = src[j];
266 const int32_t e = d - s;
267 err += e * e;
268 }
269 dat += dat_stride;
270 src += src_stride;
David Barker3a0df182016-12-21 10:44:52 +0000271 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700272 }
273 }
274 return err;
275}
276
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700277#define USE_SGRPROJ_REFINEMENT_SEARCH 1
278static int64_t finer_search_pixel_proj_error(
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100279 const uint8_t *src8, int width, int height, int src_stride,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000280 const uint8_t *dat8, int dat_stride, int use_highbitdepth, int32_t *flt0,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700281 int flt0_stride, int32_t *flt1, int flt1_stride, int start_step, int *xqd,
282 const sgr_params_type *params) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000283 int64_t err = get_pixel_proj_error(
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000284 src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth, flt0,
285 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700286 (void)start_step;
287#if USE_SGRPROJ_REFINEMENT_SEARCH
288 int64_t err2;
289 int tap_min[] = { SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MIN1 };
290 int tap_max[] = { SGRPROJ_PRJ_MAX0, SGRPROJ_PRJ_MAX1 };
291 for (int s = start_step; s >= 1; s >>= 1) {
292 for (int p = 0; p < 2; ++p) {
Urvang Joshi3715b882018-05-14 20:05:25 -0400293 if ((params->r[0] == 0 && p == 0) || (params->r[1] == 0 && p == 1)) {
294 continue;
295 }
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700296 int skip = 0;
297 do {
298 if (xqd[p] - s >= tap_min[p]) {
299 xqd[p] -= s;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000300 err2 =
301 get_pixel_proj_error(src8, width, height, src_stride, dat8,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000302 dat_stride, use_highbitdepth, flt0,
303 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700304 if (err2 > err) {
305 xqd[p] += s;
306 } else {
307 err = err2;
308 skip = 1;
309 // At the highest step size continue moving in the same direction
310 if (s == start_step) continue;
311 }
312 }
313 break;
314 } while (1);
315 if (skip) break;
316 do {
317 if (xqd[p] + s <= tap_max[p]) {
318 xqd[p] += s;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000319 err2 =
320 get_pixel_proj_error(src8, width, height, src_stride, dat8,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000321 dat_stride, use_highbitdepth, flt0,
322 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700323 if (err2 > err) {
324 xqd[p] -= s;
325 } else {
326 err = err2;
327 // At the highest step size continue moving in the same direction
328 if (s == start_step) continue;
329 }
330 }
331 break;
332 } while (1);
333 }
334 }
335#endif // USE_SGRPROJ_REFINEMENT_SEARCH
336 return err;
337}
338
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100339static void get_proj_subspace(const uint8_t *src8, int width, int height,
David Barkerbfbd8b32017-11-01 12:34:23 +0000340 int src_stride, const uint8_t *dat8,
341 int dat_stride, int use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000342 int32_t *flt0, int flt0_stride, int32_t *flt1,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700343 int flt1_stride, int *xq,
344 const sgr_params_type *params) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700345 int i, j;
346 double H[2][2] = { { 0, 0 }, { 0, 0 } };
347 double C[2] = { 0, 0 };
348 double Det;
349 double x[2];
350 const int size = width * height;
351
Jingning Han041c67b2017-04-14 21:39:26 -0700352 aom_clear_system_state();
353
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800354 // Default
355 xq[0] = 0;
356 xq[1] = 0;
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100357 if (!use_highbitdepth) {
David Barker3a0df182016-12-21 10:44:52 +0000358 const uint8_t *src = src8;
359 const uint8_t *dat = dat8;
360 for (i = 0; i < height; ++i) {
361 for (j = 0; j < width; ++j) {
362 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
363 const double s =
364 (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000365 const double f1 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400366 (params->r[0] > 0) ? (double)flt0[i * flt0_stride + j] - u : 0;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000367 const double f2 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400368 (params->r[1] > 0) ? (double)flt1[i * flt1_stride + j] - u : 0;
David Barker3a0df182016-12-21 10:44:52 +0000369 H[0][0] += f1 * f1;
370 H[1][1] += f2 * f2;
371 H[0][1] += f1 * f2;
372 C[0] += f1 * s;
373 C[1] += f2 * s;
374 }
375 }
376 } else {
377 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
378 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
379 for (i = 0; i < height; ++i) {
380 for (j = 0; j < width; ++j) {
381 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
382 const double s =
383 (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000384 const double f1 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400385 (params->r[0] > 0) ? (double)flt0[i * flt0_stride + j] - u : 0;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000386 const double f2 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400387 (params->r[1] > 0) ? (double)flt1[i * flt1_stride + j] - u : 0;
David Barker3a0df182016-12-21 10:44:52 +0000388 H[0][0] += f1 * f1;
389 H[1][1] += f2 * f2;
390 H[0][1] += f1 * f2;
391 C[0] += f1 * s;
392 C[1] += f2 * s;
393 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700394 }
395 }
396 H[0][0] /= size;
397 H[0][1] /= size;
398 H[1][1] /= size;
399 H[1][0] = H[0][1];
400 C[0] /= size;
401 C[1] /= size;
Urvang Joshi3715b882018-05-14 20:05:25 -0400402 if (params->r[0] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000403 // H matrix is now only the scalar H[1][1]
404 // C vector is now only the scalar C[1]
405 Det = H[1][1];
406 if (Det < 1e-8) return; // ill-posed, return default values
407 x[0] = 0;
408 x[1] = C[1] / Det;
409
410 xq[0] = 0;
411 xq[1] = (int)rint(x[1] * (1 << SGRPROJ_PRJ_BITS));
Urvang Joshi3715b882018-05-14 20:05:25 -0400412 } else if (params->r[1] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000413 // H matrix is now only the scalar H[0][0]
414 // C vector is now only the scalar C[0]
415 Det = H[0][0];
416 if (Det < 1e-8) return; // ill-posed, return default values
417 x[0] = C[0] / Det;
418 x[1] = 0;
419
420 xq[0] = (int)rint(x[0] * (1 << SGRPROJ_PRJ_BITS));
421 xq[1] = 0;
422 } else {
423 Det = (H[0][0] * H[1][1] - H[0][1] * H[1][0]);
424 if (Det < 1e-8) return; // ill-posed, return default values
425 x[0] = (H[1][1] * C[0] - H[0][1] * C[1]) / Det;
426 x[1] = (H[0][0] * C[1] - H[1][0] * C[0]) / Det;
427
428 xq[0] = (int)rint(x[0] * (1 << SGRPROJ_PRJ_BITS));
429 xq[1] = (int)rint(x[1] * (1 << SGRPROJ_PRJ_BITS));
430 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700431}
432
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000433void encode_xq(int *xq, int *xqd, const sgr_params_type *params) {
Urvang Joshi3715b882018-05-14 20:05:25 -0400434 if (params->r[0] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000435 xqd[0] = 0;
436 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xq[1], SGRPROJ_PRJ_MIN1,
437 SGRPROJ_PRJ_MAX1);
Urvang Joshi3715b882018-05-14 20:05:25 -0400438 } else if (params->r[1] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000439 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
Debargha Mukherjee114575d2018-02-23 11:18:37 -0800440 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0], SGRPROJ_PRJ_MIN1,
441 SGRPROJ_PRJ_MAX1);
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000442 } else {
443 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
444 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0] - xq[1], SGRPROJ_PRJ_MIN1,
445 SGRPROJ_PRJ_MAX1);
446 }
447}
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700448
David Barkerbfbd8b32017-11-01 12:34:23 +0000449// Apply the self-guided filter across an entire restoration unit.
Urvang Joshic079f7a2018-05-11 16:13:56 -0700450static void apply_sgr(int sgr_params_idx, const uint8_t *dat8, int width,
451 int height, int dat_stride, int use_highbd, int bit_depth,
452 int pu_width, int pu_height, int32_t *flt0, int32_t *flt1,
453 int flt_stride) {
David Barkerbfbd8b32017-11-01 12:34:23 +0000454 for (int i = 0; i < height; i += pu_height) {
455 const int h = AOMMIN(pu_height, height - i);
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000456 int32_t *flt0_row = flt0 + i * flt_stride;
David Barkerbfbd8b32017-11-01 12:34:23 +0000457 int32_t *flt1_row = flt1 + i * flt_stride;
David Barkerbfbd8b32017-11-01 12:34:23 +0000458 const uint8_t *dat8_row = dat8 + i * dat_stride;
459
460 // Iterate over the stripe in blocks of width pu_width
461 for (int j = 0; j < width; j += pu_width) {
462 const int w = AOMMIN(pu_width, width - j);
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000463 av1_selfguided_restoration(dat8_row + j, w, h, dat_stride, flt0_row + j,
Urvang Joshic079f7a2018-05-11 16:13:56 -0700464 flt1_row + j, flt_stride, sgr_params_idx,
465 bit_depth, use_highbd);
David Barkerbfbd8b32017-11-01 12:34:23 +0000466 }
467 }
468}
469
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100470static SgrprojInfo search_selfguided_restoration(
David Barkerbfbd8b32017-11-01 12:34:23 +0000471 const uint8_t *dat8, int width, int height, int dat_stride,
472 const uint8_t *src8, int src_stride, int use_highbitdepth, int bit_depth,
473 int pu_width, int pu_height, int32_t *rstbuf) {
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000474 int32_t *flt0 = rstbuf;
Urvang Joshi813186b2018-03-08 15:38:46 -0800475 int32_t *flt1 = flt0 + RESTORATION_UNITPELS_MAX;
David Barker506eb722017-03-08 13:35:49 +0000476 int ep, bestep = 0;
David Barkerbfbd8b32017-11-01 12:34:23 +0000477 int64_t besterr = -1;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700478 int exqd[2], bestxqd[2] = { 0, 0 };
David Barkerbfbd8b32017-11-01 12:34:23 +0000479 int flt_stride = ((width + 7) & ~7) + 8;
Debargha Mukherjee7a5587a2017-08-31 07:41:30 -0700480 assert(pu_width == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
481 pu_width == RESTORATION_PROC_UNIT_SIZE);
482 assert(pu_height == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
483 pu_height == RESTORATION_PROC_UNIT_SIZE);
David Barker3a0df182016-12-21 10:44:52 +0000484
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700485 for (ep = 0; ep < SGRPROJ_PARAMS; ep++) {
486 int exq[2];
Urvang Joshic079f7a2018-05-11 16:13:56 -0700487 apply_sgr(ep, dat8, width, height, dat_stride, use_highbitdepth, bit_depth,
488 pu_width, pu_height, flt0, flt1, flt_stride);
Debargha Mukherjee7ae7aea2017-05-04 15:17:17 -0700489 aom_clear_system_state();
Urvang Joshic079f7a2018-05-11 16:13:56 -0700490 const sgr_params_type *const params = &sgr_params[ep];
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000491 get_proj_subspace(src8, width, height, src_stride, dat8, dat_stride,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000492 use_highbitdepth, flt0, flt_stride, flt1, flt_stride, exq,
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000493 params);
Debargha Mukherjee7ae7aea2017-05-04 15:17:17 -0700494 aom_clear_system_state();
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000495 encode_xq(exq, exqd, params);
496 int64_t err = finer_search_pixel_proj_error(
497 src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000498 flt0, flt_stride, flt1, flt_stride, 2, exqd, params);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700499 if (besterr == -1 || err < besterr) {
500 bestep = ep;
501 besterr = err;
502 bestxqd[0] = exqd[0];
503 bestxqd[1] = exqd[1];
504 }
505 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100506
507 SgrprojInfo ret;
508 ret.ep = bestep;
509 ret.xqd[0] = bestxqd[0];
510 ret.xqd[1] = bestxqd[1];
511 return ret;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700512}
513
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700514static int count_sgrproj_bits(SgrprojInfo *sgrproj_info,
515 SgrprojInfo *ref_sgrproj_info) {
516 int bits = SGRPROJ_PARAMS_BITS;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000517 const sgr_params_type *params = &sgr_params[sgrproj_info->ep];
Urvang Joshi3715b882018-05-14 20:05:25 -0400518 if (params->r[0] > 0)
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000519 bits += aom_count_primitive_refsubexpfin(
520 SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
521 ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0,
522 sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0);
Urvang Joshi3715b882018-05-14 20:05:25 -0400523 if (params->r[1] > 0)
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000524 bits += aom_count_primitive_refsubexpfin(
525 SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
526 ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1,
527 sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700528 return bits;
529}
530
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100531static void search_sgrproj(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000532 const AV1PixelRect *tile, int rest_unit_idx,
533 void *priv) {
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100534 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
535 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100536
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100537 const MACROBLOCK *const x = rsc->x;
538 const AV1_COMMON *const cm = rsc->cm;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100539 const int highbd = cm->use_highbitdepth;
540 const int bit_depth = cm->bit_depth;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100541
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +0100542 uint8_t *dgd_start =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100543 rsc->dgd_buffer + limits->v_start * rsc->dgd_stride + limits->h_start;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100544 const uint8_t *src_start =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100545 rsc->src_buffer + limits->v_start * rsc->src_stride + limits->h_start;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100546
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000547 const int is_uv = rsc->plane > 0;
548 const int ss_x = is_uv && cm->subsampling_x;
549 const int ss_y = is_uv && cm->subsampling_y;
550 const int procunit_width = RESTORATION_PROC_UNIT_SIZE >> ss_x;
551 const int procunit_height = RESTORATION_PROC_UNIT_SIZE >> ss_y;
552
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100553 rusi->sgrproj = search_selfguided_restoration(
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +0100554 dgd_start, limits->h_end - limits->h_start,
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100555 limits->v_end - limits->v_start, rsc->dgd_stride, src_start,
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000556 rsc->src_stride, highbd, bit_depth, procunit_width, procunit_height,
557 cm->rst_tmpbuf);
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100558
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100559 RestorationUnitInfo rui;
560 rui.restoration_type = RESTORE_SGRPROJ;
561 rui.sgrproj_info = rusi->sgrproj;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100562
Urvang Joshi813186b2018-03-08 15:38:46 -0800563 rusi->sse[RESTORE_SGRPROJ] = try_restoration_unit(rsc, limits, tile, &rui);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100564
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100565 const int64_t bits_none = x->sgrproj_restore_cost[0];
566 const int64_t bits_sgr = x->sgrproj_restore_cost[1] +
567 (count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj)
568 << AV1_PROB_COST_SHIFT);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100569
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100570 double cost_none =
571 RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
572 double cost_sgr =
573 RDCOST_DBL(x->rdmult, bits_sgr >> 4, rusi->sse[RESTORE_SGRPROJ]);
Debargha Mukherjeeda394fb2018-05-18 09:05:16 -0700574 if (rusi->sgrproj.ep < 10)
575 cost_sgr *= (1 + DUAL_SGR_PENALTY_MULT * rsc->sf->dual_sgr_penalty_level);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100576
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100577 RestorationType rtype =
578 (cost_sgr < cost_none) ? RESTORE_SGRPROJ : RESTORE_NONE;
579 rusi->best_rtype[RESTORE_SGRPROJ - 1] = rtype;
Debargha Mukherjeee168a782017-08-31 12:30:10 -0700580
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100581 rsc->sse += rusi->sse[rtype];
582 rsc->bits += (cost_sgr < cost_none) ? bits_sgr : bits_none;
583 if (cost_sgr < cost_none) rsc->sgrproj = rusi->sgrproj;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700584}
585
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100586static double find_average(const uint8_t *src, int h_start, int h_end,
587 int v_start, int v_end, int stride) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700588 uint64_t sum = 0;
589 double avg = 0;
590 int i, j;
Jingning Han041c67b2017-04-14 21:39:26 -0700591 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700592 for (i = v_start; i < v_end; i++)
593 for (j = h_start; j < h_end; j++) sum += src[i * stride + j];
594 avg = (double)sum / ((v_end - v_start) * (h_end - h_start));
595 return avg;
596}
597
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100598static void compute_stats(int wiener_win, const uint8_t *dgd,
599 const uint8_t *src, int h_start, int h_end,
600 int v_start, int v_end, int dgd_stride,
601 int src_stride, double *M, double *H) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700602 int i, j, k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800603 double Y[WIENER_WIN2];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700604 const int wiener_win2 = wiener_win * wiener_win;
605 const int wiener_halfwin = (wiener_win >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606 const double avg =
607 find_average(dgd, h_start, h_end, v_start, v_end, dgd_stride);
608
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700609 memset(M, 0, sizeof(*M) * wiener_win2);
610 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700611 for (i = v_start; i < v_end; i++) {
612 for (j = h_start; j < h_end; j++) {
613 const double X = (double)src[i * src_stride + j] - avg;
614 int idx = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700615 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
616 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700617 Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg;
618 idx++;
619 }
620 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700621 assert(idx == wiener_win2);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700622 for (k = 0; k < wiener_win2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623 M[k] += Y[k] * X;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700624 H[k * wiener_win2 + k] += Y[k] * Y[k];
625 for (l = k + 1; l < wiener_win2; ++l) {
David Barker33f3bfd2017-01-06 15:34:50 +0000626 // H is a symmetric matrix, so we only need to fill out the upper
627 // triangle here. We can copy it down to the lower triangle outside
628 // the (i, j) loops.
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700629 H[k * wiener_win2 + l] += Y[k] * Y[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700630 }
631 }
632 }
633 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700634 for (k = 0; k < wiener_win2; ++k) {
635 for (l = k + 1; l < wiener_win2; ++l) {
636 H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
David Barker33f3bfd2017-01-06 15:34:50 +0000637 }
638 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700639}
640
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100641static double find_average_highbd(const uint16_t *src, int h_start, int h_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700642 int v_start, int v_end, int stride) {
643 uint64_t sum = 0;
644 double avg = 0;
645 int i, j;
Jingning Han041c67b2017-04-14 21:39:26 -0700646 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700647 for (i = v_start; i < v_end; i++)
648 for (j = h_start; j < h_end; j++) sum += src[i * stride + j];
649 avg = (double)sum / ((v_end - v_start) * (h_end - h_start));
650 return avg;
651}
652
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +0900653static AOM_FORCE_INLINE void compute_stats_highbd(
654 int wiener_win, const uint8_t *dgd8, const uint8_t *src8, int h_start,
655 int h_end, int v_start, int v_end, int dgd_stride, int src_stride,
656 double *M, double *H) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657 int i, j, k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800658 double Y[WIENER_WIN2];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700659 const int wiener_win2 = wiener_win * wiener_win;
660 const int wiener_halfwin = (wiener_win >> 1);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100661 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
662 const uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700663 const double avg =
664 find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
665
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700666 memset(M, 0, sizeof(*M) * wiener_win2);
667 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668 for (i = v_start; i < v_end; i++) {
669 for (j = h_start; j < h_end; j++) {
670 const double X = (double)src[i * src_stride + j] - avg;
671 int idx = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700672 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
673 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700674 Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg;
675 idx++;
676 }
677 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700678 assert(idx == wiener_win2);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700679 for (k = 0; k < wiener_win2; ++k) {
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +0900680 double Yk = Y[k];
681 M[k] += Yk * X;
682 double *H2 = &H[k * wiener_win2];
683 H2[k] += Yk * Yk;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700684 for (l = k + 1; l < wiener_win2; ++l) {
David Barker33f3bfd2017-01-06 15:34:50 +0000685 // H is a symmetric matrix, so we only need to fill out the upper
686 // triangle here. We can copy it down to the lower triangle outside
687 // the (i, j) loops.
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +0900688 H2[l] += Yk * Y[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689 }
690 }
691 }
692 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700693 for (k = 0; k < wiener_win2; ++k) {
694 for (l = k + 1; l < wiener_win2; ++l) {
695 H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
David Barker33f3bfd2017-01-06 15:34:50 +0000696 }
697 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700698}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700699
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700700static INLINE int wrap_index(int i, int wiener_win) {
701 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
702 return (i >= wiener_halfwin1 ? wiener_win - 1 - i : i);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703}
704
705// Fix vector b, update vector a
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700706static void update_a_sep_sym(int wiener_win, double **Mc, double **Hc,
707 double *a, double *b) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700708 int i, j;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800709 double S[WIENER_WIN];
Debargha Mukherjee6ae588f2017-04-14 00:40:02 -0700710 double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700711 const int wiener_win2 = wiener_win * wiener_win;
712 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700713 memset(A, 0, sizeof(A));
714 memset(B, 0, sizeof(B));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700715 for (i = 0; i < wiener_win; i++) {
716 for (j = 0; j < wiener_win; ++j) {
717 const int jj = wrap_index(j, wiener_win);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718 A[jj] += Mc[i][j] * b[i];
719 }
720 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700721 for (i = 0; i < wiener_win; i++) {
722 for (j = 0; j < wiener_win; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723 int k, l;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700724 for (k = 0; k < wiener_win; ++k)
725 for (l = 0; l < wiener_win; ++l) {
726 const int kk = wrap_index(k, wiener_win);
727 const int ll = wrap_index(l, wiener_win);
728 B[ll * wiener_halfwin1 + kk] +=
729 Hc[j * wiener_win + i][k * wiener_win2 + l] * b[i] * b[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730 }
731 }
732 }
733 // Normalization enforcement in the system of equations itself
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700734 for (i = 0; i < wiener_halfwin1 - 1; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735 A[i] -=
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700736 A[wiener_halfwin1 - 1] * 2 +
737 B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
738 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
739 for (i = 0; i < wiener_halfwin1 - 1; ++i)
740 for (j = 0; j < wiener_halfwin1 - 1; ++j)
741 B[i * wiener_halfwin1 + j] -=
742 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
743 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
744 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
745 (wiener_halfwin1 - 1)]);
746 if (linsolve(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
747 S[wiener_halfwin1 - 1] = 1.0;
748 for (i = wiener_halfwin1; i < wiener_win; ++i) {
749 S[i] = S[wiener_win - 1 - i];
750 S[wiener_halfwin1 - 1] -= 2 * S[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700751 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700752 memcpy(a, S, wiener_win * sizeof(*a));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700753 }
754}
755
756// Fix vector a, update vector b
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700757static void update_b_sep_sym(int wiener_win, double **Mc, double **Hc,
758 double *a, double *b) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700759 int i, j;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800760 double S[WIENER_WIN];
Debargha Mukherjee6ae588f2017-04-14 00:40:02 -0700761 double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700762 const int wiener_win2 = wiener_win * wiener_win;
763 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700764 memset(A, 0, sizeof(A));
765 memset(B, 0, sizeof(B));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700766 for (i = 0; i < wiener_win; i++) {
767 const int ii = wrap_index(i, wiener_win);
768 for (j = 0; j < wiener_win; j++) A[ii] += Mc[i][j] * a[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700769 }
770
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700771 for (i = 0; i < wiener_win; i++) {
772 for (j = 0; j < wiener_win; j++) {
773 const int ii = wrap_index(i, wiener_win);
774 const int jj = wrap_index(j, wiener_win);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700775 int k, l;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700776 for (k = 0; k < wiener_win; ++k)
777 for (l = 0; l < wiener_win; ++l)
778 B[jj * wiener_halfwin1 + ii] +=
779 Hc[i * wiener_win + j][k * wiener_win2 + l] * a[k] * a[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780 }
781 }
782 // Normalization enforcement in the system of equations itself
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700783 for (i = 0; i < wiener_halfwin1 - 1; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700784 A[i] -=
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700785 A[wiener_halfwin1 - 1] * 2 +
786 B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
787 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
788 for (i = 0; i < wiener_halfwin1 - 1; ++i)
789 for (j = 0; j < wiener_halfwin1 - 1; ++j)
790 B[i * wiener_halfwin1 + j] -=
791 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
792 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
793 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
794 (wiener_halfwin1 - 1)]);
795 if (linsolve(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
796 S[wiener_halfwin1 - 1] = 1.0;
797 for (i = wiener_halfwin1; i < wiener_win; ++i) {
798 S[i] = S[wiener_win - 1 - i];
799 S[wiener_halfwin1 - 1] -= 2 * S[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700801 memcpy(b, S, wiener_win * sizeof(*b));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802 }
803}
804
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700805static int wiener_decompose_sep_sym(int wiener_win, double *M, double *H,
806 double *a, double *b) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700807 static const int init_filt[WIENER_WIN] = {
808 WIENER_FILT_TAP0_MIDV, WIENER_FILT_TAP1_MIDV, WIENER_FILT_TAP2_MIDV,
809 WIENER_FILT_TAP3_MIDV, WIENER_FILT_TAP2_MIDV, WIENER_FILT_TAP1_MIDV,
810 WIENER_FILT_TAP0_MIDV,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700811 };
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800812 double *Hc[WIENER_WIN2];
813 double *Mc[WIENER_WIN];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700814 int i, j, iter;
815 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
816 const int wiener_win2 = wiener_win * wiener_win;
817 for (i = 0; i < wiener_win; i++) {
818 a[i] = b[i] = (double)init_filt[i + plane_off] / WIENER_FILT_STEP;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700819 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700820 for (i = 0; i < wiener_win; i++) {
821 Mc[i] = M + i * wiener_win;
822 for (j = 0; j < wiener_win; j++) {
823 Hc[i * wiener_win + j] =
824 H + i * wiener_win * wiener_win2 + j * wiener_win;
825 }
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700826 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700827
828 iter = 1;
Debargha Mukherjee1b3dbf02017-03-13 14:47:21 -0700829 while (iter < NUM_WIENER_ITERS) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700830 update_a_sep_sym(wiener_win, Mc, Hc, a, b);
831 update_b_sep_sym(wiener_win, Mc, Hc, a, b);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700832 iter++;
833 }
834 return 1;
835}
836
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800837// Computes the function x'*H*x - x'*M for the learned 2D filter x, and compares
Yaowu Xuc27fc142016-08-22 16:08:15 -0700838// against identity filters; Final score is defined as the difference between
839// the function values
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700840static double compute_score(int wiener_win, double *M, double *H,
841 InterpKernel vfilt, InterpKernel hfilt) {
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800842 double ab[WIENER_WIN * WIENER_WIN];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843 int i, k, l;
844 double P = 0, Q = 0;
845 double iP = 0, iQ = 0;
846 double Score, iScore;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800847 double a[WIENER_WIN], b[WIENER_WIN];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700848 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
849 const int wiener_win2 = wiener_win * wiener_win;
Jingning Han041c67b2017-04-14 21:39:26 -0700850
851 aom_clear_system_state();
852
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800853 a[WIENER_HALFWIN] = b[WIENER_HALFWIN] = 1.0;
854 for (i = 0; i < WIENER_HALFWIN; ++i) {
855 a[i] = a[WIENER_WIN - i - 1] = (double)vfilt[i] / WIENER_FILT_STEP;
856 b[i] = b[WIENER_WIN - i - 1] = (double)hfilt[i] / WIENER_FILT_STEP;
857 a[WIENER_HALFWIN] -= 2 * a[i];
858 b[WIENER_HALFWIN] -= 2 * b[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700860 memset(ab, 0, sizeof(ab));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700861 for (k = 0; k < wiener_win; ++k) {
862 for (l = 0; l < wiener_win; ++l)
863 ab[k * wiener_win + l] = a[l + plane_off] * b[k + plane_off];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700864 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700865 for (k = 0; k < wiener_win2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700866 P += ab[k] * M[k];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700867 for (l = 0; l < wiener_win2; ++l)
868 Q += ab[k] * H[k * wiener_win2 + l] * ab[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700869 }
870 Score = Q - 2 * P;
871
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700872 iP = M[wiener_win2 >> 1];
873 iQ = H[(wiener_win2 >> 1) * wiener_win2 + (wiener_win2 >> 1)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874 iScore = iQ - 2 * iP;
875
876 return Score - iScore;
877}
878
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700879static void quantize_sym_filter(int wiener_win, double *f, InterpKernel fi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700880 int i;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700881 const int wiener_halfwin = (wiener_win >> 1);
882 for (i = 0; i < wiener_halfwin; ++i) {
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800883 fi[i] = RINT(f[i] * WIENER_FILT_STEP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700884 }
885 // Specialize for 7-tap filter
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700886 if (wiener_win == WIENER_WIN) {
887 fi[0] = CLIP(fi[0], WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP0_MAXV);
888 fi[1] = CLIP(fi[1], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
889 fi[2] = CLIP(fi[2], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
890 } else {
891 fi[2] = CLIP(fi[1], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
892 fi[1] = CLIP(fi[0], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
893 fi[0] = 0;
894 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800895 // Satisfy filter constraints
896 fi[WIENER_WIN - 1] = fi[0];
897 fi[WIENER_WIN - 2] = fi[1];
898 fi[WIENER_WIN - 3] = fi[2];
David Barker1e8e6b92017-01-13 13:45:51 +0000899 // The central element has an implicit +WIENER_FILT_STEP
900 fi[3] = -2 * (fi[0] + fi[1] + fi[2]);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800901}
902
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700903static int count_wiener_bits(int wiener_win, WienerInfo *wiener_info,
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700904 WienerInfo *ref_wiener_info) {
905 int bits = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700906 if (wiener_win == WIENER_WIN)
907 bits += aom_count_primitive_refsubexpfin(
908 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
909 WIENER_FILT_TAP0_SUBEXP_K,
910 ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV,
911 wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700912 bits += aom_count_primitive_refsubexpfin(
913 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
914 WIENER_FILT_TAP1_SUBEXP_K,
915 ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV,
916 wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV);
917 bits += aom_count_primitive_refsubexpfin(
918 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
919 WIENER_FILT_TAP2_SUBEXP_K,
920 ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV,
921 wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700922 if (wiener_win == WIENER_WIN)
923 bits += aom_count_primitive_refsubexpfin(
924 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
925 WIENER_FILT_TAP0_SUBEXP_K,
926 ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV,
927 wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700928 bits += aom_count_primitive_refsubexpfin(
929 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
930 WIENER_FILT_TAP1_SUBEXP_K,
931 ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV,
932 wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV);
933 bits += aom_count_primitive_refsubexpfin(
934 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
935 WIENER_FILT_TAP2_SUBEXP_K,
936 ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV,
937 wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV);
938 return bits;
939}
940
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700941#define USE_WIENER_REFINEMENT_SEARCH 1
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000942static int64_t finer_tile_search_wiener(const RestSearchCtxt *rsc,
943 const RestorationTileLimits *limits,
944 const AV1PixelRect *tile,
945 RestorationUnitInfo *rui,
946 int wiener_win) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700947 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
Urvang Joshi813186b2018-03-08 15:38:46 -0800948 int64_t err = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700949#if USE_WIENER_REFINEMENT_SEARCH
950 int64_t err2;
951 int tap_min[] = { WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP1_MINV,
952 WIENER_FILT_TAP2_MINV };
953 int tap_max[] = { WIENER_FILT_TAP0_MAXV, WIENER_FILT_TAP1_MAXV,
954 WIENER_FILT_TAP2_MAXV };
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100955
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100956 WienerInfo *plane_wiener = &rui->wiener_info;
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100957
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700958 // printf("err pre = %"PRId64"\n", err);
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000959 const int start_step = 4;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700960 for (int s = start_step; s >= 1; s >>= 1) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700961 for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700962 int skip = 0;
963 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100964 if (plane_wiener->hfilter[p] - s >= tap_min[p]) {
965 plane_wiener->hfilter[p] -= s;
966 plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
967 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -0800968 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700969 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100970 plane_wiener->hfilter[p] += s;
971 plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
972 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700973 } else {
974 err = err2;
975 skip = 1;
976 // At the highest step size continue moving in the same direction
977 if (s == start_step) continue;
978 }
979 }
980 break;
981 } while (1);
982 if (skip) break;
983 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100984 if (plane_wiener->hfilter[p] + s <= tap_max[p]) {
985 plane_wiener->hfilter[p] += s;
986 plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
987 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -0800988 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700989 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100990 plane_wiener->hfilter[p] -= s;
991 plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
992 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700993 } else {
994 err = err2;
995 // At the highest step size continue moving in the same direction
996 if (s == start_step) continue;
997 }
998 }
999 break;
1000 } while (1);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -07001001 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -07001002 for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001003 int skip = 0;
1004 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001005 if (plane_wiener->vfilter[p] - s >= tap_min[p]) {
1006 plane_wiener->vfilter[p] -= s;
1007 plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
1008 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -08001009 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001010 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001011 plane_wiener->vfilter[p] += s;
1012 plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
1013 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001014 } else {
1015 err = err2;
1016 skip = 1;
1017 // At the highest step size continue moving in the same direction
1018 if (s == start_step) continue;
1019 }
1020 }
1021 break;
1022 } while (1);
1023 if (skip) break;
1024 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001025 if (plane_wiener->vfilter[p] + s <= tap_max[p]) {
1026 plane_wiener->vfilter[p] += s;
1027 plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
1028 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -08001029 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001030 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001031 plane_wiener->vfilter[p] -= s;
1032 plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
1033 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001034 } else {
1035 err = err2;
1036 // At the highest step size continue moving in the same direction
1037 if (s == start_step) continue;
1038 }
1039 }
1040 break;
1041 } while (1);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -07001042 }
1043 }
1044// printf("err post = %"PRId64"\n", err);
1045#endif // USE_WIENER_REFINEMENT_SEARCH
1046 return err;
1047}
1048
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001049static void search_wiener(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001050 const AV1PixelRect *tile_rect, int rest_unit_idx,
1051 void *priv) {
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001052 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1053 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1054
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001055 const int wiener_win =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001056 (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001057
1058 double M[WIENER_WIN2];
1059 double H[WIENER_WIN2 * WIENER_WIN2];
1060 double vfilterd[WIENER_WIN], hfilterd[WIENER_WIN];
1061
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001062 const AV1_COMMON *const cm = rsc->cm;
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +09001063 if (cm->use_highbitdepth) {
1064 if (rsc->plane == AOM_PLANE_Y) {
1065 compute_stats_highbd(WIENER_WIN, rsc->dgd_buffer, rsc->src_buffer,
1066 limits->h_start, limits->h_end, limits->v_start,
1067 limits->v_end, rsc->dgd_stride, rsc->src_stride, M,
1068 H);
1069 } else {
1070 compute_stats_highbd(WIENER_WIN_CHROMA, rsc->dgd_buffer, rsc->src_buffer,
1071 limits->h_start, limits->h_end, limits->v_start,
1072 limits->v_end, rsc->dgd_stride, rsc->src_stride, M,
1073 H);
1074 }
1075 } else {
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001076 compute_stats(wiener_win, rsc->dgd_buffer, rsc->src_buffer, limits->h_start,
1077 limits->h_end, limits->v_start, limits->v_end,
1078 rsc->dgd_stride, rsc->src_stride, M, H);
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +09001079 }
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001080
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001081 const MACROBLOCK *const x = rsc->x;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001082 const int64_t bits_none = x->wiener_restore_cost[0];
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001083
1084 if (!wiener_decompose_sep_sym(wiener_win, M, H, vfilterd, hfilterd)) {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001085 rsc->bits += bits_none;
1086 rsc->sse += rusi->sse[RESTORE_NONE];
1087 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1088 rusi->sse[RESTORE_WIENER] = INT64_MAX;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001089 return;
1090 }
1091
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001092 RestorationUnitInfo rui;
1093 memset(&rui, 0, sizeof(rui));
1094 rui.restoration_type = RESTORE_WIENER;
1095 quantize_sym_filter(wiener_win, vfilterd, rui.wiener_info.vfilter);
1096 quantize_sym_filter(wiener_win, hfilterd, rui.wiener_info.hfilter);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001097
1098 // Filter score computes the value of the function x'*A*x - x'*b for the
1099 // learned filter and compares it against identity filer. If there is no
1100 // reduction in the function, the filter is reverted back to identity
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001101 if (compute_score(wiener_win, M, H, rui.wiener_info.vfilter,
1102 rui.wiener_info.hfilter) > 0) {
1103 rsc->bits += bits_none;
1104 rsc->sse += rusi->sse[RESTORE_NONE];
1105 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1106 rusi->sse[RESTORE_WIENER] = INT64_MAX;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001107 return;
1108 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001109
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001110 aom_clear_system_state();
1111
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001112 rusi->sse[RESTORE_WIENER] =
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001113 finer_tile_search_wiener(rsc, limits, tile_rect, &rui, wiener_win);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001114 rusi->wiener = rui.wiener_info;
1115
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001116 if (wiener_win != WIENER_WIN) {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001117 assert(rui.wiener_info.vfilter[0] == 0 &&
1118 rui.wiener_info.vfilter[WIENER_WIN - 1] == 0);
1119 assert(rui.wiener_info.hfilter[0] == 0 &&
1120 rui.wiener_info.hfilter[WIENER_WIN - 1] == 0);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001121 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001122
1123 const int64_t bits_wiener =
1124 x->wiener_restore_cost[1] +
1125 (count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener)
1126 << AV1_PROB_COST_SHIFT);
1127
1128 double cost_none =
1129 RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
1130 double cost_wiener =
1131 RDCOST_DBL(x->rdmult, bits_wiener >> 4, rusi->sse[RESTORE_WIENER]);
1132
1133 RestorationType rtype =
1134 (cost_wiener < cost_none) ? RESTORE_WIENER : RESTORE_NONE;
1135 rusi->best_rtype[RESTORE_WIENER - 1] = rtype;
1136
1137 rsc->sse += rusi->sse[rtype];
1138 rsc->bits += (cost_wiener < cost_none) ? bits_wiener : bits_none;
1139 if (cost_wiener < cost_none) rsc->wiener = rusi->wiener;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001140}
1141
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001142static void search_norestore(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001143 const AV1PixelRect *tile_rect, int rest_unit_idx,
1144 void *priv) {
1145 (void)tile_rect;
1146
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001147 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1148 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1149
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001150 const int highbd = rsc->cm->use_highbitdepth;
Urvang Joshi813186b2018-03-08 15:38:46 -08001151 rusi->sse[RESTORE_NONE] = sse_restoration_unit(
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001152 limits, rsc->src, rsc->cm->frame_to_show, rsc->plane, highbd);
Rupert Swarbrick64b8bbd2017-10-16 15:53:07 +01001153
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001154 rsc->sse += rusi->sse[RESTORE_NONE];
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001155}
1156
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001157static void search_switchable(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001158 const AV1PixelRect *tile_rect, int rest_unit_idx,
1159 void *priv) {
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +01001160 (void)limits;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001161 (void)tile_rect;
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001162 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1163 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001164
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001165 const MACROBLOCK *const x = rsc->x;
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001166
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001167 const int wiener_win =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001168 (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001169
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001170 double best_cost = 0;
1171 int64_t best_bits = 0;
1172 RestorationType best_rtype = RESTORE_NONE;
1173
1174 for (RestorationType r = 0; r < RESTORE_SWITCHABLE_TYPES; ++r) {
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001175 // Check for the condition that wiener or sgrproj search could not
1176 // find a solution or the solution was worse than RESTORE_NONE.
1177 // In either case the best_rtype will be set as RESTORE_NONE. These
1178 // should be skipped from the test below.
1179 if (r > RESTORE_NONE) {
1180 if (rusi->best_rtype[r - 1] == RESTORE_NONE) continue;
1181 }
1182
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001183 const int64_t sse = rusi->sse[r];
1184 int64_t coeff_pcost = 0;
1185 switch (r) {
1186 case RESTORE_NONE: coeff_pcost = 0; break;
1187 case RESTORE_WIENER:
1188 coeff_pcost =
1189 count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener);
1190 break;
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001191 case RESTORE_SGRPROJ:
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001192 coeff_pcost = count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj);
1193 break;
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001194 default: assert(0); break;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001195 }
1196 const int64_t coeff_bits = coeff_pcost << AV1_PROB_COST_SHIFT;
1197 const int64_t bits = x->switchable_restore_cost[r] + coeff_bits;
1198 double cost = RDCOST_DBL(x->rdmult, bits >> 4, sse);
Debargha Mukherjeeda394fb2018-05-18 09:05:16 -07001199 if (r == RESTORE_SGRPROJ && rusi->sgrproj.ep < 10)
1200 cost *= (1 + DUAL_SGR_PENALTY_MULT * rsc->sf->dual_sgr_penalty_level);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001201 if (r == 0 || cost < best_cost) {
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001202 best_cost = cost;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001203 best_bits = bits;
1204 best_rtype = r;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001205 }
1206 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001207
1208 rusi->best_rtype[RESTORE_SWITCHABLE - 1] = best_rtype;
1209
1210 rsc->sse += rusi->sse[best_rtype];
1211 rsc->bits += best_bits;
1212 if (best_rtype == RESTORE_WIENER) rsc->wiener = rusi->wiener;
1213 if (best_rtype == RESTORE_SGRPROJ) rsc->sgrproj = rusi->sgrproj;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001214}
1215
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001216static void copy_unit_info(RestorationType frame_rtype,
1217 const RestUnitSearchInfo *rusi,
1218 RestorationUnitInfo *rui) {
1219 assert(frame_rtype > 0);
1220 rui->restoration_type = rusi->best_rtype[frame_rtype - 1];
1221 if (rui->restoration_type == RESTORE_WIENER)
1222 rui->wiener_info = rusi->wiener;
1223 else
1224 rui->sgrproj_info = rusi->sgrproj;
1225}
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001226
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001227static double search_rest_type(RestSearchCtxt *rsc, RestorationType rtype) {
1228 static const rest_unit_visitor_t funs[RESTORE_TYPES] = {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001229 search_norestore, search_wiener, search_sgrproj, search_switchable
1230 };
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001231
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001232 reset_rsc(rsc);
1233 av1_foreach_rest_unit_in_frame(rsc->cm, rsc->plane, rsc_on_tile, funs[rtype],
1234 rsc);
1235 return RDCOST_DBL(rsc->x->rdmult, rsc->bits >> 4, rsc->sse);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001236}
1237
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001238static int rest_tiles_in_plane(const AV1_COMMON *cm, int plane) {
1239 const RestorationInfo *rsi = &cm->rst_info[plane];
David Barkeree0ae202018-03-28 16:58:57 +01001240 return rsi->units_per_tile;
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001241}
1242
Rupert Swarbrick146a0602017-10-17 16:52:20 +01001243void av1_pick_filter_restoration(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001244 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001245 const int num_planes = av1_num_planes(cm);
Urvang Joshi14072aa2018-03-21 17:43:36 -07001246 assert(!cm->all_lossless);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001247
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001248 int ntiles[2];
1249 for (int is_uv = 0; is_uv < 2; ++is_uv)
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001250 ntiles[is_uv] = rest_tiles_in_plane(cm, is_uv);
Debargha Mukherjeed48f5732017-05-19 14:58:07 -07001251
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001252 assert(ntiles[1] <= ntiles[0]);
1253 RestUnitSearchInfo *rusi =
Yaowu Xu43e30f42017-11-27 14:26:00 -08001254 (RestUnitSearchInfo *)aom_memalign(16, sizeof(*rusi) * ntiles[0]);
Debargha Mukherjeed48f5732017-05-19 14:58:07 -07001255
Imdad Sardharwallab08544d2018-01-23 12:45:31 +00001256 // If the restoration unit dimensions are not multiples of
1257 // rsi->restoration_unit_size then some elements of the rusi array may be
1258 // left uninitialised when we reach copy_unit_info(...). This is not a
1259 // problem, as these elements are ignored later, but in order to quiet
1260 // Valgrind's warnings we initialise the array below.
1261 memset(rusi, 0, sizeof(*rusi) * ntiles[0]);
1262
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001263 RestSearchCtxt rsc;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001264 const int plane_start = AOM_PLANE_Y;
1265 const int plane_end = num_planes > 1 ? AOM_PLANE_V : AOM_PLANE_Y;
1266 for (int plane = plane_start; plane <= plane_end; ++plane) {
Debargha Mukherjeeda394fb2018-05-18 09:05:16 -07001267 init_rsc(src, &cpi->common, &cpi->td.mb, &cpi->sf, plane, rusi,
1268 &cpi->trial_frame_rst, &rsc);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001269
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001270 const int plane_ntiles = ntiles[plane > 0];
1271 const RestorationType num_rtypes =
1272 (plane_ntiles > 1) ? RESTORE_TYPES : RESTORE_SWITCHABLE_TYPES;
1273
1274 double best_cost = 0;
1275 RestorationType best_rtype = RESTORE_NONE;
1276
Rupert Swarbrick5b401362017-10-31 10:56:27 +00001277 const int highbd = rsc.cm->use_highbitdepth;
Rupert Swarbrick5b401362017-10-31 10:56:27 +00001278 extend_frame(rsc.dgd_buffer, rsc.plane_width, rsc.plane_height,
1279 rsc.dgd_stride, RESTORATION_BORDER, RESTORATION_BORDER,
1280 highbd);
1281
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001282 for (RestorationType r = 0; r < num_rtypes; ++r) {
1283 if ((force_restore_type != RESTORE_TYPES) && (r != RESTORE_NONE) &&
1284 (r != force_restore_type))
1285 continue;
1286
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001287 double cost = search_rest_type(&rsc, r);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001288
1289 if (r == 0 || cost < best_cost) {
1290 best_cost = cost;
1291 best_rtype = r;
1292 }
1293 }
1294
1295 cm->rst_info[plane].frame_restoration_type = best_rtype;
1296 if (force_restore_type != RESTORE_TYPES)
1297 assert(best_rtype == force_restore_type || best_rtype == RESTORE_NONE);
1298
1299 if (best_rtype != RESTORE_NONE) {
1300 for (int u = 0; u < plane_ntiles; ++u) {
1301 copy_unit_info(best_rtype, &rusi[u], &cm->rst_info[plane].unit_info[u]);
1302 }
1303 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001304 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001305
1306 aom_free(rusi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001307}