restoration.h 14.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 11
 */

Yaowu Xu's avatar
Yaowu Xu committed
12 13
#ifndef AV1_COMMON_RESTORATION_H_
#define AV1_COMMON_RESTORATION_H_
14

15
#include "aom_ports/mem.h"
Yaowu Xu's avatar
Yaowu Xu committed
16
#include "./aom_config.h"
17

18
#include "av1/common/blockd.h"
19
#include "av1/common/enums.h"
20 21 22 23 24

#ifdef __cplusplus
extern "C" {
#endif

25 26 27
#define CLIP(x, lo, hi) ((x) < (lo) ? (lo) : (x) > (hi) ? (hi) : (x))
#define RINT(x) ((x) < 0 ? (int)((x)-0.5) : (int)((x) + 0.5))

28
#define RESTORATION_PROC_UNIT_SIZE 64
29

30 31 32
// Filter tile grid offset upwards compared to the superblock grid
#define RESTORATION_TILE_OFFSET 8

33 34
#define SGRPROJ_BORDER_VERT 3  // Vertical border used for Sgr
#define SGRPROJ_BORDER_HORZ 3  // Horizontal border used for Sgr
35

36
#define WIENER_BORDER_VERT 2  // Vertical border used for Wiener
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#define WIENER_HALFWIN 3
#define WIENER_BORDER_HORZ (WIENER_HALFWIN)  // Horizontal border for Wiener

// RESTORATION_BORDER_VERT determines line buffer requirement for LR.
// Should be set at the max of SGRPROJ_BORDER_VERT and WIENER_BORDER_VERT.
// Note the line buffer needed is twice the value of this macro.
#if SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT
#define RESTORATION_BORDER_VERT (SGRPROJ_BORDER_VERT)
#else
#define RESTORATION_BORDER_VERT (WIENER_BORDER_VERT)
#endif  // SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT

#if SGRPROJ_BORDER_HORZ >= WIENER_BORDER_HORZ
#define RESTORATION_BORDER_HORZ (SGRPROJ_BORDER_HORZ)
#else
#define RESTORATION_BORDER_HORZ (WIENER_BORDER_HORZ)
#endif  // SGRPROJ_BORDER_VERT >= WIENER_BORDER_VERT
54

55 56 57 58 59 60 61
// How many border pixels do we need for each processing unit?
#define RESTORATION_BORDER 3

// How many rows of deblocked pixels do we save above/below each processing
// stripe?
#define RESTORATION_CTX_VERT 2

62 63 64 65
// Additional pixels to the left and right in above/below buffers
// It is RESTORATION_BORDER_HORZ rounded up to get nicer buffer alignment
#define RESTORATION_EXTRA_HORZ 4

66 67 68 69 70 71 72
// Pad up to 20 more (may be much less is needed)
#define RESTORATION_PADDING 20
#define RESTORATION_PROC_UNIT_PELS                             \
  ((RESTORATION_PROC_UNIT_SIZE + RESTORATION_BORDER_HORZ * 2 + \
    RESTORATION_PADDING) *                                     \
   (RESTORATION_PROC_UNIT_SIZE + RESTORATION_BORDER_VERT * 2 + \
    RESTORATION_PADDING))
73

74
#define RESTORATION_TILESIZE_MAX 256
75 76 77 78 79 80 81
#define RESTORATION_TILEPELS_HORZ_MAX \
  (RESTORATION_TILESIZE_MAX * 3 / 2 + 2 * RESTORATION_BORDER_HORZ + 16)
#define RESTORATION_TILEPELS_VERT_MAX                                \
  ((RESTORATION_TILESIZE_MAX * 3 / 2 + 2 * RESTORATION_BORDER_VERT + \
    RESTORATION_TILE_OFFSET))
#define RESTORATION_TILEPELS_MAX \
  (RESTORATION_TILEPELS_HORZ_MAX * RESTORATION_TILEPELS_VERT_MAX)
82 83 84 85 86

// Two 32-bit buffers needed for the restored versions from two filters
// TODO(debargha, rupert): Refactor to not need the large tilesize to be stored
// on the decoder side.
#define SGRPROJ_TMPBUF_SIZE (RESTORATION_TILEPELS_MAX * 2 * sizeof(int32_t))
87

88
#define SGRPROJ_EXTBUF_SIZE (0)
89
#define SGRPROJ_PARAMS_BITS 4
90 91 92 93 94 95 96 97 98 99
#define SGRPROJ_PARAMS (1 << SGRPROJ_PARAMS_BITS)

// Precision bits for projection
#define SGRPROJ_PRJ_BITS 7
// Restoration precision bits generated higher than source before projection
#define SGRPROJ_RST_BITS 4
// Internal precision bits for core selfguided_restoration
#define SGRPROJ_SGR_BITS 8
#define SGRPROJ_SGR (1 << SGRPROJ_SGR_BITS)

100
#define SGRPROJ_PRJ_MIN0 (-(1 << SGRPROJ_PRJ_BITS) * 3 / 4)
101 102 103 104
#define SGRPROJ_PRJ_MAX0 (SGRPROJ_PRJ_MIN0 + (1 << SGRPROJ_PRJ_BITS) - 1)
#define SGRPROJ_PRJ_MIN1 (-(1 << SGRPROJ_PRJ_BITS) / 4)
#define SGRPROJ_PRJ_MAX1 (SGRPROJ_PRJ_MIN1 + (1 << SGRPROJ_PRJ_BITS) - 1)

105 106
#define SGRPROJ_PRJ_SUBEXP_K 4

107 108
#define SGRPROJ_BITS (SGRPROJ_PRJ_BITS * 2 + SGRPROJ_PARAMS_BITS)

109
#define MAX_RADIUS 2  // Only 1, 2, 3 allowed
David Barker's avatar
David Barker committed
110 111 112 113 114
#define MAX_EPS 80    // Max value of eps
#define MAX_NELEM ((2 * MAX_RADIUS + 1) * (2 * MAX_RADIUS + 1))
#define SGRPROJ_MTABLE_BITS 20
#define SGRPROJ_RECIP_BITS 12

115 116 117 118
#define WIENER_HALFWIN1 (WIENER_HALFWIN + 1)
#define WIENER_WIN (2 * WIENER_HALFWIN + 1)
#define WIENER_WIN2 ((WIENER_WIN) * (WIENER_WIN))
#define WIENER_TMPBUF_SIZE (0)
119
#define WIENER_EXTBUF_SIZE (0)
120

121 122 123 124
// If WIENER_WIN_CHROMA == WIENER_WIN - 2, that implies 5x5 filters are used for
// chroma. To use 7x7 for chroma set WIENER_WIN_CHROMA to WIENER_WIN.
#define WIENER_WIN_CHROMA (WIENER_WIN - 2)

125 126
#define WIENER_FILT_PREC_BITS 7
#define WIENER_FILT_STEP (1 << WIENER_FILT_PREC_BITS)
127

128
// Whether to use high intermediate precision filtering
129
#define USE_WIENER_HIGH_INTERMEDIATE_PRECISION 1
130

131 132 133
// Central values for the taps
#define WIENER_FILT_TAP0_MIDV (3)
#define WIENER_FILT_TAP1_MIDV (-7)
134
#define WIENER_FILT_TAP2_MIDV (15)
135 136 137
#define WIENER_FILT_TAP3_MIDV                                              \
  (WIENER_FILT_STEP - 2 * (WIENER_FILT_TAP0_MIDV + WIENER_FILT_TAP1_MIDV + \
                           WIENER_FILT_TAP2_MIDV))
138

clang-format's avatar
clang-format committed
139 140 141
#define WIENER_FILT_TAP0_BITS 4
#define WIENER_FILT_TAP1_BITS 5
#define WIENER_FILT_TAP2_BITS 6
142

143 144 145
#define WIENER_FILT_BITS \
  ((WIENER_FILT_TAP0_BITS + WIENER_FILT_TAP1_BITS + WIENER_FILT_TAP2_BITS) * 2)

146 147 148 149 150 151 152
#define WIENER_FILT_TAP0_MINV \
  (WIENER_FILT_TAP0_MIDV - (1 << WIENER_FILT_TAP0_BITS) / 2)
#define WIENER_FILT_TAP1_MINV \
  (WIENER_FILT_TAP1_MIDV - (1 << WIENER_FILT_TAP1_BITS) / 2)
#define WIENER_FILT_TAP2_MINV \
  (WIENER_FILT_TAP2_MIDV - (1 << WIENER_FILT_TAP2_BITS) / 2)

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 159
  (WIENER_FILT_TAP2_MIDV - 1 + (1 << WIENER_FILT_TAP2_BITS) / 2)

160 161 162 163
#define WIENER_FILT_TAP0_SUBEXP_K 1
#define WIENER_FILT_TAP1_SUBEXP_K 2
#define WIENER_FILT_TAP2_SUBEXP_K 3

164 165
// Max of SGRPROJ_TMPBUF_SIZE, DOMAINTXFMRF_TMPBUF_SIZE, WIENER_TMPBUF_SIZE
#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 171 172 173 174 175 176
// Check the assumptions of the existing code
#if SUBPEL_TAPS != WIENER_WIN + 1
#error "Wiener filter currently only works if SUBPEL_TAPS == WIENER_WIN + 1"
#endif
#if WIENER_FILT_PREC_BITS != 7
#error "Wiener filter currently only works if WIENER_FILT_PREC_BITS == 7"
#endif
177

178 179 180 181 182 183 184
typedef struct {
  int r1;
  int e1;
  int r2;
  int e2;
} sgr_params_type;

185
typedef struct {
186 187 188 189 190 191 192 193
  RestorationType restoration_type;
  WienerInfo wiener_info;
  SgrprojInfo sgrproj_info;
} RestorationUnitInfo;

// A restoration line buffer needs space for two lines plus a horizontal filter
// margin of RESTORATION_EXTRA_HORZ on each side.
#define RESTORATION_LINEBUFFER_WIDTH \
194
  (RESTORATION_TILESIZE_MAX * 3 / 2 + 2 * RESTORATION_EXTRA_HORZ)
195

196 197 198 199 200 201
// Similarly, the column buffers (used when we're at a vertical tile edge
// that we can't filter across) need space for one processing unit's worth
// of pixels, plus the top/bottom border width
#define RESTORATION_COLBUFFER_HEIGHT \
  (RESTORATION_PROC_UNIT_SIZE + 2 * RESTORATION_BORDER)

202
typedef struct {
203
  // Temporary buffers to save/restore 3 lines above/below the restoration
204
  // stripe.
205 206
  uint16_t tmp_save_above[RESTORATION_BORDER][RESTORATION_LINEBUFFER_WIDTH];
  uint16_t tmp_save_below[RESTORATION_BORDER][RESTORATION_LINEBUFFER_WIDTH];
207
#if CONFIG_LOOPFILTERING_ACROSS_TILES || CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
208 209 210 211 212 213
  // Column buffers, for storing 3 pixels at the left/right of each tile
  // when loopfiltering across tiles is disabled.
  //
  // Note: These arrays only need to store the pixels immediately left/right
  // of each processing unit; the corner pixels (top-left, etc.) are always
  // stored into the above/below arrays.
214 215
  uint16_t tmp_save_left[RESTORATION_BORDER][RESTORATION_COLBUFFER_HEIGHT];
  uint16_t tmp_save_right[RESTORATION_BORDER][RESTORATION_COLBUFFER_HEIGHT];
216
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
217 218 219
} RestorationLineBuffers;

typedef struct {
220 221 222
  uint8_t *stripe_boundary_above;
  uint8_t *stripe_boundary_below;
  int stripe_boundary_stride;
223
  int stripe_boundary_size;
224 225 226 227
} RestorationStripeBoundaries;

typedef struct {
  RestorationType frame_restoration_type;
228 229 230 231 232 233 234 235 236 237 238 239
  int restoration_unit_size;

  // Fields below here are allocated and initialised by
  // av1_alloc_restoration_struct. (horz_)units_per_tile give the number of
  // restoration units in (one row of) the largest tile in the frame. The data
  // in unit_info is laid out with units_per_tile entries for each tile, which
  // have stride horz_units_per_tile.
  //
  // Even if there are tiles of different sizes, the data in unit_info is laid
  // out as if all tiles are of full size.
  int units_per_tile;
  int vert_units_per_tile, horz_units_per_tile;
240 241
  RestorationUnitInfo *unit_info;
  RestorationStripeBoundaries boundaries;
242
} RestorationInfo;
243

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
static INLINE void set_default_sgrproj(SgrprojInfo *sgrproj_info) {
  sgrproj_info->xqd[0] = (SGRPROJ_PRJ_MIN0 + SGRPROJ_PRJ_MAX0) / 2;
  sgrproj_info->xqd[1] = (SGRPROJ_PRJ_MIN1 + SGRPROJ_PRJ_MAX1) / 2;
}

static INLINE void set_default_wiener(WienerInfo *wiener_info) {
  wiener_info->vfilter[0] = wiener_info->hfilter[0] = WIENER_FILT_TAP0_MIDV;
  wiener_info->vfilter[1] = wiener_info->hfilter[1] = WIENER_FILT_TAP1_MIDV;
  wiener_info->vfilter[2] = wiener_info->hfilter[2] = WIENER_FILT_TAP2_MIDV;
  wiener_info->vfilter[WIENER_HALFWIN] = wiener_info->hfilter[WIENER_HALFWIN] =
      -2 *
      (WIENER_FILT_TAP2_MIDV + WIENER_FILT_TAP1_MIDV + WIENER_FILT_TAP0_MIDV);
  wiener_info->vfilter[4] = wiener_info->hfilter[4] = WIENER_FILT_TAP2_MIDV;
  wiener_info->vfilter[5] = wiener_info->hfilter[5] = WIENER_FILT_TAP1_MIDV;
  wiener_info->vfilter[6] = wiener_info->hfilter[6] = WIENER_FILT_TAP0_MIDV;
}

261 262 263
typedef struct {
  int h_start, h_end, v_start, v_end;
} RestorationTileLimits;
264

265
extern const sgr_params_type sgr_params[SGRPROJ_PARAMS];
David Barker's avatar
David Barker committed
266 267 268
extern int sgrproj_mtable[MAX_EPS][MAX_NELEM];
extern const int32_t x_by_xplus1[256];
extern const int32_t one_by_x[MAX_NELEM];
269

270 271
void av1_alloc_restoration_struct(struct AV1Common *cm, RestorationInfo *rsi,
                                  int is_uv);
272 273
void av1_free_restoration_struct(RestorationInfo *rst_info);

274
void extend_frame(uint8_t *data, int width, int height, int stride,
275
                  int border_horz, int border_vert, int highbd);
276 277 278 279 280 281 282
void decode_xq(const int *xqd, int *xq);

// Filter a single loop restoration unit.
//
// limits is the limits of the unit. rui gives the mode to use for this unit
// and its coefficients. If striped loop restoration is enabled, rsb contains
// deblocked pixels to use for stripe boundaries; rlbs is just some space to
283 284
// use as a scratch buffer. tile_rect gives the limits of the tile containing
// this unit. tile_stripe0 is the index of the first stripe in this tile.
285
//
286 287 288
// ss_x and ss_y are flags which should be 1 if this is a plane with
// horizontal/vertical subsampling, respectively. highbd is a flag which should
// be 1 in high bit depth mode, in which case bit_depth is the bit depth.
289 290 291 292 293 294 295 296
//
// data8 is the frame data (pointing at the top-left corner of the frame, not
// the restoration unit) and stride is its stride. dst8 is the buffer where the
// results will be written and has stride dst_stride. Like data8, dst8 should
// point at the top-left corner of the frame.
//
// Finally tmpbuf is a scratch buffer used by the sgrproj filter which should
// be at least SGRPROJ_TMPBUF_SIZE big.
297 298 299
void av1_loop_restoration_filter_unit(
    const RestorationTileLimits *limits, const RestorationUnitInfo *rui,
    const RestorationStripeBoundaries *rsb, RestorationLineBuffers *rlbs,
300
    const AV1PixelRect *tile_rect, int tile_stripe0,
301
#if CONFIG_LOOPFILTERING_ACROSS_TILES
302 303 304 305
#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
    int loop_filter_across_tiles_v_enabled,
    int loop_filter_across_tiles_h_enabled,
#else
306
    int loop_filter_across_tiles_enabled,
307
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
308
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
309 310
    int ss_x, int ss_y, int highbd, int bit_depth, uint8_t *data8, int stride,
    uint8_t *dst8, int dst_stride, int32_t *tmpbuf);
311 312

void av1_loop_restoration_filter_frame(YV12_BUFFER_CONFIG *frame,
313
                                       struct AV1Common *cm);
Yaowu Xu's avatar
Yaowu Xu committed
314
void av1_loop_restoration_precal();
315

316
typedef void (*rest_unit_visitor_t)(const RestorationTileLimits *limits,
317
                                    const AV1PixelRect *tile_rect,
318 319 320 321 322 323 324 325 326 327 328 329
                                    int rest_unit_idx, void *priv);

typedef void (*rest_tile_start_visitor_t)(int tile_row, int tile_col,
                                          void *priv);

// Call on_rest_unit for each loop restoration unit in the frame. At the start
// of each tile, call on_tile.
void av1_foreach_rest_unit_in_frame(const struct AV1Common *cm, int plane,
                                    rest_tile_start_visitor_t on_tile,
                                    rest_unit_visitor_t on_rest_unit,
                                    void *priv);

330 331 332 333 334
// Return 1 iff the block at mi_row, mi_col with size bsize is a
// top-level superblock containing the top-left corner of at least one
// loop restoration tile.
//
// If the block is a top-level superblock, the function writes to
335 336 337 338
// *rcol0, *rcol1, *rrow0, *rrow1. The rectangle of restoration unit
// indices given by [*rcol0, *rcol1) x [*rrow0, *rrow1) are relative
// to the current tile, whose starting index is returned as
// *tile_tl_idx.
339 340 341
int av1_loop_restoration_corners_in_sb(const struct AV1Common *cm, int plane,
                                       int mi_row, int mi_col, BLOCK_SIZE bsize,
                                       int *rcol0, int *rcol1, int *rrow0,
342
                                       int *rrow1, int *tile_tl_idx);
343

344
void av1_loop_restoration_save_boundary_lines(const YV12_BUFFER_CONFIG *frame,
345 346
                                              struct AV1Common *cm,
                                              int after_cdef);
347 348 349 350
#ifdef __cplusplus
}  // extern "C"
#endif

Yaowu Xu's avatar
Yaowu Xu committed
351
#endif  // AV1_COMMON_RESTORATION_H_