xref: /aosp_15_r20/external/libavc/decoder/ih264d_cabac.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1*495ae853SAndroid Build Coastguard Worker /******************************************************************************
2*495ae853SAndroid Build Coastguard Worker  *
3*495ae853SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
4*495ae853SAndroid Build Coastguard Worker  *
5*495ae853SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
6*495ae853SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
7*495ae853SAndroid Build Coastguard Worker  * You may obtain a copy of the License at:
8*495ae853SAndroid Build Coastguard Worker  *
9*495ae853SAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
10*495ae853SAndroid Build Coastguard Worker  *
11*495ae853SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
12*495ae853SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
13*495ae853SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*495ae853SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
15*495ae853SAndroid Build Coastguard Worker  * limitations under the License.
16*495ae853SAndroid Build Coastguard Worker  *
17*495ae853SAndroid Build Coastguard Worker  *****************************************************************************
18*495ae853SAndroid Build Coastguard Worker  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19*495ae853SAndroid Build Coastguard Worker */
20*495ae853SAndroid Build Coastguard Worker /*!
21*495ae853SAndroid Build Coastguard Worker  ***************************************************************************
22*495ae853SAndroid Build Coastguard Worker  * \file ih264d_cabac.c
23*495ae853SAndroid Build Coastguard Worker  *
24*495ae853SAndroid Build Coastguard Worker  * \brief
25*495ae853SAndroid Build Coastguard Worker  *    This file contains Binary decoding routines.
26*495ae853SAndroid Build Coastguard Worker  *
27*495ae853SAndroid Build Coastguard Worker  * \date
28*495ae853SAndroid Build Coastguard Worker  *    04/02/2003
29*495ae853SAndroid Build Coastguard Worker  *
30*495ae853SAndroid Build Coastguard Worker  * \author  NS
31*495ae853SAndroid Build Coastguard Worker  ***************************************************************************
32*495ae853SAndroid Build Coastguard Worker  */
33*495ae853SAndroid Build Coastguard Worker #include <string.h>
34*495ae853SAndroid Build Coastguard Worker #include "ih264_typedefs.h"
35*495ae853SAndroid Build Coastguard Worker #include "ih264_macros.h"
36*495ae853SAndroid Build Coastguard Worker #include "ih264_platform_macros.h"
37*495ae853SAndroid Build Coastguard Worker #include "ih264d_structs.h"
38*495ae853SAndroid Build Coastguard Worker #include "ih264d_cabac.h"
39*495ae853SAndroid Build Coastguard Worker #include "ih264d_bitstrm.h"
40*495ae853SAndroid Build Coastguard Worker #include "ih264d_error_handler.h"
41*495ae853SAndroid Build Coastguard Worker #include "ih264d_defs.h"
42*495ae853SAndroid Build Coastguard Worker #include "ih264d_debug.h"
43*495ae853SAndroid Build Coastguard Worker #include "ih264d_tables.h"
44*495ae853SAndroid Build Coastguard Worker #include "ih264d_parse_cabac.h"
45*495ae853SAndroid Build Coastguard Worker #include "ih264d_tables.h"
46*495ae853SAndroid Build Coastguard Worker 
47*495ae853SAndroid Build Coastguard Worker 
48*495ae853SAndroid Build Coastguard Worker 
49*495ae853SAndroid Build Coastguard Worker /*!
50*495ae853SAndroid Build Coastguard Worker  **************************************************************************
51*495ae853SAndroid Build Coastguard Worker  * \if Function name : ih264d_init_cabac_dec_envirnoment \endif
52*495ae853SAndroid Build Coastguard Worker  *
53*495ae853SAndroid Build Coastguard Worker  * \brief
54*495ae853SAndroid Build Coastguard Worker  *    This function initializes CABAC decoding envirnoment. This function
55*495ae853SAndroid Build Coastguard Worker  *    implements 9.3.3.2.3.1 of ISO/IEC14496-10.
56*495ae853SAndroid Build Coastguard Worker  *
57*495ae853SAndroid Build Coastguard Worker  * \return
58*495ae853SAndroid Build Coastguard Worker  *    None
59*495ae853SAndroid Build Coastguard Worker  *
60*495ae853SAndroid Build Coastguard Worker  **************************************************************************
61*495ae853SAndroid Build Coastguard Worker  */
ih264d_init_cabac_dec_envirnoment(decoding_envirnoment_t * ps_cab_env,dec_bit_stream_t * ps_bitstrm)62*495ae853SAndroid Build Coastguard Worker WORD32 ih264d_init_cabac_dec_envirnoment(decoding_envirnoment_t * ps_cab_env,
63*495ae853SAndroid Build Coastguard Worker                                        dec_bit_stream_t *ps_bitstrm)
64*495ae853SAndroid Build Coastguard Worker {
65*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_code_int_val_ofst;
66*495ae853SAndroid Build Coastguard Worker 
67*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_range = (HALF - 2) << 23;
68*495ae853SAndroid Build Coastguard Worker     NEXTBITS(u4_code_int_val_ofst, ps_bitstrm->u4_ofst, ps_bitstrm->pu4_buffer,
69*495ae853SAndroid Build Coastguard Worker              32);
70*495ae853SAndroid Build Coastguard Worker     FLUSHBITS(ps_bitstrm->u4_ofst, 9)
71*495ae853SAndroid Build Coastguard Worker 
72*495ae853SAndroid Build Coastguard Worker     if(EXCEED_OFFSET(ps_bitstrm))
73*495ae853SAndroid Build Coastguard Worker         return ERROR_EOB_FLUSHBITS_T;
74*495ae853SAndroid Build Coastguard Worker 
75*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
76*495ae853SAndroid Build Coastguard Worker 
77*495ae853SAndroid Build Coastguard Worker     /*brief description of the design adopted for CABAC*/
78*495ae853SAndroid Build Coastguard Worker     /*according to the standard the u4_code_int_range needs to be initialized 0x 1FE(10 bits) and
79*495ae853SAndroid Build Coastguard Worker      9 bits from the bit stream need to be read and into the u4_code_int_val_ofst.As and when the
80*495ae853SAndroid Build Coastguard Worker      u4_code_int_range becomes less than 10 bits we need to renormalize and read from the bitstream*
81*495ae853SAndroid Build Coastguard Worker 
82*495ae853SAndroid Build Coastguard Worker      In the implemented design
83*495ae853SAndroid Build Coastguard Worker      initially
84*495ae853SAndroid Build Coastguard Worker 
85*495ae853SAndroid Build Coastguard Worker      range_new = range <<23
86*495ae853SAndroid Build Coastguard Worker      valOffset_new = valOffset << 23 + 23 bits(read from the bit stream)
87*495ae853SAndroid Build Coastguard Worker 
88*495ae853SAndroid Build Coastguard Worker      Thus we have read 23 more bits ahead of time.
89*495ae853SAndroid Build Coastguard Worker 
90*495ae853SAndroid Build Coastguard Worker      It can be mathematical proved that even with the modified range and u4_ofst the operations
91*495ae853SAndroid Build Coastguard Worker      like comparison and subtraction needed for a bin decode are still valid(both in the regular case and the bypass case)
92*495ae853SAndroid Build Coastguard Worker 
93*495ae853SAndroid Build Coastguard Worker      As bins are decoded..we consume the bits that we have already read into the valOffset.The clz of Range
94*495ae853SAndroid Build Coastguard Worker      gives us the number of bits we consumed of the 23 bits that we have read ahead of time.
95*495ae853SAndroid Build Coastguard Worker 
96*495ae853SAndroid Build Coastguard Worker      when the number bits we have consumed exceeds 23 ,we renormalize..and  we read from the bitstream again*/
97*495ae853SAndroid Build Coastguard Worker 
98*495ae853SAndroid Build Coastguard Worker RESET_BIN_COUNTS(ps_cab_env)
99*495ae853SAndroid Build Coastguard Worker 
100*495ae853SAndroid Build Coastguard Worker     return OK;
101*495ae853SAndroid Build Coastguard Worker }
102*495ae853SAndroid Build Coastguard Worker 
103*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
104*495ae853SAndroid Build Coastguard Worker /*                                                                           */
105*495ae853SAndroid Build Coastguard Worker /*  Function Name : ih264d_init_cabac_contexts                                      */
106*495ae853SAndroid Build Coastguard Worker /*                                                                           */
107*495ae853SAndroid Build Coastguard Worker /*  Description   : This function initializes the cabac contexts             */
108*495ae853SAndroid Build Coastguard Worker /*                  depending upon slice type and Init_Idc value.            */
109*495ae853SAndroid Build Coastguard Worker /*  Inputs        : ps_dec, slice type                                       */
110*495ae853SAndroid Build Coastguard Worker /*  Globals       : <Does it use any global variables?>                      */
111*495ae853SAndroid Build Coastguard Worker /*  Outputs       :                                                          */
112*495ae853SAndroid Build Coastguard Worker /*  Returns       : void                                                     */
113*495ae853SAndroid Build Coastguard Worker /*                                                                           */
114*495ae853SAndroid Build Coastguard Worker /*  Issues        : none                                                     */
115*495ae853SAndroid Build Coastguard Worker /*                                                                           */
116*495ae853SAndroid Build Coastguard Worker /*  Revision History:                                                        */
117*495ae853SAndroid Build Coastguard Worker /*                                                                           */
118*495ae853SAndroid Build Coastguard Worker /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
119*495ae853SAndroid Build Coastguard Worker /*         03 05 2005   100153)         Draft                                */
120*495ae853SAndroid Build Coastguard Worker /*                                                                           */
121*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
122*495ae853SAndroid Build Coastguard Worker 
ih264d_init_cabac_contexts(UWORD8 u1_slice_type,dec_struct_t * ps_dec)123*495ae853SAndroid Build Coastguard Worker void ih264d_init_cabac_contexts(UWORD8 u1_slice_type, dec_struct_t * ps_dec)
124*495ae853SAndroid Build Coastguard Worker {
125*495ae853SAndroid Build Coastguard Worker 
126*495ae853SAndroid Build Coastguard Worker     bin_ctxt_model_t *p_cabac_ctxt_table_t = ps_dec->p_cabac_ctxt_table_t;
127*495ae853SAndroid Build Coastguard Worker     UWORD8 u1_qp_y = ps_dec->ps_cur_slice->u1_slice_qp;
128*495ae853SAndroid Build Coastguard Worker     UWORD8 u1_cabac_init_Idc = 0;
129*495ae853SAndroid Build Coastguard Worker 
130*495ae853SAndroid Build Coastguard Worker     if(I_SLICE != u1_slice_type)
131*495ae853SAndroid Build Coastguard Worker     {
132*495ae853SAndroid Build Coastguard Worker         u1_cabac_init_Idc = ps_dec->ps_cur_slice->u1_cabac_init_idc;
133*495ae853SAndroid Build Coastguard Worker     }
134*495ae853SAndroid Build Coastguard Worker 
135*495ae853SAndroid Build Coastguard Worker     {
136*495ae853SAndroid Build Coastguard Worker         /* MAKING ps_dec->p_ctxt_inc_mb_map a scratch buffer */
137*495ae853SAndroid Build Coastguard Worker         /* 0th entry of CtxtIncMbMap will be always be containing default values
138*495ae853SAndroid Build Coastguard Worker          for CABAC context representing MB not available */
139*495ae853SAndroid Build Coastguard Worker         ctxt_inc_mb_info_t *p_DefCtxt = ps_dec->p_ctxt_inc_mb_map - 1;
140*495ae853SAndroid Build Coastguard Worker         UWORD8 *pu1_temp;
141*495ae853SAndroid Build Coastguard Worker         WORD8 i;
142*495ae853SAndroid Build Coastguard Worker         p_DefCtxt->u1_mb_type = CAB_SKIP;
143*495ae853SAndroid Build Coastguard Worker 
144*495ae853SAndroid Build Coastguard Worker         p_DefCtxt->u1_cbp = 0x0f;
145*495ae853SAndroid Build Coastguard Worker         p_DefCtxt->u1_intra_chroma_pred_mode = 0;
146*495ae853SAndroid Build Coastguard Worker 
147*495ae853SAndroid Build Coastguard Worker         p_DefCtxt->u1_yuv_dc_csbp = 0x7;
148*495ae853SAndroid Build Coastguard Worker 
149*495ae853SAndroid Build Coastguard Worker         p_DefCtxt->u1_transform8x8_ctxt = 0;
150*495ae853SAndroid Build Coastguard Worker 
151*495ae853SAndroid Build Coastguard Worker         pu1_temp = (UWORD8*)p_DefCtxt->i1_ref_idx;
152*495ae853SAndroid Build Coastguard Worker         for(i = 0; i < 4; i++, pu1_temp++)
153*495ae853SAndroid Build Coastguard Worker             (*pu1_temp) = 0;
154*495ae853SAndroid Build Coastguard Worker         pu1_temp = (UWORD8*)p_DefCtxt->u1_mv;
155*495ae853SAndroid Build Coastguard Worker         for(i = 0; i < 16; i++, pu1_temp++)
156*495ae853SAndroid Build Coastguard Worker             (*pu1_temp) = 0;
157*495ae853SAndroid Build Coastguard Worker         ps_dec->ps_def_ctxt_mb_info = p_DefCtxt;
158*495ae853SAndroid Build Coastguard Worker     }
159*495ae853SAndroid Build Coastguard Worker 
160*495ae853SAndroid Build Coastguard Worker     if(u1_slice_type == I_SLICE)
161*495ae853SAndroid Build Coastguard Worker     {
162*495ae853SAndroid Build Coastguard Worker         u1_cabac_init_Idc = 3;
163*495ae853SAndroid Build Coastguard Worker         ps_dec->p_mb_type_t = p_cabac_ctxt_table_t + MB_TYPE_I_SLICE;
164*495ae853SAndroid Build Coastguard Worker     }
165*495ae853SAndroid Build Coastguard Worker     else if(u1_slice_type == P_SLICE)
166*495ae853SAndroid Build Coastguard Worker     {
167*495ae853SAndroid Build Coastguard Worker         ps_dec->p_mb_type_t = p_cabac_ctxt_table_t + MB_TYPE_P_SLICE;
168*495ae853SAndroid Build Coastguard Worker         ps_dec->p_mb_skip_flag_t = p_cabac_ctxt_table_t + MB_SKIP_FLAG_P_SLICE;
169*495ae853SAndroid Build Coastguard Worker         ps_dec->p_sub_mb_type_t = p_cabac_ctxt_table_t + SUB_MB_TYPE_P_SLICE;
170*495ae853SAndroid Build Coastguard Worker     }
171*495ae853SAndroid Build Coastguard Worker     else if(u1_slice_type == B_SLICE)
172*495ae853SAndroid Build Coastguard Worker     {
173*495ae853SAndroid Build Coastguard Worker         ps_dec->p_mb_type_t = p_cabac_ctxt_table_t + MB_TYPE_B_SLICE;
174*495ae853SAndroid Build Coastguard Worker         ps_dec->p_mb_skip_flag_t = p_cabac_ctxt_table_t + MB_SKIP_FLAG_B_SLICE;
175*495ae853SAndroid Build Coastguard Worker         ps_dec->p_sub_mb_type_t = p_cabac_ctxt_table_t + SUB_MB_TYPE_B_SLICE;
176*495ae853SAndroid Build Coastguard Worker     }
177*495ae853SAndroid Build Coastguard Worker     {
178*495ae853SAndroid Build Coastguard Worker         bin_ctxt_model_t *p_cabac_ctxt_table_t_tmp = p_cabac_ctxt_table_t;
179*495ae853SAndroid Build Coastguard Worker         if(ps_dec->ps_cur_slice->u1_field_pic_flag)
180*495ae853SAndroid Build Coastguard Worker         {
181*495ae853SAndroid Build Coastguard Worker             p_cabac_ctxt_table_t_tmp += SIGNIFICANT_COEFF_FLAG_FLD;
182*495ae853SAndroid Build Coastguard Worker 
183*495ae853SAndroid Build Coastguard Worker         }
184*495ae853SAndroid Build Coastguard Worker         else
185*495ae853SAndroid Build Coastguard Worker         {
186*495ae853SAndroid Build Coastguard Worker             p_cabac_ctxt_table_t_tmp += SIGNIFICANT_COEFF_FLAG_FRAME;
187*495ae853SAndroid Build Coastguard Worker         }
188*495ae853SAndroid Build Coastguard Worker         {
189*495ae853SAndroid Build Coastguard Worker             bin_ctxt_model_t * * p_significant_coeff_flag_t =
190*495ae853SAndroid Build Coastguard Worker                             ps_dec->p_significant_coeff_flag_t;
191*495ae853SAndroid Build Coastguard Worker             p_significant_coeff_flag_t[0] = p_cabac_ctxt_table_t_tmp
192*495ae853SAndroid Build Coastguard Worker                             + SIG_COEFF_CTXT_CAT_0_OFFSET;
193*495ae853SAndroid Build Coastguard Worker             p_significant_coeff_flag_t[1] = p_cabac_ctxt_table_t_tmp
194*495ae853SAndroid Build Coastguard Worker                             + SIG_COEFF_CTXT_CAT_1_OFFSET;
195*495ae853SAndroid Build Coastguard Worker             p_significant_coeff_flag_t[2] = p_cabac_ctxt_table_t_tmp
196*495ae853SAndroid Build Coastguard Worker                             + SIG_COEFF_CTXT_CAT_2_OFFSET;
197*495ae853SAndroid Build Coastguard Worker             p_significant_coeff_flag_t[3] = p_cabac_ctxt_table_t_tmp
198*495ae853SAndroid Build Coastguard Worker                             + SIG_COEFF_CTXT_CAT_3_OFFSET;
199*495ae853SAndroid Build Coastguard Worker             p_significant_coeff_flag_t[4] = p_cabac_ctxt_table_t_tmp
200*495ae853SAndroid Build Coastguard Worker                             + SIG_COEFF_CTXT_CAT_4_OFFSET;
201*495ae853SAndroid Build Coastguard Worker 
202*495ae853SAndroid Build Coastguard Worker             p_significant_coeff_flag_t[5] = p_cabac_ctxt_table_t_tmp
203*495ae853SAndroid Build Coastguard Worker                             + SIG_COEFF_CTXT_CAT_5_OFFSET;
204*495ae853SAndroid Build Coastguard Worker 
205*495ae853SAndroid Build Coastguard Worker         }
206*495ae853SAndroid Build Coastguard Worker     }
207*495ae853SAndroid Build Coastguard Worker 
208*495ae853SAndroid Build Coastguard Worker     memcpy(p_cabac_ctxt_table_t,
209*495ae853SAndroid Build Coastguard Worker            gau1_ih264d_cabac_ctxt_init_table[u1_cabac_init_Idc][u1_qp_y],
210*495ae853SAndroid Build Coastguard Worker            NUM_CABAC_CTXTS * sizeof(bin_ctxt_model_t));
211*495ae853SAndroid Build Coastguard Worker }
212*495ae853SAndroid Build Coastguard Worker /*!
213*495ae853SAndroid Build Coastguard Worker  **************************************************************************
214*495ae853SAndroid Build Coastguard Worker  * \if Function name : ih264d_decode_bin \endif
215*495ae853SAndroid Build Coastguard Worker  *
216*495ae853SAndroid Build Coastguard Worker  * \brief
217*495ae853SAndroid Build Coastguard Worker  *    This function implements decoding process of a decision as defined
218*495ae853SAndroid Build Coastguard Worker  *    in 9.3.3.2.2.
219*495ae853SAndroid Build Coastguard Worker  *
220*495ae853SAndroid Build Coastguard Worker  * \return
221*495ae853SAndroid Build Coastguard Worker  *    Returns symbol decoded.
222*495ae853SAndroid Build Coastguard Worker  *
223*495ae853SAndroid Build Coastguard Worker  * \note
224*495ae853SAndroid Build Coastguard Worker  *    It is specified in 9.3.3.2.3.2 that, one of the input to this function
225*495ae853SAndroid Build Coastguard Worker  *    is CtxIdx. CtxIdx is used to identify state and MPS of that context
226*495ae853SAndroid Build Coastguard Worker  *    (Refer Fig 9.11 - Flowchart for encoding a decision). To suffice that
227*495ae853SAndroid Build Coastguard Worker  *    here we pass a pointer bin_ctxt_model_t which contains these values.
228*495ae853SAndroid Build Coastguard Worker  *
229*495ae853SAndroid Build Coastguard Worker  **************************************************************************
230*495ae853SAndroid Build Coastguard Worker  */
231*495ae853SAndroid Build Coastguard Worker 
ih264d_decode_bin(UWORD32 u4_ctx_inc,bin_ctxt_model_t * ps_src_bin_ctxt,dec_bit_stream_t * ps_bitstrm,decoding_envirnoment_t * ps_cab_env)232*495ae853SAndroid Build Coastguard Worker UWORD32 ih264d_decode_bin(UWORD32 u4_ctx_inc,
233*495ae853SAndroid Build Coastguard Worker                           bin_ctxt_model_t *ps_src_bin_ctxt,
234*495ae853SAndroid Build Coastguard Worker                           dec_bit_stream_t *ps_bitstrm,
235*495ae853SAndroid Build Coastguard Worker                           decoding_envirnoment_t *ps_cab_env)
236*495ae853SAndroid Build Coastguard Worker 
237*495ae853SAndroid Build Coastguard Worker {
238*495ae853SAndroid Build Coastguard Worker 
239*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_qnt_int_range, u4_code_int_range, u4_code_int_val_ofst,
240*495ae853SAndroid Build Coastguard Worker                     u4_int_range_lps;
241*495ae853SAndroid Build Coastguard Worker 
242*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_symbol, u4_mps_state;
243*495ae853SAndroid Build Coastguard Worker 
244*495ae853SAndroid Build Coastguard Worker     bin_ctxt_model_t *ps_bin_ctxt;
245*495ae853SAndroid Build Coastguard Worker 
246*495ae853SAndroid Build Coastguard Worker     UWORD32 table_lookup;
247*495ae853SAndroid Build Coastguard Worker     const UWORD32 *pu4_table = (const UWORD32 *)ps_cab_env->cabac_table;
248*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_clz;
249*495ae853SAndroid Build Coastguard Worker 
250*495ae853SAndroid Build Coastguard Worker     ps_bin_ctxt = ps_src_bin_ctxt + u4_ctx_inc;
251*495ae853SAndroid Build Coastguard Worker 
252*495ae853SAndroid Build Coastguard Worker     u4_code_int_range = ps_cab_env->u4_code_int_range;
253*495ae853SAndroid Build Coastguard Worker     u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
254*495ae853SAndroid Build Coastguard Worker 
255*495ae853SAndroid Build Coastguard Worker     u4_mps_state = (ps_bin_ctxt->u1_mps_state);
256*495ae853SAndroid Build Coastguard Worker     u4_clz = CLZ(u4_code_int_range);
257*495ae853SAndroid Build Coastguard Worker 
258*495ae853SAndroid Build Coastguard Worker     u4_qnt_int_range = u4_code_int_range << u4_clz;
259*495ae853SAndroid Build Coastguard Worker     u4_qnt_int_range = (u4_qnt_int_range >> 29) & 0x3;
260*495ae853SAndroid Build Coastguard Worker 
261*495ae853SAndroid Build Coastguard Worker     table_lookup = pu4_table[(u4_mps_state << 2) + u4_qnt_int_range];
262*495ae853SAndroid Build Coastguard Worker     u4_int_range_lps = table_lookup & 0xff;
263*495ae853SAndroid Build Coastguard Worker 
264*495ae853SAndroid Build Coastguard Worker     u4_int_range_lps = u4_int_range_lps << (23 - u4_clz);
265*495ae853SAndroid Build Coastguard Worker     u4_code_int_range = u4_code_int_range - u4_int_range_lps;
266*495ae853SAndroid Build Coastguard Worker 
267*495ae853SAndroid Build Coastguard Worker     u4_symbol = ((u4_mps_state >> 6) & 0x1);
268*495ae853SAndroid Build Coastguard Worker 
269*495ae853SAndroid Build Coastguard Worker     u4_mps_state = (table_lookup >> 8) & 0x7F;
270*495ae853SAndroid Build Coastguard Worker 
271*495ae853SAndroid Build Coastguard Worker     CHECK_IF_LPS(u4_code_int_range, u4_code_int_val_ofst, u4_symbol,
272*495ae853SAndroid Build Coastguard Worker                  u4_int_range_lps, u4_mps_state, table_lookup)
273*495ae853SAndroid Build Coastguard Worker 
274*495ae853SAndroid Build Coastguard Worker     if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_8)
275*495ae853SAndroid Build Coastguard Worker     {
276*495ae853SAndroid Build Coastguard Worker         UWORD32 *pu4_buffer, u4_offset;
277*495ae853SAndroid Build Coastguard Worker 
278*495ae853SAndroid Build Coastguard Worker         pu4_buffer = ps_bitstrm->pu4_buffer;
279*495ae853SAndroid Build Coastguard Worker         u4_offset = ps_bitstrm->u4_ofst;
280*495ae853SAndroid Build Coastguard Worker 
281*495ae853SAndroid Build Coastguard Worker         RENORM_RANGE_OFFSET(u4_code_int_range, u4_code_int_val_ofst, u4_offset,
282*495ae853SAndroid Build Coastguard Worker                             pu4_buffer)
283*495ae853SAndroid Build Coastguard Worker 
284*495ae853SAndroid Build Coastguard Worker         ps_bitstrm->u4_ofst = u4_offset;
285*495ae853SAndroid Build Coastguard Worker     }
286*495ae853SAndroid Build Coastguard Worker 
287*495ae853SAndroid Build Coastguard Worker     INC_BIN_COUNT(ps_cab_env)
288*495ae853SAndroid Build Coastguard Worker 
289*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
290*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_range = u4_code_int_range;
291*495ae853SAndroid Build Coastguard Worker     ps_bin_ctxt->u1_mps_state = u4_mps_state;
292*495ae853SAndroid Build Coastguard Worker 
293*495ae853SAndroid Build Coastguard Worker     return (u4_symbol);
294*495ae853SAndroid Build Coastguard Worker }
295*495ae853SAndroid Build Coastguard Worker 
296*495ae853SAndroid Build Coastguard Worker /*!
297*495ae853SAndroid Build Coastguard Worker  **************************************************************************
298*495ae853SAndroid Build Coastguard Worker  * \if Function name : ih264d_decode_terminate \endif
299*495ae853SAndroid Build Coastguard Worker  *
300*495ae853SAndroid Build Coastguard Worker  * \brief
301*495ae853SAndroid Build Coastguard Worker  *    This function implements decoding process of a termination as defined
302*495ae853SAndroid Build Coastguard Worker  *    9.3.3.2.2.3 of ISO/IEC14496-10.
303*495ae853SAndroid Build Coastguard Worker  *
304*495ae853SAndroid Build Coastguard Worker  * \return
305*495ae853SAndroid Build Coastguard Worker  *    Returns symbol decoded.
306*495ae853SAndroid Build Coastguard Worker  *
307*495ae853SAndroid Build Coastguard Worker  * \note
308*495ae853SAndroid Build Coastguard Worker  *    This routine is called while decoding "end_of_skice_flag" and of the
309*495ae853SAndroid Build Coastguard Worker  *    bin indicating PCM mode in MBType.
310*495ae853SAndroid Build Coastguard Worker  *
311*495ae853SAndroid Build Coastguard Worker  **************************************************************************
312*495ae853SAndroid Build Coastguard Worker  */
ih264d_decode_terminate(decoding_envirnoment_t * ps_cab_env,dec_bit_stream_t * ps_stream)313*495ae853SAndroid Build Coastguard Worker UWORD8 ih264d_decode_terminate(decoding_envirnoment_t * ps_cab_env,
314*495ae853SAndroid Build Coastguard Worker                                dec_bit_stream_t * ps_stream)
315*495ae853SAndroid Build Coastguard Worker {
316*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_symbol;
317*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_code_int_val_ofst, u4_code_int_range;
318*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_clz;
319*495ae853SAndroid Build Coastguard Worker 
320*495ae853SAndroid Build Coastguard Worker     u4_code_int_range = ps_cab_env->u4_code_int_range;
321*495ae853SAndroid Build Coastguard Worker     u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
322*495ae853SAndroid Build Coastguard Worker 
323*495ae853SAndroid Build Coastguard Worker     u4_clz = CLZ(u4_code_int_range);
324*495ae853SAndroid Build Coastguard Worker     u4_code_int_range -= (2 << (23 - u4_clz));
325*495ae853SAndroid Build Coastguard Worker 
326*495ae853SAndroid Build Coastguard Worker     if(u4_code_int_val_ofst >= u4_code_int_range)
327*495ae853SAndroid Build Coastguard Worker     {
328*495ae853SAndroid Build Coastguard Worker         /* S=1 */
329*495ae853SAndroid Build Coastguard Worker         u4_symbol = 1;
330*495ae853SAndroid Build Coastguard Worker 
331*495ae853SAndroid Build Coastguard Worker         {
332*495ae853SAndroid Build Coastguard Worker 
333*495ae853SAndroid Build Coastguard Worker             /*the u4_ofst needs to be updated before termination*/
334*495ae853SAndroid Build Coastguard Worker             ps_stream->u4_ofst += u4_clz;
335*495ae853SAndroid Build Coastguard Worker 
336*495ae853SAndroid Build Coastguard Worker         }
337*495ae853SAndroid Build Coastguard Worker 
338*495ae853SAndroid Build Coastguard Worker     }
339*495ae853SAndroid Build Coastguard Worker     else
340*495ae853SAndroid Build Coastguard Worker     {
341*495ae853SAndroid Build Coastguard Worker         /* S=0 */
342*495ae853SAndroid Build Coastguard Worker         u4_symbol = 0;
343*495ae853SAndroid Build Coastguard Worker 
344*495ae853SAndroid Build Coastguard Worker         if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_8)
345*495ae853SAndroid Build Coastguard Worker         {
346*495ae853SAndroid Build Coastguard Worker             UWORD32 *pu4_buffer, u4_offset;
347*495ae853SAndroid Build Coastguard Worker 
348*495ae853SAndroid Build Coastguard Worker             pu4_buffer = ps_stream->pu4_buffer;
349*495ae853SAndroid Build Coastguard Worker             u4_offset = ps_stream->u4_ofst;
350*495ae853SAndroid Build Coastguard Worker 
351*495ae853SAndroid Build Coastguard Worker             RENORM_RANGE_OFFSET(u4_code_int_range, u4_code_int_val_ofst, u4_offset,
352*495ae853SAndroid Build Coastguard Worker                                 pu4_buffer)
353*495ae853SAndroid Build Coastguard Worker             ps_stream->u4_ofst = u4_offset;
354*495ae853SAndroid Build Coastguard Worker         }
355*495ae853SAndroid Build Coastguard Worker     }
356*495ae853SAndroid Build Coastguard Worker 
357*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_range = u4_code_int_range;
358*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
359*495ae853SAndroid Build Coastguard Worker 
360*495ae853SAndroid Build Coastguard Worker     INC_BIN_COUNT(ps_cab_env)
361*495ae853SAndroid Build Coastguard Worker 
362*495ae853SAndroid Build Coastguard Worker     return (u4_symbol);
363*495ae853SAndroid Build Coastguard Worker }
364*495ae853SAndroid Build Coastguard Worker 
365*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
366*495ae853SAndroid Build Coastguard Worker /*                                                                           */
367*495ae853SAndroid Build Coastguard Worker /*  Function Name : ih264d_decode_bins_tunary                                */
368*495ae853SAndroid Build Coastguard Worker /*                                                                           */
369*495ae853SAndroid Build Coastguard Worker /*  Description   : This function decodes bins in the case of TUNARY         */
370*495ae853SAndroid Build Coastguard Worker /*                  binarization technique.valid_length is assumed  equal to 3 */
371*495ae853SAndroid Build Coastguard Worker /*                  and u1_max_bins <= 4 in this functon.                                              */
372*495ae853SAndroid Build Coastguard Worker /*  Inputs        : <What inputs does the function take?>                    */
373*495ae853SAndroid Build Coastguard Worker /*  Globals       : <Does it use any global variables?>                      */
374*495ae853SAndroid Build Coastguard Worker /*  Processing    : <Describe how the function operates - include algorithm  */
375*495ae853SAndroid Build Coastguard Worker /*                  description>                                             */
376*495ae853SAndroid Build Coastguard Worker /*  Outputs       : <What does the function produce?>                        */
377*495ae853SAndroid Build Coastguard Worker /*  Returns       : <What does the function return?>                         */
378*495ae853SAndroid Build Coastguard Worker /*                                                                           */
379*495ae853SAndroid Build Coastguard Worker /*  Issues        :                                                          */
380*495ae853SAndroid Build Coastguard Worker /*                                                                           */
381*495ae853SAndroid Build Coastguard Worker /*  Revision History:                                                        */
382*495ae853SAndroid Build Coastguard Worker /*                                                                           */
383*495ae853SAndroid Build Coastguard Worker /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
384*495ae853SAndroid Build Coastguard Worker /*         20 11 2008   SH          Draft                                   */
385*495ae853SAndroid Build Coastguard Worker /*                                                                           */
386*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
387*495ae853SAndroid Build Coastguard Worker 
ih264d_decode_bins_tunary(UWORD8 u1_max_bins,UWORD32 u4_ctx_inc,bin_ctxt_model_t * ps_src_bin_ctxt,dec_bit_stream_t * ps_bitstrm,decoding_envirnoment_t * ps_cab_env)388*495ae853SAndroid Build Coastguard Worker UWORD32 ih264d_decode_bins_tunary(UWORD8 u1_max_bins,
389*495ae853SAndroid Build Coastguard Worker                                   UWORD32 u4_ctx_inc,
390*495ae853SAndroid Build Coastguard Worker                                   bin_ctxt_model_t *ps_src_bin_ctxt,
391*495ae853SAndroid Build Coastguard Worker                                   dec_bit_stream_t *ps_bitstrm,
392*495ae853SAndroid Build Coastguard Worker                                   decoding_envirnoment_t *ps_cab_env)
393*495ae853SAndroid Build Coastguard Worker 
394*495ae853SAndroid Build Coastguard Worker {
395*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_value;
396*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_symbol;
397*495ae853SAndroid Build Coastguard Worker     UWORD8 u4_ctx_Inc;
398*495ae853SAndroid Build Coastguard Worker     bin_ctxt_model_t *ps_bin_ctxt;
399*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_code_int_range, u4_code_int_val_ofst;
400*495ae853SAndroid Build Coastguard Worker     const UWORD32 *pu4_table = (const UWORD32 *)ps_cab_env->cabac_table;
401*495ae853SAndroid Build Coastguard Worker 
402*495ae853SAndroid Build Coastguard Worker     u4_value = 0;
403*495ae853SAndroid Build Coastguard Worker 
404*495ae853SAndroid Build Coastguard Worker     /*u1_max_bins has to be less than or equal to 4, u1_max_bins <= 4 for  this function*/
405*495ae853SAndroid Build Coastguard Worker 
406*495ae853SAndroid Build Coastguard Worker     /*here the valid length is assumed to be equal to 3 ,so the calling function is expected
407*495ae853SAndroid Build Coastguard Worker      to duplicate CtxInc if valid lenth is 2 and cmaxbin is greater than2*/
408*495ae853SAndroid Build Coastguard Worker     u4_code_int_range = ps_cab_env->u4_code_int_range;
409*495ae853SAndroid Build Coastguard Worker     u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
410*495ae853SAndroid Build Coastguard Worker 
411*495ae853SAndroid Build Coastguard Worker     do
412*495ae853SAndroid Build Coastguard Worker     {
413*495ae853SAndroid Build Coastguard Worker         u4_ctx_Inc = u4_ctx_inc & 0xF;
414*495ae853SAndroid Build Coastguard Worker         u4_ctx_inc = u4_ctx_inc >> 4;
415*495ae853SAndroid Build Coastguard Worker 
416*495ae853SAndroid Build Coastguard Worker         ps_bin_ctxt = ps_src_bin_ctxt + u4_ctx_Inc;
417*495ae853SAndroid Build Coastguard Worker 
418*495ae853SAndroid Build Coastguard Worker         DECODE_ONE_BIN_MACRO(ps_bin_ctxt, u4_code_int_range, u4_code_int_val_ofst,
419*495ae853SAndroid Build Coastguard Worker                              pu4_table, ps_bitstrm, u4_symbol)
420*495ae853SAndroid Build Coastguard Worker 
421*495ae853SAndroid Build Coastguard Worker         INC_BIN_COUNT(ps_cab_env);INC_DECISION_BINS(ps_cab_env);
422*495ae853SAndroid Build Coastguard Worker 
423*495ae853SAndroid Build Coastguard Worker         u4_value++;
424*495ae853SAndroid Build Coastguard Worker     }
425*495ae853SAndroid Build Coastguard Worker     while((u4_value < u1_max_bins) & (u4_symbol));
426*495ae853SAndroid Build Coastguard Worker 
427*495ae853SAndroid Build Coastguard Worker     u4_value = u4_value - 1 + u4_symbol;
428*495ae853SAndroid Build Coastguard Worker 
429*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_range = u4_code_int_range;
430*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
431*495ae853SAndroid Build Coastguard Worker 
432*495ae853SAndroid Build Coastguard Worker     return (u4_value);
433*495ae853SAndroid Build Coastguard Worker 
434*495ae853SAndroid Build Coastguard Worker }
435*495ae853SAndroid Build Coastguard Worker 
436*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
437*495ae853SAndroid Build Coastguard Worker /*                                                                           */
438*495ae853SAndroid Build Coastguard Worker /*  Function Name : ih264d_decode_bins                                */
439*495ae853SAndroid Build Coastguard Worker /*                                                                           */
440*495ae853SAndroid Build Coastguard Worker /*  Description   : This function decodes bins in the case of MSB_FIRST_FLC  */
441*495ae853SAndroid Build Coastguard Worker /*                  binarization technique.valid_length is always equal max_bins */
442*495ae853SAndroid Build Coastguard Worker /*                  for MSB_FIRST_FLC. assumes  u1_max_bins <= 4               */
443*495ae853SAndroid Build Coastguard Worker /*  Inputs        : <What inputs does the function take?>                    */
444*495ae853SAndroid Build Coastguard Worker /*  Globals       : <Does it use any global variables?>                      */
445*495ae853SAndroid Build Coastguard Worker /*  Processing    : <Describe how the function operates - include algorithm  */
446*495ae853SAndroid Build Coastguard Worker /*                  description>                                             */
447*495ae853SAndroid Build Coastguard Worker /*  Outputs       : <What does the function produce?>                        */
448*495ae853SAndroid Build Coastguard Worker /*  Returns       : <What does the function return?>                         */
449*495ae853SAndroid Build Coastguard Worker /*                                                                           */
450*495ae853SAndroid Build Coastguard Worker /*  Issues        : <List any issues or problems with this function>         */
451*495ae853SAndroid Build Coastguard Worker /*                                                                           */
452*495ae853SAndroid Build Coastguard Worker /*  Revision History:                                                        */
453*495ae853SAndroid Build Coastguard Worker /*                                                                           */
454*495ae853SAndroid Build Coastguard Worker /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
455*495ae853SAndroid Build Coastguard Worker /*         20 11 2008   SH          Draft                                   */
456*495ae853SAndroid Build Coastguard Worker /*                                                                           */
457*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
458*495ae853SAndroid Build Coastguard Worker 
ih264d_decode_bins(UWORD8 u1_max_bins,UWORD32 u4_ctx_inc,bin_ctxt_model_t * ps_src_bin_ctxt,dec_bit_stream_t * ps_bitstrm,decoding_envirnoment_t * ps_cab_env)459*495ae853SAndroid Build Coastguard Worker UWORD32 ih264d_decode_bins(UWORD8 u1_max_bins,
460*495ae853SAndroid Build Coastguard Worker                            UWORD32 u4_ctx_inc,
461*495ae853SAndroid Build Coastguard Worker                            bin_ctxt_model_t *ps_src_bin_ctxt,
462*495ae853SAndroid Build Coastguard Worker                            dec_bit_stream_t *ps_bitstrm,
463*495ae853SAndroid Build Coastguard Worker                            decoding_envirnoment_t *ps_cab_env)
464*495ae853SAndroid Build Coastguard Worker 
465*495ae853SAndroid Build Coastguard Worker {
466*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_value;
467*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_symbol, i;
468*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_ctxt_inc;
469*495ae853SAndroid Build Coastguard Worker     bin_ctxt_model_t *ps_bin_ctxt;
470*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_code_int_range, u4_code_int_val_ofst;
471*495ae853SAndroid Build Coastguard Worker     const UWORD32 *pu4_table = (const UWORD32 *)ps_cab_env->cabac_table;
472*495ae853SAndroid Build Coastguard Worker 
473*495ae853SAndroid Build Coastguard Worker     i = 0;
474*495ae853SAndroid Build Coastguard Worker 
475*495ae853SAndroid Build Coastguard Worker     u4_value = 0;
476*495ae853SAndroid Build Coastguard Worker 
477*495ae853SAndroid Build Coastguard Worker     /*u1_max_bins has to be less than or equal to 4, u1_max_bins <= 4 for  this fucntion*/
478*495ae853SAndroid Build Coastguard Worker     u4_code_int_range = ps_cab_env->u4_code_int_range;
479*495ae853SAndroid Build Coastguard Worker     u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
480*495ae853SAndroid Build Coastguard Worker 
481*495ae853SAndroid Build Coastguard Worker     do
482*495ae853SAndroid Build Coastguard Worker     {
483*495ae853SAndroid Build Coastguard Worker         u4_ctxt_inc = u4_ctx_inc & 0xf;
484*495ae853SAndroid Build Coastguard Worker         u4_ctx_inc = u4_ctx_inc >> 4;
485*495ae853SAndroid Build Coastguard Worker 
486*495ae853SAndroid Build Coastguard Worker         ps_bin_ctxt = ps_src_bin_ctxt + u4_ctxt_inc;
487*495ae853SAndroid Build Coastguard Worker 
488*495ae853SAndroid Build Coastguard Worker         DECODE_ONE_BIN_MACRO(ps_bin_ctxt, u4_code_int_range, u4_code_int_val_ofst,
489*495ae853SAndroid Build Coastguard Worker                              pu4_table, ps_bitstrm, u4_symbol)
490*495ae853SAndroid Build Coastguard Worker 
491*495ae853SAndroid Build Coastguard Worker         INC_BIN_COUNT(ps_cab_env);INC_DECISION_BINS(ps_cab_env);
492*495ae853SAndroid Build Coastguard Worker 
493*495ae853SAndroid Build Coastguard Worker         u4_value = (u4_value << 1) | (u4_symbol);
494*495ae853SAndroid Build Coastguard Worker 
495*495ae853SAndroid Build Coastguard Worker         i++;
496*495ae853SAndroid Build Coastguard Worker     }
497*495ae853SAndroid Build Coastguard Worker     while(i < u1_max_bins);
498*495ae853SAndroid Build Coastguard Worker 
499*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_range = u4_code_int_range;
500*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
501*495ae853SAndroid Build Coastguard Worker 
502*495ae853SAndroid Build Coastguard Worker     return (u4_value);
503*495ae853SAndroid Build Coastguard Worker 
504*495ae853SAndroid Build Coastguard Worker }
505*495ae853SAndroid Build Coastguard Worker 
506*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
507*495ae853SAndroid Build Coastguard Worker /*                                                                           */
508*495ae853SAndroid Build Coastguard Worker /*  Function Name : ih264d_decode_bins_unary                                */
509*495ae853SAndroid Build Coastguard Worker /*                                                                           */
510*495ae853SAndroid Build Coastguard Worker /*  Description   : This function decodes bins in the case of UNARY         */
511*495ae853SAndroid Build Coastguard Worker /*                  binarization technique.here the valid length is taken to 5*/
512*495ae853SAndroid Build Coastguard Worker /*                  and cmax is always greater than 9                       */
513*495ae853SAndroid Build Coastguard Worker /*  Inputs        : <What inputs does the function take?>                    */
514*495ae853SAndroid Build Coastguard Worker /*  Globals       : <Does it use any global variables?>                      */
515*495ae853SAndroid Build Coastguard Worker /*  Processing    : <Describe how the function operates - include algorithm  */
516*495ae853SAndroid Build Coastguard Worker /*                  description>                                             */
517*495ae853SAndroid Build Coastguard Worker /*  Outputs       : <What does the function produce?>                        */
518*495ae853SAndroid Build Coastguard Worker /*  Returns       : <What does the function return?>                         */
519*495ae853SAndroid Build Coastguard Worker /*                                                                           */
520*495ae853SAndroid Build Coastguard Worker /*  Issues        : <List any issues or problems with this function>         */
521*495ae853SAndroid Build Coastguard Worker /*                                                                           */
522*495ae853SAndroid Build Coastguard Worker /*  Revision History:                                                        */
523*495ae853SAndroid Build Coastguard Worker /*                                                                           */
524*495ae853SAndroid Build Coastguard Worker /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
525*495ae853SAndroid Build Coastguard Worker /*         20 11 2008   SH          Draft                                   */
526*495ae853SAndroid Build Coastguard Worker /*                                                                           */
527*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
ih264d_decode_bins_unary(UWORD8 u1_max_bins,UWORD32 u4_ctx_inc,bin_ctxt_model_t * ps_src_bin_ctxt,dec_bit_stream_t * ps_bitstrm,decoding_envirnoment_t * ps_cab_env)528*495ae853SAndroid Build Coastguard Worker UWORD32 ih264d_decode_bins_unary(UWORD8 u1_max_bins,
529*495ae853SAndroid Build Coastguard Worker                                  UWORD32 u4_ctx_inc,
530*495ae853SAndroid Build Coastguard Worker                                  bin_ctxt_model_t *ps_src_bin_ctxt,
531*495ae853SAndroid Build Coastguard Worker                                  dec_bit_stream_t *ps_bitstrm,
532*495ae853SAndroid Build Coastguard Worker                                  decoding_envirnoment_t *ps_cab_env)
533*495ae853SAndroid Build Coastguard Worker {
534*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_value;
535*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_symbol;
536*495ae853SAndroid Build Coastguard Worker     bin_ctxt_model_t *ps_bin_ctxt;
537*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_ctx_Inc;
538*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_code_int_range, u4_code_int_val_ofst;
539*495ae853SAndroid Build Coastguard Worker     const UWORD32 *pu4_table = (const UWORD32 *)ps_cab_env->cabac_table;
540*495ae853SAndroid Build Coastguard Worker 
541*495ae853SAndroid Build Coastguard Worker     /* in this function the valid length for u4_ctx_inc is always taken to be,so if the
542*495ae853SAndroid Build Coastguard Worker      the valid length is lessthan 5 the caller need to duplicate accordingly*/
543*495ae853SAndroid Build Coastguard Worker 
544*495ae853SAndroid Build Coastguard Worker     /*u1_max_bins is always greater or equal to 9 we have the check for u1_max_bins only after the 2 loop*/
545*495ae853SAndroid Build Coastguard Worker     u4_value = 0;
546*495ae853SAndroid Build Coastguard Worker     u4_code_int_range = ps_cab_env->u4_code_int_range;
547*495ae853SAndroid Build Coastguard Worker     u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
548*495ae853SAndroid Build Coastguard Worker 
549*495ae853SAndroid Build Coastguard Worker     do
550*495ae853SAndroid Build Coastguard Worker     {
551*495ae853SAndroid Build Coastguard Worker         u4_ctx_Inc = u4_ctx_inc & 0xf;
552*495ae853SAndroid Build Coastguard Worker         u4_ctx_inc = u4_ctx_inc >> 4;
553*495ae853SAndroid Build Coastguard Worker 
554*495ae853SAndroid Build Coastguard Worker         ps_bin_ctxt = ps_src_bin_ctxt + u4_ctx_Inc;
555*495ae853SAndroid Build Coastguard Worker 
556*495ae853SAndroid Build Coastguard Worker         DECODE_ONE_BIN_MACRO(ps_bin_ctxt, u4_code_int_range, u4_code_int_val_ofst,
557*495ae853SAndroid Build Coastguard Worker                              pu4_table, ps_bitstrm, u4_symbol)
558*495ae853SAndroid Build Coastguard Worker 
559*495ae853SAndroid Build Coastguard Worker         INC_BIN_COUNT(ps_cab_env);INC_DECISION_BINS(ps_cab_env);
560*495ae853SAndroid Build Coastguard Worker 
561*495ae853SAndroid Build Coastguard Worker         u4_value++;
562*495ae853SAndroid Build Coastguard Worker 
563*495ae853SAndroid Build Coastguard Worker     }
564*495ae853SAndroid Build Coastguard Worker     while(u4_symbol && u4_value < 4);
565*495ae853SAndroid Build Coastguard Worker 
566*495ae853SAndroid Build Coastguard Worker     if(u4_symbol && (u4_value < u1_max_bins))
567*495ae853SAndroid Build Coastguard Worker     {
568*495ae853SAndroid Build Coastguard Worker 
569*495ae853SAndroid Build Coastguard Worker         u4_ctx_Inc = u4_ctx_inc & 0xf;
570*495ae853SAndroid Build Coastguard Worker 
571*495ae853SAndroid Build Coastguard Worker         ps_bin_ctxt = ps_src_bin_ctxt + u4_ctx_Inc;
572*495ae853SAndroid Build Coastguard Worker 
573*495ae853SAndroid Build Coastguard Worker         do
574*495ae853SAndroid Build Coastguard Worker         {
575*495ae853SAndroid Build Coastguard Worker 
576*495ae853SAndroid Build Coastguard Worker             DECODE_ONE_BIN_MACRO(ps_bin_ctxt, u4_code_int_range, u4_code_int_val_ofst,
577*495ae853SAndroid Build Coastguard Worker                                  pu4_table, ps_bitstrm, u4_symbol)
578*495ae853SAndroid Build Coastguard Worker 
579*495ae853SAndroid Build Coastguard Worker             INC_BIN_COUNT(ps_cab_env);INC_DECISION_BINS(ps_cab_env);
580*495ae853SAndroid Build Coastguard Worker 
581*495ae853SAndroid Build Coastguard Worker             u4_value++;
582*495ae853SAndroid Build Coastguard Worker 
583*495ae853SAndroid Build Coastguard Worker         }
584*495ae853SAndroid Build Coastguard Worker         while(u4_symbol && (u4_value < u1_max_bins));
585*495ae853SAndroid Build Coastguard Worker 
586*495ae853SAndroid Build Coastguard Worker     }
587*495ae853SAndroid Build Coastguard Worker 
588*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_range = u4_code_int_range;
589*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
590*495ae853SAndroid Build Coastguard Worker 
591*495ae853SAndroid Build Coastguard Worker     u4_value = u4_value - 1 + u4_symbol;
592*495ae853SAndroid Build Coastguard Worker 
593*495ae853SAndroid Build Coastguard Worker     return (u4_value);
594*495ae853SAndroid Build Coastguard Worker 
595*495ae853SAndroid Build Coastguard Worker }
596*495ae853SAndroid Build Coastguard Worker 
597*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
598*495ae853SAndroid Build Coastguard Worker /*                                                                           */
599*495ae853SAndroid Build Coastguard Worker /*  Function Name : ih264d_decode_bypass_bins_unary                                     */
600*495ae853SAndroid Build Coastguard Worker /*                                                                           */
601*495ae853SAndroid Build Coastguard Worker /*  Description   : This function is used in the case of UNARY coding       */
602*495ae853SAndroid Build Coastguard Worker /*                                                                           */
603*495ae853SAndroid Build Coastguard Worker /*                                                                           */
604*495ae853SAndroid Build Coastguard Worker /*  Inputs        : <What inputs does the function take?>                    */
605*495ae853SAndroid Build Coastguard Worker /*  Globals       : <Does it use any global variables?>                      */
606*495ae853SAndroid Build Coastguard Worker /*  Processing    : <Describe how the function operates - include algorithm  */
607*495ae853SAndroid Build Coastguard Worker /*                  description>                                             */
608*495ae853SAndroid Build Coastguard Worker /*  Outputs       : <What does the function produce?>                        */
609*495ae853SAndroid Build Coastguard Worker /*  Returns       : <What does the function return?>                         */
610*495ae853SAndroid Build Coastguard Worker /*                                                                           */
611*495ae853SAndroid Build Coastguard Worker /*  Issues        : <List any issues or problems with this function>         */
612*495ae853SAndroid Build Coastguard Worker /*                                                                           */
613*495ae853SAndroid Build Coastguard Worker /*  Revision History:                                                        */
614*495ae853SAndroid Build Coastguard Worker /*                                                                           */
615*495ae853SAndroid Build Coastguard Worker /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
616*495ae853SAndroid Build Coastguard Worker /*         13 10 2005   Ittiam          Draft                                */
617*495ae853SAndroid Build Coastguard Worker /*                                                                           */
618*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
619*495ae853SAndroid Build Coastguard Worker 
ih264d_decode_bypass_bins_unary(decoding_envirnoment_t * ps_cab_env,dec_bit_stream_t * ps_bitstrm)620*495ae853SAndroid Build Coastguard Worker UWORD32 ih264d_decode_bypass_bins_unary(decoding_envirnoment_t *ps_cab_env,
621*495ae853SAndroid Build Coastguard Worker                                         dec_bit_stream_t *ps_bitstrm)
622*495ae853SAndroid Build Coastguard Worker {
623*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_value;
624*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_bin;
625*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_code_int_val_ofst, u4_code_int_range;
626*495ae853SAndroid Build Coastguard Worker 
627*495ae853SAndroid Build Coastguard Worker     UWORD32 u1_max_bins;
628*495ae853SAndroid Build Coastguard Worker 
629*495ae853SAndroid Build Coastguard Worker     u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
630*495ae853SAndroid Build Coastguard Worker     u4_code_int_range = ps_cab_env->u4_code_int_range;
631*495ae853SAndroid Build Coastguard Worker 
632*495ae853SAndroid Build Coastguard Worker     if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_9)
633*495ae853SAndroid Build Coastguard Worker     {
634*495ae853SAndroid Build Coastguard Worker         UWORD32 *pu4_buffer, u4_offset;
635*495ae853SAndroid Build Coastguard Worker 
636*495ae853SAndroid Build Coastguard Worker         pu4_buffer = ps_bitstrm->pu4_buffer;
637*495ae853SAndroid Build Coastguard Worker         u4_offset = ps_bitstrm->u4_ofst;
638*495ae853SAndroid Build Coastguard Worker 
639*495ae853SAndroid Build Coastguard Worker         RENORM_RANGE_OFFSET(u4_code_int_range, u4_code_int_val_ofst, u4_offset,
640*495ae853SAndroid Build Coastguard Worker                             pu4_buffer)
641*495ae853SAndroid Build Coastguard Worker         ps_bitstrm->u4_ofst = u4_offset;
642*495ae853SAndroid Build Coastguard Worker     }
643*495ae853SAndroid Build Coastguard Worker 
644*495ae853SAndroid Build Coastguard Worker     /*as it is called only form mvd*/
645*495ae853SAndroid Build Coastguard Worker     u1_max_bins = 32;
646*495ae853SAndroid Build Coastguard Worker     u4_value = 0;
647*495ae853SAndroid Build Coastguard Worker 
648*495ae853SAndroid Build Coastguard Worker     do
649*495ae853SAndroid Build Coastguard Worker     {
650*495ae853SAndroid Build Coastguard Worker         u4_value++;
651*495ae853SAndroid Build Coastguard Worker 
652*495ae853SAndroid Build Coastguard Worker         u4_code_int_range = u4_code_int_range >> 1;
653*495ae853SAndroid Build Coastguard Worker         if(u4_code_int_val_ofst >= u4_code_int_range)
654*495ae853SAndroid Build Coastguard Worker         {
655*495ae853SAndroid Build Coastguard Worker             /* S=1 */
656*495ae853SAndroid Build Coastguard Worker             u4_bin = 1;
657*495ae853SAndroid Build Coastguard Worker             u4_code_int_val_ofst -= u4_code_int_range;
658*495ae853SAndroid Build Coastguard Worker         }
659*495ae853SAndroid Build Coastguard Worker         else
660*495ae853SAndroid Build Coastguard Worker         {
661*495ae853SAndroid Build Coastguard Worker             /* S=0 */
662*495ae853SAndroid Build Coastguard Worker             u4_bin = 0;
663*495ae853SAndroid Build Coastguard Worker         }
664*495ae853SAndroid Build Coastguard Worker 
665*495ae853SAndroid Build Coastguard Worker         INC_BIN_COUNT(ps_cab_env);INC_BYPASS_BINS(ps_cab_env);
666*495ae853SAndroid Build Coastguard Worker 
667*495ae853SAndroid Build Coastguard Worker         if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_9)
668*495ae853SAndroid Build Coastguard Worker         {
669*495ae853SAndroid Build Coastguard Worker             UWORD32 *pu4_buffer, u4_offset;
670*495ae853SAndroid Build Coastguard Worker 
671*495ae853SAndroid Build Coastguard Worker             pu4_buffer = ps_bitstrm->pu4_buffer;
672*495ae853SAndroid Build Coastguard Worker             u4_offset = ps_bitstrm->u4_ofst;
673*495ae853SAndroid Build Coastguard Worker 
674*495ae853SAndroid Build Coastguard Worker             RENORM_RANGE_OFFSET(u4_code_int_range, u4_code_int_val_ofst, u4_offset,
675*495ae853SAndroid Build Coastguard Worker                                 pu4_buffer)
676*495ae853SAndroid Build Coastguard Worker 
677*495ae853SAndroid Build Coastguard Worker             ps_bitstrm->u4_ofst = u4_offset;
678*495ae853SAndroid Build Coastguard Worker         }
679*495ae853SAndroid Build Coastguard Worker 
680*495ae853SAndroid Build Coastguard Worker     }
681*495ae853SAndroid Build Coastguard Worker     while(u4_bin && (u4_value < u1_max_bins));
682*495ae853SAndroid Build Coastguard Worker 
683*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
684*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_range = u4_code_int_range;
685*495ae853SAndroid Build Coastguard Worker     u4_value = (u4_value - 1 + u4_bin);
686*495ae853SAndroid Build Coastguard Worker 
687*495ae853SAndroid Build Coastguard Worker return (u4_value);
688*495ae853SAndroid Build Coastguard Worker }
689*495ae853SAndroid Build Coastguard Worker 
690*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
691*495ae853SAndroid Build Coastguard Worker /*                                                                           */
692*495ae853SAndroid Build Coastguard Worker /*  Function Name : ih264d_decode_bypass_bins                                     */
693*495ae853SAndroid Build Coastguard Worker /*                                                                           */
694*495ae853SAndroid Build Coastguard Worker /*  Description   : This function is used in the case of FLC coding       */
695*495ae853SAndroid Build Coastguard Worker /*                                                                           */
696*495ae853SAndroid Build Coastguard Worker /*                                                                           */
697*495ae853SAndroid Build Coastguard Worker /*  Inputs        : <What inputs does the function take?>                    */
698*495ae853SAndroid Build Coastguard Worker /*  Globals       : <Does it use any global variables?>                      */
699*495ae853SAndroid Build Coastguard Worker /*  Processing    : <Describe how the function operates - include algorithm  */
700*495ae853SAndroid Build Coastguard Worker /*                  description>                                             */
701*495ae853SAndroid Build Coastguard Worker /*  Outputs       : <What does the function produce?>                        */
702*495ae853SAndroid Build Coastguard Worker /*  Returns       : <What does the function return?>                         */
703*495ae853SAndroid Build Coastguard Worker /*                                                                           */
704*495ae853SAndroid Build Coastguard Worker /*  Issues        : <List any issues or problems with this function>         */
705*495ae853SAndroid Build Coastguard Worker /*                                                                           */
706*495ae853SAndroid Build Coastguard Worker /*  Revision History:                                                        */
707*495ae853SAndroid Build Coastguard Worker /*                                                                           */
708*495ae853SAndroid Build Coastguard Worker /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
709*495ae853SAndroid Build Coastguard Worker /*         13 10 2005   Ittiam          Draft                                */
710*495ae853SAndroid Build Coastguard Worker /*                                                                           */
711*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
712*495ae853SAndroid Build Coastguard Worker 
ih264d_decode_bypass_bins(decoding_envirnoment_t * ps_cab_env,UWORD8 u1_max_bins,dec_bit_stream_t * ps_bitstrm)713*495ae853SAndroid Build Coastguard Worker UWORD32 ih264d_decode_bypass_bins(decoding_envirnoment_t *ps_cab_env,
714*495ae853SAndroid Build Coastguard Worker                                   UWORD8 u1_max_bins,
715*495ae853SAndroid Build Coastguard Worker                                   dec_bit_stream_t *ps_bitstrm)
716*495ae853SAndroid Build Coastguard Worker {
717*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_bins;
718*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_bin;
719*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_code_int_val_ofst, u4_code_int_range;
720*495ae853SAndroid Build Coastguard Worker 
721*495ae853SAndroid Build Coastguard Worker     u4_bins = 0;
722*495ae853SAndroid Build Coastguard Worker     u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
723*495ae853SAndroid Build Coastguard Worker     u4_code_int_range = ps_cab_env->u4_code_int_range;
724*495ae853SAndroid Build Coastguard Worker 
725*495ae853SAndroid Build Coastguard Worker     if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_9)
726*495ae853SAndroid Build Coastguard Worker     {
727*495ae853SAndroid Build Coastguard Worker         UWORD32 *pu4_buffer, u4_offset;
728*495ae853SAndroid Build Coastguard Worker 
729*495ae853SAndroid Build Coastguard Worker         pu4_buffer = ps_bitstrm->pu4_buffer;
730*495ae853SAndroid Build Coastguard Worker         u4_offset = ps_bitstrm->u4_ofst;
731*495ae853SAndroid Build Coastguard Worker 
732*495ae853SAndroid Build Coastguard Worker         RENORM_RANGE_OFFSET(u4_code_int_range, u4_code_int_val_ofst, u4_offset,
733*495ae853SAndroid Build Coastguard Worker                             pu4_buffer)
734*495ae853SAndroid Build Coastguard Worker         ps_bitstrm->u4_ofst = u4_offset;
735*495ae853SAndroid Build Coastguard Worker     }
736*495ae853SAndroid Build Coastguard Worker 
737*495ae853SAndroid Build Coastguard Worker     do
738*495ae853SAndroid Build Coastguard Worker     {
739*495ae853SAndroid Build Coastguard Worker 
740*495ae853SAndroid Build Coastguard Worker         u4_code_int_range = u4_code_int_range >> 1;
741*495ae853SAndroid Build Coastguard Worker 
742*495ae853SAndroid Build Coastguard Worker         if(u4_code_int_val_ofst >= u4_code_int_range)
743*495ae853SAndroid Build Coastguard Worker         {
744*495ae853SAndroid Build Coastguard Worker             /* S=1 */
745*495ae853SAndroid Build Coastguard Worker             u4_bin = 1;
746*495ae853SAndroid Build Coastguard Worker             u4_code_int_val_ofst -= u4_code_int_range;
747*495ae853SAndroid Build Coastguard Worker         }
748*495ae853SAndroid Build Coastguard Worker         else
749*495ae853SAndroid Build Coastguard Worker         {
750*495ae853SAndroid Build Coastguard Worker             /* S=0 */
751*495ae853SAndroid Build Coastguard Worker             u4_bin = 0;
752*495ae853SAndroid Build Coastguard Worker         }
753*495ae853SAndroid Build Coastguard Worker 
754*495ae853SAndroid Build Coastguard Worker         INC_BIN_COUNT(ps_cab_env);INC_BYPASS_BINS(ps_cab_env);
755*495ae853SAndroid Build Coastguard Worker 
756*495ae853SAndroid Build Coastguard Worker         u4_bins = ((u4_bins << 1) | u4_bin);
757*495ae853SAndroid Build Coastguard Worker         u1_max_bins--;
758*495ae853SAndroid Build Coastguard Worker 
759*495ae853SAndroid Build Coastguard Worker         if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_9)
760*495ae853SAndroid Build Coastguard Worker         {
761*495ae853SAndroid Build Coastguard Worker             UWORD32 *pu4_buffer, u4_offset;
762*495ae853SAndroid Build Coastguard Worker 
763*495ae853SAndroid Build Coastguard Worker             pu4_buffer = ps_bitstrm->pu4_buffer;
764*495ae853SAndroid Build Coastguard Worker             u4_offset = ps_bitstrm->u4_ofst;
765*495ae853SAndroid Build Coastguard Worker 
766*495ae853SAndroid Build Coastguard Worker             RENORM_RANGE_OFFSET(u4_code_int_range, u4_code_int_val_ofst, u4_offset,
767*495ae853SAndroid Build Coastguard Worker                                 pu4_buffer)
768*495ae853SAndroid Build Coastguard Worker             ps_bitstrm->u4_ofst = u4_offset;
769*495ae853SAndroid Build Coastguard Worker         }
770*495ae853SAndroid Build Coastguard Worker 
771*495ae853SAndroid Build Coastguard Worker     }
772*495ae853SAndroid Build Coastguard Worker     while(u1_max_bins);
773*495ae853SAndroid Build Coastguard Worker 
774*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
775*495ae853SAndroid Build Coastguard Worker     ps_cab_env->u4_code_int_range = u4_code_int_range;
776*495ae853SAndroid Build Coastguard Worker 
777*495ae853SAndroid Build Coastguard Worker     return (u4_bins);
778*495ae853SAndroid Build Coastguard Worker }
779*495ae853SAndroid Build Coastguard Worker 
780