aboutsummaryrefslogtreecommitdiffstats
path: root/libFDK/src/qmf.cpp
blob: b02a06602bba26d558f3ec475e9813072f5e2ca7 (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
/********************************  Fraunhofer IIS  ***************************

                        (C) Copyright Fraunhofer IIS (2006)
                               All Rights Reserved

    Please be advised that this software and/or program delivery is
    Confidential Information of Fraunhofer and subject to and covered by the

    Fraunhofer IIS Software Evaluation Agreement
    between Google Inc. and  Fraunhofer
    effective and in full force since March 1, 2012.

    You may use this software and/or program only under the terms and
    conditions described in the above mentioned Fraunhofer IIS Software
    Evaluation Agreement. Any other and/or further use requires a separate agreement.


   $Id$
   Author(s):   Markus Lohwasser, Josef Hoepfl, Manuel Jander
   Description: QMF filterbank

   This software and/or program is protected by copyright law and international
   treaties. Any reproduction or distribution of this software and/or program,
   or any portion of it, may result in severe civil and criminal penalties, and
   will be prosecuted to the maximum extent possible under law.

******************************************************************************/
/*!
  \file
  \brief  Complex qmf analysis/synthesis, $Revision: 36871 $
  This module contains the qmf filterbank for analysis [ cplxAnalysisQmfFiltering() ] and
  synthesis [ cplxSynthesisQmfFiltering() ]. It is a polyphase implementation of a complex
  exponential modulated filter bank. The analysis part usually runs at half the sample rate
  than the synthesis part. (So called "dual-rate" mode.)

  The coefficients of the prototype filter are specified in #sbr_qmf_64_640 (in sbr_rom.cpp).
  Thus only a 64 channel version (32 on the analysis side) with a 640 tap prototype filter
  are used.

  \anchor PolyphaseFiltering <h2>About polyphase filtering</h2>
  The polyphase implementation of a filterbank requires filtering at the input and output.
  This is implemented as part of cplxAnalysisQmfFiltering() and cplxSynthesisQmfFiltering().
  The implementation requires the filter coefficients in a specific structure as described in
  #sbr_qmf_64_640_qmf (in sbr_rom.cpp).

  This module comprises the computationally most expensive functions of the SBR decoder. The accuracy of
  computations is also important and has a direct impact on the overall sound quality. Therefore a special
  test program is available which can be used to only test the filterbank: main_audio.cpp

  This modules also uses scaling of data to provide better SNR on fixed-point processors. See #QMF_SCALE_FACTOR (in sbr_scale.h) for details.
  An interesting note: The function getScalefactor() can constitute a significant amount of computational complexity - very much depending on the
  bitrate. Since it is a rather small function, effective assembler optimization might be possible.

*/

#include "qmf.h"


#include "fixpoint_math.h"
#include "dct.h"

#ifdef QMFSYN_STATES_16BIT
#define QSSCALE (7)
#define FX_DBL2FX_QSS(x) ((FIXP_QSS) ((x)>>(DFRACT_BITS-QSS_BITS-QSSCALE) ))
#define FX_QSS2FX_DBL(x) ((FIXP_DBL)((LONG)x)<<(DFRACT_BITS-QSS_BITS-QSSCALE))
#else
#define QSSCALE (0)
#define FX_DBL2FX_QSS(x) (x)
#define FX_QSS2FX_DBL(x) (x)
#endif


#if defined(__arm__)
#include "arm/qmf_arm.cpp"

#endif

/*!
 * \brief Algorithmic scaling in sbrForwardModulation()
 *
 * The scaling in sbrForwardModulation() is caused by:
 *
 *   \li 1 R_SHIFT in sbrForwardModulation()
 *   \li 5/6 R_SHIFT in dct3() if using 32/64 Bands
 *   \li 1 ommited gain of 2.0 in qmfForwardModulation()
 */
#define ALGORITHMIC_SCALING_IN_ANALYSIS_FILTERBANK 7

/*!
 * \brief Algorithmic scaling in cplxSynthesisQmfFiltering()
 *
 * The scaling in cplxSynthesisQmfFiltering() is caused by:
 *
 *   \li  5/6 R_SHIFT in dct2() if using 32/64 Bands
 *   \li  1 ommited gain of 2.0 in qmfInverseModulation()
 *   \li -6 division by 64 in synthesis filterbank
 *   \li x bits external influence
 */
#define ALGORITHMIC_SCALING_IN_SYNTHESIS_FILTERBANK 1


/*!
  \brief Perform Synthesis Prototype Filtering on a single slot of input data.

  The filter takes 2 * qmf->no_channels of input data and
  generates qmf->no_channels time domain output samples.
*/
static
#ifndef FUNCTION_qmfSynPrototypeFirSlot
void qmfSynPrototypeFirSlot(
#else
void qmfSynPrototypeFirSlot_fallback(
#endif
                             HANDLE_QMF_FILTER_BANK qmf,
                             FIXP_QMF *RESTRICT realSlot,            /*!< Input: Pointer to real Slot */
                             FIXP_QMF *RESTRICT imagSlot,            /*!< Input: Pointer to imag Slot */
                             INT_PCM  *RESTRICT timeOut,             /*!< Time domain data */
                             int       stride
                            )
{
  FIXP_QSS* FilterStates = (FIXP_QSS*)qmf->FilterStates;
  int       no_channels = qmf->no_channels;
  const FIXP_PFT *p_Filter = qmf->p_filter;
  int p_stride = qmf->p_stride;
  int j;
  FIXP_QSS *RESTRICT sta = FilterStates;
  const FIXP_PFT *RESTRICT p_flt, *RESTRICT p_fltm;
  int scale = ((DFRACT_BITS-SAMPLE_BITS)-1-qmf->outScalefactor);

  p_flt  = p_Filter+p_stride*QMF_NO_POLY;          /*                     5-ter von 330 */
  p_fltm = p_Filter+(qmf->FilterSize/2)-p_stride*QMF_NO_POLY;  /* 5 + (320 - 2*5) = 315-ter von 330 */

  FDK_ASSERT(SAMPLE_BITS-1-qmf->outScalefactor >= 0); //   (DFRACT_BITS-SAMPLE_BITS)-1-qmf->outScalefactor >= 0);

  for (j = no_channels-1; j >= 0; j--) {  /* ---- l�uft ueber alle Linien eines Slots ---- */
    FIXP_QMF imag  =  imagSlot[j];  // no_channels-1 .. 0
    FIXP_QMF real  =  realSlot[j];  // ~~"~~
    {
      INT_PCM tmp;
      FIXP_DBL Are = FX_QSS2FX_DBL(sta[0]) + fMultDiv2( p_fltm[0] , real);

      if (qmf->outGain!=(FIXP_DBL)0x80000000) {
        Are = fMult(Are,qmf->outGain);
      }

  #if SAMPLE_BITS > 16
      tmp = (INT_PCM)(SATURATE_SHIFT(fAbs(Are), scale, SAMPLE_BITS));
  #else
      tmp = (INT_PCM)(SATURATE_RIGHT_SHIFT(fAbs(Are), scale, SAMPLE_BITS));
  #endif
      if (Are < (FIXP_QMF)0) {
        tmp = -tmp;
      }
      timeOut[ (j)*stride ] = tmp;
    }

    sta[0] = sta[1] + FX_DBL2FX_QSS(fMultDiv2( p_flt [4] , imag ));
    sta[1] = sta[2] + FX_DBL2FX_QSS(fMultDiv2( p_fltm[1] , real ));
    sta[2] = sta[3] + FX_DBL2FX_QSS(fMultDiv2( p_flt [3] , imag ));
    sta[3] = sta[4] + FX_DBL2FX_QSS(fMultDiv2( p_fltm[2] , real ));
    sta[4] = sta[5] + FX_DBL2FX_QSS(fMultDiv2( p_flt [2] , imag ));
    sta[5] = sta[6] + FX_DBL2FX_QSS(fMultDiv2( p_fltm[3] , real ));
    sta[6] = sta[7] + FX_DBL2FX_QSS(fMultDiv2( p_flt [1] , imag ));
    sta[7] = sta[8] + FX_DBL2FX_QSS(fMultDiv2( p_fltm[4] , real ));
    sta[8] =          FX_DBL2FX_QSS(fMultDiv2( p_flt [0] , imag ));

    p_flt  += (p_stride*QMF_NO_POLY);
    p_fltm -= (p_stride*QMF_NO_POLY);
    sta    += 9; // = (2*QMF_NO_POLY-1);
  }
}

#ifndef FUNCTION_qmfSynPrototypeFirSlot_NonSymmetric
/*!
  \brief Perform Synthesis Prototype Filtering on a single slot of input data.

  The filter takes 2 * qmf->no_channels of input data and
  generates qmf->no_channels time domain output samples.
*/
static
void qmfSynPrototypeFirSlot_NonSymmetric(
                             HANDLE_QMF_FILTER_BANK qmf,
                             FIXP_QMF *RESTRICT realSlot,            /*!< Input: Pointer to real Slot */
                             FIXP_QMF *RESTRICT imagSlot,            /*!< Input: Pointer to imag Slot */
                             INT_PCM  *RESTRICT timeOut,             /*!< Time domain data */
                             int       stride
                            )
{
  FIXP_QSS* FilterStates = (FIXP_QSS*)qmf->FilterStates;
  int       no_channels = qmf->no_channels;
  const FIXP_PFT *p_Filter = qmf->p_filter;
  int p_stride = qmf->p_stride;
  int j;
  FIXP_QSS *RESTRICT sta = FilterStates;
  const FIXP_PFT *RESTRICT p_flt, *RESTRICT p_fltm;
  int scale = ((DFRACT_BITS-SAMPLE_BITS)-1-qmf->outScalefactor);

  p_flt  = p_Filter;                           /*!< Pointer to first half of filter coefficients */
  p_fltm = &p_flt[qmf->FilterSize/2];  /* at index 320, overall 640 coefficients */

  FDK_ASSERT(SAMPLE_BITS-1-qmf->outScalefactor >= 0); //   (DFRACT_BITS-SAMPLE_BITS)-1-qmf->outScalefactor >= 0);

  for (j = no_channels-1; j >= 0; j--) {  /* ---- l�uft ueber alle Linien eines Slots ---- */

    FIXP_QMF imag  =  imagSlot[j];  // no_channels-1 .. 0
    FIXP_QMF real  =  realSlot[j];  // ~~"~~
    {
      INT_PCM tmp;
      FIXP_QMF Are = sta[0] + FX_DBL2FX_QSS(fMultDiv2( p_fltm[4] , real ));

  #if SAMPLE_BITS > 16
      tmp = (INT_PCM)(SATURATE_SHIFT(fAbs(Are), scale, SAMPLE_BITS));
  #else
      tmp = (INT_PCM)(SATURATE_RIGHT_SHIFT(fAbs(Are), scale, SAMPLE_BITS));
  #endif
      if (Are < (FIXP_QMF)0) {
        tmp = -tmp;
      }
      timeOut[j*stride] = tmp;
    }

    sta[0] = sta[1] + FX_DBL2FX_QSS(fMultDiv2( p_flt [4] , imag ));
    sta[1] = sta[2] + FX_DBL2FX_QSS(fMultDiv2( p_fltm[3] , real ));
    sta[2] = sta[3] + FX_DBL2FX_QSS(fMultDiv2( p_flt [3] , imag ));

    sta[3] = sta[4] + FX_DBL2FX_QSS(fMultDiv2( p_fltm[2] , real ));
    sta[4] = sta[5] + FX_DBL2FX_QSS(fMultDiv2( p_flt [2] , imag ));
    sta[5] = sta[6] + FX_DBL2FX_QSS(fMultDiv2( p_fltm[1] , real ));
    sta[6] = sta[7] + FX_DBL2FX_QSS(fMultDiv2( p_flt [1] , imag ));

    sta[7] = sta[8] + FX_DBL2FX_QSS(fMultDiv2( p_fltm[0] , real ));
    sta[8] =          FX_DBL2FX_QSS(fMultDiv2( p_flt [0] , imag ));

    p_flt  += (p_stride*QMF_NO_POLY);
    p_fltm += (p_stride*QMF_NO_POLY);
    sta    += 9; // = (2*QMF_NO_POLY-1);
  }

}
#endif /* FUNCTION_qmfSynPrototypeFirSlot_NonSymmetric */

#ifndef FUNCTION_qmfAnaPrototypeFirSlot
/*!
  \brief Perform Analysis Prototype Filtering on a single slot of input data.
*/
static
void qmfAnaPrototypeFirSlot( FIXP_QMF *analysisBuffer,
                             int       no_channels,             /*!< Number channels of analysis filter */
                             const FIXP_PFT *p_filter,
                             int       p_stride,                /*!< Stide of analysis filter    */
                             FIXP_QAS *RESTRICT pFilterStates
                            )
{
    int k;

    FIXP_DBL accu;
    const FIXP_PFT *RESTRICT p_flt = p_filter;
    FIXP_QMF *RESTRICT pData_0 = analysisBuffer + 2*no_channels - 1;
    FIXP_QMF *RESTRICT pData_1 = analysisBuffer;

    FIXP_QAS *RESTRICT sta_0 = (FIXP_QAS *)pFilterStates;
    FIXP_QAS *RESTRICT sta_1 = (FIXP_QAS *)pFilterStates + (2*QMF_NO_POLY*no_channels) - 1;
    int pfltStep = QMF_NO_POLY * (p_stride);
    int staStep1 = no_channels<<1;
    int staStep2 = (no_channels<<3) - 1; /* Rewind one less */

    /* FIR filter 0 */
    accu =   fMultDiv2( p_flt[0], *sta_1);  sta_1 -= staStep1;
    accu +=  fMultDiv2( p_flt[1], *sta_1);  sta_1 -= staStep1;
    accu +=  fMultDiv2( p_flt[2], *sta_1);  sta_1 -= staStep1;
    accu +=  fMultDiv2( p_flt[3], *sta_1);  sta_1 -= staStep1;
    accu +=  fMultDiv2( p_flt[4], *sta_1);
    *pData_1++ = FX_DBL2FX_QMF(accu<<1);
    sta_1 += staStep2;

    p_flt += pfltStep;

    /* FIR filters 1..63 127..65 */
    for (k=0; k<no_channels-1; k++)
    {
      accu =  fMultDiv2( p_flt[0], *sta_0);  sta_0 += staStep1;
      accu += fMultDiv2( p_flt[1], *sta_0);  sta_0 += staStep1;
      accu += fMultDiv2( p_flt[2], *sta_0);  sta_0 += staStep1;
      accu += fMultDiv2( p_flt[3], *sta_0);  sta_0 += staStep1;
      accu += fMultDiv2( p_flt[4], *sta_0);
      *pData_0-- = FX_DBL2FX_QMF(accu<<1);
      sta_0 -= staStep2;

      accu =   fMultDiv2( p_flt[0], *sta_1);  sta_1 -= staStep1;
      accu +=  fMultDiv2( p_flt[1], *sta_1);  sta_1 -= staStep1;
      accu +=  fMultDiv2( p_flt[2], *sta_1);  sta_1 -= staStep1;
      accu +=  fMultDiv2( p_flt[3], *sta_1);  sta_1 -= staStep1;
      accu +=  fMultDiv2( p_flt[4], *sta_1);
      *pData_1++ = FX_DBL2FX_QMF(accu<<1);
      sta_1 += staStep2;

      p_flt += pfltStep;
    }

    /* FIR filter 64 */
    accu =  fMultDiv2( p_flt[0], *sta_0);  sta_0 += staStep1;
    accu += fMultDiv2( p_flt[1], *sta_0);  sta_0 += staStep1;
    accu += fMultDiv2( p_flt[2], *sta_0);  sta_0 += staStep1;
    accu += fMultDiv2( p_flt[3], *sta_0);  sta_0 += staStep1;
    accu += fMultDiv2( p_flt[4], *sta_0);
    *pData_0-- = FX_DBL2FX_QMF(accu<<1);
    sta_0 -= staStep2;
}
#endif /* !defined(FUNCTION_qmfAnaPrototypeFirSlot) */


#ifndef FUNCTION_qmfAnaPrototypeFirSlot_NonSymmetric
/*!
  \brief Perform Analysis Prototype Filtering on a single slot of input data.
*/
static
void qmfAnaPrototypeFirSlot_NonSymmetric(
                                        FIXP_QMF *analysisBuffer,
                                        int       no_channels,             /*!< Number channels of analysis filter */
                                        const FIXP_PFT *p_filter,
                                        int       p_stride,                /*!< Stide of analysis filter    */
                                        FIXP_QAS *RESTRICT pFilterStates
                                       )
{
  const FIXP_PFT *RESTRICT p_flt = p_filter;
  int  p, k;

  for (k = 0; k < 2*no_channels; k++)
  {
    FIXP_DBL accu = (FIXP_DBL)0;

    p_flt += QMF_NO_POLY * (p_stride - 1);

    /*
      Perform FIR-Filter
    */
    for (p = 0; p < QMF_NO_POLY; p++) {
      accu +=  fMultDiv2(*p_flt++, pFilterStates[2*no_channels * p]);
    }
    analysisBuffer[2*no_channels - 1 - k] = FX_DBL2FX_QMF(accu<<1);
    pFilterStates++;
  }
}
#endif /* FUNCTION_qmfAnaPrototypeFirSlot_NonSymmetric */

/*!
 *
 * \brief Perform real-valued forward modulation of the time domain
 *        data of timeIn and stores the real part of the subband
 *        samples in rSubband
 *
 */
static void
qmfForwardModulationLP_even( HANDLE_QMF_FILTER_BANK anaQmf, /*!< Handle of Qmf Analysis Bank  */
                             FIXP_QMF *timeIn,              /*!< Time Signal */
                             FIXP_QMF *rSubband )           /*!< Real Output */
{
  int i;
  int L = anaQmf->no_channels;
  int M = L>>1;
  int scale;
  FIXP_QMF accu;

  const FIXP_QMF *timeInTmp1 = (FIXP_QMF *) &timeIn[3 * M];
  const FIXP_QMF *timeInTmp2 = timeInTmp1;
  FIXP_QMF *rSubbandTmp = rSubband;

  rSubband[0] = timeIn[3 * M] >> 1;

  for (i = M-1; i != 0; i--) {
    accu = ((*--timeInTmp1) >> 1) + ((*++timeInTmp2) >> 1);
    *++rSubbandTmp = accu;
  }

  timeInTmp1 = &timeIn[2 * M];
  timeInTmp2 = &timeIn[0];
  rSubbandTmp = &rSubband[M];

  for (i = L-M; i != 0; i--) {
    accu = ((*timeInTmp1--) >> 1) - ((*timeInTmp2++) >> 1);
    *rSubbandTmp++ = accu;
  }

  dct_III(rSubband, timeIn, L, &scale);
}

#if !defined(FUNCTION_qmfForwardModulationLP_odd)
static void
qmfForwardModulationLP_odd( HANDLE_QMF_FILTER_BANK anaQmf, /*!< Handle of Qmf Analysis Bank  */
                            const FIXP_QMF *timeIn,        /*!< Time Signal */
                            FIXP_QMF *rSubband )           /*!< Real Output */
{
  int i;
  int L = anaQmf->no_channels;
  int M = L>>1;
  int shift = (anaQmf->no_channels>>6) + 1;

  for (i = 0; i < M; i++) {
    rSubband[M + i]     = (timeIn[L - 1 - i]>>1) - (timeIn[i]>>shift);
    rSubband[M - 1 - i] = (timeIn[L + i]>>1)     + (timeIn[2 * L - 1 - i]>>shift);
  }

  dct_IV(rSubband, L, &shift);
}
#endif /* !defined(FUNCTION_qmfForwardModulationLP_odd) */



/*!
 *
 * \brief Perform complex-valued forward modulation of the time domain
 *        data of timeIn and stores the real part of the subband
 *        samples in rSubband, and the imaginary part in iSubband
 *
 *        Only the lower bands are obtained (upto anaQmf->lsb). For
 *        a full bandwidth analysis it is required to set both anaQmf->lsb
 *        and anaQmf->usb to the amount of QMF bands.
 *
 */
static void
qmfForwardModulationHQ( HANDLE_QMF_FILTER_BANK anaQmf,     /*!< Handle of Qmf Analysis Bank  */
                        const FIXP_QMF *RESTRICT timeIn,   /*!< Time Signal */
                        FIXP_QMF *RESTRICT rSubband,       /*!< Real Output */
                        FIXP_QMF *RESTRICT iSubband        /*!< Imaginary Output */
                       )
{
  int i;
  int L = anaQmf->no_channels;
  int L2 = L<<1;
  int shift = 0;

  for (i = 0; i < L; i+=2) {
    FIXP_QMF x0, x1, y0, y1;

    x0 = timeIn[i] >> 1;
    x1 = timeIn[i+1] >> 1;
    y0 = timeIn[L2 - 1 - i] >> 1;
    y1 = timeIn[L2 - 2 - i] >> 1;

    rSubband[i] = x0 - y0;
    rSubband[i+1] = x1 - y1;
    iSubband[i] = x0 + y0;
    iSubband[i+1] = x1 + y1;
  }

  dct_IV(rSubband, L, &shift);
  dst_IV(iSubband, L, &shift);

  {
    {
      const FIXP_QTW *RESTRICT sbr_t_cos;
      const FIXP_QTW *RESTRICT sbr_t_sin;
      sbr_t_cos = anaQmf->t_cos;
      sbr_t_sin = anaQmf->t_sin;

      for (i = 0; i < anaQmf->lsb; i++) {
        cplxMult(&iSubband[i], &rSubband[i], iSubband[i], rSubband[i], sbr_t_cos[i], sbr_t_sin[i]);
      }
    }
  }
}

/*
 * \brief Perform one QMF slot analysis of the time domain data of timeIn
 *        with specified stride and stores the real part of the subband
 *        samples in rSubband, and the imaginary part in iSubband
 *
 *        Only the lower bands are obtained (upto anaQmf->lsb). For
 *        a full bandwidth analysis it is required to set both anaQmf->lsb
 *        and anaQmf->usb to the amount of QMF bands.
 */
void
qmfAnalysisFilteringSlot( HANDLE_QMF_FILTER_BANK anaQmf,  /*!< Handle of Qmf Synthesis Bank  */
                          FIXP_QMF      *qmfReal,         /*!< Low and High band, real */
                          FIXP_QMF      *qmfImag,         /*!< Low and High band, imag */
                          const INT_PCM *RESTRICT timeIn, /*!< Pointer to input */
                          const int      stride,          /*!< stride factor of input */
                          FIXP_QMF      *pWorkBuffer      /*!< pointer to temporal working buffer */
                         )
{
    int i;
    int offset = anaQmf->no_channels*(QMF_NO_POLY*2-1);
    /*
      Feed time signal into oldest anaQmf->no_channels states
    */
    {
      FIXP_QAS *RESTRICT FilterStatesAnaTmp = ((FIXP_QAS*)anaQmf->FilterStates)+offset;

      /* Feed and scale actual time in slot */
      for(i=anaQmf->no_channels>>1; i!=0; i--) {
        /* Place INT_PCM value left aligned in scaledTimeIn */
#if (QAS_BITS==SAMPLE_BITS)
        *FilterStatesAnaTmp++ = (FIXP_QAS)*timeIn; timeIn += stride;
        *FilterStatesAnaTmp++ = (FIXP_QAS)*timeIn; timeIn += stride;
#elif (QAS_BITS>SAMPLE_BITS)
        *FilterStatesAnaTmp++ = (FIXP_QAS)((*timeIn)<<(QAS_BITS-SAMPLE_BITS)); timeIn += stride;
        *FilterStatesAnaTmp++ = (FIXP_QAS)((*timeIn)<<(QAS_BITS-SAMPLE_BITS)); timeIn += stride;
#else
        *FilterStatesAnaTmp++ = (FIXP_QAS)((*timeIn)>>(SAMPLE_BITS-QAS_BITS)); timeIn += stride;
        *FilterStatesAnaTmp++ = (FIXP_QAS)((*timeIn)>>(SAMPLE_BITS-QAS_BITS)); timeIn += stride;
#endif
      }
    }

    if (anaQmf->flags & QMF_FLAG_NONSYMMETRIC) {
      qmfAnaPrototypeFirSlot_NonSymmetric(
                              pWorkBuffer,
                              anaQmf->no_channels,
                              anaQmf->p_filter,
                              anaQmf->p_stride,
                              (FIXP_QAS*)anaQmf->FilterStates
                            );
    } else {
      qmfAnaPrototypeFirSlot( pWorkBuffer,
                              anaQmf->no_channels,
                              anaQmf->p_filter,
                              anaQmf->p_stride,
                              (FIXP_QAS*)anaQmf->FilterStates
                            );
    }

    if (anaQmf->flags & QMF_FLAG_LP) {
      if (anaQmf->flags & QMF_FLAG_CLDFB)
        qmfForwardModulationLP_odd( anaQmf,
                                    pWorkBuffer,
                                    qmfReal );
      else
        qmfForwardModulationLP_even( anaQmf,
                                     pWorkBuffer,
                                     qmfReal );

    } else {
      qmfForwardModulationHQ( anaQmf,
                              pWorkBuffer,
                              qmfReal,
                              qmfImag
                             );
    }
    /*
      Shift filter states

      Should be realized with modulo adressing on a DSP instead of a true buffer shift
    */
    FDKmemmove ((FIXP_QAS*)anaQmf->FilterStates, (FIXP_QAS*)anaQmf->FilterStates+anaQmf->no_channels, offset*sizeof(FIXP_QAS));
}


/*!
 *
 * \brief Perform complex-valued subband filtering of the time domain
 *        data of timeIn and stores the real part of the subband
 *        samples in rAnalysis, and the imaginary part in iAnalysis
 * The qmf coefficient table is symmetric. The symmetry is expoited by
 * shrinking the coefficient table to half the size. The addressing mode
 * takes care of the symmetries.
 *
 * Only the lower bands are obtained (upto anaQmf->lsb). For
 * a full bandwidth analysis it is required to set both anaQmf->lsb
 * and anaQmf->usb to the amount of QMF bands.
 *
 * \sa PolyphaseFiltering
 */

void
qmfAnalysisFiltering( HANDLE_QMF_FILTER_BANK anaQmf,    /*!< Handle of Qmf Analysis Bank */
                      FIXP_QMF **qmfReal,               /*!< Pointer to real subband slots */
                      FIXP_QMF **qmfImag,               /*!< Pointer to imag subband slots */
                      QMF_SCALE_FACTOR *scaleFactor,
                      const INT_PCM *timeIn,            /*!< Time signal */
                      const int  stride,
                      FIXP_QMF  *pWorkBuffer            /*!< pointer to temporal working buffer */
                      )
{
  int i;
  int no_channels = anaQmf->no_channels;

  scaleFactor->lb_scale = -ALGORITHMIC_SCALING_IN_ANALYSIS_FILTERBANK;
  scaleFactor->lb_scale -= anaQmf->filterScale;

  for (i = 0; i < anaQmf->no_col; i++)
  {
      FIXP_QMF *qmfImagSlot = NULL;

      if (!(anaQmf->flags & QMF_FLAG_LP)) {
        qmfImagSlot = qmfImag[i];
      }

      qmfAnalysisFilteringSlot( anaQmf, qmfReal[i], qmfImagSlot, timeIn , stride, pWorkBuffer );

      timeIn += no_channels*stride;

  } /* no_col loop  i  */
}

/*!
 *
 * \brief Perform low power inverse modulation of the subband
 *        samples stored in rSubband (real part) and iSubband (imaginary
 *        part) and stores the result in pWorkBuffer.
 *
 */
inline
static void
qmfInverseModulationLP_even( HANDLE_QMF_FILTER_BANK synQmf,   /*!< Handle of Qmf Synthesis Bank  */
                             const FIXP_QMF *qmfReal,         /*!< Pointer to qmf real subband slot (input) */
                             const int   scaleFactorLowBand,  /*!< Scalefactor for Low band */
                             const int   scaleFactorHighBand, /*!< Scalefactor for High band */
                             FIXP_QMF *pTimeOut               /*!< Pointer to qmf subband slot (output)*/
                           )
{
  int i;
  int L = synQmf->no_channels;
  int M = L>>1;
  int scale;
  FIXP_QMF tmp;
  FIXP_QMF *RESTRICT tReal = pTimeOut;
  FIXP_QMF *RESTRICT tImag = pTimeOut + L;

  /* Move input to output vector with offset */
  scaleValues(&tReal[0],             &qmfReal[0],             synQmf->lsb,             scaleFactorLowBand);
  scaleValues(&tReal[0+synQmf->lsb], &qmfReal[0+synQmf->lsb], synQmf->usb-synQmf->lsb, scaleFactorHighBand);
  FDKmemclear(&tReal[0+synQmf->usb], (L-synQmf->usb)*sizeof(FIXP_QMF));

  /* Dct type-2 transform */
  dct_II(tReal, tImag, L, &scale);

  /* Expand output and replace inplace the output buffers */
  tImag[0] = tReal[M];
  tImag[M] = (FIXP_QMF)0;
  tmp = tReal [0];
  tReal [0] = tReal[M];
  tReal [M] = tmp;

  for (i = 1; i < M/2; i++) {
    /* Imag */
    tmp = tReal[L - i];
    tImag[M - i] =  tmp;
    tImag[i + M] = -tmp;

    tmp = tReal[M + i];
    tImag[i] =  tmp;
    tImag[L - i] = -tmp;

    /* Real */
    tReal [M + i] = tReal[i];
    tReal [L - i] = tReal[M - i];
    tmp = tReal[i];
    tReal[i] = tReal [M - i];
    tReal [M - i] = tmp;

  }
  /* Remaining odd terms */
  tmp = tReal[M + M/2];
  tImag[M/2]     =  tmp;
  tImag[M/2 + M] = -tmp;

  tReal [M + M/2] = tReal[M/2];
}

inline
static void
qmfInverseModulationLP_odd( HANDLE_QMF_FILTER_BANK synQmf,   /*!< Handle of Qmf Synthesis Bank  */
                            const FIXP_QMF *qmfReal,         /*!< Pointer to qmf real subband slot (input) */
                            const int scaleFactorLowBand,    /*!< Scalefactor for Low band */
                            const int scaleFactorHighBand,   /*!< Scalefactor for High band */
                            FIXP_QMF *pTimeOut               /*!< Pointer to qmf subband slot (output)*/
                          )
{
  int i;
  int L = synQmf->no_channels;
  int M = L>>1;
  int shift = 0;

  /* Move input to output vector with offset */
  scaleValues(pTimeOut+M,              qmfReal,             synQmf->lsb,             scaleFactorLowBand);
  scaleValues(pTimeOut+M+synQmf->lsb,  qmfReal+synQmf->lsb, synQmf->usb-synQmf->lsb, scaleFactorHighBand);
  FDKmemclear(pTimeOut+M+synQmf->usb, (L-synQmf->usb)*sizeof(FIXP_QMF));

  dct_IV(pTimeOut+M, L, &shift);
  for (i = 0; i < M; i++) {
    pTimeOut[i]             =  pTimeOut[L - 1 - i];
    pTimeOut[2 * L - 1 - i] = -pTimeOut[L + i];
  }
}


/*!
 *
 * \brief Perform complex-valued inverse modulation of the subband
 *        samples stored in rSubband (real part) and iSubband (imaginary
 *        part) and stores the result in pWorkBuffer.
 *
 */
inline
static void
qmfInverseModulationHQ( HANDLE_QMF_FILTER_BANK synQmf,  /*!< Handle of Qmf Synthesis Bank     */
                        const FIXP_QMF *qmfReal,        /*!< Pointer to qmf real subband slot */
                        const FIXP_QMF *qmfImag,        /*!< Pointer to qmf imag subband slot */
                        const int   scaleFactorLowBand, /*!< Scalefactor for Low band         */
                        const int   scaleFactorHighBand,/*!< Scalefactor for High band        */
                        FIXP_QMF  *pWorkBuffer          /*!< WorkBuffer (output)              */
                      )
{
  int i;
  int L = synQmf->no_channels;
  int M = L>>1;
  int shift = 0;
  FIXP_QMF *RESTRICT tReal = pWorkBuffer;
  FIXP_QMF *RESTRICT tImag = pWorkBuffer+L;

  if (synQmf->flags & QMF_FLAG_CLDFB){
    for (i = 0; i < synQmf->lsb; i++) {
      cplxMult(&tImag[i], &tReal[i],
                scaleValue(qmfImag[i],scaleFactorLowBand), scaleValue(qmfReal[i],scaleFactorLowBand),
                synQmf->t_cos[i], synQmf->t_sin[i]);
    }
    for (; i < synQmf->usb; i++) {
      cplxMult(&tImag[i], &tReal[i],
                scaleValue(qmfImag[i],scaleFactorHighBand), scaleValue(qmfReal[i],scaleFactorHighBand),
                synQmf->t_cos[i], synQmf->t_sin[i]);
    }
  }

  if ( (synQmf->flags & QMF_FLAG_CLDFB) == 0) {
    scaleValues(&tReal[0],             &qmfReal[0],             synQmf->lsb,             scaleFactorLowBand);
    scaleValues(&tReal[0+synQmf->lsb], &qmfReal[0+synQmf->lsb], synQmf->usb-synQmf->lsb, scaleFactorHighBand);
    scaleValues(&tImag[0],             &qmfImag[0],             synQmf->lsb,             scaleFactorLowBand);
    scaleValues(&tImag[0+synQmf->lsb], &qmfImag[0+synQmf->lsb], synQmf->usb-synQmf->lsb, scaleFactorHighBand);
  }

  FDKmemclear(&tReal[synQmf->usb], (synQmf->no_channels-synQmf->usb)*sizeof(FIXP_QMF));
  FDKmemclear(&tImag[synQmf->usb], (synQmf->no_channels-synQmf->usb)*sizeof(FIXP_QMF));

  dct_IV(tReal, L, &shift);
  dst_IV(tImag, L, &shift);

  if (synQmf->flags & QMF_FLAG_CLDFB){
    for (i = 0; i < M; i++) {
      FIXP_QMF r1, i1, r2, i2;
      r1 = tReal[i];
      i2 = tImag[L - 1 - i];
      r2 = tReal[L - i - 1];
      i1 = tImag[i];

      tReal[i] = (r1 - i1)>>1;
      tImag[L - 1 - i] = -(r1 + i1)>>1;
      tReal[L - i - 1] =  (r2 - i2)>>1;
      tImag[i] = -(r2 + i2)>>1;
    }
  } else
  {
    /* The array accesses are negative to compensate the missing minus sign in the low and hi band gain. */
    /* 26 cycles on ARM926 */
    for (i = 0; i < M; i++) {
      FIXP_QMF r1, i1, r2, i2;
      r1 = -tReal[i];
      i2 = -tImag[L - 1 - i];
      r2 = -tReal[L - i - 1];
      i1 = -tImag[i];

      tReal[i] = (r1 - i1)>>1;
      tImag[L - 1 - i] = -(r1 + i1)>>1;
      tReal[L - i - 1] =  (r2 - i2)>>1;
      tImag[i] = -(r2 + i2)>>1;
    }
  }
}

void qmfSynthesisFilteringSlot( HANDLE_QMF_FILTER_BANK  synQmf,
                                const FIXP_QMF  *realSlot,
                                const FIXP_QMF  *imagSlot,
                                const int        scaleFactorLowBand,
                                const int        scaleFactorHighBand,
                                INT_PCM         *timeOut,
                                const int        stride,
                                FIXP_QMF        *pWorkBuffer)
{
    if (!(synQmf->flags & QMF_FLAG_LP))
      qmfInverseModulationHQ ( synQmf,
                               realSlot,
                               imagSlot,
                               scaleFactorLowBand,
                               scaleFactorHighBand,
                               pWorkBuffer
                             );
    else
    {
      if (synQmf->flags & QMF_FLAG_CLDFB) {
        qmfInverseModulationLP_odd ( synQmf,
                                 realSlot,
                                 scaleFactorLowBand,
                                 scaleFactorHighBand,
                                 pWorkBuffer
                               );
      } else {
        qmfInverseModulationLP_even ( synQmf,
                                 realSlot,
                                 scaleFactorLowBand,
                                 scaleFactorHighBand,
                                 pWorkBuffer
                               );
      }
    }

    if (synQmf->flags & QMF_FLAG_NONSYMMETRIC) {
        qmfSynPrototypeFirSlot_NonSymmetric (
                                 synQmf,
                                 pWorkBuffer,
                                 pWorkBuffer+synQmf->no_channels,
                                 timeOut,
                                 stride
                               );
    } else {
        qmfSynPrototypeFirSlot ( synQmf,
                                 pWorkBuffer,
                                 pWorkBuffer+synQmf->no_channels,
                                 timeOut,
                                 stride
                               );
    }
}


/*!
 *
 *
 * \brief Perform complex-valued subband synthesis of the
 *        low band and the high band and store the
 *        time domain data in timeOut
 *
 * First step: Calculate the proper scaling factor of current
 * spectral data in qmfReal/qmfImag, old spectral data in the overlap
 * range and filter states.
 *
 * Second step: Perform Frequency-to-Time mapping with inverse
 * Modulation slot-wise.
 *
 * Third step: Perform FIR-filter slot-wise. To save space for filter
 * states, the MAC operations are executed directly on the filter states
 * instead of accumulating several products in the accumulator. The
 * buffer shift at the end of the function should be replaced by a
 * modulo operation, which is available on some DSPs.
 *
 * Last step: Copy the upper part of the spectral data to the overlap buffer.
 *
 * The qmf coefficient table is symmetric. The symmetry is exploited by
 * shrinking the coefficient table to half the size. The addressing mode
 * takes care of the symmetries.  If the #define #QMFTABLE_FULL is set,
 * coefficient addressing works on the full table size. The code will be
 * slightly faster and slightly more compact.
 *
 * Workbuffer requirement: 2 x sizeof(**QmfBufferReal) * synQmf->no_channels
 */
void
qmfSynthesisFiltering( HANDLE_QMF_FILTER_BANK synQmf,       /*!< Handle of Qmf Synthesis Bank  */
                       FIXP_QMF  **QmfBufferReal,           /*!< Low and High band, real */
                       FIXP_QMF  **QmfBufferImag,           /*!< Low and High band, imag */
                       const QMF_SCALE_FACTOR *scaleFactor,
                       const INT   ov_len,                  /*!< split Slot of overlap and actual slots */
                       INT_PCM    *timeOut,                 /*!< Pointer to output */
                       const INT   stride,                  /*!< stride factor of output */
                       FIXP_QMF   *pWorkBuffer              /*!< pointer to temporal working buffer */
                      )
{
  int i;
  int L = synQmf->no_channels;
  SCHAR scaleFactorHighBand;
  SCHAR scaleFactorLowBand_ov, scaleFactorLowBand_no_ov;

  /* adapt scaling */
  scaleFactorHighBand = -ALGORITHMIC_SCALING_IN_ANALYSIS_FILTERBANK - scaleFactor->hb_scale;
  scaleFactorLowBand_ov = - ALGORITHMIC_SCALING_IN_ANALYSIS_FILTERBANK - scaleFactor->ov_lb_scale;
  scaleFactorLowBand_no_ov = - ALGORITHMIC_SCALING_IN_ANALYSIS_FILTERBANK - scaleFactor->lb_scale;

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

    SCHAR scaleFactorLowBand = (i<ov_len) ? scaleFactorLowBand_ov : scaleFactorLowBand_no_ov;

    if (!(synQmf->flags & QMF_FLAG_LP))
        QmfBufferImagSlot = QmfBufferImag[i];

    qmfSynthesisFilteringSlot(  synQmf,
                                QmfBufferReal[i],
                                QmfBufferImagSlot,
                                scaleFactorLowBand,
                                scaleFactorHighBand,
                                timeOut+(i*L*stride),
                                stride,
                                pWorkBuffer);
  } /* no_col loop  i  */

}


/*!
 *
 * \brief Create QMF filter bank instance
 *
 * \return 0 if successful
 *
 */
static int
qmfInitFilterBank (HANDLE_QMF_FILTER_BANK h_Qmf,     /*!< Handle to return */
                   void *pFilterStates,              /*!< Handle to filter states */
                   int noCols,                       /*!< Number of timeslots per frame */
                   int lsb,                          /*!< Lower end of QMF frequency range */
                   int usb,                          /*!< Upper end of QMF frequency range */
                   int no_channels,                  /*!< Number of channels (bands) */
                   UINT flags)                       /*!< flags */
{
  FDKmemclear(h_Qmf,sizeof(QMF_FILTER_BANK));

  if (flags & QMF_FLAG_MPSLDFB)
  {
    return -1;
  }

  if ( !(flags & QMF_FLAG_MPSLDFB) && (flags & QMF_FLAG_CLDFB) )
  {
    flags |= QMF_FLAG_NONSYMMETRIC;
    h_Qmf->filterScale = QMF_CLDFB_PFT_SCALE;

    h_Qmf->p_stride = 1;
    switch (no_channels) {
      case 64:
        h_Qmf->t_cos = qmf_phaseshift_cos64_cldfb;
        h_Qmf->t_sin = qmf_phaseshift_sin64_cldfb;
        h_Qmf->p_filter = qmf_cldfb_640;
        h_Qmf->FilterSize = 640;
        break;
      case 32:
        h_Qmf->t_cos = qmf_phaseshift_cos32_cldfb;
        h_Qmf->t_sin = qmf_phaseshift_sin32_cldfb;
        h_Qmf->p_filter = qmf_cldfb_320;
        h_Qmf->FilterSize = 320;
        break;
      default:
        return -1;
    }
  }

  if ( !(flags & QMF_FLAG_MPSLDFB) && ((flags & QMF_FLAG_CLDFB) == 0) )
  {
    switch (no_channels) {
      case 64:
        h_Qmf->p_filter = qmf_64;
        h_Qmf->t_cos = qmf_phaseshift_cos64;
        h_Qmf->t_sin = qmf_phaseshift_sin64;
        h_Qmf->p_stride = 1;
        h_Qmf->FilterSize = 640;
        h_Qmf->filterScale = 0;
        break;
      case 32:
        h_Qmf->p_filter = qmf_64;
        h_Qmf->t_cos = qmf_phaseshift_cos32;
        h_Qmf->t_sin = qmf_phaseshift_sin32;
        h_Qmf->p_stride = 2;
        h_Qmf->FilterSize = 640;
        h_Qmf->filterScale = 0;
        break;
      default:
        return -1;
    }
  }

  h_Qmf->flags = flags;

  h_Qmf->no_channels = no_channels;
  h_Qmf->no_col = noCols;

  h_Qmf->lsb = lsb;
  h_Qmf->usb = fMin(usb, h_Qmf->no_channels);

  h_Qmf->FilterStates = (void*)pFilterStates;

  h_Qmf->outScalefactor = ALGORITHMIC_SCALING_IN_ANALYSIS_FILTERBANK + ALGORITHMIC_SCALING_IN_SYNTHESIS_FILTERBANK + h_Qmf->filterScale;

  if (h_Qmf->p_stride == 2) {
    h_Qmf->outScalefactor -= 1;
  }
  h_Qmf->outGain = (FIXP_DBL)0x80000000; /* default init value will be not applied */

  return (0);
}

/*!
 *
 * \brief Adjust synthesis qmf filter states
 *
 * \return void
 *
 */
static inline void
qmfAdaptFilterStates (HANDLE_QMF_FILTER_BANK synQmf,     /*!< Handle of Qmf Filter Bank */
                      int scaleFactorDiff)               /*!< Scale factor difference to be applied */
{
  if (synQmf == NULL || synQmf->FilterStates == NULL) {
    return;
  }
  scaleValues((FIXP_QSS*)synQmf->FilterStates, synQmf->no_channels*(QMF_NO_POLY*2 - 1), scaleFactorDiff);
}

/*!
 *
 * \brief Create QMF filter bank instance
 *
 * Only the lower bands are obtained (upto anaQmf->lsb). For
 * a full bandwidth analysis it is required to set both anaQmf->lsb
 * and anaQmf->usb to the amount of QMF bands.
 *
 * \return 0 if succesful
 *
 */
int
qmfInitAnalysisFilterBank (HANDLE_QMF_FILTER_BANK h_Qmf,   /*!< Returns handle */
                           FIXP_QAS *pFilterStates,        /*!< Handle to filter states */
                           int noCols,                     /*!< Number of timeslots per frame */
                           int lsb,                        /*!< lower end of QMF */
                           int usb,                        /*!< upper end of QMF */
                           int no_channels,                /*!< Number of channels (bands) */
                           int flags)                      /*!< Low Power flag */
{
  int err = qmfInitFilterBank(h_Qmf, pFilterStates, noCols, lsb, usb, no_channels, flags);
  if ( !(flags & QMF_FLAG_KEEP_STATES) && (h_Qmf->FilterStates != NULL) ) {
    FDKmemclear(h_Qmf->FilterStates, (2*QMF_NO_POLY-1)*h_Qmf->no_channels*sizeof(FIXP_QAS));
  }

  return err;
}

/*!
 *
 * \brief Create QMF filter bank instance
 *
 * Only the lower bands are obtained (upto anaQmf->lsb). For
 * a full bandwidth analysis it is required to set both anaQmf->lsb
 * and anaQmf->usb to the amount of QMF bands.
 *
 * \return 0 if succesful
 *
 */
int
qmfInitSynthesisFilterBank (HANDLE_QMF_FILTER_BANK h_Qmf,   /*!< Returns handle */
                            FIXP_QSS *pFilterStates,        /*!< Handle to filter states */
                            int noCols,                     /*!< Number of timeslots per frame */
                            int lsb,                        /*!< lower end of QMF */
                            int usb,                        /*!< upper end of QMF */
                            int no_channels,                /*!< Number of channels (bands) */
                            int flags)                      /*!< Low Power flag */
{
  int oldOutScale = h_Qmf->outScalefactor;
  int err = qmfInitFilterBank(h_Qmf, pFilterStates, noCols, lsb, usb, no_channels, flags);
  if ( h_Qmf->FilterStates != NULL ) {
    if ( !(flags & QMF_FLAG_KEEP_STATES) ) {
      FDKmemclear(h_Qmf->FilterStates, (2*QMF_NO_POLY-1)*h_Qmf->no_channels*sizeof(FIXP_QSS));
    } else {
      qmfAdaptFilterStates(h_Qmf, oldOutScale-h_Qmf->outScalefactor);
    }
  }
  return err;
}




/*!
 *
 * \brief Change scale factor for output data and adjust qmf filter states
 *
 * \return void
 *
 */
void
qmfChangeOutScalefactor (HANDLE_QMF_FILTER_BANK synQmf,     /*!< Handle of Qmf Synthesis Bank */
                         int outScalefactor                 /*!< New scaling factor for output data */
                        )
{
  if (synQmf == NULL || synQmf->FilterStates == NULL) {
    return;
  }

  /* Add internal filterbank scale */
  outScalefactor += ALGORITHMIC_SCALING_IN_ANALYSIS_FILTERBANK + ALGORITHMIC_SCALING_IN_SYNTHESIS_FILTERBANK + synQmf->filterScale;

  if (synQmf->p_stride == 2) {
    outScalefactor -= 1;
  }

  /* adjust filter states when scale factor has been changed */
  if (synQmf->outScalefactor != outScalefactor)
  {
    int diff;

    if (outScalefactor > (SAMPLE_BITS - 1)) {
      outScalefactor = SAMPLE_BITS - 1;
    } else if (outScalefactor < (1 - SAMPLE_BITS)) {
      outScalefactor = 1 - SAMPLE_BITS;
    }

    diff = synQmf->outScalefactor - outScalefactor;

    qmfAdaptFilterStates(synQmf, diff);

    /* save new scale factor */
    synQmf->outScalefactor = outScalefactor;
  }
}

/*!
 *
 * \brief Change gain for output data
 *
 * \return void
 *
 */
void
qmfChangeOutGain (HANDLE_QMF_FILTER_BANK synQmf,     /*!< Handle of Qmf Synthesis Bank */
                  FIXP_DBL outputGain                /*!< New gain for output data */
                 )
{
  synQmf->outGain = outputGain;
}