1/* -----------------------------------------------------------------------------
2Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4© Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5Forschung e.V. All rights reserved.
6
7 1.    INTRODUCTION
8The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10scheme for digital audio. This FDK AAC Codec software is intended to be used on
11a wide variety of Android devices.
12
13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14general perceptual audio codecs. AAC-ELD is considered the best-performing
15full-bandwidth communications codec by independent studies and is widely
16deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17specifications.
18
19Patent licenses for necessary patent claims for the FDK AAC Codec (including
20those of Fraunhofer) may be obtained through Via Licensing
21(www.vialicensing.com) or through the respective patent owners individually for
22the purpose of encoding or decoding bit streams in products that are compliant
23with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24Android devices already license these patent claims through Via Licensing or
25directly from the patent owners, and therefore FDK AAC Codec software may
26already be covered under those patent licenses when it is used for those
27licensed purposes only.
28
29Commercially-licensed AAC software libraries, including floating-point versions
30with enhanced sound quality, are also available from Fraunhofer. Users are
31encouraged to check the Fraunhofer website for additional applications
32information and documentation.
33
342.    COPYRIGHT LICENSE
35
36Redistribution and use in source and binary forms, with or without modification,
37are permitted without payment of copyright license fees provided that you
38satisfy the following conditions:
39
40You must retain the complete text of this software license in redistributions of
41the FDK AAC Codec or your modifications thereto in source code form.
42
43You must retain the complete text of this software license in the documentation
44and/or other materials provided with redistributions of the FDK AAC Codec or
45your modifications thereto in binary form. You must make available free of
46charge copies of the complete source code of the FDK AAC Codec and your
47modifications thereto to recipients of copies in binary form.
48
49The name of Fraunhofer may not be used to endorse or promote products derived
50from this library without prior written permission.
51
52You may not charge copyright license fees for anyone to use, copy or distribute
53the FDK AAC Codec software or your modifications thereto.
54
55Your modified versions of the FDK AAC Codec must carry prominent notices stating
56that you changed the software and the date of any change. For modified versions
57of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59AAC Codec Library for Android."
60
613.    NO PATENT LICENSE
62
63NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65Fraunhofer provides no warranty of patent non-infringement with respect to this
66software.
67
68You may use this FDK AAC Codec software or modifications thereto only for
69purposes that are authorized by appropriate patent licenses.
70
714.    DISCLAIMER
72
73This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75including but not limited to the implied warranties of merchantability and
76fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78or consequential damages, including but not limited to procurement of substitute
79goods or services; loss of use, data, or profits, or business interruption,
80however caused and on any theory of liability, whether in contract, strict
81liability, or tort (including negligence), arising in any way out of the use of
82this software, even if advised of the possibility of such damage.
83
845.    CONTACT INFORMATION
85
86Fraunhofer Institute for Integrated Circuits IIS
87Attention: Audio and Multimedia Departments - FDK AAC LL
88Am Wolfsmantel 33
8991058 Erlangen, Germany
90
91www.iis.fraunhofer.de/amm
92amm-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
107typedef struct {
108  INT chanBitRate;
109  INT bandWidthMono;
110  INT bandWidth2AndMoreChan;
111
112} BANDWIDTH_TAB;
113
114static 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
119static 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
124static 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
129static 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
134static 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
140static 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
146typedef struct {
147  AACENC_BITRATE_MODE bitrateMode;
148  int bandWidthMono;
149  int bandWidth2AndMoreChan;
150} BANDWIDTH_TAB_VBR;
151
152static const BANDWIDTH_TAB_VBR bandWidthTableVBR[] = {
153    {AACENC_BR_MODE_CBR, 0, 0},
154    {AACENC_BR_MODE_VBR_1, 13050, 13050},
155    {AACENC_BR_MODE_VBR_2, 13050, 13050},
156    {AACENC_BR_MODE_VBR_3, 14260, 14260},
157    {AACENC_BR_MODE_VBR_4, 15500, 15500},
158    {AACENC_BR_MODE_VBR_5, 48000, 48000},
159    {AACENC_BR_MODE_SFR, 0, 0},
160    {AACENC_BR_MODE_FF, 0, 0}
161
162};
163
164static 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
260AAC_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