Coverage Report

Created: 2025-07-11 06:54

/src/aac/libAACenc/src/bandwidth.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 - 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
/**************************** AAC encoder library ******************************
96
97
   Author(s):   M. Schug / A. Groeschel
98
99
   Description: bandwidth expert
100
101
*******************************************************************************/
102
103
#include "channel_map.h"
104
#include "bandwidth.h"
105
#include "aacEnc_ram.h"
106
107
typedef struct {
108
  INT chanBitRate;
109
  INT bandWidthMono;
110
  INT bandWidth2AndMoreChan;
111
112
} BANDWIDTH_TAB;
113
114
static const BANDWIDTH_TAB bandWidthTable[] = {
115
    {0, 3700, 5000},       {12000, 5000, 6400},   {20000, 6900, 9640},
116
    {28000, 9600, 13050},  {40000, 12060, 14260}, {56000, 13950, 15500},
117
    {72000, 14200, 16120}, {96000, 17000, 17000}, {576001, 17000, 17000}};
118
119
static const BANDWIDTH_TAB bandWidthTable_LD_22050[] = {
120
    {8000, 2000, 2400},    {12000, 2500, 2700},   {16000, 3300, 3100},
121
    {24000, 6250, 7200},   {32000, 9200, 10500},  {40000, 16000, 16000},
122
    {48000, 16000, 16000}, {282241, 16000, 16000}};
123
124
static const BANDWIDTH_TAB bandWidthTable_LD_24000[] = {
125
    {8000, 2000, 2000},    {12000, 2000, 2300},   {16000, 2200, 2500},
126
    {24000, 5650, 7200},   {32000, 11600, 12000}, {40000, 12000, 16000},
127
    {48000, 16000, 16000}, {64000, 16000, 16000}, {307201, 16000, 16000}};
128
129
static const BANDWIDTH_TAB bandWidthTable_LD_32000[] = {
130
    {8000, 2000, 2000},    {12000, 2000, 2000},   {24000, 4250, 7200},
131
    {32000, 8400, 9000},   {40000, 9400, 11300},  {48000, 11900, 14700},
132
    {64000, 14800, 16000}, {76000, 16000, 16000}, {409601, 16000, 16000}};
133
134
static const BANDWIDTH_TAB bandWidthTable_LD_44100[] = {
135
    {8000, 2000, 2000},     {24000, 2000, 2000},   {32000, 4400, 5700},
136
    {40000, 7400, 8800},    {48000, 9000, 10700},  {56000, 11000, 12900},
137
    {64000, 14400, 15500},  {80000, 16000, 16200}, {96000, 16500, 16000},
138
    {128000, 16000, 16000}, {564481, 16000, 16000}};
139
140
static const BANDWIDTH_TAB bandWidthTable_LD_48000[] = {
141
    {8000, 2000, 2000},     {24000, 2000, 2000},   {32000, 4400, 5700},
142
    {40000, 7400, 8800},    {48000, 9000, 10700},  {56000, 11000, 12800},
143
    {64000, 14300, 15400},  {80000, 16000, 16200}, {96000, 16500, 16000},
144
    {128000, 16000, 16000}, {614401, 16000, 16000}};
145
146
typedef struct {
147
  AACENC_BITRATE_MODE bitrateMode;
148
  int bandWidthMono;
149
  int bandWidth2AndMoreChan;
150
} BANDWIDTH_TAB_VBR;
151
152
static const BANDWIDTH_TAB_VBR bandWidthTableVBR[] = {
153
    {AACENC_BR_MODE_CBR, 0, 0},
154
    {AACENC_BR_MODE_VBR_1, 13000, 13000},
155
    {AACENC_BR_MODE_VBR_2, 13000, 13000},
156
    {AACENC_BR_MODE_VBR_3, 15750, 15750},
157
    {AACENC_BR_MODE_VBR_4, 16500, 16500},
158
    {AACENC_BR_MODE_VBR_5, 19293, 19293},
159
    {AACENC_BR_MODE_SFR, 0, 0},
160
    {AACENC_BR_MODE_FF, 0, 0}
161
162
};
163
164
static INT GetBandwidthEntry(const INT frameLength, const INT sampleRate,
165
0
                             const INT chanBitRate, const INT entryNo) {
166
0
  INT bandwidth = -1;
167
0
  const BANDWIDTH_TAB *pBwTab = NULL;
168
0
  INT bwTabSize = 0;
169
170
0
  switch (frameLength) {
171
0
    case 960:
172
0
    case 1024:
173
0
      pBwTab = bandWidthTable;
174
0
      bwTabSize = sizeof(bandWidthTable) / sizeof(BANDWIDTH_TAB);
175
0
      break;
176
0
    case 120:
177
0
    case 128:
178
0
    case 240:
179
0
    case 256:
180
0
    case 480:
181
0
    case 512:
182
0
      switch (sampleRate) {
183
0
        case 8000:
184
0
        case 11025:
185
0
        case 12000:
186
0
        case 16000:
187
0
        case 22050:
188
0
          pBwTab = bandWidthTable_LD_22050;
189
0
          bwTabSize = sizeof(bandWidthTable_LD_22050) / sizeof(BANDWIDTH_TAB);
190
0
          break;
191
0
        case 24000:
192
0
          pBwTab = bandWidthTable_LD_24000;
193
0
          bwTabSize = sizeof(bandWidthTable_LD_24000) / sizeof(BANDWIDTH_TAB);
194
0
          break;
195
0
        case 32000:
196
0
          pBwTab = bandWidthTable_LD_32000;
197
0
          bwTabSize = sizeof(bandWidthTable_LD_32000) / sizeof(BANDWIDTH_TAB);
198
0
          break;
199
0
        case 44100:
200
0
          pBwTab = bandWidthTable_LD_44100;
201
0
          bwTabSize = sizeof(bandWidthTable_LD_44100) / sizeof(BANDWIDTH_TAB);
202
0
          break;
203
0
        case 48000:
204
0
        case 64000:
205
0
        case 88200:
206
0
        case 96000:
207
0
          pBwTab = bandWidthTable_LD_48000;
208
0
          bwTabSize = sizeof(bandWidthTable_LD_48000) / sizeof(BANDWIDTH_TAB);
209
0
          break;
210
0
      }
211
0
      break;
212
0
    default:
213
0
      pBwTab = NULL;
214
0
      bwTabSize = 0;
215
0
  }
216
217
0
  if (pBwTab != NULL) {
218
0
    int i;
219
0
    for (i = 0; i < bwTabSize - 1; i++) {
220
0
      if (chanBitRate >= pBwTab[i].chanBitRate &&
221
0
          chanBitRate < pBwTab[i + 1].chanBitRate) {
222
0
        switch (frameLength) {
223
0
          case 960:
224
0
          case 1024:
225
0
            bandwidth = (entryNo == 0) ? pBwTab[i].bandWidthMono
226
0
                                       : pBwTab[i].bandWidth2AndMoreChan;
227
0
            break;
228
0
          case 120:
229
0
          case 128:
230
0
          case 240:
231
0
          case 256:
232
0
          case 480:
233
0
          case 512: {
234
0
            INT q_res = 0;
235
0
            INT startBw = (entryNo == 0) ? pBwTab[i].bandWidthMono
236
0
                                         : pBwTab[i].bandWidth2AndMoreChan;
237
0
            INT endBw = (entryNo == 0) ? pBwTab[i + 1].bandWidthMono
238
0
                                       : pBwTab[i + 1].bandWidth2AndMoreChan;
239
0
            INT startBr = pBwTab[i].chanBitRate;
240
0
            INT endBr = pBwTab[i + 1].chanBitRate;
241
242
0
            FIXP_DBL bwFac_fix =
243
0
                fDivNorm(chanBitRate - startBr, endBr - startBr, &q_res);
244
0
            bandwidth =
245
0
                (INT)scaleValue(fMult(bwFac_fix, (FIXP_DBL)(endBw - startBw)),
246
0
                                q_res) +
247
0
                startBw;
248
0
          } break;
249
0
          default:
250
0
            bandwidth = -1;
251
0
        }
252
0
        break;
253
0
      } /* within bitrate range */
254
0
    }
255
0
  } /* pBwTab!=NULL */
256
257
0
  return bandwidth;
258
0
}
259
260
AAC_ENCODER_ERROR FDKaacEnc_DetermineBandWidth(
261
    const INT proposedBandWidth, const INT bitrate,
262
    const AACENC_BITRATE_MODE bitrateMode, const INT sampleRate,
263
    const INT frameLength, const CHANNEL_MAPPING *const cm,
264
0
    const CHANNEL_MODE encoderMode, INT *const bandWidth) {
265
0
  AAC_ENCODER_ERROR ErrorStatus = AAC_ENC_OK;
266
0
  INT chanBitRate = bitrate / cm->nChannelsEff;
267
268
0
  switch (bitrateMode) {
269
0
    case AACENC_BR_MODE_VBR_1:
270
0
    case AACENC_BR_MODE_VBR_2:
271
0
    case AACENC_BR_MODE_VBR_3:
272
0
    case AACENC_BR_MODE_VBR_4:
273
0
    case AACENC_BR_MODE_VBR_5:
274
0
      if (proposedBandWidth != 0) {
275
        /* use given bw */
276
0
        *bandWidth = proposedBandWidth;
277
0
      } else {
278
        /* take bw from table */
279
0
        switch (encoderMode) {
280
0
          case MODE_1:
281
0
            *bandWidth = bandWidthTableVBR[bitrateMode].bandWidthMono;
282
0
            break;
283
0
          case MODE_2:
284
0
          case MODE_1_2:
285
0
          case MODE_1_2_1:
286
0
          case MODE_1_2_2:
287
0
          case MODE_1_2_2_1:
288
0
          case MODE_6_1:
289
0
          case MODE_1_2_2_2_1:
290
0
          case MODE_7_1_REAR_SURROUND:
291
0
          case MODE_7_1_FRONT_CENTER:
292
0
          case MODE_7_1_BACK:
293
0
          case MODE_7_1_TOP_FRONT:
294
0
            *bandWidth = bandWidthTableVBR[bitrateMode].bandWidth2AndMoreChan;
295
0
            break;
296
0
          default:
297
0
            return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
298
0
        }
299
0
      }
300
0
      break;
301
0
    case AACENC_BR_MODE_CBR:
302
0
    case AACENC_BR_MODE_SFR:
303
0
    case AACENC_BR_MODE_FF:
304
305
      /* bandwidth limiting */
306
0
      if (proposedBandWidth != 0) {
307
0
        *bandWidth = fMin(proposedBandWidth, fMin(20000, sampleRate >> 1));
308
0
      } else { /* search reasonable bandwidth */
309
310
0
        int entryNo = 0;
311
312
0
        switch (encoderMode) {
313
0
          case MODE_1:   /* mono      */
314
0
            entryNo = 0; /* use mono bandwidth settings */
315
0
            break;
316
317
0
          case MODE_2:       /* stereo    */
318
0
          case MODE_1_2:     /* sce + cpe */
319
0
          case MODE_1_2_1:   /* sce + cpe + sce */
320
0
          case MODE_1_2_2:   /* sce + cpe + cpe */
321
0
          case MODE_1_2_2_1: /* (5.1) sce + cpe + cpe + lfe */
322
0
          case MODE_6_1:
323
0
          case MODE_1_2_2_2_1:
324
0
          case MODE_7_1_REAR_SURROUND:
325
0
          case MODE_7_1_FRONT_CENTER:
326
0
          case MODE_7_1_BACK:
327
0
          case MODE_7_1_TOP_FRONT:
328
0
            entryNo = 1; /* use stereo bandwidth settings */
329
0
            break;
330
331
0
          default:
332
0
            return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
333
0
        }
334
335
0
        *bandWidth =
336
0
            GetBandwidthEntry(frameLength, sampleRate, chanBitRate, entryNo);
337
338
0
        if (*bandWidth == -1) {
339
0
          switch (frameLength) {
340
0
            case 120:
341
0
            case 128:
342
0
            case 240:
343
0
            case 256:
344
0
              *bandWidth = 16000;
345
0
              break;
346
0
            default:
347
0
              ErrorStatus = AAC_ENC_INVALID_CHANNEL_BITRATE;
348
0
          }
349
0
        }
350
0
      }
351
0
      break;
352
0
    default:
353
0
      *bandWidth = 0;
354
0
      return AAC_ENC_UNSUPPORTED_BITRATE_MODE;
355
0
  }
356
357
0
  *bandWidth = fMin(*bandWidth, sampleRate / 2);
358
359
0
  return ErrorStatus;
360
0
}