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 #ifndef AOM_AV1_ENCODER_PICKCDEF_H_
12 #define AOM_AV1_ENCODER_PICKCDEF_H_
13
14 #include "av1/common/cdef.h"
15 #include "av1/encoder/speed_features.h"
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 /*!\enum CDEF_CONTROL
22 * \brief This enum controls to which frames CDEF is applied.
23 */
24 typedef enum {
25 CDEF_NONE = 0, /* Disable CDEF on all frames. */
26 CDEF_ALL = 1, /* Enable CDEF for all frames. */
27 CDEF_REFERENCE = 2, /* Disable CDEF on non reference frames. */
28 CDEF_ADAPTIVE = 3, /* Enable CDEF adaptively based on frame qindex */
29 } CDEF_CONTROL;
30
31 /*!\cond */
32 struct MultiThreadInfo;
33
34 #define REDUCED_PRI_STRENGTHS_LVL1 8
35 #define REDUCED_PRI_STRENGTHS_LVL2 5
36 #define REDUCED_SEC_STRENGTHS_LVL3 2
37 #define REDUCED_SEC_STRENGTHS_LVL5 1
38 #define REDUCED_PRI_STRENGTHS_LVL4 2
39
40 #define REDUCED_TOTAL_STRENGTHS_LVL1 \
41 (REDUCED_PRI_STRENGTHS_LVL1 * CDEF_SEC_STRENGTHS)
42 #define REDUCED_TOTAL_STRENGTHS_LVL2 \
43 (REDUCED_PRI_STRENGTHS_LVL2 * CDEF_SEC_STRENGTHS)
44 #define REDUCED_TOTAL_STRENGTHS_LVL3 \
45 (REDUCED_PRI_STRENGTHS_LVL2 * REDUCED_SEC_STRENGTHS_LVL3)
46 #define REDUCED_TOTAL_STRENGTHS_LVL4 \
47 (REDUCED_PRI_STRENGTHS_LVL4 * REDUCED_SEC_STRENGTHS_LVL3)
48 #define REDUCED_TOTAL_STRENGTHS_LVL5 \
49 (REDUCED_PRI_STRENGTHS_LVL4 * REDUCED_SEC_STRENGTHS_LVL5)
50 #define TOTAL_STRENGTHS (CDEF_PRI_STRENGTHS * CDEF_SEC_STRENGTHS)
51
52 static const int priconv_lvl1[REDUCED_PRI_STRENGTHS_LVL1] = { 0, 1, 2, 3,
53 5, 7, 10, 13 };
54 static const int priconv_lvl2[REDUCED_PRI_STRENGTHS_LVL2] = { 0, 2, 4, 8, 14 };
55 static const int priconv_lvl4[REDUCED_PRI_STRENGTHS_LVL4] = { 0, 11 };
56 static const int priconv_lvl5[REDUCED_PRI_STRENGTHS_LVL4] = { 0, 5 };
57 static const int secconv_lvl3[REDUCED_SEC_STRENGTHS_LVL3] = { 0, 2 };
58 static const int secconv_lvl5[REDUCED_SEC_STRENGTHS_LVL5] = { 0 };
59 static const int nb_cdef_strengths[CDEF_PICK_METHODS] = {
60 TOTAL_STRENGTHS,
61 REDUCED_TOTAL_STRENGTHS_LVL1,
62 REDUCED_TOTAL_STRENGTHS_LVL2,
63 REDUCED_TOTAL_STRENGTHS_LVL3,
64 REDUCED_TOTAL_STRENGTHS_LVL4,
65 REDUCED_TOTAL_STRENGTHS_LVL5,
66 TOTAL_STRENGTHS
67 };
68
69 typedef void (*copy_fn_t)(uint16_t *dst, int dstride, const uint8_t *src,
70 int src_voffset, int src_hoffset, int sstride,
71 int vsize, int hsize);
72 typedef uint64_t (*compute_cdef_dist_t)(void *dst, int dstride, uint16_t *src,
73 cdef_list *dlist, int cdef_count,
74 BLOCK_SIZE bsize, int coeff_shift,
75 int row, int col);
76
77 /*! \brief CDEF search context.
78 */
79 typedef struct {
80 /*!
81 * Pointer to the frame buffer holding the source frame
82 */
83 const YV12_BUFFER_CONFIG *ref;
84 /*!
85 * Pointer to params related to MB_MODE_INFO arrays and related info
86 */
87 CommonModeInfoParams *mi_params;
88 /*!
89 * Info specific to each plane
90 */
91 struct macroblockd_plane plane[MAX_MB_PLANE];
92 /*!
93 * Function pointer of copy_fn
94 */
95 copy_fn_t copy_fn;
96 /*!
97 * Function pointer of compute_cdef_dist_fn
98 */
99 compute_cdef_dist_t compute_cdef_dist_fn;
100 /*!
101 * Number of strenghts evaluated in CDEF filter search
102 */
103 int total_strengths;
104 /*!
105 * Bit-depth dependent shift
106 */
107 int coeff_shift;
108 /*!
109 * CDEF damping factor
110 */
111 int damping;
112 /*!
113 * Search method used to select CDEF parameters
114 */
115 int pick_method;
116 /*!
117 * Number of planes
118 */
119 int num_planes;
120 /*!
121 * Log2 of width of the MI unit in pixels. mi_wide_l2[i]
122 * indicates the width of the MI unit in pixels for the ith plane
123 */
124 int mi_wide_l2[MAX_MB_PLANE];
125 /*!
126 * Log2 of height of the MI unit in pixels. mi_high_l2[i]
127 * indicates the height of the MI unit in pixels for the ith plane
128 */
129 int mi_high_l2[MAX_MB_PLANE];
130 /*!
131 * Subsampling in x direction. xdec[i] indicates the subsampling
132 * for the ith plane
133 */
134 int xdec[MAX_MB_PLANE];
135 /*!
136 * Subsampling in y direction. ydec[i] indicates the subsampling
137 * for the ith plane
138 */
139 int ydec[MAX_MB_PLANE];
140 /*!
141 * bsize[i] indicates the block size of ith plane
142 */
143 int bsize[MAX_MB_PLANE];
144 /*!
145 * Number of 64x64 blocks in vertical direction of a frame
146 */
147 int nvfb;
148 /*!
149 * Number of 64x64 blocks in horizontal direction of a frame
150 */
151 int nhfb;
152 /*!
153 * Pointer to the mean squared error between the CDEF filtered block and the
154 * source block. mse[i][j][k] stores the MSE of the ith plane (i=0 corresponds
155 * to Y-plane, i=1 corresponds to U and V planes), jth block and kth strength
156 * index
157 */
158 uint64_t (*mse[2])[TOTAL_STRENGTHS];
159 /*!
160 * Holds the position (in units of mi's) of the cdef filtered
161 * block in raster scan order
162 */
163 int *sb_index;
164 /*!
165 * Holds the count of cdef filtered blocks
166 */
167 int sb_count;
168 /*!
169 * Indicates if 16bit frame buffers are to be used i.e., the content bit-depth
170 * is > 8-bit
171 */
172 bool use_highbitdepth;
173 } CdefSearchCtx;
174
sb_all_skip(const CommonModeInfoParams * const mi_params,int mi_row,int mi_col)175 static inline int sb_all_skip(const CommonModeInfoParams *const mi_params,
176 int mi_row, int mi_col) {
177 const int maxr = AOMMIN(mi_params->mi_rows - mi_row, MI_SIZE_64X64);
178 const int maxc = AOMMIN(mi_params->mi_cols - mi_col, MI_SIZE_64X64);
179 const int stride = mi_params->mi_stride;
180 MB_MODE_INFO **mbmi = mi_params->mi_grid_base + mi_row * stride + mi_col;
181 for (int r = 0; r < maxr; ++r, mbmi += stride) {
182 for (int c = 0; c < maxc; ++c) {
183 if (!mbmi[c]->skip_txfm) return 0;
184 }
185 }
186 return 1;
187 }
188
189 // Checks if cdef processing can be skipped for particular sb.
190 // Inputs:
191 // cdef_search_ctx: Pointer to the structure containing parameters related to
192 // CDEF search context.
193 // fbr: Row index in units of 64x64 block
194 // fbc: Column index in units of 64x64 block
195 // Returns:
196 // 1/0 will be returned to indicate skip/don't skip cdef processing of sb
197 // respectively.
cdef_sb_skip(const CommonModeInfoParams * const mi_params,int fbr,int fbc)198 static inline int cdef_sb_skip(const CommonModeInfoParams *const mi_params,
199 int fbr, int fbc) {
200 const MB_MODE_INFO *const mbmi =
201 mi_params->mi_grid_base[MI_SIZE_64X64 * fbr * mi_params->mi_stride +
202 MI_SIZE_64X64 * fbc];
203 // No filtering if the entire filter block is skipped.
204 if (sb_all_skip(mi_params, fbr * MI_SIZE_64X64, fbc * MI_SIZE_64X64))
205 return 1;
206 // Skip odd numbered 64x64 block rows(cols) when bsize is BLOCK_128X128,
207 // BLOCK_64X128(BLOCK_128X128, BLOCK_128X64) as for such blocks CDEF filtering
208 // is done at the corresponding block sizes.
209 if (((fbc & 1) &&
210 (mbmi->bsize == BLOCK_128X128 || mbmi->bsize == BLOCK_128X64)) ||
211 ((fbr & 1) &&
212 (mbmi->bsize == BLOCK_128X128 || mbmi->bsize == BLOCK_64X128)))
213 return 1;
214 return 0;
215 }
216
217 void av1_cdef_dealloc_data(CdefSearchCtx *cdef_search_ctx);
218
219 void av1_cdef_mse_calc_block(CdefSearchCtx *cdef_search_ctx,
220 struct aom_internal_error_info *error_info,
221 int fbr, int fbc, int sb_count);
222 /*!\endcond */
223
224 /*!\brief AV1 CDEF parameter search
225 *
226 * \ingroup in_loop_cdef
227 *
228 * Searches for optimal CDEF parameters for frame
229 *
230 * \param[in,out] cpi Top level encoder structure
231 *
232 * \remark Nothing is returned. Instead, optimal CDEF parameters are stored
233 * in the \c cdef_info structure of type \ref CdefInfo inside \c cm:
234 * \arg \c cdef_bits: Bits of strength parameters
235 * \arg \c nb_cdef_strengths: Number of strength parameters
236 * \arg \c cdef_strengths: list of \c nb_cdef_strengths strength parameters
237 * for the luma plane.
238 * \arg \c uv_cdef_strengths: list of \c nb_cdef_strengths strength parameters
239 * for the chroma planes.
240 * \arg \c damping_factor: CDEF damping factor.
241 *
242 */
243 void av1_cdef_search(struct AV1_COMP *cpi);
244
245 /*!\brief AV1 CDEF level from QP
246 *
247 * \ingroup in_loop_cdef
248 *
249 * Calculates CDEF levels from frame QP. Only used for speed 7+ with RT mode.
250 *
251 * \param[in,out] cm Pointer to top level common structure
252 * \param[in] skip_cdef Flag to skip CDEF filtering
253 * \param[in] is_screen_content Flag indicating screen content
254 *
255 */
256 void av1_pick_cdef_from_qp(AV1_COMMON *const cm, int skip_cdef,
257 int is_screen_content);
258
259 #ifdef __cplusplus
260 } // extern "C"
261 #endif
262 #endif // AOM_AV1_ENCODER_PICKCDEF_H_
263