1
2/* -----------------------------------------------------------------------------------------------------------
3Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5© Copyright  1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
6  All rights reserved.
7
8 1.    INTRODUCTION
9The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16of the MPEG specifications.
17
18Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20individually for the purpose of encoding or decoding bit streams in products that are compliant with
21the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27applications information and documentation.
28
292.    COPYRIGHT LICENSE
30
31Redistribution and use in source and binary forms, with or without modification, are permitted without
32payment of copyright license fees provided that you satisfy the following conditions:
33
34You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35your modifications thereto in source code form.
36
37You must retain the complete text of this software license in the documentation and/or other materials
38provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40modifications thereto to recipients of copies in binary form.
41
42The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43prior written permission.
44
45You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46software or your modifications thereto.
47
48Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49and the date of any change. For modified versions of the FDK AAC Codec, the term
50"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52
533.    NO PATENT LICENSE
54
55NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57respect to this software.
58
59You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60by appropriate patent licenses.
61
624.    DISCLAIMER
63
64This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69or business interruption, however caused and on any theory of liability, whether in contract, strict
70liability, or tort (including negligence), arising in any way out of the use of this software, even if
71advised of the possibility of such damage.
72
735.    CONTACT INFORMATION
74
75Fraunhofer Institute for Integrated Circuits IIS
76Attention: Audio and Multimedia Departments - FDK AAC LL
77Am Wolfsmantel 33
7891058 Erlangen, Germany
79
80www.iis.fraunhofer.de/amm
81amm-info@iis.fraunhofer.de
82----------------------------------------------------------------------------------------------------------- */
83
84/*************************  Fast MPEG AAC Audio Encoder  **********************
85
86   Initial author:       M. Schug / A. Groeschel
87   contents/description: bandwidth expert
88
89******************************************************************************/
90
91#include "channel_map.h"
92#include "bandwidth.h"
93#include "aacEnc_ram.h"
94
95typedef struct{
96  INT chanBitRate;
97  INT bandWidthMono;
98  INT bandWidth2AndMoreChan;
99
100} BANDWIDTH_TAB;
101
102static const BANDWIDTH_TAB bandWidthTable[] = {
103  {0,      3700,  5000},
104  {12000,  5000,  6400},
105  {20000,  6900,  9640},
106  {28000,  9600, 13050},
107  {40000, 12060, 14260},
108  {56000, 13950, 15500},
109  {72000, 14200, 16120},
110  {96000, 17000, 17000},
111  {576001,17000, 17000}
112};
113
114
115static const BANDWIDTH_TAB bandWidthTable_LD_22050[] = {
116  { 8000,  2000,  2400},
117  {12000,  2500,  2700},
118  {16000,  3300,  3100},
119  {24000,  6250,  7200},
120  {32000,  9200,  10500},
121  {40000,  16000, 16000},
122  {48000,  16000, 16000},
123  {360001, 16000, 16000}
124};
125
126static const BANDWIDTH_TAB bandWidthTable_LD_24000[] = {
127  { 8000,  2000,  2000},
128  {12000,  2000,  2300},
129  {16000,  2200,  2500},
130  {24000,  5650,  6400},
131  {32000,  11600, 12000},
132  {40000,  12000, 16000},
133  {48000,  16000, 16000},
134  {64000,  16000, 16000},
135  {360001, 16000, 16000}
136};
137
138static const BANDWIDTH_TAB bandWidthTable_LD_32000[] = {
139  { 8000,  2000,  2000},
140  {12000,  2000,  2000},
141  {24000,  4250,  5200},
142  {32000,  8400,  9000},
143  {40000,  9400,  11300},
144  {48000,  11900, 13700},
145  {64000,  14800, 16000},
146  {76000,  16000, 16000},
147  {360001, 16000, 16000}
148};
149
150static const BANDWIDTH_TAB bandWidthTable_LD_44100[] = {
151  { 8000,  2000,  2000},
152  {24000,  2000,  2000},
153  {32000,  4400,  5700},
154  {40000,  7400,  8800},
155  {48000,  9000,  10700},
156  {56000,  11000, 12900},
157  {64000,  14400, 15500},
158  {80000,  16000, 16200},
159  {96000,  16500, 16000},
160  {128000, 16000, 16000},
161  {360001, 16000, 16000}
162};
163
164static const BANDWIDTH_TAB bandWidthTable_LD_48000[] = {
165  { 8000,  2000,  2000},
166  {24000,  2000,  2000},
167  {32000,  4400,  5700},
168  {40000,  7400,  8800},
169  {48000,  9000,  10700},
170  {56000,  11000, 12800},
171  {64000,  14300, 15400},
172  {80000,  16000, 16200},
173  {96000,  16500, 16000},
174  {128000, 16000, 16000},
175  {360001, 16000, 16000}
176};
177
178typedef struct{
179  AACENC_BITRATE_MODE bitrateMode;
180  int bandWidthMono;
181  int bandWidth2AndMoreChan;
182} BANDWIDTH_TAB_VBR;
183
184static const BANDWIDTH_TAB_VBR bandWidthTableVBR[]= {
185  {AACENC_BR_MODE_CBR,        0,     0},
186  {AACENC_BR_MODE_VBR_1,  13050, 13050},
187  {AACENC_BR_MODE_VBR_2,  13050, 13050},
188  {AACENC_BR_MODE_VBR_3,  14260, 14260},
189  {AACENC_BR_MODE_VBR_4,  15500, 15500},
190  {AACENC_BR_MODE_VBR_5,  48000, 48000},
191  {AACENC_BR_MODE_SFR,        0,     0},
192  {AACENC_BR_MODE_FF,         0,     0}
193
194};
195
196static INT GetBandwidthEntry(
197            const INT frameLength,
198            const INT sampleRate,
199            const INT chanBitRate,
200            const INT entryNo)
201{
202  INT bandwidth = -1;
203  const BANDWIDTH_TAB *pBwTab = NULL;
204  INT bwTabSize = 0;
205
206  switch (frameLength) {
207    case 960:
208    case 1024:
209      pBwTab = bandWidthTable;
210      bwTabSize = sizeof(bandWidthTable)/sizeof(BANDWIDTH_TAB);
211      break;
212    case 480:
213    case 512:
214      switch (sampleRate) {
215        case 8000:
216        case 11025:
217        case 12000:
218        case 16000:
219        case 22050:
220          pBwTab = bandWidthTable_LD_22050;
221          bwTabSize = sizeof(bandWidthTable_LD_22050)/sizeof(BANDWIDTH_TAB);
222          break;
223        case 24000:
224          pBwTab = bandWidthTable_LD_24000;
225          bwTabSize = sizeof(bandWidthTable_LD_24000)/sizeof(BANDWIDTH_TAB);
226          break;
227        case 32000:
228          pBwTab = bandWidthTable_LD_32000;
229          bwTabSize = sizeof(bandWidthTable_LD_32000)/sizeof(BANDWIDTH_TAB);
230          break;
231        case (44100):
232          pBwTab = bandWidthTable_LD_44100;
233          bwTabSize = sizeof(bandWidthTable_LD_44100)/sizeof(BANDWIDTH_TAB);
234          break;
235        case 48000:
236        case 64000:
237        case 88200:
238        case 96000:
239          pBwTab = bandWidthTable_LD_48000;
240          bwTabSize = sizeof(bandWidthTable_LD_48000)/sizeof(BANDWIDTH_TAB);
241          break;
242      }
243      break;
244    default:
245      pBwTab = NULL;
246      bwTabSize = 0;
247  }
248
249  if (pBwTab!=NULL) {
250    int i;
251    for (i=0; i<bwTabSize-1; i++) {
252      if (chanBitRate >= pBwTab[i].chanBitRate &&
253          chanBitRate < pBwTab[i+1].chanBitRate)
254      {
255        switch (frameLength) {
256          case 960:
257          case 1024:
258            bandwidth = (entryNo==0)
259              ? pBwTab[i].bandWidthMono
260              : pBwTab[i].bandWidth2AndMoreChan;
261            break;
262          case 480:
263          case 512:
264            {
265              INT q_res = 0;
266              INT startBw = (entryNo==0) ?  pBwTab[i  ].bandWidthMono : pBwTab[i  ].bandWidth2AndMoreChan;
267              INT endBw =   (entryNo==0) ?  pBwTab[i+1].bandWidthMono : pBwTab[i+1].bandWidth2AndMoreChan;
268              INT startBr = pBwTab[i].chanBitRate;
269              INT endBr = pBwTab[i+1].chanBitRate;
270
271              FIXP_DBL bwFac_fix = fDivNorm(chanBitRate-startBr, endBr-startBr, &q_res);
272              bandwidth = (INT)scaleValue(fMult(bwFac_fix, (FIXP_DBL)(endBw-startBw)),q_res) + startBw;
273            }
274            break;
275          default:
276            bandwidth = -1;
277        }
278        break;
279      } /* within bitrate range */
280    }
281  } /* pBwTab!=NULL */
282
283  return bandwidth;
284}
285
286
287AAC_ENCODER_ERROR FDKaacEnc_DetermineBandWidth(INT* bandWidth,
288                                               INT proposedBandWidth,
289                                               INT bitrate,
290                                               AACENC_BITRATE_MODE bitrateMode,
291                                               INT sampleRate,
292                                               INT frameLength,
293                                               CHANNEL_MAPPING* cm,
294                                               CHANNEL_MODE encoderMode)
295{
296  AAC_ENCODER_ERROR ErrorStatus = AAC_ENC_OK;
297  INT chanBitRate = bitrate/cm->nChannels;
298
299  /* vbr */
300  switch(bitrateMode){
301  case AACENC_BR_MODE_VBR_1:
302  case AACENC_BR_MODE_VBR_2:
303  case AACENC_BR_MODE_VBR_3:
304  case AACENC_BR_MODE_VBR_4:
305  case AACENC_BR_MODE_VBR_5:
306    if (proposedBandWidth != 0){
307      /* use given bw */
308      *bandWidth = proposedBandWidth;
309    } else {
310      /* take bw from table */
311      switch(encoderMode){
312      case MODE_1:
313        *bandWidth = bandWidthTableVBR[bitrateMode].bandWidthMono;
314        break;
315      case MODE_2:
316      case MODE_1_2:
317      case MODE_1_2_1:
318      case MODE_1_2_2:
319      case MODE_1_2_2_1:
320      case MODE_1_2_2_2_1:
321        *bandWidth = bandWidthTableVBR[bitrateMode].bandWidth2AndMoreChan;
322        break;
323      default:
324        return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
325      }
326    }
327    break;
328  case AACENC_BR_MODE_CBR:
329  case AACENC_BR_MODE_SFR:
330  case AACENC_BR_MODE_FF:
331
332    /* bandwidth limiting */
333    if (proposedBandWidth != 0) {
334      *bandWidth = FDKmin(proposedBandWidth, FDKmin(20000, sampleRate>>1));
335    }
336    else { /* search reasonable bandwidth */
337
338      int entryNo = 0;
339
340      switch(encoderMode){
341      case MODE_1:        /* mono      */
342        entryNo = 0;      /* use mono bandwith settings */
343        break;
344
345      case MODE_2:        /* stereo    */
346      case MODE_1_2:      /* sce + cpe */
347      case MODE_1_2_1:    /* sce + cpe + sce */
348      case MODE_1_2_2:    /* sce + cpe + cpe */
349      case MODE_1_2_2_1:  /* (5.1) sce + cpe + cpe + lfe */
350      case MODE_1_2_2_2_1: /* (7.1) sce + cpe + cpe + cpe + lfe */
351        entryNo = 1;      /* use stereo bandwith settings */
352        break;
353
354      default:
355        return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
356      }
357
358      *bandWidth = GetBandwidthEntry(
359            frameLength,
360            sampleRate,
361            chanBitRate,
362            entryNo);
363
364      if (*bandWidth==-1) {
365        ErrorStatus = AAC_ENC_INVALID_CHANNEL_BITRATE;
366      }
367    }
368    break;
369  default:
370    *bandWidth = 0;
371    return AAC_ENC_UNSUPPORTED_BITRATE_MODE;
372  }
373
374  *bandWidth = FDKmin(*bandWidth, sampleRate/2);
375
376  return ErrorStatus;;
377}
378