1 /*
2 * Copyright(c) 2018 Intel Corporation
3 * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 */
5 
6 #ifndef EbCodingUnit_h
7 #define EbCodingUnit_h
8 
9 #include "EbDefinitions.h"
10 #include "EbSyntaxElements.h"
11 #include "EbMotionEstimationLcuResults.h"
12 #include "EbDefinitions.h"
13 #include "EbPictureBufferDesc.h"
14 #include "EbPredictionUnit.h"
15 #include "EbTransformUnit.h"
16 #include "EbCabacContextModel.h"
17 #include "EbObject.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 /*
23 Requirements:
24 -Must have enough CodingUnits for every single CU pattern
25 -Easy to expand/insert CU
26 -Easy to collapse a CU
27 -Easy to replace CUs
28 -Statically Allocated
29 -Contains the leaf count
30 
31 */
32 
33 // Macros for deblocking filter
34 #define MAX_LCU_SIZE_IN_4X4BLK                                  (MAX_LCU_SIZE >> 2)
35 #define VERTICAL_EDGE_BS_ARRAY_SIZE                             (MAX_LCU_SIZE_IN_4X4BLK * MAX_LCU_SIZE_IN_4X4BLK)
36 #define HORIZONTAL_EDGE_BS_ARRAY_SIZE                           (MAX_LCU_SIZE_IN_4X4BLK * MAX_LCU_SIZE_IN_4X4BLK)
37 
38 #define MAX_NUMBER_OF_BS_EDGES_PER_TREEBLOCK    128
39 #define MAX_NUMBER_OF_LEAFS_PER_TREEBLOCK       64
40 #define MAX_NUMBER_OF_4x4_TUs_IN_8x8_LEAF       4
41 
42 #define SAO_BO_INTERVALS                                32
43 #define SAO_BO_LEN                                      4
44 #define SAO_EO_TYPES                                    4
45 #define SAO_EO_CATEGORIES                               4
46 #define MIN_SAO_OFFSET_VALUE                          -31// -7
47 #define MAX_SAO_OFFSET_VALUE                           31// 7
48 #define NUMBER_SAO_OFFSETS                              4
49 #define SAO_COMPONENT_LUMA                              0
50 #define SAO_COMPONENT_CHROMA                            1
51 #define SAO_COMPONENT_CHROMA_CB                         2
52 #define SAO_COMPONENT_CHROMA_CR                         3
53 
54 struct PictureControlSet_s;
55 
56 #define MAX_CU_COST (0xFFFFFFFFFFFFFFFFull >> 1)
57 #define INVALID_FAST_CANDIDATE_INDEX	~0
58 
59 #define MAX_OIS_0   7 // when I Slice
60 #define MAX_OIS_1   9 // when P/B Slice and oisKernelLevel = 0
61 #define MAX_OIS_2  18 // when P/B Slice and oisKernelLevel = 1
62 
63 typedef struct CodingUnit_s
64 {
65     TransformUnit_t             transformUnitArray[TRANSFORM_UNIT_MAX_COUNT]; // 2-bytes * 21 = 42-bytes
66     PredictionUnit_t            predictionUnitArray[MAX_NUM_OF_PU_PER_CU];    // 35-bytes * 4 = 140 bytes
67 
68     unsigned                    skipFlagContext             : 2;
69     unsigned                    predictionModeFlag          : 2;
70     unsigned                    rootCbf                     : 1;
71     unsigned                    splitFlagContext            : 2;
72     unsigned                    qp                          : 6;
73     unsigned                    refQp                       : 6;
74 
75 	signed 						deltaQp						: 8; // can be signed 8bits
76 	signed 						orgDeltaQp					: 8;
77 
78 
79 
80         // Coded Tree
81 	struct {
82 		unsigned                   leafIndex : 8;
83 		unsigned                   splitFlag : 1;
84 		unsigned                   skipFlag  : 1;
85 
86 	};
87 
88 } CodingUnit_t;
89 
90 
91 typedef struct OisCandidate_s {
92     union {
93         struct {
94             unsigned distortion         :   20;
95             unsigned validDistortion    :   1;
96             unsigned                    :   3;
97             unsigned intraMode          :   8;
98         };
99         EB_U32 oisResults;
100     };
101 } OisCandidate_t;
102 
103 typedef struct OisLcuResults_s
104 {
105     EB_U8           totalIntraLumaMode[CU_MAX_COUNT];
106     OisCandidate_t  sortedOisCandidate[CU_MAX_COUNT][MAX_OIS_2];
107 } OisLcuResults_t;
108 
109 
110 
111 typedef struct OisCu32Cu16Results_s
112 {
113 	EB_U8            totalIntraLumaMode[21];
114 	OisCandidate_t*  sortedOisCandidate[21];
115 
116 } OisCu32Cu16Results_t;
117 
118 typedef struct OisCu8Results_s
119 {
120 	EB_U8            totalIntraLumaMode[64];
121 	OisCandidate_t*  sortedOisCandidate[64];
122 
123 } OisCu8Results_t;
124 
125 
126 typedef struct SaoStats_s {
127     EbDctor                         dctor;
128    	EB_S32                        **boDiff;
129     EB_U16                        **boCount;
130 	EB_S32                          eoDiff[3][SAO_EO_TYPES][SAO_EO_CATEGORIES+1];
131     EB_U16                          eoCount[3][SAO_EO_TYPES][SAO_EO_CATEGORIES+1];
132     EB_S32                         *eoDiff1D;
133     EB_U32                         *eoCount1D;
134 
135 } SaoStats_t;
136 
137 typedef struct SaoParameters_s {
138 
139     // SAO
140     EB_BOOL                         saoMergeLeftFlag;
141     EB_BOOL                         saoMergeUpFlag;
142     EB_U32                          saoTypeIndex[2];
143     EB_S32                          saoOffset[3][4];
144     EB_U32                          saoBandPosition[3];
145 
146 } SaoParameters_t;
147 
148 typedef struct QpmLcuResults_s {
149     EB_U8  cuQP;
150 	EB_U8  cuIntraQP;
151     EB_U8  cuInterQP;
152     EB_S8  deltaQp;
153 	EB_S8  innerLcuCudeltaQp;
154 
155 } QpmLcuResults_t;
156 
157 
158 typedef struct EdgeLcuResults_s {
159     EB_U8  edgeBlockNum;
160 	EB_U8  isolatedHighIntensityLcu;
161 
162 } EdgeLcuResults_t;
163 
164 typedef struct TileInfo_s {
165     EB_U16 tileLcuOriginX;
166     EB_U16 tileLcuOriginY;
167     EB_U16 tileLcuEndX;
168     EB_U16 tileLcuEndY;
169 
170     EB_U16 tilePxlOriginX;
171     EB_U16 tilePxlOriginY;
172     EB_U16 tilePxlEndX;
173     EB_U16 tilePxlEndY;
174 } TileInfo_t;
175 
176 typedef struct LcuTileInfo_s {
177     EB_U8     pictureLeftEdgeFlag                : 1;
178     EB_U8     pictureTopEdgeFlag                 : 1;
179     EB_U8     pictureRightEdgeFlag               : 1;
180     EB_U8     tileLeftEdgeFlag                   : 1;
181     EB_U8     tileTopEdgeFlag                    : 2;
182     EB_U8     tileRightEdgeFlag                  : 2;
183     EB_U16    tileIndexInRaster;
184 } LcuEdgeInfo_t;
185 
186 typedef struct LargestCodingUnit_s {
187     EbDctor                         dctor;
188     struct PictureControlSet_s     *pictureControlSetPtr;
189     CodingUnit_t                  **codedLeafArrayPtr;
190 
191     // Coding Units
192     EB_AURA_STATUS                  auraStatus;
193 
194     unsigned     qp                                 : 8;
195     unsigned     size                               : 8;
196     unsigned     sizeLog2                           : 4;
197     unsigned     pred64                             : 4;
198 
199     unsigned     index                              : 14; // supports up to 8k resolution
200     unsigned     originX                            : 13; // supports up to 8k resolution 8191
201     unsigned     originY                            : 13; // supports up to 8k resolution 8191
202 
203     // SAO
204     SaoParameters_t                 saoParams;
205 
206     //Bits only used for quantized coeffs
207     EB_U32                          quantizedCoeffsBits;
208     EB_U32                          totalBits;
209 
210     // Quantized Coefficients
211     EbPictureBufferDesc_t          *quantizedCoeff;
212     EB_U8                           intra4x4Mode[256];
213     EB_U8                           preMdcRefinementLevel;
214 
215 	EB_U8							chromaEncodeMode;
216 
217     EB_INTRA4x4_SEARCH_METHOD       intra4x4SearchMethod;
218 
219     // Lcu/Tiles related info, stored in ppcs, double check if need to copy here
220     LcuEdgeInfo_t                   *lcuEdgeInfoPtr;
221     TileInfo_t                      *tileInfoPtr;
222 } LargestCodingUnit_t;
223 
224 
225 extern EB_ERRORTYPE LargestCodingUnitCtor(
226     LargestCodingUnit_t         *largetCodingUnitPtr,
227     EB_U8                        lcuSize,
228     EB_U32                       pictureWidth,
229     EB_U32                       pictureHeight,
230     EB_U16                       lcuOriginX,
231     EB_U16                       lcuOriginY,
232     EB_U16                       lcuIndex,
233     struct PictureControlSet_s  *pictureControlSet);
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 #endif // EbCodingUnit_h
239