blob: 90a9f0d8daf23da9513d486ced4f2792cc0015ba [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
19#include "aom_dsp/psnr.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070020#include "aom_dsp/aom_dsp_common.h"
21#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070022#include "aom_ports/mem.h"
Jingning Han041c67b2017-04-14 21:39:26 -070023#include "aom_ports/system_state.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070024
25#include "av1/common/onyxc_int.h"
26#include "av1/common/quant_common.h"
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -080027#include "av1/common/restoration.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070028
Tom Finegan17ce8b12017-02-08 12:46:31 -080029#include "av1/encoder/av1_quantize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070030#include "av1/encoder/encoder.h"
31#include "av1/encoder/picklpf.h"
32#include "av1/encoder/pickrst.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033
Debargha Mukherjeefdbe3f72017-04-06 12:09:19 -070034// When set to RESTORE_WIENER or RESTORE_SGRPROJ only those are allowed.
35// When set to RESTORE_NONE (0) we allow switchable.
36const RestorationType force_restore_type = RESTORE_NONE;
Debargha Mukherjee1b3dbf02017-03-13 14:47:21 -070037
38// Number of Wiener iterations
39#define NUM_WIENER_ITERS 10
40
Debargha Mukherjee5d89a632016-09-17 13:16:58 -070041typedef double (*search_restore_type)(const YV12_BUFFER_CONFIG *src,
Debargha Mukherjee00c54332017-03-03 15:44:17 -080042 AV1_COMP *cpi, int partial_frame,
43 RestorationInfo *info,
Debargha Mukherjee994ccd72017-01-06 11:18:23 -080044 RestorationType *rest_level,
David Barker9666e752016-12-08 11:25:47 +000045 double *best_tile_cost,
46 YV12_BUFFER_CONFIG *dst_frame);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -070047
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -080048const int frame_level_restore_bits[RESTORE_TYPES] = { 2, 2, 2, 2 };
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -070049
50static int64_t sse_restoration_tile(const YV12_BUFFER_CONFIG *src,
David Barker9666e752016-12-08 11:25:47 +000051 const YV12_BUFFER_CONFIG *dst,
52 const AV1_COMMON *cm, int h_start,
Debargha Mukherjee874d36d2016-12-14 16:53:17 -080053 int width, int v_start, int height,
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -080054 int components_pattern) {
55 int64_t filt_err = 0;
Debargha Mukherjeed7489142017-01-05 13:58:16 -080056 (void)cm;
57 // Y and UV components cannot be mixed
58 assert(components_pattern == 1 || components_pattern == 2 ||
59 components_pattern == 4 || components_pattern == 6);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020060#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -070061 if (cm->use_highbitdepth) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -080062 if ((components_pattern >> AOM_PLANE_Y) & 1) {
63 filt_err +=
64 aom_highbd_get_y_sse_part(src, dst, h_start, width, v_start, height);
65 }
66 if ((components_pattern >> AOM_PLANE_U) & 1) {
Debargha Mukherjeed7489142017-01-05 13:58:16 -080067 filt_err +=
68 aom_highbd_get_u_sse_part(src, dst, h_start, width, v_start, height);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -080069 }
70 if ((components_pattern >> AOM_PLANE_V) & 1) {
Debargha Mukherjeed7489142017-01-05 13:58:16 -080071 filt_err +=
72 aom_highbd_get_v_sse_part(src, dst, h_start, width, v_start, height);
Debargha Mukherjee874d36d2016-12-14 16:53:17 -080073 }
74 return filt_err;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -070075 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020076#endif // CONFIG_HIGHBITDEPTH
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -080077 if ((components_pattern >> AOM_PLANE_Y) & 1) {
78 filt_err += aom_get_y_sse_part(src, dst, h_start, width, v_start, height);
79 }
80 if ((components_pattern >> AOM_PLANE_U) & 1) {
Debargha Mukherjeed7489142017-01-05 13:58:16 -080081 filt_err += aom_get_u_sse_part(src, dst, h_start, width, v_start, height);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -080082 }
83 if ((components_pattern >> AOM_PLANE_V) & 1) {
Debargha Mukherjeed7489142017-01-05 13:58:16 -080084 filt_err += aom_get_v_sse_part(src, dst, h_start, width, v_start, height);
Debargha Mukherjee874d36d2016-12-14 16:53:17 -080085 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -070086 return filt_err;
87}
88
David Barker60a055b2017-01-18 15:10:43 +000089static int64_t sse_restoration_frame(AV1_COMMON *const cm,
90 const YV12_BUFFER_CONFIG *src,
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -080091 const YV12_BUFFER_CONFIG *dst,
92 int components_pattern) {
93 int64_t filt_err = 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020094#if CONFIG_HIGHBITDEPTH
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -080095 if (cm->use_highbitdepth) {
96 if ((components_pattern >> AOM_PLANE_Y) & 1) {
97 filt_err += aom_highbd_get_y_sse(src, dst);
98 }
99 if ((components_pattern >> AOM_PLANE_U) & 1) {
100 filt_err += aom_highbd_get_u_sse(src, dst);
101 }
102 if ((components_pattern >> AOM_PLANE_V) & 1) {
103 filt_err += aom_highbd_get_v_sse(src, dst);
104 }
105 return filt_err;
106 }
David Barker60a055b2017-01-18 15:10:43 +0000107#else
108 (void)cm;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200109#endif // CONFIG_HIGHBITDEPTH
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800110 if ((components_pattern >> AOM_PLANE_Y) & 1) {
111 filt_err = aom_get_y_sse(src, dst);
112 }
113 if ((components_pattern >> AOM_PLANE_U) & 1) {
114 filt_err += aom_get_u_sse(src, dst);
115 }
116 if ((components_pattern >> AOM_PLANE_V) & 1) {
117 filt_err += aom_get_v_sse(src, dst);
118 }
119 return filt_err;
120}
121
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700122static int64_t try_restoration_tile(const YV12_BUFFER_CONFIG *src,
123 AV1_COMP *const cpi, RestorationInfo *rsi,
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800124 int components_pattern, int partial_frame,
125 int tile_idx, int subtile_idx,
126 int subtile_bits,
David Barker9666e752016-12-08 11:25:47 +0000127 YV12_BUFFER_CONFIG *dst_frame) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700128 AV1_COMMON *const cm = &cpi->common;
129 int64_t filt_err;
130 int tile_width, tile_height, nhtiles, nvtiles;
131 int h_start, h_end, v_start, v_end;
Debargha Mukherjeed7489142017-01-05 13:58:16 -0800132 int ntiles, width, height;
133
134 // Y and UV components cannot be mixed
135 assert(components_pattern == 1 || components_pattern == 2 ||
136 components_pattern == 4 || components_pattern == 6);
137
138 if (components_pattern == 1) { // Y only
139 width = src->y_crop_width;
140 height = src->y_crop_height;
141 } else { // Color
142 width = src->uv_crop_width;
143 height = src->uv_crop_height;
144 }
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -0800145 ntiles = av1_get_rest_ntiles(
146 width, height, cm->rst_info[components_pattern > 1].restoration_tilesize,
147 &tile_width, &tile_height, &nhtiles, &nvtiles);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700148 (void)ntiles;
149
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800150 av1_loop_restoration_frame(cm->frame_to_show, cm, rsi, components_pattern,
151 partial_frame, dst_frame);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700152 av1_get_rest_tile_limits(tile_idx, subtile_idx, subtile_bits, nhtiles,
Debargha Mukherjeed7489142017-01-05 13:58:16 -0800153 nvtiles, tile_width, tile_height, width, height, 0,
154 0, &h_start, &h_end, &v_start, &v_end);
David Barker9666e752016-12-08 11:25:47 +0000155 filt_err = sse_restoration_tile(src, dst_frame, cm, h_start, h_end - h_start,
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800156 v_start, v_end - v_start, components_pattern);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700157
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700158 return filt_err;
159}
160
161static int64_t try_restoration_frame(const YV12_BUFFER_CONFIG *src,
Yaowu Xuf883b422016-08-30 14:01:10 -0700162 AV1_COMP *const cpi, RestorationInfo *rsi,
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800163 int components_pattern, int partial_frame,
David Barker9666e752016-12-08 11:25:47 +0000164 YV12_BUFFER_CONFIG *dst_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700165 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166 int64_t filt_err;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800167 av1_loop_restoration_frame(cm->frame_to_show, cm, rsi, components_pattern,
168 partial_frame, dst_frame);
David Barker60a055b2017-01-18 15:10:43 +0000169 filt_err = sse_restoration_frame(cm, src, dst_frame, components_pattern);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700170 return filt_err;
171}
172
David Barker3a0df182016-12-21 10:44:52 +0000173static int64_t get_pixel_proj_error(uint8_t *src8, int width, int height,
174 int src_stride, uint8_t *dat8,
175 int dat_stride, int bit_depth,
176 int32_t *flt1, int flt1_stride,
177 int32_t *flt2, int flt2_stride, int *xqd) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700178 int i, j;
179 int64_t err = 0;
180 int xq[2];
181 decode_xq(xqd, xq);
David Barker3a0df182016-12-21 10:44:52 +0000182 if (bit_depth == 8) {
183 const uint8_t *src = src8;
184 const uint8_t *dat = dat8;
185 for (i = 0; i < height; ++i) {
186 for (j = 0; j < width; ++j) {
187 const int32_t u =
188 (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
189 const int32_t f1 = (int32_t)flt1[i * flt1_stride + j] - u;
190 const int32_t f2 = (int32_t)flt2[i * flt2_stride + j] - u;
David Barkerce110cc2017-02-22 10:38:59 +0000191 const int32_t v = xq[0] * f1 + xq[1] * f2 + (u << SGRPROJ_PRJ_BITS);
David Barker3a0df182016-12-21 10:44:52 +0000192 const int32_t e =
193 ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) -
194 src[i * src_stride + j];
195 err += e * e;
196 }
197 }
198 } else {
199 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
200 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
201 for (i = 0; i < height; ++i) {
202 for (j = 0; j < width; ++j) {
203 const int32_t u =
204 (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
205 const int32_t f1 = (int32_t)flt1[i * flt1_stride + j] - u;
206 const int32_t f2 = (int32_t)flt2[i * flt2_stride + j] - u;
David Barkerce110cc2017-02-22 10:38:59 +0000207 const int32_t v = xq[0] * f1 + xq[1] * f2 + (u << SGRPROJ_PRJ_BITS);
David Barker3a0df182016-12-21 10:44:52 +0000208 const int32_t e =
209 ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) -
210 src[i * src_stride + j];
211 err += e * e;
212 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700213 }
214 }
215 return err;
216}
217
David Barker3a0df182016-12-21 10:44:52 +0000218static void get_proj_subspace(uint8_t *src8, int width, int height,
219 int src_stride, uint8_t *dat8, int dat_stride,
220 int bit_depth, int32_t *flt1, int flt1_stride,
221 int32_t *flt2, int flt2_stride, int *xq) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700222 int i, j;
223 double H[2][2] = { { 0, 0 }, { 0, 0 } };
224 double C[2] = { 0, 0 };
225 double Det;
226 double x[2];
227 const int size = width * height;
228
Jingning Han041c67b2017-04-14 21:39:26 -0700229 aom_clear_system_state();
230
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800231 // Default
232 xq[0] = 0;
233 xq[1] = 0;
David Barker3a0df182016-12-21 10:44:52 +0000234 if (bit_depth == 8) {
235 const uint8_t *src = src8;
236 const uint8_t *dat = dat8;
237 for (i = 0; i < height; ++i) {
238 for (j = 0; j < width; ++j) {
239 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
240 const double s =
241 (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
242 const double f1 = (double)flt1[i * flt1_stride + j] - u;
243 const double f2 = (double)flt2[i * flt2_stride + j] - u;
244 H[0][0] += f1 * f1;
245 H[1][1] += f2 * f2;
246 H[0][1] += f1 * f2;
247 C[0] += f1 * s;
248 C[1] += f2 * s;
249 }
250 }
251 } else {
252 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
253 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
254 for (i = 0; i < height; ++i) {
255 for (j = 0; j < width; ++j) {
256 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
257 const double s =
258 (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
259 const double f1 = (double)flt1[i * flt1_stride + j] - u;
260 const double f2 = (double)flt2[i * flt2_stride + j] - u;
261 H[0][0] += f1 * f1;
262 H[1][1] += f2 * f2;
263 H[0][1] += f1 * f2;
264 C[0] += f1 * s;
265 C[1] += f2 * s;
266 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700267 }
268 }
269 H[0][0] /= size;
270 H[0][1] /= size;
271 H[1][1] /= size;
272 H[1][0] = H[0][1];
273 C[0] /= size;
274 C[1] /= size;
275 Det = (H[0][0] * H[1][1] - H[0][1] * H[1][0]);
276 if (Det < 1e-8) return; // ill-posed, return default values
277 x[0] = (H[1][1] * C[0] - H[0][1] * C[1]) / Det;
278 x[1] = (H[0][0] * C[1] - H[1][0] * C[0]) / Det;
279 xq[0] = (int)rint(x[0] * (1 << SGRPROJ_PRJ_BITS));
280 xq[1] = (int)rint(x[1] * (1 << SGRPROJ_PRJ_BITS));
281}
282
283void encode_xq(int *xq, int *xqd) {
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800284 xqd[0] = xq[0];
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700285 xqd[0] = clamp(xqd[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800286 xqd[1] = (1 << SGRPROJ_PRJ_BITS) - xqd[0] - xq[1];
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700287 xqd[1] = clamp(xqd[1], SGRPROJ_PRJ_MIN1, SGRPROJ_PRJ_MAX1);
288}
289
290static void search_selfguided_restoration(uint8_t *dat8, int width, int height,
291 int dat_stride, uint8_t *src8,
292 int src_stride, int bit_depth,
David Barker3a0df182016-12-21 10:44:52 +0000293 int *eps, int *xqd, int32_t *rstbuf) {
294 int32_t *flt1 = rstbuf;
Debargha Mukherjee519dbcf2016-12-16 03:13:02 -0800295 int32_t *flt2 = flt1 + RESTORATION_TILEPELS_MAX;
David Barker3a0df182016-12-21 10:44:52 +0000296 int32_t *tmpbuf2 = flt2 + RESTORATION_TILEPELS_MAX;
David Barker506eb722017-03-08 13:35:49 +0000297 int ep, bestep = 0;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700298 int64_t err, besterr = -1;
299 int exqd[2], bestxqd[2] = { 0, 0 };
David Barker3a0df182016-12-21 10:44:52 +0000300
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700301 for (ep = 0; ep < SGRPROJ_PARAMS; ep++) {
302 int exq[2];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200303#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700304 if (bit_depth > 8) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700305 uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800306#if USE_HIGHPASS_IN_SGRPROJ
307 av1_highpass_filter_highbd(dat, width, height, dat_stride, flt1, width,
David Barkereed824e2017-03-10 11:35:22 +0000308 sgr_params[ep].corner, sgr_params[ep].edge);
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800309#else
David Barker506eb722017-03-08 13:35:49 +0000310 av1_selfguided_restoration_highbd(dat, width, height, dat_stride, flt1,
311 width, bit_depth, sgr_params[ep].r1,
312 sgr_params[ep].e1, tmpbuf2);
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800313#endif // USE_HIGHPASS_IN_SGRPROJ
David Barker506eb722017-03-08 13:35:49 +0000314 av1_selfguided_restoration_highbd(dat, width, height, dat_stride, flt2,
315 width, bit_depth, sgr_params[ep].r2,
316 sgr_params[ep].e2, tmpbuf2);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700317 } else {
David Barker506eb722017-03-08 13:35:49 +0000318#endif
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800319#if USE_HIGHPASS_IN_SGRPROJ
320 av1_highpass_filter(dat8, width, height, dat_stride, flt1, width,
David Barkereed824e2017-03-10 11:35:22 +0000321 sgr_params[ep].corner, sgr_params[ep].edge);
Debargha Mukherjeeb7bb0972017-03-09 06:47:43 -0800322#else
323 av1_selfguided_restoration(dat8, width, height, dat_stride, flt1, width,
324 sgr_params[ep].r1, sgr_params[ep].e1, tmpbuf2);
325#endif // USE_HIGHPASS_IN_SGRPROJ
David Barker506eb722017-03-08 13:35:49 +0000326 av1_selfguided_restoration(dat8, width, height, dat_stride, flt2, width,
David Barker4d2af5d2017-03-09 11:46:50 +0000327 sgr_params[ep].r2, sgr_params[ep].e2, tmpbuf2);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200328#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700329 }
David Barker506eb722017-03-08 13:35:49 +0000330#endif
David Barker3a0df182016-12-21 10:44:52 +0000331 get_proj_subspace(src8, width, height, src_stride, dat8, dat_stride,
332 bit_depth, flt1, width, flt2, width, exq);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700333 encode_xq(exq, exqd);
David Barker3a0df182016-12-21 10:44:52 +0000334 err =
335 get_pixel_proj_error(src8, width, height, src_stride, dat8, dat_stride,
336 bit_depth, flt1, width, flt2, width, exqd);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700337 if (besterr == -1 || err < besterr) {
338 bestep = ep;
339 besterr = err;
340 bestxqd[0] = exqd[0];
341 bestxqd[1] = exqd[1];
342 }
343 }
344 *eps = bestep;
345 xqd[0] = bestxqd[0];
346 xqd[1] = bestxqd[1];
347}
348
349static double search_sgrproj(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi,
Debargha Mukherjee00c54332017-03-03 15:44:17 -0800350 int partial_frame, RestorationInfo *info,
351 RestorationType *type, double *best_tile_cost,
David Barker9666e752016-12-08 11:25:47 +0000352 YV12_BUFFER_CONFIG *dst_frame) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700353 SgrprojInfo *sgrproj_info = info->sgrproj_info;
354 double err, cost_norestore, cost_sgrproj;
355 int bits;
356 MACROBLOCK *x = &cpi->td.mb;
357 AV1_COMMON *const cm = &cpi->common;
358 const YV12_BUFFER_CONFIG *dgd = cm->frame_to_show;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800359 RestorationInfo *rsi = &cpi->rst_search[0];
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700360 int tile_idx, tile_width, tile_height, nhtiles, nvtiles;
361 int h_start, h_end, v_start, v_end;
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800362 // Allocate for the src buffer at high precision
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -0800363 const int ntiles = av1_get_rest_ntiles(
364 cm->width, cm->height, cm->rst_info[0].restoration_tilesize, &tile_width,
365 &tile_height, &nhtiles, &nvtiles);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800366 rsi->frame_restoration_type = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700367
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800368 for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) {
369 rsi->restoration_type[tile_idx] = RESTORE_NONE;
370 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700371 // Compute best Sgrproj filters for each tile
372 for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) {
373 av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width,
374 tile_height, cm->width, cm->height, 0, 0, &h_start,
375 &h_end, &v_start, &v_end);
David Barker9666e752016-12-08 11:25:47 +0000376 err = sse_restoration_tile(src, cm->frame_to_show, cm, h_start,
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800377 h_end - h_start, v_start, v_end - v_start, 1);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700378 // #bits when a tile is not restored
379 bits = av1_cost_bit(RESTORE_NONE_SGRPROJ_PROB, 0);
380 cost_norestore = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err);
381 best_tile_cost[tile_idx] = DBL_MAX;
382 search_selfguided_restoration(
383 dgd->y_buffer + v_start * dgd->y_stride + h_start, h_end - h_start,
384 v_end - v_start, dgd->y_stride,
385 src->y_buffer + v_start * src->y_stride + h_start, src->y_stride,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200386#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700387 cm->bit_depth,
388#else
389 8,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200390#endif // CONFIG_HIGHBITDEPTH
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800391 &rsi->sgrproj_info[tile_idx].ep, rsi->sgrproj_info[tile_idx].xqd,
David Barker3a0df182016-12-21 10:44:52 +0000392 cm->rst_internal.tmpbuf);
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800393 rsi->restoration_type[tile_idx] = RESTORE_SGRPROJ;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800394 err = try_restoration_tile(src, cpi, rsi, 1, partial_frame, tile_idx, 0, 0,
David Barker9666e752016-12-08 11:25:47 +0000395 dst_frame);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700396 bits = SGRPROJ_BITS << AV1_PROB_COST_SHIFT;
397 bits += av1_cost_bit(RESTORE_NONE_SGRPROJ_PROB, 1);
398 cost_sgrproj = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err);
399 if (cost_sgrproj >= cost_norestore) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800400 type[tile_idx] = RESTORE_NONE;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700401 } else {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800402 type[tile_idx] = RESTORE_SGRPROJ;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800403 memcpy(&sgrproj_info[tile_idx], &rsi->sgrproj_info[tile_idx],
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700404 sizeof(sgrproj_info[tile_idx]));
405 bits = SGRPROJ_BITS << AV1_PROB_COST_SHIFT;
406 best_tile_cost[tile_idx] = RDCOST_DBL(
407 x->rdmult, x->rddiv,
408 (bits + cpi->switchable_restore_cost[RESTORE_SGRPROJ]) >> 4, err);
409 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800410 rsi->restoration_type[tile_idx] = RESTORE_NONE;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700411 }
412 // Cost for Sgrproj filtering
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800413 bits = frame_level_restore_bits[rsi->frame_restoration_type]
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700414 << AV1_PROB_COST_SHIFT;
415 for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) {
416 bits +=
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800417 av1_cost_bit(RESTORE_NONE_SGRPROJ_PROB, type[tile_idx] != RESTORE_NONE);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800418 memcpy(&rsi->sgrproj_info[tile_idx], &sgrproj_info[tile_idx],
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700419 sizeof(sgrproj_info[tile_idx]));
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800420 if (type[tile_idx] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700421 bits += (SGRPROJ_BITS << AV1_PROB_COST_SHIFT);
422 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800423 rsi->restoration_type[tile_idx] = type[tile_idx];
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700424 }
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800425 err = try_restoration_frame(src, cpi, rsi, 1, partial_frame, dst_frame);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700426 cost_sgrproj = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err);
427
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700428 return cost_sgrproj;
429}
430
Yaowu Xuc27fc142016-08-22 16:08:15 -0700431static double find_average(uint8_t *src, int h_start, int h_end, int v_start,
432 int v_end, int stride) {
433 uint64_t sum = 0;
434 double avg = 0;
435 int i, j;
Jingning Han041c67b2017-04-14 21:39:26 -0700436 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700437 for (i = v_start; i < v_end; i++)
438 for (j = h_start; j < h_end; j++) sum += src[i * stride + j];
439 avg = (double)sum / ((v_end - v_start) * (h_end - h_start));
440 return avg;
441}
442
443static void compute_stats(uint8_t *dgd, uint8_t *src, int h_start, int h_end,
444 int v_start, int v_end, int dgd_stride,
445 int src_stride, double *M, double *H) {
446 int i, j, k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800447 double Y[WIENER_WIN2];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700448 const double avg =
449 find_average(dgd, h_start, h_end, v_start, v_end, dgd_stride);
450
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800451 memset(M, 0, sizeof(*M) * WIENER_WIN2);
452 memset(H, 0, sizeof(*H) * WIENER_WIN2 * WIENER_WIN2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700453 for (i = v_start; i < v_end; i++) {
454 for (j = h_start; j < h_end; j++) {
455 const double X = (double)src[i * src_stride + j] - avg;
456 int idx = 0;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800457 for (k = -WIENER_HALFWIN; k <= WIENER_HALFWIN; k++) {
458 for (l = -WIENER_HALFWIN; l <= WIENER_HALFWIN; l++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700459 Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg;
460 idx++;
461 }
462 }
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800463 for (k = 0; k < WIENER_WIN2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700464 M[k] += Y[k] * X;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800465 H[k * WIENER_WIN2 + k] += Y[k] * Y[k];
466 for (l = k + 1; l < WIENER_WIN2; ++l) {
David Barker33f3bfd2017-01-06 15:34:50 +0000467 // H is a symmetric matrix, so we only need to fill out the upper
468 // triangle here. We can copy it down to the lower triangle outside
469 // the (i, j) loops.
470 H[k * WIENER_WIN2 + l] += Y[k] * Y[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700471 }
472 }
473 }
474 }
David Barker33f3bfd2017-01-06 15:34:50 +0000475 for (k = 0; k < WIENER_WIN2; ++k) {
476 for (l = k + 1; l < WIENER_WIN2; ++l) {
477 H[l * WIENER_WIN2 + k] = H[k * WIENER_WIN2 + l];
478 }
479 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700480}
481
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200482#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700483static double find_average_highbd(uint16_t *src, int h_start, int h_end,
484 int v_start, int v_end, int stride) {
485 uint64_t sum = 0;
486 double avg = 0;
487 int i, j;
Jingning Han041c67b2017-04-14 21:39:26 -0700488 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700489 for (i = v_start; i < v_end; i++)
490 for (j = h_start; j < h_end; j++) sum += src[i * stride + j];
491 avg = (double)sum / ((v_end - v_start) * (h_end - h_start));
492 return avg;
493}
494
495static void compute_stats_highbd(uint8_t *dgd8, uint8_t *src8, int h_start,
496 int h_end, int v_start, int v_end,
497 int dgd_stride, int src_stride, double *M,
498 double *H) {
499 int i, j, k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800500 double Y[WIENER_WIN2];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700501 uint16_t *src = CONVERT_TO_SHORTPTR(src8);
502 uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
503 const double avg =
504 find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
505
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800506 memset(M, 0, sizeof(*M) * WIENER_WIN2);
507 memset(H, 0, sizeof(*H) * WIENER_WIN2 * WIENER_WIN2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508 for (i = v_start; i < v_end; i++) {
509 for (j = h_start; j < h_end; j++) {
510 const double X = (double)src[i * src_stride + j] - avg;
511 int idx = 0;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800512 for (k = -WIENER_HALFWIN; k <= WIENER_HALFWIN; k++) {
513 for (l = -WIENER_HALFWIN; l <= WIENER_HALFWIN; l++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700514 Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg;
515 idx++;
516 }
517 }
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800518 for (k = 0; k < WIENER_WIN2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700519 M[k] += Y[k] * X;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800520 H[k * WIENER_WIN2 + k] += Y[k] * Y[k];
521 for (l = k + 1; l < WIENER_WIN2; ++l) {
David Barker33f3bfd2017-01-06 15:34:50 +0000522 // H is a symmetric matrix, so we only need to fill out the upper
523 // triangle here. We can copy it down to the lower triangle outside
524 // the (i, j) loops.
525 H[k * WIENER_WIN2 + l] += Y[k] * Y[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700526 }
527 }
528 }
529 }
David Barker33f3bfd2017-01-06 15:34:50 +0000530 for (k = 0; k < WIENER_WIN2; ++k) {
531 for (l = k + 1; l < WIENER_WIN2; ++l) {
532 H[l * WIENER_WIN2 + k] = H[k * WIENER_WIN2 + l];
533 }
534 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700535}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200536#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700537
538// Solves Ax = b, where x and b are column vectors
539static int linsolve(int n, double *A, int stride, double *b, double *x) {
540 int i, j, k;
541 double c;
Jingning Han041c67b2017-04-14 21:39:26 -0700542
543 aom_clear_system_state();
544
Yaowu Xuc27fc142016-08-22 16:08:15 -0700545 // Forward elimination
546 for (k = 0; k < n - 1; k++) {
Jingning Han041c67b2017-04-14 21:39:26 -0700547 // Bring the largest magitude to the diagonal position
548 for (i = n - 1; i > k; i--) {
549 if (fabs(A[(i - 1) * stride + k]) < fabs(A[i * stride + k])) {
550 for (j = 0; j < n; j++) {
551 c = A[i * stride + j];
552 A[i * stride + j] = A[(i - 1) * stride + j];
553 A[(i - 1) * stride + j] = c;
554 }
555 c = b[i];
556 b[i] = b[i - 1];
557 b[i - 1] = c;
558 }
559 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700560 for (i = k; i < n - 1; i++) {
Jingning Han041c67b2017-04-14 21:39:26 -0700561 if (fabs(A[k * stride + k]) < 1e-10) return 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562 c = A[(i + 1) * stride + k] / A[k * stride + k];
563 for (j = 0; j < n; j++) A[(i + 1) * stride + j] -= c * A[k * stride + j];
564 b[i + 1] -= c * b[k];
565 }
566 }
567 // Backward substitution
568 for (i = n - 1; i >= 0; i--) {
569 if (fabs(A[i * stride + i]) < 1e-10) return 0;
570 c = 0;
571 for (j = i + 1; j <= n - 1; j++) c += A[i * stride + j] * x[j];
572 x[i] = (b[i] - c) / A[i * stride + i];
573 }
Jingning Han041c67b2017-04-14 21:39:26 -0700574
Yaowu Xuc27fc142016-08-22 16:08:15 -0700575 return 1;
576}
577
578static INLINE int wrap_index(int i) {
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800579 return (i >= WIENER_HALFWIN1 ? WIENER_WIN - 1 - i : i);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700580}
581
582// Fix vector b, update vector a
583static void update_a_sep_sym(double **Mc, double **Hc, double *a, double *b) {
584 int i, j;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800585 double S[WIENER_WIN];
Debargha Mukherjee6ae588f2017-04-14 00:40:02 -0700586 double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700587 int w, w2;
588 memset(A, 0, sizeof(A));
589 memset(B, 0, sizeof(B));
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800590 for (i = 0; i < WIENER_WIN; i++) {
591 for (j = 0; j < WIENER_WIN; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700592 const int jj = wrap_index(j);
593 A[jj] += Mc[i][j] * b[i];
594 }
595 }
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800596 for (i = 0; i < WIENER_WIN; i++) {
597 for (j = 0; j < WIENER_WIN; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700598 int k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800599 for (k = 0; k < WIENER_WIN; ++k)
600 for (l = 0; l < WIENER_WIN; ++l) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601 const int kk = wrap_index(k);
602 const int ll = wrap_index(l);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800603 B[ll * WIENER_HALFWIN1 + kk] +=
604 Hc[j * WIENER_WIN + i][k * WIENER_WIN2 + l] * b[i] * b[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700605 }
606 }
607 }
608 // Normalization enforcement in the system of equations itself
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800609 w = WIENER_WIN;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610 w2 = (w >> 1) + 1;
611 for (i = 0; i < w2 - 1; ++i)
612 A[i] -=
613 A[w2 - 1] * 2 + B[i * w2 + w2 - 1] - 2 * B[(w2 - 1) * w2 + (w2 - 1)];
614 for (i = 0; i < w2 - 1; ++i)
615 for (j = 0; j < w2 - 1; ++j)
616 B[i * w2 + j] -= 2 * (B[i * w2 + (w2 - 1)] + B[(w2 - 1) * w2 + j] -
617 2 * B[(w2 - 1) * w2 + (w2 - 1)]);
618 if (linsolve(w2 - 1, B, w2, A, S)) {
619 S[w2 - 1] = 1.0;
620 for (i = w2; i < w; ++i) {
621 S[i] = S[w - 1 - i];
622 S[w2 - 1] -= 2 * S[i];
623 }
624 memcpy(a, S, w * sizeof(*a));
625 }
626}
627
628// Fix vector a, update vector b
629static void update_b_sep_sym(double **Mc, double **Hc, double *a, double *b) {
630 int i, j;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800631 double S[WIENER_WIN];
Debargha Mukherjee6ae588f2017-04-14 00:40:02 -0700632 double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700633 int w, w2;
634 memset(A, 0, sizeof(A));
635 memset(B, 0, sizeof(B));
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800636 for (i = 0; i < WIENER_WIN; i++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637 const int ii = wrap_index(i);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800638 for (j = 0; j < WIENER_WIN; j++) A[ii] += Mc[i][j] * a[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700639 }
640
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800641 for (i = 0; i < WIENER_WIN; i++) {
642 for (j = 0; j < WIENER_WIN; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700643 const int ii = wrap_index(i);
644 const int jj = wrap_index(j);
645 int k, l;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800646 for (k = 0; k < WIENER_WIN; ++k)
647 for (l = 0; l < WIENER_WIN; ++l)
648 B[jj * WIENER_HALFWIN1 + ii] +=
649 Hc[i * WIENER_WIN + j][k * WIENER_WIN2 + l] * a[k] * a[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700650 }
651 }
652 // Normalization enforcement in the system of equations itself
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800653 w = WIENER_WIN;
654 w2 = WIENER_HALFWIN1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655 for (i = 0; i < w2 - 1; ++i)
656 A[i] -=
657 A[w2 - 1] * 2 + B[i * w2 + w2 - 1] - 2 * B[(w2 - 1) * w2 + (w2 - 1)];
658 for (i = 0; i < w2 - 1; ++i)
659 for (j = 0; j < w2 - 1; ++j)
660 B[i * w2 + j] -= 2 * (B[i * w2 + (w2 - 1)] + B[(w2 - 1) * w2 + j] -
661 2 * B[(w2 - 1) * w2 + (w2 - 1)]);
662 if (linsolve(w2 - 1, B, w2, A, S)) {
663 S[w2 - 1] = 1.0;
664 for (i = w2; i < w; ++i) {
665 S[i] = S[w - 1 - i];
666 S[w2 - 1] -= 2 * S[i];
667 }
668 memcpy(b, S, w * sizeof(*b));
669 }
670}
671
672static int wiener_decompose_sep_sym(double *M, double *H, double *a,
673 double *b) {
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800674 static const double init_filt[WIENER_WIN] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700675 0.035623, -0.127154, 0.211436, 0.760190, 0.211436, -0.127154, 0.035623,
676 };
677 int i, j, iter;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800678 double *Hc[WIENER_WIN2];
679 double *Mc[WIENER_WIN];
680 for (i = 0; i < WIENER_WIN; i++) {
681 Mc[i] = M + i * WIENER_WIN;
682 for (j = 0; j < WIENER_WIN; j++) {
683 Hc[i * WIENER_WIN + j] =
684 H + i * WIENER_WIN * WIENER_WIN2 + j * WIENER_WIN;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700685 }
686 }
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800687 memcpy(a, init_filt, sizeof(*a) * WIENER_WIN);
688 memcpy(b, init_filt, sizeof(*b) * WIENER_WIN);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689
690 iter = 1;
Debargha Mukherjee1b3dbf02017-03-13 14:47:21 -0700691 while (iter < NUM_WIENER_ITERS) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700692 update_a_sep_sym(Mc, Hc, a, b);
693 update_b_sep_sym(Mc, Hc, a, b);
694 iter++;
695 }
696 return 1;
697}
698
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800699// Computes the function x'*H*x - x'*M for the learned 2D filter x, and compares
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700// against identity filters; Final score is defined as the difference between
701// the function values
David Barker1e8e6b92017-01-13 13:45:51 +0000702static double compute_score(double *M, double *H, InterpKernel vfilt,
703 InterpKernel hfilt) {
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800704 double ab[WIENER_WIN * WIENER_WIN];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700705 int i, k, l;
706 double P = 0, Q = 0;
707 double iP = 0, iQ = 0;
708 double Score, iScore;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800709 double a[WIENER_WIN], b[WIENER_WIN];
Jingning Han041c67b2017-04-14 21:39:26 -0700710
711 aom_clear_system_state();
712
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800713 a[WIENER_HALFWIN] = b[WIENER_HALFWIN] = 1.0;
714 for (i = 0; i < WIENER_HALFWIN; ++i) {
715 a[i] = a[WIENER_WIN - i - 1] = (double)vfilt[i] / WIENER_FILT_STEP;
716 b[i] = b[WIENER_WIN - i - 1] = (double)hfilt[i] / WIENER_FILT_STEP;
717 a[WIENER_HALFWIN] -= 2 * a[i];
718 b[WIENER_HALFWIN] -= 2 * b[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700719 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800720 for (k = 0; k < WIENER_WIN; ++k) {
721 for (l = 0; l < WIENER_WIN; ++l) ab[k * WIENER_WIN + l] = a[l] * b[k];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700722 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800723 for (k = 0; k < WIENER_WIN2; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700724 P += ab[k] * M[k];
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800725 for (l = 0; l < WIENER_WIN2; ++l)
726 Q += ab[k] * H[k * WIENER_WIN2 + l] * ab[l];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700727 }
728 Score = Q - 2 * P;
729
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800730 iP = M[WIENER_WIN2 >> 1];
731 iQ = H[(WIENER_WIN2 >> 1) * WIENER_WIN2 + (WIENER_WIN2 >> 1)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 iScore = iQ - 2 * iP;
733
734 return Score - iScore;
735}
736
David Barker1e8e6b92017-01-13 13:45:51 +0000737static void quantize_sym_filter(double *f, InterpKernel fi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700738 int i;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800739 for (i = 0; i < WIENER_HALFWIN; ++i) {
740 fi[i] = RINT(f[i] * WIENER_FILT_STEP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741 }
742 // Specialize for 7-tap filter
743 fi[0] = CLIP(fi[0], WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP0_MAXV);
744 fi[1] = CLIP(fi[1], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
745 fi[2] = CLIP(fi[2], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800746 // Satisfy filter constraints
747 fi[WIENER_WIN - 1] = fi[0];
748 fi[WIENER_WIN - 2] = fi[1];
749 fi[WIENER_WIN - 3] = fi[2];
David Barker1e8e6b92017-01-13 13:45:51 +0000750 // The central element has an implicit +WIENER_FILT_STEP
751 fi[3] = -2 * (fi[0] + fi[1] + fi[2]);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800752}
753
754static double search_wiener_uv(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi,
Debargha Mukherjee00c54332017-03-03 15:44:17 -0800755 int partial_frame, int plane,
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800756 RestorationInfo *info, RestorationType *type,
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800757 YV12_BUFFER_CONFIG *dst_frame) {
758 WienerInfo *wiener_info = info->wiener_info;
759 AV1_COMMON *const cm = &cpi->common;
760 RestorationInfo *rsi = cpi->rst_search;
761 int64_t err;
762 int bits;
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800763 double cost_wiener, cost_norestore, cost_wiener_frame, cost_norestore_frame;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800764 MACROBLOCK *x = &cpi->td.mb;
765 double M[WIENER_WIN2];
766 double H[WIENER_WIN2 * WIENER_WIN2];
767 double vfilterd[WIENER_WIN], hfilterd[WIENER_WIN];
768 const YV12_BUFFER_CONFIG *dgd = cm->frame_to_show;
769 const int width = src->uv_crop_width;
770 const int height = src->uv_crop_height;
771 const int src_stride = src->uv_stride;
772 const int dgd_stride = dgd->uv_stride;
773 double score;
774 int tile_idx, tile_width, tile_height, nhtiles, nvtiles;
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800775 int h_start, h_end, v_start, v_end;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -0800776 const int ntiles =
777 av1_get_rest_ntiles(width, height, cm->rst_info[1].restoration_tilesize,
778 &tile_width, &tile_height, &nhtiles, &nvtiles);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800779 assert(width == dgd->uv_crop_width);
780 assert(height == dgd->uv_crop_height);
781
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800782 rsi[plane].frame_restoration_type = RESTORE_NONE;
David Barker60a055b2017-01-18 15:10:43 +0000783 err = sse_restoration_frame(cm, src, cm->frame_to_show, (1 << plane));
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800784 bits = 0;
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800785 cost_norestore_frame = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800786
787 rsi[plane].frame_restoration_type = RESTORE_WIENER;
David Barker33f3bfd2017-01-06 15:34:50 +0000788
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800789 for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) {
790 rsi[plane].restoration_type[tile_idx] = RESTORE_NONE;
791 }
792
793 // Compute best Wiener filters for each tile
794 for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) {
795 av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width,
796 tile_height, width, height, 0, 0, &h_start, &h_end,
797 &v_start, &v_end);
798 err = sse_restoration_tile(src, cm->frame_to_show, cm, h_start,
799 h_end - h_start, v_start, v_end - v_start,
800 1 << plane);
801 // #bits when a tile is not restored
802 bits = av1_cost_bit(RESTORE_NONE_WIENER_PROB, 0);
803 cost_norestore = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err);
804 // best_tile_cost[tile_idx] = DBL_MAX;
805
806 av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width,
807 tile_height, width, height, WIENER_HALFWIN,
808 WIENER_HALFWIN, &h_start, &h_end, &v_start,
809 &v_end);
David Barker33f3bfd2017-01-06 15:34:50 +0000810 if (plane == AOM_PLANE_U) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200811#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800812 if (cm->use_highbitdepth)
813 compute_stats_highbd(dgd->u_buffer, src->u_buffer, h_start, h_end,
814 v_start, v_end, dgd_stride, src_stride, M, H);
815 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200816#endif // CONFIG_HIGHBITDEPTH
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800817 compute_stats(dgd->u_buffer, src->u_buffer, h_start, h_end, v_start,
818 v_end, dgd_stride, src_stride, M, H);
David Barker33f3bfd2017-01-06 15:34:50 +0000819 } else if (plane == AOM_PLANE_V) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200820#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800821 if (cm->use_highbitdepth)
822 compute_stats_highbd(dgd->v_buffer, src->v_buffer, h_start, h_end,
823 v_start, v_end, dgd_stride, src_stride, M, H);
824 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200825#endif // CONFIG_HIGHBITDEPTH
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800826 compute_stats(dgd->v_buffer, src->v_buffer, h_start, h_end, v_start,
827 v_end, dgd_stride, src_stride, M, H);
David Barker33f3bfd2017-01-06 15:34:50 +0000828 } else {
829 assert(0);
830 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800831
832 type[tile_idx] = RESTORE_WIENER;
833
834 if (!wiener_decompose_sep_sym(M, H, vfilterd, hfilterd)) {
835 type[tile_idx] = RESTORE_NONE;
836 continue;
David Barker33f3bfd2017-01-06 15:34:50 +0000837 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800838 quantize_sym_filter(vfilterd, rsi[plane].wiener_info[tile_idx].vfilter);
839 quantize_sym_filter(hfilterd, rsi[plane].wiener_info[tile_idx].hfilter);
David Barker33f3bfd2017-01-06 15:34:50 +0000840
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800841 // Filter score computes the value of the function x'*A*x - x'*b for the
842 // learned filter and compares it against identity filer. If there is no
843 // reduction in the function, the filter is reverted back to identity
844 score = compute_score(M, H, rsi[plane].wiener_info[tile_idx].vfilter,
845 rsi[plane].wiener_info[tile_idx].hfilter);
846 if (score > 0.0) {
847 type[tile_idx] = RESTORE_NONE;
848 continue;
849 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800850
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800851 rsi[plane].restoration_type[tile_idx] = RESTORE_WIENER;
852 err = try_restoration_tile(src, cpi, rsi, 1 << plane, partial_frame,
853 tile_idx, 0, 0, dst_frame);
854 bits = WIENER_FILT_BITS << AV1_PROB_COST_SHIFT;
855 bits += av1_cost_bit(RESTORE_NONE_WIENER_PROB, 1);
856 cost_wiener = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err);
857 if (cost_wiener >= cost_norestore) {
858 type[tile_idx] = RESTORE_NONE;
859 } else {
860 type[tile_idx] = RESTORE_WIENER;
861 memcpy(&wiener_info[tile_idx], &rsi[plane].wiener_info[tile_idx],
862 sizeof(wiener_info[tile_idx]));
863 }
864 rsi[plane].restoration_type[tile_idx] = RESTORE_NONE;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800865 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800866 // Cost for Wiener filtering
867 bits = 0;
868 for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) {
869 bits +=
870 av1_cost_bit(RESTORE_NONE_WIENER_PROB, type[tile_idx] != RESTORE_NONE);
871 memcpy(&rsi[plane].wiener_info[tile_idx], &wiener_info[tile_idx],
872 sizeof(wiener_info[tile_idx]));
873 if (type[tile_idx] == RESTORE_WIENER) {
874 bits += (WIENER_FILT_BITS << AV1_PROB_COST_SHIFT);
875 }
876 rsi[plane].restoration_type[tile_idx] = type[tile_idx];
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800877 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800878 err = try_restoration_frame(src, cpi, rsi, 1 << plane, partial_frame,
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800879 dst_frame);
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800880 cost_wiener_frame = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err);
881
882 if (cost_wiener_frame < cost_norestore_frame) {
883 info->frame_restoration_type = RESTORE_WIENER;
884 } else {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800885 info->frame_restoration_type = RESTORE_NONE;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800886 }
887
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800888 return info->frame_restoration_type == RESTORE_WIENER ? cost_wiener_frame
889 : cost_norestore_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700890}
891
Debargha Mukherjee5d89a632016-09-17 13:16:58 -0700892static double search_wiener(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi,
Debargha Mukherjee00c54332017-03-03 15:44:17 -0800893 int partial_frame, RestorationInfo *info,
894 RestorationType *type, double *best_tile_cost,
David Barker9666e752016-12-08 11:25:47 +0000895 YV12_BUFFER_CONFIG *dst_frame) {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -0700896 WienerInfo *wiener_info = info->wiener_info;
Yaowu Xuf883b422016-08-30 14:01:10 -0700897 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800898 RestorationInfo *rsi = cpi->rst_search;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700899 int64_t err;
900 int bits;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700901 double cost_wiener, cost_norestore;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902 MACROBLOCK *x = &cpi->td.mb;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800903 double M[WIENER_WIN2];
904 double H[WIENER_WIN2 * WIENER_WIN2];
905 double vfilterd[WIENER_WIN], hfilterd[WIENER_WIN];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700906 const YV12_BUFFER_CONFIG *dgd = cm->frame_to_show;
907 const int width = cm->width;
908 const int height = cm->height;
909 const int src_stride = src->y_stride;
910 const int dgd_stride = dgd->y_stride;
911 double score;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700912 int tile_idx, tile_width, tile_height, nhtiles, nvtiles;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700913 int h_start, h_end, v_start, v_end;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -0800914 const int ntiles =
915 av1_get_rest_ntiles(width, height, cm->rst_info[0].restoration_tilesize,
916 &tile_width, &tile_height, &nhtiles, &nvtiles);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700917 assert(width == dgd->y_crop_width);
918 assert(height == dgd->y_crop_height);
919 assert(width == src->y_crop_width);
920 assert(height == src->y_crop_height);
921
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800922 rsi->frame_restoration_type = RESTORE_WIENER;
Debargha Mukherjee5d89a632016-09-17 13:16:58 -0700923
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800924 for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) {
925 rsi->restoration_type[tile_idx] = RESTORE_NONE;
926 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700927
David Barker33f3bfd2017-01-06 15:34:50 +0000928// Construct a (WIENER_HALFWIN)-pixel border around the frame
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200929#if CONFIG_HIGHBITDEPTH
David Barker33f3bfd2017-01-06 15:34:50 +0000930 if (cm->use_highbitdepth)
931 extend_frame_highbd(CONVERT_TO_SHORTPTR(dgd->y_buffer), width, height,
932 dgd_stride);
933 else
934#endif
935 extend_frame(dgd->y_buffer, width, height, dgd_stride);
936
Yaowu Xuc27fc142016-08-22 16:08:15 -0700937 // Compute best Wiener filters for each tile
938 for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700939 av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width,
940 tile_height, width, height, 0, 0, &h_start, &h_end,
941 &v_start, &v_end);
David Barker9666e752016-12-08 11:25:47 +0000942 err = sse_restoration_tile(src, cm->frame_to_show, cm, h_start,
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800943 h_end - h_start, v_start, v_end - v_start, 1);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700944 // #bits when a tile is not restored
945 bits = av1_cost_bit(RESTORE_NONE_WIENER_PROB, 0);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700946 cost_norestore = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -0700947 best_tile_cost[tile_idx] = DBL_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700948
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700949 av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width,
David Barker33f3bfd2017-01-06 15:34:50 +0000950 tile_height, width, height, 0, 0, &h_start, &h_end,
951 &v_start, &v_end);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200952#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700953 if (cm->use_highbitdepth)
954 compute_stats_highbd(dgd->y_buffer, src->y_buffer, h_start, h_end,
955 v_start, v_end, dgd_stride, src_stride, M, H);
956 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200957#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700958 compute_stats(dgd->y_buffer, src->y_buffer, h_start, h_end, v_start,
959 v_end, dgd_stride, src_stride, M, H);
960
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800961 type[tile_idx] = RESTORE_WIENER;
962
Yaowu Xuc27fc142016-08-22 16:08:15 -0700963 if (!wiener_decompose_sep_sym(M, H, vfilterd, hfilterd)) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800964 type[tile_idx] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700965 continue;
966 }
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800967 quantize_sym_filter(vfilterd, rsi->wiener_info[tile_idx].vfilter);
968 quantize_sym_filter(hfilterd, rsi->wiener_info[tile_idx].hfilter);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700969
970 // Filter score computes the value of the function x'*A*x - x'*b for the
971 // learned filter and compares it against identity filer. If there is no
972 // reduction in the function, the filter is reverted back to identity
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800973 score = compute_score(M, H, rsi->wiener_info[tile_idx].vfilter,
974 rsi->wiener_info[tile_idx].hfilter);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700975 if (score > 0.0) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800976 type[tile_idx] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700977 continue;
978 }
979
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800980 rsi->restoration_type[tile_idx] = RESTORE_WIENER;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800981 err = try_restoration_tile(src, cpi, rsi, 1, partial_frame, tile_idx, 0, 0,
David Barker9666e752016-12-08 11:25:47 +0000982 dst_frame);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700983 bits = WIENER_FILT_BITS << AV1_PROB_COST_SHIFT;
984 bits += av1_cost_bit(RESTORE_NONE_WIENER_PROB, 1);
985 cost_wiener = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -0700986 if (cost_wiener >= cost_norestore) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800987 type[tile_idx] = RESTORE_NONE;
Debargha Mukherjee5d89a632016-09-17 13:16:58 -0700988 } else {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800989 type[tile_idx] = RESTORE_WIENER;
990 memcpy(&wiener_info[tile_idx], &rsi->wiener_info[tile_idx],
991 sizeof(wiener_info[tile_idx]));
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700992 bits = WIENER_FILT_BITS << AV1_PROB_COST_SHIFT;
993 best_tile_cost[tile_idx] = RDCOST_DBL(
994 x->rdmult, x->rddiv,
995 (bits + cpi->switchable_restore_cost[RESTORE_WIENER]) >> 4, err);
996 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -0800997 rsi->restoration_type[tile_idx] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700998 }
999 // Cost for Wiener filtering
Debargha Mukherjee999d2f62016-12-15 13:23:21 -08001000 bits = frame_level_restore_bits[rsi->frame_restoration_type]
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001001 << AV1_PROB_COST_SHIFT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001002 for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001003 bits +=
1004 av1_cost_bit(RESTORE_NONE_WIENER_PROB, type[tile_idx] != RESTORE_NONE);
1005 memcpy(&rsi->wiener_info[tile_idx], &wiener_info[tile_idx],
1006 sizeof(wiener_info[tile_idx]));
1007 if (type[tile_idx] == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001008 bits += (WIENER_FILT_BITS << AV1_PROB_COST_SHIFT);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001009 }
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001010 rsi->restoration_type[tile_idx] = type[tile_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001011 }
Debargha Mukherjee999d2f62016-12-15 13:23:21 -08001012 err = try_restoration_frame(src, cpi, rsi, 1, partial_frame, dst_frame);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001013 cost_wiener = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001014
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001015 return cost_wiener;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001016}
1017
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001018static double search_norestore(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi,
Debargha Mukherjee00c54332017-03-03 15:44:17 -08001019 int partial_frame, RestorationInfo *info,
1020 RestorationType *type, double *best_tile_cost,
David Barker9666e752016-12-08 11:25:47 +00001021 YV12_BUFFER_CONFIG *dst_frame) {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001022 double err, cost_norestore;
1023 int bits;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001024 MACROBLOCK *x = &cpi->td.mb;
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001025 AV1_COMMON *const cm = &cpi->common;
1026 int tile_idx, tile_width, tile_height, nhtiles, nvtiles;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001027 int h_start, h_end, v_start, v_end;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001028 const int ntiles = av1_get_rest_ntiles(
1029 cm->width, cm->height, cm->rst_info[0].restoration_tilesize, &tile_width,
1030 &tile_height, &nhtiles, &nvtiles);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001031 (void)info;
David Barker9666e752016-12-08 11:25:47 +00001032 (void)dst_frame;
Debargha Mukherjee00c54332017-03-03 15:44:17 -08001033 (void)partial_frame;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001034
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001035 for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) {
1036 av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width,
1037 tile_height, cm->width, cm->height, 0, 0, &h_start,
1038 &h_end, &v_start, &v_end);
David Barker9666e752016-12-08 11:25:47 +00001039 err = sse_restoration_tile(src, cm->frame_to_show, cm, h_start,
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08001040 h_end - h_start, v_start, v_end - v_start, 1);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001041 best_tile_cost[tile_idx] =
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001042 RDCOST_DBL(x->rdmult, x->rddiv,
1043 (cpi->switchable_restore_cost[RESTORE_NONE] >> 4), err);
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001044 type[tile_idx] = RESTORE_NONE;
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001045 }
1046 // RD cost associated with no restoration
David Barker9666e752016-12-08 11:25:47 +00001047 err = sse_restoration_tile(src, cm->frame_to_show, cm, 0, cm->width, 0,
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08001048 cm->height, 1);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001049 bits = frame_level_restore_bits[RESTORE_NONE] << AV1_PROB_COST_SHIFT;
1050 cost_norestore = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001051 return cost_norestore;
1052}
1053
1054static double search_switchable_restoration(
Debargha Mukherjee00c54332017-03-03 15:44:17 -08001055 AV1_COMP *cpi, int partial_frame, RestorationInfo *rsi,
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001056 double *tile_cost[RESTORE_SWITCHABLE_TYPES]) {
1057 AV1_COMMON *const cm = &cpi->common;
1058 MACROBLOCK *x = &cpi->td.mb;
1059 double cost_switchable = 0;
Debargha Mukherjeefdbe3f72017-04-06 12:09:19 -07001060 int bits, tile_idx;
1061 RestorationType r;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001062 const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
1063 cm->rst_info[0].restoration_tilesize,
1064 NULL, NULL, NULL, NULL);
Debargha Mukherjee00c54332017-03-03 15:44:17 -08001065 (void)partial_frame;
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001066
1067 rsi->frame_restoration_type = RESTORE_SWITCHABLE;
1068 bits = frame_level_restore_bits[rsi->frame_restoration_type]
1069 << AV1_PROB_COST_SHIFT;
1070 cost_switchable = RDCOST_DBL(x->rdmult, x->rddiv, bits >> 4, 0);
1071 for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) {
1072 double best_cost = tile_cost[RESTORE_NONE][tile_idx];
1073 rsi->restoration_type[tile_idx] = RESTORE_NONE;
1074 for (r = 1; r < RESTORE_SWITCHABLE_TYPES; r++) {
Debargha Mukherjeefdbe3f72017-04-06 12:09:19 -07001075 if (r != force_restore_type) continue;
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001076 if (tile_cost[r][tile_idx] < best_cost) {
1077 rsi->restoration_type[tile_idx] = r;
1078 best_cost = tile_cost[r][tile_idx];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001079 }
1080 }
Debargha Mukherjeefdbe3f72017-04-06 12:09:19 -07001081 if (force_restore_type != 0)
1082 assert(rsi->restoration_type[tile_idx] == force_restore_type ||
1083 rsi->restoration_type[tile_idx] == RESTORE_NONE);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001084 cost_switchable += best_cost;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001085 }
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001086 return cost_switchable;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001087}
1088
1089void av1_pick_filter_restoration(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi,
Yaowu Xuf883b422016-08-30 14:01:10 -07001090 LPF_PICK_METHOD method) {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001091 static search_restore_type search_restore_fun[RESTORE_SWITCHABLE_TYPES] = {
Debargha Mukherjee4bfd72e2017-03-08 22:20:31 -08001092 search_norestore, search_wiener, search_sgrproj,
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001093 };
Yaowu Xuf883b422016-08-30 14:01:10 -07001094 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001095 double cost_restore[RESTORE_TYPES];
1096 double *tile_cost[RESTORE_SWITCHABLE_TYPES];
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001097 RestorationType *restore_types[RESTORE_SWITCHABLE_TYPES];
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001098 double best_cost_restore;
1099 RestorationType r, best_restore;
1100
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001101 const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
1102 cm->rst_info[0].restoration_tilesize,
1103 NULL, NULL, NULL, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001104
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001105 for (r = 0; r < RESTORE_SWITCHABLE_TYPES; r++) {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001106 tile_cost[r] = (double *)aom_malloc(sizeof(*tile_cost[0]) * ntiles);
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001107 restore_types[r] =
1108 (RestorationType *)aom_malloc(sizeof(*restore_types[0]) * ntiles);
1109 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001110
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001111 for (r = 0; r < RESTORE_SWITCHABLE_TYPES; ++r) {
Debargha Mukherjeefdbe3f72017-04-06 12:09:19 -07001112 if (force_restore_type != 0)
1113 if (r != RESTORE_NONE && r != force_restore_type) continue;
David Barker9666e752016-12-08 11:25:47 +00001114 cost_restore[r] = search_restore_fun[r](
Debargha Mukherjee00c54332017-03-03 15:44:17 -08001115 src, cpi, method == LPF_PICK_FROM_SUBIMAGE, &cm->rst_info[0],
1116 restore_types[r], tile_cost[r], &cpi->trial_frame_rst);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001117 }
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001118 cost_restore[RESTORE_SWITCHABLE] = search_switchable_restoration(
Debargha Mukherjee00c54332017-03-03 15:44:17 -08001119 cpi, method == LPF_PICK_FROM_SUBIMAGE, &cm->rst_info[0], tile_cost);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001120
1121 best_cost_restore = DBL_MAX;
1122 best_restore = 0;
1123 for (r = 0; r < RESTORE_TYPES; ++r) {
Debargha Mukherjeefdbe3f72017-04-06 12:09:19 -07001124 if (force_restore_type != 0)
1125 if (r != RESTORE_NONE && r != force_restore_type) continue;
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001126 if (cost_restore[r] < best_cost_restore) {
1127 best_restore = r;
1128 best_cost_restore = cost_restore[r];
1129 }
1130 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001131 cm->rst_info[0].frame_restoration_type = best_restore;
Debargha Mukherjeefdbe3f72017-04-06 12:09:19 -07001132 if (force_restore_type != 0)
1133 assert(best_restore == force_restore_type || best_restore == RESTORE_NONE);
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001134 if (best_restore != RESTORE_SWITCHABLE) {
1135 memcpy(cm->rst_info[0].restoration_type, restore_types[best_restore],
1136 ntiles * sizeof(restore_types[best_restore][0]));
1137 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001138
1139 // Color components
Debargha Mukherjee00c54332017-03-03 15:44:17 -08001140 search_wiener_uv(src, cpi, method == LPF_PICK_FROM_SUBIMAGE, AOM_PLANE_U,
1141 &cm->rst_info[AOM_PLANE_U],
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001142 cm->rst_info[AOM_PLANE_U].restoration_type,
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001143 &cpi->trial_frame_rst);
Debargha Mukherjee00c54332017-03-03 15:44:17 -08001144 search_wiener_uv(src, cpi, method == LPF_PICK_FROM_SUBIMAGE, AOM_PLANE_V,
1145 &cm->rst_info[AOM_PLANE_V],
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001146 cm->rst_info[AOM_PLANE_V].restoration_type,
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001147 &cpi->trial_frame_rst);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001148 /*
Debargha Mukherjee09ad6d82017-01-06 13:02:57 -08001149 printf("Frame %d/%d restore types: %d %d %d\n",
1150 cm->current_video_frame, cm->show_frame,
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001151 cm->rst_info[0].frame_restoration_type,
1152 cm->rst_info[1].frame_restoration_type,
1153 cm->rst_info[2].frame_restoration_type);
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08001154 printf("Frame %d/%d frame_restore_type %d : %f %f %f %f\n",
1155 cm->current_video_frame, cm->show_frame,
1156 cm->rst_info[0].frame_restoration_type, cost_restore[0],
1157 cost_restore[1], cost_restore[2], cost_restore[3]);
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07001158 */
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001159
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08001160 for (r = 0; r < RESTORE_SWITCHABLE_TYPES; r++) {
1161 aom_free(tile_cost[r]);
1162 aom_free(restore_types[r]);
1163 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001164}