1
2/* -----------------------------------------------------------------------------------------------------------
3Software 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
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/***************************  Fraunhofer IIS FDK Tools  **********************
85
86   Author(s):   Josef Hoepfl, Manuel Jander
87   Description: MDCT routines
88
89******************************************************************************/
90
91#include "mdct.h"
92
93
94#include "FDK_tools_rom.h"
95#include "dct.h"
96#include "fixpoint_math.h"
97
98
99void mdct_init( H_MDCT hMdct,
100                FIXP_DBL *overlap,
101                INT overlapBufferSize )
102{
103  hMdct->overlap.freq = overlap;
104  //FDKmemclear(overlap, overlapBufferSize*sizeof(FIXP_DBL));
105  hMdct->prev_fr = 0;
106  hMdct->prev_nr = 0;
107  hMdct->prev_tl = 0;
108  hMdct->ov_size = overlapBufferSize;
109}
110
111
112void imdct_gain(FIXP_DBL *pGain_m, int *pGain_e, int tl)
113{
114  FIXP_DBL gain_m = *pGain_m;
115  int gain_e = *pGain_e;
116  int log2_tl;
117
118  log2_tl = DFRACT_BITS-1-fNormz((FIXP_DBL)tl);
119
120  gain_e += -MDCT_OUTPUT_GAIN - log2_tl - MDCT_OUT_HEADROOM + 1;
121
122  /* Detect non-radix 2 transform length and add amplitude compensation factor
123     which cannot be included into the exponent above */
124  switch ( (tl) >> (log2_tl - 2) ) {
125    case 0x7: /* 10 ms, 1/tl = 1.0/(FDKpow(2.0, -log2_tl) * 0.53333333333333333333) */
126      if (gain_m == (FIXP_DBL)0) {
127        gain_m = FL2FXCONST_DBL(0.53333333333333333333f);
128      } else {
129        gain_m = fMult(gain_m, FL2FXCONST_DBL(0.53333333333333333333f));
130      }
131      break;
132    case 0x6: /* 3/4 of radix 2, 1/tl = 1.0/(FDKpow(2.0, -log2_tl) * 2.0/3.0) */
133      if (gain_m == (FIXP_DBL)0) {
134        gain_m = FL2FXCONST_DBL(2.0/3.0f);
135      } else {
136        gain_m = fMult(gain_m, FL2FXCONST_DBL(2.0/3.0f));
137      }
138      break;
139    case 0x4:
140      /* radix 2, nothing to do. */
141      break;
142    default:
143      /* unsupported */
144      FDK_ASSERT(0);
145      break;
146  }
147
148  *pGain_m = gain_m;
149  *pGain_e = gain_e;
150}
151
152INT imdct_drain(
153        H_MDCT hMdct,
154        FIXP_DBL *output,
155        INT nrSamplesRoom
156        )
157{
158  int buffered_samples = 0;
159
160  if (nrSamplesRoom > 0) {
161    buffered_samples = hMdct->ov_offset;
162
163    FDK_ASSERT(buffered_samples <= nrSamplesRoom);
164
165    if (buffered_samples > 0)  {
166      FDKmemcpy(output, hMdct->overlap.time, buffered_samples*sizeof(FIXP_DBL));
167      hMdct->ov_offset = 0;
168    }
169  }
170  return buffered_samples;
171}
172
173INT imdct_copy_ov_and_nr(
174        H_MDCT hMdct,
175        FIXP_DBL * pTimeData,
176        INT nrSamples
177        )
178{
179  FIXP_DBL *pOvl;
180  int nt, nf, i;
181
182  nt = fMin(hMdct->ov_offset, nrSamples);
183  nrSamples -= nt;
184  nf = fMin(hMdct->prev_nr, nrSamples);
185  nrSamples -= nf;
186  FDKmemcpy(pTimeData, hMdct->overlap.time, nt*sizeof(FIXP_DBL));
187  pTimeData += nt;
188
189  pOvl = hMdct->overlap.freq + hMdct->ov_size - 1;
190  for (i=0; i<nf; i++) {
191    FIXP_DBL x = - (*pOvl--);
192    *pTimeData = IMDCT_SCALE_DBL(x);
193    pTimeData ++;
194  }
195
196  return (nt+nf);
197}
198
199void imdct_adapt_parameters(H_MDCT hMdct, int *pfl, int *pnl, int tl, const FIXP_WTP *wls, int noOutSamples)
200{
201  int fl = *pfl, nl = *pnl;
202  int window_diff, use_current = 0, use_previous = 0;
203  if (hMdct->prev_tl == 0) {
204    hMdct->prev_wrs    = wls;
205    hMdct->prev_fr     = fl;
206    hMdct->prev_nr     = (noOutSamples-fl)>>1;
207    hMdct->prev_tl     = noOutSamples;
208    hMdct->ov_offset   = 0;
209    use_current = 1;
210  }
211
212  window_diff = (hMdct->prev_fr - fl)>>1;
213
214  /* check if the previous window slope can be adjusted to match the current window slope */
215  if (hMdct->prev_nr + window_diff > 0) {
216    use_current = 1;
217  }
218  /* check if the current window slope can be adjusted to match the previous window slope */
219  if (nl - window_diff > 0 ) {
220    use_previous = 1;
221  }
222
223  /* if both is possible choose the larger of both window slope lengths */
224  if (use_current && use_previous) {
225    if (fl < hMdct->prev_fr) {
226      use_current = 0;
227    } else {
228      use_previous = 0;
229    }
230  }
231  /*
232   * If the previous transform block is big enough, enlarge previous window overlap,
233   * if not, then shrink current window overlap.
234   */
235  if (use_current) {
236    hMdct->prev_nr += window_diff;
237    hMdct->prev_fr = fl;
238    hMdct->prev_wrs = wls;
239  } else {
240    nl -= window_diff;
241    fl = hMdct->prev_fr;
242  }
243
244  *pfl = fl;
245  *pnl = nl;
246}
247
248INT  imdct_block(
249        H_MDCT hMdct,
250        FIXP_DBL *output,
251        FIXP_DBL *spectrum,
252        const SHORT scalefactor[],
253        const INT nSpec,
254        const INT noOutSamples,
255        const INT tl,
256        const FIXP_WTP *wls,
257        INT fl,
258        const FIXP_WTP *wrs,
259        const INT fr,
260        FIXP_DBL gain
261        )
262{
263  FIXP_DBL *pOvl;
264  FIXP_DBL *pOut0 = output, *pOut1;
265  INT nl, nr;
266  int w, i, nrSamples = 0, specShiftScale, transform_gain_e = 0;
267
268  /* Derive NR and NL */
269  nr = (tl - fr)>>1;
270  nl = (tl - fl)>>1;
271
272  /* Include 2/N IMDCT gain into gain factor and exponent. */
273  imdct_gain(&gain, &transform_gain_e, tl);
274
275  /* Detect FRprevious / FL mismatches and override parameters accordingly */
276  if (hMdct->prev_fr != fl) {
277    imdct_adapt_parameters(hMdct, &fl, &nl, tl, wls, noOutSamples);
278  }
279
280  pOvl = hMdct->overlap.freq + hMdct->ov_size - 1;
281
282  if ( noOutSamples > nrSamples ) {
283    /* Purge buffered output. */
284    for (i=0; i<hMdct->ov_offset; i++) {
285      *pOut0 = hMdct->overlap.time[i];
286      pOut0 ++;
287    }
288    nrSamples = hMdct->ov_offset;
289    hMdct->ov_offset = 0;
290  }
291
292  for (w=0; w<nSpec; w++)
293  {
294    FIXP_DBL *pSpec, *pCurr;
295    const FIXP_WTP *pWindow;
296
297    specShiftScale = transform_gain_e;
298
299    /* Setup window pointers */
300    pWindow = hMdct->prev_wrs;
301
302    /* Current spectrum */
303    pSpec = spectrum+w*tl;
304
305    /* DCT IV of current spectrum. */
306    dct_IV(pSpec, tl, &specShiftScale);
307
308    /* Optional scaling of time domain - no yet windowed - of current spectrum */
309    /* and de-scale current spectrum signal (time domain, no yet windowed) */
310    if (gain != (FIXP_DBL)0) {
311      scaleValuesWithFactor(pSpec, gain, tl, scalefactor[w] + specShiftScale);
312    } else {
313      scaleValues(pSpec, tl, scalefactor[w] + specShiftScale);
314    }
315
316    if ( noOutSamples <= nrSamples ) {
317      /* Divert output first half to overlap buffer if we already got enough output samples. */
318      pOut0 = hMdct->overlap.time + hMdct->ov_offset;
319      hMdct->ov_offset += hMdct->prev_nr + fl/2;
320    } else {
321      /* Account output samples */
322      nrSamples += hMdct->prev_nr + fl/2;
323    }
324
325    /* NR output samples 0 .. NR. -overlap[TL/2..TL/2-NR] */
326    for (i=0; i<hMdct->prev_nr; i++) {
327      FIXP_DBL x = - (*pOvl--);
328      *pOut0 = IMDCT_SCALE_DBL(x);
329      pOut0 ++;
330    }
331
332    if ( noOutSamples <= nrSamples ) {
333      /* Divert output second half to overlap buffer if we already got enough output samples. */
334      pOut1 = hMdct->overlap.time + hMdct->ov_offset + fl/2 - 1;
335      hMdct->ov_offset += fl/2 + nl;
336    } else {
337      pOut1 = pOut0 + (fl - 1);
338      nrSamples += fl/2 + nl;
339    }
340
341    /* output samples before window crossing point NR .. TL/2. -overlap[TL/2-NR..TL/2-NR-FL/2] + current[NR..TL/2] */
342    /* output samples after window crossing point TL/2 .. TL/2+FL/2. -overlap[0..FL/2] - current[TL/2..FL/2] */
343    pCurr = pSpec + tl - fl/2;
344    for (i=0; i<fl/2; i++) {
345      FIXP_DBL x0, x1;
346
347      cplxMult(&x1, &x0, *pCurr++, - *pOvl--, pWindow[i]);
348      *pOut0 = IMDCT_SCALE_DBL(x0);
349      *pOut1 = IMDCT_SCALE_DBL(-x1);
350      pOut0 ++;
351      pOut1 --;
352    }
353    pOut0 += (fl/2);
354
355    /* NL output samples TL/2+FL/2..TL. - current[FL/2..0] */
356    pOut1 += (fl/2) + 1;
357    pCurr = pSpec + tl - fl/2 - 1;
358    for (i=0; i<nl; i++) {
359      FIXP_DBL x = - (*pCurr--);
360      *pOut1 = IMDCT_SCALE_DBL(x);
361      pOut1 ++;
362    }
363
364    /* Set overlap source pointer for next window pOvl = pSpec + tl/2 - 1; */
365    pOvl = pSpec + tl/2 - 1;
366
367    /* Previous window values. */
368    hMdct->prev_nr = nr;
369    hMdct->prev_fr = fr;
370    hMdct->prev_tl = tl;
371    hMdct->prev_wrs = wrs;
372  }
373
374  /* Save overlap */
375
376  pOvl = hMdct->overlap.freq + hMdct->ov_size - tl/2;
377  FDK_ASSERT(pOvl >= hMdct->overlap.time + hMdct->ov_offset);
378  FDK_ASSERT(tl/2 <= hMdct->ov_size);
379  for (i=0; i<tl/2; i++) {
380    pOvl[i] = spectrum[i+(nSpec-1)*tl];
381  }
382
383  return nrSamples;
384}
385
386