Coverage Report

Created: 2024-06-24 06:30

/src/libmpeg2/decoder/impeg2d_vld.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2015 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
#include <string.h>
21
22
#include "iv_datatypedef.h"
23
#include "iv.h"
24
25
#include "impeg2_buf_mgr.h"
26
#include "impeg2_disp_mgr.h"
27
#include "impeg2_defs.h"
28
#include "impeg2_platform_macros.h"
29
#include "impeg2_inter_pred.h"
30
#include "impeg2_idct.h"
31
#include "impeg2_globals.h"
32
#include "impeg2_mem_func.h"
33
#include "impeg2_format_conv.h"
34
#include "impeg2_macros.h"
35
36
#include "ivd.h"
37
#include "impeg2d.h"
38
#include "impeg2d_bitstream.h"
39
#include "impeg2d_structs.h"
40
#include "impeg2d_vld_tables.h"
41
#include "impeg2d_vld.h"
42
#include "impeg2d_pic_proc.h"
43
#include "impeg2d_debug.h"
44
45
46
/*******************************************************************************
47
* Function name : impeg2d_dec_vld_symbol
48
*
49
* Description   : Performs decoding of VLD symbol. It performs decoding by
50
*                 processing 1 bit at a time
51
*
52
* Arguments     :
53
* stream        : Bitstream
54
* ai2_code_table     : Table used for decoding
55
* maxLen        : Maximum Length of the decoded symbol in bits
56
*
57
* Value Returned: Decoded symbol
58
*******************************************************************************/
59
WORD16 impeg2d_dec_vld_symbol(stream_t *ps_stream,const WORD16 ai2_code_table[][2],  UWORD16 u2_max_len)
60
501M
{
61
501M
  UWORD16 u2_data;
62
501M
  WORD16  u2_end = 0;
63
501M
  UWORD16 u2_org_max_len = u2_max_len;
64
501M
  UWORD16 u2_i_bit;
65
66
  /* Get the maximum number of bits needed to decode a symbol */
67
501M
  u2_data = impeg2d_bit_stream_nxt(ps_stream,u2_max_len);
68
501M
  do
69
1.28G
  {
70
1.28G
    u2_max_len--;
71
    /* Read one bit at a time from the variable to decode the huffman code */
72
1.28G
    u2_i_bit = (UWORD8)((u2_data >> u2_max_len) & 0x1);
73
74
    /* Get the next node pointer or the symbol from the tree */
75
1.28G
    u2_end = ai2_code_table[u2_end][u2_i_bit];
76
1.28G
  }while(u2_end > 0);
77
78
  /* Flush the appropriate number of bits from the ps_stream */
79
501M
  impeg2d_bit_stream_flush(ps_stream,(UWORD8)(u2_org_max_len - u2_max_len));
80
501M
  return(u2_end);
81
501M
}
82
/*******************************************************************************
83
* Function name : impeg2d_fast_dec_vld_symbol
84
*
85
* Description   : Performs decoding of VLD symbol. It performs decoding by
86
*                 processing n bits at a time
87
*
88
* Arguments     :
89
* stream        : Bitstream
90
* ai2_code_table     : Code table containing huffman value
91
* indexTable    : Index table containing index
92
* maxLen        : Maximum Length of the decoded symbol in bits
93
*
94
* Value Returned: Decoded symbol
95
*******************************************************************************/
96
WORD16 impeg2d_fast_dec_vld_symbol(stream_t *ps_stream,
97
                     const WORD16  ai2_code_table[][2],
98
                     const UWORD16 au2_indexTable[][2],
99
                     UWORD16 u2_max_len)
100
0
{
101
0
    UWORD16 u2_cur_code;
102
0
    UWORD16 u2_num_bits;
103
0
    UWORD16 u2_vld_offset;
104
0
    UWORD16 u2_start_len;
105
0
    WORD16  u2_value;
106
0
    UWORD16 u2_len;
107
0
    UWORD16 u2_huffCode;
108
109
0
    u2_start_len  = au2_indexTable[0][0];
110
0
    u2_vld_offset = 0;
111
0
    u2_huffCode  = impeg2d_bit_stream_nxt(ps_stream,u2_max_len);
112
0
    do
113
0
    {
114
0
        u2_cur_code = u2_huffCode >> (u2_max_len - u2_start_len);
115
0
        u2_num_bits = ai2_code_table[u2_cur_code + u2_vld_offset][0];
116
0
        if(u2_num_bits == 0)
117
0
        {
118
0
            u2_huffCode  &= ((1 << (u2_max_len - u2_start_len)) - 1);
119
0
            u2_max_len    -= u2_start_len;
120
0
            u2_start_len   = au2_indexTable[ai2_code_table[u2_cur_code + u2_vld_offset][1]][0];
121
0
            u2_vld_offset  = au2_indexTable[ai2_code_table[u2_cur_code + u2_vld_offset][1]][1];
122
0
        }
123
0
        else
124
0
        {
125
0
            u2_value = ai2_code_table[u2_cur_code + u2_vld_offset][1];
126
0
            u2_len   = u2_num_bits;
127
0
        }
128
0
    }while(u2_num_bits == 0);
129
0
    impeg2d_bit_stream_flush(ps_stream,u2_len);
130
0
    return(u2_value);
131
0
}
132
/******************************************************************************
133
*
134
*  Function Name   : impeg2d_dec_ac_coeff_zero
135
*
136
*  Description     : Decodes using Table B.14
137
*
138
*  Arguments       : Pointer to VideoObjectLayerStructure
139
*
140
*  Values Returned : Decoded value
141
*
142
*  Revision History:
143
*
144
*         28 02 2002  AR        Creation
145
*******************************************************************************/
146
UWORD16 impeg2d_dec_ac_coeff_zero(stream_t *ps_stream, UWORD16* pu2_sym_len, UWORD16* pu2_sym_val)
147
0
{
148
0
    UWORD16 u2_offset,u2_decoded_value;
149
0
    UWORD8  u1_shift;
150
0
    UWORD32 u4_bits_read;
151
152
0
    u4_bits_read = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,MPEG2_AC_COEFF_MAX_LEN);
153
154
0
    if ((UWORD16)u4_bits_read >= 0x0800)
155
0
    {
156
0
        u2_offset = (UWORD16)u4_bits_read >> 11;
157
0
    }
158
0
    else if ((UWORD16)u4_bits_read >= 0x40)
159
0
    {
160
0
        u2_offset = 31 + ((UWORD16)u4_bits_read >> 6);
161
0
    }
162
0
    else if ((UWORD16)u4_bits_read >= 0x20)
163
0
    {
164
0
        u2_offset = 64;
165
0
    }
166
0
    else
167
0
    {
168
0
        u2_offset      = 63;
169
0
        u4_bits_read    = (UWORD16)u4_bits_read - 0x10;
170
0
    }
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
0
    u2_offset = gau2_impeg2d_offset_zero[u2_offset];
177
0
    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
0
     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
0
    u2_decoded_value = gau2_impeg2d_dct_coeff_zero[u2_offset];
190
0
    if(u2_decoded_value == END_OF_BLOCK)
191
0
    {
192
0
        *pu2_sym_len = 2;
193
0
        *pu2_sym_val = EOB_CODE_VALUE;
194
0
    }
195
0
    else if(u2_decoded_value == ESCAPE_CODE)
196
0
    {
197
0
        *pu2_sym_len     = u2_decoded_value & 0x1F;
198
0
        *pu2_sym_val = ESC_CODE_VALUE;
199
0
    }
200
0
    else
201
0
    {
202
0
        *pu2_sym_len = u2_decoded_value & 0x1F;
203
0
        *pu2_sym_val = u2_decoded_value >> 5;
204
0
    }
205
0
    return(u2_decoded_value);
206
0
}
207
208
/******************************************************************************
209
*
210
*  Function Name   : impeg2d_dec_ac_coeff_one
211
*
212
*  Description     : Decodes using Table B.15
213
*
214
*  Arguments       : Pointer to VideoObjectLayerStructure
215
*
216
*  Values Returned : Decoded value
217
*
218
*  Revision History:
219
*
220
*         28 02 2002  AR        Creation
221
*******************************************************************************/
222
UWORD16 impeg2d_dec_ac_coeff_one(stream_t *ps_stream, UWORD16* pu2_sym_len, UWORD16* pu2_sym_val)
223
0
{
224
0
    UWORD16 u2_offset, u2_decoded_value;
225
0
    UWORD8  u1_shift;
226
0
    UWORD32 u4_bits_read;
227
228
229
0
    u4_bits_read = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,MPEG2_AC_COEFF_MAX_LEN);
230
231
0
    if ((UWORD16)u4_bits_read >= 0x8000)
232
0
    {
233
        /* If the MSB of the vld code is 1 */
234
0
        if (((UWORD16)u4_bits_read >> 12) == 0xF)
235
0
            u2_offset = ((UWORD16)u4_bits_read >> 8) & 0xF;
236
0
        else
237
0
            u2_offset = (UWORD16)u4_bits_read >> 11;
238
0
        u2_offset += gau2_impeg2d_offset_one[0];
239
0
    }
240
0
    else if ((UWORD16)u4_bits_read >= 0x400)
241
0
    {
242
0
        u2_offset =(UWORD16) u4_bits_read >> 10;
243
0
        u2_offset = gau2_impeg2d_offset_one[u2_offset];
244
0
        u1_shift = u2_offset & 0xF;
245
0
        u2_offset = (u2_offset >> 4) + ((UWORD16)u4_bits_read >> u1_shift);
246
0
    }
247
0
    else if ((UWORD16)u4_bits_read >= 0x20)
248
0
    {
249
0
        u2_offset = ((UWORD16)u4_bits_read >> 5) + 31;
250
0
        u2_offset = gau2_impeg2d_offset_one[u2_offset];
251
0
        u1_shift = u2_offset & 0xF;
252
0
        u2_offset = (u2_offset >> 4) + ((UWORD16)u4_bits_read >> u1_shift);
253
0
    }
254
0
    else
255
0
    {
256
0
        u2_offset = gau2_impeg2d_offset_one[63] + ((UWORD16)u4_bits_read & 0xF);
257
0
    }
258
    /*-----------------------------------------------------------------------
259
    * DecodedValue has the Run, Level and the number of bits used by Vld code
260
    *-----------------------------------------------------------------------*/
261
0
    u2_decoded_value = gau2_impeg2d_dct_coeff_one[u2_offset];
262
263
0
    if(u2_decoded_value == END_OF_BLOCK)
264
0
    {
265
0
        *pu2_sym_len = 4;
266
0
        *pu2_sym_val = EOB_CODE_VALUE;
267
0
    }
268
0
    else if(u2_decoded_value == ESCAPE_CODE)
269
0
    {
270
0
        *pu2_sym_len     = u2_decoded_value & 0x1F;
271
0
        *pu2_sym_val = ESC_CODE_VALUE;
272
0
    }
273
0
    else
274
0
    {
275
0
        *pu2_sym_len = u2_decoded_value & 0x1F;
276
0
        *pu2_sym_val = u2_decoded_value >> 5;
277
0
    }
278
279
0
    return(u2_decoded_value);
280
0
}
281
282
/******************************************************************************
283
 *
284
 *  Function Name   : impeg2d_vld_inv_quant_mpeg1
285
 *
286
 *  Description     : Performs VLD operation for MPEG1/2
287
 *
288
 *  Arguments       :
289
 *  state           : VLCD state parameter
290
 *  regs            : Registers of VLCD
291
 *
292
 *  Values Returned : None
293
 ******************************************************************************/
294
IMPEG2D_ERROR_CODES_T impeg2d_vld_inv_quant_mpeg1(
295
                             void  *pv_dec,           /* Decoder State */
296
                             WORD16       *pi2_out_addr,       /*!< Address where decoded symbols will be stored */
297
                             const UWORD8 *pu1_scan,          /*!< Scan table to be used */
298
                             UWORD16      u2_intra_flag,      /*!< Intra Macroblock or not */
299
                             UWORD16      u2_colr_comp,      /*!< 0 - Luma,1 - U comp, 2 - V comp */
300
                             UWORD16      u2_d_picture        /*!< D Picture or not */
301
                             )
302
1.26M
{
303
1.26M
    UWORD8  *pu1_weighting_matrix;
304
1.26M
    dec_state_t *ps_dec    = (dec_state_t *) pv_dec;
305
1.26M
    IMPEG2D_ERROR_CODES_T e_error   = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
306
307
1.26M
    WORD16  pi2_coeffs[NUM_COEFFS];
308
1.26M
    UWORD8  pu1_pos[NUM_COEFFS];
309
1.26M
    WORD32  i4_num_coeffs;
310
311
    /* Perform VLD on the stream to get the coefficients and their positions */
312
1.26M
    e_error = impeg2d_vld_decode(ps_dec, pi2_coeffs, pu1_scan, pu1_pos, u2_intra_flag,
313
1.26M
                                 u2_colr_comp, u2_d_picture, ps_dec->u2_intra_vlc_format,
314
1.26M
                                 ps_dec->u2_is_mpeg2, &i4_num_coeffs);
315
1.26M
    if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
316
4.72k
    {
317
4.72k
        return e_error;
318
4.72k
    }
319
320
    /* For YUV420 format,Select the weighting matrix according to Table 7.5 */
321
1.25M
    pu1_weighting_matrix = (u2_intra_flag == 1) ? ps_dec->au1_intra_quant_matrix:
322
1.25M
                    ps_dec->au1_inter_quant_matrix;
323
324
1.25M
    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
1.25M
    {
329
        /* Clear output matrix */
330
1.25M
        PROFILE_DISABLE_MEMSET_RESBUF_IF0
331
1.25M
        if (1 != (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
332
726k
        {
333
726k
            ps_dec->pf_memset_16bit_8x8_linear_block (pi2_out_addr);
334
726k
        }
335
336
1.25M
        impeg2d_inv_quant_mpeg1(pi2_out_addr, pu1_weighting_matrix,
337
1.25M
                                  ps_dec->u1_quant_scale, u2_intra_flag,
338
1.25M
                                  i4_num_coeffs, pi2_coeffs, pu1_pos,
339
1.25M
                                  pu1_scan, &ps_dec->u2_def_dc_pred[u2_colr_comp],
340
1.25M
                                  ps_dec->u2_intra_dc_precision);
341
342
1.25M
        if (0 != pi2_out_addr[0])
343
1.19M
        {
344
            /* The first coeff might've become non-zero due to intra_dc_decision
345
             * value. So, check here after inverse quantization.
346
             */
347
1.19M
            ps_dec->u4_non_zero_cols  |= 0x1;
348
1.19M
            ps_dec->u4_non_zero_rows  |= 0x1;
349
1.19M
        }
350
1.25M
    }
351
352
1.25M
    return e_error;
353
1.26M
}
354
355
/******************************************************************************
356
  *
357
  *  Function Name   : impeg2d_vld_inv_quant_mpeg2
358
  *
359
  *  Description     : Performs VLD operation for MPEG1/2
360
  *
361
  *  Arguments       :
362
  *  state           : VLCD state parameter
363
  *  regs            : Registers of VLCD
364
  *
365
  *  Values Returned : None
366
  ******************************************************************************/
367
IMPEG2D_ERROR_CODES_T impeg2d_vld_inv_quant_mpeg2(
368
                             void  *pv_dec,           /* Decoder State */
369
                             WORD16       *pi2_out_addr,       /*!< Address where decoded symbols will be stored */
370
                             const UWORD8 *pu1_scan,          /*!< Scan table to be used */
371
                             UWORD16      u2_intra_flag,      /*!< Intra Macroblock or not */
372
                             UWORD16      u2_colr_comp,      /*!< 0 - Luma,1 - U comp, 2 - V comp */
373
                             UWORD16      u2_d_picture        /*!< D Picture or not */
374
                             )
375
314k
{
376
314k
    UWORD8  *pu1_weighting_matrix;
377
314k
    WORD32 i4_sum;
378
314k
    dec_state_t *ps_dec = (dec_state_t *)pv_dec;
379
314k
    IMPEG2D_ERROR_CODES_T e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
380
381
314k
    WORD16  pi2_coeffs[NUM_COEFFS];
382
314k
    UWORD8  pi4_pos[NUM_COEFFS];
383
314k
    WORD32  i4_num_coeffs;
384
385
    /* Perform VLD on the stream to get the coefficients and their positions */
386
314k
    e_error = impeg2d_vld_decode(ps_dec, pi2_coeffs, pu1_scan, pi4_pos, u2_intra_flag,
387
314k
                                 u2_colr_comp, u2_d_picture, ps_dec->u2_intra_vlc_format,
388
314k
                                 ps_dec->u2_is_mpeg2, &i4_num_coeffs);
389
314k
    if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
390
3.69k
    {
391
3.69k
        return e_error;
392
3.69k
    }
393
394
    /* For YUV420 format,Select the weighting matrix according to Table 7.5 */
395
310k
    pu1_weighting_matrix = (u2_intra_flag == 1) ? ps_dec->au1_intra_quant_matrix:
396
310k
                    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
310k
    ps_dec->i4_last_value_one = 0;
401
402
310k
    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
310k
    {
408
        /* Clear output matrix */
409
310k
        PROFILE_DISABLE_MEMSET_RESBUF_IF0
410
310k
        if (1 != (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
411
274k
        {
412
274k
            ps_dec->pf_memset_16bit_8x8_linear_block (pi2_out_addr);
413
274k
        }
414
415
310k
        i4_sum  = impeg2d_inv_quant_mpeg2(pi2_out_addr, pu1_weighting_matrix,
416
310k
                                                 ps_dec->u1_quant_scale, u2_intra_flag,
417
310k
                                                 i4_num_coeffs, pi2_coeffs,
418
310k
                                                 pi4_pos, pu1_scan,
419
310k
                                                 &ps_dec->u2_def_dc_pred[u2_colr_comp],
420
310k
                                                 ps_dec->u2_intra_dc_precision);
421
422
310k
        if (0 != pi2_out_addr[0])
423
252k
        {
424
            /* The first coeff might've become non-zero due to intra_dc_decision
425
             * value. So, check here after inverse quantization.
426
             */
427
252k
            ps_dec->u4_non_zero_cols  |= 0x1;
428
252k
            ps_dec->u4_non_zero_rows  |= 0x1;
429
252k
        }
430
431
310k
        if (1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
432
38.7k
        {
433
38.7k
            ps_dec->i4_last_value_one = 1 - (pi2_out_addr[0] & 1);
434
38.7k
        }
435
271k
        else
436
271k
        {
437
            /*toggle last bit if sum is even ,else retain it as it is*/
438
271k
            pi2_out_addr[63]        ^= (i4_sum & 1);
439
440
271k
            if (0 != pi2_out_addr[63])
441
176k
            {
442
176k
                ps_dec->u4_non_zero_cols  |= 0x80;
443
176k
                ps_dec->u4_non_zero_rows  |= 0x80;
444
176k
            }
445
271k
        }
446
310k
    }
447
448
310k
    return e_error;
449
314k
}
450
451
452
/******************************************************************************
453
*
454
*  Function Name   : impeg2d_vld_decode
455
*
456
*  Description     : Performs VLD operation for MPEG1/2
457
*
458
*  Arguments       :
459
*  state           : VLCD state parameter
460
*  regs            : Registers of VLCD
461
*
462
*  Values Returned : None
463
******************************************************************************/
464
IMPEG2D_ERROR_CODES_T impeg2d_vld_decode(
465
    dec_state_t *ps_dec,
466
    WORD16      *pi2_outAddr,       /*!< Address where decoded symbols will be stored */
467
    const UWORD8 *pu1_scan,         /*!< Scan table to be used */
468
    UWORD8      *pu1_pos,       /*!< Scan table to be used */
469
    UWORD16     u2_intra_flag,      /*!< Intra Macroblock or not */
470
    UWORD16     u2_chroma_flag,     /*!< Chroma Block or not */
471
    UWORD16     u2_d_picture,       /*!< D Picture or not */
472
    UWORD16     u2_intra_vlc_format, /*!< Intra VLC format */
473
    UWORD16     u2_mpeg2,          /*!< MPEG-2 or not */
474
    WORD32      *pi4_num_coeffs /*!< Returns the number of coeffs in block */
475
    )
476
1.57M
{
477
478
1.57M
    UWORD32 u4_sym_len;
479
480
1.57M
    UWORD32 u4_decoded_value;
481
1.57M
    WORD32 i4_level_first_byte;
482
1.57M
    WORD32  i4_level;
483
1.57M
    UWORD32 u4_run, u4_numCoeffs;
484
1.57M
    UWORD32 u4_buf;
485
1.57M
    UWORD32 u4_buf_nxt;
486
1.57M
    UWORD32 u4_offset;
487
1.57M
    UWORD32 *pu4_buf_aligned;
488
1.57M
    UWORD32 u4_bits;
489
1.57M
    stream_t *ps_stream = &ps_dec->s_bit_stream;
490
1.57M
    WORD32  u4_pos;
491
1.57M
    UWORD32 u4_nz_cols;
492
1.57M
    UWORD32 u4_nz_rows;
493
494
1.57M
    *pi4_num_coeffs = 0;
495
496
1.57M
    ps_dec->u4_non_zero_cols = 0;
497
1.57M
    ps_dec->u4_non_zero_rows = 0;
498
1.57M
    u4_nz_cols = ps_dec->u4_non_zero_cols;
499
1.57M
    u4_nz_rows = ps_dec->u4_non_zero_rows;
500
501
1.57M
    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
1.57M
    if(u2_intra_flag)
506
1.22M
    {
507
1.22M
        WORD32 dc_size;
508
1.22M
        WORD32 dc_diff;
509
1.22M
        WORD32 maxLen;
510
1.22M
        WORD32 idx;
511
512
513
1.22M
        maxLen = MPEG2_DCT_DC_SIZE_LEN;
514
1.22M
        idx = 0;
515
1.22M
        if(u2_chroma_flag != 0)
516
407k
        {
517
407k
            maxLen += 1;
518
407k
            idx++;
519
407k
        }
520
521
522
1.22M
        {
523
1.22M
            WORD16  end = 0;
524
1.22M
            UWORD32 maxLen_tmp = maxLen;
525
1.22M
            UWORD16 m_iBit;
526
527
528
            /* Get the maximum number of bits needed to decode a symbol */
529
1.22M
            IBITS_NXT(u4_buf,u4_buf_nxt,u4_offset,u4_bits,maxLen)
530
1.22M
            do
531
3.39M
            {
532
3.39M
                maxLen_tmp--;
533
                /* Read one bit at a time from the variable to decode the huffman code */
534
3.39M
                m_iBit = (UWORD8)((u4_bits >> maxLen_tmp) & 0x1);
535
536
                /* Get the next node pointer or the symbol from the tree */
537
3.39M
                end = gai2_impeg2d_dct_dc_size[idx][end][m_iBit];
538
3.39M
            }while(end > 0);
539
1.22M
            dc_size = end + MPEG2_DCT_DC_SIZE_OFFSET;
540
541
            /* Flush the appropriate number of bits from the stream */
542
1.22M
            FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,(maxLen - maxLen_tmp),pu4_buf_aligned)
543
544
1.22M
        }
545
546
547
548
1.22M
        if (dc_size != 0)
549
1.18M
        {
550
1.18M
            UWORD32 u4_bits;
551
552
1.18M
            IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned, dc_size)
553
1.18M
            dc_diff = u4_bits;
554
555
1.18M
            if ((dc_diff & (1 << (dc_size - 1))) == 0) //v Probably the prediction algo?
556
140k
                dc_diff -= (1 << dc_size) - 1;
557
1.18M
        }
558
43.1k
        else
559
43.1k
        {
560
43.1k
            dc_diff = 0;
561
43.1k
        }
562
563
564
1.22M
        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
1.22M
        pu1_pos[*pi4_num_coeffs]    = pu1_scan[0];
569
1.22M
        (*pi4_num_coeffs)++;
570
571
1.22M
        if (0 != dc_diff)
572
1.18M
        {
573
1.18M
            u4_nz_cols |= 0x01;
574
1.18M
            u4_nz_rows |= 0x01;
575
1.18M
        }
576
577
1.22M
        u4_numCoeffs = 1;
578
1.22M
    }
579
    /**************************************************************************/
580
    /* Decoding of first AC coefficient in case of non Intra block            */
581
    /**************************************************************************/
582
352k
    else
583
352k
    {
584
        /* First symbol can be 1s */
585
352k
        UWORD32 u4_bits;
586
587
352k
        IBITS_NXT(u4_buf,u4_buf_nxt,u4_offset,u4_bits,1)
588
589
352k
        if(u4_bits == 1)
590
318k
        {
591
592
318k
            FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,1, pu4_buf_aligned)
593
318k
            IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned, 1)
594
318k
            if(u4_bits == 1)
595
240k
            {
596
240k
                pi2_outAddr[*pi4_num_coeffs] = -1;
597
240k
            }
598
77.4k
            else
599
77.4k
            {
600
77.4k
                pi2_outAddr[*pi4_num_coeffs] = 1;
601
77.4k
            }
602
603
            /* This indicates the position of the coefficient. Since this is the DC
604
             * coefficient, we put the position as 0.
605
             */
606
318k
            pu1_pos[*pi4_num_coeffs]    = pu1_scan[0];
607
318k
            (*pi4_num_coeffs)++;
608
318k
            u4_numCoeffs = 1;
609
610
318k
            u4_nz_cols |= 0x01;
611
318k
            u4_nz_rows |= 0x01;
612
318k
        }
613
34.5k
        else
614
34.5k
        {
615
34.5k
            u4_numCoeffs = 0;
616
34.5k
        }
617
352k
    }
618
1.57M
    if (1 == u2_d_picture)
619
0
    {
620
0
        PUT_TEMP_STREAM_DATA(u4_buf, u4_buf_nxt, u4_offset, pu4_buf_aligned, ps_stream)
621
0
        ps_dec->u4_non_zero_cols  = u4_nz_cols;
622
0
        ps_dec->u4_non_zero_rows  = u4_nz_rows;
623
0
        return ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE);
624
0
    }
625
626
627
628
1.57M
        if (1 == u2_intra_vlc_format && u2_intra_flag)
629
37.3k
        {
630
631
641k
            while(1)
632
641k
            {
633
                //Putting the impeg2d_dec_ac_coeff_one function inline.
634
635
641k
                UWORD32 lead_zeros;
636
641k
                WORD16 DecodedValue;
637
638
641k
                u4_sym_len = 17;
639
641k
                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
641k
                lead_zeros = CLZ(u4_bits) - 15;
645
641k
                if (lead_zeros > 11)
646
860
                {
647
860
                    return IMPEG2D_MB_DATA_DECODE_ERR;
648
860
                }
649
650
640k
                DecodedValue = gau2_impeg2d_tab_one_1_9[u4_bits >> 8];
651
640k
                u4_sym_len = (DecodedValue & 0xf);
652
640k
                i4_level = DecodedValue >> 9;
653
                /* One table lookup */
654
640k
                if(0 != i4_level)
655
363k
                {
656
363k
                    u4_run = ((DecodedValue >> 4) & 0x1f);
657
363k
                    u4_numCoeffs       += u4_run;
658
363k
                    if (u4_numCoeffs >= NUM_COEFFS)
659
293
                    {
660
293
                        return IMPEG2D_MB_TEX_DECODE_ERR;
661
293
                    }
662
362k
                    u4_pos             = pu1_scan[u4_numCoeffs++];
663
362k
                    pu1_pos[*pi4_num_coeffs]    = u4_pos;
664
665
362k
                    FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
666
362k
                    pi2_outAddr[*pi4_num_coeffs]    = i4_level;
667
668
362k
                    (*pi4_num_coeffs)++;
669
362k
                }
670
277k
                else
671
277k
                {
672
277k
                    if (DecodedValue == END_OF_BLOCK_ONE)
673
35.3k
                    {
674
35.3k
                        u4_sym_len = 4;
675
676
35.3k
                        break;
677
35.3k
                    }
678
242k
                    else
679
242k
                    {
680
                        /*Second table lookup*/
681
242k
                        lead_zeros = CLZ(u4_bits) - 20;/* -16 since we are dealing with WORD32 */
682
242k
                        if (0 != lead_zeros)
683
3.68k
                        {
684
685
3.68k
                            u4_bits         = (u4_bits >> (6 - lead_zeros)) & 0x001F;
686
687
                            /* Flush the number of bits */
688
3.68k
                            if (1 == lead_zeros)
689
2.09k
                            {
690
2.09k
                                u4_sym_len         = ((u4_bits & 0x18) >> 3) == 2 ? 11:10;
691
2.09k
                            }
692
1.59k
                            else
693
1.59k
                            {
694
1.59k
                                u4_sym_len         = 11 + lead_zeros;
695
1.59k
                            }
696
                            /* flushing */
697
3.68k
                            FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
698
699
                            /* Calculate the address */
700
3.68k
                            u4_bits         = ((lead_zeros - 1) << 5) + u4_bits;
701
702
3.68k
                            DecodedValue    = gau2_impeg2d_tab_one_10_16[u4_bits];
703
704
3.68k
                            u4_run = BITS(DecodedValue, 8,4);
705
3.68k
                            i4_level = ((WORD16) DecodedValue) >> 9;
706
707
3.68k
                            u4_numCoeffs       += u4_run;
708
3.68k
                            if (u4_numCoeffs >= NUM_COEFFS)
709
166
                            {
710
166
                                return IMPEG2D_MB_TEX_DECODE_ERR;
711
166
                            }
712
3.51k
                            u4_pos             = pu1_scan[u4_numCoeffs++];
713
3.51k
                            pu1_pos[*pi4_num_coeffs]    = u4_pos;
714
3.51k
                            pi2_outAddr[*pi4_num_coeffs]    = i4_level;
715
3.51k
                            (*pi4_num_coeffs)++;
716
3.51k
                        }
717
                        /*********************************************************************/
718
                        /* MPEG2 Escape Code                                                 */
719
                        /*********************************************************************/
720
238k
                        else if(u2_mpeg2 == 1)
721
242k
                        {
722
242k
                            u4_sym_len         = 6;
723
242k
                            FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
724
242k
                                IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,18)
725
242k
                                u4_decoded_value    = u4_bits;
726
242k
                            u4_run             = (u4_decoded_value >> 12);
727
242k
                            i4_level           = (u4_decoded_value & 0x0FFF);
728
729
242k
                            if (i4_level)
730
122k
                                i4_level = (i4_level - ((i4_level & 0x0800) << 1));
731
732
242k
                            u4_numCoeffs       += u4_run;
733
242k
                            if (u4_numCoeffs >= NUM_COEFFS)
734
582
                            {
735
582
                                return IMPEG2D_MB_TEX_DECODE_ERR;
736
582
                            }
737
241k
                            u4_pos             = pu1_scan[u4_numCoeffs++];
738
241k
                            pu1_pos[*pi4_num_coeffs]    = u4_pos;
739
241k
                            pi2_outAddr[*pi4_num_coeffs]    = i4_level;
740
241k
                            (*pi4_num_coeffs)++;
741
241k
                        }
742
                        /*********************************************************************/
743
                        /* MPEG1 Escape Code                                                 */
744
                        /*********************************************************************/
745
18.4E
                        else
746
18.4E
                        {
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
18.4E
                            u4_sym_len         = 6;
769
18.4E
                            FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
770
18.4E
                                IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,14)
771
18.4E
                                u4_decoded_value     = u4_bits;
772
18.4E
                            u4_run              = (u4_decoded_value >> 8);
773
18.4E
                            i4_level_first_byte = (u4_decoded_value & 0x0FF);
774
18.4E
                            if(i4_level_first_byte & 0x7F)
775
0
                            {
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
0
                                i4_level = (i4_level_first_byte -
784
0
                                    ((i4_level_first_byte & 0x80) << 1));
785
0
                            }
786
18.4E
                            else
787
18.4E
                            {
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
18.4E
                                IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,8)
796
18.4E
                                    i4_level = u4_bits;
797
18.4E
                                i4_level = (i4_level - (i4_level_first_byte << 1));
798
18.4E
                            }
799
18.4E
                            u4_numCoeffs += u4_run;
800
18.4E
                            if (u4_numCoeffs >= NUM_COEFFS)
801
0
                            {
802
0
                                return IMPEG2D_MB_TEX_DECODE_ERR;
803
0
                            }
804
805
18.4E
                            u4_pos = pu1_scan[u4_numCoeffs++];
806
807
18.4E
                            pu1_pos[*pi4_num_coeffs]    = u4_pos;
808
18.4E
                            pi2_outAddr[*pi4_num_coeffs]    = i4_level;
809
18.4E
                            (*pi4_num_coeffs)++;
810
18.4E
                        }
811
242k
                    }
812
277k
                }
813
814
604k
                u4_nz_cols |= 1 << (u4_pos & 0x7);
815
604k
                u4_nz_rows |= 1 << (u4_pos >> 0x3);
816
817
604k
            }
818
35.4k
            IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,u4_sym_len)
819
35.4k
        }
820
1.53M
        else
821
1.53M
        {
822
            // Inline
823
4.13M
            while(1)
824
4.13M
            {
825
826
4.13M
                UWORD32 lead_zeros;
827
4.13M
                UWORD16 DecodedValue;
828
829
4.13M
                u4_sym_len = 17;
830
4.13M
                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
4.13M
                lead_zeros = CLZ(u4_bits) - 15;
836
4.13M
                if (lead_zeros > 11)
837
5.06k
                {
838
5.06k
                    return IMPEG2D_MB_DATA_DECODE_ERR;
839
5.06k
                }
840
841
4.13M
                DecodedValue = gau2_impeg2d_tab_zero_1_9[u4_bits >> 8];
842
4.13M
                u4_sym_len = BITS(DecodedValue, 3, 0);
843
4.13M
                i4_level = ((WORD16) DecodedValue) >> 9;
844
845
4.13M
                if (0 != i4_level)
846
2.53M
                {
847
2.53M
                    u4_run = BITS(DecodedValue, 8,4);
848
849
2.53M
                    u4_numCoeffs       += u4_run;
850
2.53M
                    if (u4_numCoeffs >= NUM_COEFFS)
851
670
                    {
852
670
                        return IMPEG2D_MB_TEX_DECODE_ERR;
853
670
                    }
854
855
2.53M
                    u4_pos                 = pu1_scan[u4_numCoeffs++];
856
2.53M
                    pu1_pos[*pi4_num_coeffs]    = u4_pos;
857
858
2.53M
                    FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
859
2.53M
                    pi2_outAddr[*pi4_num_coeffs]    = i4_level;
860
2.53M
                    (*pi4_num_coeffs)++;
861
2.53M
                }
862
1.60M
                else
863
1.60M
                {
864
1.60M
                    if(DecodedValue == END_OF_BLOCK_ZERO)
865
1.53M
                    {
866
1.53M
                        u4_sym_len = 2;
867
868
1.53M
                        break;
869
1.53M
                    }
870
63.5k
                    else
871
63.5k
                    {
872
63.5k
                        lead_zeros = CLZ(u4_bits) - 20;/* -15 since we are dealing with WORD32 */
873
                        /*Second table lookup*/
874
63.5k
                        if (0 != lead_zeros)
875
12.6k
                        {
876
12.6k
                            u4_bits         = (u4_bits >> (6 - lead_zeros)) & 0x001F;
877
878
                            /* Flush the number of bits */
879
12.6k
                            u4_sym_len         = 11 + lead_zeros;
880
881
                            /* Calculate the address */
882
12.6k
                            u4_bits         = ((lead_zeros - 1) << 5) + u4_bits;
883
884
12.6k
                            DecodedValue    = gau2_impeg2d_tab_zero_10_16[u4_bits];
885
886
12.6k
                            u4_run = BITS(DecodedValue, 8,4);
887
12.6k
                            i4_level = ((WORD16) DecodedValue) >> 9;
888
889
12.6k
                            u4_numCoeffs       += u4_run;
890
12.6k
                            if (u4_numCoeffs >= NUM_COEFFS)
891
246
                            {
892
246
                                return IMPEG2D_MB_TEX_DECODE_ERR;
893
246
                            }
894
895
12.3k
                            u4_pos                 = pu1_scan[u4_numCoeffs++];
896
12.3k
                            pu1_pos[*pi4_num_coeffs]    = u4_pos;
897
12.3k
                            if (1 == lead_zeros)
898
2.75k
                                u4_sym_len--;
899
                            /* flushing */
900
12.3k
                            FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
901
12.3k
                            pi2_outAddr[*pi4_num_coeffs]    = i4_level;
902
903
12.3k
                            (*pi4_num_coeffs)++;
904
12.3k
                        }
905
                        /*Escape Sequence*/
906
50.9k
                        else if(u2_mpeg2 == 1)
907
4.16k
                        {
908
4.16k
                            u4_sym_len         = 6;
909
4.16k
                            FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
910
4.16k
                            IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,18)
911
4.16k
                            u4_decoded_value    = u4_bits;
912
4.16k
                            u4_run             = (u4_decoded_value >> 12);
913
4.16k
                            i4_level           = (u4_decoded_value & 0x0FFF);
914
915
4.16k
                            if (i4_level)
916
3.33k
                                i4_level = (i4_level - ((i4_level & 0x0800) << 1));
917
918
4.16k
                            u4_numCoeffs           += u4_run;
919
4.16k
                            if (u4_numCoeffs >= NUM_COEFFS)
920
260
                            {
921
260
                                return IMPEG2D_MB_TEX_DECODE_ERR;
922
260
                            }
923
924
3.90k
                            u4_pos                 = pu1_scan[u4_numCoeffs++];
925
3.90k
                            pu1_pos[*pi4_num_coeffs]    = u4_pos;
926
3.90k
                            pi2_outAddr[*pi4_num_coeffs]    = i4_level;
927
928
3.90k
                            (*pi4_num_coeffs)++;
929
3.90k
                        }
930
                        /*********************************************************************/
931
                        /* MPEG1 Escape Code                                                 */
932
                        /*********************************************************************/
933
46.8k
                        else
934
46.8k
                        {
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
46.8k
                            u4_sym_len             = 6;
957
46.8k
                            FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned)
958
46.8k
                            IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,14)
959
46.8k
                            u4_decoded_value        = u4_bits;
960
46.8k
                            u4_run                 = (u4_decoded_value >> 8);
961
46.8k
                            i4_level_first_byte    = (u4_decoded_value & 0x0FF);
962
46.8k
                            if(i4_level_first_byte & 0x7F)
963
27.7k
                            {
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
27.7k
                                i4_level = (i4_level_first_byte -
972
27.7k
                                    ((i4_level_first_byte & 0x80) << 1));
973
27.7k
                            }
974
19.0k
                            else
975
19.0k
                            {
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
19.0k
                                IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,8)
984
19.0k
                                i4_level = u4_bits;
985
19.0k
                                i4_level = (i4_level - (i4_level_first_byte << 1));
986
19.0k
                            }
987
46.8k
                            u4_numCoeffs           += u4_run;
988
46.8k
                            if (u4_numCoeffs >= NUM_COEFFS)
989
282
                            {
990
282
                                return IMPEG2D_MB_TEX_DECODE_ERR;
991
282
                            }
992
993
46.5k
                            u4_pos                 = pu1_scan[u4_numCoeffs++];
994
46.5k
                            pu1_pos[*pi4_num_coeffs]    = u4_pos;
995
46.5k
                            pi2_outAddr[*pi4_num_coeffs]    = i4_level;
996
997
46.5k
                            (*pi4_num_coeffs)++;
998
46.5k
                        }
999
63.5k
                    }
1000
1.60M
                }
1001
1002
2.59M
                u4_nz_cols |= 1 << (u4_pos & 0x7);
1003
2.59M
                u4_nz_rows |= 1 << (u4_pos >> 0x3);
1004
1005
2.59M
            }
1006
1007
1.53M
            IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,u4_sym_len)
1008
1009
1.53M
        }
1010
1011
1.56M
        PUT_TEMP_STREAM_DATA(u4_buf, u4_buf_nxt, u4_offset, pu4_buf_aligned, ps_stream)
1012
1013
1.56M
        ps_dec->u4_non_zero_cols  = u4_nz_cols;
1014
1.56M
        ps_dec->u4_non_zero_rows  = u4_nz_rows;
1015
1016
1.56M
            return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
1017
1.57M
}
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
/*****************************************************************************/
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
1.26M
{
1061
1.26M
    UWORD16 i4_pos;
1062
1063
1.26M
    WORD32  i4_iter;
1064
1065
    /* Inverse Quantize the predicted DC value for intra MB*/
1066
1.26M
    if(u4_intra_flag == 1)
1067
1.15M
    {
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
1.15M
        {
1073
1.15M
            pi2_coeffs[0]   += *pu2_def_dc_pred;
1074
1.15M
            *pu2_def_dc_pred      = pi2_coeffs[0];
1075
1.15M
            pi2_coeffs[0]   <<= (3 - u2_intra_dc_precision);
1076
1.15M
            pi2_coeffs[0]   = CLIP_S12(pi2_coeffs[0]);
1077
1.15M
        }
1078
1079
1.15M
        pi2_blk[pu1_scan[0]]  = pi2_coeffs[0];
1080
1.15M
    }
1081
    /************************************************************************/
1082
    /* Inverse quantization of other DCT coefficients                       */
1083
    /************************************************************************/
1084
3.16M
    for(i4_iter = u4_intra_flag; i4_iter < i4_num_coeffs; i4_iter++)
1085
1.89M
    {
1086
1087
1.89M
        WORD16 sign;
1088
1.89M
        WORD32 temp, temp1;
1089
1090
        /* Position is the inverse scan of the index stored */
1091
1.89M
        i4_pos      = pu1_pos[i4_iter];
1092
1.89M
        pi2_blk[i4_pos] = pi2_coeffs[i4_iter];
1093
1094
1.89M
        sign = SIGN(pi2_blk[i4_pos]);
1095
1.89M
        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
1.89M
        temp = temp + (1 * !u4_intra_flag);
1101
1102
1.89M
        temp = temp * pu1_weighting_matrix[i4_pos] * u1_quant_scale;
1103
1104
1.89M
        temp = temp >> 5;
1105
1106
1.89M
        temp1 = temp | 1;
1107
1108
1.89M
        temp1 = (temp1 > temp) ? (temp1 - temp) : (temp - temp1);
1109
1110
1.89M
        temp = temp - temp1;
1111
1112
1.89M
        if(temp < 0)
1113
297k
        {
1114
297k
            temp = 0;
1115
297k
        }
1116
1117
1.89M
        temp = temp * sign;
1118
1119
1.89M
        temp = CLIP_S12(temp);
1120
1121
1.89M
        pi2_blk[i4_pos] = temp;
1122
1.89M
    }
1123
1124
    /*return value is used in the case of mpeg2 for mismatch control*/
1125
1.26M
    return  (0);
1126
1.26M
} /* 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
/*****************************************************************************/
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
310k
{
1170
1171
310k
    WORD32  i4_pos;
1172
    /* Used for Mismatch control */
1173
310k
    WORD32 sum;
1174
1175
310k
    WORD32  i4_iter;
1176
1177
310k
    sum = 0;
1178
1179
    /* Inverse Quantize the predicted DC value for intra MB*/
1180
310k
    if(u4_intra_flag == 1)
1181
71.5k
    {
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
71.5k
        {
1187
71.5k
            pi2_coeffs[0]   += *pu2_def_dc_pred;
1188
71.5k
            *pu2_def_dc_pred      = pi2_coeffs[0];
1189
71.5k
            pi2_coeffs[0]   <<= (3 - u2_intra_dc_precision);
1190
71.5k
            pi2_coeffs[0]   = CLIP_S12(pi2_coeffs[0]);
1191
71.5k
        }
1192
1193
71.5k
        pi2_blk[pu1_scan[0]]  = pi2_coeffs[0];
1194
71.5k
        sum = pi2_blk[0];
1195
71.5k
    }
1196
1197
    /************************************************************************/
1198
    /* Inverse quantization of other DCT coefficients                       */
1199
    /************************************************************************/
1200
1.89M
    for(i4_iter = u4_intra_flag; i4_iter < i4_num_coeffs; i4_iter++)
1201
1.58M
    {
1202
1.58M
        WORD16 sign;
1203
1.58M
        WORD32 temp;
1204
        /* Position is the inverse scan of the index stored */
1205
1.58M
        i4_pos      = pu1_pos[i4_iter];
1206
1.58M
        pi2_blk[i4_pos] = pi2_coeffs[i4_iter];
1207
1208
1.58M
        sign = SIGN(pi2_blk[i4_pos]);
1209
1.58M
        temp = ABS(pi2_blk[i4_pos] << 1);
1210
1.58M
        temp = temp + (1 * !u4_intra_flag);
1211
1.58M
        temp = temp * pu1_weighting_matrix[i4_pos] * u1_quant_scale;
1212
1213
1.58M
        temp = temp >> 5;
1214
1215
1.58M
        temp = temp * sign;
1216
1217
1.58M
        temp = CLIP_S12(temp);
1218
1219
1.58M
        pi2_blk[i4_pos] = temp;
1220
1221
1.58M
        sum += temp;
1222
1.58M
    }
1223
310k
    return (sum ^ 1);
1224
310k
} /* End of inv_quant() */