Coverage Report

Created: 2023-03-26 06:07

/src/aac/libSBRdec/src/sbrdec_drc.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2020 Fraunhofer-Gesellschaft zur Förderung der angewandten
5
Forschung e.V. All rights reserved.
6
7
 1.    INTRODUCTION
8
The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9
that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10
scheme for digital audio. This FDK AAC Codec software is intended to be used on
11
a wide variety of Android devices.
12
13
AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14
general perceptual audio codecs. AAC-ELD is considered the best-performing
15
full-bandwidth communications codec by independent studies and is widely
16
deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17
specifications.
18
19
Patent licenses for necessary patent claims for the FDK AAC Codec (including
20
those of Fraunhofer) may be obtained through Via Licensing
21
(www.vialicensing.com) or through the respective patent owners individually for
22
the purpose of encoding or decoding bit streams in products that are compliant
23
with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24
Android devices already license these patent claims through Via Licensing or
25
directly from the patent owners, and therefore FDK AAC Codec software may
26
already be covered under those patent licenses when it is used for those
27
licensed purposes only.
28
29
Commercially-licensed AAC software libraries, including floating-point versions
30
with enhanced sound quality, are also available from Fraunhofer. Users are
31
encouraged to check the Fraunhofer website for additional applications
32
information and documentation.
33
34
2.    COPYRIGHT LICENSE
35
36
Redistribution and use in source and binary forms, with or without modification,
37
are permitted without payment of copyright license fees provided that you
38
satisfy the following conditions:
39
40
You must retain the complete text of this software license in redistributions of
41
the FDK AAC Codec or your modifications thereto in source code form.
42
43
You must retain the complete text of this software license in the documentation
44
and/or other materials provided with redistributions of the FDK AAC Codec or
45
your modifications thereto in binary form. You must make available free of
46
charge copies of the complete source code of the FDK AAC Codec and your
47
modifications thereto to recipients of copies in binary form.
48
49
The name of Fraunhofer may not be used to endorse or promote products derived
50
from this library without prior written permission.
51
52
You may not charge copyright license fees for anyone to use, copy or distribute
53
the FDK AAC Codec software or your modifications thereto.
54
55
Your modified versions of the FDK AAC Codec must carry prominent notices stating
56
that you changed the software and the date of any change. For modified versions
57
of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58
must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59
AAC Codec Library for Android."
60
61
3.    NO PATENT LICENSE
62
63
NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64
limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65
Fraunhofer provides no warranty of patent non-infringement with respect to this
66
software.
67
68
You may use this FDK AAC Codec software or modifications thereto only for
69
purposes that are authorized by appropriate patent licenses.
70
71
4.    DISCLAIMER
72
73
This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74
holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75
including but not limited to the implied warranties of merchantability and
76
fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77
CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78
or consequential damages, including but not limited to procurement of substitute
79
goods or services; loss of use, data, or profits, or business interruption,
80
however caused and on any theory of liability, whether in contract, strict
81
liability, or tort (including negligence), arising in any way out of the use of
82
this software, even if advised of the possibility of such damage.
83
84
5.    CONTACT INFORMATION
85
86
Fraunhofer Institute for Integrated Circuits IIS
87
Attention: Audio and Multimedia Departments - FDK AAC LL
88
Am Wolfsmantel 33
89
91058 Erlangen, Germany
90
91
www.iis.fraunhofer.de/amm
92
amm-info@iis.fraunhofer.de
93
----------------------------------------------------------------------------- */
94
95
/**************************** SBR decoder library ******************************
96
97
   Author(s):   Christian Griebel
98
99
   Description: Dynamic range control (DRC) decoder tool for SBR
100
101
*******************************************************************************/
102
103
#include "sbrdec_drc.h"
104
105
/* DRC - Offset table for QMF interpolation. Shifted by one index position.
106
   The table defines the (short) window borders rounded to the nearest QMF
107
   timeslot. It has the size 16 because it is accessed with the
108
   drcInterpolationScheme that is read from the bitstream with 4 bit. */
109
static const UCHAR winBorderToColMappingTab[2][16] = {
110
    /*-1, 0, 1, 2,  3,  4,  5,  6,  7,  8 */
111
    {0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 32, 32, 32, 32, 32,
112
     32}, /* 1024 framing */
113
    {0, 0, 4, 8, 11, 15, 19, 23, 26, 30, 30, 30, 30, 30, 30,
114
     30} /*  960 framing */
115
};
116
117
/*!
118
  \brief Initialize DRC QMF factors
119
120
  \hDrcData Handle to DRC channel data.
121
122
  \return none
123
*/
124
0
void sbrDecoder_drcInitChannel(HANDLE_SBR_DRC_CHANNEL hDrcData) {
125
0
  int band;
126
127
0
  if (hDrcData == NULL) {
128
0
    return;
129
0
  }
130
131
0
  for (band = 0; band < (64); band++) {
132
0
    hDrcData->prevFact_mag[band] = FL2FXCONST_DBL(0.5f);
133
0
  }
134
135
0
  for (band = 0; band < SBRDEC_MAX_DRC_BANDS; band++) {
136
0
    hDrcData->currFact_mag[band] = FL2FXCONST_DBL(0.5f);
137
0
    hDrcData->nextFact_mag[band] = FL2FXCONST_DBL(0.5f);
138
0
  }
139
140
0
  hDrcData->prevFact_exp = 1;
141
0
  hDrcData->currFact_exp = 1;
142
0
  hDrcData->nextFact_exp = 1;
143
144
0
  hDrcData->numBandsCurr = 1;
145
0
  hDrcData->numBandsNext = 1;
146
147
0
  hDrcData->winSequenceCurr = 0;
148
0
  hDrcData->winSequenceNext = 0;
149
150
0
  hDrcData->drcInterpolationSchemeCurr = 0;
151
0
  hDrcData->drcInterpolationSchemeNext = 0;
152
153
0
  hDrcData->enable = 0;
154
0
}
155
156
/*!
157
  \brief Swap DRC QMF scaling factors after they have been applied.
158
159
  \hDrcData Handle to DRC channel data.
160
161
  \return none
162
*/
163
0
void sbrDecoder_drcUpdateChannel(HANDLE_SBR_DRC_CHANNEL hDrcData) {
164
0
  if (hDrcData == NULL) {
165
0
    return;
166
0
  }
167
0
  if (hDrcData->enable != 1) {
168
0
    return;
169
0
  }
170
171
  /* swap previous data */
172
0
  FDKmemcpy(hDrcData->currFact_mag, hDrcData->nextFact_mag,
173
0
            SBRDEC_MAX_DRC_BANDS * sizeof(FIXP_DBL));
174
175
0
  hDrcData->currFact_exp = hDrcData->nextFact_exp;
176
177
0
  hDrcData->numBandsCurr = hDrcData->numBandsNext;
178
179
0
  FDKmemcpy(hDrcData->bandTopCurr, hDrcData->bandTopNext,
180
0
            SBRDEC_MAX_DRC_BANDS * sizeof(USHORT));
181
182
0
  hDrcData->drcInterpolationSchemeCurr = hDrcData->drcInterpolationSchemeNext;
183
184
0
  hDrcData->winSequenceCurr = hDrcData->winSequenceNext;
185
0
}
186
187
/*!
188
  \brief Apply DRC factors slot based.
189
190
  \hDrcData Handle to DRC channel data.
191
  \qmfRealSlot Pointer to real valued QMF data of one time slot.
192
  \qmfImagSlot Pointer to the imaginary QMF data of one time slot.
193
  \col Number of the time slot.
194
  \numQmfSubSamples Total number of time slots for one frame.
195
  \scaleFactor Pointer to the out scale factor of the time slot.
196
197
  \return None.
198
*/
199
void sbrDecoder_drcApplySlot(HANDLE_SBR_DRC_CHANNEL hDrcData,
200
                             FIXP_DBL *qmfRealSlot, FIXP_DBL *qmfImagSlot,
201
0
                             int col, int numQmfSubSamples, int maxShift) {
202
0
  const UCHAR *winBorderToColMap;
203
204
0
  int band, bottomMdct, topMdct, bin, useLP;
205
0
  int indx = numQmfSubSamples - (numQmfSubSamples >> 1) - 10; /* l_border */
206
0
  int frameLenFlag = (numQmfSubSamples == 30) ? 1 : 0;
207
0
  int frameSize = (frameLenFlag == 1) ? 960 : 1024;
208
209
0
  const FIXP_DBL *fact_mag = NULL;
210
0
  INT fact_exp = 0;
211
0
  UINT numBands = 0;
212
0
  USHORT *bandTop = NULL;
213
0
  int shortDrc = 0;
214
215
0
  FIXP_DBL alphaValue = FL2FXCONST_DBL(0.0f);
216
217
0
  if (hDrcData == NULL) {
218
0
    return;
219
0
  }
220
0
  if (hDrcData->enable != 1) {
221
0
    return;
222
0
  }
223
224
0
  winBorderToColMap = winBorderToColMappingTab[frameLenFlag];
225
226
0
  useLP = (qmfImagSlot == NULL) ? 1 : 0;
227
228
0
  col += indx;
229
0
  bottomMdct = 0;
230
231
  /* get respective data and calc interpolation factor */
232
0
  if (col < (numQmfSubSamples >> 1)) {    /* first half of current frame */
233
0
    if (hDrcData->winSequenceCurr != 2) { /* long window */
234
0
      int j = col + (numQmfSubSamples >> 1);
235
236
0
      if (j < winBorderToColMap[15]) {
237
0
        if (hDrcData->drcInterpolationSchemeCurr == 0) {
238
0
          INT k = (frameLenFlag) ? 0x4444445 : 0x4000000;
239
240
0
          alphaValue = (FIXP_DBL)(j * k);
241
0
        } else {
242
0
          if (j >=
243
0
              (int)winBorderToColMap[hDrcData->drcInterpolationSchemeCurr]) {
244
0
            alphaValue = (FIXP_DBL)MAXVAL_DBL;
245
0
          }
246
0
        }
247
0
      } else {
248
0
        alphaValue = (FIXP_DBL)MAXVAL_DBL;
249
0
      }
250
0
    } else { /* short windows */
251
0
      shortDrc = 1;
252
0
    }
253
254
0
    fact_mag = hDrcData->currFact_mag;
255
0
    fact_exp = hDrcData->currFact_exp;
256
0
    numBands = hDrcData->numBandsCurr;
257
0
    bandTop = hDrcData->bandTopCurr;
258
0
  } else if (col < numQmfSubSamples) {    /* second half of current frame */
259
0
    if (hDrcData->winSequenceNext != 2) { /* next: long window */
260
0
      int j = col - (numQmfSubSamples >> 1);
261
262
0
      if (j < winBorderToColMap[15]) {
263
0
        if (hDrcData->drcInterpolationSchemeNext == 0) {
264
0
          INT k = (frameLenFlag) ? 0x4444445 : 0x4000000;
265
266
0
          alphaValue = (FIXP_DBL)(j * k);
267
0
        } else {
268
0
          if (j >=
269
0
              (int)winBorderToColMap[hDrcData->drcInterpolationSchemeNext]) {
270
0
            alphaValue = (FIXP_DBL)MAXVAL_DBL;
271
0
          }
272
0
        }
273
0
      } else {
274
0
        alphaValue = (FIXP_DBL)MAXVAL_DBL;
275
0
      }
276
277
0
      fact_mag = hDrcData->nextFact_mag;
278
0
      fact_exp = hDrcData->nextFact_exp;
279
0
      numBands = hDrcData->numBandsNext;
280
0
      bandTop = hDrcData->bandTopNext;
281
0
    } else {                                /* next: short windows */
282
0
      if (hDrcData->winSequenceCurr != 2) { /* current: long window */
283
0
        alphaValue = (FIXP_DBL)0;
284
285
0
        fact_mag = hDrcData->nextFact_mag;
286
0
        fact_exp = hDrcData->nextFact_exp;
287
0
        numBands = hDrcData->numBandsNext;
288
0
        bandTop = hDrcData->bandTopNext;
289
0
      } else { /* current: short windows */
290
0
        shortDrc = 1;
291
292
0
        fact_mag = hDrcData->currFact_mag;
293
0
        fact_exp = hDrcData->currFact_exp;
294
0
        numBands = hDrcData->numBandsCurr;
295
0
        bandTop = hDrcData->bandTopCurr;
296
0
      }
297
0
    }
298
0
  } else {                                /* first half of next frame */
299
0
    if (hDrcData->winSequenceNext != 2) { /* long window */
300
0
      int j = col - (numQmfSubSamples >> 1);
301
302
0
      if (j < winBorderToColMap[15]) {
303
0
        if (hDrcData->drcInterpolationSchemeNext == 0) {
304
0
          INT k = (frameLenFlag) ? 0x4444445 : 0x4000000;
305
306
0
          alphaValue = (FIXP_DBL)(j * k);
307
0
        } else {
308
0
          if (j >=
309
0
              (int)winBorderToColMap[hDrcData->drcInterpolationSchemeNext]) {
310
0
            alphaValue = (FIXP_DBL)MAXVAL_DBL;
311
0
          }
312
0
        }
313
0
      } else {
314
0
        alphaValue = (FIXP_DBL)MAXVAL_DBL;
315
0
      }
316
0
    } else { /* short windows */
317
0
      shortDrc = 1;
318
0
    }
319
320
0
    fact_mag = hDrcData->nextFact_mag;
321
0
    fact_exp = hDrcData->nextFact_exp;
322
0
    numBands = hDrcData->numBandsNext;
323
0
    bandTop = hDrcData->bandTopNext;
324
325
0
    col -= numQmfSubSamples;
326
0
  }
327
328
  /* process bands */
329
0
  for (band = 0; band < (int)numBands; band++) {
330
0
    int bottomQmf, topQmf;
331
332
0
    FIXP_DBL drcFact_mag = (FIXP_DBL)MAXVAL_DBL;
333
334
0
    topMdct = (bandTop[band] + 1) << 2;
335
336
0
    if (!shortDrc) { /* long window */
337
0
      if (frameLenFlag) {
338
        /* 960 framing */
339
0
        bottomQmf = fMultIfloor((FIXP_DBL)0x4444445, bottomMdct);
340
0
        topQmf = fMultIfloor((FIXP_DBL)0x4444445, topMdct);
341
342
0
        topMdct = 30 * topQmf;
343
0
      } else {
344
        /* 1024 framing */
345
0
        topMdct &= ~0x1f;
346
347
0
        bottomQmf = bottomMdct >> 5;
348
0
        topQmf = topMdct >> 5;
349
0
      }
350
351
0
      if (band == ((int)numBands - 1)) {
352
0
        topQmf = (64);
353
0
      }
354
355
0
      for (bin = bottomQmf; bin < topQmf; bin++) {
356
0
        FIXP_DBL drcFact1_mag = hDrcData->prevFact_mag[bin];
357
0
        FIXP_DBL drcFact2_mag = fact_mag[band];
358
359
        /* normalize scale factors */
360
0
        if (hDrcData->prevFact_exp < maxShift) {
361
0
          drcFact1_mag >>= maxShift - hDrcData->prevFact_exp;
362
0
        }
363
0
        if (fact_exp < maxShift) {
364
0
          drcFact2_mag >>= maxShift - fact_exp;
365
0
        }
366
367
        /* interpolate */
368
0
        if (alphaValue == (FIXP_DBL)0) {
369
0
          drcFact_mag = drcFact1_mag;
370
0
        } else if (alphaValue == (FIXP_DBL)MAXVAL_DBL) {
371
0
          drcFact_mag = drcFact2_mag;
372
0
        } else {
373
0
          drcFact_mag =
374
0
              fMult(alphaValue, drcFact2_mag) +
375
0
              fMult(((FIXP_DBL)MAXVAL_DBL - alphaValue), drcFact1_mag);
376
0
        }
377
378
        /* apply scaling */
379
0
        qmfRealSlot[bin] = fMult(qmfRealSlot[bin], drcFact_mag);
380
0
        if (!useLP) {
381
0
          qmfImagSlot[bin] = fMult(qmfImagSlot[bin], drcFact_mag);
382
0
        }
383
384
        /* save previous factors */
385
0
        if (col == (numQmfSubSamples >> 1) - 1) {
386
0
          hDrcData->prevFact_mag[bin] = fact_mag[band];
387
0
        }
388
0
      }
389
0
    } else { /* short windows */
390
0
      unsigned startWinIdx, stopWinIdx;
391
0
      int startCol, stopCol;
392
0
      FIXP_DBL invFrameSizeDiv8 =
393
0
          (frameLenFlag) ? (FIXP_DBL)0x1111112 : (FIXP_DBL)0x1000000;
394
395
      /* limit top at the frame borders */
396
0
      if (topMdct < 0) {
397
0
        topMdct = 0;
398
0
      }
399
0
      if (topMdct >= frameSize) {
400
0
        topMdct = frameSize - 1;
401
0
      }
402
403
0
      if (frameLenFlag) {
404
        /*  960 framing */
405
0
        topMdct = fMultIfloor((FIXP_DBL)0x78000000,
406
0
                              fMultIfloor((FIXP_DBL)0x22222223, topMdct) << 2);
407
408
0
        startWinIdx = fMultIfloor(invFrameSizeDiv8, bottomMdct) +
409
0
                      1; /* winBorderToColMap table has offset of 1 */
410
0
        stopWinIdx = fMultIceil(invFrameSizeDiv8 - (FIXP_DBL)1, topMdct) + 1;
411
0
      } else {
412
        /* 1024 framing */
413
0
        topMdct &= ~0x03;
414
415
0
        startWinIdx = fMultIfloor(invFrameSizeDiv8, bottomMdct) + 1;
416
0
        stopWinIdx = fMultIceil(invFrameSizeDiv8, topMdct) + 1;
417
0
      }
418
419
      /* startCol is truncated to the nearest corresponding start subsample in
420
         the QMF of the short window bottom is present in:*/
421
0
      startCol = (int)winBorderToColMap[startWinIdx];
422
423
      /* stopCol is rounded upwards to the nearest corresponding stop subsample
424
         in the QMF of the short window top is present in. */
425
0
      stopCol = (int)winBorderToColMap[stopWinIdx];
426
427
0
      bottomQmf = fMultIfloor(invFrameSizeDiv8,
428
0
                              ((bottomMdct % (numQmfSubSamples << 2)) << 5));
429
0
      topQmf = fMultIfloor(invFrameSizeDiv8,
430
0
                           ((topMdct % (numQmfSubSamples << 2)) << 5));
431
432
      /* extend last band */
433
0
      if (band == ((int)numBands - 1)) {
434
0
        topQmf = (64);
435
0
        stopCol = numQmfSubSamples;
436
0
        stopWinIdx = 10;
437
0
      }
438
439
0
      if (topQmf == 0) {
440
0
        if (frameLenFlag) {
441
0
          FIXP_DBL rem = fMult(invFrameSizeDiv8,
442
0
                               (FIXP_DBL)(topMdct << (DFRACT_BITS - 12)));
443
0
          if ((LONG)rem & (LONG)0x1F) {
444
0
            stopWinIdx -= 1;
445
0
            stopCol = (int)winBorderToColMap[stopWinIdx];
446
0
          }
447
0
        }
448
0
        topQmf = (64);
449
0
      }
450
451
      /* save previous factors */
452
0
      if (stopCol == numQmfSubSamples) {
453
0
        int tmpBottom = bottomQmf;
454
455
0
        if ((int)winBorderToColMap[8] > startCol) {
456
0
          tmpBottom = 0; /* band starts in previous short window */
457
0
        }
458
459
0
        for (bin = tmpBottom; bin < topQmf; bin++) {
460
0
          hDrcData->prevFact_mag[bin] = fact_mag[band];
461
0
        }
462
0
      }
463
464
      /* apply */
465
0
      if ((col >= startCol) && (col < stopCol)) {
466
0
        if (col >= (int)winBorderToColMap[startWinIdx + 1]) {
467
0
          bottomQmf = 0; /* band starts in previous short window */
468
0
        }
469
0
        if (col < (int)winBorderToColMap[stopWinIdx - 1]) {
470
0
          topQmf = (64); /* band ends in next short window */
471
0
        }
472
473
0
        drcFact_mag = fact_mag[band];
474
475
        /* normalize scale factor */
476
0
        if (fact_exp < maxShift) {
477
0
          drcFact_mag >>= maxShift - fact_exp;
478
0
        }
479
480
        /* apply scaling */
481
0
        for (bin = bottomQmf; bin < topQmf; bin++) {
482
0
          qmfRealSlot[bin] = fMult(qmfRealSlot[bin], drcFact_mag);
483
0
          if (!useLP) {
484
0
            qmfImagSlot[bin] = fMult(qmfImagSlot[bin], drcFact_mag);
485
0
          }
486
0
        }
487
0
      }
488
0
    }
489
490
0
    bottomMdct = topMdct;
491
0
  } /* end of bands loop */
492
493
0
  if (col == (numQmfSubSamples >> 1) - 1) {
494
0
    hDrcData->prevFact_exp = fact_exp;
495
0
  }
496
0
}
497
498
/*!
499
  \brief Apply DRC factors frame based.
500
501
  \hDrcData Handle to DRC channel data.
502
  \qmfRealSlot Pointer to real valued QMF data of the whole frame.
503
  \qmfImagSlot Pointer to the imaginary QMF data of the whole frame.
504
  \numQmfSubSamples Total number of time slots for one frame.
505
  \scaleFactor Pointer to the out scale factor of the frame.
506
507
  \return None.
508
*/
509
void sbrDecoder_drcApply(HANDLE_SBR_DRC_CHANNEL hDrcData,
510
                         FIXP_DBL **QmfBufferReal, FIXP_DBL **QmfBufferImag,
511
0
                         int numQmfSubSamples, int *scaleFactor) {
512
0
  int col;
513
0
  int maxShift = 0;
514
515
0
  if (hDrcData == NULL) {
516
0
    return;
517
0
  }
518
0
  if (hDrcData->enable == 0) {
519
0
    return; /* Avoid changing the scaleFactor even though the processing is
520
               disabled. */
521
0
  }
522
523
  /* get max scale factor */
524
0
  if (hDrcData->prevFact_exp > maxShift) {
525
0
    maxShift = hDrcData->prevFact_exp;
526
0
  }
527
0
  if (hDrcData->currFact_exp > maxShift) {
528
0
    maxShift = hDrcData->currFact_exp;
529
0
  }
530
0
  if (hDrcData->nextFact_exp > maxShift) {
531
0
    maxShift = hDrcData->nextFact_exp;
532
0
  }
533
534
0
  for (col = 0; col < numQmfSubSamples; col++) {
535
0
    FIXP_DBL *qmfSlotReal = QmfBufferReal[col];
536
0
    FIXP_DBL *qmfSlotImag = (QmfBufferImag == NULL) ? NULL : QmfBufferImag[col];
537
538
0
    sbrDecoder_drcApplySlot(hDrcData, qmfSlotReal, qmfSlotImag, col,
539
0
                            numQmfSubSamples, maxShift);
540
0
  }
541
542
0
  *scaleFactor += maxShift;
543
0
}