Home | History | Annotate | Download | only in common
      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_WIN2_CHROMA ((WIENER_WIN_CHROMA) * (WIENER_WIN_CHROMA))
    126 
    127 #define WIENER_FILT_PREC_BITS 7
    128 #define WIENER_FILT_STEP (1 << WIENER_FILT_PREC_BITS)
    129 
    130 // Central values for the taps
    131 #define WIENER_FILT_TAP0_MIDV (3)
    132 #define WIENER_FILT_TAP1_MIDV (-7)
    133 #define WIENER_FILT_TAP2_MIDV (15)
    134 #define WIENER_FILT_TAP3_MIDV                                              \
    135   (WIENER_FILT_STEP - 2 * (WIENER_FILT_TAP0_MIDV + WIENER_FILT_TAP1_MIDV + \
    136                            WIENER_FILT_TAP2_MIDV))
    137 
    138 #define WIENER_FILT_TAP0_BITS 4
    139 #define WIENER_FILT_TAP1_BITS 5
    140 #define WIENER_FILT_TAP2_BITS 6
    141 
    142 #define WIENER_FILT_BITS \
    143   ((WIENER_FILT_TAP0_BITS + WIENER_FILT_TAP1_BITS + WIENER_FILT_TAP2_BITS) * 2)
    144 
    145 #define WIENER_FILT_TAP0_MINV \
    146   (WIENER_FILT_TAP0_MIDV - (1 << WIENER_FILT_TAP0_BITS) / 2)
    147 #define WIENER_FILT_TAP1_MINV \
    148   (WIENER_FILT_TAP1_MIDV - (1 << WIENER_FILT_TAP1_BITS) / 2)
    149 #define WIENER_FILT_TAP2_MINV \
    150   (WIENER_FILT_TAP2_MIDV - (1 << WIENER_FILT_TAP2_BITS) / 2)
    151 
    152 #define WIENER_FILT_TAP0_MAXV \
    153   (WIENER_FILT_TAP0_MIDV - 1 + (1 << WIENER_FILT_TAP0_BITS) / 2)
    154 #define WIENER_FILT_TAP1_MAXV \
    155   (WIENER_FILT_TAP1_MIDV - 1 + (1 << WIENER_FILT_TAP1_BITS) / 2)
    156 #define WIENER_FILT_TAP2_MAXV \
    157   (WIENER_FILT_TAP2_MIDV - 1 + (1 << WIENER_FILT_TAP2_BITS) / 2)
    158 
    159 #define WIENER_FILT_TAP0_SUBEXP_K 1
    160 #define WIENER_FILT_TAP1_SUBEXP_K 2
    161 #define WIENER_FILT_TAP2_SUBEXP_K 3
    162 
    163 // Max of SGRPROJ_TMPBUF_SIZE, DOMAINTXFMRF_TMPBUF_SIZE, WIENER_TMPBUF_SIZE
    164 #define RESTORATION_TMPBUF_SIZE (SGRPROJ_TMPBUF_SIZE)
    165 
    166 // Max of SGRPROJ_EXTBUF_SIZE, WIENER_EXTBUF_SIZE
    167 #define RESTORATION_EXTBUF_SIZE (WIENER_EXTBUF_SIZE)
    168 
    169 // Check the assumptions of the existing code
    170 #if SUBPEL_TAPS != WIENER_WIN + 1
    171 #error "Wiener filter currently only works if SUBPEL_TAPS == WIENER_WIN + 1"
    172 #endif
    173 #if WIENER_FILT_PREC_BITS != 7
    174 #error "Wiener filter currently only works if WIENER_FILT_PREC_BITS == 7"
    175 #endif
    176 
    177 #define LR_TILE_ROW 0
    178 #define LR_TILE_COL 0
    179 #define LR_TILE_COLS 1
    180 
    181 typedef struct {
    182   int r[2];  // radii
    183   int s[2];  // sgr parameters for r[0] and r[1], based on GenSgrprojVtable()
    184 } sgr_params_type;
    185 
    186 typedef struct {
    187   RestorationType restoration_type;
    188   WienerInfo wiener_info;
    189   SgrprojInfo sgrproj_info;
    190 } RestorationUnitInfo;
    191 
    192 // A restoration line buffer needs space for two lines plus a horizontal filter
    193 // margin of RESTORATION_EXTRA_HORZ on each side.
    194 #define RESTORATION_LINEBUFFER_WIDTH \
    195   (RESTORATION_UNITSIZE_MAX * 3 / 2 + 2 * RESTORATION_EXTRA_HORZ)
    196 
    197 // Similarly, the column buffers (used when we're at a vertical tile edge
    198 // that we can't filter across) need space for one processing unit's worth
    199 // of pixels, plus the top/bottom border width
    200 #define RESTORATION_COLBUFFER_HEIGHT \
    201   (RESTORATION_PROC_UNIT_SIZE + 2 * RESTORATION_BORDER)
    202 
    203 typedef struct {
    204   // Temporary buffers to save/restore 3 lines above/below the restoration
    205   // stripe.
    206   uint16_t tmp_save_above[RESTORATION_BORDER][RESTORATION_LINEBUFFER_WIDTH];
    207   uint16_t tmp_save_below[RESTORATION_BORDER][RESTORATION_LINEBUFFER_WIDTH];
    208 } RestorationLineBuffers;
    209 
    210 typedef struct {
    211   uint8_t *stripe_boundary_above;
    212   uint8_t *stripe_boundary_below;
    213   int stripe_boundary_stride;
    214   int stripe_boundary_size;
    215 } RestorationStripeBoundaries;
    216 
    217 typedef struct {
    218   RestorationType frame_restoration_type;
    219   int restoration_unit_size;
    220 
    221   // Fields below here are allocated and initialised by
    222   // av1_alloc_restoration_struct. (horz_)units_per_tile give the number of
    223   // restoration units in (one row of) the largest tile in the frame. The data
    224   // in unit_info is laid out with units_per_tile entries for each tile, which
    225   // have stride horz_units_per_tile.
    226   //
    227   // Even if there are tiles of different sizes, the data in unit_info is laid
    228   // out as if all tiles are of full size.
    229   int units_per_tile;
    230   int vert_units_per_tile, horz_units_per_tile;
    231   RestorationUnitInfo *unit_info;
    232   RestorationStripeBoundaries boundaries;
    233   int optimized_lr;
    234 } RestorationInfo;
    235 
    236 static INLINE void set_default_sgrproj(SgrprojInfo *sgrproj_info) {
    237   sgrproj_info->xqd[0] = (SGRPROJ_PRJ_MIN0 + SGRPROJ_PRJ_MAX0) / 2;
    238   sgrproj_info->xqd[1] = (SGRPROJ_PRJ_MIN1 + SGRPROJ_PRJ_MAX1) / 2;
    239 }
    240 
    241 static INLINE void set_default_wiener(WienerInfo *wiener_info) {
    242   wiener_info->vfilter[0] = wiener_info->hfilter[0] = WIENER_FILT_TAP0_MIDV;
    243   wiener_info->vfilter[1] = wiener_info->hfilter[1] = WIENER_FILT_TAP1_MIDV;
    244   wiener_info->vfilter[2] = wiener_info->hfilter[2] = WIENER_FILT_TAP2_MIDV;
    245   wiener_info->vfilter[WIENER_HALFWIN] = wiener_info->hfilter[WIENER_HALFWIN] =
    246       -2 *
    247       (WIENER_FILT_TAP2_MIDV + WIENER_FILT_TAP1_MIDV + WIENER_FILT_TAP0_MIDV);
    248   wiener_info->vfilter[4] = wiener_info->hfilter[4] = WIENER_FILT_TAP2_MIDV;
    249   wiener_info->vfilter[5] = wiener_info->hfilter[5] = WIENER_FILT_TAP1_MIDV;
    250   wiener_info->vfilter[6] = wiener_info->hfilter[6] = WIENER_FILT_TAP0_MIDV;
    251 }
    252 
    253 typedef struct {
    254   int h_start, h_end, v_start, v_end;
    255 } RestorationTileLimits;
    256 
    257 typedef void (*rest_unit_visitor_t)(const RestorationTileLimits *limits,
    258                                     const AV1PixelRect *tile_rect,
    259                                     int rest_unit_idx, void *priv,
    260                                     int32_t *tmpbuf,
    261                                     RestorationLineBuffers *rlbs);
    262 
    263 typedef struct FilterFrameCtxt {
    264   const RestorationInfo *rsi;
    265   int tile_stripe0;
    266   int ss_x, ss_y;
    267   int highbd, bit_depth;
    268   uint8_t *data8, *dst8;
    269   int data_stride, dst_stride;
    270   AV1PixelRect tile_rect;
    271 } FilterFrameCtxt;
    272 
    273 typedef struct AV1LrStruct {
    274   rest_unit_visitor_t on_rest_unit;
    275   FilterFrameCtxt ctxt[MAX_MB_PLANE];
    276   YV12_BUFFER_CONFIG *frame;
    277   YV12_BUFFER_CONFIG *dst;
    278 } AV1LrStruct;
    279 
    280 extern const sgr_params_type sgr_params[SGRPROJ_PARAMS];
    281 extern int sgrproj_mtable[SGRPROJ_PARAMS][2];
    282 extern const int32_t x_by_xplus1[256];
    283 extern const int32_t one_by_x[MAX_NELEM];
    284 
    285 void av1_alloc_restoration_struct(struct AV1Common *cm, RestorationInfo *rsi,
    286                                   int is_uv);
    287 void av1_free_restoration_struct(RestorationInfo *rst_info);
    288 
    289 void extend_frame(uint8_t *data, int width, int height, int stride,
    290                   int border_horz, int border_vert, int highbd);
    291 void decode_xq(const int *xqd, int *xq, const sgr_params_type *params);
    292 
    293 // Filter a single loop restoration unit.
    294 //
    295 // limits is the limits of the unit. rui gives the mode to use for this unit
    296 // and its coefficients. If striped loop restoration is enabled, rsb contains
    297 // deblocked pixels to use for stripe boundaries; rlbs is just some space to
    298 // use as a scratch buffer. tile_rect gives the limits of the tile containing
    299 // this unit. tile_stripe0 is the index of the first stripe in this tile.
    300 //
    301 // ss_x and ss_y are flags which should be 1 if this is a plane with
    302 // horizontal/vertical subsampling, respectively. highbd is a flag which should
    303 // be 1 in high bit depth mode, in which case bit_depth is the bit depth.
    304 //
    305 // data8 is the frame data (pointing at the top-left corner of the frame, not
    306 // the restoration unit) and stride is its stride. dst8 is the buffer where the
    307 // results will be written and has stride dst_stride. Like data8, dst8 should
    308 // point at the top-left corner of the frame.
    309 //
    310 // Finally tmpbuf is a scratch buffer used by the sgrproj filter which should
    311 // be at least SGRPROJ_TMPBUF_SIZE big.
    312 void av1_loop_restoration_filter_unit(
    313     const RestorationTileLimits *limits, const RestorationUnitInfo *rui,
    314     const RestorationStripeBoundaries *rsb, RestorationLineBuffers *rlbs,
    315     const AV1PixelRect *tile_rect, int tile_stripe0, int ss_x, int ss_y,
    316     int highbd, int bit_depth, uint8_t *data8, int stride, uint8_t *dst8,
    317     int dst_stride, int32_t *tmpbuf, int optimized_lr);
    318 
    319 void av1_loop_restoration_filter_frame(YV12_BUFFER_CONFIG *frame,
    320                                        struct AV1Common *cm, int optimized_lr,
    321                                        void *lr_ctxt);
    322 void av1_loop_restoration_precal();
    323 
    324 typedef void (*rest_tile_start_visitor_t)(int tile_row, int tile_col,
    325                                           void *priv);
    326 struct AV1LrSyncData;
    327 
    328 typedef void (*sync_read_fn_t)(void *const lr_sync, int r, int c, int plane);
    329 
    330 typedef void (*sync_write_fn_t)(void *const lr_sync, int r, int c,
    331                                 const int sb_cols, int plane);
    332 
    333 // Call on_rest_unit for each loop restoration unit in the plane.
    334 void av1_foreach_rest_unit_in_plane(const struct AV1Common *cm, int plane,
    335                                     rest_unit_visitor_t on_rest_unit,
    336                                     void *priv, AV1PixelRect *tile_rect,
    337                                     int32_t *tmpbuf,
    338                                     RestorationLineBuffers *rlbs);
    339 
    340 // Return 1 iff the block at mi_row, mi_col with size bsize is a
    341 // top-level superblock containing the top-left corner of at least one
    342 // loop restoration unit.
    343 //
    344 // If the block is a top-level superblock, the function writes to
    345 // *rcol0, *rcol1, *rrow0, *rrow1. The rectangle of restoration unit
    346 // indices given by [*rcol0, *rcol1) x [*rrow0, *rrow1) are relative
    347 // to the current tile, whose starting index is returned as
    348 // *tile_tl_idx.
    349 int av1_loop_restoration_corners_in_sb(const struct AV1Common *cm, int plane,
    350                                        int mi_row, int mi_col, BLOCK_SIZE bsize,
    351                                        int *rcol0, int *rcol1, int *rrow0,
    352                                        int *rrow1);
    353 
    354 void av1_loop_restoration_save_boundary_lines(const YV12_BUFFER_CONFIG *frame,
    355                                               struct AV1Common *cm,
    356                                               int after_cdef);
    357 void av1_loop_restoration_filter_frame_init(AV1LrStruct *lr_ctxt,
    358                                             YV12_BUFFER_CONFIG *frame,
    359                                             struct AV1Common *cm,
    360                                             int optimized_lr, int num_planes);
    361 void av1_loop_restoration_copy_planes(AV1LrStruct *loop_rest_ctxt,
    362                                       struct AV1Common *cm, int num_planes);
    363 void av1_foreach_rest_unit_in_row(
    364     RestorationTileLimits *limits, const AV1PixelRect *tile_rect,
    365     rest_unit_visitor_t on_rest_unit, int row_number, int unit_size,
    366     int unit_idx0, int hunits_per_tile, int vunits_per_tile, int plane,
    367     void *priv, int32_t *tmpbuf, RestorationLineBuffers *rlbs,
    368     sync_read_fn_t on_sync_read, sync_write_fn_t on_sync_write,
    369     struct AV1LrSyncData *const lr_sync);
    370 AV1PixelRect av1_whole_frame_rect(const struct AV1Common *cm, int is_uv);
    371 int av1_lr_count_units_in_tile(int unit_size, int tile_size);
    372 void av1_lr_sync_read_dummy(void *const lr_sync, int r, int c, int plane);
    373 void av1_lr_sync_write_dummy(void *const lr_sync, int r, int c,
    374                              const int sb_cols, int plane);
    375 #ifdef __cplusplus
    376 }  // extern "C"
    377 #endif
    378 
    379 #endif  // AOM_AV1_COMMON_RESTORATION_H_
    380