blob: 86bfe040b3c91347cdf27bf4ac8383bc97f83668 [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
Tom Finegan44702c82018-05-22 13:00:39 -070017#include "config/aom_scale_rtcd.h"
Xing Jin37ee03b2018-07-25 10:15:42 +080018#include "config/av1_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019
Yaowu Xuf883b422016-08-30 14:01:10 -070020#include "aom_dsp/aom_dsp_common.h"
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -070021#include "aom_dsp/binary_codes_writer.h"
22#include "aom_dsp/psnr.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070023#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070024#include "aom_ports/mem.h"
Jingning Han041c67b2017-04-14 21:39:26 -070025#include "aom_ports/system_state.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070026#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;
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +0530113 AV1PixelRect tile_rect;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000114} RestSearchCtxt;
115
116static void rsc_on_tile(int tile_row, int tile_col, void *priv) {
117 (void)tile_col;
118
119 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
120 set_default_sgrproj(&rsc->sgrproj);
121 set_default_wiener(&rsc->wiener);
122
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000123 rsc->tile_stripe0 =
124 (tile_row == 0) ? 0 : rsc->cm->rst_end_stripe[tile_row - 1];
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000125}
126
127static void reset_rsc(RestSearchCtxt *rsc) {
128 rsc->sse = 0;
129 rsc->bits = 0;
130}
131
132static void init_rsc(const YV12_BUFFER_CONFIG *src, const AV1_COMMON *cm,
Debargha Mukherjeeda394fb2018-05-18 09:05:16 -0700133 const MACROBLOCK *x, const SPEED_FEATURES *sf, int plane,
134 RestUnitSearchInfo *rusi, YV12_BUFFER_CONFIG *dst,
135 RestSearchCtxt *rsc) {
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000136 rsc->src = src;
137 rsc->dst = dst;
138 rsc->cm = cm;
139 rsc->x = x;
140 rsc->plane = plane;
141 rsc->rusi = rusi;
Debargha Mukherjeeda394fb2018-05-18 09:05:16 -0700142 rsc->sf = sf;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000143
144 const YV12_BUFFER_CONFIG *dgd = cm->frame_to_show;
145 const int is_uv = plane != AOM_PLANE_Y;
146 rsc->plane_width = src->crop_widths[is_uv];
David Barker21f43072017-12-14 14:21:45 +0000147 rsc->plane_height = src->crop_heights[is_uv];
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000148 rsc->src_buffer = src->buffers[plane];
149 rsc->src_stride = src->strides[is_uv];
150 rsc->dgd_buffer = dgd->buffers[plane];
151 rsc->dgd_stride = dgd->strides[is_uv];
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +0530152 rsc->tile_rect = av1_whole_frame_rect(cm, is_uv);
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000153 assert(src->crop_widths[is_uv] == dgd->crop_widths[is_uv]);
154 assert(src->crop_heights[is_uv] == dgd->crop_heights[is_uv]);
155}
156
Urvang Joshi813186b2018-03-08 15:38:46 -0800157static int64_t try_restoration_unit(const RestSearchCtxt *rsc,
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100158 const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000159 const AV1PixelRect *tile_rect,
160 const RestorationUnitInfo *rui) {
161 const AV1_COMMON *const cm = rsc->cm;
162 const int plane = rsc->plane;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100163 const int is_uv = plane > 0;
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000164 const RestorationInfo *rsi = &cm->rst_info[plane];
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100165 RestorationLineBuffers rlbs;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700166 const int bit_depth = cm->seq_params.bit_depth;
167 const int highbd = cm->seq_params.use_highbitdepth;
Rupert Swarbrick64b8bbd2017-10-16 15:53:07 +0100168
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100169 const YV12_BUFFER_CONFIG *fts = cm->frame_to_show;
Yunqing Wange8025032018-04-10 18:50:29 -0700170 // TODO(yunqing): For now, only use optimized LR filter in decoder. Can be
171 // also used in encoder.
Yunqing Wang2ff71af2018-04-24 15:10:10 -0700172 const int optimized_lr = 0;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700173
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000174 av1_loop_restoration_filter_unit(
Debargha Mukherjee5105f7a2018-01-29 16:05:54 -0800175 limits, rui, &rsi->boundaries, &rlbs, tile_rect, rsc->tile_stripe0,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700176 is_uv && cm->seq_params.subsampling_x,
177 is_uv && cm->seq_params.subsampling_y, highbd, bit_depth,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000178 fts->buffers[plane], fts->strides[is_uv], rsc->dst->buffers[plane],
Yunqing Wang2ff71af2018-04-24 15:10:10 -0700179 rsc->dst->strides[is_uv], cm->rst_tmpbuf, optimized_lr);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700180
Urvang Joshi813186b2018-03-08 15:38:46 -0800181 return sse_restoration_unit(limits, rsc->src, rsc->dst, plane, highbd);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700182}
183
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100184static int64_t get_pixel_proj_error(const uint8_t *src8, int width, int height,
185 int src_stride, const uint8_t *dat8,
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100186 int dat_stride, int use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000187 int32_t *flt0, int flt0_stride,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700188 int32_t *flt1, int flt1_stride, int *xqd,
189 const sgr_params_type *params) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700190 int i, j;
191 int64_t err = 0;
192 int xq[2];
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000193 decode_xq(xqd, xq, params);
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100194 if (!use_highbitdepth) {
David Barker3a0df182016-12-21 10:44:52 +0000195 const uint8_t *src = src8;
196 const uint8_t *dat = dat8;
197 for (i = 0; i < height; ++i) {
198 for (j = 0; j < width; ++j) {
199 const int32_t u =
200 (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000201 int32_t v = u << SGRPROJ_PRJ_BITS;
Urvang Joshi3715b882018-05-14 20:05:25 -0400202 if (params->r[0] > 0) v += xq[0] * (flt0[i * flt0_stride + j] - u);
203 if (params->r[1] > 0) v += xq[1] * (flt1[i * flt1_stride + j] - u);
David Barker3a0df182016-12-21 10:44:52 +0000204 const int32_t e =
205 ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) -
206 src[i * src_stride + j];
207 err += e * e;
208 }
209 }
210 } else {
211 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
212 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900213 const int32_t half = 1 << (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS - 1);
Urvang Joshi3715b882018-05-14 20:05:25 -0400214 if (params->r[0] > 0 && params->r[1] > 0) {
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900215 int xq0 = xq[0];
216 int xq1 = xq[1];
217 for (i = 0; i < height; ++i) {
218 for (j = 0; j < width; ++j) {
219 const int32_t d = dat[j];
220 const int32_t s = src[j];
221 const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
222 int32_t v0 = flt0[j] - u;
223 int32_t v1 = flt1[j] - u;
224 int32_t v = half;
225 v += xq0 * v0;
226 v += xq1 * v1;
227 const int32_t e =
228 (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
229 err += e * e;
230 }
231 dat += dat_stride;
232 flt0 += flt0_stride;
233 flt1 += flt1_stride;
234 src += src_stride;
235 }
Urvang Joshi3715b882018-05-14 20:05:25 -0400236 } else if (params->r[0] > 0 || params->r[1] > 0) {
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900237 int exq;
238 int32_t *flt;
239 int flt_stride;
Urvang Joshi3715b882018-05-14 20:05:25 -0400240 if (params->r[0] > 0) {
Katsuhisa Yuasace7ded92018-04-23 01:11:57 +0900241 exq = xq[0];
242 flt = flt0;
243 flt_stride = flt0_stride;
244 } else {
245 exq = xq[1];
246 flt = flt1;
247 flt_stride = flt1_stride;
248 }
249 for (i = 0; i < height; ++i) {
250 for (j = 0; j < width; ++j) {
251 const int32_t d = dat[j];
252 const int32_t s = src[j];
253 const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
254 int32_t v = half;
255 v += exq * (flt[j] - u);
256 const int32_t e =
257 (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
258 err += e * e;
259 }
260 dat += dat_stride;
261 flt += flt_stride;
262 src += src_stride;
263 }
264 } else {
265 for (i = 0; i < height; ++i) {
266 for (j = 0; j < width; ++j) {
267 const int32_t d = dat[j];
268 const int32_t s = src[j];
269 const int32_t e = d - s;
270 err += e * e;
271 }
272 dat += dat_stride;
273 src += src_stride;
David Barker3a0df182016-12-21 10:44:52 +0000274 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700275 }
276 }
277 return err;
278}
279
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700280#define USE_SGRPROJ_REFINEMENT_SEARCH 1
281static int64_t finer_search_pixel_proj_error(
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100282 const uint8_t *src8, int width, int height, int src_stride,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000283 const uint8_t *dat8, int dat_stride, int use_highbitdepth, int32_t *flt0,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700284 int flt0_stride, int32_t *flt1, int flt1_stride, int start_step, int *xqd,
285 const sgr_params_type *params) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000286 int64_t err = get_pixel_proj_error(
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000287 src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth, flt0,
288 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700289 (void)start_step;
290#if USE_SGRPROJ_REFINEMENT_SEARCH
291 int64_t err2;
292 int tap_min[] = { SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MIN1 };
293 int tap_max[] = { SGRPROJ_PRJ_MAX0, SGRPROJ_PRJ_MAX1 };
294 for (int s = start_step; s >= 1; s >>= 1) {
295 for (int p = 0; p < 2; ++p) {
Urvang Joshi3715b882018-05-14 20:05:25 -0400296 if ((params->r[0] == 0 && p == 0) || (params->r[1] == 0 && p == 1)) {
297 continue;
298 }
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700299 int skip = 0;
300 do {
301 if (xqd[p] - s >= tap_min[p]) {
302 xqd[p] -= s;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000303 err2 =
304 get_pixel_proj_error(src8, width, height, src_stride, dat8,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000305 dat_stride, use_highbitdepth, flt0,
306 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700307 if (err2 > err) {
308 xqd[p] += s;
309 } else {
310 err = err2;
311 skip = 1;
312 // At the highest step size continue moving in the same direction
313 if (s == start_step) continue;
314 }
315 }
316 break;
317 } while (1);
318 if (skip) break;
319 do {
320 if (xqd[p] + s <= tap_max[p]) {
321 xqd[p] += s;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000322 err2 =
323 get_pixel_proj_error(src8, width, height, src_stride, dat8,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000324 dat_stride, use_highbitdepth, flt0,
325 flt0_stride, flt1, flt1_stride, xqd, params);
Debargha Mukherjee749f5cd2017-05-31 11:26:51 -0700326 if (err2 > err) {
327 xqd[p] -= s;
328 } else {
329 err = err2;
330 // At the highest step size continue moving in the same direction
331 if (s == start_step) continue;
332 }
333 }
334 break;
335 } while (1);
336 }
337 }
338#endif // USE_SGRPROJ_REFINEMENT_SEARCH
339 return err;
340}
341
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100342static void get_proj_subspace(const uint8_t *src8, int width, int height,
David Barkerbfbd8b32017-11-01 12:34:23 +0000343 int src_stride, const uint8_t *dat8,
344 int dat_stride, int use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000345 int32_t *flt0, int flt0_stride, int32_t *flt1,
Debargha Mukherjee25afc9b2018-03-27 10:45:19 -0700346 int flt1_stride, int *xq,
347 const sgr_params_type *params) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700348 int i, j;
349 double H[2][2] = { { 0, 0 }, { 0, 0 } };
350 double C[2] = { 0, 0 };
351 double Det;
352 double x[2];
353 const int size = width * height;
354
Jingning Han041c67b2017-04-14 21:39:26 -0700355 aom_clear_system_state();
356
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800357 // Default
358 xq[0] = 0;
359 xq[1] = 0;
Rupert Swarbrick32d150b2017-09-04 10:35:51 +0100360 if (!use_highbitdepth) {
David Barker3a0df182016-12-21 10:44:52 +0000361 const uint8_t *src = src8;
362 const uint8_t *dat = dat8;
363 for (i = 0; i < height; ++i) {
364 for (j = 0; j < width; ++j) {
365 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
366 const double s =
367 (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000368 const double f1 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400369 (params->r[0] > 0) ? (double)flt0[i * flt0_stride + j] - u : 0;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000370 const double f2 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400371 (params->r[1] > 0) ? (double)flt1[i * flt1_stride + j] - u : 0;
David Barker3a0df182016-12-21 10:44:52 +0000372 H[0][0] += f1 * f1;
373 H[1][1] += f2 * f2;
374 H[0][1] += f1 * f2;
375 C[0] += f1 * s;
376 C[1] += f2 * s;
377 }
378 }
379 } else {
380 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
381 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
382 for (i = 0; i < height; ++i) {
383 for (j = 0; j < width; ++j) {
384 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
385 const double s =
386 (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000387 const double f1 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400388 (params->r[0] > 0) ? (double)flt0[i * flt0_stride + j] - u : 0;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000389 const double f2 =
Urvang Joshi3715b882018-05-14 20:05:25 -0400390 (params->r[1] > 0) ? (double)flt1[i * flt1_stride + j] - u : 0;
David Barker3a0df182016-12-21 10:44:52 +0000391 H[0][0] += f1 * f1;
392 H[1][1] += f2 * f2;
393 H[0][1] += f1 * f2;
394 C[0] += f1 * s;
395 C[1] += f2 * s;
396 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700397 }
398 }
399 H[0][0] /= size;
400 H[0][1] /= size;
401 H[1][1] /= size;
402 H[1][0] = H[0][1];
403 C[0] /= size;
404 C[1] /= size;
Urvang Joshi3715b882018-05-14 20:05:25 -0400405 if (params->r[0] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000406 // H matrix is now only the scalar H[1][1]
407 // C vector is now only the scalar C[1]
408 Det = H[1][1];
409 if (Det < 1e-8) return; // ill-posed, return default values
410 x[0] = 0;
411 x[1] = C[1] / Det;
412
413 xq[0] = 0;
414 xq[1] = (int)rint(x[1] * (1 << SGRPROJ_PRJ_BITS));
Urvang Joshi3715b882018-05-14 20:05:25 -0400415 } else if (params->r[1] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000416 // H matrix is now only the scalar H[0][0]
417 // C vector is now only the scalar C[0]
418 Det = H[0][0];
419 if (Det < 1e-8) return; // ill-posed, return default values
420 x[0] = C[0] / Det;
421 x[1] = 0;
422
423 xq[0] = (int)rint(x[0] * (1 << SGRPROJ_PRJ_BITS));
424 xq[1] = 0;
425 } else {
426 Det = (H[0][0] * H[1][1] - H[0][1] * H[1][0]);
427 if (Det < 1e-8) return; // ill-posed, return default values
428 x[0] = (H[1][1] * C[0] - H[0][1] * C[1]) / Det;
429 x[1] = (H[0][0] * C[1] - H[1][0] * C[0]) / Det;
430
431 xq[0] = (int)rint(x[0] * (1 << SGRPROJ_PRJ_BITS));
432 xq[1] = (int)rint(x[1] * (1 << SGRPROJ_PRJ_BITS));
433 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700434}
435
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000436void encode_xq(int *xq, int *xqd, const sgr_params_type *params) {
Urvang Joshi3715b882018-05-14 20:05:25 -0400437 if (params->r[0] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000438 xqd[0] = 0;
439 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xq[1], SGRPROJ_PRJ_MIN1,
440 SGRPROJ_PRJ_MAX1);
Urvang Joshi3715b882018-05-14 20:05:25 -0400441 } else if (params->r[1] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000442 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
Debargha Mukherjee114575d2018-02-23 11:18:37 -0800443 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0], SGRPROJ_PRJ_MIN1,
444 SGRPROJ_PRJ_MAX1);
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000445 } else {
446 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
447 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0] - xq[1], SGRPROJ_PRJ_MIN1,
448 SGRPROJ_PRJ_MAX1);
449 }
450}
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700451
David Barkerbfbd8b32017-11-01 12:34:23 +0000452// Apply the self-guided filter across an entire restoration unit.
Urvang Joshic079f7a2018-05-11 16:13:56 -0700453static void apply_sgr(int sgr_params_idx, const uint8_t *dat8, int width,
454 int height, int dat_stride, int use_highbd, int bit_depth,
455 int pu_width, int pu_height, int32_t *flt0, int32_t *flt1,
456 int flt_stride) {
David Barkerbfbd8b32017-11-01 12:34:23 +0000457 for (int i = 0; i < height; i += pu_height) {
458 const int h = AOMMIN(pu_height, height - i);
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000459 int32_t *flt0_row = flt0 + i * flt_stride;
David Barkerbfbd8b32017-11-01 12:34:23 +0000460 int32_t *flt1_row = flt1 + i * flt_stride;
David Barkerbfbd8b32017-11-01 12:34:23 +0000461 const uint8_t *dat8_row = dat8 + i * dat_stride;
462
463 // Iterate over the stripe in blocks of width pu_width
464 for (int j = 0; j < width; j += pu_width) {
465 const int w = AOMMIN(pu_width, width - j);
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000466 av1_selfguided_restoration(dat8_row + j, w, h, dat_stride, flt0_row + j,
Urvang Joshic079f7a2018-05-11 16:13:56 -0700467 flt1_row + j, flt_stride, sgr_params_idx,
468 bit_depth, use_highbd);
David Barkerbfbd8b32017-11-01 12:34:23 +0000469 }
470 }
471}
472
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100473static SgrprojInfo search_selfguided_restoration(
David Barkerbfbd8b32017-11-01 12:34:23 +0000474 const uint8_t *dat8, int width, int height, int dat_stride,
475 const uint8_t *src8, int src_stride, int use_highbitdepth, int bit_depth,
476 int pu_width, int pu_height, int32_t *rstbuf) {
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000477 int32_t *flt0 = rstbuf;
Urvang Joshi813186b2018-03-08 15:38:46 -0800478 int32_t *flt1 = flt0 + RESTORATION_UNITPELS_MAX;
David Barker506eb722017-03-08 13:35:49 +0000479 int ep, bestep = 0;
David Barkerbfbd8b32017-11-01 12:34:23 +0000480 int64_t besterr = -1;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700481 int exqd[2], bestxqd[2] = { 0, 0 };
David Barkerbfbd8b32017-11-01 12:34:23 +0000482 int flt_stride = ((width + 7) & ~7) + 8;
Debargha Mukherjee7a5587a2017-08-31 07:41:30 -0700483 assert(pu_width == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
484 pu_width == RESTORATION_PROC_UNIT_SIZE);
485 assert(pu_height == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
486 pu_height == RESTORATION_PROC_UNIT_SIZE);
David Barker3a0df182016-12-21 10:44:52 +0000487
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700488 for (ep = 0; ep < SGRPROJ_PARAMS; ep++) {
489 int exq[2];
Urvang Joshic079f7a2018-05-11 16:13:56 -0700490 apply_sgr(ep, dat8, width, height, dat_stride, use_highbitdepth, bit_depth,
491 pu_width, pu_height, flt0, flt1, flt_stride);
Debargha Mukherjee7ae7aea2017-05-04 15:17:17 -0700492 aom_clear_system_state();
Urvang Joshic079f7a2018-05-11 16:13:56 -0700493 const sgr_params_type *const params = &sgr_params[ep];
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000494 get_proj_subspace(src8, width, height, src_stride, dat8, dat_stride,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000495 use_highbitdepth, flt0, flt_stride, flt1, flt_stride, exq,
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000496 params);
Debargha Mukherjee7ae7aea2017-05-04 15:17:17 -0700497 aom_clear_system_state();
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000498 encode_xq(exq, exqd, params);
499 int64_t err = finer_search_pixel_proj_error(
500 src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth,
Imdad Sardharwalla7d3bd8d2018-02-22 15:47:33 +0000501 flt0, flt_stride, flt1, flt_stride, 2, exqd, params);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700502 if (besterr == -1 || err < besterr) {
503 bestep = ep;
504 besterr = err;
505 bestxqd[0] = exqd[0];
506 bestxqd[1] = exqd[1];
507 }
508 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100509
510 SgrprojInfo ret;
511 ret.ep = bestep;
512 ret.xqd[0] = bestxqd[0];
513 ret.xqd[1] = bestxqd[1];
514 return ret;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700515}
516
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700517static int count_sgrproj_bits(SgrprojInfo *sgrproj_info,
518 SgrprojInfo *ref_sgrproj_info) {
519 int bits = SGRPROJ_PARAMS_BITS;
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000520 const sgr_params_type *params = &sgr_params[sgrproj_info->ep];
Urvang Joshi3715b882018-05-14 20:05:25 -0400521 if (params->r[0] > 0)
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000522 bits += aom_count_primitive_refsubexpfin(
523 SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
524 ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0,
525 sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0);
Urvang Joshi3715b882018-05-14 20:05:25 -0400526 if (params->r[1] > 0)
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +0000527 bits += aom_count_primitive_refsubexpfin(
528 SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
529 ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1,
530 sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700531 return bits;
532}
533
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100534static void search_sgrproj(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000535 const AV1PixelRect *tile, int rest_unit_idx,
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +0530536 void *priv, int32_t *tmpbuf,
537 RestorationLineBuffers *rlbs) {
538 (void)rlbs;
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100539 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
540 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100541
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100542 const MACROBLOCK *const x = rsc->x;
543 const AV1_COMMON *const cm = rsc->cm;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700544 const int highbd = cm->seq_params.use_highbitdepth;
545 const int bit_depth = cm->seq_params.bit_depth;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100546
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +0100547 uint8_t *dgd_start =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100548 rsc->dgd_buffer + limits->v_start * rsc->dgd_stride + limits->h_start;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100549 const uint8_t *src_start =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100550 rsc->src_buffer + limits->v_start * rsc->src_stride + limits->h_start;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100551
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000552 const int is_uv = rsc->plane > 0;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700553 const int ss_x = is_uv && cm->seq_params.subsampling_x;
554 const int ss_y = is_uv && cm->seq_params.subsampling_y;
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000555 const int procunit_width = RESTORATION_PROC_UNIT_SIZE >> ss_x;
556 const int procunit_height = RESTORATION_PROC_UNIT_SIZE >> ss_y;
557
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100558 rusi->sgrproj = search_selfguided_restoration(
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +0100559 dgd_start, limits->h_end - limits->h_start,
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +0100560 limits->v_end - limits->v_start, rsc->dgd_stride, src_start,
Rupert Swarbrickcb493d82017-11-02 10:22:10 +0000561 rsc->src_stride, highbd, bit_depth, procunit_width, procunit_height,
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +0530562 tmpbuf);
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100563
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100564 RestorationUnitInfo rui;
565 rui.restoration_type = RESTORE_SGRPROJ;
566 rui.sgrproj_info = rusi->sgrproj;
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100567
Urvang Joshi813186b2018-03-08 15:38:46 -0800568 rusi->sse[RESTORE_SGRPROJ] = try_restoration_unit(rsc, limits, tile, &rui);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100569
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100570 const int64_t bits_none = x->sgrproj_restore_cost[0];
571 const int64_t bits_sgr = x->sgrproj_restore_cost[1] +
572 (count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj)
573 << AV1_PROB_COST_SHIFT);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100574
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100575 double cost_none =
576 RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
577 double cost_sgr =
578 RDCOST_DBL(x->rdmult, bits_sgr >> 4, rusi->sse[RESTORE_SGRPROJ]);
Debargha Mukherjeeda394fb2018-05-18 09:05:16 -0700579 if (rusi->sgrproj.ep < 10)
580 cost_sgr *= (1 + DUAL_SGR_PENALTY_MULT * rsc->sf->dual_sgr_penalty_level);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100581
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100582 RestorationType rtype =
583 (cost_sgr < cost_none) ? RESTORE_SGRPROJ : RESTORE_NONE;
584 rusi->best_rtype[RESTORE_SGRPROJ - 1] = rtype;
Debargha Mukherjeee168a782017-08-31 12:30:10 -0700585
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +0100586 rsc->sse += rusi->sse[rtype];
587 rsc->bits += (cost_sgr < cost_none) ? bits_sgr : bits_none;
588 if (cost_sgr < cost_none) rsc->sgrproj = rusi->sgrproj;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700589}
590
Xing Jin04c7c842018-07-30 17:08:30 +0800591void av1_compute_stats_c(int wiener_win, const uint8_t *dgd, const uint8_t *src,
592 int h_start, int h_end, int v_start, int v_end,
593 int dgd_stride, int src_stride, double *M, double *H) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594 int i, j, k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800595 double Y[WIENER_WIN2];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700596 const int wiener_win2 = wiener_win * wiener_win;
597 const int wiener_halfwin = (wiener_win >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700598 const double avg =
599 find_average(dgd, h_start, h_end, v_start, v_end, dgd_stride);
600
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700601 memset(M, 0, sizeof(*M) * wiener_win2);
602 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700603 for (i = v_start; i < v_end; i++) {
604 for (j = h_start; j < h_end; j++) {
605 const double X = (double)src[i * src_stride + j] - avg;
606 int idx = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700607 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
608 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700609 Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg;
610 idx++;
611 }
612 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700613 assert(idx == wiener_win2);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700614 for (k = 0; k < wiener_win2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700615 M[k] += Y[k] * X;
Xing Jin37ee03b2018-07-25 10:15:42 +0800616 for (l = k; l < wiener_win2; ++l) {
David Barker33f3bfd2017-01-06 15:34:50 +0000617 // H is a symmetric matrix, so we only need to fill out the upper
618 // triangle here. We can copy it down to the lower triangle outside
619 // the (i, j) loops.
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700620 H[k * wiener_win2 + l] += Y[k] * Y[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 }
622 }
623 }
624 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700625 for (k = 0; k < wiener_win2; ++k) {
626 for (l = k + 1; l < wiener_win2; ++l) {
627 H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
David Barker33f3bfd2017-01-06 15:34:50 +0000628 }
629 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700630}
631
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100632static double find_average_highbd(const uint16_t *src, int h_start, int h_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700633 int v_start, int v_end, int stride) {
634 uint64_t sum = 0;
635 double avg = 0;
636 int i, j;
Jingning Han041c67b2017-04-14 21:39:26 -0700637 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700638 for (i = v_start; i < v_end; i++)
639 for (j = h_start; j < h_end; j++) sum += src[i * stride + j];
640 avg = (double)sum / ((v_end - v_start) * (h_end - h_start));
641 return avg;
642}
643
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +0900644static AOM_FORCE_INLINE void compute_stats_highbd(
645 int wiener_win, const uint8_t *dgd8, const uint8_t *src8, int h_start,
646 int h_end, int v_start, int v_end, int dgd_stride, int src_stride,
647 double *M, double *H) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700648 int i, j, k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800649 double Y[WIENER_WIN2];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700650 const int wiener_win2 = wiener_win * wiener_win;
651 const int wiener_halfwin = (wiener_win >> 1);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +0100652 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
653 const uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700654 const double avg =
655 find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
656
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700657 memset(M, 0, sizeof(*M) * wiener_win2);
658 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700659 for (i = v_start; i < v_end; i++) {
660 for (j = h_start; j < h_end; j++) {
661 const double X = (double)src[i * src_stride + j] - avg;
662 int idx = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700663 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
664 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700665 Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg;
666 idx++;
667 }
668 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700669 assert(idx == wiener_win2);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700670 for (k = 0; k < wiener_win2; ++k) {
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +0900671 double Yk = Y[k];
672 M[k] += Yk * X;
673 double *H2 = &H[k * wiener_win2];
674 H2[k] += Yk * Yk;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700675 for (l = k + 1; l < wiener_win2; ++l) {
David Barker33f3bfd2017-01-06 15:34:50 +0000676 // H is a symmetric matrix, so we only need to fill out the upper
677 // triangle here. We can copy it down to the lower triangle outside
678 // the (i, j) loops.
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +0900679 H2[l] += Yk * Y[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700680 }
681 }
682 }
683 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700684 for (k = 0; k < wiener_win2; ++k) {
685 for (l = k + 1; l < wiener_win2; ++l) {
686 H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
David Barker33f3bfd2017-01-06 15:34:50 +0000687 }
688 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700691static INLINE int wrap_index(int i, int wiener_win) {
692 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
693 return (i >= wiener_halfwin1 ? wiener_win - 1 - i : i);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700694}
695
696// Fix vector b, update vector a
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700697static void update_a_sep_sym(int wiener_win, double **Mc, double **Hc,
698 double *a, double *b) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700699 int i, j;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800700 double S[WIENER_WIN];
Debargha Mukherjee6ae588f2017-04-14 00:40:02 -0700701 double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700702 const int wiener_win2 = wiener_win * wiener_win;
703 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700704 memset(A, 0, sizeof(A));
705 memset(B, 0, sizeof(B));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700706 for (i = 0; i < wiener_win; i++) {
707 for (j = 0; j < wiener_win; ++j) {
708 const int jj = wrap_index(j, wiener_win);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700709 A[jj] += Mc[i][j] * b[i];
710 }
711 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700712 for (i = 0; i < wiener_win; i++) {
713 for (j = 0; j < wiener_win; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700714 int k, l;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700715 for (k = 0; k < wiener_win; ++k)
716 for (l = 0; l < wiener_win; ++l) {
717 const int kk = wrap_index(k, wiener_win);
718 const int ll = wrap_index(l, wiener_win);
719 B[ll * wiener_halfwin1 + kk] +=
720 Hc[j * wiener_win + i][k * wiener_win2 + l] * b[i] * b[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700721 }
722 }
723 }
724 // Normalization enforcement in the system of equations itself
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700725 for (i = 0; i < wiener_halfwin1 - 1; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700726 A[i] -=
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700727 A[wiener_halfwin1 - 1] * 2 +
728 B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
729 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
730 for (i = 0; i < wiener_halfwin1 - 1; ++i)
731 for (j = 0; j < wiener_halfwin1 - 1; ++j)
732 B[i * wiener_halfwin1 + j] -=
733 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
734 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
735 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
736 (wiener_halfwin1 - 1)]);
737 if (linsolve(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
738 S[wiener_halfwin1 - 1] = 1.0;
739 for (i = wiener_halfwin1; i < wiener_win; ++i) {
740 S[i] = S[wiener_win - 1 - i];
741 S[wiener_halfwin1 - 1] -= 2 * S[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700742 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700743 memcpy(a, S, wiener_win * sizeof(*a));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700744 }
745}
746
747// Fix vector a, update vector b
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700748static void update_b_sep_sym(int wiener_win, double **Mc, double **Hc,
749 double *a, double *b) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700750 int i, j;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800751 double S[WIENER_WIN];
Debargha Mukherjee6ae588f2017-04-14 00:40:02 -0700752 double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700753 const int wiener_win2 = wiener_win * wiener_win;
754 const int wiener_halfwin1 = (wiener_win >> 1) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700755 memset(A, 0, sizeof(A));
756 memset(B, 0, sizeof(B));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700757 for (i = 0; i < wiener_win; i++) {
758 const int ii = wrap_index(i, wiener_win);
759 for (j = 0; j < wiener_win; j++) A[ii] += Mc[i][j] * a[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760 }
761
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700762 for (i = 0; i < wiener_win; i++) {
763 for (j = 0; j < wiener_win; j++) {
764 const int ii = wrap_index(i, wiener_win);
765 const int jj = wrap_index(j, wiener_win);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700766 int k, l;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700767 for (k = 0; k < wiener_win; ++k)
768 for (l = 0; l < wiener_win; ++l)
769 B[jj * wiener_halfwin1 + ii] +=
770 Hc[i * wiener_win + j][k * wiener_win2 + l] * a[k] * a[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700771 }
772 }
773 // Normalization enforcement in the system of equations itself
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700774 for (i = 0; i < wiener_halfwin1 - 1; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700775 A[i] -=
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700776 A[wiener_halfwin1 - 1] * 2 +
777 B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
778 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
779 for (i = 0; i < wiener_halfwin1 - 1; ++i)
780 for (j = 0; j < wiener_halfwin1 - 1; ++j)
781 B[i * wiener_halfwin1 + j] -=
782 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
783 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
784 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
785 (wiener_halfwin1 - 1)]);
786 if (linsolve(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
787 S[wiener_halfwin1 - 1] = 1.0;
788 for (i = wiener_halfwin1; i < wiener_win; ++i) {
789 S[i] = S[wiener_win - 1 - i];
790 S[wiener_halfwin1 - 1] -= 2 * S[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700792 memcpy(b, S, wiener_win * sizeof(*b));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793 }
794}
795
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700796static int wiener_decompose_sep_sym(int wiener_win, double *M, double *H,
797 double *a, double *b) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700798 static const int init_filt[WIENER_WIN] = {
799 WIENER_FILT_TAP0_MIDV, WIENER_FILT_TAP1_MIDV, WIENER_FILT_TAP2_MIDV,
800 WIENER_FILT_TAP3_MIDV, WIENER_FILT_TAP2_MIDV, WIENER_FILT_TAP1_MIDV,
801 WIENER_FILT_TAP0_MIDV,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802 };
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800803 double *Hc[WIENER_WIN2];
804 double *Mc[WIENER_WIN];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700805 int i, j, iter;
806 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
807 const int wiener_win2 = wiener_win * wiener_win;
808 for (i = 0; i < wiener_win; i++) {
809 a[i] = b[i] = (double)init_filt[i + plane_off] / WIENER_FILT_STEP;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700810 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700811 for (i = 0; i < wiener_win; i++) {
812 Mc[i] = M + i * wiener_win;
813 for (j = 0; j < wiener_win; j++) {
814 Hc[i * wiener_win + j] =
815 H + i * wiener_win * wiener_win2 + j * wiener_win;
816 }
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700817 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818
819 iter = 1;
Debargha Mukherjee1b3dbf02017-03-13 14:47:21 -0700820 while (iter < NUM_WIENER_ITERS) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700821 update_a_sep_sym(wiener_win, Mc, Hc, a, b);
822 update_b_sep_sym(wiener_win, Mc, Hc, a, b);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700823 iter++;
824 }
825 return 1;
826}
827
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800828// Computes the function x'*H*x - x'*M for the learned 2D filter x, and compares
Yaowu Xuc27fc142016-08-22 16:08:15 -0700829// against identity filters; Final score is defined as the difference between
830// the function values
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700831static double compute_score(int wiener_win, double *M, double *H,
832 InterpKernel vfilt, InterpKernel hfilt) {
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800833 double ab[WIENER_WIN * WIENER_WIN];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700834 int i, k, l;
835 double P = 0, Q = 0;
836 double iP = 0, iQ = 0;
837 double Score, iScore;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800838 double a[WIENER_WIN], b[WIENER_WIN];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700839 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
840 const int wiener_win2 = wiener_win * wiener_win;
Jingning Han041c67b2017-04-14 21:39:26 -0700841
842 aom_clear_system_state();
843
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800844 a[WIENER_HALFWIN] = b[WIENER_HALFWIN] = 1.0;
845 for (i = 0; i < WIENER_HALFWIN; ++i) {
846 a[i] = a[WIENER_WIN - i - 1] = (double)vfilt[i] / WIENER_FILT_STEP;
847 b[i] = b[WIENER_WIN - i - 1] = (double)hfilt[i] / WIENER_FILT_STEP;
848 a[WIENER_HALFWIN] -= 2 * a[i];
849 b[WIENER_HALFWIN] -= 2 * b[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700850 }
Debargha Mukherjeea1a1e362017-10-04 20:01:03 -0700851 memset(ab, 0, sizeof(ab));
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700852 for (k = 0; k < wiener_win; ++k) {
853 for (l = 0; l < wiener_win; ++l)
854 ab[k * wiener_win + l] = a[l + plane_off] * b[k + plane_off];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700856 for (k = 0; k < wiener_win2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857 P += ab[k] * M[k];
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700858 for (l = 0; l < wiener_win2; ++l)
859 Q += ab[k] * H[k * wiener_win2 + l] * ab[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700860 }
861 Score = Q - 2 * P;
862
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700863 iP = M[wiener_win2 >> 1];
864 iQ = H[(wiener_win2 >> 1) * wiener_win2 + (wiener_win2 >> 1)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865 iScore = iQ - 2 * iP;
866
867 return Score - iScore;
868}
869
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700870static void quantize_sym_filter(int wiener_win, double *f, InterpKernel fi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700871 int i;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700872 const int wiener_halfwin = (wiener_win >> 1);
873 for (i = 0; i < wiener_halfwin; ++i) {
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800874 fi[i] = RINT(f[i] * WIENER_FILT_STEP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 }
876 // Specialize for 7-tap filter
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700877 if (wiener_win == WIENER_WIN) {
878 fi[0] = CLIP(fi[0], WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP0_MAXV);
879 fi[1] = CLIP(fi[1], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
880 fi[2] = CLIP(fi[2], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
881 } else {
882 fi[2] = CLIP(fi[1], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
883 fi[1] = CLIP(fi[0], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
884 fi[0] = 0;
885 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800886 // Satisfy filter constraints
887 fi[WIENER_WIN - 1] = fi[0];
888 fi[WIENER_WIN - 2] = fi[1];
889 fi[WIENER_WIN - 3] = fi[2];
David Barker1e8e6b92017-01-13 13:45:51 +0000890 // The central element has an implicit +WIENER_FILT_STEP
891 fi[3] = -2 * (fi[0] + fi[1] + fi[2]);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800892}
893
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700894static int count_wiener_bits(int wiener_win, WienerInfo *wiener_info,
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700895 WienerInfo *ref_wiener_info) {
896 int bits = 0;
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700897 if (wiener_win == WIENER_WIN)
898 bits += aom_count_primitive_refsubexpfin(
899 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
900 WIENER_FILT_TAP0_SUBEXP_K,
901 ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV,
902 wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700903 bits += aom_count_primitive_refsubexpfin(
904 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
905 WIENER_FILT_TAP1_SUBEXP_K,
906 ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV,
907 wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV);
908 bits += aom_count_primitive_refsubexpfin(
909 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
910 WIENER_FILT_TAP2_SUBEXP_K,
911 ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV,
912 wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV);
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700913 if (wiener_win == WIENER_WIN)
914 bits += aom_count_primitive_refsubexpfin(
915 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
916 WIENER_FILT_TAP0_SUBEXP_K,
917 ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV,
918 wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -0700919 bits += aom_count_primitive_refsubexpfin(
920 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
921 WIENER_FILT_TAP1_SUBEXP_K,
922 ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV,
923 wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV);
924 bits += aom_count_primitive_refsubexpfin(
925 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
926 WIENER_FILT_TAP2_SUBEXP_K,
927 ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV,
928 wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV);
929 return bits;
930}
931
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700932#define USE_WIENER_REFINEMENT_SEARCH 1
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000933static int64_t finer_tile_search_wiener(const RestSearchCtxt *rsc,
934 const RestorationTileLimits *limits,
935 const AV1PixelRect *tile,
936 RestorationUnitInfo *rui,
937 int wiener_win) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700938 const int plane_off = (WIENER_WIN - wiener_win) >> 1;
Urvang Joshi813186b2018-03-08 15:38:46 -0800939 int64_t err = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700940#if USE_WIENER_REFINEMENT_SEARCH
941 int64_t err2;
942 int tap_min[] = { WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP1_MINV,
943 WIENER_FILT_TAP2_MINV };
944 int tap_max[] = { WIENER_FILT_TAP0_MAXV, WIENER_FILT_TAP1_MAXV,
945 WIENER_FILT_TAP2_MAXV };
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100946
Rupert Swarbrick2ec2a6f2017-10-20 09:52:13 +0100947 WienerInfo *plane_wiener = &rui->wiener_info;
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100948
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700949 // printf("err pre = %"PRId64"\n", err);
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +0000950 const int start_step = 4;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700951 for (int s = start_step; s >= 1; s >>= 1) {
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700952 for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700953 int skip = 0;
954 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100955 if (plane_wiener->hfilter[p] - s >= tap_min[p]) {
956 plane_wiener->hfilter[p] -= s;
957 plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
958 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -0800959 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700960 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100961 plane_wiener->hfilter[p] += s;
962 plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
963 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700964 } else {
965 err = err2;
966 skip = 1;
967 // At the highest step size continue moving in the same direction
968 if (s == start_step) continue;
969 }
970 }
971 break;
972 } while (1);
973 if (skip) break;
974 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100975 if (plane_wiener->hfilter[p] + s <= tap_max[p]) {
976 plane_wiener->hfilter[p] += s;
977 plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
978 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -0800979 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700980 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100981 plane_wiener->hfilter[p] -= s;
982 plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
983 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700984 } else {
985 err = err2;
986 // At the highest step size continue moving in the same direction
987 if (s == start_step) continue;
988 }
989 }
990 break;
991 } while (1);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -0700992 }
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -0700993 for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -0700994 int skip = 0;
995 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +0100996 if (plane_wiener->vfilter[p] - s >= tap_min[p]) {
997 plane_wiener->vfilter[p] -= s;
998 plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
999 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -08001000 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001001 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001002 plane_wiener->vfilter[p] += s;
1003 plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
1004 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001005 } else {
1006 err = err2;
1007 skip = 1;
1008 // At the highest step size continue moving in the same direction
1009 if (s == start_step) continue;
1010 }
1011 }
1012 break;
1013 } while (1);
1014 if (skip) break;
1015 do {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001016 if (plane_wiener->vfilter[p] + s <= tap_max[p]) {
1017 plane_wiener->vfilter[p] += s;
1018 plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
1019 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
Urvang Joshi813186b2018-03-08 15:38:46 -08001020 err2 = try_restoration_unit(rsc, limits, tile, rui);
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001021 if (err2 > err) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001022 plane_wiener->vfilter[p] -= s;
1023 plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
1024 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
Debargha Mukherjee0c18b2c2017-05-15 21:15:30 -07001025 } else {
1026 err = err2;
1027 // At the highest step size continue moving in the same direction
1028 if (s == start_step) continue;
1029 }
1030 }
1031 break;
1032 } while (1);
Debargha Mukherjeee39e2ee2017-05-11 03:38:03 -07001033 }
1034 }
1035// printf("err post = %"PRId64"\n", err);
1036#endif // USE_WIENER_REFINEMENT_SEARCH
1037 return err;
1038}
1039
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001040static void search_wiener(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001041 const AV1PixelRect *tile_rect, int rest_unit_idx,
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +05301042 void *priv, int32_t *tmpbuf,
1043 RestorationLineBuffers *rlbs) {
1044 (void)tmpbuf;
1045 (void)rlbs;
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001046 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1047 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1048
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001049 const int wiener_win =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001050 (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001051
1052 double M[WIENER_WIN2];
1053 double H[WIENER_WIN2 * WIENER_WIN2];
1054 double vfilterd[WIENER_WIN], hfilterd[WIENER_WIN];
1055
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001056 const AV1_COMMON *const cm = rsc->cm;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001057 if (cm->seq_params.use_highbitdepth) {
Dandan Dingda757bb2018-06-01 11:26:06 +08001058 compute_stats_highbd(wiener_win, rsc->dgd_buffer, rsc->src_buffer,
1059 limits->h_start, limits->h_end, limits->v_start,
1060 limits->v_end, rsc->dgd_stride, rsc->src_stride, M, H);
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +09001061 } else {
Xing Jin04c7c842018-07-30 17:08:30 +08001062 av1_compute_stats(wiener_win, rsc->dgd_buffer, rsc->src_buffer,
1063 limits->h_start, limits->h_end, limits->v_start,
1064 limits->v_end, rsc->dgd_stride, rsc->src_stride, M, H);
Katsuhisa Yuasa468097e2018-04-22 21:02:45 +09001065 }
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001066
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001067 const MACROBLOCK *const x = rsc->x;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001068 const int64_t bits_none = x->wiener_restore_cost[0];
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001069
1070 if (!wiener_decompose_sep_sym(wiener_win, M, H, vfilterd, hfilterd)) {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001071 rsc->bits += bits_none;
1072 rsc->sse += rusi->sse[RESTORE_NONE];
1073 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1074 rusi->sse[RESTORE_WIENER] = INT64_MAX;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001075 return;
1076 }
1077
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001078 RestorationUnitInfo rui;
1079 memset(&rui, 0, sizeof(rui));
1080 rui.restoration_type = RESTORE_WIENER;
1081 quantize_sym_filter(wiener_win, vfilterd, rui.wiener_info.vfilter);
1082 quantize_sym_filter(wiener_win, hfilterd, rui.wiener_info.hfilter);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001083
1084 // Filter score computes the value of the function x'*A*x - x'*b for the
1085 // learned filter and compares it against identity filer. If there is no
1086 // reduction in the function, the filter is reverted back to identity
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001087 if (compute_score(wiener_win, M, H, rui.wiener_info.vfilter,
1088 rui.wiener_info.hfilter) > 0) {
1089 rsc->bits += bits_none;
1090 rsc->sse += rusi->sse[RESTORE_NONE];
1091 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1092 rusi->sse[RESTORE_WIENER] = INT64_MAX;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001093 return;
1094 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001095
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001096 aom_clear_system_state();
1097
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001098 rusi->sse[RESTORE_WIENER] =
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001099 finer_tile_search_wiener(rsc, limits, tile_rect, &rui, wiener_win);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001100 rusi->wiener = rui.wiener_info;
1101
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001102 if (wiener_win != WIENER_WIN) {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001103 assert(rui.wiener_info.vfilter[0] == 0 &&
1104 rui.wiener_info.vfilter[WIENER_WIN - 1] == 0);
1105 assert(rui.wiener_info.hfilter[0] == 0 &&
1106 rui.wiener_info.hfilter[WIENER_WIN - 1] == 0);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001107 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001108
1109 const int64_t bits_wiener =
1110 x->wiener_restore_cost[1] +
1111 (count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener)
1112 << AV1_PROB_COST_SHIFT);
1113
1114 double cost_none =
1115 RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
1116 double cost_wiener =
1117 RDCOST_DBL(x->rdmult, bits_wiener >> 4, rusi->sse[RESTORE_WIENER]);
1118
1119 RestorationType rtype =
1120 (cost_wiener < cost_none) ? RESTORE_WIENER : RESTORE_NONE;
1121 rusi->best_rtype[RESTORE_WIENER - 1] = rtype;
1122
1123 rsc->sse += rusi->sse[rtype];
1124 rsc->bits += (cost_wiener < cost_none) ? bits_wiener : bits_none;
1125 if (cost_wiener < cost_none) rsc->wiener = rusi->wiener;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001126}
1127
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001128static void search_norestore(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001129 const AV1PixelRect *tile_rect, int rest_unit_idx,
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +05301130 void *priv, int32_t *tmpbuf,
1131 RestorationLineBuffers *rlbs) {
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001132 (void)tile_rect;
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +05301133 (void)tmpbuf;
1134 (void)rlbs;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001135
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001136 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1137 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1138
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001139 const int highbd = rsc->cm->seq_params.use_highbitdepth;
Urvang Joshi813186b2018-03-08 15:38:46 -08001140 rusi->sse[RESTORE_NONE] = sse_restoration_unit(
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001141 limits, rsc->src, rsc->cm->frame_to_show, rsc->plane, highbd);
Rupert Swarbrick64b8bbd2017-10-16 15:53:07 +01001142
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001143 rsc->sse += rusi->sse[RESTORE_NONE];
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001144}
1145
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001146static void search_switchable(const RestorationTileLimits *limits,
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001147 const AV1PixelRect *tile_rect, int rest_unit_idx,
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +05301148 void *priv, int32_t *tmpbuf,
1149 RestorationLineBuffers *rlbs) {
Rupert Swarbrick5d2e7292017-09-26 11:32:17 +01001150 (void)limits;
Rupert Swarbrickdee00eb2017-11-02 17:24:37 +00001151 (void)tile_rect;
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +05301152 (void)tmpbuf;
1153 (void)rlbs;
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001154 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1155 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001156
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001157 const MACROBLOCK *const x = rsc->x;
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001158
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001159 const int wiener_win =
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001160 (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001161
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001162 double best_cost = 0;
1163 int64_t best_bits = 0;
1164 RestorationType best_rtype = RESTORE_NONE;
1165
1166 for (RestorationType r = 0; r < RESTORE_SWITCHABLE_TYPES; ++r) {
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001167 // Check for the condition that wiener or sgrproj search could not
1168 // find a solution or the solution was worse than RESTORE_NONE.
1169 // In either case the best_rtype will be set as RESTORE_NONE. These
1170 // should be skipped from the test below.
1171 if (r > RESTORE_NONE) {
1172 if (rusi->best_rtype[r - 1] == RESTORE_NONE) continue;
1173 }
1174
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001175 const int64_t sse = rusi->sse[r];
1176 int64_t coeff_pcost = 0;
1177 switch (r) {
1178 case RESTORE_NONE: coeff_pcost = 0; break;
1179 case RESTORE_WIENER:
1180 coeff_pcost =
1181 count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener);
1182 break;
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001183 case RESTORE_SGRPROJ:
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001184 coeff_pcost = count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj);
1185 break;
Debargha Mukherjee35bcd512017-11-11 15:10:59 -08001186 default: assert(0); break;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001187 }
1188 const int64_t coeff_bits = coeff_pcost << AV1_PROB_COST_SHIFT;
1189 const int64_t bits = x->switchable_restore_cost[r] + coeff_bits;
1190 double cost = RDCOST_DBL(x->rdmult, bits >> 4, sse);
Debargha Mukherjeeda394fb2018-05-18 09:05:16 -07001191 if (r == RESTORE_SGRPROJ && rusi->sgrproj.ep < 10)
1192 cost *= (1 + DUAL_SGR_PENALTY_MULT * rsc->sf->dual_sgr_penalty_level);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001193 if (r == 0 || cost < best_cost) {
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001194 best_cost = cost;
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001195 best_bits = bits;
1196 best_rtype = r;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001197 }
1198 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001199
1200 rusi->best_rtype[RESTORE_SWITCHABLE - 1] = best_rtype;
1201
1202 rsc->sse += rusi->sse[best_rtype];
1203 rsc->bits += best_bits;
1204 if (best_rtype == RESTORE_WIENER) rsc->wiener = rusi->wiener;
1205 if (best_rtype == RESTORE_SGRPROJ) rsc->sgrproj = rusi->sgrproj;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001206}
1207
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001208static void copy_unit_info(RestorationType frame_rtype,
1209 const RestUnitSearchInfo *rusi,
1210 RestorationUnitInfo *rui) {
1211 assert(frame_rtype > 0);
1212 rui->restoration_type = rusi->best_rtype[frame_rtype - 1];
1213 if (rui->restoration_type == RESTORE_WIENER)
1214 rui->wiener_info = rusi->wiener;
1215 else
1216 rui->sgrproj_info = rusi->sgrproj;
1217}
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001218
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001219static double search_rest_type(RestSearchCtxt *rsc, RestorationType rtype) {
1220 static const rest_unit_visitor_t funs[RESTORE_TYPES] = {
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001221 search_norestore, search_wiener, search_sgrproj, search_switchable
1222 };
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001223
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001224 reset_rsc(rsc);
Ravi Chaudhary93730f62018-06-04 14:20:00 +05301225 rsc_on_tile(LR_TILE_ROW, LR_TILE_COL, rsc);
Ravi Chaudhary9b099052018-05-31 14:31:49 +05301226 av1_foreach_rest_unit_in_plane(rsc->cm, rsc->plane, funs[rtype], rsc,
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +05301227 &rsc->tile_rect, rsc->cm->rst_tmpbuf, NULL);
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001228 return RDCOST_DBL(rsc->x->rdmult, rsc->bits >> 4, rsc->sse);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001229}
1230
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001231static int rest_tiles_in_plane(const AV1_COMMON *cm, int plane) {
1232 const RestorationInfo *rsi = &cm->rst_info[plane];
David Barkeree0ae202018-03-28 16:58:57 +01001233 return rsi->units_per_tile;
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001234}
1235
Rupert Swarbrick146a0602017-10-17 16:52:20 +01001236void av1_pick_filter_restoration(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001237 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001238 const int num_planes = av1_num_planes(cm);
Urvang Joshi14072aa2018-03-21 17:43:36 -07001239 assert(!cm->all_lossless);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001240
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001241 int ntiles[2];
1242 for (int is_uv = 0; is_uv < 2; ++is_uv)
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001243 ntiles[is_uv] = rest_tiles_in_plane(cm, is_uv);
Debargha Mukherjeed48f5732017-05-19 14:58:07 -07001244
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001245 assert(ntiles[1] <= ntiles[0]);
1246 RestUnitSearchInfo *rusi =
Yaowu Xu43e30f42017-11-27 14:26:00 -08001247 (RestUnitSearchInfo *)aom_memalign(16, sizeof(*rusi) * ntiles[0]);
Debargha Mukherjeed48f5732017-05-19 14:58:07 -07001248
Imdad Sardharwallab08544d2018-01-23 12:45:31 +00001249 // If the restoration unit dimensions are not multiples of
1250 // rsi->restoration_unit_size then some elements of the rusi array may be
1251 // left uninitialised when we reach copy_unit_info(...). This is not a
1252 // problem, as these elements are ignored later, but in order to quiet
1253 // Valgrind's warnings we initialise the array below.
1254 memset(rusi, 0, sizeof(*rusi) * ntiles[0]);
Ravi Chaudhary41726042018-07-23 15:51:28 +05301255 cpi->td.mb.rdmult = cpi->rd.RDMULT;
Imdad Sardharwallab08544d2018-01-23 12:45:31 +00001256
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001257 RestSearchCtxt rsc;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001258 const int plane_start = AOM_PLANE_Y;
1259 const int plane_end = num_planes > 1 ? AOM_PLANE_V : AOM_PLANE_Y;
1260 for (int plane = plane_start; plane <= plane_end; ++plane) {
Debargha Mukherjeeda394fb2018-05-18 09:05:16 -07001261 init_rsc(src, &cpi->common, &cpi->td.mb, &cpi->sf, plane, rusi,
1262 &cpi->trial_frame_rst, &rsc);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001263
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001264 const int plane_ntiles = ntiles[plane > 0];
1265 const RestorationType num_rtypes =
1266 (plane_ntiles > 1) ? RESTORE_TYPES : RESTORE_SWITCHABLE_TYPES;
1267
1268 double best_cost = 0;
1269 RestorationType best_rtype = RESTORE_NONE;
1270
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001271 const int highbd = rsc.cm->seq_params.use_highbitdepth;
Rupert Swarbrick5b401362017-10-31 10:56:27 +00001272 extend_frame(rsc.dgd_buffer, rsc.plane_width, rsc.plane_height,
1273 rsc.dgd_stride, RESTORATION_BORDER, RESTORATION_BORDER,
1274 highbd);
1275
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001276 for (RestorationType r = 0; r < num_rtypes; ++r) {
1277 if ((force_restore_type != RESTORE_TYPES) && (r != RESTORE_NONE) &&
1278 (r != force_restore_type))
1279 continue;
1280
Rupert Swarbrick33ed9e62017-10-23 13:32:37 +01001281 double cost = search_rest_type(&rsc, r);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001282
1283 if (r == 0 || cost < best_cost) {
1284 best_cost = cost;
1285 best_rtype = r;
1286 }
1287 }
1288
1289 cm->rst_info[plane].frame_restoration_type = best_rtype;
1290 if (force_restore_type != RESTORE_TYPES)
1291 assert(best_rtype == force_restore_type || best_rtype == RESTORE_NONE);
1292
1293 if (best_rtype != RESTORE_NONE) {
1294 for (int u = 0; u < plane_ntiles; ++u) {
1295 copy_unit_info(best_rtype, &rusi[u], &cm->rst_info[plane].unit_info[u]);
1296 }
1297 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001298 }
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01001299
1300 aom_free(rusi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001301}