Coverage Report

Created: 2025-11-16 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libDRCdec/src/drcDec_gainDecoder.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-D DRC decoder library **************************
96
97
   Author(s):
98
99
   Description:
100
101
*******************************************************************************/
102
103
#include "drcDec_types.h"
104
#include "drcDec_gainDecoder.h"
105
#include "drcGainDec_preprocess.h"
106
#include "drcGainDec_init.h"
107
#include "drcGainDec_process.h"
108
#include "drcDec_tools.h"
109
110
/*******************************************/
111
/* static functions                        */
112
/*******************************************/
113
114
static int _fitsLocation(DRC_INSTRUCTIONS_UNI_DRC* pInst,
115
24.7k
                         const GAIN_DEC_LOCATION drcLocation) {
116
24.7k
  int downmixId = pInst->drcApplyToDownmix ? pInst->downmixId[0] : 0;
117
24.7k
  switch (drcLocation) {
118
12.4k
    case GAIN_DEC_DRC1:
119
12.4k
      return (downmixId == 0);
120
0
    case GAIN_DEC_DRC1_DRC2:
121
0
      return ((downmixId == 0) || (downmixId == DOWNMIX_ID_ANY_DOWNMIX));
122
0
    case GAIN_DEC_DRC2:
123
0
      return (downmixId == DOWNMIX_ID_ANY_DOWNMIX);
124
0
    case GAIN_DEC_DRC3:
125
0
      return ((downmixId != 0) && (downmixId != DOWNMIX_ID_ANY_DOWNMIX));
126
12.3k
    case GAIN_DEC_DRC2_DRC3:
127
12.3k
      return (downmixId != 0);
128
24.7k
  }
129
0
  return 0;
130
24.7k
}
131
132
static void _setChannelGains(HANDLE_DRC_GAIN_DECODER hGainDec,
133
                             const int numChannelGains,
134
9.78k
                             const FIXP_DBL* channelGainDb) {
135
9.78k
  int i, channelGain_e;
136
9.78k
  FIXP_DBL channelGain;
137
9.78k
  FDK_ASSERT(numChannelGains <= 8);
138
21.8k
  for (i = 0; i < numChannelGains; i++) {
139
12.0k
    if (channelGainDb[i] == (FIXP_DBL)MINVAL_DBL) {
140
0
      hGainDec->channelGain[i] = (FIXP_DBL)0;
141
12.0k
    } else {
142
      /* add loudness normalisation gain (dB) to channel gain (dB) */
143
12.0k
      FIXP_DBL tmp_channelGainDb = (channelGainDb[i] >> 1) +
144
12.0k
                                   (hGainDec->loudnessNormalisationGainDb >> 2);
145
12.0k
      tmp_channelGainDb =
146
12.0k
          SATURATE_LEFT_SHIFT(tmp_channelGainDb, 1, DFRACT_BITS);
147
12.0k
      channelGain = dB2lin(tmp_channelGainDb, 8, &channelGain_e);
148
12.0k
      hGainDec->channelGain[i] = scaleValue(channelGain, channelGain_e - 8);
149
12.0k
    }
150
12.0k
  }
151
9.78k
}
152
153
/*******************************************/
154
/* public functions                        */
155
/*******************************************/
156
157
DRC_ERROR
158
31.2k
drcDec_GainDecoder_Open(HANDLE_DRC_GAIN_DECODER* phGainDec) {
159
31.2k
  DRC_GAIN_DECODER* hGainDec = NULL;
160
161
31.2k
  hGainDec = (DRC_GAIN_DECODER*)FDKcalloc(1, sizeof(DRC_GAIN_DECODER));
162
31.2k
  if (hGainDec == NULL) return DE_MEMORY_ERROR;
163
164
31.2k
  hGainDec->multiBandActiveDrcIndex = -1;
165
31.2k
  hGainDec->channelGainActiveDrcIndex = -1;
166
167
31.2k
  *phGainDec = hGainDec;
168
169
31.2k
  return DE_OK;
170
31.2k
}
171
172
DRC_ERROR
173
140k
drcDec_GainDecoder_Init(HANDLE_DRC_GAIN_DECODER hGainDec) {
174
140k
  DRC_ERROR err = DE_OK;
175
176
140k
  err = initGainDec(hGainDec);
177
140k
  if (err) return err;
178
179
140k
  initDrcGainBuffers(hGainDec->frameSize, &hGainDec->drcGainBuffers);
180
181
140k
  return err;
182
140k
}
183
184
DRC_ERROR
185
drcDec_GainDecoder_SetParam(HANDLE_DRC_GAIN_DECODER hGainDec,
186
                            const GAIN_DEC_PARAM paramType,
187
300k
                            const int paramValue) {
188
300k
  switch (paramType) {
189
150k
    case GAIN_DEC_FRAME_SIZE:
190
150k
      if (paramValue < 0) return DE_PARAM_OUT_OF_RANGE;
191
150k
      hGainDec->frameSize = paramValue;
192
150k
      break;
193
150k
    case GAIN_DEC_SAMPLE_RATE:
194
150k
      if (paramValue < 0) return DE_PARAM_OUT_OF_RANGE;
195
150k
      hGainDec->deltaTminDefault = getDeltaTmin(paramValue);
196
150k
      break;
197
0
    default:
198
0
      return DE_PARAM_INVALID;
199
300k
  }
200
300k
  return DE_OK;
201
300k
}
202
203
DRC_ERROR
204
drcDec_GainDecoder_SetCodecDependentParameters(
205
    HANDLE_DRC_GAIN_DECODER hGainDec, const DELAY_MODE delayMode,
206
    const int timeDomainSupported,
207
18.8k
    const SUBBAND_DOMAIN_MODE subbandDomainSupported) {
208
18.8k
  if ((delayMode != DM_REGULAR_DELAY) && (delayMode != DM_LOW_DELAY)) {
209
0
    return DE_NOT_OK;
210
0
  }
211
18.8k
  hGainDec->delayMode = delayMode;
212
18.8k
  hGainDec->timeDomainSupported = timeDomainSupported;
213
18.8k
  hGainDec->subbandDomainSupported = subbandDomainSupported;
214
215
18.8k
  return DE_OK;
216
18.8k
}
217
218
DRC_ERROR
219
drcDec_GainDecoder_Config(HANDLE_DRC_GAIN_DECODER hGainDec,
220
                          HANDLE_UNI_DRC_CONFIG hUniDrcConfig,
221
                          const UCHAR numSelectedDrcSets,
222
                          const SCHAR* selectedDrcSetIds,
223
95.8k
                          const UCHAR* selectedDownmixIds) {
224
95.8k
  DRC_ERROR err = DE_OK;
225
95.8k
  int a;
226
227
95.8k
  hGainDec->nActiveDrcs = 0;
228
95.8k
  hGainDec->multiBandActiveDrcIndex = -1;
229
95.8k
  hGainDec->channelGainActiveDrcIndex = -1;
230
192k
  for (a = 0; a < numSelectedDrcSets; a++) {
231
97.1k
    err = initActiveDrc(hGainDec, hUniDrcConfig, selectedDrcSetIds[a],
232
97.1k
                        selectedDownmixIds[a]);
233
97.1k
    if (err) return err;
234
97.1k
  }
235
236
95.1k
  err = initActiveDrcOffset(hGainDec);
237
95.1k
  if (err) return err;
238
239
94.9k
  return err;
240
95.1k
}
241
242
DRC_ERROR
243
31.2k
drcDec_GainDecoder_Close(HANDLE_DRC_GAIN_DECODER* phGainDec) {
244
31.2k
  if (*phGainDec != NULL) {
245
31.2k
    FDKfree(*phGainDec);
246
31.2k
    *phGainDec = NULL;
247
31.2k
  }
248
249
31.2k
  return DE_OK;
250
31.2k
}
251
252
DRC_ERROR
253
drcDec_GainDecoder_Preprocess(HANDLE_DRC_GAIN_DECODER hGainDec,
254
                              HANDLE_UNI_DRC_GAIN hUniDrcGain,
255
                              const FIXP_DBL loudnessNormalizationGainDb,
256
9.78k
                              const FIXP_SGL boost, const FIXP_SGL compress) {
257
9.78k
  DRC_ERROR err = DE_OK;
258
9.78k
  int a, c;
259
260
  /* lnbPointer is the index on the most recent node buffer */
261
9.78k
  hGainDec->drcGainBuffers.lnbPointer++;
262
9.78k
  if (hGainDec->drcGainBuffers.lnbPointer >= NUM_LNB_FRAMES)
263
1.70k
    hGainDec->drcGainBuffers.lnbPointer = 0;
264
265
22.2k
  for (a = 0; a < hGainDec->nActiveDrcs; a++) {
266
    /* prepare gain interpolation of sequences used by copying and modifying
267
     * nodes in node buffers */
268
13.1k
    err = prepareDrcGain(hGainDec, hUniDrcGain, compress, boost,
269
13.1k
                         loudnessNormalizationGainDb, a);
270
13.1k
    if (err) return err;
271
13.1k
  }
272
273
36.6k
  for (a = 0; a < MAX_ACTIVE_DRCS; a++) {
274
247k
    for (c = 0; c < 8; c++) {
275
219k
      hGainDec->activeDrc[a]
276
219k
          .lnbIndexForChannel[c][hGainDec->drcGainBuffers.lnbPointer] =
277
219k
          -1; /* "no DRC processing" */
278
219k
    }
279
27.4k
    hGainDec->activeDrc[a].subbandGainsReady = 0;
280
27.4k
  }
281
282
82.4k
  for (c = 0; c < 8; c++) {
283
73.2k
    hGainDec->drcGainBuffers
284
73.2k
        .channelGain[c][hGainDec->drcGainBuffers.lnbPointer] =
285
73.2k
        FL2FXCONST_DBL(1.0f / (float)(1 << 8));
286
73.2k
  }
287
288
9.16k
  return err;
289
9.78k
}
290
291
/* create gain sequence out of gain sequences of last frame for concealment and
292
 * flushing */
293
DRC_ERROR
294
drcDec_GainDecoder_Conceal(HANDLE_DRC_GAIN_DECODER hGainDec,
295
                           HANDLE_UNI_DRC_CONFIG hUniDrcConfig,
296
7.40k
                           HANDLE_UNI_DRC_GAIN hUniDrcGain) {
297
7.40k
  int seq, gainSequenceCount;
298
7.40k
  DRC_COEFFICIENTS_UNI_DRC* pCoef =
299
7.40k
      selectDrcCoefficients(hUniDrcConfig, LOCATION_SELECTED);
300
7.40k
  if (pCoef && pCoef->gainSequenceCount) {
301
3.66k
    gainSequenceCount = fMin(pCoef->gainSequenceCount, (UCHAR)12);
302
3.74k
  } else {
303
3.74k
    gainSequenceCount = 1;
304
3.74k
  }
305
306
29.7k
  for (seq = 0; seq < gainSequenceCount; seq++) {
307
22.3k
    int lastNodeIndex = 0;
308
22.3k
    FIXP_SGL lastGainDb = (FIXP_SGL)0;
309
310
22.3k
    lastNodeIndex = hUniDrcGain->nNodes[seq] - 1;
311
22.3k
    if ((lastNodeIndex >= 0) && (lastNodeIndex < 16)) {
312
20.1k
      lastGainDb = hUniDrcGain->gainNode[seq][lastNodeIndex].gainDb;
313
20.1k
    }
314
315
22.3k
    hUniDrcGain->nNodes[seq] = 1;
316
22.3k
    if (lastGainDb > (FIXP_SGL)0) {
317
3.77k
      hUniDrcGain->gainNode[seq][0].gainDb =
318
3.77k
          FX_DBL2FX_SGL(fMult(FL2FXCONST_SGL(0.9f), lastGainDb));
319
18.5k
    } else {
320
18.5k
      hUniDrcGain->gainNode[seq][0].gainDb =
321
18.5k
          FX_DBL2FX_SGL(fMult(FL2FXCONST_SGL(0.98f), lastGainDb));
322
18.5k
    }
323
22.3k
    hUniDrcGain->gainNode[seq][0].time = hGainDec->frameSize - 1;
324
22.3k
  }
325
7.40k
  return DE_OK;
326
7.40k
}
327
328
void drcDec_GainDecoder_SetChannelGains(HANDLE_DRC_GAIN_DECODER hGainDec,
329
                                        const int numChannels,
330
                                        const int frameSize,
331
                                        const FIXP_DBL* channelGainDb,
332
                                        const int audioBufferChannelOffset,
333
9.78k
                                        FIXP_DBL* audioBuffer) {
334
9.78k
  int c, i;
335
336
9.78k
  if (hGainDec->channelGainActiveDrcIndex >= 0) {
337
    /* channel gains will be applied in drcDec_GainDecoder_ProcessTimeDomain or
338
     * drcDec_GainDecoder_ProcessSubbandDomain, respectively. */
339
6.83k
    _setChannelGains(hGainDec, numChannels, channelGainDb);
340
341
6.83k
    if (!hGainDec->status) { /* overwrite previous channel gains at startup */
342
371
      DRC_GAIN_BUFFERS* pDrcGainBuffers = &hGainDec->drcGainBuffers;
343
753
      for (c = 0; c < numChannels; c++) {
344
2.29k
        for (i = 0; i < NUM_LNB_FRAMES; i++) {
345
1.91k
          pDrcGainBuffers->channelGain[c][i] = hGainDec->channelGain[c];
346
1.91k
        }
347
382
      }
348
371
      hGainDec->status = 1;
349
371
    }
350
6.83k
  } else {
351
    /* smooth and apply channel gains */
352
2.94k
    FIXP_DBL prevChannelGain[8];
353
7.70k
    for (c = 0; c < numChannels; c++) {
354
4.75k
      prevChannelGain[c] = hGainDec->channelGain[c];
355
4.75k
    }
356
357
2.94k
    _setChannelGains(hGainDec, numChannels, channelGainDb);
358
359
2.94k
    if (!hGainDec->status) { /* overwrite previous channel gains at startup */
360
640
      for (c = 0; c < numChannels; c++)
361
382
        prevChannelGain[c] = hGainDec->channelGain[c];
362
258
      hGainDec->status = 1;
363
258
    }
364
365
7.70k
    for (c = 0; c < numChannels; c++) {
366
4.75k
      INT n_min = fMin(fMin(CntLeadingZeros(prevChannelGain[c]),
367
4.75k
                            CntLeadingZeros(hGainDec->channelGain[c])) -
368
4.75k
                           1,
369
4.75k
                       9);
370
4.75k
      FIXP_DBL gain = prevChannelGain[c] << n_min;
371
4.75k
      FIXP_DBL stepsize = ((hGainDec->channelGain[c] << n_min) - gain);
372
4.75k
      if (stepsize != (FIXP_DBL)0) {
373
194
        if (frameSize == 1024)
374
0
          stepsize = stepsize >> 10;
375
194
        else
376
194
          stepsize = (LONG)stepsize / frameSize;
377
194
      }
378
4.75k
      n_min = 9 - n_min;
379
#ifdef FUNCTION_drcDec_GainDecoder_SetChannelGains_func1
380
      drcDec_GainDecoder_SetChannelGains_func1(audioBuffer, gain, stepsize,
381
                                               n_min, frameSize);
382
#else
383
8.52M
      for (i = 0; i < frameSize; i++) {
384
8.52M
        audioBuffer[i] = fMultDiv2(audioBuffer[i], gain) << n_min;
385
8.52M
        gain += stepsize;
386
8.52M
      }
387
4.75k
#endif
388
4.75k
      audioBuffer += audioBufferChannelOffset;
389
4.75k
    }
390
2.94k
  }
391
9.78k
}
392
393
DRC_ERROR
394
drcDec_GainDecoder_ProcessTimeDomain(
395
    HANDLE_DRC_GAIN_DECODER hGainDec, const int delaySamples,
396
    const GAIN_DEC_LOCATION drcLocation, const int channelOffset,
397
    const int drcChannelOffset, const int numChannelsProcessed,
398
18.3k
    const int timeDataChannelOffset, FIXP_DBL* audioIOBuffer) {
399
18.3k
  DRC_ERROR err = DE_OK;
400
18.3k
  int a;
401
402
18.3k
  if (!hGainDec->timeDomainSupported) {
403
0
    return DE_NOT_OK;
404
0
  }
405
406
42.9k
  for (a = 0; a < hGainDec->nActiveDrcs; a++) {
407
24.7k
    if (!_fitsLocation(hGainDec->activeDrc[a].pInst, drcLocation)) continue;
408
409
    /* Apply DRC */
410
12.4k
    err = processDrcTime(hGainDec, a, delaySamples, channelOffset,
411
12.4k
                         drcChannelOffset, numChannelsProcessed,
412
12.4k
                         timeDataChannelOffset, audioIOBuffer);
413
12.4k
    if (err) return err;
414
12.4k
  }
415
416
18.1k
  return err;
417
18.3k
}
418
419
DRC_ERROR
420
drcDec_GainDecoder_ProcessSubbandDomain(
421
    HANDLE_DRC_GAIN_DECODER hGainDec, const int delaySamples,
422
    const GAIN_DEC_LOCATION drcLocation, const int channelOffset,
423
    const int drcChannelOffset, const int numChannelsProcessed,
424
    const int processSingleTimeslot, FIXP_DBL* audioIOBufferReal[],
425
0
    FIXP_DBL* audioIOBufferImag[]) {
426
0
  DRC_ERROR err = DE_OK;
427
0
  int a;
428
429
0
  if (hGainDec->subbandDomainSupported == SDM_OFF) {
430
0
    return DE_NOT_OK;
431
0
  }
432
433
0
  for (a = 0; a < hGainDec->nActiveDrcs; a++) {
434
0
    if (!_fitsLocation(hGainDec->activeDrc[a].pInst, drcLocation)) continue;
435
436
    /* Apply DRC */
437
0
    err = processDrcSubband(hGainDec, a, delaySamples, channelOffset,
438
0
                            drcChannelOffset, numChannelsProcessed,
439
0
                            processSingleTimeslot, audioIOBufferReal,
440
0
                            audioIOBufferImag);
441
0
    if (err) return err;
442
0
  }
443
444
0
  return err;
445
0
}
446
447
DRC_ERROR
448
drcDec_GainDecoder_SetLoudnessNormalizationGainDb(
449
9.78k
    HANDLE_DRC_GAIN_DECODER hGainDec, FIXP_DBL loudnessNormalizationGainDb) {
450
9.78k
  hGainDec->loudnessNormalisationGainDb = loudnessNormalizationGainDb;
451
452
9.78k
  return DE_OK;
453
9.78k
}
454
455
2.38k
int drcDec_GainDecoder_GetFrameSize(HANDLE_DRC_GAIN_DECODER hGainDec) {
456
2.38k
  if (hGainDec == NULL) return -1;
457
458
2.38k
  return hGainDec->frameSize;
459
2.38k
}
460
461
2.38k
int drcDec_GainDecoder_GetDeltaTminDefault(HANDLE_DRC_GAIN_DECODER hGainDec) {
462
2.38k
  if (hGainDec == NULL) return -1;
463
464
2.38k
  return hGainDec->deltaTminDefault;
465
2.38k
}