xref: /aosp_15_r20/external/libaom/av1/common/restoration.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 #ifndef AOM_AV1_COMMON_RESTORATION_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_COMMON_RESTORATION_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/blockd.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/enums.h"
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
22*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
23*77c1e3ccSAndroid Build Coastguard Worker #endif
24*77c1e3ccSAndroid Build Coastguard Worker 
25*77c1e3ccSAndroid Build Coastguard Worker /*! @file */
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */
28*77c1e3ccSAndroid Build Coastguard Worker 
29*77c1e3ccSAndroid Build Coastguard Worker // Border for Loop restoration buffer
30*77c1e3ccSAndroid Build Coastguard Worker #define AOM_RESTORATION_FRAME_BORDER 32
31*77c1e3ccSAndroid Build Coastguard Worker #define CLIP(x, lo, hi) ((x) < (lo) ? (lo) : (x) > (hi) ? (hi) : (x))
32*77c1e3ccSAndroid Build Coastguard Worker #define RINT(x) ((x) < 0 ? (int)((x)-0.5) : (int)((x) + 0.5))
33*77c1e3ccSAndroid Build Coastguard Worker 
34*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_PROC_UNIT_SIZE 64
35*77c1e3ccSAndroid Build Coastguard Worker 
36*77c1e3ccSAndroid Build Coastguard Worker // Filter stripe grid offset upwards compared to the superblock grid
37*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_UNIT_OFFSET 8
38*77c1e3ccSAndroid Build Coastguard Worker 
39*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_BORDER_VERT 3  // Vertical border used for Sgr
40*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_BORDER_HORZ 3  // Horizontal border used for Sgr
41*77c1e3ccSAndroid Build Coastguard Worker 
42*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_BORDER_VERT 2  // Vertical border used for Wiener
43*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_HALFWIN 3
44*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_BORDER_HORZ (WIENER_HALFWIN)  // Horizontal border for Wiener
45*77c1e3ccSAndroid Build Coastguard Worker 
46*77c1e3ccSAndroid Build Coastguard Worker // RESTORATION_BORDER_VERT determines line buffer requirement for LR.
47*77c1e3ccSAndroid Build Coastguard Worker // Should be set at the max of SGRPROJ_BORDER_VERT and WIENER_BORDER_VERT.
48*77c1e3ccSAndroid Build Coastguard Worker // Note the line buffer needed is twice the value of this macro.
49*77c1e3ccSAndroid Build Coastguard Worker #if SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT
50*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_BORDER_VERT (SGRPROJ_BORDER_VERT)
51*77c1e3ccSAndroid Build Coastguard Worker #else
52*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_BORDER_VERT (WIENER_BORDER_VERT)
53*77c1e3ccSAndroid Build Coastguard Worker #endif  // SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT
54*77c1e3ccSAndroid Build Coastguard Worker 
55*77c1e3ccSAndroid Build Coastguard Worker #if SGRPROJ_BORDER_HORZ >= WIENER_BORDER_HORZ
56*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_BORDER_HORZ (SGRPROJ_BORDER_HORZ)
57*77c1e3ccSAndroid Build Coastguard Worker #else
58*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_BORDER_HORZ (WIENER_BORDER_HORZ)
59*77c1e3ccSAndroid Build Coastguard Worker #endif  // SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT
60*77c1e3ccSAndroid Build Coastguard Worker 
61*77c1e3ccSAndroid Build Coastguard Worker // How many border pixels do we need for each processing unit?
62*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_BORDER 3
63*77c1e3ccSAndroid Build Coastguard Worker 
64*77c1e3ccSAndroid Build Coastguard Worker // How many rows of deblocked pixels do we save above/below each processing
65*77c1e3ccSAndroid Build Coastguard Worker // stripe?
66*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_CTX_VERT 2
67*77c1e3ccSAndroid Build Coastguard Worker 
68*77c1e3ccSAndroid Build Coastguard Worker // Additional pixels to the left and right in above/below buffers
69*77c1e3ccSAndroid Build Coastguard Worker // It is RESTORATION_BORDER_HORZ rounded up to get nicer buffer alignment
70*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_EXTRA_HORZ 4
71*77c1e3ccSAndroid Build Coastguard Worker 
72*77c1e3ccSAndroid Build Coastguard Worker // Pad up to 20 more (may be much less is needed)
73*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_PADDING 20
74*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_PROC_UNIT_PELS                             \
75*77c1e3ccSAndroid Build Coastguard Worker   ((RESTORATION_PROC_UNIT_SIZE + RESTORATION_BORDER_HORZ * 2 + \
76*77c1e3ccSAndroid Build Coastguard Worker     RESTORATION_PADDING) *                                     \
77*77c1e3ccSAndroid Build Coastguard Worker    (RESTORATION_PROC_UNIT_SIZE + RESTORATION_BORDER_VERT * 2 + \
78*77c1e3ccSAndroid Build Coastguard Worker     RESTORATION_PADDING))
79*77c1e3ccSAndroid Build Coastguard Worker 
80*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_UNITSIZE_MAX 256
81*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_UNITPELS_HORZ_MAX \
82*77c1e3ccSAndroid Build Coastguard Worker   (RESTORATION_UNITSIZE_MAX * 3 / 2 + 2 * RESTORATION_BORDER_HORZ + 16)
83*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_UNITPELS_VERT_MAX                                \
84*77c1e3ccSAndroid Build Coastguard Worker   ((RESTORATION_UNITSIZE_MAX * 3 / 2 + 2 * RESTORATION_BORDER_VERT + \
85*77c1e3ccSAndroid Build Coastguard Worker     RESTORATION_UNIT_OFFSET))
86*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_UNITPELS_MAX \
87*77c1e3ccSAndroid Build Coastguard Worker   (RESTORATION_UNITPELS_HORZ_MAX * RESTORATION_UNITPELS_VERT_MAX)
88*77c1e3ccSAndroid Build Coastguard Worker 
89*77c1e3ccSAndroid Build Coastguard Worker // Two 32-bit buffers needed for the restored versions from two filters
90*77c1e3ccSAndroid Build Coastguard Worker // TODO(debargha, rupert): Refactor to not need the large tilesize to be stored
91*77c1e3ccSAndroid Build Coastguard Worker // on the decoder side.
92*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_TMPBUF_SIZE (RESTORATION_UNITPELS_MAX * 2 * sizeof(int32_t))
93*77c1e3ccSAndroid Build Coastguard Worker 
94*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_EXTBUF_SIZE (0)
95*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_PARAMS_BITS 4
96*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_PARAMS (1 << SGRPROJ_PARAMS_BITS)
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker // Precision bits for projection
99*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_PRJ_BITS 7
100*77c1e3ccSAndroid Build Coastguard Worker // Restoration precision bits generated higher than source before projection
101*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_RST_BITS 4
102*77c1e3ccSAndroid Build Coastguard Worker // Internal precision bits for core selfguided_restoration
103*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_SGR_BITS 8
104*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_SGR (1 << SGRPROJ_SGR_BITS)
105*77c1e3ccSAndroid Build Coastguard Worker 
106*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_PRJ_MIN0 (-(1 << SGRPROJ_PRJ_BITS) * 3 / 4)
107*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_PRJ_MAX0 (SGRPROJ_PRJ_MIN0 + (1 << SGRPROJ_PRJ_BITS) - 1)
108*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_PRJ_MIN1 (-(1 << SGRPROJ_PRJ_BITS) / 4)
109*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_PRJ_MAX1 (SGRPROJ_PRJ_MIN1 + (1 << SGRPROJ_PRJ_BITS) - 1)
110*77c1e3ccSAndroid Build Coastguard Worker 
111*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_PRJ_SUBEXP_K 4
112*77c1e3ccSAndroid Build Coastguard Worker 
113*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_BITS (SGRPROJ_PRJ_BITS * 2 + SGRPROJ_PARAMS_BITS)
114*77c1e3ccSAndroid Build Coastguard Worker 
115*77c1e3ccSAndroid Build Coastguard Worker #define MAX_RADIUS 2  // Only 1, 2, 3 allowed
116*77c1e3ccSAndroid Build Coastguard Worker #define MAX_NELEM ((2 * MAX_RADIUS + 1) * (2 * MAX_RADIUS + 1))
117*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_MTABLE_BITS 20
118*77c1e3ccSAndroid Build Coastguard Worker #define SGRPROJ_RECIP_BITS 12
119*77c1e3ccSAndroid Build Coastguard Worker 
120*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_HALFWIN1 (WIENER_HALFWIN + 1)
121*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_WIN (2 * WIENER_HALFWIN + 1)
122*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_WIN2 ((WIENER_WIN) * (WIENER_WIN))
123*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_TMPBUF_SIZE (0)
124*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_EXTBUF_SIZE (0)
125*77c1e3ccSAndroid Build Coastguard Worker 
126*77c1e3ccSAndroid Build Coastguard Worker // If WIENER_WIN_CHROMA == WIENER_WIN - 2, that implies 5x5 filters are used for
127*77c1e3ccSAndroid Build Coastguard Worker // chroma. To use 7x7 for chroma set WIENER_WIN_CHROMA to WIENER_WIN.
128*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_WIN_CHROMA (WIENER_WIN - 2)
129*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_WIN_REDUCED (WIENER_WIN - 2)
130*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_WIN2_CHROMA ((WIENER_WIN_CHROMA) * (WIENER_WIN_CHROMA))
131*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_STATS_DOWNSAMPLE_FACTOR 4
132*77c1e3ccSAndroid Build Coastguard Worker 
133*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_PREC_BITS 7
134*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_STEP (1 << WIENER_FILT_PREC_BITS)
135*77c1e3ccSAndroid Build Coastguard Worker 
136*77c1e3ccSAndroid Build Coastguard Worker // Central values for the taps
137*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP0_MIDV (3)
138*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP1_MIDV (-7)
139*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP2_MIDV (15)
140*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP3_MIDV                                              \
141*77c1e3ccSAndroid Build Coastguard Worker   (WIENER_FILT_STEP - 2 * (WIENER_FILT_TAP0_MIDV + WIENER_FILT_TAP1_MIDV + \
142*77c1e3ccSAndroid Build Coastguard Worker                            WIENER_FILT_TAP2_MIDV))
143*77c1e3ccSAndroid Build Coastguard Worker 
144*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP0_BITS 4
145*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP1_BITS 5
146*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP2_BITS 6
147*77c1e3ccSAndroid Build Coastguard Worker 
148*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_BITS \
149*77c1e3ccSAndroid Build Coastguard Worker   ((WIENER_FILT_TAP0_BITS + WIENER_FILT_TAP1_BITS + WIENER_FILT_TAP2_BITS) * 2)
150*77c1e3ccSAndroid Build Coastguard Worker 
151*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP0_MINV \
152*77c1e3ccSAndroid Build Coastguard Worker   (WIENER_FILT_TAP0_MIDV - (1 << WIENER_FILT_TAP0_BITS) / 2)
153*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP1_MINV \
154*77c1e3ccSAndroid Build Coastguard Worker   (WIENER_FILT_TAP1_MIDV - (1 << WIENER_FILT_TAP1_BITS) / 2)
155*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP2_MINV \
156*77c1e3ccSAndroid Build Coastguard Worker   (WIENER_FILT_TAP2_MIDV - (1 << WIENER_FILT_TAP2_BITS) / 2)
157*77c1e3ccSAndroid Build Coastguard Worker 
158*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP0_MAXV \
159*77c1e3ccSAndroid Build Coastguard Worker   (WIENER_FILT_TAP0_MIDV - 1 + (1 << WIENER_FILT_TAP0_BITS) / 2)
160*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP1_MAXV \
161*77c1e3ccSAndroid Build Coastguard Worker   (WIENER_FILT_TAP1_MIDV - 1 + (1 << WIENER_FILT_TAP1_BITS) / 2)
162*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP2_MAXV \
163*77c1e3ccSAndroid Build Coastguard Worker   (WIENER_FILT_TAP2_MIDV - 1 + (1 << WIENER_FILT_TAP2_BITS) / 2)
164*77c1e3ccSAndroid Build Coastguard Worker 
165*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP0_SUBEXP_K 1
166*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP1_SUBEXP_K 2
167*77c1e3ccSAndroid Build Coastguard Worker #define WIENER_FILT_TAP2_SUBEXP_K 3
168*77c1e3ccSAndroid Build Coastguard Worker 
169*77c1e3ccSAndroid Build Coastguard Worker // Max of SGRPROJ_TMPBUF_SIZE, DOMAINTXFMRF_TMPBUF_SIZE, WIENER_TMPBUF_SIZE
170*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_TMPBUF_SIZE (SGRPROJ_TMPBUF_SIZE)
171*77c1e3ccSAndroid Build Coastguard Worker 
172*77c1e3ccSAndroid Build Coastguard Worker // Max of SGRPROJ_EXTBUF_SIZE, WIENER_EXTBUF_SIZE
173*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_EXTBUF_SIZE (WIENER_EXTBUF_SIZE)
174*77c1e3ccSAndroid Build Coastguard Worker 
175*77c1e3ccSAndroid Build Coastguard Worker // Check the assumptions of the existing code
176*77c1e3ccSAndroid Build Coastguard Worker #if SUBPEL_TAPS != WIENER_WIN + 1
177*77c1e3ccSAndroid Build Coastguard Worker #error "Wiener filter currently only works if SUBPEL_TAPS == WIENER_WIN + 1"
178*77c1e3ccSAndroid Build Coastguard Worker #endif
179*77c1e3ccSAndroid Build Coastguard Worker #if WIENER_FILT_PREC_BITS != 7
180*77c1e3ccSAndroid Build Coastguard Worker #error "Wiener filter currently only works if WIENER_FILT_PREC_BITS == 7"
181*77c1e3ccSAndroid Build Coastguard Worker #endif
182*77c1e3ccSAndroid Build Coastguard Worker 
183*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
184*77c1e3ccSAndroid Build Coastguard Worker   int r[2];  // radii
185*77c1e3ccSAndroid Build Coastguard Worker   int s[2];  // sgr parameters for r[0] and r[1], based on GenSgrprojVtable()
186*77c1e3ccSAndroid Build Coastguard Worker } sgr_params_type;
187*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */
188*77c1e3ccSAndroid Build Coastguard Worker 
189*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Parameters related to Restoration Unit Info */
190*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
191*77c1e3ccSAndroid Build Coastguard Worker   /*!
192*77c1e3ccSAndroid Build Coastguard Worker    * restoration type
193*77c1e3ccSAndroid Build Coastguard Worker    */
194*77c1e3ccSAndroid Build Coastguard Worker   RestorationType restoration_type;
195*77c1e3ccSAndroid Build Coastguard Worker 
196*77c1e3ccSAndroid Build Coastguard Worker   /*!
197*77c1e3ccSAndroid Build Coastguard Worker    * Wiener filter parameters if restoration_type indicates Wiener
198*77c1e3ccSAndroid Build Coastguard Worker    */
199*77c1e3ccSAndroid Build Coastguard Worker   WienerInfo wiener_info;
200*77c1e3ccSAndroid Build Coastguard Worker 
201*77c1e3ccSAndroid Build Coastguard Worker   /*!
202*77c1e3ccSAndroid Build Coastguard Worker    * Sgrproj filter parameters if restoration_type indicates Sgrproj
203*77c1e3ccSAndroid Build Coastguard Worker    */
204*77c1e3ccSAndroid Build Coastguard Worker   SgrprojInfo sgrproj_info;
205*77c1e3ccSAndroid Build Coastguard Worker } RestorationUnitInfo;
206*77c1e3ccSAndroid Build Coastguard Worker 
207*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */
208*77c1e3ccSAndroid Build Coastguard Worker 
209*77c1e3ccSAndroid Build Coastguard Worker // A restoration line buffer needs space for two lines plus a horizontal filter
210*77c1e3ccSAndroid Build Coastguard Worker // margin of RESTORATION_EXTRA_HORZ on each side.
211*77c1e3ccSAndroid Build Coastguard Worker #define RESTORATION_LINEBUFFER_WIDTH \
212*77c1e3ccSAndroid Build Coastguard Worker   (RESTORATION_UNITSIZE_MAX * 3 / 2 + 2 * RESTORATION_EXTRA_HORZ)
213*77c1e3ccSAndroid Build Coastguard Worker 
214*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
215*77c1e3ccSAndroid Build Coastguard Worker   // Temporary buffers to save/restore 3 lines above/below the restoration
216*77c1e3ccSAndroid Build Coastguard Worker   // stripe.
217*77c1e3ccSAndroid Build Coastguard Worker   uint16_t tmp_save_above[RESTORATION_BORDER][RESTORATION_LINEBUFFER_WIDTH];
218*77c1e3ccSAndroid Build Coastguard Worker   uint16_t tmp_save_below[RESTORATION_BORDER][RESTORATION_LINEBUFFER_WIDTH];
219*77c1e3ccSAndroid Build Coastguard Worker } RestorationLineBuffers;
220*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */
221*77c1e3ccSAndroid Build Coastguard Worker 
222*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Parameters related to Restoration Stripe boundaries */
223*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
224*77c1e3ccSAndroid Build Coastguard Worker   /*!
225*77c1e3ccSAndroid Build Coastguard Worker    * stripe boundary above
226*77c1e3ccSAndroid Build Coastguard Worker    */
227*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *stripe_boundary_above;
228*77c1e3ccSAndroid Build Coastguard Worker 
229*77c1e3ccSAndroid Build Coastguard Worker   /*!
230*77c1e3ccSAndroid Build Coastguard Worker    * stripe boundary below
231*77c1e3ccSAndroid Build Coastguard Worker    */
232*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *stripe_boundary_below;
233*77c1e3ccSAndroid Build Coastguard Worker 
234*77c1e3ccSAndroid Build Coastguard Worker   /*!
235*77c1e3ccSAndroid Build Coastguard Worker    * strides for stripe boundaries above and below
236*77c1e3ccSAndroid Build Coastguard Worker    */
237*77c1e3ccSAndroid Build Coastguard Worker   int stripe_boundary_stride;
238*77c1e3ccSAndroid Build Coastguard Worker 
239*77c1e3ccSAndroid Build Coastguard Worker   /*!
240*77c1e3ccSAndroid Build Coastguard Worker    * size of stripe boundaries above and below
241*77c1e3ccSAndroid Build Coastguard Worker    */
242*77c1e3ccSAndroid Build Coastguard Worker   int stripe_boundary_size;
243*77c1e3ccSAndroid Build Coastguard Worker } RestorationStripeBoundaries;
244*77c1e3ccSAndroid Build Coastguard Worker 
245*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Parameters related to Restoration Info */
246*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
247*77c1e3ccSAndroid Build Coastguard Worker   /*!
248*77c1e3ccSAndroid Build Coastguard Worker    * Restoration type for frame
249*77c1e3ccSAndroid Build Coastguard Worker    */
250*77c1e3ccSAndroid Build Coastguard Worker   RestorationType frame_restoration_type;
251*77c1e3ccSAndroid Build Coastguard Worker 
252*77c1e3ccSAndroid Build Coastguard Worker   /*!
253*77c1e3ccSAndroid Build Coastguard Worker    * Restoration unit size
254*77c1e3ccSAndroid Build Coastguard Worker    */
255*77c1e3ccSAndroid Build Coastguard Worker   int restoration_unit_size;
256*77c1e3ccSAndroid Build Coastguard Worker 
257*77c1e3ccSAndroid Build Coastguard Worker   /**
258*77c1e3ccSAndroid Build Coastguard Worker    * \name Fields allocated and initialised by av1_alloc_restoration_struct.
259*77c1e3ccSAndroid Build Coastguard Worker    */
260*77c1e3ccSAndroid Build Coastguard Worker   /**@{*/
261*77c1e3ccSAndroid Build Coastguard Worker   /*!
262*77c1e3ccSAndroid Build Coastguard Worker    * Total number of restoration units in this plane
263*77c1e3ccSAndroid Build Coastguard Worker    */
264*77c1e3ccSAndroid Build Coastguard Worker   int num_rest_units;
265*77c1e3ccSAndroid Build Coastguard Worker 
266*77c1e3ccSAndroid Build Coastguard Worker   /*!
267*77c1e3ccSAndroid Build Coastguard Worker    * Number of vertical restoration units in this plane
268*77c1e3ccSAndroid Build Coastguard Worker    */
269*77c1e3ccSAndroid Build Coastguard Worker   int vert_units;
270*77c1e3ccSAndroid Build Coastguard Worker 
271*77c1e3ccSAndroid Build Coastguard Worker   /*!
272*77c1e3ccSAndroid Build Coastguard Worker    * Number of horizontal restoration units in this plane
273*77c1e3ccSAndroid Build Coastguard Worker    */
274*77c1e3ccSAndroid Build Coastguard Worker   int horz_units;
275*77c1e3ccSAndroid Build Coastguard Worker   /**@}*/
276*77c1e3ccSAndroid Build Coastguard Worker 
277*77c1e3ccSAndroid Build Coastguard Worker   /*!
278*77c1e3ccSAndroid Build Coastguard Worker    * Parameters for each restoration unit in this plane
279*77c1e3ccSAndroid Build Coastguard Worker    */
280*77c1e3ccSAndroid Build Coastguard Worker   RestorationUnitInfo *unit_info;
281*77c1e3ccSAndroid Build Coastguard Worker 
282*77c1e3ccSAndroid Build Coastguard Worker   /*!
283*77c1e3ccSAndroid Build Coastguard Worker    * Restoration Stripe boundary info
284*77c1e3ccSAndroid Build Coastguard Worker    */
285*77c1e3ccSAndroid Build Coastguard Worker   RestorationStripeBoundaries boundaries;
286*77c1e3ccSAndroid Build Coastguard Worker 
287*77c1e3ccSAndroid Build Coastguard Worker   /*!
288*77c1e3ccSAndroid Build Coastguard Worker    * Whether optimized lr can be used for speed.
289*77c1e3ccSAndroid Build Coastguard Worker    * That includes cases of no cdef and no superres, or if fast trial runs
290*77c1e3ccSAndroid Build Coastguard Worker    * are used on the encoder side.
291*77c1e3ccSAndroid Build Coastguard Worker    */
292*77c1e3ccSAndroid Build Coastguard Worker   int optimized_lr;
293*77c1e3ccSAndroid Build Coastguard Worker } RestorationInfo;
294*77c1e3ccSAndroid Build Coastguard Worker 
295*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */
296*77c1e3ccSAndroid Build Coastguard Worker 
set_default_sgrproj(SgrprojInfo * sgrproj_info)297*77c1e3ccSAndroid Build Coastguard Worker static inline void set_default_sgrproj(SgrprojInfo *sgrproj_info) {
298*77c1e3ccSAndroid Build Coastguard Worker   sgrproj_info->xqd[0] = (SGRPROJ_PRJ_MIN0 + SGRPROJ_PRJ_MAX0) / 2;
299*77c1e3ccSAndroid Build Coastguard Worker   sgrproj_info->xqd[1] = (SGRPROJ_PRJ_MIN1 + SGRPROJ_PRJ_MAX1) / 2;
300*77c1e3ccSAndroid Build Coastguard Worker }
301*77c1e3ccSAndroid Build Coastguard Worker 
set_default_wiener(WienerInfo * wiener_info)302*77c1e3ccSAndroid Build Coastguard Worker static inline void set_default_wiener(WienerInfo *wiener_info) {
303*77c1e3ccSAndroid Build Coastguard Worker   wiener_info->vfilter[0] = wiener_info->hfilter[0] = WIENER_FILT_TAP0_MIDV;
304*77c1e3ccSAndroid Build Coastguard Worker   wiener_info->vfilter[1] = wiener_info->hfilter[1] = WIENER_FILT_TAP1_MIDV;
305*77c1e3ccSAndroid Build Coastguard Worker   wiener_info->vfilter[2] = wiener_info->hfilter[2] = WIENER_FILT_TAP2_MIDV;
306*77c1e3ccSAndroid Build Coastguard Worker   wiener_info->vfilter[WIENER_HALFWIN] = wiener_info->hfilter[WIENER_HALFWIN] =
307*77c1e3ccSAndroid Build Coastguard Worker       -2 *
308*77c1e3ccSAndroid Build Coastguard Worker       (WIENER_FILT_TAP2_MIDV + WIENER_FILT_TAP1_MIDV + WIENER_FILT_TAP0_MIDV);
309*77c1e3ccSAndroid Build Coastguard Worker   wiener_info->vfilter[4] = wiener_info->hfilter[4] = WIENER_FILT_TAP2_MIDV;
310*77c1e3ccSAndroid Build Coastguard Worker   wiener_info->vfilter[5] = wiener_info->hfilter[5] = WIENER_FILT_TAP1_MIDV;
311*77c1e3ccSAndroid Build Coastguard Worker   wiener_info->vfilter[6] = wiener_info->hfilter[6] = WIENER_FILT_TAP0_MIDV;
312*77c1e3ccSAndroid Build Coastguard Worker }
313*77c1e3ccSAndroid Build Coastguard Worker 
314*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
315*77c1e3ccSAndroid Build Coastguard Worker   int h_start, h_end, v_start, v_end;
316*77c1e3ccSAndroid Build Coastguard Worker } RestorationTileLimits;
317*77c1e3ccSAndroid Build Coastguard Worker 
318*77c1e3ccSAndroid Build Coastguard Worker typedef void (*rest_unit_visitor_t)(const RestorationTileLimits *limits,
319*77c1e3ccSAndroid Build Coastguard Worker                                     int rest_unit_idx, void *priv,
320*77c1e3ccSAndroid Build Coastguard Worker                                     int32_t *tmpbuf,
321*77c1e3ccSAndroid Build Coastguard Worker                                     RestorationLineBuffers *rlbs,
322*77c1e3ccSAndroid Build Coastguard Worker                                     struct aom_internal_error_info *error_info);
323*77c1e3ccSAndroid Build Coastguard Worker 
324*77c1e3ccSAndroid Build Coastguard Worker typedef struct FilterFrameCtxt {
325*77c1e3ccSAndroid Build Coastguard Worker   const RestorationInfo *rsi;
326*77c1e3ccSAndroid Build Coastguard Worker   int ss_x, ss_y;
327*77c1e3ccSAndroid Build Coastguard Worker   int plane_w, plane_h;
328*77c1e3ccSAndroid Build Coastguard Worker   int highbd, bit_depth;
329*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *data8, *dst8;
330*77c1e3ccSAndroid Build Coastguard Worker   int data_stride, dst_stride;
331*77c1e3ccSAndroid Build Coastguard Worker } FilterFrameCtxt;
332*77c1e3ccSAndroid Build Coastguard Worker 
333*77c1e3ccSAndroid Build Coastguard Worker typedef struct AV1LrStruct {
334*77c1e3ccSAndroid Build Coastguard Worker   rest_unit_visitor_t on_rest_unit;
335*77c1e3ccSAndroid Build Coastguard Worker   FilterFrameCtxt ctxt[MAX_MB_PLANE];
336*77c1e3ccSAndroid Build Coastguard Worker   YV12_BUFFER_CONFIG *frame;
337*77c1e3ccSAndroid Build Coastguard Worker   YV12_BUFFER_CONFIG *dst;
338*77c1e3ccSAndroid Build Coastguard Worker } AV1LrStruct;
339*77c1e3ccSAndroid Build Coastguard Worker 
340*77c1e3ccSAndroid Build Coastguard Worker extern const sgr_params_type av1_sgr_params[SGRPROJ_PARAMS];
341*77c1e3ccSAndroid Build Coastguard Worker extern int sgrproj_mtable[SGRPROJ_PARAMS][2];
342*77c1e3ccSAndroid Build Coastguard Worker extern const int32_t av1_x_by_xplus1[256];
343*77c1e3ccSAndroid Build Coastguard Worker extern const int32_t av1_one_by_x[MAX_NELEM];
344*77c1e3ccSAndroid Build Coastguard Worker 
345*77c1e3ccSAndroid Build Coastguard Worker void av1_alloc_restoration_struct(struct AV1Common *cm, RestorationInfo *rsi,
346*77c1e3ccSAndroid Build Coastguard Worker                                   int is_uv);
347*77c1e3ccSAndroid Build Coastguard Worker void av1_free_restoration_struct(RestorationInfo *rst_info);
348*77c1e3ccSAndroid Build Coastguard Worker 
349*77c1e3ccSAndroid Build Coastguard Worker void av1_extend_frame(uint8_t *data, int width, int height, int stride,
350*77c1e3ccSAndroid Build Coastguard Worker                       int border_horz, int border_vert, int highbd);
351*77c1e3ccSAndroid Build Coastguard Worker void av1_decode_xq(const int *xqd, int *xq, const sgr_params_type *params);
352*77c1e3ccSAndroid Build Coastguard Worker 
353*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */
354*77c1e3ccSAndroid Build Coastguard Worker 
355*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Function for applying loop restoration filter to a single unit.
356*77c1e3ccSAndroid Build Coastguard Worker  *
357*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup in_loop_restoration
358*77c1e3ccSAndroid Build Coastguard Worker  * This function applies the loop restoration filter to a single
359*77c1e3ccSAndroid Build Coastguard Worker  * loop restoration unit.
360*77c1e3ccSAndroid Build Coastguard Worker  *
361*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       limits        Limits of the unit
362*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       rui           The parameters to use for this unit and its
363*77c1e3ccSAndroid Build Coastguard Worker  *                                coefficients
364*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       rsb           Deblocked pixels to use for stripe boundaries
365*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       rlbs          Space to use as a scratch buffer
366*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       ss_x          Horizontal subsampling for plane
367*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       ss_y          Vertical subsampling for plane
368*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       plane_w       Width of the current plane
369*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       plane_h       Height of the current plane
370*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       highbd        Whether high bitdepth pipeline is used
371*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       bit_depth     Bit-depth of the video
372*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       data8         Frame data (pointing at the top-left corner of
373*77c1e3ccSAndroid Build Coastguard Worker  *                                the frame, not the restoration unit).
374*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       stride        Stride of \c data8
375*77c1e3ccSAndroid Build Coastguard Worker  * \param[out]      dst8          Buffer where the results will be written. Like
376*77c1e3ccSAndroid Build Coastguard Worker  *                                \c data8, \c dst8 should point at the top-left
377*77c1e3ccSAndroid Build Coastguard Worker  *                                corner of the frame
378*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       dst_stride    Stride of \c dst8
379*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       tmpbuf        Scratch buffer used by the sgrproj filter
380*77c1e3ccSAndroid Build Coastguard Worker  *                                which should be at least SGRPROJ_TMPBUF_SIZE
381*77c1e3ccSAndroid Build Coastguard Worker  *                                big.
382*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       optimized_lr  Whether to use fast optimized Loop Restoration
383*77c1e3ccSAndroid Build Coastguard Worker  * \param[in,out]   error_info    Error info for reporting errors
384*77c1e3ccSAndroid Build Coastguard Worker  *
385*77c1e3ccSAndroid Build Coastguard Worker  * \remark Nothing is returned. Instead, the filtered unit is output in
386*77c1e3ccSAndroid Build Coastguard Worker  * \c dst8 at the proper restoration unit offset.
387*77c1e3ccSAndroid Build Coastguard Worker  */
388*77c1e3ccSAndroid Build Coastguard Worker void av1_loop_restoration_filter_unit(
389*77c1e3ccSAndroid Build Coastguard Worker     const RestorationTileLimits *limits, const RestorationUnitInfo *rui,
390*77c1e3ccSAndroid Build Coastguard Worker     const RestorationStripeBoundaries *rsb, RestorationLineBuffers *rlbs,
391*77c1e3ccSAndroid Build Coastguard Worker     int plane_w, int plane_h, int ss_x, int ss_y, int highbd, int bit_depth,
392*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *data8, int stride, uint8_t *dst8, int dst_stride, int32_t *tmpbuf,
393*77c1e3ccSAndroid Build Coastguard Worker     int optimized_lr, struct aom_internal_error_info *error_info);
394*77c1e3ccSAndroid Build Coastguard Worker 
395*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Function for applying loop restoration filter to a frame
396*77c1e3ccSAndroid Build Coastguard Worker  *
397*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup in_loop_restoration
398*77c1e3ccSAndroid Build Coastguard Worker  * This function applies the loop restoration filter to a frame.
399*77c1e3ccSAndroid Build Coastguard Worker  *
400*77c1e3ccSAndroid Build Coastguard Worker  * \param[in,out]   frame         Compressed frame buffer
401*77c1e3ccSAndroid Build Coastguard Worker  * \param[in,out]   cm            Pointer to top level common structure
402*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       optimized_lr  Whether to use fast optimized Loop Restoration
403*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]       lr_ctxt       Loop restoration context
404*77c1e3ccSAndroid Build Coastguard Worker  *
405*77c1e3ccSAndroid Build Coastguard Worker  * \remark Nothing is returned. Instead, the filtered frame is output in
406*77c1e3ccSAndroid Build Coastguard Worker  * \c frame.
407*77c1e3ccSAndroid Build Coastguard Worker  */
408*77c1e3ccSAndroid Build Coastguard Worker void av1_loop_restoration_filter_frame(YV12_BUFFER_CONFIG *frame,
409*77c1e3ccSAndroid Build Coastguard Worker                                        struct AV1Common *cm, int optimized_lr,
410*77c1e3ccSAndroid Build Coastguard Worker                                        void *lr_ctxt);
411*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */
412*77c1e3ccSAndroid Build Coastguard Worker 
413*77c1e3ccSAndroid Build Coastguard Worker void av1_loop_restoration_precal(void);
414*77c1e3ccSAndroid Build Coastguard Worker 
415*77c1e3ccSAndroid Build Coastguard Worker struct AV1LrSyncData;
416*77c1e3ccSAndroid Build Coastguard Worker 
417*77c1e3ccSAndroid Build Coastguard Worker typedef void (*sync_read_fn_t)(void *const lr_sync, int r, int c, int plane);
418*77c1e3ccSAndroid Build Coastguard Worker 
419*77c1e3ccSAndroid Build Coastguard Worker typedef void (*sync_write_fn_t)(void *const lr_sync, int r, int c,
420*77c1e3ccSAndroid Build Coastguard Worker                                 const int sb_cols, int plane);
421*77c1e3ccSAndroid Build Coastguard Worker 
422*77c1e3ccSAndroid Build Coastguard Worker // Return 1 iff the block at mi_row, mi_col with size bsize is a
423*77c1e3ccSAndroid Build Coastguard Worker // top-level superblock containing the top-left corner of at least one
424*77c1e3ccSAndroid Build Coastguard Worker // loop restoration unit.
425*77c1e3ccSAndroid Build Coastguard Worker //
426*77c1e3ccSAndroid Build Coastguard Worker // If the block is a top-level superblock, the function writes to
427*77c1e3ccSAndroid Build Coastguard Worker // *rcol0, *rcol1, *rrow0, *rrow1. This means that the parameters for all
428*77c1e3ccSAndroid Build Coastguard Worker // restoration units in the rectangle [*rcol0, *rcol1) x [*rrow0, *rrow1)
429*77c1e3ccSAndroid Build Coastguard Worker // are signaled in this superblock.
430*77c1e3ccSAndroid Build Coastguard Worker int av1_loop_restoration_corners_in_sb(const struct AV1Common *cm, int plane,
431*77c1e3ccSAndroid Build Coastguard Worker                                        int mi_row, int mi_col, BLOCK_SIZE bsize,
432*77c1e3ccSAndroid Build Coastguard Worker                                        int *rcol0, int *rcol1, int *rrow0,
433*77c1e3ccSAndroid Build Coastguard Worker                                        int *rrow1);
434*77c1e3ccSAndroid Build Coastguard Worker 
435*77c1e3ccSAndroid Build Coastguard Worker void av1_loop_restoration_save_boundary_lines(const YV12_BUFFER_CONFIG *frame,
436*77c1e3ccSAndroid Build Coastguard Worker                                               struct AV1Common *cm,
437*77c1e3ccSAndroid Build Coastguard Worker                                               int after_cdef);
438*77c1e3ccSAndroid Build Coastguard Worker void av1_loop_restoration_filter_frame_init(AV1LrStruct *lr_ctxt,
439*77c1e3ccSAndroid Build Coastguard Worker                                             YV12_BUFFER_CONFIG *frame,
440*77c1e3ccSAndroid Build Coastguard Worker                                             struct AV1Common *cm,
441*77c1e3ccSAndroid Build Coastguard Worker                                             int optimized_lr, int num_planes);
442*77c1e3ccSAndroid Build Coastguard Worker void av1_loop_restoration_copy_planes(AV1LrStruct *loop_rest_ctxt,
443*77c1e3ccSAndroid Build Coastguard Worker                                       struct AV1Common *cm, int num_planes);
444*77c1e3ccSAndroid Build Coastguard Worker void av1_foreach_rest_unit_in_row(
445*77c1e3ccSAndroid Build Coastguard Worker     RestorationTileLimits *limits, int plane_w,
446*77c1e3ccSAndroid Build Coastguard Worker     rest_unit_visitor_t on_rest_unit, int row_number, int unit_size,
447*77c1e3ccSAndroid Build Coastguard Worker     int hnum_rest_units, int vnum_rest_units, int plane, void *priv,
448*77c1e3ccSAndroid Build Coastguard Worker     int32_t *tmpbuf, RestorationLineBuffers *rlbs, sync_read_fn_t on_sync_read,
449*77c1e3ccSAndroid Build Coastguard Worker     sync_write_fn_t on_sync_write, struct AV1LrSyncData *const lr_sync,
450*77c1e3ccSAndroid Build Coastguard Worker     struct aom_internal_error_info *error_info);
451*77c1e3ccSAndroid Build Coastguard Worker 
452*77c1e3ccSAndroid Build Coastguard Worker void av1_get_upsampled_plane_size(const struct AV1Common *cm, int is_uv,
453*77c1e3ccSAndroid Build Coastguard Worker                                   int *plane_w, int *plane_h);
454*77c1e3ccSAndroid Build Coastguard Worker int av1_lr_count_units(int unit_size, int plane_size);
455*77c1e3ccSAndroid Build Coastguard Worker void av1_lr_sync_read_dummy(void *const lr_sync, int r, int c, int plane);
456*77c1e3ccSAndroid Build Coastguard Worker void av1_lr_sync_write_dummy(void *const lr_sync, int r, int c,
457*77c1e3ccSAndroid Build Coastguard Worker                              const int sb_cols, int plane);
458*77c1e3ccSAndroid Build Coastguard Worker 
459*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */
460*77c1e3ccSAndroid Build Coastguard Worker 
461*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
462*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
463*77c1e3ccSAndroid Build Coastguard Worker #endif
464*77c1e3ccSAndroid Build Coastguard Worker 
465*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AV1_COMMON_RESTORATION_H_
466