1 /*
2 * Copyright(c) 2019 Intel Corporation
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 https://www.aomedia.org/license/software-license. 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 https://www.aomedia.org/license/patent-license.
10 */
11 
12 #ifndef EbCodingUnit_h
13 #define EbCodingUnit_h
14 
15 #include "EbMotionEstimationLcuResults.h"
16 #include "EbPictureBufferDesc.h"
17 #include "EbPredictionUnit.h"
18 #include "EbTransformUnit.h"
19 #include "EbBlockStructures.h"
20 #include "EbCabacContextModel.h"
21 #include "hash.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 /*
27     Requirements:
28     -Must have enough CodingUnits for every single CU pattern
29     -Easy to expand/insert CU
30     -Easy to collapse a CU
31     -Easy to replace CUs
32     -Statically Allocated
33     -Contains the leaf count
34     */
35 
36 // Macros for deblocking filter
37 #define MAX_SB_SIZE_IN_4X4BLK (BLOCK_SIZE_64 >> 2)
38 #define VERTICAL_EDGE_BS_ARRAY_SIZE (MAX_SB_SIZE_IN_4X4BLK * MAX_SB_SIZE_IN_4X4BLK)
39 #define HORIZONTAL_EDGE_BS_ARRAY_SIZE (MAX_SB_SIZE_IN_4X4BLK * MAX_SB_SIZE_IN_4X4BLK)
40 
41 #define MAX_NUMBER_OF_BS_EDGES_PER_TREEBLOCK 128
42 #define MAX_NUMBER_OF_LEAFS_PER_TREEBLOCK 64
43 #define MAX_NUMBER_OF_4x4_TUs_IN_8x8_LEAF 4
44 #define MAX_CU_COST (0xFFFFFFFFFFFFFFFFull >> 1)
45 #define MAX_MODE_COST \
46     (13616969489728 * 8) // RDCOST(6544618, 128 * 128 * 255 * 255, 128 * 128 * 255 * 255) * 8;
47 #define INVALID_FAST_CANDIDATE_INDEX ~0
48 #define MAX_OIS_CANDIDATES 61 //18//18
49 
50 static const uint32_t intra_hev_cmode_to_intra_av1_mode[35] = {
51     /*SMOOTH_PRED   */ SMOOTH_PRED, // EB_INTRA_PLANAR
52     /*DC_PRED       */ DC_PRED, // EB_INTRA_DC
53     /*D203_PRED     */ D203_PRED,
54     D203_PRED,
55     D203_PRED,
56     D203_PRED,
57     D203_PRED,
58     D203_PRED, // EB_INTRA_MODE_2 -> EB_INTRA_MODE_7
59     /*H_PRED        */ H_PRED,
60     H_PRED,
61     H_PRED,
62     H_PRED,
63     H_PRED, // EB_INTRA_MODE_8 -> EB_INTRA_HORIZONTAL -> EB_INTRA_MODE_12
64     /*D157_PRED     */ D157_PRED,
65     D157_PRED,
66     D157_PRED, // EB_INTRA_MODE_13 -> EB_INTRA_MODE_15
67     /*D135_PRED     */ D135_PRED,
68     D135_PRED,
69     D135_PRED,
70     D135_PRED,
71     D135_PRED, // EB_INTRA_MODE_16 -> EB_INTRA_MODE_20
72     /*D113_PRED     */ D113_PRED,
73     D113_PRED,
74     D113_PRED, // EB_INTRA_MODE_21 -> EB_INTRA_MODE_23
75     /*V_PRED        */ V_PRED,
76     V_PRED,
77     V_PRED,
78     V_PRED, // EB_INTRA_MODE_24 -> EB_INTRA_VERTICAL -> EB_INTRA_MODE_27
79     /*D67_PRED      */ D67_PRED,
80     D67_PRED,
81     D67_PRED, // EB_INTRA_MODE_28 -> EB_INTRA_MODE_30
82     /*D45_PRED      */ D45_PRED,
83     D45_PRED,
84     D45_PRED,
85     D45_PRED, // EB_INTRA_MODE_31 -> EB_INTRA_MODE_34
86 };
87 static const int8_t hevc_mode_to_angle_delta_map[35] = {
88     /*SMOOTH_PRED   */ 0, // EB_INTRA_PLANAR ->
89     /*DC_PRED       */ 0, // EB_INTRA_DC
90     /*D203_PRED     */ 3,
91     2,
92     1,
93     0,
94     -1,
95     -2, // EB_INTRA_MODE_2 -> EB_INTRA_MODE_7
96     /*H_PRED        */ 3,
97     2,
98     0,
99     -2,
100     -3, // EB_INTRA_MODE_8 -> EB_INTRA_HORIZONTAL -> EB_INTRA_MODE_12
101     /*D157_PRED     */ 2,
102     0,
103     -2, // EB_INTRA_MODE_13 -> EB_INTRA_MODE_15
104     /*D135_PRED     */ 3,
105     2,
106     0,
107     -2,
108     -3, // EB_INTRA_MODE_16 -> EB_INTRA_MODE_20
109     /*D113_PRED     */ 2,
110     0,
111     -2, // EB_INTRA_MODE_21 -> EB_INTRA_MODE_23
112     /*V_PRED        */ 3,
113     2,
114     0,
115     -2, // EB_INTRA_MODE_24 -> EB_INTRA_VERTICAL -> EB_INTRA_MODE_27
116     /*D67_PRED      */ 2,
117     0,
118     -2, // EB_INTRA_MODE_28 -> EB_INTRA_MODE_30
119     /*D45_PRED      */ 3,
120     2,
121     0,
122     -2, // EB_INTRA_MODE_31 -> EB_INTRA_MODE_34
123 };
124 static const uint32_t intra_luma_to_chroma[INTRA_MODES] = // EB_INTRA_PLANAR
125     {
126         UV_DC_PRED, // Average of above and left pixels
127         UV_V_PRED, // Vertical
128         UV_H_PRED, // Horizontal
129         UV_D45_PRED, // Directional 45  degree
130         UV_D135_PRED, // Directional 135 degree
131         UV_D113_PRED, // Directional 113 degree
132         UV_D157_PRED, // Directional 157 degree
133         UV_D203_PRED, // Directional 203 degree
134         UV_D67_PRED, // Directional 67  degree
135         UV_SMOOTH_PRED, // Combination of horizontal and vertical interpolation
136         UV_SMOOTH_V_PRED, // Vertical interpolation
137         UV_SMOOTH_H_PRED, // Horizontal interpolation
138         UV_PAETH_PRED, // Predict from the direction of smallest gradient
139 };
140 
141 static const TxType chroma_transform_type[14] = {
142     /*UV_DC_PRED,          */ DCT_DCT,
143     /*UV_V_PRED,           */ ADST_DCT,
144     /*UV_H_PRED,           */ DCT_ADST,
145     /*UV_D45_PRED,         */ DCT_DCT,
146     /*UV_D135_PRED,        */ ADST_ADST,
147     /*UV_D113_PRED,        */ ADST_DCT,
148     /*UV_D157_PRED,        */ DCT_ADST,
149     /*UV_D203_PRED,        */ DCT_ADST,
150     /*UV_D67_PRED,         */ ADST_DCT,
151     /*UV_SMOOTH_PRED,      */ ADST_ADST,
152     /*UV_SMOOTH_V_PRED,    */ ADST_DCT,
153     /*UV_SMOOTH_H_PRED,    */ DCT_ADST,
154     /*UV_PAETH_PRED,       */ ADST_ADST,
155     /*UV_CFL_PRED,          */ DCT_DCT,
156 };
157 static const uint8_t av1_is_directional_chroma[UV_INTRA_MODES] = {
158     /*UV_DC_PRED,        */ 0,
159     /*UV_V_PRED,         */ 0,
160     /*UV_H_PRED,         */ 1,
161     /*UV_D45_PRED,       */ 1,
162     /*UV_D135_PRED,      */ 1,
163     /*UV_D113_PRED,      */ 1,
164     /*UV_D157_PRED,      */ 1,
165     /*UV_D203_PRED,      */ 1,
166     /*UV_D67_PRED,       */ 1,
167     /*UV_SMOOTH_PRED,    */ 0,
168     /* UV_SMOOTH_PRED,   */ 0,
169     /* UV_SMOOTH_V_PRED, */ 0,
170     /* UV_SMOOTH_H_PRED, */ 0,
171     /* UV_PAETH_PRED,    */ 0,
172 };
173 static const uint8_t av1_is_directional[35] = {
174     0, // EB_INTRA_PLANAR
175     0, // EB_INTRA_DC
176     1, 1, 1, 1, 1, 1, 1, 1, // EB_INTRA_MODE_2 -> EB_INTRA_MODE_9
177     1, // EB_INTRA_HORIZONTAL
178     1, 1, 1, 1, 1, // EB_INTRA_MODE_11 -> EB_INTRA_MODE_15
179     1, 1, 1, 1, 1, // EB_INTRA_MODE_16 -> EB_INTRA_MODE_20
180     1, 1, 1, 1, 1, // EB_INTRA_MODE_21 -> EB_INTRA_MODE_25
181     1, // EB_INTRA_VERTICAL
182     1, 1, 1, 1, // EB_INTRA_MODE_27 -> EB_INTRA_MODE_30
183     1, 1, 1, 1, // EB_INTRA_MODE_31 -> EB_INTRA_MODE_34
184 };
185 
186 struct PictureControlSet;
187 
188 typedef struct {
189     IntMv   mfmv0;
190     uint8_t ref_frame_offset;
191 } TPL_MV_REF;
192 typedef struct {
193     IntMv            mv;
194     MvReferenceFrame ref_frame;
195 } MV_REF;
196 
197 typedef struct ModeInfo {
198     MbModeInfo mbmi;
199 } ModeInfo;
200 
201 typedef struct MacroBlockDPlane {
202     int          subsampling_x;
203     int          subsampling_y;
204     struct Buf2D dst;
205     struct Buf2D pre[2];
206     // block size in pixels
207     uint8_t width, height;
208 } MacroBlockDPlane;
209 
210 typedef struct MacroBlockPlane {
211     struct Buf2D src;
212     /*
213         DECLARE_ALIGNED(16, int16_t, src_diff[MAX_SB_SQUARE]);
214         TranLow *qcoeff;
215         TranLow *coeff;
216         uint16_t *eobs;
217         uint8_t *txb_entropy_ctx;
218 
219         // Quantizer setings
220         // These are used/accessed only in the quantization process
221         // RDO does not / must not depend on any of these values
222         // All values below share the coefficient scale/shift used in TX
223         const int16_t *quant_fp_qtx;
224         const int16_t *round_fp_qtx;
225         const int16_t *quant_qtx;
226         const int16_t *quant_shift_qtx;
227         const int16_t *zbin_qtx;
228         const int16_t *round_qtx;
229         const int16_t *dequant_qtx;
230 */
231 } MacroBlockPlane;
232 
233 typedef struct macroblockd_plane {
234     int subsampling_x;
235     int subsampling_y;
236 } MACROBLOCKD_PLANE;
237 
238 typedef enum InterPredMode {
239     UNIFORM_PRED,
240     WARP_PRED,
241     MASK_PRED,
242 } InterPredMode;
243 
244 typedef struct InterPredParams {
245     InterPredMode        mode;
246     EbWarpedMotionParams warp_params;
247     ConvolveParams       conv_params;
248     //const InterpFilterParams *interp_filter_params[2];
249     InterpFilterParams         interp_filter_params[2];
250     int                        block_width;
251     int                        block_height;
252     int                        pix_row;
253     int                        pix_col;
254     struct Buf2D               ref_frame_buf;
255     int                        subsampling_x;
256     int                        subsampling_y;
257     const struct ScaleFactors *scale_factors;
258     int                        bit_depth;
259     int                        use_hbd_buf;
260     int                        is_intrabc;
261 } InterPredParams;
262 typedef struct MacroBlockD {
263     // block dimension in the unit of mode_info.
264     uint8_t     n8_w, n8_h;
265     uint8_t     n4_w, n4_h; // for warped motion
266     uint8_t     ref_mv_count[MODE_CTX_REF_FRAMES];
267     CandidateMv final_ref_mv_stack[MAX_REF_MV_STACK_SIZE];
268     uint8_t     is_sec_rect;
269     int8_t      up_available;
270     int8_t      left_available;
271     int8_t      chroma_up_available;
272     int8_t      chroma_left_available;
273     TileInfo    tile;
274     int32_t     mi_stride;
275     ModeInfo ** mi;
276 
277     /* Distance of MB away from frame edges in subpixels (1/8th pixel)  */
278     int32_t                  mb_to_left_edge;
279     int32_t                  mb_to_right_edge;
280     int32_t                  mb_to_top_edge;
281     int32_t                  mb_to_bottom_edge;
282     int                      mi_row; // Row position in mi units
283     int                      mi_col; // Column position in mi units
284     uint8_t                  neighbors_ref_counts[TOTAL_REFS_PER_FRAME];
285     MbModeInfo *             above_mbmi;
286     MbModeInfo *             left_mbmi;
287     MbModeInfo *             chroma_above_mbmi;
288     MbModeInfo *             chroma_left_mbmi;
289     FRAME_CONTEXT *          tile_ctx;
290     TXFM_CONTEXT *           above_txfm_context;
291     TXFM_CONTEXT *           left_txfm_context;
292     struct macroblockd_plane plane[MAX_MB_PLANE];
293     BlockSize                sb_type;
294 } MacroBlockD;
295 
296 typedef struct Macroblock {
297     int32_t rdmult;
298     int32_t switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
299     int32_t wiener_restore_cost[2];
300     int32_t sgrproj_restore_cost[2];
301 } Macroblock;
302 
303 typedef struct IntraBcContext {
304     int32_t                 rdmult;
305     struct MacroBlockDPlane xdplane[MAX_MB_PLANE];
306     struct MacroBlockPlane  plane[MAX_MB_PLANE];
307     MvLimits                mv_limits;
308     // The equivalend SAD error of one (whole) bit at the current quantizer
309     // for large blocks.
310     int sadperbit16;
311     // The equivalent error at the current rdmult of one whole bit (not one
312     // bitcost unit).
313     int errorperbit;
314     // Store the best motion vector during motion search
315     IntMv best_mv;
316     // Store the second best motion vector during full-pixel motion search
317     IntMv        second_best_mv;
318     MacroBlockD *xd;
319     int *        nmv_vec_cost;
320     int **       mv_cost_stack;
321     // buffer for hash value calculation of a block
322     // used only in svt_av1_get_block_hash_value()
323     // [first hash/second hash]
324     // [two buffers used ping-pong]
325     uint32_t *     hash_value_buffer[2][2];
326     uint8_t        is_exhaustive_allowed;
327     CRC_CALCULATOR crc_calculator1;
328     CRC_CALCULATOR crc_calculator2;
329 } IntraBcContext;
330 
331 typedef struct BlkStruct {
332     TransformUnit          txb_array[TRANSFORM_UNIT_MAX_COUNT]; // ec
333     PredictionUnit         prediction_unit_array[MAX_NUM_OF_PU_PER_CU]; // ec
334     PaletteInfo            palette_info; // ec
335     IntMv                  predmv[2]; // ec
336     MacroBlockD *          av1xd;
337     InterInterCompoundData interinter_comp; // ec
338     uint32_t               interp_filters; // ec
339     int32_t                interintra_wedge_index; // ec
340     // uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
341     int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES]; // ec
342     uint16_t
343         mds_idx; //equivalent of leaf_index in the nscu context. we will keep both for now and use the right one on a case by case basis.
344     // txb
345     uint8_t tx_depth; // ec
346     uint8_t compound_idx; // ec
347     uint8_t comp_group_idx; // ec
348 
349     unsigned skip_flag_context : 2; // to do
350     unsigned prediction_mode_flag : 2; // ec
351     unsigned block_has_coeff : 1; // ec
352     unsigned split_flag_context : 2; // to do
353 
354     uint8_t qindex; // ec
355     uint8_t split_flag;
356     uint8_t skip_flag; // ec
357     uint8_t mdc_split_flag; // ?
358 #if NO_ENCDEC
359     EbPictureBufferDesc *quant_tmp;
360     EbPictureBufferDesc *coeff_tmp;
361     EbPictureBufferDesc *recon_tmp;
362     uint32_t             cand_buff_index;
363 #endif
364     uint8_t drl_index; // ec
365     int8_t  drl_ctx[2]; // Store the drl ctx in coding loop to avoid storing
366         // final_ref_mv_stack and ref_mv_count for EC
367     int8_t drl_ctx_near[2]; // Store the drl ctx in coding loop to avoid storing
368         // final_ref_mv_stack and ref_mv_count for EC
369     PredictionMode pred_mode; // ec
370 
371     uint8_t  reference_mode_context;
372     uint8_t  compoud_reference_type_context;
373     uint32_t is_inter_ctx;
374 
375     uint8_t       segment_id; // ec
376     uint8_t       seg_id_predicted; // valid only when temporal_update is enabled
377     PartitionType part;
378     InterIntraMode interintra_mode; // ec
379     uint8_t        is_interintra_used; // ec
380     uint8_t        use_wedge_interintra; // ec
381     uint8_t  filter_intra_mode; // ec
382     uint8_t  use_intrabc;
383     uint64_t total_rate;
384 } BlkStruct;
385 
386 typedef struct TplStats {
387     int64_t  srcrf_dist;
388     int64_t  recrf_dist;
389     int64_t  srcrf_rate;
390     int64_t  recrf_rate;
391     int64_t  mc_dep_rate;
392     int64_t  mc_dep_dist;
393     MV       mv;
394     uint64_t ref_frame_poc;
395 } TplStats;
396 typedef struct SuperBlock {
397     EbDctor                   dctor;
398     struct PictureControlSet *pcs_ptr;
399 
400     BlkStruct *final_blk_arr;
401     //for memory free only
402     MacroBlockD *  av1xd;
403     PartitionType *cu_partition_array;
404     unsigned       picture_left_edge_flag : 1;
405     unsigned       picture_top_edge_flag : 1;
406     unsigned       picture_right_edge_flag : 1;
407     unsigned       index : 12;
408     unsigned       origin_x : 12;
409     unsigned       origin_y : 12;
410     uint8_t        qindex;
411     uint32_t       total_bits;
412 
413     // Quantized Coefficients
414     TileInfo       tile_info;
415 } SuperBlock;
416 
417 extern EbErrorType largest_coding_unit_ctor(SuperBlock *larget_coding_unit_ptr, uint8_t sb_sz,
418                                             uint16_t sb_origin_x, uint16_t sb_origin_y,
419                                             uint16_t                  sb_index,
420                                             uint8_t enc_mode,
421                                             struct PictureControlSet *picture_control_set);
422 
423 #ifdef __cplusplus
424 }
425 #endif
426 #endif // EbCodingUnit_h
427