Coverage Report

Created: 2025-08-26 06:50

/src/aac/libSACenc/src/sacenc_onsetdetect.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 surround encoder library *************************
96
97
   Author(s):   Max Neuendorf
98
99
   Description: Encoder Library Interface
100
                Detect Onset in current frame
101
102
*******************************************************************************/
103
104
/**************************************************************************/ /**
105
   \file
106
   Description of file contents
107
 ******************************************************************************/
108
109
/* Includes ******************************************************************/
110
#include "sacenc_onsetdetect.h"
111
#include "genericStds.h"
112
#include "sacenc_vectorfunctions.h"
113
114
/* Defines *******************************************************************/
115
#define SPACE_ONSET_THRESHOLD (3.0)
116
0
#define SPACE_ONSET_THRESHOLD_SF (3)
117
#define SPACE_ONSET_THRESHOLD_SQUARE                                        \
118
0
  (FL2FXCONST_DBL((1.0 / (SPACE_ONSET_THRESHOLD * SPACE_ONSET_THRESHOLD)) * \
119
0
                  (float)(1 << SPACE_ONSET_THRESHOLD_SF)))
120
121
/* Data Types ****************************************************************/
122
struct ONSET_DETECT {
123
  INT maxTimeSlots;
124
  INT minTransientDistance;
125
  INT avgEnergyDistance;
126
  INT lowerBoundOnsetDetection;
127
  INT upperBoundOnsetDetection;
128
  FIXP_DBL *pEnergyHist__FDK;
129
  SCHAR *pEnergyHistScale;
130
  SCHAR avgEnergyDistanceScale;
131
};
132
133
/* Constants *****************************************************************/
134
135
/* Function / Class Declarations *********************************************/
136
137
/* Function / Class Definition ***********************************************/
138
FDK_SACENC_ERROR fdk_sacenc_onsetDetect_Open(HANDLE_ONSET_DETECT *phOnset,
139
0
                                             const UINT maxTimeSlots) {
140
0
  FDK_SACENC_ERROR error = SACENC_OK;
141
0
  HANDLE_ONSET_DETECT hOnset = NULL;
142
143
0
  if (NULL == phOnset) {
144
0
    error = SACENC_INVALID_HANDLE;
145
0
  } else {
146
    /* Memory Allocation */
147
0
    FDK_ALLOCATE_MEMORY_1D(hOnset, 1, struct ONSET_DETECT);
148
0
    FDK_ALLOCATE_MEMORY_1D(hOnset->pEnergyHist__FDK, 16 + maxTimeSlots,
149
0
                           FIXP_DBL);
150
0
    FDK_ALLOCATE_MEMORY_1D(hOnset->pEnergyHistScale, 16 + maxTimeSlots, SCHAR);
151
152
0
    hOnset->maxTimeSlots = maxTimeSlots;
153
0
    hOnset->minTransientDistance =
154
0
        8; /* minimum distance between detected transients */
155
0
    hOnset->avgEnergyDistance = 16; /* average energy distance */
156
157
0
    hOnset->avgEnergyDistanceScale = 4;
158
0
    *phOnset = hOnset;
159
0
  }
160
0
  return error;
161
162
0
bail:
163
0
  fdk_sacenc_onsetDetect_Close(&hOnset);
164
0
  return ((SACENC_OK == error) ? SACENC_MEMORY_ERROR : error);
165
0
}
166
167
FDK_SACENC_ERROR fdk_sacenc_onsetDetect_Init(
168
    HANDLE_ONSET_DETECT hOnset,
169
0
    const ONSET_DETECT_CONFIG *const pOnsetDetectConfig, const UINT initFlags) {
170
0
  FDK_SACENC_ERROR error = SACENC_OK;
171
172
0
  if ((NULL == hOnset) || (pOnsetDetectConfig == NULL)) {
173
0
    error = SACENC_INVALID_HANDLE;
174
0
  } else {
175
0
    if ((pOnsetDetectConfig->maxTimeSlots > hOnset->maxTimeSlots) ||
176
0
        (pOnsetDetectConfig->upperBoundOnsetDetection <
177
0
         hOnset->lowerBoundOnsetDetection)) {
178
0
      error = SACENC_INVALID_CONFIG;
179
0
      goto bail;
180
0
    }
181
182
0
    hOnset->maxTimeSlots = pOnsetDetectConfig->maxTimeSlots;
183
0
    hOnset->lowerBoundOnsetDetection =
184
0
        pOnsetDetectConfig->lowerBoundOnsetDetection;
185
0
    hOnset->upperBoundOnsetDetection =
186
0
        pOnsetDetectConfig->upperBoundOnsetDetection;
187
188
0
    hOnset->minTransientDistance =
189
0
        8; /* minimum distance between detected transients */
190
0
    hOnset->avgEnergyDistance = 16; /* average energy distance */
191
192
0
    hOnset->avgEnergyDistanceScale = 4;
193
194
    /* Init / Reset */
195
0
    if (initFlags) {
196
0
      int i;
197
0
      for (i = 0; i < hOnset->avgEnergyDistance + hOnset->maxTimeSlots; i++)
198
0
        hOnset->pEnergyHistScale[i] = -(DFRACT_BITS - 3);
199
200
0
      FDKmemset_flex(
201
0
          hOnset->pEnergyHist__FDK,
202
0
          FL2FXCONST_DBL(SACENC_FLOAT_EPSILON * (1 << (DFRACT_BITS - 3))),
203
0
          hOnset->avgEnergyDistance + hOnset->maxTimeSlots);
204
0
    }
205
0
  }
206
207
0
bail:
208
0
  return error;
209
0
}
210
211
/**************************************************************************/
212
213
0
FDK_SACENC_ERROR fdk_sacenc_onsetDetect_Close(HANDLE_ONSET_DETECT *phOnset) {
214
0
  FDK_SACENC_ERROR error = SACENC_OK;
215
216
0
  if ((NULL != phOnset) && (NULL != *phOnset)) {
217
0
    if (NULL != (*phOnset)->pEnergyHist__FDK) {
218
0
      FDKfree((*phOnset)->pEnergyHist__FDK);
219
0
    }
220
0
    (*phOnset)->pEnergyHist__FDK = NULL;
221
222
0
    if (NULL != (*phOnset)->pEnergyHistScale) {
223
0
      FDKfree((*phOnset)->pEnergyHistScale);
224
0
    }
225
0
    (*phOnset)->pEnergyHistScale = NULL;
226
0
    FDKfree(*phOnset);
227
0
    *phOnset = NULL;
228
0
  }
229
0
  return error;
230
0
}
231
232
/**************************************************************************/
233
234
FDK_SACENC_ERROR fdk_sacenc_onsetDetect_Update(HANDLE_ONSET_DETECT hOnset,
235
0
                                               const INT timeSlots) {
236
0
  FDK_SACENC_ERROR error = SACENC_OK;
237
238
0
  if (NULL == hOnset) {
239
0
    error = SACENC_INVALID_HANDLE;
240
0
  } else {
241
0
    if (timeSlots > hOnset->maxTimeSlots) {
242
0
      error = SACENC_INVALID_CONFIG;
243
0
    } else {
244
0
      int i;
245
      /* Shift old data */
246
0
      for (i = 0; i < hOnset->avgEnergyDistance; i++) {
247
0
        hOnset->pEnergyHist__FDK[i] = hOnset->pEnergyHist__FDK[i + timeSlots];
248
0
        hOnset->pEnergyHistScale[i] = hOnset->pEnergyHistScale[i + timeSlots];
249
0
      }
250
251
      /* Clear for new data */
252
0
      FDKmemset_flex(&hOnset->pEnergyHist__FDK[hOnset->avgEnergyDistance],
253
0
                     FL2FXCONST_DBL(SACENC_FLOAT_EPSILON), timeSlots);
254
0
    }
255
0
  }
256
0
  return error;
257
0
}
258
259
/**************************************************************************/
260
261
FDK_SACENC_ERROR fdk_sacenc_onsetDetect_Apply(
262
    HANDLE_ONSET_DETECT hOnset, const INT nTimeSlots, const INT nHybridBands,
263
    FIXP_DPK *const *const ppHybridData__FDK, const INT hybridDataScale,
264
0
    const INT prevPos, INT pTransientPos[MAX_NUM_TRANS]) {
265
0
  FDK_SACENC_ERROR error = SACENC_OK;
266
267
0
  C_ALLOC_SCRATCH_START(envs, FIXP_DBL, (16 + MAX_TIME_SLOTS))
268
0
  FDKmemclear(envs, (16 + MAX_TIME_SLOTS) * sizeof(FIXP_DBL));
269
270
0
  if ((hOnset == NULL) || (pTransientPos == NULL) ||
271
0
      (ppHybridData__FDK == NULL)) {
272
0
    error = SACENC_INVALID_HANDLE;
273
0
  } else {
274
0
    int i, ts, trCnt, currPos;
275
276
0
    if ((nTimeSlots < 0) || (nTimeSlots > hOnset->maxTimeSlots) ||
277
0
        (hOnset->lowerBoundOnsetDetection < -1) ||
278
0
        (hOnset->upperBoundOnsetDetection > nHybridBands)) {
279
0
      error = SACENC_INVALID_CONFIG;
280
0
      goto bail;
281
0
    }
282
283
0
    const int lowerBoundOnsetDetection = hOnset->lowerBoundOnsetDetection;
284
0
    const int upperBoundOnsetDetection = hOnset->upperBoundOnsetDetection;
285
0
    const int M = hOnset->avgEnergyDistance;
286
287
0
    {
288
0
      SCHAR *envScale = hOnset->pEnergyHistScale;
289
0
      FIXP_DBL *env = hOnset->pEnergyHist__FDK;
290
0
      const FIXP_DBL threshold_square = SPACE_ONSET_THRESHOLD_SQUARE;
291
292
0
      trCnt = 0;
293
294
      /* reset transient array */
295
0
      FDKmemset_flex(pTransientPos, -1, MAX_NUM_TRANS);
296
297
      /* minimum transient distance of minTransDist QMF samples */
298
0
      if (prevPos > 0) {
299
0
        currPos = FDKmax(nTimeSlots,
300
0
                         prevPos - nTimeSlots + hOnset->minTransientDistance);
301
0
      } else {
302
0
        currPos = nTimeSlots;
303
0
      }
304
305
      /* get energy and scalefactor for each time slot */
306
0
      int outScale;
307
0
      int inScale = 3; /* scale factor determined empirically */
308
0
      for (ts = 0; ts < nTimeSlots; ts++) {
309
0
        env[M + ts] = sumUpCplxPow2(
310
0
            &ppHybridData__FDK[ts][lowerBoundOnsetDetection + 1],
311
0
            SUM_UP_DYNAMIC_SCALE, inScale, &outScale,
312
0
            upperBoundOnsetDetection - lowerBoundOnsetDetection - 1);
313
0
        envScale[M + ts] = outScale + (hybridDataScale << 1);
314
0
      }
315
316
      /* calculate common scale for all time slots */
317
0
      SCHAR maxScale = -(DFRACT_BITS - 1);
318
0
      for (i = 0; i < (nTimeSlots + M); i++) {
319
0
        maxScale = fixMax(maxScale, envScale[i]);
320
0
      }
321
322
      /* apply common scale and store energy in temporary buffer */
323
0
      for (i = 0; i < (nTimeSlots + M); i++) {
324
0
        envs[i] = env[i] >> fixMin((maxScale - envScale[i]), (DFRACT_BITS - 1));
325
0
      }
326
327
0
      FIXP_DBL maxVal = FL2FXCONST_DBL(0.0f);
328
0
      for (i = 0; i < (nTimeSlots + M); i++) {
329
0
        maxVal |= fAbs(envs[i]);
330
0
      }
331
332
0
      int s = fixMax(0, CntLeadingZeros(maxVal) - 1);
333
334
0
      for (i = 0; i < (nTimeSlots + M); i++) {
335
0
        envs[i] = envs[i] << s;
336
0
      }
337
338
0
      int currPosPrev = currPos;
339
0
      FIXP_DBL p1, p2;
340
0
      p2 = FL2FXCONST_DBL(0.0f);
341
0
      for (; (currPos < (nTimeSlots << 1)) && (trCnt < MAX_NUM_TRANS);
342
0
           currPos++) {
343
0
        p1 = fMultDiv2(envs[currPos - nTimeSlots + M], threshold_square) >>
344
0
             (SPACE_ONSET_THRESHOLD_SF - 1);
345
346
        /* Calculate average of past M energy values */
347
0
        if (currPosPrev == (currPos - 1)) {
348
          /* remove last and add new element */
349
0
          p2 -= (envs[currPosPrev - nTimeSlots] >>
350
0
                 (int)hOnset->avgEnergyDistanceScale);
351
0
          p2 += (envs[currPos - nTimeSlots + M - 1] >>
352
0
                 (int)hOnset->avgEnergyDistanceScale);
353
0
        } else {
354
          /* calculate complete vector */
355
0
          p2 = FL2FXCONST_DBL(0.0f);
356
0
          for (ts = 0; ts < M; ts++) {
357
0
            p2 += (envs[currPos - nTimeSlots + ts] >>
358
0
                   (int)hOnset->avgEnergyDistanceScale);
359
0
          }
360
0
        }
361
0
        currPosPrev = currPos;
362
363
0
        {
364
          /* save position if transient found */
365
0
          if (p1 > p2) {
366
0
            pTransientPos[trCnt++] = currPos;
367
0
            currPos += hOnset->minTransientDistance;
368
0
          }
369
0
        }
370
0
      } /* for currPos */
371
0
    }
372
373
0
  } /* valid handle*/
374
0
bail:
375
376
0
  C_ALLOC_SCRATCH_END(envs, FIXP_DBL, (16 + MAX_TIME_SLOTS))
377
378
0
  return error;
379
0
}
380
381
/**************************************************************************/