AOMedia AV1 Codec
restoration.h
Go to the documentation of this file.
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
22extern "C" {
23#endif
24
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 stripe 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#define WIENER_STATS_DOWNSAMPLE_FACTOR 4
132
133#define WIENER_FILT_PREC_BITS 7
134#define WIENER_FILT_STEP (1 << WIENER_FILT_PREC_BITS)
135
136// Central values for the taps
137#define WIENER_FILT_TAP0_MIDV (3)
138#define WIENER_FILT_TAP1_MIDV (-7)
139#define WIENER_FILT_TAP2_MIDV (15)
140#define WIENER_FILT_TAP3_MIDV \
141 (WIENER_FILT_STEP - 2 * (WIENER_FILT_TAP0_MIDV + WIENER_FILT_TAP1_MIDV + \
142 WIENER_FILT_TAP2_MIDV))
143
144#define WIENER_FILT_TAP0_BITS 4
145#define WIENER_FILT_TAP1_BITS 5
146#define WIENER_FILT_TAP2_BITS 6
147
148#define WIENER_FILT_BITS \
149 ((WIENER_FILT_TAP0_BITS + WIENER_FILT_TAP1_BITS + WIENER_FILT_TAP2_BITS) * 2)
150
151#define WIENER_FILT_TAP0_MINV \
152 (WIENER_FILT_TAP0_MIDV - (1 << WIENER_FILT_TAP0_BITS) / 2)
153#define WIENER_FILT_TAP1_MINV \
154 (WIENER_FILT_TAP1_MIDV - (1 << WIENER_FILT_TAP1_BITS) / 2)
155#define WIENER_FILT_TAP2_MINV \
156 (WIENER_FILT_TAP2_MIDV - (1 << WIENER_FILT_TAP2_BITS) / 2)
157
158#define WIENER_FILT_TAP0_MAXV \
159 (WIENER_FILT_TAP0_MIDV - 1 + (1 << WIENER_FILT_TAP0_BITS) / 2)
160#define WIENER_FILT_TAP1_MAXV \
161 (WIENER_FILT_TAP1_MIDV - 1 + (1 << WIENER_FILT_TAP1_BITS) / 2)
162#define WIENER_FILT_TAP2_MAXV \
163 (WIENER_FILT_TAP2_MIDV - 1 + (1 << WIENER_FILT_TAP2_BITS) / 2)
164
165#define WIENER_FILT_TAP0_SUBEXP_K 1
166#define WIENER_FILT_TAP1_SUBEXP_K 2
167#define WIENER_FILT_TAP2_SUBEXP_K 3
168
169// Max of SGRPROJ_TMPBUF_SIZE, DOMAINTXFMRF_TMPBUF_SIZE, WIENER_TMPBUF_SIZE
170#define RESTORATION_TMPBUF_SIZE (SGRPROJ_TMPBUF_SIZE)
171
172// Max of SGRPROJ_EXTBUF_SIZE, WIENER_EXTBUF_SIZE
173#define RESTORATION_EXTBUF_SIZE (WIENER_EXTBUF_SIZE)
174
175// Check the assumptions of the existing code
176#if SUBPEL_TAPS != WIENER_WIN + 1
177#error "Wiener filter currently only works if SUBPEL_TAPS == WIENER_WIN + 1"
178#endif
179#if WIENER_FILT_PREC_BITS != 7
180#error "Wiener filter currently only works if WIENER_FILT_PREC_BITS == 7"
181#endif
182
183typedef struct {
184 int r[2]; // radii
185 int s[2]; // sgr parameters for r[0] and r[1], based on GenSgrprojVtable()
186} sgr_params_type;
206
209// A restoration line buffer needs space for two lines plus a horizontal filter
210// margin of RESTORATION_EXTRA_HORZ on each side.
211#define RESTORATION_LINEBUFFER_WIDTH \
212 (RESTORATION_UNITSIZE_MAX * 3 / 2 + 2 * RESTORATION_EXTRA_HORZ)
213
214typedef struct {
215 // Temporary buffers to save/restore 3 lines above/below the restoration
216 // stripe.
217 uint16_t tmp_save_above[RESTORATION_BORDER][RESTORATION_LINEBUFFER_WIDTH];
218 uint16_t tmp_save_below[RESTORATION_BORDER][RESTORATION_LINEBUFFER_WIDTH];
219} RestorationLineBuffers;
244
294
297static INLINE void set_default_sgrproj(SgrprojInfo *sgrproj_info) {
298 sgrproj_info->xqd[0] = (SGRPROJ_PRJ_MIN0 + SGRPROJ_PRJ_MAX0) / 2;
299 sgrproj_info->xqd[1] = (SGRPROJ_PRJ_MIN1 + SGRPROJ_PRJ_MAX1) / 2;
300}
301
302static INLINE void set_default_wiener(WienerInfo *wiener_info) {
303 wiener_info->vfilter[0] = wiener_info->hfilter[0] = WIENER_FILT_TAP0_MIDV;
304 wiener_info->vfilter[1] = wiener_info->hfilter[1] = WIENER_FILT_TAP1_MIDV;
305 wiener_info->vfilter[2] = wiener_info->hfilter[2] = WIENER_FILT_TAP2_MIDV;
306 wiener_info->vfilter[WIENER_HALFWIN] = wiener_info->hfilter[WIENER_HALFWIN] =
307 -2 *
308 (WIENER_FILT_TAP2_MIDV + WIENER_FILT_TAP1_MIDV + WIENER_FILT_TAP0_MIDV);
309 wiener_info->vfilter[4] = wiener_info->hfilter[4] = WIENER_FILT_TAP2_MIDV;
310 wiener_info->vfilter[5] = wiener_info->hfilter[5] = WIENER_FILT_TAP1_MIDV;
311 wiener_info->vfilter[6] = wiener_info->hfilter[6] = WIENER_FILT_TAP0_MIDV;
312}
313
314typedef struct {
315 int h_start, h_end, v_start, v_end;
316} RestorationTileLimits;
317
318typedef void (*rest_unit_visitor_t)(const RestorationTileLimits *limits,
319 int rest_unit_idx, void *priv,
320 int32_t *tmpbuf,
321 RestorationLineBuffers *rlbs,
322 struct aom_internal_error_info *error_info);
323
324typedef struct FilterFrameCtxt {
325 const RestorationInfo *rsi;
326 int ss_x, ss_y;
327 int plane_w, plane_h;
328 int highbd, bit_depth;
329 uint8_t *data8, *dst8;
330 int data_stride, dst_stride;
331} FilterFrameCtxt;
332
333typedef struct AV1LrStruct {
334 rest_unit_visitor_t on_rest_unit;
335 FilterFrameCtxt ctxt[MAX_MB_PLANE];
336 YV12_BUFFER_CONFIG *frame;
338} AV1LrStruct;
339
340extern const sgr_params_type av1_sgr_params[SGRPROJ_PARAMS];
341extern int sgrproj_mtable[SGRPROJ_PARAMS][2];
342extern const int32_t av1_x_by_xplus1[256];
343extern const int32_t av1_one_by_x[MAX_NELEM];
344
345void av1_alloc_restoration_struct(struct AV1Common *cm, RestorationInfo *rsi,
346 int is_uv);
347void av1_free_restoration_struct(RestorationInfo *rst_info);
348
349void av1_extend_frame(uint8_t *data, int width, int height, int stride,
350 int border_horz, int border_vert, int highbd);
351void av1_decode_xq(const int *xqd, int *xq, const sgr_params_type *params);
352
389 const RestorationTileLimits *limits, const RestorationUnitInfo *rui,
390 const RestorationStripeBoundaries *rsb, RestorationLineBuffers *rlbs,
391 int plane_w, int plane_h, int ss_x, int ss_y, int highbd, int bit_depth,
392 uint8_t *data8, int stride, uint8_t *dst8, int dst_stride, int32_t *tmpbuf,
393 int optimized_lr, struct aom_internal_error_info *error_info);
394
409 struct AV1Common *cm, int optimized_lr,
410 void *lr_ctxt);
413void av1_loop_restoration_precal(void);
414
415struct AV1LrSyncData;
416
417typedef void (*sync_read_fn_t)(void *const lr_sync, int r, int c, int plane);
418
419typedef void (*sync_write_fn_t)(void *const lr_sync, int r, int c,
420 const int sb_cols, int plane);
421
422// Call on_rest_unit for each loop restoration unit in the plane.
423void av1_foreach_rest_unit_in_plane(const struct AV1Common *cm, int plane,
424 rest_unit_visitor_t on_rest_unit,
425 void *priv, int32_t *tmpbuf,
426 RestorationLineBuffers *rlbs);
427
428// Return 1 iff the block at mi_row, mi_col with size bsize is a
429// top-level superblock containing the top-left corner of at least one
430// loop restoration unit.
431//
432// If the block is a top-level superblock, the function writes to
433// *rcol0, *rcol1, *rrow0, *rrow1. This means that the parameters for all
434// restoration units in the rectangle [*rcol0, *rcol1) x [*rrow0, *rrow1)
435// are signaled in this superblock.
436int av1_loop_restoration_corners_in_sb(const struct AV1Common *cm, int plane,
437 int mi_row, int mi_col, BLOCK_SIZE bsize,
438 int *rcol0, int *rcol1, int *rrow0,
439 int *rrow1);
440
441void av1_loop_restoration_save_boundary_lines(const YV12_BUFFER_CONFIG *frame,
442 struct AV1Common *cm,
443 int after_cdef);
444void av1_loop_restoration_filter_frame_init(AV1LrStruct *lr_ctxt,
445 YV12_BUFFER_CONFIG *frame,
446 struct AV1Common *cm,
447 int optimized_lr, int num_planes);
448void av1_loop_restoration_copy_planes(AV1LrStruct *loop_rest_ctxt,
449 struct AV1Common *cm, int num_planes);
450void av1_foreach_rest_unit_in_row(
451 RestorationTileLimits *limits, int plane_w,
452 rest_unit_visitor_t on_rest_unit, int row_number, int unit_size,
453 int hnum_rest_units, int vnum_rest_units, int plane, void *priv,
454 int32_t *tmpbuf, RestorationLineBuffers *rlbs, sync_read_fn_t on_sync_read,
455 sync_write_fn_t on_sync_write, struct AV1LrSyncData *const lr_sync,
456 struct aom_internal_error_info *error_info);
457
458void av1_get_upsampled_plane_size(const struct AV1Common *cm, int is_uv,
459 int *plane_w, int *plane_h);
460int av1_lr_count_units(int unit_size, int plane_size);
461void av1_lr_sync_read_dummy(void *const lr_sync, int r, int c, int plane);
462void av1_lr_sync_write_dummy(void *const lr_sync, int r, int c,
463 const int sb_cols, int plane);
464
467#ifdef __cplusplus
468} // extern "C"
469#endif
470
471#endif // AOM_AV1_COMMON_RESTORATION_H_
RestorationType
This enumeration defines various restoration types supported.
Definition enums.h:609
void av1_loop_restoration_filter_frame(YV12_BUFFER_CONFIG *frame, struct AV1Common *cm, int optimized_lr, void *lr_ctxt)
Function for applying loop restoration filter to a frame.
void av1_loop_restoration_filter_unit(const RestorationTileLimits *limits, const RestorationUnitInfo *rui, const RestorationStripeBoundaries *rsb, RestorationLineBuffers *rlbs, int plane_w, int plane_h, 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, int optimized_lr, struct aom_internal_error_info *error_info)
Function for applying loop restoration filter to a single unit.
Top level common structure used by both encoder and decoder.
Definition av1_common_int.h:752
Parameters related to Restoration Info.
Definition restoration.h:246
RestorationStripeBoundaries boundaries
Definition restoration.h:285
RestorationUnitInfo * unit_info
Definition restoration.h:280
int horz_units
Definition restoration.h:274
RestorationType frame_restoration_type
Definition restoration.h:250
int vert_units
Definition restoration.h:269
int num_rest_units
Definition restoration.h:264
int optimized_lr
Definition restoration.h:292
int restoration_unit_size
Definition restoration.h:255
Parameters related to Restoration Stripe boundaries.
Definition restoration.h:223
int stripe_boundary_stride
Definition restoration.h:237
int stripe_boundary_size
Definition restoration.h:242
uint8_t * stripe_boundary_above
Definition restoration.h:227
uint8_t * stripe_boundary_below
Definition restoration.h:232
Parameters related to Restoration Unit Info.
Definition restoration.h:190
WienerInfo wiener_info
Definition restoration.h:199
RestorationType restoration_type
Definition restoration.h:194
SgrprojInfo sgrproj_info
Definition restoration.h:204
Parameters related to Sgrproj Filter.
Definition blockd.h:507
int xqd[2]
Definition blockd.h:516
Parameters related to Wiener Filter.
Definition blockd.h:494
InterpKernel hfilter
Definition blockd.h:503
InterpKernel vfilter
Definition blockd.h:498
YV12 frame buffer data structure.
Definition yv12config.h:46