1 
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4 
5 � Copyright  1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6   All rights reserved.
7 
8  1.    INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17 
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24 
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28 
29 2.    COPYRIGHT LICENSE
30 
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33 
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36 
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41 
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44 
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47 
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and 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 
53 3.    NO PATENT LICENSE
54 
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58 
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61 
62 4.    DISCLAIMER
63 
64 This 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
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72 
73 5.    CONTACT INFORMATION
74 
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79 
80 www.iis.fraunhofer.de/amm
81 amm-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 
95 typedef struct{
96   INT chanBitRate;
97   INT bandWidthMono;
98   INT bandWidth2AndMoreChan;
99 
100 } BANDWIDTH_TAB;
101 
102 static 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 
115 static 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 
126 static 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 
138 static 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 
150 static 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 
164 static 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 
178 typedef struct{
179   AACENC_BITRATE_MODE bitrateMode;
180   int bandWidthMono;
181   int bandWidth2AndMoreChan;
182 } BANDWIDTH_TAB_VBR;
183 
184 static 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 
GetBandwidthEntry(const INT frameLength,const INT sampleRate,const INT chanBitRate,const INT entryNo)196 static 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 1024:
208       pBwTab = bandWidthTable;
209       bwTabSize = sizeof(bandWidthTable)/sizeof(BANDWIDTH_TAB);
210       break;
211     case 480:
212     case 512:
213       switch (sampleRate) {
214         case 8000:
215         case 11025:
216         case 12000:
217         case 16000:
218         case 22050:
219           pBwTab = bandWidthTable_LD_22050;
220           bwTabSize = sizeof(bandWidthTable_LD_22050)/sizeof(BANDWIDTH_TAB);
221           break;
222         case 24000:
223           pBwTab = bandWidthTable_LD_24000;
224           bwTabSize = sizeof(bandWidthTable_LD_24000)/sizeof(BANDWIDTH_TAB);
225           break;
226         case 32000:
227           pBwTab = bandWidthTable_LD_32000;
228           bwTabSize = sizeof(bandWidthTable_LD_32000)/sizeof(BANDWIDTH_TAB);
229           break;
230         case (44100):
231           pBwTab = bandWidthTable_LD_44100;
232           bwTabSize = sizeof(bandWidthTable_LD_44100)/sizeof(BANDWIDTH_TAB);
233           break;
234         case 48000:
235         case 64000:
236         case 88200:
237         case 96000:
238           pBwTab = bandWidthTable_LD_48000;
239           bwTabSize = sizeof(bandWidthTable_LD_48000)/sizeof(BANDWIDTH_TAB);
240           break;
241       }
242       break;
243     default:
244       pBwTab = NULL;
245       bwTabSize = 0;
246   }
247 
248   if (pBwTab!=NULL) {
249     int i;
250     for (i=0; i<bwTabSize-1; i++) {
251       if (chanBitRate >= pBwTab[i].chanBitRate &&
252           chanBitRate < pBwTab[i+1].chanBitRate)
253       {
254         switch (frameLength) {
255           case 1024:
256             bandwidth = (entryNo==0)
257               ? pBwTab[i].bandWidthMono
258               : pBwTab[i].bandWidth2AndMoreChan;
259             break;
260           case 480:
261           case 512:
262             {
263               INT q_res = 0;
264               INT startBw = (entryNo==0) ?  pBwTab[i  ].bandWidthMono : pBwTab[i  ].bandWidth2AndMoreChan;
265               INT endBw =   (entryNo==0) ?  pBwTab[i+1].bandWidthMono : pBwTab[i+1].bandWidth2AndMoreChan;
266               INT startBr = pBwTab[i].chanBitRate;
267               INT endBr = pBwTab[i+1].chanBitRate;
268 
269               FIXP_DBL bwFac_fix = fDivNorm(chanBitRate-startBr, endBr-startBr, &q_res);
270               bandwidth = (INT)scaleValue(fMult(bwFac_fix, (FIXP_DBL)(endBw-startBw)),q_res) + startBw;
271             }
272             break;
273           default:
274             bandwidth = -1;
275         }
276         break;
277       } /* within bitrate range */
278     }
279   } /* pBwTab!=NULL */
280 
281   return bandwidth;
282 }
283 
284 
FDKaacEnc_DetermineBandWidth(INT * bandWidth,INT proposedBandWidth,INT bitrate,AACENC_BITRATE_MODE bitrateMode,INT sampleRate,INT frameLength,CHANNEL_MAPPING * cm,CHANNEL_MODE encoderMode)285 AAC_ENCODER_ERROR FDKaacEnc_DetermineBandWidth(INT* bandWidth,
286                                                INT proposedBandWidth,
287                                                INT bitrate,
288                                                AACENC_BITRATE_MODE bitrateMode,
289                                                INT sampleRate,
290                                                INT frameLength,
291                                                CHANNEL_MAPPING* cm,
292                                                CHANNEL_MODE encoderMode)
293 {
294   AAC_ENCODER_ERROR ErrorStatus = AAC_ENC_OK;
295   INT chanBitRate = bitrate/cm->nChannels;
296 
297   /* vbr */
298   switch(bitrateMode){
299   case AACENC_BR_MODE_VBR_1:
300   case AACENC_BR_MODE_VBR_2:
301   case AACENC_BR_MODE_VBR_3:
302   case AACENC_BR_MODE_VBR_4:
303   case AACENC_BR_MODE_VBR_5:
304     if (proposedBandWidth != 0){
305       /* use given bw */
306       *bandWidth = proposedBandWidth;
307     } else {
308       /* take bw from table */
309       switch(encoderMode){
310       case MODE_1:
311         *bandWidth = bandWidthTableVBR[bitrateMode].bandWidthMono;
312         break;
313       case MODE_2:
314       case MODE_1_2:
315       case MODE_1_2_1:
316       case MODE_1_2_2:
317       case MODE_1_2_2_1:
318       case MODE_1_2_2_2_1:
319       case MODE_7_1_REAR_SURROUND:
320       case MODE_7_1_FRONT_CENTER:
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       case MODE_7_1_REAR_SURROUND:
352       case MODE_7_1_FRONT_CENTER:
353         entryNo = 1;      /* use stereo bandwith settings */
354         break;
355 
356       default:
357         return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
358       }
359 
360       *bandWidth = GetBandwidthEntry(
361             frameLength,
362             sampleRate,
363             chanBitRate,
364             entryNo);
365 
366       if (*bandWidth==-1) {
367         ErrorStatus = AAC_ENC_INVALID_CHANNEL_BITRATE;
368       }
369     }
370     break;
371   default:
372     *bandWidth = 0;
373     return AAC_ENC_UNSUPPORTED_BITRATE_MODE;
374   }
375 
376   *bandWidth = FDKmin(*bandWidth, sampleRate/2);
377 
378   return ErrorStatus;;
379 }
380