summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/aacdec/long_term_synthesis.cpp
blob: c361db161c09b3e9d9909463d1c7f323f2c0d3f6 (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
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
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * 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.
 * -------------------------------------------------------------------
 */
/*

 Pathname: long_term_synthesis.c


------------------------------------------------------------------------------
 REVISION HISTORY

 Description: Made the following changes based on the review comments.
              1. Separated "shift_factor>=0" on line 395 to "shift_factor>0"
                 and "shift_factor=0" two cases.
              2. Added comments on line 393 to explain why factor 2 is being
                 used to calculate shift_factor.
              3. Added comments for short window implementation.
              4. Changed "*(pPredicted_spectral++) = *pPredicted_spectral>>2"
                 to "*(pPredicted++)>>=2" although they are the same.
              5. Changed pseudo code "X+=Y" to "X=X+Y".
              6. Fixed ending comment of "for" loop.
              7. Passed in the size of the array and deleted some of the
                 include files.

 Description: Unroll the loops.

 Description: Changed index "wnd" in previous line 584 with "wnd_offset"
              and made other correspondent changes to the code.


 Description: Based on Ken's suggestion, modified the function with the
              passing-in Q format as scalefactor band basis in order to
              simplify TNS block functions.

 Description: Optimization.

 Description: Made changes based on review comments.
              1. Changed misspellings.
              2. Changed win_sfb_top[] from two dimensional array to one
              dimensional array and correspondently changed the code.
              3. Changed function prototype to remove some redundant
              informations.
              4. Fixed the adjusting Q format part code.
              5. Fixed lines 825, 826 with correct updating pointers.

 Description: Due to TNS and LTP Q format issue, added code to adjust
              predicted_spectral() to maximum resolution before perform
              long term synthesis.

 Description: Modified based on review comments.

 Description: Changed "max" data type from UInt to UInt32.

 Description: Changed so that nothing is done for the case of "all zero"
 data coming from the output of Trans4m_time_2_freq. Also, included more
 efficient calculation of the abs(x).  And, I updated the pseudocode.

 Description: Use an auxiliary variable temp, to avoid using the same
    pointer and a post-increment pointer in the same line. This may not
    work with all compilers.

 Who:                                   Date:
 Description:

------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    win_seq = type of window sequence (WINDOW_SEQUENCE).
    sfb_per_win = number of scalefactor bands for each window, 1024 for
                  long window, 128 for short window, type of Int.
    win_sfb_top = buffer (Int16) containing the top coefficient per
                  scalefactor band for each window.
    win_prediction_used = buffer (Int) containing the prediction flag
                          information for short windows. Each item in the
                          buffer toggles prediction on(1)/off(0) for each
                          window separately.
    sfb_prediction_used = buffer (Int) containing the prediction flag
                          information for scalefactor band(sfb). Each item
                          toggle prediction on(1)/off(0) on each scalefactor
                          band of every window.
    current_frame = channel buffer (Int32) containing the dequantized
                    spectral coefficients or errors of current frame.
    q_format = buffer (Int) containing Q format for each scalefactor band of
               input current_frame.
    predicted_spectral = buffer (Int32) containing predicted spectral
                         components of current frame.
    pred_q_format = Q format (Int) for predicted spectral components of
                    current frame.
    coef_per_win = number of coefficients per window for short windows.
                   type of Int.
    short_window_num = number of short windows, type of Int.
    reconstruct_sfb_num = number of scalefactor bands used for reconstruction
                          for short windows, type of Int.

 Local Stores/Buffers/Pointers Needed:
    None

 Global Stores/Buffers/Pointers Needed:
    None

 Outputs:
    None

 Pointers and Buffers Modified:
    current_frame contents are the dequantized spectrum with a prediction
    vector added when prediction is turned on.

    q_format contents are updated with the new Q format (Int) for each
    scalefactor band of output current_frame buffer.

 Local Stores Modified:
    None

 Global Stores Modified:
    None

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 This function performs long term synthesis using transmitted spectral
 coeffients or errors and predicted spectral components.

 Long term synthesis is part of long term prediction (LTP) which is used to
 reduce the redundancy of a signal between successive coding frames. The
 functionality of long term synthesis is to reconstruct the frequency domain
 spectral by adding the predicted spectral components and the transmitted
 spectral error when prediction is turned on.

------------------------------------------------------------------------------
 REQUIREMENTS

 None

------------------------------------------------------------------------------
 REFERENCES

 (1) ISO/IEC 14496-3:1999(E)
     Part 3: Audio
        Subpart 4.6.6   Long Term Prediction (LTP)

 (2) MPEG-2 NBC Audio Decoder
     "This software module was originally developed by Nokia in the course
     of development of the MPEG-2 AAC/MPEG-4 Audio standard ISO/IEC13818-7,
     14496-1, 2 and 3. This software module is an implementation of a part
     of one or more MPEG-2 AAC/MPEG-4 Audio tools as specified by the MPEG-2
     aac/MPEG-4 Audio standard. ISO/IEC  gives users of the MPEG-2aac/MPEG-4
     Audio standards free license to this software module or modifications
     thereof for use in hardware or software products claiming conformance
     to the MPEG-2 aac/MPEG-4 Audio  standards. Those intending to use this
     software module in hardware or software products are advised that this
     use may infringe existing patents. The original developer of this
     software module, the subsequent editors and their companies, and ISO/IEC
     have no liability for use of this software module or modifications
     thereof in an implementation. Copyright is not released for non MPEG-2
     aac/MPEG-4 Audio conforming products. The original developer retains
     full right to use the code for the developer's own purpose, assign or
     donate the code to a third party and to inhibit third party from using
     the code for non MPEG-2 aac/MPEG-4 Audio conforming products. This
     copyright notice must be included in all copies or derivative works.
     Copyright (c)1997.

------------------------------------------------------------------------------
 PSEUDO-CODE

    pPredicted_spectral = &predicted_spectral[0];
    pPredicted_spectral_start = pPredicted_spectral;
    pSfb_prediction_used = &sfb_prediction_used[0];

    IF (win_seq != EIGHT_SHORT_SEQUENCE)
    THEN

        sfb_offset = 0;

        pWinSfbTop = &pWin_sfb_top[0];

        pQ_format = &q_format[0];

        FOR (i = sfb_per_frame; i>0; i--)

            IF (*(pSfb_prediction_used++) != FALSE)
            THEN

                pPredicted_offset = pPredicted_spectral_start +
                                                            sfb_offset;
                pCurrent_frame = &current_frame[sfb_offset];

                quarter_sfb_width = (*pWinSfbTop - sfb_offset) >> 2;

                max = 0;

                pPredicted_spectral = pPredicted_offset;

                FOR (j = (*pWinSfbTop - sfb_offset); j>0 ; j--)

                    tmpInt32 = *(pPredicted_spectral++);

                    IF (tmpInt32 < 0)
                    THEN

                        tmpInt32 = -tmpInt32;

                    ENDIF

                    max |= tmpInt32;

                ENDFOR

                tmpInt = 0;

                IF (max != 0)
                THEN

                    WHILE (max < 0x40000000L)

                        max <<= 1;
                        tmpInt++;

                    ENDWHILE

                    pPredicted_spectral = pPredicted_offset;

                    FOR (j = quarter_sfb_width; j>0 ; j--)

                        *(pPredicted_spectral++) <<= tmpInt;
                        *(pPredicted_spectral++) <<= tmpInt;
                        *(pPredicted_spectral++) <<= tmpInt;
                        *(pPredicted_spectral++) <<= tmpInt;

                    ENDFOR

                    adjusted_pred_q = pred_q_format + tmpInt;

                    pPredicted_spectral = pPredicted_offset;

                    shift_factor = *(pQ_format) - adjusted_pred_q;

                    IF ((shift_factor >= 0) && (shift_factor < 31))
                    THEN

                        shift_factor = shift_factor + 1;

                        FOR (j = quarter_sfb_width; j>0 ; j--)

                            *(pCurrent_frame++) =
                                (*pCurrent_frame>>shift_factor)
                              + (*(pPredicted_spectral++)>>1);
                            *(pCurrent_frame++) =
                                (*pCurrent_frame>>shift_factor)
                              + (*(pPredicted_spectral++)>>1);
                            *(pCurrent_frame++) =
                                (*pCurrent_frame>>shift_factor)
                              + (*(pPredicted_spectral++)>>1);
                            *(pCurrent_frame++) =
                                (*pCurrent_frame>>shift_factor)
                              + (*(pPredicted_spectral++)>>1);

                        ENDFOR

                        *(pQ_format) = adjusted_pred_q - 1;

                    ELSEIF (shift_factor >= 31)
                    THEN

                        FOR (j = quarter_sfb_width; j>0 ; j--)

                            *(pCurrent_frame++) = *(pPredicted_spectral++);
                            *(pCurrent_frame++) = *(pPredicted_spectral++);
                            *(pCurrent_frame++) = *(pPredicted_spectral++);
                            *(pCurrent_frame++) = *(pPredicted_spectral++);

                        ENDFOR

                        *(pQ_format) = adjusted_pred_q;

                    ELSEIF ((shift_factor < 0) && (shift_factor > -31))
                    THEN

                        shift_factor = 1 - shift_factor;

                        FOR (j = quarter_sfb_width; j>0 ; j--)

                            *(pCurrent_frame++) = (*pCurrent_frame>>1) +
                                (*(pPredicted_spectral++)>>shift_factor);
                            *(pCurrent_frame++) = (*pCurrent_frame>>1) +
                                (*(pPredicted_spectral++)>>shift_factor);
                            *(pCurrent_frame++) = (*pCurrent_frame>>1) +
                                (*(pPredicted_spectral++)>>shift_factor);
                            *(pCurrent_frame++) = (*pCurrent_frame>>1) +
                                (*(pPredicted_spectral++)>>shift_factor);

                        ENDFOR

                        *(pQ_format) = *(pQ_format) - 1;

                    ENDIF

                ENDIF

            ENDIF [ IF (*(pSfb_prediction_used++) != FALSE) ]

            sfb_offset = *pWinSfbTop;
            pWinSfbTop = pWinSfbTop + 1;
            pQ_format = pQ_format + 1;

        ENDFOR [ FOR (i = sfb_per_frame; i>0; i--) ]

    ELSE

        pCurrent_frame_start = &current_frame[0];

        pQ_format_start = &q_format[0];

        num_sfb = sfb_per_win[0];

        FOR (wnd=0; wnd<short_window_num; wnd++)

            pWinSfbTop = &pWin_sfb_top[0];

            pQ_format = pQ_format_start;

            IF (win_prediction_used[wnd] != FALSE)
            THEN

                sfb_offset = 0;

                FOR (i = reconstruct_sfb_num; i > 0; i--)

                    pPredicted_offset = pPredicted_spectral_start +
                                                                sfb_offset;
                    pCurrent_frame = pCurrent_frame_start + sfb_offset;

                    quarter_sfb_width = (*pWinSfbTop - sfb_offset) >> 2;

                    max = 0;

                    pPredicted_spectral = pPredicted_offset;

                    FOR (j = (*pWinSfbTop - sfb_offset); j>0 ; j--)

                        tmpInt32 = *(pPredicted_spectral++);

                        IF (tmpInt32 < 0)
                        THEN

                            tmpInt32 = -tmpInt32;

                        ENDIF

                        max |= tmpInt32;

                    ENDFOR

                    tmpInt = 0;

                    IF (max != 0)
                    THEN

                        WHILE (max < 0x40000000L)

                            max <<= 1;
                            tmpInt++;

                        ENDWHILE


                        pPredicted_spectral = pPredicted_offset;

                        FOR (j = quarter_sfb_width; j>0 ; j--)

                            *(pPredicted_spectral++) <<= tmpInt;
                            *(pPredicted_spectral++) <<= tmpInt;
                            *(pPredicted_spectral++) <<= tmpInt;
                            *(pPredicted_spectral++) <<= tmpInt;

                        ENDFOR

                        adjusted_pred_q = pred_q_format + tmpInt;

                        pPredicted_spectral = pPredicted_offset;

                        shift_factor = *(pQ_format) - adjusted_pred_q;

                        IF ((shift_factor >= 0) && (shift_factor < 31))
                        THEN

                            shift_factor = shift_factor + 1;

                            FOR (j = quarter_sfb_width; j>0 ; j--)

                                *(pCurrent_frame++) =
                                    (*pCurrent_frame>>shift_factor) +
                                                (*(pPredicted_spectral++)>>1);
                                *(pCurrent_frame++) =
                                    (*pCurrent_frame>>shift_factor) +
                                                (*(pPredicted_spectral++)>>1);
                                *(pCurrent_frame++) =
                                    (*pCurrent_frame>>shift_factor) +
                                                (*(pPredicted_spectral++)>>1);
                                *(pCurrent_frame++) =
                                    (*pCurrent_frame>>shift_factor) +
                                                (*(pPredicted_spectral++)>>1);

                            ENDFOR

                            *(pQ_format) = adjusted_pred_q - 1;

                        ELSEIF (shift_factor >= 31)
                        THEN

                            FOR (j = quarter_sfb_width; j>0 ; j--)

                                *(pCurrent_frame++) = *(pPredicted_spectral++);
                                *(pCurrent_frame++) = *(pPredicted_spectral++);
                                *(pCurrent_frame++) = *(pPredicted_spectral++);
                                *(pCurrent_frame++) = *(pPredicted_spectral++);

                            ENDFOR

                            *(pQ_format) = adjusted_pred_q;

                        ELSEIF ((shift_factor < 0) && (shift_factor > -31))
                        THEN

                            shift_factor = 1 - shift_factor;

                            FOR (j = quarter_sfb_width; j>0 ; j--)

                                *(pCurrent_frame++) = (*pCurrent_frame>>1) +
                                    (*(pPredicted_spectral++)>>shift_factor);
                                *(pCurrent_frame++) = (*pCurrent_frame>>1) +
                                    (*(pPredicted_spectral++)>>shift_factor);
                                *(pCurrent_frame++) = (*pCurrent_frame>>1) +
                                    (*(pPredicted_spectral++)>>shift_factor);
                                *(pCurrent_frame++) = (*pCurrent_frame>>1) +
                                    (*(pPredicted_spectral++)>>shift_factor);

                            ENDFOR

                            *(pQ_format) = *(pQ_format) - 1;

                        ENDIF

                    ENDIF

                    sfb_offset = *pWinSfbTop;
                    pWinSfbTop = pWinSfbTop + 1;
                    pQ_format = pQ_format + 1;

                ENDFOR [ FOR (i = reconstruct_sfb_num; i > 0; i--) ]

            ENDIF [ IF (win_prediction_used[wnd] != FALSE) ]

            pPredicted_spectral_start = pPredicted_spectral_start + num_sfb;
            pCurrent_frame_start = pCurrent_frame_start + num_sfb;
            wnd_offset = wnd_offset + num_sfb;
            pQ_format_start = pQ_format_start + num_sfb;

        ENDFOR [ FOR (wnd=0; wnd<short_window_num; wnd++) ]

    ENDIF [ IF (win_seq != EIGHT_SHORT_SEQUENCE) ]

    RETURN

------------------------------------------------------------------------------
 RESOURCES USED
   When the code is written for a specific target processor the
     the resources used should be documented below.

 STACK USAGE: [stack count for this module] + [variable to represent
          stack usage for each subroutine called]

     where: [stack usage variable] = stack usage for [subroutine
         name] (see [filename].ext)

 DATA MEMORY USED: x words

 PROGRAM MEMORY USED: x words

 CLOCK CYCLES: [cycle count equation for this module] + [variable
           used to represent cycle count for each subroutine
           called]

     where: [cycle count variable] = cycle count for [subroutine
        name] (see [filename].ext)

------------------------------------------------------------------------------
*/


/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include "pv_audio_type_defs.h"
#include "e_window_sequence.h"
#include "long_term_synthesis.h"

/*----------------------------------------------------------------------------
; MACROS
; Define module specific macros here
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; LOCAL FUNCTION DEFINITIONS
; Function Prototype declaration
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; LOCAL STORE/BUFFER/POINTER DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; EXTERNAL FUNCTION REFERENCES
; Declare functions defined elsewhere and referenced in this module
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
; Declare variables used in this module but defined elsewhere
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/
void long_term_synthesis(
    WINDOW_SEQUENCE     win_seq,
    Int                 sfb_per_win,
    Int16               win_sfb_top[],
    Int                 win_prediction_used[],
    Int                 sfb_prediction_used[],
    Int32               current_frame[],
    Int                 q_format[],         /* for each sfb */
    Int32               predicted_spectral[],
    Int                 pred_q_format,      /* for predicted_spectral[] */
    Int                 coef_per_win,
    Int                 short_window_num,
    Int                 reconstruct_sfb_num)
{
    /*----------------------------------------------------------------------------
    ; Define all local variables
    ----------------------------------------------------------------------------*/
    /* Scalefactor band offset */
    Int sfb_offset;

    /* Window index */
    Int wnd;

    /* Pointer to array containing predicted samples */
    Int32 *pPredicted_spectral;

    /* Pointer to the beginning of array containing predicted samples */
    Int32 *pPredicted_spectral_start;

    Int32 *pPredicted_offset;

    /* Pointer to array containing current spectral components for a channel*/
    Int32 *pCurrent_frame;

    /* Another pointer to array containing current spectral components */
    Int32 *pCurrent_frame_start;

    /* Pointer to prediction flag for each scalefactor band */
    Int *pSfb_prediction_used;

    /* Pointer to top coef per scalefactor band */
    Int16 *pWinSfbTop;

    /* Pointer to q_format array */
    Int *pQ_format;
    Int *pQ_format_start;
    Int32   temp;

    Int i;
    Int j;

    Int quarter_sfb_width;
    Int num_sfb;
    Int shift_factor;

    UInt32  max;
    Int32   tmpInt32;

    Int tmpInt;
    Int adjusted_pred_q;
    Int pred_shift;

    /*----------------------------------------------------------------------------
    ; Function body here
    ----------------------------------------------------------------------------*/
    /* Initialize pointers */
    pPredicted_spectral = &predicted_spectral[0];
    pPredicted_spectral_start = pPredicted_spectral;

    /*
     * NOTE:
     * sfb_prediction_used[] start from 0 or 1 depending on nok_lt_decode.c;
     * currently we agree to make it start from 0;
     */
    pSfb_prediction_used = &sfb_prediction_used[0];

    /*********************************/
    /* LTP synthesis for long window */
    /*********************************/
    if (win_seq != EIGHT_SHORT_SEQUENCE)
    {

        /*******************************************************/
        /* Reconstruction of current frequency domain spectrum */
        /*******************************************************/

        /* Initialize scalefactor band offset */
        sfb_offset = 0;

        /*
         * Reconstruction is processed on scalefactor band basis.
         * 1. When prediction is turned on, all the predicted spectral
         * components will be used for reconstruction.
         * 2. When prediction is turned off, reconstruction is not
         * needed. Spectral components of current frame will directly
         * come from the transmitted data.
         */
        pWinSfbTop = &win_sfb_top[0];

        pQ_format = &q_format[0];

        for (i = sfb_per_win; i > 0; i--)
        {
            /* Check prediction flag for each scalefactor band. */
            if (*(pSfb_prediction_used++) != FALSE)
            {
                /*
                 * Prediction is on. Do reconstruction routine.
                 * Reconstruct spectral component of current
                 * frame by adding the predicted spectral
                 * components and the quantized prediction
                 * errors that reconstructed from transmitted
                 * data when prediction is turned on.
                 */

                /* Set pointers to the offset of scalefactor bands */
                pPredicted_offset = pPredicted_spectral_start +
                                    sfb_offset;
                pCurrent_frame = &current_frame[sfb_offset];

                /*
                 * (*pWinSfbTop - sfb_offset) is number of coefficients
                 * of the scalefactor band.
                 * ">>2" is used to set up for later unrolling the loop.
                 */
                quarter_sfb_width = (*pWinSfbTop - sfb_offset) >> 2;

                /*
                 * Adjust pred_q_format and predicted_spectral() to
                 * maximum resolution.
                 */
                max = 0;

                pPredicted_spectral = pPredicted_offset;

                /* Find the maximum absolute value */
                for (j = (*pWinSfbTop - sfb_offset); j > 0 ; j--)
                {
                    tmpInt32 = *(pPredicted_spectral++);

                    /*
                     * Note: overflow is protected here even though
                     * tmpInt32 = 0x80000000 is very rare case.
                     *
                     *  if (tmpInt32 == LONG_MIN)
                     *  {
                     *      tmpInt32 = LONG_MAX;
                     *  }
                     *  if (tmpInt32 < 0)
                     *  {
                     *      tmpInt32 = -tmpInt32;
                     *  }
                     */

                    max |= tmpInt32 ^(tmpInt32 >> 31);
                }

                /*
                 * IF the LTP data is all zeros
                 * (max == 0) - do nothing for this sfb.
                 */

                if (max != 0)
                {
                    /* Find the number of bits to reach the max resolution */
                    tmpInt = 0;

                    while (max < 0x40000000L)
                    {
                        max <<= 1;
                        tmpInt++;
                    }

                    /*
                     * The following codes are combinded into shift factor
                     * adjusting and reconstruction section.
                     *
                     * pPredicted_spectral = pPredicted_offset;
                     * for(j = quarter_sfb_width; j>0 ; j--)
                     * {
                     *      *(pPredicted_spectral++) <<= tmpInt;
                     *      *(pPredicted_spectral++) <<= tmpInt;
                     *      *(pPredicted_spectral++) <<= tmpInt;
                     *      *(pPredicted_spectral++) <<= tmpInt;
                     * }
                     *
                     */

                    /* Adjust Q format for predicted_spectral() */
                    adjusted_pred_q = pred_q_format + tmpInt;

                    /*
                     * Adjust Q format to prevent overflow that may occur during
                     * frequency domain reconstruction.
                     *
                     */
                    pPredicted_spectral = pPredicted_offset;

                    shift_factor = *(pQ_format) - adjusted_pred_q;

                    if ((shift_factor >= 0) && (shift_factor < 31))
                    {
                        shift_factor = shift_factor + 1;
                        pred_shift = tmpInt - 1;

                        if (pred_shift >= 0)
                        {
                            for (j = quarter_sfb_width; j > 0 ; j--)
                            {
                                temp = *pCurrent_frame >> shift_factor;
                                *(pCurrent_frame++) = temp
                                                      + (*(pPredicted_spectral++) << pred_shift);
                                temp = *pCurrent_frame >> shift_factor;
                                *(pCurrent_frame++) = temp
                                                      + (*(pPredicted_spectral++) << pred_shift);
                                temp = *pCurrent_frame >> shift_factor;
                                *(pCurrent_frame++) = temp
                                                      + (*(pPredicted_spectral++) << pred_shift);
                                temp = *pCurrent_frame >> shift_factor;
                                *(pCurrent_frame++) = temp
                                                      + (*(pPredicted_spectral++) << pred_shift);
                            }
                        }
                        else
                        {
                            for (j = quarter_sfb_width; j > 0 ; j--)
                            {
                                temp = *pCurrent_frame >> shift_factor;
                                *(pCurrent_frame++) = temp
                                                      + (*(pPredicted_spectral++) >> 1);
                                temp = *pCurrent_frame >> shift_factor;
                                *(pCurrent_frame++) = temp
                                                      + (*(pPredicted_spectral++) >> 1);
                                temp = *pCurrent_frame >> shift_factor;
                                *(pCurrent_frame++) = temp
                                                      + (*(pPredicted_spectral++) >> 1);
                                temp = *pCurrent_frame >> shift_factor;
                                *(pCurrent_frame++) = temp
                                                      + (*(pPredicted_spectral++) >> 1);
                            }
                        }

                        /* Updated new Q format for current scalefactor band */
                        *(pQ_format) = adjusted_pred_q  - 1;
                    }
                    else if (shift_factor >= 31)
                    {
                        for (j = quarter_sfb_width; j > 0 ; j--)
                        {
                            *(pCurrent_frame++) =
                                *(pPredicted_spectral++) << tmpInt;
                            *(pCurrent_frame++) =
                                *(pPredicted_spectral++) << tmpInt;
                            *(pCurrent_frame++) =
                                *(pPredicted_spectral++) << tmpInt;
                            *(pCurrent_frame++) =
                                *(pPredicted_spectral++) << tmpInt;
                        }

                        /* Updated new Q format for current scalefactor band */
                        *(pQ_format) = adjusted_pred_q ;
                    }
                    else if ((shift_factor < 0) && (shift_factor > -31))
                    {
                        shift_factor = 1 - shift_factor;
                        pred_shift = tmpInt - shift_factor;

                        if (pred_shift >= 0)
                        {
                            for (j = quarter_sfb_width; j > 0 ; j--)
                            {
                                temp = *pCurrent_frame >> 1;
                                *(pCurrent_frame++) =  temp +
                                                       (*(pPredicted_spectral++) << pred_shift);
                                temp = *pCurrent_frame >> 1;
                                *(pCurrent_frame++) =  temp +
                                                       (*(pPredicted_spectral++) << pred_shift);
                                temp = *pCurrent_frame >> 1;
                                *(pCurrent_frame++) =  temp +
                                                       (*(pPredicted_spectral++) << pred_shift);
                                temp = *pCurrent_frame >> 1;
                                *(pCurrent_frame++) =  temp +
                                                       (*(pPredicted_spectral++) << pred_shift);
                            }
                        }
                        else
                        {
                            pred_shift = -pred_shift;

                            for (j = quarter_sfb_width; j > 0 ; j--)
                            {
                                temp = *pCurrent_frame >> 1;
                                *(pCurrent_frame++) =  temp +
                                                       (*(pPredicted_spectral++) >> pred_shift);
                                temp = *pCurrent_frame >> 1;
                                *(pCurrent_frame++) =  temp +
                                                       (*(pPredicted_spectral++) >> pred_shift);
                                temp = *pCurrent_frame >> 1;
                                *(pCurrent_frame++) =  temp +
                                                       (*(pPredicted_spectral++) >> pred_shift);
                                temp = *pCurrent_frame >> 1;
                                *(pCurrent_frame++) =  temp +
                                                       (*(pPredicted_spectral++) >> pred_shift);
                            }
                        }

                        /*
                         * Updated new Q format for current scalefactor band
                         *
                         * This is NOT a pointer decrement
                         */
                        (*pQ_format)--;
                    }

                } /* if (max != 0) */

                /*
                 * For case (shift_factor <= -31), *pCurrent_frame and
                 * *pQ_format do not need to be updated.
                 */

            } /* if (*(pSfb_prediction_used++) != FALSE) */

            /* Updated to next scalefactor band. */
            sfb_offset = *(pWinSfbTop++);

            /* Updated pointer to next scalefactor band's Q-format */
            pQ_format++;

        } /* for (i = sfb_per_frame; i>0; i--) */

    } /* if (win_seq!=EIGHT_SHORT_SEQUENCE) */

    /**********************************/
    /* LTP synthesis for short window */
    /**********************************/
    else
    {
        /******************************************************/
        /*Reconstruction of current frequency domain spectrum */
        /******************************************************/
        pCurrent_frame_start = &current_frame[0];

        pQ_format_start = &q_format[0];

        num_sfb = sfb_per_win;

        /* Reconstruction is processed on window basis */
        for (wnd = 0; wnd < short_window_num; wnd++)
        {
            pWinSfbTop = &win_sfb_top[0];

            pQ_format = pQ_format_start;

            /* Check if prediction flag is on for each window */
            if (win_prediction_used[wnd] != FALSE)
            {
                /* Initialize scalefactor band offset */
                sfb_offset = 0;

                /*
                 * Reconstruction is processed on scalefactor band basis.
                 * 1. When prediction is turned on, all the predicted
                 * spectral components will be used for reconstruction.
                 * 2. When prediction is turned off, reconstruction is
                 * not needed. Spectral components of current frame
                 * will directly come from the transmitted data.
                 */

                /*
                 * According to ISO/IEC 14496-3 pg.91
                 * Only the spectral components in first eight scalefactor
                 * bands are added to the quantized prediction error.
                 */
                for (i = reconstruct_sfb_num; i > 0; i--)
                {
                    /* Set pointer to the offset of scalefactor bands */
                    pPredicted_offset = pPredicted_spectral_start +
                                        sfb_offset;
                    pCurrent_frame = pCurrent_frame_start + sfb_offset;

                    /*
                     * Prediction is on. Do reconstruction routine.
                     * Reconstruct spectral component of
                     * current frame by adding the predicted
                     * spectral components and the quantized
                     * prediction errors that reconstructed
                     * from transmitted data when prediction
                     * is turned on.
                     */

                    /*
                     * (*pWinSfbTop - sfb_offset) is number of coefficients
                     * of the scalefactor band.
                     * ">>2" is used to set up for later unrolling the loop.
                     */
                    quarter_sfb_width = (*pWinSfbTop - sfb_offset) >> 2;

                    /*
                     * Adjust pred_q_format and predicted_spectral() to
                     * maximum resolution.
                     */
                    max = 0;
                    pPredicted_spectral = pPredicted_offset;

                    /* Find the maximum absolute value */
                    for (j = (*pWinSfbTop - sfb_offset); j > 0 ; j--)
                    {
                        tmpInt32 = *(pPredicted_spectral++);


                        /*
                         * Note: overflow is protected here even though
                         * tmpInt32 = 0x80000000 is very rare case.
                         *
                         *  if (tmpInt32 == LONG_MIN)
                         *  {
                         *      tmpInt32 = LONG_MAX;
                         *  }
                         *  if (tmpInt32 < 0)
                         *  {
                         *      tmpInt32 = -tmpInt32;
                         *  }
                         */

                        max |= tmpInt32 ^(tmpInt32 >> 31);
                    }

                    if (max != 0)
                    {
                        /* Find the number of bits to reach
                         * the max resolution
                         */
                        tmpInt = 0;

                        while (max < 0x40000000L)
                        {
                            max <<= 1;
                            tmpInt++;
                        }
                        /*
                         * The following codes are combined into shift factor
                         * adjusting and reconstruction section.
                         *
                         * pPredicted_spectral = pPredicted_offset;
                         * for(j = quarter_sfb_width; j>0 ; j--)
                         * {
                         *      *(pPredicted_spectral++) <<= tmpInt;
                         *      *(pPredicted_spectral++) <<= tmpInt;
                         *      *(pPredicted_spectral++) <<= tmpInt;
                         *      *(pPredicted_spectral++) <<= tmpInt;
                         * }
                         *
                         */

                        /* Adjust Q format for predicted_spectral() */
                        adjusted_pred_q = pred_q_format + tmpInt;

                        /*
                         * Adjust Q format to prevent overflow that may occur
                         * during frequency domain reconstruction.
                         */
                        pPredicted_spectral = pPredicted_offset;

                        shift_factor = *(pQ_format) - adjusted_pred_q;

                        if ((shift_factor >= 0) && (shift_factor < 31))
                        {
                            shift_factor = shift_factor + 1;

                            pred_shift = tmpInt - 1;

                            if (pred_shift >= 0)
                            {
                                for (j = quarter_sfb_width; j > 0 ; j--)
                                {
                                    temp = *pCurrent_frame >> shift_factor;
                                    *(pCurrent_frame++) = temp
                                                          + (*(pPredicted_spectral++) << pred_shift);
                                    temp = *pCurrent_frame >> shift_factor;
                                    *(pCurrent_frame++) = temp
                                                          + (*(pPredicted_spectral++) << pred_shift);
                                    temp = *pCurrent_frame >> shift_factor;
                                    *(pCurrent_frame++) = temp
                                                          + (*(pPredicted_spectral++) << pred_shift);
                                    temp = *pCurrent_frame >> shift_factor;
                                    *(pCurrent_frame++) = temp
                                                          + (*(pPredicted_spectral++) << pred_shift);

                                }
                            }
                            else
                            {
                                for (j = quarter_sfb_width; j > 0 ; j--)
                                {
                                    temp = *pCurrent_frame >> shift_factor;
                                    *(pCurrent_frame++) = temp
                                                          + (*(pPredicted_spectral++) >> 1);
                                    temp = *pCurrent_frame >> shift_factor;
                                    *(pCurrent_frame++) = temp
                                                          + (*(pPredicted_spectral++) >> 1);
                                    temp = *pCurrent_frame >> shift_factor;
                                    *(pCurrent_frame++) = temp
                                                          + (*(pPredicted_spectral++) >> 1);
                                    temp = *pCurrent_frame >> shift_factor;
                                    *(pCurrent_frame++) = temp
                                                          + (*(pPredicted_spectral++) >> 1);
                                }
                            }

                            /* Updated new Q format for current scalefactor band*/
                            *(pQ_format) = adjusted_pred_q - 1;
                        }
                        else if (shift_factor >= 31)
                        {
                            for (j = quarter_sfb_width; j > 0 ; j--)
                            {
                                *(pCurrent_frame++) =
                                    *(pPredicted_spectral++) << tmpInt;
                                *(pCurrent_frame++) =
                                    *(pPredicted_spectral++) << tmpInt;
                                *(pCurrent_frame++) =
                                    *(pPredicted_spectral++) << tmpInt;
                                *(pCurrent_frame++) =
                                    *(pPredicted_spectral++) << tmpInt;
                            }

                            /* Updated new Q format for current scalefactor band*/
                            *(pQ_format) = adjusted_pred_q;
                        }
                        else if ((shift_factor < 0) && (shift_factor > -31))
                        {
                            shift_factor = 1 - shift_factor;

                            pred_shift = tmpInt - shift_factor;

                            if (pred_shift >= 0)
                            {
                                for (j = quarter_sfb_width; j > 0 ; j--)
                                {
                                    temp = *pCurrent_frame >> 1;
                                    *(pCurrent_frame++) =  temp +
                                                           (*(pPredicted_spectral++) << pred_shift);
                                    temp = *pCurrent_frame >> 1;
                                    *(pCurrent_frame++) =  temp +
                                                           (*(pPredicted_spectral++) << pred_shift);
                                    temp = *pCurrent_frame >> 1;
                                    *(pCurrent_frame++) =  temp +
                                                           (*(pPredicted_spectral++) << pred_shift);
                                    temp = *pCurrent_frame >> 1;
                                    *(pCurrent_frame++) =  temp +
                                                           (*(pPredicted_spectral++) << pred_shift);

                                }
                            }
                            else
                            {
                                pred_shift = -pred_shift;

                                for (j = quarter_sfb_width; j > 0 ; j--)
                                {
                                    temp = *pCurrent_frame >> 1;
                                    *(pCurrent_frame++) =  temp +
                                                           (*(pPredicted_spectral++) >> pred_shift);
                                    temp = *pCurrent_frame >> 1;
                                    *(pCurrent_frame++) =  temp +
                                                           (*(pPredicted_spectral++) >> pred_shift);
                                    temp = *pCurrent_frame >> 1;
                                    *(pCurrent_frame++) =  temp +
                                                           (*(pPredicted_spectral++) >> pred_shift);
                                    temp = *pCurrent_frame >> 1;
                                    *(pCurrent_frame++) =  temp +
                                                           (*(pPredicted_spectral++) >> pred_shift);
                                }
                            }

                            /* Updated new Q format for current scalefactor band*/
                            *(pQ_format) = *(pQ_format) - 1;
                        }

                        /*
                         * For case (shift_factor <= -31), *pCurrent_frame and
                         * *pQ_format do not need to be updated.
                         */

                    } /* if (max != 0) */

                    /* Updated to next scalefactor band. */
                    sfb_offset = *(pWinSfbTop++);

                    /* Updated pointer to next scalefactor band's Q-format */
                    pQ_format++;

                } /* for (i = reconstruct_sfb_num; i > 0; i--) */

            } /* if (win_prediction_used[wnd] != FALSE) */

            /* Updated to next window */
            pPredicted_spectral_start += coef_per_win;
            pCurrent_frame_start += coef_per_win;
            pQ_format_start += num_sfb;

        } /* for (wnd=0; wnd<short_window_num; wnd++) */

    } /* else */

    /*----------------------------------------------------------------------------
    ; Return nothing or data or data pointer
    ----------------------------------------------------------------------------*/
    return;
} /* long_term_synthesis */