1 /******************************************************************************
2 *
3 * Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 /**
19 ******************************************************************************
20 * @file ihevcd_cabac.c
21 *
22 * @brief
23 * This file contains function definitions related to CABAC parsing
24 *
25 * @author
26 * Ittiam
27 *
28 *
29 * List of Functions
30 *
31 * ihevcd_cabac_init()
32 * ihevcd_cabac_decode_bin()
33 * ihevcd_cabac_decode_bypass_bin()
34 * ihevcd_cabac_decode_bypass_bins_tunary()
35 * ihevcd_cabac_decode_terminate()
36 * ihevcd_cabac_decode_bin_tunary()
37 * ihevcd_cabac_decode_bypass_bins()
38 * ihevcd_cabac_decode_bypass_bins_egk()
39 * ihevcd_cabac_decode_trunc_rice()
40 * ihevcd_cabac_flush()
41 *
42 ******************************************************************************
43 */
44
45 /*****************************************************************************/
46 /* File Includes */
47 /*****************************************************************************/
48 #include <stdio.h>
49 #include <stddef.h>
50 #include <assert.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "ihevc_typedefs.h"
55 #include "iv.h"
56 #include "ivd.h"
57 #include "ihevcd_cxa.h"
58
59
60 #include "ihevc_debug.h"
61 #include "ihevc_macros.h"
62 #include "ihevc_platform_macros.h"
63 #include "ihevc_cabac_tables.h"
64 #include "ihevc_defs.h"
65 #include "ihevc_structs.h"
66 #include "ihevc_cabac_tables.h"
67
68
69 #include "ihevcd_defs.h"
70 #include "ihevcd_function_selector.h"
71 #include "ihevcd_structs.h"
72 #include "ihevcd_error.h"
73 #include "ihevcd_bitstream.h"
74 #include "ihevcd_cabac.h"
75 #include "ihevcd_trace.h"
76
77 #ifdef TRACE
78 extern trace_t g_trace;
79 #endif
80 #if DEBUG_CABAC_RANGE_OFST
81 #if FULLRANGE
82 #define DEBUG_RANGE_OFST(str, m_range, m_ofst ) \
83 {\
84 UWORD32 m_clz, m_range_shift, m_ofst_shift; \
85 m_clz = CLZ(m_range); \
86 m_clz -= (32 - RANGE_NUMBITS); \
87 m_range_shift = m_range << m_clz; \
88 m_range_shift = m_range_shift >> RANGE_SHIFT; \
89 m_ofst_shift = m_ofst << m_clz; \
90 m_ofst_shift = m_ofst_shift >> RANGE_SHIFT; \
91 fprintf( g_trace.fp, "%-40s R: %3d O: %3d\n", str, m_range_shift, m_ofst_shift); \
92 }
93
94 #else
95 #define DEBUG_RANGE_OFST(str, m_range, m_ofst) \
96 fprintf( g_trace.fp, "%-40s R: %3d O: %3d\n", str, m_range, m_ofst);
97 #endif
98 #else
99 #define DEBUG_RANGE_OFST(str, m_range, m_ofst )
100 #endif
101 /*****************************************************************************/
102 /* Function Definitions */
103 /*****************************************************************************/
104
105 /**
106 ******************************************************************************
107 *
108 * @brief Initializes the decoder cabac engine
109 *
110 * @par Description
111 * This routine needs to be called at start of slice/frame decode
112 *
113 * @param[in,out] ps_cabac_ctxt
114 * pointer to cabac context (handle)
115 *
116 * @param[in] ps_bitstrm
117 * pointer to bitstream context (handle)
118 *
119 * @param[in] qp
120 * current slice Qp
121 *
122 * @param[in] cabac_init_idc
123 * current slice init idc (range [0 - 2])
124 *
125 * @param[in] pu1_init_ctxt
126 * Init cabac context to be used (range [0 - 2])
127 *
128 * @return success or failure error code
129 *
130 ******************************************************************************
131 */
ihevcd_cabac_init(cab_ctxt_t * ps_cabac,bitstrm_t * ps_bitstrm,WORD32 qp,WORD32 cabac_init_idc,const UWORD8 * pu1_init_ctxt)132 IHEVCD_ERROR_T ihevcd_cabac_init(cab_ctxt_t *ps_cabac,
133 bitstrm_t *ps_bitstrm,
134 WORD32 qp,
135 WORD32 cabac_init_idc,
136 const UWORD8 *pu1_init_ctxt)
137 {
138 /* Sanity checks */
139 ASSERT(ps_cabac != NULL);
140 ASSERT(ps_bitstrm != NULL);
141 ASSERT((qp >= 0) && (qp < 52));
142 ASSERT((cabac_init_idc >= 0) && (cabac_init_idc < 3));
143 UNUSED(qp);
144 UNUSED(cabac_init_idc);
145 /* CABAC engine uses 32 bit range instead of 9 bits as specified by
146 * the spec. This is done to reduce number of renormalizations
147 */
148 /* cabac engine initialization */
149 #if FULLRANGE
150 ps_cabac->u4_range = (UWORD32)510 << RANGE_SHIFT;
151 BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
152 ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (9 + RANGE_SHIFT));
153
154 #else
155 ps_cabac->u4_range = (UWORD32)510;
156 BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
157 ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, 9);
158
159 #endif
160
161 /* cabac context initialization based on init idc and slice qp */
162 memcpy(ps_cabac->au1_ctxt_models,
163 pu1_init_ctxt,
164 IHEVC_CAB_CTXT_END);
165 DEBUG_RANGE_OFST("init", ps_cabac->u4_range, ps_cabac->u4_ofst);
166
167 /*
168 * If the offset is greater than or equal to range, return fail.
169 */
170 if(ps_cabac->u4_ofst >= ps_cabac->u4_range)
171 {
172 return ((IHEVCD_ERROR_T)IHEVCD_FAIL);
173 }
174
175 return ((IHEVCD_ERROR_T)IHEVCD_SUCCESS);
176 }
177
ihevcd_cabac_reset(cab_ctxt_t * ps_cabac,bitstrm_t * ps_bitstrm)178 IHEVCD_ERROR_T ihevcd_cabac_reset(cab_ctxt_t *ps_cabac,
179 bitstrm_t *ps_bitstrm)
180 {
181 /* Sanity checks */
182 ASSERT(ps_cabac != NULL);
183 ASSERT(ps_bitstrm != NULL);
184
185 /* CABAC engine uses 32 bit range instead of 9 bits as specified by
186 * the spec. This is done to reduce number of renormalizations
187 */
188 /* cabac engine initialization */
189 #if FULLRANGE
190 ps_cabac->u4_range = (UWORD32)510 << RANGE_SHIFT;
191 BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
192 ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (9 + RANGE_SHIFT));
193
194 #else
195 ps_cabac->u4_range = (UWORD32)510;
196 BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
197 ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, 9);
198
199 #endif
200
201 return ((IHEVCD_ERROR_T)IHEVCD_SUCCESS);
202 }
203
204 /**
205 ******************************************************************************
206 *
207 * @brief Decodes a bin based on probablilty and mps packed context model
208 *
209 * @par Description
210 * Decodes a bin as per Section : 9.3.3.2.1 and calls renormalization if required
211 * as per section 9.3.3.2.2
212 * 1. Apart from decoding bin, context model is updated as per state transition
213 * 2. Range and Low renormalization is done based on bin and original state
214 * 3. After renorm bistream is updated (if required)
215 *
216 * @param[in,out] ps_cabac
217 * pointer to cabac context (handle)
218 *
219 * @param[in] ctxt_index
220 * index of cabac context model containing pState[bits6-1] | MPS[bit0]
221 *
222 * @param[in] ps_bitstrm
223 * Bitstream context
224 *
225 * @return bin(boolean) to be decoded
226 *
227 ******************************************************************************
228 */
ihevcd_cabac_decode_bin(cab_ctxt_t * ps_cabac,bitstrm_t * ps_bitstrm,WORD32 ctxt_index)229 UWORD32 ihevcd_cabac_decode_bin(cab_ctxt_t *ps_cabac,
230 bitstrm_t *ps_bitstrm,
231 WORD32 ctxt_index
232
233 )
234 {
235 UWORD32 u4_range = ps_cabac->u4_range;
236 UWORD32 u4_ofst = ps_cabac->u4_ofst;
237 UWORD32 u4_rlps;
238 UWORD32 u4_bin;
239 UWORD8 *pu1_ctxt_model = &ps_cabac->au1_ctxt_models[ctxt_index];
240 WORD32 state_mps = *pu1_ctxt_model;
241 #if FULLRANGE
242 WORD32 clz;
243 #endif
244 UWORD32 u4_qnt_range;
245
246 /* Sanity checks */
247 ASSERT(u4_range >= 256);
248 ASSERT((ctxt_index >= 0) && (ctxt_index < IHEVC_CAB_CTXT_END));
249 ASSERT(state_mps < 128);
250 #if FULLRANGE
251 clz = CLZ(u4_range);
252 clz -= (32 - RANGE_NUMBITS);
253 u4_qnt_range = u4_range << clz;
254 u4_qnt_range = (u4_qnt_range >> (RANGE_SHIFT + 6)) & 0x3;
255 #else
256 u4_qnt_range = (u4_range >> 6) & 0x3;
257 #endif
258 /* Get the lps range from LUT based on quantized range and state */
259 u4_rlps = gau1_ihevc_cabac_rlps[state_mps >> 1][u4_qnt_range];
260 #if FULLRANGE
261 u4_rlps = u4_rlps << (RANGE_SHIFT - clz);
262 #endif
263 u4_range -= u4_rlps;
264
265 u4_bin = state_mps & 1;
266
267 if(u4_ofst >= u4_range)
268 {
269 u4_bin = 1 - u4_bin;
270 u4_ofst -= u4_range;
271 u4_range = u4_rlps;
272 }
273
274 *pu1_ctxt_model = gau1_ihevc_next_state[(state_mps << 1) | u4_bin];
275
276 /*****************************************************************/
277 /* Re-normalization; calculate bits generated based on range(R) */
278 /*****************************************************************/
279 if(u4_range < (1 << 8))
280 {
281 UWORD32 u4_bits;
282 WORD32 numbits;
283 numbits = CLZ(u4_range);
284 numbits -= (32 - RANGE_NUMBITS);
285 #if !FULLRANGE
286 numbits -= RANGE_SHIFT;
287 #endif
288 BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
289 ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbits);
290
291 u4_ofst <<= numbits;
292 u4_ofst |= u4_bits;
293 u4_range <<= numbits;
294
295 }
296 /* Update the cabac context */
297 ps_cabac->u4_range = u4_range;
298 ps_cabac->u4_ofst = u4_ofst;
299 DEBUG_RANGE_OFST("bin", ps_cabac->u4_range, ps_cabac->u4_ofst);
300
301 return (u4_bin);
302
303
304 }
305
306 /**
307 ******************************************************************************
308 *
309 * @brief Decodes a bypass bin (equi-probable 0 / 1)
310 *
311 * @par Description
312 * Decodes a bypss bin as per Section : 9.3.3.2.3
313 *
314 * @param[in,out] ps_cabac
315 * pointer to cabac context (handle)
316 *
317 * @param[in] ps_bitstrm
318 * Bitstream context
319 *
320 * @return Decoded bypass bin
321 *
322 ******************************************************************************
323 */
ihevcd_cabac_decode_bypass_bin(cab_ctxt_t * ps_cabac,bitstrm_t * ps_bitstrm)324 UWORD32 ihevcd_cabac_decode_bypass_bin(cab_ctxt_t *ps_cabac,
325 bitstrm_t *ps_bitstrm)
326 {
327
328 UWORD32 u4_bin;
329 UWORD32 u4_range = ps_cabac->u4_range;
330 UWORD32 u4_ofst = ps_cabac->u4_ofst;
331 UWORD32 u4_bits;
332
333 /* Sanity checks */
334 ASSERT(u4_range >= 256);
335
336 BIT_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
337 ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word);
338
339 u4_ofst <<= 1;
340 u4_ofst |= u4_bits;
341
342 u4_bin = 0;
343 if(u4_ofst >= u4_range)
344 {
345 u4_bin = 1;
346 u4_ofst -= u4_range;
347 }
348
349 /* Update the cabac context */
350 ps_cabac->u4_ofst = u4_ofst;
351 DEBUG_RANGE_OFST("bypass end", ps_cabac->u4_range, ps_cabac->u4_ofst);
352 return (u4_bin);
353 }
354
355 /**
356 ******************************************************************************
357 *
358 * @brief Decodes a terminate bin (1:terminate 0:do not terminate)
359 *
360 * @par Description
361 * Decodes a terminate bin to be called for end_of_slice_flag and pcm_flag
362 * as per Section : 9.3.3.2.4
363 *
364 * @param[in,out] ps_cabac
365 * pointer to cabac context (handle)
366 *
367 * @param[in] ps_bitstrm
368 * Bitstream context
369 *
370 * @return Decoded Bin to indicate whether to terminate or not
371 *
372 ******************************************************************************
373 */
ihevcd_cabac_decode_terminate(cab_ctxt_t * ps_cabac,bitstrm_t * ps_bitstrm)374 UWORD32 ihevcd_cabac_decode_terminate(cab_ctxt_t *ps_cabac,
375 bitstrm_t *ps_bitstrm)
376 {
377 UWORD32 u4_range = ps_cabac->u4_range;
378 UWORD32 u4_ofst = ps_cabac->u4_ofst;
379 UWORD32 u4_bin;
380 #if FULLRANGE
381 WORD32 clz;
382 #endif
383 /* Sanity checks */
384 ASSERT(u4_range >= 256);
385 #if FULLRANGE
386 clz = CLZ(u4_range);
387 clz -= (32 - RANGE_NUMBITS);
388 u4_range -= 2 << (RANGE_SHIFT - clz);
389 #else
390 u4_range -= 2;
391 #endif
392
393 if(u4_ofst >= u4_range)
394 {
395 u4_bin = 1;
396
397 #if FULLRANGE
398 /* In case of FULLRANGE extra bits read earlier need to pushed back to the bitstream */
399 {
400 WORD32 clz;
401 WORD32 numbits;
402 clz = CLZ(ps_cabac->u4_range);
403
404 numbits = (32 - clz);
405 numbits -= 9;
406
407 ihevcd_bits_seek(ps_bitstrm, -numbits);
408 }
409 #endif
410
411 }
412 else
413 {
414 u4_bin = 0;
415 }
416 if(0 == u4_bin)
417 {
418 UWORD32 u4_bits;
419 WORD32 numbits;
420 numbits = CLZ(u4_range);
421 numbits -= (32 - RANGE_NUMBITS);
422 #if !FULLRANGE
423 numbits -= RANGE_SHIFT;
424 #endif
425 /* Renormalize if required */
426 if(numbits)
427 {
428 BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
429 ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbits);
430
431 u4_ofst <<= numbits;
432 u4_ofst |= u4_bits;
433 u4_range <<= numbits;
434 }
435 }
436 /* bits to be inserted in the bitstream */
437 ps_cabac->u4_range = u4_range;
438 ps_cabac->u4_ofst = u4_ofst;
439 DEBUG_RANGE_OFST("term", ps_cabac->u4_range, ps_cabac->u4_ofst);
440
441 return (u4_bin);
442 }
443
444 /**
445 ******************************************************************************
446 *
447 * @brief Decodes a bypass bin (equi-probable 0 / 1)
448 *
449 * @par Description
450 * Decodes a bypss bin as per Section : 9.3.3.2.3
451 *
452 * @param[in,out] ps_cabac
453 * pointer to cabac context (handle)
454 *
455 * @param[in] ps_bitstrm
456 * Bitstream context
457 *
458 * @param[in] numbins
459 * Number of bins to decoded
460 *
461 * @return Decoded bypass bin
462 *
463 * @remarks Tested only for numbins less than 17
464 *
465 ******************************************************************************
466 */
467
ihevcd_cabac_decode_bypass_bins(cab_ctxt_t * ps_cabac,bitstrm_t * ps_bitstrm,WORD32 numbins)468 UWORD32 ihevcd_cabac_decode_bypass_bins(cab_ctxt_t *ps_cabac,
469 bitstrm_t *ps_bitstrm,
470 WORD32 numbins)
471 {
472 UWORD32 u4_bins;
473
474
475 UWORD32 u4_range = ps_cabac->u4_range;
476 UWORD32 u4_ofst = ps_cabac->u4_ofst;
477 UWORD32 u4_bits;
478 ASSERT(u4_range >= 256);
479 ASSERT(numbins > 0);
480
481 /* Sanity checks */
482 ASSERT(numbins < 17);
483
484 u4_bins = 0;
485
486 BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
487 ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbins);
488
489 do
490 {
491 UWORD32 u4_bit;
492 numbins--;
493 u4_bit = (u4_bits >> numbins) & 1;
494 u4_ofst <<= 1;
495 u4_ofst |= u4_bit;
496
497 u4_bins <<= 1;
498 if(u4_ofst >= u4_range)
499 {
500 u4_bins += 1;
501 u4_ofst -= u4_range;
502 }
503 }while(numbins);
504
505 /* Update the cabac context */
506 ps_cabac->u4_ofst = u4_ofst;
507 DEBUG_RANGE_OFST("bypass", ps_cabac->u4_range, ps_cabac->u4_ofst);
508 return (u4_bins);
509 }
510
511 /**
512 ******************************************************************************
513 *
514 * @brief Decodes a truncated unary symbol associated with context model(s)
515 *
516 * @par Description
517 * Decodes symbols coded with TUnary binarization as per sec 9.3.2.2
518 * This is used for computing symbols like qp_delta,
519 * last_sig_coeff_prefix_x, last_sig_coeff_prefix_y.
520 *
521 * The context models associated with each bin is computed as :
522 * current bin context = "base context idx" + (bin_idx >> shift)
523 * where
524 * 1. "base context idx" is the base index for the syntax element
525 * 2. "bin_idx" is the current bin index of the unary code
526 * 3. "shift" is the shift factor associated with this syntax element
527 *
528 * @param[in,out] ps_cabac
529 * pointer to cabac context (handle)
530 *
531 * @param[in] ps_bitstrm
532 * Bitstream context
533 *
534 * @param[in] c_max
535 * maximum value of sym (required for tunary binarization)
536 *
537 * @param[in] ctxt_index
538 * base context model index for this syntax element
539 *
540 * @param[in] ctxt_shift
541 * shift factor for context increments associated with this syntax element
542 *
543 * @param[in] ctxt_inc_max
544 * max value of context increment beyond which all bins will use same ctxt
545 *
546 * @return syntax element decoded
547 *
548 ******************************************************************************
549 */
ihevcd_cabac_decode_bins_tunary(cab_ctxt_t * ps_cabac,bitstrm_t * ps_bitstrm,WORD32 c_max,WORD32 ctxt_index,WORD32 ctxt_shift,WORD32 ctxt_inc_max)550 UWORD32 ihevcd_cabac_decode_bins_tunary(cab_ctxt_t *ps_cabac,
551 bitstrm_t *ps_bitstrm,
552 WORD32 c_max,
553 WORD32 ctxt_index,
554 WORD32 ctxt_shift,
555 WORD32 ctxt_inc_max)
556 {
557 UWORD32 u4_sym;
558 WORD32 bin;
559
560 /* Sanity checks */
561 ASSERT(c_max > 0);
562 ASSERT((ctxt_index >= 0) && (ctxt_index < IHEVC_CAB_CTXT_END));
563 ASSERT((ctxt_index + (c_max >> ctxt_shift)) < IHEVC_CAB_CTXT_END);
564
565 u4_sym = 0;
566 do
567 {
568 WORD32 bin_index;
569 bin_index = ctxt_index + MIN((u4_sym >> ctxt_shift), (UWORD32)ctxt_inc_max);
570 IHEVCD_CABAC_DECODE_BIN(bin, ps_cabac, ps_bitstrm, bin_index);
571 u4_sym++;
572 }while(((WORD32)u4_sym < c_max) && bin);
573
574 u4_sym = u4_sym - 1 + bin;
575
576 return (u4_sym);
577 }
578
579 /**
580 ******************************************************************************
581 *
582 * @brief Decodes a syntax element as truncated unary bypass bins
583 *
584 * @par Description
585 * Decodes symbols coded with TUnary binarization as per sec 9.3.2.2
586 * These symbols are coded as bypass bins
587 * This is used for computing symbols like merge_idx,
588 * mpm_idx etc
589 *
590 * @param[in,out]ps_cabac
591 * pointer to cabac context (handle)
592 *
593 * @param[in] ps_bitstrm
594 * Bitstream context
595 *
596 * @param[in] c_max
597 * maximum value of sym (required for tunary binarization)
598 *
599 * @return syntax element decoded
600 *
601 ******************************************************************************
602 */
ihevcd_cabac_decode_bypass_bins_tunary(cab_ctxt_t * ps_cabac,bitstrm_t * ps_bitstrm,WORD32 c_max)603 UWORD32 ihevcd_cabac_decode_bypass_bins_tunary(cab_ctxt_t *ps_cabac,
604 bitstrm_t *ps_bitstrm,
605 WORD32 c_max)
606 {
607
608 UWORD32 u4_sym;
609 WORD32 bin;
610 UWORD32 u4_ofst = ps_cabac->u4_ofst;
611 UWORD32 u4_range = ps_cabac->u4_range;
612 UWORD32 u4_bits;
613 /* Sanity checks */
614 ASSERT(c_max > 0);
615 ASSERT(u4_range >= 256);
616 u4_sym = 0;
617 BITS_NXT(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
618 ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (UWORD32)c_max);
619 u4_bits <<= (32 - c_max);
620 do
621 {
622 u4_ofst <<= 1;
623 u4_ofst |= (u4_bits >> 31);
624 u4_bits <<= 1;
625
626 bin = 0;
627 if(u4_ofst >= u4_range)
628 {
629 bin = 1;
630 u4_ofst -= u4_range;
631 }
632 u4_sym++;
633 }while(((WORD32)u4_sym < c_max) && bin);
634 BITS_FLUSH(ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
635 ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, u4_sym);
636
637 u4_sym = u4_sym - 1 + bin;
638 /* Update the cabac context */
639 ps_cabac->u4_ofst = u4_ofst;
640
641 return (u4_sym);
642 }
643
644 /**
645 ******************************************************************************
646 *
647 * @brief Decodes a syntax element as kth order Exp-Golomb code (EGK)
648 *
649 * @par Description
650 * Decodes a syntax element binarized as kth order Exp-Golomb code (EGK)
651 * Elements are coded as bypass bins
652 *
653 * @param[in,out] ps_cabac
654 * pointer to cabac context (handle)
655 *
656 * @param[in] u4_sym
657 * syntax element to be coded as EGK
658 *
659 * @param[in] k
660 * order of EGk
661 *
662 * @return success or failure error code
663 *
664 ******************************************************************************
665 */
ihevcd_cabac_decode_bypass_bins_egk(cab_ctxt_t * ps_cabac,bitstrm_t * ps_bitstrm,WORD32 k)666 UWORD32 ihevcd_cabac_decode_bypass_bins_egk(cab_ctxt_t *ps_cabac,
667 bitstrm_t *ps_bitstrm,
668 WORD32 k)
669 {
670
671 UWORD32 u4_sym;
672 WORD32 numones;
673 WORD32 bin;
674
675 /* Sanity checks */
676 ASSERT((k >= 0));
677
678 numones = k;
679 bin = 1;
680 u4_sym = 0;
681 while(bin && (numones <= 16))
682 {
683 IHEVCD_CABAC_DECODE_BYPASS_BIN(bin, ps_cabac, ps_bitstrm);
684 u4_sym += bin << numones++;
685 }
686
687 numones -= 1;
688
689 if(numones)
690 {
691 UWORD32 u4_suffix;
692
693 IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_suffix, ps_cabac, ps_bitstrm, numones);
694 u4_sym += u4_suffix;
695 }
696 return (u4_sym);
697 }
698
699 /**
700 ******************************************************************************
701 *
702 * @brief Decodes a syntax element as truncated rice code (TR)
703 *
704 * @par Description
705 * Decodes a syntax element as truncated rice code (TR)
706 * Elements are coded as bypass bins
707 * This function ise used for coeff_abs_level_remaining coding when
708 * level is less than c_rice_max
709 *
710 * @param[in,out] ps_cabac
711 * pointer to cabac context (handle)
712 *
713 * @param[in] u4_sym
714 * syntax element to be coded as truncated rice code
715 *
716 * @param[in] c_rice_param
717 * shift factor for truncated unary prefix coding of (u4_sym >> c_rice_param)
718 *
719 * @param[in] c_rice_max
720 * max symbol val below which a suffix is coded as (u4_sym%(1<<c_rice_param))
721 * This is currently (4 << c_rice_param) for coeff_abs_level_remaining
722 *
723 * @return success or failure error code
724 *
725 ******************************************************************************
726 */
ihevcd_cabac_decode_bypass_bins_trunc_rice(cab_ctxt_t * ps_cabac,bitstrm_t * ps_bitstrm,WORD32 c_rice_param,WORD32 c_rice_max)727 UWORD32 ihevcd_cabac_decode_bypass_bins_trunc_rice(cab_ctxt_t *ps_cabac,
728 bitstrm_t *ps_bitstrm,
729 WORD32 c_rice_param,
730 WORD32 c_rice_max)
731 {
732 UWORD32 u4_sym;
733 WORD32 bin;
734 WORD32 c_max;
735 UWORD32 u4_suffix;
736 /* Sanity checks */
737 ASSERT((c_rice_param >= 0));
738
739
740 /* Decode prefix coded as TUnary */
741 c_max = c_rice_max >> c_rice_param;
742 u4_sym = 0;
743 do
744 {
745 IHEVCD_CABAC_DECODE_BYPASS_BIN(bin, ps_cabac, ps_bitstrm);
746 u4_sym++;
747
748 }while(((WORD32)u4_sym < c_max) && bin);
749 u4_sym = u4_sym - 1 + bin;
750
751 /* If suffix is present, then decode c_rice_param number of bins */
752 if(c_rice_param)
753 {
754 IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_suffix, ps_cabac, ps_bitstrm, c_rice_param);
755
756 u4_sym = (u4_sym << c_rice_param) | u4_suffix;
757 }
758 return (u4_sym);
759 }
760