1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * 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 https://www.aomedia.org/license/software-license. 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 https://www.aomedia.org/license/patent-license.
10 */
11
12 #include <math.h>
13
14 #include "EbPsnr.h"
15 #include "EbPictureControlSet.h"
16 #include "aom_dsp_rtcd.h"
17 #include "EbRestoration.h"
18 #include "EbRestorationPick.h"
19
20 #include "EbRestProcess.h"
21 #include "EbLog.h"
22
23 void av1_foreach_rest_unit_in_frame_seg(Av1Common *cm, int32_t plane, RestTileStartVisitor on_tile,
24 RestUnitVisitor on_rest_unit, void *priv,
25 uint8_t rest_segments_column_count,
26 uint8_t rest_segments_row_count, uint32_t segment_index);
27
28 void svt_av1_selfguided_restoration_c(const uint8_t *dgd8, int32_t width, int32_t height,
29 int32_t dgd_stride, int32_t *flt0, int32_t *flt1,
30 int32_t flt_stride, int32_t sgr_params_idx, int32_t bit_depth,
31 int32_t highbd);
32 void av1_foreach_rest_unit_in_frame(Av1Common *cm, int32_t plane, RestTileStartVisitor on_tile,
33 RestUnitVisitor on_rest_unit, void *priv);
34
35 // When set to RESTORE_WIENER or RESTORE_SGRPROJ only those are allowed.
36 // When set to RESTORE_TYPES we allow switchable.
37 //static const RestorationType force_restore_type = RESTORE_TYPES;
38
39 // Number of Wiener iterations
40 #define NUM_WIENER_ITERS 5
41 // Working precision for Wiener filter coefficients
42 #define WIENER_TAP_SCALE_FACTOR ((int64_t)1 << 16)
43
44 typedef int64_t (*SsePartExtractorType)(const Yv12BufferConfig *a, const Yv12BufferConfig *b,
45 int32_t hstart, int32_t width, int32_t vstart,
46 int32_t height);
47
48 #define NUM_EXTRACTORS (3 * (1 + 1))
49
50 static const SsePartExtractorType sse_part_extractors[NUM_EXTRACTORS] = {
51 svt_aom_get_y_sse_part,
52 svt_aom_get_u_sse_part,
53 svt_aom_get_v_sse_part,
54 svt_aom_highbd_get_y_sse_part,
55 svt_aom_highbd_get_u_sse_part,
56 svt_aom_highbd_get_v_sse_part,
57 };
sse_restoration_unit(const RestorationTileLimits * limits,const Yv12BufferConfig * src,const Yv12BufferConfig * dst,int32_t plane,int32_t highbd)58 static int64_t sse_restoration_unit(const RestorationTileLimits *limits,
59 const Yv12BufferConfig *src, const Yv12BufferConfig *dst,
60 int32_t plane, int32_t highbd) {
61 return sse_part_extractors[3 * highbd + plane](src,
62 dst,
63 limits->h_start,
64 limits->h_end - limits->h_start,
65 limits->v_start,
66 limits->v_end - limits->v_start);
67 }
68
69 typedef struct {
70 const Yv12BufferConfig *src;
71 Yv12BufferConfig * dst;
72
73 Av1Common * cm;
74 const Macroblock * x;
75 int32_t plane;
76 int32_t plane_width;
77 int32_t plane_height;
78 RestUnitSearchInfo *rusi;
79 RestUnitSearchInfo *rusi_pic;
80 uint32_t pic_num;
81 Yv12BufferConfig * org_frame_to_show;
82 int32_t * tmpbuf;
83
84 uint8_t * dgd_buffer;
85 int32_t dgd_stride;
86 const uint8_t *src_buffer;
87 int32_t src_stride;
88
89 // sse and bits are initialised by reset_rsc in search_rest_type
90 int64_t sse;
91 int64_t bits;
92 int32_t tile_y0, tile_stripe0;
93
94 // sgrproj and wiener are initialised by rsc_on_tile when starting the first
95 // tile in the frame.
96 SgrprojInfo sgrproj;
97 WienerInfo wiener;
98 } RestSearchCtxt;
99
rsc_on_tile(int32_t tile_row,int32_t tile_col,void * priv)100 static void rsc_on_tile(int32_t tile_row, int32_t tile_col, void *priv) {
101 (void)tile_col;
102
103 RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
104 set_default_sgrproj(&rsc->sgrproj);
105 set_default_wiener(&rsc->wiener);
106
107 rsc->tile_stripe0 = (tile_row == 0) ? 0 : rsc->cm->child_pcs->rst_end_stripe[tile_row - 1];
108 }
109
reset_rsc(RestSearchCtxt * rsc)110 static void reset_rsc(RestSearchCtxt *rsc) {
111 rsc->sse = 0;
112 rsc->bits = 0;
113 }
init_rsc_seg(Yv12BufferConfig * org_fts,const Yv12BufferConfig * src,Av1Common * cm,const Macroblock * x,int32_t plane,RestUnitSearchInfo * rusi,Yv12BufferConfig * dst,RestSearchCtxt * rsc)114 static void init_rsc_seg(Yv12BufferConfig *org_fts, const Yv12BufferConfig *src, Av1Common *cm,
115 const Macroblock *x, int32_t plane, RestUnitSearchInfo *rusi,
116 Yv12BufferConfig *dst, RestSearchCtxt *rsc) {
117 rsc->src = src;
118 rsc->dst = dst;
119 rsc->cm = cm;
120 rsc->x = x;
121 rsc->plane = plane;
122 rsc->rusi = rusi;
123
124 rsc->org_frame_to_show = org_fts;
125
126 const Yv12BufferConfig *dgd = org_fts;
127 const int32_t is_uv = plane != AOM_PLANE_Y;
128 rsc->plane_width = src->crop_widths[is_uv];
129 rsc->plane_height = src->crop_heights[is_uv];
130 rsc->src_buffer = src->buffers[plane];
131 rsc->src_stride = src->strides[is_uv];
132 rsc->dgd_buffer = dgd->buffers[plane];
133 rsc->dgd_stride = dgd->strides[is_uv];
134 assert(src->crop_widths[is_uv] == dgd->crop_widths[is_uv]);
135 assert(src->crop_heights[is_uv] == dgd->crop_heights[is_uv]);
136 }
try_restoration_unit_seg(const RestSearchCtxt * rsc,const RestorationTileLimits * limits,const Av1PixelRect * tile_rect,const RestorationUnitInfo * rui)137 static int64_t try_restoration_unit_seg(const RestSearchCtxt * rsc,
138 const RestorationTileLimits *limits,
139 const Av1PixelRect * tile_rect,
140 const RestorationUnitInfo * rui) {
141 const Av1Common *const cm = rsc->cm;
142 const int32_t plane = rsc->plane;
143 const int32_t is_uv = plane > 0;
144 const RestorationInfo *rsi = &cm->child_pcs->rst_info[plane];
145 RestorationLineBuffers rlbs;
146 const int32_t bit_depth = cm->bit_depth;
147 const int32_t highbd = cm->use_highbitdepth;
148
149 const Yv12BufferConfig *fts = rsc->org_frame_to_show;
150
151 const int32_t optimized_lr = 0;
152
153 // If boundaries are enabled for filtering, recon gets updated using setup/restore
154 // processing_stripe_bounadaries. Many threads doing so will result in race condition.
155 // Only use boundaries during the filter search if a copy of recon is made for each
156 // thread (controlled with scs_ptr->seq_header.use_boundaries_in_rest_search).
157 svt_av1_loop_restoration_filter_unit(cm->use_boundaries_in_rest_search,
158 limits,
159 rui,
160 &rsi->boundaries,
161 &rlbs,
162 tile_rect,
163 rsc->tile_stripe0,
164 is_uv && cm->subsampling_x,
165 is_uv && cm->subsampling_y,
166 highbd,
167 bit_depth,
168 fts->buffers[plane],
169 fts->strides[is_uv],
170 rsc->dst->buffers[plane],
171 rsc->dst->strides[is_uv],
172 rsc->tmpbuf,
173 optimized_lr);
174 return sse_restoration_unit(limits, rsc->src, rsc->dst, plane, highbd);
175 }
176
svt_av1_lowbd_pixel_proj_error_c(const uint8_t * src8,int32_t width,int32_t height,int32_t src_stride,const uint8_t * dat8,int32_t dat_stride,int32_t * flt0,int32_t flt0_stride,int32_t * flt1,int32_t flt1_stride,int32_t xq[2],const SgrParamsType * params)177 int64_t svt_av1_lowbd_pixel_proj_error_c(const uint8_t *src8, int32_t width, int32_t height,
178 int32_t src_stride, const uint8_t *dat8,
179 int32_t dat_stride, int32_t *flt0, int32_t flt0_stride,
180 int32_t *flt1, int32_t flt1_stride, int32_t xq[2],
181 const SgrParamsType *params) {
182 int32_t i, j;
183 const uint8_t *src = src8;
184 const uint8_t *dat = dat8;
185 int64_t err = 0;
186 if (params->r[0] > 0 && params->r[1] > 0) {
187 for (i = 0; i < height; ++i) {
188 for (j = 0; j < width; ++j) {
189 assert(flt1[j] < (1 << 15) && flt1[j] > -(1 << 15));
190 assert(flt0[j] < (1 << 15) && flt0[j] > -(1 << 15));
191 const int32_t u = (int32_t)(dat[j] << SGRPROJ_RST_BITS);
192 int32_t v = u << SGRPROJ_PRJ_BITS;
193 v += xq[0] * (flt0[j] - u) + xq[1] * (flt1[j] - u);
194 const int32_t e = ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) -
195 src[j];
196 err += e * e;
197 }
198 dat += dat_stride;
199 src += src_stride;
200 flt0 += flt0_stride;
201 flt1 += flt1_stride;
202 }
203 } else if (params->r[0] > 0) {
204 for (i = 0; i < height; ++i) {
205 for (j = 0; j < width; ++j) {
206 assert(flt0[j] < (1 << 15) && flt0[j] > -(1 << 15));
207 const int32_t u = (int32_t)(dat[j] << SGRPROJ_RST_BITS);
208 int32_t v = u << SGRPROJ_PRJ_BITS;
209 v += xq[0] * (flt0[j] - u);
210 const int32_t e = ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) -
211 src[j];
212 err += e * e;
213 }
214 dat += dat_stride;
215 src += src_stride;
216 flt0 += flt0_stride;
217 }
218 } else if (params->r[1] > 0) {
219 for (i = 0; i < height; ++i) {
220 for (j = 0; j < width; ++j) {
221 assert(flt1[j] < (1 << 15) && flt1[j] > -(1 << 15));
222 const int32_t u = (int32_t)(dat[j] << SGRPROJ_RST_BITS);
223 int32_t v = u << SGRPROJ_PRJ_BITS;
224 v += xq[1] * (flt1[j] - u);
225 const int32_t e = ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) -
226 src[j];
227 err += e * e;
228 }
229 dat += dat_stride;
230 src += src_stride;
231 flt1 += flt1_stride;
232 }
233 } else {
234 for (i = 0; i < height; ++i) {
235 for (j = 0; j < width; ++j) {
236 const int32_t e = (int32_t)(dat[j]) - src[j];
237 err += e * e;
238 }
239 dat += dat_stride;
240 src += src_stride;
241 }
242 }
243
244 return err;
245 }
246
svt_av1_highbd_pixel_proj_error_c(const uint8_t * src8,int32_t width,int32_t height,int32_t src_stride,const uint8_t * dat8,int32_t dat_stride,int32_t * flt0,int32_t flt0_stride,int32_t * flt1,int32_t flt1_stride,int32_t xq[2],const SgrParamsType * params)247 int64_t svt_av1_highbd_pixel_proj_error_c(const uint8_t *src8, int32_t width, int32_t height,
248 int32_t src_stride, const uint8_t *dat8,
249 int32_t dat_stride, int32_t *flt0, int32_t flt0_stride,
250 int32_t *flt1, int32_t flt1_stride, int32_t xq[2],
251 const SgrParamsType *params) {
252 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
253 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
254 int32_t i, j;
255 int64_t err = 0;
256 const int32_t half = 1 << (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS - 1);
257 if (params->r[0] > 0 && params->r[1] > 0) {
258 int32_t xq0 = xq[0];
259 int32_t xq1 = xq[1];
260 for (i = 0; i < height; ++i) {
261 for (j = 0; j < width; ++j) {
262 const int32_t d = dat[j];
263 const int32_t s = src[j];
264 const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
265 int32_t v0 = flt0[j] - u;
266 int32_t v1 = flt1[j] - u;
267 int32_t v = half;
268 v += xq0 * v0;
269 v += xq1 * v1;
270 const int32_t e = (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
271 err += e * e;
272 }
273 dat += dat_stride;
274 flt0 += flt0_stride;
275 flt1 += flt1_stride;
276 src += src_stride;
277 }
278 } else if (params->r[0] > 0 || params->r[1] > 0) {
279 int32_t exq;
280 int32_t *flt;
281 int32_t flt_stride;
282 if (params->r[0] > 0) {
283 exq = xq[0];
284 flt = flt0;
285 flt_stride = flt0_stride;
286 } else {
287 exq = xq[1];
288 flt = flt1;
289 flt_stride = flt1_stride;
290 }
291 for (i = 0; i < height; ++i) {
292 for (j = 0; j < width; ++j) {
293 const int32_t d = dat[j];
294 const int32_t s = src[j];
295 const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
296 int32_t v = half;
297 v += exq * (flt[j] - u);
298 const int32_t e = (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
299 err += e * e;
300 }
301 dat += dat_stride;
302 flt += flt_stride;
303 src += src_stride;
304 }
305 } else {
306 for (i = 0; i < height; ++i) {
307 for (j = 0; j < width; ++j) {
308 const int32_t d = dat[j];
309 const int32_t s = src[j];
310 const int32_t e = d - s;
311 err += e * e;
312 }
313 dat += dat_stride;
314 src += src_stride;
315 }
316 }
317 return err;
318 }
319
get_pixel_proj_error(const uint8_t * src8,int32_t width,int32_t height,int32_t src_stride,const uint8_t * dat8,int32_t dat_stride,int32_t use_highbitdepth,int32_t * flt0,int32_t flt0_stride,int32_t * flt1,int32_t flt1_stride,int32_t * xqd,const SgrParamsType * params)320 static int64_t get_pixel_proj_error(const uint8_t *src8, int32_t width, int32_t height,
321 int32_t src_stride, const uint8_t *dat8, int32_t dat_stride,
322 int32_t use_highbitdepth, int32_t *flt0, int32_t flt0_stride,
323 int32_t *flt1, int32_t flt1_stride, int32_t *xqd,
324 const SgrParamsType *params) {
325 int32_t xq[2];
326 svt_decode_xq(xqd, xq, params);
327 if (!use_highbitdepth) {
328 return svt_av1_lowbd_pixel_proj_error(src8,
329 width,
330 height,
331 src_stride,
332 dat8,
333 dat_stride,
334 flt0,
335 flt0_stride,
336 flt1,
337 flt1_stride,
338 xq,
339 params);
340 } else {
341 return svt_av1_highbd_pixel_proj_error(src8,
342 width,
343 height,
344 src_stride,
345 dat8,
346 dat_stride,
347 flt0,
348 flt0_stride,
349 flt1,
350 flt1_stride,
351 xq,
352 params);
353 }
354 }
355
finer_search_pixel_proj_error(const uint8_t * src8,int32_t width,int32_t height,int32_t src_stride,const uint8_t * dat8,int32_t dat_stride,int32_t use_highbitdepth,int32_t * flt0,int32_t flt0_stride,int32_t * flt1,int32_t flt1_stride,int32_t start_step,int32_t * xqd,const SgrParamsType * params)356 static int64_t finer_search_pixel_proj_error(const uint8_t *src8, int32_t width, int32_t height,
357 int32_t src_stride, const uint8_t *dat8,
358 int32_t dat_stride, int32_t use_highbitdepth,
359 int32_t *flt0, int32_t flt0_stride, int32_t *flt1,
360 int32_t flt1_stride, int32_t start_step, int32_t *xqd,
361 const SgrParamsType *params) {
362 int64_t err = get_pixel_proj_error(src8,
363 width,
364 height,
365 src_stride,
366 dat8,
367 dat_stride,
368 use_highbitdepth,
369 flt0,
370 flt0_stride,
371 flt1,
372 flt1_stride,
373 xqd,
374 params);
375 (void)start_step;
376 int64_t err2;
377 int32_t tap_min[] = {SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MIN1};
378 int32_t tap_max[] = {SGRPROJ_PRJ_MAX0, SGRPROJ_PRJ_MAX1};
379 for (int32_t s = start_step; s >= 1; s >>= 1) {
380 for (int32_t p = 0; p < 2; ++p) {
381 if ((params->r[0] == 0 && p == 0) || (params->r[1] == 0 && p == 1))
382 continue;
383 int32_t skip = 0;
384 do {
385 if (xqd[p] - s >= tap_min[p]) {
386 xqd[p] -= s;
387 err2 = get_pixel_proj_error(src8,
388 width,
389 height,
390 src_stride,
391 dat8,
392 dat_stride,
393 use_highbitdepth,
394 flt0,
395 flt0_stride,
396 flt1,
397 flt1_stride,
398 xqd,
399 params);
400 if (err2 > err)
401 xqd[p] += s;
402 else {
403 err = err2;
404 skip = 1;
405 // At the highest step size continue moving in the same direction
406 if (s == start_step)
407 continue;
408 }
409 }
410 break;
411 } while (1);
412 if (skip)
413 break;
414 do {
415 if (xqd[p] + s <= tap_max[p]) {
416 xqd[p] += s;
417 err2 = get_pixel_proj_error(src8,
418 width,
419 height,
420 src_stride,
421 dat8,
422 dat_stride,
423 use_highbitdepth,
424 flt0,
425 flt0_stride,
426 flt1,
427 flt1_stride,
428 xqd,
429 params);
430 if (err2 > err)
431 xqd[p] -= s;
432 else {
433 err = err2;
434 // At the highest step size continue moving in the same direction
435 if (s == start_step)
436 continue;
437 }
438 }
439 break;
440 } while (1);
441 }
442 }
443
444 return err;
445 }
446
447 #ifdef ARCH_X86_64
448 extern void RunEmms();
449 #endif
450
svt_get_proj_subspace_c(const uint8_t * src8,int32_t width,int32_t height,int32_t src_stride,const uint8_t * dat8,int32_t dat_stride,int32_t use_highbitdepth,int32_t * flt0,int32_t flt0_stride,int32_t * flt1,int32_t flt1_stride,int32_t * xq,const SgrParamsType * params)451 void svt_get_proj_subspace_c(const uint8_t *src8, int32_t width, int32_t height, int32_t src_stride,
452 const uint8_t *dat8, int32_t dat_stride, int32_t use_highbitdepth,
453 int32_t *flt0, int32_t flt0_stride, int32_t *flt1, int32_t flt1_stride,
454 int32_t *xq, const SgrParamsType *params) {
455 int32_t i, j;
456 double H[2][2] = {{0, 0}, {0, 0}};
457 double C[2] = {0, 0};
458 double det;
459 double x[2];
460 const int32_t size = width * height;
461
462 #ifdef ARCH_X86_64
463 aom_clear_system_state();
464 #endif
465
466 // Default
467 xq[0] = 0;
468 xq[1] = 0;
469 if (!use_highbitdepth) {
470 const uint8_t *src = src8;
471 const uint8_t *dat = dat8;
472 for (i = 0; i < height; ++i) {
473 for (j = 0; j < width; ++j) {
474 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
475 const double s = (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
476 const double f1 = (params->r[0] > 0) ? (double)flt0[i * flt0_stride + j] - u : 0;
477 const double f2 = (params->r[1] > 0) ? (double)flt1[i * flt1_stride + j] - u : 0;
478 H[0][0] += f1 * f1;
479 H[1][1] += f2 * f2;
480 H[0][1] += f1 * f2;
481 C[0] += f1 * s;
482 C[1] += f2 * s;
483 }
484 }
485 } else {
486 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
487 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
488 for (i = 0; i < height; ++i) {
489 for (j = 0; j < width; ++j) {
490 const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
491 const double s = (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
492 const double f1 = (params->r[0] > 0) ? (double)flt0[i * flt0_stride + j] - u : 0;
493 const double f2 = (params->r[1] > 0) ? (double)flt1[i * flt1_stride + j] - u : 0;
494 H[0][0] += f1 * f1;
495 H[1][1] += f2 * f2;
496 H[0][1] += f1 * f2;
497 C[0] += f1 * s;
498 C[1] += f2 * s;
499 }
500 }
501 }
502 H[0][0] /= size;
503 H[0][1] /= size;
504 H[1][1] /= size;
505 H[1][0] = H[0][1];
506 C[0] /= size;
507 C[1] /= size;
508 if (params->r[0] == 0) {
509 // H matrix is now only the scalar H[1][1]
510 // C vector is now only the scalar C[1]
511 det = H[1][1];
512 if (det < 1e-8)
513 return; // ill-posed, return default values
514 x[0] = 0;
515 x[1] = C[1] / det;
516
517 xq[0] = 0;
518 xq[1] = (int32_t)rint(x[1] * (1 << SGRPROJ_PRJ_BITS));
519 } else if (params->r[1] == 0) {
520 // H matrix is now only the scalar H[0][0]
521 // C vector is now only the scalar C[0]
522 det = H[0][0];
523 if (det < 1e-8)
524 return; // ill-posed, return default values
525 x[0] = C[0] / det;
526 x[1] = 0;
527
528 xq[0] = (int32_t)rint(x[0] * (1 << SGRPROJ_PRJ_BITS));
529 xq[1] = 0;
530 } else {
531 det = (H[0][0] * H[1][1] - H[0][1] * H[1][0]);
532 if (det < 1e-8)
533 return; // ill-posed, return default values
534 x[0] = (H[1][1] * C[0] - H[0][1] * C[1]) / det;
535 x[1] = (H[0][0] * C[1] - H[1][0] * C[0]) / det;
536
537 xq[0] = (int32_t)rint(x[0] * (1 << SGRPROJ_PRJ_BITS));
538 xq[1] = (int32_t)rint(x[1] * (1 << SGRPROJ_PRJ_BITS));
539 }
540 }
541
encode_xq(int32_t * xq,int32_t * xqd,const SgrParamsType * params)542 static INLINE void encode_xq(int32_t *xq, int32_t *xqd, const SgrParamsType *params) {
543 if (params->r[0] == 0) {
544 xqd[0] = 0;
545 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xq[1], SGRPROJ_PRJ_MIN1, SGRPROJ_PRJ_MAX1);
546 } else if (params->r[1] == 0) {
547 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
548 xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0], SGRPROJ_PRJ_MIN1, SGRPROJ_PRJ_MAX1);
549 } else {
550 xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
551 xqd[1] = clamp(
552 (1 << SGRPROJ_PRJ_BITS) - xqd[0] - xq[1], SGRPROJ_PRJ_MIN1, SGRPROJ_PRJ_MAX1);
553 }
554 }
555
556 // Apply the self-guided filter across an entire restoration unit.
apply_sgr(int32_t sgr_params_idx,const uint8_t * dat8,int32_t width,int32_t height,int32_t dat_stride,int32_t use_highbd,int32_t bit_depth,int32_t pu_width,int32_t pu_height,int32_t * flt0,int32_t * flt1,int32_t flt_stride)557 static INLINE void apply_sgr(int32_t sgr_params_idx, const uint8_t *dat8, int32_t width,
558 int32_t height, int32_t dat_stride, int32_t use_highbd,
559 int32_t bit_depth, int32_t pu_width, int32_t pu_height, int32_t *flt0,
560 int32_t *flt1, int32_t flt_stride) {
561 for (int32_t i = 0; i < height; i += pu_height) {
562 const int32_t h = AOMMIN(pu_height, height - i);
563 int32_t * flt0_row = flt0 + i * flt_stride;
564 int32_t * flt1_row = flt1 + i * flt_stride;
565 const uint8_t *dat8_row = dat8 + i * dat_stride;
566
567 // Iterate over the stripe in blocks of width pu_width
568 for (int32_t j = 0; j < width; j += pu_width) {
569 const int32_t w = AOMMIN(pu_width, width - j);
570
571 //CHKN SSE
572 svt_av1_selfguided_restoration(dat8_row + j,
573 w,
574 h,
575 dat_stride,
576 flt0_row + j,
577 flt1_row + j,
578 flt_stride,
579 sgr_params_idx,
580 bit_depth,
581 use_highbd);
582 }
583 }
584 }
585
search_selfguided_restoration(const uint8_t * dat8,int32_t width,int32_t height,int32_t dat_stride,const uint8_t * src8,int32_t src_stride,int32_t use_highbitdepth,int32_t bit_depth,int32_t pu_width,int32_t pu_height,int32_t * rstbuf,int8_t sg_ref_frame_ep[2],int32_t sg_frame_ep_cnt[SGRPROJ_PARAMS],int8_t step)586 static SgrprojInfo search_selfguided_restoration(
587 const uint8_t *dat8, int32_t width, int32_t height, int32_t dat_stride, const uint8_t *src8,
588 int32_t src_stride, int32_t use_highbitdepth, int32_t bit_depth, int32_t pu_width,
589 int32_t pu_height, int32_t *rstbuf, int8_t sg_ref_frame_ep[2],
590 int32_t sg_frame_ep_cnt[SGRPROJ_PARAMS], int8_t step) {
591 int32_t *flt0 = rstbuf;
592 int32_t *flt1 = flt0 + RESTORATION_UNITPELS_MAX;
593 int32_t ep, bestep = 0;
594 int64_t besterr = -1;
595 int32_t exqd[2], bestxqd[2] = {0, 0};
596 int32_t flt_stride = ((width + 7) & ~7) + 8;
597 assert(pu_width == (RESTORATION_PROC_UNIT_SIZE >> 1) || pu_width == RESTORATION_PROC_UNIT_SIZE);
598 assert(pu_height == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
599 pu_height == RESTORATION_PROC_UNIT_SIZE);
600 int8_t mid_ep = sg_ref_frame_ep[0] < 0 && sg_ref_frame_ep[1] < 0 ? 0
601 : sg_ref_frame_ep[1] < 0 ? sg_ref_frame_ep[0]
602 : sg_ref_frame_ep[0] < 0 ? sg_ref_frame_ep[1]
603 : (sg_ref_frame_ep[0] + sg_ref_frame_ep[1]) / 2;
604
605 int8_t start_ep = sg_ref_frame_ep[0] < 0 && sg_ref_frame_ep[1] < 0 ? 0
606 : AOMMAX(0, mid_ep - step);
607 int8_t end_ep = sg_ref_frame_ep[0] < 0 && sg_ref_frame_ep[1] < 0
608 ? SGRPROJ_PARAMS
609 : AOMMIN(SGRPROJ_PARAMS, mid_ep + step);
610
611 for (ep = start_ep; ep < end_ep; ep++) {
612 int32_t exq[2];
613 apply_sgr(ep,
614 dat8,
615 width,
616 height,
617 dat_stride,
618 use_highbitdepth,
619 bit_depth,
620 pu_width,
621 pu_height,
622 flt0,
623 flt1,
624 flt_stride);
625 #ifdef ARCH_X86_64
626 aom_clear_system_state();
627 #endif
628 const SgrParamsType *const params = &eb_sgr_params[ep];
629 svt_get_proj_subspace(src8,
630 width,
631 height,
632 src_stride,
633 dat8,
634 dat_stride,
635 use_highbitdepth,
636 flt0,
637 flt_stride,
638 flt1,
639 flt_stride,
640 exq,
641 params);
642 #ifdef ARCH_X86_64
643 aom_clear_system_state();
644 #endif
645 encode_xq(exq, exqd, params);
646 int64_t err = finer_search_pixel_proj_error(src8,
647 width,
648 height,
649 src_stride,
650 dat8,
651 dat_stride,
652 use_highbitdepth,
653 flt0,
654 flt_stride,
655 flt1,
656 flt_stride,
657 2,
658 exqd,
659 params);
660 if (besterr == -1 || err < besterr) {
661 bestep = ep;
662 besterr = err;
663 bestxqd[0] = exqd[0];
664 bestxqd[1] = exqd[1];
665 }
666 }
667 sg_frame_ep_cnt[bestep]++;
668
669 SgrprojInfo ret;
670 ret.ep = bestep;
671 ret.xqd[0] = bestxqd[0];
672 ret.xqd[1] = bestxqd[1];
673 return ret;
674 }
675 extern int32_t svt_aom_count_primitive_refsubexpfin(uint16_t n, uint16_t k, uint16_t ref,
676 uint16_t v);
677
count_sgrproj_bits(SgrprojInfo * sgrproj_info,SgrprojInfo * ref_sgrproj_info)678 static int32_t count_sgrproj_bits(SgrprojInfo *sgrproj_info, SgrprojInfo *ref_sgrproj_info) {
679 int32_t bits = SGRPROJ_PARAMS_BITS;
680 const SgrParamsType *params = &eb_sgr_params[sgrproj_info->ep];
681 if (params->r[0] > 0)
682 bits += svt_aom_count_primitive_refsubexpfin(
683 SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1,
684 SGRPROJ_PRJ_SUBEXP_K,
685 (uint16_t)(ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0),
686 (uint16_t)(sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0));
687 if (params->r[1] > 0)
688 bits += svt_aom_count_primitive_refsubexpfin(
689 SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1,
690 SGRPROJ_PRJ_SUBEXP_K,
691 (uint16_t)(ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1),
692 (uint16_t)(sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1));
693 return bits;
694 }
695
get_sg_step(int8_t sg_filter_mode)696 int8_t get_sg_step(int8_t sg_filter_mode) {
697 int8_t step;
698 switch (sg_filter_mode) {
699 case 1: step = 0; break;
700 case 2: step = 1; break;
701 case 3: step = 4; break;
702 case 4: step = 16; break;
703 default: step = 16; break;
704 }
705 return step;
706 }
svt_av1_compute_stats_c(int32_t wiener_win,const uint8_t * dgd,const uint8_t * src,int32_t h_start,int32_t h_end,int32_t v_start,int32_t v_end,int32_t dgd_stride,int32_t src_stride,int64_t * M,int64_t * H)707 void svt_av1_compute_stats_c(int32_t wiener_win, const uint8_t *dgd, const uint8_t *src,
708 int32_t h_start, int32_t h_end, int32_t v_start, int32_t v_end,
709 int32_t dgd_stride, int32_t src_stride, int64_t *M, int64_t *H) {
710 int32_t i, j, k, l;
711 int16_t y[WIENER_WIN2] = {0};
712 const int32_t wiener_win2 = wiener_win * wiener_win;
713 const int32_t wiener_halfwin = (wiener_win >> 1);
714 uint8_t avg = find_average(dgd, h_start, h_end, v_start, v_end, dgd_stride);
715
716 memset(M, 0, sizeof(*M) * wiener_win2);
717 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
718 for (i = v_start; i < v_end; i++) {
719 for (j = h_start; j < h_end; j++) {
720 const int16_t x = (int16_t)src[i * src_stride + j] - (int16_t)avg;
721 int32_t idx = 0;
722 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
723 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
724 y[idx] = (int16_t)dgd[(i + l) * dgd_stride + (j + k)] - (int16_t)avg;
725 idx++;
726 }
727 }
728 assert(idx == wiener_win2);
729 for (k = 0; k < wiener_win2; ++k) {
730 M[k] += (int32_t)y[k] * x;
731 for (l = k; l < wiener_win2; ++l) {
732 // H is a symmetric matrix, so we only need to fill out the upper
733 // triangle here. We can copy it down to the lower triangle outside
734 // the (i, j) loops.
735 H[k * wiener_win2 + l] += (int32_t)y[k] * y[l];
736 }
737 }
738 }
739 }
740 for (k = 0; k < wiener_win2; ++k) {
741 for (l = k + 1; l < wiener_win2; ++l) H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
742 }
743 }
svt_av1_compute_stats_highbd_c(int32_t wiener_win,const uint8_t * dgd8,const uint8_t * src8,int32_t h_start,int32_t h_end,int32_t v_start,int32_t v_end,int32_t dgd_stride,int32_t src_stride,int64_t * M,int64_t * H,AomBitDepth bit_depth)744 void svt_av1_compute_stats_highbd_c(int32_t wiener_win, const uint8_t *dgd8, const uint8_t *src8,
745 int32_t h_start, int32_t h_end, int32_t v_start, int32_t v_end,
746 int32_t dgd_stride, int32_t src_stride, int64_t *M, int64_t *H,
747 AomBitDepth bit_depth) {
748 int32_t i, j, k, l;
749 int32_t y[WIENER_WIN2] = {0};
750 const int32_t wiener_win2 = wiener_win * wiener_win;
751 const int32_t wiener_halfwin = (wiener_win >> 1);
752 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
753 const uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
754 uint16_t avg = find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
755
756 uint8_t bit_depth_divider = 1;
757 if (bit_depth == AOM_BITS_12)
758 bit_depth_divider = 16;
759 else if (bit_depth == AOM_BITS_10)
760 bit_depth_divider = 4;
761
762 memset(M, 0, sizeof(*M) * wiener_win2);
763 memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
764 for (i = v_start; i < v_end; i++) {
765 for (j = h_start; j < h_end; j++) {
766 const int32_t x = (int32_t)src[i * src_stride + j] - (int32_t)avg;
767 int32_t idx = 0;
768 for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
769 for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
770 y[idx] = (int32_t)dgd[(i + l) * dgd_stride + (j + k)] - (int32_t)avg;
771 idx++;
772 }
773 }
774 assert(idx == wiener_win2);
775 for (k = 0; k < wiener_win2; ++k) {
776 M[k] += (int64_t)y[k] * x;
777 for (l = k; l < wiener_win2; ++l) {
778 // H is a symmetric matrix, so we only need to fill out the upper
779 // triangle here. We can copy it down to the lower triangle outside
780 // the (i, j) loops.
781 H[k * wiener_win2 + l] += (int64_t)y[k] * y[l];
782 }
783 }
784 }
785 }
786 for (k = 0; k < wiener_win2; ++k) {
787 M[k] /= bit_depth_divider;
788 H[k * wiener_win2 + k] /= bit_depth_divider;
789 for (l = k + 1; l < wiener_win2; ++l) {
790 H[k * wiener_win2 + l] /= bit_depth_divider;
791 H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
792 }
793 }
794 }
795
wrap_index(int32_t i,int32_t wiener_win)796 static INLINE int32_t wrap_index(int32_t i, int32_t wiener_win) {
797 const int32_t wiener_halfwin1 = (wiener_win >> 1) + 1;
798 return (i >= wiener_halfwin1 ? wiener_win - 1 - i : i);
799 }
800
801 // Solve linear equations to find Wiener filter tap values
802 // Taps are output scaled by WIENER_FILT_STEP
linsolve_wiener(int32_t n,int64_t * A,int32_t stride,int64_t * b,int32_t * x)803 static int32_t linsolve_wiener(int32_t n, int64_t *A, int32_t stride, int64_t *b, int32_t *x) {
804 for (int32_t k = 0; k < n - 1; k++) {
805 // Partial pivoting: bring the row with the largest pivot to the top
806 for (int32_t i = n - 1; i > k; i--) {
807 // If row i has a better (bigger) pivot than row (i-1), swap them
808 if (llabs(A[(i - 1) * stride + k]) < llabs(A[i * stride + k])) {
809 for (int32_t j = 0; j < n; j++) {
810 const int64_t c = A[i * stride + j];
811 A[i * stride + j] = A[(i - 1) * stride + j];
812 A[(i - 1) * stride + j] = c;
813 }
814 const int64_t c = b[i];
815 b[i] = b[i - 1];
816 b[i - 1] = c;
817 }
818 }
819 // Forward elimination (convert A to row-echelon form)
820 for (int32_t i = k; i < n - 1; i++) {
821 if (A[k * stride + k] == 0)
822 return 0;
823 const int64_t c = A[(i + 1) * stride + k];
824 const int64_t cd = A[k * stride + k];
825 for (int32_t j = 0; j < n; j++)
826 A[(i + 1) * stride + j] -= c / 256 * A[k * stride + j] / cd * 256;
827 b[i + 1] -= c * b[k] / cd;
828 }
829 }
830 // Back-substitution
831 for (int32_t i = n - 1; i >= 0; i--) {
832 if (A[i * stride + i] == 0)
833 return 0;
834 int64_t c = 0;
835 for (int32_t j = i + 1; j <= n - 1; j++)
836 c += A[i * stride + j] * x[j] / WIENER_TAP_SCALE_FACTOR;
837 // Store filter taps x in scaled form.
838 x[i] = (int32_t)(WIENER_TAP_SCALE_FACTOR * (b[i] - c) / A[i * stride + i]);
839 }
840
841 return 1;
842 }
843 // Fix vector b, update vector a
update_a_sep_sym(int32_t wiener_win,int64_t ** mc,int64_t ** hc,int32_t * a,int32_t * b)844 static void update_a_sep_sym(int32_t wiener_win, int64_t **mc, int64_t **hc, int32_t *a,
845 int32_t *b) {
846 int32_t i, j;
847 int32_t S[WIENER_WIN];
848 int64_t A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
849 const int32_t wiener_win2 = wiener_win * wiener_win;
850 const int32_t wiener_halfwin1 = (wiener_win >> 1) + 1;
851 memset(A, 0, sizeof(A));
852 memset(B, 0, sizeof(B));
853 for (i = 0; i < wiener_win; i++) {
854 for (j = 0; j < wiener_win; ++j) {
855 const int32_t jj = wrap_index(j, wiener_win);
856 A[jj] += mc[i][j] * b[i] / WIENER_TAP_SCALE_FACTOR;
857 }
858 }
859 for (i = 0; i < wiener_win; i++) {
860 for (j = 0; j < wiener_win; j++) {
861 int32_t k, l;
862 for (k = 0; k < wiener_win; ++k) {
863 for (l = 0; l < wiener_win; ++l) {
864 const int32_t kk = wrap_index(k, wiener_win);
865 const int32_t ll = wrap_index(l, wiener_win);
866 B[ll * wiener_halfwin1 + kk] += hc[j * wiener_win + i][k * wiener_win2 + l] *
867 b[i] / WIENER_TAP_SCALE_FACTOR * b[j] / WIENER_TAP_SCALE_FACTOR;
868 }
869 }
870 }
871 }
872 // Normalization enforcement in the system of equations itself
873 assert(wiener_halfwin1 <= WIENER_HALFWIN1);
874 int64_t a_halfwin_1 = A[wiener_halfwin1 - 1];
875 for (i = 0; i < wiener_halfwin1 - 1; ++i) {
876 A[i] -= a_halfwin_1 * 2 + B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
877 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
878 }
879 for (i = 0; i < wiener_halfwin1 - 1; ++i) {
880 for (j = 0; j < wiener_halfwin1 - 1; ++j) {
881 B[i * wiener_halfwin1 + j] -= 2 *
882 (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
883 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
884 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)]);
885 }
886 }
887 if (linsolve_wiener(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
888 S[wiener_halfwin1 - 1] = WIENER_TAP_SCALE_FACTOR;
889 for (i = wiener_halfwin1; i < wiener_win; ++i) {
890 S[i] = S[wiener_win - 1 - i];
891 S[wiener_halfwin1 - 1] -= 2 * S[i];
892 }
893 svt_memcpy(a, S, wiener_win * sizeof(*a));
894 }
895 }
896
897 // Fix vector a, update vector b
update_b_sep_sym(int32_t wiener_win,int64_t ** Mc,int64_t ** hc,int32_t * a,int32_t * b)898 static void update_b_sep_sym(int32_t wiener_win, int64_t **Mc, int64_t **hc, int32_t *a,
899 int32_t *b) {
900 int32_t i, j;
901 int32_t S[WIENER_WIN];
902 int64_t A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
903 const int32_t wiener_win2 = wiener_win * wiener_win;
904 const int32_t wiener_halfwin1 = (wiener_win >> 1) + 1;
905 memset(A, 0, sizeof(A));
906 memset(B, 0, sizeof(B));
907 for (i = 0; i < wiener_win; i++) {
908 const int32_t ii = wrap_index(i, wiener_win);
909 for (j = 0; j < wiener_win; j++) A[ii] += Mc[i][j] * a[j] / WIENER_TAP_SCALE_FACTOR;
910 }
911
912 for (i = 0; i < wiener_win; i++) {
913 for (j = 0; j < wiener_win; j++) {
914 const int32_t ii = wrap_index(i, wiener_win);
915 const int32_t jj = wrap_index(j, wiener_win);
916 int32_t k, l;
917 for (k = 0; k < wiener_win; ++k) {
918 for (l = 0; l < wiener_win; ++l) {
919 B[jj * wiener_halfwin1 + ii] += hc[i * wiener_win + j][k * wiener_win2 + l] *
920 a[k] / WIENER_TAP_SCALE_FACTOR * a[l] / WIENER_TAP_SCALE_FACTOR;
921 }
922 }
923 }
924 }
925 // Normalization enforcement in the system of equations itself
926 int64_t a_halfwin_1 = A[wiener_halfwin1 - 1];
927 for (i = 0; i < wiener_halfwin1 - 1; ++i) {
928 A[i] -= a_halfwin_1 * 2 + B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
929 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
930 }
931 for (i = 0; i < wiener_halfwin1 - 1; ++i) {
932 for (j = 0; j < wiener_halfwin1 - 1; ++j) {
933 B[i * wiener_halfwin1 + j] -= 2 *
934 (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
935 B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
936 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)]);
937 }
938 }
939 if (linsolve_wiener(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
940 S[wiener_halfwin1 - 1] = WIENER_TAP_SCALE_FACTOR;
941 for (i = wiener_halfwin1; i < wiener_win; ++i) {
942 S[i] = S[wiener_win - 1 - i];
943 S[wiener_halfwin1 - 1] -= 2 * S[i];
944 }
945 svt_memcpy(b, S, wiener_win * sizeof(*b));
946 }
947 }
948
wiener_decompose_sep_sym(int32_t wiener_win,int64_t * M,int64_t * H,int32_t * a,int32_t * b)949 static int32_t wiener_decompose_sep_sym(int32_t wiener_win, int64_t *M, int64_t *H, int32_t *a,
950 int32_t *b) {
951 static const int32_t init_filt[WIENER_WIN] = {
952 WIENER_FILT_TAP0_MIDV,
953 WIENER_FILT_TAP1_MIDV,
954 WIENER_FILT_TAP2_MIDV,
955 WIENER_FILT_TAP3_MIDV,
956 WIENER_FILT_TAP2_MIDV,
957 WIENER_FILT_TAP1_MIDV,
958 WIENER_FILT_TAP0_MIDV,
959 };
960 int64_t * hc[WIENER_WIN2];
961 int64_t * Mc[WIENER_WIN];
962 int32_t i, j, iter;
963 const int32_t plane_off = (WIENER_WIN - wiener_win) >> 1;
964 const int32_t wiener_win2 = wiener_win * wiener_win;
965 for (i = 0; i < wiener_win; i++) {
966 a[i] = b[i] = WIENER_TAP_SCALE_FACTOR / WIENER_FILT_STEP * init_filt[i + plane_off];
967 }
968 for (i = 0; i < wiener_win; i++) {
969 Mc[i] = M + i * wiener_win;
970 for (j = 0; j < wiener_win; j++) {
971 hc[i * wiener_win + j] = H + i * wiener_win * wiener_win2 + j * wiener_win;
972 }
973 }
974
975 iter = 1;
976 while (iter < NUM_WIENER_ITERS) {
977 update_a_sep_sym(wiener_win, Mc, hc, a, b);
978 update_b_sep_sym(wiener_win, Mc, hc, a, b);
979 iter++;
980 }
981 return 1;
982 }
compute_score(int32_t wiener_win,int64_t * M,int64_t * H,InterpKernel vfilt,InterpKernel hfilt)983 static int64_t compute_score(int32_t wiener_win, int64_t *M, int64_t *H, InterpKernel vfilt,
984 InterpKernel hfilt) {
985 int32_t ab[WIENER_WIN * WIENER_WIN];
986 int16_t a[WIENER_WIN], b[WIENER_WIN];
987 int64_t P = 0, Q = 0;
988 int64_t i_p = 0, i_q = 0;
989 int64_t score, i_score;
990 int32_t i, k, l;
991 const int32_t plane_off = (WIENER_WIN - wiener_win) >> 1;
992 const int32_t wiener_win2 = wiener_win * wiener_win;
993 #ifdef ARCH_X86_64
994 aom_clear_system_state();
995 #endif
996
997 a[WIENER_HALFWIN] = b[WIENER_HALFWIN] = WIENER_FILT_STEP;
998 for (i = 0; i < WIENER_HALFWIN; ++i) {
999 a[i] = a[WIENER_WIN - i - 1] = vfilt[i];
1000 b[i] = b[WIENER_WIN - i - 1] = hfilt[i];
1001 a[WIENER_HALFWIN] -= 2 * a[i];
1002 b[WIENER_HALFWIN] -= 2 * b[i];
1003 }
1004 memset(ab, 0, sizeof(ab));
1005 for (k = 0; k < wiener_win; ++k) {
1006 for (l = 0; l < wiener_win; ++l)
1007 ab[k * wiener_win + l] = a[l + plane_off] * b[k + plane_off];
1008 }
1009 for (k = 0; k < wiener_win2; ++k) {
1010 P += ab[k] * M[k] / WIENER_FILT_STEP / WIENER_FILT_STEP;
1011 for (l = 0; l < wiener_win2; ++l) {
1012 Q += ab[k] * H[k * wiener_win2 + l] * ab[l] / WIENER_FILT_STEP / WIENER_FILT_STEP /
1013 WIENER_FILT_STEP / WIENER_FILT_STEP;
1014 }
1015 }
1016 score = Q - 2 * P;
1017
1018 i_p = M[wiener_win2 >> 1];
1019 i_q = H[(wiener_win2 >> 1) * wiener_win2 + (wiener_win2 >> 1)];
1020 i_score = i_q - 2 * i_p;
1021
1022 return score - i_score;
1023 }
1024
finalize_sym_filter(int32_t wiener_win,int32_t * f,InterpKernel fi)1025 static void finalize_sym_filter(int32_t wiener_win, int32_t *f, InterpKernel fi) {
1026 int32_t i;
1027 const int32_t wiener_halfwin = (wiener_win >> 1);
1028
1029 for (i = 0; i < wiener_halfwin; ++i) {
1030 const int64_t dividend = (int64_t)f[i] * WIENER_FILT_STEP;
1031 const int64_t divisor = WIENER_TAP_SCALE_FACTOR;
1032 // Perform this division with proper rounding rather than truncation
1033 if (dividend < 0)
1034 fi[i] = (int16_t)((dividend - (divisor / 2)) / divisor);
1035 else
1036 fi[i] = (int16_t)((dividend + (divisor / 2)) / divisor);
1037 }
1038 // Specialize for 7-tap filter
1039 if (wiener_win == WIENER_WIN) {
1040 fi[0] = CLIP(fi[0], WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP0_MAXV);
1041 fi[1] = CLIP(fi[1], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
1042 fi[2] = CLIP(fi[2], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
1043 } else {
1044 fi[2] = CLIP(fi[1], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
1045 fi[1] = CLIP(fi[0], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
1046 fi[0] = 0;
1047 }
1048 // Satisfy filter constraints
1049 fi[WIENER_WIN - 1] = fi[0];
1050 fi[WIENER_WIN - 2] = fi[1];
1051 fi[WIENER_WIN - 3] = fi[2];
1052 // The central element has an implicit +WIENER_FILT_STEP
1053 fi[3] = -2 * (fi[0] + fi[1] + fi[2]);
1054 }
1055
count_wiener_bits(int32_t wiener_win,WienerInfo * wiener_info,WienerInfo * ref_wiener_info)1056 static int32_t count_wiener_bits(int32_t wiener_win, WienerInfo *wiener_info,
1057 WienerInfo *ref_wiener_info) {
1058 int32_t bits = 0;
1059 if (wiener_win == WIENER_WIN)
1060 bits += svt_aom_count_primitive_refsubexpfin(
1061 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1062 WIENER_FILT_TAP0_SUBEXP_K,
1063 ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV,
1064 wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV);
1065 bits += svt_aom_count_primitive_refsubexpfin(
1066 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1067 WIENER_FILT_TAP1_SUBEXP_K,
1068 ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV,
1069 wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV);
1070 bits += svt_aom_count_primitive_refsubexpfin(
1071 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1072 WIENER_FILT_TAP2_SUBEXP_K,
1073 ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV,
1074 wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV);
1075 if (wiener_win == WIENER_WIN)
1076 bits += svt_aom_count_primitive_refsubexpfin(
1077 WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1078 WIENER_FILT_TAP0_SUBEXP_K,
1079 ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV,
1080 wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV);
1081 bits += svt_aom_count_primitive_refsubexpfin(
1082 WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1083 WIENER_FILT_TAP1_SUBEXP_K,
1084 ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV,
1085 wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV);
1086 bits += svt_aom_count_primitive_refsubexpfin(
1087 WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1088 WIENER_FILT_TAP2_SUBEXP_K,
1089 ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV,
1090 wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV);
1091 return bits;
1092 }
1093
1094 #define USE_WIENER_REFINEMENT_SEARCH 1
finer_tile_search_wiener_seg(const RestSearchCtxt * rsc,const RestorationTileLimits * limits,const Av1PixelRect * tile,RestorationUnitInfo * rui,int32_t wiener_win)1095 static int64_t finer_tile_search_wiener_seg(const RestSearchCtxt * rsc,
1096 const RestorationTileLimits *limits,
1097 const Av1PixelRect *tile, RestorationUnitInfo *rui,
1098 int32_t wiener_win) {
1099 const int32_t plane_off = (WIENER_WIN - wiener_win) >> 1;
1100 int64_t err = try_restoration_unit_seg(rsc, limits, tile, rui);
1101 #if USE_WIENER_REFINEMENT_SEARCH
1102 int64_t err2;
1103 int32_t tap_min[] = {WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP2_MINV};
1104 int32_t tap_max[] = {WIENER_FILT_TAP0_MAXV, WIENER_FILT_TAP1_MAXV, WIENER_FILT_TAP2_MAXV};
1105
1106 WienerInfo *plane_wiener = &rui->wiener_info;
1107
1108 // SVT_LOG("err pre = %"PRId64"\n", err);
1109 const int32_t start_step = 4;
1110 for (int32_t s = start_step; s >= 1; s >>= 1) {
1111 for (int32_t p = plane_off; p < WIENER_HALFWIN; ++p) {
1112 int32_t skip = 0;
1113 do {
1114 if (plane_wiener->hfilter[p] - s >= tap_min[p]) {
1115 plane_wiener->hfilter[p] -= (int16_t)s;
1116 plane_wiener->hfilter[WIENER_WIN - p - 1] -= (int16_t)s;
1117 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * (int16_t)s;
1118 err2 = try_restoration_unit_seg(rsc, limits, tile, rui);
1119 if (err2 > err) {
1120 plane_wiener->hfilter[p] += (int16_t)s;
1121 plane_wiener->hfilter[WIENER_WIN - p - 1] += (int16_t)s;
1122 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * (int16_t)s;
1123 } else {
1124 err = err2;
1125 skip = 1;
1126 // At the highest step size continue moving in the same direction
1127 if (s == start_step)
1128 continue;
1129 }
1130 }
1131 break;
1132 } while (1);
1133 if (skip)
1134 break;
1135 do {
1136 if (plane_wiener->hfilter[p] + s <= tap_max[p]) {
1137 plane_wiener->hfilter[p] += (int16_t)s;
1138 plane_wiener->hfilter[WIENER_WIN - p - 1] += (int16_t)s;
1139 plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * (int16_t)s;
1140 err2 = try_restoration_unit_seg(rsc, limits, tile, rui);
1141 if (err2 > err) {
1142 plane_wiener->hfilter[p] -= (int16_t)s;
1143 plane_wiener->hfilter[WIENER_WIN - p - 1] -= (int16_t)s;
1144 plane_wiener->hfilter[WIENER_HALFWIN] += 2 * (int16_t)s;
1145 } else {
1146 err = err2;
1147 // At the highest step size continue moving in the same direction
1148 if (s == start_step)
1149 continue;
1150 }
1151 }
1152 break;
1153 } while (1);
1154 }
1155 for (int32_t p = plane_off; p < WIENER_HALFWIN; ++p) {
1156 int32_t skip = 0;
1157 do {
1158 if (plane_wiener->vfilter[p] - s >= tap_min[p]) {
1159 plane_wiener->vfilter[p] -= (int16_t)s;
1160 plane_wiener->vfilter[WIENER_WIN - p - 1] -= (int16_t)s;
1161 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * (int16_t)s;
1162 err2 = try_restoration_unit_seg(rsc, limits, tile, rui);
1163 if (err2 > err) {
1164 plane_wiener->vfilter[p] += (int16_t)s;
1165 plane_wiener->vfilter[WIENER_WIN - p - 1] += (int16_t)s;
1166 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * (int16_t)s;
1167 } else {
1168 err = err2;
1169 skip = 1;
1170 // At the highest step size continue moving in the same direction
1171 if (s == start_step)
1172 continue;
1173 }
1174 }
1175 break;
1176 } while (1);
1177 if (skip)
1178 break;
1179 do {
1180 if (plane_wiener->vfilter[p] + s <= tap_max[p]) {
1181 plane_wiener->vfilter[p] += (int16_t)s;
1182 plane_wiener->vfilter[WIENER_WIN - p - 1] += (int16_t)s;
1183 plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * (int16_t)s;
1184 err2 = try_restoration_unit_seg(rsc, limits, tile, rui);
1185 if (err2 > err) {
1186 plane_wiener->vfilter[p] -= (int16_t)s;
1187 plane_wiener->vfilter[WIENER_WIN - p - 1] -= (int16_t)s;
1188 plane_wiener->vfilter[WIENER_HALFWIN] += 2 * (int16_t)s;
1189 } else {
1190 err = err2;
1191 // At the highest step size continue moving in the same direction
1192 if (s == start_step)
1193 continue;
1194 }
1195 }
1196 break;
1197 } while (1);
1198 }
1199 }
1200 // SVT_LOG("err post = %"PRId64"\n", err);
1201 #endif // USE_WIENER_REFINEMENT_SEARCH
1202 return err;
1203 }
search_switchable(const RestorationTileLimits * limits,const Av1PixelRect * tile_rect,int32_t rest_unit_idx,void * priv)1204 static void search_switchable(const RestorationTileLimits *limits, const Av1PixelRect *tile_rect,
1205 int32_t rest_unit_idx, void *priv) {
1206 (void)limits;
1207 (void)tile_rect;
1208 RestSearchCtxt * rsc = (RestSearchCtxt *)priv;
1209 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1210
1211 const Macroblock *const x = rsc->x;
1212
1213 const int32_t wiener_win = (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
1214
1215 double best_cost = 0;
1216 int64_t best_bits = 0;
1217 RestorationType best_rtype = RESTORE_NONE;
1218
1219 //CHKN for (RestorationType r = 0; r < RESTORE_SWITCHABLE_TYPES; ++r) {
1220 for (int32_t rest_type = 0; rest_type < RESTORE_SWITCHABLE_TYPES; ++rest_type) {
1221 RestorationType r = (RestorationType)rest_type;
1222
1223 // Check for the condition that wiener or sgrproj search could not
1224 // find a solution or the solution was worse than RESTORE_NONE.
1225 // In either case the best_rtype will be set as RESTORE_NONE. These
1226 // should be skipped from the test below.
1227 if (r > RESTORE_NONE)
1228 if (rusi->best_rtype[r - 1] == RESTORE_NONE)
1229 continue;
1230 const int64_t sse = rusi->sse[r];
1231 int64_t coeff_pcost = 0;
1232 switch (r) {
1233 case RESTORE_NONE: coeff_pcost = 0; break;
1234 case RESTORE_WIENER:
1235 coeff_pcost = count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener);
1236 break;
1237 case RESTORE_SGRPROJ:
1238 coeff_pcost = count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj);
1239 break;
1240 default: assert(0); break;
1241 }
1242 const int64_t coeff_bits = coeff_pcost << AV1_PROB_COST_SHIFT;
1243 const int64_t bits = x->switchable_restore_cost[r] + coeff_bits;
1244 double cost = RDCOST_DBL(x->rdmult, bits >> 4, sse);
1245 if (r == 0 || cost < best_cost) {
1246 best_cost = cost;
1247 best_bits = bits;
1248 best_rtype = r;
1249 }
1250 }
1251
1252 rusi->best_rtype[RESTORE_SWITCHABLE - 1] = best_rtype;
1253
1254 rsc->sse += rusi->sse[best_rtype];
1255 rsc->bits += best_bits;
1256 if (best_rtype == RESTORE_WIENER)
1257 rsc->wiener = rusi->wiener;
1258 if (best_rtype == RESTORE_SGRPROJ)
1259 rsc->sgrproj = rusi->sgrproj;
1260 }
1261
copy_unit_info(RestorationType frame_rtype,const RestUnitSearchInfo * rusi,RestorationUnitInfo * rui)1262 static void copy_unit_info(RestorationType frame_rtype, const RestUnitSearchInfo *rusi,
1263 RestorationUnitInfo *rui) {
1264 if (frame_rtype >= 1)
1265 rui->restoration_type = rusi->best_rtype[frame_rtype - 1];
1266 if (rui->restoration_type == RESTORE_WIENER)
1267 rui->wiener_info = rusi->wiener;
1268 else
1269 rui->sgrproj_info = rusi->sgrproj;
1270 }
1271
rest_tiles_in_plane(const Av1Common * cm,int32_t plane)1272 static int32_t rest_tiles_in_plane(const Av1Common *cm, int32_t plane) {
1273 const RestorationInfo *rsi = &cm->child_pcs->rst_info[plane];
1274 return rsi->units_per_tile;
1275 }
1276
1277 void *svt_aom_memalign(size_t align, size_t size);
1278 void svt_aom_free(void *memblk);
1279
search_sgrproj_seg(const RestorationTileLimits * limits,const Av1PixelRect * tile,int32_t rest_unit_idx,void * priv)1280 static void search_sgrproj_seg(const RestorationTileLimits *limits, const Av1PixelRect *tile,
1281 int32_t rest_unit_idx, void *priv) {
1282 RestSearchCtxt * rsc = (RestSearchCtxt *)priv;
1283 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1284
1285 Av1Common *const cm = rsc->cm;
1286 const int32_t highbd = cm->use_highbitdepth;
1287 const int32_t bit_depth = cm->bit_depth;
1288
1289 uint8_t *dgd_start = rsc->dgd_buffer + limits->v_start * rsc->dgd_stride + limits->h_start;
1290 const uint8_t *src_start = rsc->src_buffer + limits->v_start * rsc->src_stride +
1291 limits->h_start;
1292
1293 const int32_t is_uv = rsc->plane > 0;
1294 const int32_t ss_x = is_uv && cm->subsampling_x;
1295 const int32_t ss_y = is_uv && cm->subsampling_y;
1296 const int32_t procunit_width = RESTORATION_PROC_UNIT_SIZE >> ss_x;
1297 const int32_t procunit_height = RESTORATION_PROC_UNIT_SIZE >> ss_y;
1298 int8_t step = get_sg_step(cm->sg_filter_mode);
1299
1300 rusi->sgrproj = search_selfguided_restoration(dgd_start,
1301 limits->h_end - limits->h_start,
1302 limits->v_end - limits->v_start,
1303 rsc->dgd_stride,
1304 src_start,
1305 rsc->src_stride,
1306 highbd,
1307 bit_depth,
1308 procunit_width,
1309 procunit_height,
1310 rsc->tmpbuf,
1311 cm->sg_ref_frame_ep,
1312 cm->sg_frame_ep_cnt,
1313 step);
1314
1315 RestorationUnitInfo rui;
1316 rui.restoration_type = RESTORE_SGRPROJ;
1317 rui.sgrproj_info = rusi->sgrproj;
1318
1319 rusi->sse[RESTORE_SGRPROJ] = try_restoration_unit_seg(rsc, limits, tile, &rui);
1320 }
1321
search_sgrproj_finish(const RestorationTileLimits * limits,const Av1PixelRect * tile,int32_t rest_unit_idx,void * priv)1322 static void search_sgrproj_finish(const RestorationTileLimits *limits, const Av1PixelRect *tile,
1323 int32_t rest_unit_idx, void *priv) {
1324 (void)limits;
1325 (void)tile;
1326 RestSearchCtxt * rsc = (RestSearchCtxt *)priv;
1327 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1328
1329 const Macroblock *const x = rsc->x;
1330
1331 rusi->sse[RESTORE_SGRPROJ] = rsc->rusi_pic[rest_unit_idx].sse[RESTORE_SGRPROJ];
1332 rusi->sgrproj = rsc->rusi_pic[rest_unit_idx].sgrproj;
1333
1334 const int64_t bits_none = x->sgrproj_restore_cost[0];
1335 const int64_t bits_sgr = x->sgrproj_restore_cost[1] +
1336 (count_sgrproj_bits(&rusi->sgrproj, &rsc->sgrproj) << AV1_PROB_COST_SHIFT);
1337
1338 double cost_none = RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
1339 double cost_sgr = RDCOST_DBL(x->rdmult, bits_sgr >> 4, rusi->sse[RESTORE_SGRPROJ]);
1340
1341 RestorationType rtype = (cost_sgr < cost_none) ? RESTORE_SGRPROJ : RESTORE_NONE;
1342 rusi->best_rtype[RESTORE_SGRPROJ - 1] = rtype;
1343
1344 rsc->sse += rusi->sse[rtype];
1345 rsc->bits += (cost_sgr < cost_none) ? bits_sgr : bits_none;
1346 if (cost_sgr < cost_none)
1347 rsc->sgrproj = rusi->sgrproj;
1348 }
1349
search_wiener_seg(const RestorationTileLimits * limits,const Av1PixelRect * tile_rect,int32_t rest_unit_idx,void * priv)1350 static void search_wiener_seg(const RestorationTileLimits *limits, const Av1PixelRect *tile_rect,
1351 int32_t rest_unit_idx, void *priv) {
1352 RestSearchCtxt * rsc = (RestSearchCtxt *)priv;
1353 RestUnitSearchInfo * rusi = &rsc->rusi[rest_unit_idx];
1354 const Av1Common *const cm = rsc->cm;
1355 int32_t wn_luma = cm->wn_filter_mode == 1 ? WIENER_WIN_3TAP
1356 : cm->wn_filter_mode == 2 ? WIENER_WIN_CHROMA
1357 : WIENER_WIN;
1358 const int32_t wiener_win = cm->wn_filter_mode == 1 ? WIENER_WIN_3TAP
1359 : (rsc->plane == AOM_PLANE_Y) ? wn_luma
1360 : WIENER_WIN_CHROMA;
1361 EB_ALIGN(32) int64_t M[WIENER_WIN2];
1362 EB_ALIGN(32) int64_t H[WIENER_WIN2 * WIENER_WIN2];
1363 int32_t vfilterd[WIENER_WIN], hfilterd[WIENER_WIN];
1364
1365 if (cm->use_highbitdepth)
1366 svt_av1_compute_stats_highbd(wiener_win,
1367 rsc->dgd_buffer,
1368 rsc->src_buffer,
1369 limits->h_start,
1370 limits->h_end,
1371 limits->v_start,
1372 limits->v_end,
1373 rsc->dgd_stride,
1374 rsc->src_stride,
1375 M,
1376 H,
1377 (AomBitDepth)cm->bit_depth);
1378 else
1379 svt_av1_compute_stats(wiener_win,
1380 rsc->dgd_buffer,
1381 rsc->src_buffer,
1382 limits->h_start,
1383 limits->h_end,
1384 limits->v_start,
1385 limits->v_end,
1386 rsc->dgd_stride,
1387 rsc->src_stride,
1388 M,
1389 H);
1390
1391 if (!wiener_decompose_sep_sym(wiener_win, M, H, vfilterd, hfilterd)) {
1392 SVT_LOG("CHKN never get here\n");
1393 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1394 rusi->sse[RESTORE_WIENER] = INT64_MAX;
1395 return;
1396 }
1397
1398 RestorationUnitInfo rui;
1399 memset(&rui, 0, sizeof(rui));
1400 rui.restoration_type = RESTORE_WIENER;
1401 finalize_sym_filter(wiener_win, vfilterd, rui.wiener_info.vfilter);
1402 finalize_sym_filter(wiener_win, hfilterd, rui.wiener_info.hfilter);
1403
1404 // Filter score computes the value of the function x'*A*x - x'*b for the
1405 // learned filter and compares it against identity filer. If there is no
1406 // reduction in the function, the filter is reverted back to identity
1407 if (compute_score(wiener_win, M, H, rui.wiener_info.vfilter, rui.wiener_info.hfilter) > 0) {
1408 rusi->sse[RESTORE_WIENER] = INT64_MAX;
1409 return;
1410 }
1411
1412 #ifdef ARCH_X86_64
1413 aom_clear_system_state();
1414 #endif
1415
1416 rusi->sse[RESTORE_WIENER] = finer_tile_search_wiener_seg(
1417 rsc, limits, tile_rect, &rui, wiener_win);
1418 rusi->wiener = rui.wiener_info;
1419
1420 if (wiener_win != WIENER_WIN) {
1421 assert(rui.wiener_info.vfilter[0] == 0 && rui.wiener_info.vfilter[WIENER_WIN - 1] == 0);
1422 assert(rui.wiener_info.hfilter[0] == 0 && rui.wiener_info.hfilter[WIENER_WIN - 1] == 0);
1423 }
1424 }
search_wiener_finish(const RestorationTileLimits * limits,const Av1PixelRect * tile_rect,int32_t rest_unit_idx,void * priv)1425 static void search_wiener_finish(const RestorationTileLimits *limits, const Av1PixelRect *tile_rect,
1426 int32_t rest_unit_idx, void *priv) {
1427 (void)limits;
1428 (void)tile_rect;
1429 RestSearchCtxt * rsc = (RestSearchCtxt *)priv;
1430 RestUnitSearchInfo * rusi = &rsc->rusi[rest_unit_idx];
1431 const Av1Common *const cm = rsc->cm;
1432 int32_t wn_luma = cm->wn_filter_mode == 1 ? WIENER_WIN_3TAP
1433 : cm->wn_filter_mode == 2 ? WIENER_WIN_CHROMA
1434 : WIENER_WIN;
1435 const int32_t wiener_win = cm->wn_filter_mode == 1 ? WIENER_WIN_3TAP
1436 : (rsc->plane == AOM_PLANE_Y) ? wn_luma
1437 : WIENER_WIN_CHROMA;
1438
1439 const Macroblock *const x = rsc->x;
1440 const int64_t bits_none = x->wiener_restore_cost[0];
1441
1442 RestorationUnitInfo rui;
1443 memset(&rui, 0, sizeof(rui));
1444 rui.restoration_type = RESTORE_WIENER;
1445
1446 // Filter score computes the value of the function x'*A*x - x'*b for the
1447 // learned filter and compares it against identity filer. If there is no
1448 // reduction in the function, the filter is reverted back to identity
1449
1450 rusi->sse[RESTORE_WIENER] = rsc->rusi_pic[rest_unit_idx].sse[RESTORE_WIENER];
1451 if (rusi->sse[RESTORE_WIENER] == INT64_MAX) {
1452 rsc->bits += bits_none;
1453 rsc->sse += rusi->sse[RESTORE_NONE];
1454 rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1455 rusi->sse[RESTORE_WIENER] = INT64_MAX;
1456 return;
1457 }
1458
1459 #ifdef ARCH_X86_64
1460 aom_clear_system_state();
1461 #endif
1462
1463 rusi->wiener = rsc->rusi_pic[rest_unit_idx].wiener;
1464
1465 const int64_t bits_wiener = x->wiener_restore_cost[1] +
1466 (count_wiener_bits(wiener_win, &rusi->wiener, &rsc->wiener) << AV1_PROB_COST_SHIFT);
1467
1468 double cost_none = RDCOST_DBL(x->rdmult, bits_none >> 4, rusi->sse[RESTORE_NONE]);
1469 double cost_wiener = RDCOST_DBL(x->rdmult, bits_wiener >> 4, rusi->sse[RESTORE_WIENER]);
1470
1471 RestorationType rtype = (cost_wiener < cost_none) ? RESTORE_WIENER : RESTORE_NONE;
1472 rusi->best_rtype[RESTORE_WIENER - 1] = rtype;
1473
1474 rsc->sse += rusi->sse[rtype];
1475 rsc->bits += (cost_wiener < cost_none) ? bits_wiener : bits_none;
1476 if (cost_wiener < cost_none)
1477 rsc->wiener = rusi->wiener;
1478 }
search_norestore_seg(const RestorationTileLimits * limits,const Av1PixelRect * tile_rect,int32_t rest_unit_idx,void * priv)1479 static void search_norestore_seg(const RestorationTileLimits *limits, const Av1PixelRect *tile_rect,
1480 int32_t rest_unit_idx, void *priv) {
1481 (void)tile_rect;
1482
1483 RestSearchCtxt * rsc = (RestSearchCtxt *)priv;
1484 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1485
1486 const int32_t highbd = rsc->cm->use_highbitdepth;
1487 rusi->sse[RESTORE_NONE] = sse_restoration_unit(
1488 limits, rsc->src, rsc->cm->frame_to_show, rsc->plane, highbd);
1489 }
search_norestore_finish(const RestorationTileLimits * limits,const Av1PixelRect * tile_rect,int32_t rest_unit_idx,void * priv)1490 static void search_norestore_finish(const RestorationTileLimits *limits,
1491 const Av1PixelRect *tile_rect, int32_t rest_unit_idx,
1492 void *priv) {
1493 (void)tile_rect;
1494 (void)limits;
1495
1496 RestSearchCtxt * rsc = (RestSearchCtxt *)priv;
1497 RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1498
1499 rusi->sse[RESTORE_NONE] = rsc->rusi_pic[rest_unit_idx].sse[RESTORE_NONE];
1500
1501 rsc->sse += rusi->sse[RESTORE_NONE];
1502 }
search_rest_type_finish(RestSearchCtxt * rsc,RestorationType rtype)1503 static double search_rest_type_finish(RestSearchCtxt *rsc, RestorationType rtype) {
1504 static const RestUnitVisitor funs[RESTORE_TYPES] = {
1505 search_norestore_finish, search_wiener_finish, search_sgrproj_finish, search_switchable};
1506
1507 reset_rsc(rsc);
1508
1509 av1_foreach_rest_unit_in_frame(rsc->cm, rsc->plane, rsc_on_tile, funs[rtype], rsc);
1510
1511 return RDCOST_DBL(rsc->x->rdmult, rsc->bits >> 4, rsc->sse);
1512 }
1513
restoration_seg_search(int32_t * rst_tmpbuf,Yv12BufferConfig * org_fts,const Yv12BufferConfig * src,Yv12BufferConfig * trial_frame_rst,PictureControlSet * pcs_ptr,uint32_t segment_index)1514 void restoration_seg_search(int32_t *rst_tmpbuf, Yv12BufferConfig *org_fts,
1515 const Yv12BufferConfig *src, Yv12BufferConfig *trial_frame_rst,
1516 PictureControlSet *pcs_ptr, uint32_t segment_index) {
1517 Av1Common *const cm = pcs_ptr->parent_pcs_ptr->av1_cm;
1518 Macroblock * x = pcs_ptr->parent_pcs_ptr->av1x;
1519
1520 // If the restoration unit dimensions are not multiples of
1521 // rsi->restoration_unit_size then some elements of the rusi array may be
1522 // left uninitialised when we reach copy_unit_info(...). This is not a
1523 // problem, as these elements are ignored later, but in order to quiet
1524 // Valgrind's warnings we initialise the array to zero.
1525
1526 RestSearchCtxt rsc; //this context is specific for this segment
1527 RestSearchCtxt *rsc_p = &rsc;
1528
1529 const int32_t plane_start = AOM_PLANE_Y;
1530 const int32_t plane_end = AOM_PLANE_V;
1531 for (int32_t plane = plane_start; plane <= plane_end; ++plane) {
1532 RestUnitSearchInfo *rusi = pcs_ptr->rusi_picture[plane];
1533
1534 init_rsc_seg(org_fts, src, cm, x, plane, rusi, trial_frame_rst, &rsc);
1535
1536 rsc_p->tmpbuf = rst_tmpbuf;
1537
1538 const int32_t highbd = rsc.cm->use_highbitdepth;
1539 svt_extend_frame(rsc.dgd_buffer,
1540 rsc.plane_width,
1541 rsc.plane_height,
1542 rsc.dgd_stride,
1543 RESTORATION_BORDER,
1544 RESTORATION_BORDER,
1545 highbd);
1546
1547 av1_foreach_rest_unit_in_frame_seg(rsc_p->cm,
1548 rsc_p->plane,
1549 rsc_on_tile,
1550 search_norestore_seg,
1551 rsc_p,
1552 pcs_ptr->rest_segments_column_count,
1553 pcs_ptr->rest_segments_row_count,
1554 segment_index);
1555 if (cm->wn_filter_mode)
1556 av1_foreach_rest_unit_in_frame_seg(rsc_p->cm,
1557 rsc_p->plane,
1558 rsc_on_tile,
1559 search_wiener_seg,
1560 rsc_p,
1561 pcs_ptr->rest_segments_column_count,
1562 pcs_ptr->rest_segments_row_count,
1563 segment_index);
1564 av1_foreach_rest_unit_in_frame_seg(rsc_p->cm,
1565 rsc_p->plane,
1566 rsc_on_tile,
1567 search_sgrproj_seg,
1568 rsc_p,
1569 pcs_ptr->rest_segments_column_count,
1570 pcs_ptr->rest_segments_row_count,
1571 segment_index);
1572 }
1573 }
1574
rest_finish_search(PictureControlSet * pcs_ptr)1575 void rest_finish_search(PictureControlSet *pcs_ptr){
1576 Macroblock *x = pcs_ptr->parent_pcs_ptr->av1x;
1577 Av1Common *const cm = pcs_ptr->parent_pcs_ptr->av1_cm;
1578 PictureParentControlSet *p_pcs_ptr = pcs_ptr->parent_pcs_ptr;
1579 RestorationType force_restore_type_d = (cm->wn_filter_mode) ? RESTORE_TYPES : RESTORE_SGRPROJ;
1580 int32_t ntiles[2];
1581 for (int32_t is_uv = 0; is_uv < 2; ++is_uv) ntiles[is_uv] = rest_tiles_in_plane(cm, is_uv);
1582
1583 assert(ntiles[1] <= ntiles[0]);
1584 RestUnitSearchInfo *rusi = (RestUnitSearchInfo *)svt_aom_memalign(16,
1585 sizeof(*rusi) * ntiles[0]);
1586
1587 // If the restoration unit dimensions are not multiples of
1588 // rsi->restoration_unit_size then some elements of the rusi array may be
1589 // left uninitialised when we reach copy_unit_info(...). This is not a
1590 // problem, as these elements are ignored later, but in order to quiet
1591 // Valgrind's warnings we initialise the array below.
1592 memset(rusi, 0, sizeof(*rusi) * ntiles[0]);
1593
1594 RestSearchCtxt rsc;
1595 const int32_t plane_start = AOM_PLANE_Y;
1596 const int32_t plane_end = AOM_PLANE_V;
1597 for (int32_t plane = plane_start; plane <= plane_end; ++plane) {
1598 //init rsc context for this plane
1599 rsc.cm = cm;
1600 rsc.x = x;
1601 rsc.plane = plane;
1602 rsc.rusi = rusi;
1603 rsc.pic_num = (uint32_t)p_pcs_ptr->picture_number;
1604 rsc.rusi_pic = pcs_ptr->rusi_picture[plane];
1605
1606 const int32_t plane_ntiles = ntiles[plane > 0];
1607 const RestorationType num_rtypes = (plane_ntiles > 1) ? RESTORE_TYPES
1608 : RESTORE_SWITCHABLE_TYPES;
1609
1610 double best_cost = 0;
1611 RestorationType best_rtype = RESTORE_NONE;
1612
1613 for (int32_t rest_type = 0; rest_type < num_rtypes; ++rest_type) {
1614 RestorationType r = (RestorationType)rest_type;
1615
1616 if ((force_restore_type_d != RESTORE_TYPES) && (r != RESTORE_NONE) &&
1617 (r != force_restore_type_d))
1618 continue;
1619
1620 double cost = search_rest_type_finish(&rsc, r);
1621
1622 if (r == 0 || cost < best_cost) {
1623 best_cost = cost;
1624 best_rtype = r;
1625 }
1626 }
1627 cm->child_pcs->rst_info[plane].frame_restoration_type = best_rtype;
1628 if (force_restore_type_d != RESTORE_TYPES)
1629 assert(best_rtype == force_restore_type_d || best_rtype == RESTORE_NONE);
1630
1631 if (best_rtype != RESTORE_NONE) {
1632 for (int32_t u = 0; u < plane_ntiles; ++u)
1633 copy_unit_info(best_rtype, &rusi[u], &cm->child_pcs->rst_info[plane].unit_info[u]);
1634 }
1635 }
1636
1637 svt_aom_free(rusi);
1638 }
1639