1 /*
2 * Copyright(c) 2018 Intel Corporation
3 * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 */
5 
6 #ifndef EbModeDecision_h
7 #define EbModeDecision_h
8 
9 #include "EbDefinitions.h"
10 #include "EbUtility.h"
11 #include "EbPictureControlSet.h"
12 #include "EbCodingUnit.h"
13 #include "EbPredictionUnit.h"
14 #include "EbSyntaxElements.h"
15 #include "EbPictureBufferDesc.h"
16 //#include "EbMdRateEstimation.h"
17 #include "EbAdaptiveMotionVectorPrediction.h"
18 #include "EbPictureOperators.h"
19 #include "EbNeighborArrays.h"
20 #include "EbObject.h"
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 #define ENABLE_AMVP_MV_FOR_RC_PU   0
25 
26 #define MAX_MPM_CANDIDATES		   3
27 
28 	static const EB_U32 tuIndexList[3][16] =
29 	{
30 		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
31 		{ 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 },
32 		{ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
33 	};
34 
35 #define MERGE_PENALTY                          10
36 
37 	// Create incomplete struct definition for the following function pointer typedefs
38 	struct ModeDecisionCandidateBuffer_s;
39 	struct ModeDecisionContext_s;
40 
41 	/**************************************
42 	* Function Ptrs Definitions
43 	**************************************/
44 typedef EB_ERRORTYPE(*EB_PREDICTION_FUNC)(
45 	    struct ModeDecisionContext_s           *contextPtr,
46 		EB_U32                                  componentMask,
47 		PictureControlSet_t                    *pictureControlSetPtr,
48 	    struct ModeDecisionCandidateBuffer_s   *candidateBufferPtr);
49 
50 	typedef EB_ERRORTYPE(*EB_FAST_COST_FUNC)(
51         struct ModeDecisionContext_s           *contextPtr,
52 		CodingUnit_t                           *cuPtr,
53 	struct ModeDecisionCandidateBuffer_s   *candidateBufferPtr,
54 		EB_U32                                  qp,
55 		EB_U64                                  lumaDistortion,
56 		EB_U64                                  chromaDistortion,
57 		EB_U64                                  lambda,
58 		PictureControlSet_t                    *pictureControlSetPtr);
59 
60 	typedef EB_ERRORTYPE(*EB_FULL_COST_FUNC)(
61 		LargestCodingUnit_t					   *lcuPtr,
62 		CodingUnit_t                           *cuPtr,
63 		EB_U32                                  cuSize,
64 		EB_U32                                  cuSizeLog2,
65 	struct ModeDecisionCandidateBuffer_s       *candidateBufferPtr,
66 		EB_U32                                  qp,
67 		EB_U64                                 *yDistortion,
68 		EB_U64                                 *cbDistortion,
69 		EB_U64                                 *crDistortion,
70 		EB_U64                                  lambda,
71 		EB_U64                                  lambdaChroma,
72 		EB_U64                                 *yCoeffBits,
73 		EB_U64                                 *cbCoeffBits,
74 		EB_U64                                 *crCoeffBits,
75 		EB_U32                                  transformSize,
76 		EB_U32                                  transformChromaSize,
77 		PictureControlSet_t                    *pictureControlSetPtr);
78 
79 	typedef EB_ERRORTYPE(*EB_FULL_LUMA_COST_FUNC)(
80 		CodingUnit_t                           *cuPtr,
81 		EB_U32                                  cuSize,
82 		EB_U32                                  cuSizeLog2,
83 	struct ModeDecisionCandidateBuffer_s   *candidateBufferPtr,
84 		EB_U64                                 *yDistortion,
85 		EB_U64                                  lambda,
86 		EB_U64                                 *yCoeffBits,
87 		EB_U32                                  transformSize);
88 
89 	/**************************************
90 	* Mode Decision Candidate
91 	**************************************/
92 	typedef struct ModeDecisionCandidate_s
93 	{
94 		// *Warning - this struct has been organized to be cache efficient when being
95 		//    constructured in the function GenerateAmvpMergeInterIntraMdCandidatesCU.
96 		//    Changing the ordering could affect performance
97 
98 		union {
99 			struct {
100 				unsigned                        meDistortion    : 20;
101 				unsigned                        distortionReady : 1;
102 				unsigned                                        : 3;
103 				unsigned                        intraLumaMode   : 8;
104 			};
105 			EB_U32 oisResults;
106 		};
107         union {
108             struct{
109                 union {
110 
111                     struct{
112                         EB_S16              motionVector_x_L0;  //Note: Do not change the order of these fields
113                         EB_S16              motionVector_y_L0;
114                     };
115                     EB_U32 MVsL0;
116                 };
117                 union {
118 
119                     struct{
120                         EB_S16              motionVector_x_L1;  //Note: Do not change the order of these fields
121                         EB_S16              motionVector_y_L1;
122                     };
123                     EB_U32 MVsL1;
124                 };
125             };
126             EB_U64 MVs;
127         };
128 
129 		EB_U8                                   skipFlag;
130 		EB_BOOL                                 mergeFlag;
131 		EB_U8                                   mergeIndex;
132         EB_U16                                  countNonZeroCoeffs;
133 		EB_BOOL                                 predictionIsReady;
134 		EB_BOOL                                 predictionIsReadyLuma;
135 		EB_U8                                   type;
136         EB_BOOL									mpmFlag;
137 
138 		// MD Rate Estimation Ptr
139 		MdRateEstimationContext_t              *mdRateEstimationPtr; // 64 bits
140 		EB_U64                                  fastLumaRate;
141 		EB_U64                                  fastChromaRate;
142 		EB_U64                                  chromaDistortion;
143 		EB_U64                                  chromaDistortionInterDepth;
144 		EB_U32                                  lumaDistortion;
145 		EB_U32                                  fullDistortion;
146         EB_U32									fastLoopLumaDistortion;
147 
148 		// Function Pointers
149 		EB_PTR                                  predictionContextPtr;
150 		EB_FAST_COST_FUNC                       fastCostFuncPtr;
151 		EB_FULL_COST_FUNC                       fullCostFuncPtr;
152 		EB_FULL_LUMA_COST_FUNC                  fullLumaCostFuncPtr;
153 		EB_PREDDIRECTION                        predictionDirection[MAX_NUM_OF_PU_PER_CU]; // 2 bits
154 
155 
156 		EB_S16                                  motionVectorPred_x[MAX_NUM_OF_REF_PIC_LIST]; // 16 bits
157 		EB_S16                                  motionVectorPred_y[MAX_NUM_OF_REF_PIC_LIST]; // 16 bits
158 		EB_U8                                   motionVectorPredIdx[MAX_NUM_OF_REF_PIC_LIST]; // 2 bits
159 		EB_U8                                   transformSize;       // 8 bit
160 		EB_U8                                   transformChromaSize; // 8 bit
161 		EB_U8                                   rootCbf;             // ?? bit - determine empirically
162 		EB_U8                                   cbCbf;               // ?? bit
163 		EB_U8                                   crCbf;               // ?? bit
164         EB_U32                                  yCbf;                // Issue, should be less than 32
165 
166 
167 	} ModeDecisionCandidate_t;
168 
169 	/**************************************
170 	* Mode Decision Candidate Buffer
171 	**************************************/
172 	typedef struct IntraChromaCandidateBuffer_s {
173 		EB_U32                                  mode;
174 		EB_U64                                  cost;
175 		EB_U64                                  distortion;
176 
177 		EbPictureBufferDesc_t                  *predictionPtr;
178 		EbPictureBufferDesc_t                  *residualPtr;
179 
180 	} IntraChromaCandidateBuffer_t;
181 
182 	/**************************************
183 	* Mode Decision Candidate Buffer
184 	**************************************/
185 	typedef struct ModeDecisionCandidateBuffer_s {
186         EbDctor                                 dctor;
187 		// Candidate Ptr
188 		ModeDecisionCandidate_t                *candidatePtr;
189 
190 		// Video Buffers
191 		EbPictureBufferDesc_t                  *predictionPtr;
192 		EbPictureBufferDesc_t                  *residualQuantCoeffPtr;// One buffer for residual and quantized coefficient
193 		EbPictureBufferDesc_t                  *reconCoeffPtr;
194 
195 		// *Note - We should be able to combine the reconCoeffPtr & reconPtr pictures (they aren't needed at the same time)
196 		EbPictureBufferDesc_t                  *reconPtr;
197 
198 		// Distortion (SAD)
199 		EB_U64                                  residualLumaSad;
200 
201 		EB_U64									fullLambdaRate;
202         EB_U64                                  fullCostLuma;
203 
204 		// Costs
205 		EB_U64                                 *fastCostPtr;
206 		EB_U64                                 *fullCostPtr;
207 		EB_U64                                 fullCostPlusCuBoundDist;	// fullCostPlusCuBoundDist= fullCost + CU boundaries distortion if derived otherwise fullCostPlusCuBoundDist = fullCost
208 
209 		EB_U64                                 *fullCostSkipPtr;
210 		EB_U64                                 *fullCostMergePtr;
211 		//
212 		EB_U64                                 cbCoeffBits;
213 		EB_U64                                 cbDistortion[2];
214 		EB_U64                                 crCoeffBits;
215 		EB_U64                                 crDistortion[2];
216 		EB_U64                                 fullCostNoAc;
217 		CoeffCtxtMdl_t                         candBuffCoeffCtxModel;
218         EB_BOOL                                weightChromaDistortion;
219         EB_U64                                 yFullDistortion[DIST_CALC_TOTAL];
220         EB_U64                                 yCoeffBits;
221 		EB_S16                                 yDc[4];// Store the ABS of DC values per TU. If one TU, stored in 0, otherwise 4 tus stored in 0 to 3
222         EB_U16                                 yCountNonZeroCoeffs[4];// Store nonzero CoeffNum, per TU. If one TU, stored in 0, otherwise 4 tus stored in 0 to 3
223 
224 	} ModeDecisionCandidateBuffer_t;
225 
226 	/**************************************
227 	* Extern Function Declarations
228 	**************************************/
229 extern EB_ERRORTYPE ModeDecisionCandidateBufferCtor(
230 		ModeDecisionCandidateBuffer_t  *bufferPtr,
231 		EB_U16                          lcuMaxSize,
232 		EB_BITDEPTH                     maxBitdepth,
233 		EB_U64                         *fastCostPtr,
234 		EB_U64                         *fullCostPtr,
235 		EB_U64                         *fullCostSkipPtr,
236 		EB_U64                         *fullCostMergePtr);
237 
238 
239     EB_ERRORTYPE ProductGenerateAmvpMergeInterIntraMdCandidatesCU(
240 		LargestCodingUnit_t            *lcuPtr,
241                 struct ModeDecisionContext_s   *contextPtr,
242 		const EB_U32                    leafIndex,
243 		const EB_U32                    lcuAddr,
244 		EB_U32                         *bufferTotalCount,
245 		EB_U32                         *fastCandidateTotalCount,
246 		EB_PTR                          interPredContextPtr,
247 		PictureControlSet_t            *pictureControlSetPtr,
248 		EB_BOOL							mpmSearch,
249 		EB_U8	                        mpmSearchCandidate,
250 		EB_U32                         *mostProbableModeArray);
251 
252 
253 
254 	EB_U8 ProductFullModeDecision(
255         struct ModeDecisionContext_s   *contextPtr,
256 		CodingUnit_t                   *cuPtr,
257 		EB_U8                           cuSize,
258 		ModeDecisionCandidateBuffer_t **bufferPtrArray,
259 		EB_U32                          candidateTotalCount,
260 		EB_U8                          *bestCandidateIndexArray,
261 		EB_U32                         *bestIntraMode);
262 
263 	EB_ERRORTYPE PreModeDecision(
264 		CodingUnit_t                   *cuPtr,
265 		EB_U32                          bufferTotalCount,
266 		ModeDecisionCandidateBuffer_t **bufferPtrArray,
267 		EB_U32                         *fullCandidateTotalCountPtr,
268 		EB_U8                          *bestCandidateIndexArray,
269 		EB_U8                          *disableMergeIndex,
270         EB_BOOL                         sameFastFullCandidate);
271 
272 	typedef EB_ERRORTYPE(*EB_INTRA_4x4_FAST_LUMA_COST_FUNC)(
273 		struct ModeDecisionContext_s           *contextPtr,
274 		EB_U32                                  puIndex,
275 	    struct ModeDecisionCandidateBuffer_s   *candidateBufferPtr,
276 		EB_U64                                  lumaDistortion,
277 		EB_U64                                  lambda);
278 
279 	typedef EB_ERRORTYPE(*EB_INTRA_4x4_FULL_LUMA_COST_FUNC)(
280 	    struct ModeDecisionCandidateBuffer_s   *candidateBufferPtr,
281 		EB_U64                                 *yDistortion,
282 		EB_U64                                  lambda,
283 		EB_U64                                 *yCoeffBits,
284 		EB_U32                                  transformSize);
285 
286 	typedef EB_ERRORTYPE(*EB_INTRA_NxN_FAST_COST_FUNC)(
287 		CodingUnit_t                           *cuPtr,
288 	    struct ModeDecisionCandidateBuffer_s   *candidateBufferPtr,
289 		EB_U32                                  qp,
290 		EB_U64                                  lumaDistortion,
291 		EB_U64                                  chromaDistortion,
292 		EB_U64                                  lambda,
293 		PictureControlSet_t                    *pictureControlSetPtr);
294 
295 typedef EB_ERRORTYPE(*EB_INTRA_NxN_FULL_COST_FUNC)(
296 	PictureControlSet_t                    *pictureControlSetPtr,
297 	struct ModeDecisionCandidateBuffer_s   *candidateBufferPtr,
298 		EB_U32                                  qp,
299 		EB_U64                                 *yDistortion,
300 		EB_U64                                 *cbDistortion,
301 		EB_U64                                 *crDistortion,
302 		EB_U64                                  lambda,
303 		EB_U64                                  lambdaChroma,
304 		EB_U64                                 *yCoeffBits,
305 		EB_U64                                 *cbCoeffBits,
306 		EB_U64                                 *crCoeffBits,
307 		EB_U32                                  transformSize);
308 
309 
310 	typedef EB_ERRORTYPE(*EB_FULL_NXN_COST_FUNC)(
311 		PictureControlSet_t                    *pictureControlSetPtr,
312 	    struct ModeDecisionCandidateBuffer_s   *candidateBufferPtr,
313 		EB_U32                                  qp,
314 		EB_U64                                 *yDistortion,
315 		EB_U64                                 *cbDistortion,
316 		EB_U64                                 *crDistortion,
317 		EB_U64                                  lambda,
318 		EB_U64                                  lambdaChroma,
319 		EB_U64                                 *yCoeffBits,
320 		EB_U64                                 *cbCoeffBits,
321 		EB_U64                                 *crCoeffBits,
322 		EB_U32                                  transformSize);
323 
324 	struct CodingLoopContext_s;
325 #ifdef __cplusplus
326 }
327 #endif
328 #endif // EbModeDecision_h
329