1 /*
2  * Copyright (c) 2020, 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_ENCODER_ENCODER_UTILS_H_
13 #define AOM_AV1_ENCODER_ENCODER_UTILS_H_
14 
15 #include "config/aom_dsp_rtcd.h"
16 #include "config/aom_scale_rtcd.h"
17 
18 #include "av1/encoder/encoder.h"
19 #include "av1/encoder/encodetxb.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #define AM_SEGMENT_ID_INACTIVE 7
26 #define AM_SEGMENT_ID_ACTIVE 0
27 #define DUMP_RECON_FRAMES 0
28 
29 extern const int default_tx_type_probs[FRAME_UPDATE_TYPES][TX_SIZES_ALL]
30                                       [TX_TYPES];
31 
32 extern const int default_obmc_probs[FRAME_UPDATE_TYPES][BLOCK_SIZES_ALL];
33 
34 extern const int default_warped_probs[FRAME_UPDATE_TYPES];
35 
36 extern const int default_switchable_interp_probs[FRAME_UPDATE_TYPES]
37                                                 [SWITCHABLE_FILTER_CONTEXTS]
38                                                 [SWITCHABLE_FILTERS];
39 
40 // Mark all inactive blocks as active. Other segmentation features may be set
41 // so memset cannot be used, instead only inactive blocks should be reset.
suppress_active_map(AV1_COMP * cpi)42 static AOM_INLINE void suppress_active_map(AV1_COMP *cpi) {
43   unsigned char *const seg_map = cpi->enc_seg.map;
44   int i;
45   if (cpi->active_map.enabled || cpi->active_map.update)
46     for (i = 0;
47          i < cpi->common.mi_params.mi_rows * cpi->common.mi_params.mi_cols; ++i)
48       if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
49         seg_map[i] = AM_SEGMENT_ID_ACTIVE;
50 }
51 
set_mb_mi(CommonModeInfoParams * mi_params,int width,int height)52 static AOM_INLINE void set_mb_mi(CommonModeInfoParams *mi_params, int width,
53                                  int height) {
54   // Ensure that the decoded width and height are both multiples of
55   // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
56   // subsampling is used).
57   // This simplifies the implementation of various experiments,
58   // eg. cdef, which operates on units of 8x8 luma pixels.
59   const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
60   const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
61 
62   mi_params->mi_cols = aligned_width >> MI_SIZE_LOG2;
63   mi_params->mi_rows = aligned_height >> MI_SIZE_LOG2;
64   mi_params->mi_stride = calc_mi_size(mi_params->mi_cols);
65 
66   mi_params->mb_cols = (mi_params->mi_cols + 2) >> 2;
67   mi_params->mb_rows = (mi_params->mi_rows + 2) >> 2;
68   mi_params->MBs = mi_params->mb_rows * mi_params->mb_cols;
69 
70   const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
71   mi_params->mi_alloc_stride =
72       (mi_params->mi_stride + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
73 
74   assert(mi_size_wide[mi_params->mi_alloc_bsize] ==
75          mi_size_high[mi_params->mi_alloc_bsize]);
76 
77 #if CONFIG_LPF_MASK
78   av1_alloc_loop_filter_mask(mi_params);
79 #endif
80 }
81 
enc_free_mi(CommonModeInfoParams * mi_params)82 static AOM_INLINE void enc_free_mi(CommonModeInfoParams *mi_params) {
83   aom_free(mi_params->mi_alloc);
84   mi_params->mi_alloc = NULL;
85   aom_free(mi_params->mi_grid_base);
86   mi_params->mi_grid_base = NULL;
87   mi_params->mi_alloc_size = 0;
88   aom_free(mi_params->tx_type_map);
89   mi_params->tx_type_map = NULL;
90 }
91 
enc_set_mb_mi(CommonModeInfoParams * mi_params,int width,int height)92 static AOM_INLINE void enc_set_mb_mi(CommonModeInfoParams *mi_params, int width,
93                                      int height) {
94   const int is_4k_or_larger = AOMMIN(width, height) >= 2160;
95   mi_params->mi_alloc_bsize = is_4k_or_larger ? BLOCK_8X8 : BLOCK_4X4;
96 
97   set_mb_mi(mi_params, width, height);
98 }
99 
stat_stage_set_mb_mi(CommonModeInfoParams * mi_params,int width,int height)100 static AOM_INLINE void stat_stage_set_mb_mi(CommonModeInfoParams *mi_params,
101                                             int width, int height) {
102   mi_params->mi_alloc_bsize = BLOCK_16X16;
103 
104   set_mb_mi(mi_params, width, height);
105 }
106 
enc_setup_mi(CommonModeInfoParams * mi_params)107 static AOM_INLINE void enc_setup_mi(CommonModeInfoParams *mi_params) {
108   const int mi_grid_size =
109       mi_params->mi_stride * calc_mi_size(mi_params->mi_rows);
110   memset(mi_params->mi_alloc, 0,
111          mi_params->mi_alloc_size * sizeof(*mi_params->mi_alloc));
112   memset(mi_params->mi_grid_base, 0,
113          mi_grid_size * sizeof(*mi_params->mi_grid_base));
114   memset(mi_params->tx_type_map, 0,
115          mi_grid_size * sizeof(*mi_params->tx_type_map));
116 }
117 
init_buffer_indices(ForceIntegerMVInfo * const force_intpel_info,int * const remapped_ref_idx)118 static AOM_INLINE void init_buffer_indices(
119     ForceIntegerMVInfo *const force_intpel_info, int *const remapped_ref_idx) {
120   int fb_idx;
121   for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
122     remapped_ref_idx[fb_idx] = fb_idx;
123   force_intpel_info->rate_index = 0;
124   force_intpel_info->rate_size = 0;
125 }
126 
127 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
128   cpi->fn_ptr[BT].sdf = SDF;                                           \
129   cpi->fn_ptr[BT].sdaf = SDAF;                                         \
130   cpi->fn_ptr[BT].vf = VF;                                             \
131   cpi->fn_ptr[BT].svf = SVF;                                           \
132   cpi->fn_ptr[BT].svaf = SVAF;                                         \
133   cpi->fn_ptr[BT].sdx4df = SDX4DF;                                     \
134   cpi->fn_ptr[BT].jsdaf = JSDAF;                                       \
135   cpi->fn_ptr[BT].jsvaf = JSVAF;
136 
137 #define HIGHBD_BFP_WRAPPER(WIDTH, HEIGHT, BD)                                \
138   HIGHBD_BFP(                                                                \
139       BLOCK_##WIDTH##X##HEIGHT, aom_highbd_sad##WIDTH##x##HEIGHT##_bits##BD, \
140       aom_highbd_sad##WIDTH##x##HEIGHT##_avg_bits##BD,                       \
141       aom_highbd_##BD##_variance##WIDTH##x##HEIGHT,                          \
142       aom_highbd_##BD##_sub_pixel_variance##WIDTH##x##HEIGHT,                \
143       aom_highbd_##BD##_sub_pixel_avg_variance##WIDTH##x##HEIGHT,            \
144       aom_highbd_sad##WIDTH##x##HEIGHT##x4d_bits##BD,                        \
145       aom_highbd_dist_wtd_sad##WIDTH##x##HEIGHT##_avg_bits##BD,              \
146       aom_highbd_##BD##_dist_wtd_sub_pixel_avg_variance##WIDTH##x##HEIGHT)
147 
148 #define MAKE_BFP_SAD_WRAPPER(fnname)                                           \
149   static unsigned int fnname##_bits8(const uint8_t *src_ptr,                   \
150                                      int source_stride,                        \
151                                      const uint8_t *ref_ptr, int ref_stride) { \
152     return fnname(src_ptr, source_stride, ref_ptr, ref_stride);                \
153   }                                                                            \
154   static unsigned int fnname##_bits10(                                         \
155       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
156       int ref_stride) {                                                        \
157     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2;           \
158   }                                                                            \
159   static unsigned int fnname##_bits12(                                         \
160       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
161       int ref_stride) {                                                        \
162     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4;           \
163   }
164 
165 #define MAKE_BFP_SADAVG_WRAPPER(fnname)                                        \
166   static unsigned int fnname##_bits8(                                          \
167       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
168       int ref_stride, const uint8_t *second_pred) {                            \
169     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred);   \
170   }                                                                            \
171   static unsigned int fnname##_bits10(                                         \
172       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
173       int ref_stride, const uint8_t *second_pred) {                            \
174     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
175            2;                                                                  \
176   }                                                                            \
177   static unsigned int fnname##_bits12(                                         \
178       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
179       int ref_stride, const uint8_t *second_pred) {                            \
180     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
181            4;                                                                  \
182   }
183 
184 #define MAKE_BFP_SAD4D_WRAPPER(fnname)                                        \
185   static void fnname##_bits8(const uint8_t *src_ptr, int source_stride,       \
186                              const uint8_t *const ref_ptr[], int ref_stride,  \
187                              unsigned int *sad_array) {                       \
188     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
189   }                                                                           \
190   static void fnname##_bits10(const uint8_t *src_ptr, int source_stride,      \
191                               const uint8_t *const ref_ptr[], int ref_stride, \
192                               unsigned int *sad_array) {                      \
193     int i;                                                                    \
194     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
195     for (i = 0; i < 4; i++) sad_array[i] >>= 2;                               \
196   }                                                                           \
197   static void fnname##_bits12(const uint8_t *src_ptr, int source_stride,      \
198                               const uint8_t *const ref_ptr[], int ref_stride, \
199                               unsigned int *sad_array) {                      \
200     int i;                                                                    \
201     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
202     for (i = 0; i < 4; i++) sad_array[i] >>= 4;                               \
203   }
204 
205 #define MAKE_BFP_JSADAVG_WRAPPER(fnname)                                    \
206   static unsigned int fnname##_bits8(                                       \
207       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,    \
208       int ref_stride, const uint8_t *second_pred,                           \
209       const DIST_WTD_COMP_PARAMS *jcp_param) {                              \
210     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
211                   jcp_param);                                               \
212   }                                                                         \
213   static unsigned int fnname##_bits10(                                      \
214       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,    \
215       int ref_stride, const uint8_t *second_pred,                           \
216       const DIST_WTD_COMP_PARAMS *jcp_param) {                              \
217     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
218                   jcp_param) >>                                             \
219            2;                                                               \
220   }                                                                         \
221   static unsigned int fnname##_bits12(                                      \
222       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,    \
223       int ref_stride, const uint8_t *second_pred,                           \
224       const DIST_WTD_COMP_PARAMS *jcp_param) {                              \
225     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
226                   jcp_param) >>                                             \
227            4;                                                               \
228   }
229 
230 #if CONFIG_AV1_HIGHBITDEPTH
231 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)232 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
233 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
234 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
235 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
236 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
237 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
238 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
239 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
240 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
241 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
242 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
243 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
244 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
245 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
246 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
247 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
248 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
249 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
250 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
251 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
252 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
253 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
254 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
255 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
256 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
257 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
258 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
259 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
260 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
261 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
262 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
263 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
264 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
265 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
266 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
267 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
268 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
269 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
270 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
271 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
272 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
273 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
274 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
275 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
276 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
277 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
278 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
279 
280 #if !CONFIG_REALTIME_ONLY
281 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
282 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
283 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
284 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
285 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
286 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
287 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
288 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
289 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
290 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
291 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
292 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
293 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
294 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
295 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
296 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
297 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
298 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
299 #endif
300 
301 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
302 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
303 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
304 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
305 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
306 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
307 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
308 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
309 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
310 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
311 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
312 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
313 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
314 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
315 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
316 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
317 #if !CONFIG_REALTIME_ONLY
318 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
319 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
320 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
321 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
322 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
323 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
324 #endif
325 #endif  // CONFIG_AV1_HIGHBITDEPTH
326 
327 #define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
328   cpi->fn_ptr[BT].msdf = MCSDF;       \
329   cpi->fn_ptr[BT].msvf = MCSVF;
330 
331 #define HIGHBD_MBFP_WRAPPER(WIDTH, HEIGHT, BD)                    \
332   HIGHBD_MBFP(BLOCK_##WIDTH##X##HEIGHT,                           \
333               aom_highbd_masked_sad##WIDTH##x##HEIGHT##_bits##BD, \
334               aom_highbd_##BD##_masked_sub_pixel_variance##WIDTH##x##HEIGHT)
335 
336 #define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname)                           \
337   static unsigned int fnname##_bits8(                                    \
338       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
339       int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m,  \
340       int m_stride, int invert_mask) {                                   \
341     return fnname(src_ptr, source_stride, ref_ptr, ref_stride,           \
342                   second_pred_ptr, m, m_stride, invert_mask);            \
343   }                                                                      \
344   static unsigned int fnname##_bits10(                                   \
345       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
346       int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m,  \
347       int m_stride, int invert_mask) {                                   \
348     return fnname(src_ptr, source_stride, ref_ptr, ref_stride,           \
349                   second_pred_ptr, m, m_stride, invert_mask) >>          \
350            2;                                                            \
351   }                                                                      \
352   static unsigned int fnname##_bits12(                                   \
353       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
354       int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m,  \
355       int m_stride, int invert_mask) {                                   \
356     return fnname(src_ptr, source_stride, ref_ptr, ref_stride,           \
357                   second_pred_ptr, m, m_stride, invert_mask) >>          \
358            4;                                                            \
359   }
360 
361 #if CONFIG_AV1_HIGHBITDEPTH
362 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
363 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
364 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
365 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
366 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
367 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
368 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
369 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
370 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
371 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
372 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
373 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
374 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
375 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
376 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
377 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
378 #if !CONFIG_REALTIME_ONLY
379 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
380 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
381 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
382 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
383 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
384 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
385 #endif
386 #endif
387 
388 #define HIGHBD_SDSFP(BT, SDSF, SDSX4DF) \
389   cpi->fn_ptr[BT].sdsf = SDSF;          \
390   cpi->fn_ptr[BT].sdsx4df = SDSX4DF;
391 
392 #define HIGHBD_SDSFP_WRAPPER(WIDTH, HEIGHT, BD)                   \
393   HIGHBD_SDSFP(BLOCK_##WIDTH##X##HEIGHT,                          \
394                aom_highbd_sad_skip_##WIDTH##x##HEIGHT##_bits##BD, \
395                aom_highbd_sad_skip_##WIDTH##x##HEIGHT##x4d##_bits##BD)
396 
397 #define MAKE_SDSF_SKIP_SAD_WRAPPER(fnname)                                  \
398   static unsigned int fnname##_bits8(const uint8_t *src, int src_stride,    \
399                                      const uint8_t *ref, int ref_stride) {  \
400     return fnname(src, src_stride, ref, ref_stride);                        \
401   }                                                                         \
402   static unsigned int fnname##_bits10(const uint8_t *src, int src_stride,   \
403                                       const uint8_t *ref, int ref_stride) { \
404     return fnname(src, src_stride, ref, ref_stride) >> 2;                   \
405   }                                                                         \
406   static unsigned int fnname##_bits12(const uint8_t *src, int src_stride,   \
407                                       const uint8_t *ref, int ref_stride) { \
408     return fnname(src, src_stride, ref, ref_stride) >> 4;                   \
409   }
410 
411 #define MAKE_SDSF_SKIP_SAD_4D_WRAPPER(fnname)                                 \
412   static void fnname##_bits8(const uint8_t *src_ptr, int source_stride,       \
413                              const uint8_t *const ref_ptr[], int ref_stride,  \
414                              unsigned int *sad_array) {                       \
415     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
416   }                                                                           \
417   static void fnname##_bits10(const uint8_t *src_ptr, int source_stride,      \
418                               const uint8_t *const ref_ptr[], int ref_stride, \
419                               unsigned int *sad_array) {                      \
420     int i;                                                                    \
421     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
422     for (i = 0; i < 4; i++) sad_array[i] >>= 2;                               \
423   }                                                                           \
424   static void fnname##_bits12(const uint8_t *src_ptr, int source_stride,      \
425                               const uint8_t *const ref_ptr[], int ref_stride, \
426                               unsigned int *sad_array) {                      \
427     int i;                                                                    \
428     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
429     for (i = 0; i < 4; i++) sad_array[i] >>= 4;                               \
430   }
431 
432 #if CONFIG_AV1_HIGHBITDEPTH
433 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_128x128)
434 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_128x64)
435 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_64x128)
436 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_64x64)
437 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_64x32)
438 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_32x64)
439 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_32x32)
440 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_32x16)
441 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_16x32)
442 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_16x16)
443 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_16x8)
444 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_8x16)
445 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_8x8)
446 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_4x8)
447 
448 #if !CONFIG_REALTIME_ONLY
449 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_64x16)
450 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_32x8)
451 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_16x64)
452 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_4x16)
453 MAKE_SDSF_SKIP_SAD_WRAPPER(aom_highbd_sad_skip_8x32)
454 #endif
455 
456 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_128x128x4d)
457 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_128x64x4d)
458 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_64x128x4d)
459 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_64x64x4d)
460 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_64x32x4d)
461 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_32x64x4d)
462 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_32x32x4d)
463 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_32x16x4d)
464 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_16x32x4d)
465 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_16x16x4d)
466 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_16x8x4d)
467 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_8x16x4d)
468 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_8x8x4d)
469 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_4x8x4d)
470 
471 #if !CONFIG_REALTIME_ONLY
472 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_64x16x4d)
473 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_32x8x4d)
474 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_16x64x4d)
475 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_4x16x4d)
476 MAKE_SDSF_SKIP_SAD_4D_WRAPPER(aom_highbd_sad_skip_8x32x4d)
477 #endif
478 #endif
479 
480 #if !CONFIG_REALTIME_ONLY
481 
482 #if CONFIG_AV1_HIGHBITDEPTH
483 #define HIGHBD_OBFP_WRAPPER_8(WIDTH, HEIGHT)                 \
484   HIGHBD_OBFP(BLOCK_##WIDTH##X##HEIGHT,                      \
485               aom_highbd_obmc_sad##WIDTH##x##HEIGHT##_bits8, \
486               aom_highbd_obmc_variance##WIDTH##x##HEIGHT,    \
487               aom_highbd_obmc_sub_pixel_variance##WIDTH##x##HEIGHT)
488 
489 #define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
490   cpi->fn_ptr[BT].osdf = OSDF;           \
491   cpi->fn_ptr[BT].ovf = OVF;             \
492   cpi->fn_ptr[BT].osvf = OSVF;
493 
494 #define HIGHBD_OBFP_WRAPPER(WIDTH, HEIGHT, BD)                   \
495   HIGHBD_OBFP(BLOCK_##WIDTH##X##HEIGHT,                          \
496               aom_highbd_obmc_sad##WIDTH##x##HEIGHT##_bits##BD,  \
497               aom_highbd_##BD##_obmc_variance##WIDTH##x##HEIGHT, \
498               aom_highbd_##BD##_obmc_sub_pixel_variance##WIDTH##x##HEIGHT)
499 
500 #define MAKE_OBFP_SAD_WRAPPER(fnname)                                     \
501   static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride,  \
502                                      const int32_t *wsrc,                 \
503                                      const int32_t *msk) {                \
504     return fnname(ref, ref_stride, wsrc, msk);                            \
505   }                                                                       \
506   static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
507                                       const int32_t *wsrc,                \
508                                       const int32_t *msk) {               \
509     return fnname(ref, ref_stride, wsrc, msk) >> 2;                       \
510   }                                                                       \
511   static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
512                                       const int32_t *wsrc,                \
513                                       const int32_t *msk) {               \
514     return fnname(ref, ref_stride, wsrc, msk) >> 4;                       \
515   }
516 #endif  // CONFIG_AV1_HIGHBITDEPTH
517 #endif  // !CONFIG_REALTIME_ONLY
518 
519 #if CONFIG_AV1_HIGHBITDEPTH
520 #if !CONFIG_REALTIME_ONLY
521 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
522 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
523 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
524 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
525 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
526 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
527 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
528 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
529 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
530 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
531 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
532 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
533 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
534 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
535 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
536 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
537 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
538 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
539 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
540 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
541 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
542 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
543 #endif
544 
545 static AOM_INLINE void highbd_set_var_fns(AV1_COMP *const cpi) {
546   AV1_COMMON *const cm = &cpi->common;
547   if (cm->seq_params.use_highbitdepth) {
548     switch (cm->seq_params.bit_depth) {
549       case AOM_BITS_8:
550 #if !CONFIG_REALTIME_ONLY
551         HIGHBD_BFP_WRAPPER(64, 16, 8)
552         HIGHBD_BFP_WRAPPER(16, 64, 8)
553         HIGHBD_BFP_WRAPPER(32, 8, 8)
554         HIGHBD_BFP_WRAPPER(8, 32, 8)
555         HIGHBD_BFP_WRAPPER(16, 4, 8)
556         HIGHBD_BFP_WRAPPER(4, 16, 8)
557 #endif
558         HIGHBD_BFP_WRAPPER(32, 16, 8)
559         HIGHBD_BFP_WRAPPER(16, 32, 8)
560         HIGHBD_BFP_WRAPPER(64, 32, 8)
561         HIGHBD_BFP_WRAPPER(32, 64, 8)
562         HIGHBD_BFP_WRAPPER(32, 32, 8)
563         HIGHBD_BFP_WRAPPER(64, 64, 8)
564         HIGHBD_BFP_WRAPPER(16, 16, 8)
565         HIGHBD_BFP_WRAPPER(16, 8, 8)
566         HIGHBD_BFP_WRAPPER(8, 16, 8)
567         HIGHBD_BFP_WRAPPER(8, 8, 8)
568         HIGHBD_BFP_WRAPPER(8, 4, 8)
569         HIGHBD_BFP_WRAPPER(4, 8, 8)
570         HIGHBD_BFP_WRAPPER(4, 4, 8)
571         HIGHBD_BFP_WRAPPER(128, 128, 8)
572         HIGHBD_BFP_WRAPPER(128, 64, 8)
573         HIGHBD_BFP_WRAPPER(64, 128, 8)
574 
575         HIGHBD_MBFP_WRAPPER(128, 128, 8)
576         HIGHBD_MBFP_WRAPPER(128, 64, 8)
577         HIGHBD_MBFP_WRAPPER(64, 128, 8)
578         HIGHBD_MBFP_WRAPPER(64, 64, 8)
579         HIGHBD_MBFP_WRAPPER(64, 32, 8)
580         HIGHBD_MBFP_WRAPPER(32, 64, 8)
581         HIGHBD_MBFP_WRAPPER(32, 32, 8)
582         HIGHBD_MBFP_WRAPPER(32, 16, 8)
583         HIGHBD_MBFP_WRAPPER(16, 32, 8)
584         HIGHBD_MBFP_WRAPPER(16, 16, 8)
585         HIGHBD_MBFP_WRAPPER(8, 16, 8)
586         HIGHBD_MBFP_WRAPPER(16, 8, 8)
587         HIGHBD_MBFP_WRAPPER(8, 8, 8)
588         HIGHBD_MBFP_WRAPPER(4, 8, 8)
589         HIGHBD_MBFP_WRAPPER(8, 4, 8)
590         HIGHBD_MBFP_WRAPPER(4, 4, 8)
591 #if !CONFIG_REALTIME_ONLY
592         HIGHBD_MBFP_WRAPPER(64, 16, 8)
593         HIGHBD_MBFP_WRAPPER(16, 64, 8)
594         HIGHBD_MBFP_WRAPPER(32, 8, 8)
595         HIGHBD_MBFP_WRAPPER(8, 32, 8)
596         HIGHBD_MBFP_WRAPPER(16, 4, 8)
597         HIGHBD_MBFP_WRAPPER(4, 16, 8)
598 #endif
599 
600 // OBMC excluded from realtime only build.
601 #if !CONFIG_REALTIME_ONLY
602         HIGHBD_OBFP_WRAPPER_8(128, 128)
603         HIGHBD_OBFP_WRAPPER_8(128, 64)
604         HIGHBD_OBFP_WRAPPER_8(64, 128)
605         HIGHBD_OBFP_WRAPPER_8(64, 64)
606         HIGHBD_OBFP_WRAPPER_8(64, 32)
607         HIGHBD_OBFP_WRAPPER_8(32, 64)
608         HIGHBD_OBFP_WRAPPER_8(32, 32)
609         HIGHBD_OBFP_WRAPPER_8(32, 16)
610         HIGHBD_OBFP_WRAPPER_8(16, 32)
611         HIGHBD_OBFP_WRAPPER_8(16, 16)
612         HIGHBD_OBFP_WRAPPER_8(8, 16)
613         HIGHBD_OBFP_WRAPPER_8(16, 8)
614         HIGHBD_OBFP_WRAPPER_8(8, 8)
615         HIGHBD_OBFP_WRAPPER_8(4, 8)
616         HIGHBD_OBFP_WRAPPER_8(8, 4)
617         HIGHBD_OBFP_WRAPPER_8(4, 4)
618         HIGHBD_OBFP_WRAPPER_8(64, 16)
619         HIGHBD_OBFP_WRAPPER_8(16, 64)
620         HIGHBD_OBFP_WRAPPER_8(32, 8)
621         HIGHBD_OBFP_WRAPPER_8(8, 32)
622         HIGHBD_OBFP_WRAPPER_8(16, 4)
623         HIGHBD_OBFP_WRAPPER_8(4, 16)
624 #endif
625 
626         HIGHBD_SDSFP_WRAPPER(128, 128, 8);
627         HIGHBD_SDSFP_WRAPPER(128, 64, 8);
628         HIGHBD_SDSFP_WRAPPER(64, 128, 8);
629         HIGHBD_SDSFP_WRAPPER(64, 64, 8);
630         HIGHBD_SDSFP_WRAPPER(64, 32, 8);
631         HIGHBD_SDSFP_WRAPPER(32, 64, 8);
632         HIGHBD_SDSFP_WRAPPER(32, 32, 8);
633         HIGHBD_SDSFP_WRAPPER(32, 16, 8);
634         HIGHBD_SDSFP_WRAPPER(16, 32, 8);
635         HIGHBD_SDSFP_WRAPPER(16, 16, 8);
636         HIGHBD_SDSFP_WRAPPER(16, 8, 8);
637         HIGHBD_SDSFP_WRAPPER(8, 16, 8);
638         HIGHBD_SDSFP_WRAPPER(8, 8, 8);
639         HIGHBD_SDSFP_WRAPPER(4, 8, 8);
640 #if !CONFIG_REALTIME_ONLY
641         HIGHBD_SDSFP_WRAPPER(64, 16, 8);
642         HIGHBD_SDSFP_WRAPPER(32, 8, 8);
643         HIGHBD_SDSFP_WRAPPER(16, 64, 8);
644         HIGHBD_SDSFP_WRAPPER(8, 32, 8);
645         HIGHBD_SDSFP_WRAPPER(4, 16, 8);
646 #endif
647         break;
648 
649       case AOM_BITS_10:
650 #if !CONFIG_REALTIME_ONLY
651         HIGHBD_BFP_WRAPPER(64, 16, 10)
652         HIGHBD_BFP_WRAPPER(16, 64, 10)
653         HIGHBD_BFP_WRAPPER(32, 8, 10)
654         HIGHBD_BFP_WRAPPER(8, 32, 10)
655         HIGHBD_BFP_WRAPPER(16, 4, 10)
656         HIGHBD_BFP_WRAPPER(4, 16, 10)
657 #endif
658         HIGHBD_BFP_WRAPPER(32, 16, 10)
659         HIGHBD_BFP_WRAPPER(16, 32, 10)
660         HIGHBD_BFP_WRAPPER(64, 32, 10)
661         HIGHBD_BFP_WRAPPER(32, 64, 10)
662         HIGHBD_BFP_WRAPPER(32, 32, 10)
663         HIGHBD_BFP_WRAPPER(64, 64, 10)
664         HIGHBD_BFP_WRAPPER(16, 16, 10)
665         HIGHBD_BFP_WRAPPER(16, 8, 10)
666         HIGHBD_BFP_WRAPPER(8, 16, 10)
667         HIGHBD_BFP_WRAPPER(8, 8, 10)
668         HIGHBD_BFP_WRAPPER(8, 4, 10)
669         HIGHBD_BFP_WRAPPER(4, 8, 10)
670         HIGHBD_BFP_WRAPPER(4, 4, 10)
671         HIGHBD_BFP_WRAPPER(128, 128, 10)
672         HIGHBD_BFP_WRAPPER(128, 64, 10)
673         HIGHBD_BFP_WRAPPER(64, 128, 10)
674 
675         HIGHBD_MBFP_WRAPPER(128, 128, 10)
676         HIGHBD_MBFP_WRAPPER(128, 64, 10)
677         HIGHBD_MBFP_WRAPPER(64, 128, 10)
678         HIGHBD_MBFP_WRAPPER(64, 64, 10)
679         HIGHBD_MBFP_WRAPPER(64, 32, 10)
680         HIGHBD_MBFP_WRAPPER(32, 64, 10)
681         HIGHBD_MBFP_WRAPPER(32, 32, 10)
682         HIGHBD_MBFP_WRAPPER(32, 16, 10)
683         HIGHBD_MBFP_WRAPPER(16, 32, 10)
684         HIGHBD_MBFP_WRAPPER(16, 16, 10)
685         HIGHBD_MBFP_WRAPPER(8, 16, 10)
686         HIGHBD_MBFP_WRAPPER(16, 8, 10)
687         HIGHBD_MBFP_WRAPPER(8, 8, 10)
688         HIGHBD_MBFP_WRAPPER(4, 8, 10)
689         HIGHBD_MBFP_WRAPPER(8, 4, 10)
690         HIGHBD_MBFP_WRAPPER(4, 4, 10)
691 #if !CONFIG_REALTIME_ONLY
692         HIGHBD_MBFP_WRAPPER(64, 16, 10)
693         HIGHBD_MBFP_WRAPPER(16, 64, 10)
694         HIGHBD_MBFP_WRAPPER(32, 8, 10)
695         HIGHBD_MBFP_WRAPPER(8, 32, 10)
696         HIGHBD_MBFP_WRAPPER(16, 4, 10)
697         HIGHBD_MBFP_WRAPPER(4, 16, 10)
698 #endif
699 
700 // OBMC excluded from realtime only build.
701 #if !CONFIG_REALTIME_ONLY
702         HIGHBD_OBFP_WRAPPER(128, 128, 10)
703         HIGHBD_OBFP_WRAPPER(128, 64, 10)
704         HIGHBD_OBFP_WRAPPER(64, 128, 10)
705         HIGHBD_OBFP_WRAPPER(64, 64, 10)
706         HIGHBD_OBFP_WRAPPER(64, 32, 10)
707         HIGHBD_OBFP_WRAPPER(32, 64, 10)
708         HIGHBD_OBFP_WRAPPER(32, 32, 10)
709         HIGHBD_OBFP_WRAPPER(32, 16, 10)
710         HIGHBD_OBFP_WRAPPER(16, 32, 10)
711         HIGHBD_OBFP_WRAPPER(16, 16, 10)
712         HIGHBD_OBFP_WRAPPER(8, 16, 10)
713         HIGHBD_OBFP_WRAPPER(16, 8, 10)
714         HIGHBD_OBFP_WRAPPER(8, 8, 10)
715         HIGHBD_OBFP_WRAPPER(4, 8, 10)
716         HIGHBD_OBFP_WRAPPER(8, 4, 10)
717         HIGHBD_OBFP_WRAPPER(4, 4, 10)
718         HIGHBD_OBFP_WRAPPER(64, 16, 10)
719         HIGHBD_OBFP_WRAPPER(16, 64, 10)
720         HIGHBD_OBFP_WRAPPER(32, 8, 10)
721         HIGHBD_OBFP_WRAPPER(8, 32, 10)
722         HIGHBD_OBFP_WRAPPER(16, 4, 10)
723         HIGHBD_OBFP_WRAPPER(4, 16, 10)
724 #endif
725 
726         HIGHBD_SDSFP_WRAPPER(128, 128, 10);
727         HIGHBD_SDSFP_WRAPPER(128, 64, 10);
728         HIGHBD_SDSFP_WRAPPER(64, 128, 10);
729         HIGHBD_SDSFP_WRAPPER(64, 64, 10);
730         HIGHBD_SDSFP_WRAPPER(64, 32, 10);
731         HIGHBD_SDSFP_WRAPPER(32, 64, 10);
732         HIGHBD_SDSFP_WRAPPER(32, 32, 10);
733         HIGHBD_SDSFP_WRAPPER(32, 16, 10);
734         HIGHBD_SDSFP_WRAPPER(16, 32, 10);
735         HIGHBD_SDSFP_WRAPPER(16, 16, 10);
736         HIGHBD_SDSFP_WRAPPER(16, 8, 10);
737         HIGHBD_SDSFP_WRAPPER(8, 16, 10);
738         HIGHBD_SDSFP_WRAPPER(8, 8, 10);
739         HIGHBD_SDSFP_WRAPPER(4, 8, 10);
740 
741 #if !CONFIG_REALTIME_ONLY
742         HIGHBD_SDSFP_WRAPPER(64, 16, 10);
743         HIGHBD_SDSFP_WRAPPER(32, 8, 10);
744         HIGHBD_SDSFP_WRAPPER(16, 64, 10);
745         HIGHBD_SDSFP_WRAPPER(8, 32, 10);
746         HIGHBD_SDSFP_WRAPPER(4, 16, 10);
747 #endif
748         break;
749 
750       case AOM_BITS_12:
751 #if !CONFIG_REALTIME_ONLY
752         HIGHBD_BFP_WRAPPER(64, 16, 12)
753         HIGHBD_BFP_WRAPPER(16, 64, 12)
754         HIGHBD_BFP_WRAPPER(32, 8, 12)
755         HIGHBD_BFP_WRAPPER(8, 32, 12)
756         HIGHBD_BFP_WRAPPER(16, 4, 12)
757         HIGHBD_BFP_WRAPPER(4, 16, 12)
758 #endif
759         HIGHBD_BFP_WRAPPER(32, 16, 12)
760         HIGHBD_BFP_WRAPPER(16, 32, 12)
761         HIGHBD_BFP_WRAPPER(64, 32, 12)
762         HIGHBD_BFP_WRAPPER(32, 64, 12)
763         HIGHBD_BFP_WRAPPER(32, 32, 12)
764         HIGHBD_BFP_WRAPPER(64, 64, 12)
765         HIGHBD_BFP_WRAPPER(16, 16, 12)
766         HIGHBD_BFP_WRAPPER(16, 8, 12)
767         HIGHBD_BFP_WRAPPER(8, 16, 12)
768         HIGHBD_BFP_WRAPPER(8, 8, 12)
769         HIGHBD_BFP_WRAPPER(8, 4, 12)
770         HIGHBD_BFP_WRAPPER(4, 8, 12)
771         HIGHBD_BFP_WRAPPER(4, 4, 12)
772         HIGHBD_BFP_WRAPPER(128, 128, 12)
773         HIGHBD_BFP_WRAPPER(128, 64, 12)
774         HIGHBD_BFP_WRAPPER(64, 128, 12)
775 
776         HIGHBD_MBFP_WRAPPER(128, 128, 12)
777         HIGHBD_MBFP_WRAPPER(128, 64, 12)
778         HIGHBD_MBFP_WRAPPER(64, 128, 12)
779         HIGHBD_MBFP_WRAPPER(64, 64, 12)
780         HIGHBD_MBFP_WRAPPER(64, 32, 12)
781         HIGHBD_MBFP_WRAPPER(32, 64, 12)
782         HIGHBD_MBFP_WRAPPER(32, 32, 12)
783         HIGHBD_MBFP_WRAPPER(32, 16, 12)
784         HIGHBD_MBFP_WRAPPER(16, 32, 12)
785         HIGHBD_MBFP_WRAPPER(16, 16, 12)
786         HIGHBD_MBFP_WRAPPER(8, 16, 12)
787         HIGHBD_MBFP_WRAPPER(16, 8, 12)
788         HIGHBD_MBFP_WRAPPER(8, 8, 12)
789         HIGHBD_MBFP_WRAPPER(4, 8, 12)
790         HIGHBD_MBFP_WRAPPER(8, 4, 12)
791         HIGHBD_MBFP_WRAPPER(4, 4, 12)
792 #if !CONFIG_REALTIME_ONLY
793         HIGHBD_MBFP_WRAPPER(64, 16, 12)
794         HIGHBD_MBFP_WRAPPER(16, 64, 12)
795         HIGHBD_MBFP_WRAPPER(32, 8, 12)
796         HIGHBD_MBFP_WRAPPER(8, 32, 12)
797         HIGHBD_MBFP_WRAPPER(16, 4, 12)
798         HIGHBD_MBFP_WRAPPER(4, 16, 12)
799 #endif
800 
801 // OBMC excluded from realtime only build.
802 #if !CONFIG_REALTIME_ONLY
803         HIGHBD_OBFP_WRAPPER(128, 128, 12)
804         HIGHBD_OBFP_WRAPPER(128, 64, 12)
805         HIGHBD_OBFP_WRAPPER(64, 128, 12)
806         HIGHBD_OBFP_WRAPPER(64, 64, 12)
807         HIGHBD_OBFP_WRAPPER(64, 32, 12)
808         HIGHBD_OBFP_WRAPPER(32, 64, 12)
809         HIGHBD_OBFP_WRAPPER(32, 32, 12)
810         HIGHBD_OBFP_WRAPPER(32, 16, 12)
811         HIGHBD_OBFP_WRAPPER(16, 32, 12)
812         HIGHBD_OBFP_WRAPPER(16, 16, 12)
813         HIGHBD_OBFP_WRAPPER(8, 16, 12)
814         HIGHBD_OBFP_WRAPPER(16, 8, 12)
815         HIGHBD_OBFP_WRAPPER(8, 8, 12)
816         HIGHBD_OBFP_WRAPPER(4, 8, 12)
817         HIGHBD_OBFP_WRAPPER(8, 4, 12)
818         HIGHBD_OBFP_WRAPPER(4, 4, 12)
819         HIGHBD_OBFP_WRAPPER(64, 16, 12)
820         HIGHBD_OBFP_WRAPPER(16, 64, 12)
821         HIGHBD_OBFP_WRAPPER(32, 8, 12)
822         HIGHBD_OBFP_WRAPPER(8, 32, 12)
823         HIGHBD_OBFP_WRAPPER(16, 4, 12)
824         HIGHBD_OBFP_WRAPPER(4, 16, 12)
825 #endif
826 
827         HIGHBD_SDSFP_WRAPPER(128, 128, 12);
828         HIGHBD_SDSFP_WRAPPER(128, 64, 12);
829         HIGHBD_SDSFP_WRAPPER(64, 128, 12);
830         HIGHBD_SDSFP_WRAPPER(64, 64, 12);
831         HIGHBD_SDSFP_WRAPPER(64, 32, 12);
832         HIGHBD_SDSFP_WRAPPER(32, 64, 12);
833         HIGHBD_SDSFP_WRAPPER(32, 32, 12);
834         HIGHBD_SDSFP_WRAPPER(32, 16, 12);
835         HIGHBD_SDSFP_WRAPPER(16, 32, 12);
836         HIGHBD_SDSFP_WRAPPER(16, 16, 12);
837         HIGHBD_SDSFP_WRAPPER(16, 8, 12);
838         HIGHBD_SDSFP_WRAPPER(8, 16, 12);
839         HIGHBD_SDSFP_WRAPPER(8, 8, 12);
840         HIGHBD_SDSFP_WRAPPER(4, 8, 12);
841 
842 #if !CONFIG_REALTIME_ONLY
843         HIGHBD_SDSFP_WRAPPER(64, 16, 12);
844         HIGHBD_SDSFP_WRAPPER(32, 8, 12);
845         HIGHBD_SDSFP_WRAPPER(16, 64, 12);
846         HIGHBD_SDSFP_WRAPPER(8, 32, 12);
847         HIGHBD_SDSFP_WRAPPER(4, 16, 12);
848 #endif
849         break;
850 
851       default:
852         assert(0 &&
853                "cm->seq_params.bit_depth should be AOM_BITS_8, "
854                "AOM_BITS_10 or AOM_BITS_12");
855     }
856   }
857 }
858 #endif  // CONFIG_AV1_HIGHBITDEPTH
859 
copy_frame_prob_info(AV1_COMP * cpi)860 static AOM_INLINE void copy_frame_prob_info(AV1_COMP *cpi) {
861   FrameProbInfo *const frame_probs = &cpi->frame_probs;
862   if (cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats) {
863     av1_copy(frame_probs->tx_type_probs, default_tx_type_probs);
864   }
865   if (cpi->sf.inter_sf.prune_obmc_prob_thresh > 0 &&
866       cpi->sf.inter_sf.prune_obmc_prob_thresh < INT_MAX) {
867     av1_copy(frame_probs->obmc_probs, default_obmc_probs);
868   }
869   if (cpi->sf.inter_sf.prune_warped_prob_thresh > 0) {
870     av1_copy(frame_probs->warped_probs, default_warped_probs);
871   }
872   if (cpi->sf.interp_sf.adaptive_interp_filter_search == 2) {
873     av1_copy(frame_probs->switchable_interp_probs,
874              default_switchable_interp_probs);
875   }
876 }
877 
878 // Coding context that only needs to be restored when recode loop includes
879 // filtering (deblocking, CDEF, superres post-encode upscale and/or loop
880 // restoraton).
restore_extra_coding_context(AV1_COMP * cpi)881 static AOM_INLINE void restore_extra_coding_context(AV1_COMP *cpi) {
882   CODING_CONTEXT *const cc = &cpi->coding_context;
883   AV1_COMMON *cm = &cpi->common;
884   cm->lf = cc->lf;
885   cm->cdef_info = cc->cdef_info;
886   cpi->rc = cc->rc;
887   cpi->mv_stats = cc->mv_stats;
888 }
889 
equal_dimensions_and_border(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)890 static AOM_INLINE int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
891                                                   const YV12_BUFFER_CONFIG *b) {
892   return a->y_height == b->y_height && a->y_width == b->y_width &&
893          a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
894          a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
895          a->border == b->border &&
896          (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
897              (b->flags & YV12_FLAG_HIGHBITDEPTH);
898 }
899 
update_entropy(bool * ext_refresh_frame_context,bool * ext_refresh_frame_context_pending,bool update)900 static AOM_INLINE int update_entropy(bool *ext_refresh_frame_context,
901                                      bool *ext_refresh_frame_context_pending,
902                                      bool update) {
903   *ext_refresh_frame_context = update;
904   *ext_refresh_frame_context_pending = 1;
905   return 0;
906 }
907 
908 #if !CONFIG_REALTIME_ONLY
combine_prior_with_tpl_boost(double min_factor,double max_factor,int prior_boost,int tpl_boost,int frames_to_key)909 static AOM_INLINE int combine_prior_with_tpl_boost(double min_factor,
910                                                    double max_factor,
911                                                    int prior_boost,
912                                                    int tpl_boost,
913                                                    int frames_to_key) {
914   double factor = sqrt((double)frames_to_key);
915   double range = max_factor - min_factor;
916   factor = AOMMIN(factor, max_factor);
917   factor = AOMMAX(factor, min_factor);
918   factor -= min_factor;
919   int boost =
920       (int)((factor * prior_boost + (range - factor) * tpl_boost) / range);
921   return boost;
922 }
923 #endif
924 
set_size_independent_vars(AV1_COMP * cpi)925 static AOM_INLINE void set_size_independent_vars(AV1_COMP *cpi) {
926   int i;
927   AV1_COMMON *const cm = &cpi->common;
928   for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
929     cm->global_motion[i] = default_warp_params;
930   }
931   cpi->gm_info.search_done = 0;
932 
933   av1_set_speed_features_framesize_independent(cpi, cpi->speed);
934   av1_set_rd_speed_thresholds(cpi);
935   cm->features.interp_filter = SWITCHABLE;
936   cm->features.switchable_motion_mode = 1;
937 }
938 
release_scaled_references(AV1_COMP * cpi)939 static AOM_INLINE void release_scaled_references(AV1_COMP *cpi) {
940   // TODO(isbs): only refresh the necessary frames, rather than all of them
941   for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
942     RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
943     if (buf != NULL) {
944       --buf->ref_count;
945       cpi->scaled_ref_buf[i] = NULL;
946     }
947   }
948 }
949 
restore_all_coding_context(AV1_COMP * cpi)950 static AOM_INLINE void restore_all_coding_context(AV1_COMP *cpi) {
951   restore_extra_coding_context(cpi);
952   if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
953 }
954 
955 // Refresh reference frame buffers according to refresh_frame_flags.
refresh_reference_frames(AV1_COMP * cpi)956 static AOM_INLINE void refresh_reference_frames(AV1_COMP *cpi) {
957   AV1_COMMON *const cm = &cpi->common;
958   // All buffers are refreshed for shown keyframes and S-frames.
959 
960   for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
961     if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
962       assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
963     }
964   }
965 }
966 
967 void av1_update_film_grain_parameters(struct AV1_COMP *cpi,
968                                       const AV1EncoderConfig *oxcf);
969 
970 void av1_scale_references(AV1_COMP *cpi, const InterpFilter filter,
971                           const int phase, const int use_optimized_scaler);
972 
973 void av1_setup_frame(AV1_COMP *cpi);
974 
975 BLOCK_SIZE av1_select_sb_size(const AV1_COMP *const cpi);
976 
977 void av1_apply_active_map(AV1_COMP *cpi);
978 
979 #if !CONFIG_REALTIME_ONLY
980 uint16_t av1_setup_interp_filter_search_mask(AV1_COMP *cpi);
981 
982 void av1_determine_sc_tools_with_encoding(AV1_COMP *cpi, const int q_orig);
983 #endif
984 
985 void av1_set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
986                                  int *top_index);
987 
988 void av1_finalize_encoded_frame(AV1_COMP *const cpi);
989 
990 int av1_is_integer_mv(const YV12_BUFFER_CONFIG *cur_picture,
991                       const YV12_BUFFER_CONFIG *last_picture,
992                       ForceIntegerMVInfo *const force_intpel_info);
993 
994 void av1_set_mb_ssim_rdmult_scaling(AV1_COMP *cpi);
995 
996 void av1_save_all_coding_context(AV1_COMP *cpi);
997 
998 #if DUMP_RECON_FRAMES == 1
999 void av1_dump_filtered_recon_frames(AV1_COMP *cpi);
1000 #endif
1001 
1002 #ifdef __cplusplus
1003 }  // extern "C"
1004 #endif
1005 
1006 #endif  // AOM_AV1_ENCODER_ENCODER_UTILS_H_
1007