1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #ifndef AV1_COMMON_RESTORATION_H_
13 #define AV1_COMMON_RESTORATION_H_
14
15 #include "aom_ports/mem.h"
16 #include "./aom_config.h"
17
18 #include "av1/common/blockd.h"
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #define CLIP(x, lo, hi) ((x) < (lo) ? (lo) : (x) > (hi) ? (hi) : (x))
25 #define RINT(x) ((x) < 0 ? (int)((x)-0.5) : (int)((x) + 0.5))
26
27 #define RESTORATION_PROC_UNIT_SIZE 64
28
29 #if CONFIG_STRIPED_LOOP_RESTORATION
30 // Filter tile grid offset upwards compared to the superblock grid
31 #define RESTORATION_TILE_OFFSET 8
32 #endif
33
34 #if CONFIG_STRIPED_LOOP_RESTORATION
35 #define SGRPROJ_BORDER_VERT 2 // Vertical border used for Sgr
36 #else
37 #define SGRPROJ_BORDER_VERT 1 // Vertical border used for Sgr
38 #endif
39 #define SGRPROJ_BORDER_HORZ 2 // Horizontal border used for Sgr
40
41 #if CONFIG_STRIPED_LOOP_RESTORATION
42 #define WIENER_BORDER_VERT 2 // Vertical border used for Wiener
43 #else
44 #define WIENER_BORDER_VERT 1 // Vertical border used for Wiener
45 #endif
46 #define WIENER_HALFWIN 3
47 #define WIENER_BORDER_HORZ (WIENER_HALFWIN) // Horizontal border for Wiener
48
49 // RESTORATION_BORDER_VERT determines line buffer requirement for LR.
50 // Should be set at the max of SGRPROJ_BORDER_VERT and WIENER_BORDER_VERT.
51 // Note the line buffer needed is twice the value of this macro.
52 #if SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT
53 #define RESTORATION_BORDER_VERT (SGRPROJ_BORDER_VERT)
54 #else
55 #define RESTORATION_BORDER_VERT (WIENER_BORDER_VERT)
56 #endif // SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT
57
58 #if SGRPROJ_BORDER_HORZ >= WIENER_BORDER_HORZ
59 #define RESTORATION_BORDER_HORZ (SGRPROJ_BORDER_HORZ)
60 #else
61 #define RESTORATION_BORDER_HORZ (WIENER_BORDER_HORZ)
62 #endif // SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT
63
64 #if CONFIG_STRIPED_LOOP_RESTORATION
65 // Additional pixels to the left and right in above/below buffers
66 // It is RESTORATION_BORDER_HORZ rounded up to get nicer buffer alignment
67 #define RESTORATION_EXTRA_HORZ 4
68 #endif
69
70 // Pad up to 20 more (may be much less is needed)
71 #define RESTORATION_PADDING 20
72 #define RESTORATION_PROC_UNIT_PELS \
73 ((RESTORATION_PROC_UNIT_SIZE + RESTORATION_BORDER_HORZ * 2 + \
74 RESTORATION_PADDING) * \
75 (RESTORATION_PROC_UNIT_SIZE + RESTORATION_BORDER_VERT * 2 + \
76 RESTORATION_PADDING))
77
78 #define RESTORATION_TILESIZE_MAX 256
79 #if CONFIG_STRIPED_LOOP_RESTORATION
80 #define RESTORATION_TILEPELS_HORZ_MAX \
81 (RESTORATION_TILESIZE_MAX * 3 / 2 + 2 * RESTORATION_BORDER_HORZ + 16)
82 #define RESTORATION_TILEPELS_VERT_MAX \
83 ((RESTORATION_TILESIZE_MAX * 3 / 2 + 2 * RESTORATION_BORDER_VERT + \
84 RESTORATION_TILE_OFFSET))
85 #define RESTORATION_TILEPELS_MAX \
86 (RESTORATION_TILEPELS_HORZ_MAX * RESTORATION_TILEPELS_VERT_MAX)
87 #else
88 #define RESTORATION_TILEPELS_MAX \
89 ((RESTORATION_TILESIZE_MAX * 3 / 2 + 2 * RESTORATION_BORDER_HORZ + 16) * \
90 (RESTORATION_TILESIZE_MAX * 3 / 2 + 2 * RESTORATION_BORDER_VERT))
91 #endif
92
93 // Two 32-bit buffers needed for the restored versions from two filters
94 // TODO(debargha, rupert): Refactor to not need the large tilesize to be stored
95 // on the decoder side.
96 #define SGRPROJ_TMPBUF_SIZE (RESTORATION_TILEPELS_MAX * 2 * sizeof(int32_t))
97
98 #define SGRPROJ_EXTBUF_SIZE (0)
99 #define SGRPROJ_PARAMS_BITS 4
100 #define SGRPROJ_PARAMS (1 << SGRPROJ_PARAMS_BITS)
101 #define USE_HIGHPASS_IN_SGRPROJ 0
102
103 // Precision bits for projection
104 #define SGRPROJ_PRJ_BITS 7
105 // Restoration precision bits generated higher than source before projection
106 #define SGRPROJ_RST_BITS 4
107 // Internal precision bits for core selfguided_restoration
108 #define SGRPROJ_SGR_BITS 8
109 #define SGRPROJ_SGR (1 << SGRPROJ_SGR_BITS)
110
111 #if USE_HIGHPASS_IN_SGRPROJ
112 #define SGRPROJ_PRJ_MIN0 (-(1 << SGRPROJ_PRJ_BITS) / 8)
113 #define SGRPROJ_PRJ_MAX0 (SGRPROJ_PRJ_MIN0 + (1 << SGRPROJ_PRJ_BITS) - 1)
114 #define SGRPROJ_PRJ_MIN1 (-(1 << SGRPROJ_PRJ_BITS) / 2)
115 #define SGRPROJ_PRJ_MAX1 (SGRPROJ_PRJ_MIN1 + (1 << SGRPROJ_PRJ_BITS) - 1)
116 #else
117 #define SGRPROJ_PRJ_MIN0 (-(1 << SGRPROJ_PRJ_BITS) * 3 / 4)
118 #define SGRPROJ_PRJ_MAX0 (SGRPROJ_PRJ_MIN0 + (1 << SGRPROJ_PRJ_BITS) - 1)
119 #define SGRPROJ_PRJ_MIN1 (-(1 << SGRPROJ_PRJ_BITS) / 4)
120 #define SGRPROJ_PRJ_MAX1 (SGRPROJ_PRJ_MIN1 + (1 << SGRPROJ_PRJ_BITS) - 1)
121 #endif // USE_HIGHPASS_IN_SGRPROJ
122
123 #define SGRPROJ_PRJ_SUBEXP_K 4
124
125 #define SGRPROJ_BITS (SGRPROJ_PRJ_BITS * 2 + SGRPROJ_PARAMS_BITS)
126
127 #define MAX_RADIUS 2 // Only 1, 2, 3 allowed
128 #define MAX_EPS 80 // Max value of eps
129 #define MAX_NELEM ((2 * MAX_RADIUS + 1) * (2 * MAX_RADIUS + 1))
130 #define SGRPROJ_MTABLE_BITS 20
131 #define SGRPROJ_RECIP_BITS 12
132
133 #define WIENER_HALFWIN1 (WIENER_HALFWIN + 1)
134 #define WIENER_WIN (2 * WIENER_HALFWIN + 1)
135 #define WIENER_WIN2 ((WIENER_WIN) * (WIENER_WIN))
136 #define WIENER_TMPBUF_SIZE (0)
137 #define WIENER_EXTBUF_SIZE (0)
138
139 // If WIENER_WIN_CHROMA == WIENER_WIN - 2, that implies 5x5 filters are used for
140 // chroma. To use 7x7 for chroma set WIENER_WIN_CHROMA to WIENER_WIN.
141 #define WIENER_WIN_CHROMA (WIENER_WIN - 2)
142
143 #define WIENER_FILT_PREC_BITS 7
144 #define WIENER_FILT_STEP (1 << WIENER_FILT_PREC_BITS)
145
146 // Whether to use high intermediate precision filtering
147 #define USE_WIENER_HIGH_INTERMEDIATE_PRECISION 1
148
149 // Central values for the taps
150 #define WIENER_FILT_TAP0_MIDV (3)
151 #define WIENER_FILT_TAP1_MIDV (-7)
152 #define WIENER_FILT_TAP2_MIDV (15)
153 #define WIENER_FILT_TAP3_MIDV \
154 (WIENER_FILT_STEP - \
155 2 * (WIENER_FILT_TAP0_MIDV + WIENER_FILT_TAP1_MIDV + \
156 WIENER_FILT_TAP2_MIDV))
157
158 #define WIENER_FILT_TAP0_BITS 4
159 #define WIENER_FILT_TAP1_BITS 5
160 #define WIENER_FILT_TAP2_BITS 6
161
162 #define WIENER_FILT_BITS \
163 ((WIENER_FILT_TAP0_BITS + WIENER_FILT_TAP1_BITS + WIENER_FILT_TAP2_BITS) * 2)
164
165 #define WIENER_FILT_TAP0_MINV \
166 (WIENER_FILT_TAP0_MIDV - (1 << WIENER_FILT_TAP0_BITS) / 2)
167 #define WIENER_FILT_TAP1_MINV \
168 (WIENER_FILT_TAP1_MIDV - (1 << WIENER_FILT_TAP1_BITS) / 2)
169 #define WIENER_FILT_TAP2_MINV \
170 (WIENER_FILT_TAP2_MIDV - (1 << WIENER_FILT_TAP2_BITS) / 2)
171
172 #define WIENER_FILT_TAP0_MAXV \
173 (WIENER_FILT_TAP0_MIDV - 1 + (1 << WIENER_FILT_TAP0_BITS) / 2)
174 #define WIENER_FILT_TAP1_MAXV \
175 (WIENER_FILT_TAP1_MIDV - 1 + (1 << WIENER_FILT_TAP1_BITS) / 2)
176 #define WIENER_FILT_TAP2_MAXV \
177 (WIENER_FILT_TAP2_MIDV - 1 + (1 << WIENER_FILT_TAP2_BITS) / 2)
178
179 #define WIENER_FILT_TAP0_SUBEXP_K 1
180 #define WIENER_FILT_TAP1_SUBEXP_K 2
181 #define WIENER_FILT_TAP2_SUBEXP_K 3
182
183 // Max of SGRPROJ_TMPBUF_SIZE, DOMAINTXFMRF_TMPBUF_SIZE, WIENER_TMPBUF_SIZE
184 #define RESTORATION_TMPBUF_SIZE (SGRPROJ_TMPBUF_SIZE)
185
186 // Max of SGRPROJ_EXTBUF_SIZE, WIENER_EXTBUF_SIZE
187 #define RESTORATION_EXTBUF_SIZE (WIENER_EXTBUF_SIZE)
188
189 // Check the assumptions of the existing code
190 #if SUBPEL_TAPS != WIENER_WIN + 1
191 #error "Wiener filter currently only works if SUBPEL_TAPS == WIENER_WIN + 1"
192 #endif
193 #if WIENER_FILT_PREC_BITS != 7
194 #error "Wiener filter currently only works if WIENER_FILT_PREC_BITS == 7"
195 #endif
196
197 typedef struct {
198 #if USE_HIGHPASS_IN_SGRPROJ
199 int corner;
200 int edge;
201 #else
202 int r1;
203 int e1;
204 #endif // USE_HIGHPASS_IN_SGRPROJ
205 int r2;
206 int e2;
207 } sgr_params_type;
208
209 typedef struct {
210 int restoration_tilesize;
211 int procunit_width, procunit_height;
212 RestorationType frame_restoration_type;
213 RestorationType *restoration_type;
214 // Wiener filter
215 WienerInfo *wiener_info;
216 // Selfguided proj filter
217 SgrprojInfo *sgrproj_info;
218 } RestorationInfo;
219
220 typedef struct {
221 RestorationInfo *rsi;
222 int keyframe;
223 int ntiles;
224 int tile_width, tile_height;
225 int nhtiles, nvtiles;
226 int32_t *tmpbuf;
227 #if CONFIG_STRIPED_LOOP_RESTORATION
228 int component;
229 int subsampling_y;
230 uint8_t *stripe_boundary_above[MAX_MB_PLANE];
231 uint8_t *stripe_boundary_below[MAX_MB_PLANE];
232 int stripe_boundary_stride[MAX_MB_PLANE];
233 // Temporary buffers to save/restore 2 lines above/below the restoration
234 // stripe
235 // Allow for filter margin to left and right
236 uint16_t
237 tmp_save_above[2][RESTORATION_TILESIZE_MAX + 2 * RESTORATION_EXTRA_HORZ];
238 uint16_t
239 tmp_save_below[2][RESTORATION_TILESIZE_MAX + 2 * RESTORATION_EXTRA_HORZ];
240 #endif
241 } RestorationInternal;
242
set_default_sgrproj(SgrprojInfo * sgrproj_info)243 static INLINE void set_default_sgrproj(SgrprojInfo *sgrproj_info) {
244 sgrproj_info->xqd[0] = (SGRPROJ_PRJ_MIN0 + SGRPROJ_PRJ_MAX0) / 2;
245 sgrproj_info->xqd[1] = (SGRPROJ_PRJ_MIN1 + SGRPROJ_PRJ_MAX1) / 2;
246 }
247
set_default_wiener(WienerInfo * wiener_info)248 static INLINE void set_default_wiener(WienerInfo *wiener_info) {
249 wiener_info->vfilter[0] = wiener_info->hfilter[0] = WIENER_FILT_TAP0_MIDV;
250 wiener_info->vfilter[1] = wiener_info->hfilter[1] = WIENER_FILT_TAP1_MIDV;
251 wiener_info->vfilter[2] = wiener_info->hfilter[2] = WIENER_FILT_TAP2_MIDV;
252 wiener_info->vfilter[WIENER_HALFWIN] = wiener_info->hfilter[WIENER_HALFWIN] =
253 -2 *
254 (WIENER_FILT_TAP2_MIDV + WIENER_FILT_TAP1_MIDV + WIENER_FILT_TAP0_MIDV);
255 wiener_info->vfilter[4] = wiener_info->hfilter[4] = WIENER_FILT_TAP2_MIDV;
256 wiener_info->vfilter[5] = wiener_info->hfilter[5] = WIENER_FILT_TAP1_MIDV;
257 wiener_info->vfilter[6] = wiener_info->hfilter[6] = WIENER_FILT_TAP0_MIDV;
258 }
259
av1_get_rest_ntiles(int width,int height,int tilesize,int * tile_width,int * tile_height,int * nhtiles,int * nvtiles)260 static INLINE int av1_get_rest_ntiles(int width, int height, int tilesize,
261 int *tile_width, int *tile_height,
262 int *nhtiles, int *nvtiles) {
263 int nhtiles_, nvtiles_;
264 int tile_width_, tile_height_;
265 tile_width_ = (tilesize < 0) ? width : AOMMIN(tilesize, width);
266 tile_height_ = (tilesize < 0) ? height : AOMMIN(tilesize, height);
267 assert(tile_width_ > 0 && tile_height_ > 0);
268
269 nhtiles_ = (width + (tile_width_ >> 1)) / tile_width_;
270 nvtiles_ = (height + (tile_height_ >> 1)) / tile_height_;
271 if (tile_width) *tile_width = tile_width_;
272 if (tile_height) *tile_height = tile_height_;
273 if (nhtiles) *nhtiles = nhtiles_;
274 if (nvtiles) *nvtiles = nvtiles_;
275 return (nhtiles_ * nvtiles_);
276 }
277
278 typedef struct { int h_start, h_end, v_start, v_end; } RestorationTileLimits;
279
280 static INLINE RestorationTileLimits
av1_get_rest_tile_limits(int tile_idx,int nhtiles,int nvtiles,int tile_width,int tile_height,int im_width,int im_height,int subsampling_y)281 av1_get_rest_tile_limits(int tile_idx, int nhtiles, int nvtiles, int tile_width,
282 int tile_height, int im_width,
283 #if CONFIG_STRIPED_LOOP_RESTORATION
284 int im_height, int subsampling_y) {
285 #else
286 int im_height) {
287 #endif
288 const int htile_idx = tile_idx % nhtiles;
289 const int vtile_idx = tile_idx / nhtiles;
290 RestorationTileLimits limits;
291 limits.h_start = htile_idx * tile_width;
292 limits.v_start = vtile_idx * tile_height;
293 limits.h_end =
294 (htile_idx < nhtiles - 1) ? limits.h_start + tile_width : im_width;
295 limits.v_end =
296 (vtile_idx < nvtiles - 1) ? limits.v_start + tile_height : im_height;
297 #if CONFIG_STRIPED_LOOP_RESTORATION
298 // Offset the tile upwards to align with the restoration processing stripe
299 limits.v_start -= RESTORATION_TILE_OFFSET >> subsampling_y;
300 if (limits.v_start < 0) limits.v_start = 0;
301 if (limits.v_end < im_height)
302 limits.v_end -= RESTORATION_TILE_OFFSET >> subsampling_y;
303 #endif
304 return limits;
305 }
306
307 extern const sgr_params_type sgr_params[SGRPROJ_PARAMS];
308 extern int sgrproj_mtable[MAX_EPS][MAX_NELEM];
309 extern const int32_t x_by_xplus1[256];
310 extern const int32_t one_by_x[MAX_NELEM];
311
312 int av1_alloc_restoration_struct(struct AV1Common *cm,
313 RestorationInfo *rst_info, int width,
314 int height);
315 void av1_free_restoration_struct(RestorationInfo *rst_info);
316
317 void extend_frame(uint8_t *data, int width, int height, int stride,
318 int border_horz, int border_vert);
319 #if CONFIG_HIGHBITDEPTH
320 void extend_frame_highbd(uint16_t *data, int width, int height, int stride,
321 int border_horz, int border_vert);
322 #endif // CONFIG_HIGHBITDEPTH
323 void decode_xq(int *xqd, int *xq);
324 void av1_loop_restoration_frame(YV12_BUFFER_CONFIG *frame, struct AV1Common *cm,
325 RestorationInfo *rsi, int components_pattern,
326 int partial_frame, YV12_BUFFER_CONFIG *dst);
327 void av1_loop_restoration_precal();
328
329 // Return 1 iff the block at mi_row, mi_col with size bsize is a
330 // top-level superblock containing the top-left corner of at least one
331 // loop restoration tile.
332 //
333 // If the block is a top-level superblock, the function writes to
334 // *rcol0, *rcol1, *rrow0, *rrow1. The rectangle of indices given by
335 // [*rcol0, *rcol1) x [*rrow0, *rrow1) will point at the set of rtiles
336 // whose top left corners lie in the superblock. Note that the set is
337 // only nonempty if *rcol0 < *rcol1 and *rrow0 < *rrow1.
338 int av1_loop_restoration_corners_in_sb(const struct AV1Common *cm, int plane,
339 int mi_row, int mi_col, BLOCK_SIZE bsize,
340 int *rcol0, int *rcol1, int *rrow0,
341 int *rrow1, int *nhtiles);
342
343 void av1_loop_restoration_save_boundary_lines(YV12_BUFFER_CONFIG *frame,
344 struct AV1Common *cm);
345 #ifdef __cplusplus
346 } // extern "C"
347 #endif
348
349 #endif // AV1_COMMON_RESTORATION_H_
350