1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <float.h>
14*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
15*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
16*77c1e3ccSAndroid Build Coastguard Worker
17*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_scale_rtcd.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "config/av1_rtcd.h"
19*77c1e3ccSAndroid Build Coastguard Worker
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/binary_codes_writer.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/mathutils.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/psnr.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "aom_mem/aom_mem.h"
25*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem.h"
26*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/av1_common_int.h"
27*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/quant_common.h"
28*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/restoration.h"
29*77c1e3ccSAndroid Build Coastguard Worker
30*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/av1_quantize.h"
31*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
32*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/picklpf.h"
33*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/pickrst.h"
34*77c1e3ccSAndroid Build Coastguard Worker
35*77c1e3ccSAndroid Build Coastguard Worker // Number of Wiener iterations
36*77c1e3ccSAndroid Build Coastguard Worker #define NUM_WIENER_ITERS 5
37*77c1e3ccSAndroid Build Coastguard Worker
38*77c1e3ccSAndroid Build Coastguard Worker // Penalty factor for use of dual sgr
39*77c1e3ccSAndroid Build Coastguard Worker #define DUAL_SGR_PENALTY_MULT 0.01
40*77c1e3ccSAndroid Build Coastguard Worker
41*77c1e3ccSAndroid Build Coastguard Worker // Working precision for Wiener filter coefficients
42*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_TAP_SCALE_FACTOR ((int64_t)1 << 16)
43*77c1e3ccSAndroid Build Coastguard Worker
44*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_EP_GRP1_START_IDX 0
45*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_EP_GRP1_END_IDX 9
46*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_EP_GRP1_SEARCH_COUNT 4
47*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_EP_GRP2_3_SEARCH_COUNT 2
48*77c1e3ccSAndroid Build Coastguard Worker static const int sgproj_ep_grp1_seed[SGRPROJ_EP_GRP1_SEARCH_COUNT] = { 0, 3, 6,
49*77c1e3ccSAndroid Build Coastguard Worker 9 };
50*77c1e3ccSAndroid Build Coastguard Worker static const int sgproj_ep_grp2_3[SGRPROJ_EP_GRP2_3_SEARCH_COUNT][14] = {
51*77c1e3ccSAndroid Build Coastguard Worker { 10, 10, 11, 11, 12, 12, 13, 13, 13, 13, -1, -1, -1, -1 },
52*77c1e3ccSAndroid Build Coastguard Worker { 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15 }
53*77c1e3ccSAndroid Build Coastguard Worker };
54*77c1e3ccSAndroid Build Coastguard Worker
55*77c1e3ccSAndroid Build Coastguard Worker #if DEBUG_LR_COSTING
56*77c1e3ccSAndroid Build Coastguard Worker RestorationUnitInfo lr_ref_params[RESTORE_TYPES][MAX_MB_PLANE]
57*77c1e3ccSAndroid Build Coastguard Worker [MAX_LR_UNITS_W * MAX_LR_UNITS_H];
58*77c1e3ccSAndroid Build Coastguard Worker #endif // DEBUG_LR_COSTING
59*77c1e3ccSAndroid Build Coastguard Worker
60*77c1e3ccSAndroid Build Coastguard Worker typedef int64_t (*sse_extractor_type)(const YV12_BUFFER_CONFIG *a,
61*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *b);
62*77c1e3ccSAndroid Build Coastguard Worker typedef int64_t (*sse_part_extractor_type)(const YV12_BUFFER_CONFIG *a,
63*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *b,
64*77c1e3ccSAndroid Build Coastguard Worker int hstart, int width, int vstart,
65*77c1e3ccSAndroid Build Coastguard Worker int height);
66*77c1e3ccSAndroid Build Coastguard Worker typedef uint64_t (*var_part_extractor_type)(const YV12_BUFFER_CONFIG *a,
67*77c1e3ccSAndroid Build Coastguard Worker int hstart, int width, int vstart,
68*77c1e3ccSAndroid Build Coastguard Worker int height);
69*77c1e3ccSAndroid Build Coastguard Worker
70*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
71*77c1e3ccSAndroid Build Coastguard Worker #define NUM_EXTRACTORS (3 * (1 + 1))
72*77c1e3ccSAndroid Build Coastguard Worker #else
73*77c1e3ccSAndroid Build Coastguard Worker #define NUM_EXTRACTORS 3
74*77c1e3ccSAndroid Build Coastguard Worker #endif
75*77c1e3ccSAndroid Build Coastguard Worker static const sse_part_extractor_type sse_part_extractors[NUM_EXTRACTORS] = {
76*77c1e3ccSAndroid Build Coastguard Worker aom_get_y_sse_part, aom_get_u_sse_part,
77*77c1e3ccSAndroid Build Coastguard Worker aom_get_v_sse_part,
78*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
79*77c1e3ccSAndroid Build Coastguard Worker aom_highbd_get_y_sse_part, aom_highbd_get_u_sse_part,
80*77c1e3ccSAndroid Build Coastguard Worker aom_highbd_get_v_sse_part,
81*77c1e3ccSAndroid Build Coastguard Worker #endif
82*77c1e3ccSAndroid Build Coastguard Worker };
83*77c1e3ccSAndroid Build Coastguard Worker static const var_part_extractor_type var_part_extractors[NUM_EXTRACTORS] = {
84*77c1e3ccSAndroid Build Coastguard Worker aom_get_y_var, aom_get_u_var, aom_get_v_var,
85*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
86*77c1e3ccSAndroid Build Coastguard Worker aom_highbd_get_y_var, aom_highbd_get_u_var, aom_highbd_get_v_var,
87*77c1e3ccSAndroid Build Coastguard Worker #endif
88*77c1e3ccSAndroid Build Coastguard Worker };
89*77c1e3ccSAndroid Build Coastguard Worker
sse_restoration_unit(const RestorationTileLimits * limits,const YV12_BUFFER_CONFIG * src,const YV12_BUFFER_CONFIG * dst,int plane,int highbd)90*77c1e3ccSAndroid Build Coastguard Worker static int64_t sse_restoration_unit(const RestorationTileLimits *limits,
91*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *src,
92*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *dst, int plane,
93*77c1e3ccSAndroid Build Coastguard Worker int highbd) {
94*77c1e3ccSAndroid Build Coastguard Worker return sse_part_extractors[3 * highbd + plane](
95*77c1e3ccSAndroid Build Coastguard Worker src, dst, limits->h_start, limits->h_end - limits->h_start,
96*77c1e3ccSAndroid Build Coastguard Worker limits->v_start, limits->v_end - limits->v_start);
97*77c1e3ccSAndroid Build Coastguard Worker }
98*77c1e3ccSAndroid Build Coastguard Worker
var_restoration_unit(const RestorationTileLimits * limits,const YV12_BUFFER_CONFIG * src,int plane,int highbd)99*77c1e3ccSAndroid Build Coastguard Worker static uint64_t var_restoration_unit(const RestorationTileLimits *limits,
100*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *src, int plane,
101*77c1e3ccSAndroid Build Coastguard Worker int highbd) {
102*77c1e3ccSAndroid Build Coastguard Worker return var_part_extractors[3 * highbd + plane](
103*77c1e3ccSAndroid Build Coastguard Worker src, limits->h_start, limits->h_end - limits->h_start, limits->v_start,
104*77c1e3ccSAndroid Build Coastguard Worker limits->v_end - limits->v_start);
105*77c1e3ccSAndroid Build Coastguard Worker }
106*77c1e3ccSAndroid Build Coastguard Worker
107*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
108*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *src;
109*77c1e3ccSAndroid Build Coastguard Worker YV12_BUFFER_CONFIG *dst;
110*77c1e3ccSAndroid Build Coastguard Worker
111*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *cm;
112*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCK *x;
113*77c1e3ccSAndroid Build Coastguard Worker int plane;
114*77c1e3ccSAndroid Build Coastguard Worker int plane_w;
115*77c1e3ccSAndroid Build Coastguard Worker int plane_h;
116*77c1e3ccSAndroid Build Coastguard Worker RestUnitSearchInfo *rusi;
117*77c1e3ccSAndroid Build Coastguard Worker
118*77c1e3ccSAndroid Build Coastguard Worker // Speed features
119*77c1e3ccSAndroid Build Coastguard Worker const LOOP_FILTER_SPEED_FEATURES *lpf_sf;
120*77c1e3ccSAndroid Build Coastguard Worker
121*77c1e3ccSAndroid Build Coastguard Worker uint8_t *dgd_buffer;
122*77c1e3ccSAndroid Build Coastguard Worker int dgd_stride;
123*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src_buffer;
124*77c1e3ccSAndroid Build Coastguard Worker int src_stride;
125*77c1e3ccSAndroid Build Coastguard Worker
126*77c1e3ccSAndroid Build Coastguard Worker // SSE values for each restoration mode for the current RU
127*77c1e3ccSAndroid Build Coastguard Worker // These are saved by each search function for use in search_switchable()
128*77c1e3ccSAndroid Build Coastguard Worker int64_t sse[RESTORE_SWITCHABLE_TYPES];
129*77c1e3ccSAndroid Build Coastguard Worker
130*77c1e3ccSAndroid Build Coastguard Worker // This flag will be set based on the speed feature
131*77c1e3ccSAndroid Build Coastguard Worker // 'prune_sgr_based_on_wiener'. 0 implies no pruning and 1 implies pruning.
132*77c1e3ccSAndroid Build Coastguard Worker uint8_t skip_sgr_eval;
133*77c1e3ccSAndroid Build Coastguard Worker
134*77c1e3ccSAndroid Build Coastguard Worker // Total rate and distortion so far for each restoration type
135*77c1e3ccSAndroid Build Coastguard Worker // These are initialised by reset_rsc in search_rest_type
136*77c1e3ccSAndroid Build Coastguard Worker int64_t total_sse[RESTORE_TYPES];
137*77c1e3ccSAndroid Build Coastguard Worker int64_t total_bits[RESTORE_TYPES];
138*77c1e3ccSAndroid Build Coastguard Worker
139*77c1e3ccSAndroid Build Coastguard Worker // Reference parameters for delta-coding
140*77c1e3ccSAndroid Build Coastguard Worker //
141*77c1e3ccSAndroid Build Coastguard Worker // For each restoration type, we need to store the latest parameter set which
142*77c1e3ccSAndroid Build Coastguard Worker // has been used, so that we can properly cost up the next parameter set.
143*77c1e3ccSAndroid Build Coastguard Worker // Note that we have two sets of these - one for the single-restoration-mode
144*77c1e3ccSAndroid Build Coastguard Worker // search (ie, frame_restoration_type = RESTORE_WIENER or RESTORE_SGRPROJ)
145*77c1e3ccSAndroid Build Coastguard Worker // and one for the switchable mode. This is because these two cases can lead
146*77c1e3ccSAndroid Build Coastguard Worker // to different sets of parameters being signaled, but we don't know which
147*77c1e3ccSAndroid Build Coastguard Worker // we will pick for sure until the end of the search process.
148*77c1e3ccSAndroid Build Coastguard Worker WienerInfo ref_wiener;
149*77c1e3ccSAndroid Build Coastguard Worker SgrprojInfo ref_sgrproj;
150*77c1e3ccSAndroid Build Coastguard Worker WienerInfo switchable_ref_wiener;
151*77c1e3ccSAndroid Build Coastguard Worker SgrprojInfo switchable_ref_sgrproj;
152*77c1e3ccSAndroid Build Coastguard Worker
153*77c1e3ccSAndroid Build Coastguard Worker // Buffers used to hold dgd-avg and src-avg data respectively during SIMD
154*77c1e3ccSAndroid Build Coastguard Worker // call of Wiener filter.
155*77c1e3ccSAndroid Build Coastguard Worker int16_t *dgd_avg;
156*77c1e3ccSAndroid Build Coastguard Worker int16_t *src_avg;
157*77c1e3ccSAndroid Build Coastguard Worker } RestSearchCtxt;
158*77c1e3ccSAndroid Build Coastguard Worker
rsc_on_tile(void * priv)159*77c1e3ccSAndroid Build Coastguard Worker static inline void rsc_on_tile(void *priv) {
160*77c1e3ccSAndroid Build Coastguard Worker RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
161*77c1e3ccSAndroid Build Coastguard Worker set_default_wiener(&rsc->ref_wiener);
162*77c1e3ccSAndroid Build Coastguard Worker set_default_sgrproj(&rsc->ref_sgrproj);
163*77c1e3ccSAndroid Build Coastguard Worker set_default_wiener(&rsc->switchable_ref_wiener);
164*77c1e3ccSAndroid Build Coastguard Worker set_default_sgrproj(&rsc->switchable_ref_sgrproj);
165*77c1e3ccSAndroid Build Coastguard Worker }
166*77c1e3ccSAndroid Build Coastguard Worker
reset_rsc(RestSearchCtxt * rsc)167*77c1e3ccSAndroid Build Coastguard Worker static inline void reset_rsc(RestSearchCtxt *rsc) {
168*77c1e3ccSAndroid Build Coastguard Worker memset(rsc->total_sse, 0, sizeof(rsc->total_sse));
169*77c1e3ccSAndroid Build Coastguard Worker memset(rsc->total_bits, 0, sizeof(rsc->total_bits));
170*77c1e3ccSAndroid Build Coastguard Worker }
171*77c1e3ccSAndroid Build Coastguard Worker
init_rsc(const YV12_BUFFER_CONFIG * src,const AV1_COMMON * cm,const MACROBLOCK * x,const LOOP_FILTER_SPEED_FEATURES * lpf_sf,int plane,RestUnitSearchInfo * rusi,YV12_BUFFER_CONFIG * dst,RestSearchCtxt * rsc)172*77c1e3ccSAndroid Build Coastguard Worker static inline void init_rsc(const YV12_BUFFER_CONFIG *src, const AV1_COMMON *cm,
173*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCK *x,
174*77c1e3ccSAndroid Build Coastguard Worker const LOOP_FILTER_SPEED_FEATURES *lpf_sf, int plane,
175*77c1e3ccSAndroid Build Coastguard Worker RestUnitSearchInfo *rusi, YV12_BUFFER_CONFIG *dst,
176*77c1e3ccSAndroid Build Coastguard Worker RestSearchCtxt *rsc) {
177*77c1e3ccSAndroid Build Coastguard Worker rsc->src = src;
178*77c1e3ccSAndroid Build Coastguard Worker rsc->dst = dst;
179*77c1e3ccSAndroid Build Coastguard Worker rsc->cm = cm;
180*77c1e3ccSAndroid Build Coastguard Worker rsc->x = x;
181*77c1e3ccSAndroid Build Coastguard Worker rsc->plane = plane;
182*77c1e3ccSAndroid Build Coastguard Worker rsc->rusi = rusi;
183*77c1e3ccSAndroid Build Coastguard Worker rsc->lpf_sf = lpf_sf;
184*77c1e3ccSAndroid Build Coastguard Worker
185*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *dgd = &cm->cur_frame->buf;
186*77c1e3ccSAndroid Build Coastguard Worker const int is_uv = plane != AOM_PLANE_Y;
187*77c1e3ccSAndroid Build Coastguard Worker int plane_w, plane_h;
188*77c1e3ccSAndroid Build Coastguard Worker av1_get_upsampled_plane_size(cm, is_uv, &plane_w, &plane_h);
189*77c1e3ccSAndroid Build Coastguard Worker assert(plane_w == src->crop_widths[is_uv]);
190*77c1e3ccSAndroid Build Coastguard Worker assert(plane_h == src->crop_heights[is_uv]);
191*77c1e3ccSAndroid Build Coastguard Worker assert(src->crop_widths[is_uv] == dgd->crop_widths[is_uv]);
192*77c1e3ccSAndroid Build Coastguard Worker assert(src->crop_heights[is_uv] == dgd->crop_heights[is_uv]);
193*77c1e3ccSAndroid Build Coastguard Worker
194*77c1e3ccSAndroid Build Coastguard Worker rsc->plane_w = plane_w;
195*77c1e3ccSAndroid Build Coastguard Worker rsc->plane_h = plane_h;
196*77c1e3ccSAndroid Build Coastguard Worker rsc->src_buffer = src->buffers[plane];
197*77c1e3ccSAndroid Build Coastguard Worker rsc->src_stride = src->strides[is_uv];
198*77c1e3ccSAndroid Build Coastguard Worker rsc->dgd_buffer = dgd->buffers[plane];
199*77c1e3ccSAndroid Build Coastguard Worker rsc->dgd_stride = dgd->strides[is_uv];
200*77c1e3ccSAndroid Build Coastguard Worker }
201*77c1e3ccSAndroid Build Coastguard Worker
try_restoration_unit(const RestSearchCtxt * rsc,const RestorationTileLimits * limits,const RestorationUnitInfo * rui)202*77c1e3ccSAndroid Build Coastguard Worker static int64_t try_restoration_unit(const RestSearchCtxt *rsc,
203*77c1e3ccSAndroid Build Coastguard Worker const RestorationTileLimits *limits,
204*77c1e3ccSAndroid Build Coastguard Worker const RestorationUnitInfo *rui) {
205*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = rsc->cm;
206*77c1e3ccSAndroid Build Coastguard Worker const int plane = rsc->plane;
207*77c1e3ccSAndroid Build Coastguard Worker const int is_uv = plane > 0;
208*77c1e3ccSAndroid Build Coastguard Worker const RestorationInfo *rsi = &cm->rst_info[plane];
209*77c1e3ccSAndroid Build Coastguard Worker RestorationLineBuffers rlbs;
210*77c1e3ccSAndroid Build Coastguard Worker const int bit_depth = cm->seq_params->bit_depth;
211*77c1e3ccSAndroid Build Coastguard Worker const int highbd = cm->seq_params->use_highbitdepth;
212*77c1e3ccSAndroid Build Coastguard Worker
213*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *fts = &cm->cur_frame->buf;
214*77c1e3ccSAndroid Build Coastguard Worker // TODO(yunqing): For now, only use optimized LR filter in decoder. Can be
215*77c1e3ccSAndroid Build Coastguard Worker // also used in encoder.
216*77c1e3ccSAndroid Build Coastguard Worker const int optimized_lr = 0;
217*77c1e3ccSAndroid Build Coastguard Worker
218*77c1e3ccSAndroid Build Coastguard Worker av1_loop_restoration_filter_unit(
219*77c1e3ccSAndroid Build Coastguard Worker limits, rui, &rsi->boundaries, &rlbs, rsc->plane_w, rsc->plane_h,
220*77c1e3ccSAndroid Build Coastguard Worker is_uv && cm->seq_params->subsampling_x,
221*77c1e3ccSAndroid Build Coastguard Worker is_uv && cm->seq_params->subsampling_y, highbd, bit_depth,
222*77c1e3ccSAndroid Build Coastguard Worker fts->buffers[plane], fts->strides[is_uv], rsc->dst->buffers[plane],
223*77c1e3ccSAndroid Build Coastguard Worker rsc->dst->strides[is_uv], cm->rst_tmpbuf, optimized_lr, cm->error);
224*77c1e3ccSAndroid Build Coastguard Worker
225*77c1e3ccSAndroid Build Coastguard Worker return sse_restoration_unit(limits, rsc->src, rsc->dst, plane, highbd);
226*77c1e3ccSAndroid Build Coastguard Worker }
227*77c1e3ccSAndroid Build Coastguard Worker
av1_lowbd_pixel_proj_error_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int xq[2],const sgr_params_type * params)228*77c1e3ccSAndroid Build Coastguard Worker int64_t av1_lowbd_pixel_proj_error_c(const uint8_t *src8, int width, int height,
229*77c1e3ccSAndroid Build Coastguard Worker int src_stride, const uint8_t *dat8,
230*77c1e3ccSAndroid Build Coastguard Worker int dat_stride, int32_t *flt0,
231*77c1e3ccSAndroid Build Coastguard Worker int flt0_stride, int32_t *flt1,
232*77c1e3ccSAndroid Build Coastguard Worker int flt1_stride, int xq[2],
233*77c1e3ccSAndroid Build Coastguard Worker const sgr_params_type *params) {
234*77c1e3ccSAndroid Build Coastguard Worker int i, j;
235*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src = src8;
236*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat = dat8;
237*77c1e3ccSAndroid Build Coastguard Worker int64_t err = 0;
238*77c1e3ccSAndroid Build Coastguard Worker if (params->r[0] > 0 && params->r[1] > 0) {
239*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < height; ++i) {
240*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < width; ++j) {
241*77c1e3ccSAndroid Build Coastguard Worker assert(flt1[j] < (1 << 15) && flt1[j] > -(1 << 15));
242*77c1e3ccSAndroid Build Coastguard Worker assert(flt0[j] < (1 << 15) && flt0[j] > -(1 << 15));
243*77c1e3ccSAndroid Build Coastguard Worker const int32_t u = (int32_t)(dat[j] << SGRPROJ_RST_BITS);
244*77c1e3ccSAndroid Build Coastguard Worker int32_t v = u << SGRPROJ_PRJ_BITS;
245*77c1e3ccSAndroid Build Coastguard Worker v += xq[0] * (flt0[j] - u) + xq[1] * (flt1[j] - u);
246*77c1e3ccSAndroid Build Coastguard Worker const int32_t e =
247*77c1e3ccSAndroid Build Coastguard Worker ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) - src[j];
248*77c1e3ccSAndroid Build Coastguard Worker err += ((int64_t)e * e);
249*77c1e3ccSAndroid Build Coastguard Worker }
250*77c1e3ccSAndroid Build Coastguard Worker dat += dat_stride;
251*77c1e3ccSAndroid Build Coastguard Worker src += src_stride;
252*77c1e3ccSAndroid Build Coastguard Worker flt0 += flt0_stride;
253*77c1e3ccSAndroid Build Coastguard Worker flt1 += flt1_stride;
254*77c1e3ccSAndroid Build Coastguard Worker }
255*77c1e3ccSAndroid Build Coastguard Worker } else if (params->r[0] > 0) {
256*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < height; ++i) {
257*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < width; ++j) {
258*77c1e3ccSAndroid Build Coastguard Worker assert(flt0[j] < (1 << 15) && flt0[j] > -(1 << 15));
259*77c1e3ccSAndroid Build Coastguard Worker const int32_t u = (int32_t)(dat[j] << SGRPROJ_RST_BITS);
260*77c1e3ccSAndroid Build Coastguard Worker int32_t v = u << SGRPROJ_PRJ_BITS;
261*77c1e3ccSAndroid Build Coastguard Worker v += xq[0] * (flt0[j] - u);
262*77c1e3ccSAndroid Build Coastguard Worker const int32_t e =
263*77c1e3ccSAndroid Build Coastguard Worker ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) - src[j];
264*77c1e3ccSAndroid Build Coastguard Worker err += ((int64_t)e * e);
265*77c1e3ccSAndroid Build Coastguard Worker }
266*77c1e3ccSAndroid Build Coastguard Worker dat += dat_stride;
267*77c1e3ccSAndroid Build Coastguard Worker src += src_stride;
268*77c1e3ccSAndroid Build Coastguard Worker flt0 += flt0_stride;
269*77c1e3ccSAndroid Build Coastguard Worker }
270*77c1e3ccSAndroid Build Coastguard Worker } else if (params->r[1] > 0) {
271*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < height; ++i) {
272*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < width; ++j) {
273*77c1e3ccSAndroid Build Coastguard Worker assert(flt1[j] < (1 << 15) && flt1[j] > -(1 << 15));
274*77c1e3ccSAndroid Build Coastguard Worker const int32_t u = (int32_t)(dat[j] << SGRPROJ_RST_BITS);
275*77c1e3ccSAndroid Build Coastguard Worker int32_t v = u << SGRPROJ_PRJ_BITS;
276*77c1e3ccSAndroid Build Coastguard Worker v += xq[1] * (flt1[j] - u);
277*77c1e3ccSAndroid Build Coastguard Worker const int32_t e =
278*77c1e3ccSAndroid Build Coastguard Worker ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) - src[j];
279*77c1e3ccSAndroid Build Coastguard Worker err += ((int64_t)e * e);
280*77c1e3ccSAndroid Build Coastguard Worker }
281*77c1e3ccSAndroid Build Coastguard Worker dat += dat_stride;
282*77c1e3ccSAndroid Build Coastguard Worker src += src_stride;
283*77c1e3ccSAndroid Build Coastguard Worker flt1 += flt1_stride;
284*77c1e3ccSAndroid Build Coastguard Worker }
285*77c1e3ccSAndroid Build Coastguard Worker } else {
286*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < height; ++i) {
287*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < width; ++j) {
288*77c1e3ccSAndroid Build Coastguard Worker const int32_t e = (int32_t)(dat[j]) - src[j];
289*77c1e3ccSAndroid Build Coastguard Worker err += ((int64_t)e * e);
290*77c1e3ccSAndroid Build Coastguard Worker }
291*77c1e3ccSAndroid Build Coastguard Worker dat += dat_stride;
292*77c1e3ccSAndroid Build Coastguard Worker src += src_stride;
293*77c1e3ccSAndroid Build Coastguard Worker }
294*77c1e3ccSAndroid Build Coastguard Worker }
295*77c1e3ccSAndroid Build Coastguard Worker
296*77c1e3ccSAndroid Build Coastguard Worker return err;
297*77c1e3ccSAndroid Build Coastguard Worker }
298*77c1e3ccSAndroid Build Coastguard Worker
299*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
av1_highbd_pixel_proj_error_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int xq[2],const sgr_params_type * params)300*77c1e3ccSAndroid Build Coastguard Worker int64_t av1_highbd_pixel_proj_error_c(const uint8_t *src8, int width,
301*77c1e3ccSAndroid Build Coastguard Worker int height, int src_stride,
302*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat8, int dat_stride,
303*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt0, int flt0_stride,
304*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt1, int flt1_stride, int xq[2],
305*77c1e3ccSAndroid Build Coastguard Worker const sgr_params_type *params) {
306*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
307*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
308*77c1e3ccSAndroid Build Coastguard Worker int i, j;
309*77c1e3ccSAndroid Build Coastguard Worker int64_t err = 0;
310*77c1e3ccSAndroid Build Coastguard Worker const int32_t half = 1 << (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS - 1);
311*77c1e3ccSAndroid Build Coastguard Worker if (params->r[0] > 0 && params->r[1] > 0) {
312*77c1e3ccSAndroid Build Coastguard Worker int xq0 = xq[0];
313*77c1e3ccSAndroid Build Coastguard Worker int xq1 = xq[1];
314*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < height; ++i) {
315*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < width; ++j) {
316*77c1e3ccSAndroid Build Coastguard Worker const int32_t d = dat[j];
317*77c1e3ccSAndroid Build Coastguard Worker const int32_t s = src[j];
318*77c1e3ccSAndroid Build Coastguard Worker const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
319*77c1e3ccSAndroid Build Coastguard Worker int32_t v0 = flt0[j] - u;
320*77c1e3ccSAndroid Build Coastguard Worker int32_t v1 = flt1[j] - u;
321*77c1e3ccSAndroid Build Coastguard Worker int32_t v = half;
322*77c1e3ccSAndroid Build Coastguard Worker v += xq0 * v0;
323*77c1e3ccSAndroid Build Coastguard Worker v += xq1 * v1;
324*77c1e3ccSAndroid Build Coastguard Worker const int32_t e = (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
325*77c1e3ccSAndroid Build Coastguard Worker err += ((int64_t)e * e);
326*77c1e3ccSAndroid Build Coastguard Worker }
327*77c1e3ccSAndroid Build Coastguard Worker dat += dat_stride;
328*77c1e3ccSAndroid Build Coastguard Worker flt0 += flt0_stride;
329*77c1e3ccSAndroid Build Coastguard Worker flt1 += flt1_stride;
330*77c1e3ccSAndroid Build Coastguard Worker src += src_stride;
331*77c1e3ccSAndroid Build Coastguard Worker }
332*77c1e3ccSAndroid Build Coastguard Worker } else if (params->r[0] > 0 || params->r[1] > 0) {
333*77c1e3ccSAndroid Build Coastguard Worker int exq;
334*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt;
335*77c1e3ccSAndroid Build Coastguard Worker int flt_stride;
336*77c1e3ccSAndroid Build Coastguard Worker if (params->r[0] > 0) {
337*77c1e3ccSAndroid Build Coastguard Worker exq = xq[0];
338*77c1e3ccSAndroid Build Coastguard Worker flt = flt0;
339*77c1e3ccSAndroid Build Coastguard Worker flt_stride = flt0_stride;
340*77c1e3ccSAndroid Build Coastguard Worker } else {
341*77c1e3ccSAndroid Build Coastguard Worker exq = xq[1];
342*77c1e3ccSAndroid Build Coastguard Worker flt = flt1;
343*77c1e3ccSAndroid Build Coastguard Worker flt_stride = flt1_stride;
344*77c1e3ccSAndroid Build Coastguard Worker }
345*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < height; ++i) {
346*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < width; ++j) {
347*77c1e3ccSAndroid Build Coastguard Worker const int32_t d = dat[j];
348*77c1e3ccSAndroid Build Coastguard Worker const int32_t s = src[j];
349*77c1e3ccSAndroid Build Coastguard Worker const int32_t u = (int32_t)(d << SGRPROJ_RST_BITS);
350*77c1e3ccSAndroid Build Coastguard Worker int32_t v = half;
351*77c1e3ccSAndroid Build Coastguard Worker v += exq * (flt[j] - u);
352*77c1e3ccSAndroid Build Coastguard Worker const int32_t e = (v >> (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS)) + d - s;
353*77c1e3ccSAndroid Build Coastguard Worker err += ((int64_t)e * e);
354*77c1e3ccSAndroid Build Coastguard Worker }
355*77c1e3ccSAndroid Build Coastguard Worker dat += dat_stride;
356*77c1e3ccSAndroid Build Coastguard Worker flt += flt_stride;
357*77c1e3ccSAndroid Build Coastguard Worker src += src_stride;
358*77c1e3ccSAndroid Build Coastguard Worker }
359*77c1e3ccSAndroid Build Coastguard Worker } else {
360*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < height; ++i) {
361*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < width; ++j) {
362*77c1e3ccSAndroid Build Coastguard Worker const int32_t d = dat[j];
363*77c1e3ccSAndroid Build Coastguard Worker const int32_t s = src[j];
364*77c1e3ccSAndroid Build Coastguard Worker const int32_t e = d - s;
365*77c1e3ccSAndroid Build Coastguard Worker err += ((int64_t)e * e);
366*77c1e3ccSAndroid Build Coastguard Worker }
367*77c1e3ccSAndroid Build Coastguard Worker dat += dat_stride;
368*77c1e3ccSAndroid Build Coastguard Worker src += src_stride;
369*77c1e3ccSAndroid Build Coastguard Worker }
370*77c1e3ccSAndroid Build Coastguard Worker }
371*77c1e3ccSAndroid Build Coastguard Worker return err;
372*77c1e3ccSAndroid Build Coastguard Worker }
373*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_AV1_HIGHBITDEPTH
374*77c1e3ccSAndroid Build Coastguard Worker
get_pixel_proj_error(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int use_highbitdepth,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int * xqd,const sgr_params_type * params)375*77c1e3ccSAndroid Build Coastguard Worker static int64_t get_pixel_proj_error(const uint8_t *src8, int width, int height,
376*77c1e3ccSAndroid Build Coastguard Worker int src_stride, const uint8_t *dat8,
377*77c1e3ccSAndroid Build Coastguard Worker int dat_stride, int use_highbitdepth,
378*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt0, int flt0_stride,
379*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt1, int flt1_stride, int *xqd,
380*77c1e3ccSAndroid Build Coastguard Worker const sgr_params_type *params) {
381*77c1e3ccSAndroid Build Coastguard Worker int xq[2];
382*77c1e3ccSAndroid Build Coastguard Worker av1_decode_xq(xqd, xq, params);
383*77c1e3ccSAndroid Build Coastguard Worker
384*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
385*77c1e3ccSAndroid Build Coastguard Worker if (use_highbitdepth) {
386*77c1e3ccSAndroid Build Coastguard Worker return av1_highbd_pixel_proj_error(src8, width, height, src_stride, dat8,
387*77c1e3ccSAndroid Build Coastguard Worker dat_stride, flt0, flt0_stride, flt1,
388*77c1e3ccSAndroid Build Coastguard Worker flt1_stride, xq, params);
389*77c1e3ccSAndroid Build Coastguard Worker
390*77c1e3ccSAndroid Build Coastguard Worker } else {
391*77c1e3ccSAndroid Build Coastguard Worker return av1_lowbd_pixel_proj_error(src8, width, height, src_stride, dat8,
392*77c1e3ccSAndroid Build Coastguard Worker dat_stride, flt0, flt0_stride, flt1,
393*77c1e3ccSAndroid Build Coastguard Worker flt1_stride, xq, params);
394*77c1e3ccSAndroid Build Coastguard Worker }
395*77c1e3ccSAndroid Build Coastguard Worker #else
396*77c1e3ccSAndroid Build Coastguard Worker (void)use_highbitdepth;
397*77c1e3ccSAndroid Build Coastguard Worker return av1_lowbd_pixel_proj_error(src8, width, height, src_stride, dat8,
398*77c1e3ccSAndroid Build Coastguard Worker dat_stride, flt0, flt0_stride, flt1,
399*77c1e3ccSAndroid Build Coastguard Worker flt1_stride, xq, params);
400*77c1e3ccSAndroid Build Coastguard Worker #endif
401*77c1e3ccSAndroid Build Coastguard Worker }
402*77c1e3ccSAndroid Build Coastguard Worker
403*77c1e3ccSAndroid Build Coastguard Worker #define USE_SGRPROJ_REFINEMENT_SEARCH 1
finer_search_pixel_proj_error(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int use_highbitdepth,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int start_step,int * xqd,const sgr_params_type * params)404*77c1e3ccSAndroid Build Coastguard Worker static int64_t finer_search_pixel_proj_error(
405*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src8, int width, int height, int src_stride,
406*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat8, int dat_stride, int use_highbitdepth, int32_t *flt0,
407*77c1e3ccSAndroid Build Coastguard Worker int flt0_stride, int32_t *flt1, int flt1_stride, int start_step, int *xqd,
408*77c1e3ccSAndroid Build Coastguard Worker const sgr_params_type *params) {
409*77c1e3ccSAndroid Build Coastguard Worker int64_t err = get_pixel_proj_error(
410*77c1e3ccSAndroid Build Coastguard Worker src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth, flt0,
411*77c1e3ccSAndroid Build Coastguard Worker flt0_stride, flt1, flt1_stride, xqd, params);
412*77c1e3ccSAndroid Build Coastguard Worker (void)start_step;
413*77c1e3ccSAndroid Build Coastguard Worker #if USE_SGRPROJ_REFINEMENT_SEARCH
414*77c1e3ccSAndroid Build Coastguard Worker int64_t err2;
415*77c1e3ccSAndroid Build Coastguard Worker int tap_min[] = { SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MIN1 };
416*77c1e3ccSAndroid Build Coastguard Worker int tap_max[] = { SGRPROJ_PRJ_MAX0, SGRPROJ_PRJ_MAX1 };
417*77c1e3ccSAndroid Build Coastguard Worker for (int s = start_step; s >= 1; s >>= 1) {
418*77c1e3ccSAndroid Build Coastguard Worker for (int p = 0; p < 2; ++p) {
419*77c1e3ccSAndroid Build Coastguard Worker if ((params->r[0] == 0 && p == 0) || (params->r[1] == 0 && p == 1)) {
420*77c1e3ccSAndroid Build Coastguard Worker continue;
421*77c1e3ccSAndroid Build Coastguard Worker }
422*77c1e3ccSAndroid Build Coastguard Worker int skip = 0;
423*77c1e3ccSAndroid Build Coastguard Worker do {
424*77c1e3ccSAndroid Build Coastguard Worker if (xqd[p] - s >= tap_min[p]) {
425*77c1e3ccSAndroid Build Coastguard Worker xqd[p] -= s;
426*77c1e3ccSAndroid Build Coastguard Worker err2 =
427*77c1e3ccSAndroid Build Coastguard Worker get_pixel_proj_error(src8, width, height, src_stride, dat8,
428*77c1e3ccSAndroid Build Coastguard Worker dat_stride, use_highbitdepth, flt0,
429*77c1e3ccSAndroid Build Coastguard Worker flt0_stride, flt1, flt1_stride, xqd, params);
430*77c1e3ccSAndroid Build Coastguard Worker if (err2 > err) {
431*77c1e3ccSAndroid Build Coastguard Worker xqd[p] += s;
432*77c1e3ccSAndroid Build Coastguard Worker } else {
433*77c1e3ccSAndroid Build Coastguard Worker err = err2;
434*77c1e3ccSAndroid Build Coastguard Worker skip = 1;
435*77c1e3ccSAndroid Build Coastguard Worker // At the highest step size continue moving in the same direction
436*77c1e3ccSAndroid Build Coastguard Worker if (s == start_step) continue;
437*77c1e3ccSAndroid Build Coastguard Worker }
438*77c1e3ccSAndroid Build Coastguard Worker }
439*77c1e3ccSAndroid Build Coastguard Worker break;
440*77c1e3ccSAndroid Build Coastguard Worker } while (1);
441*77c1e3ccSAndroid Build Coastguard Worker if (skip) break;
442*77c1e3ccSAndroid Build Coastguard Worker do {
443*77c1e3ccSAndroid Build Coastguard Worker if (xqd[p] + s <= tap_max[p]) {
444*77c1e3ccSAndroid Build Coastguard Worker xqd[p] += s;
445*77c1e3ccSAndroid Build Coastguard Worker err2 =
446*77c1e3ccSAndroid Build Coastguard Worker get_pixel_proj_error(src8, width, height, src_stride, dat8,
447*77c1e3ccSAndroid Build Coastguard Worker dat_stride, use_highbitdepth, flt0,
448*77c1e3ccSAndroid Build Coastguard Worker flt0_stride, flt1, flt1_stride, xqd, params);
449*77c1e3ccSAndroid Build Coastguard Worker if (err2 > err) {
450*77c1e3ccSAndroid Build Coastguard Worker xqd[p] -= s;
451*77c1e3ccSAndroid Build Coastguard Worker } else {
452*77c1e3ccSAndroid Build Coastguard Worker err = err2;
453*77c1e3ccSAndroid Build Coastguard Worker // At the highest step size continue moving in the same direction
454*77c1e3ccSAndroid Build Coastguard Worker if (s == start_step) continue;
455*77c1e3ccSAndroid Build Coastguard Worker }
456*77c1e3ccSAndroid Build Coastguard Worker }
457*77c1e3ccSAndroid Build Coastguard Worker break;
458*77c1e3ccSAndroid Build Coastguard Worker } while (1);
459*77c1e3ccSAndroid Build Coastguard Worker }
460*77c1e3ccSAndroid Build Coastguard Worker }
461*77c1e3ccSAndroid Build Coastguard Worker #endif // USE_SGRPROJ_REFINEMENT_SEARCH
462*77c1e3ccSAndroid Build Coastguard Worker return err;
463*77c1e3ccSAndroid Build Coastguard Worker }
464*77c1e3ccSAndroid Build Coastguard Worker
signed_rounded_divide(int64_t dividend,int64_t divisor)465*77c1e3ccSAndroid Build Coastguard Worker static int64_t signed_rounded_divide(int64_t dividend, int64_t divisor) {
466*77c1e3ccSAndroid Build Coastguard Worker if (dividend < 0)
467*77c1e3ccSAndroid Build Coastguard Worker return (dividend - divisor / 2) / divisor;
468*77c1e3ccSAndroid Build Coastguard Worker else
469*77c1e3ccSAndroid Build Coastguard Worker return (dividend + divisor / 2) / divisor;
470*77c1e3ccSAndroid Build Coastguard Worker }
471*77c1e3ccSAndroid Build Coastguard Worker
calc_proj_params_r0_r1_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])472*77c1e3ccSAndroid Build Coastguard Worker static inline void calc_proj_params_r0_r1_c(const uint8_t *src8, int width,
473*77c1e3ccSAndroid Build Coastguard Worker int height, int src_stride,
474*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat8, int dat_stride,
475*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt0, int flt0_stride,
476*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt1, int flt1_stride,
477*77c1e3ccSAndroid Build Coastguard Worker int64_t H[2][2], int64_t C[2]) {
478*77c1e3ccSAndroid Build Coastguard Worker const int size = width * height;
479*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src = src8;
480*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat = dat8;
481*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < height; ++i) {
482*77c1e3ccSAndroid Build Coastguard Worker for (int j = 0; j < width; ++j) {
483*77c1e3ccSAndroid Build Coastguard Worker const int32_t u = (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
484*77c1e3ccSAndroid Build Coastguard Worker const int32_t s =
485*77c1e3ccSAndroid Build Coastguard Worker (int32_t)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
486*77c1e3ccSAndroid Build Coastguard Worker const int32_t f1 = (int32_t)flt0[i * flt0_stride + j] - u;
487*77c1e3ccSAndroid Build Coastguard Worker const int32_t f2 = (int32_t)flt1[i * flt1_stride + j] - u;
488*77c1e3ccSAndroid Build Coastguard Worker H[0][0] += (int64_t)f1 * f1;
489*77c1e3ccSAndroid Build Coastguard Worker H[1][1] += (int64_t)f2 * f2;
490*77c1e3ccSAndroid Build Coastguard Worker H[0][1] += (int64_t)f1 * f2;
491*77c1e3ccSAndroid Build Coastguard Worker C[0] += (int64_t)f1 * s;
492*77c1e3ccSAndroid Build Coastguard Worker C[1] += (int64_t)f2 * s;
493*77c1e3ccSAndroid Build Coastguard Worker }
494*77c1e3ccSAndroid Build Coastguard Worker }
495*77c1e3ccSAndroid Build Coastguard Worker H[0][0] /= size;
496*77c1e3ccSAndroid Build Coastguard Worker H[0][1] /= size;
497*77c1e3ccSAndroid Build Coastguard Worker H[1][1] /= size;
498*77c1e3ccSAndroid Build Coastguard Worker H[1][0] = H[0][1];
499*77c1e3ccSAndroid Build Coastguard Worker C[0] /= size;
500*77c1e3ccSAndroid Build Coastguard Worker C[1] /= size;
501*77c1e3ccSAndroid Build Coastguard Worker }
502*77c1e3ccSAndroid Build Coastguard Worker
503*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
calc_proj_params_r0_r1_high_bd_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])504*77c1e3ccSAndroid Build Coastguard Worker static inline void calc_proj_params_r0_r1_high_bd_c(
505*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src8, int width, int height, int src_stride,
506*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
507*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt1, int flt1_stride, int64_t H[2][2], int64_t C[2]) {
508*77c1e3ccSAndroid Build Coastguard Worker const int size = width * height;
509*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
510*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
511*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < height; ++i) {
512*77c1e3ccSAndroid Build Coastguard Worker for (int j = 0; j < width; ++j) {
513*77c1e3ccSAndroid Build Coastguard Worker const int32_t u = (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
514*77c1e3ccSAndroid Build Coastguard Worker const int32_t s =
515*77c1e3ccSAndroid Build Coastguard Worker (int32_t)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
516*77c1e3ccSAndroid Build Coastguard Worker const int32_t f1 = (int32_t)flt0[i * flt0_stride + j] - u;
517*77c1e3ccSAndroid Build Coastguard Worker const int32_t f2 = (int32_t)flt1[i * flt1_stride + j] - u;
518*77c1e3ccSAndroid Build Coastguard Worker H[0][0] += (int64_t)f1 * f1;
519*77c1e3ccSAndroid Build Coastguard Worker H[1][1] += (int64_t)f2 * f2;
520*77c1e3ccSAndroid Build Coastguard Worker H[0][1] += (int64_t)f1 * f2;
521*77c1e3ccSAndroid Build Coastguard Worker C[0] += (int64_t)f1 * s;
522*77c1e3ccSAndroid Build Coastguard Worker C[1] += (int64_t)f2 * s;
523*77c1e3ccSAndroid Build Coastguard Worker }
524*77c1e3ccSAndroid Build Coastguard Worker }
525*77c1e3ccSAndroid Build Coastguard Worker H[0][0] /= size;
526*77c1e3ccSAndroid Build Coastguard Worker H[0][1] /= size;
527*77c1e3ccSAndroid Build Coastguard Worker H[1][1] /= size;
528*77c1e3ccSAndroid Build Coastguard Worker H[1][0] = H[0][1];
529*77c1e3ccSAndroid Build Coastguard Worker C[0] /= size;
530*77c1e3ccSAndroid Build Coastguard Worker C[1] /= size;
531*77c1e3ccSAndroid Build Coastguard Worker }
532*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_AV1_HIGHBITDEPTH
533*77c1e3ccSAndroid Build Coastguard Worker
calc_proj_params_r0_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int64_t H[2][2],int64_t C[2])534*77c1e3ccSAndroid Build Coastguard Worker static inline void calc_proj_params_r0_c(const uint8_t *src8, int width,
535*77c1e3ccSAndroid Build Coastguard Worker int height, int src_stride,
536*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat8, int dat_stride,
537*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt0, int flt0_stride,
538*77c1e3ccSAndroid Build Coastguard Worker int64_t H[2][2], int64_t C[2]) {
539*77c1e3ccSAndroid Build Coastguard Worker const int size = width * height;
540*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src = src8;
541*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat = dat8;
542*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < height; ++i) {
543*77c1e3ccSAndroid Build Coastguard Worker for (int j = 0; j < width; ++j) {
544*77c1e3ccSAndroid Build Coastguard Worker const int32_t u = (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
545*77c1e3ccSAndroid Build Coastguard Worker const int32_t s =
546*77c1e3ccSAndroid Build Coastguard Worker (int32_t)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
547*77c1e3ccSAndroid Build Coastguard Worker const int32_t f1 = (int32_t)flt0[i * flt0_stride + j] - u;
548*77c1e3ccSAndroid Build Coastguard Worker H[0][0] += (int64_t)f1 * f1;
549*77c1e3ccSAndroid Build Coastguard Worker C[0] += (int64_t)f1 * s;
550*77c1e3ccSAndroid Build Coastguard Worker }
551*77c1e3ccSAndroid Build Coastguard Worker }
552*77c1e3ccSAndroid Build Coastguard Worker H[0][0] /= size;
553*77c1e3ccSAndroid Build Coastguard Worker C[0] /= size;
554*77c1e3ccSAndroid Build Coastguard Worker }
555*77c1e3ccSAndroid Build Coastguard Worker
556*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
calc_proj_params_r0_high_bd_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int64_t H[2][2],int64_t C[2])557*77c1e3ccSAndroid Build Coastguard Worker static inline void calc_proj_params_r0_high_bd_c(
558*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src8, int width, int height, int src_stride,
559*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
560*77c1e3ccSAndroid Build Coastguard Worker int64_t H[2][2], int64_t C[2]) {
561*77c1e3ccSAndroid Build Coastguard Worker const int size = width * height;
562*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
563*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
564*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < height; ++i) {
565*77c1e3ccSAndroid Build Coastguard Worker for (int j = 0; j < width; ++j) {
566*77c1e3ccSAndroid Build Coastguard Worker const int32_t u = (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
567*77c1e3ccSAndroid Build Coastguard Worker const int32_t s =
568*77c1e3ccSAndroid Build Coastguard Worker (int32_t)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
569*77c1e3ccSAndroid Build Coastguard Worker const int32_t f1 = (int32_t)flt0[i * flt0_stride + j] - u;
570*77c1e3ccSAndroid Build Coastguard Worker H[0][0] += (int64_t)f1 * f1;
571*77c1e3ccSAndroid Build Coastguard Worker C[0] += (int64_t)f1 * s;
572*77c1e3ccSAndroid Build Coastguard Worker }
573*77c1e3ccSAndroid Build Coastguard Worker }
574*77c1e3ccSAndroid Build Coastguard Worker H[0][0] /= size;
575*77c1e3ccSAndroid Build Coastguard Worker C[0] /= size;
576*77c1e3ccSAndroid Build Coastguard Worker }
577*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_AV1_HIGHBITDEPTH
578*77c1e3ccSAndroid Build Coastguard Worker
calc_proj_params_r1_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])579*77c1e3ccSAndroid Build Coastguard Worker static inline void calc_proj_params_r1_c(const uint8_t *src8, int width,
580*77c1e3ccSAndroid Build Coastguard Worker int height, int src_stride,
581*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat8, int dat_stride,
582*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt1, int flt1_stride,
583*77c1e3ccSAndroid Build Coastguard Worker int64_t H[2][2], int64_t C[2]) {
584*77c1e3ccSAndroid Build Coastguard Worker const int size = width * height;
585*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src = src8;
586*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat = dat8;
587*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < height; ++i) {
588*77c1e3ccSAndroid Build Coastguard Worker for (int j = 0; j < width; ++j) {
589*77c1e3ccSAndroid Build Coastguard Worker const int32_t u = (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
590*77c1e3ccSAndroid Build Coastguard Worker const int32_t s =
591*77c1e3ccSAndroid Build Coastguard Worker (int32_t)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
592*77c1e3ccSAndroid Build Coastguard Worker const int32_t f2 = (int32_t)flt1[i * flt1_stride + j] - u;
593*77c1e3ccSAndroid Build Coastguard Worker H[1][1] += (int64_t)f2 * f2;
594*77c1e3ccSAndroid Build Coastguard Worker C[1] += (int64_t)f2 * s;
595*77c1e3ccSAndroid Build Coastguard Worker }
596*77c1e3ccSAndroid Build Coastguard Worker }
597*77c1e3ccSAndroid Build Coastguard Worker H[1][1] /= size;
598*77c1e3ccSAndroid Build Coastguard Worker C[1] /= size;
599*77c1e3ccSAndroid Build Coastguard Worker }
600*77c1e3ccSAndroid Build Coastguard Worker
601*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
calc_proj_params_r1_high_bd_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])602*77c1e3ccSAndroid Build Coastguard Worker static inline void calc_proj_params_r1_high_bd_c(
603*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src8, int width, int height, int src_stride,
604*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat8, int dat_stride, int32_t *flt1, int flt1_stride,
605*77c1e3ccSAndroid Build Coastguard Worker int64_t H[2][2], int64_t C[2]) {
606*77c1e3ccSAndroid Build Coastguard Worker const int size = width * height;
607*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
608*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
609*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < height; ++i) {
610*77c1e3ccSAndroid Build Coastguard Worker for (int j = 0; j < width; ++j) {
611*77c1e3ccSAndroid Build Coastguard Worker const int32_t u = (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS);
612*77c1e3ccSAndroid Build Coastguard Worker const int32_t s =
613*77c1e3ccSAndroid Build Coastguard Worker (int32_t)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u;
614*77c1e3ccSAndroid Build Coastguard Worker const int32_t f2 = (int32_t)flt1[i * flt1_stride + j] - u;
615*77c1e3ccSAndroid Build Coastguard Worker H[1][1] += (int64_t)f2 * f2;
616*77c1e3ccSAndroid Build Coastguard Worker C[1] += (int64_t)f2 * s;
617*77c1e3ccSAndroid Build Coastguard Worker }
618*77c1e3ccSAndroid Build Coastguard Worker }
619*77c1e3ccSAndroid Build Coastguard Worker H[1][1] /= size;
620*77c1e3ccSAndroid Build Coastguard Worker C[1] /= size;
621*77c1e3ccSAndroid Build Coastguard Worker }
622*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_AV1_HIGHBITDEPTH
623*77c1e3ccSAndroid Build Coastguard Worker
624*77c1e3ccSAndroid Build Coastguard Worker // The function calls 3 subfunctions for the following cases :
625*77c1e3ccSAndroid Build Coastguard Worker // 1) When params->r[0] > 0 and params->r[1] > 0. In this case all elements
626*77c1e3ccSAndroid Build Coastguard Worker // of C and H need to be computed.
627*77c1e3ccSAndroid Build Coastguard Worker // 2) When only params->r[0] > 0. In this case only H[0][0] and C[0] are
628*77c1e3ccSAndroid Build Coastguard Worker // non-zero and need to be computed.
629*77c1e3ccSAndroid Build Coastguard Worker // 3) When only params->r[1] > 0. In this case only H[1][1] and C[1] are
630*77c1e3ccSAndroid Build Coastguard Worker // non-zero and need to be computed.
av1_calc_proj_params_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2],const sgr_params_type * params)631*77c1e3ccSAndroid Build Coastguard Worker void av1_calc_proj_params_c(const uint8_t *src8, int width, int height,
632*77c1e3ccSAndroid Build Coastguard Worker int src_stride, const uint8_t *dat8, int dat_stride,
633*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt0, int flt0_stride, int32_t *flt1,
634*77c1e3ccSAndroid Build Coastguard Worker int flt1_stride, int64_t H[2][2], int64_t C[2],
635*77c1e3ccSAndroid Build Coastguard Worker const sgr_params_type *params) {
636*77c1e3ccSAndroid Build Coastguard Worker if ((params->r[0] > 0) && (params->r[1] > 0)) {
637*77c1e3ccSAndroid Build Coastguard Worker calc_proj_params_r0_r1_c(src8, width, height, src_stride, dat8, dat_stride,
638*77c1e3ccSAndroid Build Coastguard Worker flt0, flt0_stride, flt1, flt1_stride, H, C);
639*77c1e3ccSAndroid Build Coastguard Worker } else if (params->r[0] > 0) {
640*77c1e3ccSAndroid Build Coastguard Worker calc_proj_params_r0_c(src8, width, height, src_stride, dat8, dat_stride,
641*77c1e3ccSAndroid Build Coastguard Worker flt0, flt0_stride, H, C);
642*77c1e3ccSAndroid Build Coastguard Worker } else if (params->r[1] > 0) {
643*77c1e3ccSAndroid Build Coastguard Worker calc_proj_params_r1_c(src8, width, height, src_stride, dat8, dat_stride,
644*77c1e3ccSAndroid Build Coastguard Worker flt1, flt1_stride, H, C);
645*77c1e3ccSAndroid Build Coastguard Worker }
646*77c1e3ccSAndroid Build Coastguard Worker }
647*77c1e3ccSAndroid Build Coastguard Worker
648*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
av1_calc_proj_params_high_bd_c(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2],const sgr_params_type * params)649*77c1e3ccSAndroid Build Coastguard Worker void av1_calc_proj_params_high_bd_c(const uint8_t *src8, int width, int height,
650*77c1e3ccSAndroid Build Coastguard Worker int src_stride, const uint8_t *dat8,
651*77c1e3ccSAndroid Build Coastguard Worker int dat_stride, int32_t *flt0,
652*77c1e3ccSAndroid Build Coastguard Worker int flt0_stride, int32_t *flt1,
653*77c1e3ccSAndroid Build Coastguard Worker int flt1_stride, int64_t H[2][2],
654*77c1e3ccSAndroid Build Coastguard Worker int64_t C[2],
655*77c1e3ccSAndroid Build Coastguard Worker const sgr_params_type *params) {
656*77c1e3ccSAndroid Build Coastguard Worker if ((params->r[0] > 0) && (params->r[1] > 0)) {
657*77c1e3ccSAndroid Build Coastguard Worker calc_proj_params_r0_r1_high_bd_c(src8, width, height, src_stride, dat8,
658*77c1e3ccSAndroid Build Coastguard Worker dat_stride, flt0, flt0_stride, flt1,
659*77c1e3ccSAndroid Build Coastguard Worker flt1_stride, H, C);
660*77c1e3ccSAndroid Build Coastguard Worker } else if (params->r[0] > 0) {
661*77c1e3ccSAndroid Build Coastguard Worker calc_proj_params_r0_high_bd_c(src8, width, height, src_stride, dat8,
662*77c1e3ccSAndroid Build Coastguard Worker dat_stride, flt0, flt0_stride, H, C);
663*77c1e3ccSAndroid Build Coastguard Worker } else if (params->r[1] > 0) {
664*77c1e3ccSAndroid Build Coastguard Worker calc_proj_params_r1_high_bd_c(src8, width, height, src_stride, dat8,
665*77c1e3ccSAndroid Build Coastguard Worker dat_stride, flt1, flt1_stride, H, C);
666*77c1e3ccSAndroid Build Coastguard Worker }
667*77c1e3ccSAndroid Build Coastguard Worker }
668*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_AV1_HIGHBITDEPTH
669*77c1e3ccSAndroid Build Coastguard Worker
get_proj_subspace(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int use_highbitdepth,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int * xq,const sgr_params_type * params)670*77c1e3ccSAndroid Build Coastguard Worker static inline void get_proj_subspace(const uint8_t *src8, int width, int height,
671*77c1e3ccSAndroid Build Coastguard Worker int src_stride, const uint8_t *dat8,
672*77c1e3ccSAndroid Build Coastguard Worker int dat_stride, int use_highbitdepth,
673*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt0, int flt0_stride,
674*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt1, int flt1_stride, int *xq,
675*77c1e3ccSAndroid Build Coastguard Worker const sgr_params_type *params) {
676*77c1e3ccSAndroid Build Coastguard Worker int64_t H[2][2] = { { 0, 0 }, { 0, 0 } };
677*77c1e3ccSAndroid Build Coastguard Worker int64_t C[2] = { 0, 0 };
678*77c1e3ccSAndroid Build Coastguard Worker
679*77c1e3ccSAndroid Build Coastguard Worker // Default values to be returned if the problem becomes ill-posed
680*77c1e3ccSAndroid Build Coastguard Worker xq[0] = 0;
681*77c1e3ccSAndroid Build Coastguard Worker xq[1] = 0;
682*77c1e3ccSAndroid Build Coastguard Worker
683*77c1e3ccSAndroid Build Coastguard Worker if (!use_highbitdepth) {
684*77c1e3ccSAndroid Build Coastguard Worker if ((width & 0x7) == 0) {
685*77c1e3ccSAndroid Build Coastguard Worker av1_calc_proj_params(src8, width, height, src_stride, dat8, dat_stride,
686*77c1e3ccSAndroid Build Coastguard Worker flt0, flt0_stride, flt1, flt1_stride, H, C, params);
687*77c1e3ccSAndroid Build Coastguard Worker } else {
688*77c1e3ccSAndroid Build Coastguard Worker av1_calc_proj_params_c(src8, width, height, src_stride, dat8, dat_stride,
689*77c1e3ccSAndroid Build Coastguard Worker flt0, flt0_stride, flt1, flt1_stride, H, C,
690*77c1e3ccSAndroid Build Coastguard Worker params);
691*77c1e3ccSAndroid Build Coastguard Worker }
692*77c1e3ccSAndroid Build Coastguard Worker }
693*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
694*77c1e3ccSAndroid Build Coastguard Worker else { // NOLINT
695*77c1e3ccSAndroid Build Coastguard Worker if ((width & 0x7) == 0) {
696*77c1e3ccSAndroid Build Coastguard Worker av1_calc_proj_params_high_bd(src8, width, height, src_stride, dat8,
697*77c1e3ccSAndroid Build Coastguard Worker dat_stride, flt0, flt0_stride, flt1,
698*77c1e3ccSAndroid Build Coastguard Worker flt1_stride, H, C, params);
699*77c1e3ccSAndroid Build Coastguard Worker } else {
700*77c1e3ccSAndroid Build Coastguard Worker av1_calc_proj_params_high_bd_c(src8, width, height, src_stride, dat8,
701*77c1e3ccSAndroid Build Coastguard Worker dat_stride, flt0, flt0_stride, flt1,
702*77c1e3ccSAndroid Build Coastguard Worker flt1_stride, H, C, params);
703*77c1e3ccSAndroid Build Coastguard Worker }
704*77c1e3ccSAndroid Build Coastguard Worker }
705*77c1e3ccSAndroid Build Coastguard Worker #endif
706*77c1e3ccSAndroid Build Coastguard Worker
707*77c1e3ccSAndroid Build Coastguard Worker if (params->r[0] == 0) {
708*77c1e3ccSAndroid Build Coastguard Worker // H matrix is now only the scalar H[1][1]
709*77c1e3ccSAndroid Build Coastguard Worker // C vector is now only the scalar C[1]
710*77c1e3ccSAndroid Build Coastguard Worker const int64_t Det = H[1][1];
711*77c1e3ccSAndroid Build Coastguard Worker if (Det == 0) return; // ill-posed, return default values
712*77c1e3ccSAndroid Build Coastguard Worker xq[0] = 0;
713*77c1e3ccSAndroid Build Coastguard Worker xq[1] = (int)signed_rounded_divide(C[1] * (1 << SGRPROJ_PRJ_BITS), Det);
714*77c1e3ccSAndroid Build Coastguard Worker } else if (params->r[1] == 0) {
715*77c1e3ccSAndroid Build Coastguard Worker // H matrix is now only the scalar H[0][0]
716*77c1e3ccSAndroid Build Coastguard Worker // C vector is now only the scalar C[0]
717*77c1e3ccSAndroid Build Coastguard Worker const int64_t Det = H[0][0];
718*77c1e3ccSAndroid Build Coastguard Worker if (Det == 0) return; // ill-posed, return default values
719*77c1e3ccSAndroid Build Coastguard Worker xq[0] = (int)signed_rounded_divide(C[0] * (1 << SGRPROJ_PRJ_BITS), Det);
720*77c1e3ccSAndroid Build Coastguard Worker xq[1] = 0;
721*77c1e3ccSAndroid Build Coastguard Worker } else {
722*77c1e3ccSAndroid Build Coastguard Worker const int64_t Det = H[0][0] * H[1][1] - H[0][1] * H[1][0];
723*77c1e3ccSAndroid Build Coastguard Worker if (Det == 0) return; // ill-posed, return default values
724*77c1e3ccSAndroid Build Coastguard Worker
725*77c1e3ccSAndroid Build Coastguard Worker // If scaling up dividend would overflow, instead scale down the divisor
726*77c1e3ccSAndroid Build Coastguard Worker const int64_t div1 = H[1][1] * C[0] - H[0][1] * C[1];
727*77c1e3ccSAndroid Build Coastguard Worker if ((div1 > 0 && INT64_MAX / (1 << SGRPROJ_PRJ_BITS) < div1) ||
728*77c1e3ccSAndroid Build Coastguard Worker (div1 < 0 && INT64_MIN / (1 << SGRPROJ_PRJ_BITS) > div1))
729*77c1e3ccSAndroid Build Coastguard Worker xq[0] = (int)signed_rounded_divide(div1, Det / (1 << SGRPROJ_PRJ_BITS));
730*77c1e3ccSAndroid Build Coastguard Worker else
731*77c1e3ccSAndroid Build Coastguard Worker xq[0] = (int)signed_rounded_divide(div1 * (1 << SGRPROJ_PRJ_BITS), Det);
732*77c1e3ccSAndroid Build Coastguard Worker
733*77c1e3ccSAndroid Build Coastguard Worker const int64_t div2 = H[0][0] * C[1] - H[1][0] * C[0];
734*77c1e3ccSAndroid Build Coastguard Worker if ((div2 > 0 && INT64_MAX / (1 << SGRPROJ_PRJ_BITS) < div2) ||
735*77c1e3ccSAndroid Build Coastguard Worker (div2 < 0 && INT64_MIN / (1 << SGRPROJ_PRJ_BITS) > div2))
736*77c1e3ccSAndroid Build Coastguard Worker xq[1] = (int)signed_rounded_divide(div2, Det / (1 << SGRPROJ_PRJ_BITS));
737*77c1e3ccSAndroid Build Coastguard Worker else
738*77c1e3ccSAndroid Build Coastguard Worker xq[1] = (int)signed_rounded_divide(div2 * (1 << SGRPROJ_PRJ_BITS), Det);
739*77c1e3ccSAndroid Build Coastguard Worker }
740*77c1e3ccSAndroid Build Coastguard Worker }
741*77c1e3ccSAndroid Build Coastguard Worker
encode_xq(int * xq,int * xqd,const sgr_params_type * params)742*77c1e3ccSAndroid Build Coastguard Worker static inline void encode_xq(int *xq, int *xqd, const sgr_params_type *params) {
743*77c1e3ccSAndroid Build Coastguard Worker if (params->r[0] == 0) {
744*77c1e3ccSAndroid Build Coastguard Worker xqd[0] = 0;
745*77c1e3ccSAndroid Build Coastguard Worker xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xq[1], SGRPROJ_PRJ_MIN1,
746*77c1e3ccSAndroid Build Coastguard Worker SGRPROJ_PRJ_MAX1);
747*77c1e3ccSAndroid Build Coastguard Worker } else if (params->r[1] == 0) {
748*77c1e3ccSAndroid Build Coastguard Worker xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
749*77c1e3ccSAndroid Build Coastguard Worker xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0], SGRPROJ_PRJ_MIN1,
750*77c1e3ccSAndroid Build Coastguard Worker SGRPROJ_PRJ_MAX1);
751*77c1e3ccSAndroid Build Coastguard Worker } else {
752*77c1e3ccSAndroid Build Coastguard Worker xqd[0] = clamp(xq[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0);
753*77c1e3ccSAndroid Build Coastguard Worker xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - xqd[0] - xq[1], SGRPROJ_PRJ_MIN1,
754*77c1e3ccSAndroid Build Coastguard Worker SGRPROJ_PRJ_MAX1);
755*77c1e3ccSAndroid Build Coastguard Worker }
756*77c1e3ccSAndroid Build Coastguard Worker }
757*77c1e3ccSAndroid Build Coastguard Worker
758*77c1e3ccSAndroid Build Coastguard Worker // Apply the self-guided filter across an entire restoration unit.
apply_sgr(int sgr_params_idx,const uint8_t * dat8,int width,int height,int dat_stride,int use_highbd,int bit_depth,int pu_width,int pu_height,int32_t * flt0,int32_t * flt1,int flt_stride,struct aom_internal_error_info * error_info)759*77c1e3ccSAndroid Build Coastguard Worker static inline void apply_sgr(int sgr_params_idx, const uint8_t *dat8, int width,
760*77c1e3ccSAndroid Build Coastguard Worker int height, int dat_stride, int use_highbd,
761*77c1e3ccSAndroid Build Coastguard Worker int bit_depth, int pu_width, int pu_height,
762*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt0, int32_t *flt1, int flt_stride,
763*77c1e3ccSAndroid Build Coastguard Worker struct aom_internal_error_info *error_info) {
764*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < height; i += pu_height) {
765*77c1e3ccSAndroid Build Coastguard Worker const int h = AOMMIN(pu_height, height - i);
766*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt0_row = flt0 + i * flt_stride;
767*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt1_row = flt1 + i * flt_stride;
768*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat8_row = dat8 + i * dat_stride;
769*77c1e3ccSAndroid Build Coastguard Worker
770*77c1e3ccSAndroid Build Coastguard Worker // Iterate over the stripe in blocks of width pu_width
771*77c1e3ccSAndroid Build Coastguard Worker for (int j = 0; j < width; j += pu_width) {
772*77c1e3ccSAndroid Build Coastguard Worker const int w = AOMMIN(pu_width, width - j);
773*77c1e3ccSAndroid Build Coastguard Worker if (av1_selfguided_restoration(
774*77c1e3ccSAndroid Build Coastguard Worker dat8_row + j, w, h, dat_stride, flt0_row + j, flt1_row + j,
775*77c1e3ccSAndroid Build Coastguard Worker flt_stride, sgr_params_idx, bit_depth, use_highbd) != 0) {
776*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(
777*77c1e3ccSAndroid Build Coastguard Worker error_info, AOM_CODEC_MEM_ERROR,
778*77c1e3ccSAndroid Build Coastguard Worker "Error allocating buffer in av1_selfguided_restoration");
779*77c1e3ccSAndroid Build Coastguard Worker }
780*77c1e3ccSAndroid Build Coastguard Worker }
781*77c1e3ccSAndroid Build Coastguard Worker }
782*77c1e3ccSAndroid Build Coastguard Worker }
783*77c1e3ccSAndroid Build Coastguard Worker
compute_sgrproj_err(const uint8_t * dat8,const int width,const int height,const int dat_stride,const uint8_t * src8,const int src_stride,const int use_highbitdepth,const int bit_depth,const int pu_width,const int pu_height,const int ep,int32_t * flt0,int32_t * flt1,const int flt_stride,int * exqd,int64_t * err,struct aom_internal_error_info * error_info)784*77c1e3ccSAndroid Build Coastguard Worker static inline void compute_sgrproj_err(
785*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat8, const int width, const int height,
786*77c1e3ccSAndroid Build Coastguard Worker const int dat_stride, const uint8_t *src8, const int src_stride,
787*77c1e3ccSAndroid Build Coastguard Worker const int use_highbitdepth, const int bit_depth, const int pu_width,
788*77c1e3ccSAndroid Build Coastguard Worker const int pu_height, const int ep, int32_t *flt0, int32_t *flt1,
789*77c1e3ccSAndroid Build Coastguard Worker const int flt_stride, int *exqd, int64_t *err,
790*77c1e3ccSAndroid Build Coastguard Worker struct aom_internal_error_info *error_info) {
791*77c1e3ccSAndroid Build Coastguard Worker int exq[2];
792*77c1e3ccSAndroid Build Coastguard Worker apply_sgr(ep, dat8, width, height, dat_stride, use_highbitdepth, bit_depth,
793*77c1e3ccSAndroid Build Coastguard Worker pu_width, pu_height, flt0, flt1, flt_stride, error_info);
794*77c1e3ccSAndroid Build Coastguard Worker const sgr_params_type *const params = &av1_sgr_params[ep];
795*77c1e3ccSAndroid Build Coastguard Worker get_proj_subspace(src8, width, height, src_stride, dat8, dat_stride,
796*77c1e3ccSAndroid Build Coastguard Worker use_highbitdepth, flt0, flt_stride, flt1, flt_stride, exq,
797*77c1e3ccSAndroid Build Coastguard Worker params);
798*77c1e3ccSAndroid Build Coastguard Worker encode_xq(exq, exqd, params);
799*77c1e3ccSAndroid Build Coastguard Worker *err = finer_search_pixel_proj_error(
800*77c1e3ccSAndroid Build Coastguard Worker src8, width, height, src_stride, dat8, dat_stride, use_highbitdepth, flt0,
801*77c1e3ccSAndroid Build Coastguard Worker flt_stride, flt1, flt_stride, 2, exqd, params);
802*77c1e3ccSAndroid Build Coastguard Worker }
803*77c1e3ccSAndroid Build Coastguard Worker
get_best_error(int64_t * besterr,const int64_t err,const int * exqd,int * bestxqd,int * bestep,const int ep)804*77c1e3ccSAndroid Build Coastguard Worker static inline void get_best_error(int64_t *besterr, const int64_t err,
805*77c1e3ccSAndroid Build Coastguard Worker const int *exqd, int *bestxqd, int *bestep,
806*77c1e3ccSAndroid Build Coastguard Worker const int ep) {
807*77c1e3ccSAndroid Build Coastguard Worker if (*besterr == -1 || err < *besterr) {
808*77c1e3ccSAndroid Build Coastguard Worker *bestep = ep;
809*77c1e3ccSAndroid Build Coastguard Worker *besterr = err;
810*77c1e3ccSAndroid Build Coastguard Worker bestxqd[0] = exqd[0];
811*77c1e3ccSAndroid Build Coastguard Worker bestxqd[1] = exqd[1];
812*77c1e3ccSAndroid Build Coastguard Worker }
813*77c1e3ccSAndroid Build Coastguard Worker }
814*77c1e3ccSAndroid Build Coastguard Worker
search_selfguided_restoration(const uint8_t * dat8,int width,int height,int dat_stride,const uint8_t * src8,int src_stride,int use_highbitdepth,int bit_depth,int pu_width,int pu_height,int32_t * rstbuf,int enable_sgr_ep_pruning,struct aom_internal_error_info * error_info)815*77c1e3ccSAndroid Build Coastguard Worker static SgrprojInfo search_selfguided_restoration(
816*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *dat8, int width, int height, int dat_stride,
817*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src8, int src_stride, int use_highbitdepth, int bit_depth,
818*77c1e3ccSAndroid Build Coastguard Worker int pu_width, int pu_height, int32_t *rstbuf, int enable_sgr_ep_pruning,
819*77c1e3ccSAndroid Build Coastguard Worker struct aom_internal_error_info *error_info) {
820*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt0 = rstbuf;
821*77c1e3ccSAndroid Build Coastguard Worker int32_t *flt1 = flt0 + RESTORATION_UNITPELS_MAX;
822*77c1e3ccSAndroid Build Coastguard Worker int ep, idx, bestep = 0;
823*77c1e3ccSAndroid Build Coastguard Worker int64_t besterr = -1;
824*77c1e3ccSAndroid Build Coastguard Worker int exqd[2], bestxqd[2] = { 0, 0 };
825*77c1e3ccSAndroid Build Coastguard Worker int flt_stride = ((width + 7) & ~7) + 8;
826*77c1e3ccSAndroid Build Coastguard Worker assert(pu_width == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
827*77c1e3ccSAndroid Build Coastguard Worker pu_width == RESTORATION_PROC_UNIT_SIZE);
828*77c1e3ccSAndroid Build Coastguard Worker assert(pu_height == (RESTORATION_PROC_UNIT_SIZE >> 1) ||
829*77c1e3ccSAndroid Build Coastguard Worker pu_height == RESTORATION_PROC_UNIT_SIZE);
830*77c1e3ccSAndroid Build Coastguard Worker if (!enable_sgr_ep_pruning) {
831*77c1e3ccSAndroid Build Coastguard Worker for (ep = 0; ep < SGRPROJ_PARAMS; ep++) {
832*77c1e3ccSAndroid Build Coastguard Worker int64_t err;
833*77c1e3ccSAndroid Build Coastguard Worker compute_sgrproj_err(dat8, width, height, dat_stride, src8, src_stride,
834*77c1e3ccSAndroid Build Coastguard Worker use_highbitdepth, bit_depth, pu_width, pu_height, ep,
835*77c1e3ccSAndroid Build Coastguard Worker flt0, flt1, flt_stride, exqd, &err, error_info);
836*77c1e3ccSAndroid Build Coastguard Worker get_best_error(&besterr, err, exqd, bestxqd, &bestep, ep);
837*77c1e3ccSAndroid Build Coastguard Worker }
838*77c1e3ccSAndroid Build Coastguard Worker } else {
839*77c1e3ccSAndroid Build Coastguard Worker // evaluate first four seed ep in first group
840*77c1e3ccSAndroid Build Coastguard Worker for (idx = 0; idx < SGRPROJ_EP_GRP1_SEARCH_COUNT; idx++) {
841*77c1e3ccSAndroid Build Coastguard Worker ep = sgproj_ep_grp1_seed[idx];
842*77c1e3ccSAndroid Build Coastguard Worker int64_t err;
843*77c1e3ccSAndroid Build Coastguard Worker compute_sgrproj_err(dat8, width, height, dat_stride, src8, src_stride,
844*77c1e3ccSAndroid Build Coastguard Worker use_highbitdepth, bit_depth, pu_width, pu_height, ep,
845*77c1e3ccSAndroid Build Coastguard Worker flt0, flt1, flt_stride, exqd, &err, error_info);
846*77c1e3ccSAndroid Build Coastguard Worker get_best_error(&besterr, err, exqd, bestxqd, &bestep, ep);
847*77c1e3ccSAndroid Build Coastguard Worker }
848*77c1e3ccSAndroid Build Coastguard Worker // evaluate left and right ep of winner in seed ep
849*77c1e3ccSAndroid Build Coastguard Worker int bestep_ref = bestep;
850*77c1e3ccSAndroid Build Coastguard Worker for (ep = bestep_ref - 1; ep < bestep_ref + 2; ep += 2) {
851*77c1e3ccSAndroid Build Coastguard Worker if (ep < SGRPROJ_EP_GRP1_START_IDX || ep > SGRPROJ_EP_GRP1_END_IDX)
852*77c1e3ccSAndroid Build Coastguard Worker continue;
853*77c1e3ccSAndroid Build Coastguard Worker int64_t err;
854*77c1e3ccSAndroid Build Coastguard Worker compute_sgrproj_err(dat8, width, height, dat_stride, src8, src_stride,
855*77c1e3ccSAndroid Build Coastguard Worker use_highbitdepth, bit_depth, pu_width, pu_height, ep,
856*77c1e3ccSAndroid Build Coastguard Worker flt0, flt1, flt_stride, exqd, &err, error_info);
857*77c1e3ccSAndroid Build Coastguard Worker get_best_error(&besterr, err, exqd, bestxqd, &bestep, ep);
858*77c1e3ccSAndroid Build Coastguard Worker }
859*77c1e3ccSAndroid Build Coastguard Worker // evaluate last two group
860*77c1e3ccSAndroid Build Coastguard Worker for (idx = 0; idx < SGRPROJ_EP_GRP2_3_SEARCH_COUNT; idx++) {
861*77c1e3ccSAndroid Build Coastguard Worker ep = sgproj_ep_grp2_3[idx][bestep];
862*77c1e3ccSAndroid Build Coastguard Worker int64_t err;
863*77c1e3ccSAndroid Build Coastguard Worker compute_sgrproj_err(dat8, width, height, dat_stride, src8, src_stride,
864*77c1e3ccSAndroid Build Coastguard Worker use_highbitdepth, bit_depth, pu_width, pu_height, ep,
865*77c1e3ccSAndroid Build Coastguard Worker flt0, flt1, flt_stride, exqd, &err, error_info);
866*77c1e3ccSAndroid Build Coastguard Worker get_best_error(&besterr, err, exqd, bestxqd, &bestep, ep);
867*77c1e3ccSAndroid Build Coastguard Worker }
868*77c1e3ccSAndroid Build Coastguard Worker }
869*77c1e3ccSAndroid Build Coastguard Worker
870*77c1e3ccSAndroid Build Coastguard Worker SgrprojInfo ret;
871*77c1e3ccSAndroid Build Coastguard Worker ret.ep = bestep;
872*77c1e3ccSAndroid Build Coastguard Worker ret.xqd[0] = bestxqd[0];
873*77c1e3ccSAndroid Build Coastguard Worker ret.xqd[1] = bestxqd[1];
874*77c1e3ccSAndroid Build Coastguard Worker return ret;
875*77c1e3ccSAndroid Build Coastguard Worker }
876*77c1e3ccSAndroid Build Coastguard Worker
count_sgrproj_bits(SgrprojInfo * sgrproj_info,SgrprojInfo * ref_sgrproj_info)877*77c1e3ccSAndroid Build Coastguard Worker static int count_sgrproj_bits(SgrprojInfo *sgrproj_info,
878*77c1e3ccSAndroid Build Coastguard Worker SgrprojInfo *ref_sgrproj_info) {
879*77c1e3ccSAndroid Build Coastguard Worker int bits = SGRPROJ_PARAMS_BITS;
880*77c1e3ccSAndroid Build Coastguard Worker const sgr_params_type *params = &av1_sgr_params[sgrproj_info->ep];
881*77c1e3ccSAndroid Build Coastguard Worker if (params->r[0] > 0)
882*77c1e3ccSAndroid Build Coastguard Worker bits += aom_count_primitive_refsubexpfin(
883*77c1e3ccSAndroid Build Coastguard Worker SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
884*77c1e3ccSAndroid Build Coastguard Worker ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0,
885*77c1e3ccSAndroid Build Coastguard Worker sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0);
886*77c1e3ccSAndroid Build Coastguard Worker if (params->r[1] > 0)
887*77c1e3ccSAndroid Build Coastguard Worker bits += aom_count_primitive_refsubexpfin(
888*77c1e3ccSAndroid Build Coastguard Worker SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
889*77c1e3ccSAndroid Build Coastguard Worker ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1,
890*77c1e3ccSAndroid Build Coastguard Worker sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1);
891*77c1e3ccSAndroid Build Coastguard Worker return bits;
892*77c1e3ccSAndroid Build Coastguard Worker }
893*77c1e3ccSAndroid Build Coastguard Worker
search_sgrproj(const RestorationTileLimits * limits,int rest_unit_idx,void * priv,int32_t * tmpbuf,RestorationLineBuffers * rlbs,struct aom_internal_error_info * error_info)894*77c1e3ccSAndroid Build Coastguard Worker static inline void search_sgrproj(const RestorationTileLimits *limits,
895*77c1e3ccSAndroid Build Coastguard Worker int rest_unit_idx, void *priv,
896*77c1e3ccSAndroid Build Coastguard Worker int32_t *tmpbuf, RestorationLineBuffers *rlbs,
897*77c1e3ccSAndroid Build Coastguard Worker struct aom_internal_error_info *error_info) {
898*77c1e3ccSAndroid Build Coastguard Worker (void)rlbs;
899*77c1e3ccSAndroid Build Coastguard Worker RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
900*77c1e3ccSAndroid Build Coastguard Worker RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
901*77c1e3ccSAndroid Build Coastguard Worker
902*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCK *const x = rsc->x;
903*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = rsc->cm;
904*77c1e3ccSAndroid Build Coastguard Worker const int highbd = cm->seq_params->use_highbitdepth;
905*77c1e3ccSAndroid Build Coastguard Worker const int bit_depth = cm->seq_params->bit_depth;
906*77c1e3ccSAndroid Build Coastguard Worker
907*77c1e3ccSAndroid Build Coastguard Worker const int64_t bits_none = x->mode_costs.sgrproj_restore_cost[0];
908*77c1e3ccSAndroid Build Coastguard Worker // Prune evaluation of RESTORE_SGRPROJ if 'skip_sgr_eval' is set
909*77c1e3ccSAndroid Build Coastguard Worker if (rsc->skip_sgr_eval) {
910*77c1e3ccSAndroid Build Coastguard Worker rsc->total_bits[RESTORE_SGRPROJ] += bits_none;
911*77c1e3ccSAndroid Build Coastguard Worker rsc->total_sse[RESTORE_SGRPROJ] += rsc->sse[RESTORE_NONE];
912*77c1e3ccSAndroid Build Coastguard Worker rusi->best_rtype[RESTORE_SGRPROJ - 1] = RESTORE_NONE;
913*77c1e3ccSAndroid Build Coastguard Worker rsc->sse[RESTORE_SGRPROJ] = INT64_MAX;
914*77c1e3ccSAndroid Build Coastguard Worker return;
915*77c1e3ccSAndroid Build Coastguard Worker }
916*77c1e3ccSAndroid Build Coastguard Worker
917*77c1e3ccSAndroid Build Coastguard Worker uint8_t *dgd_start =
918*77c1e3ccSAndroid Build Coastguard Worker rsc->dgd_buffer + limits->v_start * rsc->dgd_stride + limits->h_start;
919*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src_start =
920*77c1e3ccSAndroid Build Coastguard Worker rsc->src_buffer + limits->v_start * rsc->src_stride + limits->h_start;
921*77c1e3ccSAndroid Build Coastguard Worker
922*77c1e3ccSAndroid Build Coastguard Worker const int is_uv = rsc->plane > 0;
923*77c1e3ccSAndroid Build Coastguard Worker const int ss_x = is_uv && cm->seq_params->subsampling_x;
924*77c1e3ccSAndroid Build Coastguard Worker const int ss_y = is_uv && cm->seq_params->subsampling_y;
925*77c1e3ccSAndroid Build Coastguard Worker const int procunit_width = RESTORATION_PROC_UNIT_SIZE >> ss_x;
926*77c1e3ccSAndroid Build Coastguard Worker const int procunit_height = RESTORATION_PROC_UNIT_SIZE >> ss_y;
927*77c1e3ccSAndroid Build Coastguard Worker
928*77c1e3ccSAndroid Build Coastguard Worker rusi->sgrproj = search_selfguided_restoration(
929*77c1e3ccSAndroid Build Coastguard Worker dgd_start, limits->h_end - limits->h_start,
930*77c1e3ccSAndroid Build Coastguard Worker limits->v_end - limits->v_start, rsc->dgd_stride, src_start,
931*77c1e3ccSAndroid Build Coastguard Worker rsc->src_stride, highbd, bit_depth, procunit_width, procunit_height,
932*77c1e3ccSAndroid Build Coastguard Worker tmpbuf, rsc->lpf_sf->enable_sgr_ep_pruning, error_info);
933*77c1e3ccSAndroid Build Coastguard Worker
934*77c1e3ccSAndroid Build Coastguard Worker RestorationUnitInfo rui;
935*77c1e3ccSAndroid Build Coastguard Worker rui.restoration_type = RESTORE_SGRPROJ;
936*77c1e3ccSAndroid Build Coastguard Worker rui.sgrproj_info = rusi->sgrproj;
937*77c1e3ccSAndroid Build Coastguard Worker
938*77c1e3ccSAndroid Build Coastguard Worker rsc->sse[RESTORE_SGRPROJ] = try_restoration_unit(rsc, limits, &rui);
939*77c1e3ccSAndroid Build Coastguard Worker
940*77c1e3ccSAndroid Build Coastguard Worker const int64_t bits_sgr =
941*77c1e3ccSAndroid Build Coastguard Worker x->mode_costs.sgrproj_restore_cost[1] +
942*77c1e3ccSAndroid Build Coastguard Worker (count_sgrproj_bits(&rusi->sgrproj, &rsc->ref_sgrproj)
943*77c1e3ccSAndroid Build Coastguard Worker << AV1_PROB_COST_SHIFT);
944*77c1e3ccSAndroid Build Coastguard Worker double cost_none = RDCOST_DBL_WITH_NATIVE_BD_DIST(
945*77c1e3ccSAndroid Build Coastguard Worker x->rdmult, bits_none >> 4, rsc->sse[RESTORE_NONE], bit_depth);
946*77c1e3ccSAndroid Build Coastguard Worker double cost_sgr = RDCOST_DBL_WITH_NATIVE_BD_DIST(
947*77c1e3ccSAndroid Build Coastguard Worker x->rdmult, bits_sgr >> 4, rsc->sse[RESTORE_SGRPROJ], bit_depth);
948*77c1e3ccSAndroid Build Coastguard Worker if (rusi->sgrproj.ep < 10)
949*77c1e3ccSAndroid Build Coastguard Worker cost_sgr *=
950*77c1e3ccSAndroid Build Coastguard Worker (1 + DUAL_SGR_PENALTY_MULT * rsc->lpf_sf->dual_sgr_penalty_level);
951*77c1e3ccSAndroid Build Coastguard Worker
952*77c1e3ccSAndroid Build Coastguard Worker RestorationType rtype =
953*77c1e3ccSAndroid Build Coastguard Worker (cost_sgr < cost_none) ? RESTORE_SGRPROJ : RESTORE_NONE;
954*77c1e3ccSAndroid Build Coastguard Worker rusi->best_rtype[RESTORE_SGRPROJ - 1] = rtype;
955*77c1e3ccSAndroid Build Coastguard Worker
956*77c1e3ccSAndroid Build Coastguard Worker #if DEBUG_LR_COSTING
957*77c1e3ccSAndroid Build Coastguard Worker // Store ref params for later checking
958*77c1e3ccSAndroid Build Coastguard Worker lr_ref_params[RESTORE_SGRPROJ][rsc->plane][rest_unit_idx].sgrproj_info =
959*77c1e3ccSAndroid Build Coastguard Worker rsc->ref_sgrproj;
960*77c1e3ccSAndroid Build Coastguard Worker #endif // DEBUG_LR_COSTING
961*77c1e3ccSAndroid Build Coastguard Worker
962*77c1e3ccSAndroid Build Coastguard Worker rsc->total_sse[RESTORE_SGRPROJ] += rsc->sse[rtype];
963*77c1e3ccSAndroid Build Coastguard Worker rsc->total_bits[RESTORE_SGRPROJ] +=
964*77c1e3ccSAndroid Build Coastguard Worker (cost_sgr < cost_none) ? bits_sgr : bits_none;
965*77c1e3ccSAndroid Build Coastguard Worker if (cost_sgr < cost_none) rsc->ref_sgrproj = rusi->sgrproj;
966*77c1e3ccSAndroid Build Coastguard Worker }
967*77c1e3ccSAndroid Build Coastguard Worker
acc_stat_one_line(const uint8_t * dgd,const uint8_t * src,int dgd_stride,int h_start,int h_end,uint8_t avg,const int wiener_halfwin,const int wiener_win2,int32_t * M_int32,int32_t * H_int32,int count)968*77c1e3ccSAndroid Build Coastguard Worker static void acc_stat_one_line(const uint8_t *dgd, const uint8_t *src,
969*77c1e3ccSAndroid Build Coastguard Worker int dgd_stride, int h_start, int h_end,
970*77c1e3ccSAndroid Build Coastguard Worker uint8_t avg, const int wiener_halfwin,
971*77c1e3ccSAndroid Build Coastguard Worker const int wiener_win2, int32_t *M_int32,
972*77c1e3ccSAndroid Build Coastguard Worker int32_t *H_int32, int count) {
973*77c1e3ccSAndroid Build Coastguard Worker int j, k, l;
974*77c1e3ccSAndroid Build Coastguard Worker int16_t Y[WIENER_WIN2];
975*77c1e3ccSAndroid Build Coastguard Worker
976*77c1e3ccSAndroid Build Coastguard Worker for (j = h_start; j < h_end; j++) {
977*77c1e3ccSAndroid Build Coastguard Worker const int16_t X = (int16_t)src[j] - (int16_t)avg;
978*77c1e3ccSAndroid Build Coastguard Worker int idx = 0;
979*77c1e3ccSAndroid Build Coastguard Worker for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
980*77c1e3ccSAndroid Build Coastguard Worker for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
981*77c1e3ccSAndroid Build Coastguard Worker Y[idx] =
982*77c1e3ccSAndroid Build Coastguard Worker (int16_t)dgd[(count + l) * dgd_stride + (j + k)] - (int16_t)avg;
983*77c1e3ccSAndroid Build Coastguard Worker idx++;
984*77c1e3ccSAndroid Build Coastguard Worker }
985*77c1e3ccSAndroid Build Coastguard Worker }
986*77c1e3ccSAndroid Build Coastguard Worker assert(idx == wiener_win2);
987*77c1e3ccSAndroid Build Coastguard Worker for (k = 0; k < wiener_win2; ++k) {
988*77c1e3ccSAndroid Build Coastguard Worker M_int32[k] += (int32_t)Y[k] * X;
989*77c1e3ccSAndroid Build Coastguard Worker for (l = k; l < wiener_win2; ++l) {
990*77c1e3ccSAndroid Build Coastguard Worker // H is a symmetric matrix, so we only need to fill out the upper
991*77c1e3ccSAndroid Build Coastguard Worker // triangle here. We can copy it down to the lower triangle outside
992*77c1e3ccSAndroid Build Coastguard Worker // the (i, j) loops.
993*77c1e3ccSAndroid Build Coastguard Worker H_int32[k * wiener_win2 + l] += (int32_t)Y[k] * Y[l];
994*77c1e3ccSAndroid Build Coastguard Worker }
995*77c1e3ccSAndroid Build Coastguard Worker }
996*77c1e3ccSAndroid Build Coastguard Worker }
997*77c1e3ccSAndroid Build Coastguard Worker }
998*77c1e3ccSAndroid Build Coastguard Worker
av1_compute_stats_c(int wiener_win,const uint8_t * dgd,const uint8_t * src,int16_t * dgd_avg,int16_t * src_avg,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,int use_downsampled_wiener_stats)999*77c1e3ccSAndroid Build Coastguard Worker void av1_compute_stats_c(int wiener_win, const uint8_t *dgd, const uint8_t *src,
1000*77c1e3ccSAndroid Build Coastguard Worker int16_t *dgd_avg, int16_t *src_avg, int h_start,
1001*77c1e3ccSAndroid Build Coastguard Worker int h_end, int v_start, int v_end, int dgd_stride,
1002*77c1e3ccSAndroid Build Coastguard Worker int src_stride, int64_t *M, int64_t *H,
1003*77c1e3ccSAndroid Build Coastguard Worker int use_downsampled_wiener_stats) {
1004*77c1e3ccSAndroid Build Coastguard Worker (void)dgd_avg;
1005*77c1e3ccSAndroid Build Coastguard Worker (void)src_avg;
1006*77c1e3ccSAndroid Build Coastguard Worker int i, k, l;
1007*77c1e3ccSAndroid Build Coastguard Worker const int wiener_win2 = wiener_win * wiener_win;
1008*77c1e3ccSAndroid Build Coastguard Worker const int wiener_halfwin = (wiener_win >> 1);
1009*77c1e3ccSAndroid Build Coastguard Worker uint8_t avg = find_average(dgd, h_start, h_end, v_start, v_end, dgd_stride);
1010*77c1e3ccSAndroid Build Coastguard Worker int32_t M_row[WIENER_WIN2] = { 0 };
1011*77c1e3ccSAndroid Build Coastguard Worker int32_t H_row[WIENER_WIN2 * WIENER_WIN2] = { 0 };
1012*77c1e3ccSAndroid Build Coastguard Worker int downsample_factor =
1013*77c1e3ccSAndroid Build Coastguard Worker use_downsampled_wiener_stats ? WIENER_STATS_DOWNSAMPLE_FACTOR : 1;
1014*77c1e3ccSAndroid Build Coastguard Worker
1015*77c1e3ccSAndroid Build Coastguard Worker memset(M, 0, sizeof(*M) * wiener_win2);
1016*77c1e3ccSAndroid Build Coastguard Worker memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
1017*77c1e3ccSAndroid Build Coastguard Worker
1018*77c1e3ccSAndroid Build Coastguard Worker for (i = v_start; i < v_end; i = i + downsample_factor) {
1019*77c1e3ccSAndroid Build Coastguard Worker if (use_downsampled_wiener_stats &&
1020*77c1e3ccSAndroid Build Coastguard Worker (v_end - i < WIENER_STATS_DOWNSAMPLE_FACTOR)) {
1021*77c1e3ccSAndroid Build Coastguard Worker downsample_factor = v_end - i;
1022*77c1e3ccSAndroid Build Coastguard Worker }
1023*77c1e3ccSAndroid Build Coastguard Worker
1024*77c1e3ccSAndroid Build Coastguard Worker memset(M_row, 0, sizeof(int32_t) * WIENER_WIN2);
1025*77c1e3ccSAndroid Build Coastguard Worker memset(H_row, 0, sizeof(int32_t) * WIENER_WIN2 * WIENER_WIN2);
1026*77c1e3ccSAndroid Build Coastguard Worker acc_stat_one_line(dgd, src + i * src_stride, dgd_stride, h_start, h_end,
1027*77c1e3ccSAndroid Build Coastguard Worker avg, wiener_halfwin, wiener_win2, M_row, H_row, i);
1028*77c1e3ccSAndroid Build Coastguard Worker
1029*77c1e3ccSAndroid Build Coastguard Worker for (k = 0; k < wiener_win2; ++k) {
1030*77c1e3ccSAndroid Build Coastguard Worker // Scale M matrix based on the downsampling factor
1031*77c1e3ccSAndroid Build Coastguard Worker M[k] += ((int64_t)M_row[k] * downsample_factor);
1032*77c1e3ccSAndroid Build Coastguard Worker for (l = k; l < wiener_win2; ++l) {
1033*77c1e3ccSAndroid Build Coastguard Worker // H is a symmetric matrix, so we only need to fill out the upper
1034*77c1e3ccSAndroid Build Coastguard Worker // triangle here. We can copy it down to the lower triangle outside
1035*77c1e3ccSAndroid Build Coastguard Worker // the (i, j) loops.
1036*77c1e3ccSAndroid Build Coastguard Worker // Scale H Matrix based on the downsampling factor
1037*77c1e3ccSAndroid Build Coastguard Worker H[k * wiener_win2 + l] +=
1038*77c1e3ccSAndroid Build Coastguard Worker ((int64_t)H_row[k * wiener_win2 + l] * downsample_factor);
1039*77c1e3ccSAndroid Build Coastguard Worker }
1040*77c1e3ccSAndroid Build Coastguard Worker }
1041*77c1e3ccSAndroid Build Coastguard Worker }
1042*77c1e3ccSAndroid Build Coastguard Worker
1043*77c1e3ccSAndroid Build Coastguard Worker for (k = 0; k < wiener_win2; ++k) {
1044*77c1e3ccSAndroid Build Coastguard Worker for (l = k + 1; l < wiener_win2; ++l) {
1045*77c1e3ccSAndroid Build Coastguard Worker H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
1046*77c1e3ccSAndroid Build Coastguard Worker }
1047*77c1e3ccSAndroid Build Coastguard Worker }
1048*77c1e3ccSAndroid Build Coastguard Worker }
1049*77c1e3ccSAndroid Build Coastguard Worker
1050*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
av1_compute_stats_highbd_c(int wiener_win,const uint8_t * dgd8,const uint8_t * src8,int16_t * dgd_avg,int16_t * src_avg,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,aom_bit_depth_t bit_depth)1051*77c1e3ccSAndroid Build Coastguard Worker void av1_compute_stats_highbd_c(int wiener_win, const uint8_t *dgd8,
1052*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src8, int16_t *dgd_avg,
1053*77c1e3ccSAndroid Build Coastguard Worker int16_t *src_avg, int h_start, int h_end,
1054*77c1e3ccSAndroid Build Coastguard Worker int v_start, int v_end, int dgd_stride,
1055*77c1e3ccSAndroid Build Coastguard Worker int src_stride, int64_t *M, int64_t *H,
1056*77c1e3ccSAndroid Build Coastguard Worker aom_bit_depth_t bit_depth) {
1057*77c1e3ccSAndroid Build Coastguard Worker (void)dgd_avg;
1058*77c1e3ccSAndroid Build Coastguard Worker (void)src_avg;
1059*77c1e3ccSAndroid Build Coastguard Worker int i, j, k, l;
1060*77c1e3ccSAndroid Build Coastguard Worker int32_t Y[WIENER_WIN2];
1061*77c1e3ccSAndroid Build Coastguard Worker const int wiener_win2 = wiener_win * wiener_win;
1062*77c1e3ccSAndroid Build Coastguard Worker const int wiener_halfwin = (wiener_win >> 1);
1063*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1064*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
1065*77c1e3ccSAndroid Build Coastguard Worker uint16_t avg =
1066*77c1e3ccSAndroid Build Coastguard Worker find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
1067*77c1e3ccSAndroid Build Coastguard Worker
1068*77c1e3ccSAndroid Build Coastguard Worker uint8_t bit_depth_divider = 1;
1069*77c1e3ccSAndroid Build Coastguard Worker if (bit_depth == AOM_BITS_12)
1070*77c1e3ccSAndroid Build Coastguard Worker bit_depth_divider = 16;
1071*77c1e3ccSAndroid Build Coastguard Worker else if (bit_depth == AOM_BITS_10)
1072*77c1e3ccSAndroid Build Coastguard Worker bit_depth_divider = 4;
1073*77c1e3ccSAndroid Build Coastguard Worker
1074*77c1e3ccSAndroid Build Coastguard Worker memset(M, 0, sizeof(*M) * wiener_win2);
1075*77c1e3ccSAndroid Build Coastguard Worker memset(H, 0, sizeof(*H) * wiener_win2 * wiener_win2);
1076*77c1e3ccSAndroid Build Coastguard Worker for (i = v_start; i < v_end; i++) {
1077*77c1e3ccSAndroid Build Coastguard Worker for (j = h_start; j < h_end; j++) {
1078*77c1e3ccSAndroid Build Coastguard Worker const int32_t X = (int32_t)src[i * src_stride + j] - (int32_t)avg;
1079*77c1e3ccSAndroid Build Coastguard Worker int idx = 0;
1080*77c1e3ccSAndroid Build Coastguard Worker for (k = -wiener_halfwin; k <= wiener_halfwin; k++) {
1081*77c1e3ccSAndroid Build Coastguard Worker for (l = -wiener_halfwin; l <= wiener_halfwin; l++) {
1082*77c1e3ccSAndroid Build Coastguard Worker Y[idx] = (int32_t)dgd[(i + l) * dgd_stride + (j + k)] - (int32_t)avg;
1083*77c1e3ccSAndroid Build Coastguard Worker idx++;
1084*77c1e3ccSAndroid Build Coastguard Worker }
1085*77c1e3ccSAndroid Build Coastguard Worker }
1086*77c1e3ccSAndroid Build Coastguard Worker assert(idx == wiener_win2);
1087*77c1e3ccSAndroid Build Coastguard Worker for (k = 0; k < wiener_win2; ++k) {
1088*77c1e3ccSAndroid Build Coastguard Worker M[k] += (int64_t)Y[k] * X;
1089*77c1e3ccSAndroid Build Coastguard Worker for (l = k; l < wiener_win2; ++l) {
1090*77c1e3ccSAndroid Build Coastguard Worker // H is a symmetric matrix, so we only need to fill out the upper
1091*77c1e3ccSAndroid Build Coastguard Worker // triangle here. We can copy it down to the lower triangle outside
1092*77c1e3ccSAndroid Build Coastguard Worker // the (i, j) loops.
1093*77c1e3ccSAndroid Build Coastguard Worker H[k * wiener_win2 + l] += (int64_t)Y[k] * Y[l];
1094*77c1e3ccSAndroid Build Coastguard Worker }
1095*77c1e3ccSAndroid Build Coastguard Worker }
1096*77c1e3ccSAndroid Build Coastguard Worker }
1097*77c1e3ccSAndroid Build Coastguard Worker }
1098*77c1e3ccSAndroid Build Coastguard Worker for (k = 0; k < wiener_win2; ++k) {
1099*77c1e3ccSAndroid Build Coastguard Worker M[k] /= bit_depth_divider;
1100*77c1e3ccSAndroid Build Coastguard Worker H[k * wiener_win2 + k] /= bit_depth_divider;
1101*77c1e3ccSAndroid Build Coastguard Worker for (l = k + 1; l < wiener_win2; ++l) {
1102*77c1e3ccSAndroid Build Coastguard Worker H[k * wiener_win2 + l] /= bit_depth_divider;
1103*77c1e3ccSAndroid Build Coastguard Worker H[l * wiener_win2 + k] = H[k * wiener_win2 + l];
1104*77c1e3ccSAndroid Build Coastguard Worker }
1105*77c1e3ccSAndroid Build Coastguard Worker }
1106*77c1e3ccSAndroid Build Coastguard Worker }
1107*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_AV1_HIGHBITDEPTH
1108*77c1e3ccSAndroid Build Coastguard Worker
wrap_index(int i,int wiener_win)1109*77c1e3ccSAndroid Build Coastguard Worker static inline int wrap_index(int i, int wiener_win) {
1110*77c1e3ccSAndroid Build Coastguard Worker const int wiener_halfwin1 = (wiener_win >> 1) + 1;
1111*77c1e3ccSAndroid Build Coastguard Worker return (i >= wiener_halfwin1 ? wiener_win - 1 - i : i);
1112*77c1e3ccSAndroid Build Coastguard Worker }
1113*77c1e3ccSAndroid Build Coastguard Worker
1114*77c1e3ccSAndroid Build Coastguard Worker // Splits each w[i] into smaller components w1[i] and w2[i] such that
1115*77c1e3ccSAndroid Build Coastguard Worker // w[i] = w1[i] * WIENER_TAP_SCALE_FACTOR + w2[i].
split_wiener_filter_coefficients(int wiener_win,const int32_t * w,int32_t * w1,int32_t * w2)1116*77c1e3ccSAndroid Build Coastguard Worker static inline void split_wiener_filter_coefficients(int wiener_win,
1117*77c1e3ccSAndroid Build Coastguard Worker const int32_t *w,
1118*77c1e3ccSAndroid Build Coastguard Worker int32_t *w1, int32_t *w2) {
1119*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < wiener_win; i++) {
1120*77c1e3ccSAndroid Build Coastguard Worker w1[i] = w[i] / WIENER_TAP_SCALE_FACTOR;
1121*77c1e3ccSAndroid Build Coastguard Worker w2[i] = w[i] - w1[i] * WIENER_TAP_SCALE_FACTOR;
1122*77c1e3ccSAndroid Build Coastguard Worker assert(w[i] == w1[i] * WIENER_TAP_SCALE_FACTOR + w2[i]);
1123*77c1e3ccSAndroid Build Coastguard Worker }
1124*77c1e3ccSAndroid Build Coastguard Worker }
1125*77c1e3ccSAndroid Build Coastguard Worker
1126*77c1e3ccSAndroid Build Coastguard Worker // Calculates x * w / WIENER_TAP_SCALE_FACTOR, where
1127*77c1e3ccSAndroid Build Coastguard Worker // w = w1 * WIENER_TAP_SCALE_FACTOR + w2.
1128*77c1e3ccSAndroid Build Coastguard Worker //
1129*77c1e3ccSAndroid Build Coastguard Worker // The multiplication x * w may overflow, so we multiply x by the components of
1130*77c1e3ccSAndroid Build Coastguard Worker // w (w1 and w2) and combine the multiplication with the division.
multiply_and_scale(int64_t x,int32_t w1,int32_t w2)1131*77c1e3ccSAndroid Build Coastguard Worker static inline int64_t multiply_and_scale(int64_t x, int32_t w1, int32_t w2) {
1132*77c1e3ccSAndroid Build Coastguard Worker // Let y = x * w / WIENER_TAP_SCALE_FACTOR
1133*77c1e3ccSAndroid Build Coastguard Worker // = x * (w1 * WIENER_TAP_SCALE_FACTOR + w2) / WIENER_TAP_SCALE_FACTOR
1134*77c1e3ccSAndroid Build Coastguard Worker const int64_t y = x * w1 + x * w2 / WIENER_TAP_SCALE_FACTOR;
1135*77c1e3ccSAndroid Build Coastguard Worker return y;
1136*77c1e3ccSAndroid Build Coastguard Worker }
1137*77c1e3ccSAndroid Build Coastguard Worker
1138*77c1e3ccSAndroid Build Coastguard Worker // Solve linear equations to find Wiener filter tap values
1139*77c1e3ccSAndroid Build Coastguard Worker // Taps are output scaled by WIENER_FILT_STEP
linsolve_wiener(int n,int64_t * A,int stride,int64_t * b,int64_t * x)1140*77c1e3ccSAndroid Build Coastguard Worker static int linsolve_wiener(int n, int64_t *A, int stride, int64_t *b,
1141*77c1e3ccSAndroid Build Coastguard Worker int64_t *x) {
1142*77c1e3ccSAndroid Build Coastguard Worker for (int k = 0; k < n - 1; k++) {
1143*77c1e3ccSAndroid Build Coastguard Worker // Partial pivoting: bring the row with the largest pivot to the top
1144*77c1e3ccSAndroid Build Coastguard Worker for (int i = n - 1; i > k; i--) {
1145*77c1e3ccSAndroid Build Coastguard Worker // If row i has a better (bigger) pivot than row (i-1), swap them
1146*77c1e3ccSAndroid Build Coastguard Worker if (llabs(A[(i - 1) * stride + k]) < llabs(A[i * stride + k])) {
1147*77c1e3ccSAndroid Build Coastguard Worker for (int j = 0; j < n; j++) {
1148*77c1e3ccSAndroid Build Coastguard Worker const int64_t c = A[i * stride + j];
1149*77c1e3ccSAndroid Build Coastguard Worker A[i * stride + j] = A[(i - 1) * stride + j];
1150*77c1e3ccSAndroid Build Coastguard Worker A[(i - 1) * stride + j] = c;
1151*77c1e3ccSAndroid Build Coastguard Worker }
1152*77c1e3ccSAndroid Build Coastguard Worker const int64_t c = b[i];
1153*77c1e3ccSAndroid Build Coastguard Worker b[i] = b[i - 1];
1154*77c1e3ccSAndroid Build Coastguard Worker b[i - 1] = c;
1155*77c1e3ccSAndroid Build Coastguard Worker }
1156*77c1e3ccSAndroid Build Coastguard Worker }
1157*77c1e3ccSAndroid Build Coastguard Worker
1158*77c1e3ccSAndroid Build Coastguard Worker // b/278065963: The multiplies
1159*77c1e3ccSAndroid Build Coastguard Worker // c / 256 * A[k * stride + j] / cd * 256
1160*77c1e3ccSAndroid Build Coastguard Worker // and
1161*77c1e3ccSAndroid Build Coastguard Worker // c / 256 * b[k] / cd * 256
1162*77c1e3ccSAndroid Build Coastguard Worker // within Gaussian elimination can cause a signed integer overflow. Rework
1163*77c1e3ccSAndroid Build Coastguard Worker // the multiplies so that larger scaling is used without significantly
1164*77c1e3ccSAndroid Build Coastguard Worker // impacting the overall precision.
1165*77c1e3ccSAndroid Build Coastguard Worker //
1166*77c1e3ccSAndroid Build Coastguard Worker // Precision guidance:
1167*77c1e3ccSAndroid Build Coastguard Worker // scale_threshold: Pick as high as possible.
1168*77c1e3ccSAndroid Build Coastguard Worker // For max_abs_akj >= scale_threshold scenario:
1169*77c1e3ccSAndroid Build Coastguard Worker // scaler_A: Pick as low as possible. Needed for A[(i + 1) * stride + j].
1170*77c1e3ccSAndroid Build Coastguard Worker // scaler_c: Pick as low as possible while maintaining scaler_c >=
1171*77c1e3ccSAndroid Build Coastguard Worker // (1 << 7). Needed for A[(i + 1) * stride + j] and b[i + 1].
1172*77c1e3ccSAndroid Build Coastguard Worker int64_t max_abs_akj = 0;
1173*77c1e3ccSAndroid Build Coastguard Worker for (int j = 0; j < n; j++) {
1174*77c1e3ccSAndroid Build Coastguard Worker const int64_t abs_akj = llabs(A[k * stride + j]);
1175*77c1e3ccSAndroid Build Coastguard Worker if (abs_akj > max_abs_akj) max_abs_akj = abs_akj;
1176*77c1e3ccSAndroid Build Coastguard Worker }
1177*77c1e3ccSAndroid Build Coastguard Worker const int scale_threshold = 1 << 22;
1178*77c1e3ccSAndroid Build Coastguard Worker const int scaler_A = max_abs_akj < scale_threshold ? 1 : (1 << 6);
1179*77c1e3ccSAndroid Build Coastguard Worker const int scaler_c = max_abs_akj < scale_threshold ? 1 : (1 << 7);
1180*77c1e3ccSAndroid Build Coastguard Worker const int scaler = scaler_c * scaler_A;
1181*77c1e3ccSAndroid Build Coastguard Worker
1182*77c1e3ccSAndroid Build Coastguard Worker // Forward elimination (convert A to row-echelon form)
1183*77c1e3ccSAndroid Build Coastguard Worker for (int i = k; i < n - 1; i++) {
1184*77c1e3ccSAndroid Build Coastguard Worker if (A[k * stride + k] == 0) return 0;
1185*77c1e3ccSAndroid Build Coastguard Worker const int64_t c = A[(i + 1) * stride + k] / scaler_c;
1186*77c1e3ccSAndroid Build Coastguard Worker const int64_t cd = A[k * stride + k];
1187*77c1e3ccSAndroid Build Coastguard Worker for (int j = 0; j < n; j++) {
1188*77c1e3ccSAndroid Build Coastguard Worker A[(i + 1) * stride + j] -=
1189*77c1e3ccSAndroid Build Coastguard Worker A[k * stride + j] / scaler_A * c / cd * scaler;
1190*77c1e3ccSAndroid Build Coastguard Worker }
1191*77c1e3ccSAndroid Build Coastguard Worker b[i + 1] -= c * b[k] / cd * scaler_c;
1192*77c1e3ccSAndroid Build Coastguard Worker }
1193*77c1e3ccSAndroid Build Coastguard Worker }
1194*77c1e3ccSAndroid Build Coastguard Worker // Back-substitution
1195*77c1e3ccSAndroid Build Coastguard Worker for (int i = n - 1; i >= 0; i--) {
1196*77c1e3ccSAndroid Build Coastguard Worker if (A[i * stride + i] == 0) return 0;
1197*77c1e3ccSAndroid Build Coastguard Worker int64_t c = 0;
1198*77c1e3ccSAndroid Build Coastguard Worker for (int j = i + 1; j <= n - 1; j++) {
1199*77c1e3ccSAndroid Build Coastguard Worker c += A[i * stride + j] * x[j] / WIENER_TAP_SCALE_FACTOR;
1200*77c1e3ccSAndroid Build Coastguard Worker }
1201*77c1e3ccSAndroid Build Coastguard Worker // Store filter taps x in scaled form.
1202*77c1e3ccSAndroid Build Coastguard Worker x[i] = WIENER_TAP_SCALE_FACTOR * (b[i] - c) / A[i * stride + i];
1203*77c1e3ccSAndroid Build Coastguard Worker }
1204*77c1e3ccSAndroid Build Coastguard Worker
1205*77c1e3ccSAndroid Build Coastguard Worker return 1;
1206*77c1e3ccSAndroid Build Coastguard Worker }
1207*77c1e3ccSAndroid Build Coastguard Worker
1208*77c1e3ccSAndroid Build Coastguard Worker // Fix vector b, update vector a
update_a_sep_sym(int wiener_win,int64_t ** Mc,int64_t ** Hc,int32_t * a,const int32_t * b)1209*77c1e3ccSAndroid Build Coastguard Worker static inline void update_a_sep_sym(int wiener_win, int64_t **Mc, int64_t **Hc,
1210*77c1e3ccSAndroid Build Coastguard Worker int32_t *a, const int32_t *b) {
1211*77c1e3ccSAndroid Build Coastguard Worker int i, j;
1212*77c1e3ccSAndroid Build Coastguard Worker int64_t S[WIENER_WIN];
1213*77c1e3ccSAndroid Build Coastguard Worker int64_t A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
1214*77c1e3ccSAndroid Build Coastguard Worker int32_t b1[WIENER_WIN], b2[WIENER_WIN];
1215*77c1e3ccSAndroid Build Coastguard Worker const int wiener_win2 = wiener_win * wiener_win;
1216*77c1e3ccSAndroid Build Coastguard Worker const int wiener_halfwin1 = (wiener_win >> 1) + 1;
1217*77c1e3ccSAndroid Build Coastguard Worker memset(A, 0, sizeof(A));
1218*77c1e3ccSAndroid Build Coastguard Worker memset(B, 0, sizeof(B));
1219*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_win; i++) {
1220*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < wiener_win; ++j) {
1221*77c1e3ccSAndroid Build Coastguard Worker const int jj = wrap_index(j, wiener_win);
1222*77c1e3ccSAndroid Build Coastguard Worker A[jj] += Mc[i][j] * b[i] / WIENER_TAP_SCALE_FACTOR;
1223*77c1e3ccSAndroid Build Coastguard Worker }
1224*77c1e3ccSAndroid Build Coastguard Worker }
1225*77c1e3ccSAndroid Build Coastguard Worker split_wiener_filter_coefficients(wiener_win, b, b1, b2);
1226*77c1e3ccSAndroid Build Coastguard Worker
1227*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_win; i++) {
1228*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < wiener_win; j++) {
1229*77c1e3ccSAndroid Build Coastguard Worker int k, l;
1230*77c1e3ccSAndroid Build Coastguard Worker for (k = 0; k < wiener_win; ++k) {
1231*77c1e3ccSAndroid Build Coastguard Worker const int kk = wrap_index(k, wiener_win);
1232*77c1e3ccSAndroid Build Coastguard Worker for (l = 0; l < wiener_win; ++l) {
1233*77c1e3ccSAndroid Build Coastguard Worker const int ll = wrap_index(l, wiener_win);
1234*77c1e3ccSAndroid Build Coastguard Worker // Calculate
1235*77c1e3ccSAndroid Build Coastguard Worker // B[ll * wiener_halfwin1 + kk] +=
1236*77c1e3ccSAndroid Build Coastguard Worker // Hc[j * wiener_win + i][k * wiener_win2 + l] * b[i] /
1237*77c1e3ccSAndroid Build Coastguard Worker // WIENER_TAP_SCALE_FACTOR * b[j] / WIENER_TAP_SCALE_FACTOR;
1238*77c1e3ccSAndroid Build Coastguard Worker //
1239*77c1e3ccSAndroid Build Coastguard Worker // The last multiplication may overflow, so we combine the last
1240*77c1e3ccSAndroid Build Coastguard Worker // multiplication with the last division.
1241*77c1e3ccSAndroid Build Coastguard Worker const int64_t x = Hc[j * wiener_win + i][k * wiener_win2 + l] * b[i] /
1242*77c1e3ccSAndroid Build Coastguard Worker WIENER_TAP_SCALE_FACTOR;
1243*77c1e3ccSAndroid Build Coastguard Worker // b[j] = b1[j] * WIENER_TAP_SCALE_FACTOR + b2[j]
1244*77c1e3ccSAndroid Build Coastguard Worker B[ll * wiener_halfwin1 + kk] += multiply_and_scale(x, b1[j], b2[j]);
1245*77c1e3ccSAndroid Build Coastguard Worker }
1246*77c1e3ccSAndroid Build Coastguard Worker }
1247*77c1e3ccSAndroid Build Coastguard Worker }
1248*77c1e3ccSAndroid Build Coastguard Worker }
1249*77c1e3ccSAndroid Build Coastguard Worker // Normalization enforcement in the system of equations itself
1250*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_halfwin1 - 1; ++i) {
1251*77c1e3ccSAndroid Build Coastguard Worker A[i] -=
1252*77c1e3ccSAndroid Build Coastguard Worker A[wiener_halfwin1 - 1] * 2 +
1253*77c1e3ccSAndroid Build Coastguard Worker B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
1254*77c1e3ccSAndroid Build Coastguard Worker 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
1255*77c1e3ccSAndroid Build Coastguard Worker }
1256*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_halfwin1 - 1; ++i) {
1257*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < wiener_halfwin1 - 1; ++j) {
1258*77c1e3ccSAndroid Build Coastguard Worker B[i * wiener_halfwin1 + j] -=
1259*77c1e3ccSAndroid Build Coastguard Worker 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
1260*77c1e3ccSAndroid Build Coastguard Worker B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
1261*77c1e3ccSAndroid Build Coastguard Worker 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
1262*77c1e3ccSAndroid Build Coastguard Worker (wiener_halfwin1 - 1)]);
1263*77c1e3ccSAndroid Build Coastguard Worker }
1264*77c1e3ccSAndroid Build Coastguard Worker }
1265*77c1e3ccSAndroid Build Coastguard Worker if (linsolve_wiener(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
1266*77c1e3ccSAndroid Build Coastguard Worker S[wiener_halfwin1 - 1] = WIENER_TAP_SCALE_FACTOR;
1267*77c1e3ccSAndroid Build Coastguard Worker for (i = wiener_halfwin1; i < wiener_win; ++i) {
1268*77c1e3ccSAndroid Build Coastguard Worker S[i] = S[wiener_win - 1 - i];
1269*77c1e3ccSAndroid Build Coastguard Worker S[wiener_halfwin1 - 1] -= 2 * S[i];
1270*77c1e3ccSAndroid Build Coastguard Worker }
1271*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_win; ++i) {
1272*77c1e3ccSAndroid Build Coastguard Worker a[i] = (int32_t)CLIP(S[i], -(1 << (WIENER_FILT_BITS - 1)),
1273*77c1e3ccSAndroid Build Coastguard Worker (1 << (WIENER_FILT_BITS - 1)) - 1);
1274*77c1e3ccSAndroid Build Coastguard Worker }
1275*77c1e3ccSAndroid Build Coastguard Worker }
1276*77c1e3ccSAndroid Build Coastguard Worker }
1277*77c1e3ccSAndroid Build Coastguard Worker
1278*77c1e3ccSAndroid Build Coastguard Worker // Fix vector a, update vector b
update_b_sep_sym(int wiener_win,int64_t ** Mc,int64_t ** Hc,const int32_t * a,int32_t * b)1279*77c1e3ccSAndroid Build Coastguard Worker static inline void update_b_sep_sym(int wiener_win, int64_t **Mc, int64_t **Hc,
1280*77c1e3ccSAndroid Build Coastguard Worker const int32_t *a, int32_t *b) {
1281*77c1e3ccSAndroid Build Coastguard Worker int i, j;
1282*77c1e3ccSAndroid Build Coastguard Worker int64_t S[WIENER_WIN];
1283*77c1e3ccSAndroid Build Coastguard Worker int64_t A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1];
1284*77c1e3ccSAndroid Build Coastguard Worker int32_t a1[WIENER_WIN], a2[WIENER_WIN];
1285*77c1e3ccSAndroid Build Coastguard Worker const int wiener_win2 = wiener_win * wiener_win;
1286*77c1e3ccSAndroid Build Coastguard Worker const int wiener_halfwin1 = (wiener_win >> 1) + 1;
1287*77c1e3ccSAndroid Build Coastguard Worker memset(A, 0, sizeof(A));
1288*77c1e3ccSAndroid Build Coastguard Worker memset(B, 0, sizeof(B));
1289*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_win; i++) {
1290*77c1e3ccSAndroid Build Coastguard Worker const int ii = wrap_index(i, wiener_win);
1291*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < wiener_win; j++) {
1292*77c1e3ccSAndroid Build Coastguard Worker A[ii] += Mc[i][j] * a[j] / WIENER_TAP_SCALE_FACTOR;
1293*77c1e3ccSAndroid Build Coastguard Worker }
1294*77c1e3ccSAndroid Build Coastguard Worker }
1295*77c1e3ccSAndroid Build Coastguard Worker split_wiener_filter_coefficients(wiener_win, a, a1, a2);
1296*77c1e3ccSAndroid Build Coastguard Worker
1297*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_win; i++) {
1298*77c1e3ccSAndroid Build Coastguard Worker const int ii = wrap_index(i, wiener_win);
1299*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < wiener_win; j++) {
1300*77c1e3ccSAndroid Build Coastguard Worker const int jj = wrap_index(j, wiener_win);
1301*77c1e3ccSAndroid Build Coastguard Worker int k, l;
1302*77c1e3ccSAndroid Build Coastguard Worker for (k = 0; k < wiener_win; ++k) {
1303*77c1e3ccSAndroid Build Coastguard Worker for (l = 0; l < wiener_win; ++l) {
1304*77c1e3ccSAndroid Build Coastguard Worker // Calculate
1305*77c1e3ccSAndroid Build Coastguard Worker // B[jj * wiener_halfwin1 + ii] +=
1306*77c1e3ccSAndroid Build Coastguard Worker // Hc[i * wiener_win + j][k * wiener_win2 + l] * a[k] /
1307*77c1e3ccSAndroid Build Coastguard Worker // WIENER_TAP_SCALE_FACTOR * a[l] / WIENER_TAP_SCALE_FACTOR;
1308*77c1e3ccSAndroid Build Coastguard Worker //
1309*77c1e3ccSAndroid Build Coastguard Worker // The last multiplication may overflow, so we combine the last
1310*77c1e3ccSAndroid Build Coastguard Worker // multiplication with the last division.
1311*77c1e3ccSAndroid Build Coastguard Worker const int64_t x = Hc[i * wiener_win + j][k * wiener_win2 + l] * a[k] /
1312*77c1e3ccSAndroid Build Coastguard Worker WIENER_TAP_SCALE_FACTOR;
1313*77c1e3ccSAndroid Build Coastguard Worker // a[l] = a1[l] * WIENER_TAP_SCALE_FACTOR + a2[l]
1314*77c1e3ccSAndroid Build Coastguard Worker B[jj * wiener_halfwin1 + ii] += multiply_and_scale(x, a1[l], a2[l]);
1315*77c1e3ccSAndroid Build Coastguard Worker }
1316*77c1e3ccSAndroid Build Coastguard Worker }
1317*77c1e3ccSAndroid Build Coastguard Worker }
1318*77c1e3ccSAndroid Build Coastguard Worker }
1319*77c1e3ccSAndroid Build Coastguard Worker // Normalization enforcement in the system of equations itself
1320*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_halfwin1 - 1; ++i) {
1321*77c1e3ccSAndroid Build Coastguard Worker A[i] -=
1322*77c1e3ccSAndroid Build Coastguard Worker A[wiener_halfwin1 - 1] * 2 +
1323*77c1e3ccSAndroid Build Coastguard Worker B[i * wiener_halfwin1 + wiener_halfwin1 - 1] -
1324*77c1e3ccSAndroid Build Coastguard Worker 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 + (wiener_halfwin1 - 1)];
1325*77c1e3ccSAndroid Build Coastguard Worker }
1326*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_halfwin1 - 1; ++i) {
1327*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < wiener_halfwin1 - 1; ++j) {
1328*77c1e3ccSAndroid Build Coastguard Worker B[i * wiener_halfwin1 + j] -=
1329*77c1e3ccSAndroid Build Coastguard Worker 2 * (B[i * wiener_halfwin1 + (wiener_halfwin1 - 1)] +
1330*77c1e3ccSAndroid Build Coastguard Worker B[(wiener_halfwin1 - 1) * wiener_halfwin1 + j] -
1331*77c1e3ccSAndroid Build Coastguard Worker 2 * B[(wiener_halfwin1 - 1) * wiener_halfwin1 +
1332*77c1e3ccSAndroid Build Coastguard Worker (wiener_halfwin1 - 1)]);
1333*77c1e3ccSAndroid Build Coastguard Worker }
1334*77c1e3ccSAndroid Build Coastguard Worker }
1335*77c1e3ccSAndroid Build Coastguard Worker if (linsolve_wiener(wiener_halfwin1 - 1, B, wiener_halfwin1, A, S)) {
1336*77c1e3ccSAndroid Build Coastguard Worker S[wiener_halfwin1 - 1] = WIENER_TAP_SCALE_FACTOR;
1337*77c1e3ccSAndroid Build Coastguard Worker for (i = wiener_halfwin1; i < wiener_win; ++i) {
1338*77c1e3ccSAndroid Build Coastguard Worker S[i] = S[wiener_win - 1 - i];
1339*77c1e3ccSAndroid Build Coastguard Worker S[wiener_halfwin1 - 1] -= 2 * S[i];
1340*77c1e3ccSAndroid Build Coastguard Worker }
1341*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_win; ++i) {
1342*77c1e3ccSAndroid Build Coastguard Worker b[i] = (int32_t)CLIP(S[i], -(1 << (WIENER_FILT_BITS - 1)),
1343*77c1e3ccSAndroid Build Coastguard Worker (1 << (WIENER_FILT_BITS - 1)) - 1);
1344*77c1e3ccSAndroid Build Coastguard Worker }
1345*77c1e3ccSAndroid Build Coastguard Worker }
1346*77c1e3ccSAndroid Build Coastguard Worker }
1347*77c1e3ccSAndroid Build Coastguard Worker
wiener_decompose_sep_sym(int wiener_win,int64_t * M,int64_t * H,int32_t * a,int32_t * b)1348*77c1e3ccSAndroid Build Coastguard Worker static void wiener_decompose_sep_sym(int wiener_win, int64_t *M, int64_t *H,
1349*77c1e3ccSAndroid Build Coastguard Worker int32_t *a, int32_t *b) {
1350*77c1e3ccSAndroid Build Coastguard Worker static const int32_t init_filt[WIENER_WIN] = {
1351*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP0_MIDV, WIENER_FILT_TAP1_MIDV, WIENER_FILT_TAP2_MIDV,
1352*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP3_MIDV, WIENER_FILT_TAP2_MIDV, WIENER_FILT_TAP1_MIDV,
1353*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP0_MIDV,
1354*77c1e3ccSAndroid Build Coastguard Worker };
1355*77c1e3ccSAndroid Build Coastguard Worker int64_t *Hc[WIENER_WIN2];
1356*77c1e3ccSAndroid Build Coastguard Worker int64_t *Mc[WIENER_WIN];
1357*77c1e3ccSAndroid Build Coastguard Worker int i, j, iter;
1358*77c1e3ccSAndroid Build Coastguard Worker const int plane_off = (WIENER_WIN - wiener_win) >> 1;
1359*77c1e3ccSAndroid Build Coastguard Worker const int wiener_win2 = wiener_win * wiener_win;
1360*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_win; i++) {
1361*77c1e3ccSAndroid Build Coastguard Worker a[i] = b[i] =
1362*77c1e3ccSAndroid Build Coastguard Worker WIENER_TAP_SCALE_FACTOR / WIENER_FILT_STEP * init_filt[i + plane_off];
1363*77c1e3ccSAndroid Build Coastguard Worker }
1364*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_win; i++) {
1365*77c1e3ccSAndroid Build Coastguard Worker Mc[i] = M + i * wiener_win;
1366*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < wiener_win; j++) {
1367*77c1e3ccSAndroid Build Coastguard Worker Hc[i * wiener_win + j] =
1368*77c1e3ccSAndroid Build Coastguard Worker H + i * wiener_win * wiener_win2 + j * wiener_win;
1369*77c1e3ccSAndroid Build Coastguard Worker }
1370*77c1e3ccSAndroid Build Coastguard Worker }
1371*77c1e3ccSAndroid Build Coastguard Worker
1372*77c1e3ccSAndroid Build Coastguard Worker iter = 1;
1373*77c1e3ccSAndroid Build Coastguard Worker while (iter < NUM_WIENER_ITERS) {
1374*77c1e3ccSAndroid Build Coastguard Worker update_a_sep_sym(wiener_win, Mc, Hc, a, b);
1375*77c1e3ccSAndroid Build Coastguard Worker update_b_sep_sym(wiener_win, Mc, Hc, a, b);
1376*77c1e3ccSAndroid Build Coastguard Worker iter++;
1377*77c1e3ccSAndroid Build Coastguard Worker }
1378*77c1e3ccSAndroid Build Coastguard Worker }
1379*77c1e3ccSAndroid Build Coastguard Worker
1380*77c1e3ccSAndroid Build Coastguard Worker // Computes the function x'*H*x - x'*M for the learned 2D filter x, and compares
1381*77c1e3ccSAndroid Build Coastguard Worker // against identity filters; Final score is defined as the difference between
1382*77c1e3ccSAndroid Build Coastguard Worker // the function values
compute_score(int wiener_win,int64_t * M,int64_t * H,InterpKernel vfilt,InterpKernel hfilt)1383*77c1e3ccSAndroid Build Coastguard Worker static int64_t compute_score(int wiener_win, int64_t *M, int64_t *H,
1384*77c1e3ccSAndroid Build Coastguard Worker InterpKernel vfilt, InterpKernel hfilt) {
1385*77c1e3ccSAndroid Build Coastguard Worker int32_t ab[WIENER_WIN * WIENER_WIN];
1386*77c1e3ccSAndroid Build Coastguard Worker int16_t a[WIENER_WIN], b[WIENER_WIN];
1387*77c1e3ccSAndroid Build Coastguard Worker int64_t P = 0, Q = 0;
1388*77c1e3ccSAndroid Build Coastguard Worker int64_t iP = 0, iQ = 0;
1389*77c1e3ccSAndroid Build Coastguard Worker int64_t Score, iScore;
1390*77c1e3ccSAndroid Build Coastguard Worker int i, k, l;
1391*77c1e3ccSAndroid Build Coastguard Worker const int plane_off = (WIENER_WIN - wiener_win) >> 1;
1392*77c1e3ccSAndroid Build Coastguard Worker const int wiener_win2 = wiener_win * wiener_win;
1393*77c1e3ccSAndroid Build Coastguard Worker
1394*77c1e3ccSAndroid Build Coastguard Worker a[WIENER_HALFWIN] = b[WIENER_HALFWIN] = WIENER_FILT_STEP;
1395*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < WIENER_HALFWIN; ++i) {
1396*77c1e3ccSAndroid Build Coastguard Worker a[i] = a[WIENER_WIN - i - 1] = vfilt[i];
1397*77c1e3ccSAndroid Build Coastguard Worker b[i] = b[WIENER_WIN - i - 1] = hfilt[i];
1398*77c1e3ccSAndroid Build Coastguard Worker a[WIENER_HALFWIN] -= 2 * a[i];
1399*77c1e3ccSAndroid Build Coastguard Worker b[WIENER_HALFWIN] -= 2 * b[i];
1400*77c1e3ccSAndroid Build Coastguard Worker }
1401*77c1e3ccSAndroid Build Coastguard Worker memset(ab, 0, sizeof(ab));
1402*77c1e3ccSAndroid Build Coastguard Worker for (k = 0; k < wiener_win; ++k) {
1403*77c1e3ccSAndroid Build Coastguard Worker for (l = 0; l < wiener_win; ++l)
1404*77c1e3ccSAndroid Build Coastguard Worker ab[k * wiener_win + l] = a[l + plane_off] * b[k + plane_off];
1405*77c1e3ccSAndroid Build Coastguard Worker }
1406*77c1e3ccSAndroid Build Coastguard Worker for (k = 0; k < wiener_win2; ++k) {
1407*77c1e3ccSAndroid Build Coastguard Worker P += ab[k] * M[k] / WIENER_FILT_STEP / WIENER_FILT_STEP;
1408*77c1e3ccSAndroid Build Coastguard Worker for (l = 0; l < wiener_win2; ++l) {
1409*77c1e3ccSAndroid Build Coastguard Worker Q += ab[k] * H[k * wiener_win2 + l] * ab[l] / WIENER_FILT_STEP /
1410*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_STEP / WIENER_FILT_STEP / WIENER_FILT_STEP;
1411*77c1e3ccSAndroid Build Coastguard Worker }
1412*77c1e3ccSAndroid Build Coastguard Worker }
1413*77c1e3ccSAndroid Build Coastguard Worker Score = Q - 2 * P;
1414*77c1e3ccSAndroid Build Coastguard Worker
1415*77c1e3ccSAndroid Build Coastguard Worker iP = M[wiener_win2 >> 1];
1416*77c1e3ccSAndroid Build Coastguard Worker iQ = H[(wiener_win2 >> 1) * wiener_win2 + (wiener_win2 >> 1)];
1417*77c1e3ccSAndroid Build Coastguard Worker iScore = iQ - 2 * iP;
1418*77c1e3ccSAndroid Build Coastguard Worker
1419*77c1e3ccSAndroid Build Coastguard Worker return Score - iScore;
1420*77c1e3ccSAndroid Build Coastguard Worker }
1421*77c1e3ccSAndroid Build Coastguard Worker
finalize_sym_filter(int wiener_win,int32_t * f,InterpKernel fi)1422*77c1e3ccSAndroid Build Coastguard Worker static inline void finalize_sym_filter(int wiener_win, int32_t *f,
1423*77c1e3ccSAndroid Build Coastguard Worker InterpKernel fi) {
1424*77c1e3ccSAndroid Build Coastguard Worker int i;
1425*77c1e3ccSAndroid Build Coastguard Worker const int wiener_halfwin = (wiener_win >> 1);
1426*77c1e3ccSAndroid Build Coastguard Worker
1427*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < wiener_halfwin; ++i) {
1428*77c1e3ccSAndroid Build Coastguard Worker const int64_t dividend = (int64_t)f[i] * WIENER_FILT_STEP;
1429*77c1e3ccSAndroid Build Coastguard Worker const int64_t divisor = WIENER_TAP_SCALE_FACTOR;
1430*77c1e3ccSAndroid Build Coastguard Worker // Perform this division with proper rounding rather than truncation
1431*77c1e3ccSAndroid Build Coastguard Worker if (dividend < 0) {
1432*77c1e3ccSAndroid Build Coastguard Worker fi[i] = (int16_t)((dividend - (divisor / 2)) / divisor);
1433*77c1e3ccSAndroid Build Coastguard Worker } else {
1434*77c1e3ccSAndroid Build Coastguard Worker fi[i] = (int16_t)((dividend + (divisor / 2)) / divisor);
1435*77c1e3ccSAndroid Build Coastguard Worker }
1436*77c1e3ccSAndroid Build Coastguard Worker }
1437*77c1e3ccSAndroid Build Coastguard Worker // Specialize for 7-tap filter
1438*77c1e3ccSAndroid Build Coastguard Worker if (wiener_win == WIENER_WIN) {
1439*77c1e3ccSAndroid Build Coastguard Worker fi[0] = CLIP(fi[0], WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP0_MAXV);
1440*77c1e3ccSAndroid Build Coastguard Worker fi[1] = CLIP(fi[1], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
1441*77c1e3ccSAndroid Build Coastguard Worker fi[2] = CLIP(fi[2], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
1442*77c1e3ccSAndroid Build Coastguard Worker } else {
1443*77c1e3ccSAndroid Build Coastguard Worker fi[2] = CLIP(fi[1], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV);
1444*77c1e3ccSAndroid Build Coastguard Worker fi[1] = CLIP(fi[0], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV);
1445*77c1e3ccSAndroid Build Coastguard Worker fi[0] = 0;
1446*77c1e3ccSAndroid Build Coastguard Worker }
1447*77c1e3ccSAndroid Build Coastguard Worker // Satisfy filter constraints
1448*77c1e3ccSAndroid Build Coastguard Worker fi[WIENER_WIN - 1] = fi[0];
1449*77c1e3ccSAndroid Build Coastguard Worker fi[WIENER_WIN - 2] = fi[1];
1450*77c1e3ccSAndroid Build Coastguard Worker fi[WIENER_WIN - 3] = fi[2];
1451*77c1e3ccSAndroid Build Coastguard Worker // The central element has an implicit +WIENER_FILT_STEP
1452*77c1e3ccSAndroid Build Coastguard Worker fi[3] = -2 * (fi[0] + fi[1] + fi[2]);
1453*77c1e3ccSAndroid Build Coastguard Worker }
1454*77c1e3ccSAndroid Build Coastguard Worker
count_wiener_bits(int wiener_win,WienerInfo * wiener_info,WienerInfo * ref_wiener_info)1455*77c1e3ccSAndroid Build Coastguard Worker static int count_wiener_bits(int wiener_win, WienerInfo *wiener_info,
1456*77c1e3ccSAndroid Build Coastguard Worker WienerInfo *ref_wiener_info) {
1457*77c1e3ccSAndroid Build Coastguard Worker int bits = 0;
1458*77c1e3ccSAndroid Build Coastguard Worker if (wiener_win == WIENER_WIN)
1459*77c1e3ccSAndroid Build Coastguard Worker bits += aom_count_primitive_refsubexpfin(
1460*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1461*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP0_SUBEXP_K,
1462*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV,
1463*77c1e3ccSAndroid Build Coastguard Worker wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV);
1464*77c1e3ccSAndroid Build Coastguard Worker bits += aom_count_primitive_refsubexpfin(
1465*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1466*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP1_SUBEXP_K,
1467*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV,
1468*77c1e3ccSAndroid Build Coastguard Worker wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV);
1469*77c1e3ccSAndroid Build Coastguard Worker bits += aom_count_primitive_refsubexpfin(
1470*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1471*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP2_SUBEXP_K,
1472*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV,
1473*77c1e3ccSAndroid Build Coastguard Worker wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV);
1474*77c1e3ccSAndroid Build Coastguard Worker if (wiener_win == WIENER_WIN)
1475*77c1e3ccSAndroid Build Coastguard Worker bits += aom_count_primitive_refsubexpfin(
1476*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1477*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP0_SUBEXP_K,
1478*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV,
1479*77c1e3ccSAndroid Build Coastguard Worker wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV);
1480*77c1e3ccSAndroid Build Coastguard Worker bits += aom_count_primitive_refsubexpfin(
1481*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1482*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP1_SUBEXP_K,
1483*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV,
1484*77c1e3ccSAndroid Build Coastguard Worker wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV);
1485*77c1e3ccSAndroid Build Coastguard Worker bits += aom_count_primitive_refsubexpfin(
1486*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1487*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP2_SUBEXP_K,
1488*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV,
1489*77c1e3ccSAndroid Build Coastguard Worker wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV);
1490*77c1e3ccSAndroid Build Coastguard Worker return bits;
1491*77c1e3ccSAndroid Build Coastguard Worker }
1492*77c1e3ccSAndroid Build Coastguard Worker
finer_search_wiener(const RestSearchCtxt * rsc,const RestorationTileLimits * limits,RestorationUnitInfo * rui,int wiener_win)1493*77c1e3ccSAndroid Build Coastguard Worker static int64_t finer_search_wiener(const RestSearchCtxt *rsc,
1494*77c1e3ccSAndroid Build Coastguard Worker const RestorationTileLimits *limits,
1495*77c1e3ccSAndroid Build Coastguard Worker RestorationUnitInfo *rui, int wiener_win) {
1496*77c1e3ccSAndroid Build Coastguard Worker const int plane_off = (WIENER_WIN - wiener_win) >> 1;
1497*77c1e3ccSAndroid Build Coastguard Worker int64_t err = try_restoration_unit(rsc, limits, rui);
1498*77c1e3ccSAndroid Build Coastguard Worker
1499*77c1e3ccSAndroid Build Coastguard Worker if (rsc->lpf_sf->disable_wiener_coeff_refine_search) return err;
1500*77c1e3ccSAndroid Build Coastguard Worker
1501*77c1e3ccSAndroid Build Coastguard Worker // Refinement search around the wiener filter coefficients.
1502*77c1e3ccSAndroid Build Coastguard Worker int64_t err2;
1503*77c1e3ccSAndroid Build Coastguard Worker int tap_min[] = { WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP1_MINV,
1504*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP2_MINV };
1505*77c1e3ccSAndroid Build Coastguard Worker int tap_max[] = { WIENER_FILT_TAP0_MAXV, WIENER_FILT_TAP1_MAXV,
1506*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP2_MAXV };
1507*77c1e3ccSAndroid Build Coastguard Worker
1508*77c1e3ccSAndroid Build Coastguard Worker WienerInfo *plane_wiener = &rui->wiener_info;
1509*77c1e3ccSAndroid Build Coastguard Worker
1510*77c1e3ccSAndroid Build Coastguard Worker // printf("err pre = %"PRId64"\n", err);
1511*77c1e3ccSAndroid Build Coastguard Worker const int start_step = 4;
1512*77c1e3ccSAndroid Build Coastguard Worker for (int s = start_step; s >= 1; s >>= 1) {
1513*77c1e3ccSAndroid Build Coastguard Worker for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
1514*77c1e3ccSAndroid Build Coastguard Worker int skip = 0;
1515*77c1e3ccSAndroid Build Coastguard Worker do {
1516*77c1e3ccSAndroid Build Coastguard Worker if (plane_wiener->hfilter[p] - s >= tap_min[p]) {
1517*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[p] -= s;
1518*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
1519*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
1520*77c1e3ccSAndroid Build Coastguard Worker err2 = try_restoration_unit(rsc, limits, rui);
1521*77c1e3ccSAndroid Build Coastguard Worker if (err2 > err) {
1522*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[p] += s;
1523*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
1524*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
1525*77c1e3ccSAndroid Build Coastguard Worker } else {
1526*77c1e3ccSAndroid Build Coastguard Worker err = err2;
1527*77c1e3ccSAndroid Build Coastguard Worker skip = 1;
1528*77c1e3ccSAndroid Build Coastguard Worker // At the highest step size continue moving in the same direction
1529*77c1e3ccSAndroid Build Coastguard Worker if (s == start_step) continue;
1530*77c1e3ccSAndroid Build Coastguard Worker }
1531*77c1e3ccSAndroid Build Coastguard Worker }
1532*77c1e3ccSAndroid Build Coastguard Worker break;
1533*77c1e3ccSAndroid Build Coastguard Worker } while (1);
1534*77c1e3ccSAndroid Build Coastguard Worker if (skip) break;
1535*77c1e3ccSAndroid Build Coastguard Worker do {
1536*77c1e3ccSAndroid Build Coastguard Worker if (plane_wiener->hfilter[p] + s <= tap_max[p]) {
1537*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[p] += s;
1538*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[WIENER_WIN - p - 1] += s;
1539*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[WIENER_HALFWIN] -= 2 * s;
1540*77c1e3ccSAndroid Build Coastguard Worker err2 = try_restoration_unit(rsc, limits, rui);
1541*77c1e3ccSAndroid Build Coastguard Worker if (err2 > err) {
1542*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[p] -= s;
1543*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[WIENER_WIN - p - 1] -= s;
1544*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->hfilter[WIENER_HALFWIN] += 2 * s;
1545*77c1e3ccSAndroid Build Coastguard Worker } else {
1546*77c1e3ccSAndroid Build Coastguard Worker err = err2;
1547*77c1e3ccSAndroid Build Coastguard Worker // At the highest step size continue moving in the same direction
1548*77c1e3ccSAndroid Build Coastguard Worker if (s == start_step) continue;
1549*77c1e3ccSAndroid Build Coastguard Worker }
1550*77c1e3ccSAndroid Build Coastguard Worker }
1551*77c1e3ccSAndroid Build Coastguard Worker break;
1552*77c1e3ccSAndroid Build Coastguard Worker } while (1);
1553*77c1e3ccSAndroid Build Coastguard Worker }
1554*77c1e3ccSAndroid Build Coastguard Worker for (int p = plane_off; p < WIENER_HALFWIN; ++p) {
1555*77c1e3ccSAndroid Build Coastguard Worker int skip = 0;
1556*77c1e3ccSAndroid Build Coastguard Worker do {
1557*77c1e3ccSAndroid Build Coastguard Worker if (plane_wiener->vfilter[p] - s >= tap_min[p]) {
1558*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[p] -= s;
1559*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
1560*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
1561*77c1e3ccSAndroid Build Coastguard Worker err2 = try_restoration_unit(rsc, limits, rui);
1562*77c1e3ccSAndroid Build Coastguard Worker if (err2 > err) {
1563*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[p] += s;
1564*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
1565*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
1566*77c1e3ccSAndroid Build Coastguard Worker } else {
1567*77c1e3ccSAndroid Build Coastguard Worker err = err2;
1568*77c1e3ccSAndroid Build Coastguard Worker skip = 1;
1569*77c1e3ccSAndroid Build Coastguard Worker // At the highest step size continue moving in the same direction
1570*77c1e3ccSAndroid Build Coastguard Worker if (s == start_step) continue;
1571*77c1e3ccSAndroid Build Coastguard Worker }
1572*77c1e3ccSAndroid Build Coastguard Worker }
1573*77c1e3ccSAndroid Build Coastguard Worker break;
1574*77c1e3ccSAndroid Build Coastguard Worker } while (1);
1575*77c1e3ccSAndroid Build Coastguard Worker if (skip) break;
1576*77c1e3ccSAndroid Build Coastguard Worker do {
1577*77c1e3ccSAndroid Build Coastguard Worker if (plane_wiener->vfilter[p] + s <= tap_max[p]) {
1578*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[p] += s;
1579*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[WIENER_WIN - p - 1] += s;
1580*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[WIENER_HALFWIN] -= 2 * s;
1581*77c1e3ccSAndroid Build Coastguard Worker err2 = try_restoration_unit(rsc, limits, rui);
1582*77c1e3ccSAndroid Build Coastguard Worker if (err2 > err) {
1583*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[p] -= s;
1584*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[WIENER_WIN - p - 1] -= s;
1585*77c1e3ccSAndroid Build Coastguard Worker plane_wiener->vfilter[WIENER_HALFWIN] += 2 * s;
1586*77c1e3ccSAndroid Build Coastguard Worker } else {
1587*77c1e3ccSAndroid Build Coastguard Worker err = err2;
1588*77c1e3ccSAndroid Build Coastguard Worker // At the highest step size continue moving in the same direction
1589*77c1e3ccSAndroid Build Coastguard Worker if (s == start_step) continue;
1590*77c1e3ccSAndroid Build Coastguard Worker }
1591*77c1e3ccSAndroid Build Coastguard Worker }
1592*77c1e3ccSAndroid Build Coastguard Worker break;
1593*77c1e3ccSAndroid Build Coastguard Worker } while (1);
1594*77c1e3ccSAndroid Build Coastguard Worker }
1595*77c1e3ccSAndroid Build Coastguard Worker }
1596*77c1e3ccSAndroid Build Coastguard Worker // printf("err post = %"PRId64"\n", err);
1597*77c1e3ccSAndroid Build Coastguard Worker return err;
1598*77c1e3ccSAndroid Build Coastguard Worker }
1599*77c1e3ccSAndroid Build Coastguard Worker
search_wiener(const RestorationTileLimits * limits,int rest_unit_idx,void * priv,int32_t * tmpbuf,RestorationLineBuffers * rlbs,struct aom_internal_error_info * error_info)1600*77c1e3ccSAndroid Build Coastguard Worker static inline void search_wiener(const RestorationTileLimits *limits,
1601*77c1e3ccSAndroid Build Coastguard Worker int rest_unit_idx, void *priv, int32_t *tmpbuf,
1602*77c1e3ccSAndroid Build Coastguard Worker RestorationLineBuffers *rlbs,
1603*77c1e3ccSAndroid Build Coastguard Worker struct aom_internal_error_info *error_info) {
1604*77c1e3ccSAndroid Build Coastguard Worker (void)tmpbuf;
1605*77c1e3ccSAndroid Build Coastguard Worker (void)rlbs;
1606*77c1e3ccSAndroid Build Coastguard Worker (void)error_info;
1607*77c1e3ccSAndroid Build Coastguard Worker RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1608*77c1e3ccSAndroid Build Coastguard Worker RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1609*77c1e3ccSAndroid Build Coastguard Worker
1610*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCK *const x = rsc->x;
1611*77c1e3ccSAndroid Build Coastguard Worker const int64_t bits_none = x->mode_costs.wiener_restore_cost[0];
1612*77c1e3ccSAndroid Build Coastguard Worker
1613*77c1e3ccSAndroid Build Coastguard Worker // Skip Wiener search for low variance contents
1614*77c1e3ccSAndroid Build Coastguard Worker if (rsc->lpf_sf->prune_wiener_based_on_src_var) {
1615*77c1e3ccSAndroid Build Coastguard Worker const int scale[3] = { 0, 1, 2 };
1616*77c1e3ccSAndroid Build Coastguard Worker // Obtain the normalized Qscale
1617*77c1e3ccSAndroid Build Coastguard Worker const int qs = av1_dc_quant_QTX(rsc->cm->quant_params.base_qindex, 0,
1618*77c1e3ccSAndroid Build Coastguard Worker rsc->cm->seq_params->bit_depth) >>
1619*77c1e3ccSAndroid Build Coastguard Worker 3;
1620*77c1e3ccSAndroid Build Coastguard Worker // Derive threshold as sqr(normalized Qscale) * scale / 16,
1621*77c1e3ccSAndroid Build Coastguard Worker const uint64_t thresh =
1622*77c1e3ccSAndroid Build Coastguard Worker (qs * qs * scale[rsc->lpf_sf->prune_wiener_based_on_src_var]) >> 4;
1623*77c1e3ccSAndroid Build Coastguard Worker const int highbd = rsc->cm->seq_params->use_highbitdepth;
1624*77c1e3ccSAndroid Build Coastguard Worker const uint64_t src_var =
1625*77c1e3ccSAndroid Build Coastguard Worker var_restoration_unit(limits, rsc->src, rsc->plane, highbd);
1626*77c1e3ccSAndroid Build Coastguard Worker // Do not perform Wiener search if source variance is lower than threshold
1627*77c1e3ccSAndroid Build Coastguard Worker // or if the reconstruction error is zero
1628*77c1e3ccSAndroid Build Coastguard Worker int prune_wiener = (src_var < thresh) || (rsc->sse[RESTORE_NONE] == 0);
1629*77c1e3ccSAndroid Build Coastguard Worker if (prune_wiener) {
1630*77c1e3ccSAndroid Build Coastguard Worker rsc->total_bits[RESTORE_WIENER] += bits_none;
1631*77c1e3ccSAndroid Build Coastguard Worker rsc->total_sse[RESTORE_WIENER] += rsc->sse[RESTORE_NONE];
1632*77c1e3ccSAndroid Build Coastguard Worker rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1633*77c1e3ccSAndroid Build Coastguard Worker rsc->sse[RESTORE_WIENER] = INT64_MAX;
1634*77c1e3ccSAndroid Build Coastguard Worker if (rsc->lpf_sf->prune_sgr_based_on_wiener == 2) rsc->skip_sgr_eval = 1;
1635*77c1e3ccSAndroid Build Coastguard Worker return;
1636*77c1e3ccSAndroid Build Coastguard Worker }
1637*77c1e3ccSAndroid Build Coastguard Worker }
1638*77c1e3ccSAndroid Build Coastguard Worker
1639*77c1e3ccSAndroid Build Coastguard Worker const int wiener_win =
1640*77c1e3ccSAndroid Build Coastguard Worker (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
1641*77c1e3ccSAndroid Build Coastguard Worker
1642*77c1e3ccSAndroid Build Coastguard Worker int reduced_wiener_win = wiener_win;
1643*77c1e3ccSAndroid Build Coastguard Worker if (rsc->lpf_sf->reduce_wiener_window_size) {
1644*77c1e3ccSAndroid Build Coastguard Worker reduced_wiener_win =
1645*77c1e3ccSAndroid Build Coastguard Worker (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN_REDUCED : WIENER_WIN_CHROMA;
1646*77c1e3ccSAndroid Build Coastguard Worker }
1647*77c1e3ccSAndroid Build Coastguard Worker
1648*77c1e3ccSAndroid Build Coastguard Worker int64_t M[WIENER_WIN2];
1649*77c1e3ccSAndroid Build Coastguard Worker int64_t H[WIENER_WIN2 * WIENER_WIN2];
1650*77c1e3ccSAndroid Build Coastguard Worker int32_t vfilter[WIENER_WIN], hfilter[WIENER_WIN];
1651*77c1e3ccSAndroid Build Coastguard Worker
1652*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
1653*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = rsc->cm;
1654*77c1e3ccSAndroid Build Coastguard Worker if (cm->seq_params->use_highbitdepth) {
1655*77c1e3ccSAndroid Build Coastguard Worker // TODO(any) : Add support for use_downsampled_wiener_stats SF in HBD
1656*77c1e3ccSAndroid Build Coastguard Worker // functions. Optimize intrinsics of HBD design similar to LBD (i.e.,
1657*77c1e3ccSAndroid Build Coastguard Worker // pre-calculate d and s buffers and avoid most of the C operations).
1658*77c1e3ccSAndroid Build Coastguard Worker av1_compute_stats_highbd(reduced_wiener_win, rsc->dgd_buffer,
1659*77c1e3ccSAndroid Build Coastguard Worker rsc->src_buffer, rsc->dgd_avg, rsc->src_avg,
1660*77c1e3ccSAndroid Build Coastguard Worker limits->h_start, limits->h_end, limits->v_start,
1661*77c1e3ccSAndroid Build Coastguard Worker limits->v_end, rsc->dgd_stride, rsc->src_stride, M,
1662*77c1e3ccSAndroid Build Coastguard Worker H, cm->seq_params->bit_depth);
1663*77c1e3ccSAndroid Build Coastguard Worker } else {
1664*77c1e3ccSAndroid Build Coastguard Worker av1_compute_stats(reduced_wiener_win, rsc->dgd_buffer, rsc->src_buffer,
1665*77c1e3ccSAndroid Build Coastguard Worker rsc->dgd_avg, rsc->src_avg, limits->h_start,
1666*77c1e3ccSAndroid Build Coastguard Worker limits->h_end, limits->v_start, limits->v_end,
1667*77c1e3ccSAndroid Build Coastguard Worker rsc->dgd_stride, rsc->src_stride, M, H,
1668*77c1e3ccSAndroid Build Coastguard Worker rsc->lpf_sf->use_downsampled_wiener_stats);
1669*77c1e3ccSAndroid Build Coastguard Worker }
1670*77c1e3ccSAndroid Build Coastguard Worker #else
1671*77c1e3ccSAndroid Build Coastguard Worker av1_compute_stats(reduced_wiener_win, rsc->dgd_buffer, rsc->src_buffer,
1672*77c1e3ccSAndroid Build Coastguard Worker rsc->dgd_avg, rsc->src_avg, limits->h_start, limits->h_end,
1673*77c1e3ccSAndroid Build Coastguard Worker limits->v_start, limits->v_end, rsc->dgd_stride,
1674*77c1e3ccSAndroid Build Coastguard Worker rsc->src_stride, M, H,
1675*77c1e3ccSAndroid Build Coastguard Worker rsc->lpf_sf->use_downsampled_wiener_stats);
1676*77c1e3ccSAndroid Build Coastguard Worker #endif
1677*77c1e3ccSAndroid Build Coastguard Worker
1678*77c1e3ccSAndroid Build Coastguard Worker wiener_decompose_sep_sym(reduced_wiener_win, M, H, vfilter, hfilter);
1679*77c1e3ccSAndroid Build Coastguard Worker
1680*77c1e3ccSAndroid Build Coastguard Worker RestorationUnitInfo rui;
1681*77c1e3ccSAndroid Build Coastguard Worker memset(&rui, 0, sizeof(rui));
1682*77c1e3ccSAndroid Build Coastguard Worker rui.restoration_type = RESTORE_WIENER;
1683*77c1e3ccSAndroid Build Coastguard Worker finalize_sym_filter(reduced_wiener_win, vfilter, rui.wiener_info.vfilter);
1684*77c1e3ccSAndroid Build Coastguard Worker finalize_sym_filter(reduced_wiener_win, hfilter, rui.wiener_info.hfilter);
1685*77c1e3ccSAndroid Build Coastguard Worker
1686*77c1e3ccSAndroid Build Coastguard Worker // Filter score computes the value of the function x'*A*x - x'*b for the
1687*77c1e3ccSAndroid Build Coastguard Worker // learned filter and compares it against identity filer. If there is no
1688*77c1e3ccSAndroid Build Coastguard Worker // reduction in the function, the filter is reverted back to identity
1689*77c1e3ccSAndroid Build Coastguard Worker if (compute_score(reduced_wiener_win, M, H, rui.wiener_info.vfilter,
1690*77c1e3ccSAndroid Build Coastguard Worker rui.wiener_info.hfilter) > 0) {
1691*77c1e3ccSAndroid Build Coastguard Worker rsc->total_bits[RESTORE_WIENER] += bits_none;
1692*77c1e3ccSAndroid Build Coastguard Worker rsc->total_sse[RESTORE_WIENER] += rsc->sse[RESTORE_NONE];
1693*77c1e3ccSAndroid Build Coastguard Worker rusi->best_rtype[RESTORE_WIENER - 1] = RESTORE_NONE;
1694*77c1e3ccSAndroid Build Coastguard Worker rsc->sse[RESTORE_WIENER] = INT64_MAX;
1695*77c1e3ccSAndroid Build Coastguard Worker if (rsc->lpf_sf->prune_sgr_based_on_wiener == 2) rsc->skip_sgr_eval = 1;
1696*77c1e3ccSAndroid Build Coastguard Worker return;
1697*77c1e3ccSAndroid Build Coastguard Worker }
1698*77c1e3ccSAndroid Build Coastguard Worker
1699*77c1e3ccSAndroid Build Coastguard Worker rsc->sse[RESTORE_WIENER] =
1700*77c1e3ccSAndroid Build Coastguard Worker finer_search_wiener(rsc, limits, &rui, reduced_wiener_win);
1701*77c1e3ccSAndroid Build Coastguard Worker rusi->wiener = rui.wiener_info;
1702*77c1e3ccSAndroid Build Coastguard Worker
1703*77c1e3ccSAndroid Build Coastguard Worker if (reduced_wiener_win != WIENER_WIN) {
1704*77c1e3ccSAndroid Build Coastguard Worker assert(rui.wiener_info.vfilter[0] == 0 &&
1705*77c1e3ccSAndroid Build Coastguard Worker rui.wiener_info.vfilter[WIENER_WIN - 1] == 0);
1706*77c1e3ccSAndroid Build Coastguard Worker assert(rui.wiener_info.hfilter[0] == 0 &&
1707*77c1e3ccSAndroid Build Coastguard Worker rui.wiener_info.hfilter[WIENER_WIN - 1] == 0);
1708*77c1e3ccSAndroid Build Coastguard Worker }
1709*77c1e3ccSAndroid Build Coastguard Worker
1710*77c1e3ccSAndroid Build Coastguard Worker const int64_t bits_wiener =
1711*77c1e3ccSAndroid Build Coastguard Worker x->mode_costs.wiener_restore_cost[1] +
1712*77c1e3ccSAndroid Build Coastguard Worker (count_wiener_bits(wiener_win, &rusi->wiener, &rsc->ref_wiener)
1713*77c1e3ccSAndroid Build Coastguard Worker << AV1_PROB_COST_SHIFT);
1714*77c1e3ccSAndroid Build Coastguard Worker
1715*77c1e3ccSAndroid Build Coastguard Worker double cost_none = RDCOST_DBL_WITH_NATIVE_BD_DIST(
1716*77c1e3ccSAndroid Build Coastguard Worker x->rdmult, bits_none >> 4, rsc->sse[RESTORE_NONE],
1717*77c1e3ccSAndroid Build Coastguard Worker rsc->cm->seq_params->bit_depth);
1718*77c1e3ccSAndroid Build Coastguard Worker double cost_wiener = RDCOST_DBL_WITH_NATIVE_BD_DIST(
1719*77c1e3ccSAndroid Build Coastguard Worker x->rdmult, bits_wiener >> 4, rsc->sse[RESTORE_WIENER],
1720*77c1e3ccSAndroid Build Coastguard Worker rsc->cm->seq_params->bit_depth);
1721*77c1e3ccSAndroid Build Coastguard Worker
1722*77c1e3ccSAndroid Build Coastguard Worker RestorationType rtype =
1723*77c1e3ccSAndroid Build Coastguard Worker (cost_wiener < cost_none) ? RESTORE_WIENER : RESTORE_NONE;
1724*77c1e3ccSAndroid Build Coastguard Worker rusi->best_rtype[RESTORE_WIENER - 1] = rtype;
1725*77c1e3ccSAndroid Build Coastguard Worker
1726*77c1e3ccSAndroid Build Coastguard Worker // Set 'skip_sgr_eval' based on rdcost ratio of RESTORE_WIENER and
1727*77c1e3ccSAndroid Build Coastguard Worker // RESTORE_NONE or based on best_rtype
1728*77c1e3ccSAndroid Build Coastguard Worker if (rsc->lpf_sf->prune_sgr_based_on_wiener == 1) {
1729*77c1e3ccSAndroid Build Coastguard Worker rsc->skip_sgr_eval = cost_wiener > (1.01 * cost_none);
1730*77c1e3ccSAndroid Build Coastguard Worker } else if (rsc->lpf_sf->prune_sgr_based_on_wiener == 2) {
1731*77c1e3ccSAndroid Build Coastguard Worker rsc->skip_sgr_eval = rusi->best_rtype[RESTORE_WIENER - 1] == RESTORE_NONE;
1732*77c1e3ccSAndroid Build Coastguard Worker }
1733*77c1e3ccSAndroid Build Coastguard Worker
1734*77c1e3ccSAndroid Build Coastguard Worker #if DEBUG_LR_COSTING
1735*77c1e3ccSAndroid Build Coastguard Worker // Store ref params for later checking
1736*77c1e3ccSAndroid Build Coastguard Worker lr_ref_params[RESTORE_WIENER][rsc->plane][rest_unit_idx].wiener_info =
1737*77c1e3ccSAndroid Build Coastguard Worker rsc->ref_wiener;
1738*77c1e3ccSAndroid Build Coastguard Worker #endif // DEBUG_LR_COSTING
1739*77c1e3ccSAndroid Build Coastguard Worker
1740*77c1e3ccSAndroid Build Coastguard Worker rsc->total_sse[RESTORE_WIENER] += rsc->sse[rtype];
1741*77c1e3ccSAndroid Build Coastguard Worker rsc->total_bits[RESTORE_WIENER] +=
1742*77c1e3ccSAndroid Build Coastguard Worker (cost_wiener < cost_none) ? bits_wiener : bits_none;
1743*77c1e3ccSAndroid Build Coastguard Worker if (cost_wiener < cost_none) rsc->ref_wiener = rusi->wiener;
1744*77c1e3ccSAndroid Build Coastguard Worker }
1745*77c1e3ccSAndroid Build Coastguard Worker
search_norestore(const RestorationTileLimits * limits,int rest_unit_idx,void * priv,int32_t * tmpbuf,RestorationLineBuffers * rlbs,struct aom_internal_error_info * error_info)1746*77c1e3ccSAndroid Build Coastguard Worker static inline void search_norestore(
1747*77c1e3ccSAndroid Build Coastguard Worker const RestorationTileLimits *limits, int rest_unit_idx, void *priv,
1748*77c1e3ccSAndroid Build Coastguard Worker int32_t *tmpbuf, RestorationLineBuffers *rlbs,
1749*77c1e3ccSAndroid Build Coastguard Worker struct aom_internal_error_info *error_info) {
1750*77c1e3ccSAndroid Build Coastguard Worker (void)rest_unit_idx;
1751*77c1e3ccSAndroid Build Coastguard Worker (void)tmpbuf;
1752*77c1e3ccSAndroid Build Coastguard Worker (void)rlbs;
1753*77c1e3ccSAndroid Build Coastguard Worker (void)error_info;
1754*77c1e3ccSAndroid Build Coastguard Worker
1755*77c1e3ccSAndroid Build Coastguard Worker RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1756*77c1e3ccSAndroid Build Coastguard Worker
1757*77c1e3ccSAndroid Build Coastguard Worker const int highbd = rsc->cm->seq_params->use_highbitdepth;
1758*77c1e3ccSAndroid Build Coastguard Worker rsc->sse[RESTORE_NONE] = sse_restoration_unit(
1759*77c1e3ccSAndroid Build Coastguard Worker limits, rsc->src, &rsc->cm->cur_frame->buf, rsc->plane, highbd);
1760*77c1e3ccSAndroid Build Coastguard Worker
1761*77c1e3ccSAndroid Build Coastguard Worker rsc->total_sse[RESTORE_NONE] += rsc->sse[RESTORE_NONE];
1762*77c1e3ccSAndroid Build Coastguard Worker }
1763*77c1e3ccSAndroid Build Coastguard Worker
search_switchable(const RestorationTileLimits * limits,int rest_unit_idx,void * priv,int32_t * tmpbuf,RestorationLineBuffers * rlbs,struct aom_internal_error_info * error_info)1764*77c1e3ccSAndroid Build Coastguard Worker static inline void search_switchable(
1765*77c1e3ccSAndroid Build Coastguard Worker const RestorationTileLimits *limits, int rest_unit_idx, void *priv,
1766*77c1e3ccSAndroid Build Coastguard Worker int32_t *tmpbuf, RestorationLineBuffers *rlbs,
1767*77c1e3ccSAndroid Build Coastguard Worker struct aom_internal_error_info *error_info) {
1768*77c1e3ccSAndroid Build Coastguard Worker (void)limits;
1769*77c1e3ccSAndroid Build Coastguard Worker (void)tmpbuf;
1770*77c1e3ccSAndroid Build Coastguard Worker (void)rlbs;
1771*77c1e3ccSAndroid Build Coastguard Worker (void)error_info;
1772*77c1e3ccSAndroid Build Coastguard Worker RestSearchCtxt *rsc = (RestSearchCtxt *)priv;
1773*77c1e3ccSAndroid Build Coastguard Worker RestUnitSearchInfo *rusi = &rsc->rusi[rest_unit_idx];
1774*77c1e3ccSAndroid Build Coastguard Worker
1775*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCK *const x = rsc->x;
1776*77c1e3ccSAndroid Build Coastguard Worker
1777*77c1e3ccSAndroid Build Coastguard Worker const int wiener_win =
1778*77c1e3ccSAndroid Build Coastguard Worker (rsc->plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
1779*77c1e3ccSAndroid Build Coastguard Worker
1780*77c1e3ccSAndroid Build Coastguard Worker double best_cost = 0;
1781*77c1e3ccSAndroid Build Coastguard Worker int64_t best_bits = 0;
1782*77c1e3ccSAndroid Build Coastguard Worker RestorationType best_rtype = RESTORE_NONE;
1783*77c1e3ccSAndroid Build Coastguard Worker
1784*77c1e3ccSAndroid Build Coastguard Worker for (RestorationType r = 0; r < RESTORE_SWITCHABLE_TYPES; ++r) {
1785*77c1e3ccSAndroid Build Coastguard Worker // If this restoration mode was skipped, or could not find a solution
1786*77c1e3ccSAndroid Build Coastguard Worker // that was better than RESTORE_NONE, then we can't select it here either.
1787*77c1e3ccSAndroid Build Coastguard Worker //
1788*77c1e3ccSAndroid Build Coastguard Worker // Note: It is possible for the restoration search functions to find a
1789*77c1e3ccSAndroid Build Coastguard Worker // filter which is better than RESTORE_NONE when looking purely at SSE, but
1790*77c1e3ccSAndroid Build Coastguard Worker // for it to be rejected overall due to its rate cost. In this case, there
1791*77c1e3ccSAndroid Build Coastguard Worker // is a chance that it may be have a lower rate cost when looking at
1792*77c1e3ccSAndroid Build Coastguard Worker // RESTORE_SWITCHABLE, and so it might be acceptable here.
1793*77c1e3ccSAndroid Build Coastguard Worker //
1794*77c1e3ccSAndroid Build Coastguard Worker // Therefore we prune based on SSE, rather than on whether or not the
1795*77c1e3ccSAndroid Build Coastguard Worker // previous search function selected this mode.
1796*77c1e3ccSAndroid Build Coastguard Worker if (r > RESTORE_NONE) {
1797*77c1e3ccSAndroid Build Coastguard Worker if (rsc->sse[r] > rsc->sse[RESTORE_NONE]) continue;
1798*77c1e3ccSAndroid Build Coastguard Worker }
1799*77c1e3ccSAndroid Build Coastguard Worker
1800*77c1e3ccSAndroid Build Coastguard Worker const int64_t sse = rsc->sse[r];
1801*77c1e3ccSAndroid Build Coastguard Worker int64_t coeff_pcost = 0;
1802*77c1e3ccSAndroid Build Coastguard Worker switch (r) {
1803*77c1e3ccSAndroid Build Coastguard Worker case RESTORE_NONE: coeff_pcost = 0; break;
1804*77c1e3ccSAndroid Build Coastguard Worker case RESTORE_WIENER:
1805*77c1e3ccSAndroid Build Coastguard Worker coeff_pcost = count_wiener_bits(wiener_win, &rusi->wiener,
1806*77c1e3ccSAndroid Build Coastguard Worker &rsc->switchable_ref_wiener);
1807*77c1e3ccSAndroid Build Coastguard Worker break;
1808*77c1e3ccSAndroid Build Coastguard Worker case RESTORE_SGRPROJ:
1809*77c1e3ccSAndroid Build Coastguard Worker coeff_pcost =
1810*77c1e3ccSAndroid Build Coastguard Worker count_sgrproj_bits(&rusi->sgrproj, &rsc->switchable_ref_sgrproj);
1811*77c1e3ccSAndroid Build Coastguard Worker break;
1812*77c1e3ccSAndroid Build Coastguard Worker default: assert(0); break;
1813*77c1e3ccSAndroid Build Coastguard Worker }
1814*77c1e3ccSAndroid Build Coastguard Worker const int64_t coeff_bits = coeff_pcost << AV1_PROB_COST_SHIFT;
1815*77c1e3ccSAndroid Build Coastguard Worker const int64_t bits = x->mode_costs.switchable_restore_cost[r] + coeff_bits;
1816*77c1e3ccSAndroid Build Coastguard Worker double cost = RDCOST_DBL_WITH_NATIVE_BD_DIST(
1817*77c1e3ccSAndroid Build Coastguard Worker x->rdmult, bits >> 4, sse, rsc->cm->seq_params->bit_depth);
1818*77c1e3ccSAndroid Build Coastguard Worker if (r == RESTORE_SGRPROJ && rusi->sgrproj.ep < 10)
1819*77c1e3ccSAndroid Build Coastguard Worker cost *= (1 + DUAL_SGR_PENALTY_MULT * rsc->lpf_sf->dual_sgr_penalty_level);
1820*77c1e3ccSAndroid Build Coastguard Worker if (r == 0 || cost < best_cost) {
1821*77c1e3ccSAndroid Build Coastguard Worker best_cost = cost;
1822*77c1e3ccSAndroid Build Coastguard Worker best_bits = bits;
1823*77c1e3ccSAndroid Build Coastguard Worker best_rtype = r;
1824*77c1e3ccSAndroid Build Coastguard Worker }
1825*77c1e3ccSAndroid Build Coastguard Worker }
1826*77c1e3ccSAndroid Build Coastguard Worker
1827*77c1e3ccSAndroid Build Coastguard Worker rusi->best_rtype[RESTORE_SWITCHABLE - 1] = best_rtype;
1828*77c1e3ccSAndroid Build Coastguard Worker
1829*77c1e3ccSAndroid Build Coastguard Worker #if DEBUG_LR_COSTING
1830*77c1e3ccSAndroid Build Coastguard Worker // Store ref params for later checking
1831*77c1e3ccSAndroid Build Coastguard Worker lr_ref_params[RESTORE_SWITCHABLE][rsc->plane][rest_unit_idx].wiener_info =
1832*77c1e3ccSAndroid Build Coastguard Worker rsc->switchable_ref_wiener;
1833*77c1e3ccSAndroid Build Coastguard Worker lr_ref_params[RESTORE_SWITCHABLE][rsc->plane][rest_unit_idx].sgrproj_info =
1834*77c1e3ccSAndroid Build Coastguard Worker rsc->switchable_ref_sgrproj;
1835*77c1e3ccSAndroid Build Coastguard Worker #endif // DEBUG_LR_COSTING
1836*77c1e3ccSAndroid Build Coastguard Worker
1837*77c1e3ccSAndroid Build Coastguard Worker rsc->total_sse[RESTORE_SWITCHABLE] += rsc->sse[best_rtype];
1838*77c1e3ccSAndroid Build Coastguard Worker rsc->total_bits[RESTORE_SWITCHABLE] += best_bits;
1839*77c1e3ccSAndroid Build Coastguard Worker if (best_rtype == RESTORE_WIENER) rsc->switchable_ref_wiener = rusi->wiener;
1840*77c1e3ccSAndroid Build Coastguard Worker if (best_rtype == RESTORE_SGRPROJ)
1841*77c1e3ccSAndroid Build Coastguard Worker rsc->switchable_ref_sgrproj = rusi->sgrproj;
1842*77c1e3ccSAndroid Build Coastguard Worker }
1843*77c1e3ccSAndroid Build Coastguard Worker
copy_unit_info(RestorationType frame_rtype,const RestUnitSearchInfo * rusi,RestorationUnitInfo * rui)1844*77c1e3ccSAndroid Build Coastguard Worker static inline void copy_unit_info(RestorationType frame_rtype,
1845*77c1e3ccSAndroid Build Coastguard Worker const RestUnitSearchInfo *rusi,
1846*77c1e3ccSAndroid Build Coastguard Worker RestorationUnitInfo *rui) {
1847*77c1e3ccSAndroid Build Coastguard Worker assert(frame_rtype > 0);
1848*77c1e3ccSAndroid Build Coastguard Worker rui->restoration_type = rusi->best_rtype[frame_rtype - 1];
1849*77c1e3ccSAndroid Build Coastguard Worker if (rui->restoration_type == RESTORE_WIENER)
1850*77c1e3ccSAndroid Build Coastguard Worker rui->wiener_info = rusi->wiener;
1851*77c1e3ccSAndroid Build Coastguard Worker else
1852*77c1e3ccSAndroid Build Coastguard Worker rui->sgrproj_info = rusi->sgrproj;
1853*77c1e3ccSAndroid Build Coastguard Worker }
1854*77c1e3ccSAndroid Build Coastguard Worker
restoration_search(AV1_COMMON * cm,int plane,RestSearchCtxt * rsc,bool * disable_lr_filter)1855*77c1e3ccSAndroid Build Coastguard Worker static void restoration_search(AV1_COMMON *cm, int plane, RestSearchCtxt *rsc,
1856*77c1e3ccSAndroid Build Coastguard Worker bool *disable_lr_filter) {
1857*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
1858*77c1e3ccSAndroid Build Coastguard Worker const int mib_size_log2 = cm->seq_params->mib_size_log2;
1859*77c1e3ccSAndroid Build Coastguard Worker const CommonTileParams *tiles = &cm->tiles;
1860*77c1e3ccSAndroid Build Coastguard Worker const int is_uv = plane > 0;
1861*77c1e3ccSAndroid Build Coastguard Worker const int ss_y = is_uv && cm->seq_params->subsampling_y;
1862*77c1e3ccSAndroid Build Coastguard Worker RestorationInfo *rsi = &cm->rst_info[plane];
1863*77c1e3ccSAndroid Build Coastguard Worker const int ru_size = rsi->restoration_unit_size;
1864*77c1e3ccSAndroid Build Coastguard Worker const int ext_size = ru_size * 3 / 2;
1865*77c1e3ccSAndroid Build Coastguard Worker
1866*77c1e3ccSAndroid Build Coastguard Worker int plane_w, plane_h;
1867*77c1e3ccSAndroid Build Coastguard Worker av1_get_upsampled_plane_size(cm, is_uv, &plane_w, &plane_h);
1868*77c1e3ccSAndroid Build Coastguard Worker
1869*77c1e3ccSAndroid Build Coastguard Worker static const rest_unit_visitor_t funs[RESTORE_TYPES] = {
1870*77c1e3ccSAndroid Build Coastguard Worker search_norestore, search_wiener, search_sgrproj, search_switchable
1871*77c1e3ccSAndroid Build Coastguard Worker };
1872*77c1e3ccSAndroid Build Coastguard Worker
1873*77c1e3ccSAndroid Build Coastguard Worker const int plane_num_units = rsi->num_rest_units;
1874*77c1e3ccSAndroid Build Coastguard Worker const RestorationType num_rtypes =
1875*77c1e3ccSAndroid Build Coastguard Worker (plane_num_units > 1) ? RESTORE_TYPES : RESTORE_SWITCHABLE_TYPES;
1876*77c1e3ccSAndroid Build Coastguard Worker
1877*77c1e3ccSAndroid Build Coastguard Worker reset_rsc(rsc);
1878*77c1e3ccSAndroid Build Coastguard Worker
1879*77c1e3ccSAndroid Build Coastguard Worker // Iterate over restoration units in encoding order, so that each RU gets
1880*77c1e3ccSAndroid Build Coastguard Worker // the correct reference parameters when we cost it up. This is effectively
1881*77c1e3ccSAndroid Build Coastguard Worker // a nested iteration over:
1882*77c1e3ccSAndroid Build Coastguard Worker // * Each tile, order does not matter
1883*77c1e3ccSAndroid Build Coastguard Worker // * Each superblock within that tile, in raster order
1884*77c1e3ccSAndroid Build Coastguard Worker // * Each LR unit which is coded within that superblock, in raster order
1885*77c1e3ccSAndroid Build Coastguard Worker for (int tile_row = 0; tile_row < tiles->rows; tile_row++) {
1886*77c1e3ccSAndroid Build Coastguard Worker int sb_row_start = tiles->row_start_sb[tile_row];
1887*77c1e3ccSAndroid Build Coastguard Worker int sb_row_end = tiles->row_start_sb[tile_row + 1];
1888*77c1e3ccSAndroid Build Coastguard Worker for (int tile_col = 0; tile_col < tiles->cols; tile_col++) {
1889*77c1e3ccSAndroid Build Coastguard Worker int sb_col_start = tiles->col_start_sb[tile_col];
1890*77c1e3ccSAndroid Build Coastguard Worker int sb_col_end = tiles->col_start_sb[tile_col + 1];
1891*77c1e3ccSAndroid Build Coastguard Worker
1892*77c1e3ccSAndroid Build Coastguard Worker // Reset reference parameters for delta-coding at the start of each tile
1893*77c1e3ccSAndroid Build Coastguard Worker rsc_on_tile(rsc);
1894*77c1e3ccSAndroid Build Coastguard Worker
1895*77c1e3ccSAndroid Build Coastguard Worker for (int sb_row = sb_row_start; sb_row < sb_row_end; sb_row++) {
1896*77c1e3ccSAndroid Build Coastguard Worker int mi_row = sb_row << mib_size_log2;
1897*77c1e3ccSAndroid Build Coastguard Worker for (int sb_col = sb_col_start; sb_col < sb_col_end; sb_col++) {
1898*77c1e3ccSAndroid Build Coastguard Worker int mi_col = sb_col << mib_size_log2;
1899*77c1e3ccSAndroid Build Coastguard Worker
1900*77c1e3ccSAndroid Build Coastguard Worker int rcol0, rcol1, rrow0, rrow1;
1901*77c1e3ccSAndroid Build Coastguard Worker int has_lr_info = av1_loop_restoration_corners_in_sb(
1902*77c1e3ccSAndroid Build Coastguard Worker cm, plane, mi_row, mi_col, sb_size, &rcol0, &rcol1, &rrow0,
1903*77c1e3ccSAndroid Build Coastguard Worker &rrow1);
1904*77c1e3ccSAndroid Build Coastguard Worker
1905*77c1e3ccSAndroid Build Coastguard Worker if (!has_lr_info) continue;
1906*77c1e3ccSAndroid Build Coastguard Worker
1907*77c1e3ccSAndroid Build Coastguard Worker RestorationTileLimits limits;
1908*77c1e3ccSAndroid Build Coastguard Worker for (int rrow = rrow0; rrow < rrow1; rrow++) {
1909*77c1e3ccSAndroid Build Coastguard Worker int y0 = rrow * ru_size;
1910*77c1e3ccSAndroid Build Coastguard Worker int remaining_h = plane_h - y0;
1911*77c1e3ccSAndroid Build Coastguard Worker int h = (remaining_h < ext_size) ? remaining_h : ru_size;
1912*77c1e3ccSAndroid Build Coastguard Worker
1913*77c1e3ccSAndroid Build Coastguard Worker limits.v_start = y0;
1914*77c1e3ccSAndroid Build Coastguard Worker limits.v_end = y0 + h;
1915*77c1e3ccSAndroid Build Coastguard Worker assert(limits.v_end <= plane_h);
1916*77c1e3ccSAndroid Build Coastguard Worker // Offset upwards to align with the restoration processing stripe
1917*77c1e3ccSAndroid Build Coastguard Worker const int voffset = RESTORATION_UNIT_OFFSET >> ss_y;
1918*77c1e3ccSAndroid Build Coastguard Worker limits.v_start = AOMMAX(0, limits.v_start - voffset);
1919*77c1e3ccSAndroid Build Coastguard Worker if (limits.v_end < plane_h) limits.v_end -= voffset;
1920*77c1e3ccSAndroid Build Coastguard Worker
1921*77c1e3ccSAndroid Build Coastguard Worker for (int rcol = rcol0; rcol < rcol1; rcol++) {
1922*77c1e3ccSAndroid Build Coastguard Worker int x0 = rcol * ru_size;
1923*77c1e3ccSAndroid Build Coastguard Worker int remaining_w = plane_w - x0;
1924*77c1e3ccSAndroid Build Coastguard Worker int w = (remaining_w < ext_size) ? remaining_w : ru_size;
1925*77c1e3ccSAndroid Build Coastguard Worker
1926*77c1e3ccSAndroid Build Coastguard Worker limits.h_start = x0;
1927*77c1e3ccSAndroid Build Coastguard Worker limits.h_end = x0 + w;
1928*77c1e3ccSAndroid Build Coastguard Worker assert(limits.h_end <= plane_w);
1929*77c1e3ccSAndroid Build Coastguard Worker
1930*77c1e3ccSAndroid Build Coastguard Worker const int unit_idx = rrow * rsi->horz_units + rcol;
1931*77c1e3ccSAndroid Build Coastguard Worker
1932*77c1e3ccSAndroid Build Coastguard Worker rsc->skip_sgr_eval = 0;
1933*77c1e3ccSAndroid Build Coastguard Worker for (RestorationType r = RESTORE_NONE; r < num_rtypes; r++) {
1934*77c1e3ccSAndroid Build Coastguard Worker if (disable_lr_filter[r]) continue;
1935*77c1e3ccSAndroid Build Coastguard Worker
1936*77c1e3ccSAndroid Build Coastguard Worker funs[r](&limits, unit_idx, rsc, rsc->cm->rst_tmpbuf, NULL,
1937*77c1e3ccSAndroid Build Coastguard Worker cm->error);
1938*77c1e3ccSAndroid Build Coastguard Worker }
1939*77c1e3ccSAndroid Build Coastguard Worker }
1940*77c1e3ccSAndroid Build Coastguard Worker }
1941*77c1e3ccSAndroid Build Coastguard Worker }
1942*77c1e3ccSAndroid Build Coastguard Worker }
1943*77c1e3ccSAndroid Build Coastguard Worker }
1944*77c1e3ccSAndroid Build Coastguard Worker }
1945*77c1e3ccSAndroid Build Coastguard Worker }
1946*77c1e3ccSAndroid Build Coastguard Worker
av1_derive_flags_for_lr_processing(const LOOP_FILTER_SPEED_FEATURES * lpf_sf,bool * disable_lr_filter)1947*77c1e3ccSAndroid Build Coastguard Worker static inline void av1_derive_flags_for_lr_processing(
1948*77c1e3ccSAndroid Build Coastguard Worker const LOOP_FILTER_SPEED_FEATURES *lpf_sf, bool *disable_lr_filter) {
1949*77c1e3ccSAndroid Build Coastguard Worker const bool is_wiener_disabled = lpf_sf->disable_wiener_filter;
1950*77c1e3ccSAndroid Build Coastguard Worker const bool is_sgr_disabled = lpf_sf->disable_sgr_filter;
1951*77c1e3ccSAndroid Build Coastguard Worker
1952*77c1e3ccSAndroid Build Coastguard Worker // Enable None Loop restoration filter if either of Wiener or Self-guided is
1953*77c1e3ccSAndroid Build Coastguard Worker // enabled.
1954*77c1e3ccSAndroid Build Coastguard Worker disable_lr_filter[RESTORE_NONE] = (is_wiener_disabled && is_sgr_disabled);
1955*77c1e3ccSAndroid Build Coastguard Worker
1956*77c1e3ccSAndroid Build Coastguard Worker disable_lr_filter[RESTORE_WIENER] = is_wiener_disabled;
1957*77c1e3ccSAndroid Build Coastguard Worker disable_lr_filter[RESTORE_SGRPROJ] = is_sgr_disabled;
1958*77c1e3ccSAndroid Build Coastguard Worker
1959*77c1e3ccSAndroid Build Coastguard Worker // Enable Swicthable Loop restoration filter if both of the Wiener and
1960*77c1e3ccSAndroid Build Coastguard Worker // Self-guided are enabled.
1961*77c1e3ccSAndroid Build Coastguard Worker disable_lr_filter[RESTORE_SWITCHABLE] =
1962*77c1e3ccSAndroid Build Coastguard Worker (is_wiener_disabled || is_sgr_disabled);
1963*77c1e3ccSAndroid Build Coastguard Worker }
1964*77c1e3ccSAndroid Build Coastguard Worker
1965*77c1e3ccSAndroid Build Coastguard Worker #define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
1966*77c1e3ccSAndroid Build Coastguard Worker // Allocate both decoder-side and encoder-side info structs for a single plane.
1967*77c1e3ccSAndroid Build Coastguard Worker // The unit size passed in should be the minimum size which we are going to
1968*77c1e3ccSAndroid Build Coastguard Worker // search; before each search, set_restoration_unit_size() must be called to
1969*77c1e3ccSAndroid Build Coastguard Worker // configure the actual size.
allocate_search_structs(AV1_COMMON * cm,RestorationInfo * rsi,int is_uv,int min_luma_unit_size)1970*77c1e3ccSAndroid Build Coastguard Worker static RestUnitSearchInfo *allocate_search_structs(AV1_COMMON *cm,
1971*77c1e3ccSAndroid Build Coastguard Worker RestorationInfo *rsi,
1972*77c1e3ccSAndroid Build Coastguard Worker int is_uv,
1973*77c1e3ccSAndroid Build Coastguard Worker int min_luma_unit_size) {
1974*77c1e3ccSAndroid Build Coastguard Worker #if COUPLED_CHROMA_FROM_LUMA_RESTORATION
1975*77c1e3ccSAndroid Build Coastguard Worker int sx = cm->seq_params.subsampling_x;
1976*77c1e3ccSAndroid Build Coastguard Worker int sy = cm->seq_params.subsampling_y;
1977*77c1e3ccSAndroid Build Coastguard Worker int s = (p > 0) ? AOMMIN(sx, sy) : 0;
1978*77c1e3ccSAndroid Build Coastguard Worker #else
1979*77c1e3ccSAndroid Build Coastguard Worker int s = 0;
1980*77c1e3ccSAndroid Build Coastguard Worker #endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
1981*77c1e3ccSAndroid Build Coastguard Worker int min_unit_size = min_luma_unit_size >> s;
1982*77c1e3ccSAndroid Build Coastguard Worker
1983*77c1e3ccSAndroid Build Coastguard Worker int plane_w, plane_h;
1984*77c1e3ccSAndroid Build Coastguard Worker av1_get_upsampled_plane_size(cm, is_uv, &plane_w, &plane_h);
1985*77c1e3ccSAndroid Build Coastguard Worker
1986*77c1e3ccSAndroid Build Coastguard Worker const int max_horz_units = av1_lr_count_units(min_unit_size, plane_w);
1987*77c1e3ccSAndroid Build Coastguard Worker const int max_vert_units = av1_lr_count_units(min_unit_size, plane_h);
1988*77c1e3ccSAndroid Build Coastguard Worker const int max_num_units = max_horz_units * max_vert_units;
1989*77c1e3ccSAndroid Build Coastguard Worker
1990*77c1e3ccSAndroid Build Coastguard Worker aom_free(rsi->unit_info);
1991*77c1e3ccSAndroid Build Coastguard Worker CHECK_MEM_ERROR(cm, rsi->unit_info,
1992*77c1e3ccSAndroid Build Coastguard Worker (RestorationUnitInfo *)aom_memalign(
1993*77c1e3ccSAndroid Build Coastguard Worker 16, sizeof(*rsi->unit_info) * max_num_units));
1994*77c1e3ccSAndroid Build Coastguard Worker
1995*77c1e3ccSAndroid Build Coastguard Worker RestUnitSearchInfo *rusi;
1996*77c1e3ccSAndroid Build Coastguard Worker CHECK_MEM_ERROR(
1997*77c1e3ccSAndroid Build Coastguard Worker cm, rusi,
1998*77c1e3ccSAndroid Build Coastguard Worker (RestUnitSearchInfo *)aom_memalign(16, sizeof(*rusi) * max_num_units));
1999*77c1e3ccSAndroid Build Coastguard Worker
2000*77c1e3ccSAndroid Build Coastguard Worker // If the restoration unit dimensions are not multiples of
2001*77c1e3ccSAndroid Build Coastguard Worker // rsi->restoration_unit_size then some elements of the rusi array may be
2002*77c1e3ccSAndroid Build Coastguard Worker // left uninitialised when we reach copy_unit_info(...). This is not a
2003*77c1e3ccSAndroid Build Coastguard Worker // problem, as these elements are ignored later, but in order to quiet
2004*77c1e3ccSAndroid Build Coastguard Worker // Valgrind's warnings we initialise the array below.
2005*77c1e3ccSAndroid Build Coastguard Worker memset(rusi, 0, sizeof(*rusi) * max_num_units);
2006*77c1e3ccSAndroid Build Coastguard Worker
2007*77c1e3ccSAndroid Build Coastguard Worker return rusi;
2008*77c1e3ccSAndroid Build Coastguard Worker }
2009*77c1e3ccSAndroid Build Coastguard Worker
set_restoration_unit_size(AV1_COMMON * cm,RestorationInfo * rsi,int is_uv,int luma_unit_size)2010*77c1e3ccSAndroid Build Coastguard Worker static void set_restoration_unit_size(AV1_COMMON *cm, RestorationInfo *rsi,
2011*77c1e3ccSAndroid Build Coastguard Worker int is_uv, int luma_unit_size) {
2012*77c1e3ccSAndroid Build Coastguard Worker #if COUPLED_CHROMA_FROM_LUMA_RESTORATION
2013*77c1e3ccSAndroid Build Coastguard Worker int sx = cm->seq_params.subsampling_x;
2014*77c1e3ccSAndroid Build Coastguard Worker int sy = cm->seq_params.subsampling_y;
2015*77c1e3ccSAndroid Build Coastguard Worker int s = (p > 0) ? AOMMIN(sx, sy) : 0;
2016*77c1e3ccSAndroid Build Coastguard Worker #else
2017*77c1e3ccSAndroid Build Coastguard Worker int s = 0;
2018*77c1e3ccSAndroid Build Coastguard Worker #endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
2019*77c1e3ccSAndroid Build Coastguard Worker int unit_size = luma_unit_size >> s;
2020*77c1e3ccSAndroid Build Coastguard Worker
2021*77c1e3ccSAndroid Build Coastguard Worker int plane_w, plane_h;
2022*77c1e3ccSAndroid Build Coastguard Worker av1_get_upsampled_plane_size(cm, is_uv, &plane_w, &plane_h);
2023*77c1e3ccSAndroid Build Coastguard Worker
2024*77c1e3ccSAndroid Build Coastguard Worker const int horz_units = av1_lr_count_units(unit_size, plane_w);
2025*77c1e3ccSAndroid Build Coastguard Worker const int vert_units = av1_lr_count_units(unit_size, plane_h);
2026*77c1e3ccSAndroid Build Coastguard Worker
2027*77c1e3ccSAndroid Build Coastguard Worker rsi->restoration_unit_size = unit_size;
2028*77c1e3ccSAndroid Build Coastguard Worker rsi->num_rest_units = horz_units * vert_units;
2029*77c1e3ccSAndroid Build Coastguard Worker rsi->horz_units = horz_units;
2030*77c1e3ccSAndroid Build Coastguard Worker rsi->vert_units = vert_units;
2031*77c1e3ccSAndroid Build Coastguard Worker }
2032*77c1e3ccSAndroid Build Coastguard Worker
av1_pick_filter_restoration(const YV12_BUFFER_CONFIG * src,AV1_COMP * cpi)2033*77c1e3ccSAndroid Build Coastguard Worker void av1_pick_filter_restoration(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi) {
2034*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
2035*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCK *const x = &cpi->td.mb;
2036*77c1e3ccSAndroid Build Coastguard Worker const SequenceHeader *const seq_params = cm->seq_params;
2037*77c1e3ccSAndroid Build Coastguard Worker const LOOP_FILTER_SPEED_FEATURES *lpf_sf = &cpi->sf.lpf_sf;
2038*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
2039*77c1e3ccSAndroid Build Coastguard Worker const int highbd = cm->seq_params->use_highbitdepth;
2040*77c1e3ccSAndroid Build Coastguard Worker assert(!cm->features.all_lossless);
2041*77c1e3ccSAndroid Build Coastguard Worker
2042*77c1e3ccSAndroid Build Coastguard Worker av1_fill_lr_rates(&x->mode_costs, x->e_mbd.tile_ctx);
2043*77c1e3ccSAndroid Build Coastguard Worker
2044*77c1e3ccSAndroid Build Coastguard Worker // Select unit size based on speed feature settings, and allocate
2045*77c1e3ccSAndroid Build Coastguard Worker // rui structs based on this size
2046*77c1e3ccSAndroid Build Coastguard Worker int min_lr_unit_size = cpi->sf.lpf_sf.min_lr_unit_size;
2047*77c1e3ccSAndroid Build Coastguard Worker int max_lr_unit_size = cpi->sf.lpf_sf.max_lr_unit_size;
2048*77c1e3ccSAndroid Build Coastguard Worker
2049*77c1e3ccSAndroid Build Coastguard Worker // The minimum allowed unit size at a syntax level is 1 superblock.
2050*77c1e3ccSAndroid Build Coastguard Worker // Apply this constraint here so that the speed features code which sets
2051*77c1e3ccSAndroid Build Coastguard Worker // cpi->sf.lpf_sf.min_lr_unit_size does not need to know the superblock size
2052*77c1e3ccSAndroid Build Coastguard Worker min_lr_unit_size =
2053*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(min_lr_unit_size, block_size_wide[cm->seq_params->sb_size]);
2054*77c1e3ccSAndroid Build Coastguard Worker
2055*77c1e3ccSAndroid Build Coastguard Worker for (int plane = 0; plane < num_planes; ++plane) {
2056*77c1e3ccSAndroid Build Coastguard Worker cpi->pick_lr_ctxt.rusi[plane] = allocate_search_structs(
2057*77c1e3ccSAndroid Build Coastguard Worker cm, &cm->rst_info[plane], plane > 0, min_lr_unit_size);
2058*77c1e3ccSAndroid Build Coastguard Worker }
2059*77c1e3ccSAndroid Build Coastguard Worker
2060*77c1e3ccSAndroid Build Coastguard Worker x->rdmult = cpi->rd.RDMULT;
2061*77c1e3ccSAndroid Build Coastguard Worker
2062*77c1e3ccSAndroid Build Coastguard Worker // Allocate the frame buffer trial_frame_rst, which is used to temporarily
2063*77c1e3ccSAndroid Build Coastguard Worker // store the loop restored frame.
2064*77c1e3ccSAndroid Build Coastguard Worker if (aom_realloc_frame_buffer(
2065*77c1e3ccSAndroid Build Coastguard Worker &cpi->trial_frame_rst, cm->superres_upscaled_width,
2066*77c1e3ccSAndroid Build Coastguard Worker cm->superres_upscaled_height, seq_params->subsampling_x,
2067*77c1e3ccSAndroid Build Coastguard Worker seq_params->subsampling_y, highbd, AOM_RESTORATION_FRAME_BORDER,
2068*77c1e3ccSAndroid Build Coastguard Worker cm->features.byte_alignment, NULL, NULL, NULL, false, 0))
2069*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
2070*77c1e3ccSAndroid Build Coastguard Worker "Failed to allocate trial restored frame buffer");
2071*77c1e3ccSAndroid Build Coastguard Worker
2072*77c1e3ccSAndroid Build Coastguard Worker RestSearchCtxt rsc;
2073*77c1e3ccSAndroid Build Coastguard Worker
2074*77c1e3ccSAndroid Build Coastguard Worker // The buffers 'src_avg' and 'dgd_avg' are used to compute H and M buffers.
2075*77c1e3ccSAndroid Build Coastguard Worker // These buffers are only required for the AVX2 and NEON implementations of
2076*77c1e3ccSAndroid Build Coastguard Worker // av1_compute_stats. The buffer size required is calculated based on maximum
2077*77c1e3ccSAndroid Build Coastguard Worker // width and height of the LRU (i.e., from foreach_rest_unit_in_plane() 1.5
2078*77c1e3ccSAndroid Build Coastguard Worker // times the RESTORATION_UNITSIZE_MAX) allowed for Wiener filtering. The width
2079*77c1e3ccSAndroid Build Coastguard Worker // and height aligned to multiple of 16 is considered for intrinsic purpose.
2080*77c1e3ccSAndroid Build Coastguard Worker rsc.dgd_avg = NULL;
2081*77c1e3ccSAndroid Build Coastguard Worker rsc.src_avg = NULL;
2082*77c1e3ccSAndroid Build Coastguard Worker #if HAVE_AVX2 || HAVE_NEON || HAVE_SVE
2083*77c1e3ccSAndroid Build Coastguard Worker // The buffers allocated below are used during Wiener filter processing.
2084*77c1e3ccSAndroid Build Coastguard Worker // Hence, allocate the same when Wiener filter is enabled. Make sure to
2085*77c1e3ccSAndroid Build Coastguard Worker // allocate these buffers only for the SIMD extensions that make use of them
2086*77c1e3ccSAndroid Build Coastguard Worker // (i.e. AVX2 for low bitdepth and NEON and SVE for low and high bitdepth).
2087*77c1e3ccSAndroid Build Coastguard Worker #if HAVE_AVX2
2088*77c1e3ccSAndroid Build Coastguard Worker bool allocate_buffers = !cpi->sf.lpf_sf.disable_wiener_filter && !highbd;
2089*77c1e3ccSAndroid Build Coastguard Worker #elif HAVE_NEON || HAVE_SVE
2090*77c1e3ccSAndroid Build Coastguard Worker bool allocate_buffers = !cpi->sf.lpf_sf.disable_wiener_filter;
2091*77c1e3ccSAndroid Build Coastguard Worker #endif
2092*77c1e3ccSAndroid Build Coastguard Worker if (allocate_buffers) {
2093*77c1e3ccSAndroid Build Coastguard Worker const int buf_size = sizeof(*cpi->pick_lr_ctxt.dgd_avg) * 6 *
2094*77c1e3ccSAndroid Build Coastguard Worker RESTORATION_UNITSIZE_MAX * RESTORATION_UNITSIZE_MAX;
2095*77c1e3ccSAndroid Build Coastguard Worker CHECK_MEM_ERROR(cm, cpi->pick_lr_ctxt.dgd_avg,
2096*77c1e3ccSAndroid Build Coastguard Worker (int16_t *)aom_memalign(32, buf_size));
2097*77c1e3ccSAndroid Build Coastguard Worker
2098*77c1e3ccSAndroid Build Coastguard Worker rsc.dgd_avg = cpi->pick_lr_ctxt.dgd_avg;
2099*77c1e3ccSAndroid Build Coastguard Worker // When LRU width isn't multiple of 16, the 256 bits load instruction used
2100*77c1e3ccSAndroid Build Coastguard Worker // in AVX2 intrinsic can read data beyond valid LRU. Hence, in order to
2101*77c1e3ccSAndroid Build Coastguard Worker // silence Valgrind warning this buffer is initialized with zero. Overhead
2102*77c1e3ccSAndroid Build Coastguard Worker // due to this initialization is negligible since it is done at frame level.
2103*77c1e3ccSAndroid Build Coastguard Worker memset(rsc.dgd_avg, 0, buf_size);
2104*77c1e3ccSAndroid Build Coastguard Worker rsc.src_avg =
2105*77c1e3ccSAndroid Build Coastguard Worker rsc.dgd_avg + 3 * RESTORATION_UNITSIZE_MAX * RESTORATION_UNITSIZE_MAX;
2106*77c1e3ccSAndroid Build Coastguard Worker // Asserts the starting address of src_avg is always 32-bytes aligned.
2107*77c1e3ccSAndroid Build Coastguard Worker assert(!((intptr_t)rsc.src_avg % 32));
2108*77c1e3ccSAndroid Build Coastguard Worker }
2109*77c1e3ccSAndroid Build Coastguard Worker #endif
2110*77c1e3ccSAndroid Build Coastguard Worker
2111*77c1e3ccSAndroid Build Coastguard Worker // Initialize all planes, so that any planes we skip searching will still have
2112*77c1e3ccSAndroid Build Coastguard Worker // valid data
2113*77c1e3ccSAndroid Build Coastguard Worker for (int plane = 0; plane < num_planes; plane++) {
2114*77c1e3ccSAndroid Build Coastguard Worker cm->rst_info[plane].frame_restoration_type = RESTORE_NONE;
2115*77c1e3ccSAndroid Build Coastguard Worker }
2116*77c1e3ccSAndroid Build Coastguard Worker
2117*77c1e3ccSAndroid Build Coastguard Worker // Decide which planes to search
2118*77c1e3ccSAndroid Build Coastguard Worker int plane_start, plane_end;
2119*77c1e3ccSAndroid Build Coastguard Worker
2120*77c1e3ccSAndroid Build Coastguard Worker if (lpf_sf->disable_loop_restoration_luma) {
2121*77c1e3ccSAndroid Build Coastguard Worker plane_start = AOM_PLANE_U;
2122*77c1e3ccSAndroid Build Coastguard Worker } else {
2123*77c1e3ccSAndroid Build Coastguard Worker plane_start = AOM_PLANE_Y;
2124*77c1e3ccSAndroid Build Coastguard Worker }
2125*77c1e3ccSAndroid Build Coastguard Worker
2126*77c1e3ccSAndroid Build Coastguard Worker if (num_planes == 1 || lpf_sf->disable_loop_restoration_chroma) {
2127*77c1e3ccSAndroid Build Coastguard Worker plane_end = AOM_PLANE_Y;
2128*77c1e3ccSAndroid Build Coastguard Worker } else {
2129*77c1e3ccSAndroid Build Coastguard Worker plane_end = AOM_PLANE_V;
2130*77c1e3ccSAndroid Build Coastguard Worker }
2131*77c1e3ccSAndroid Build Coastguard Worker
2132*77c1e3ccSAndroid Build Coastguard Worker // Derive the flags to enable/disable Loop restoration filters based on the
2133*77c1e3ccSAndroid Build Coastguard Worker // speed features 'disable_wiener_filter' and 'disable_sgr_filter'.
2134*77c1e3ccSAndroid Build Coastguard Worker bool disable_lr_filter[RESTORE_TYPES] = { false };
2135*77c1e3ccSAndroid Build Coastguard Worker av1_derive_flags_for_lr_processing(lpf_sf, disable_lr_filter);
2136*77c1e3ccSAndroid Build Coastguard Worker
2137*77c1e3ccSAndroid Build Coastguard Worker for (int plane = plane_start; plane <= plane_end; plane++) {
2138*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *dgd = &cm->cur_frame->buf;
2139*77c1e3ccSAndroid Build Coastguard Worker const int is_uv = plane != AOM_PLANE_Y;
2140*77c1e3ccSAndroid Build Coastguard Worker int plane_w, plane_h;
2141*77c1e3ccSAndroid Build Coastguard Worker av1_get_upsampled_plane_size(cm, is_uv, &plane_w, &plane_h);
2142*77c1e3ccSAndroid Build Coastguard Worker av1_extend_frame(dgd->buffers[plane], plane_w, plane_h, dgd->strides[is_uv],
2143*77c1e3ccSAndroid Build Coastguard Worker RESTORATION_BORDER, RESTORATION_BORDER, highbd);
2144*77c1e3ccSAndroid Build Coastguard Worker }
2145*77c1e3ccSAndroid Build Coastguard Worker
2146*77c1e3ccSAndroid Build Coastguard Worker double best_cost = DBL_MAX;
2147*77c1e3ccSAndroid Build Coastguard Worker int best_luma_unit_size = max_lr_unit_size;
2148*77c1e3ccSAndroid Build Coastguard Worker for (int luma_unit_size = max_lr_unit_size;
2149*77c1e3ccSAndroid Build Coastguard Worker luma_unit_size >= min_lr_unit_size; luma_unit_size >>= 1) {
2150*77c1e3ccSAndroid Build Coastguard Worker int64_t bits_this_size = 0;
2151*77c1e3ccSAndroid Build Coastguard Worker int64_t sse_this_size = 0;
2152*77c1e3ccSAndroid Build Coastguard Worker RestorationType best_rtype[MAX_MB_PLANE] = { RESTORE_NONE, RESTORE_NONE,
2153*77c1e3ccSAndroid Build Coastguard Worker RESTORE_NONE };
2154*77c1e3ccSAndroid Build Coastguard Worker for (int plane = plane_start; plane <= plane_end; ++plane) {
2155*77c1e3ccSAndroid Build Coastguard Worker set_restoration_unit_size(cm, &cm->rst_info[plane], plane > 0,
2156*77c1e3ccSAndroid Build Coastguard Worker luma_unit_size);
2157*77c1e3ccSAndroid Build Coastguard Worker init_rsc(src, &cpi->common, x, lpf_sf, plane,
2158*77c1e3ccSAndroid Build Coastguard Worker cpi->pick_lr_ctxt.rusi[plane], &cpi->trial_frame_rst, &rsc);
2159*77c1e3ccSAndroid Build Coastguard Worker
2160*77c1e3ccSAndroid Build Coastguard Worker restoration_search(cm, plane, &rsc, disable_lr_filter);
2161*77c1e3ccSAndroid Build Coastguard Worker
2162*77c1e3ccSAndroid Build Coastguard Worker const int plane_num_units = cm->rst_info[plane].num_rest_units;
2163*77c1e3ccSAndroid Build Coastguard Worker const RestorationType num_rtypes =
2164*77c1e3ccSAndroid Build Coastguard Worker (plane_num_units > 1) ? RESTORE_TYPES : RESTORE_SWITCHABLE_TYPES;
2165*77c1e3ccSAndroid Build Coastguard Worker double best_cost_this_plane = DBL_MAX;
2166*77c1e3ccSAndroid Build Coastguard Worker for (RestorationType r = 0; r < num_rtypes; ++r) {
2167*77c1e3ccSAndroid Build Coastguard Worker // Disable Loop restoration filter based on the flags set using speed
2168*77c1e3ccSAndroid Build Coastguard Worker // feature 'disable_wiener_filter' and 'disable_sgr_filter'.
2169*77c1e3ccSAndroid Build Coastguard Worker if (disable_lr_filter[r]) continue;
2170*77c1e3ccSAndroid Build Coastguard Worker
2171*77c1e3ccSAndroid Build Coastguard Worker double cost_this_plane = RDCOST_DBL_WITH_NATIVE_BD_DIST(
2172*77c1e3ccSAndroid Build Coastguard Worker x->rdmult, rsc.total_bits[r] >> 4, rsc.total_sse[r],
2173*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->bit_depth);
2174*77c1e3ccSAndroid Build Coastguard Worker
2175*77c1e3ccSAndroid Build Coastguard Worker if (cost_this_plane < best_cost_this_plane) {
2176*77c1e3ccSAndroid Build Coastguard Worker best_cost_this_plane = cost_this_plane;
2177*77c1e3ccSAndroid Build Coastguard Worker best_rtype[plane] = r;
2178*77c1e3ccSAndroid Build Coastguard Worker }
2179*77c1e3ccSAndroid Build Coastguard Worker }
2180*77c1e3ccSAndroid Build Coastguard Worker
2181*77c1e3ccSAndroid Build Coastguard Worker bits_this_size += rsc.total_bits[best_rtype[plane]];
2182*77c1e3ccSAndroid Build Coastguard Worker sse_this_size += rsc.total_sse[best_rtype[plane]];
2183*77c1e3ccSAndroid Build Coastguard Worker }
2184*77c1e3ccSAndroid Build Coastguard Worker
2185*77c1e3ccSAndroid Build Coastguard Worker double cost_this_size = RDCOST_DBL_WITH_NATIVE_BD_DIST(
2186*77c1e3ccSAndroid Build Coastguard Worker x->rdmult, bits_this_size >> 4, sse_this_size,
2187*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->bit_depth);
2188*77c1e3ccSAndroid Build Coastguard Worker
2189*77c1e3ccSAndroid Build Coastguard Worker if (cost_this_size < best_cost) {
2190*77c1e3ccSAndroid Build Coastguard Worker best_cost = cost_this_size;
2191*77c1e3ccSAndroid Build Coastguard Worker best_luma_unit_size = luma_unit_size;
2192*77c1e3ccSAndroid Build Coastguard Worker // Copy parameters out of rusi struct, before we overwrite it at
2193*77c1e3ccSAndroid Build Coastguard Worker // the start of the next iteration
2194*77c1e3ccSAndroid Build Coastguard Worker bool all_none = true;
2195*77c1e3ccSAndroid Build Coastguard Worker for (int plane = plane_start; plane <= plane_end; ++plane) {
2196*77c1e3ccSAndroid Build Coastguard Worker cm->rst_info[plane].frame_restoration_type = best_rtype[plane];
2197*77c1e3ccSAndroid Build Coastguard Worker if (best_rtype[plane] != RESTORE_NONE) {
2198*77c1e3ccSAndroid Build Coastguard Worker all_none = false;
2199*77c1e3ccSAndroid Build Coastguard Worker const int plane_num_units = cm->rst_info[plane].num_rest_units;
2200*77c1e3ccSAndroid Build Coastguard Worker for (int u = 0; u < plane_num_units; ++u) {
2201*77c1e3ccSAndroid Build Coastguard Worker copy_unit_info(best_rtype[plane], &cpi->pick_lr_ctxt.rusi[plane][u],
2202*77c1e3ccSAndroid Build Coastguard Worker &cm->rst_info[plane].unit_info[u]);
2203*77c1e3ccSAndroid Build Coastguard Worker }
2204*77c1e3ccSAndroid Build Coastguard Worker }
2205*77c1e3ccSAndroid Build Coastguard Worker }
2206*77c1e3ccSAndroid Build Coastguard Worker // Heuristic: If all best_rtype entries are RESTORE_NONE, this means we
2207*77c1e3ccSAndroid Build Coastguard Worker // couldn't find any good filters at this size. So we likely won't find
2208*77c1e3ccSAndroid Build Coastguard Worker // any good filters at a smaller size either, so skip
2209*77c1e3ccSAndroid Build Coastguard Worker if (all_none) {
2210*77c1e3ccSAndroid Build Coastguard Worker break;
2211*77c1e3ccSAndroid Build Coastguard Worker }
2212*77c1e3ccSAndroid Build Coastguard Worker } else {
2213*77c1e3ccSAndroid Build Coastguard Worker // Heuristic: If this size is worse than the previous (larger) size, then
2214*77c1e3ccSAndroid Build Coastguard Worker // the next size down will likely be even worse, so skip
2215*77c1e3ccSAndroid Build Coastguard Worker break;
2216*77c1e3ccSAndroid Build Coastguard Worker }
2217*77c1e3ccSAndroid Build Coastguard Worker }
2218*77c1e3ccSAndroid Build Coastguard Worker
2219*77c1e3ccSAndroid Build Coastguard Worker // Final fixup to set the correct unit size
2220*77c1e3ccSAndroid Build Coastguard Worker // We set this for all planes, even ones we have skipped searching,
2221*77c1e3ccSAndroid Build Coastguard Worker // so that other code does not need to care which planes were and weren't
2222*77c1e3ccSAndroid Build Coastguard Worker // searched
2223*77c1e3ccSAndroid Build Coastguard Worker for (int plane = 0; plane < num_planes; ++plane) {
2224*77c1e3ccSAndroid Build Coastguard Worker set_restoration_unit_size(cm, &cm->rst_info[plane], plane > 0,
2225*77c1e3ccSAndroid Build Coastguard Worker best_luma_unit_size);
2226*77c1e3ccSAndroid Build Coastguard Worker }
2227*77c1e3ccSAndroid Build Coastguard Worker
2228*77c1e3ccSAndroid Build Coastguard Worker #if HAVE_AVX2 || HAVE_NEON || HAVE_SVE
2229*77c1e3ccSAndroid Build Coastguard Worker #if HAVE_AVX2
2230*77c1e3ccSAndroid Build Coastguard Worker bool free_buffers = !cpi->sf.lpf_sf.disable_wiener_filter && !highbd;
2231*77c1e3ccSAndroid Build Coastguard Worker #elif HAVE_NEON || HAVE_SVE
2232*77c1e3ccSAndroid Build Coastguard Worker bool free_buffers = !cpi->sf.lpf_sf.disable_wiener_filter;
2233*77c1e3ccSAndroid Build Coastguard Worker #endif
2234*77c1e3ccSAndroid Build Coastguard Worker if (free_buffers) {
2235*77c1e3ccSAndroid Build Coastguard Worker aom_free(cpi->pick_lr_ctxt.dgd_avg);
2236*77c1e3ccSAndroid Build Coastguard Worker cpi->pick_lr_ctxt.dgd_avg = NULL;
2237*77c1e3ccSAndroid Build Coastguard Worker }
2238*77c1e3ccSAndroid Build Coastguard Worker #endif
2239*77c1e3ccSAndroid Build Coastguard Worker for (int plane = 0; plane < num_planes; plane++) {
2240*77c1e3ccSAndroid Build Coastguard Worker aom_free(cpi->pick_lr_ctxt.rusi[plane]);
2241*77c1e3ccSAndroid Build Coastguard Worker cpi->pick_lr_ctxt.rusi[plane] = NULL;
2242*77c1e3ccSAndroid Build Coastguard Worker }
2243*77c1e3ccSAndroid Build Coastguard Worker }
2244