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