summaryrefslogtreecommitdiffstats
path: root/libSBRdec/src/sbr_dec.cpp
blob: 208120c8827201b5fade46643f0f5c600951f661 (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

/* -----------------------------------------------------------------------------------------------------------
Software License for The Fraunhofer FDK AAC Codec Library for Android

© Copyright  1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
  All rights reserved.

 1.    INTRODUCTION
The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
This FDK AAC Codec software is intended to be used on a wide variety of Android devices.

AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
of the MPEG specifications.

Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
individually for the purpose of encoding or decoding bit streams in products that are compliant with
the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
software may already be covered under those patent licenses when it is used for those licensed purposes only.

Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
applications information and documentation.

2.    COPYRIGHT LICENSE

Redistribution and use in source and binary forms, with or without modification, are permitted without
payment of copyright license fees provided that you satisfy the following conditions:

You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
your modifications thereto in source code form.

You must retain the complete text of this software license in the documentation and/or other materials
provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
modifications thereto to recipients of copies in binary form.

The name of Fraunhofer may not be used to endorse or promote products derived from this library without
prior written permission.

You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
software or your modifications thereto.

Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
and the date of any change. For modified versions of the FDK AAC Codec, the term
"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."

3.    NO PATENT LICENSE

NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
respect to this software.

You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
by appropriate patent licenses.

4.    DISCLAIMER

This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
or business interruption, however caused and on any theory of liability, whether in contract, strict
liability, or tort (including negligence), arising in any way out of the use of this software, even if
advised of the possibility of such damage.

5.    CONTACT INFORMATION

Fraunhofer Institute for Integrated Circuits IIS
Attention: Audio and Multimedia Departments - FDK AAC LL
Am Wolfsmantel 33
91058 Erlangen, Germany

www.iis.fraunhofer.de/amm
amm-info@iis.fraunhofer.de
----------------------------------------------------------------------------------------------------------- */

/*!
  \file
  \brief  Sbr decoder  
  This module provides the actual decoder implementation. The SBR data (side information) is already
  decoded. Only three functions are provided:

  \li 1.) createSbrDec(): One time initialization
  \li 2.) resetSbrDec(): Called by sbr_Apply() when the information contained in an SBR_HEADER_ELEMENT requires a reset
  and recalculation of important SBR structures.
  \li 3.) sbr_dec(): The actual decoder. Calls the different tools such as filterbanks, lppTransposer(), and calculateSbrEnvelope()
  [the envelope adjuster].

  \sa sbr_dec(), \ref documentationOverview
*/

#include "sbr_dec.h"

#include "sbr_ram.h"
#include "env_extr.h"
#include "env_calc.h"
#include "scale.h"

#include "genericStds.h"

#include "sbrdec_drc.h"



static void assignLcTimeSlots( HANDLE_SBR_DEC hSbrDec,                     /*!< handle to Decoder channel */
                               FIXP_DBL  **QmfBufferReal,
                               int noCols )
{
  int slot, i;
  FIXP_DBL  *ptr;

  /* Number of QMF timeslots in the overlap buffer: */
  ptr = hSbrDec->pSbrOverlapBuffer;
  for(slot=0; slot<hSbrDec->LppTrans.pSettings->overlap; slot++) {
    QmfBufferReal[slot] = ptr; ptr += (64);
  }

  /* Assign timeslots to Workbuffer1 */
  ptr = hSbrDec->WorkBuffer1;
  for(i=0; i<noCols; i++) {
    QmfBufferReal[slot] = ptr; ptr += (64);
    slot++;
  }
}


static void assignHqTimeSlots( HANDLE_SBR_DEC hSbrDec,                     /*!< handle to Decoder channel */
                               FIXP_DBL  **QmfBufferReal,
                               FIXP_DBL  **QmfBufferImag,
                               int noCols )
{
  FIXP_DBL  *ptr;
  int slot;

  /* Number of QMF timeslots in one half of a frame (size of Workbuffer1 or 2): */
  int halflen = (noCols >> 1) + hSbrDec->LppTrans.pSettings->overlap;
  int totCols = noCols + hSbrDec->LppTrans.pSettings->overlap;

  /* Number of QMF timeslots in the overlap buffer: */
  ptr = hSbrDec->pSbrOverlapBuffer;
  for(slot=0; slot<hSbrDec->LppTrans.pSettings->overlap; slot++) {
    QmfBufferReal[slot] = ptr; ptr += (64);
    QmfBufferImag[slot] = ptr; ptr += (64);
  }

  /* Assign first half of timeslots to Workbuffer1 */
  ptr = hSbrDec->WorkBuffer1;
  for(; slot<halflen; slot++) {
    QmfBufferReal[slot] = ptr; ptr += (64);
    QmfBufferImag[slot] = ptr; ptr += (64);
  }

  /* Assign second half of timeslots to Workbuffer2 */
  ptr = hSbrDec->WorkBuffer2;
  for(; slot<totCols; slot++) {
    QmfBufferReal[slot] = ptr; ptr += (64);
    QmfBufferImag[slot] = ptr; ptr += (64);
  }
}


static void assignTimeSlots( HANDLE_SBR_DEC hSbrDec,                     /*!< handle to Decoder channel */
                             int noCols,
                             int useLP )
{
 /* assign qmf time slots */
  hSbrDec->useLP = useLP;
  if (useLP) {
    hSbrDec->SynthesisQMF.flags |= QMF_FLAG_LP;
    hSbrDec->AnalysiscQMF.flags |= QMF_FLAG_LP;
  } else {
    hSbrDec->SynthesisQMF.flags &= ~QMF_FLAG_LP;
    hSbrDec->AnalysiscQMF.flags &= ~QMF_FLAG_LP;
  }
  if (!useLP)
    assignHqTimeSlots( hSbrDec, hSbrDec->QmfBufferReal, hSbrDec->QmfBufferImag, noCols );
  else
  {
    assignLcTimeSlots( hSbrDec, hSbrDec->QmfBufferReal, noCols );
  }
}

static void changeQmfType( HANDLE_SBR_DEC hSbrDec,                     /*!< handle to Decoder channel */
                           int useLdTimeAlign )
{
  UINT synQmfFlags = hSbrDec->SynthesisQMF.flags;
  UINT anaQmfFlags = hSbrDec->AnalysiscQMF.flags;
  int  resetSynQmf = 0;
  int  resetAnaQmf = 0;

  /* assign qmf type */
  if (useLdTimeAlign) {
    if (synQmfFlags & QMF_FLAG_CLDFB) {
      /* change the type to MPSLD */
      synQmfFlags &= ~QMF_FLAG_CLDFB;
      synQmfFlags |=  QMF_FLAG_MPSLDFB;
      resetSynQmf = 1;
    }
    if (anaQmfFlags & QMF_FLAG_CLDFB) {
      /* change the type to MPSLD */
      anaQmfFlags &= ~QMF_FLAG_CLDFB;
      anaQmfFlags |=  QMF_FLAG_MPSLDFB;
      resetAnaQmf = 1;
    }
  } else {
    if (synQmfFlags & QMF_FLAG_MPSLDFB) {
      /* change the type to CLDFB */
      synQmfFlags &= ~QMF_FLAG_MPSLDFB;
      synQmfFlags |=  QMF_FLAG_CLDFB;
      resetSynQmf = 1;
    }
    if (anaQmfFlags & QMF_FLAG_MPSLDFB) {
      /* change the type to CLDFB */
      anaQmfFlags &= ~QMF_FLAG_MPSLDFB;
      anaQmfFlags |=  QMF_FLAG_CLDFB;
      resetAnaQmf = 1;
    }
  }

  if (resetAnaQmf) {
    int qmfErr = qmfInitAnalysisFilterBank (
           &hSbrDec->AnalysiscQMF,
            hSbrDec->anaQmfStates,
            hSbrDec->AnalysiscQMF.no_col,
            hSbrDec->AnalysiscQMF.lsb,
            hSbrDec->AnalysiscQMF.usb,
            hSbrDec->AnalysiscQMF.no_channels,
            anaQmfFlags | QMF_FLAG_KEEP_STATES
            );
    if (qmfErr != 0) {
      FDK_ASSERT(0);
    }
  }

  if (resetSynQmf) {
    int qmfErr = qmfInitSynthesisFilterBank (
           &hSbrDec->SynthesisQMF,
            hSbrDec->pSynQmfStates,
            hSbrDec->SynthesisQMF.no_col,
            hSbrDec->SynthesisQMF.lsb,
            hSbrDec->SynthesisQMF.usb,
            hSbrDec->SynthesisQMF.no_channels,
            synQmfFlags | QMF_FLAG_KEEP_STATES
            );

    if (qmfErr != 0) {
      FDK_ASSERT(0);
    }
  }
}


/*!
  \brief      SBR decoder core function for one channel

  \image html  BufferMgmtDetailed-1632.png

  Besides the filter states of the QMF filter bank and the LPC-states of
  the LPP-Transposer, processing is mainly based on four buffers:
  #timeIn, #timeOut, #WorkBuffer2 and #OverlapBuffer. The #WorkBuffer2
  is reused for all channels and might be used by the core decoder, a
  static overlap buffer is required for each channel. Du to in-place
  processing, #timeIn and #timeOut point to identical locations.

  The spectral data is organized in so-called slots, each slot
  containing 64 bands of complex data. The number of slots per frame is
  dependend on the frame size. For mp3PRO, there are 18 slots per frame
  and 6 slots per #OverlapBuffer. It is not necessary to have the slots
  in located consecutive address ranges.

  To optimize memory usage and to minimize the number of memory
  accesses, the memory management is organized as follows (Slot numbers
  based on mp3PRO):

  1.) Input time domain signal is located in #timeIn, the last slots
  (0..5) of the spectral data of the previous frame are located in the
  #OverlapBuffer. In addition, #frameData of the current frame resides
  in the upper part of #timeIn.

  2.) During the cplxAnalysisQmfFiltering(), 32 samples from #timeIn are transformed
  into a slot of up to 32 complex spectral low band values at a
  time. The first spectral slot -- nr. 6 -- is written at slot number
  zero of #WorkBuffer2. #WorkBuffer2 will be completely filled with
  spectral data.

  3.) LPP-Transposition in lppTransposer() is processed on 24 slots. During the
  transposition, the high band part of the spectral data is replicated
  based on the low band data.

  Envelope Adjustment is processed on the high band part of the spectral
  data only by calculateSbrEnvelope().

  4.) The cplxSynthesisQmfFiltering() creates 64 time domain samples out
  of a slot of 64 complex spectral values at a time. The first 6 slots
  in #timeOut are filled from the results of spectral slots 0..5 in the
  #OverlapBuffer. The consecutive slots in timeOut are now filled with
  the results of spectral slots 6..17.

  5.) The preprocessed slots 18..23 have to be stored in the
  #OverlapBuffer.

*/

void
sbr_dec ( HANDLE_SBR_DEC hSbrDec,            /*!< handle to Decoder channel */
          INT_PCM *timeIn,                   /*!< pointer to input time signal */
          INT_PCM *timeOut,                  /*!< pointer to output time signal */
          HANDLE_SBR_DEC hSbrDecRight,       /*!< handle to Decoder channel right */
          INT_PCM *timeOutRight,             /*!< pointer to output time signal */
          const int strideIn,                /*!< Time data traversal strideIn */
          const int strideOut,               /*!< Time data traversal strideOut */
          HANDLE_SBR_HEADER_DATA hHeaderData,/*!< Static control data */
          HANDLE_SBR_FRAME_DATA hFrameData,  /*!< Control data of current frame */
          HANDLE_SBR_PREV_FRAME_DATA hPrevFrameData,  /*!< Some control data of last frame */
          const int applyProcessing,         /*!< Flag for SBR operation */
          HANDLE_PS_DEC h_ps_d,
          const UINT flags
         )
{
  int i, slot, reserve;
  int saveLbScale;
  int ov_len;
  int lastSlotOffs;
  FIXP_DBL maxVal;

  /* 1+1/3 frames of spectral data: */
  FIXP_DBL **QmfBufferReal = hSbrDec->QmfBufferReal;
  FIXP_DBL **QmfBufferImag = hSbrDec->QmfBufferImag;

 /* Number of QMF timeslots in the overlap buffer: */
 ov_len = hSbrDec->LppTrans.pSettings->overlap;

 /* Number of QMF slots per frame */
  int noCols = hHeaderData->numberTimeSlots * hHeaderData->timeStep;

 /* assign qmf time slots */
  if ( ((flags & SBRDEC_LOW_POWER ) ? 1 : 0) != ((hSbrDec->SynthesisQMF.flags & QMF_FLAG_LP) ? 1 : 0) ) {
    assignTimeSlots( hSbrDec, hHeaderData->numberTimeSlots * hHeaderData->timeStep, flags & SBRDEC_LOW_POWER);
  }

  if (flags & SBRDEC_ELD_GRID) {
    /* Choose the right low delay filter bank */
    changeQmfType( hSbrDec, (flags & SBRDEC_LD_MPS_QMF) ? 1 : 0 );
  }

  /*
    low band codec signal subband filtering
   */

  {
    C_AALLOC_SCRATCH_START(qmfTemp, FIXP_DBL, 2*(64));

    qmfAnalysisFiltering( &hSbrDec->AnalysiscQMF,
                           QmfBufferReal + ov_len,
                           QmfBufferImag + ov_len,
                          &hSbrDec->sbrScaleFactor,
                           timeIn,
                           strideIn,
                           qmfTemp
                         );

    C_AALLOC_SCRATCH_END(qmfTemp, FIXP_DBL, 2*(64));
  }

  /*
    Clear upper half of spectrum
  */
  {
    int nAnalysisBands = hHeaderData->numberOfAnalysisBands;

    if (! (flags & SBRDEC_LOW_POWER)) {
      for (slot = ov_len; slot < noCols+ov_len; slot++) {
        FDKmemclear(&QmfBufferReal[slot][nAnalysisBands],((64)-nAnalysisBands)*sizeof(FIXP_DBL));
        FDKmemclear(&QmfBufferImag[slot][nAnalysisBands],((64)-nAnalysisBands)*sizeof(FIXP_DBL));
      }
    } else
    for (slot = ov_len; slot < noCols+ov_len; slot++) {
      FDKmemclear(&QmfBufferReal[slot][nAnalysisBands],((64)-nAnalysisBands)*sizeof(FIXP_DBL));
    }
  }



  /*
    Shift spectral data left to gain accuracy in transposer and adjustor
  */
  maxVal = maxSubbandSample( QmfBufferReal,
                            (flags & SBRDEC_LOW_POWER) ? NULL : QmfBufferImag,
                             0,
                             hSbrDec->AnalysiscQMF.lsb,
                             ov_len,
                             noCols+ov_len );

  reserve = fixMax(0,CntLeadingZeros(maxVal)-1) ;
  reserve = fixMin(reserve,DFRACT_BITS-1-hSbrDec->sbrScaleFactor.lb_scale);

  /* If all data is zero, lb_scale could become too large */
  rescaleSubbandSamples( QmfBufferReal,
                         (flags & SBRDEC_LOW_POWER) ? NULL : QmfBufferImag,
                         0,
                         hSbrDec->AnalysiscQMF.lsb,
                         ov_len,
                         noCols+ov_len,
                         reserve);

  hSbrDec->sbrScaleFactor.lb_scale += reserve;

  /*
    save low band scale, wavecoding or parametric stereo may modify it
  */
  saveLbScale = hSbrDec->sbrScaleFactor.lb_scale;


  if (applyProcessing)
  {
    UCHAR * borders = hFrameData->frameInfo.borders;
    lastSlotOffs =  borders[hFrameData->frameInfo.nEnvelopes] - hHeaderData->numberTimeSlots;

    FIXP_DBL degreeAlias[(64)];

    /* The transposer will override most values in degreeAlias[].
       The array needs to be cleared at least from lowSubband to highSubband before. */
    if (flags & SBRDEC_LOW_POWER)
      FDKmemclear(&degreeAlias[hHeaderData->freqBandData.lowSubband], (hHeaderData->freqBandData.highSubband-hHeaderData->freqBandData.lowSubband)*sizeof(FIXP_DBL));

    /*
      Inverse filtering of lowband and transposition into the SBR-frequency range
    */

    lppTransposer ( &hSbrDec->LppTrans,
                    &hSbrDec->sbrScaleFactor,
                    QmfBufferReal,
                    degreeAlias,                  // only used if useLP = 1
                    QmfBufferImag,
                    flags & SBRDEC_LOW_POWER,
                    hHeaderData->timeStep,
                    borders[0],
                    lastSlotOffs,
                    hHeaderData->freqBandData.nInvfBands,
                    hFrameData->sbr_invf_mode,
                    hPrevFrameData->sbr_invf_mode );





    /*
      Adjust envelope of current frame.
    */

    calculateSbrEnvelope (&hSbrDec->sbrScaleFactor,
                          &hSbrDec->SbrCalculateEnvelope,
                          hHeaderData,
                          hFrameData,
                          QmfBufferReal,
                          QmfBufferImag,
                          flags & SBRDEC_LOW_POWER,

                          degreeAlias,
                          flags,
                          (hHeaderData->frameErrorFlag || hPrevFrameData->frameErrorFlag));


    /*
      Update hPrevFrameData (to be used in the next frame)
    */
    for (i=0; i<hHeaderData->freqBandData.nInvfBands; i++) {
      hPrevFrameData->sbr_invf_mode[i] = hFrameData->sbr_invf_mode[i];
    }
    hPrevFrameData->coupling = hFrameData->coupling;
    hPrevFrameData->stopPos = borders[hFrameData->frameInfo.nEnvelopes];
    hPrevFrameData->ampRes = hFrameData->ampResolutionCurrentFrame;
  }
  else {
    /* Reset hb_scale if no highband is present, because hb_scale is considered in the QMF-synthesis */
    hSbrDec->sbrScaleFactor.hb_scale = saveLbScale;
  }


  for (i=0; i<LPC_ORDER; i++){
    /*
      Store the unmodified qmf Slots values (required for LPC filtering)
    */
    if (! (flags & SBRDEC_LOW_POWER)) {
      FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesReal[i], QmfBufferReal[noCols-LPC_ORDER+i], hSbrDec->AnalysiscQMF.lsb*sizeof(FIXP_DBL));
      FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesImag[i], QmfBufferImag[noCols-LPC_ORDER+i], hSbrDec->AnalysiscQMF.lsb*sizeof(FIXP_DBL));
    } else
    FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesReal[i], QmfBufferReal[noCols-LPC_ORDER+i], hSbrDec->AnalysiscQMF.lsb*sizeof(FIXP_DBL));
  }

  /*
    Synthesis subband filtering.
  */

  if ( ! (flags & SBRDEC_PS_DECODED) ) {

    {
      int outScalefactor = 0;

      if (h_ps_d != NULL) {
        h_ps_d->procFrameBased = 1;  /* we here do frame based processing */
      }


      sbrDecoder_drcApply(&hSbrDec->sbrDrcChannel,
                           QmfBufferReal,
                           (flags & SBRDEC_LOW_POWER) ? NULL : QmfBufferImag,
                           hSbrDec->SynthesisQMF.no_col,
                          &outScalefactor
                          );



      qmfChangeOutScalefactor(&hSbrDec->SynthesisQMF, outScalefactor );

      {
        C_AALLOC_SCRATCH_START(qmfTemp, FIXP_DBL, 2*(64));

        qmfSynthesisFiltering( &hSbrDec->SynthesisQMF,
                                QmfBufferReal,
                                (flags & SBRDEC_LOW_POWER) ? NULL : QmfBufferImag,
                               &hSbrDec->sbrScaleFactor,
                                hSbrDec->LppTrans.pSettings->overlap,
                                timeOut,
                                strideOut,
                                qmfTemp);

        C_AALLOC_SCRATCH_END(qmfTemp, FIXP_DBL, 2*(64));
      }

    }

  } else { /* (flags & SBRDEC_PS_DECODED) */
    INT i, sdiff, outScalefactor, scaleFactorLowBand, scaleFactorHighBand;
    SCHAR scaleFactorLowBand_ov, scaleFactorLowBand_no_ov;

    HANDLE_QMF_FILTER_BANK synQmf      = &hSbrDec->SynthesisQMF;
    HANDLE_QMF_FILTER_BANK synQmfRight = &hSbrDecRight->SynthesisQMF;

    /* adapt scaling */
    sdiff = hSbrDec->sbrScaleFactor.lb_scale - reserve;                  /* Scaling difference         */
    scaleFactorHighBand   = sdiff - hSbrDec->sbrScaleFactor.hb_scale;    /* Scale of current high band */
    scaleFactorLowBand_ov = sdiff - hSbrDec->sbrScaleFactor.ov_lb_scale; /* Scale of low band overlapping QMF data */
    scaleFactorLowBand_no_ov = sdiff - hSbrDec->sbrScaleFactor.lb_scale; /* Scale of low band current QMF data     */
    outScalefactor  = 0;                                                 /* Initial output scale */

    if (h_ps_d->procFrameBased == 1)    /* If we have switched from frame to slot based processing copy filter states */
    { /* procFrameBased will be unset later */
      /* copy filter states from left to right */
      FDKmemcpy(synQmfRight->FilterStates, synQmf->FilterStates, ((640)-(64))*sizeof(FIXP_QSS));
    }

    /* scale ALL qmf vales ( real and imag ) of mono / left channel to the
       same scale factor ( ov_lb_sf, lb_sf and hq_sf )                      */
    scalFilterBankValues( h_ps_d,                             /* parametric stereo decoder handle     */
                          QmfBufferReal,                      /* qmf filterbank values                */
                          QmfBufferImag,                      /* qmf filterbank values                */
                          synQmf->lsb,                        /* sbr start subband                    */
                          hSbrDec->sbrScaleFactor.ov_lb_scale,
                          hSbrDec->sbrScaleFactor.lb_scale,
                         &scaleFactorLowBand_ov,              /* adapt scaling values */
                         &scaleFactorLowBand_no_ov,           /* adapt scaling values */
                          hSbrDec->sbrScaleFactor.hb_scale,   /* current frame ( highband ) */
                         &scaleFactorHighBand,
                          synQmf->no_col);

    /* use the same synthese qmf values for left and right channel */
    synQmfRight->no_col = synQmf->no_col;
    synQmfRight->lsb    = synQmf->lsb;
    synQmfRight->usb    = synQmf->usb;

    int env=0;

      outScalefactor += (SCAL_HEADROOM+1); /* psDiffScale! */

    {
      C_ALLOC_SCRATCH_START(pWorkBuffer, FIXP_DBL, 2*(64));

      int maxShift = 0;

      if (hSbrDec->sbrDrcChannel.prevFact_exp > maxShift) {
        maxShift = hSbrDec->sbrDrcChannel.prevFact_exp;
      }
      if (hSbrDec->sbrDrcChannel.currFact_exp > maxShift) {
        maxShift = hSbrDec->sbrDrcChannel.currFact_exp;
      }
      if (hSbrDec->sbrDrcChannel.nextFact_exp > maxShift) {
        maxShift = hSbrDec->sbrDrcChannel.nextFact_exp;
      }

      /* copy DRC data to right channel (with PS both channels use the same DRC gains) */
      FDKmemcpy(&hSbrDecRight->sbrDrcChannel, &hSbrDec->sbrDrcChannel, sizeof(SBRDEC_DRC_CHANNEL));

      for (i = 0; i < synQmf->no_col; i++) {  /* ----- no_col loop ----- */

        INT outScalefactorR, outScalefactorL;
        outScalefactorR = outScalefactorL = outScalefactor;

        /* qmf timeslot of right channel */
        FIXP_DBL* rQmfReal = pWorkBuffer;
        FIXP_DBL* rQmfImag = pWorkBuffer + 64;


        {
          if ( i == h_ps_d->bsData[h_ps_d->processSlot].mpeg.aEnvStartStop[env] ) {
            initSlotBasedRotation( h_ps_d, env, hHeaderData->freqBandData.highSubband );
            env++;
          }

          ApplyPsSlot( h_ps_d,                   /* parametric stereo decoder handle  */
                      (QmfBufferReal + i),       /* one timeslot of left/mono channel */
                      (QmfBufferImag + i),       /* one timeslot of left/mono channel */
                       rQmfReal,                 /* one timeslot or right channel     */
                       rQmfImag);                /* one timeslot or right channel     */
        }


        scaleFactorLowBand = (i<(6)) ? scaleFactorLowBand_ov : scaleFactorLowBand_no_ov;


        sbrDecoder_drcApplySlot ( /* right channel */
                                 &hSbrDecRight->sbrDrcChannel,
                                  rQmfReal,
                                  rQmfImag,
                                  i,
                                  synQmfRight->no_col,
                                  maxShift
                                );

        outScalefactorR += maxShift;

        sbrDecoder_drcApplySlot ( /* left channel */
                                 &hSbrDec->sbrDrcChannel,
                                 *(QmfBufferReal + i),
                                 *(QmfBufferImag + i),
                                  i,
                                  synQmf->no_col,
                                  maxShift
                                );

        outScalefactorL += maxShift;


        /* scale filter states for left and right channel */
        qmfChangeOutScalefactor( synQmf, outScalefactorL );
        qmfChangeOutScalefactor( synQmfRight, outScalefactorR );

        {

          qmfSynthesisFilteringSlot( synQmfRight,
                                     rQmfReal,                /* QMF real buffer */
                                     rQmfImag,                /* QMF imag buffer */
                                     scaleFactorLowBand,
                                     scaleFactorHighBand,
                                     timeOutRight+(i*synQmf->no_channels*strideOut),
                                     strideOut,
                                     pWorkBuffer);

          qmfSynthesisFilteringSlot( synQmf,
                                   *(QmfBufferReal + i),      /* QMF real buffer */
                                   *(QmfBufferImag + i),      /* QMF imag buffer */
                                     scaleFactorLowBand,
                                     scaleFactorHighBand,
                                     timeOut+(i*synQmf->no_channels*strideOut),
                                     strideOut,
                                     pWorkBuffer);

        }
      } /* no_col loop  i  */

      /* scale back (6) timeslots look ahead for hybrid filterbank to original value */
      rescalFilterBankValues( h_ps_d,
                              QmfBufferReal,
                              QmfBufferImag,
                              synQmf->lsb,
                              synQmf->no_col );

      C_ALLOC_SCRATCH_END(pWorkBuffer, FIXP_DBL, 2*(64));
    }
  }

  sbrDecoder_drcUpdateChannel( &hSbrDec->sbrDrcChannel );


  /*
    Update overlap buffer
    Even bands above usb are copied to avoid outdated spectral data in case
    the stop frequency raises.
  */

  if (hSbrDec->LppTrans.pSettings->overlap > 0)
  {
    if (! (flags & SBRDEC_LOW_POWER)) {
      for ( i=0; i<hSbrDec->LppTrans.pSettings->overlap; i++ ) {
        FDKmemcpy(QmfBufferReal[i], QmfBufferReal[i+noCols], (64)*sizeof(FIXP_DBL));
        FDKmemcpy(QmfBufferImag[i], QmfBufferImag[i+noCols], (64)*sizeof(FIXP_DBL));
      }
    } else
      for ( i=0; i<hSbrDec->LppTrans.pSettings->overlap; i++ ) {
        FDKmemcpy(QmfBufferReal[i], QmfBufferReal[i+noCols], (64)*sizeof(FIXP_DBL));
      }
  }

  hSbrDec->sbrScaleFactor.ov_lb_scale = saveLbScale;

  /* Save current frame status */
  hPrevFrameData->frameErrorFlag = hHeaderData->frameErrorFlag;

} // sbr_dec()


/*!
  \brief     Creates sbr decoder structure
  \return    errorCode, 0 if successful
*/
SBR_ERROR
createSbrDec (SBR_CHANNEL * hSbrChannel,
              HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */
              TRANSPOSER_SETTINGS *pSettings,
              const int     downsampleFac,        /*!< Downsampling factor */
              const UINT    qmfFlags,             /*!< flags -> 1: HQ/LP selector, 2: CLDFB */
              const UINT    flags,
              const int     overlap, 
              int           chan)                 /*!< Channel for which to assign buffers etc. */

{
  SBR_ERROR err = SBRDEC_OK;
  int timeSlots = hHeaderData->numberTimeSlots;   /* Number of SBR slots per frame */
  int noCols = timeSlots * hHeaderData->timeStep; /* Number of QMF slots per frame */
  HANDLE_SBR_DEC hs = &(hSbrChannel->SbrDec);

  /* Initialize scale factors */
  hs->sbrScaleFactor.ov_lb_scale  = 0;
  hs->sbrScaleFactor.ov_hb_scale  = 0;
  hs->sbrScaleFactor.hb_scale     = 0;


  /*
    create envelope calculator
  */
  err = createSbrEnvelopeCalc (&hs->SbrCalculateEnvelope,
                               hHeaderData,
                               chan,
                               flags);
  if (err != SBRDEC_OK) {
    return err;
  }

  /*
    create QMF filter banks
  */
  {
    int qmfErr;

    qmfErr = qmfInitAnalysisFilterBank (
                    &hs->AnalysiscQMF,
                     hs->anaQmfStates,
                     noCols,
                     hHeaderData->freqBandData.lowSubband,
                     hHeaderData->freqBandData.highSubband,
                     hHeaderData->numberOfAnalysisBands,
                     qmfFlags & (~QMF_FLAG_KEEP_STATES)
                     );
    if (qmfErr != 0) {
      return SBRDEC_UNSUPPORTED_CONFIG;
    }
  }
  if (hs->pSynQmfStates == NULL) {
    hs->pSynQmfStates = GetRam_sbr_QmfStatesSynthesis(chan);
    if (hs->pSynQmfStates == NULL)
      return SBRDEC_MEM_ALLOC_FAILED;
  }

  {
    int qmfErr;

    qmfErr = qmfInitSynthesisFilterBank (
           &hs->SynthesisQMF,
            hs->pSynQmfStates,
            noCols,
            hHeaderData->freqBandData.lowSubband,
            hHeaderData->freqBandData.highSubband,
            (64) / downsampleFac,
            qmfFlags & (~QMF_FLAG_KEEP_STATES)
            );

    if (qmfErr != 0) {
      return SBRDEC_UNSUPPORTED_CONFIG;
    }
  }
  initSbrPrevFrameData (&hSbrChannel->prevFrameData, timeSlots);

  /*
    create transposer
  */
  err = createLppTransposer (&hs->LppTrans,
                             pSettings,
                             hHeaderData->freqBandData.lowSubband,
                             hHeaderData->freqBandData.v_k_master,
                             hHeaderData->freqBandData.numMaster,
                             hs->SynthesisQMF.usb,
                             timeSlots,
                             hs->AnalysiscQMF.no_col,
                             hHeaderData->freqBandData.freqBandTableNoise,
                             hHeaderData->freqBandData.nNfb,
                             hHeaderData->sbrProcSmplRate,
                             chan,
                             overlap );
  if (err != SBRDEC_OK) {
    return err;
  }

  /* The CLDFB does not have overlap */
  if ((qmfFlags & QMF_FLAG_CLDFB) == 0) {
    if (hs->pSbrOverlapBuffer == NULL) {
      hs->pSbrOverlapBuffer = GetRam_sbr_OverlapBuffer(chan);
      if (hs->pSbrOverlapBuffer == NULL)  {
        return SBRDEC_MEM_ALLOC_FAILED;
      }
    } else {
      /* Clear overlap buffer */
      FDKmemclear( hs->pSbrOverlapBuffer,
                   sizeof(FIXP_DBL) * 2 * (6) * (64)
                 );
    }
  }

  /* assign qmf time slots */
  assignTimeSlots( &hSbrChannel->SbrDec, hHeaderData->numberTimeSlots * hHeaderData->timeStep, qmfFlags & QMF_FLAG_LP);

  return err;
}

/*!
  \brief     Delete sbr decoder structure
  \return    errorCode, 0 if successful
*/
int
deleteSbrDec (SBR_CHANNEL * hSbrChannel)
{
  HANDLE_SBR_DEC hs = &hSbrChannel->SbrDec;

  deleteSbrEnvelopeCalc (&hs->SbrCalculateEnvelope);

  /* delete QMF filter states */
  if (hs->pSynQmfStates != NULL) {
    FreeRam_sbr_QmfStatesSynthesis(&hs->pSynQmfStates);
  }


  if (hs->pSbrOverlapBuffer != NULL) {
    FreeRam_sbr_OverlapBuffer(&hs->pSbrOverlapBuffer);
  }

  return 0;
}


/*!
  \brief     resets sbr decoder structure
  \return    errorCode, 0 if successful
*/
SBR_ERROR
resetSbrDec (HANDLE_SBR_DEC hSbrDec,
             HANDLE_SBR_HEADER_DATA hHeaderData,
             HANDLE_SBR_PREV_FRAME_DATA hPrevFrameData,
             const int useLP,
             const int downsampleFac
             )
{
  SBR_ERROR sbrError = SBRDEC_OK;
  
  int old_lsb = hSbrDec->SynthesisQMF.lsb;
  int new_lsb = hHeaderData->freqBandData.lowSubband;
  int l, startBand, stopBand, startSlot, size;

  int source_scale, target_scale, delta_scale, target_lsb, target_usb, reserve;
  FIXP_DBL maxVal;

  /* overlapBuffer point to first (6) slots */
  FIXP_DBL  **OverlapBufferReal = hSbrDec->QmfBufferReal;
  FIXP_DBL  **OverlapBufferImag = hSbrDec->QmfBufferImag;

  /* assign qmf time slots */
  assignTimeSlots( hSbrDec, hHeaderData->numberTimeSlots * hHeaderData->timeStep, useLP);



  resetSbrEnvelopeCalc (&hSbrDec->SbrCalculateEnvelope);

  hSbrDec->SynthesisQMF.lsb = hHeaderData->freqBandData.lowSubband;
  hSbrDec->SynthesisQMF.usb = fixMin((INT)hSbrDec->SynthesisQMF.no_channels, (INT)hHeaderData->freqBandData.highSubband);

  hSbrDec->AnalysiscQMF.lsb = hSbrDec->SynthesisQMF.lsb;
  hSbrDec->AnalysiscQMF.usb = hSbrDec->SynthesisQMF.usb;


  /*
    The following initialization of spectral data in the overlap buffer
    is required for dynamic x-over or a change of the start-freq for 2 reasons:

    1. If the lowband gets _wider_, unadjusted data would remain

    2. If the lowband becomes _smaller_, the highest bands of the old lowband
       must be cleared because the whitening would be affected
  */
  startBand = old_lsb;
  stopBand  = new_lsb;
  startSlot = hHeaderData->timeStep * (hPrevFrameData->stopPos - hHeaderData->numberTimeSlots);
  size      = fixMax(0,stopBand-startBand);

  /* keep already adjusted data in the x-over-area */
  if (!useLP) {
    for (l=startSlot; l<hSbrDec->LppTrans.pSettings->overlap; l++) {
      FDKmemclear(&OverlapBufferReal[l][startBand], size*sizeof(FIXP_DBL));
      FDKmemclear(&OverlapBufferImag[l][startBand], size*sizeof(FIXP_DBL));
    }
  } else
  for (l=startSlot; l<hSbrDec->LppTrans.pSettings->overlap ; l++) {
    FDKmemclear(&OverlapBufferReal[l][startBand], size*sizeof(FIXP_DBL));
  }


  /*
    reset LPC filter states
  */
  startBand = fixMin(old_lsb,new_lsb);
  stopBand  = fixMax(old_lsb,new_lsb);
  size      = fixMax(0,stopBand-startBand);

  FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesReal[0][startBand], size*sizeof(FIXP_DBL));
  FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesReal[1][startBand], size*sizeof(FIXP_DBL));
  if (!useLP) {
    FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesImag[0][startBand], size*sizeof(FIXP_DBL));
    FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesImag[1][startBand], size*sizeof(FIXP_DBL));
  }


  /*
    Rescale already processed spectral data between old and new x-over frequency.
    This must be done because of the separate scalefactors for lowband and highband.
  */
  startBand = fixMin(old_lsb,new_lsb);
  stopBand =  fixMax(old_lsb,new_lsb);

  if (new_lsb > old_lsb) {
    /* The x-over-area was part of the highband before and will now belong to the lowband */
    source_scale = hSbrDec->sbrScaleFactor.ov_hb_scale;
    target_scale = hSbrDec->sbrScaleFactor.ov_lb_scale;
    target_lsb   = 0;
    target_usb   = old_lsb;
  }
  else {
    /* The x-over-area was part of the lowband before and will now belong to the highband */
    source_scale = hSbrDec->sbrScaleFactor.ov_lb_scale;
    target_scale = hSbrDec->sbrScaleFactor.ov_hb_scale;
    /* jdr: The values old_lsb and old_usb might be wrong because the previous frame might have been "upsamling". */
    target_lsb   = hSbrDec->SynthesisQMF.lsb;
    target_usb   = hSbrDec->SynthesisQMF.usb;
  }

  /* Shift left all samples of the x-over-area as much as possible
     An unnecessary coarse scale could cause ov_lb_scale or ov_hb_scale to be
     adapted and the accuracy in the next frame would seriously suffer! */

  maxVal = maxSubbandSample( OverlapBufferReal,
                             (useLP) ? NULL : OverlapBufferImag,
                             startBand,
                             stopBand,
                             0,
                             startSlot);

  reserve = CntLeadingZeros(maxVal)-1;
  reserve = fixMin(reserve,DFRACT_BITS-1-source_scale);

  rescaleSubbandSamples( OverlapBufferReal,
                         (useLP) ? NULL : OverlapBufferImag,
                         startBand,
                         stopBand,
                         0,
                         startSlot,
                         reserve);
  source_scale += reserve;

  delta_scale = target_scale - source_scale;

  if (delta_scale > 0) { /* x-over-area is dominant */
    delta_scale = -delta_scale;
    startBand = target_lsb;
    stopBand = target_usb;

    if (new_lsb > old_lsb) {
      /* The lowband has to be rescaled */
      hSbrDec->sbrScaleFactor.ov_lb_scale = source_scale;
    }
    else {
      /* The highband has be be rescaled */
      hSbrDec->sbrScaleFactor.ov_hb_scale = source_scale;
    }
  }

  FDK_ASSERT(startBand <= stopBand);

  if (!useLP) {
    for (l=0; l<startSlot; l++) {
      scaleValues( OverlapBufferReal[l] + startBand, stopBand-startBand, delta_scale );
      scaleValues( OverlapBufferImag[l] + startBand, stopBand-startBand, delta_scale );
    }
  } else
  for (l=0; l<startSlot; l++) {
    scaleValues( OverlapBufferReal[l] + startBand, stopBand-startBand, delta_scale );
  }


  /*
    Initialize transposer and limiter
  */
  sbrError = resetLppTransposer (&hSbrDec->LppTrans,
                                 hHeaderData->freqBandData.lowSubband,
                                 hHeaderData->freqBandData.v_k_master,
                                 hHeaderData->freqBandData.numMaster,
                                 hHeaderData->freqBandData.freqBandTableNoise,
                                 hHeaderData->freqBandData.nNfb,
                                 hHeaderData->freqBandData.highSubband,
                                 hHeaderData->sbrProcSmplRate);
  if (sbrError != SBRDEC_OK)
    return sbrError;

  sbrError = ResetLimiterBands ( hHeaderData->freqBandData.limiterBandTable,
                                 &hHeaderData->freqBandData.noLimiterBands,
                                 hHeaderData->freqBandData.freqBandTable[0],
                                 hHeaderData->freqBandData.nSfb[0],
                                 hSbrDec->LppTrans.pSettings->patchParam,
                                 hSbrDec->LppTrans.pSettings->noOfPatches,
                                 hHeaderData->bs_data.limiterBands);


  return sbrError;
}