1 /*!
2 * \copy
3 * Copyright (c) 2013, Cisco Systems
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33 //wels_common_basis.h
34 #ifndef WELS_COMMON_BASIS_H__
35 #define WELS_COMMON_BASIS_H__
36
37 #include "typedefs.h"
38 #include "macros.h"
39
40 #include "wels_common_defs.h"
41
42 using namespace WelsCommon;
43
44 namespace WelsDec {
45
46 /*common use table*/
47 extern const uint8_t g_kuiScan8[24];
48 extern const uint8_t g_kuiLumaDcZigzagScan[16];
49 extern const uint8_t g_kuiChromaDcScan[4];
50 extern const uint8_t g_kMbNonZeroCountIdx[24];
51 extern const uint8_t g_kCacheNzcScanIdx[4 * 4 + 4 + 4 + 3];
52 extern const uint8_t g_kCache26ScanIdx[16];
53 extern const uint8_t g_kCache30ScanIdx[16];
54 extern const uint8_t g_kNonZeroScanIdxC[4];
55 /* Profile IDC */
56 typedef uint8_t ProfileIdc;
57
58 /* Position Offset structure */
59 typedef struct TagPosOffset {
60 int32_t iLeftOffset;
61 int32_t iTopOffset;
62 int32_t iRightOffset;
63 int32_t iBottomOffset;
64 } SPosOffset;
65
66 /* MB Type & Sub-MB Type */
67 typedef uint32_t MbType;
68 typedef uint32_t SubMbType;
69
70 #define I16_LUMA_DC 1
71 #define I16_LUMA_AC 2
72 #define LUMA_DC_AC 3
73 #define CHROMA_DC 4
74 #define CHROMA_AC 5
75 #define LUMA_DC_AC_8 6
76 #define CHROMA_DC_U 7
77 #define CHROMA_DC_V 8
78 #define CHROMA_AC_U 9
79 #define CHROMA_AC_V 10
80 #define LUMA_DC_AC_INTRA 11
81 #define LUMA_DC_AC_INTER 12
82 #define CHROMA_DC_U_INTER 13
83 #define CHROMA_DC_V_INTER 14
84 #define CHROMA_AC_U_INTER 15
85 #define CHROMA_AC_V_INTER 16
86 #define LUMA_DC_AC_INTRA_8 17
87 #define LUMA_DC_AC_INTER_8 18
88
89 #define SHIFT_BUFFER(pBitsCache) { pBitsCache->pBuf+=2; pBitsCache->uiRemainBits += 16; pBitsCache->uiCache32Bit |= (((pBitsCache->pBuf[2] << 8) | pBitsCache->pBuf[3]) << (32 - pBitsCache->uiRemainBits)); }
90 #define POP_BUFFER(pBitsCache, iCount) { pBitsCache->uiCache32Bit <<= iCount; pBitsCache->uiRemainBits -= iCount; }
91
92 static const uint8_t g_kuiZigzagScan[16] = { //4*4block residual zig-zag scan order
93 0, 1, 4, 8,
94 5, 2, 3, 6,
95 9, 12, 13, 10,
96 7, 11, 14, 15,
97 };
98
99 static const uint8_t g_kuiZigzagScan8x8[64] = { //8x8 block residual zig-zag scan order
100 0, 1, 8, 16, 9, 2, 3, 10,
101 17, 24, 32, 25, 18, 11, 4, 5,
102 12, 19, 26, 33, 40, 48, 41, 34,
103 27, 20, 13, 6, 7, 14, 21, 28,
104 35, 42, 49, 56, 57, 50, 43, 36,
105 29, 22, 15, 23, 30, 37, 44, 51,
106 58, 59, 52, 45, 38, 31, 39, 46,
107 53, 60, 61, 54, 47, 55, 62, 63,
108 };
109
110 static const uint8_t g_kuiIdx2CtxSignificantCoeffFlag8x8[64] = { // Table 9-43, Page 289
111 0, 1, 2, 3, 4, 5, 5, 4,
112 4, 3, 3, 4, 4, 4, 5, 5,
113 4, 4, 4, 4, 3, 3, 6, 7,
114 7, 7, 8, 9, 10, 9, 8, 7,
115 7, 6, 11, 12, 13, 11, 6, 7,
116 8, 9, 14, 10, 9, 8, 6, 11,
117 12, 13, 11, 6, 9, 14, 10, 9,
118 11, 12, 13, 11, 14, 10, 12, 14,
119 };
120
121 static const uint8_t g_kuiIdx2CtxLastSignificantCoeffFlag8x8[64] = { // Table 9-43, Page 289
122 0, 1, 1, 1, 1, 1, 1, 1,
123 1, 1, 1, 1, 1, 1, 1, 1,
124 2, 2, 2, 2, 2, 2, 2, 2,
125 2, 2, 2, 2, 2, 2, 2, 2,
126 3, 3, 3, 3, 3, 3, 3, 3,
127 4, 4, 4, 4, 4, 4, 4, 4,
128 5, 5, 5, 5, 6, 6, 6, 6,
129 7, 7, 7, 7, 8, 8, 8, 8,
130 };
131
GetMbResProperty(int32_t * pMBproperty,int32_t * pResidualProperty,bool bCavlc)132 static inline void GetMbResProperty (int32_t* pMBproperty, int32_t* pResidualProperty, bool bCavlc) {
133 switch (*pResidualProperty) {
134 case CHROMA_AC_U:
135 *pMBproperty = 1;
136 *pResidualProperty = bCavlc ? CHROMA_AC : CHROMA_AC_U;
137 break;
138 case CHROMA_AC_V:
139 *pMBproperty = 2;
140 *pResidualProperty = bCavlc ? CHROMA_AC : CHROMA_AC_V;
141 break;
142 case LUMA_DC_AC_INTRA:
143 *pMBproperty = 0;
144 *pResidualProperty = LUMA_DC_AC;
145 break;
146 case CHROMA_DC_U:
147 *pMBproperty = 1;
148 *pResidualProperty = bCavlc ? CHROMA_DC : CHROMA_DC_U;
149 break;
150 case CHROMA_DC_V:
151 *pMBproperty = 2;
152 *pResidualProperty = bCavlc ? CHROMA_DC : CHROMA_DC_V;
153 break;
154 case I16_LUMA_AC:
155 *pMBproperty = 0;
156 break;
157 case I16_LUMA_DC:
158 *pMBproperty = 0;
159 break;
160 case LUMA_DC_AC_INTER:
161 *pMBproperty = 3;
162 *pResidualProperty = LUMA_DC_AC;
163 break;
164 case CHROMA_DC_U_INTER:
165 *pMBproperty = 4;
166 *pResidualProperty = bCavlc ? CHROMA_DC : CHROMA_DC_U;
167 break;
168 case CHROMA_DC_V_INTER:
169 *pMBproperty = 5;
170 *pResidualProperty = bCavlc ? CHROMA_DC : CHROMA_DC_V;
171 break;
172 case CHROMA_AC_U_INTER:
173 *pMBproperty = 4;
174 *pResidualProperty = bCavlc ? CHROMA_AC : CHROMA_AC_U;
175 break;
176 case CHROMA_AC_V_INTER:
177 *pMBproperty = 5;
178 *pResidualProperty = bCavlc ? CHROMA_AC : CHROMA_AC_V;
179 break;
180 // Reference to Table 7-2
181 case LUMA_DC_AC_INTRA_8:
182 *pMBproperty = 6;
183 *pResidualProperty = LUMA_DC_AC_8;
184 break;
185 case LUMA_DC_AC_INTER_8:
186 *pMBproperty = 7;
187 *pResidualProperty = LUMA_DC_AC_8;
188 break;
189 }
190 }
191
192 typedef struct TagI16PredInfo {
193 int8_t iPredMode;
194 int8_t iLeftAvail;
195 int8_t iTopAvail;
196 int8_t iLeftTopAvail;
197 } SI16PredInfo;
198 static const SI16PredInfo g_ksI16PredInfo[4] = {
199 {I16_PRED_V, 0, 1, 0},
200 {I16_PRED_H, 1, 0, 0},
201 { 0, 0, 0, 0},
202 {I16_PRED_P, 1, 1, 1},
203 };
204
205 static const SI16PredInfo g_ksChromaPredInfo[4] = {
206 { 0, 0, 0, 0},
207 {C_PRED_H, 1, 0, 0},
208 {C_PRED_V, 0, 1, 0},
209 {C_PRED_P, 1, 1, 1},
210 };
211
212
213 typedef struct TagI4PredInfo {
214 int8_t iPredMode;
215 int8_t iLeftAvail;
216 int8_t iTopAvail;
217 int8_t iLeftTopAvail;
218 // int8_t right_top_avail; //when right_top unavailable but top avail, we can pad the right-top with the rightmost pixel of top
219 } SI4PredInfo;
220 static const SI4PredInfo g_ksI4PredInfo[9] = {
221 { I4_PRED_V, 0, 1, 0},
222 { I4_PRED_H, 1, 0, 0},
223 { 0, 0, 0, 0},
224 {I4_PRED_DDL, 0, 1, 0},
225 {I4_PRED_DDR, 1, 1, 1},
226 { I4_PRED_VR, 1, 1, 1},
227 { I4_PRED_HD, 1, 1, 1},
228 { I4_PRED_VL, 0, 1, 0},
229 { I4_PRED_HU, 1, 0, 0},
230 };
231
232 static const uint8_t g_kuiI16CbpTable[6] = {0, 16, 32, 15, 31, 47};
233
234
235 typedef struct TagPartMbInfo {
236 MbType iType;
237 int8_t iPartCount; //P_16*16, P_16*8, P_8*16, P_8*8 based on 8*8 block; P_8*4, P_4*8, P_4*4 based on 4*4 block
238 int8_t iPartWidth; //based on 4*4 block
239 } SPartMbInfo;
240
241 //Table 7.13. Macroblock type values 0 to 4 for P slices.
242 static const SPartMbInfo g_ksInterPMbTypeInfo[5] = {
243 {MB_TYPE_16x16, 1, 4},
244 {MB_TYPE_16x8, 2, 4},
245 {MB_TYPE_8x16, 2, 2},
246 {MB_TYPE_8x8, 4, 4},
247 {MB_TYPE_8x8_REF0, 4, 4}, //ref0--ref_idx not present in bit-stream and default as 0
248 };
249
250 //Table 7.14. Macroblock type values 0 to 22 for B slices.
251 static const SPartMbInfo g_ksInterBMbTypeInfo[] = {
252 // Part 0 Part 1
253 { MB_TYPE_DIRECT, 1, 4 }, //B_Direct_16x16
254 { MB_TYPE_16x16 | MB_TYPE_P0L0, 1, 4 }, //B_L0_16x16
255 { MB_TYPE_16x16 | MB_TYPE_P0L1, 1, 4 }, //B_L1_16x16
256 { MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1, 1, 4 }, //B_Bi_16x16
257 { MB_TYPE_16x8 | MB_TYPE_P0L0 | MB_TYPE_P1L0, 2, 4 }, //B_L0_L0_16x8
258 { MB_TYPE_8x16 | MB_TYPE_P0L0 | MB_TYPE_P1L0, 2, 2 }, //B_L0_L0_8x16
259 { MB_TYPE_16x8 | MB_TYPE_P0L1 | MB_TYPE_P1L1, 2, 4 }, //B_L1_L1_16x8
260 { MB_TYPE_8x16 | MB_TYPE_P0L1 | MB_TYPE_P1L1, 2, 2 }, //B_L1_L1_8x16
261 { MB_TYPE_16x8 | MB_TYPE_P0L0 | MB_TYPE_P1L1, 2, 4 }, //B_L0_L1_16x8
262 { MB_TYPE_8x16 | MB_TYPE_P0L0 | MB_TYPE_P1L1, 2, 2 }, //B_L0_L1_8x16
263 { MB_TYPE_16x8 | MB_TYPE_P0L1 | MB_TYPE_P1L0, 2, 4 }, //B_L1_L0_16x8
264 { MB_TYPE_8x16 | MB_TYPE_P0L1 | MB_TYPE_P1L0, 2, 2 }, //B_L1_L0_8x16
265 { MB_TYPE_16x8 | MB_TYPE_P0L0 | MB_TYPE_P1L0 | MB_TYPE_P1L1, 2, 4 }, //B_L0_Bi_16x8
266 { MB_TYPE_8x16 | MB_TYPE_P0L0 | MB_TYPE_P1L0 | MB_TYPE_P1L1, 2, 2 }, //B_L0_Bi_8x16
267 { MB_TYPE_16x8 | MB_TYPE_P0L1 | MB_TYPE_P1L0 | MB_TYPE_P1L1, 2, 4 }, //B_L1_Bi_16x8
268 { MB_TYPE_8x16 | MB_TYPE_P0L1 | MB_TYPE_P1L0 | MB_TYPE_P1L1, 2, 2 }, //B_L1_Bi_8x16
269 { MB_TYPE_16x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 | MB_TYPE_P1L0, 2, 4 }, //B_Bi_L0_16x8
270 { MB_TYPE_8x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 | MB_TYPE_P1L0, 2, 2 }, //B_Bi_L0_8x16
271 { MB_TYPE_16x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 | MB_TYPE_P1L1, 2, 4 }, //B_Bi_L1_16x8
272 { MB_TYPE_8x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 | MB_TYPE_P1L1, 2, 2 }, //B_Bi_L1_8x16
273 { MB_TYPE_16x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 | MB_TYPE_P1L0 | MB_TYPE_P1L1, 2, 4 }, //B_Bi_Bi_16x8
274 { MB_TYPE_8x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 | MB_TYPE_P1L0 | MB_TYPE_P1L1, 2, 2 }, //B_Bi_Bi_8x16
275 { MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 | MB_TYPE_P1L0 | MB_TYPE_P1L1, 4, 4 } //B_8x8
276 };
277
278 //Table 7.17 Sub-macroblock types in B macroblocks.
279 static const SPartMbInfo g_ksInterPSubMbTypeInfo[4] = {
280 {SUB_MB_TYPE_8x8, 1, 2},
281 {SUB_MB_TYPE_8x4, 2, 2},
282 {SUB_MB_TYPE_4x8, 2, 1},
283 {SUB_MB_TYPE_4x4, 4, 1},
284 };
285
286 //Table 7.18 Sub-macroblock types in B macroblocks.
287 static const SPartMbInfo g_ksInterBSubMbTypeInfo[] = {
288 { MB_TYPE_DIRECT, 1, 2 }, //B_Direct_8x8
289 { SUB_MB_TYPE_8x8 | MB_TYPE_P0L0, 1, 2 }, //B_L0_8x8
290 { SUB_MB_TYPE_8x8 | MB_TYPE_P0L1, 1, 2 }, //B_L1_8x8
291 { SUB_MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1, 1, 2 }, //B_Bi_8x8
292 { SUB_MB_TYPE_8x4 | MB_TYPE_P0L0, 2, 2 }, //B_L0_8x4
293 { SUB_MB_TYPE_4x8 | MB_TYPE_P0L0, 2, 1 }, //B_L0_4x8
294 { SUB_MB_TYPE_8x4 | MB_TYPE_P0L1, 2, 2 }, //B_L1_8x4
295 { SUB_MB_TYPE_4x8 | MB_TYPE_P0L1, 2, 1 }, //B_L1_4x8
296 { SUB_MB_TYPE_8x4 | MB_TYPE_P0L0 | MB_TYPE_P0L1, 2, 2 }, //B_Bi_8x4
297 { SUB_MB_TYPE_4x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1, 2, 1 }, //B_Bi_4x8
298 { SUB_MB_TYPE_4x4 | MB_TYPE_P0L0, 4, 1 }, //B_L0_4x4
299 { SUB_MB_TYPE_4x4 | MB_TYPE_P0L1, 4, 1 }, //B_L1_4x4
300 { SUB_MB_TYPE_4x4 | MB_TYPE_P0L0 | MB_TYPE_P0L1, 4, 1 } //B_Bi_4x4
301 };
302
303 typedef struct TagSar {
304 uint32_t uiWidth;
305 uint32_t uiHeight;
306 } sSar;
307 static const sSar g_ksVuiSampleAspectRatio[17] = { //Table E-1
308 { 0, 0}, { 1, 1}, {12, 11}, { 10, 11}, {16, 11}, //0~4
309 {40, 33}, {24, 11}, {20, 11}, { 32, 11}, {80, 33}, //5~9
310 {18, 11}, {15, 11}, {64, 33}, {160, 99}, { 4, 3}, //10~14
311 { 3, 2}, { 2, 1} //15~16
312 };
313
314
315 } // namespace WelsDec
316
317 #endif//WELS_COMMON_BASIS_H__
318