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_CFL_H_
13 #define AOM_AV1_COMMON_CFL_H_
14
15 #include "av1/common/blockd.h"
16 #include "av1/common/onyxc_int.h"
17
18 // Can we use CfL for the current block?
is_cfl_allowed(const MACROBLOCKD * xd)19 static INLINE CFL_ALLOWED_TYPE is_cfl_allowed(const MACROBLOCKD *xd) {
20 const MB_MODE_INFO *mbmi = xd->mi[0];
21 const BLOCK_SIZE bsize = mbmi->sb_type;
22 assert(bsize < BLOCK_SIZES_ALL);
23 if (xd->lossless[mbmi->segment_id]) {
24 // In lossless, CfL is available when the partition size is equal to the
25 // transform size.
26 const int ssx = xd->plane[AOM_PLANE_U].subsampling_x;
27 const int ssy = xd->plane[AOM_PLANE_U].subsampling_y;
28 const int plane_bsize = get_plane_block_size(bsize, ssx, ssy);
29 return (CFL_ALLOWED_TYPE)(plane_bsize == BLOCK_4X4);
30 }
31 // Spec: CfL is available to luma partitions lesser than or equal to 32x32
32 return (CFL_ALLOWED_TYPE)(block_size_wide[bsize] <= 32 &&
33 block_size_high[bsize] <= 32);
34 }
35
36 // Do we need to save the luma pixels from the current block,
37 // for a possible future CfL prediction?
store_cfl_required(const AV1_COMMON * cm,const MACROBLOCKD * xd)38 static INLINE CFL_ALLOWED_TYPE store_cfl_required(const AV1_COMMON *cm,
39 const MACROBLOCKD *xd) {
40 const MB_MODE_INFO *mbmi = xd->mi[0];
41
42 if (cm->seq_params.monochrome) return CFL_DISALLOWED;
43
44 if (!xd->cfl.is_chroma_reference) {
45 // For non-chroma-reference blocks, we should always store the luma pixels,
46 // in case the corresponding chroma-reference block uses CfL.
47 // Note that this can only happen for block sizes which are <8 on
48 // their shortest side, as otherwise they would be chroma reference
49 // blocks.
50 return CFL_ALLOWED;
51 }
52
53 // If this block has chroma information, we know whether we're
54 // actually going to perform a CfL prediction
55 return (CFL_ALLOWED_TYPE)(!is_inter_block(mbmi) &&
56 mbmi->uv_mode == UV_CFL_PRED);
57 }
58
get_scaled_luma_q0(int alpha_q3,int16_t pred_buf_q3)59 static INLINE int get_scaled_luma_q0(int alpha_q3, int16_t pred_buf_q3) {
60 int scaled_luma_q6 = alpha_q3 * pred_buf_q3;
61 return ROUND_POWER_OF_TWO_SIGNED(scaled_luma_q6, 6);
62 }
63
get_cfl_pred_type(PLANE_TYPE plane)64 static INLINE CFL_PRED_TYPE get_cfl_pred_type(PLANE_TYPE plane) {
65 assert(plane > 0);
66 return (CFL_PRED_TYPE)(plane - 1);
67 }
68
69 void cfl_predict_block(MACROBLOCKD *const xd, uint8_t *dst, int dst_stride,
70 TX_SIZE tx_size, int plane);
71
72 void cfl_store_block(MACROBLOCKD *const xd, BLOCK_SIZE bsize, TX_SIZE tx_size);
73
74 void cfl_store_tx(MACROBLOCKD *const xd, int row, int col, TX_SIZE tx_size,
75 BLOCK_SIZE bsize);
76
77 void cfl_store_dc_pred(MACROBLOCKD *const xd, const uint8_t *input,
78 CFL_PRED_TYPE pred_plane, int width);
79
80 void cfl_load_dc_pred(MACROBLOCKD *const xd, uint8_t *dst, int dst_stride,
81 TX_SIZE tx_size, CFL_PRED_TYPE pred_plane);
82
83 // Null function used for invalid tx_sizes
84 void cfl_subsample_lbd_null(const uint8_t *input, int input_stride,
85 uint16_t *output_q3);
86
87 // Null function used for invalid tx_sizes
88 void cfl_subsample_hbd_null(const uint16_t *input, int input_stride,
89 uint16_t *output_q3);
90
91 // Allows the CFL_SUBSAMPLE function to switch types depending on the bitdepth.
92 #define CFL_lbd_TYPE uint8_t *cfl_type
93 #define CFL_hbd_TYPE uint16_t *cfl_type
94
95 // Declare a size-specific wrapper for the size-generic function. The compiler
96 // will inline the size generic function in here, the advantage is that the size
97 // will be constant allowing for loop unrolling and other constant propagated
98 // goodness.
99 #define CFL_SUBSAMPLE(arch, sub, bd, width, height) \
100 void subsample_##bd##_##sub##_##width##x##height##_##arch( \
101 const CFL_##bd##_TYPE, int input_stride, uint16_t *output_q3) { \
102 cfl_luma_subsampling_##sub##_##bd##_##arch(cfl_type, input_stride, \
103 output_q3, width, height); \
104 }
105
106 // Declare size-specific wrappers for all valid CfL sizes.
107 #define CFL_SUBSAMPLE_FUNCTIONS(arch, sub, bd) \
108 CFL_SUBSAMPLE(arch, sub, bd, 4, 4) \
109 CFL_SUBSAMPLE(arch, sub, bd, 8, 8) \
110 CFL_SUBSAMPLE(arch, sub, bd, 16, 16) \
111 CFL_SUBSAMPLE(arch, sub, bd, 32, 32) \
112 CFL_SUBSAMPLE(arch, sub, bd, 4, 8) \
113 CFL_SUBSAMPLE(arch, sub, bd, 8, 4) \
114 CFL_SUBSAMPLE(arch, sub, bd, 8, 16) \
115 CFL_SUBSAMPLE(arch, sub, bd, 16, 8) \
116 CFL_SUBSAMPLE(arch, sub, bd, 16, 32) \
117 CFL_SUBSAMPLE(arch, sub, bd, 32, 16) \
118 CFL_SUBSAMPLE(arch, sub, bd, 4, 16) \
119 CFL_SUBSAMPLE(arch, sub, bd, 16, 4) \
120 CFL_SUBSAMPLE(arch, sub, bd, 8, 32) \
121 CFL_SUBSAMPLE(arch, sub, bd, 32, 8) \
122 cfl_subsample_##bd##_fn cfl_get_luma_subsampling_##sub##_##bd##_##arch( \
123 TX_SIZE tx_size) { \
124 CFL_SUBSAMPLE_FUNCTION_ARRAY(arch, sub, bd) \
125 return subfn_##sub[tx_size]; \
126 }
127
128 // Declare an architecture-specific array of function pointers for size-specific
129 // wrappers.
130 #define CFL_SUBSAMPLE_FUNCTION_ARRAY(arch, sub, bd) \
131 static const cfl_subsample_##bd##_fn subfn_##sub[TX_SIZES_ALL] = { \
132 subsample_##bd##_##sub##_4x4_##arch, /* 4x4 */ \
133 subsample_##bd##_##sub##_8x8_##arch, /* 8x8 */ \
134 subsample_##bd##_##sub##_16x16_##arch, /* 16x16 */ \
135 subsample_##bd##_##sub##_32x32_##arch, /* 32x32 */ \
136 cfl_subsample_##bd##_null, /* 64x64 (invalid CFL size) */ \
137 subsample_##bd##_##sub##_4x8_##arch, /* 4x8 */ \
138 subsample_##bd##_##sub##_8x4_##arch, /* 8x4 */ \
139 subsample_##bd##_##sub##_8x16_##arch, /* 8x16 */ \
140 subsample_##bd##_##sub##_16x8_##arch, /* 16x8 */ \
141 subsample_##bd##_##sub##_16x32_##arch, /* 16x32 */ \
142 subsample_##bd##_##sub##_32x16_##arch, /* 32x16 */ \
143 cfl_subsample_##bd##_null, /* 32x64 (invalid CFL size) */ \
144 cfl_subsample_##bd##_null, /* 64x32 (invalid CFL size) */ \
145 subsample_##bd##_##sub##_4x16_##arch, /* 4x16 */ \
146 subsample_##bd##_##sub##_16x4_##arch, /* 16x4 */ \
147 subsample_##bd##_##sub##_8x32_##arch, /* 8x32 */ \
148 subsample_##bd##_##sub##_32x8_##arch, /* 32x8 */ \
149 cfl_subsample_##bd##_null, /* 16x64 (invalid CFL size) */ \
150 cfl_subsample_##bd##_null, /* 64x16 (invalid CFL size) */ \
151 };
152
153 // The RTCD script does not support passing in an array, so we wrap it in this
154 // function.
155 #define CFL_GET_SUBSAMPLE_FUNCTION(arch) \
156 CFL_SUBSAMPLE_FUNCTIONS(arch, 420, lbd) \
157 CFL_SUBSAMPLE_FUNCTIONS(arch, 422, lbd) \
158 CFL_SUBSAMPLE_FUNCTIONS(arch, 444, lbd) \
159 CFL_SUBSAMPLE_FUNCTIONS(arch, 420, hbd) \
160 CFL_SUBSAMPLE_FUNCTIONS(arch, 422, hbd) \
161 CFL_SUBSAMPLE_FUNCTIONS(arch, 444, hbd)
162
163 // Null function used for invalid tx_sizes
cfl_subtract_average_null(const uint16_t * src,int16_t * dst)164 static INLINE void cfl_subtract_average_null(const uint16_t *src,
165 int16_t *dst) {
166 (void)dst;
167 (void)src;
168 assert(0);
169 }
170
171 // Declare a size-specific wrapper for the size-generic function. The compiler
172 // will inline the size generic function in here, the advantage is that the size
173 // will be constant allowing for loop unrolling and other constant propagated
174 // goodness.
175 #define CFL_SUB_AVG_X(arch, width, height, round_offset, num_pel_log2) \
176 void subtract_average_##width##x##height##_##arch(const uint16_t *src, \
177 int16_t *dst) { \
178 subtract_average_##arch(src, dst, width, height, round_offset, \
179 num_pel_log2); \
180 }
181
182 // Declare size-specific wrappers for all valid CfL sizes.
183 #define CFL_SUB_AVG_FN(arch) \
184 CFL_SUB_AVG_X(arch, 4, 4, 8, 4) \
185 CFL_SUB_AVG_X(arch, 4, 8, 16, 5) \
186 CFL_SUB_AVG_X(arch, 4, 16, 32, 6) \
187 CFL_SUB_AVG_X(arch, 8, 4, 16, 5) \
188 CFL_SUB_AVG_X(arch, 8, 8, 32, 6) \
189 CFL_SUB_AVG_X(arch, 8, 16, 64, 7) \
190 CFL_SUB_AVG_X(arch, 8, 32, 128, 8) \
191 CFL_SUB_AVG_X(arch, 16, 4, 32, 6) \
192 CFL_SUB_AVG_X(arch, 16, 8, 64, 7) \
193 CFL_SUB_AVG_X(arch, 16, 16, 128, 8) \
194 CFL_SUB_AVG_X(arch, 16, 32, 256, 9) \
195 CFL_SUB_AVG_X(arch, 32, 8, 128, 8) \
196 CFL_SUB_AVG_X(arch, 32, 16, 256, 9) \
197 CFL_SUB_AVG_X(arch, 32, 32, 512, 10) \
198 cfl_subtract_average_fn get_subtract_average_fn_##arch(TX_SIZE tx_size) { \
199 static const cfl_subtract_average_fn sub_avg[TX_SIZES_ALL] = { \
200 subtract_average_4x4_##arch, /* 4x4 */ \
201 subtract_average_8x8_##arch, /* 8x8 */ \
202 subtract_average_16x16_##arch, /* 16x16 */ \
203 subtract_average_32x32_##arch, /* 32x32 */ \
204 cfl_subtract_average_null, /* 64x64 (invalid CFL size) */ \
205 subtract_average_4x8_##arch, /* 4x8 */ \
206 subtract_average_8x4_##arch, /* 8x4 */ \
207 subtract_average_8x16_##arch, /* 8x16 */ \
208 subtract_average_16x8_##arch, /* 16x8 */ \
209 subtract_average_16x32_##arch, /* 16x32 */ \
210 subtract_average_32x16_##arch, /* 32x16 */ \
211 cfl_subtract_average_null, /* 32x64 (invalid CFL size) */ \
212 cfl_subtract_average_null, /* 64x32 (invalid CFL size) */ \
213 subtract_average_4x16_##arch, /* 4x16 (invalid CFL size) */ \
214 subtract_average_16x4_##arch, /* 16x4 (invalid CFL size) */ \
215 subtract_average_8x32_##arch, /* 8x32 (invalid CFL size) */ \
216 subtract_average_32x8_##arch, /* 32x8 (invalid CFL size) */ \
217 cfl_subtract_average_null, /* 16x64 (invalid CFL size) */ \
218 cfl_subtract_average_null, /* 64x16 (invalid CFL size) */ \
219 }; \
220 /* Modulo TX_SIZES_ALL to ensure that an attacker won't be able to */ \
221 /* index the function pointer array out of bounds. */ \
222 return sub_avg[tx_size % TX_SIZES_ALL]; \
223 }
224
225 // For VSX SIMD optimization, the C versions of width == 4 subtract are
226 // faster than the VSX. As such, the VSX code calls the C versions.
227 void subtract_average_4x4_c(const uint16_t *src, int16_t *dst);
228 void subtract_average_4x8_c(const uint16_t *src, int16_t *dst);
229 void subtract_average_4x16_c(const uint16_t *src, int16_t *dst);
230
231 #define CFL_PREDICT_lbd(arch, width, height) \
232 void predict_lbd_##width##x##height##_##arch(const int16_t *pred_buf_q3, \
233 uint8_t *dst, int dst_stride, \
234 int alpha_q3) { \
235 cfl_predict_lbd_##arch(pred_buf_q3, dst, dst_stride, alpha_q3, width, \
236 height); \
237 }
238
239 #define CFL_PREDICT_hbd(arch, width, height) \
240 void predict_hbd_##width##x##height##_##arch(const int16_t *pred_buf_q3, \
241 uint16_t *dst, int dst_stride, \
242 int alpha_q3, int bd) { \
243 cfl_predict_hbd_##arch(pred_buf_q3, dst, dst_stride, alpha_q3, bd, width, \
244 height); \
245 }
246
247 // This wrapper exists because clang format does not like calling macros with
248 // lowercase letters.
249 #define CFL_PREDICT_X(arch, width, height, bd) \
250 CFL_PREDICT_##bd(arch, width, height)
251
252 // Null function used for invalid tx_sizes
253 void cfl_predict_lbd_null(const int16_t *pred_buf_q3, uint8_t *dst,
254 int dst_stride, int alpha_q3);
255
256 // Null function used for invalid tx_sizes
257 void cfl_predict_hbd_null(const int16_t *pred_buf_q3, uint16_t *dst,
258 int dst_stride, int alpha_q3, int bd);
259
260 #define CFL_PREDICT_FN(arch, bd) \
261 CFL_PREDICT_X(arch, 4, 4, bd) \
262 CFL_PREDICT_X(arch, 4, 8, bd) \
263 CFL_PREDICT_X(arch, 4, 16, bd) \
264 CFL_PREDICT_X(arch, 8, 4, bd) \
265 CFL_PREDICT_X(arch, 8, 8, bd) \
266 CFL_PREDICT_X(arch, 8, 16, bd) \
267 CFL_PREDICT_X(arch, 8, 32, bd) \
268 CFL_PREDICT_X(arch, 16, 4, bd) \
269 CFL_PREDICT_X(arch, 16, 8, bd) \
270 CFL_PREDICT_X(arch, 16, 16, bd) \
271 CFL_PREDICT_X(arch, 16, 32, bd) \
272 CFL_PREDICT_X(arch, 32, 8, bd) \
273 CFL_PREDICT_X(arch, 32, 16, bd) \
274 CFL_PREDICT_X(arch, 32, 32, bd) \
275 cfl_predict_##bd##_fn get_predict_##bd##_fn_##arch(TX_SIZE tx_size) { \
276 static const cfl_predict_##bd##_fn pred[TX_SIZES_ALL] = { \
277 predict_##bd##_4x4_##arch, /* 4x4 */ \
278 predict_##bd##_8x8_##arch, /* 8x8 */ \
279 predict_##bd##_16x16_##arch, /* 16x16 */ \
280 predict_##bd##_32x32_##arch, /* 32x32 */ \
281 cfl_predict_##bd##_null, /* 64x64 (invalid CFL size) */ \
282 predict_##bd##_4x8_##arch, /* 4x8 */ \
283 predict_##bd##_8x4_##arch, /* 8x4 */ \
284 predict_##bd##_8x16_##arch, /* 8x16 */ \
285 predict_##bd##_16x8_##arch, /* 16x8 */ \
286 predict_##bd##_16x32_##arch, /* 16x32 */ \
287 predict_##bd##_32x16_##arch, /* 32x16 */ \
288 cfl_predict_##bd##_null, /* 32x64 (invalid CFL size) */ \
289 cfl_predict_##bd##_null, /* 64x32 (invalid CFL size) */ \
290 predict_##bd##_4x16_##arch, /* 4x16 */ \
291 predict_##bd##_16x4_##arch, /* 16x4 */ \
292 predict_##bd##_8x32_##arch, /* 8x32 */ \
293 predict_##bd##_32x8_##arch, /* 32x8 */ \
294 cfl_predict_##bd##_null, /* 16x64 (invalid CFL size) */ \
295 cfl_predict_##bd##_null, /* 64x16 (invalid CFL size) */ \
296 }; \
297 /* Modulo TX_SIZES_ALL to ensure that an attacker won't be able to */ \
298 /* index the function pointer array out of bounds. */ \
299 return pred[tx_size % TX_SIZES_ALL]; \
300 }
301
302 #endif // AOM_AV1_COMMON_CFL_H_
303