1 /*!
2 * \copy
3 * Copyright (c) 2009-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 * \file golomb.h
33 *
34 * \brief Exponential Golomb entropy coding/decoding routine
35 *
36 * \date 03/13/2009 Created
37 *
38 *************************************************************************************
39 */
40 #ifndef WELS_EXPONENTIAL_GOLOMB_ENTROPY_CODING_H__
41 #define WELS_EXPONENTIAL_GOLOMB_ENTROPY_CODING_H__
42
43 #include "typedefs.h"
44 #include "bit_stream.h"
45 #include "macros.h"
46 //#include <assert.h>
47 #include "ls_defines.h"
48 #include "error_code.h"
49
50 namespace WelsDec {
51
52 #define WELS_READ_VERIFY(uiRet) do{ \
53 uint32_t uiRetTmp = (uint32_t)uiRet; \
54 if( uiRetTmp != ERR_NONE ) \
55 return uiRetTmp; \
56 }while(0)
57 #define GET_WORD(iCurBits, pBufPtr, iLeftBits, iAllowedBytes, iReadBytes) { \
58 if (iReadBytes > iAllowedBytes+1) { \
59 return ERR_INFO_READ_OVERFLOW; \
60 } \
61 iCurBits |= ((uint32_t)((pBufPtr[0] << 8) | pBufPtr[1])) << (iLeftBits); \
62 iLeftBits -= 16; \
63 pBufPtr +=2; \
64 }
65 #define NEED_BITS(iCurBits, pBufPtr, iLeftBits, iAllowedBytes, iReadBytes) { \
66 if (iLeftBits > 0) { \
67 GET_WORD(iCurBits, pBufPtr, iLeftBits, iAllowedBytes, iReadBytes); \
68 } \
69 }
70 #define UBITS(iCurBits, iNumBits) (iCurBits>>(32-(iNumBits)))
71 #define DUMP_BITS(iCurBits, pBufPtr, iLeftBits, iNumBits, iAllowedBytes, iReadBytes) { \
72 iCurBits <<= (iNumBits); \
73 iLeftBits += (iNumBits); \
74 NEED_BITS(iCurBits, pBufPtr, iLeftBits, iAllowedBytes, iReadBytes); \
75 }
76
BsGetBits(PBitStringAux pBs,int32_t iNumBits,uint32_t * pCode)77 static inline int32_t BsGetBits (PBitStringAux pBs, int32_t iNumBits, uint32_t* pCode) {
78 intX_t iRc = UBITS (pBs->uiCurBits, iNumBits);
79 intX_t iAllowedBytes = pBs->pEndBuf - pBs->pStartBuf; //actual stream bytes
80 intX_t iReadBytes = pBs->pCurBuf - pBs->pStartBuf;
81 DUMP_BITS (pBs->uiCurBits, pBs->pCurBuf, pBs->iLeftBits, iNumBits, iAllowedBytes, iReadBytes);
82 *pCode = (uint32_t)iRc;
83 return ERR_NONE;
84 }
85
86 /*
87 * Exponential Golomb codes decoding routines
88 */
89
90 // for data sharing cross modules and try to reduce size of binary generated, 12/10/2009
91 extern const uint8_t g_kuiIntra4x4CbpTable[48];
92 extern const uint8_t g_kuiIntra4x4CbpTable400[16];
93 extern const uint8_t g_kuiInterCbpTable[48];
94 extern const uint8_t g_kuiInterCbpTable400[16];
95
96 extern const uint8_t g_kuiLeadingZeroTable[256];
97
98 static const uint32_t g_kuiPrefix8BitsTable[16] = {
99 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3
100 };
101
102
GetPrefixBits(uint32_t uiValue)103 static inline uint32_t GetPrefixBits (uint32_t uiValue) {
104 uint32_t iNumBit = 0;
105
106 if (uiValue & 0xffff0000) {
107 uiValue >>= 16;
108 iNumBit += 16;
109 }
110 if (uiValue & 0xff00) {
111 uiValue >>= 8;
112 iNumBit += 8;
113 }
114
115 if (uiValue & 0xf0) {
116 uiValue >>= 4;
117 iNumBit += 4;
118 }
119 iNumBit += g_kuiPrefix8BitsTable[uiValue];
120
121 return (32 - iNumBit);
122 }
123
124 /*
125 * Read one bit from bit stream followed
126 */
BsGetOneBit(PBitStringAux pBs,uint32_t * pCode)127 static inline uint32_t BsGetOneBit (PBitStringAux pBs, uint32_t* pCode) {
128 return (BsGetBits (pBs, 1, pCode));
129 }
130
GetLeadingZeroBits(uint32_t iCurBits)131 static inline int32_t GetLeadingZeroBits (uint32_t iCurBits) { //<=32 bits
132 uint32_t uiValue;
133
134 uiValue = UBITS (iCurBits, 8); //ShowBits( bs, 8 );
135 if (uiValue) {
136 return g_kuiLeadingZeroTable[uiValue];
137 }
138
139 uiValue = UBITS (iCurBits, 16); //ShowBits( bs, 16 );
140 if (uiValue) {
141 return (g_kuiLeadingZeroTable[uiValue] + 8);
142 }
143
144 uiValue = UBITS (iCurBits, 24); //ShowBits( bs, 24 );
145 if (uiValue) {
146 return (g_kuiLeadingZeroTable[uiValue] + 16);
147 }
148
149 uiValue = iCurBits; //ShowBits( bs, 32 );
150 if (uiValue) {
151 return (g_kuiLeadingZeroTable[uiValue] + 24);
152 }
153 //ASSERT(false); // should not go here
154 return -1;
155 }
156
BsGetUe(PBitStringAux pBs,uint32_t * pCode)157 static inline uint32_t BsGetUe (PBitStringAux pBs, uint32_t* pCode) {
158 uint32_t iValue = 0;
159 int32_t iLeadingZeroBits = GetLeadingZeroBits (pBs->uiCurBits);
160 intX_t iAllowedBytes, iReadBytes;
161 iAllowedBytes = pBs->pEndBuf - pBs->pStartBuf; //actual stream bytes
162
163 if (iLeadingZeroBits == -1) { //bistream error
164 return ERR_INFO_READ_LEADING_ZERO;//-1
165 } else if (iLeadingZeroBits >
166 16) { //rarely into this condition (even may be bitstream error), prevent from 16-bit reading overflow
167 //using two-step reading instead of one time reading of >16 bits.
168 iReadBytes = pBs->pCurBuf - pBs->pStartBuf;
169 DUMP_BITS (pBs->uiCurBits, pBs->pCurBuf, pBs->iLeftBits, 16, iAllowedBytes, iReadBytes);
170 iReadBytes = pBs->pCurBuf - pBs->pStartBuf;
171 DUMP_BITS (pBs->uiCurBits, pBs->pCurBuf, pBs->iLeftBits, iLeadingZeroBits + 1 - 16, iAllowedBytes, iReadBytes);
172 } else {
173 iReadBytes = pBs->pCurBuf - pBs->pStartBuf;
174 DUMP_BITS (pBs->uiCurBits, pBs->pCurBuf, pBs->iLeftBits, iLeadingZeroBits + 1, iAllowedBytes, iReadBytes);
175 }
176 if (iLeadingZeroBits) {
177 iValue = UBITS (pBs->uiCurBits, iLeadingZeroBits);
178 iReadBytes = pBs->pCurBuf - pBs->pStartBuf;
179 DUMP_BITS (pBs->uiCurBits, pBs->pCurBuf, pBs->iLeftBits, iLeadingZeroBits, iAllowedBytes, iReadBytes);
180 }
181
182 *pCode = ((1u << iLeadingZeroBits) - 1 + iValue);
183 return ERR_NONE;
184 }
185
186
187 /*
188 * Read signed exp golomb codes
189 */
BsGetSe(PBitStringAux pBs,int32_t * pCode)190 static inline int32_t BsGetSe (PBitStringAux pBs, int32_t* pCode) {
191 uint32_t uiCodeNum;
192
193 WELS_READ_VERIFY (BsGetUe (pBs, &uiCodeNum));
194
195 if (uiCodeNum & 0x01) {
196 *pCode = (int32_t) ((uiCodeNum + 1) >> 1);
197 } else {
198 *pCode = NEG_NUM ((int32_t) (uiCodeNum >> 1));
199 }
200 return ERR_NONE;
201 }
202
203 /*
204 * Get unsigned truncated exp golomb code.
205 */
BsGetTe0(PBitStringAux pBs,int32_t iRange,uint32_t * pCode)206 static inline int32_t BsGetTe0 (PBitStringAux pBs, int32_t iRange, uint32_t* pCode) {
207 if (iRange == 1) {
208 *pCode = 0;
209 } else if (iRange == 2) {
210 WELS_READ_VERIFY (BsGetOneBit (pBs, pCode));
211 *pCode ^= 1;
212 } else {
213 WELS_READ_VERIFY (BsGetUe (pBs, pCode));
214 }
215 return ERR_NONE;
216 }
217
218 /*
219 * Get number of trailing bits
220 */
BsGetTrailingBits(uint8_t * pBuf)221 static inline int32_t BsGetTrailingBits (uint8_t* pBuf) {
222 // TODO
223 uint32_t uiValue = *pBuf;
224 int32_t iRetNum = 0;
225
226 do {
227 if (uiValue & 1)
228 return iRetNum;
229 uiValue >>= 1;
230 ++ iRetNum;
231 } while (iRetNum < 9);
232
233 return 0;
234 }
235
236 /*
237 * Check whether there is more rbsp data for processing
238 */
CheckMoreRBSPData(PBitStringAux pBsAux)239 static inline bool CheckMoreRBSPData (PBitStringAux pBsAux) {
240 if ((pBsAux->iBits - ((pBsAux->pCurBuf - pBsAux->pStartBuf - 2) << 3) - pBsAux->iLeftBits) > 1) {
241 return true;
242 } else {
243 return false;
244 }
245 }
246
247 //define macros to check syntax elements
248 #define WELS_CHECK_SE_BOTH_ERROR(val, lower_bound, upper_bound, syntax_name, ret_code) do {\
249 if ((val < lower_bound) || (val > upper_bound)) {\
250 WelsLog(&(pCtx->sLogCtx), WELS_LOG_ERROR, "invalid syntax " syntax_name " %d", val);\
251 return ret_code;\
252 }\
253 }while(0)
254
255 #define WELS_CHECK_SE_LOWER_ERROR(val, lower_bound, syntax_name, ret_code) do {\
256 if (val < lower_bound) {\
257 WelsLog(&(pCtx->sLogCtx), WELS_LOG_ERROR, "invalid syntax " syntax_name " %d", val);\
258 return ret_code;\
259 }\
260 }while(0)
261
262 #define WELS_CHECK_SE_UPPER_ERROR(val, upper_bound, syntax_name, ret_code) do {\
263 if (val > upper_bound) {\
264 WelsLog(&(pCtx->sLogCtx), WELS_LOG_ERROR, "invalid syntax " syntax_name " %d", val);\
265 return ret_code;\
266 }\
267 }while(0)
268
269 #define WELS_CHECK_SE_BOTH_ERROR_NOLOG(val, lower_bound, upper_bound, syntax_name, ret_code) do {\
270 if ((val < lower_bound) || (val > upper_bound)) {\
271 return ret_code;\
272 }\
273 }while(0)
274
275 #define WELS_CHECK_SE_LOWER_ERROR_NOLOG(val, lower_bound, syntax_name, ret_code) do {\
276 if (val < lower_bound) {\
277 return ret_code;\
278 }\
279 }while(0)
280
281 #define WELS_CHECK_SE_UPPER_ERROR_NOLOG(val, upper_bound, syntax_name, ret_code) do {\
282 if (val > upper_bound) {\
283 return ret_code;\
284 }\
285 }while(0)
286
287
288 #define WELS_CHECK_SE_BOTH_WARNING(val, lower_bound, upper_bound, syntax_name) do {\
289 if ((val < lower_bound) || (val > upper_bound)) {\
290 WelsLog(&(pCtx->sLogCtx), WELS_LOG_WARNING, "invalid syntax " syntax_name " %d", val);\
291 }\
292 }while(0)
293
294 #define WELS_CHECK_SE_LOWER_WARNING(val, lower_bound, syntax_name) do {\
295 if (val < lower_bound) {\
296 WelsLog(&(pCtx->sLogCtx), WELS_LOG_WARNING, "invalid syntax " syntax_name " %d", val);\
297 }\
298 }while(0)
299
300 #define WELS_CHECK_SE_UPPER_WARNING(val, upper_bound, syntax_name) do {\
301 if (val > upper_bound) {\
302 WelsLog(&(pCtx->sLogCtx), WELS_LOG_WARNING, "invalid syntax " syntax_name " %d", val);\
303 }\
304 }while(0)
305 // below define syntax element offset
306 // for bit_depth_luma_minus8 and bit_depth_chroma_minus8
307 #define BIT_DEPTH_LUMA_OFFSET 8
308 #define BIT_DEPTH_CHROMA_OFFSET 8
309 // for log2_max_frame_num_minus4
310 #define LOG2_MAX_FRAME_NUM_OFFSET 4
311 // for log2_max_pic_order_cnt_lsb_minus4
312 #define LOG2_MAX_PIC_ORDER_CNT_LSB_OFFSET 4
313 // for pic_width_in_mbs_minus1
314 #define PIC_WIDTH_IN_MBS_OFFSET 1
315 // for pic_height_in_map_units_minus1
316 #define PIC_HEIGHT_IN_MAP_UNITS_OFFSET 1
317 // for bit_depth_aux_minus8
318 #define BIT_DEPTH_AUX_OFFSET 8
319 // for num_slice_groups_minus1
320 #define NUM_SLICE_GROUPS_OFFSET 1
321 // for run_length_minus1
322 #define RUN_LENGTH_OFFSET 1
323 // for slice_group_change_rate_minus1
324 #define SLICE_GROUP_CHANGE_RATE_OFFSET 1
325 // for pic_size_in_map_units_minus1
326 #define PIC_SIZE_IN_MAP_UNITS_OFFSET 1
327 // for num_ref_idx_l0_default_active_minus1 and num_ref_idx_l1_default_active_minus1
328 #define NUM_REF_IDX_L0_DEFAULT_ACTIVE_OFFSET 1
329 #define NUM_REF_IDX_L1_DEFAULT_ACTIVE_OFFSET 1
330 // for pic_init_qp_minus26 and pic_init_qs_minus26
331 #define PIC_INIT_QP_OFFSET 26
332 #define PIC_INIT_QS_OFFSET 26
333 // for num_ref_idx_l0_active_minus1 and num_ref_idx_l1_active_minus1
334 #define NUM_REF_IDX_L0_ACTIVE_OFFSET 1
335 #define NUM_REF_IDX_L1_ACTIVE_OFFSET 1
336
337 // From Level 5.2
338 #define MAX_MB_SIZE 36864
339 // for aspect_ratio_idc
340 #define EXTENDED_SAR 255
341
342 } // namespace WelsDec
343
344 #endif//WELS_EXPONENTIAL_GOLOMB_ENTROPY_CODING_H__
345