Coverage Report

Created: 2026-04-01 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/decoder/ihevcd_cabac.c
Line
Count
Source
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
 */
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
#ifdef ENABLE_MAIN_REXT_PROFILE
138
                                 ,
139
                                 const WORD32 *pi4_rice_stat_coeff
140
#endif
141
                                 )
142
61.5k
{
143
    /* Sanity checks */
144
61.5k
    ASSERT(ps_cabac != NULL);
145
61.5k
    ASSERT(ps_bitstrm != NULL);
146
61.5k
    ASSERT((qp >= 0) && (qp < 52));
147
61.5k
    ASSERT((cabac_init_idc >= 0) && (cabac_init_idc < 3));
148
61.5k
    UNUSED(qp);
149
61.5k
    UNUSED(cabac_init_idc);
150
    /* CABAC engine uses 32 bit range instead of 9 bits as specified by
151
     * the spec. This is done to reduce number of renormalizations
152
     */
153
    /* cabac engine initialization */
154
61.5k
#if FULLRANGE
155
61.5k
    ps_cabac->u4_range = (UWORD32)510 << RANGE_SHIFT;
156
61.5k
    BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
157
61.5k
                    ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (9 + RANGE_SHIFT));
158
159
#else
160
    ps_cabac->u4_range = (UWORD32)510;
161
    BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
162
                    ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, 9);
163
164
#endif
165
166
    /* cabac context initialization based on init idc and slice qp */
167
61.5k
    memcpy(ps_cabac->au1_ctxt_models,
168
61.5k
           pu1_init_ctxt,
169
61.5k
           IHEVC_CAB_CTXT_END);
170
171
61.5k
#ifdef ENABLE_MAIN_REXT_PROFILE
172
    /* golomb rice statistics */
173
61.5k
    if(pi4_rice_stat_coeff)
174
0
    {
175
0
        memcpy(ps_cabac->ai4_rice_stat_coeff, pi4_rice_stat_coeff,
176
0
               sizeof(ps_cabac->ai4_rice_stat_coeff));
177
0
    }
178
61.5k
#endif
179
180
61.5k
    DEBUG_RANGE_OFST("init", ps_cabac->u4_range, ps_cabac->u4_ofst);
181
182
    /*
183
     * If the offset is greater than or equal to range, return fail.
184
     */
185
61.5k
    if(ps_cabac->u4_ofst >= ps_cabac->u4_range)
186
802
    {
187
802
        return ((IHEVCD_ERROR_T)IHEVCD_FAIL);
188
802
    }
189
190
60.7k
    return ((IHEVCD_ERROR_T)IHEVCD_SUCCESS);
191
61.5k
}
192
193
IHEVCD_ERROR_T ihevcd_cabac_reset(cab_ctxt_t *ps_cabac,
194
                                  bitstrm_t *ps_bitstrm)
195
4.94k
{
196
    /* Sanity checks */
197
4.94k
    ASSERT(ps_cabac != NULL);
198
4.94k
    ASSERT(ps_bitstrm != NULL);
199
200
    /* CABAC engine uses 32 bit range instead of 9 bits as specified by
201
     * the spec. This is done to reduce number of renormalizations
202
     */
203
    /* cabac engine initialization */
204
4.94k
#if FULLRANGE
205
4.94k
    ps_cabac->u4_range = (UWORD32)510 << RANGE_SHIFT;
206
4.94k
    BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
207
4.94k
                    ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (9 + RANGE_SHIFT));
208
209
#else
210
    ps_cabac->u4_range = (UWORD32)510;
211
    BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
212
                    ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, 9);
213
214
#endif
215
216
4.94k
    return ((IHEVCD_ERROR_T)IHEVCD_SUCCESS);
217
4.94k
}
218
219
/**
220
 ******************************************************************************
221
 *
222
 *  @brief Decodes a bin based on probablilty and mps packed context model
223
 *
224
 *  @par   Description
225
 *  Decodes a bin as per Section : 9.3.3.2.1 and calls renormalization if required
226
 *  as per section 9.3.3.2.2
227
 *  1. Apart from decoding bin, context model is updated as per state transition
228
 *  2. Range and Low renormalization is done based on bin and original state
229
 *  3. After renorm bistream is updated (if required)
230
 *
231
 *  @param[in,out]   ps_cabac
232
 *  pointer to cabac context (handle)
233
 *
234
 *  @param[in]   ctxt_index
235
 *  index of cabac context model containing pState[bits6-1] | MPS[bit0]
236
 *
237
 *  @param[in]   ps_bitstrm
238
 *  Bitstream context
239
 *
240
 *  @return      bin(boolean) to be decoded
241
 *
242
 ******************************************************************************
243
 */
244
UWORD32 ihevcd_cabac_decode_bin(cab_ctxt_t *ps_cabac,
245
                                bitstrm_t *ps_bitstrm,
246
                                WORD32 ctxt_index
247
248
                               )
249
43.9M
{
250
43.9M
    UWORD32 u4_range = ps_cabac->u4_range;
251
43.9M
    UWORD32 u4_ofst = ps_cabac->u4_ofst;
252
43.9M
    UWORD32 u4_rlps;
253
43.9M
    UWORD32 u4_bin;
254
43.9M
    UWORD8 *pu1_ctxt_model = &ps_cabac->au1_ctxt_models[ctxt_index];
255
43.9M
    WORD32 state_mps = *pu1_ctxt_model;
256
43.9M
#if FULLRANGE
257
43.9M
    WORD32 clz;
258
43.9M
#endif
259
43.9M
    UWORD32 u4_qnt_range;
260
261
    /* Sanity checks */
262
43.9M
    ASSERT(u4_range >= 256);
263
43.9M
    ASSERT((ctxt_index >= 0) && (ctxt_index < IHEVC_CAB_CTXT_END));
264
43.9M
    ASSERT(state_mps < 128);
265
43.9M
#if FULLRANGE
266
43.9M
    clz = CLZ(u4_range);
267
43.9M
    clz -= (32 - RANGE_NUMBITS);
268
43.9M
    u4_qnt_range = u4_range << clz;
269
43.9M
    u4_qnt_range = (u4_qnt_range >> (RANGE_SHIFT + 6)) & 0x3;
270
#else
271
    u4_qnt_range = (u4_range >> 6) & 0x3;
272
#endif
273
    /* Get the lps range from LUT based on quantized range and state */
274
43.9M
    u4_rlps = gau1_ihevc_cabac_rlps[state_mps >> 1][u4_qnt_range];
275
43.9M
#if FULLRANGE
276
43.9M
    u4_rlps = u4_rlps << (RANGE_SHIFT - clz);
277
43.9M
#endif
278
43.9M
    u4_range -= u4_rlps;
279
280
43.9M
    u4_bin = state_mps & 1;
281
282
43.9M
    if(u4_ofst >= u4_range)
283
10.2M
    {
284
10.2M
        u4_bin = 1 - u4_bin;
285
10.2M
        u4_ofst -= u4_range;
286
10.2M
        u4_range = u4_rlps;
287
10.2M
    }
288
289
43.9M
    *pu1_ctxt_model = gau1_ihevc_next_state[(state_mps << 1) | u4_bin];
290
291
    /*****************************************************************/
292
    /* Re-normalization; calculate bits generated based on range(R)  */
293
    /*****************************************************************/
294
43.9M
    if(u4_range < (1 << 8))
295
1.04M
    {
296
1.04M
        UWORD32 u4_bits;
297
1.04M
        WORD32 numbits;
298
1.04M
        numbits = CLZ(u4_range);
299
1.04M
        numbits -= (32 - RANGE_NUMBITS);
300
#if !FULLRANGE
301
        numbits -= RANGE_SHIFT;
302
#endif
303
1.04M
        BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
304
1.04M
                 ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbits);
305
306
1.04M
        u4_ofst <<= numbits;
307
1.04M
        u4_ofst |= u4_bits;
308
1.04M
        u4_range <<= numbits;
309
310
1.04M
    }
311
    /* Update the cabac context */
312
43.9M
    ps_cabac->u4_range = u4_range;
313
43.9M
    ps_cabac->u4_ofst = u4_ofst;
314
43.9M
    DEBUG_RANGE_OFST("bin", ps_cabac->u4_range, ps_cabac->u4_ofst);
315
316
43.9M
    return (u4_bin);
317
318
319
43.9M
}
320
321
/**
322
 ******************************************************************************
323
 *
324
 *  @brief Decodes a bypass bin (equi-probable 0 / 1)
325
 *
326
 *  @par   Description
327
 *  Decodes a bypss bin as per Section : 9.3.3.2.3
328
 *
329
 *  @param[in,out]  ps_cabac
330
 *  pointer to cabac context (handle)
331
 *
332
 *  @param[in]   ps_bitstrm
333
 *  Bitstream context
334
 *
335
 *  @return      Decoded bypass bin
336
 *
337
 ******************************************************************************
338
 */
339
UWORD32 ihevcd_cabac_decode_bypass_bin(cab_ctxt_t *ps_cabac,
340
                                       bitstrm_t *ps_bitstrm)
341
2.50M
{
342
343
2.50M
    UWORD32 u4_bin;
344
2.50M
    UWORD32 u4_range = ps_cabac->u4_range;
345
2.50M
    UWORD32 u4_ofst = ps_cabac->u4_ofst;
346
2.50M
    UWORD32 u4_bits;
347
348
    /* Sanity checks */
349
2.50M
    ASSERT(u4_range >= 256);
350
351
2.50M
    BIT_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
352
2.50M
            ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word);
353
354
2.50M
    u4_ofst <<= 1;
355
2.50M
    u4_ofst |= u4_bits;
356
357
2.50M
    u4_bin = 0;
358
2.50M
    if(u4_ofst >= u4_range)
359
1.23M
    {
360
1.23M
        u4_bin = 1;
361
1.23M
        u4_ofst -= u4_range;
362
1.23M
    }
363
364
    /* Update the cabac context */
365
2.50M
    ps_cabac->u4_ofst = u4_ofst;
366
2.50M
    DEBUG_RANGE_OFST("bypass end", ps_cabac->u4_range, ps_cabac->u4_ofst);
367
2.50M
    return (u4_bin);
368
2.50M
}
369
370
/**
371
 ******************************************************************************
372
 *
373
 *  @brief Decodes a terminate bin (1:terminate 0:do not terminate)
374
 *
375
 *  @par   Description
376
 *  Decodes a terminate bin to be called for end_of_slice_flag and pcm_flag
377
 *  as per Section : 9.3.3.2.4
378
 *
379
 *  @param[in,out]  ps_cabac
380
 *  pointer to cabac context (handle)
381
 *
382
 *  @param[in]   ps_bitstrm
383
 *  Bitstream context
384
 *
385
 *  @return    Decoded Bin to indicate whether to terminate or not
386
 *
387
 ******************************************************************************
388
 */
389
UWORD32 ihevcd_cabac_decode_terminate(cab_ctxt_t *ps_cabac,
390
                                      bitstrm_t *ps_bitstrm)
391
867k
{
392
867k
    UWORD32 u4_range = ps_cabac->u4_range;
393
867k
    UWORD32 u4_ofst = ps_cabac->u4_ofst;
394
867k
    UWORD32 u4_bin;
395
867k
#if FULLRANGE
396
867k
    WORD32 clz;
397
867k
#endif
398
    /* Sanity checks */
399
867k
    ASSERT(u4_range >= 256);
400
867k
#if FULLRANGE
401
867k
    clz = CLZ(u4_range);
402
867k
    clz -= (32 - RANGE_NUMBITS);
403
867k
    u4_range -= 2 << (RANGE_SHIFT - clz);
404
#else
405
    u4_range -= 2;
406
#endif
407
408
867k
    if(u4_ofst >= u4_range)
409
21.2k
    {
410
21.2k
        u4_bin = 1;
411
412
21.2k
#if FULLRANGE
413
        /* In case of FULLRANGE extra bits read earlier need to pushed back to the bitstream */
414
21.2k
        {
415
21.2k
            WORD32 clz;
416
21.2k
            WORD32 numbits;
417
21.2k
            clz = CLZ(ps_cabac->u4_range);
418
419
21.2k
            numbits = (32 - clz);
420
21.2k
            numbits -= 9;
421
422
21.2k
            ihevcd_bits_seek(ps_bitstrm, -numbits);
423
21.2k
        }
424
21.2k
#endif
425
426
21.2k
    }
427
846k
    else
428
846k
    {
429
846k
        u4_bin = 0;
430
846k
    }
431
867k
    if(0 == u4_bin)
432
846k
    {
433
846k
        UWORD32 u4_bits;
434
846k
        WORD32 numbits;
435
846k
        numbits = CLZ(u4_range);
436
846k
        numbits -= (32 - RANGE_NUMBITS);
437
#if !FULLRANGE
438
        numbits -= RANGE_SHIFT;
439
#endif
440
        /* Renormalize if required */
441
846k
        if(numbits)
442
636k
        {
443
636k
            BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
444
636k
                     ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbits);
445
446
636k
            u4_ofst <<= numbits;
447
636k
            u4_ofst |= u4_bits;
448
636k
            u4_range <<= numbits;
449
636k
        }
450
846k
    }
451
    /* bits to be inserted in the bitstream */
452
867k
    ps_cabac->u4_range = u4_range;
453
867k
    ps_cabac->u4_ofst = u4_ofst;
454
867k
    DEBUG_RANGE_OFST("term", ps_cabac->u4_range, ps_cabac->u4_ofst);
455
456
867k
    return (u4_bin);
457
867k
}
458
459
/**
460
 ******************************************************************************
461
 *
462
 *  @brief Decodes a bypass bin (equi-probable 0 / 1)
463
 *
464
 *  @par   Description
465
 *  Decodes a bypss bin as per Section : 9.3.3.2.3
466
 *
467
 *  @param[in,out]  ps_cabac
468
 *  pointer to cabac context (handle)
469
 *
470
 *  @param[in]   ps_bitstrm
471
 *  Bitstream context
472
 *
473
 *  @param[in]   numbins
474
 *  Number of bins to decoded
475
 *
476
 *  @return      Decoded bypass bin
477
 *
478
 *  @remarks     Tested only for numbins less than 17
479
 *
480
 ******************************************************************************
481
 */
482
483
UWORD32 ihevcd_cabac_decode_bypass_bins(cab_ctxt_t *ps_cabac,
484
                                        bitstrm_t *ps_bitstrm,
485
                                        WORD32 numbins)
486
2.33M
{
487
2.33M
    UWORD32 u4_bins;
488
489
490
2.33M
    UWORD32 u4_range = ps_cabac->u4_range;
491
2.33M
    UWORD32 u4_ofst = ps_cabac->u4_ofst;
492
2.33M
    UWORD32 u4_bits;
493
2.33M
    ASSERT(u4_range >= 256);
494
2.33M
    ASSERT(numbins > 0);
495
496
    /* Sanity checks */
497
2.33M
    ASSERT(numbins < 17);
498
499
2.33M
    u4_bins = 0;
500
501
2.33M
    BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
502
2.33M
                    ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbins);
503
504
2.33M
    do
505
7.94M
    {
506
7.94M
        UWORD32 u4_bit;
507
7.94M
        numbins--;
508
7.94M
        u4_bit = (u4_bits >> numbins) & 1;
509
7.94M
        u4_ofst <<= 1;
510
7.94M
        u4_ofst |= u4_bit;
511
512
7.94M
        u4_bins <<= 1;
513
7.94M
        if(u4_ofst >= u4_range)
514
3.24M
        {
515
3.24M
            u4_bins += 1;
516
3.24M
            u4_ofst -= u4_range;
517
3.24M
        }
518
7.94M
    }while(numbins);
519
520
    /* Update the cabac context */
521
2.33M
    ps_cabac->u4_ofst = u4_ofst;
522
2.33M
    DEBUG_RANGE_OFST("bypass", ps_cabac->u4_range, ps_cabac->u4_ofst);
523
2.33M
    return (u4_bins);
524
2.33M
}
525
526
/**
527
 ******************************************************************************
528
 *
529
 *  @brief Decodes a truncated unary symbol associated with context model(s)
530
 *
531
 *  @par   Description
532
 *  Decodes symbols coded with TUnary binarization as per sec 9.3.2.2
533
 *  This is used for computing symbols like qp_delta,
534
 *  last_sig_coeff_prefix_x, last_sig_coeff_prefix_y.
535
 *
536
 *  The context models associated with each bin is computed as :
537
 *   current bin context = "base context idx" + (bin_idx >> shift)
538
 *  where
539
 *   1. "base context idx" is the base index for the syntax element
540
 *   2. "bin_idx" is the current bin index of the unary code
541
 *   3. "shift" is the shift factor associated with this syntax element
542
 *
543
 *  @param[in,out] ps_cabac
544
 *   pointer to cabac context (handle)
545
 *
546
 *  @param[in]   ps_bitstrm
547
 *  Bitstream context
548
 *
549
 *  @param[in]   c_max
550
 *   maximum value of sym (required for tunary binarization)
551
 *
552
 *  @param[in]   ctxt_index
553
 *   base context model index for this syntax element
554
 *
555
 *  @param[in]   ctxt_shift
556
 *   shift factor for context increments associated with this syntax element
557
 *
558
 *  @param[in]   ctxt_inc_max
559
 *   max value of context increment beyond which all bins will use same ctxt
560
 *
561
 *  @return     syntax element decoded
562
 *
563
 ******************************************************************************
564
 */
565
UWORD32 ihevcd_cabac_decode_bins_tunary(cab_ctxt_t *ps_cabac,
566
                                        bitstrm_t *ps_bitstrm,
567
                                        WORD32 c_max,
568
                                        WORD32 ctxt_index,
569
                                        WORD32 ctxt_shift,
570
                                        WORD32 ctxt_inc_max)
571
6.74M
{
572
6.74M
    UWORD32 u4_sym;
573
6.74M
    WORD32 bin;
574
575
    /* Sanity checks */
576
6.74M
    ASSERT(c_max > 0);
577
6.74M
    ASSERT((ctxt_index >= 0) && (ctxt_index < IHEVC_CAB_CTXT_END));
578
6.74M
    ASSERT((ctxt_index + (c_max >> ctxt_shift)) < IHEVC_CAB_CTXT_END);
579
580
6.74M
    u4_sym = 0;
581
6.74M
    do
582
15.1M
    {
583
15.1M
        WORD32 bin_index;
584
15.1M
        bin_index = ctxt_index + MIN((u4_sym >> ctxt_shift), (UWORD32)ctxt_inc_max);
585
60.7M
        IHEVCD_CABAC_DECODE_BIN(bin, ps_cabac, ps_bitstrm,  bin_index);
586
60.7M
        u4_sym++;
587
60.7M
    }while(((WORD32)u4_sym < c_max) && bin);
588
589
6.74M
    u4_sym = u4_sym - 1 + bin;
590
591
6.74M
    return (u4_sym);
592
6.74M
}
593
594
/**
595
 ******************************************************************************
596
 *
597
 *  @brief Decodes a syntax element as truncated unary bypass bins
598
 *
599
 *  @par   Description
600
 *  Decodes symbols coded with TUnary binarization as per sec 9.3.2.2
601
 *  These symbols are coded as bypass bins
602
 *   This is used for computing symbols like merge_idx,
603
 *  mpm_idx etc
604
 *
605
 *  @param[in,out]ps_cabac
606
 *   pointer to cabac context (handle)
607
 *
608
 *  @param[in]   ps_bitstrm
609
 *  Bitstream context
610
 *
611
 *  @param[in]   c_max
612
 *   maximum value of sym (required for tunary binarization)
613
 *
614
 *  @return      syntax element decoded
615
 *
616
 ******************************************************************************
617
 */
618
UWORD32 ihevcd_cabac_decode_bypass_bins_tunary(cab_ctxt_t *ps_cabac,
619
                                               bitstrm_t *ps_bitstrm,
620
                                               WORD32 c_max)
621
3.65M
{
622
623
3.65M
    UWORD32 u4_sym;
624
3.65M
    WORD32 bin;
625
3.65M
    UWORD32 u4_ofst = ps_cabac->u4_ofst;
626
3.65M
    UWORD32 u4_range = ps_cabac->u4_range;
627
3.65M
    UWORD32 u4_bits;
628
    /* Sanity checks */
629
3.65M
    ASSERT(c_max > 0);
630
3.65M
    ASSERT(u4_range >= 256);
631
3.65M
    u4_sym = 0;
632
3.65M
    BITS_NXT(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
633
3.65M
                    ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (UWORD32)c_max);
634
3.65M
    u4_bits <<= (32 - c_max);
635
3.65M
    do
636
6.30M
    {
637
6.30M
        u4_ofst <<= 1;
638
6.30M
        u4_ofst |= (u4_bits >> 31);
639
6.30M
        u4_bits <<= 1;
640
641
6.30M
        bin = 0;
642
6.30M
        if(u4_ofst >= u4_range)
643
3.05M
        {
644
3.05M
            bin = 1;
645
3.05M
            u4_ofst -= u4_range;
646
3.05M
        }
647
6.30M
        u4_sym++;
648
6.30M
    }while(((WORD32)u4_sym < c_max) && bin);
649
3.65M
    BITS_FLUSH(ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
650
3.65M
                    ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, u4_sym);
651
652
3.65M
    u4_sym = u4_sym - 1 + bin;
653
    /* Update the cabac context */
654
3.65M
    ps_cabac->u4_ofst = u4_ofst;
655
656
3.65M
    return (u4_sym);
657
3.65M
}
658
659
/**
660
 ******************************************************************************
661
 *
662
 *  @brief Decodes a syntax element as kth order Exp-Golomb code (EGK)
663
 *
664
 *  @par   Description
665
 *  Decodes a syntax element binarized as kth order Exp-Golomb code (EGK)
666
 *  Elements are coded as bypass bins
667
 *
668
 *  @param[in,out] ps_cabac
669
 *   pointer to cabac context (handle)
670
 *
671
 *  @param[in]   u4_sym
672
 *   syntax element to be coded as EGK
673
 *
674
 *  @param[in]   k
675
 *   order of EGk
676
 *
677
 *  @return      success or failure error code
678
 *
679
 ******************************************************************************
680
 */
681
UWORD32 ihevcd_cabac_decode_bypass_bins_egk(cab_ctxt_t *ps_cabac,
682
                                            bitstrm_t *ps_bitstrm,
683
                                            WORD32 k)
684
909k
{
685
686
909k
    UWORD32 u4_sym;
687
909k
    WORD32 numones;
688
909k
    WORD32 bin;
689
690
    /* Sanity checks */
691
909k
    ASSERT((k >= 0));
692
693
909k
    numones = k;
694
909k
    bin = 1;
695
909k
    u4_sym = 0;
696
2.67M
    while(bin && (numones <= 16))
697
1.76M
    {
698
3.52M
        IHEVCD_CABAC_DECODE_BYPASS_BIN(bin, ps_cabac, ps_bitstrm);
699
3.52M
        u4_sym += bin << numones++;
700
3.52M
    }
701
702
909k
    numones -= 1;
703
704
909k
    if(numones)
705
894k
    {
706
894k
        UWORD32 u4_suffix;
707
708
3.57M
        IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_suffix, ps_cabac, ps_bitstrm, numones);
709
3.57M
        u4_sym += u4_suffix;
710
3.57M
    }
711
909k
    return (u4_sym);
712
909k
}
713
714
/**
715
 ******************************************************************************
716
 *
717
 *  @brief Decodes a syntax element as truncated rice code (TR)
718
 *
719
 *  @par   Description
720
 *  Decodes a syntax element as truncated rice code (TR)
721
 *  Elements are coded as bypass bins
722
 *  This function ise used for coeff_abs_level_remaining coding when
723
 *  level is less than c_rice_max
724
 *
725
 *  @param[in,out] ps_cabac
726
 *   pointer to cabac context (handle)
727
 *
728
 *  @param[in]   u4_sym
729
 *   syntax element to be coded as truncated rice code
730
 *
731
 *  @param[in]   c_rice_param
732
 *    shift factor for truncated unary prefix coding of (u4_sym >> c_rice_param)
733
 *
734
 *  @param[in]   c_rice_max
735
 *    max symbol val below which a suffix is coded as (u4_sym%(1<<c_rice_param))
736
 *    This is currently (4 << c_rice_param) for coeff_abs_level_remaining
737
 *
738
 *  @return      success or failure error code
739
 *
740
 ******************************************************************************
741
 */
742
UWORD32 ihevcd_cabac_decode_bypass_bins_trunc_rice(cab_ctxt_t *ps_cabac,
743
                                                   bitstrm_t *ps_bitstrm,
744
                                                   WORD32 c_rice_param,
745
                                                   WORD32 c_rice_max)
746
0
{
747
0
    UWORD32 u4_sym;
748
0
    WORD32 bin;
749
0
    WORD32 c_max;
750
0
    UWORD32 u4_suffix;
751
    /* Sanity checks */
752
0
    ASSERT((c_rice_param >= 0));
753
754
755
    /* Decode prefix coded as TUnary */
756
0
    c_max = c_rice_max >> c_rice_param;
757
0
    u4_sym = 0;
758
0
    do
759
0
    {
760
0
        IHEVCD_CABAC_DECODE_BYPASS_BIN(bin, ps_cabac, ps_bitstrm);
761
0
        u4_sym++;
762
763
0
    }while(((WORD32)u4_sym < c_max) && bin);
764
0
    u4_sym = u4_sym - 1 + bin;
765
766
    /* If suffix is present, then decode c_rice_param number of bins */
767
0
    if(c_rice_param)
768
0
    {
769
0
        IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_suffix, ps_cabac, ps_bitstrm, c_rice_param);
770
771
0
        u4_sym = (u4_sym << c_rice_param) | u4_suffix;
772
0
    }
773
0
    return (u4_sym);
774
0
}