summaryrefslogtreecommitdiff
path: root/third_party/g7221/decode/decoder.c
blob: d642a9102cd7ec8d18226ca088f905d564425872 (plain)
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
/***************************************************************************
**
**   ITU-T G.722.1 (2005-05) - Fixed point implementation for main body and Annex C
**   > Software Release 2.1 (2008-06)
**     (Simple repackaging; no change from 2005-05 Release 2.0 code)
**
**   © 2004 Polycom, Inc.
**
**   All rights reserved.
**
***************************************************************************/

/***************************************************************************
  Filename:    decoder.c    

  Purpose:     Contains files used to implement the G.722.1 Annex C decoder
		
  Design Notes:

***************************************************************************/

/***************************************************************************
 Include files                                                           
***************************************************************************/
#include "defs.h"
#include "tables.h"
#include "huff_def.h"
#include "count.h"


/***************************************************************************
 Function:    decoder

 Syntax:      void decoder(Bit_Obj *bitobj,                   
                           Rand_Obj *randobj,             
                           Word16 number_of_regions,
                           Word16 *decoder_mlt_coefs,     
                           Word16 *p_mag_shift,           
                           Word16 *p_old_mag_shift,       
                           Word16 *old_decoder_mlt_coefs,    
                           Word16 frame_error_flag)      
              
              inputs:    Bit_Obj *bitobj
                         Rand_Obj *randobj
                         Word16 number_of_regions
                         Word16 *p_old_mag_shift
                         Word16 *old_decoder_mlt_coefs
                         Word16 frame_error_flag
              
              outputs:   Word16 *decoder_mlt_coefs,    
                         Word16 *p_mag_shift,          
                  
                  

 Description: Decodes the out_words into mlt coefs using G.722.1 Annex C

 Design Notes:
 
 WMOPS:     7kHz |   24kbit    |    32kbit
          -------|-------------|----------------
            AVG  |    0.84     |    0.94
          -------|-------------|----------------  
            MAX  |    0.90     |    1.00
          -------|-------------|---------------- 
				
           14kHz |   24kbit    |    32kbit      |     48kbit
          -------|-------------|----------------|----------------
            AVG  |    1.31     |    1.56        |     1.88   
          -------|-------------|----------------|----------------
            MAX  |    1.59     |    1.80        |     1.98   
          -------|-------------|----------------|----------------
				
***************************************************************************/
void decoder(Bit_Obj *bitobj,
             Rand_Obj *randobj,
             Word16 number_of_regions,
	         Word16 *decoder_mlt_coefs,
	         Word16 *p_mag_shift,
             Word16 *p_old_mag_shift,
             Word16 *old_decoder_mlt_coefs,
             Word16 frame_error_flag)
{


    Word16  absolute_region_power_index[MAX_NUMBER_OF_REGIONS];
    Word16  decoder_power_categories[MAX_NUMBER_OF_REGIONS];
    Word16  decoder_category_balances[MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES-1];
    UWord16 categorization_control;
    Word16  decoder_region_standard_deviation[MAX_NUMBER_OF_REGIONS];
    Word16  i;

    Word16  num_categorization_control_bits;
    Word16  num_categorization_control_possibilities;
    Word16  number_of_coefs;
    Word16  number_of_valid_coefs;
    
    
    test();
    if (number_of_regions==NUMBER_OF_REGIONS)
    {
        num_categorization_control_bits = NUM_CATEGORIZATION_CONTROL_BITS;
        move16();
        num_categorization_control_possibilities = NUM_CATEGORIZATION_CONTROL_POSSIBILITIES;
        move16();
        number_of_coefs = DCT_LENGTH;
        move16();
        number_of_valid_coefs = NUMBER_OF_VALID_COEFS;
        move16();
    }
    else
    {
        num_categorization_control_bits = MAX_NUM_CATEGORIZATION_CONTROL_BITS;
        move16();
        num_categorization_control_possibilities = MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES;
        move16();
        number_of_coefs = MAX_DCT_LENGTH;
        move16();
        number_of_valid_coefs = MAX_NUMBER_OF_VALID_COEFS;
        move16();
    }

    test();
    if (frame_error_flag == 0) 
    {

        /* convert the bits to absolute region power index and decoder_region_standard_deviation */
        
        decode_envelope(bitobj,
                        number_of_regions,
                        decoder_region_standard_deviation,
		                absolute_region_power_index,
		                p_mag_shift);

        /* fill the categorization_control with NUM_CATEGORIZATION_CONTROL_BITS */
        categorization_control = 0;
        for (i=0; i<num_categorization_control_bits; i++) 
        {
        	get_next_bit(bitobj);
        	categorization_control = shl_nocheck(categorization_control,1);
        	categorization_control = add(categorization_control,bitobj->next_bit);
        }
        
        bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,num_categorization_control_bits);

        /* obtain decoder power categories and category balances */
        /* based on the absolute region power index              */
        categorize(bitobj->number_of_bits_left,
	               number_of_regions,
	               num_categorization_control_possibilities,
	               absolute_region_power_index,
	               decoder_power_categories,
	               decoder_category_balances);

        /* perform adjustmaents to the power categories and category balances based on the cat control */
        rate_adjust_categories(categorization_control,
			                   decoder_power_categories,
			                   decoder_category_balances);

        /* decode the quantized bits into mlt coefs */
        decode_vector_quantized_mlt_indices(bitobj,
                                            randobj,
                                            number_of_regions,
                                            decoder_region_standard_deviation,
					                        decoder_power_categories,
					                        decoder_mlt_coefs);

        /* test for frame errors */
        test_4_frame_errors(bitobj,
                            number_of_regions,
                            num_categorization_control_possibilities,
                            &frame_error_flag,
                            categorization_control,
                            absolute_region_power_index);
    }

    /* perform error handling operations */
    error_handling(number_of_coefs,
                   number_of_valid_coefs,
                   &frame_error_flag,
                   decoder_mlt_coefs,
                   old_decoder_mlt_coefs,
                   p_mag_shift,
                   p_old_mag_shift);

}

/***************************************************************************
 Function:    decode_envelope

 Syntax:      void decode_envelope(Bit_Obj *bitobj,                              
                                   Word16  number_of_regions,
                                   Word16  *decoder_region_standard_deviation,   
                                   Word16  *absolute_region_power_index,         
                                   Word16  *p_mag_shift)                         
              
              inputs:   Bit_Obj *bitobj
                        Word16  number_of_regions
                        
                        
              outputs:  Word16  *decoder_region_standard_deviation
                        Word16  *absolute_region_power_index
                        Word16  *p_mag_shift
              
 
 Description: Recover differential_region_power_index from code bits

 Design Notes:
 
 WMOPS:     7kHz |    24kbit    |    32kbit
          -------|--------------|----------------
            AVG  |     0.04     |    0.04
          -------|--------------|----------------  
            MAX  |     0.05     |    0.05
          -------|--------------|---------------- 
				
           14kHz |    24kbit    |    32kbit      |     48kbit
          -------|--------------|----------------|----------------
            AVG  |     0.08     |    0.08        |     0.08   
          -------|--------------|----------------|----------------
            MAX  |     0.10     |    0.10        |     0.10   
          -------|--------------|----------------|----------------
				
***************************************************************************/
void decode_envelope(Bit_Obj *bitobj,
                     Word16  number_of_regions,
                     Word16  *decoder_region_standard_deviation,
		             Word16  *absolute_region_power_index,
		             Word16  *p_mag_shift)
     
{
    Word16 region;
    Word16 i;
    Word16 index;
    Word16 differential_region_power_index[MAX_NUMBER_OF_REGIONS];
    Word16 max_index;
    
    Word16 temp;
    Word16 temp1;
    Word16 temp2;
    Word32 acca;

    index = 0;
    move16();

    /* get 5 bits from the current code word */
    for (i=0; i<5; i++) 
    {
        get_next_bit(bitobj);
        index = shl_nocheck(index,1);
        index = add(index,bitobj->next_bit);
    }
    bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,5);

    /* ESF_ADJUSTMENT_TO_RMS_INDEX compensates for the current (9/30/96)
        IMLT being scaled to high by the ninth power of sqrt(2). */
    differential_region_power_index[0] = sub(index,ESF_ADJUSTMENT_TO_RMS_INDEX);
    move16();

    /* obtain differential_region_power_index */
    for (region=1; region<number_of_regions; region++) 
    {
        index = 0;
        move16();
        do 
        {
            get_next_bit(bitobj);
            test();
            if (bitobj->next_bit == 0)
            {
	            index = differential_region_power_decoder_tree[region][index][0];
                move16();
            }
            else
            {
	            index = differential_region_power_decoder_tree[region][index][1];
                move16();
            }
            bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
            test();
        } while (index > 0);
        
        differential_region_power_index[region] = negate(index);
        move16();
    }

    /* Reconstruct absolute_region_power_index[] from differential_region_power_index[]. */
    absolute_region_power_index[0] = differential_region_power_index[0];
    move16();
    for (region=1; region<number_of_regions; region++) 
    {
        acca = L_add(absolute_region_power_index[region-1],differential_region_power_index[region]);
        acca = L_add(acca,DRP_DIFF_MIN);
        absolute_region_power_index[region] = extract_l(acca);        
    }

    /* Reconstruct decoder_region_standard_deviation[] from absolute_region_power_index[]. */
    /* DEBUG!!!! - This integer method jointly computes the mag_shift
       and the standard deviations already mag_shift compensated. It
       relies on REGION_POWER_STEPSIZE_DB being exactly 3.010299957 db
       or a square root of 2 chnage in standard deviation. If
       REGION_POWER_STEPSIZE_DB changes, this software must be
       reworked. */

    temp = 0;
    move16();
    max_index = 0;
    move16();
    for (region=0; region<number_of_regions; region++) 
    {
        acca = L_add(absolute_region_power_index[region],REGION_POWER_TABLE_NUM_NEGATIVES);
        i = extract_l(acca);
        
        temp1 = sub(i,max_index);
        test();
        if (temp1 > 0) 
        {
            max_index = i;
            move16();
        }
        temp = add(temp,int_region_standard_deviation_table[i]);
    }
    i = 9;
    move16();

    temp1 = sub(temp,8);
    temp2 = sub(max_index,28);
    test();
    test();
    logic16();
    test();
    logic16();
    while ((i >= 0) && ((temp1 >= 0) || (temp2 > 0))) 
    {
        i = sub(i,1);
        temp = shr_nocheck(temp,1);
        max_index = sub(max_index,2);
        temp1 = sub(temp,8);
        temp2 = sub(max_index,28);
        test();
        test();
        logic16();
        test();
        logic16();
    }
    
    *p_mag_shift = i;
    move16();
    
    /* pointer arithmetic */
    temp = (Word16 )(REGION_POWER_TABLE_NUM_NEGATIVES + (*p_mag_shift * 2));
    
    for (region=0; region<number_of_regions; region++) 
    {
        acca = L_add(absolute_region_power_index[region],temp);
        i = extract_l(acca);
        decoder_region_standard_deviation[region] = int_region_standard_deviation_table[i];
        move16();
    }

}

/***************************************************************************
 Function:     rate_adjust_categories

 Syntax:       void rate_adjust_categories(Word16 categorization_control,            
                                           Word16 *decoder_power_categories,         
                                           Word16 *decoder_category_balances)        
               
               inputs:    Word16 categorization_control,   
                          Word16 *decoder_power_categories,
                          Word16 *decoder_category_balances
                          
               outputs:   Word16 categorization_control,   
                          Word16 *decoder_power_categories,
 
 Description:  Adjust the power categories based on the categorization control

 Design Notes:
 
 WMOPS:     7kHz |    24kbit    |    32kbit
          -------|--------------|----------------
            AVG  |    0.00      |    0.00
          -------|--------------|----------------  
            MAX  |    0.00      |    0.00
          -------|--------------|---------------- 
				
           14kHz |    24kbit    |    32kbit      |     48kbit
          -------|--------------|----------------|----------------
            AVG  |    0.00      |    0.00        |     0.00   
          -------|--------------|----------------|----------------
            MAX  |    0.01      |    0.01        |     0.01   
          -------|--------------|----------------|----------------
				
***************************************************************************/
void rate_adjust_categories(Word16 categorization_control,
			                Word16 *decoder_power_categories,
			                Word16 *decoder_category_balances)
{
    Word16 i;
    Word16 region;
    
    i = 0;
    move16();

    test();
    while (categorization_control > 0) 
    {
        region = decoder_category_balances[i++];
        move16();
        decoder_power_categories[region] = add(decoder_power_categories[region],1);
        move16();
        categorization_control = sub(categorization_control,1);
    }

}

/***************************************************************************
 Function:    decode_vector_quantized_mlt_indices

 Syntax:      void decode_vector_quantized_mlt_indices(Bit_Obj  *bitobj,                                      
                                                       Rand_Obj *randobj,                           
                                                       Word16   number_of_regions,
                                                       Word16   *decoder_region_standard_deviation, 
                                                       Word16   *decoder_power_categories,          
                                                       Word16   *decoder_mlt_coefs)                 
              inputs:    Bit_Obj  *bitobj                           
                         Rand_Obj *randobj
                         Word16   number_of_regions
                         Word16   *decoder_region_standard_deviation
                         Word16   *decoder_power_categories
            
            
              outputs:   Word16   *decoder_mlt_coefs
             

 Description: Decode MLT coefficients

 Design Notes:
 
 WMOPS:     7kHz |    24kbit    |    32kbit
          -------|--------------|----------------
            AVG  |    0.60      |    0.72
          -------|--------------|----------------  
            MAX  |    0.67      |    0.76
          -------|--------------|---------------- 
				
           14kHz |    24kbit    |    32kbit      |     48kbit
          -------|--------------|----------------|----------------
            AVG  |    0.77      |    0.98        |     1.28   
          -------|--------------|----------------|----------------
            MAX  |    1.05      |    1.18        |     1.36   
          -------|--------------|----------------|----------------
				
***************************************************************************/
void decode_vector_quantized_mlt_indices(Bit_Obj  *bitobj,
                                         Rand_Obj *randobj,
                                         Word16   number_of_regions,
                                         Word16   *decoder_region_standard_deviation,
					                     Word16   *decoder_power_categories,
					                     Word16   *decoder_mlt_coefs)
{
    Word16 standard_deviation;
    Word16 *decoder_mlt_ptr;
    Word16 decoder_mlt_value;
    Word16 noifillpos;
    Word16 noifillneg;
    Word16 noise_fill_factor[3] = {5793,8192,23170};
    Word16 region;
    Word16 category;
    Word16 j,n;
    Word16 k[MAX_VECTOR_DIMENSION];
    Word16 vec_dim;
    Word16 num_vecs;
    Word16 index;
    Word16 signs_index;
    Word16 bit;
    Word16 num_sign_bits;
    Word16 ran_out_of_bits_flag;
    Word16 *decoder_table_ptr;
    Word16 random_word;
    
    Word16 temp1;
    Word16 temp;
    Word32 acca;

    ran_out_of_bits_flag = 0;
    move16();

    for (region=0; region<number_of_regions; region++) 
    {
        category = (Word16)decoder_power_categories[region];
        move16();
        acca = L_mult0(region,REGION_SIZE);
        index = extract_l(acca);
        decoder_mlt_ptr = &decoder_mlt_coefs[index];
        move16();
        standard_deviation = decoder_region_standard_deviation[region];
        move16();
        
        temp = sub(category,7);
        test();
        if (temp < 0)
        {
            /* Get the proper table of decoder tables, vec_dim, and num_vecs for the cat */
            decoder_table_ptr = (Word16 *) table_of_decoder_tables[category];
            move16();
            vec_dim = vector_dimension[category];
            move16();
            num_vecs = number_of_vectors[category];
            move16();
            
            for (n=0; n<num_vecs; n++) 
            {
                index = 0;
                move16();
                
                /* get index */
                do 
                {
                    test();
                    if (bitobj->number_of_bits_left <= 0) 
                    {
                        ran_out_of_bits_flag = 1;
                        move16();
    	                break;
    	            }
    
    	            get_next_bit(bitobj);
	                
                    test();
                    if (bitobj->next_bit == 0)
	                {
                        temp = shl_nocheck(index,1);
                        index = (Word16)*(decoder_table_ptr + temp);
                        move16();
                    }
	                else
	                {
                        temp = shl_nocheck(index,1);
                        index = (Word16)*(decoder_table_ptr + temp + 1);
                        move16();
                    }
	                bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
                    test();
	            
                } while (index > 0);
	  
                test();
                if (ran_out_of_bits_flag != 0)
	                break;
	  
                index = negate(index);
	            
                /* convert index into array used to access the centroid table */
                /* get the number of sign bits in the index */
                num_sign_bits = index_to_array(index,k,category);

	            temp = sub(bitobj->number_of_bits_left,num_sign_bits);
                test();
                if (temp >= 0) 
                {
	                test();
                    if (num_sign_bits != 0) 
                    {
	                    signs_index = 0;
	                    move16();
                        for (j=0; j<num_sign_bits; j++) 
                        {
		                    get_next_bit(bitobj);
       		                signs_index = shl_nocheck(signs_index,1);
		                    signs_index = add(signs_index,bitobj->next_bit);
		                    bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
	                    }
	                    temp = sub(num_sign_bits,1);
                        bit = shl_nocheck(1,(temp));
	                }
	                
                    for (j=0; j<vec_dim; j++) 
                    {
	                    acca = L_mult0(standard_deviation,mlt_quant_centroid[category][k[j]]);
                        acca = L_shr_nocheck(acca,12);
                        decoder_mlt_value = extract_l(acca);
	                    
                        test();
                        if (decoder_mlt_value != 0) 
                        {
		                    test();
                            if ((signs_index & bit) == 0)
		                        decoder_mlt_value = negate(decoder_mlt_value);
		                    bit = shr_nocheck(bit,1);
	                    }
                        *decoder_mlt_ptr++ = decoder_mlt_value;
                        move16();
	                }
	            }
	            else 
                {
	                ran_out_of_bits_flag = 1;
                    move16();
	                break;
	            }
	        }
            /* If ran out of bits during decoding do noise fill for remaining regions. */
            /* DEBUG!! - For now also redo all of last region with all noise fill. */
        	test();
            if (ran_out_of_bits_flag != 0) 
            {
        	    temp = add(region,1);
                for (j=temp; j<number_of_regions; j++)
                {
                    decoder_power_categories[j] = 7;
                    move16();
                }
        	    category = 7;
                move16();
        	    decoder_mlt_ptr = &decoder_mlt_coefs[region*REGION_SIZE];
                move16();
        	}
        }

        temp = sub(category,5);
        temp1 = sub(category,6);
        test();
        test();
        logic16();
        if ((temp == 0) || (temp1 == 0))
        {
 
	        decoder_mlt_ptr = &decoder_mlt_coefs[region*REGION_SIZE];
	        move16();
            noifillpos = mult(standard_deviation,noise_fill_factor[category - 5]);
            noifillneg = negate(noifillpos);

	        random_word = get_rand(randobj);

	        for (j=0; j<10; j++) 
            {
	            test();
                if (*decoder_mlt_ptr == 0) 
                {
	                logic16();
                    test();
                    if ((random_word & 1) == 0) 
                    {
                        temp1 = noifillneg;
                        move16();
                    }
	                else
                    {
                        temp1 = noifillpos;
                        move16();
                    }
	                *decoder_mlt_ptr = temp1;
                    move16();
	                random_word = shr_nocheck(random_word,1);
	            }
	            /* pointer arithmetic */
                decoder_mlt_ptr++;
	        }
	        random_word = get_rand(randobj);
	        for (j=0; j<10; j++) 
            {
	            test();
                if (*decoder_mlt_ptr == 0) 
                {
	                logic16();
                    test();
                    if ((random_word & 1) == 0) 
                    {
                        temp1 = noifillneg;
                        move16();
                    }
	                else
                    {
                        temp1 = noifillpos;
                        move16();
                    }
	                *decoder_mlt_ptr = temp1;
                    move16();
	                random_word  = shr_nocheck(random_word,1);
	            }
	            /* pointer arithmetic */
                decoder_mlt_ptr++;
	        }
        }

        /* if (category == 7) */
        temp1 = sub(category,7);
        test();
        if (temp1 == 0)
        {
	        index = sub(category,5);
            noifillpos = mult(standard_deviation,noise_fill_factor[index]);
	        noifillneg = negate(noifillpos);

            random_word = get_rand(randobj);
            for (j=0; j<10; j++) 
            {
                logic16();
                test();
                if ((random_word & 1) == 0) 
                {
                    temp1 = noifillneg;
                    move16();
                }
                else
                {
                    temp1 = noifillpos;
                    move16();
                }
                *decoder_mlt_ptr++ = temp1;
                move16();
                random_word = shr_nocheck(random_word,1);
            }
            random_word = get_rand(randobj);
            for (j=0; j<10; j++) 
            {
                logic16();
                test();
                if ((random_word & 1) == 0) 
                {
                    temp1 = noifillneg;
                    move16();
                }
                else
                {
                    temp1 = noifillpos;
                    move16();
                }
                
                *decoder_mlt_ptr++ = temp1;
                move16();
                random_word = shr_nocheck(random_word,1);
            }
        }
    }

    test();
    if (ran_out_of_bits_flag)
        bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
}
/****************************************************************************************
 Function:    index_to_array 

 Syntax:      number_of_non_zero = index_to_array(Word16 index, 
                                                  Word16 array[MAX_VECTOR_DIMENSION],
                                                  Word16 category)

                inputs:  Word16 index
                         Word16 category                     
                       
                outputs: Word16 array[MAX_VECTOR_DIMENSION] - used in decoder to access
                                                             mlt_quant_centroid table
                        
                         Word16 number_of_non_zero          - number of non zero elements
                                                             in the array
 
 Description: Computes an array of sign bits with the length of the category vector
              Returns the number of sign bits and the array

 WMOPS:     7kHz |    24kbit    |    32kbit
          -------|--------------|----------------
            AVG  |     0.00     |     0.00
          -------|--------------|----------------  
            MAX  |     0.00     |     0.00
          -------|--------------|---------------- 

           14kHz |    24kbit    |    32kbit      |     48kbit
          -------|--------------|----------------|----------------
            AVG  |     0.00     |     0.00       |      0.00   
          -------|--------------|----------------|----------------
            MAX  |     0.00     |     0.00       |      0.00   
          -------|--------------|----------------|----------------

****************************************************************************************/
Word16 index_to_array(Word16 index,Word16 *array,Word16 category)
{
    Word16 j,q,p;
    Word16 number_of_non_zero;
    Word16 max_bin_plus_one;
    Word16 inverse_of_max_bin_plus_one;
    Word16 temp;

    number_of_non_zero = 0;
    move16();

    p = index;
    move16();

    max_bin_plus_one = add(max_bin[category],1);
    inverse_of_max_bin_plus_one = max_bin_plus_one_inverse[category];
    move16();

    temp = sub(vector_dimension[category],1);
    for (j=temp; j>=0; j--) 
    {
        q = mult(p,inverse_of_max_bin_plus_one);
		temp = extract_l(L_mult0(q,max_bin_plus_one));
        array[j] = sub(p,temp);
        move16();

        p = q;
        move16();

        temp = array[j];
        move16();
        test();
        if (temp != 0) 
            number_of_non_zero = add(number_of_non_zero,1);
    }
    return(number_of_non_zero);
}
/***************************************************************************
 Function:     test_4_frame_errors

 Syntax:       void test_4_frame_errors(Bit_Obj *bitobj,                        
                                        Word16 number_of_regions,
                                        Word16 num_categorization_control_possibilities,
                                        Word16 *frame_error_flag,                
                                        Word16 categorization_control,          
                                        Word16 *absolute_region_power_index)    
        
               inputs:   bit_obj
                         number_of_regions
                         num_categorization_control_possibilities
                         frame_error_flag
                         categorization_control
                         absolute_region_power_index
               
               
               outputs:  frame_error_flag
               
               
               
        
 Description:  Tests for error conditions and sets the frame_error_flag accordingly 

 Design Notes:
 
 WMOPS:     7kHz |    24kbit    |    32kbit
          -------|--------------|----------------
            AVG  |    0.01      |     0.01
          -------|--------------|----------------  
            MAX  |    0.04      |     0.08
          -------|--------------|---------------- 
				
           14kHz |    24kbit    |    32kbit      |     48kbit
          -------|--------------|----------------|----------------
            AVG  |    0.01      |     0.01       |      0.01   
          -------|--------------|----------------|----------------
            MAX  |    0.02      |     0.06       |      0.08   
          -------|--------------|----------------|----------------
				
***************************************************************************/
void test_4_frame_errors(Bit_Obj *bitobj,
                         Word16 number_of_regions,
                         Word16 num_categorization_control_possibilities,
                         Word16 *frame_error_flag,
                         Word16 categorization_control,
                         Word16 *absolute_region_power_index)
{
    Word16 region;
    Word16 i;
    Word16 temp;
    Word32 acca;
    Word32 accb;
    
    /* Test for bit stream errors. */

    test();
    if (bitobj->number_of_bits_left > 0) 
    {
        for (i=0; i<bitobj->number_of_bits_left; i++) 
        {
            get_next_bit(bitobj);
            test();
            if (bitobj->next_bit == 0) 
            {
                *frame_error_flag = 1;
                move16();
            }
        }	
    }
    else 
    {
        temp = sub(categorization_control,sub(num_categorization_control_possibilities,1));
        test();
        if (temp < 0) 
        {
            test();
            if (bitobj->number_of_bits_left < 0)
            {
                *frame_error_flag |= 2;
                logic16();
            }
        }
    }

    /* checks to ensure that abs_region_power_index is within range */
    /* the error flag is set if it is out of range */
    for (region=0; region<number_of_regions; region++) 
    {
        /*  the next two lines of comments were modified in release 1.2
	   *  to correct the description of the range of 
	   *  absolute_region_power_index[] to be tested in the next
	   *  9 lines of code.
	   */
	  /*  if ((absolute_region_power_index[region] > 31) ||
            (absolute_region_power_index[region] < -8) */

        acca = L_add(absolute_region_power_index[region],ESF_ADJUSTMENT_TO_RMS_INDEX);
        accb = L_sub(acca,31);
        acca = L_add(acca,8);
        test();

        /* the next line was modifed in release 1.2 to
	   * correct miss typed code and error checking.
	   */
        if ((accb > 0) || (acca < 0))
        {
            *frame_error_flag |= 4;
            logic16();
        }
    }

}
/***************************************************************************
 Function:    error_handling

 Syntax:      void error_handling(Word16 number_of_coefs,
                                  Word16 number_of_valid_coefs,
                                  Word16 *frame_error_flag,     
                                  Word16 *decoder_mlt_coefs,    
                                  Word16 *old_decoder_mlt_coefs,
                                  Word16 *p_mag_shift,          
                                  Word16 *p_old_mag_shift)      
              
              inputs:  number_of_coefs
                       number_of_valid_coefs
                       frame_error_flag
                       old_decoder_mlt_coefs
                       p_old_mag_shift
              
              
              outputs: decoder_mlt_coefs
                       old_decoder_mlt_coefs
                       p_mag_shift
                       p_old_mag_shift
 
       

 Description: If both the current and previous frames are errored,             
              set the mlt coefficients to 0. If only the current frame         
              is errored, then repeat the previous frame's mlt coefficients.   
    
 Design Notes:
 
 WMOPS:     7kHz |    24kbit    |    32kbit
          -------|--------------|----------------
            AVG  |    0.02      |     0.02
          -------|--------------|----------------  
            MAX  |    0.03      |     0.03
          -------|--------------|---------------- 
				
           14kHz |    24kbit    |    32kbit      |     48kbit
          -------|--------------|----------------|----------------
            AVG  |    0.03      |     0.03       |     0.03   
          -------|--------------|----------------|----------------
            MAX  |    0.03      |     0.03       |     0.06   
          -------|--------------|----------------|----------------
				
***************************************************************************/
void error_handling(Word16 number_of_coefs,
                    Word16 number_of_valid_coefs,
                    Word16 *frame_error_flag,
                    Word16 *decoder_mlt_coefs,
                    Word16 *old_decoder_mlt_coefs,
                    Word16 *p_mag_shift,
                    Word16 *p_old_mag_shift)
{
    Word16 i;

    test();
    if (*frame_error_flag != 0) 
    {

        for (i = 0; i < number_of_valid_coefs; i++)
        {
            decoder_mlt_coefs[i] = old_decoder_mlt_coefs[i];
            move16();
        }

        for (i = 0; i < number_of_valid_coefs; i++)
        {
            old_decoder_mlt_coefs[i] = 0;
            move16();
        }
        
        *p_mag_shift = *p_old_mag_shift;
        move16();

        *p_old_mag_shift = 0;
        move16();
    }
    else 
    {
        /* Store in case next frame is errored. */
        for (i = 0; i < number_of_valid_coefs; i++)
        {
            old_decoder_mlt_coefs[i] = decoder_mlt_coefs[i];
            move16();
        }
  
        *p_old_mag_shift = *p_mag_shift;
        move16();
    }


    /* Zero out the upper 1/8 of the spectrum. */
    for (i = number_of_valid_coefs; i < number_of_coefs; i++)
    {
        decoder_mlt_coefs[i] = 0;
        move16();
    }

}
/****************************************************************************************
 Function:    get_next_bit

 Syntax:      void get_next_bit(Bit_Obj *bitobj)
 
 Description: Returns the next bit in the current word inside the bit object
 
 WMOPS:     7kHz |    24kbit    |    32kbit
          -------|--------------|----------------
            AVG  |    0.00      |    0.00
          -------|--------------|----------------  
            MAX  |    0.00      |    0.00
          -------|--------------|---------------- 

           14kHz |    24kbit    |    32kbit      |     48kbit
          -------|--------------|----------------|----------------
            AVG  |    0.00      |    0.00        |     0.00   
          -------|--------------|----------------|----------------
            MAX  |    0.00      |    0.00        |     0.00   
          -------|--------------|----------------|----------------

****************************************************************************************/
void get_next_bit(Bit_Obj *bitobj)
{
    Word16 temp;

    test();
    if (bitobj->code_bit_count == 0)
    {                        
        bitobj->current_word = *bitobj->code_word_ptr++; 
        move16();
        bitobj->code_bit_count = 16;           
        move16();
    }
    bitobj->code_bit_count = sub(bitobj->code_bit_count,1);
    temp = shr_nocheck(bitobj->current_word,bitobj->code_bit_count);
    logic16();
    bitobj->next_bit = (Word16 )(temp & 1);

}
/****************************************************************************************
 Function:    get_rand

 Syntax:      Word16 get_rand(Rand_Obj *randobj)
 
 Description: Returns a random Word16 based on the seeds inside the rand object  
 
 WMOPS:     7kHz |    24kbit    |    32kbit
          -------|--------------|----------------
            AVG  |    0.00      |    0.00
          -------|--------------|----------------  
            MAX  |    0.00      |    0.00
          -------|--------------|---------------- 

           14kHz |    24kbit    |    32kbit      |     48kbit
          -------|--------------|----------------|----------------
            AVG  |    0.00      |    0.00        |     0.00   
          -------|--------------|----------------|----------------
            MAX  |    0.00      |    0.00        |     0.00   
          -------|--------------|----------------|----------------

****************************************************************************************/
Word16 get_rand(Rand_Obj *randobj)
{
    Word16 random_word;
    Word32 acca;

    acca = L_add(randobj->seed0,randobj->seed3); 
    random_word = extract_l(acca);
    
    logic16();
    test();
	if ((random_word & 32768L) != 0)
        random_word = add(random_word,1);
    
    randobj->seed3 = randobj->seed2;
    move16();
    randobj->seed2 = randobj->seed1;
    move16();
    randobj->seed1 = randobj->seed0;
    move16();
    randobj->seed0 = random_word;
    move16();

    return(random_word);
}