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/*****************************  MPEG-4 AAC Decoder  **************************
85
86   Author(s):   Christian Griebel
87   Description: Dynamic range control (DRC) decoder tool for SBR
88
89******************************************************************************/
90
91#include "sbrdec_drc.h"
92
93
94/* DRC - Offset table for QMF interpolation. */
95static const int offsetTab[2][16] =
96{
97  { 0, 4, 8, 12, 16, 20, 24, 28, 0, 0, 0, 0, 0, 0, 0, 0 },  /* 1024 framing */
98  { 0, 4, 8, 12, 16, 19, 22, 26, 0, 0, 0, 0, 0, 0, 0, 0 }   /*  960 framing */
99};
100
101/*!
102  \brief Initialize DRC QMF factors
103
104  \hDrcData Handle to DRC channel data.
105
106  \return none
107*/
108void sbrDecoder_drcInitChannel (
109    HANDLE_SBR_DRC_CHANNEL  hDrcData )
110{
111  int band;
112
113  if (hDrcData == NULL) {
114    return;
115  }
116
117  for (band = 0; band < (64); band++) {
118    hDrcData->prevFact_mag[band] = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_DBL(1.0f)*/;
119  }
120
121  for (band = 0; band < SBRDEC_MAX_DRC_BANDS; band++) {
122    hDrcData->currFact_mag[band] = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_DBL(1.0f)*/;
123    hDrcData->nextFact_mag[band] = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_DBL(1.0f)*/;
124  }
125
126  hDrcData->prevFact_exp = 0;
127  hDrcData->currFact_exp = 0;
128  hDrcData->nextFact_exp = 0;
129
130  hDrcData->numBandsCurr = 0;
131  hDrcData->numBandsNext = 0;
132
133  hDrcData->winSequenceCurr = 0;
134  hDrcData->winSequenceNext = 0;
135
136  hDrcData->drcInterpolationSchemeCurr = 0;
137  hDrcData->drcInterpolationSchemeNext = 0;
138
139  hDrcData->enable = 0;
140}
141
142
143/*!
144  \brief Swap DRC QMF scaling factors after they have been applied.
145
146  \hDrcData Handle to DRC channel data.
147
148  \return none
149*/
150void sbrDecoder_drcUpdateChannel (
151    HANDLE_SBR_DRC_CHANNEL  hDrcData )
152{
153  if (hDrcData == NULL) {
154    return;
155  }
156  if (hDrcData->enable != 1) {
157    return;
158  }
159
160  /* swap previous data */
161  FDKmemcpy( hDrcData->currFact_mag,
162             hDrcData->nextFact_mag,
163             SBRDEC_MAX_DRC_BANDS * sizeof(FIXP_DBL) );
164
165  hDrcData->currFact_exp = hDrcData->nextFact_exp;
166
167  hDrcData->numBandsCurr = hDrcData->numBandsNext;
168
169  FDKmemcpy( hDrcData->bandTopCurr,
170             hDrcData->bandTopNext,
171             SBRDEC_MAX_DRC_BANDS * sizeof(USHORT) );
172
173  hDrcData->drcInterpolationSchemeCurr = hDrcData->drcInterpolationSchemeNext;
174
175  hDrcData->winSequenceCurr = hDrcData->winSequenceNext;
176}
177
178
179/*!
180  \brief Apply DRC factors slot based.
181
182  \hDrcData Handle to DRC channel data.
183  \qmfRealSlot Pointer to real valued QMF data of one time slot.
184  \qmfImagSlot Pointer to the imaginary QMF data of one time slot.
185  \col Number of the time slot.
186  \numQmfSubSamples Total number of time slots for one frame.
187  \scaleFactor Pointer to the out scale factor of the time slot.
188
189  \return None.
190*/
191void sbrDecoder_drcApplySlot (
192    HANDLE_SBR_DRC_CHANNEL  hDrcData,
193    FIXP_DBL   *qmfRealSlot,
194    FIXP_DBL   *qmfImagSlot,
195    int  col,
196    int  numQmfSubSamples,
197    int  maxShift
198  )
199{
200  const int *offset;
201
202  int band, bottomMdct, topMdct, bin, useLP;
203  int indx = numQmfSubSamples - (numQmfSubSamples >> 1) - 10;   /* l_border */
204  int frameLenFlag = (numQmfSubSamples == 30) ? 1 : 0;
205
206  const FIXP_DBL *fact_mag = NULL;
207  INT fact_exp = 0;
208  UINT numBands = 0;
209  USHORT *bandTop = NULL;
210  int shortDrc = 0;
211
212  FIXP_DBL alphaValue = FL2FXCONST_DBL(0.0f);
213
214  if (hDrcData == NULL) {
215    return;
216  }
217  if (hDrcData->enable != 1) {
218    return;
219  }
220
221  offset = offsetTab[frameLenFlag];
222
223  useLP = (qmfImagSlot == NULL) ? 1 : 0;
224
225  col += indx;
226  bottomMdct = 0;
227  bin = 0;
228
229  /* get respective data and calc interpolation factor */
230  if (col < (numQmfSubSamples>>1)) {  /* first half of current frame */
231    if (hDrcData->winSequenceCurr != 2) { /* long window */
232      int j = col + (numQmfSubSamples>>1);
233
234      if (hDrcData->drcInterpolationSchemeCurr == 0) {
235        INT k = (frameLenFlag) ? 0x4444444 : 0x4000000;
236
237        alphaValue = (FIXP_DBL)(j * k);
238      }
239      else {
240        if (j >= offset[hDrcData->drcInterpolationSchemeCurr - 1]) {
241          alphaValue = FL2FXCONST_DBL(1.0f);
242        }
243      }
244    }
245    else {  /* short windows */
246      shortDrc = 1;
247    }
248
249    fact_mag = hDrcData->currFact_mag;
250    fact_exp = hDrcData->currFact_exp;
251    numBands = hDrcData->numBandsCurr;
252    bandTop = hDrcData->bandTopCurr;
253  }
254  else if (col < numQmfSubSamples) {  /* second half of current frame */
255    if (hDrcData->winSequenceNext != 2) { /* next: long window */
256      int j = col - (numQmfSubSamples>>1);
257
258      if (hDrcData->drcInterpolationSchemeNext == 0) {
259        INT k = (frameLenFlag) ? 0x4444444 : 0x4000000;
260
261        alphaValue = (FIXP_DBL)(j * k);
262      }
263      else {
264        if (j >= offset[hDrcData->drcInterpolationSchemeNext - 1]) {
265          alphaValue = FL2FXCONST_DBL(1.0f);
266        }
267      }
268
269      fact_mag = hDrcData->nextFact_mag;
270      fact_exp = hDrcData->nextFact_exp;
271      numBands = hDrcData->numBandsNext;
272      bandTop = hDrcData->bandTopNext;
273    }
274    else {  /* next: short windows */
275      if (hDrcData->winSequenceCurr != 2) {  /* current: long window */
276        alphaValue = (FIXP_DBL)0;
277
278        fact_mag = hDrcData->nextFact_mag;
279        fact_exp = hDrcData->nextFact_exp;
280        numBands = hDrcData->numBandsNext;
281        bandTop = hDrcData->bandTopNext;
282      }
283      else {  /* current: short windows */
284        shortDrc = 1;
285
286        fact_mag = hDrcData->currFact_mag;
287        fact_exp = hDrcData->currFact_exp;
288        numBands = hDrcData->numBandsCurr;
289        bandTop = hDrcData->bandTopCurr;
290      }
291    }
292  }
293  else {  /* first half of next frame */
294    if (hDrcData->winSequenceNext != 2) { /* long window */
295      int j = col - (numQmfSubSamples>>1);
296
297      if (hDrcData->drcInterpolationSchemeNext == 0) {
298        INT k = (frameLenFlag) ? 0x4444444 : 0x4000000;
299
300        alphaValue = (FIXP_DBL)(j * k);
301      }
302      else {
303        if (j >= offset[hDrcData->drcInterpolationSchemeNext - 1]) {
304          alphaValue = FL2FXCONST_DBL(1.0f);
305        }
306      }
307    }
308    else {  /* short windows */
309      shortDrc = 1;
310    }
311
312    fact_mag = hDrcData->nextFact_mag;
313    fact_exp = hDrcData->nextFact_exp;
314    numBands = hDrcData->numBandsNext;
315    bandTop = hDrcData->bandTopNext;
316
317    col -= numQmfSubSamples;
318  }
319
320
321  /* process bands */
322  for (band = 0; band < (int)numBands; band++) {
323    int bottomQmf, topQmf;
324
325    FIXP_DBL drcFact_mag = FL2FXCONST_DBL(1.0f);
326
327    topMdct = (bandTop[band]+1) << 2;
328
329    if (!shortDrc) {  /* long window */
330      if (frameLenFlag) {
331        /* 960 framing */
332        bottomMdct = 30 * (bottomMdct / 30);
333        topMdct    = 30 * (topMdct / 30);
334
335        bottomQmf = fMultIfloor((FIXP_DBL)0x4444444, bottomMdct);
336        topQmf    = fMultIfloor((FIXP_DBL)0x4444444, topMdct);
337      }
338      else {
339        /* 1024 framing */
340        bottomMdct &= ~0x1f;
341        topMdct    &= ~0x1f;
342
343        bottomQmf = bottomMdct >> 5;
344        topQmf    = topMdct >> 5;
345      }
346
347      if (band == ((int)numBands-1)) {
348        topQmf = (64);
349      }
350
351      for (bin = bottomQmf; bin < topQmf; bin++) {
352        FIXP_DBL drcFact1_mag = hDrcData->prevFact_mag[bin];
353        FIXP_DBL drcFact2_mag = fact_mag[band];
354
355        /* normalize scale factors */
356        if (hDrcData->prevFact_exp < maxShift) {
357          drcFact1_mag >>= maxShift - hDrcData->prevFact_exp;
358        }
359        if (fact_exp < maxShift) {
360          drcFact2_mag >>= maxShift - fact_exp;
361        }
362
363        /* interpolate */
364        drcFact_mag = fMult(alphaValue, drcFact2_mag) + fMult((FL2FXCONST_DBL(1.0f) - alphaValue), drcFact1_mag);
365
366        /* apply scaling */
367        qmfRealSlot[bin] = fMult(qmfRealSlot[bin], drcFact_mag);
368        if (!useLP) {
369          qmfImagSlot[bin] = fMult(qmfImagSlot[bin], drcFact_mag);
370        }
371
372        /* save previous factors */
373        if (col == (numQmfSubSamples>>1)-1) {
374          hDrcData->prevFact_mag[bin] = fact_mag[band];
375        }
376      }
377    }
378    else {  /* short windows */
379      int startSample, stopSample;
380      FIXP_DBL invFrameSizeDiv8 = (frameLenFlag) ? (FIXP_DBL)0x1111111 : (FIXP_DBL)0x1000000;
381
382      if (frameLenFlag) {
383        /*  960 framing */
384        bottomMdct = 30/8 * (bottomMdct*8/30);
385        topMdct    = 30/8 * (topMdct*8/30);
386      }
387      else {
388        /* 1024 framing */
389        bottomMdct &= ~0x03;
390        topMdct    &= ~0x03;
391      }
392
393      /* startSample is truncated to the nearest corresponding start subsample in
394         the QMF of the short window bottom is present in:*/
395      startSample  = ((fMultIfloor( invFrameSizeDiv8, bottomMdct ) & 0x7) * numQmfSubSamples) >> 3;
396
397      /* stopSample is rounded upwards to the nearest corresponding stop subsample
398         in the QMF of the short window top is present in. */
399      stopSample  = ((fMultIceil( invFrameSizeDiv8, topMdct ) & 0xf) * numQmfSubSamples) >> 3;
400
401      bottomQmf = fMultIfloor( invFrameSizeDiv8, ((bottomMdct%(numQmfSubSamples<<2)) << 5) );
402      topQmf    = fMultIfloor( invFrameSizeDiv8, ((topMdct%(numQmfSubSamples<<2)) << 5) );
403
404      /* extend last band */
405      if (band == ((int)numBands-1)) {
406        topQmf = (64);
407        stopSample = numQmfSubSamples;
408      }
409
410      if (topQmf == 0) {
411        topQmf = (64);
412      }
413
414      /* save previous factors */
415      if (stopSample == numQmfSubSamples) {
416        int tmpBottom = bottomQmf;
417
418        if (((numQmfSubSamples-1) & ~0x03) > startSample) {
419            tmpBottom = 0;    /* band starts in previous short window */
420        }
421
422        for (bin = tmpBottom; bin < topQmf; bin++) {
423          hDrcData->prevFact_mag[bin] = fact_mag[band];
424        }
425      }
426
427      /* apply */
428      if ((col >= startSample) && (col < stopSample)) {
429        if ((col & ~0x03) > startSample) {
430            bottomQmf = 0;    /* band starts in previous short window */
431        }
432        if (col < ((stopSample-1) & ~0x03)) {
433            topQmf = (64);   /* band ends in next short window */
434        }
435
436        drcFact_mag = fact_mag[band];
437
438        /* normalize scale factor */
439        if (fact_exp < maxShift) {
440          drcFact_mag >>= maxShift - fact_exp;
441        }
442
443        /* apply scaling */
444        for (bin = bottomQmf; bin < topQmf; bin++) {
445          qmfRealSlot[bin] = fMult(qmfRealSlot[bin], drcFact_mag);
446          if (!useLP) {
447            qmfImagSlot[bin] = fMult(qmfImagSlot[bin], drcFact_mag);
448          }
449        }
450      }
451    }
452
453    bottomMdct = topMdct;
454  }   /* end of bands loop */
455
456  if (col == (numQmfSubSamples>>1)-1) {
457    hDrcData->prevFact_exp = fact_exp;
458  }
459}
460
461
462/*!
463  \brief Apply DRC factors frame based.
464
465  \hDrcData Handle to DRC channel data.
466  \qmfRealSlot Pointer to real valued QMF data of the whole frame.
467  \qmfImagSlot Pointer to the imaginary QMF data of the whole frame.
468  \numQmfSubSamples Total number of time slots for one frame.
469  \scaleFactor Pointer to the out scale factor of the frame.
470
471  \return None.
472*/
473void sbrDecoder_drcApply (
474    HANDLE_SBR_DRC_CHANNEL  hDrcData,
475    FIXP_DBL **QmfBufferReal,
476    FIXP_DBL **QmfBufferImag,
477    int  numQmfSubSamples,
478    int *scaleFactor
479  )
480{
481  int col;
482  int maxShift = 0;
483
484  /* get max scale factor */
485  if (hDrcData->prevFact_exp > maxShift) {
486    maxShift = hDrcData->prevFact_exp;
487  }
488  if (hDrcData->currFact_exp > maxShift) {
489    maxShift = hDrcData->currFact_exp;
490  }
491  if (hDrcData->nextFact_exp > maxShift) {
492    maxShift = hDrcData->nextFact_exp;
493  }
494
495  for (col = 0; col < numQmfSubSamples; col++)
496  {
497    FIXP_DBL *qmfSlotReal = QmfBufferReal[col];
498    FIXP_DBL *qmfSlotImag = (QmfBufferImag == NULL) ? NULL : QmfBufferImag[col];
499
500    sbrDecoder_drcApplySlot (
501      hDrcData,
502      qmfSlotReal,
503      qmfSlotImag,
504      col,
505      numQmfSubSamples,
506      maxShift
507    );
508  }
509
510  *scaleFactor += maxShift;
511}
512
513