Home | History | Annotate | Download | only in decoder
      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 #include <string.h>
     21 
     22 #include "iv_datatypedef.h"
     23 #include "iv.h"
     24 
     25 #include "impeg2_buf_mgr.h"
     26 #include "impeg2_disp_mgr.h"
     27 #include "impeg2_defs.h"
     28 #include "impeg2_platform_macros.h"
     29 #include "impeg2_inter_pred.h"
     30 #include "impeg2_idct.h"
     31 #include "impeg2_globals.h"
     32 #include "impeg2_mem_func.h"
     33 #include "impeg2_format_conv.h"
     34 #include "impeg2_macros.h"
     35 
     36 #include "ivd.h"
     37 #include "impeg2d.h"
     38 #include "impeg2d_bitstream.h"
     39 #include "impeg2d_structs.h"
     40 #include "impeg2d_vld_tables.h"
     41 #include "impeg2d_vld.h"
     42 #include "impeg2d_pic_proc.h"
     43 #include "impeg2d_debug.h"
     44 
     45 
     46 /*******************************************************************************
     47 * Function name : impeg2d_dec_vld_symbol
     48 *
     49 * Description   : Performs decoding of VLD symbol. It performs decoding by
     50 *                 processing 1 bit at a time
     51 *
     52 * Arguments     :
     53 * stream        : Bitstream
     54 * ai2_code_table     : Table used for decoding
     55 * maxLen        : Maximum Length of the decoded symbol in bits
     56 *
     57 * Value Returned: Decoded symbol
     58 *******************************************************************************/
     59 WORD16 impeg2d_dec_vld_symbol(stream_t *ps_stream,const WORD16 ai2_code_table[][2],  UWORD16 u2_max_len)
     60 {
     61   UWORD16 u2_data;
     62   WORD16  u2_end = 0;
     63   UWORD16 u2_org_max_len = u2_max_len;
     64   UWORD16 u2_i_bit;
     65 
     66   /* Get the maximum number of bits needed to decode a symbol */
     67   u2_data = impeg2d_bit_stream_nxt(ps_stream,u2_max_len);
     68   do
     69   {
     70     u2_max_len--;
     71     /* Read one bit at a time from the variable to decode the huffman code */
     72     u2_i_bit = (UWORD8)((u2_data >> u2_max_len) & 0x1);
     73 
     74     /* Get the next node pointer or the symbol from the tree */
     75     u2_end = ai2_code_table[u2_end][u2_i_bit];
     76   }while(u2_end > 0);
     77 
     78   /* Flush the appropriate number of bits from the ps_stream */
     79   impeg2d_bit_stream_flush(ps_stream,(UWORD8)(u2_org_max_len - u2_max_len));
     80   return(u2_end);
     81 }
     82 /*******************************************************************************
     83 * Function name : impeg2d_fast_dec_vld_symbol
     84 *
     85 * Description   : Performs decoding of VLD symbol. It performs decoding by
     86 *                 processing n bits at a time
     87 *
     88 * Arguments     :
     89 * stream        : Bitstream
     90 * ai2_code_table     : Code table containing huffman value
     91 * indexTable    : Index table containing index
     92 * maxLen        : Maximum Length of the decoded symbol in bits
     93 *
     94 * Value Returned: Decoded symbol
     95 *******************************************************************************/
     96 WORD16 impeg2d_fast_dec_vld_symbol(stream_t *ps_stream,
     97                      const WORD16  ai2_code_table[][2],
     98                      const UWORD16 au2_indexTable[][2],
     99                      UWORD16 u2_max_len)
    100 {
    101     UWORD16 u2_cur_code;
    102     UWORD16 u2_num_bits;
    103     UWORD16 u2_vld_offset;
    104     UWORD16 u2_start_len;
    105     WORD16  u2_value;
    106     UWORD16 u2_len;
    107     UWORD16 u2_huffCode;
    108 
    109     u2_start_len  = au2_indexTable[0][0];
    110     u2_vld_offset = 0;
    111     u2_huffCode  = impeg2d_bit_stream_nxt(ps_stream,u2_max_len);
    112     do
    113     {
    114         u2_cur_code = u2_huffCode >> (u2_max_len - u2_start_len);
    115         u2_num_bits = ai2_code_table[u2_cur_code + u2_vld_offset][0];
    116         if(u2_num_bits == 0)
    117         {
    118             u2_huffCode  &= ((1 << (u2_max_len - u2_start_len)) - 1);
    119             u2_max_len    -= u2_start_len;
    120             u2_start_len   = au2_indexTable[ai2_code_table[u2_cur_code + u2_vld_offset][1]][0];
    121             u2_vld_offset  = au2_indexTable[ai2_code_table[u2_cur_code + u2_vld_offset][1]][1];
    122         }
    123         else
    124         {
    125             u2_value = ai2_code_table[u2_cur_code + u2_vld_offset][1];
    126             u2_len   = u2_num_bits;
    127         }
    128     }while(u2_num_bits == 0);
    129     impeg2d_bit_stream_flush(ps_stream,u2_len);
    130     return(u2_value);
    131 }
    132 /******************************************************************************
    133 *
    134 *  Function Name   : impeg2d_dec_ac_coeff_zero
    135 *
    136 *  Description     : Decodes using Table B.14
    137 *
    138 *  Arguments       : Pointer to VideoObjectLayerStructure
    139 *
    140 *  Values Returned : Decoded value
    141 *
    142 *  Revision History:
    143 *
    144 *         28 02 2002  AR        Creation
    145 *******************************************************************************/
    146 UWORD16 impeg2d_dec_ac_coeff_zero(stream_t *ps_stream, UWORD16* pu2_sym_len, UWORD16* pu2_sym_val)
    147 {
    148     UWORD16 u2_offset,u2_decoded_value;
    149     UWORD8  u1_shift;
    150     UWORD32 u4_bits_read;
    151 
    152     u4_bits_read = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,MPEG2_AC_COEFF_MAX_LEN);
    153 
    154     if ((UWORD16)u4_bits_read >= 0x0800)
    155     {
    156         u2_offset = (UWORD16)u4_bits_read >> 11;
    157     }
    158     else if ((UWORD16)u4_bits_read >= 0x40)
    159     {
    160         u2_offset = 31 + ((UWORD16)u4_bits_read >> 6);
    161     }
    162     else if ((UWORD16)u4_bits_read >= 0x20)
    163     {
    164         u2_offset = 64;
    165     }
    166     else
    167     {
    168         u2_offset      = 63;
    169         u4_bits_read    = (UWORD16)u4_bits_read - 0x10;
    170     }
    171     /*-----------------------------------------------------------------------
    172      * The table gOffset contains both the offset for the group to which the
    173      * Vld code belongs in the Ac Coeff Table and the no of bits with which
    174      * the BitsRead should be shifted
    175      *-----------------------------------------------------------------------*/
    176     u2_offset = gau2_impeg2d_offset_zero[u2_offset];
    177     u1_shift  = u2_offset & 0xF;
    178 
    179     /*-----------------------------------------------------------------------
    180      * Depending upon the vld code, we index exactly to that particular
    181      * Vld codes value in the Ac Coeff Table.
    182      * (Offset >> 4)       gives the offset for the group in the AcCoeffTable.
    183      * (BitsRead >> shift) gives the offset within its group
    184      *-----------------------------------------------------------------------*/
    185      u2_offset = (u2_offset >> 4) + ((UWORD16)u4_bits_read >> u1_shift);
    186     /*-----------------------------------------------------------------------
    187      * DecodedValue has the Run, Level and the number of bits used by Vld code
    188      *-----------------------------------------------------------------------*/
    189     u2_decoded_value = gau2_impeg2d_dct_coeff_zero[u2_offset];
    190     if(u2_decoded_value == END_OF_BLOCK)
    191     {
    192         *pu2_sym_len = 2;
    193         *pu2_sym_val = EOB_CODE_VALUE;
    194     }
    195     else if(u2_decoded_value == ESCAPE_CODE)
    196     {
    197         *pu2_sym_len     = u2_decoded_value & 0x1F;
    198         *pu2_sym_val = ESC_CODE_VALUE;
    199     }
    200     else
    201     {
    202         *pu2_sym_len = u2_decoded_value & 0x1F;
    203         *pu2_sym_val = u2_decoded_value >> 5;
    204     }
    205     return(u2_decoded_value);
    206 }
    207 
    208 /******************************************************************************
    209 *
    210 *  Function Name   : impeg2d_dec_ac_coeff_one
    211 *
    212 *  Description     : Decodes using Table B.15
    213 *
    214 *  Arguments       : Pointer to VideoObjectLayerStructure
    215 *
    216 *  Values Returned : Decoded value
    217 *
    218 *  Revision History:
    219 *
    220 *         28 02 2002  AR        Creation
    221 *******************************************************************************/
    222 UWORD16 impeg2d_dec_ac_coeff_one(stream_t *ps_stream, UWORD16* pu2_sym_len, UWORD16* pu2_sym_val)
    223 {
    224     UWORD16 u2_offset, u2_decoded_value;
    225     UWORD8  u1_shift;
    226     UWORD32 u4_bits_read;
    227 
    228 
    229     u4_bits_read = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,MPEG2_AC_COEFF_MAX_LEN);
    230 
    231     if ((UWORD16)u4_bits_read >= 0x8000)
    232     {
    233         /* If the MSB of the vld code is 1 */
    234         if (((UWORD16)u4_bits_read >> 12) == 0xF)
    235             u2_offset = ((UWORD16)u4_bits_read >> 8) & 0xF;
    236         else
    237             u2_offset = (UWORD16)u4_bits_read >> 11;
    238         u2_offset += gau2_impeg2d_offset_one[0];
    239     }
    240     else if ((UWORD16)u4_bits_read >= 0x400)
    241     {
    242         u2_offset =(UWORD16) u4_bits_read >> 10;
    243         u2_offset = gau2_impeg2d_offset_one[u2_offset];
    244         u1_shift = u2_offset & 0xF;
    245         u2_offset = (u2_offset >> 4) + ((UWORD16)u4_bits_read >> u1_shift);
    246     }
    247     else if ((UWORD16)u4_bits_read >= 0x20)
    248     {
    249         u2_offset = ((UWORD16)u4_bits_read >> 5) + 31;
    250         u2_offset = gau2_impeg2d_offset_one[u2_offset];
    251         u1_shift = u2_offset & 0xF;
    252         u2_offset = (u2_offset >> 4) + ((UWORD16)u4_bits_read >> u1_shift);
    253     }
    254     else
    255     {
    256         u2_offset = gau2_impeg2d_offset_one[63] + ((UWORD16)u4_bits_read & 0xF);
    257     }
    258     /*-----------------------------------------------------------------------
    259     * DecodedValue has the Run, Level and the number of bits used by Vld code
    260     *-----------------------------------------------------------------------*/
    261     u2_decoded_value = gau2_impeg2d_dct_coeff_one[u2_offset];
    262 
    263     if(u2_decoded_value == END_OF_BLOCK)
    264     {
    265         *pu2_sym_len = 4;
    266         *pu2_sym_val = EOB_CODE_VALUE;
    267     }
    268     else if(u2_decoded_value == ESCAPE_CODE)
    269     {
    270         *pu2_sym_len     = u2_decoded_value & 0x1F;
    271         *pu2_sym_val = ESC_CODE_VALUE;
    272     }
    273     else
    274     {
    275         *pu2_sym_len = u2_decoded_value & 0x1F;
    276         *pu2_sym_val = u2_decoded_value >> 5;
    277     }
    278 
    279     return(u2_decoded_value);
    280 }
    281 
    282 /******************************************************************************
    283  *
    284  *  Function Name   : impeg2d_vld_inv_quant_mpeg1
    285  *
    286  *  Description     : Performs VLD operation for MPEG1/2
    287  *
    288  *  Arguments       :
    289  *  state           : VLCD state parameter
    290  *  regs            : Registers of VLCD
    291  *
    292  *  Values Returned : None
    293  ******************************************************************************/
    294 IMPEG2D_ERROR_CODES_T impeg2d_vld_inv_quant_mpeg1(
    295                              void  *pv_dec,           /* Decoder State */
    296                              WORD16       *pi2_out_addr,       /*!< Address where decoded symbols will be stored */
    297                              const UWORD8 *pu1_scan,          /*!< Scan table to be used */
    298                              UWORD16      u2_intra_flag,      /*!< Intra Macroblock or not */
    299                              UWORD16      u2_colr_comp,      /*!< 0 - Luma,1 - U comp, 2 - V comp */
    300                              UWORD16      u2_d_picture        /*!< D Picture or not */
    301                              )
    302 {
    303     UWORD8  *pu1_weighting_matrix;
    304     dec_state_t *ps_dec    = (dec_state_t *) pv_dec;
    305     IMPEG2D_ERROR_CODES_T e_error   = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
    306 
    307     WORD16  pi2_coeffs[NUM_COEFFS];
    308     UWORD8  pu1_pos[NUM_COEFFS];
    309     WORD32  i4_num_coeffs;
    310 
    311     /* Perform VLD on the stream to get the coefficients and their positions */
    312     e_error = impeg2d_vld_decode(ps_dec, pi2_coeffs, pu1_scan, pu1_pos, u2_intra_flag,
    313                                  u2_colr_comp, u2_d_picture, ps_dec->u2_intra_vlc_format,
    314                                  ps_dec->u2_is_mpeg2, &i4_num_coeffs);
    315     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
    316     {
    317         return e_error;
    318     }
    319 
    320     /* For YUV420 format,Select the weighting matrix according to Table 7.5 */
    321     pu1_weighting_matrix = (u2_intra_flag == 1) ? ps_dec->au1_intra_quant_matrix:
    322                     ps_dec->au1_inter_quant_matrix;
    323 
    324     IMPEG2D_IQNT_INP_STATISTICS(pi2_out_addr, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
    325     /* Inverse Quantize the Output of VLD */
    326     PROFILE_DISABLE_INVQUANT_IF0
    327 
    328     {
    329         /* Clear output matrix */
    330         PROFILE_DISABLE_MEMSET_RESBUF_IF0
    331         if (1 != (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
    332         {
    333             ps_dec->pf_memset_16bit_8x8_linear_block (pi2_out_addr);
    334         }
    335 
    336         impeg2d_inv_quant_mpeg1(pi2_out_addr, pu1_weighting_matrix,
    337                                   ps_dec->u1_quant_scale, u2_intra_flag,
    338                                   i4_num_coeffs, pi2_coeffs, pu1_pos,
    339                                   pu1_scan, &ps_dec->u2_def_dc_pred[u2_colr_comp],
    340                                   ps_dec->u2_intra_dc_precision);
    341 
    342         if (0 != pi2_out_addr[0])
    343         {
    344             /* The first coeff might've become non-zero due to intra_dc_decision
    345              * value. So, check here after inverse quantization.
    346              */
    347             ps_dec->u4_non_zero_cols  |= 0x1;
    348             ps_dec->u4_non_zero_rows  |= 0x1;
    349         }
    350     }
    351 
    352     return e_error;
    353 }
    354 
    355 /******************************************************************************
    356   *
    357   *  Function Name   : impeg2d_vld_inv_quant_mpeg2
    358   *
    359   *  Description     : Performs VLD operation for MPEG1/2
    360   *
    361   *  Arguments       :
    362   *  state           : VLCD state parameter
    363   *  regs            : Registers of VLCD
    364   *
    365   *  Values Returned : None
    366   ******************************************************************************/
    367 IMPEG2D_ERROR_CODES_T impeg2d_vld_inv_quant_mpeg2(
    368                              void  *pv_dec,           /* Decoder State */
    369                              WORD16       *pi2_out_addr,       /*!< Address where decoded symbols will be stored */
    370                              const UWORD8 *pu1_scan,          /*!< Scan table to be used */
    371                              UWORD16      u2_intra_flag,      /*!< Intra Macroblock or not */
    372                              UWORD16      u2_colr_comp,      /*!< 0 - Luma,1 - U comp, 2 - V comp */
    373                              UWORD16      u2_d_picture        /*!< D Picture or not */
    374                              )
    375 {
    376     UWORD8  *pu1_weighting_matrix;
    377     WORD32 u4_sum_is_even;
    378     dec_state_t *ps_dec = (dec_state_t *)pv_dec;
    379     IMPEG2D_ERROR_CODES_T e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
    380 
    381     WORD16  pi2_coeffs[NUM_COEFFS];
    382     UWORD8  pi4_pos[NUM_COEFFS];
    383     WORD32  i4_num_coeffs;
    384 
    385     /* Perform VLD on the stream to get the coefficients and their positions */
    386     e_error = impeg2d_vld_decode(ps_dec, pi2_coeffs, pu1_scan, pi4_pos, u2_intra_flag,
    387                                  u2_colr_comp, u2_d_picture, ps_dec->u2_intra_vlc_format,
    388                                  ps_dec->u2_is_mpeg2, &i4_num_coeffs);
    389     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
    390     {
    391         return e_error;
    392     }
    393 
    394     /* For YUV420 format,Select the weighting matrix according to Table 7.5 */
    395     pu1_weighting_matrix = (u2_intra_flag == 1) ? ps_dec->au1_intra_quant_matrix:
    396                     ps_dec->au1_inter_quant_matrix;
    397 
    398     /*mismatch control for mpeg2*/
    399     /* Check if the block has only one non-zero coeff which is DC  */
    400     ps_dec->i4_last_value_one = 0;
    401 
    402     IMPEG2D_IQNT_INP_STATISTICS(pi2_out_addr, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
    403 
    404     /* Inverse Quantize the Output of VLD */
    405     PROFILE_DISABLE_INVQUANT_IF0
    406 
    407     {
    408         /* Clear output matrix */
    409         PROFILE_DISABLE_MEMSET_RESBUF_IF0
    410         if (1 != (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
    411         {
    412             ps_dec->pf_memset_16bit_8x8_linear_block (pi2_out_addr);
    413         }
    414 
    415         u4_sum_is_even  = impeg2d_inv_quant_mpeg2(pi2_out_addr, pu1_weighting_matrix,
    416                                                  ps_dec->u1_quant_scale, u2_intra_flag,
    417                                                  i4_num_coeffs, pi2_coeffs,
    418                                                  pi4_pos, pu1_scan,
    419                                                  &ps_dec->u2_def_dc_pred[u2_colr_comp],
    420                                                  ps_dec->u2_intra_dc_precision);
    421 
    422         if (0 != pi2_out_addr[0])
    423         {
    424             /* The first coeff might've become non-zero due to intra_dc_decision
    425              * value. So, check here after inverse quantization.
    426              */
    427             ps_dec->u4_non_zero_cols  |= 0x1;
    428             ps_dec->u4_non_zero_rows  |= 0x1;
    429         }
    430 
    431         if (1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
    432         {
    433             ps_dec->i4_last_value_one = 1 - (pi2_out_addr[0] & 1);
    434         }
    435         else
    436         {
    437             /*toggle last bit if sum is even ,else retain it as it is*/
    438             pi2_out_addr[63]        ^= (u4_sum_is_even & 1);
    439 
    440             if (0 != pi2_out_addr[63])
    441             {
    442                 ps_dec->u4_non_zero_cols  |= 0x80;
    443                 ps_dec->u4_non_zero_rows  |= 0x80;
    444             }
    445         }
    446     }
    447 
    448     return e_error;
    449 }
    450 
    451 
    452 /******************************************************************************
    453 *
    454 *  Function Name   : impeg2d_vld_decode
    455 *
    456 *  Description     : Performs VLD operation for MPEG1/2
    457 *
    458 *  Arguments       :
    459 *  state           : VLCD state parameter
    460 *  regs            : Registers of VLCD
    461 *
    462 *  Values Returned : None
    463 ******************************************************************************/
    464 IMPEG2D_ERROR_CODES_T impeg2d_vld_decode(
    465     dec_state_t *ps_dec,
    466     WORD16      *pi2_outAddr,       /*!< Address where decoded symbols will be stored */
    467     const UWORD8 *pu1_scan,         /*!< Scan table to be used */
    468     UWORD8      *pu1_pos,       /*!< Scan table to be used */
    469     UWORD16     u2_intra_flag,      /*!< Intra Macroblock or not */
    470     UWORD16     u2_chroma_flag,     /*!< Chroma Block or not */
    471     UWORD16     u2_d_picture,       /*!< D Picture or not */
    472     UWORD16     u2_intra_vlc_format, /*!< Intra VLC format */
    473     UWORD16     u2_mpeg2,          /*!< MPEG-2 or not */
    474     WORD32      *pi4_num_coeffs /*!< Returns the number of coeffs in block */
    475     )
    476 {
    477 
    478     UWORD32 u4_sym_len;
    479 
    480     UWORD32 u4_decoded_value;
    481     UWORD32 u4_level_first_byte;
    482     WORD32  u4_level;
    483     UWORD32 u4_run, u4_numCoeffs;
    484     UWORD32 u4_buf;
    485     UWORD32 u4_buf_nxt;
    486     UWORD32 u4_offset;
    487     UWORD32 *pu4_buf_aligned;
    488     UWORD32 u4_bits;
    489     stream_t *ps_stream = &ps_dec->s_bit_stream;
    490     WORD32  u4_pos;
    491     UWORD32 u4_nz_cols;
    492     UWORD32 u4_nz_rows;
    493 
    494     *pi4_num_coeffs = 0;
    495 
    496     ps_dec->u4_non_zero_cols = 0;
    497     ps_dec->u4_non_zero_rows = 0;
    498     u4_nz_cols = ps_dec->u4_non_zero_cols;
    499     u4_nz_rows = ps_dec->u4_non_zero_rows;
    500 
    501     GET_TEMP_STREAM_DATA(u4_buf,u4_buf_nxt,u4_offset,pu4_buf_aligned,ps_stream)
    502     /**************************************************************************/
    503     /* Decode the DC coefficient in case of Intra block                       */
    504     /**************************************************************************/
    505     if(u2_intra_flag)
    506     {
    507         WORD32 dc_size;
    508         WORD32 dc_diff;
    509         WORD32 maxLen;
    510         WORD32 idx;
    511 
    512 
    513         maxLen = MPEG2_DCT_DC_SIZE_LEN;
    514         idx = 0;
    515         if(u2_chroma_flag != 0)
    516         {
    517             maxLen += 1;
    518             idx++;
    519         }
    520 
    521 
    522         {
    523             WORD16  end = 0;
    524             UWORD32 maxLen_tmp = maxLen;
    525             UWORD16 m_iBit;
    526 
    527 
    528             /* Get the maximum number of bits needed to decode a symbol */
    529             IBITS_NXT(u4_buf,u4_buf_nxt,u4_offset,u4_bits,maxLen)
    530             do
    531             {
    532                 maxLen_tmp--;
    533                 /* Read one bit at a time from the variable to decode the huffman code */
    534                 m_iBit = (UWORD8)((u4_bits >> maxLen_tmp) & 0x1);
    535 
    536                 /* Get the next node pointer or the symbol from the tree */
    537                 end = gai2_impeg2d_dct_dc_size[idx][end][m_iBit];
    538             }while(end > 0);
    539             dc_size = end + MPEG2_DCT_DC_SIZE_OFFSET;
    540 
    541             /* Flush the appropriate number of bits from the stream */
    542             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,(maxLen - maxLen_tmp),pu4_buf_aligned)
    543 
    544         }
    545 
    546 
    547 
    548         if (dc_size != 0)
    549         {
    550             UWORD32 u4_bits;
    551 
    552             IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned, dc_size)
    553             dc_diff = u4_bits;
    554 
    555             if ((dc_diff & (1 << (dc_size - 1))) == 0) //v Probably the prediction algo?
    556                 dc_diff -= (1 << dc_size) - 1;
    557         }
    558         else
    559         {
    560             dc_diff = 0;
    561         }
    562 
    563 
    564         pi2_outAddr[*pi4_num_coeffs]    = dc_diff;
    565         /* This indicates the position of the coefficient. Since this is the DC
    566          * coefficient, we put the position as 0.
    567          */
    568         pu1_pos[*pi4_num_coeffs]    = pu1_scan[0];
    569         (*pi4_num_coeffs)++;
    570 
    571         if (0 != dc_diff)
    572         {
    573             u4_nz_cols |= 0x01;
    574             u4_nz_rows |= 0x01;
    575         }
    576 
    577         u4_numCoeffs = 1;
    578     }
    579     /**************************************************************************/
    580     /* Decoding of first AC coefficient in case of non Intra block            */
    581     /**************************************************************************/
    582     else
    583     {
    584         /* First symbol can be 1s */
    585         UWORD32 u4_bits;
    586 
    587         IBITS_NXT(u4_buf,u4_buf_nxt,u4_offset,u4_bits,1)
    588 
    589         if(u4_bits == 1)
    590         {
    591 
    592             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,1, pu4_buf_aligned)
    593             IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned, 1)
    594             if(u4_bits == 1)
    595             {
    596                 pi2_outAddr[*pi4_num_coeffs] = -1;
    597             }
    598             else
    599             {
    600                 pi2_outAddr[*pi4_num_coeffs] = 1;
    601             }
    602 
    603             /* This indicates the position of the coefficient. Since this is the DC
    604              * coefficient, we put the position as 0.
    605              */
    606             pu1_pos[*pi4_num_coeffs]    = pu1_scan[0];
    607             (*pi4_num_coeffs)++;
    608             u4_numCoeffs = 1;
    609 
    610             u4_nz_cols |= 0x01;
    611             u4_nz_rows |= 0x01;
    612         }
    613         else
    614         {
    615             u4_numCoeffs = 0;
    616         }
    617     }
    618     if (1 == u2_d_picture)
    619     {
    620         PUT_TEMP_STREAM_DATA(u4_buf, u4_buf_nxt, u4_offset, pu4_buf_aligned, ps_stream)
    621         ps_dec->u4_non_zero_cols  = u4_nz_cols;
    622         ps_dec->u4_non_zero_rows  = u4_nz_rows;
    623         return ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE);
    624     }
    625 
    626 
    627 
    628         if (1 == u2_intra_vlc_format && u2_intra_flag)
    629         {
    630 
    631             while(1)
    632             {
    633                 //Putting the impeg2d_dec_ac_coeff_one function inline.
    634 
    635                 UWORD32 lead_zeros;
    636                 WORD16 DecodedValue;
    637 
    638                 u4_sym_len = 17;
    639                 IBITS_NXT(u4_buf,u4_buf_nxt,u4_offset,u4_bits,u4_sym_len)
    640 
    641                 DecodedValue = gau2_impeg2d_tab_one_1_9[u4_bits >> 8];
    642                 u4_sym_len = (DecodedValue & 0xf);
    643                 u4_level = DecodedValue >> 9;
    644                 /* One table lookup */
    645                 if(0 != u4_level)
    646                 {
    647                     u4_run = ((DecodedValue >> 4) & 0x1f);
    648                     u4_numCoeffs       += u4_run;
    649                     u4_pos             = pu1_scan[u4_numCoeffs++ & 63];
    650                     pu1_pos[*pi4_num_coeffs]    = u4_pos;
    651 
    652                     FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
    653                     pi2_outAddr[*pi4_num_coeffs]    = u4_level;
    654 
    655                     (*pi4_num_coeffs)++;
    656                 }
    657                 else
    658                 {
    659                     if (DecodedValue == END_OF_BLOCK_ONE)
    660                     {
    661                         u4_sym_len = 4;
    662 
    663                         break;
    664                     }
    665                     else
    666                     {
    667                         /*Second table lookup*/
    668                         lead_zeros = CLZ(u4_bits) - 20;/* -16 since we are dealing with WORD32 */
    669                         if (0 != lead_zeros)
    670                         {
    671 
    672                             u4_bits         = (u4_bits >> (6 - lead_zeros)) & 0x001F;
    673 
    674                             /* Flush the number of bits */
    675                             if (1 == lead_zeros)
    676                             {
    677                                 u4_sym_len         = ((u4_bits & 0x18) >> 3) == 2 ? 11:10;
    678                             }
    679                             else
    680                             {
    681                                 u4_sym_len         = 11 + lead_zeros;
    682                             }
    683                             /* flushing */
    684                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
    685 
    686                             /* Calculate the address */
    687                             u4_bits         = ((lead_zeros - 1) << 5) + u4_bits;
    688 
    689                             DecodedValue    = gau2_impeg2d_tab_one_10_16[u4_bits];
    690 
    691                             u4_run = BITS(DecodedValue, 8,4);
    692                             u4_level = ((WORD16) DecodedValue) >> 9;
    693 
    694                             u4_numCoeffs       += u4_run;
    695                             u4_pos             = pu1_scan[u4_numCoeffs++ & 63];
    696                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
    697                             pi2_outAddr[*pi4_num_coeffs]    = u4_level;
    698                             (*pi4_num_coeffs)++;
    699                         }
    700                         /*********************************************************************/
    701                         /* MPEG2 Escape Code                                                 */
    702                         /*********************************************************************/
    703                         else if(u2_mpeg2 == 1)
    704                         {
    705                             u4_sym_len         = 6;
    706                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
    707                                 IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,18)
    708                                 u4_decoded_value    = u4_bits;
    709                             u4_run             = (u4_decoded_value >> 12);
    710                             u4_level           = (u4_decoded_value & 0x0FFF);
    711 
    712                             if (u4_level)
    713                                 u4_level = (u4_level - ((u4_level & 0x0800) << 1));
    714 
    715                             u4_numCoeffs       += u4_run;
    716                             u4_pos             = pu1_scan[u4_numCoeffs++ & 63];
    717                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
    718                             pi2_outAddr[*pi4_num_coeffs]    = u4_level;
    719                             (*pi4_num_coeffs)++;
    720                         }
    721                         /*********************************************************************/
    722                         /* MPEG1 Escape Code                                                 */
    723                         /*********************************************************************/
    724                         else
    725                         {
    726                             /*-----------------------------------------------------------
    727                             * MPEG-1 Stream
    728                             *
    729                             * <See D.9.3 of MPEG-2> Run-level escape syntax
    730                             * Run-level values that cannot be coded with a VLC are coded
    731                             * by the escape code '0000 01' followed by
    732                             * either a 14-bit FLC (127 <= level <= 127),
    733                             * or a 22-bit FLC (255 <= level <= 255).
    734                             * This is described in Annex B,B.5f of MPEG-1.standard
    735                             *-----------------------------------------------------------*/
    736 
    737                             /*-----------------------------------------------------------
    738                             * First 6 bits are the value of the Run. Next is First 8 bits
    739                             * of Level. These bits decide whether it is 14 bit FLC or
    740                             * 22-bit FLC.
    741                             *
    742                             * If( first 8 bits of Level == '1000000' or '00000000')
    743                             *      then its is 22-bit FLC.
    744                             * else
    745                             *      it is 14-bit FLC.
    746                             *-----------------------------------------------------------*/
    747                             u4_sym_len         = 6;
    748                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
    749                                 IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,14)
    750                                 u4_decoded_value     = u4_bits;
    751                             u4_run              = (u4_decoded_value >> 8);
    752                             u4_level_first_byte = (u4_decoded_value & 0x0FF);
    753                             if(u4_level_first_byte & 0x7F)
    754                             {
    755                                 /*-------------------------------------------------------
    756                                 * First 8 bits of level are neither 1000000 nor 00000000
    757                                 * Hence 14-bit FLC (Last 8 bits are used to get level)
    758                                 *
    759                                 *  Level = (msb of Level_First_Byte is 1)?
    760                                 *          Level_First_Byte - 256 : Level_First_Byte
    761                                 *-------------------------------------------------------*/
    762                                 u4_level = (u4_level_first_byte -
    763                                     ((u4_level_first_byte & 0x80) << 1));
    764                             }
    765                             else
    766                             {
    767                                 /*-------------------------------------------------------
    768                                 * Next 8 bits are either 1000000 or 00000000
    769                                 * Hence 22-bit FLC (Last 16 bits are used to get level)
    770                                 *
    771                                 *  Level = (msb of Level_First_Byte is 1)?
    772                                 *          Level_Second_Byte - 256 : Level_Second_Byte
    773                                 *-------------------------------------------------------*/
    774                                 IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,8)
    775                                     u4_level = u4_bits;
    776                                 u4_level = (u4_level - (u4_level_first_byte << 1));
    777                             }
    778                             u4_numCoeffs += u4_run;
    779 
    780                             u4_pos = pu1_scan[u4_numCoeffs++ & 63];
    781 
    782                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
    783                             pi2_outAddr[*pi4_num_coeffs]    = u4_level;
    784                             (*pi4_num_coeffs)++;
    785                         }
    786                     }
    787                 }
    788 
    789                 u4_nz_cols |= 1 << (u4_pos & 0x7);
    790                 u4_nz_rows |= 1 << (u4_pos >> 0x3);
    791 
    792                 if (u4_numCoeffs > 64)
    793                 {
    794                     return IMPEG2D_MB_TEX_DECODE_ERR;
    795                 }
    796 
    797             }
    798             IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,u4_sym_len)
    799         }
    800         else
    801         {
    802             // Inline
    803             while(1)
    804             {
    805 
    806                 UWORD32 lead_zeros;
    807                 UWORD16 DecodedValue;
    808 
    809                 u4_sym_len = 17;
    810                 IBITS_NXT(u4_buf, u4_buf_nxt, u4_offset, u4_bits, u4_sym_len)
    811 
    812 
    813                 DecodedValue = gau2_impeg2d_tab_zero_1_9[u4_bits >> 8];
    814                 u4_sym_len = BITS(DecodedValue, 3, 0);
    815                 u4_level = ((WORD16) DecodedValue) >> 9;
    816 
    817                 if (0 != u4_level)
    818                 {
    819                     u4_run = BITS(DecodedValue, 8,4);
    820 
    821                     u4_numCoeffs       += u4_run;
    822 
    823                     u4_pos                 = pu1_scan[u4_numCoeffs++ & 63];
    824                     pu1_pos[*pi4_num_coeffs]    = u4_pos;
    825 
    826                     FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
    827                     pi2_outAddr[*pi4_num_coeffs]    = u4_level;
    828                     (*pi4_num_coeffs)++;
    829                 }
    830                 else
    831                 {
    832                     if(DecodedValue == END_OF_BLOCK_ZERO)
    833                     {
    834                         u4_sym_len = 2;
    835 
    836                         break;
    837                     }
    838                     else
    839                     {
    840                         lead_zeros = CLZ(u4_bits) - 20;/* -15 since we are dealing with WORD32 */
    841                         /*Second table lookup*/
    842                         if (0 != lead_zeros)
    843                         {
    844                             u4_bits         = (u4_bits >> (6 - lead_zeros)) & 0x001F;
    845 
    846                             /* Flush the number of bits */
    847                             u4_sym_len         = 11 + lead_zeros;
    848 
    849                             /* Calculate the address */
    850                             u4_bits         = ((lead_zeros - 1) << 5) + u4_bits;
    851 
    852                             DecodedValue    = gau2_impeg2d_tab_zero_10_16[u4_bits];
    853 
    854                             u4_run = BITS(DecodedValue, 8,4);
    855                             u4_level = ((WORD16) DecodedValue) >> 9;
    856 
    857                             u4_numCoeffs       += u4_run;
    858 
    859                             u4_pos                 = pu1_scan[u4_numCoeffs++ & 63];
    860                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
    861                             if (1 == lead_zeros)
    862                                 u4_sym_len--;
    863                             /* flushing */
    864                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
    865                             pi2_outAddr[*pi4_num_coeffs]    = u4_level;
    866 
    867                             (*pi4_num_coeffs)++;
    868                         }
    869                         /*Escape Sequence*/
    870                         else if(u2_mpeg2 == 1)
    871                         {
    872                             u4_sym_len         = 6;
    873                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
    874                             IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,18)
    875                             u4_decoded_value    = u4_bits;
    876                             u4_run             = (u4_decoded_value >> 12);
    877                             u4_level           = (u4_decoded_value & 0x0FFF);
    878 
    879                             if (u4_level)
    880                                 u4_level = (u4_level - ((u4_level & 0x0800) << 1));
    881 
    882                             u4_numCoeffs           += u4_run;
    883 
    884                             u4_pos                 = pu1_scan[u4_numCoeffs++ & 63];
    885                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
    886                             pi2_outAddr[*pi4_num_coeffs]    = u4_level;
    887 
    888                             (*pi4_num_coeffs)++;
    889                         }
    890                         /*********************************************************************/
    891                         /* MPEG1 Escape Code                                                 */
    892                         /*********************************************************************/
    893                         else
    894                         {
    895                             /*-----------------------------------------------------------
    896                             * MPEG-1 Stream
    897                             *
    898                             * <See D.9.3 of MPEG-2> Run-level escape syntax
    899                             * Run-level values that cannot be coded with a VLC are coded
    900                             * by the escape code '0000 01' followed by
    901                             * either a 14-bit FLC (127 <= level <= 127),
    902                             * or a 22-bit FLC (255 <= level <= 255).
    903                             * This is described in Annex B,B.5f of MPEG-1.standard
    904                             *-----------------------------------------------------------*/
    905 
    906                             /*-----------------------------------------------------------
    907                             * First 6 bits are the value of the Run. Next is First 8 bits
    908                             * of Level. These bits decide whether it is 14 bit FLC or
    909                             * 22-bit FLC.
    910                             *
    911                             * If( first 8 bits of Level == '1000000' or '00000000')
    912                             *      then its is 22-bit FLC.
    913                             * else
    914                             *      it is 14-bit FLC.
    915                             *-----------------------------------------------------------*/
    916                             u4_sym_len             = 6;
    917                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
    918                             IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,14)
    919                             u4_decoded_value        = u4_bits;
    920                             u4_run                 = (u4_decoded_value >> 8);
    921                             u4_level_first_byte    = (u4_decoded_value & 0x0FF);
    922                             if(u4_level_first_byte & 0x7F)
    923                             {
    924                                 /*-------------------------------------------------------
    925                                 * First 8 bits of level are neither 1000000 nor 00000000
    926                                 * Hence 14-bit FLC (Last 8 bits are used to get level)
    927                                 *
    928                                 *  Level = (msb of Level_First_Byte is 1)?
    929                                 *          Level_First_Byte - 256 : Level_First_Byte
    930                                 *-------------------------------------------------------*/
    931                                 u4_level = (u4_level_first_byte -
    932                                     ((u4_level_first_byte & 0x80) << 1));
    933                             }
    934                             else
    935                             {
    936                                 /*-------------------------------------------------------
    937                                 * Next 8 bits are either 1000000 or 00000000
    938                                 * Hence 22-bit FLC (Last 16 bits are used to get level)
    939                                 *
    940                                 *  Level = (msb of Level_First_Byte is 1)?
    941                                 *          Level_Second_Byte - 256 : Level_Second_Byte
    942                                 *-------------------------------------------------------*/
    943                                 IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,8)
    944                                 u4_level = u4_bits;
    945                                 u4_level = (u4_level - (u4_level_first_byte << 1));
    946                             }
    947                             u4_numCoeffs           += u4_run;
    948 
    949                             u4_pos                 = pu1_scan[u4_numCoeffs++ & 63];
    950                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
    951                             pi2_outAddr[*pi4_num_coeffs]    = u4_level;
    952 
    953                             (*pi4_num_coeffs)++;
    954                         }
    955                     }
    956                 }
    957 
    958                 u4_nz_cols |= 1 << (u4_pos & 0x7);
    959                 u4_nz_rows |= 1 << (u4_pos >> 0x3);
    960                 if (u4_numCoeffs > 64)
    961                 {
    962                     return IMPEG2D_MB_TEX_DECODE_ERR;
    963                 }
    964 
    965             }
    966 
    967             IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,u4_sym_len)
    968 
    969         }
    970 
    971         PUT_TEMP_STREAM_DATA(u4_buf, u4_buf_nxt, u4_offset, pu4_buf_aligned, ps_stream)
    972 
    973         ps_dec->u4_non_zero_cols  = u4_nz_cols;
    974         ps_dec->u4_non_zero_rows  = u4_nz_rows;
    975 
    976             return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
    977 }
    978 
    979 
    980 
    981 /*****************************************************************************/
    982 /*                                                                           */
    983 /*  Function Name : impeg2d_inv_quant_mpeg1                                   */
    984 /*                                                                           */
    985 /*  Description   : Inverse quantizes the output of VLD                      */
    986 /*                                                                           */
    987 /*  Inputs        :                                                          */
    988 /*  blk,              - Block to be inverse quantized                        */
    989 /*  weighting_matrix  - Matrix to be used in inverse quant                   */
    990 /*  intra_dc_precision- Precision reqd to scale intra DC value               */
    991 /*  quant_scale       - Quanization scale for inverse quant                  */
    992 /*  intra_flag        - Intra or Not                                         */
    993 /*                                                                           */
    994 /*  Globals       : None                                                     */
    995 /*                                                                           */
    996 /*  Processing    : Implements the inverse quantize equation                 */
    997 /*                                                                           */
    998 /*  Outputs       : Inverse quantized values in the block                    */
    999 /*                                                                           */
   1000 /*  Returns       : None                                                     */
   1001 /*                                                                           */
   1002 /*  Issues        : None                                                     */
   1003 /*                                                                           */
   1004 /*  Revision History:                                                        */
   1005 /*                                                                           */
   1006 /*         DD MM YYYY   Author(s)       Changes                              */
   1007 /*         05 09 2005   Harish M        First Version                        */
   1008 /*                                                                           */
   1009 /*****************************************************************************/
   1010 UWORD8 impeg2d_inv_quant_mpeg1(WORD16 *pi2_blk,
   1011                               UWORD8 *pu1_weighting_matrix,
   1012                               UWORD8 u1_quant_scale,
   1013                               WORD32 u4_intra_flag,
   1014                               WORD32 i4_num_coeffs,
   1015                               WORD16 *pi2_coeffs,
   1016                               UWORD8 *pu1_pos,
   1017                               const UWORD8 *pu1_scan,
   1018                               UWORD16 *pu2_def_dc_pred,
   1019                               UWORD16 u2_intra_dc_precision)
   1020 {
   1021     UWORD16 i4_pos;
   1022 
   1023     WORD32  i4_iter;
   1024 
   1025     /* Inverse Quantize the predicted DC value for intra MB*/
   1026     if(u4_intra_flag == 1)
   1027     {
   1028         /**************************************************************************/
   1029         /* Decode the DC coefficient in case of Intra block and also update       */
   1030         /* DC predictor value of the corresponding color component                */
   1031         /**************************************************************************/
   1032         {
   1033             pi2_coeffs[0]   += *pu2_def_dc_pred;
   1034             *pu2_def_dc_pred      = pi2_coeffs[0];
   1035             pi2_coeffs[0]   <<= (3 - u2_intra_dc_precision);
   1036             pi2_coeffs[0]   = CLIP_S12(pi2_coeffs[0]);
   1037         }
   1038 
   1039         pi2_blk[pu1_scan[0]]  = pi2_coeffs[0];
   1040     }
   1041     /************************************************************************/
   1042     /* Inverse quantization of other DCT coefficients                       */
   1043     /************************************************************************/
   1044     for(i4_iter = u4_intra_flag; i4_iter < i4_num_coeffs; i4_iter++)
   1045     {
   1046 
   1047         WORD16 sign;
   1048         WORD32 temp, temp1;
   1049 
   1050         /* Position is the inverse scan of the index stored */
   1051         i4_pos      = pu1_pos[i4_iter];
   1052         pi2_blk[i4_pos] = pi2_coeffs[i4_iter];
   1053 
   1054         sign = SIGN(pi2_blk[i4_pos]);
   1055         temp = ABS(pi2_blk[i4_pos] << 1);
   1056 
   1057         /* pi2_coeffs has only non-zero elements. So no need to check
   1058          * if the coeff is non-zero.
   1059          */
   1060         temp = temp + (1 * !u4_intra_flag);
   1061 
   1062         temp = temp * pu1_weighting_matrix[i4_pos] * u1_quant_scale;
   1063 
   1064         temp = temp >> 5;
   1065 
   1066         temp1 = temp | 1;
   1067 
   1068         temp1 = (temp1 > temp) ? (temp1 - temp) : (temp - temp1);
   1069 
   1070         temp = temp - temp1;
   1071 
   1072         if(temp < 0)
   1073         {
   1074             temp = 0;
   1075         }
   1076 
   1077         temp = temp * sign;
   1078 
   1079         temp = CLIP_S12(temp);
   1080 
   1081         pi2_blk[i4_pos] = temp;
   1082     }
   1083 
   1084     /*return value is used in the case of mpeg2 for mismatch control*/
   1085     return  (0);
   1086 } /* End of inv_quant() */
   1087 
   1088 
   1089 
   1090 /*****************************************************************************/
   1091 /*                                                                           */
   1092 /*  Function Name : impeg2d_inv_quant_mpeg2                                   */
   1093 /*                                                                           */
   1094 /*  Description   : Inverse quantizes the output of VLD                      */
   1095 /*                                                                           */
   1096 /*  Inputs        :                                                          */
   1097 /*  blk,              - Block to be inverse quantized                        */
   1098 /*  weighting_matrix  - Matrix to be used in inverse quant                   */
   1099 /*  intra_dc_precision- Precision reqd to scale intra DC value               */
   1100 /*  quant_scale       - Quanization scale for inverse quant                  */
   1101 /*  intra_flag        - Intra or Not                                         */
   1102 /*                                                                           */
   1103 /*  Globals       : None                                                     */
   1104 /*                                                                           */
   1105 /*  Processing    : Implements the inverse quantize equation                 */
   1106 /*                                                                           */
   1107 /*  Outputs       : Inverse quantized values in the block                    */
   1108 /*                                                                           */
   1109 /*  Returns       : None                                                     */
   1110 /*                                                                           */
   1111 /*  Issues        : None                                                     */
   1112 /*                                                                           */
   1113 /*  Revision History:                                                        */
   1114 /*                                                                           */
   1115 /*         DD MM YYYY   Author(s)       Changes                              */
   1116 /*         05 09 2005   Harish M        First Version                        */
   1117 /*                                                                           */
   1118 /*****************************************************************************/
   1119 UWORD8 impeg2d_inv_quant_mpeg2(WORD16 *pi2_blk,
   1120                               UWORD8 *pu1_weighting_matrix,
   1121                               UWORD8 u1_quant_scale,
   1122                               WORD32 u4_intra_flag,
   1123                               WORD32 i4_num_coeffs,
   1124                               WORD16 *pi2_coeffs,
   1125                               UWORD8 *pu1_pos,
   1126                               const UWORD8 *pu1_scan,
   1127                               UWORD16 *pu2_def_dc_pred,
   1128                               UWORD16 u2_intra_dc_precision)
   1129 {
   1130 
   1131     WORD32  i4_pos;
   1132     /* Used for Mismatch control */
   1133     UWORD32 sum;
   1134 
   1135     WORD32  i4_iter;
   1136 
   1137     sum = 0;
   1138 
   1139     /* Inverse Quantize the predicted DC value for intra MB*/
   1140     if(u4_intra_flag == 1)
   1141     {
   1142         /**************************************************************************/
   1143         /* Decode the DC coefficient in case of Intra block and also update       */
   1144         /* DC predictor value of the corresponding color component                */
   1145         /**************************************************************************/
   1146         {
   1147             pi2_coeffs[0]   += *pu2_def_dc_pred;
   1148             *pu2_def_dc_pred      = pi2_coeffs[0];
   1149             pi2_coeffs[0]   <<= (3 - u2_intra_dc_precision);
   1150             pi2_coeffs[0]   = CLIP_S12(pi2_coeffs[0]);
   1151         }
   1152 
   1153         pi2_blk[pu1_scan[0]]  = pi2_coeffs[0];
   1154         sum = pi2_blk[0];
   1155     }
   1156 
   1157     /************************************************************************/
   1158     /* Inverse quantization of other DCT coefficients                       */
   1159     /************************************************************************/
   1160     for(i4_iter = u4_intra_flag; i4_iter < i4_num_coeffs; i4_iter++)
   1161     {
   1162         WORD16 sign;
   1163         WORD32 temp;
   1164         /* Position is the inverse scan of the index stored */
   1165         i4_pos      = pu1_pos[i4_iter];
   1166         pi2_blk[i4_pos] = pi2_coeffs[i4_iter];
   1167 
   1168         sign = SIGN(pi2_blk[i4_pos]);
   1169         temp = ABS(pi2_blk[i4_pos] << 1);
   1170         temp = temp + (1 * !u4_intra_flag);
   1171         temp = temp * pu1_weighting_matrix[i4_pos] * u1_quant_scale;
   1172 
   1173         temp = temp >> 5;
   1174 
   1175         temp = temp * sign;
   1176 
   1177         temp = CLIP_S12(temp);
   1178 
   1179         pi2_blk[i4_pos] = temp;
   1180 
   1181         sum += temp;
   1182     }
   1183     return (sum ^ 1);
   1184 } /* End of inv_quant() */
   1185