aboutsummaryrefslogtreecommitdiffstats
path: root/libFDK/src/FDK_hybrid.cpp
blob: b661f82d81a6f5e38d6eae159629cd2cfe468df9 (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
/* -----------------------------------------------------------------------------
Software License for The Fraunhofer FDK AAC Codec Library for Android

© Copyright  1995 - 2018 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
----------------------------------------------------------------------------- */

/******************* Library for basic calculation routines ********************

   Author(s):   Markus Lohwasser

   Description: FDK Tools Hybrid Filterbank

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

#include "FDK_hybrid.h"

#include "fft.h"

/*--------------- defines -----------------------------*/
#define FFT_IDX_R(a) (2 * a)
#define FFT_IDX_I(a) (2 * a + 1)

#define HYB_COEF8_0 (0.00746082949812f)
#define HYB_COEF8_1 (0.02270420949825f)
#define HYB_COEF8_2 (0.04546865930473f)
#define HYB_COEF8_3 (0.07266113929591f)
#define HYB_COEF8_4 (0.09885108575264f)
#define HYB_COEF8_5 (0.11793710567217f)
#define HYB_COEF8_6 (0.12500000000000f)
#define HYB_COEF8_7 (HYB_COEF8_5)
#define HYB_COEF8_8 (HYB_COEF8_4)
#define HYB_COEF8_9 (HYB_COEF8_3)
#define HYB_COEF8_10 (HYB_COEF8_2)
#define HYB_COEF8_11 (HYB_COEF8_1)
#define HYB_COEF8_12 (HYB_COEF8_0)

/*--------------- structure definitions ---------------*/

#if defined(ARCH_PREFER_MULT_32x16)
#define FIXP_HTB FIXP_SGL              /* SGL data type. */
#define FIXP_HTP FIXP_SPK              /* Packed SGL data type. */
#define HTC(a) (FX_DBL2FXCONST_SGL(a)) /* Cast to SGL */
#define FL2FXCONST_HTB FL2FXCONST_SGL
#else
#define FIXP_HTB FIXP_DBL            /* SGL data type. */
#define FIXP_HTP FIXP_DPK            /* Packed DBL data type. */
#define HTC(a) ((FIXP_DBL)(LONG)(a)) /* Cast to DBL */
#define FL2FXCONST_HTB FL2FXCONST_DBL
#endif

#define HTCP(real, imag)     \
  {                          \
    { HTC(real), HTC(imag) } \
  } /* How to arrange the packed values. */

struct FDK_HYBRID_SETUP {
  UCHAR nrQmfBands;   /*!< Number of QMF bands to be converted to hybrid. */
  UCHAR nHybBands[3]; /*!< Number of Hybrid bands generated by nrQmfBands. */
  SCHAR kHybrid[3];   /*!< Filter configuration of each QMF band. */
  UCHAR protoLen;     /*!< Prototype filter length. */
  UCHAR filterDelay;  /*!< Delay caused by hybrid filter. */
  const INT
      *pReadIdxTable; /*!< Helper table to access input data ringbuffer. */
};

/*--------------- constants ---------------------------*/
static const INT ringbuffIdxTab[2 * 13] = {0, 1,  2,  3,  4, 5,  6,  7, 8,
                                           9, 10, 11, 12, 0, 1,  2,  3, 4,
                                           5, 6,  7,  8,  9, 10, 11, 12};

static const FDK_HYBRID_SETUP setup_3_16 = {3,  {8, 4, 4},    {8, 4, 4},
                                            13, (13 - 1) / 2, ringbuffIdxTab};
static const FDK_HYBRID_SETUP setup_3_12 = {3,  {8, 2, 2},    {8, 2, 2},
                                            13, (13 - 1) / 2, ringbuffIdxTab};
static const FDK_HYBRID_SETUP setup_3_10 = {3,  {6, 2, 2},    {-8, -2, 2},
                                            13, (13 - 1) / 2, ringbuffIdxTab};

static const FIXP_HTP HybFilterCoef8[] = {
    HTCP(0x10000000, 0x00000000), HTCP(0x0df26407, 0xfa391882),
    HTCP(0xff532109, 0x00acdef7), HTCP(0x08f26d36, 0xf70d92ca),
    HTCP(0xfee34b5f, 0x02af570f), HTCP(0x038f276e, 0xf7684793),
    HTCP(0x00000000, 0x05d1eac2), HTCP(0x00000000, 0x05d1eac2),
    HTCP(0x038f276e, 0x0897b86d), HTCP(0xfee34b5f, 0xfd50a8f1),
    HTCP(0x08f26d36, 0x08f26d36), HTCP(0xff532109, 0xff532109),
    HTCP(0x0df26407, 0x05c6e77e)};

static const FIXP_HTB HybFilterCoef2[3] = {FL2FXCONST_HTB(0.01899487526049f),
                                           FL2FXCONST_HTB(-0.07293139167538f),
                                           FL2FXCONST_HTB(0.30596630545168f)};

static const FIXP_HTB HybFilterCoef4[13] = {FL2FXCONST_HTB(-0.00305151927305f),
                                            FL2FXCONST_HTB(-0.00794862316203f),
                                            FL2FXCONST_HTB(0.0f),
                                            FL2FXCONST_HTB(0.04318924038756f),
                                            FL2FXCONST_HTB(0.12542448210445f),
                                            FL2FXCONST_HTB(0.21227807049160f),
                                            FL2FXCONST_HTB(0.25f),
                                            FL2FXCONST_HTB(0.21227807049160f),
                                            FL2FXCONST_HTB(0.12542448210445f),
                                            FL2FXCONST_HTB(0.04318924038756f),
                                            FL2FXCONST_HTB(0.0f),
                                            FL2FXCONST_HTB(-0.00794862316203f),
                                            FL2FXCONST_HTB(-0.00305151927305f)};

/*--------------- function declarations ---------------*/
static INT kChannelFiltering(const FIXP_DBL *const pQmfReal,
                             const FIXP_DBL *const pQmfImag,
                             const INT *const pReadIdx,
                             FIXP_DBL *const mHybridReal,
                             FIXP_DBL *const mHybridImag,
                             const SCHAR hybridConfig);

/*--------------- function definitions ----------------*/

INT FDKhybridAnalysisOpen(HANDLE_FDK_ANA_HYB_FILTER hAnalysisHybFilter,
                          FIXP_DBL *const pLFmemory, const UINT LFmemorySize,
                          FIXP_DBL *const pHFmemory, const UINT HFmemorySize) {
  INT err = 0;

  /* Save pointer to extern memory. */
  hAnalysisHybFilter->pLFmemory = pLFmemory;
  hAnalysisHybFilter->LFmemorySize = LFmemorySize;

  hAnalysisHybFilter->pHFmemory = pHFmemory;
  hAnalysisHybFilter->HFmemorySize = HFmemorySize;

  return err;
}

INT FDKhybridAnalysisInit(HANDLE_FDK_ANA_HYB_FILTER hAnalysisHybFilter,
                          const FDK_HYBRID_MODE mode, const INT qmfBands,
                          const INT cplxBands, const INT initStatesFlag) {
  int k;
  INT err = 0;
  FIXP_DBL *pMem = NULL;
  HANDLE_FDK_HYBRID_SETUP setup = NULL;

  switch (mode) {
    case THREE_TO_TEN:
      setup = &setup_3_10;
      break;
    case THREE_TO_TWELVE:
      setup = &setup_3_12;
      break;
    case THREE_TO_SIXTEEN:
      setup = &setup_3_16;
      break;
    default:
      err = -1;
      goto bail;
  }

  /* Initialize handle. */
  hAnalysisHybFilter->pSetup = setup;
  if (initStatesFlag) {
    hAnalysisHybFilter->bufferLFpos = setup->protoLen - 1;
    hAnalysisHybFilter->bufferHFpos = 0;
  }
  hAnalysisHybFilter->nrBands = qmfBands;
  hAnalysisHybFilter->cplxBands = cplxBands;
  hAnalysisHybFilter->hfMode = 0;

  /* Check available memory. */
  if (((2 * setup->nrQmfBands * setup->protoLen * sizeof(FIXP_DBL)) >
       hAnalysisHybFilter->LFmemorySize)) {
    err = -2;
    goto bail;
  }
  if (hAnalysisHybFilter->HFmemorySize != 0) {
    if (((setup->filterDelay *
          ((qmfBands - setup->nrQmfBands) + (cplxBands - setup->nrQmfBands)) *
          sizeof(FIXP_DBL)) > hAnalysisHybFilter->HFmemorySize)) {
      err = -3;
      goto bail;
    }
  }

  /* Distribute LF memory. */
  pMem = hAnalysisHybFilter->pLFmemory;
  for (k = 0; k < setup->nrQmfBands; k++) {
    hAnalysisHybFilter->bufferLFReal[k] = pMem;
    pMem += setup->protoLen;
    hAnalysisHybFilter->bufferLFImag[k] = pMem;
    pMem += setup->protoLen;
  }

  /* Distribute HF memory. */
  if (hAnalysisHybFilter->HFmemorySize != 0) {
    pMem = hAnalysisHybFilter->pHFmemory;
    for (k = 0; k < setup->filterDelay; k++) {
      hAnalysisHybFilter->bufferHFReal[k] = pMem;
      pMem += (qmfBands - setup->nrQmfBands);
      hAnalysisHybFilter->bufferHFImag[k] = pMem;
      pMem += (cplxBands - setup->nrQmfBands);
    }
  }

  if (initStatesFlag) {
    /* Clear LF buffer */
    for (k = 0; k < setup->nrQmfBands; k++) {
      FDKmemclear(hAnalysisHybFilter->bufferLFReal[k],
                  setup->protoLen * sizeof(FIXP_DBL));
      FDKmemclear(hAnalysisHybFilter->bufferLFImag[k],
                  setup->protoLen * sizeof(FIXP_DBL));
    }

    if (hAnalysisHybFilter->HFmemorySize != 0) {
      if (qmfBands > setup->nrQmfBands) {
        /* Clear HF buffer */
        for (k = 0; k < setup->filterDelay; k++) {
          FDKmemclear(hAnalysisHybFilter->bufferHFReal[k],
                      (qmfBands - setup->nrQmfBands) * sizeof(FIXP_DBL));
          FDKmemclear(hAnalysisHybFilter->bufferHFImag[k],
                      (cplxBands - setup->nrQmfBands) * sizeof(FIXP_DBL));
        }
      }
    }
  }

bail:
  return err;
}

INT FDKhybridAnalysisScaleStates(HANDLE_FDK_ANA_HYB_FILTER hAnalysisHybFilter,
                                 const INT scalingValue) {
  INT err = 0;

  if (hAnalysisHybFilter == NULL) {
    err = 1; /* invalid handle */
  } else {
    int k;
    HANDLE_FDK_HYBRID_SETUP setup = hAnalysisHybFilter->pSetup;

    /* Scale LF buffer */
    for (k = 0; k < setup->nrQmfBands; k++) {
      scaleValues(hAnalysisHybFilter->bufferLFReal[k], setup->protoLen,
                  scalingValue);
      scaleValues(hAnalysisHybFilter->bufferLFImag[k], setup->protoLen,
                  scalingValue);
    }
    if (hAnalysisHybFilter->nrBands > setup->nrQmfBands) {
      /* Scale HF buffer */
      for (k = 0; k < setup->filterDelay; k++) {
        scaleValues(hAnalysisHybFilter->bufferHFReal[k],
                    (hAnalysisHybFilter->nrBands - setup->nrQmfBands),
                    scalingValue);
        scaleValues(hAnalysisHybFilter->bufferHFImag[k],
                    (hAnalysisHybFilter->cplxBands - setup->nrQmfBands),
                    scalingValue);
      }
    }
  }
  return err;
}

INT FDKhybridAnalysisApply(HANDLE_FDK_ANA_HYB_FILTER hAnalysisHybFilter,
                           const FIXP_DBL *const pQmfReal,
                           const FIXP_DBL *const pQmfImag,
                           FIXP_DBL *const pHybridReal,
                           FIXP_DBL *const pHybridImag) {
  int k, hybOffset = 0;
  INT err = 0;
  const int nrQmfBandsLF =
      hAnalysisHybFilter->pSetup
          ->nrQmfBands; /* number of QMF bands to be converted to hybrid */

  const int writIndex = hAnalysisHybFilter->bufferLFpos;
  int readIndex = hAnalysisHybFilter->bufferLFpos;

  if (++readIndex >= hAnalysisHybFilter->pSetup->protoLen) readIndex = 0;
  const INT *pBufferLFreadIdx =
      &hAnalysisHybFilter->pSetup->pReadIdxTable[readIndex];

  /*
   * LF buffer.
   */
  for (k = 0; k < nrQmfBandsLF; k++) {
    /* New input sample. */
    hAnalysisHybFilter->bufferLFReal[k][writIndex] = pQmfReal[k];
    hAnalysisHybFilter->bufferLFImag[k][writIndex] = pQmfImag[k];

    /* Perform hybrid filtering. */
    err |=
        kChannelFiltering(hAnalysisHybFilter->bufferLFReal[k],
                          hAnalysisHybFilter->bufferLFImag[k], pBufferLFreadIdx,
                          pHybridReal + hybOffset, pHybridImag + hybOffset,
                          hAnalysisHybFilter->pSetup->kHybrid[k]);

    hybOffset += hAnalysisHybFilter->pSetup->nHybBands[k];
  }

  hAnalysisHybFilter->bufferLFpos =
      readIndex; /* Index where to write next input sample. */

  if (hAnalysisHybFilter->nrBands > nrQmfBandsLF) {
    /*
     * HF buffer.
     */
    if (hAnalysisHybFilter->hfMode != 0) {
      /* HF delay compensation was applied outside. */
      FDKmemcpy(
          pHybridReal + hybOffset, &pQmfReal[nrQmfBandsLF],
          (hAnalysisHybFilter->nrBands - nrQmfBandsLF) * sizeof(FIXP_DBL));
      FDKmemcpy(
          pHybridImag + hybOffset, &pQmfImag[nrQmfBandsLF],
          (hAnalysisHybFilter->cplxBands - nrQmfBandsLF) * sizeof(FIXP_DBL));
    } else {
      FDK_ASSERT(hAnalysisHybFilter->HFmemorySize != 0);
      /* HF delay compensation, filterlength/2. */
      FDKmemcpy(
          pHybridReal + hybOffset,
          hAnalysisHybFilter->bufferHFReal[hAnalysisHybFilter->bufferHFpos],
          (hAnalysisHybFilter->nrBands - nrQmfBandsLF) * sizeof(FIXP_DBL));
      FDKmemcpy(
          pHybridImag + hybOffset,
          hAnalysisHybFilter->bufferHFImag[hAnalysisHybFilter->bufferHFpos],
          (hAnalysisHybFilter->cplxBands - nrQmfBandsLF) * sizeof(FIXP_DBL));

      FDKmemcpy(
          hAnalysisHybFilter->bufferHFReal[hAnalysisHybFilter->bufferHFpos],
          &pQmfReal[nrQmfBandsLF],
          (hAnalysisHybFilter->nrBands - nrQmfBandsLF) * sizeof(FIXP_DBL));
      FDKmemcpy(
          hAnalysisHybFilter->bufferHFImag[hAnalysisHybFilter->bufferHFpos],
          &pQmfImag[nrQmfBandsLF],
          (hAnalysisHybFilter->cplxBands - nrQmfBandsLF) * sizeof(FIXP_DBL));

      if (++hAnalysisHybFilter->bufferHFpos >=
          hAnalysisHybFilter->pSetup->filterDelay)
        hAnalysisHybFilter->bufferHFpos = 0;
    }
  } /* process HF part*/

  return err;
}

INT FDKhybridAnalysisClose(HANDLE_FDK_ANA_HYB_FILTER hAnalysisHybFilter) {
  INT err = 0;

  if (hAnalysisHybFilter != NULL) {
    hAnalysisHybFilter->pLFmemory = NULL;
    hAnalysisHybFilter->pHFmemory = NULL;
    hAnalysisHybFilter->LFmemorySize = 0;
    hAnalysisHybFilter->HFmemorySize = 0;
  }

  return err;
}

INT FDKhybridSynthesisInit(HANDLE_FDK_SYN_HYB_FILTER hSynthesisHybFilter,
                           const FDK_HYBRID_MODE mode, const INT qmfBands,
                           const INT cplxBands) {
  INT err = 0;
  HANDLE_FDK_HYBRID_SETUP setup = NULL;

  switch (mode) {
    case THREE_TO_TEN:
      setup = &setup_3_10;
      break;
    case THREE_TO_TWELVE:
      setup = &setup_3_12;
      break;
    case THREE_TO_SIXTEEN:
      setup = &setup_3_16;
      break;
    default:
      err = -1;
      goto bail;
  }

  hSynthesisHybFilter->pSetup = setup;
  hSynthesisHybFilter->nrBands = qmfBands;
  hSynthesisHybFilter->cplxBands = cplxBands;

bail:
  return err;
}

void FDKhybridSynthesisApply(HANDLE_FDK_SYN_HYB_FILTER hSynthesisHybFilter,
                             const FIXP_DBL *const pHybridReal,
                             const FIXP_DBL *const pHybridImag,
                             FIXP_DBL *const pQmfReal,
                             FIXP_DBL *const pQmfImag) {
  int k, n, hybOffset = 0;
  const INT nrQmfBandsLF = hSynthesisHybFilter->pSetup->nrQmfBands;

  /*
   * LF buffer.
   */
  for (k = 0; k < nrQmfBandsLF; k++) {
    const int nHybBands = hSynthesisHybFilter->pSetup->nHybBands[k];

    FIXP_DBL accu1 = FL2FXCONST_DBL(0.f);
    FIXP_DBL accu2 = FL2FXCONST_DBL(0.f);

    /* Perform hybrid filtering. */
    for (n = 0; n < nHybBands; n++) {
      accu1 += pHybridReal[hybOffset + n];
      accu2 += pHybridImag[hybOffset + n];
    }
    pQmfReal[k] = accu1;
    pQmfImag[k] = accu2;

    hybOffset += nHybBands;
  }

  if (hSynthesisHybFilter->nrBands > nrQmfBandsLF) {
    /*
     * HF buffer.
     */
    FDKmemcpy(&pQmfReal[nrQmfBandsLF], &pHybridReal[hybOffset],
              (hSynthesisHybFilter->nrBands - nrQmfBandsLF) * sizeof(FIXP_DBL));
    FDKmemcpy(
        &pQmfImag[nrQmfBandsLF], &pHybridImag[hybOffset],
        (hSynthesisHybFilter->cplxBands - nrQmfBandsLF) * sizeof(FIXP_DBL));
  }

  return;
}

static void dualChannelFiltering(const FIXP_DBL *const pQmfReal,
                                 const FIXP_DBL *const pQmfImag,
                                 const INT *const pReadIdx,
                                 FIXP_DBL *const mHybridReal,
                                 FIXP_DBL *const mHybridImag,
                                 const INT invert) {
  FIXP_DBL r1, r6;
  FIXP_DBL i1, i6;

  const FIXP_HTB f0 = HybFilterCoef2[0]; /* corresponds to p1 and p11 */
  const FIXP_HTB f1 = HybFilterCoef2[1]; /* corresponds to p3 and p9  */
  const FIXP_HTB f2 = HybFilterCoef2[2]; /* corresponds to p5 and p7  */

  /* symmetric filter coefficients */
  r1 = fMultDiv2(f0, pQmfReal[pReadIdx[1]]) +
       fMultDiv2(f0, pQmfReal[pReadIdx[11]]);
  i1 = fMultDiv2(f0, pQmfImag[pReadIdx[1]]) +
       fMultDiv2(f0, pQmfImag[pReadIdx[11]]);
  r1 += fMultDiv2(f1, pQmfReal[pReadIdx[3]]) +
        fMultDiv2(f1, pQmfReal[pReadIdx[9]]);
  i1 += fMultDiv2(f1, pQmfImag[pReadIdx[3]]) +
        fMultDiv2(f1, pQmfImag[pReadIdx[9]]);
  r1 += fMultDiv2(f2, pQmfReal[pReadIdx[5]]) +
        fMultDiv2(f2, pQmfReal[pReadIdx[7]]);
  i1 += fMultDiv2(f2, pQmfImag[pReadIdx[5]]) +
        fMultDiv2(f2, pQmfImag[pReadIdx[7]]);

  r6 = pQmfReal[pReadIdx[6]] >> 2;
  i6 = pQmfImag[pReadIdx[6]] >> 2;

  FDK_ASSERT((invert == 0) || (invert == 1));
  mHybridReal[0 + invert] = (r6 + r1) << 1;
  mHybridImag[0 + invert] = (i6 + i1) << 1;

  mHybridReal[1 - invert] = (r6 - r1) << 1;
  mHybridImag[1 - invert] = (i6 - i1) << 1;
}

static void fourChannelFiltering(const FIXP_DBL *const pQmfReal,
                                 const FIXP_DBL *const pQmfImag,
                                 const INT *const pReadIdx,
                                 FIXP_DBL *const mHybridReal,
                                 FIXP_DBL *const mHybridImag,
                                 const INT invert) {
  const FIXP_HTB *p = HybFilterCoef4;

  FIXP_DBL fft[8];

  static const FIXP_DBL cr[13] = {
      FL2FXCONST_DBL(0.f),  FL2FXCONST_DBL(-0.70710678118655f),
      FL2FXCONST_DBL(-1.f), FL2FXCONST_DBL(-0.70710678118655f),
      FL2FXCONST_DBL(0.f),  FL2FXCONST_DBL(0.70710678118655f),
      FL2FXCONST_DBL(1.f),  FL2FXCONST_DBL(0.70710678118655f),
      FL2FXCONST_DBL(0.f),  FL2FXCONST_DBL(-0.70710678118655f),
      FL2FXCONST_DBL(-1.f), FL2FXCONST_DBL(-0.70710678118655f),
      FL2FXCONST_DBL(0.f)};
  static const FIXP_DBL ci[13] = {
      FL2FXCONST_DBL(-1.f), FL2FXCONST_DBL(-0.70710678118655f),
      FL2FXCONST_DBL(0.f),  FL2FXCONST_DBL(0.70710678118655f),
      FL2FXCONST_DBL(1.f),  FL2FXCONST_DBL(0.70710678118655f),
      FL2FXCONST_DBL(0.f),  FL2FXCONST_DBL(-0.70710678118655f),
      FL2FXCONST_DBL(-1.f), FL2FXCONST_DBL(-0.70710678118655f),
      FL2FXCONST_DBL(0.f),  FL2FXCONST_DBL(0.70710678118655f),
      FL2FXCONST_DBL(1.f)};

  /* FIR filter. */
  /* pre twiddeling with pre-twiddling coefficients c[n]  */
  /* multiplication with filter coefficients p[n]         */
  /* hint: (a + ib)*(c + id) = (a*c - b*d) + i(a*d + b*c) */
  /* write to fft coefficient n'                          */
  fft[FFT_IDX_R(0)] =
      (fMult(p[10], (fMultSub(fMultDiv2(cr[2], pQmfReal[pReadIdx[2]]), ci[2],
                              pQmfImag[pReadIdx[2]]))) +
       fMult(p[6], (fMultSub(fMultDiv2(cr[6], pQmfReal[pReadIdx[6]]), ci[6],
                             pQmfImag[pReadIdx[6]]))) +
       fMult(p[2], (fMultSub(fMultDiv2(cr[10], pQmfReal[pReadIdx[10]]), ci[10],
                             pQmfImag[pReadIdx[10]]))));
  fft[FFT_IDX_I(0)] =
      (fMult(p[10], (fMultAdd(fMultDiv2(ci[2], pQmfReal[pReadIdx[2]]), cr[2],
                              pQmfImag[pReadIdx[2]]))) +
       fMult(p[6], (fMultAdd(fMultDiv2(ci[6], pQmfReal[pReadIdx[6]]), cr[6],
                             pQmfImag[pReadIdx[6]]))) +
       fMult(p[2], (fMultAdd(fMultDiv2(ci[10], pQmfReal[pReadIdx[10]]), cr[10],
                             pQmfImag[pReadIdx[10]]))));

  /* twiddle dee dum */
  fft[FFT_IDX_R(1)] =
      (fMult(p[9], (fMultSub(fMultDiv2(cr[3], pQmfReal[pReadIdx[3]]), ci[3],
                             pQmfImag[pReadIdx[3]]))) +
       fMult(p[5], (fMultSub(fMultDiv2(cr[7], pQmfReal[pReadIdx[7]]), ci[7],
                             pQmfImag[pReadIdx[7]]))) +
       fMult(p[1], (fMultSub(fMultDiv2(cr[11], pQmfReal[pReadIdx[11]]), ci[11],
                             pQmfImag[pReadIdx[11]]))));
  fft[FFT_IDX_I(1)] =
      (fMult(p[9], (fMultAdd(fMultDiv2(ci[3], pQmfReal[pReadIdx[3]]), cr[3],
                             pQmfImag[pReadIdx[3]]))) +
       fMult(p[5], (fMultAdd(fMultDiv2(ci[7], pQmfReal[pReadIdx[7]]), cr[7],
                             pQmfImag[pReadIdx[7]]))) +
       fMult(p[1], (fMultAdd(fMultDiv2(ci[11], pQmfReal[pReadIdx[11]]), cr[11],
                             pQmfImag[pReadIdx[11]]))));

  /* twiddle dee dee */
  fft[FFT_IDX_R(2)] =
      (fMult(p[12], (fMultSub(fMultDiv2(cr[0], pQmfReal[pReadIdx[0]]), ci[0],
                              pQmfImag[pReadIdx[0]]))) +
       fMult(p[8], (fMultSub(fMultDiv2(cr[4], pQmfReal[pReadIdx[4]]), ci[4],
                             pQmfImag[pReadIdx[4]]))) +
       fMult(p[4], (fMultSub(fMultDiv2(cr[8], pQmfReal[pReadIdx[8]]), ci[8],
                             pQmfImag[pReadIdx[8]]))) +
       fMult(p[0], (fMultSub(fMultDiv2(cr[12], pQmfReal[pReadIdx[12]]), ci[12],
                             pQmfImag[pReadIdx[12]]))));
  fft[FFT_IDX_I(2)] =
      (fMult(p[12], (fMultAdd(fMultDiv2(ci[0], pQmfReal[pReadIdx[0]]), cr[0],
                              pQmfImag[pReadIdx[0]]))) +
       fMult(p[8], (fMultAdd(fMultDiv2(ci[4], pQmfReal[pReadIdx[4]]), cr[4],
                             pQmfImag[pReadIdx[4]]))) +
       fMult(p[4], (fMultAdd(fMultDiv2(ci[8], pQmfReal[pReadIdx[8]]), cr[8],
                             pQmfImag[pReadIdx[8]]))) +
       fMult(p[0], (fMultAdd(fMultDiv2(ci[12], pQmfReal[pReadIdx[12]]), cr[12],
                             pQmfImag[pReadIdx[12]]))));

  fft[FFT_IDX_R(3)] =
      (fMult(p[11], (fMultSub(fMultDiv2(cr[1], pQmfReal[pReadIdx[1]]), ci[1],
                              pQmfImag[pReadIdx[1]]))) +
       fMult(p[7], (fMultSub(fMultDiv2(cr[5], pQmfReal[pReadIdx[5]]), ci[5],
                             pQmfImag[pReadIdx[5]]))) +
       fMult(p[3], (fMultSub(fMultDiv2(cr[9], pQmfReal[pReadIdx[9]]), ci[9],
                             pQmfImag[pReadIdx[9]]))));
  fft[FFT_IDX_I(3)] =
      (fMult(p[11], (fMultAdd(fMultDiv2(ci[1], pQmfReal[pReadIdx[1]]), cr[1],
                              pQmfImag[pReadIdx[1]]))) +
       fMult(p[7], (fMultAdd(fMultDiv2(ci[5], pQmfReal[pReadIdx[5]]), cr[5],
                             pQmfImag[pReadIdx[5]]))) +
       fMult(p[3], (fMultAdd(fMultDiv2(ci[9], pQmfReal[pReadIdx[9]]), cr[9],
                             pQmfImag[pReadIdx[9]]))));

  /* fft modulation                                                    */
  /* here: fast manual fft modulation for a fft of length M=4          */
  /* fft_4{x[n]} = x[0]*exp(-i*2*pi/4*m*0) + x[1]*exp(-i*2*pi/4*m*1) +
  x[2]*exp(-i*2*pi/4*m*2) + x[3]*exp(-i*2*pi/4*m*3)   */

  /*
  fft bin m=0:
  X[0, n] = x[0] +   x[1] + x[2] +   x[3]
  */
  mHybridReal[0] = fft[FFT_IDX_R(0)] + fft[FFT_IDX_R(1)] + fft[FFT_IDX_R(2)] +
                   fft[FFT_IDX_R(3)];
  mHybridImag[0] = fft[FFT_IDX_I(0)] + fft[FFT_IDX_I(1)] + fft[FFT_IDX_I(2)] +
                   fft[FFT_IDX_I(3)];

  /*
  fft bin m=1:
  X[1, n] = x[0] - i*x[1] - x[2] + i*x[3]
  */
  mHybridReal[1] = fft[FFT_IDX_R(0)] + fft[FFT_IDX_I(1)] - fft[FFT_IDX_R(2)] -
                   fft[FFT_IDX_I(3)];
  mHybridImag[1] = fft[FFT_IDX_I(0)] - fft[FFT_IDX_R(1)] - fft[FFT_IDX_I(2)] +
                   fft[FFT_IDX_R(3)];

  /*
  fft bin m=2:
  X[2, n] = x[0] -   x[1] + x[2] -   x[3]
  */
  mHybridReal[2] = fft[FFT_IDX_R(0)] - fft[FFT_IDX_R(1)] + fft[FFT_IDX_R(2)] -
                   fft[FFT_IDX_R(3)];
  mHybridImag[2] = fft[FFT_IDX_I(0)] - fft[FFT_IDX_I(1)] + fft[FFT_IDX_I(2)] -
                   fft[FFT_IDX_I(3)];

  /*
  fft bin m=3:
  X[3, n] = x[0] + j*x[1] - x[2] - j*x[3]
  */
  mHybridReal[3] = fft[FFT_IDX_R(0)] - fft[FFT_IDX_I(1)] - fft[FFT_IDX_R(2)] +
                   fft[FFT_IDX_I(3)];
  mHybridImag[3] = fft[FFT_IDX_I(0)] + fft[FFT_IDX_R(1)] - fft[FFT_IDX_I(2)] -
                   fft[FFT_IDX_R(3)];
}

static void eightChannelFiltering(const FIXP_DBL *const pQmfReal,
                                  const FIXP_DBL *const pQmfImag,
                                  const INT *const pReadIdx,
                                  FIXP_DBL *const mHybridReal,
                                  FIXP_DBL *const mHybridImag,
                                  const INT invert) {
  const FIXP_HTP *p = HybFilterCoef8;
  INT k, sc;

  FIXP_DBL mfft[16 + ALIGNMENT_DEFAULT];
  FIXP_DBL *pfft = (FIXP_DBL *)ALIGN_PTR(mfft);

  FIXP_DBL accu1, accu2, accu3, accu4;

  /* pre twiddeling */
  pfft[FFT_IDX_R(0)] =
      pQmfReal[pReadIdx[6]] >>
      (3 + 1); /* fMultDiv2(p[0].v.re, pQmfReal[pReadIdx[6]]); */
  pfft[FFT_IDX_I(0)] =
      pQmfImag[pReadIdx[6]] >>
      (3 + 1); /* fMultDiv2(p[0].v.re, pQmfImag[pReadIdx[6]]); */

  cplxMultDiv2(&accu1, &accu2, pQmfReal[pReadIdx[7]], pQmfImag[pReadIdx[7]],
               p[1]);
  pfft[FFT_IDX_R(1)] = accu1;
  pfft[FFT_IDX_I(1)] = accu2;

  cplxMultDiv2(&accu1, &accu2, pQmfReal[pReadIdx[0]], pQmfImag[pReadIdx[0]],
               p[2]);
  cplxMultDiv2(&accu3, &accu4, pQmfReal[pReadIdx[8]], pQmfImag[pReadIdx[8]],
               p[3]);
  pfft[FFT_IDX_R(2)] = accu1 + accu3;
  pfft[FFT_IDX_I(2)] = accu2 + accu4;

  cplxMultDiv2(&accu1, &accu2, pQmfReal[pReadIdx[1]], pQmfImag[pReadIdx[1]],
               p[4]);
  cplxMultDiv2(&accu3, &accu4, pQmfReal[pReadIdx[9]], pQmfImag[pReadIdx[9]],
               p[5]);
  pfft[FFT_IDX_R(3)] = accu1 + accu3;
  pfft[FFT_IDX_I(3)] = accu2 + accu4;

  pfft[FFT_IDX_R(4)] = fMultDiv2(pQmfImag[pReadIdx[10]], p[7].v.im) -
                       fMultDiv2(pQmfImag[pReadIdx[2]], p[6].v.im);
  pfft[FFT_IDX_I(4)] = fMultDiv2(pQmfReal[pReadIdx[2]], p[6].v.im) -
                       fMultDiv2(pQmfReal[pReadIdx[10]], p[7].v.im);

  cplxMultDiv2(&accu1, &accu2, pQmfReal[pReadIdx[3]], pQmfImag[pReadIdx[3]],
               p[8]);
  cplxMultDiv2(&accu3, &accu4, pQmfReal[pReadIdx[11]], pQmfImag[pReadIdx[11]],
               p[9]);
  pfft[FFT_IDX_R(5)] = accu1 + accu3;
  pfft[FFT_IDX_I(5)] = accu2 + accu4;

  cplxMultDiv2(&accu1, &accu2, pQmfReal[pReadIdx[4]], pQmfImag[pReadIdx[4]],
               p[10]);
  cplxMultDiv2(&accu3, &accu4, pQmfReal[pReadIdx[12]], pQmfImag[pReadIdx[12]],
               p[11]);
  pfft[FFT_IDX_R(6)] = accu1 + accu3;
  pfft[FFT_IDX_I(6)] = accu2 + accu4;

  cplxMultDiv2(&accu1, &accu2, pQmfReal[pReadIdx[5]], pQmfImag[pReadIdx[5]],
               p[12]);
  pfft[FFT_IDX_R(7)] = accu1;
  pfft[FFT_IDX_I(7)] = accu2;

  /* fft modulation */
  fft_8(pfft);
  sc = 1 + 2;

  if (invert) {
    mHybridReal[0] = pfft[FFT_IDX_R(7)] << sc;
    mHybridImag[0] = pfft[FFT_IDX_I(7)] << sc;
    mHybridReal[1] = pfft[FFT_IDX_R(0)] << sc;
    mHybridImag[1] = pfft[FFT_IDX_I(0)] << sc;

    mHybridReal[2] = pfft[FFT_IDX_R(6)] << sc;
    mHybridImag[2] = pfft[FFT_IDX_I(6)] << sc;
    mHybridReal[3] = pfft[FFT_IDX_R(1)] << sc;
    mHybridImag[3] = pfft[FFT_IDX_I(1)] << sc;

    mHybridReal[4] = pfft[FFT_IDX_R(2)] << sc;
    mHybridReal[4] += pfft[FFT_IDX_R(5)] << sc;
    mHybridImag[4] = pfft[FFT_IDX_I(2)] << sc;
    mHybridImag[4] += pfft[FFT_IDX_I(5)] << sc;

    mHybridReal[5] = pfft[FFT_IDX_R(3)] << sc;
    mHybridReal[5] += pfft[FFT_IDX_R(4)] << sc;
    mHybridImag[5] = pfft[FFT_IDX_I(3)] << sc;
    mHybridImag[5] += pfft[FFT_IDX_I(4)] << sc;
  } else {
    for (k = 0; k < 8; k++) {
      mHybridReal[k] = pfft[FFT_IDX_R(k)] << sc;
      mHybridImag[k] = pfft[FFT_IDX_I(k)] << sc;
    }
  }
}

static INT kChannelFiltering(const FIXP_DBL *const pQmfReal,
                             const FIXP_DBL *const pQmfImag,
                             const INT *const pReadIdx,
                             FIXP_DBL *const mHybridReal,
                             FIXP_DBL *const mHybridImag,
                             const SCHAR hybridConfig) {
  INT err = 0;

  switch (hybridConfig) {
    case 2:
    case -2:
      dualChannelFiltering(pQmfReal, pQmfImag, pReadIdx, mHybridReal,
                           mHybridImag, (hybridConfig < 0) ? 1 : 0);
      break;
    case 4:
    case -4:
      fourChannelFiltering(pQmfReal, pQmfImag, pReadIdx, mHybridReal,
                           mHybridImag, (hybridConfig < 0) ? 1 : 0);
      break;
    case 8:
    case -8:
      eightChannelFiltering(pQmfReal, pQmfImag, pReadIdx, mHybridReal,
                            mHybridImag, (hybridConfig < 0) ? 1 : 0);
      break;
    default:
      err = -1;
  }

  return err;
}