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*******************************************************************************/
59WORD16 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*******************************************************************************/
96WORD16 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*******************************************************************************/
146UWORD16 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*******************************************************************************/
222UWORD16 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 ******************************************************************************/
294IMPEG2D_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  ******************************************************************************/
367IMPEG2D_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******************************************************************************/
464IMPEG2D_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
793            }
794            IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,u4_sym_len)
795            if (u4_numCoeffs > 64)
796            {
797                return IMPEG2D_MB_TEX_DECODE_ERR;
798            }
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            }
961            if (u4_numCoeffs > 64)
962            {
963                return IMPEG2D_MB_TEX_DECODE_ERR;
964            }
965
966            IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,u4_sym_len)
967
968        }
969
970        PUT_TEMP_STREAM_DATA(u4_buf, u4_buf_nxt, u4_offset, pu4_buf_aligned, ps_stream)
971
972        ps_dec->u4_non_zero_cols  = u4_nz_cols;
973        ps_dec->u4_non_zero_rows  = u4_nz_rows;
974
975            return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
976}
977
978
979
980/*****************************************************************************/
981/*                                                                           */
982/*  Function Name : impeg2d_inv_quant_mpeg1                                   */
983/*                                                                           */
984/*  Description   : Inverse quantizes the output of VLD                      */
985/*                                                                           */
986/*  Inputs        :                                                          */
987/*  blk,              - Block to be inverse quantized                        */
988/*  weighting_matrix  - Matrix to be used in inverse quant                   */
989/*  intra_dc_precision- Precision reqd to scale intra DC value               */
990/*  quant_scale       - Quanization scale for inverse quant                  */
991/*  intra_flag        - Intra or Not                                         */
992/*                                                                           */
993/*  Globals       : None                                                     */
994/*                                                                           */
995/*  Processing    : Implements the inverse quantize equation                 */
996/*                                                                           */
997/*  Outputs       : Inverse quantized values in the block                    */
998/*                                                                           */
999/*  Returns       : None                                                     */
1000/*                                                                           */
1001/*  Issues        : None                                                     */
1002/*                                                                           */
1003/*  Revision History:                                                        */
1004/*                                                                           */
1005/*         DD MM YYYY   Author(s)       Changes                              */
1006/*         05 09 2005   Harish M        First Version                        */
1007/*                                                                           */
1008/*****************************************************************************/
1009UWORD8 impeg2d_inv_quant_mpeg1(WORD16 *pi2_blk,
1010                              UWORD8 *pu1_weighting_matrix,
1011                              UWORD8 u1_quant_scale,
1012                              WORD32 u4_intra_flag,
1013                              WORD32 i4_num_coeffs,
1014                              WORD16 *pi2_coeffs,
1015                              UWORD8 *pu1_pos,
1016                              const UWORD8 *pu1_scan,
1017                              UWORD16 *pu2_def_dc_pred,
1018                              UWORD16 u2_intra_dc_precision)
1019{
1020    UWORD16 i4_pos;
1021
1022    WORD32  i4_iter;
1023
1024    /* Inverse Quantize the predicted DC value for intra MB*/
1025    if(u4_intra_flag == 1)
1026    {
1027        /**************************************************************************/
1028        /* Decode the DC coefficient in case of Intra block and also update       */
1029        /* DC predictor value of the corresponding color component                */
1030        /**************************************************************************/
1031        {
1032            pi2_coeffs[0]   += *pu2_def_dc_pred;
1033            *pu2_def_dc_pred      = pi2_coeffs[0];
1034            pi2_coeffs[0]   <<= (3 - u2_intra_dc_precision);
1035            pi2_coeffs[0]   = CLIP_S12(pi2_coeffs[0]);
1036        }
1037
1038        pi2_blk[pu1_scan[0]]  = pi2_coeffs[0];
1039    }
1040    /************************************************************************/
1041    /* Inverse quantization of other DCT coefficients                       */
1042    /************************************************************************/
1043    for(i4_iter = u4_intra_flag; i4_iter < i4_num_coeffs; i4_iter++)
1044    {
1045
1046        WORD16 sign;
1047        WORD32 temp, temp1;
1048
1049        /* Position is the inverse scan of the index stored */
1050        i4_pos      = pu1_pos[i4_iter];
1051        pi2_blk[i4_pos] = pi2_coeffs[i4_iter];
1052
1053        sign = SIGN(pi2_blk[i4_pos]);
1054        temp = ABS(pi2_blk[i4_pos] << 1);
1055
1056        /* pi2_coeffs has only non-zero elements. So no need to check
1057         * if the coeff is non-zero.
1058         */
1059        temp = temp + (1 * !u4_intra_flag);
1060
1061        temp = temp * pu1_weighting_matrix[i4_pos] * u1_quant_scale;
1062
1063        temp = temp >> 5;
1064
1065        temp1 = temp | 1;
1066
1067        temp1 = (temp1 > temp) ? (temp1 - temp) : (temp - temp1);
1068
1069        temp = temp - temp1;
1070
1071        if(temp < 0)
1072        {
1073            temp = 0;
1074        }
1075
1076        temp = temp * sign;
1077
1078        temp = CLIP_S12(temp);
1079
1080        pi2_blk[i4_pos] = temp;
1081    }
1082
1083    /*return value is used in the case of mpeg2 for mismatch control*/
1084    return  (0);
1085} /* End of inv_quant() */
1086
1087
1088
1089/*****************************************************************************/
1090/*                                                                           */
1091/*  Function Name : impeg2d_inv_quant_mpeg2                                   */
1092/*                                                                           */
1093/*  Description   : Inverse quantizes the output of VLD                      */
1094/*                                                                           */
1095/*  Inputs        :                                                          */
1096/*  blk,              - Block to be inverse quantized                        */
1097/*  weighting_matrix  - Matrix to be used in inverse quant                   */
1098/*  intra_dc_precision- Precision reqd to scale intra DC value               */
1099/*  quant_scale       - Quanization scale for inverse quant                  */
1100/*  intra_flag        - Intra or Not                                         */
1101/*                                                                           */
1102/*  Globals       : None                                                     */
1103/*                                                                           */
1104/*  Processing    : Implements the inverse quantize equation                 */
1105/*                                                                           */
1106/*  Outputs       : Inverse quantized values in the block                    */
1107/*                                                                           */
1108/*  Returns       : None                                                     */
1109/*                                                                           */
1110/*  Issues        : None                                                     */
1111/*                                                                           */
1112/*  Revision History:                                                        */
1113/*                                                                           */
1114/*         DD MM YYYY   Author(s)       Changes                              */
1115/*         05 09 2005   Harish M        First Version                        */
1116/*                                                                           */
1117/*****************************************************************************/
1118UWORD8 impeg2d_inv_quant_mpeg2(WORD16 *pi2_blk,
1119                              UWORD8 *pu1_weighting_matrix,
1120                              UWORD8 u1_quant_scale,
1121                              WORD32 u4_intra_flag,
1122                              WORD32 i4_num_coeffs,
1123                              WORD16 *pi2_coeffs,
1124                              UWORD8 *pu1_pos,
1125                              const UWORD8 *pu1_scan,
1126                              UWORD16 *pu2_def_dc_pred,
1127                              UWORD16 u2_intra_dc_precision)
1128{
1129
1130    WORD32  i4_pos;
1131    /* Used for Mismatch control */
1132    UWORD32 sum;
1133
1134    WORD32  i4_iter;
1135
1136    sum = 0;
1137
1138    /* Inverse Quantize the predicted DC value for intra MB*/
1139    if(u4_intra_flag == 1)
1140    {
1141        /**************************************************************************/
1142        /* Decode the DC coefficient in case of Intra block and also update       */
1143        /* DC predictor value of the corresponding color component                */
1144        /**************************************************************************/
1145        {
1146            pi2_coeffs[0]   += *pu2_def_dc_pred;
1147            *pu2_def_dc_pred      = pi2_coeffs[0];
1148            pi2_coeffs[0]   <<= (3 - u2_intra_dc_precision);
1149            pi2_coeffs[0]   = CLIP_S12(pi2_coeffs[0]);
1150        }
1151
1152        pi2_blk[pu1_scan[0]]  = pi2_coeffs[0];
1153        sum = pi2_blk[0];
1154    }
1155
1156    /************************************************************************/
1157    /* Inverse quantization of other DCT coefficients                       */
1158    /************************************************************************/
1159    for(i4_iter = u4_intra_flag; i4_iter < i4_num_coeffs; i4_iter++)
1160    {
1161        WORD16 sign;
1162        WORD32 temp;
1163        /* Position is the inverse scan of the index stored */
1164        i4_pos      = pu1_pos[i4_iter];
1165        pi2_blk[i4_pos] = pi2_coeffs[i4_iter];
1166
1167        sign = SIGN(pi2_blk[i4_pos]);
1168        temp = ABS(pi2_blk[i4_pos] << 1);
1169        temp = temp + (1 * !u4_intra_flag);
1170        temp = temp * pu1_weighting_matrix[i4_pos] * u1_quant_scale;
1171
1172        temp = temp >> 5;
1173
1174        temp = temp * sign;
1175
1176        temp = CLIP_S12(temp);
1177
1178        pi2_blk[i4_pos] = temp;
1179
1180        sum += temp;
1181    }
1182    return (sum ^ 1);
1183} /* End of inv_quant() */
1184