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 AOM_AV1_COMMON_RESTORATION_H_
13 #define AOM_AV1_COMMON_RESTORATION_H_
14
15 #include "aom_ports/mem.h"
16 #include "config/aom_config.h"
17
18 #include "av1/common/blockd.h"
19 #include "av1/common/enums.h"
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 // Border for Loop restoration buffer
26 #define AOM_RESTORATION_FRAME_BORDER 32
27 #define CLIP(x, lo, hi) ((x) < (lo) ? (lo) : (x) > (hi) ? (hi) : (x))
28 #define RINT(x) ((x) < 0 ? (int)((x)-0.5) : (int)((x) + 0.5))
29
30 #define RESTORATION_PROC_UNIT_SIZE 64
31
32 // Filter tile grid offset upwards compared to the superblock grid
33 #define RESTORATION_UNIT_OFFSET 8
34
35 #define SGRPROJ_BORDER_VERT 3 // Vertical border used for Sgr
36 #define SGRPROJ_BORDER_HORZ 3 // Horizontal border used for Sgr
37
38 #define WIENER_BORDER_VERT 2 // Vertical border used for Wiener
39 #define WIENER_HALFWIN 3
40 #define WIENER_BORDER_HORZ (WIENER_HALFWIN) // Horizontal border for Wiener
41
42 // RESTORATION_BORDER_VERT determines line buffer requirement for LR.
43 // Should be set at the max of SGRPROJ_BORDER_VERT and WIENER_BORDER_VERT.
44 // Note the line buffer needed is twice the value of this macro.
45 #if SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT
46 #define RESTORATION_BORDER_VERT (SGRPROJ_BORDER_VERT)
47 #else
48 #define RESTORATION_BORDER_VERT (WIENER_BORDER_VERT)
49 #endif // SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT
50
51 #if SGRPROJ_BORDER_HORZ >= WIENER_BORDER_HORZ
52 #define RESTORATION_BORDER_HORZ (SGRPROJ_BORDER_HORZ)
53 #else
54 #define RESTORATION_BORDER_HORZ (WIENER_BORDER_HORZ)
55 #endif // SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT
56
57 // How many border pixels do we need for each processing unit?
58 #define RESTORATION_BORDER 3
59
60 // How many rows of deblocked pixels do we save above/below each processing
61 // stripe?
62 #define RESTORATION_CTX_VERT 2
63
64 // Additional pixels to the left and right in above/below buffers
65 // It is RESTORATION_BORDER_HORZ rounded up to get nicer buffer alignment
66 #define RESTORATION_EXTRA_HORZ 4
67
68 // Pad up to 20 more (may be much less is needed)
69 #define RESTORATION_PADDING 20
70 #define RESTORATION_PROC_UNIT_PELS \
71 ((RESTORATION_PROC_UNIT_SIZE + RESTORATION_BORDER_HORZ * 2 + \
72 RESTORATION_PADDING) * \
73 (RESTORATION_PROC_UNIT_SIZE + RESTORATION_BORDER_VERT * 2 + \
74 RESTORATION_PADDING))
75
76 #define RESTORATION_UNITSIZE_MAX 256
77 #define RESTORATION_UNITPELS_HORZ_MAX \
78 (RESTORATION_UNITSIZE_MAX * 3 / 2 + 2 * RESTORATION_BORDER_HORZ + 16)
79 #define RESTORATION_UNITPELS_VERT_MAX \
80 ((RESTORATION_UNITSIZE_MAX * 3 / 2 + 2 * RESTORATION_BORDER_VERT + \
81 RESTORATION_UNIT_OFFSET))
82 #define RESTORATION_UNITPELS_MAX \
83 (RESTORATION_UNITPELS_HORZ_MAX * RESTORATION_UNITPELS_VERT_MAX)
84
85 // Two 32-bit buffers needed for the restored versions from two filters
86 // TODO(debargha, rupert): Refactor to not need the large tilesize to be stored
87 // on the decoder side.
88 #define SGRPROJ_TMPBUF_SIZE (RESTORATION_UNITPELS_MAX * 2 * sizeof(int32_t))
89
90 #define SGRPROJ_EXTBUF_SIZE (0)
91 #define SGRPROJ_PARAMS_BITS 4
92 #define SGRPROJ_PARAMS (1 << SGRPROJ_PARAMS_BITS)
93
94 // Precision bits for projection
95 #define SGRPROJ_PRJ_BITS 7
96 // Restoration precision bits generated higher than source before projection
97 #define SGRPROJ_RST_BITS 4
98 // Internal precision bits for core selfguided_restoration
99 #define SGRPROJ_SGR_BITS 8
100 #define SGRPROJ_SGR (1 << SGRPROJ_SGR_BITS)
101
102 #define SGRPROJ_PRJ_MIN0 (-(1 << SGRPROJ_PRJ_BITS) * 3 / 4)
103 #define SGRPROJ_PRJ_MAX0 (SGRPROJ_PRJ_MIN0 + (1 << SGRPROJ_PRJ_BITS) - 1)
104 #define SGRPROJ_PRJ_MIN1 (-(1 << SGRPROJ_PRJ_BITS) / 4)
105 #define SGRPROJ_PRJ_MAX1 (SGRPROJ_PRJ_MIN1 + (1 << SGRPROJ_PRJ_BITS) - 1)
106
107 #define SGRPROJ_PRJ_SUBEXP_K 4
108
109 #define SGRPROJ_BITS (SGRPROJ_PRJ_BITS * 2 + SGRPROJ_PARAMS_BITS)
110
111 #define MAX_RADIUS 2 // Only 1, 2, 3 allowed
112 #define MAX_NELEM ((2 * MAX_RADIUS + 1) * (2 * MAX_RADIUS + 1))
113 #define SGRPROJ_MTABLE_BITS 20
114 #define SGRPROJ_RECIP_BITS 12
115
116 #define WIENER_HALFWIN1 (WIENER_HALFWIN + 1)
117 #define WIENER_WIN (2 * WIENER_HALFWIN + 1)
118 #define WIENER_WIN2 ((WIENER_WIN) * (WIENER_WIN))
119 #define WIENER_TMPBUF_SIZE (0)
120 #define WIENER_EXTBUF_SIZE (0)
121
122 // If WIENER_WIN_CHROMA == WIENER_WIN - 2, that implies 5x5 filters are used for
123 // chroma. To use 7x7 for chroma set WIENER_WIN_CHROMA to WIENER_WIN.
124 #define WIENER_WIN_CHROMA (WIENER_WIN - 2)
125 #define WIENER_WIN_REDUCED (WIENER_WIN - 2)
126 #define WIENER_WIN2_CHROMA ((WIENER_WIN_CHROMA) * (WIENER_WIN_CHROMA))
127
128 #define WIENER_FILT_PREC_BITS 7
129 #define WIENER_FILT_STEP (1 << WIENER_FILT_PREC_BITS)
130
131 // Central values for the taps
132 #define WIENER_FILT_TAP0_MIDV (3)
133 #define WIENER_FILT_TAP1_MIDV (-7)
134 #define WIENER_FILT_TAP2_MIDV (15)
135 #define WIENER_FILT_TAP3_MIDV \
136 (WIENER_FILT_STEP - 2 * (WIENER_FILT_TAP0_MIDV + WIENER_FILT_TAP1_MIDV + \
137 WIENER_FILT_TAP2_MIDV))
138
139 #define WIENER_FILT_TAP0_BITS 4
140 #define WIENER_FILT_TAP1_BITS 5
141 #define WIENER_FILT_TAP2_BITS 6
142
143 #define WIENER_FILT_BITS \
144 ((WIENER_FILT_TAP0_BITS + WIENER_FILT_TAP1_BITS + WIENER_FILT_TAP2_BITS) * 2)
145
146 #define WIENER_FILT_TAP0_MINV \
147 (WIENER_FILT_TAP0_MIDV - (1 << WIENER_FILT_TAP0_BITS) / 2)
148 #define WIENER_FILT_TAP1_MINV \
149 (WIENER_FILT_TAP1_MIDV - (1 << WIENER_FILT_TAP1_BITS) / 2)
150 #define WIENER_FILT_TAP2_MINV \
151 (WIENER_FILT_TAP2_MIDV - (1 << WIENER_FILT_TAP2_BITS) / 2)
152
153 #define WIENER_FILT_TAP0_MAXV \
154 (WIENER_FILT_TAP0_MIDV - 1 + (1 << WIENER_FILT_TAP0_BITS) / 2)
155 #define WIENER_FILT_TAP1_MAXV \
156 (WIENER_FILT_TAP1_MIDV - 1 + (1 << WIENER_FILT_TAP1_BITS) / 2)
157 #define WIENER_FILT_TAP2_MAXV \
158 (WIENER_FILT_TAP2_MIDV - 1 + (1 << WIENER_FILT_TAP2_BITS) / 2)
159
160 #define WIENER_FILT_TAP0_SUBEXP_K 1
161 #define WIENER_FILT_TAP1_SUBEXP_K 2
162 #define WIENER_FILT_TAP2_SUBEXP_K 3
163
164 // Max of SGRPROJ_TMPBUF_SIZE, DOMAINTXFMRF_TMPBUF_SIZE, WIENER_TMPBUF_SIZE
165 #define RESTORATION_TMPBUF_SIZE (SGRPROJ_TMPBUF_SIZE)
166
167 // Max of SGRPROJ_EXTBUF_SIZE, WIENER_EXTBUF_SIZE
168 #define RESTORATION_EXTBUF_SIZE (WIENER_EXTBUF_SIZE)
169
170 // Check the assumptions of the existing code
171 #if SUBPEL_TAPS != WIENER_WIN + 1
172 #error "Wiener filter currently only works if SUBPEL_TAPS == WIENER_WIN + 1"
173 #endif
174 #if WIENER_FILT_PREC_BITS != 7
175 #error "Wiener filter currently only works if WIENER_FILT_PREC_BITS == 7"
176 #endif
177
178 #define LR_TILE_ROW 0
179 #define LR_TILE_COL 0
180 #define LR_TILE_COLS 1
181
182 typedef struct {
183 int r[2]; // radii
184 int s[2]; // sgr parameters for r[0] and r[1], based on GenSgrprojVtable()
185 } sgr_params_type;
186
187 typedef struct {
188 RestorationType restoration_type;
189 WienerInfo wiener_info;
190 SgrprojInfo sgrproj_info;
191 } RestorationUnitInfo;
192
193 // A restoration line buffer needs space for two lines plus a horizontal filter
194 // margin of RESTORATION_EXTRA_HORZ on each side.
195 #define RESTORATION_LINEBUFFER_WIDTH \
196 (RESTORATION_UNITSIZE_MAX * 3 / 2 + 2 * RESTORATION_EXTRA_HORZ)
197
198 // Similarly, the column buffers (used when we're at a vertical tile edge
199 // that we can't filter across) need space for one processing unit's worth
200 // of pixels, plus the top/bottom border width
201 #define RESTORATION_COLBUFFER_HEIGHT \
202 (RESTORATION_PROC_UNIT_SIZE + 2 * RESTORATION_BORDER)
203
204 typedef struct {
205 // Temporary buffers to save/restore 3 lines above/below the restoration
206 // stripe.
207 uint16_t tmp_save_above[RESTORATION_BORDER][RESTORATION_LINEBUFFER_WIDTH];
208 uint16_t tmp_save_below[RESTORATION_BORDER][RESTORATION_LINEBUFFER_WIDTH];
209 } RestorationLineBuffers;
210
211 typedef struct {
212 uint8_t *stripe_boundary_above;
213 uint8_t *stripe_boundary_below;
214 int stripe_boundary_stride;
215 int stripe_boundary_size;
216 } RestorationStripeBoundaries;
217
218 typedef struct {
219 RestorationType frame_restoration_type;
220 int restoration_unit_size;
221
222 // Fields below here are allocated and initialised by
223 // av1_alloc_restoration_struct. (horz_)units_per_tile give the number of
224 // restoration units in (one row of) the largest tile in the frame. The data
225 // in unit_info is laid out with units_per_tile entries for each tile, which
226 // have stride horz_units_per_tile.
227 //
228 // Even if there are tiles of different sizes, the data in unit_info is laid
229 // out as if all tiles are of full size.
230 int units_per_tile;
231 int vert_units_per_tile, horz_units_per_tile;
232 RestorationUnitInfo *unit_info;
233 RestorationStripeBoundaries boundaries;
234 int optimized_lr;
235 } RestorationInfo;
236
set_default_sgrproj(SgrprojInfo * sgrproj_info)237 static INLINE void set_default_sgrproj(SgrprojInfo *sgrproj_info) {
238 sgrproj_info->xqd[0] = (SGRPROJ_PRJ_MIN0 + SGRPROJ_PRJ_MAX0) / 2;
239 sgrproj_info->xqd[1] = (SGRPROJ_PRJ_MIN1 + SGRPROJ_PRJ_MAX1) / 2;
240 }
241
set_default_wiener(WienerInfo * wiener_info)242 static INLINE void set_default_wiener(WienerInfo *wiener_info) {
243 wiener_info->vfilter[0] = wiener_info->hfilter[0] = WIENER_FILT_TAP0_MIDV;
244 wiener_info->vfilter[1] = wiener_info->hfilter[1] = WIENER_FILT_TAP1_MIDV;
245 wiener_info->vfilter[2] = wiener_info->hfilter[2] = WIENER_FILT_TAP2_MIDV;
246 wiener_info->vfilter[WIENER_HALFWIN] = wiener_info->hfilter[WIENER_HALFWIN] =
247 -2 *
248 (WIENER_FILT_TAP2_MIDV + WIENER_FILT_TAP1_MIDV + WIENER_FILT_TAP0_MIDV);
249 wiener_info->vfilter[4] = wiener_info->hfilter[4] = WIENER_FILT_TAP2_MIDV;
250 wiener_info->vfilter[5] = wiener_info->hfilter[5] = WIENER_FILT_TAP1_MIDV;
251 wiener_info->vfilter[6] = wiener_info->hfilter[6] = WIENER_FILT_TAP0_MIDV;
252 }
253
254 typedef struct {
255 int h_start, h_end, v_start, v_end;
256 } RestorationTileLimits;
257
258 typedef void (*rest_unit_visitor_t)(const RestorationTileLimits *limits,
259 const AV1PixelRect *tile_rect,
260 int rest_unit_idx, void *priv,
261 int32_t *tmpbuf,
262 RestorationLineBuffers *rlbs);
263
264 typedef struct FilterFrameCtxt {
265 const RestorationInfo *rsi;
266 int tile_stripe0;
267 int ss_x, ss_y;
268 int highbd, bit_depth;
269 uint8_t *data8, *dst8;
270 int data_stride, dst_stride;
271 AV1PixelRect tile_rect;
272 } FilterFrameCtxt;
273
274 typedef struct AV1LrStruct {
275 rest_unit_visitor_t on_rest_unit;
276 FilterFrameCtxt ctxt[MAX_MB_PLANE];
277 YV12_BUFFER_CONFIG *frame;
278 YV12_BUFFER_CONFIG *dst;
279 } AV1LrStruct;
280
281 extern const sgr_params_type av1_sgr_params[SGRPROJ_PARAMS];
282 extern int sgrproj_mtable[SGRPROJ_PARAMS][2];
283 extern const int32_t av1_x_by_xplus1[256];
284 extern const int32_t av1_one_by_x[MAX_NELEM];
285
286 void av1_alloc_restoration_struct(struct AV1Common *cm, RestorationInfo *rsi,
287 int is_uv);
288 void av1_free_restoration_struct(RestorationInfo *rst_info);
289
290 void av1_extend_frame(uint8_t *data, int width, int height, int stride,
291 int border_horz, int border_vert, int highbd);
292 void av1_decode_xq(const int *xqd, int *xq, const sgr_params_type *params);
293
294 // Filter a single loop restoration unit.
295 //
296 // limits is the limits of the unit. rui gives the mode to use for this unit
297 // and its coefficients. If striped loop restoration is enabled, rsb contains
298 // deblocked pixels to use for stripe boundaries; rlbs is just some space to
299 // use as a scratch buffer. tile_rect gives the limits of the tile containing
300 // this unit. tile_stripe0 is the index of the first stripe in this tile.
301 //
302 // ss_x and ss_y are flags which should be 1 if this is a plane with
303 // horizontal/vertical subsampling, respectively. highbd is a flag which should
304 // be 1 in high bit depth mode, in which case bit_depth is the bit depth.
305 //
306 // data8 is the frame data (pointing at the top-left corner of the frame, not
307 // the restoration unit) and stride is its stride. dst8 is the buffer where the
308 // results will be written and has stride dst_stride. Like data8, dst8 should
309 // point at the top-left corner of the frame.
310 //
311 // Finally tmpbuf is a scratch buffer used by the sgrproj filter which should
312 // be at least SGRPROJ_TMPBUF_SIZE big.
313 void av1_loop_restoration_filter_unit(
314 const RestorationTileLimits *limits, const RestorationUnitInfo *rui,
315 const RestorationStripeBoundaries *rsb, RestorationLineBuffers *rlbs,
316 const AV1PixelRect *tile_rect, int tile_stripe0, int ss_x, int ss_y,
317 int highbd, int bit_depth, uint8_t *data8, int stride, uint8_t *dst8,
318 int dst_stride, int32_t *tmpbuf, int optimized_lr);
319
320 void av1_loop_restoration_filter_frame(YV12_BUFFER_CONFIG *frame,
321 struct AV1Common *cm, int optimized_lr,
322 void *lr_ctxt);
323 void av1_loop_restoration_precal();
324
325 typedef void (*rest_tile_start_visitor_t)(int tile_row, int tile_col,
326 void *priv);
327 struct AV1LrSyncData;
328
329 typedef void (*sync_read_fn_t)(void *const lr_sync, int r, int c, int plane);
330
331 typedef void (*sync_write_fn_t)(void *const lr_sync, int r, int c,
332 const int sb_cols, int plane);
333
334 // Call on_rest_unit for each loop restoration unit in the plane.
335 void av1_foreach_rest_unit_in_plane(const struct AV1Common *cm, int plane,
336 rest_unit_visitor_t on_rest_unit,
337 void *priv, AV1PixelRect *tile_rect,
338 int32_t *tmpbuf,
339 RestorationLineBuffers *rlbs);
340
341 // Return 1 iff the block at mi_row, mi_col with size bsize is a
342 // top-level superblock containing the top-left corner of at least one
343 // loop restoration unit.
344 //
345 // If the block is a top-level superblock, the function writes to
346 // *rcol0, *rcol1, *rrow0, *rrow1. The rectangle of restoration unit
347 // indices given by [*rcol0, *rcol1) x [*rrow0, *rrow1) are relative
348 // to the current tile, whose starting index is returned as
349 // *tile_tl_idx.
350 int av1_loop_restoration_corners_in_sb(const struct AV1Common *cm, int plane,
351 int mi_row, int mi_col, BLOCK_SIZE bsize,
352 int *rcol0, int *rcol1, int *rrow0,
353 int *rrow1);
354
355 void av1_loop_restoration_save_boundary_lines(const YV12_BUFFER_CONFIG *frame,
356 struct AV1Common *cm,
357 int after_cdef);
358 void av1_loop_restoration_filter_frame_init(AV1LrStruct *lr_ctxt,
359 YV12_BUFFER_CONFIG *frame,
360 struct AV1Common *cm,
361 int optimized_lr, int num_planes);
362 void av1_loop_restoration_copy_planes(AV1LrStruct *loop_rest_ctxt,
363 struct AV1Common *cm, int num_planes);
364 void av1_foreach_rest_unit_in_row(
365 RestorationTileLimits *limits, const AV1PixelRect *tile_rect,
366 rest_unit_visitor_t on_rest_unit, int row_number, int unit_size,
367 int unit_idx0, int hunits_per_tile, int vunits_per_tile, int plane,
368 void *priv, int32_t *tmpbuf, RestorationLineBuffers *rlbs,
369 sync_read_fn_t on_sync_read, sync_write_fn_t on_sync_write,
370 struct AV1LrSyncData *const lr_sync);
371 AV1PixelRect av1_whole_frame_rect(const struct AV1Common *cm, int is_uv);
372 int av1_lr_count_units_in_tile(int unit_size, int tile_size);
373 void av1_lr_sync_read_dummy(void *const lr_sync, int r, int c, int plane);
374 void av1_lr_sync_write_dummy(void *const lr_sync, int r, int c,
375 const int sb_cols, int plane);
376 #ifdef __cplusplus
377 } // extern "C"
378 #endif
379
380 #endif // AOM_AV1_COMMON_RESTORATION_H_
381