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