• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *******************************************************************************/
impeg2d_dec_vld_symbol(stream_t * ps_stream,const WORD16 ai2_code_table[][2],UWORD16 u2_max_len)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 *******************************************************************************/
impeg2d_fast_dec_vld_symbol(stream_t * ps_stream,const WORD16 ai2_code_table[][2],const UWORD16 au2_indexTable[][2],UWORD16 u2_max_len)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 *******************************************************************************/
impeg2d_dec_ac_coeff_zero(stream_t * ps_stream,UWORD16 * pu2_sym_len,UWORD16 * pu2_sym_val)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 *******************************************************************************/
impeg2d_dec_ac_coeff_one(stream_t * ps_stream,UWORD16 * pu2_sym_len,UWORD16 * pu2_sym_val)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  ******************************************************************************/
impeg2d_vld_inv_quant_mpeg1(void * pv_dec,WORD16 * pi2_out_addr,const UWORD8 * pu1_scan,UWORD16 u2_intra_flag,UWORD16 u2_colr_comp,UWORD16 u2_d_picture)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   ******************************************************************************/
impeg2d_vld_inv_quant_mpeg2(void * pv_dec,WORD16 * pi2_out_addr,const UWORD8 * pu1_scan,UWORD16 u2_intra_flag,UWORD16 u2_colr_comp,UWORD16 u2_d_picture)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 i4_sum;
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         i4_sum  = 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]        ^= (i4_sum & 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 ******************************************************************************/
impeg2d_vld_decode(dec_state_t * ps_dec,WORD16 * pi2_outAddr,const UWORD8 * pu1_scan,UWORD8 * pu1_pos,UWORD16 u2_intra_flag,UWORD16 u2_chroma_flag,UWORD16 u2_d_picture,UWORD16 u2_intra_vlc_format,UWORD16 u2_mpeg2,WORD32 * pi4_num_coeffs)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     WORD32 i4_level_first_byte;
482     WORD32  i4_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                 /* There cannot be more than 11 leading zeros in the decoded
642                  * symbol. The symbol is only 17 bits long, so we subtract 15.
643                  */
644                 lead_zeros = CLZ(u4_bits) - 15;
645                 if (lead_zeros > 11)
646                 {
647                     return IMPEG2D_MB_DATA_DECODE_ERR;
648                 }
649 
650                 DecodedValue = gau2_impeg2d_tab_one_1_9[u4_bits >> 8];
651                 u4_sym_len = (DecodedValue & 0xf);
652                 i4_level = DecodedValue >> 9;
653                 /* One table lookup */
654                 if(0 != i4_level)
655                 {
656                     u4_run = ((DecodedValue >> 4) & 0x1f);
657                     u4_numCoeffs       += u4_run;
658                     if (u4_numCoeffs >= NUM_COEFFS)
659                     {
660                         return IMPEG2D_MB_TEX_DECODE_ERR;
661                     }
662                     u4_pos             = pu1_scan[u4_numCoeffs++];
663                     pu1_pos[*pi4_num_coeffs]    = u4_pos;
664 
665                     FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
666                     pi2_outAddr[*pi4_num_coeffs]    = i4_level;
667 
668                     (*pi4_num_coeffs)++;
669                 }
670                 else
671                 {
672                     if (DecodedValue == END_OF_BLOCK_ONE)
673                     {
674                         u4_sym_len = 4;
675 
676                         break;
677                     }
678                     else
679                     {
680                         /*Second table lookup*/
681                         lead_zeros = CLZ(u4_bits) - 20;/* -16 since we are dealing with WORD32 */
682                         if (0 != lead_zeros)
683                         {
684 
685                             u4_bits         = (u4_bits >> (6 - lead_zeros)) & 0x001F;
686 
687                             /* Flush the number of bits */
688                             if (1 == lead_zeros)
689                             {
690                                 u4_sym_len         = ((u4_bits & 0x18) >> 3) == 2 ? 11:10;
691                             }
692                             else
693                             {
694                                 u4_sym_len         = 11 + lead_zeros;
695                             }
696                             /* flushing */
697                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
698 
699                             /* Calculate the address */
700                             u4_bits         = ((lead_zeros - 1) << 5) + u4_bits;
701 
702                             DecodedValue    = gau2_impeg2d_tab_one_10_16[u4_bits];
703 
704                             u4_run = BITS(DecodedValue, 8,4);
705                             i4_level = ((WORD16) DecodedValue) >> 9;
706 
707                             u4_numCoeffs       += u4_run;
708                             if (u4_numCoeffs >= NUM_COEFFS)
709                             {
710                                 return IMPEG2D_MB_TEX_DECODE_ERR;
711                             }
712                             u4_pos             = pu1_scan[u4_numCoeffs++];
713                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
714                             pi2_outAddr[*pi4_num_coeffs]    = i4_level;
715                             (*pi4_num_coeffs)++;
716                         }
717                         /*********************************************************************/
718                         /* MPEG2 Escape Code                                                 */
719                         /*********************************************************************/
720                         else if(u2_mpeg2 == 1)
721                         {
722                             u4_sym_len         = 6;
723                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
724                                 IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,18)
725                                 u4_decoded_value    = u4_bits;
726                             u4_run             = (u4_decoded_value >> 12);
727                             i4_level           = (u4_decoded_value & 0x0FFF);
728 
729                             if (i4_level)
730                                 i4_level = (i4_level - ((i4_level & 0x0800) << 1));
731 
732                             u4_numCoeffs       += u4_run;
733                             if (u4_numCoeffs >= NUM_COEFFS)
734                             {
735                                 return IMPEG2D_MB_TEX_DECODE_ERR;
736                             }
737                             u4_pos             = pu1_scan[u4_numCoeffs++];
738                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
739                             pi2_outAddr[*pi4_num_coeffs]    = i4_level;
740                             (*pi4_num_coeffs)++;
741                         }
742                         /*********************************************************************/
743                         /* MPEG1 Escape Code                                                 */
744                         /*********************************************************************/
745                         else
746                         {
747                             /*-----------------------------------------------------------
748                             * MPEG-1 Stream
749                             *
750                             * <See D.9.3 of MPEG-2> Run-level escape syntax
751                             * Run-level values that cannot be coded with a VLC are coded
752                             * by the escape code '0000 01' followed by
753                             * either a 14-bit FLC (127 <= level <= 127),
754                             * or a 22-bit FLC (255 <= level <= 255).
755                             * This is described in Annex B,B.5f of MPEG-1.standard
756                             *-----------------------------------------------------------*/
757 
758                             /*-----------------------------------------------------------
759                             * First 6 bits are the value of the Run. Next is First 8 bits
760                             * of Level. These bits decide whether it is 14 bit FLC or
761                             * 22-bit FLC.
762                             *
763                             * If( first 8 bits of Level == '1000000' or '00000000')
764                             *      then its is 22-bit FLC.
765                             * else
766                             *      it is 14-bit FLC.
767                             *-----------------------------------------------------------*/
768                             u4_sym_len         = 6;
769                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
770                                 IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,14)
771                                 u4_decoded_value     = u4_bits;
772                             u4_run              = (u4_decoded_value >> 8);
773                             i4_level_first_byte = (u4_decoded_value & 0x0FF);
774                             if(i4_level_first_byte & 0x7F)
775                             {
776                                 /*-------------------------------------------------------
777                                 * First 8 bits of level are neither 1000000 nor 00000000
778                                 * Hence 14-bit FLC (Last 8 bits are used to get level)
779                                 *
780                                 *  Level = (msb of Level_First_Byte is 1)?
781                                 *          Level_First_Byte - 256 : Level_First_Byte
782                                 *-------------------------------------------------------*/
783                                 i4_level = (i4_level_first_byte -
784                                     ((i4_level_first_byte & 0x80) << 1));
785                             }
786                             else
787                             {
788                                 /*-------------------------------------------------------
789                                 * Next 8 bits are either 1000000 or 00000000
790                                 * Hence 22-bit FLC (Last 16 bits are used to get level)
791                                 *
792                                 *  Level = (msb of Level_First_Byte is 1)?
793                                 *          Level_Second_Byte - 256 : Level_Second_Byte
794                                 *-------------------------------------------------------*/
795                                 IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,8)
796                                     i4_level = u4_bits;
797                                 i4_level = (i4_level - (i4_level_first_byte << 1));
798                             }
799                             u4_numCoeffs += u4_run;
800                             if (u4_numCoeffs >= NUM_COEFFS)
801                             {
802                                 return IMPEG2D_MB_TEX_DECODE_ERR;
803                             }
804 
805                             u4_pos = pu1_scan[u4_numCoeffs++];
806 
807                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
808                             pi2_outAddr[*pi4_num_coeffs]    = i4_level;
809                             (*pi4_num_coeffs)++;
810                         }
811                     }
812                 }
813 
814                 u4_nz_cols |= 1 << (u4_pos & 0x7);
815                 u4_nz_rows |= 1 << (u4_pos >> 0x3);
816 
817             }
818             IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,u4_sym_len)
819         }
820         else
821         {
822             // Inline
823             while(1)
824             {
825 
826                 UWORD32 lead_zeros;
827                 UWORD16 DecodedValue;
828 
829                 u4_sym_len = 17;
830                 IBITS_NXT(u4_buf, u4_buf_nxt, u4_offset, u4_bits, u4_sym_len)
831 
832                 /* There cannot be more than 11 leading zeros in the decoded
833                  * symbol. The symbol is only 17 bits long, so we subtract 15.
834                  */
835                 lead_zeros = CLZ(u4_bits) - 15;
836                 if (lead_zeros > 11)
837                 {
838                     return IMPEG2D_MB_DATA_DECODE_ERR;
839                 }
840 
841                 DecodedValue = gau2_impeg2d_tab_zero_1_9[u4_bits >> 8];
842                 u4_sym_len = BITS(DecodedValue, 3, 0);
843                 i4_level = ((WORD16) DecodedValue) >> 9;
844 
845                 if (0 != i4_level)
846                 {
847                     u4_run = BITS(DecodedValue, 8,4);
848 
849                     u4_numCoeffs       += u4_run;
850                     if (u4_numCoeffs >= NUM_COEFFS)
851                     {
852                         return IMPEG2D_MB_TEX_DECODE_ERR;
853                     }
854 
855                     u4_pos                 = pu1_scan[u4_numCoeffs++];
856                     pu1_pos[*pi4_num_coeffs]    = u4_pos;
857 
858                     FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
859                     pi2_outAddr[*pi4_num_coeffs]    = i4_level;
860                     (*pi4_num_coeffs)++;
861                 }
862                 else
863                 {
864                     if(DecodedValue == END_OF_BLOCK_ZERO)
865                     {
866                         u4_sym_len = 2;
867 
868                         break;
869                     }
870                     else
871                     {
872                         lead_zeros = CLZ(u4_bits) - 20;/* -15 since we are dealing with WORD32 */
873                         /*Second table lookup*/
874                         if (0 != lead_zeros)
875                         {
876                             u4_bits         = (u4_bits >> (6 - lead_zeros)) & 0x001F;
877 
878                             /* Flush the number of bits */
879                             u4_sym_len         = 11 + lead_zeros;
880 
881                             /* Calculate the address */
882                             u4_bits         = ((lead_zeros - 1) << 5) + u4_bits;
883 
884                             DecodedValue    = gau2_impeg2d_tab_zero_10_16[u4_bits];
885 
886                             u4_run = BITS(DecodedValue, 8,4);
887                             i4_level = ((WORD16) DecodedValue) >> 9;
888 
889                             u4_numCoeffs       += u4_run;
890                             if (u4_numCoeffs >= NUM_COEFFS)
891                             {
892                                 return IMPEG2D_MB_TEX_DECODE_ERR;
893                             }
894 
895                             u4_pos                 = pu1_scan[u4_numCoeffs++];
896                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
897                             if (1 == lead_zeros)
898                                 u4_sym_len--;
899                             /* flushing */
900                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
901                             pi2_outAddr[*pi4_num_coeffs]    = i4_level;
902 
903                             (*pi4_num_coeffs)++;
904                         }
905                         /*Escape Sequence*/
906                         else if(u2_mpeg2 == 1)
907                         {
908                             u4_sym_len         = 6;
909                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
910                             IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,18)
911                             u4_decoded_value    = u4_bits;
912                             u4_run             = (u4_decoded_value >> 12);
913                             i4_level           = (u4_decoded_value & 0x0FFF);
914 
915                             if (i4_level)
916                                 i4_level = (i4_level - ((i4_level & 0x0800) << 1));
917 
918                             u4_numCoeffs           += u4_run;
919                             if (u4_numCoeffs >= NUM_COEFFS)
920                             {
921                                 return IMPEG2D_MB_TEX_DECODE_ERR;
922                             }
923 
924                             u4_pos                 = pu1_scan[u4_numCoeffs++];
925                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
926                             pi2_outAddr[*pi4_num_coeffs]    = i4_level;
927 
928                             (*pi4_num_coeffs)++;
929                         }
930                         /*********************************************************************/
931                         /* MPEG1 Escape Code                                                 */
932                         /*********************************************************************/
933                         else
934                         {
935                             /*-----------------------------------------------------------
936                             * MPEG-1 Stream
937                             *
938                             * <See D.9.3 of MPEG-2> Run-level escape syntax
939                             * Run-level values that cannot be coded with a VLC are coded
940                             * by the escape code '0000 01' followed by
941                             * either a 14-bit FLC (127 <= level <= 127),
942                             * or a 22-bit FLC (255 <= level <= 255).
943                             * This is described in Annex B,B.5f of MPEG-1.standard
944                             *-----------------------------------------------------------*/
945 
946                             /*-----------------------------------------------------------
947                             * First 6 bits are the value of the Run. Next is First 8 bits
948                             * of Level. These bits decide whether it is 14 bit FLC or
949                             * 22-bit FLC.
950                             *
951                             * If( first 8 bits of Level == '1000000' or '00000000')
952                             *      then its is 22-bit FLC.
953                             * else
954                             *      it is 14-bit FLC.
955                             *-----------------------------------------------------------*/
956                             u4_sym_len             = 6;
957                             FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
958                             IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,14)
959                             u4_decoded_value        = u4_bits;
960                             u4_run                 = (u4_decoded_value >> 8);
961                             i4_level_first_byte    = (u4_decoded_value & 0x0FF);
962                             if(i4_level_first_byte & 0x7F)
963                             {
964                                 /*-------------------------------------------------------
965                                 * First 8 bits of level are neither 1000000 nor 00000000
966                                 * Hence 14-bit FLC (Last 8 bits are used to get level)
967                                 *
968                                 *  Level = (msb of Level_First_Byte is 1)?
969                                 *          Level_First_Byte - 256 : Level_First_Byte
970                                 *-------------------------------------------------------*/
971                                 i4_level = (i4_level_first_byte -
972                                     ((i4_level_first_byte & 0x80) << 1));
973                             }
974                             else
975                             {
976                                 /*-------------------------------------------------------
977                                 * Next 8 bits are either 1000000 or 00000000
978                                 * Hence 22-bit FLC (Last 16 bits are used to get level)
979                                 *
980                                 *  Level = (msb of Level_First_Byte is 1)?
981                                 *          Level_Second_Byte - 256 : Level_Second_Byte
982                                 *-------------------------------------------------------*/
983                                 IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,8)
984                                 i4_level = u4_bits;
985                                 i4_level = (i4_level - (i4_level_first_byte << 1));
986                             }
987                             u4_numCoeffs           += u4_run;
988                             if (u4_numCoeffs >= NUM_COEFFS)
989                             {
990                                 return IMPEG2D_MB_TEX_DECODE_ERR;
991                             }
992 
993                             u4_pos                 = pu1_scan[u4_numCoeffs++];
994                             pu1_pos[*pi4_num_coeffs]    = u4_pos;
995                             pi2_outAddr[*pi4_num_coeffs]    = i4_level;
996 
997                             (*pi4_num_coeffs)++;
998                         }
999                     }
1000                 }
1001 
1002                 u4_nz_cols |= 1 << (u4_pos & 0x7);
1003                 u4_nz_rows |= 1 << (u4_pos >> 0x3);
1004 
1005             }
1006 
1007             IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,u4_sym_len)
1008 
1009         }
1010 
1011         PUT_TEMP_STREAM_DATA(u4_buf, u4_buf_nxt, u4_offset, pu4_buf_aligned, ps_stream)
1012 
1013         ps_dec->u4_non_zero_cols  = u4_nz_cols;
1014         ps_dec->u4_non_zero_rows  = u4_nz_rows;
1015 
1016             return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
1017 }
1018 
1019 
1020 
1021 /*****************************************************************************/
1022 /*                                                                           */
1023 /*  Function Name : impeg2d_inv_quant_mpeg1                                   */
1024 /*                                                                           */
1025 /*  Description   : Inverse quantizes the output of VLD                      */
1026 /*                                                                           */
1027 /*  Inputs        :                                                          */
1028 /*  blk,              - Block to be inverse quantized                        */
1029 /*  weighting_matrix  - Matrix to be used in inverse quant                   */
1030 /*  intra_dc_precision- Precision reqd to scale intra DC value               */
1031 /*  quant_scale       - Quanization scale for inverse quant                  */
1032 /*  intra_flag        - Intra or Not                                         */
1033 /*                                                                           */
1034 /*  Globals       : None                                                     */
1035 /*                                                                           */
1036 /*  Processing    : Implements the inverse quantize equation                 */
1037 /*                                                                           */
1038 /*  Outputs       : Inverse quantized values in the block                    */
1039 /*                                                                           */
1040 /*  Returns       : None                                                     */
1041 /*                                                                           */
1042 /*  Issues        : None                                                     */
1043 /*                                                                           */
1044 /*  Revision History:                                                        */
1045 /*                                                                           */
1046 /*         DD MM YYYY   Author(s)       Changes                              */
1047 /*         05 09 2005   Harish M        First Version                        */
1048 /*                                                                           */
1049 /*****************************************************************************/
impeg2d_inv_quant_mpeg1(WORD16 * pi2_blk,UWORD8 * pu1_weighting_matrix,UWORD8 u1_quant_scale,WORD32 u4_intra_flag,WORD32 i4_num_coeffs,WORD16 * pi2_coeffs,UWORD8 * pu1_pos,const UWORD8 * pu1_scan,UWORD16 * pu2_def_dc_pred,UWORD16 u2_intra_dc_precision)1050 WORD32 impeg2d_inv_quant_mpeg1(WORD16 *pi2_blk,
1051                               UWORD8 *pu1_weighting_matrix,
1052                               UWORD8 u1_quant_scale,
1053                               WORD32 u4_intra_flag,
1054                               WORD32 i4_num_coeffs,
1055                               WORD16 *pi2_coeffs,
1056                               UWORD8 *pu1_pos,
1057                               const UWORD8 *pu1_scan,
1058                               UWORD16 *pu2_def_dc_pred,
1059                               UWORD16 u2_intra_dc_precision)
1060 {
1061     UWORD16 i4_pos;
1062 
1063     WORD32  i4_iter;
1064 
1065     /* Inverse Quantize the predicted DC value for intra MB*/
1066     if(u4_intra_flag == 1)
1067     {
1068         /**************************************************************************/
1069         /* Decode the DC coefficient in case of Intra block and also update       */
1070         /* DC predictor value of the corresponding color component                */
1071         /**************************************************************************/
1072         {
1073             pi2_coeffs[0]   += *pu2_def_dc_pred;
1074             *pu2_def_dc_pred      = pi2_coeffs[0];
1075             pi2_coeffs[0]   <<= (3 - u2_intra_dc_precision);
1076             pi2_coeffs[0]   = CLIP_S12(pi2_coeffs[0]);
1077         }
1078 
1079         pi2_blk[pu1_scan[0]]  = pi2_coeffs[0];
1080     }
1081     /************************************************************************/
1082     /* Inverse quantization of other DCT coefficients                       */
1083     /************************************************************************/
1084     for(i4_iter = u4_intra_flag; i4_iter < i4_num_coeffs; i4_iter++)
1085     {
1086 
1087         WORD16 sign;
1088         WORD32 temp, temp1;
1089 
1090         /* Position is the inverse scan of the index stored */
1091         i4_pos      = pu1_pos[i4_iter];
1092         pi2_blk[i4_pos] = pi2_coeffs[i4_iter];
1093 
1094         sign = SIGN(pi2_blk[i4_pos]);
1095         temp = ABS(pi2_blk[i4_pos] << 1);
1096 
1097         /* pi2_coeffs has only non-zero elements. So no need to check
1098          * if the coeff is non-zero.
1099          */
1100         temp = temp + (1 * !u4_intra_flag);
1101 
1102         temp = temp * pu1_weighting_matrix[i4_pos] * u1_quant_scale;
1103 
1104         temp = temp >> 5;
1105 
1106         temp1 = temp | 1;
1107 
1108         temp1 = (temp1 > temp) ? (temp1 - temp) : (temp - temp1);
1109 
1110         temp = temp - temp1;
1111 
1112         if(temp < 0)
1113         {
1114             temp = 0;
1115         }
1116 
1117         temp = temp * sign;
1118 
1119         temp = CLIP_S12(temp);
1120 
1121         pi2_blk[i4_pos] = temp;
1122     }
1123 
1124     /*return value is used in the case of mpeg2 for mismatch control*/
1125     return  (0);
1126 } /* End of inv_quant() */
1127 
1128 
1129 
1130 /*****************************************************************************/
1131 /*                                                                           */
1132 /*  Function Name : impeg2d_inv_quant_mpeg2                                   */
1133 /*                                                                           */
1134 /*  Description   : Inverse quantizes the output of VLD                      */
1135 /*                                                                           */
1136 /*  Inputs        :                                                          */
1137 /*  blk,              - Block to be inverse quantized                        */
1138 /*  weighting_matrix  - Matrix to be used in inverse quant                   */
1139 /*  intra_dc_precision- Precision reqd to scale intra DC value               */
1140 /*  quant_scale       - Quanization scale for inverse quant                  */
1141 /*  intra_flag        - Intra or Not                                         */
1142 /*                                                                           */
1143 /*  Globals       : None                                                     */
1144 /*                                                                           */
1145 /*  Processing    : Implements the inverse quantize equation                 */
1146 /*                                                                           */
1147 /*  Outputs       : Inverse quantized values in the block                    */
1148 /*                                                                           */
1149 /*  Returns       : None                                                     */
1150 /*                                                                           */
1151 /*  Issues        : None                                                     */
1152 /*                                                                           */
1153 /*  Revision History:                                                        */
1154 /*                                                                           */
1155 /*         DD MM YYYY   Author(s)       Changes                              */
1156 /*         05 09 2005   Harish M        First Version                        */
1157 /*                                                                           */
1158 /*****************************************************************************/
impeg2d_inv_quant_mpeg2(WORD16 * pi2_blk,UWORD8 * pu1_weighting_matrix,UWORD8 u1_quant_scale,WORD32 u4_intra_flag,WORD32 i4_num_coeffs,WORD16 * pi2_coeffs,UWORD8 * pu1_pos,const UWORD8 * pu1_scan,UWORD16 * pu2_def_dc_pred,UWORD16 u2_intra_dc_precision)1159 WORD32 impeg2d_inv_quant_mpeg2(WORD16 *pi2_blk,
1160                               UWORD8 *pu1_weighting_matrix,
1161                               UWORD8 u1_quant_scale,
1162                               WORD32 u4_intra_flag,
1163                               WORD32 i4_num_coeffs,
1164                               WORD16 *pi2_coeffs,
1165                               UWORD8 *pu1_pos,
1166                               const UWORD8 *pu1_scan,
1167                               UWORD16 *pu2_def_dc_pred,
1168                               UWORD16 u2_intra_dc_precision)
1169 {
1170 
1171     WORD32  i4_pos;
1172     /* Used for Mismatch control */
1173     WORD32 sum;
1174 
1175     WORD32  i4_iter;
1176 
1177     sum = 0;
1178 
1179     /* Inverse Quantize the predicted DC value for intra MB*/
1180     if(u4_intra_flag == 1)
1181     {
1182         /**************************************************************************/
1183         /* Decode the DC coefficient in case of Intra block and also update       */
1184         /* DC predictor value of the corresponding color component                */
1185         /**************************************************************************/
1186         {
1187             pi2_coeffs[0]   += *pu2_def_dc_pred;
1188             *pu2_def_dc_pred      = pi2_coeffs[0];
1189             pi2_coeffs[0]   <<= (3 - u2_intra_dc_precision);
1190             pi2_coeffs[0]   = CLIP_S12(pi2_coeffs[0]);
1191         }
1192 
1193         pi2_blk[pu1_scan[0]]  = pi2_coeffs[0];
1194         sum = pi2_blk[0];
1195     }
1196 
1197     /************************************************************************/
1198     /* Inverse quantization of other DCT coefficients                       */
1199     /************************************************************************/
1200     for(i4_iter = u4_intra_flag; i4_iter < i4_num_coeffs; i4_iter++)
1201     {
1202         WORD16 sign;
1203         WORD32 temp;
1204         /* Position is the inverse scan of the index stored */
1205         i4_pos      = pu1_pos[i4_iter];
1206         pi2_blk[i4_pos] = pi2_coeffs[i4_iter];
1207 
1208         sign = SIGN(pi2_blk[i4_pos]);
1209         temp = ABS(pi2_blk[i4_pos] << 1);
1210         temp = temp + (1 * !u4_intra_flag);
1211         temp = temp * pu1_weighting_matrix[i4_pos] * u1_quant_scale;
1212 
1213         temp = temp >> 5;
1214 
1215         temp = temp * sign;
1216 
1217         temp = CLIP_S12(temp);
1218 
1219         pi2_blk[i4_pos] = temp;
1220 
1221         sum += temp;
1222     }
1223     return (sum ^ 1);
1224 } /* End of inv_quant() */
1225