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_parse_cavlc.c
23  *
24  * \brief
25  *    This file contains UVLC related functions.
26  *
27  * \date
28  *    20/11/2002
29  *
30  * \author  NS
31  ***************************************************************************
32  */
33 
34 #include <string.h>
35 #include <stdio.h>
36 
37 #include "ih264d_bitstrm.h"
38 #include "ih264d_parse_cavlc.h"
39 #include "ih264d_error_handler.h"
40 #include "ih264d_defs.h"
41 #include "ih264d_debug.h"
42 #include "ih264d_cabac.h"
43 #include "ih264d_structs.h"
44 #include "ih264d_tables.h"
45 #include "ih264d_tables.h"
46 #include "ih264d_mb_utils.h"
47 
48 void ih264d_unpack_coeff4x4_dc_4x4blk(tu_sblk4x4_coeff_data_t *ps_tu_4x4,
49                                       WORD16 *pi2_out_coeff_data,
50                                       UWORD8 *pu1_inv_scan);
51 
52 /*****************************************************************************/
53 /*                                                                           */
54 /*  Function Name : ih264d_uev                                                  */
55 /*                                                                           */
56 /*  Description   : Reads the unsigned Exp Golomb codec syntax from the      */
57 /*                  ps_bitstrm as specified in section 9.1 of H264 standard      */
58 /*                  It also increases bitstream u4_ofst by the number of bits */
59 /*                  parsed for UEV decode operation                          */
60 /*                                                                           */
61 /*  Inputs        : bitstream base pointer and bitsream u4_ofst in bits       */
62 /*  Globals       : None                                                     */
63 /*  Processing    :                                                          */
64 /*  Outputs       : UEV decoded syntax element and incremented ps_bitstrm u4_ofst */
65 /*  Returns       : UEV decoded syntax element                               */
66 /*                                                                           */
67 /*  Issues        : Does not check if ps_bitstrm u4_ofst exceeds max ps_bitstrm i4_size  */
68 /*                  for performamce. Caller might have to do error resilence */
69 /*                  check for bitstream overflow                             */
70 /*                                                                           */
71 /*  Revision History:                                                        */
72 /*                                                                           */
73 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
74 /*         19 09 2008   Jay          Draft                                   */
75 /*                                                                           */
76 /*****************************************************************************/
ih264d_uev(UWORD32 * pu4_bitstrm_ofst,UWORD32 * pu4_bitstrm_buf)77 UWORD32 ih264d_uev(UWORD32 *pu4_bitstrm_ofst, UWORD32 *pu4_bitstrm_buf)
78 {
79     UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
80     UWORD32 u4_word, u4_ldz;
81 
82     /***************************************************************/
83     /* Find leading zeros in next 32 bits                          */
84     /***************************************************************/
85     NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
86     u4_ldz = CLZ(u4_word);
87     /* Flush the ps_bitstrm */
88     u4_bitstream_offset += (u4_ldz + 1);
89     /* Read the suffix from the ps_bitstrm */
90     u4_word = 0;
91     if(u4_ldz)
92         GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf, u4_ldz);
93     *pu4_bitstrm_ofst = u4_bitstream_offset;
94     return ((1 << u4_ldz) + u4_word - 1);
95 }
96 
97 /*****************************************************************************/
98 /*                                                                           */
99 /*  Function Name : ih264d_sev                                                  */
100 /*                                                                           */
101 /*  Description   : Reads the signed Exp Golomb codec syntax from the ps_bitstrm */
102 /*                  as specified in section 9.1 of H264 standard.            */
103 /*                  It also increases bitstream u4_ofst by the number of bits */
104 /*                  parsed for SEV decode operation                          */
105 /*                                                                           */
106 /*  Inputs        : bitstream base pointer and bitsream u4_ofst in bits       */
107 /*  Globals       : None                                                     */
108 /*  Processing    :                                                          */
109 /*  Outputs       : SEV decoded syntax element and incremented ps_bitstrm u4_ofst */
110 /*  Returns       : SEV decoded syntax element                               */
111 /*                                                                           */
112 /*  Issues        : Does not check if ps_bitstrm u4_ofst exceeds max ps_bitstrm i4_size  */
113 /*                  for performamce. Caller might have to do error resilence */
114 /*                  check for bitstream overflow                             */
115 /*                                                                           */
116 /*  Revision History:                                                        */
117 /*                                                                           */
118 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
119 /*         19 09 2008   Jay          Draft                                   */
120 /*                                                                           */
121 /*****************************************************************************/
ih264d_sev(UWORD32 * pu4_bitstrm_ofst,UWORD32 * pu4_bitstrm_buf)122 WORD32 ih264d_sev(UWORD32 *pu4_bitstrm_ofst, UWORD32 *pu4_bitstrm_buf)
123 {
124     UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
125     UWORD32 u4_word, u4_ldz, u4_abs_val;
126 
127     /***************************************************************/
128     /* Find leading zeros in next 32 bits                          */
129     /***************************************************************/
130     NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
131     u4_ldz = CLZ(u4_word);
132 
133     /* Flush the ps_bitstrm */
134     u4_bitstream_offset += (u4_ldz + 1);
135 
136     /* Read the suffix from the ps_bitstrm */
137     u4_word = 0;
138     if(u4_ldz)
139         GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf, u4_ldz);
140 
141     *pu4_bitstrm_ofst = u4_bitstream_offset;
142     u4_abs_val = ((1 << u4_ldz) + u4_word) >> 1;
143 
144     if(u4_word & 0x1)
145         return (-(WORD32)u4_abs_val);
146     else
147         return (u4_abs_val);
148 }
149 
150 /*****************************************************************************/
151 /*                                                                           */
152 /*  Function Name : get_tev_range_1                                          */
153 /*                                                                           */
154 /*  Description   : Reads the TEV Exp Golomb codec syntax from the ps_bitstrm    */
155 /*                  as specified in section 9.1 of H264 standard. This will  */
156 /*                  called only when the input range is 1 for TEV decode.    */
157 /*                  If range is more than 1, then UEV decode is done         */
158 /*                                                                           */
159 /*  Inputs        : bitstream base pointer and bitsream u4_ofst in bits       */
160 /*  Globals       : None                                                     */
161 /*  Processing    :                                                          */
162 /*  Outputs       : TEV decoded syntax element and incremented ps_bitstrm u4_ofst */
163 /*  Returns       : TEV decoded syntax element                               */
164 /*                                                                           */
165 /*  Issues        : Does not check if ps_bitstrm u4_ofst exceeds max ps_bitstrm i4_size  */
166 /*                  for performamce. Caller might have to do error resilence */
167 /*                  check for bitstream overflow                             */
168 /*                                                                           */
169 /*  Revision History:                                                        */
170 /*                                                                           */
171 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
172 /*         19 09 2008   Jay          Draft                                   */
173 /*                                                                           */
174 /*****************************************************************************/
ih264d_tev_range1(UWORD32 * pu4_bitstrm_ofst,UWORD32 * pu4_bitstrm_buf)175 UWORD32 ih264d_tev_range1(UWORD32 *pu4_bitstrm_ofst, UWORD32 *pu4_bitstrm_buf)
176 {
177     UWORD32 u4_code;
178     GETBIT(u4_code, *pu4_bitstrm_ofst, pu4_bitstrm_buf);
179     return (!u4_code);
180 }
181 
182 /*!
183  **************************************************************************
184  * \if Function name : ih264d_uvlc \endif
185  *
186  * \brief
187  *
188  *    Reads the unsigned/signed/truncated integer Exp-Golomb-coded syntax element
189  *    with the left bit first. The parsing process for this descriptor is specified
190  *    in subclause 9.1.
191  *
192  * \param ps_bitstrm       : Pointer to Bitstream Structure .
193  * \param u4_range           : Range value in case of Truncated Exp-Golomb-code
194  * \param pi_bitstrm_ofst : Pointer to the local copy of Bitstream u4_ofst
195  * \param u1_flag            : Flag indicating the case of UEV,SEV or TEV
196  * \param u4_bitstrm_ofst : Local copy of Bitstream u4_ofst
197  * \param pu4_bitstrm_buf : Pointer to the Bitstream buffer
198  *
199  * \return
200  *    Returns Code Value.
201  *
202  **************************************************************************
203  */
204 
ih264d_uvlc(dec_bit_stream_t * ps_bitstrm,UWORD32 u4_range,UWORD32 * pi_bitstrm_ofst,UWORD8 u1_flag,UWORD32 u4_bitstrm_ofst,UWORD32 * pu4_bitstrm_buf)205 WORD32 ih264d_uvlc(dec_bit_stream_t *ps_bitstrm,
206                    UWORD32 u4_range,
207                    UWORD32 *pi_bitstrm_ofst,
208                    UWORD8 u1_flag,
209                    UWORD32 u4_bitstrm_ofst,
210                    UWORD32 *pu4_bitstrm_buf)
211 {
212     UWORD32 word, word2, cur_bit, cur_word, code_val, code_num, clz;
213 
214     SWITCHOFFTRACE;
215     cur_bit = u4_bitstrm_ofst & 0x1F;
216     cur_word = u4_bitstrm_ofst >> 5;
217     word = pu4_bitstrm_buf[cur_word];
218     word2 = pu4_bitstrm_buf[cur_word + 1];
219 
220     if(cur_bit != 0)
221     {
222         word <<= cur_bit;
223         word2 >>= (32 - cur_bit);
224         word |= word2;
225     }
226 
227     if(u1_flag == TEV && u4_range == 1)
228     {
229         word >>= 31;
230         word = 1 - word;
231         (*pi_bitstrm_ofst)++;
232         ps_bitstrm->u4_ofst = *pi_bitstrm_ofst;
233         return (WORD32)word;
234     }
235 
236     //finding clz
237     {
238         UWORD32 ui32_code, ui32_mask;
239 
240         ui32_code = word;
241         ui32_mask = 0x80000000;
242         clz = 0;
243 
244         /* DSP implements this with LMBD instruction */
245         /* so there we don't need to break the loop */
246         while(!(ui32_code & ui32_mask))
247         {
248             clz++;
249             ui32_mask >>= 1;
250             if(0 == ui32_mask)
251                 break;
252         }
253     }
254 
255     if(clz == 0)
256     {
257         *pi_bitstrm_ofst = *pi_bitstrm_ofst + (2 * clz) + 1;
258         ps_bitstrm->u4_ofst = *pi_bitstrm_ofst;
259         return 0;
260     }
261 
262     word <<= (clz + 1);
263     word >>= (32 - clz);
264     code_num = (1 << clz) + word - 1;
265     *pi_bitstrm_ofst = *pi_bitstrm_ofst + (2 * clz) + 1;
266     ps_bitstrm->u4_ofst = *pi_bitstrm_ofst;
267 
268     if(u1_flag == TEV || u1_flag == UEV)
269         return (WORD32)code_num;
270 
271     code_val = (code_num + 1) >> 1;
272     if(!(code_num & 0x01))
273         return -((WORD32)code_val);
274     return (WORD32)code_val;
275 
276 }
277 
278 /*****************************************************************************/
279 /*                                                                           */
280 /*  Function Name : ih264d_cavlc_4x4res_block_totalcoeff_1                          */
281 /*                                                                           */
282 /*  Description   : This function does cavlc decoding of 4x4 block residual  */
283 /*                  coefficient when total coeff is equal to 1. The parsing  */
284 /*                  is done as defined in section 9.2.2 and 9.2.3 of the     */
285 /*                  H264 standard.                                           */
286 /*                                                                           */
287 /*  Inputs        : <What inputs does the function take?>                    */
288 /*  Globals       : <Does it use any global variables?>                      */
289 /*  Processing    : <Describe how the function operates - include algorithm  */
290 /*                  description>                                             */
291 /*  Outputs       : <What does the function produce?>                        */
292 /*  Returns       : <What does the function return?>                         */
293 /*                                                                           */
294 /*  Issues        : <List any issues or problems with this function>         */
295 /*                                                                           */
296 /*  Revision History:                                                        */
297 /*                                                                           */
298 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
299 /*         25 09 2008   Jay          Draft                                   */
300 /*                                                                           */
301 /*****************************************************************************/
ih264d_cavlc_4x4res_block_totalcoeff_1(UWORD32 u4_isdc,UWORD32 u4_total_coeff_trail_one,dec_bit_stream_t * ps_bitstrm)302 WORD32 ih264d_cavlc_4x4res_block_totalcoeff_1(UWORD32 u4_isdc,
303                                            UWORD32 u4_total_coeff_trail_one,
304                                            dec_bit_stream_t *ps_bitstrm)
305 {
306 
307     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
308     UWORD32 u4_bitstream_offset = ps_bitstrm->u4_ofst;
309     UWORD32 u4_trailing_ones = u4_total_coeff_trail_one & 0xFFFF;
310     WORD32 i2_level;
311     UWORD32 u4_tot_zero, u4_ldz, u4_scan_pos;
312 
313     tu_sblk4x4_coeff_data_t *ps_tu_4x4;
314     WORD16 *pi2_coeff_data;
315     dec_struct_t *ps_dec = (dec_struct_t *)ps_bitstrm->pv_codec_handle;
316 
317     ps_tu_4x4 = (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
318     ps_tu_4x4->u2_sig_coeff_map = 0;
319     pi2_coeff_data = &ps_tu_4x4->ai2_level[0];
320 
321 
322     if(u4_trailing_ones)
323     {
324         UWORD32 u4_sign;
325         /****************************************************************/
326         /* Decode Trailing One as in section 9.2.2                      */
327         /****************************************************************/
328         GETBIT(u4_sign, u4_bitstream_offset, pu4_bitstrm_buf);
329         i2_level = u4_sign ? -1 : 1;
330     }
331     else
332     {
333         /****************************************************************/
334         /* Decoding Level based on prefix and suffix  as in 9.2.2       */
335         /****************************************************************/
336         UWORD32 u4_lev_suffix, u4_lev_suffix_size;
337         WORD32 u2_lev_code, u2_abs_value;
338         UWORD32 u4_lev_prefix;
339         /***************************************************************/
340         /* Find leading zeros in next 32 bits                          */
341         /***************************************************************/
342         FIND_ONE_IN_STREAM_32(u4_lev_prefix, u4_bitstream_offset,
343                               pu4_bitstrm_buf);
344         u2_lev_code = (2 + MIN(u4_lev_prefix, 15));
345 
346         if(14 == u4_lev_prefix)
347             u4_lev_suffix_size = 4;
348         else if(15 <= u4_lev_prefix)
349         {
350             u2_lev_code += 15;
351             u4_lev_suffix_size = u4_lev_prefix - 3;
352         }
353         else
354             u4_lev_suffix_size = 0;
355 
356         //HP_LEVEL_PREFIX
357         if(16 <= u4_lev_prefix)
358         {
359             u2_lev_code += ((1 << (u4_lev_prefix - 3)) - 4096);
360         }
361         if(u4_lev_suffix_size)
362         {
363             GETBITS(u4_lev_suffix, u4_bitstream_offset, pu4_bitstrm_buf,
364                     u4_lev_suffix_size);
365             u2_lev_code += u4_lev_suffix;
366         }
367 
368         u2_abs_value = (u2_lev_code + 2) >> 1;
369         /*********************************************************/
370         /* If Level code is odd, level is negative else positive */
371         /*********************************************************/
372         i2_level = (u2_lev_code & 1) ? -u2_abs_value : u2_abs_value;
373 
374     }
375 
376     /****************************************************************/
377     /* Decoding total zeros as in section 9.2.3, table 9.7          */
378     /****************************************************************/
379     FIND_ONE_IN_STREAM_LEN(u4_ldz, u4_bitstream_offset, pu4_bitstrm_buf, 8);
380 
381     if(u4_ldz)
382     {
383         GETBIT(u4_tot_zero, u4_bitstream_offset, pu4_bitstrm_buf);
384         u4_tot_zero = (u4_ldz << 1) - u4_tot_zero;
385     }
386     else
387         u4_tot_zero = 0;
388 
389     /***********************************************************************/
390     /* Inverse scan and store  residual coeff. Update the bitstream u4_ofst */
391     /***********************************************************************/
392     u4_scan_pos = u4_tot_zero + u4_isdc;
393     if(u4_scan_pos > 15)
394         return -1;
395 
396     SET_BIT(ps_tu_4x4->u2_sig_coeff_map, u4_scan_pos);
397     *pi2_coeff_data++ = i2_level;
398 
399 
400     {
401         WORD32 offset;
402         offset = (UWORD8 *)pi2_coeff_data - (UWORD8 *)ps_tu_4x4;
403         offset = ALIGN4(offset);
404         ps_dec->pv_parse_tu_coeff_data = (void *)((UWORD8 *)ps_dec->pv_parse_tu_coeff_data + offset);
405     }
406 
407     ps_bitstrm->u4_ofst = u4_bitstream_offset;
408     return 0;
409 }
410 
411 /*****************************************************************************/
412 /*                                                                           */
413 /*  Function Name : ih264d_cavlc_4x4res_block_totalcoeff_2to10                      */
414 /*                                                                           */
415 /*  Description   : This function does cavlc decoding of 4x4 block residual  */
416 /*                  coefficient when total coeffs are between two and ten    */
417 /*                  inclusive. Parsing is done as defined in section 9.2.2   */
418 /*                  and 9.2.3 the H264 standard.                             */
419 /*                                                                           */
420 /*  Inputs        : <What inputs does the function take?>                    */
421 /*  Globals       : <Does it use any global variables?>                      */
422 /*  Processing    : <Describe how the function operates - include algorithm  */
423 /*                  description>                                             */
424 /*  Outputs       : <What does the function produce?>                        */
425 /*  Returns       : <What does the function return?>                         */
426 /*                                                                           */
427 /*  Issues        : <List any issues or problems with this function>         */
428 /*                                                                           */
429 /*  Revision History:                                                        */
430 /*                                                                           */
431 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
432 /*         25 09 2008   Jay          Draft                                   */
433 /*                                                                           */
434 /*****************************************************************************/
435 
ih264d_cavlc_4x4res_block_totalcoeff_2to10(UWORD32 u4_isdc,UWORD32 u4_total_coeff_trail_one,dec_bit_stream_t * ps_bitstrm)436 WORD32 ih264d_cavlc_4x4res_block_totalcoeff_2to10(UWORD32 u4_isdc,
437                                                UWORD32 u4_total_coeff_trail_one, /*!<TotalCoefficients<<16+trailingones*/
438                                                dec_bit_stream_t *ps_bitstrm)
439 {
440     UWORD32 u4_total_zeroes;
441     WORD32 i;
442     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
443     UWORD32 u4_bitstream_offset = ps_bitstrm->u4_ofst;
444     UWORD32 u4_trailing_ones = u4_total_coeff_trail_one & 0xFFFF;
445     UWORD32 u4_total_coeff = u4_total_coeff_trail_one >> 16;
446     WORD16 i2_level_arr[16];
447 
448     tu_sblk4x4_coeff_data_t *ps_tu_4x4;
449     WORD16 *pi2_coeff_data;
450     dec_struct_t *ps_dec = (dec_struct_t *)ps_bitstrm->pv_codec_handle;
451 
452     ps_tu_4x4 = (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
453     ps_tu_4x4->u2_sig_coeff_map = 0;
454     pi2_coeff_data = &ps_tu_4x4->ai2_level[0];
455 
456     i = u4_total_coeff - 1;
457 
458     if(u4_trailing_ones)
459     {
460         /*********************************************************************/
461         /* Decode Trailing Ones                                              */
462         /* read the sign of T1's and put them in level array                 */
463         /*********************************************************************/
464         UWORD32 u4_signs, u4_cnt = u4_trailing_ones;
465         WORD16 (*ppi2_trlone_lkup)[3] =
466                         (WORD16 (*)[3])gai2_ih264d_trailing_one_level;
467         WORD16 *pi2_trlone_lkup;
468 
469         GETBITS(u4_signs, u4_bitstream_offset, pu4_bitstrm_buf, u4_cnt);
470 
471         pi2_trlone_lkup = ppi2_trlone_lkup[(1 << u4_cnt) - 2 + u4_signs];
472 
473         while(u4_cnt--)
474             i2_level_arr[i--] = *pi2_trlone_lkup++;
475     }
476 
477     /****************************************************************/
478     /* Decoding Levels Begins                                       */
479     /****************************************************************/
480     if(i >= 0)
481     {
482         /****************************************************************/
483         /* First level is decoded outside the loop as it has lot of     */
484         /* special cases.                                               */
485         /****************************************************************/
486         UWORD32 u4_lev_suffix, u4_suffix_len, u4_lev_suffix_size;
487         WORD32 u2_lev_code, u2_abs_value;
488         UWORD32 u4_lev_prefix;
489 
490         /***************************************************************/
491         /* u4_suffix_len = 0,  Find leading zeros in next 32 bits      */
492         /***************************************************************/
493         FIND_ONE_IN_STREAM_32(u4_lev_prefix, u4_bitstream_offset,
494                               pu4_bitstrm_buf);
495 
496         /*********************************************************/
497         /* Special decoding case when trailing ones are 3        */
498         /*********************************************************/
499         u2_lev_code = MIN(15, u4_lev_prefix);
500 
501         u2_lev_code += (3 == u4_trailing_ones) ? 0 : 2;
502 
503         if(14 == u4_lev_prefix)
504             u4_lev_suffix_size = 4;
505         else if(15 <= u4_lev_prefix)
506         {
507             u2_lev_code += 15;
508             u4_lev_suffix_size = u4_lev_prefix - 3;
509         }
510         else
511             u4_lev_suffix_size = 0;
512 
513         //HP_LEVEL_PREFIX
514         if(16 <= u4_lev_prefix)
515         {
516             u2_lev_code += ((1 << (u4_lev_prefix - 3)) - 4096);
517         }
518         if(u4_lev_suffix_size)
519         {
520             GETBITS(u4_lev_suffix, u4_bitstream_offset, pu4_bitstrm_buf,
521                     u4_lev_suffix_size);
522             u2_lev_code += u4_lev_suffix;
523         }
524 
525         u2_abs_value = (u2_lev_code + 2) >> 1;
526         /*********************************************************/
527         /* If Level code is odd, level is negative else positive */
528         /*********************************************************/
529         i2_level_arr[i--] = (u2_lev_code & 1) ? -u2_abs_value : u2_abs_value;
530 
531         u4_suffix_len = (u2_abs_value > 3) ? 2 : 1;
532 
533         /*********************************************************/
534         /* Now loop over the remaining levels                    */
535         /*********************************************************/
536         while(i >= 0)
537         {
538 
539             /***************************************************************/
540             /* Find leading zeros in next 32 bits                          */
541             /***************************************************************/
542             FIND_ONE_IN_STREAM_32(u4_lev_prefix, u4_bitstream_offset,
543                                   pu4_bitstrm_buf);
544 
545             u4_lev_suffix_size =
546                             (15 <= u4_lev_prefix) ?
547                                             (u4_lev_prefix - 3) : u4_suffix_len;
548 
549             /*********************************************************/
550             /* Compute level code using prefix and suffix            */
551             /*********************************************************/
552             GETBITS(u4_lev_suffix, u4_bitstream_offset, pu4_bitstrm_buf,
553                     u4_lev_suffix_size);
554             u2_lev_code = (MIN(15,u4_lev_prefix) << u4_suffix_len)
555                             + u4_lev_suffix;
556 
557             //HP_LEVEL_PREFIX
558             if(16 <= u4_lev_prefix)
559             {
560                 u2_lev_code += ((1 << (u4_lev_prefix - 3)) - 4096);
561             }
562             u2_abs_value = (u2_lev_code + 2) >> 1;
563 
564             /*********************************************************/
565             /* If Level code is odd, level is negative else positive */
566             /*********************************************************/
567             i2_level_arr[i--] =
568                             (u2_lev_code & 1) ? -u2_abs_value : u2_abs_value;
569 
570             /*********************************************************/
571             /* Increment suffix length if required                   */
572             /*********************************************************/
573             u4_suffix_len +=
574                             (u4_suffix_len < 6) ?
575                                             (u2_abs_value
576                                                             > (3
577                                                                             << (u4_suffix_len
578                                                                                             - 1))) :
579                                             0;
580         }
581 
582         /****************************************************************/
583         /* Decoding Levels Ends                                         */
584         /****************************************************************/
585     }
586 
587     /****************************************************************/
588     /* Decoding total zeros as in section 9.2.3, table 9.7          */
589     /****************************************************************/
590     {
591         UWORD32 u4_index;
592         const UWORD8 (*ppu1_total_zero_lkup)[64] =
593                         (const UWORD8 (*)[64])gau1_ih264d_table_total_zero_2to10;
594 
595         NEXTBITS(u4_index, u4_bitstream_offset, pu4_bitstrm_buf, 6);
596         u4_total_zeroes = ppu1_total_zero_lkup[u4_total_coeff - 2][u4_index];
597 
598         FLUSHBITS(u4_bitstream_offset, (u4_total_zeroes >> 4));
599         u4_total_zeroes &= 0xf;
600     }
601 
602     /**************************************************************/
603     /* Decode the runs and form the coefficient buffer            */
604     /**************************************************************/
605     {
606         const UWORD8 *pu1_table_runbefore;
607         UWORD32 u4_run;
608         WORD32 k;
609         UWORD32 u4_scan_pos = u4_total_coeff + u4_total_zeroes - 1 + u4_isdc;
610         WORD32 u4_zeroes_left = u4_total_zeroes;
611         k = u4_total_coeff - 1;
612 
613         /**************************************************************/
614         /* Decoding Runs Begin for zeros left > 6                     */
615         /**************************************************************/
616         while((u4_zeroes_left > 6) && k)
617         {
618             UWORD32 u4_code;
619 
620             NEXTBITS(u4_code, u4_bitstream_offset, pu4_bitstrm_buf, 3);
621 
622             if(u4_code != 0)
623             {
624                 FLUSHBITS(u4_bitstream_offset, 3);
625                 u4_run = (7 - u4_code);
626             }
627             else
628             {
629 
630                 FIND_ONE_IN_STREAM_LEN(u4_code, u4_bitstream_offset,
631                                        pu4_bitstrm_buf, 11);
632                 u4_run = (4 + u4_code);
633             }
634 
635             SET_BIT(ps_tu_4x4->u2_sig_coeff_map, u4_scan_pos);
636             *pi2_coeff_data++ = i2_level_arr[k--];
637             u4_zeroes_left -= u4_run;
638             u4_scan_pos -= (u4_run + 1);
639         }
640 
641         /**************************************************************/
642         /* Decoding Runs for 0 < zeros left <=6                       */
643         /**************************************************************/
644         pu1_table_runbefore = (UWORD8 *)gau1_ih264d_table_run_before;
645         while((u4_zeroes_left > 0) && k)
646         {
647             UWORD32 u4_code;
648             NEXTBITS(u4_code, u4_bitstream_offset, pu4_bitstrm_buf, 3);
649 
650             u4_code = pu1_table_runbefore[u4_code + (u4_zeroes_left << 3)];
651             u4_run = u4_code >> 2;
652 
653             FLUSHBITS(u4_bitstream_offset, (u4_code & 0x03));
654 
655             SET_BIT(ps_tu_4x4->u2_sig_coeff_map, u4_scan_pos);
656             *pi2_coeff_data++ = i2_level_arr[k--];
657             u4_zeroes_left -= u4_run;
658             u4_scan_pos -= (u4_run + 1);
659         }
660         /**************************************************************/
661         /* Decoding Runs End                                          */
662         /**************************************************************/
663 
664         /**************************************************************/
665         /* Copy the remaining coefficients                            */
666         /**************************************************************/
667         if(u4_zeroes_left < 0)
668             return -1;
669         while(k >= 0)
670         {
671 
672             SET_BIT(ps_tu_4x4->u2_sig_coeff_map, u4_scan_pos);
673             *pi2_coeff_data++ = i2_level_arr[k--];
674             u4_scan_pos--;
675         }
676     }
677 
678     {
679         WORD32 offset;
680         offset = (UWORD8 *)pi2_coeff_data - (UWORD8 *)ps_tu_4x4;
681         offset = ALIGN4(offset);
682         ps_dec->pv_parse_tu_coeff_data = (void *)((UWORD8 *)ps_dec->pv_parse_tu_coeff_data + offset);
683     }
684 
685     ps_bitstrm->u4_ofst = u4_bitstream_offset;
686     return 0;
687 }
688 
689 /*****************************************************************************/
690 /*                                                                           */
691 /*  Function Name : ih264d_cavlc_4x4res_block_totalcoeff_11to16                     */
692 /*                                                                           */
693 /*  Description   : This function does cavlc decoding of 4x4 block residual  */
694 /*                  coefficient when total coeffs are greater than ten.      */
695 /*                  Parsing is done as defined in section 9.2.2 and 9.2.3 of */
696 /*                  the H264 standard.                                       */
697 /*                                                                           */
698 /*  Inputs        : <What inputs does the function take?>                    */
699 /*  Globals       : <Does it use any global variables?>                      */
700 /*  Processing    : <Describe how the function operates - include algorithm  */
701 /*                  description>                                             */
702 /*  Outputs       : <What does the function produce?>                        */
703 /*  Returns       : <What does the function return?>                         */
704 /*                                                                           */
705 /*  Issues        : <List any issues or problems with this function>         */
706 /*                                                                           */
707 /*  Revision History:                                                        */
708 /*                                                                           */
709 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
710 /*         25 09 2008   Jay          Draft                                   */
711 /*                                                                           */
712 /*****************************************************************************/
713 
ih264d_cavlc_4x4res_block_totalcoeff_11to16(UWORD32 u4_isdc,UWORD32 u4_total_coeff_trail_one,dec_bit_stream_t * ps_bitstrm)714 WORD32 ih264d_cavlc_4x4res_block_totalcoeff_11to16(UWORD32 u4_isdc,
715                                                 UWORD32 u4_total_coeff_trail_one, /*!<TotalCoefficients<<16+trailingones*/
716                                                 dec_bit_stream_t *ps_bitstrm )
717 {
718     UWORD32 u4_total_zeroes;
719     WORD32 i;
720     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
721     UWORD32 u4_bitstream_offset = ps_bitstrm->u4_ofst;
722     UWORD32 u4_trailing_ones = u4_total_coeff_trail_one & 0xFFFF;
723     UWORD32 u4_total_coeff = u4_total_coeff_trail_one >> 16;
724     WORD16 i2_level_arr[16];
725 
726     tu_sblk4x4_coeff_data_t *ps_tu_4x4;
727     WORD16 *pi2_coeff_data;
728     dec_struct_t *ps_dec = (dec_struct_t *)ps_bitstrm->pv_codec_handle;
729 
730     ps_tu_4x4 = (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
731     ps_tu_4x4->u2_sig_coeff_map = 0;
732     pi2_coeff_data = &ps_tu_4x4->ai2_level[0];
733 
734     i = u4_total_coeff - 1;
735     if(u4_trailing_ones)
736     {
737         /*********************************************************************/
738         /* Decode Trailing Ones                                              */
739         /* read the sign of T1's and put them in level array                 */
740         /*********************************************************************/
741         UWORD32 u4_signs, u4_cnt = u4_trailing_ones;
742         WORD16 (*ppi2_trlone_lkup)[3] =
743                         (WORD16 (*)[3])gai2_ih264d_trailing_one_level;
744         WORD16 *pi2_trlone_lkup;
745 
746         GETBITS(u4_signs, u4_bitstream_offset, pu4_bitstrm_buf, u4_cnt);
747 
748         pi2_trlone_lkup = ppi2_trlone_lkup[(1 << u4_cnt) - 2 + u4_signs];
749 
750         while(u4_cnt--)
751             i2_level_arr[i--] = *pi2_trlone_lkup++;
752     }
753 
754     /****************************************************************/
755     /* Decoding Levels Begins                                       */
756     /****************************************************************/
757     if(i >= 0)
758     {
759         /****************************************************************/
760         /* First level is decoded outside the loop as it has lot of     */
761         /* special cases.                                               */
762         /****************************************************************/
763         UWORD32 u4_lev_suffix, u4_suffix_len, u4_lev_suffix_size;
764         UWORD16 u2_lev_code, u2_abs_value;
765         UWORD32 u4_lev_prefix;
766 
767         if(u4_trailing_ones < 3)
768         {
769             /*********************************************************/
770             /* u4_suffix_len = 1                                     */
771             /*********************************************************/
772             /***************************************************************/
773             /* Find leading zeros in next 32 bits                          */
774             /***************************************************************/
775             FIND_ONE_IN_STREAM_32(u4_lev_prefix, u4_bitstream_offset,
776                                   pu4_bitstrm_buf);
777 
778             u4_lev_suffix_size =
779                             (15 <= u4_lev_prefix) ? (u4_lev_prefix - 3) : 1;
780 
781             GETBITS(u4_lev_suffix, u4_bitstream_offset, pu4_bitstrm_buf,
782                     u4_lev_suffix_size);
783             u2_lev_code = 2 + (MIN(u4_lev_prefix,15) << 1) + u4_lev_suffix;
784 
785             //HP_LEVEL_PREFIX
786             if(16 <= u4_lev_prefix)
787             {
788                 u2_lev_code += ((1 << (u4_lev_prefix - 3)) - 4096);
789             }
790         }
791         else
792         {
793             /*********************************************************/
794             /*u4_suffix_len = 0                                      */
795             /*********************************************************/
796             /***************************************************************/
797             /* Find leading zeros in next 32 bits                          */
798             /***************************************************************/
799             FIND_ONE_IN_STREAM_32(u4_lev_prefix, u4_bitstream_offset,
800                                   pu4_bitstrm_buf);
801 
802             /*********************************************************/
803             /* Special decoding case when trailing ones are 3        */
804             /*********************************************************/
805             u2_lev_code = MIN(15, u4_lev_prefix);
806 
807             u2_lev_code += (3 == u4_trailing_ones) ? 0 : (2);
808 
809             if(14 == u4_lev_prefix)
810                 u4_lev_suffix_size = 4;
811             else if(15 <= u4_lev_prefix)
812             {
813                 u2_lev_code += 15;
814                 u4_lev_suffix_size = (u4_lev_prefix - 3);
815             }
816             else
817                 u4_lev_suffix_size = 0;
818 
819             //HP_LEVEL_PREFIX
820             if(16 <= u4_lev_prefix)
821             {
822                 u2_lev_code += ((1 << (u4_lev_prefix - 3)) - 4096);
823             }
824             if(u4_lev_suffix_size)
825             {
826                 GETBITS(u4_lev_suffix, u4_bitstream_offset, pu4_bitstrm_buf,
827                         u4_lev_suffix_size);
828                 u2_lev_code += u4_lev_suffix;
829             }
830         }
831 
832         u2_abs_value = (u2_lev_code + 2) >> 1;
833         /*********************************************************/
834         /* If Level code is odd, level is negative else positive */
835         /*********************************************************/
836         i2_level_arr[i--] = (u2_lev_code & 1) ? -u2_abs_value : u2_abs_value;
837 
838         u4_suffix_len = (u2_abs_value > 3) ? 2 : 1;
839 
840         /*********************************************************/
841         /* Now loop over the remaining levels                    */
842         /*********************************************************/
843         while(i >= 0)
844         {
845 
846             /***************************************************************/
847             /* Find leading zeros in next 32 bits                          */
848             /***************************************************************/
849             FIND_ONE_IN_STREAM_32(u4_lev_prefix, u4_bitstream_offset,
850                                   pu4_bitstrm_buf);
851 
852             u4_lev_suffix_size =
853                             (15 <= u4_lev_prefix) ?
854                                             (u4_lev_prefix - 3) : u4_suffix_len;
855 
856             /*********************************************************/
857             /* Compute level code using prefix and suffix            */
858             /*********************************************************/
859             GETBITS(u4_lev_suffix, u4_bitstream_offset, pu4_bitstrm_buf,
860                     u4_lev_suffix_size);
861             u2_lev_code = (MIN(15,u4_lev_prefix) << u4_suffix_len)
862                             + u4_lev_suffix;
863 
864             //HP_LEVEL_PREFIX
865             if(16 <= u4_lev_prefix)
866             {
867                 u2_lev_code += ((1 << (u4_lev_prefix - 3)) - 4096);
868             }
869             u2_abs_value = (u2_lev_code + 2) >> 1;
870 
871             /*********************************************************/
872             /* If Level code is odd, level is negative else positive */
873             /*********************************************************/
874             i2_level_arr[i--] =
875                             (u2_lev_code & 1) ? -u2_abs_value : u2_abs_value;
876 
877             /*********************************************************/
878             /* Increment suffix length if required                   */
879             /*********************************************************/
880             u4_suffix_len +=
881                             (u4_suffix_len < 6) ?
882                                             (u2_abs_value
883                                                             > (3
884                                                                             << (u4_suffix_len
885                                                                                             - 1))) :
886                                             0;
887         }
888 
889         /****************************************************************/
890         /* Decoding Levels Ends                                         */
891         /****************************************************************/
892     }
893 
894     if(u4_total_coeff < (16 - u4_isdc))
895     {
896         UWORD32 u4_index;
897         const UWORD8 (*ppu1_total_zero_lkup)[16] =
898                         (const UWORD8 (*)[16])gau1_ih264d_table_total_zero_11to15;
899 
900         NEXTBITS(u4_index, u4_bitstream_offset, pu4_bitstrm_buf, 4);
901         u4_total_zeroes = ppu1_total_zero_lkup[u4_total_coeff - 11][u4_index];
902 
903         FLUSHBITS(u4_bitstream_offset, (u4_total_zeroes >> 4));
904         u4_total_zeroes &= 0xf;
905     }
906     else
907         u4_total_zeroes = 0;
908 
909     /**************************************************************/
910     /* Decode the runs and form the coefficient buffer            */
911     /**************************************************************/
912     {
913         const UWORD8 *pu1_table_runbefore;
914         UWORD32 u4_run;
915         WORD32 k;
916         UWORD32 u4_scan_pos = u4_total_coeff + u4_total_zeroes - 1 + u4_isdc;
917         WORD32 u4_zeroes_left = u4_total_zeroes;
918         k = u4_total_coeff - 1;
919 
920         /**************************************************************/
921         /* Decoding Runs for 0 < zeros left <=6                       */
922         /**************************************************************/
923         pu1_table_runbefore = (UWORD8 *)gau1_ih264d_table_run_before;
924         while((u4_zeroes_left > 0) && k)
925         {
926             UWORD32 u4_code;
927             NEXTBITS(u4_code, u4_bitstream_offset, pu4_bitstrm_buf, 3);
928 
929             u4_code = pu1_table_runbefore[u4_code + (u4_zeroes_left << 3)];
930             u4_run = u4_code >> 2;
931 
932             FLUSHBITS(u4_bitstream_offset, (u4_code & 0x03));
933             SET_BIT(ps_tu_4x4->u2_sig_coeff_map, u4_scan_pos);
934             *pi2_coeff_data++ = i2_level_arr[k--];
935             u4_zeroes_left -= u4_run;
936             u4_scan_pos -= (u4_run + 1);
937         }
938         /**************************************************************/
939         /* Decoding Runs End                                          */
940         /**************************************************************/
941 
942         /**************************************************************/
943         /* Copy the remaining coefficients                            */
944         /**************************************************************/
945         if(u4_zeroes_left < 0)
946             return -1;
947         while(k >= 0)
948         {
949             SET_BIT(ps_tu_4x4->u2_sig_coeff_map, u4_scan_pos);
950             *pi2_coeff_data++ = i2_level_arr[k--];
951             u4_scan_pos--;
952         }
953     }
954 
955     {
956         WORD32 offset;
957         offset = (UWORD8 *)pi2_coeff_data - (UWORD8 *)ps_tu_4x4;
958         offset = ALIGN4(offset);
959         ps_dec->pv_parse_tu_coeff_data = (void *)((UWORD8 *)ps_dec->pv_parse_tu_coeff_data + offset);
960     }
961 
962     ps_bitstrm->u4_ofst = u4_bitstream_offset;
963     return 0;
964 }
965 
966 /*****************************************************************************/
967 /*                                                                           */
968 /*  Function Name : ih264d_rest_of_residual_cav_chroma_dc_block              */
969 /*                                                                           */
970 /*  Description   : This function does the Cavlc parsing of the bitstream    */
971 /*                  for chroma dc coefficients                               */
972 /*  Inputs        : <What inputs does the function take?>                    */
973 /*  Globals       : <Does it use any global variables?>                      */
974 /*  Processing    : <Describe how the function operates - include algorithm  */
975 /*                  description>                                             */
976 /*  Outputs       : <What does the function produce?>                        */
977 /*  Returns       : <What does the function return?>                         */
978 /*                                                                           */
979 /*  Issues        : <List any issues or problems with this function>         */
980 /*                                                                           */
981 /*  Revision History:                                                        */
982 /*                                                                           */
983 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
984 /*         15 09 2008   Jay          Draft                                   */
985 /*                                                                           */
986 /*****************************************************************************/
ih264d_rest_of_residual_cav_chroma_dc_block(UWORD32 u4_total_coeff_trail_one,dec_bit_stream_t * ps_bitstrm)987 void ih264d_rest_of_residual_cav_chroma_dc_block(UWORD32 u4_total_coeff_trail_one,
988                                                  dec_bit_stream_t *ps_bitstrm)
989 {
990     UWORD32 u4_total_zeroes;
991     WORD16 i;
992     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
993     UWORD32 u4_bitstream_offset = ps_bitstrm->u4_ofst;
994     UWORD32 u4_trailing_ones = u4_total_coeff_trail_one & 0xFFFF;
995     UWORD32 u4_total_coeff = u4_total_coeff_trail_one >> 16;
996     WORD16 i2_level_arr[4];
997 
998     tu_sblk4x4_coeff_data_t *ps_tu_4x4;
999     WORD16 *pi2_coeff_data;
1000     dec_struct_t *ps_dec = (dec_struct_t *)ps_bitstrm->pv_codec_handle;
1001 
1002     ps_tu_4x4 = (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
1003     ps_tu_4x4->u2_sig_coeff_map = 0;
1004     pi2_coeff_data = &ps_tu_4x4->ai2_level[0];
1005 
1006     i = u4_total_coeff - 1;
1007     if(u4_trailing_ones)
1008     {
1009         /*********************************************************************/
1010         /* Decode Trailing Ones                                              */
1011         /* read the sign of T1's and put them in level array                 */
1012         /*********************************************************************/
1013         UWORD32 u4_signs, u4_cnt = u4_trailing_ones;
1014         WORD16 (*ppi2_trlone_lkup)[3] =
1015                         (WORD16 (*)[3])gai2_ih264d_trailing_one_level;
1016         WORD16 *pi2_trlone_lkup;
1017 
1018         GETBITS(u4_signs, u4_bitstream_offset, pu4_bitstrm_buf, u4_cnt);
1019 
1020         pi2_trlone_lkup = ppi2_trlone_lkup[(1 << u4_cnt) - 2 + u4_signs];
1021 
1022         while(u4_cnt--)
1023             i2_level_arr[i--] = *pi2_trlone_lkup++;
1024     }
1025 
1026     /****************************************************************/
1027     /* Decoding Levels Begins                                       */
1028     /****************************************************************/
1029     if(i >= 0)
1030     {
1031         /****************************************************************/
1032         /* First level is decoded outside the loop as it has lot of     */
1033         /* special cases.                                               */
1034         /****************************************************************/
1035         UWORD32 u4_lev_suffix, u4_suffix_len, u4_lev_suffix_size;
1036         UWORD16 u2_lev_code, u2_abs_value;
1037         UWORD32 u4_lev_prefix;
1038 
1039         /***************************************************************/
1040         /* u4_suffix_len = 0,  Find leading zeros in next 32 bits      */
1041         /***************************************************************/
1042         FIND_ONE_IN_STREAM_32(u4_lev_prefix, u4_bitstream_offset,
1043                               pu4_bitstrm_buf);
1044 
1045         /*********************************************************/
1046         /* Special decoding case when trailing ones are 3        */
1047         /*********************************************************/
1048         u2_lev_code = MIN(15, u4_lev_prefix);
1049 
1050         u2_lev_code += (3 == u4_trailing_ones) ? 0 : (2);
1051 
1052         if(14 == u4_lev_prefix)
1053             u4_lev_suffix_size = 4;
1054         else if(15 <= u4_lev_prefix)
1055         {
1056             u2_lev_code += 15;
1057             u4_lev_suffix_size = u4_lev_prefix - 3;
1058         }
1059         else
1060             u4_lev_suffix_size = 0;
1061 
1062         //HP_LEVEL_PREFIX
1063         if(16 <= u4_lev_prefix)
1064         {
1065             u2_lev_code += ((1 << (u4_lev_prefix - 3)) - 4096);
1066         }
1067         if(u4_lev_suffix_size)
1068         {
1069             GETBITS(u4_lev_suffix, u4_bitstream_offset, pu4_bitstrm_buf,
1070                     u4_lev_suffix_size);
1071             u2_lev_code += u4_lev_suffix;
1072         }
1073 
1074         u2_abs_value = (u2_lev_code + 2) >> 1;
1075         /*********************************************************/
1076         /* If Level code is odd, level is negative else positive */
1077         /*********************************************************/
1078         i2_level_arr[i--] = (u2_lev_code & 1) ? -u2_abs_value : u2_abs_value;
1079 
1080         u4_suffix_len = (u2_abs_value > 3) ? 2 : 1;
1081 
1082         /*********************************************************/
1083         /* Now loop over the remaining levels                    */
1084         /*********************************************************/
1085         while(i >= 0)
1086         {
1087 
1088             /***************************************************************/
1089             /* Find leading zeros in next 32 bits                          */
1090             /***************************************************************/
1091             FIND_ONE_IN_STREAM_32(u4_lev_prefix, u4_bitstream_offset,
1092                                   pu4_bitstrm_buf);
1093 
1094             u4_lev_suffix_size =
1095                             (15 <= u4_lev_prefix) ?
1096                                             (u4_lev_prefix - 3) : u4_suffix_len;
1097 
1098             /*********************************************************/
1099             /* Compute level code using prefix and suffix            */
1100             /*********************************************************/
1101             GETBITS(u4_lev_suffix, u4_bitstream_offset, pu4_bitstrm_buf,
1102                     u4_lev_suffix_size);
1103             u2_lev_code = (MIN(u4_lev_prefix,15) << u4_suffix_len)
1104                             + u4_lev_suffix;
1105 
1106             //HP_LEVEL_PREFIX
1107             if(16 <= u4_lev_prefix)
1108             {
1109                 u2_lev_code += ((1 << (u4_lev_prefix - 3)) - 4096);
1110             }
1111             u2_abs_value = (u2_lev_code + 2) >> 1;
1112 
1113             /*********************************************************/
1114             /* If Level code is odd, level is negative else positive */
1115             /*********************************************************/
1116             i2_level_arr[i--] =
1117                             (u2_lev_code & 1) ? -u2_abs_value : u2_abs_value;
1118 
1119             /*********************************************************/
1120             /* Increment suffix length if required                   */
1121             /*********************************************************/
1122             u4_suffix_len += (u2_abs_value > (3 << (u4_suffix_len - 1)));
1123         }
1124 
1125         /****************************************************************/
1126         /* Decoding Levels Ends                                         */
1127         /****************************************************************/
1128     }
1129 
1130     if(u4_total_coeff < 4)
1131     {
1132         UWORD32 u4_max_ldz = (4 - u4_total_coeff);
1133         FIND_ONE_IN_STREAM_LEN(u4_total_zeroes, u4_bitstream_offset,
1134                                pu4_bitstrm_buf, u4_max_ldz);
1135     }
1136     else
1137         u4_total_zeroes = 0;
1138 
1139     /**************************************************************/
1140     /* Decode the runs and form the coefficient buffer            */
1141     /**************************************************************/
1142     {
1143         const UWORD8 *pu1_table_runbefore;
1144         UWORD32 u4_run;
1145         UWORD32 u4_scan_pos = (u4_total_coeff + u4_total_zeroes - 1);
1146         UWORD32 u4_zeroes_left = u4_total_zeroes;
1147         i = u4_total_coeff - 1;
1148 
1149         /**************************************************************/
1150         /* Decoding Runs for 0 < zeros left <=6                       */
1151         /**************************************************************/
1152         pu1_table_runbefore = (UWORD8 *)gau1_ih264d_table_run_before;
1153         while(u4_zeroes_left && i)
1154         {
1155             UWORD32 u4_code;
1156             NEXTBITS(u4_code, u4_bitstream_offset, pu4_bitstrm_buf, 3);
1157 
1158             u4_code = pu1_table_runbefore[u4_code + (u4_zeroes_left << 3)];
1159             u4_run = u4_code >> 2;
1160 
1161             FLUSHBITS(u4_bitstream_offset, (u4_code & 0x03));
1162             SET_BIT(ps_tu_4x4->u2_sig_coeff_map, u4_scan_pos);
1163             *pi2_coeff_data++ = i2_level_arr[i--];
1164             u4_zeroes_left -= u4_run;
1165             u4_scan_pos -= (u4_run + 1);
1166         }
1167         /**************************************************************/
1168         /* Decoding Runs End                                          */
1169         /**************************************************************/
1170 
1171         /**************************************************************/
1172         /* Copy the remaining coefficients                            */
1173         /**************************************************************/
1174         while(i >= 0)
1175         {
1176             SET_BIT(ps_tu_4x4->u2_sig_coeff_map, u4_scan_pos);
1177             *pi2_coeff_data++ = i2_level_arr[i--];
1178             u4_scan_pos--;
1179         }
1180     }
1181 
1182     {
1183         WORD32 offset;
1184         offset = (UWORD8 *)pi2_coeff_data - (UWORD8 *)ps_tu_4x4;
1185         offset = ALIGN4(offset);
1186         ps_dec->pv_parse_tu_coeff_data = (void *)((UWORD8 *)ps_dec->pv_parse_tu_coeff_data + offset);
1187     }
1188 
1189     ps_bitstrm->u4_ofst = u4_bitstream_offset;
1190 }
1191 
1192 /*!
1193  **************************************************************************
1194  * \if Function name : CavlcParsingInvScanInvQuant \endif
1195  *
1196  * \brief
1197  *    This function do cavlc parsing of coefficient tokens for any block
1198  *    type except chromDc and depending
1199  *    on whenther any coefficients to be parsed calls module
1200  *    RestOfResidualBlockCavlc.
1201  *
1202  * \return
1203  *    Returns total number of non-zero coefficients.
1204  *
1205  **************************************************************************
1206  */
1207 
ih264d_cavlc_parse4x4coeff_n0to7(WORD16 * pi2_coeff_block,UWORD32 u4_isdc,WORD32 u4_n,dec_struct_t * ps_dec,UWORD32 * pu4_total_coeff)1208 WORD32 ih264d_cavlc_parse4x4coeff_n0to7(WORD16 *pi2_coeff_block,
1209                                         UWORD32 u4_isdc, /* is it a DC block */
1210                                         WORD32 u4_n,
1211                                         dec_struct_t *ps_dec,
1212                                         UWORD32 *pu4_total_coeff)
1213 {
1214     dec_bit_stream_t *ps_bitstrm = ps_dec->ps_bitstrm;
1215     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
1216     UWORD32 u4_bitstream_offset = ps_bitstrm->u4_ofst;
1217     UWORD32 u4_code, u4_index, u4_ldz;
1218     const UWORD16 *pu2_code = (const UWORD16*)gau2_ih264d_code_gx;
1219     const UWORD16 *pu2_offset_num_vlc =
1220                     (const UWORD16 *)gau2_ih264d_offset_num_vlc_tab;
1221     UWORD32 u4_offset_num_vlc = pu2_offset_num_vlc[u4_n];
1222 
1223 
1224     UNUSED(pi2_coeff_block);
1225     *pu4_total_coeff = 0;
1226     FIND_ONE_IN_STREAM_32(u4_ldz, u4_bitstream_offset, pu4_bitstrm_buf);
1227     NEXTBITS(u4_index, u4_bitstream_offset, pu4_bitstrm_buf, 3);
1228     u4_index += (u4_ldz << 3);
1229     u4_index += u4_offset_num_vlc;
1230 
1231     u4_index = MIN(u4_index, 303);
1232     u4_code = pu2_code[u4_index];
1233 
1234     FLUSHBITS(u4_bitstream_offset, (u4_code & 0x03));
1235     ps_bitstrm->u4_ofst = u4_bitstream_offset;
1236     *pu4_total_coeff = (u4_code >> 4);
1237 
1238     if(*pu4_total_coeff)
1239     {
1240         UWORD32 u4_trailing_ones, u4_offset, u4_total_coeff_tone;
1241         const UWORD8 *pu1_offset =
1242                         (UWORD8 *)gau1_ih264d_total_coeff_fn_ptr_offset;
1243         WORD32 ret;
1244         u4_trailing_ones = ((u4_code >> 2) & 0x03);
1245         u4_offset = pu1_offset[*pu4_total_coeff - 1];
1246         u4_total_coeff_tone = (*pu4_total_coeff << 16) | u4_trailing_ones;
1247 
1248         ret = ps_dec->pf_cavlc_4x4res_block[u4_offset](u4_isdc,
1249                                                        u4_total_coeff_tone,
1250                                                        ps_bitstrm);
1251         if(ret != 0)
1252             return ERROR_CAVLC_NUM_COEFF_T;
1253     }
1254 
1255     return OK;
1256 }
1257 
ih264d_cavlc_parse4x4coeff_n8(WORD16 * pi2_coeff_block,UWORD32 u4_isdc,WORD32 u4_n,dec_struct_t * ps_dec,UWORD32 * pu4_total_coeff)1258 WORD32 ih264d_cavlc_parse4x4coeff_n8(WORD16 *pi2_coeff_block,
1259                                      UWORD32 u4_isdc, /* is it a DC block */
1260                                      WORD32 u4_n,
1261                                      dec_struct_t *ps_dec,
1262                                      UWORD32 *pu4_total_coeff)
1263 {
1264 
1265     dec_bit_stream_t *ps_bitstrm = ps_dec->ps_bitstrm;
1266     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
1267     UWORD32 u4_bitstream_offset = ps_bitstrm->u4_ofst;
1268     UWORD32 u4_code;
1269     UNUSED(u4_n);
1270     UNUSED(pi2_coeff_block);
1271     GETBITS(u4_code, u4_bitstream_offset, pu4_bitstrm_buf, 6);
1272     ps_bitstrm->u4_ofst = u4_bitstream_offset;
1273     *pu4_total_coeff = 0;
1274 
1275     if(u4_code != 3)
1276     {
1277         UWORD8 *pu1_offset = (UWORD8 *)gau1_ih264d_total_coeff_fn_ptr_offset;
1278         UWORD32 u4_trailing_ones, u4_offset, u4_total_coeff_tone;
1279 
1280         *pu4_total_coeff = (u4_code >> 2) + 1;
1281         u4_trailing_ones = u4_code & 0x03;
1282         u4_offset = pu1_offset[*pu4_total_coeff - 1];
1283         u4_total_coeff_tone = (*pu4_total_coeff << 16) | u4_trailing_ones;
1284 
1285         ps_dec->pf_cavlc_4x4res_block[u4_offset](u4_isdc,
1286                                                  u4_total_coeff_tone,
1287                                                  ps_bitstrm);
1288     }
1289 
1290     return OK;
1291 }
1292 
1293 /*!
1294  **************************************************************************
1295  * \if Function name : ih264d_cavlc_parse_chroma_dc \endif
1296  *
1297  * \brief
1298  *    This function do cavlc parsing of coefficient tokens chromDc block
1299  *    and depending  on whenther any coefficients to be parsed calls module
1300  *    ih264d_rest_of_residual_cav_chroma_dc_block.
1301  *
1302  * \return
1303  *    Returns total number of non-zero coefficients.
1304  *
1305  **************************************************************************
1306  */
1307 
ih264d_cavlc_parse_chroma_dc(dec_mb_info_t * ps_cur_mb_info,WORD16 * pi2_coeff_block,dec_bit_stream_t * ps_bitstrm,UWORD32 u4_scale_u,UWORD32 u4_scale_v,WORD32 i4_mb_inter_inc)1308 void ih264d_cavlc_parse_chroma_dc(dec_mb_info_t *ps_cur_mb_info,
1309                                   WORD16 *pi2_coeff_block,
1310                                   dec_bit_stream_t *ps_bitstrm,
1311                                   UWORD32 u4_scale_u,
1312                                   UWORD32 u4_scale_v,
1313                                   WORD32 i4_mb_inter_inc)
1314 {
1315     UWORD32 u4_total_coeff, u4_trailing_ones, u4_total_coeff_tone, u4_code;
1316     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
1317     UWORD32 u4_bitstream_offset = ps_bitstrm->u4_ofst;
1318     const UWORD8 *pu1_cav_chromdc = (const UWORD8*)gau1_ih264d_cav_chromdc_vld;
1319     UNUSED(i4_mb_inter_inc);
1320     /******************************************************************/
1321     /*  Chroma DC Block for U component                               */
1322     /******************************************************************/
1323     NEXTBITS(u4_code, u4_bitstream_offset, pu4_bitstrm_buf, 8);
1324 
1325     u4_code = pu1_cav_chromdc[u4_code];
1326 
1327     FLUSHBITS(u4_bitstream_offset, ((u4_code & 0x7) + 1));
1328     ps_bitstrm->u4_ofst = u4_bitstream_offset;
1329 
1330     u4_total_coeff = (u4_code >> 5);
1331 
1332     if(u4_total_coeff)
1333     {
1334         WORD32 i_z0, i_z1, i_z2, i_z3;
1335         tu_sblk4x4_coeff_data_t *ps_tu_4x4;
1336         dec_struct_t *ps_dec = (dec_struct_t *)ps_bitstrm->pv_codec_handle;
1337         WORD16 ai2_dc_coef[4];
1338         UWORD8 pu1_inv_scan[4] =
1339                         { 0, 1, 2, 3 };
1340         WORD16 *pi2_coeff_data =
1341                                     (WORD16 *)ps_dec->pv_parse_tu_coeff_data;
1342 
1343         ps_tu_4x4 = (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
1344 
1345         u4_trailing_ones = ((u4_code >> 3) & 0x3);
1346         u4_total_coeff_tone = (u4_total_coeff << 16) | u4_trailing_ones;
1347         ih264d_rest_of_residual_cav_chroma_dc_block(u4_total_coeff_tone,
1348                                                     ps_bitstrm);
1349 
1350         ai2_dc_coef[0] = 0;
1351         ai2_dc_coef[1] = 0;
1352         ai2_dc_coef[2] = 0;
1353         ai2_dc_coef[3] = 0;
1354 
1355         ih264d_unpack_coeff4x4_dc_4x4blk(ps_tu_4x4,
1356                                          ai2_dc_coef,
1357                                          pu1_inv_scan);
1358         /*-------------------------------------------------------------------*/
1359         /* Inverse 2x2 transform and scaling  of chroma DC                   */
1360         /*-------------------------------------------------------------------*/
1361         i_z0 = (ai2_dc_coef[0] + ai2_dc_coef[2]);
1362         i_z1 = (ai2_dc_coef[0] - ai2_dc_coef[2]);
1363         i_z2 = (ai2_dc_coef[1] - ai2_dc_coef[3]);
1364         i_z3 = (ai2_dc_coef[1] + ai2_dc_coef[3]);
1365 
1366         /*-----------------------------------------------------------*/
1367         /* Scaling and storing the values back                       */
1368         /*-----------------------------------------------------------*/
1369         *pi2_coeff_data++ = ((i_z0 + i_z3) * u4_scale_u) >> 5;
1370         *pi2_coeff_data++ = ((i_z0 - i_z3) * u4_scale_u) >> 5;
1371         *pi2_coeff_data++ = ((i_z1 + i_z2) * u4_scale_u) >> 5;
1372         *pi2_coeff_data++ = ((i_z1 - i_z2) * u4_scale_u) >> 5;
1373 
1374         ps_dec->pv_parse_tu_coeff_data = (void *)pi2_coeff_data;
1375 
1376         SET_BIT(ps_cur_mb_info->u1_yuv_dc_block_flag,1);
1377     }
1378 
1379     /******************************************************************/
1380     /*  Chroma DC Block for V component                               */
1381     /******************************************************************/
1382     pi2_coeff_block += 64;
1383     u4_bitstream_offset = ps_bitstrm->u4_ofst;
1384 
1385     NEXTBITS(u4_code, u4_bitstream_offset, pu4_bitstrm_buf, 8);
1386 
1387     u4_code = pu1_cav_chromdc[u4_code];
1388 
1389     FLUSHBITS(u4_bitstream_offset, ((u4_code & 0x7) + 1));
1390     ps_bitstrm->u4_ofst = u4_bitstream_offset;
1391 
1392     u4_total_coeff = (u4_code >> 5);
1393 
1394     if(u4_total_coeff)
1395     {
1396         WORD32 i_z0, i_z1, i_z2, i_z3;
1397         tu_sblk4x4_coeff_data_t *ps_tu_4x4;
1398         dec_struct_t *ps_dec = (dec_struct_t *)ps_bitstrm->pv_codec_handle;
1399         WORD16 ai2_dc_coef[4];
1400         UWORD8 pu1_inv_scan[4] =
1401                         { 0, 1, 2, 3 };
1402         WORD16 *pi2_coeff_data =
1403                                     (WORD16 *)ps_dec->pv_parse_tu_coeff_data;
1404 
1405         ps_tu_4x4 = (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
1406 
1407         u4_trailing_ones = ((u4_code >> 3) & 0x3);
1408         u4_total_coeff_tone = (u4_total_coeff << 16) | u4_trailing_ones;
1409         ih264d_rest_of_residual_cav_chroma_dc_block(u4_total_coeff_tone,
1410                                                     ps_bitstrm);
1411 
1412         ai2_dc_coef[0] = 0;
1413         ai2_dc_coef[1] = 0;
1414         ai2_dc_coef[2] = 0;
1415         ai2_dc_coef[3] = 0;
1416 
1417         ih264d_unpack_coeff4x4_dc_4x4blk(ps_tu_4x4,
1418                                          ai2_dc_coef,
1419                                          pu1_inv_scan);
1420 
1421         /*-------------------------------------------------------------------*/
1422         /* Inverse 2x2 transform and scaling  of chroma DC                   */
1423         /*-------------------------------------------------------------------*/
1424         i_z0 = (ai2_dc_coef[0] + ai2_dc_coef[2]);
1425         i_z1 = (ai2_dc_coef[0] - ai2_dc_coef[2]);
1426         i_z2 = (ai2_dc_coef[1] - ai2_dc_coef[3]);
1427         i_z3 = (ai2_dc_coef[1] + ai2_dc_coef[3]);
1428 
1429         /*-----------------------------------------------------------*/
1430         /* Scaling and storing the values back                       */
1431         /*-----------------------------------------------------------*/
1432         *pi2_coeff_data++ = ((i_z0 + i_z3) * u4_scale_v) >> 5;
1433         *pi2_coeff_data++ = ((i_z0 - i_z3) * u4_scale_v) >> 5;
1434         *pi2_coeff_data++ = ((i_z1 + i_z2) * u4_scale_v) >> 5;
1435         *pi2_coeff_data++ = ((i_z1 - i_z2) * u4_scale_v) >> 5;
1436 
1437         ps_dec->pv_parse_tu_coeff_data = (void *)pi2_coeff_data;
1438 
1439         SET_BIT(ps_cur_mb_info->u1_yuv_dc_block_flag,2);
1440     }
1441 }
1442 
1443 /*****************************************************************************/
1444 /*                                                                           */
1445 /*  Function Name : ih264d_parse_pmb_ref_index_cavlc_range1                         */
1446 /*                                                                           */
1447 /*  Description   : This function does the Cavlc  TEV range =1 parsing of    */
1448 /*                  reference index  for a P MB. Range is 1 when             */
1449 /*                  num_ref_idx_active_minus1 is 0                           */
1450 /*                                                                           */
1451 /*  Inputs        : <What inputs does the function take?>                    */
1452 /*  Globals       : <Does it use any global variables?>                      */
1453 /*  Processing    : <Describe how the function operates - include algorithm  */
1454 /*                  description>                                             */
1455 /*  Outputs       : <What does the function produce?>                        */
1456 /*  Returns       : <What does the function return?>                         */
1457 /*                                                                           */
1458 /*  Issues        : <List any issues or problems with this function>         */
1459 /*                                                                           */
1460 /*  Revision History:                                                        */
1461 /*                                                                           */
1462 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1463 /*         19 09 2008   Jay          Draft                                   */
1464 /*                                                                           */
1465 /*****************************************************************************/
ih264d_parse_pmb_ref_index_cavlc_range1(UWORD32 u4_num_part,dec_bit_stream_t * ps_bitstrm,WORD8 * pi1_ref_idx,UWORD32 u4_num_ref_idx_active_minus1)1466 void ih264d_parse_pmb_ref_index_cavlc_range1(UWORD32 u4_num_part, /* Number of partitions in MB      */
1467                                              dec_bit_stream_t *ps_bitstrm, /* Pointer to bitstream Structure. */
1468                                              WORD8 *pi1_ref_idx, /* pointer to reference index array */
1469                                              UWORD32 u4_num_ref_idx_active_minus1 /* Not used for range 1    */
1470                                              )
1471 {
1472     UWORD32 u4_i;
1473     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
1474     UWORD32 *pu4_bitstream_off = &ps_bitstrm->u4_ofst;
1475     UNUSED(u4_num_ref_idx_active_minus1);
1476     for(u4_i = 0; u4_i < u4_num_part; u4_i++)
1477     {
1478         UWORD32 u4_ref_idx;
1479         u4_ref_idx = ih264d_tev_range1(pu4_bitstream_off, pu4_bitstrm_buf);
1480 
1481         /* Storing Reference Idx Information */
1482         pi1_ref_idx[u4_i] = (WORD8)u4_ref_idx;
1483     }
1484 }
1485 
1486 /*****************************************************************************/
1487 /*                                                                           */
1488 /*  Function Name : ih264d_parse_pmb_ref_index_cavlc                                */
1489 /*                                                                           */
1490 /*  Description   : This function does the Cavlc  TEV range > 1 parsing of   */
1491 /*                  reference index  for a P MB.                             */
1492 /*                  Range > 1 when num_ref_idx_active_minus1 > 0             */
1493 /*                                                                           */
1494 /*  Inputs        : <What inputs does the function take?>                    */
1495 /*  Globals       : <Does it use any global variables?>                      */
1496 /*  Processing    : <Describe how the function operates - include algorithm  */
1497 /*                  description>                                             */
1498 /*  Outputs       : <What does the function produce?>                        */
1499 /*  Returns       : <What does the function return?>                         */
1500 /*                                                                           */
1501 /*  Issues        : <List any issues or problems with this function>         */
1502 /*                                                                           */
1503 /*  Revision History:                                                        */
1504 /*                                                                           */
1505 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1506 /*         19 09 2008   Jay          Draft                                   */
1507 /*                                                                           */
1508 /*****************************************************************************/
1509 
ih264d_parse_pmb_ref_index_cavlc(UWORD32 u4_num_part,dec_bit_stream_t * ps_bitstrm,WORD8 * pi1_ref_idx,UWORD32 u4_num_ref_idx_active_minus1)1510 WORD32 ih264d_parse_pmb_ref_index_cavlc(UWORD32 u4_num_part, /* Number of partitions in MB      */
1511                                       dec_bit_stream_t *ps_bitstrm, /* Pointer to bitstream Structure. */
1512                                       WORD8 *pi1_ref_idx, /* pointer to reference index array */
1513                                       UWORD32 u4_num_ref_idx_active_minus1 /* Number of active references - 1  */
1514                                       )
1515 {
1516     UWORD32 u4_i;
1517     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
1518     UWORD32 *pu4_bitstream_off = &ps_bitstrm->u4_ofst;
1519 
1520     for(u4_i = 0; u4_i < u4_num_part; u4_i++)
1521     {
1522         UWORD32 u4_ref_idx;
1523 //Inlined ih264d_uev
1524         UWORD32 u4_bitstream_offset = *pu4_bitstream_off;
1525         UWORD32 u4_word, u4_ldz;
1526 
1527         /***************************************************************/
1528         /* Find leading zeros in next 32 bits                          */
1529         /***************************************************************/
1530         NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
1531         u4_ldz = CLZ(u4_word);
1532         /* Flush the ps_bitstrm */
1533         u4_bitstream_offset += (u4_ldz + 1);
1534         /* Read the suffix from the ps_bitstrm */
1535         u4_word = 0;
1536         if(u4_ldz)
1537             GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf, u4_ldz);
1538         *pu4_bitstream_off = u4_bitstream_offset;
1539         u4_ref_idx = ((1 << u4_ldz) + u4_word - 1);
1540 //Inlined ih264d_uev
1541 
1542         if(u4_ref_idx > u4_num_ref_idx_active_minus1)
1543             return ERROR_REF_IDX;
1544 
1545         /* Storing Reference Idx Information */
1546         pi1_ref_idx[u4_i] = (WORD8)u4_ref_idx;
1547     }
1548     return OK;
1549 }
1550 
1551 /*****************************************************************************/
1552 /*                                                                           */
1553 /*  Function Name : ih264d_parse_bmb_ref_index_cavlc_range1                         */
1554 /*                                                                           */
1555 /*  Description   : This function does the Cavlc  TEV range =1 parsing of    */
1556 /*                  reference index  for a B MB. Range is 1 when             */
1557 /*                  num_ref_idx_active_minus1 is 0                           */
1558 /*                                                                           */
1559 /*  Inputs        : <What inputs does the function take?>                    */
1560 /*  Globals       : <Does it use any global variables?>                      */
1561 /*  Processing    : <Describe how the function operates - include algorithm  */
1562 /*                  description>                                             */
1563 /*  Outputs       : <What does the function produce?>                        */
1564 /*  Returns       : <What does the function return?>                         */
1565 /*                                                                           */
1566 /*  Issues        : <List any issues or problems with this function>         */
1567 /*                                                                           */
1568 /*  Revision History:                                                        */
1569 /*                                                                           */
1570 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1571 /*         19 09 2008   Jay          Draft                                   */
1572 /*                                                                           */
1573 /*****************************************************************************/
ih264d_parse_bmb_ref_index_cavlc_range1(UWORD32 u4_num_part,dec_bit_stream_t * ps_bitstrm,WORD8 * pi1_ref_idx,UWORD32 u4_num_ref_idx_active_minus1)1574 void ih264d_parse_bmb_ref_index_cavlc_range1(UWORD32 u4_num_part, /* Number of partitions in MB      */
1575                                              dec_bit_stream_t *ps_bitstrm, /* Pointer to bitstream Structure. */
1576                                              WORD8 *pi1_ref_idx, /* pointer to reference index array */
1577                                              UWORD32 u4_num_ref_idx_active_minus1 /* Not used for range 1    */
1578                                              )
1579 {
1580     UWORD32 u4_i;
1581     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
1582     UWORD32 *pu4_bitstream_off = &ps_bitstrm->u4_ofst;
1583     UNUSED(u4_num_ref_idx_active_minus1);
1584     for(u4_i = 0; u4_i < u4_num_part; u4_i++)
1585     {
1586         if(pi1_ref_idx[u4_i] > -1)
1587         {
1588             UWORD32 u4_ref_idx;
1589 
1590             u4_ref_idx = ih264d_tev_range1(pu4_bitstream_off, pu4_bitstrm_buf);
1591 
1592             /* Storing Reference Idx Information */
1593             pi1_ref_idx[u4_i] = (WORD8)u4_ref_idx;
1594         }
1595     }
1596 }
1597 
1598 /*****************************************************************************/
1599 /*                                                                           */
1600 /*  Function Name : ih264d_parse_bmb_ref_index_cavlc                                */
1601 /*                                                                           */
1602 /*  Description   : This function does the Cavlc  TEV range > 1 parsing of   */
1603 /*                  reference index  for a B MB.                             */
1604 /*                  Range > 1 when num_ref_idx_active_minus1 > 0             */
1605 /*                                                                           */
1606 /*  Inputs        : <What inputs does the function take?>                    */
1607 /*  Globals       : <Does it use any global variables?>                      */
1608 /*  Processing    : <Describe how the function operates - include algorithm  */
1609 /*                  description>                                             */
1610 /*  Outputs       : <What does the function produce?>                        */
1611 /*  Returns       : <What does the function return?>                         */
1612 /*                                                                           */
1613 /*  Issues        : <List any issues or problems with this function>         */
1614 /*                                                                           */
1615 /*  Revision History:                                                        */
1616 /*                                                                           */
1617 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1618 /*         19 09 2008   Jay          Draft                                   */
1619 /*                                                                           */
1620 /*****************************************************************************/
ih264d_parse_bmb_ref_index_cavlc(UWORD32 u4_num_part,dec_bit_stream_t * ps_bitstrm,WORD8 * pi1_ref_idx,UWORD32 u4_num_ref_idx_active_minus1)1621 WORD32 ih264d_parse_bmb_ref_index_cavlc(UWORD32 u4_num_part, /* Number of partitions in MB      */
1622                                       dec_bit_stream_t *ps_bitstrm, /* Pointer to bitstream Structure. */
1623                                       WORD8 *pi1_ref_idx, /* pointer to reference index array */
1624                                       UWORD32 u4_num_ref_idx_active_minus1 /* Number of active references - 1  */
1625                                       )
1626 {
1627     UWORD32 u4_i;
1628     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
1629     UWORD32 *pu4_bitstream_off = &ps_bitstrm->u4_ofst;
1630 
1631     for(u4_i = 0; u4_i < u4_num_part; u4_i++)
1632     {
1633         if(pi1_ref_idx[u4_i] > -1)
1634         {
1635             UWORD32 u4_ref_idx;
1636 //inlining ih264d_uev
1637             UWORD32 u4_bitstream_offset = *pu4_bitstream_off;
1638             UWORD32 u4_word, u4_ldz;
1639 
1640             /***************************************************************/
1641             /* Find leading zeros in next 32 bits                          */
1642             /***************************************************************/
1643             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
1644             u4_ldz = CLZ(u4_word);
1645             /* Flush the ps_bitstrm */
1646             u4_bitstream_offset += (u4_ldz + 1);
1647             /* Read the suffix from the ps_bitstrm */
1648             u4_word = 0;
1649             if(u4_ldz)
1650                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf, u4_ldz);
1651             *pu4_bitstream_off = u4_bitstream_offset;
1652             u4_ref_idx = ((1 << u4_ldz) + u4_word - 1);
1653 //inlining ih264d_uev
1654             if(u4_ref_idx > u4_num_ref_idx_active_minus1)
1655                 return ERROR_REF_IDX;
1656 
1657             /* Storing Reference Idx Information */
1658             pi1_ref_idx[u4_i] = (WORD8)u4_ref_idx;
1659         }
1660     }
1661     return OK;
1662 }
1663 /*****************************************************************************/
1664 /*                                                                           */
1665 /*  Function Name : ih264d_cavlc_parse_8x8block_both_available                      */
1666 /*                                                                           */
1667 /*  Description   : This function does the residual parsing of 4 subblocks   */
1668 /*                  in a 8x8 block when both top and left are available      */
1669 /*                                                                           */
1670 /*  Inputs        : pi2_coeff_block : pointer to residual block where        */
1671 /*                  decoded and inverse scan coefficients are updated        */
1672 /*                                                                           */
1673 /*                  u4_sub_block_strd : indicates the number of sublocks    */
1674 /*                  in a row. It is 4 for luma and 2 for chroma.             */
1675 /*                                                                           */
1676 /*                  u4_isdc : required to indicate 4x4 parse modules if the  */
1677 /*                  current  Mb is I_16x16/chroma DC coded.                  */
1678 /*                                                                           */
1679 /*                  ps_dec : pointer to Decstruct (decoder context)          */
1680 /*                                                                           */
1681 /*                  pu1_top_nnz : top nnz pointer                            */
1682 /*                                                                           */
1683 /*                  pu1_left_nnz : left nnz pointer                          */
1684 /*                                                                           */
1685 /*  Globals       : No                                                       */
1686 /*  Processing    : Parsing for four subblocks in unrolled, top and left nnz */
1687 /*                  are updated on the fly. csbp is set in accordance to     */
1688 /*                  decoded numcoeff for the subblock index in raster order  */
1689 /*                                                                           */
1690 /*  Outputs       : The updated residue buffer, nnzs and csbp current block  */
1691 /*                                                                           */
1692 /*  Returns       : Returns the coded sub block pattern csbp for the block   */
1693 /*                                                                           */
1694 /*  Issues        : <List any issues or problems with this function>         */
1695 /*                                                                           */
1696 /*  Revision History:                                                        */
1697 /*                                                                           */
1698 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1699 /*         09 10 2008   Jay          Draft                                   */
1700 /*                                                                           */
1701 /*****************************************************************************/
ih264d_cavlc_parse_8x8block_both_available(WORD16 * pi2_coeff_block,UWORD32 u4_sub_block_strd,UWORD32 u4_isdc,dec_struct_t * ps_dec,UWORD8 * pu1_top_nnz,UWORD8 * pu1_left_nnz,UWORD8 u1_tran_form8x8,UWORD8 u1_mb_field_decodingflag,UWORD32 * pu4_csbp)1702 WORD32 ih264d_cavlc_parse_8x8block_both_available(WORD16 *pi2_coeff_block,
1703                                                   UWORD32 u4_sub_block_strd,
1704                                                   UWORD32 u4_isdc,
1705                                                   dec_struct_t * ps_dec,
1706                                                   UWORD8 *pu1_top_nnz,
1707                                                   UWORD8 *pu1_left_nnz,
1708                                                   UWORD8 u1_tran_form8x8,
1709                                                   UWORD8 u1_mb_field_decodingflag,
1710                                                   UWORD32 *pu4_csbp)
1711 {
1712     UWORD32 u4_num_coeff, u4_n, u4_subblock_coded;
1713     UWORD32 u4_top0, u4_top1;
1714     UWORD32 *pu4_dummy;
1715     WORD32 (**pf_cavlc_parse4x4coeff)(WORD16 *pi2_coeff_block,
1716                                       UWORD32 u4_isdc,
1717                                       WORD32 u4_n,
1718                                       struct _DecStruct *ps_dec,
1719                                       UWORD32 *pu4_dummy) =
1720                                       ps_dec->pf_cavlc_parse4x4coeff;
1721     UWORD32 u4_idx = 0;
1722     UWORD8 *puc_temp;
1723     WORD32 ret;
1724 
1725     *pu4_csbp = 0;
1726     /* need to change the inverse scan matrices here */
1727     puc_temp = ps_dec->pu1_inv_scan;
1728 
1729     /*------------------------------------------------------*/
1730     /* Residual 4x4 decoding: SubBlock 0                    */
1731     /*------------------------------------------------------*/
1732     if(u1_tran_form8x8)
1733     {
1734         if(!u1_mb_field_decodingflag)
1735         {
1736             ps_dec->pu1_inv_scan =
1737                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[0];
1738         }
1739         else
1740         {
1741             ps_dec->pu1_inv_scan =
1742                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[0];
1743         }
1744     }
1745     u4_n = (pu1_top_nnz[0] + pu1_left_nnz[0] + 1) >> 1;
1746     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
1747                                              u4_n, ps_dec, &u4_num_coeff);
1748     if(ret != OK)
1749         return ret;
1750 
1751     u4_top0 = u4_num_coeff;
1752     u4_subblock_coded = (u4_num_coeff != 0);
1753     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
1754 
1755     /*------------------------------------------------------*/
1756     /* Residual 4x4 decoding: SubBlock 1                    */
1757     /*------------------------------------------------------*/
1758     u4_idx++;
1759     if(u1_tran_form8x8)
1760     {
1761         if(!u1_mb_field_decodingflag)
1762         {
1763             ps_dec->pu1_inv_scan =
1764                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[1];
1765         }
1766         else
1767         {
1768             ps_dec->pu1_inv_scan =
1769                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[1];
1770         }
1771     }
1772     else
1773     {
1774         pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
1775     }
1776     u4_n = (pu1_top_nnz[1] + u4_num_coeff + 1) >> 1;
1777     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
1778                                              u4_n, ps_dec, &u4_num_coeff);
1779     if(ret != OK)
1780         return ret;
1781 
1782     u4_top1 = pu1_left_nnz[0] = u4_num_coeff;
1783     u4_subblock_coded = (u4_num_coeff != 0);
1784     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
1785 
1786     /*------------------------------------------------------*/
1787     /* Residual 4x4 decoding: SubBlock 2                    */
1788     /*------------------------------------------------------*/
1789     u4_idx += (u4_sub_block_strd - 1);
1790     if(u1_tran_form8x8)
1791     {
1792         if(!u1_mb_field_decodingflag)
1793         {
1794             ps_dec->pu1_inv_scan =
1795                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[2];
1796         }
1797         else
1798         {
1799             ps_dec->pu1_inv_scan =
1800                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[2];
1801         }
1802     }
1803     else
1804     {
1805         pi2_coeff_block += ((u4_sub_block_strd - 1) * NUM_COEFFS_IN_4x4BLK);
1806     }
1807     u4_n = (u4_top0 + pu1_left_nnz[1] + 1) >> 1;
1808     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
1809                                              u4_n, ps_dec, &u4_num_coeff);
1810     if(ret != OK)
1811         return ret;
1812 
1813     pu1_top_nnz[0] = u4_num_coeff;
1814     u4_subblock_coded = (u4_num_coeff != 0);
1815     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
1816 
1817     /*------------------------------------------------------*/
1818     /* Residual 4x4 decoding: SubBlock 3                    */
1819     /*------------------------------------------------------*/
1820     u4_idx++;
1821     if(u1_tran_form8x8)
1822     {
1823         if(!u1_mb_field_decodingflag)
1824         {
1825             ps_dec->pu1_inv_scan =
1826                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[3];
1827         }
1828         else
1829         {
1830             ps_dec->pu1_inv_scan =
1831                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[3];
1832         }
1833     }
1834     else
1835     {
1836         pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
1837     }
1838     u4_n = (u4_top1 + u4_num_coeff + 1) >> 1;
1839     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
1840                                              u4_n, ps_dec, &u4_num_coeff);
1841     if(ret != OK)
1842         return ret;
1843 
1844     pu1_top_nnz[1] = pu1_left_nnz[1] = u4_num_coeff;
1845     u4_subblock_coded = (u4_num_coeff != 0);
1846     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
1847 
1848     ps_dec->pu1_inv_scan = puc_temp;
1849 
1850     return OK;
1851 }
1852 
1853 /*****************************************************************************/
1854 /*                                                                           */
1855 /*  Function Name : ih264d_cavlc_parse_8x8block_left_available                      */
1856 /*                                                                           */
1857 /*  Description   : This function does the residual parsing of 4 subblocks   */
1858 /*                  in a 8x8 block when only left is available for block     */
1859 /*                                                                           */
1860 /*  Inputs        : pi2_coeff_block : pointer to residual block where        */
1861 /*                  decoded and inverse scan coefficients are updated        */
1862 /*                                                                           */
1863 /*                  u4_sub_block_strd : indicates the number of sublocks    */
1864 /*                  in a row. It is 4 for luma and 2 for chroma.             */
1865 /*                                                                           */
1866 /*                  u4_isdc : required to indicate 4x4 parse modules if the  */
1867 /*                  current  Mb is I_16x16/chroma DC coded.                  */
1868 /*                                                                           */
1869 /*                  ps_dec : pointer to Decstruct (decoder context)          */
1870 /*                                                                           */
1871 /*                  pu1_top_nnz : top nnz pointer                            */
1872 /*                                                                           */
1873 /*                  pu1_left_nnz : left nnz pointer                          */
1874 /*                                                                           */
1875 /*  Globals       : No                                                       */
1876 /*  Processing    : Parsing for four subblocks in unrolled, top and left nnz */
1877 /*                  are updated on the fly. csbp is set in accordance to     */
1878 /*                  decoded numcoeff for the subblock index in raster order  */
1879 /*                                                                           */
1880 /*  Outputs       : The updated residue buffer, nnzs and csbp current block  */
1881 /*                                                                           */
1882 /*  Returns       : Returns the coded sub block pattern csbp for the block   */
1883 /*                                                                           */
1884 /*  Issues        : <List any issues or problems with this function>         */
1885 /*                                                                           */
1886 /*  Revision History:                                                        */
1887 /*                                                                           */
1888 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1889 /*         09 10 2008   Jay          Draft                                   */
1890 /*                                                                           */
1891 /*****************************************************************************/
ih264d_cavlc_parse_8x8block_left_available(WORD16 * pi2_coeff_block,UWORD32 u4_sub_block_strd,UWORD32 u4_isdc,dec_struct_t * ps_dec,UWORD8 * pu1_top_nnz,UWORD8 * pu1_left_nnz,UWORD8 u1_tran_form8x8,UWORD8 u1_mb_field_decodingflag,UWORD32 * pu4_csbp)1892 WORD32 ih264d_cavlc_parse_8x8block_left_available(WORD16 *pi2_coeff_block,
1893                                                   UWORD32 u4_sub_block_strd,
1894                                                   UWORD32 u4_isdc,
1895                                                   dec_struct_t * ps_dec,
1896                                                   UWORD8 *pu1_top_nnz,
1897                                                   UWORD8 *pu1_left_nnz,
1898                                                   UWORD8 u1_tran_form8x8,
1899                                                   UWORD8 u1_mb_field_decodingflag,
1900                                                   UWORD32 *pu4_csbp)
1901 {
1902     UWORD32 u4_num_coeff, u4_n, u4_subblock_coded;
1903     UWORD32 u4_top0, u4_top1;
1904     UWORD32 *pu4_dummy;
1905     WORD32 (**pf_cavlc_parse4x4coeff)(WORD16 *pi2_coeff_block,
1906                                       UWORD32 u4_isdc,
1907                                       WORD32 u4_n,
1908                                       struct _DecStruct *ps_dec,
1909                                       UWORD32 *pu4_dummy) =
1910                                       ps_dec->pf_cavlc_parse4x4coeff;
1911     UWORD32 u4_idx = 0;
1912     UWORD8 *puc_temp;
1913     WORD32 ret;
1914 
1915     *pu4_csbp = 0;
1916     puc_temp = ps_dec->pu1_inv_scan;
1917 
1918     /*------------------------------------------------------*/
1919     /* Residual 4x4 decoding: SubBlock 0                    */
1920     /*------------------------------------------------------*/
1921     if(u1_tran_form8x8)
1922     {
1923         if(!u1_mb_field_decodingflag)
1924         {
1925             ps_dec->pu1_inv_scan =
1926                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[0];
1927         }
1928         else
1929         {
1930             ps_dec->pu1_inv_scan =
1931                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[0];
1932         }
1933     }
1934     u4_n = pu1_left_nnz[0];
1935     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
1936                                              u4_n, ps_dec, &u4_num_coeff);
1937     if(ret != OK)
1938         return ret;
1939 
1940     u4_top0 = u4_num_coeff;
1941     u4_subblock_coded = (u4_num_coeff != 0);
1942     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
1943 
1944     /*------------------------------------------------------*/
1945     /* Residual 4x4 decoding: SubBlock 1                    */
1946     /*------------------------------------------------------*/
1947     u4_idx++;
1948     if(u1_tran_form8x8)
1949     {
1950         if(!u1_mb_field_decodingflag)
1951         {
1952             ps_dec->pu1_inv_scan =
1953                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[1];
1954         }
1955         else
1956         {
1957             ps_dec->pu1_inv_scan =
1958                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[1];
1959         }
1960     }
1961     else
1962     {
1963         pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
1964     }
1965     u4_n = u4_num_coeff;
1966     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
1967                                              u4_n, ps_dec, &u4_num_coeff);
1968     if(ret != OK)
1969         return ret;
1970 
1971     u4_top1 = pu1_left_nnz[0] = u4_num_coeff;
1972     u4_subblock_coded = (u4_num_coeff != 0);
1973     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
1974 
1975     /*------------------------------------------------------*/
1976     /* Residual 4x4 decoding: SubBlock 2                    */
1977     /*------------------------------------------------------*/
1978     u4_idx += (u4_sub_block_strd - 1);
1979     if(u1_tran_form8x8)
1980     {
1981         if(!u1_mb_field_decodingflag)
1982         {
1983             ps_dec->pu1_inv_scan =
1984                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[2];
1985         }
1986         else
1987         {
1988             ps_dec->pu1_inv_scan =
1989                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[2];
1990         }
1991     }
1992     else
1993     {
1994         pi2_coeff_block += ((u4_sub_block_strd - 1) * NUM_COEFFS_IN_4x4BLK);
1995     }
1996     u4_n = (u4_top0 + pu1_left_nnz[1] + 1) >> 1;
1997     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
1998                                              u4_n, ps_dec, &u4_num_coeff);
1999     if(ret != OK)
2000         return ret;
2001 
2002     pu1_top_nnz[0] = u4_num_coeff;
2003     u4_subblock_coded = (u4_num_coeff != 0);
2004     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
2005 
2006     /*------------------------------------------------------*/
2007     /* Residual 4x4 decoding: SubBlock 3                    */
2008     /*------------------------------------------------------*/
2009     u4_idx++;
2010     if(u1_tran_form8x8)
2011     {
2012         if(!u1_mb_field_decodingflag)
2013         {
2014             ps_dec->pu1_inv_scan =
2015                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[3];
2016         }
2017         else
2018         {
2019             ps_dec->pu1_inv_scan =
2020                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[3];
2021         }
2022     }
2023     else
2024     {
2025         pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
2026     }
2027     u4_n = (u4_top1 + u4_num_coeff + 1) >> 1;
2028     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
2029                                              u4_n, ps_dec, &u4_num_coeff);
2030     if(ret != OK)
2031         return ret;
2032 
2033     pu1_top_nnz[1] = pu1_left_nnz[1] = u4_num_coeff;
2034     u4_subblock_coded = (u4_num_coeff != 0);
2035     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
2036 
2037     ps_dec->pu1_inv_scan = puc_temp;
2038 
2039     return OK;
2040 }
2041 
2042 /*****************************************************************************/
2043 /*                                                                           */
2044 /*  Function Name : ih264d_cavlc_parse_8x8block_top_available                       */
2045 /*                                                                           */
2046 /*  Description   : This function does the residual parsing of 4 subblocks   */
2047 /*                  in a 8x8 block when only top is available for block      */
2048 /*                                                                           */
2049 /*  Inputs        : pi2_coeff_block : pointer to residual block where        */
2050 /*                  decoded and inverse scan coefficients are updated        */
2051 /*                                                                           */
2052 /*                  u4_sub_block_strd : indicates the number of sublocks    */
2053 /*                  in a row. It is 4 for luma and 2 for chroma.             */
2054 /*                                                                           */
2055 /*                  u4_isdc : required to indicate 4x4 parse modules if the  */
2056 /*                  current  Mb is I_16x16/chroma DC coded.                  */
2057 /*                                                                           */
2058 /*                  ps_dec : pointer to Decstruct (decoder context)          */
2059 /*                                                                           */
2060 /*                  pu1_top_nnz : top nnz pointer                            */
2061 /*                                                                           */
2062 /*                  pu1_left_nnz : left nnz pointer                          */
2063 /*                                                                           */
2064 /*  Globals       : No                                                       */
2065 /*  Processing    : Parsing for four subblocks in unrolled, top and left nnz */
2066 /*                  are updated on the fly. csbp is set in accordance to     */
2067 /*                  decoded numcoeff for the subblock index in raster order  */
2068 /*                                                                           */
2069 /*  Outputs       : The updated residue buffer, nnzs and csbp current block  */
2070 /*                                                                           */
2071 /*  Returns       : Returns the coded sub block pattern csbp for the block   */
2072 /*                                                                           */
2073 /*  Issues        : <List any issues or problems with this function>         */
2074 /*                                                                           */
2075 /*  Revision History:                                                        */
2076 /*                                                                           */
2077 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
2078 /*         09 10 2008   Jay          Draft                                   */
2079 /*                                                                           */
2080 /*****************************************************************************/
ih264d_cavlc_parse_8x8block_top_available(WORD16 * pi2_coeff_block,UWORD32 u4_sub_block_strd,UWORD32 u4_isdc,dec_struct_t * ps_dec,UWORD8 * pu1_top_nnz,UWORD8 * pu1_left_nnz,UWORD8 u1_tran_form8x8,UWORD8 u1_mb_field_decodingflag,UWORD32 * pu4_csbp)2081 WORD32 ih264d_cavlc_parse_8x8block_top_available(WORD16 *pi2_coeff_block,
2082                                                  UWORD32 u4_sub_block_strd,
2083                                                  UWORD32 u4_isdc,
2084                                                  dec_struct_t * ps_dec,
2085                                                  UWORD8 *pu1_top_nnz,
2086                                                  UWORD8 *pu1_left_nnz,
2087                                                  UWORD8 u1_tran_form8x8,
2088                                                  UWORD8 u1_mb_field_decodingflag,
2089                                                  UWORD32 *pu4_csbp)
2090 {
2091     UWORD32 u4_num_coeff, u4_n, u4_subblock_coded;
2092     UWORD32 u4_top0, u4_top1;
2093     UWORD32 *pu4_dummy;
2094     WORD32 (**pf_cavlc_parse4x4coeff)(WORD16 *pi2_coeff_block,
2095                                       UWORD32 u4_isdc,
2096                                       WORD32 u4_n,
2097                                       struct _DecStruct *ps_dec,
2098                                       UWORD32 *pu4_dummy) =
2099                                       ps_dec->pf_cavlc_parse4x4coeff;
2100     UWORD32 u4_idx = 0;
2101     UWORD8 *puc_temp;
2102     WORD32 ret;
2103 
2104     *pu4_csbp = 0;
2105     puc_temp = ps_dec->pu1_inv_scan;
2106 
2107     /*------------------------------------------------------*/
2108     /* Residual 4x4 decoding: SubBlock 0                    */
2109     /*------------------------------------------------------*/
2110     if(u1_tran_form8x8)
2111     {
2112         if(!u1_mb_field_decodingflag)
2113         {
2114             ps_dec->pu1_inv_scan =
2115                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[0];
2116         }
2117         else
2118         {
2119             ps_dec->pu1_inv_scan =
2120                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[0];
2121         }
2122     }
2123     u4_n = pu1_top_nnz[0];
2124     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
2125                                              u4_n, ps_dec, &u4_num_coeff);
2126     if(ret != OK)
2127         return ret;
2128 
2129     u4_top0 = u4_num_coeff;
2130     u4_subblock_coded = (u4_num_coeff != 0);
2131     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
2132 
2133     /*------------------------------------------------------*/
2134     /* Residual 4x4 decoding: SubBlock 1                    */
2135     /*------------------------------------------------------*/
2136     u4_idx++;
2137     if(u1_tran_form8x8)
2138     {
2139         if(!u1_mb_field_decodingflag)
2140         {
2141             ps_dec->pu1_inv_scan =
2142                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[1];
2143         }
2144         else
2145         {
2146             ps_dec->pu1_inv_scan =
2147                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[1];
2148         }
2149     }
2150     else
2151     {
2152         pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
2153     }
2154     u4_n = (pu1_top_nnz[1] + u4_num_coeff + 1) >> 1;
2155     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
2156                                              u4_n, ps_dec, &u4_num_coeff);
2157     if(ret != OK)
2158         return ret;
2159 
2160     u4_top1 = pu1_left_nnz[0] = u4_num_coeff;
2161     u4_subblock_coded = (u4_num_coeff != 0);
2162     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
2163 
2164     /*------------------------------------------------------*/
2165     /* Residual 4x4 decoding: SubBlock 2                    */
2166     /*------------------------------------------------------*/
2167     u4_idx += (u4_sub_block_strd - 1);
2168     if(u1_tran_form8x8)
2169     {
2170         if(!u1_mb_field_decodingflag)
2171         {
2172             ps_dec->pu1_inv_scan =
2173                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[2];
2174         }
2175         else
2176         {
2177             ps_dec->pu1_inv_scan =
2178                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[2];
2179         }
2180     }
2181     else
2182     {
2183         pi2_coeff_block += ((u4_sub_block_strd - 1) * NUM_COEFFS_IN_4x4BLK);
2184     }
2185     u4_n = u4_top0;
2186     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
2187                                              u4_n, ps_dec, &u4_num_coeff);
2188     if(ret != OK)
2189         return ret;
2190 
2191     pu1_top_nnz[0] = u4_num_coeff;
2192     u4_subblock_coded = (u4_num_coeff != 0);
2193     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
2194 
2195     /*------------------------------------------------------*/
2196     /* Residual 4x4 decoding: SubBlock 3                    */
2197     /*------------------------------------------------------*/
2198     u4_idx++;
2199     if(u1_tran_form8x8)
2200     {
2201         if(!u1_mb_field_decodingflag)
2202         {
2203             ps_dec->pu1_inv_scan =
2204                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[3];
2205         }
2206         else
2207         {
2208             ps_dec->pu1_inv_scan =
2209                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[3];
2210         }
2211     }
2212     else
2213     {
2214         pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
2215     }
2216     u4_n = (u4_top1 + u4_num_coeff + 1) >> 1;
2217     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
2218                                              u4_n, ps_dec, &u4_num_coeff);
2219     if(ret != OK)
2220         return ret;
2221 
2222     pu1_top_nnz[1] = pu1_left_nnz[1] = u4_num_coeff;
2223     u4_subblock_coded = (u4_num_coeff != 0);
2224     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
2225 
2226     ps_dec->pu1_inv_scan = puc_temp;
2227 
2228     return OK;
2229 }
2230 
2231 /*****************************************************************************/
2232 /*                                                                           */
2233 /*  Function Name : ih264d_cavlc_parse_8x8block_none_available                      */
2234 /*                                                                           */
2235 /*  Description   : This function does the residual parsing of 4 subblocks   */
2236 /*                  in a 8x8 block when none of the neigbours are available  */
2237 /*                                                                           */
2238 /*  Inputs        : pi2_coeff_block : pointer to residual block where        */
2239 /*                  decoded and inverse scan coefficients are updated        */
2240 /*                                                                           */
2241 /*                  u4_sub_block_strd : indicates the number of sublocks    */
2242 /*                  in a row. It is 4 for luma and 2 for chroma.             */
2243 /*                                                                           */
2244 /*                  u4_isdc : required to indicate 4x4 parse modules if the  */
2245 /*                  current  Mb is I_16x16/chroma DC coded.                  */
2246 /*                                                                           */
2247 /*                  ps_dec : pointer to Decstruct (decoder context)          */
2248 /*                                                                           */
2249 /*                  pu1_top_nnz : top nnz pointer                            */
2250 /*                                                                           */
2251 /*                  pu1_left_nnz : left nnz pointer                          */
2252 /*                                                                           */
2253 /*  Globals       : No                                                       */
2254 /*  Processing    : Parsing for four subblocks in unrolled, top and left nnz */
2255 /*                  are updated on the fly. csbp is set in accordance to     */
2256 /*                  decoded numcoeff for the subblock index in raster order  */
2257 /*                                                                           */
2258 /*  Outputs       : The updated residue buffer, nnzs and csbp current block  */
2259 /*                                                                           */
2260 /*  Returns       : Returns the coded sub block pattern csbp for the block   */
2261 /*                                                                           */
2262 /*  Issues        : <List any issues or problems with this function>         */
2263 /*                                                                           */
2264 /*  Revision History:                                                        */
2265 /*                                                                           */
2266 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
2267 /*         09 10 2008   Jay          Draft                                   */
2268 /*                                                                           */
2269 /*****************************************************************************/
ih264d_cavlc_parse_8x8block_none_available(WORD16 * pi2_coeff_block,UWORD32 u4_sub_block_strd,UWORD32 u4_isdc,dec_struct_t * ps_dec,UWORD8 * pu1_top_nnz,UWORD8 * pu1_left_nnz,UWORD8 u1_tran_form8x8,UWORD8 u1_mb_field_decodingflag,UWORD32 * pu4_csbp)2270 WORD32 ih264d_cavlc_parse_8x8block_none_available(WORD16 *pi2_coeff_block,
2271                                                   UWORD32 u4_sub_block_strd,
2272                                                   UWORD32 u4_isdc,
2273                                                   dec_struct_t * ps_dec,
2274                                                   UWORD8 *pu1_top_nnz,
2275                                                   UWORD8 *pu1_left_nnz,
2276                                                   UWORD8 u1_tran_form8x8,
2277                                                   UWORD8 u1_mb_field_decodingflag,
2278                                                   UWORD32 *pu4_csbp)
2279 {
2280     UWORD32 u4_num_coeff, u4_n, u4_subblock_coded;
2281     UWORD32 u4_top0, u4_top1;
2282     UWORD32 *pu4_dummy;
2283     WORD32 (**pf_cavlc_parse4x4coeff)(WORD16 *pi2_coeff_block,
2284                                       UWORD32 u4_isdc,
2285                                       WORD32 u4_n,
2286                                       struct _DecStruct *ps_dec,
2287                                       UWORD32 *pu4_dummy) =
2288                                       ps_dec->pf_cavlc_parse4x4coeff;
2289     UWORD32 u4_idx = 0;
2290     UWORD8 *puc_temp;
2291     WORD32 ret;
2292 
2293     *pu4_csbp = 0;
2294     puc_temp = ps_dec->pu1_inv_scan;
2295 
2296     /*------------------------------------------------------*/
2297     /* Residual 4x4 decoding: SubBlock 0                    */
2298     /*------------------------------------------------------*/
2299     if(u1_tran_form8x8)
2300     {
2301         if(!u1_mb_field_decodingflag)
2302         {
2303             ps_dec->pu1_inv_scan =
2304                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[0];
2305         }
2306         else
2307         {
2308             ps_dec->pu1_inv_scan =
2309                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[0];
2310         }
2311     }
2312     ret = pf_cavlc_parse4x4coeff[0](pi2_coeff_block, u4_isdc, 0,
2313                                     ps_dec, &u4_num_coeff);
2314     if(ret != OK)
2315         return ret;
2316 
2317     u4_top0 = u4_num_coeff;
2318     u4_subblock_coded = (u4_num_coeff != 0);
2319     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
2320 
2321     /*------------------------------------------------------*/
2322     /* Residual 4x4 decoding: SubBlock 1                    */
2323     /*------------------------------------------------------*/
2324     u4_idx++;
2325     if(u1_tran_form8x8)
2326     {
2327         if(!u1_mb_field_decodingflag)
2328         {
2329             ps_dec->pu1_inv_scan =
2330                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[1];
2331         }
2332         else
2333         {
2334             ps_dec->pu1_inv_scan =
2335                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[1];
2336         }
2337     }
2338     else
2339     {
2340         pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
2341     }
2342     u4_n = u4_num_coeff;
2343     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
2344                                              u4_n, ps_dec, &u4_num_coeff);
2345     if(ret != OK)
2346         return ret;
2347 
2348     u4_top1 = pu1_left_nnz[0] = u4_num_coeff;
2349     u4_subblock_coded = (u4_num_coeff != 0);
2350     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
2351 
2352     /*------------------------------------------------------*/
2353     /* Residual 4x4 decoding: SubBlock 2                    */
2354     /*------------------------------------------------------*/
2355     u4_idx += (u4_sub_block_strd - 1);
2356     if(u1_tran_form8x8)
2357     {
2358         if(!u1_mb_field_decodingflag)
2359         {
2360             ps_dec->pu1_inv_scan =
2361                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[2];
2362         }
2363         else
2364         {
2365             ps_dec->pu1_inv_scan =
2366                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[2];
2367         }
2368     }
2369     else
2370     {
2371         pi2_coeff_block += ((u4_sub_block_strd - 1) * NUM_COEFFS_IN_4x4BLK);
2372     }
2373     u4_n = u4_top0;
2374     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
2375                                              u4_n, ps_dec, &u4_num_coeff);
2376     if(ret != OK)
2377         return ret;
2378 
2379     pu1_top_nnz[0] = u4_num_coeff;
2380     u4_subblock_coded = (u4_num_coeff != 0);
2381     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
2382 
2383     /*------------------------------------------------------*/
2384     /* Residual 4x4 decoding: SubBlock 3                    */
2385     /*------------------------------------------------------*/
2386     u4_idx++;
2387     if(u1_tran_form8x8)
2388     {
2389         if(!u1_mb_field_decodingflag)
2390         {
2391             ps_dec->pu1_inv_scan =
2392                             (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[3];
2393         }
2394         else
2395         {
2396             ps_dec->pu1_inv_scan =
2397                             (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[3];
2398         }
2399     }
2400     else
2401     {
2402         pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
2403     }
2404     u4_n = (u4_top1 + u4_num_coeff + 1) >> 1;
2405     ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
2406                                              u4_n, ps_dec, &u4_num_coeff);
2407     if(ret != OK)
2408         return ret;
2409 
2410     pu1_top_nnz[1] = pu1_left_nnz[1] = u4_num_coeff;
2411     u4_subblock_coded = (u4_num_coeff != 0);
2412     INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);
2413 
2414     ps_dec->pu1_inv_scan = puc_temp;
2415 
2416     return OK;
2417 }
2418 
2419 /*!
2420  **************************************************************************
2421  * \if Function name : ih264d_parse_residual4x4_cavlc \endif
2422  *
2423  * \brief
2424  *    This function parses CAVLC syntax of a Luma and Chroma AC Residuals.
2425  *
2426  * \return
2427  *    0 on Success and Error code otherwise
2428  **************************************************************************
2429  */
2430 
ih264d_parse_residual4x4_cavlc(dec_struct_t * ps_dec,dec_mb_info_t * ps_cur_mb_info,UWORD8 u1_offset)2431 WORD32 ih264d_parse_residual4x4_cavlc(dec_struct_t * ps_dec,
2432                                       dec_mb_info_t *ps_cur_mb_info,
2433                                       UWORD8 u1_offset)
2434 {
2435     UWORD8 u1_cbp = ps_cur_mb_info->u1_cbp;
2436     UWORD16 ui16_csbp = 0;
2437     UWORD32 u4_nbr_avl;
2438     WORD16 *pi2_residual_buf;
2439 
2440     UWORD8 u1_is_top_mb_avail;
2441     UWORD8 u1_is_left_mb_avail;
2442 
2443     UWORD8 *pu1_top_nnz = ps_cur_mb_info->ps_curmb->pu1_nnz_y;
2444     UWORD8 *pu1_left_nnz = ps_dec->pu1_left_nnz_y;
2445     WORD16 *pi2_coeff_block = NULL;
2446     UWORD32 *pu4_dummy;
2447     WORD32 ret;
2448 
2449     WORD32 (**pf_cavlc_parse_8x8block)(WORD16 *pi2_coeff_block,
2450                                        UWORD32 u4_sub_block_strd,
2451                                        UWORD32 u4_isdc,
2452                                        struct _DecStruct *ps_dec,
2453                                        UWORD8 *pu1_top_nnz,
2454                                        UWORD8 *pu1_left_nnz,
2455                                        UWORD8 u1_tran_form8x8,
2456                                        UWORD8 u1_mb_field_decodingflag,
2457                                        UWORD32 *pu4_dummy) = ps_dec->pf_cavlc_parse_8x8block;
2458 
2459 
2460     {
2461         UWORD8 uc_temp = ps_dec->u1_mb_ngbr_availablity;
2462         u1_is_top_mb_avail = BOOLEAN(uc_temp & TOP_MB_AVAILABLE_MASK);
2463         u1_is_left_mb_avail = BOOLEAN(uc_temp & LEFT_MB_AVAILABLE_MASK);
2464         u4_nbr_avl = (u1_is_top_mb_avail << 1) | u1_is_left_mb_avail;
2465     }
2466 
2467     ps_cur_mb_info->u1_qp_div6 = ps_dec->u1_qp_y_div6;
2468     ps_cur_mb_info->u1_qp_rem6 = ps_dec->u1_qp_y_rem6;
2469     ps_cur_mb_info->u1_qpc_div6 = ps_dec->u1_qp_u_div6;
2470     ps_cur_mb_info->u1_qpc_rem6 = ps_dec->u1_qp_u_rem6;
2471     ps_cur_mb_info->u1_qpcr_div6 = ps_dec->u1_qp_v_div6;
2472     ps_cur_mb_info->u1_qpcr_rem6 = ps_dec->u1_qp_v_rem6;
2473 
2474     if(u1_cbp & 0xf)
2475     {
2476         pu1_top_nnz[0] = ps_cur_mb_info->ps_top_mb->pu1_nnz_y[0];
2477         pu1_top_nnz[1] = ps_cur_mb_info->ps_top_mb->pu1_nnz_y[1];
2478         pu1_top_nnz[2] = ps_cur_mb_info->ps_top_mb->pu1_nnz_y[2];
2479         pu1_top_nnz[3] = ps_cur_mb_info->ps_top_mb->pu1_nnz_y[3];
2480 
2481         /*******************************************************************/
2482         /* Block 0 residual decoding, check cbp and proceed (subblock = 0) */
2483         /*******************************************************************/
2484         if(!(u1_cbp & 0x1))
2485         {
2486             *(UWORD16 *)(pu1_top_nnz) = 0;
2487             *(UWORD16 *)(pu1_left_nnz) = 0;
2488 
2489         }
2490         else
2491         {
2492             UWORD32 u4_temp;
2493             ret = pf_cavlc_parse_8x8block[u4_nbr_avl](
2494                         pi2_coeff_block, 4, u1_offset, ps_dec, pu1_top_nnz,
2495                         pu1_left_nnz, ps_cur_mb_info->u1_tran_form8x8,
2496                         ps_cur_mb_info->u1_mb_field_decodingflag, &u4_temp);
2497             if(ret != OK)
2498                 return ret;
2499             ui16_csbp = u4_temp;
2500         }
2501 
2502         /*******************************************************************/
2503         /* Block 1 residual decoding, check cbp and proceed (subblock = 2) */
2504         /*******************************************************************/
2505         if(ps_cur_mb_info->u1_tran_form8x8)
2506         {
2507             pi2_coeff_block += 64;
2508         }
2509         else
2510         {
2511             pi2_coeff_block += (2 * NUM_COEFFS_IN_4x4BLK);
2512         }
2513 
2514         if(!(u1_cbp & 0x2))
2515         {
2516             *(UWORD16 *)(pu1_top_nnz + 2) = 0;
2517             *(UWORD16 *)(pu1_left_nnz) = 0;
2518         }
2519         else
2520         {
2521             UWORD32 u4_temp = (u4_nbr_avl | 0x1);
2522             ret = pf_cavlc_parse_8x8block[u4_temp](
2523                         pi2_coeff_block, 4, u1_offset, ps_dec,
2524                         (pu1_top_nnz + 2), pu1_left_nnz,
2525                         ps_cur_mb_info->u1_tran_form8x8,
2526                         ps_cur_mb_info->u1_mb_field_decodingflag, &u4_temp);
2527             if(ret != OK)
2528                 return ret;
2529             ui16_csbp |= (u4_temp << 2);
2530         }
2531 
2532         /*******************************************************************/
2533         /* Block 2 residual decoding, check cbp and proceed (subblock = 8) */
2534         /*******************************************************************/
2535         if(ps_cur_mb_info->u1_tran_form8x8)
2536         {
2537             pi2_coeff_block += 64;
2538         }
2539         else
2540         {
2541             pi2_coeff_block += (6 * NUM_COEFFS_IN_4x4BLK);
2542         }
2543 
2544         if(!(u1_cbp & 0x4))
2545         {
2546             *(UWORD16 *)(pu1_top_nnz) = 0;
2547             *(UWORD16 *)(pu1_left_nnz + 2) = 0;
2548         }
2549         else
2550         {
2551             UWORD32 u4_temp = (u4_nbr_avl | 0x2);
2552             ret = pf_cavlc_parse_8x8block[u4_temp](
2553                         pi2_coeff_block, 4, u1_offset, ps_dec, pu1_top_nnz,
2554                         (pu1_left_nnz + 2), ps_cur_mb_info->u1_tran_form8x8,
2555                         ps_cur_mb_info->u1_mb_field_decodingflag, &u4_temp);
2556             if(ret != OK)
2557                 return ret;
2558             ui16_csbp |= (u4_temp << 8);
2559         }
2560 
2561         /*******************************************************************/
2562         /* Block 3 residual decoding, check cbp and proceed (subblock = 10)*/
2563         /*******************************************************************/
2564         if(ps_cur_mb_info->u1_tran_form8x8)
2565         {
2566             pi2_coeff_block += 64;
2567         }
2568         else
2569         {
2570             pi2_coeff_block += (2 * NUM_COEFFS_IN_4x4BLK);
2571         }
2572 
2573         if(!(u1_cbp & 0x8))
2574         {
2575             *(UWORD16 *)(pu1_top_nnz + 2) = 0;
2576             *(UWORD16 *)(pu1_left_nnz + 2) = 0;
2577         }
2578         else
2579         {
2580             UWORD32 u4_temp;
2581             ret = pf_cavlc_parse_8x8block[0x3](
2582                         pi2_coeff_block, 4, u1_offset, ps_dec,
2583                         (pu1_top_nnz + 2), (pu1_left_nnz + 2),
2584                         ps_cur_mb_info->u1_tran_form8x8,
2585                         ps_cur_mb_info->u1_mb_field_decodingflag, &u4_temp);
2586             if(ret != OK)
2587                 return ret;
2588             ui16_csbp |= (u4_temp << 10);
2589         }
2590     }
2591     else
2592     {
2593         *(UWORD32 *)(pu1_top_nnz) = 0;
2594         *(UWORD32 *)(pu1_left_nnz) = 0;
2595     }
2596 
2597     ps_cur_mb_info->u2_luma_csbp = ui16_csbp;
2598     ps_cur_mb_info->ps_curmb->u2_luma_csbp = ui16_csbp;
2599 
2600     {
2601         UWORD16 u2_chroma_csbp = 0;
2602         ps_cur_mb_info->u2_chroma_csbp = 0;
2603         pu1_top_nnz = ps_cur_mb_info->ps_curmb->pu1_nnz_uv;
2604         pu1_left_nnz = ps_dec->pu1_left_nnz_uv;
2605 
2606         u1_cbp >>= 4;
2607         /*--------------------------------------------------------------------*/
2608         /* if Chroma Component not present OR no ac values present            */
2609         /* Set the values of N to zero                                        */
2610         /*--------------------------------------------------------------------*/
2611         if(u1_cbp == CBPC_ALLZERO || u1_cbp == CBPC_ACZERO)
2612         {
2613             *(UWORD32 *)(pu1_top_nnz) = 0;
2614             *(UWORD32 *)(pu1_left_nnz) = 0;
2615         }
2616 
2617         if(u1_cbp == CBPC_ALLZERO)
2618         {
2619             return (0);
2620         }
2621         /*--------------------------------------------------------------------*/
2622         /* Decode Chroma DC values                                            */
2623         /*--------------------------------------------------------------------*/
2624         {
2625             WORD32 u4_scale_u;
2626             WORD32 u4_scale_v;
2627             WORD32 i4_mb_inter_inc;
2628             u4_scale_u = ps_dec->pu2_quant_scale_u[0] << ps_dec->u1_qp_u_div6;
2629             u4_scale_v = ps_dec->pu2_quant_scale_v[0] << ps_dec->u1_qp_v_div6;
2630             i4_mb_inter_inc = (!((ps_cur_mb_info->ps_curmb->u1_mb_type == I_4x4_MB)
2631                             || (ps_cur_mb_info->ps_curmb->u1_mb_type == I_16x16_MB)))
2632                             * 3;
2633 
2634             if(ps_dec->s_high_profile.u1_scaling_present)
2635             {
2636                 u4_scale_u *=
2637                                 ps_dec->s_high_profile.i2_scalinglist4x4[i4_mb_inter_inc
2638                                                 + 1][0];
2639                 u4_scale_v *=
2640                                 ps_dec->s_high_profile.i2_scalinglist4x4[i4_mb_inter_inc
2641                                                 + 2][0];
2642 
2643             }
2644             else
2645             {
2646                 u4_scale_u <<= 4;
2647                 u4_scale_v <<= 4;
2648             }
2649 
2650             ih264d_cavlc_parse_chroma_dc(ps_cur_mb_info,pi2_coeff_block, ps_dec->ps_bitstrm,
2651                                          u4_scale_u, u4_scale_v,
2652                                          i4_mb_inter_inc);
2653         }
2654 
2655         if(u1_cbp == CBPC_ACZERO)
2656             return (0);
2657 
2658         pu1_top_nnz[0] = ps_cur_mb_info->ps_top_mb->pu1_nnz_uv[0];
2659         pu1_top_nnz[1] = ps_cur_mb_info->ps_top_mb->pu1_nnz_uv[1];
2660         pu1_top_nnz[2] = ps_cur_mb_info->ps_top_mb->pu1_nnz_uv[2];
2661         pu1_top_nnz[3] = ps_cur_mb_info->ps_top_mb->pu1_nnz_uv[3];
2662         /*--------------------------------------------------------------------*/
2663         /* Decode Chroma AC values                                            */
2664         /*--------------------------------------------------------------------*/
2665         {
2666             UWORD32 u4_temp;
2667             /*****************************************************************/
2668             /* U Block  residual decoding, check cbp and proceed (subblock=0)*/
2669             /*****************************************************************/
2670             ret = pf_cavlc_parse_8x8block[u4_nbr_avl](
2671                         pi2_coeff_block, 2, 1, ps_dec, pu1_top_nnz,
2672                         pu1_left_nnz, 0, 0, &u4_temp);
2673             if(ret != OK)
2674                 return ret;
2675             u2_chroma_csbp = u4_temp;
2676 
2677             pi2_coeff_block += MB_CHROM_SIZE;
2678             /*****************************************************************/
2679             /* V Block  residual decoding, check cbp and proceed (subblock=1)*/
2680             /*****************************************************************/
2681             ret = pf_cavlc_parse_8x8block[u4_nbr_avl](pi2_coeff_block, 2, 1,
2682                                                       ps_dec,
2683                                                       (pu1_top_nnz + 2),
2684                                                       (pu1_left_nnz + 2), 0,
2685                                                       0, &u4_temp);
2686             if(ret != OK)
2687                 return ret;
2688             u2_chroma_csbp |= (u4_temp << 4);
2689         }
2690 
2691         ps_cur_mb_info->u2_chroma_csbp = u2_chroma_csbp;
2692     }
2693     return OK;
2694 }
2695