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