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