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 EbModeDecision_h
13 #define EbModeDecision_h
14 
15 #include "EbDefinitions.h"
16 #include "EbUtility.h"
17 #include "EbPictureControlSet.h"
18 #include "EbCodingUnit.h"
19 #include "EbPredictionUnit.h"
20 #include "EbSyntaxElements.h"
21 #include "EbPictureBufferDesc.h"
22 #include "EbAdaptiveMotionVectorPrediction.h"
23 #include "EbPictureOperators.h"
24 #include "EbNeighborArrays.h"
25 #include "EbObject.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 #define ENABLE_AMVP_MV_FOR_RC_PU 0
31 #define MAX_MB_PLANE 3
32 #define MAX_MPM_CANDIDATES 3
33 #define MERGE_PENALTY 10
34 
35 // Create incomplete struct definition for the following function pointer typedefs
36 struct ModeDecisionCandidateBuffer;
37 struct ModeDecisionContext;
38 
39 /**************************************
40     * Mode Decision Candidate
41     **************************************/
42 typedef struct ModeDecisionCandidate {
43     uint8_t intra_luma_mode;   // HEVC mode, use pred_mode for AV1
44     int16_t motion_vector_xl0;
45     int16_t motion_vector_yl0;
46     int16_t motion_vector_xl1;
47     int16_t motion_vector_yl1;
48     uint8_t      skip_flag;
49     EbBool       skip_mode_allowed;
50     uint16_t     count_non_zero_coeffs;
51     uint8_t      type;
52     PaletteInfo *palette_info;
53     uint64_t                 fast_luma_rate;
54     uint64_t                 fast_chroma_rate;
55     uint64_t                 total_rate;
56     uint32_t                 luma_fast_distortion;
57     uint64_t                 full_distortion;
58     EbPtr                    prediction_context_ptr;
59     EbPredDirection          prediction_direction
60         [MAX_NUM_OF_PU_PER_CU]; // 2 bits // Hsan: does not seem to be used why not removed ?
61 
62     int16_t motion_vector_pred_x
63         [MAX_NUM_OF_REF_PIC_LIST]; // 16 bits // Hsan: does not seem to be used why not removed ?
64     int16_t motion_vector_pred_y
65         [MAX_NUM_OF_REF_PIC_LIST]; // 16 bits // Hsan: does not seem to be used why not removed ?
66     uint8_t  block_has_coeff; // ?? bit - determine empirically
67     uint8_t  u_has_coeff; // ?? bit
68     uint8_t  v_has_coeff; // ?? bit
69     uint16_t y_has_coeff; // Issue, should be less than 32
70 
71     PredictionMode pred_mode; // AV1 mode, no need to convert
72     uint8_t        drl_index;
73     uint8_t        use_intrabc;
74     // Intra Mode
75     int32_t  angle_delta[PLANE_TYPES];
76     EbBool   is_directional_mode_flag;
77     EbBool   is_directional_chroma_mode_flag;
78     uint8_t  filter_intra_mode;
79     uint32_t intra_chroma_mode; // AV1 mode, no need to convert
80 
81     // Index of the alpha Cb and alpha Cr combination
82     int32_t cfl_alpha_idx;
83     // Joint sign of alpha Cb and alpha Cr
84     int32_t cfl_alpha_signs;
85 
86     // Inter Mode
87     EbBool                 is_compound;
88     uint8_t                ref_frame_type;
89     TxType                 transform_type[MAX_TXB_COUNT];
90     TxType                 transform_type_uv;
91     MacroblockPlane        candidate_plane[MAX_MB_PLANE];
92     uint16_t               eob[MAX_MB_PLANE][MAX_TXB_COUNT];
93     int32_t                quantized_dc[3][MAX_TXB_COUNT];
94     uint32_t               interp_filters;
95     MotionMode             motion_mode;
96     uint16_t               num_proj_ref;
97     EbBool                 local_warp_valid;
98     EbWarpedMotionParams   wm_params_l0;
99     EbWarpedMotionParams   wm_params_l1;
100     uint8_t                tx_depth;
101     InterInterCompoundData interinter_comp;
102     uint8_t                compound_idx;
103     uint8_t                comp_group_idx;
104     CandClass              cand_class;
105     InterIntraMode         interintra_mode;
106     uint8_t                is_interintra_used;
107     uint8_t                use_wedge_interintra;
108     int32_t                interintra_wedge_index; //inter_intra wedge index
109 } ModeDecisionCandidate;
110 
111 /**************************************
112     * Function Ptrs Definitions
113     **************************************/
114 typedef EbErrorType (*EbPredictionFunc)(uint8_t                             hbd_mode_decision,
115                                         struct ModeDecisionContext *        context_ptr,
116                                         PictureControlSet *                 pcs_ptr,
117                                         struct ModeDecisionCandidateBuffer *candidate_buffer_ptr);
118 typedef uint64_t (*EbFastCostFunc)(
119                                    struct ModeDecisionContext *context_ptr,
120                                    BlkStruct *                   blk_ptr,
121                                    struct ModeDecisionCandidate *candidate_buffer, uint32_t qp,
122                                    uint64_t luma_distortion, uint64_t chroma_distortion,
123                                    uint64_t lambda, PictureControlSet *pcs_ptr,
124                                    CandidateMv *ref_mv_stack, const BlockGeom *blk_geom,
125                                    uint32_t miRow, uint32_t miCol, uint8_t enable_inter_intra,
126                                    uint32_t left_neighbor_mode,
127                                    uint32_t top_neighbor_mode);
128 
129 typedef EbErrorType (*EB_FULL_COST_FUNC)(
130     SuperBlock *sb_ptr, BlkStruct *blk_ptr, uint32_t cu_size, uint32_t cu_size_log2,
131     struct ModeDecisionCandidateBuffer *candidate_buffer_ptr, uint32_t qp, uint64_t *y_distortion,
132     uint64_t *cb_distortion, uint64_t *cr_distortion, uint64_t lambda, uint64_t lambda_chroma,
133     uint64_t *y_coeff_bits, uint64_t *cb_coeff_bits, uint64_t *cr_coeff_bits,
134     uint32_t transform_size, uint32_t transform_chroma_size, PictureControlSet *pcs_ptr);
135 typedef EbErrorType (*EbAv1FullCostFunc)(
136     PictureControlSet *pcs_ptr, struct ModeDecisionContext *context_ptr,
137     struct ModeDecisionCandidateBuffer *candidate_buffer_ptr, BlkStruct *blk_ptr,
138     uint64_t *y_distortion, uint64_t *cb_distortion, uint64_t *cr_distortion, uint64_t lambda,
139     uint64_t *y_coeff_bits, uint64_t *cb_coeff_bits, uint64_t *cr_coeff_bits, BlockSize bsize);
140 
141 typedef EbErrorType (*EB_FULL_LUMA_COST_FUNC)(
142     BlkStruct *blk_ptr, uint32_t cu_size, uint32_t cu_size_log2,
143     struct ModeDecisionCandidateBuffer *candidate_buffer_ptr, uint64_t *y_distortion,
144     uint64_t lambda, uint64_t *y_coeff_bits, uint32_t transform_size);
145 /**************************************
146     * Mode Decision Candidate Buffer
147     **************************************/
148 typedef struct IntraChromacandidate_buffer {
149     uint32_t             mode;
150     uint64_t             cost;
151     uint64_t             distortion;
152     EbPictureBufferDesc *prediction_ptr;
153     EbPictureBufferDesc *residual_ptr;
154 } IntraChromacandidate_buffer;
155 
156 /**************************************
157     * Mode Decision Candidate Buffer
158     **************************************/
159 typedef struct ModeDecisionCandidateBuffer {
160     EbDctor dctor;
161     // Candidate Ptr
162     ModeDecisionCandidate *candidate_ptr;
163 
164     // Video Buffers
165     EbPictureBufferDesc *prediction_ptr;
166     EbPictureBufferDesc *recon_coeff_ptr;
167     EbPictureBufferDesc *residual_ptr;
168 
169     // *Note - We should be able to combine the recon_coeff_ptr & recon_ptr pictures (they aren't needed at the same time)
170     EbPictureBufferDesc *recon_ptr;
171 
172     // Costs
173     uint64_t *fast_cost_ptr;
174     uint64_t *full_cost_ptr;
175     uint64_t *full_cost_skip_ptr;
176     uint64_t *full_cost_merge_ptr;
177 
178 } ModeDecisionCandidateBuffer;
179 
180 /**************************************
181     * Extern Function Declarations
182     **************************************/
183 extern EbErrorType mode_decision_candidate_buffer_ctor(
184     ModeDecisionCandidateBuffer *buffer_ptr, EbBitDepthEnum max_bitdepth, uint8_t sb_size,
185     uint32_t buffer_mask, EbPictureBufferDesc *temp_residual_ptr,
186     EbPictureBufferDesc *temp_recon_ptr, uint64_t *fast_cost_ptr, uint64_t *full_cost_ptr,
187     uint64_t *full_cost_skip_ptr, uint64_t *full_cost_merge_ptr);
188 
189 extern EbErrorType mode_decision_scratch_candidate_buffer_ctor(
190     ModeDecisionCandidateBuffer *buffer_ptr, uint8_t sb_size, EbBitDepthEnum max_bitdepth);
191 
192 uint32_t product_full_mode_decision(struct ModeDecisionContext *context_ptr, BlkStruct *blk_ptr,
193                                     ModeDecisionCandidateBuffer **buffer_ptr_array,
194                                     uint32_t                      candidate_total_count,
195                                     uint32_t *                    best_candidate_index_array);
196 uint32_t get_blk_tuned_full_lambda(struct ModeDecisionContext *context_ptr,
197                                    PictureControlSet *pcs_ptr, uint32_t pic_full_lambda);
198 void     set_tuned_blk_lambda(struct ModeDecisionContext *context_ptr, PictureControlSet *pcs_ptr);
199 
200 typedef EbErrorType (*EB_INTRA_4x4_FAST_LUMA_COST_FUNC)(
201     struct ModeDecisionContext *context_ptr, uint32_t pu_index,
202     ModeDecisionCandidateBuffer *candidate_buffer_ptr, uint64_t luma_distortion, uint64_t lambda);
203 
204 typedef EbErrorType (*EB_INTRA_4x4_FULL_LUMA_COST_FUNC)(
205     ModeDecisionCandidateBuffer *candidate_buffer_ptr, uint64_t *y_distortion, uint64_t lambda,
206     uint64_t *y_coeff_bits, uint32_t transform_size);
207 
208 typedef EbErrorType (*EB_FULL_NXN_COST_FUNC)(PictureControlSet *          pcs_ptr,
209                                              ModeDecisionCandidateBuffer *candidate_buffer_ptr,
210                                              uint32_t qp, uint64_t *y_distortion,
211                                              uint64_t *cb_distortion, uint64_t *cr_distortion,
212                                              uint64_t lambda, uint64_t lambda_chroma,
213                                              uint64_t *y_coeff_bits, uint64_t *cb_coeff_bits,
214                                              uint64_t *cr_coeff_bits, uint32_t transform_size);
215 struct CodingLoopContext_s;
216 /*
217       |-------------------------------------------------------------|
218       | ref_idx          0            1           2            3       |
219       | List0            LAST        LAST2        LAST3        GOLD    |
220       | List1            BWD            ALT2            ALT                |
221       |-------------------------------------------------------------|
222     */
223 #define INVALID_REF 0xF
224 uint8_t                 get_ref_frame_idx(uint8_t ref_type);
225 extern MvReferenceFrame svt_get_ref_frame_type(uint8_t list, uint8_t ref_idx);
226 uint8_t                 get_list_idx(uint8_t ref_type);
227 #ifdef __cplusplus
228 }
229 #endif
230 #endif // EbModeDecision_h
231