Coverage Report

Created: 2025-07-11 06:54

/src/aac/libDRCdec/src/drcDec_tools.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 - 2018 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-D DRC decoder library **************************
96
97
   Author(s):
98
99
   Description:
100
101
*******************************************************************************/
102
103
#include "drcDec_types.h"
104
#include "drcDec_tools.h"
105
#include "fixpoint_math.h"
106
#include "drcDecoder.h"
107
108
233k
int getDeltaTmin(const int sampleRate) {
109
  /* half_ms = round (0.0005 * sampleRate); */
110
233k
  int half_ms = (sampleRate + 1000) / 2000;
111
233k
  int deltaTmin = 1;
112
233k
  if (sampleRate < 1000) {
113
443
    return DE_NOT_OK;
114
443
  }
115
1.49M
  while (deltaTmin <= half_ms) {
116
1.25M
    deltaTmin = deltaTmin << 1;
117
1.25M
  }
118
233k
  return deltaTmin;
119
233k
}
120
121
DRC_COEFFICIENTS_UNI_DRC* selectDrcCoefficients(
122
2.36M
    HANDLE_UNI_DRC_CONFIG hUniDrcConfig, const int location) {
123
2.36M
  int n;
124
2.36M
  int c = -1;
125
4.09M
  for (n = 0; n < hUniDrcConfig->drcCoefficientsUniDrcCount; n++) {
126
1.73M
    if (hUniDrcConfig->drcCoefficientsUniDrc[n].drcLocation == location) {
127
1.43M
      c = n;
128
1.43M
    }
129
1.73M
  }
130
2.36M
  if (c >= 0) {
131
854k
    return &(hUniDrcConfig->drcCoefficientsUniDrc[c]);
132
854k
  }
133
1.51M
  return NULL; /* possible during bitstream parsing */
134
2.36M
}
135
136
DRC_INSTRUCTIONS_UNI_DRC* selectDrcInstructions(
137
1.39M
    HANDLE_UNI_DRC_CONFIG hUniDrcConfig, const int drcSetId) {
138
1.39M
  int i;
139
2.51M
  for (i = 0; i < hUniDrcConfig->drcInstructionsCountInclVirtual; i++) {
140
2.51M
    if (hUniDrcConfig->drcInstructionsUniDrc[i].drcSetId == drcSetId) {
141
1.39M
      return &(hUniDrcConfig->drcInstructionsUniDrc[i]);
142
1.39M
    }
143
2.51M
  }
144
0
  return NULL;
145
1.39M
}
146
147
DOWNMIX_INSTRUCTIONS* selectDownmixInstructions(
148
9.02k
    HANDLE_UNI_DRC_CONFIG hUniDrcConfig, const int downmixId) {
149
9.02k
  int i;
150
33.1k
  for (i = 0; i < hUniDrcConfig->downmixInstructionsCount; i++) {
151
25.6k
    if (hUniDrcConfig->downmixInstructions[i].downmixId == downmixId) {
152
1.52k
      return &(hUniDrcConfig->downmixInstructions[i]);
153
1.52k
    }
154
25.6k
  }
155
7.49k
  return NULL;
156
9.02k
}
157
158
DRC_ERROR
159
deriveDrcChannelGroups(
160
    const int drcSetEffect,                                    /* in */
161
    const int channelCount,                                    /* in */
162
    const SCHAR* gainSetIndex,                                 /* in */
163
    const DUCKING_MODIFICATION* duckingModificationForChannel, /* in */
164
    UCHAR* nDrcChannelGroups,                                  /* out */
165
    SCHAR* uniqueIndex,     /* out (gainSetIndexForChannelGroup) */
166
    SCHAR* groupForChannel, /* out */
167
    DUCKING_MODIFICATION* duckingModificationForChannelGroup) /* out */
168
1.09M
{
169
1.09M
  int duckingSequence = -1;
170
1.09M
  int c, n, g, match, idx;
171
1.09M
  FIXP_SGL factor;
172
1.09M
  FIXP_SGL uniqueScaling[8];
173
174
9.88M
  for (g = 0; g < 8; g++) {
175
8.78M
    uniqueIndex[g] = -10;
176
8.78M
    uniqueScaling[g] = FIXP_SGL(-1.0f);
177
8.78M
  }
178
179
1.09M
  g = 0;
180
181
1.09M
  if (drcSetEffect & EB_DUCK_OTHER) {
182
47.4k
    for (c = 0; c < channelCount; c++) {
183
21.8k
      match = 0;
184
21.8k
      if (c >= 8) return DE_MEMORY_ERROR;
185
21.8k
      idx = gainSetIndex[c];
186
21.8k
      factor = duckingModificationForChannel[c].duckingScaling;
187
21.8k
      if (idx < 0) {
188
16.0k
        for (n = 0; n < g; n++) {
189
11.5k
          if (uniqueScaling[n] == factor) {
190
6.18k
            match = 1;
191
6.18k
            groupForChannel[c] = n;
192
6.18k
            break;
193
6.18k
          }
194
11.5k
        }
195
10.6k
        if (match == 0) {
196
4.47k
          if (g >= 8) return DE_MEMORY_ERROR;
197
4.47k
          uniqueIndex[g] = idx;
198
4.47k
          uniqueScaling[g] = factor;
199
4.47k
          groupForChannel[c] = g;
200
4.47k
          g++;
201
4.47k
        }
202
11.1k
      } else {
203
11.1k
        if ((duckingSequence > 0) && (duckingSequence != idx)) {
204
809
          return DE_NOT_OK;
205
809
        }
206
10.3k
        duckingSequence = idx;
207
10.3k
        groupForChannel[c] = -1;
208
10.3k
      }
209
21.8k
    }
210
25.5k
    if (duckingSequence == -1) {
211
19.8k
      return DE_NOT_OK;
212
19.8k
    }
213
1.07M
  } else if (drcSetEffect & EB_DUCK_SELF) {
214
74.1k
    for (c = 0; c < channelCount; c++) {
215
54.1k
      match = 0;
216
54.1k
      if (c >= 8) return DE_MEMORY_ERROR;
217
54.1k
      idx = gainSetIndex[c];
218
54.1k
      factor = duckingModificationForChannel[c].duckingScaling;
219
54.1k
      if (idx >= 0) {
220
114k
        for (n = 0; n < g; n++) {
221
83.0k
          if ((uniqueIndex[n] == idx) && (uniqueScaling[n] == factor)) {
222
8.26k
            match = 1;
223
8.26k
            groupForChannel[c] = n;
224
8.26k
            break;
225
8.26k
          }
226
83.0k
        }
227
39.2k
        if (match == 0) {
228
30.9k
          if (g >= 8) return DE_MEMORY_ERROR;
229
30.9k
          uniqueIndex[g] = idx;
230
30.9k
          uniqueScaling[g] = factor;
231
30.9k
          groupForChannel[c] = g;
232
30.9k
          g++;
233
30.9k
        }
234
39.2k
      } else {
235
14.9k
        groupForChannel[c] = -1;
236
14.9k
      }
237
54.1k
    }
238
1.05M
  } else { /* no ducking */
239
6.91M
    for (c = 0; c < channelCount; c++) {
240
5.86M
      if (c >= 8) return DE_MEMORY_ERROR;
241
5.86M
      idx = gainSetIndex[c];
242
5.86M
      match = 0;
243
5.86M
      if (idx >= 0) {
244
216k
        for (n = 0; n < g; n++) {
245
148k
          if (uniqueIndex[n] == idx) {
246
71.7k
            match = 1;
247
71.7k
            groupForChannel[c] = n;
248
71.7k
            break;
249
71.7k
          }
250
148k
        }
251
139k
        if (match == 0) {
252
68.1k
          if (g >= 8) return DE_MEMORY_ERROR;
253
68.1k
          uniqueIndex[g] = idx;
254
68.1k
          groupForChannel[c] = g;
255
68.1k
          g++;
256
68.1k
        }
257
5.72M
      } else {
258
5.72M
        groupForChannel[c] = -1;
259
5.72M
      }
260
5.86M
    }
261
1.05M
  }
262
1.07M
  *nDrcChannelGroups = g;
263
264
1.07M
  if (drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF)) {
265
59.0k
    for (g = 0; g < *nDrcChannelGroups; g++) {
266
33.2k
      if (drcSetEffect & EB_DUCK_OTHER) {
267
2.27k
        uniqueIndex[g] = duckingSequence;
268
2.27k
      }
269
33.2k
      duckingModificationForChannelGroup[g].duckingScaling = uniqueScaling[g];
270
33.2k
      if (uniqueScaling[g] != FL2FXCONST_SGL(1.0f / (float)(1 << 2))) {
271
18.3k
        duckingModificationForChannelGroup[g].duckingScalingPresent = 1;
272
18.3k
      } else {
273
14.8k
        duckingModificationForChannelGroup[g].duckingScalingPresent = 0;
274
14.8k
      }
275
33.2k
    }
276
25.7k
  }
277
278
1.07M
  return DE_OK;
279
1.09M
}
280
281
FIXP_DBL
282
11.0k
dB2lin(const FIXP_DBL dB_m, const int dB_e, int* pLin_e) {
283
  /* get linear value from dB.
284
     return lin_val = 10^(dB_val/20) = 2^(log2(10)/20*dB_val)
285
     with dB_val = dB_m *2^dB_e and lin_val = lin_m * 2^lin_e */
286
11.0k
  FIXP_DBL lin_m =
287
11.0k
      f2Pow(fMult(dB_m, FL2FXCONST_DBL(0.1660964f * (float)(1 << 2))), dB_e - 2,
288
11.0k
            pLin_e);
289
290
11.0k
  return lin_m;
291
11.0k
}
292
293
FIXP_DBL
294
0
lin2dB(const FIXP_DBL lin_m, const int lin_e, int* pDb_e) {
295
  /* get dB value from linear value.
296
     return dB_val = 20*log10(lin_val)
297
     with dB_val = dB_m *2^dB_e and lin_val = lin_m * 2^lin_e */
298
0
  FIXP_DBL dB_m;
299
300
0
  if (lin_m == (FIXP_DBL)0) { /* return very small value representing -inf */
301
0
    dB_m = (FIXP_DBL)MINVAL_DBL;
302
0
    *pDb_e = DFRACT_BITS - 1;
303
0
  } else {
304
    /* 20*log10(lin_val) = 20/log2(10)*log2(lin_val) */
305
0
    dB_m = fMultDiv2(FL2FXCONST_DBL(6.02059991f / (float)(1 << 3)),
306
0
                     fLog2(lin_m, lin_e, pDb_e));
307
0
    *pDb_e += 3 + 1;
308
0
  }
309
310
0
  return dB_m;
311
0
}
312
313
FIXP_DBL
314
19.5k
approxDb2lin(const FIXP_DBL dB_m, const int dB_e, int* pLin_e) {
315
  /* get linear value from approximate dB.
316
     return lin_val = 2^(dB_val/6)
317
     with dB_val = dB_m *2^dB_e and lin_val = lin_m * 2^lin_e */
318
19.5k
  FIXP_DBL lin_m =
319
19.5k
      f2Pow(fMult(dB_m, FL2FXCONST_DBL(0.1666667f * (float)(1 << 2))), dB_e - 2,
320
19.5k
            pLin_e);
321
322
19.5k
  return lin_m;
323
19.5k
}
324
325
int bitstreamContainsMultibandDrc(HANDLE_UNI_DRC_CONFIG hUniDrcConfig,
326
0
                                  const int downmixId) {
327
0
  int i, g, d, seq;
328
0
  DRC_INSTRUCTIONS_UNI_DRC* pInst;
329
0
  DRC_COEFFICIENTS_UNI_DRC* pCoef = NULL;
330
0
  int isMultiband = 0;
331
332
0
  pCoef = selectDrcCoefficients(hUniDrcConfig, LOCATION_SELECTED);
333
0
  if (pCoef == NULL) return 0;
334
335
0
  for (i = 0; i < hUniDrcConfig->drcInstructionsUniDrcCount; i++) {
336
0
    pInst = &(hUniDrcConfig->drcInstructionsUniDrc[i]);
337
0
    for (d = 0; d < pInst->downmixIdCount; d++) {
338
0
      if (downmixId == pInst->downmixId[d]) {
339
0
        for (g = 0; g < pInst->nDrcChannelGroups; g++) {
340
0
          seq = pInst->gainSetIndexForChannelGroup[g];
341
0
          if (pCoef->gainSet[seq].bandCount > 1) {
342
0
            isMultiband = 1;
343
0
          }
344
0
        }
345
0
      }
346
0
    }
347
0
  }
348
349
0
  return isMultiband;
350
0
}
351
352
0
FIXP_DBL getDownmixOffset(DOWNMIX_INSTRUCTIONS* pDown, int baseChannelCount) {
353
0
  FIXP_DBL downmixOffset = FL2FXCONST_DBL(1.0f / (1 << 1)); /* e = 1 */
354
0
  if ((pDown->bsDownmixOffset == 1) || (pDown->bsDownmixOffset == 2)) {
355
0
    int e_a, e_downmixOffset;
356
0
    FIXP_DBL a, q;
357
0
    if (baseChannelCount <= pDown->targetChannelCount) return downmixOffset;
358
359
0
    q = fDivNorm((FIXP_DBL)pDown->targetChannelCount,
360
0
                 (FIXP_DBL)baseChannelCount); /* e = 0 */
361
0
    a = lin2dB(q, 0, &e_a);
362
0
    if (pDown->bsDownmixOffset == 2) {
363
0
      e_a += 1; /* a *= 2 */
364
0
    }
365
    /* a = 0.5 * round (a) */
366
0
    a = fixp_round(a, e_a) >> 1;
367
0
    downmixOffset = dB2lin(a, e_a, &e_downmixOffset);
368
0
    downmixOffset = scaleValue(downmixOffset, e_downmixOffset - 1);
369
0
  }
370
0
  return downmixOffset;
371
0
}