xref: /aosp_15_r20/external/aac/libAACenc/src/bandwidth.cpp (revision e54365361535b070c2db7374cec45c159c7d0e7a)
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  
GetBandwidthEntry(const INT frameLength,const INT sampleRate,const INT chanBitRate,const INT entryNo)164  static INT GetBandwidthEntry(const INT frameLength, const INT sampleRate,
165                               const INT chanBitRate, const INT entryNo) {
166    INT bandwidth = -1;
167    const BANDWIDTH_TAB *pBwTab = NULL;
168    INT bwTabSize = 0;
169  
170    switch (frameLength) {
171      case 960:
172      case 1024:
173        pBwTab = bandWidthTable;
174        bwTabSize = sizeof(bandWidthTable) / sizeof(BANDWIDTH_TAB);
175        break;
176      case 120:
177      case 128:
178      case 240:
179      case 256:
180      case 480:
181      case 512:
182        switch (sampleRate) {
183          case 8000:
184          case 11025:
185          case 12000:
186          case 16000:
187          case 22050:
188            pBwTab = bandWidthTable_LD_22050;
189            bwTabSize = sizeof(bandWidthTable_LD_22050) / sizeof(BANDWIDTH_TAB);
190            break;
191          case 24000:
192            pBwTab = bandWidthTable_LD_24000;
193            bwTabSize = sizeof(bandWidthTable_LD_24000) / sizeof(BANDWIDTH_TAB);
194            break;
195          case 32000:
196            pBwTab = bandWidthTable_LD_32000;
197            bwTabSize = sizeof(bandWidthTable_LD_32000) / sizeof(BANDWIDTH_TAB);
198            break;
199          case 44100:
200            pBwTab = bandWidthTable_LD_44100;
201            bwTabSize = sizeof(bandWidthTable_LD_44100) / sizeof(BANDWIDTH_TAB);
202            break;
203          case 48000:
204          case 64000:
205          case 88200:
206          case 96000:
207            pBwTab = bandWidthTable_LD_48000;
208            bwTabSize = sizeof(bandWidthTable_LD_48000) / sizeof(BANDWIDTH_TAB);
209            break;
210        }
211        break;
212      default:
213        pBwTab = NULL;
214        bwTabSize = 0;
215    }
216  
217    if (pBwTab != NULL) {
218      int i;
219      for (i = 0; i < bwTabSize - 1; i++) {
220        if (chanBitRate >= pBwTab[i].chanBitRate &&
221            chanBitRate < pBwTab[i + 1].chanBitRate) {
222          switch (frameLength) {
223            case 960:
224            case 1024:
225              bandwidth = (entryNo == 0) ? pBwTab[i].bandWidthMono
226                                         : pBwTab[i].bandWidth2AndMoreChan;
227              break;
228            case 120:
229            case 128:
230            case 240:
231            case 256:
232            case 480:
233            case 512: {
234              INT q_res = 0;
235              INT startBw = (entryNo == 0) ? pBwTab[i].bandWidthMono
236                                           : pBwTab[i].bandWidth2AndMoreChan;
237              INT endBw = (entryNo == 0) ? pBwTab[i + 1].bandWidthMono
238                                         : pBwTab[i + 1].bandWidth2AndMoreChan;
239              INT startBr = pBwTab[i].chanBitRate;
240              INT endBr = pBwTab[i + 1].chanBitRate;
241  
242              FIXP_DBL bwFac_fix =
243                  fDivNorm(chanBitRate - startBr, endBr - startBr, &q_res);
244              bandwidth =
245                  (INT)scaleValue(fMult(bwFac_fix, (FIXP_DBL)(endBw - startBw)),
246                                  q_res) +
247                  startBw;
248            } break;
249            default:
250              bandwidth = -1;
251          }
252          break;
253        } /* within bitrate range */
254      }
255    } /* pBwTab!=NULL */
256  
257    return bandwidth;
258  }
259  
FDKaacEnc_DetermineBandWidth(const INT proposedBandWidth,const INT bitrate,const AACENC_BITRATE_MODE bitrateMode,const INT sampleRate,const INT frameLength,const CHANNEL_MAPPING * const cm,const CHANNEL_MODE encoderMode,INT * const bandWidth)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      const CHANNEL_MODE encoderMode, INT *const bandWidth) {
265    AAC_ENCODER_ERROR ErrorStatus = AAC_ENC_OK;
266    INT chanBitRate = bitrate / cm->nChannelsEff;
267  
268    switch (bitrateMode) {
269      case AACENC_BR_MODE_VBR_1:
270      case AACENC_BR_MODE_VBR_2:
271      case AACENC_BR_MODE_VBR_3:
272      case AACENC_BR_MODE_VBR_4:
273      case AACENC_BR_MODE_VBR_5:
274        if (proposedBandWidth != 0) {
275          /* use given bw */
276          *bandWidth = proposedBandWidth;
277        } else {
278          /* take bw from table */
279          switch (encoderMode) {
280            case MODE_1:
281              *bandWidth = bandWidthTableVBR[bitrateMode].bandWidthMono;
282              break;
283            case MODE_2:
284            case MODE_1_2:
285            case MODE_1_2_1:
286            case MODE_1_2_2:
287            case MODE_1_2_2_1:
288            case MODE_6_1:
289            case MODE_1_2_2_2_1:
290            case MODE_7_1_REAR_SURROUND:
291            case MODE_7_1_FRONT_CENTER:
292            case MODE_7_1_BACK:
293            case MODE_7_1_TOP_FRONT:
294              *bandWidth = bandWidthTableVBR[bitrateMode].bandWidth2AndMoreChan;
295              break;
296            default:
297              return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
298          }
299        }
300        break;
301      case AACENC_BR_MODE_CBR:
302      case AACENC_BR_MODE_SFR:
303      case AACENC_BR_MODE_FF:
304  
305        /* bandwidth limiting */
306        if (proposedBandWidth != 0) {
307          *bandWidth = fMin(proposedBandWidth, fMin(20000, sampleRate >> 1));
308        } else { /* search reasonable bandwidth */
309  
310          int entryNo = 0;
311  
312          switch (encoderMode) {
313            case MODE_1:   /* mono      */
314              entryNo = 0; /* use mono bandwidth settings */
315              break;
316  
317            case MODE_2:       /* stereo    */
318            case MODE_1_2:     /* sce + cpe */
319            case MODE_1_2_1:   /* sce + cpe + sce */
320            case MODE_1_2_2:   /* sce + cpe + cpe */
321            case MODE_1_2_2_1: /* (5.1) sce + cpe + cpe + lfe */
322            case MODE_6_1:
323            case MODE_1_2_2_2_1:
324            case MODE_7_1_REAR_SURROUND:
325            case MODE_7_1_FRONT_CENTER:
326            case MODE_7_1_BACK:
327            case MODE_7_1_TOP_FRONT:
328              entryNo = 1; /* use stereo bandwidth settings */
329              break;
330  
331            default:
332              return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
333          }
334  
335          *bandWidth =
336              GetBandwidthEntry(frameLength, sampleRate, chanBitRate, entryNo);
337  
338          if (*bandWidth == -1) {
339            switch (frameLength) {
340              case 120:
341              case 128:
342              case 240:
343              case 256:
344                *bandWidth = 16000;
345                break;
346              default:
347                ErrorStatus = AAC_ENC_INVALID_CHANNEL_BITRATE;
348            }
349          }
350        }
351        break;
352      default:
353        *bandWidth = 0;
354        return AAC_ENC_UNSUPPORTED_BITRATE_MODE;
355    }
356  
357    *bandWidth = fMin(*bandWidth, sampleRate / 2);
358  
359    return ErrorStatus;
360  }
361