1 /*
2 * Copyright(c) 2019 Intel Corporation
3 * Copyright (c) 2019, Alliance for Open Media. All rights reserved
4 *
5 * This source code is subject to the terms of the BSD 2 Clause License and
6 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7 * was not distributed with this source code in the LICENSE file, you can
8 * obtain it at https://www.aomedia.org/license/software-license. If the Alliance for Open
9 * Media Patent License 1.0 was not distributed with this source code in the
10 * PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
11 */
12 
13 #ifndef EbIntraPrediction_h
14 #define EbIntraPrediction_h
15 
16 #include "EbSvtAv1.h"
17 #include "EbObject.h"
18 #include "EbBlockStructures.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 #define MAX_PU_SIZE 64
24 
25 struct ModeDecisionContext;
26 
27 typedef void (*IntraPredFnC)(uint8_t *dst, ptrdiff_t stride, int32_t w, int32_t h,
28                              const uint8_t *above, const uint8_t *left);
29 typedef void (*IntraHighBdPredFnC)(uint16_t *dst, ptrdiff_t stride, int32_t w, int32_t h,
30                                    const uint16_t *above, const uint16_t *left, int32_t bd);
31 
32 typedef void (*IntraPredFn)(uint8_t *dst, ptrdiff_t stride, const uint8_t *above,
33                             const uint8_t *left);
34 
35 typedef void (*IntraHighPredFn)(uint16_t *dst, ptrdiff_t stride, const uint16_t *above,
36                                 const uint16_t *left, int32_t bd);
37 
38 typedef struct IntraReferenceSamples {
39     EbDctor  dctor;
40     uint8_t *cb_intra_reference_array;
41     uint8_t *cr_intra_reference_array;
42     uint8_t *y_intra_filtered_reference_array;
43 
44     uint8_t *y_intra_reference_array_reverse;
45     uint8_t *y_intra_filtered_reference_array_reverse;
46     uint8_t *cb_intra_reference_array_reverse;
47     uint8_t *cr_intra_reference_array_reverse;
48 
49     // Scratch buffers used in the interpolaiton process
50     uint8_t reference_above_line_y[(MAX_PU_SIZE << 2) + 1];
51     uint8_t reference_left_line_y[(MAX_PU_SIZE << 2) + 1];
52     EbBool  above_ready_flag_y;
53     EbBool  left_ready_flag_y;
54 
55     uint8_t reference_above_line_cb[(MAX_PU_SIZE << 2) + 2];
56     uint8_t reference_left_line_cb[(MAX_PU_SIZE << 2) + 2];
57     EbBool  above_ready_flag_cb;
58     EbBool  left_ready_flag_cb;
59 
60     uint8_t reference_above_line_cr[(MAX_PU_SIZE << 2) + 2];
61     uint8_t reference_left_line_cr[(MAX_PU_SIZE << 2) + 2];
62     EbBool  above_ready_flag_cr;
63     EbBool  left_ready_flag_cr;
64 } IntraReferenceSamples;
65 
66 typedef struct IntraReference16bitSamples {
67     EbDctor   dctor;
68     uint16_t *cb_intra_reference_array;
69     uint16_t *cr_intra_reference_array;
70     uint16_t *y_intra_filtered_reference_array;
71 
72     uint16_t *y_intra_reference_array_reverse;
73     uint16_t *y_intra_filtered_reference_array_reverse;
74     uint16_t *cb_intra_reference_array_reverse;
75     uint16_t *cr_intra_reference_array_reverse;
76 
77     // Scratch buffers used in the interpolaiton process
78     uint16_t reference_above_line_y[(MAX_PU_SIZE << 2) + 1];
79     uint16_t reference_left_line_y[(MAX_PU_SIZE << 2) + 1];
80     EbBool   above_ready_flag_y;
81     EbBool   left_ready_flag_y;
82 
83     uint16_t reference_above_line_cb[(MAX_PU_SIZE << 2) + 2];
84     uint16_t reference_left_line_cb[(MAX_PU_SIZE << 2) + 2];
85     EbBool   above_ready_flag_cb;
86     EbBool   left_ready_flag_cb;
87 
88     uint16_t reference_above_line_cr[(MAX_PU_SIZE << 2) + 2];
89     uint16_t reference_left_line_cr[(MAX_PU_SIZE << 2) + 2];
90     EbBool   above_ready_flag_cr;
91     EbBool   left_ready_flag_cr;
92 } IntraReference16bitSamples;
93 
94 #define TOTAL_LUMA_MODES 35
95 #define TOTAL_CHROMA_MODES 5
96 #define TOTAL_INTRA_GROUPS 5
97 #define INTRA_PLANAR_MODE 0
98 #define INTRA_DC_MODE 1
99 #define INTRA_HORIZONTAL_MODE 10
100 #define INTRA_VERTICAL_MODE 26
101 #define STRONG_INTRA_SMOOTHING_BLOCKSIZE 32
102 #define SMOOTHING_THRESHOLD 8
103 #define SMOOTHING_THRESHOLD_10BIT 32
104 
105 /////####.... For recursive intra prediction.....#####///
106 
107 #define FILTER_INTRA_SCALE_BITS 4
108 extern const int8_t eb_av1_filter_intra_taps[FILTER_INTRA_MODES][8][8];
109 
110 /////####.... To make functions common between EbIntraPrediction.c &
111 void *svt_aom_memset16(void *dest, int32_t val, size_t length);
112 
113 int32_t use_intra_edge_upsample(int32_t bs0, int32_t bs1, int32_t delta, int32_t type);
114 
115 BlockSize scale_chroma_bsize(BlockSize bsize, int32_t subsampling_x, int32_t subsampling_y);
116 
117 int32_t intra_edge_filter_strength(int32_t bs0, int32_t bs1, int32_t delta, int32_t type);
118 
119 enum {
120     NEED_LEFT       = 1 << 1,
121     NEED_ABOVE      = 1 << 2,
122     NEED_ABOVERIGHT = 1 << 3,
123     NEED_ABOVELEFT  = 1 << 4,
124     NEED_BOTTOMLEFT = 1 << 5,
125 };
126 
127 static const uint32_t mode_to_angle_map[] = {
128     0,
129     90,
130     180,
131     45,
132     135,
133     113,
134     157,
135     203,
136     67,
137     0,
138     0,
139     0,
140     0,
141 };
142 
143 int is_smooth(const BlockModeInfo *mbmi, int plane);
144 
145 extern const uint8_t extend_modes[INTRA_MODES];
146 
147 /* TODO: Need to harmonize with fun from EbAdaptiveMotionVectorPrediction.c */
148 int32_t intra_has_top_right(BlockSize sb_size, BlockSize bsize, int32_t mi_row, int32_t mi_col,
149                             int32_t top_available, int32_t right_available, PartitionType partition,
150                             TxSize txsz, int32_t row_off, int32_t col_off, int32_t ss_x,
151                             int32_t ss_y);
152 
153 extern int32_t intra_has_bottom_left(BlockSize sb_size, BlockSize bsize, int32_t mi_row,
154                                      int32_t mi_col, int32_t bottom_available,
155                                      int32_t left_available, PartitionType partition, TxSize txsz,
156                                      int32_t row_off, int32_t col_off, int32_t ss_x, int32_t ss_y);
157 
158 extern IntraPredFn eb_pred[INTRA_MODES][TX_SIZES_ALL];
159 extern IntraPredFn dc_pred[2][2][TX_SIZES_ALL];
160 
161 extern IntraHighPredFn pred_high[INTRA_MODES][TX_SIZES_ALL];
162 extern IntraHighPredFn dc_pred_high[2][2][TX_SIZES_ALL];
163 
164 void dr_predictor(uint8_t *dst, ptrdiff_t stride, TxSize tx_size, const uint8_t *above,
165                   const uint8_t *left, int32_t upsample_above, int32_t upsample_left,
166                   int32_t angle);
167 
168 void filter_intra_edge_corner(uint8_t *p_above, uint8_t *p_left);
169 
170 void highbd_dr_predictor(uint16_t *dst, ptrdiff_t stride, TxSize tx_size, const uint16_t *above,
171                          const uint16_t *left, int32_t upsample_above, int32_t upsample_left,
172                          int32_t angle, int32_t bd);
173 
174 void filter_intra_edge_corner_high(uint16_t *p_above, uint16_t *p_left);
175 
176 void highbd_filter_intra_predictor(uint16_t *dst, ptrdiff_t stride, TxSize tx_size,
177                                    const uint16_t *above, const uint16_t *left, int mode, int bd);
178 
179 typedef void (*EB_INTRA_NOANG_TYPE)(const uint32_t size, uint8_t *ref_samples,
180                                     uint8_t *      prediction_ptr,
181                                     const uint32_t prediction_buffer_stride, const EbBool skip);
182 typedef void (*EB_INTRA_DC_AV1_TYPE)(
183     const uint32_t size, //input parameter, denotes the size of the current PU
184     uint8_t *      ref_samples, //input parameter, pointer to the reference samples
185     uint8_t *      dst, //output parameter, pointer to the prediction
186     const uint32_t
187         prediction_buffer_stride, //input parameter, denotes the stride for the prediction ptr
188     const EbBool skip); //skip half rows
189 typedef void (*EB_INTRA_NOANG_16bit_TYPE)(const uint32_t size, uint16_t *ref_samples,
190                                           uint16_t *     prediction_ptr,
191                                           const uint32_t prediction_buffer_stride,
192                                           const EbBool   skip);
193 typedef void (*EB_INTRA_ANG_Z1_Z2_Z3_16bit_TYPE)(const uint32_t size, uint16_t *ref_samples,
194                                                  uint16_t *     dst,
195                                                  const uint32_t prediction_buffer_stride,
196                                                  const EbBool skip, uint16_t dx, uint16_t dy,
197                                                  uint16_t bd);
198 typedef void (*EB_INTRA_ANG_TYPE)(uint32_t size, uint8_t *ref_samp_main, uint8_t *prediction_ptr,
199                                   uint32_t prediction_buffer_stride, const EbBool skip,
200                                   int32_t intra_pred_angle);
201 typedef void (*EB_INTRA_ANG_16BIT_TYPE)(
202     uint32_t  size, //input parameter, denotes the size of the current PU
203     uint16_t *ref_samp_main, //input parameter, pointer to the reference samples
204     uint16_t *prediction_ptr, //output parameter, pointer to the prediction
205     uint32_t  prediction_buffer_stride, //input parameter, denotes the stride for the prediction ptr
206     const EbBool skip, int32_t intra_pred_angle);
207 
208 extern void svt_cfl_luma_subsampling_420_lbd_c(const uint8_t *input, // AMIR-> Changed to 8 bit
209                                                int32_t input_stride, int16_t *output_q3,
210                                                int32_t width, int32_t height);
211 extern void svt_cfl_luma_subsampling_420_hbd_c(const uint16_t *input, int32_t input_stride,
212                                                int16_t *output_q3, int32_t width, int32_t height);
213 extern void svt_subtract_average_c(int16_t *pred_buf_q3, int32_t width, int32_t height,
214                                    int32_t round_offset, int32_t num_pel_log2);
215 
216 //CFL_PREDICT_FN(c, lbd)
217 
218 void svt_cfl_predict_lbd_c(const int16_t *pred_buf_q3,
219                            uint8_t *      pred, // AMIR ADDED
220                            int32_t        pred_stride,
221                            uint8_t *      dst, // AMIR changed to 8 bit
222                            int32_t dst_stride, int32_t alpha_q3, int32_t bit_depth, int32_t width,
223                            int32_t height);
224 
225 void svt_cfl_predict_hbd_c(const int16_t *pred_buf_q3,
226                            uint16_t *     pred, // AMIR ADDED
227                            int32_t        pred_stride,
228                            uint16_t *     dst, // AMIR changed to 8 bit
229                            int32_t dst_stride, int32_t alpha_q3, int32_t bit_depth, int32_t width,
230                            int32_t height);
231 
cfl_idx_to_alpha(int32_t alpha_idx,int32_t joint_sign,CflPredType pred_type)232 static INLINE int32_t cfl_idx_to_alpha(int32_t alpha_idx, int32_t joint_sign,
233                                        CflPredType pred_type) {
234     const int32_t alpha_sign = (pred_type == CFL_PRED_U) ? CFL_SIGN_U(joint_sign)
235                                                          : CFL_SIGN_V(joint_sign);
236     if (alpha_sign == CFL_SIGN_ZERO)
237         return 0;
238     const int32_t abs_alpha_q3 = (pred_type == CFL_PRED_U) ? CFL_IDX_U(alpha_idx)
239                                                            : CFL_IDX_V(alpha_idx);
240     return (alpha_sign == CFL_SIGN_POS) ? abs_alpha_q3 + 1 : -abs_alpha_q3 - 1;
241 }
242 
243 extern void        filter_intra_edge(OisMbResults *ois_mb_results_ptr, uint8_t mode,
244                                      uint16_t max_frame_width, uint16_t max_frame_height, int32_t p_angle,
245                                      int32_t cu_origin_x, int32_t cu_origin_y, uint8_t *above_row,
246                                      uint8_t *left_col);
247 extern EbErrorType intra_prediction_open_loop_mb(int32_t p_angle, uint8_t ois_intra_mode,
248                                                  uint32_t srcOriginX, uint32_t srcOriginY,
249                                                  TxSize tx_size, uint8_t *above_row,
250                                                  uint8_t *left_col, uint8_t *dst,
251                                                  uint32_t dst_stride);
252 /* Function pointers return by CfL functions */
253 typedef void (*CflSubtractAverageFn)(int16_t *dst);
254 
255 CflSubtractAverageFn svt_get_subtract_average_fn_c(TxSize tx_size);
256 #define get_subtract_average_fn svt_get_subtract_average_fn_c
257 
258 // Declare a size-specific wrapper for the size-generic function. The compiler
259 // will inline the size generic function in here, the advantage is that the size
260 // will be constant allowing for loop unrolling and other constant propagated
261 // goodness.
262 #define CFL_SUB_AVG_X(arch, width, height, round_offset, num_pel_log2)               \
263     void svt_subtract_average_##width##x##height##_##arch(int16_t *buf) {            \
264         svt_subtract_average_##arch(buf, width, height, round_offset, num_pel_log2); \
265     }
266 
267 // Declare size-specific wrappers for all valid CfL sizes.
268 #define CFL_SUB_AVG_FN(arch)                                                  \
269     CFL_SUB_AVG_X(arch, 4, 4, 8, 4)                                           \
270     CFL_SUB_AVG_X(arch, 4, 8, 16, 5)                                          \
271     CFL_SUB_AVG_X(arch, 4, 16, 32, 6)                                         \
272     CFL_SUB_AVG_X(arch, 8, 4, 16, 5)                                          \
273     CFL_SUB_AVG_X(arch, 8, 8, 32, 6)                                          \
274     CFL_SUB_AVG_X(arch, 8, 16, 64, 7)                                         \
275     CFL_SUB_AVG_X(arch, 8, 32, 128, 8)                                        \
276     CFL_SUB_AVG_X(arch, 16, 4, 32, 6)                                         \
277     CFL_SUB_AVG_X(arch, 16, 8, 64, 7)                                         \
278     CFL_SUB_AVG_X(arch, 16, 16, 128, 8)                                       \
279     CFL_SUB_AVG_X(arch, 16, 32, 256, 9)                                       \
280     CFL_SUB_AVG_X(arch, 32, 8, 128, 8)                                        \
281     CFL_SUB_AVG_X(arch, 32, 16, 256, 9)                                       \
282     CFL_SUB_AVG_X(arch, 32, 32, 512, 10)                                      \
283     CflSubtractAverageFn svt_get_subtract_average_fn_##arch(TxSize tx_size) { \
284         const CflSubtractAverageFn sub_avg[TX_SIZES_ALL] = {                  \
285             svt_subtract_average_4x4_##arch, /* 4x4 */                        \
286             svt_subtract_average_8x8_##arch, /* 8x8 */                        \
287             svt_subtract_average_16x16_##arch, /* 16x16 */                    \
288             svt_subtract_average_32x32_##arch, /* 32x32 */                    \
289             NULL, /* 64x64 (invalid CFL size) */                              \
290             svt_subtract_average_4x8_##arch, /* 4x8 */                        \
291             svt_subtract_average_8x4_##arch, /* 8x4 */                        \
292             svt_subtract_average_8x16_##arch, /* 8x16 */                      \
293             svt_subtract_average_16x8_##arch, /* 16x8 */                      \
294             svt_subtract_average_16x32_##arch, /* 16x32 */                    \
295             svt_subtract_average_32x16_##arch, /* 32x16 */                    \
296             NULL, /* 32x64 (invalid CFL size) */                              \
297             NULL, /* 64x32 (invalid CFL size) */                              \
298             svt_subtract_average_4x16_##arch, /* 4x16 (invalid CFL size) */   \
299             svt_subtract_average_16x4_##arch, /* 16x4 (invalid CFL size) */   \
300             svt_subtract_average_8x32_##arch, /* 8x32 (invalid CFL size) */   \
301             svt_subtract_average_32x8_##arch, /* 32x8 (invalid CFL size) */   \
302             NULL, /* 16x64 (invalid CFL size) */                              \
303             NULL, /* 64x16 (invalid CFL size) */                              \
304         };                                                                    \
305         /* Modulo TX_SIZES_ALL to ensure that an attacker won't be able to */ \
306         /* index the function pointer array out of bounds. */                 \
307         return sub_avg[tx_size % TX_SIZES_ALL];                               \
308     }
309 
av1_is_directional_mode(PredictionMode mode)310 static INLINE int32_t av1_is_directional_mode(PredictionMode mode) {
311     return mode >= V_PRED && mode <= D67_PRED;
312 }
313 
get_palette_bsize_ctx(BlockSize bsize)314 static INLINE int get_palette_bsize_ctx(BlockSize bsize) {
315     return num_pels_log2_lookup[bsize] - num_pels_log2_lookup[BLOCK_8X8];
316 }
317 
av1_use_angle_delta(BlockSize bsize,uint8_t enable_angle_delta)318 static INLINE EbBool av1_use_angle_delta(BlockSize bsize, uint8_t enable_angle_delta) {
319     return (enable_angle_delta ? bsize >= BLOCK_8X8 : (EbBool)enable_angle_delta);
320 }
321 
322 #ifdef __cplusplus
323 }
324 #endif
325 #endif // EbIntraPrediction_h
326