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