Coverage Report

Created: 2025-07-01 06:21

/src/aac/libSACenc/src/sacenc_delay.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):   Christian Goettlinger
98
99
   Description: Encoder Library Interface
100
                delay management of the encoder
101
102
*******************************************************************************/
103
104
/**************************************************************************/ /**
105
 \file
106
 This file contains all delay infrastructure
107
 ******************************************************************************/
108
109
/* Includes ******************************************************************/
110
#include "sacenc_delay.h"
111
#include "sacenc_const.h"
112
#include "FDK_matrixCalloc.h"
113
114
/* Defines *******************************************************************/
115
116
/* Data Types ****************************************************************/
117
struct DELAY {
118
  struct DELAY_CONFIG {
119
    /* Routing Config Switches*/
120
    INT bDmxAlign;
121
    INT bTimeDomDmx;
122
    INT bMinimizeDelay;
123
    INT bSacTimeAlignmentDynamicOut;
124
125
    /* Needed Input Variables*/
126
    INT nQmfLen;
127
    INT nFrameLen;
128
    INT nSurroundDelay;
129
    INT nArbDmxDelay;
130
    INT nLimiterDelay;
131
    INT nCoreCoderDelay;
132
    INT nSacStreamMuxDelay;
133
    INT nSacTimeAlignment; /* Overwritten, if bSacTimeAlignmentDynamicOut */
134
  } config;
135
136
  /* Variable Delaybuffers -> Delays */
137
  INT nDmxAlignBuffer;
138
  INT nSurroundAnalysisBuffer;
139
  INT nArbDmxAnalysisBuffer;
140
  INT nOutputAudioBuffer;
141
  INT nBitstreamFrameBuffer;
142
  INT nOutputAudioQmfFrameBuffer;
143
  INT nDiscardOutFrames;
144
145
  /* Variable Delaybuffers Computation Variables */
146
  INT nBitstreamFrameBufferSize;
147
148
  /* Output: Infos */
149
  INT nInfoDmxDelay; /* Delay of the downmixed signal after the space encoder */
150
  INT nInfoCodecDelay; /* Delay of the whole en-/decoder including CoreCoder */
151
  INT nInfoDecoderDelay; /* Delay of the Mpeg Surround decoder */
152
};
153
154
/* Constants *****************************************************************/
155
156
/* Function / Class Declarations *********************************************/
157
158
/* Function / Class Definition ***********************************************/
159
160
/*-----------------------------------------------------------------------------
161
functionname: fdk_sacenc_delay_Open()
162
description:  initializes Delays
163
returns:      noError on success, an apropriate error code else
164
-----------------------------------------------------------------------------*/
165
0
FDK_SACENC_ERROR fdk_sacenc_delay_Open(HANDLE_DELAY *phDelay) {
166
0
  FDK_SACENC_ERROR error = SACENC_OK;
167
168
0
  if (NULL == phDelay) {
169
0
    error = SACENC_INVALID_HANDLE;
170
0
  } else {
171
0
    FDK_ALLOCATE_MEMORY_1D(*phDelay, 1, struct DELAY);
172
0
  }
173
0
  return error;
174
175
0
bail:
176
0
  fdk_sacenc_delay_Close(phDelay);
177
0
  return ((SACENC_OK == error) ? SACENC_MEMORY_ERROR : error);
178
0
}
179
180
/*-----------------------------------------------------------------------------
181
functionname: fdk_sacenc_delay_Close()
182
description:  destructs Delay
183
returns:      noError on success, an apropriate error code else
184
-----------------------------------------------------------------------------*/
185
0
FDK_SACENC_ERROR fdk_sacenc_delay_Close(HANDLE_DELAY *phDelay) {
186
0
  FDK_SACENC_ERROR error = SACENC_OK;
187
188
0
  if (NULL == phDelay) {
189
0
    error = SACENC_INVALID_HANDLE;
190
0
  } else {
191
0
    if (NULL != *phDelay) {
192
0
      FDK_FREE_MEMORY_1D(*phDelay);
193
0
    }
194
0
  }
195
0
  return error;
196
0
}
197
198
FDK_SACENC_ERROR fdk_sacenc_delay_Init(HANDLE_DELAY hDelay, const INT nQmfLen,
199
                                       const INT nFrameLen,
200
                                       const INT nCoreCoderDelay,
201
0
                                       const INT nSacStreamMuxDelay) {
202
0
  FDK_SACENC_ERROR error = SACENC_OK;
203
204
0
  if (NULL == hDelay) {
205
0
    error = SACENC_INVALID_HANDLE;
206
0
  } else {
207
    /* Fill structure before calculation */
208
0
    FDKmemclear(&hDelay->config, sizeof(hDelay->config));
209
210
0
    hDelay->config.nQmfLen = nQmfLen;
211
0
    hDelay->config.nFrameLen = nFrameLen;
212
0
    hDelay->config.nCoreCoderDelay = nCoreCoderDelay;
213
0
    hDelay->config.nSacStreamMuxDelay = nSacStreamMuxDelay;
214
0
  }
215
0
  return error;
216
0
}
217
218
/*-----------------------------------------------------------------------------
219
functionname: fdk_sacenc_delay_SubCalulateBufferDelays()
220
description:  Calculates the Delays of the buffers
221
returns:      Error Code
222
-----------------------------------------------------------------------------*/
223
0
FDK_SACENC_ERROR fdk_sacenc_delay_SubCalulateBufferDelays(HANDLE_DELAY hDel) {
224
0
  FDK_SACENC_ERROR error = SACENC_OK;
225
226
0
  if (NULL == hDel) {
227
0
    error = SACENC_INVALID_HANDLE;
228
0
  } else {
229
0
    int nEncoderAnDelay, nEncoderSynDelay, nEncoderWinDelay, nDecoderAnDelay,
230
0
        nDecoderSynDelay, nResidualCoderFrameDelay,
231
0
        nArbDmxResidualCoderFrameDelay;
232
233
0
    if (hDel->config.bSacTimeAlignmentDynamicOut > 0) {
234
0
      hDel->config.nSacTimeAlignment = 0;
235
0
    }
236
237
0
    {
238
0
      nEncoderAnDelay =
239
0
          2 * hDel->config.nQmfLen +
240
0
          hDel->config.nQmfLen / 2; /* Only Ld-QMF Delay, no hybrid */
241
0
      nEncoderSynDelay = 1 * hDel->config.nQmfLen + hDel->config.nQmfLen / 2;
242
0
      nDecoderAnDelay = 2 * hDel->config.nQmfLen + hDel->config.nQmfLen / 2;
243
0
      nDecoderSynDelay = 1 * hDel->config.nQmfLen + hDel->config.nQmfLen / 2;
244
0
      nEncoderWinDelay =
245
0
          hDel->config.nFrameLen / 2; /* WindowLookahead is just half a frame */
246
0
    }
247
248
0
    { nResidualCoderFrameDelay = 0; }
249
250
0
    { nArbDmxResidualCoderFrameDelay = 0; }
251
252
    /* Calculate variable Buffer-Delays */
253
0
    if (hDel->config.bTimeDomDmx == 0) {
254
      /* ArbitraryDmx and TdDmx off */
255
0
      int tempDelay;
256
257
0
      hDel->nSurroundAnalysisBuffer = 0;
258
0
      hDel->nArbDmxAnalysisBuffer = 0;
259
0
      tempDelay = nEncoderSynDelay + hDel->config.nLimiterDelay +
260
0
                  hDel->config.nCoreCoderDelay +
261
0
                  hDel->config.nSacTimeAlignment + nDecoderAnDelay;
262
0
      tempDelay = (nResidualCoderFrameDelay * hDel->config.nFrameLen) +
263
0
                  hDel->config.nSacStreamMuxDelay - tempDelay;
264
265
0
      if (tempDelay > 0) {
266
0
        hDel->nBitstreamFrameBuffer = 0;
267
0
        hDel->nOutputAudioBuffer = tempDelay;
268
0
      } else {
269
0
        tempDelay = -tempDelay;
270
0
        hDel->nBitstreamFrameBuffer =
271
0
            (tempDelay + hDel->config.nFrameLen - 1) / hDel->config.nFrameLen;
272
0
        hDel->nOutputAudioBuffer =
273
0
            (hDel->nBitstreamFrameBuffer * hDel->config.nFrameLen) - tempDelay;
274
0
      }
275
276
0
      hDel->nOutputAudioQmfFrameBuffer =
277
0
          (hDel->nOutputAudioBuffer + (hDel->config.nQmfLen / 2) - 1) /
278
0
          hDel->config.nQmfLen;
279
280
0
      if (hDel->config.bDmxAlign > 0) {
281
0
        tempDelay = nEncoderWinDelay + nEncoderAnDelay + nEncoderSynDelay +
282
0
                    hDel->nOutputAudioBuffer + hDel->config.nLimiterDelay +
283
0
                    hDel->config.nCoreCoderDelay;
284
0
        hDel->nDiscardOutFrames =
285
0
            (tempDelay + hDel->config.nFrameLen - 1) / hDel->config.nFrameLen;
286
0
        hDel->nDmxAlignBuffer =
287
0
            hDel->nDiscardOutFrames * hDel->config.nFrameLen - tempDelay;
288
0
      } else {
289
0
        hDel->nDiscardOutFrames = 0;
290
0
        hDel->nDmxAlignBuffer = 0;
291
0
      }
292
293
      /* Output: Info-Variables */
294
0
      hDel->nInfoDmxDelay = hDel->nSurroundAnalysisBuffer + nEncoderAnDelay +
295
0
                            nEncoderWinDelay + nEncoderSynDelay +
296
0
                            hDel->nOutputAudioBuffer +
297
0
                            hDel->config.nLimiterDelay;
298
0
      hDel->nInfoCodecDelay =
299
0
          hDel->nInfoDmxDelay + hDel->config.nCoreCoderDelay +
300
0
          hDel->config.nSacTimeAlignment + nDecoderAnDelay + nDecoderSynDelay;
301
302
0
    } else {
303
      /* ArbitraryDmx or TdDmx on */
304
0
      int tempDelay1, tempDelay2, tempDelay12, tempDelay3;
305
306
0
      tempDelay1 = hDel->config.nArbDmxDelay - hDel->config.nSurroundDelay;
307
308
0
      if (tempDelay1 >= 0) {
309
0
        hDel->nSurroundAnalysisBuffer = tempDelay1;
310
0
        hDel->nArbDmxAnalysisBuffer = 0;
311
0
      } else {
312
0
        hDel->nSurroundAnalysisBuffer = 0;
313
0
        hDel->nArbDmxAnalysisBuffer = -tempDelay1;
314
0
      }
315
316
0
      tempDelay1 = nEncoderWinDelay + hDel->config.nSurroundDelay +
317
0
                   hDel->nSurroundAnalysisBuffer +
318
0
                   nEncoderAnDelay; /*Surround Path*/
319
0
      tempDelay2 = nEncoderWinDelay + hDel->config.nArbDmxDelay +
320
0
                   hDel->nArbDmxAnalysisBuffer +
321
0
                   nEncoderAnDelay; /* ArbDmx Compare Path */
322
0
      tempDelay3 = hDel->config.nArbDmxDelay + hDel->config.nLimiterDelay +
323
0
                   hDel->config.nCoreCoderDelay +
324
0
                   hDel->config.nSacTimeAlignment +
325
0
                   nDecoderAnDelay; /* ArbDmx Passthrough*/
326
327
0
      tempDelay12 =
328
0
          FDKmax(nResidualCoderFrameDelay, nArbDmxResidualCoderFrameDelay) *
329
0
          hDel->config.nFrameLen;
330
0
      tempDelay12 += hDel->config.nSacStreamMuxDelay;
331
332
0
      if (tempDelay1 > tempDelay2) {
333
0
        tempDelay12 += tempDelay1;
334
0
      } else {
335
0
        tempDelay12 += tempDelay2;
336
0
      }
337
338
0
      if (tempDelay3 > tempDelay12) {
339
0
        if (hDel->config.bMinimizeDelay > 0) {
340
0
          hDel->nBitstreamFrameBuffer =
341
0
              (tempDelay3 - tempDelay12) / hDel->config.nFrameLen; /*floor*/
342
0
          hDel->nOutputAudioBuffer = 0;
343
0
          hDel->nSurroundAnalysisBuffer +=
344
0
              (tempDelay3 - tempDelay12 -
345
0
               (hDel->nBitstreamFrameBuffer * hDel->config.nFrameLen));
346
0
          hDel->nArbDmxAnalysisBuffer +=
347
0
              (tempDelay3 - tempDelay12 -
348
0
               (hDel->nBitstreamFrameBuffer * hDel->config.nFrameLen));
349
0
        } else {
350
0
          hDel->nBitstreamFrameBuffer =
351
0
              ((tempDelay3 - tempDelay12) + hDel->config.nFrameLen - 1) /
352
0
              hDel->config.nFrameLen;
353
0
          hDel->nOutputAudioBuffer =
354
0
              hDel->nBitstreamFrameBuffer * hDel->config.nFrameLen +
355
0
              tempDelay12 - tempDelay3;
356
0
        }
357
0
      } else {
358
0
        hDel->nBitstreamFrameBuffer = 0;
359
0
        hDel->nOutputAudioBuffer = tempDelay12 - tempDelay3;
360
0
      }
361
362
0
      if (hDel->config.bDmxAlign > 0) {
363
0
        int tempDelay = hDel->config.nArbDmxDelay + hDel->nOutputAudioBuffer +
364
0
                        hDel->config.nLimiterDelay +
365
0
                        hDel->config.nCoreCoderDelay;
366
0
        hDel->nDiscardOutFrames =
367
0
            (tempDelay + hDel->config.nFrameLen - 1) / hDel->config.nFrameLen;
368
0
        hDel->nDmxAlignBuffer =
369
0
            hDel->nDiscardOutFrames * hDel->config.nFrameLen - tempDelay;
370
0
      } else {
371
0
        hDel->nDiscardOutFrames = 0;
372
0
        hDel->nDmxAlignBuffer = 0;
373
0
      }
374
375
      /* Output: Info-Variables */
376
0
      hDel->nInfoDmxDelay = hDel->config.nArbDmxDelay +
377
0
                            hDel->nOutputAudioBuffer +
378
0
                            hDel->config.nLimiterDelay;
379
0
      hDel->nInfoCodecDelay =
380
0
          hDel->nInfoDmxDelay + hDel->config.nCoreCoderDelay +
381
0
          hDel->config.nSacTimeAlignment + nDecoderAnDelay + nDecoderSynDelay;
382
0
      hDel->nInfoDecoderDelay = nDecoderAnDelay + nDecoderSynDelay;
383
384
0
    } /* ArbitraryDmx or TdDmx on */
385
386
    /* Additonal Variables needed for Computation Issues */
387
0
    hDel->nBitstreamFrameBufferSize = hDel->nBitstreamFrameBuffer + 1;
388
0
  }
389
390
0
  return error;
391
0
}
392
393
static FDK_SACENC_ERROR assignParameterInRange(
394
    const INT startRange, /* including startRange */
395
    const INT stopRange,  /* including stopRange */
396
    const INT value,      /* value to write*/
397
    INT *const ptr        /* destination pointer*/
398
0
) {
399
0
  FDK_SACENC_ERROR error = SACENC_INVALID_CONFIG;
400
401
0
  if ((startRange <= value) && (value <= stopRange)) {
402
0
    *ptr = value;
403
0
    error = SACENC_OK;
404
0
  }
405
406
0
  return error;
407
0
}
408
409
FDK_SACENC_ERROR fdk_sacenc_delay_SetDmxAlign(HANDLE_DELAY hDelay,
410
0
                                              const INT bDmxAlignIn) {
411
0
  return (assignParameterInRange(0, 1, bDmxAlignIn, &hDelay->config.bDmxAlign));
412
0
}
413
414
FDK_SACENC_ERROR fdk_sacenc_delay_SetTimeDomDmx(HANDLE_DELAY hDelay,
415
0
                                                const INT bTimeDomDmxIn) {
416
0
  return (
417
0
      assignParameterInRange(0, 1, bTimeDomDmxIn, &hDelay->config.bTimeDomDmx));
418
0
}
419
420
FDK_SACENC_ERROR fdk_sacenc_delay_SetSacTimeAlignmentDynamicOut(
421
0
    HANDLE_DELAY hDelay, const INT bSacTimeAlignmentDynamicOutIn) {
422
0
  return (assignParameterInRange(0, 1, bSacTimeAlignmentDynamicOutIn,
423
0
                                 &hDelay->config.bSacTimeAlignmentDynamicOut));
424
0
}
425
426
FDK_SACENC_ERROR fdk_sacenc_delay_SetNSacTimeAlignment(
427
0
    HANDLE_DELAY hDelay, const INT nSacTimeAlignmentIn) {
428
0
  return (assignParameterInRange(-32768, 32767, nSacTimeAlignmentIn,
429
0
                                 &hDelay->config.nSacTimeAlignment));
430
0
}
431
432
FDK_SACENC_ERROR fdk_sacenc_delay_SetMinimizeDelay(HANDLE_DELAY hDelay,
433
0
                                                   const INT bMinimizeDelay) {
434
0
  return (assignParameterInRange(0, 1, bMinimizeDelay,
435
0
                                 &hDelay->config.bMinimizeDelay));
436
0
}
437
438
0
INT fdk_sacenc_delay_GetOutputAudioBufferDelay(HANDLE_DELAY hDelay) {
439
0
  return (hDelay->nOutputAudioBuffer);
440
0
}
441
442
0
INT fdk_sacenc_delay_GetSurroundAnalysisBufferDelay(HANDLE_DELAY hDelay) {
443
0
  return (hDelay->nSurroundAnalysisBuffer);
444
0
}
445
446
0
INT fdk_sacenc_delay_GetArbDmxAnalysisBufferDelay(HANDLE_DELAY hDelay) {
447
0
  return (hDelay->nArbDmxAnalysisBuffer);
448
0
}
449
450
0
INT fdk_sacenc_delay_GetBitstreamFrameBufferSize(HANDLE_DELAY hDelay) {
451
0
  return (hDelay->nBitstreamFrameBufferSize);
452
0
}
453
454
0
INT fdk_sacenc_delay_GetDmxAlignBufferDelay(HANDLE_DELAY hDelay) {
455
0
  return (hDelay->nDmxAlignBuffer);
456
0
}
457
458
0
INT fdk_sacenc_delay_GetDiscardOutFrames(HANDLE_DELAY hDelay) {
459
0
  return (hDelay->nDiscardOutFrames);
460
0
}
461
462
0
INT fdk_sacenc_delay_GetInfoDmxDelay(HANDLE_DELAY hDelay) {
463
0
  return (hDelay->nInfoDmxDelay);
464
0
}
465
466
0
INT fdk_sacenc_delay_GetInfoCodecDelay(HANDLE_DELAY hDelay) {
467
0
  return (hDelay->nInfoCodecDelay);
468
0
}
469
470
0
INT fdk_sacenc_delay_GetInfoDecoderDelay(HANDLE_DELAY hDelay) {
471
0
  return (hDelay->nInfoDecoderDelay);
472
0
}