Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fdk-aac/libSACdec/src/sac_tsd.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2019 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
/*********************** MPEG surround decoder library *************************
96
97
   Author(s):   Matthias Hildenbrand
98
99
   Description: USAC MPS212 Transient Steering Decorrelator (TSD)
100
101
*******************************************************************************/
102
103
#include "sac_tsd.h"
104
105
20.1k
#define TSD_START_BAND (7)
106
1.12k
#define SIZE_S (4)
107
#define SIZE_C (5)
108
109
/*** Tables ***/
110
RAM_ALIGN
111
LNK_SECTION_CONSTDATA
112
static const UCHAR nBitsTsdCW_32slots[32] = {
113
    5,  9,  13, 16, 18, 20, 22, 24, 25, 26, 27, 28, 29, 29, 30, 30,
114
    30, 29, 29, 28, 27, 26, 25, 24, 22, 20, 18, 16, 13, 9,  5,  0};
115
116
RAM_ALIGN
117
LNK_SECTION_CONSTDATA
118
static const UCHAR nBitsTsdCW_64slots[64] = {
119
    6,  11, 16, 20, 23, 27, 30, 33, 35, 38, 40, 42, 44, 46, 48, 49,
120
    51, 52, 53, 55, 56, 57, 58, 58, 59, 60, 60, 60, 61, 61, 61, 61,
121
    61, 61, 61, 60, 60, 60, 59, 58, 58, 57, 56, 55, 53, 52, 51, 49,
122
    48, 46, 44, 42, 40, 38, 35, 33, 30, 27, 23, 20, 16, 11, 6,  0};
123
124
RAM_ALIGN
125
LNK_SECTION_CONSTDATA
126
static const FIXP_DPK phiTsd[8] = {
127
    {{(FIXP_DBL)0x7fffffff, (FIXP_DBL)0x00000000}},
128
    {{(FIXP_DBL)0x5a82799a, (FIXP_DBL)0x5a82799a}},
129
    {{(FIXP_DBL)0x00000000, (FIXP_DBL)0x7fffffff}},
130
    {{(FIXP_DBL)0xa57d8666, (FIXP_DBL)0x5a82799a}},
131
    {{(FIXP_DBL)0x80000000, (FIXP_DBL)0x00000000}},
132
    {{(FIXP_DBL)0xa57d8666, (FIXP_DBL)0xa57d8666}},
133
    {{(FIXP_DBL)0x00000000, (FIXP_DBL)0x80000000}},
134
    {{(FIXP_DBL)0x5a82799a, (FIXP_DBL)0xa57d8666}}};
135
136
/*** Static Functions ***/
137
34.6k
static void longmult1(USHORT a[], USHORT b, USHORT d[], int len) {
138
34.6k
  int k;
139
34.6k
  ULONG tmp;
140
34.6k
  ULONG b0 = (ULONG)b;
141
142
34.6k
  tmp = ((ULONG)a[0]) * b0;
143
34.6k
  d[0] = (USHORT)tmp;
144
145
173k
  for (k = 1; k < len; k++) {
146
138k
    tmp = (tmp >> 16) + ((ULONG)a[k]) * b0;
147
138k
    d[k] = (USHORT)tmp;
148
138k
  }
149
34.6k
}
150
151
34.6k
static void longdiv(USHORT b[], USHORT a, USHORT d[], USHORT *pr, int len) {
152
34.6k
  ULONG r;
153
34.6k
  ULONG tmp;
154
34.6k
  int k;
155
156
34.6k
  FDK_ASSERT(a != 0);
157
158
34.6k
  r = 0;
159
160
208k
  for (k = len - 1; k >= 0; k--) {
161
173k
    tmp = ((ULONG)b[k]) + (r << 16);
162
163
173k
    if (tmp) {
164
46.8k
      d[k] = (USHORT)(tmp / a);
165
46.8k
      r = tmp - d[k] * a;
166
126k
    } else {
167
126k
      d[k] = 0;
168
126k
    }
169
173k
  }
170
34.6k
  *pr = (USHORT)r;
171
34.6k
}
172
173
6.63k
static void longsub(USHORT a[], USHORT b[], int lena, int lenb) {
174
6.63k
  int h;
175
6.63k
  LONG carry = 0;
176
177
6.63k
  FDK_ASSERT(lena >= lenb);
178
33.1k
  for (h = 0; h < lenb; h++) {
179
26.5k
    carry += ((LONG)a[h]) - ((LONG)b[h]);
180
26.5k
    a[h] = (USHORT)carry;
181
26.5k
    carry = carry >> 16;
182
26.5k
  }
183
184
6.63k
  for (; h < lena; h++) {
185
0
    carry = ((LONG)a[h]) + carry;
186
0
    a[h] = (USHORT)carry;
187
0
    carry = carry >> 16;
188
0
  }
189
190
6.63k
  FDK_ASSERT(carry ==
191
6.63k
             0); /* carry != 0 indicates subtraction underflow, e.g. b > a */
192
6.63k
  return;
193
6.63k
}
194
195
28.8k
static int longcompare(USHORT a[], USHORT b[], int len) {
196
28.8k
  int i;
197
198
108k
  for (i = len - 1; i > 0; i--) {
199
85.5k
    if (a[i] != b[i]) break;
200
85.5k
  }
201
28.8k
  return (a[i] >= b[i]) ? 1 : 0;
202
28.8k
}
203
204
46.3k
FDK_INLINE int isTrSlot(const TSD_DATA *pTsdData, const int ts) {
205
46.3k
  return (pTsdData->bsTsdTrPhaseData[ts] >= 0);
206
46.3k
}
207
208
/*** Public Functions ***/
209
1.57k
int TsdRead(HANDLE_FDK_BITSTREAM hBs, const int numSlots, TSD_DATA *pTsdData) {
210
1.57k
  int nBitsTrSlots = 0;
211
1.57k
  int bsTsdNumTrSlots;
212
1.57k
  const UCHAR *nBitsTsdCW_tab = NULL;
213
214
1.57k
  switch (numSlots) {
215
1.54k
    case 32:
216
1.54k
      nBitsTrSlots = 4;
217
1.54k
      nBitsTsdCW_tab = nBitsTsdCW_32slots;
218
1.54k
      break;
219
30
    case 64:
220
30
      nBitsTrSlots = 5;
221
30
      nBitsTsdCW_tab = nBitsTsdCW_64slots;
222
30
      break;
223
0
    default:
224
0
      return 1;
225
1.57k
  }
226
227
  /*** Read TempShapeData for bsTempShapeConfig == 3 ***/
228
1.57k
  pTsdData->bsTsdEnable = FDKreadBit(hBs);
229
1.57k
  if (!pTsdData->bsTsdEnable) {
230
449
    return 0;
231
449
  }
232
233
  /*** Parse/Decode TsdData() ***/
234
1.12k
  pTsdData->numSlots = numSlots;
235
236
1.12k
  bsTsdNumTrSlots = FDKreadBits(hBs, nBitsTrSlots);
237
238
  /* Decode transient slot positions */
239
1.12k
  {
240
1.12k
    int nBitsTsdCW = (int)nBitsTsdCW_tab[bsTsdNumTrSlots];
241
1.12k
    SCHAR *phaseData = pTsdData->bsTsdTrPhaseData;
242
1.12k
    int p = bsTsdNumTrSlots + 1;
243
1.12k
    int k, h;
244
1.12k
    USHORT s[SIZE_S] = {0};
245
1.12k
    USHORT c[SIZE_C] = {0};
246
1.12k
    USHORT r[1];
247
248
    /* Init with TsdSepData[k] = 0 */
249
37.8k
    for (k = 0; k < numSlots; k++) {
250
36.7k
      phaseData[k] = -1; /* means TsdSepData[] = 0 */
251
36.7k
    }
252
253
5.62k
    for (h = (SIZE_S - 1); h >= 0; h--) {
254
4.49k
      if (nBitsTsdCW > h * 16) {
255
1.66k
        s[h] = (USHORT)FDKreadBits(hBs, nBitsTsdCW - h * 16);
256
1.66k
        nBitsTsdCW = h * 16;
257
1.66k
      }
258
4.49k
    }
259
260
    /* c = prod_{h=1}^{p} (k-p+h)/h */
261
1.12k
    k = numSlots - 1;
262
1.12k
    c[0] = k - p + 1;
263
7.42k
    for (h = 2; h <= p; h++) {
264
6.30k
      longmult1(c, (k - p + h), c, 5); /* c *= k - p + h; */
265
6.30k
      longdiv(c, h, c, r, 5);          /* c /= h; */
266
6.30k
      FDK_ASSERT(*r == 0);
267
6.30k
    }
268
269
    /* go through all slots */
270
29.5k
    for (; k >= 0; k--) {
271
29.5k
      if (p > k) {
272
1.48k
        for (; k >= 0; k--) {
273
793
          phaseData[k] = 1; /* means TsdSepData[] = 1 */
274
793
        }
275
696
        break;
276
696
      }
277
28.8k
      if (longcompare(s, c, 4)) { /* (s >= c) */
278
6.63k
        longsub(s, c, 4, 4);      /* s -= c; */
279
6.63k
        phaseData[k] = 1;         /* means TsdSepData[] = 1 */
280
6.63k
        if (p == 1) {
281
428
          break;
282
428
        }
283
        /* Update c for next iteration: c_new = c_old * p / k */
284
6.20k
        longmult1(c, p, c, 5);
285
6.20k
        p--;
286
22.1k
      } else {
287
        /* Update c for next iteration: c_new = c_old * (k-p) / k */
288
22.1k
        longmult1(c, (k - p), c, 5);
289
22.1k
      }
290
28.3k
      longdiv(c, k, c, r, 5);
291
28.3k
      FDK_ASSERT(*r == 0);
292
28.3k
    }
293
294
    /* Read phase data */
295
37.8k
    for (k = 0; k < numSlots; k++) {
296
36.7k
      if (phaseData[k] == 1) {
297
7.42k
        phaseData[k] = FDKreadBits(hBs, 3);
298
7.42k
      }
299
36.7k
    }
300
1.12k
  }
301
302
0
  return 0;
303
1.12k
}
304
305
void TsdGenerateNonTr(const int numHybridBands, const TSD_DATA *pTsdData,
306
                      const int ts, FIXP_DBL *pVdirectReal,
307
                      FIXP_DBL *pVdirectImag, FIXP_DBL *pVnonTrReal,
308
                      FIXP_DBL *pVnonTrImag, FIXP_DBL **ppDecorrInReal,
309
23.1k
                      FIXP_DBL **ppDecorrInImag) {
310
23.1k
  int k = 0;
311
312
23.1k
  if (!isTrSlot(pTsdData, ts)) {
313
    /* Let allpass based decorrelator read from direct input. */
314
20.9k
    *ppDecorrInReal = pVdirectReal;
315
20.9k
    *ppDecorrInImag = pVdirectImag;
316
20.9k
    return;
317
20.9k
  }
318
319
  /* Generate nonTr input signal for allpass based decorrelator */
320
17.8k
  for (; k < TSD_START_BAND; k++) {
321
15.6k
    pVnonTrReal[k] = pVdirectReal[k];
322
15.6k
    pVnonTrImag[k] = pVdirectImag[k];
323
15.6k
  }
324
131k
  for (; k < numHybridBands; k++) {
325
128k
    pVnonTrReal[k] = (FIXP_DBL)0;
326
128k
    pVnonTrImag[k] = (FIXP_DBL)0;
327
128k
  }
328
2.23k
  *ppDecorrInReal = pVnonTrReal;
329
2.23k
  *ppDecorrInImag = pVnonTrImag;
330
2.23k
}
331
332
void TsdApply(const int numHybridBands, const TSD_DATA *pTsdData, int *pTsdTs,
333
              const FIXP_DBL *pVdirectReal, const FIXP_DBL *pVdirectImag,
334
23.1k
              FIXP_DBL *pDnonTrReal, FIXP_DBL *pDnonTrImag) {
335
23.1k
  const int ts = *pTsdTs;
336
337
23.1k
  if (isTrSlot(pTsdData, ts)) {
338
2.23k
    int k;
339
2.23k
    const FIXP_DPK *phi = &phiTsd[pTsdData->bsTsdTrPhaseData[ts]];
340
2.23k
    FDK_ASSERT((pTsdData->bsTsdTrPhaseData[ts] >= 0) &&
341
2.23k
               (pTsdData->bsTsdTrPhaseData[ts] < 8));
342
343
    /* d = d_nonTr + v_direct * exp(j * bsTsdTrPhaseData[ts]/4 * pi ) */
344
131k
    for (k = TSD_START_BAND; k < numHybridBands; k++) {
345
128k
      FIXP_DBL tempReal, tempImag;
346
128k
      cplxMultDiv2(&tempReal, &tempImag, pVdirectReal[k], pVdirectImag[k],
347
128k
                   *phi);
348
128k
      pDnonTrReal[k] = SATURATE_LEFT_SHIFT(
349
128k
          (pDnonTrReal[k] >> 2) + (tempReal >> 1), 2, DFRACT_BITS);
350
128k
      pDnonTrImag[k] = SATURATE_LEFT_SHIFT(
351
128k
          (pDnonTrImag[k] >> 2) + (tempImag >> 1), 2, DFRACT_BITS);
352
128k
    }
353
2.23k
  }
354
355
  /* The modulo MAX_TSD_TIME_SLOTS operation is to avoid illegal memory accesses
356
   * in case of errors. */
357
23.1k
  *pTsdTs = (ts + 1) & (MAX_TSD_TIME_SLOTS - 1);
358
23.1k
  return;
359
23.1k
}