2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
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.
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.
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.
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.
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:
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.
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.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
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.
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."
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.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
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.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
84 /***************************** MPEG-4 AAC Decoder **************************
86 Author(s): Christian Griebel
87 Description: Dynamic range control (DRC) decoder tool for SBR
89 ******************************************************************************/
91 #include "sbrdec_drc.h"
94 /* DRC - Offset table for QMF interpolation. */
95 static const int offsetTab
[2][16] =
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 */
102 \brief Initialize DRC QMF factors
104 \hDrcData Handle to DRC channel data.
108 void sbrDecoder_drcInitChannel (
109 HANDLE_SBR_DRC_CHANNEL hDrcData
)
113 if (hDrcData
== NULL
) {
117 for (band
= 0; band
< (64); band
++) {
118 hDrcData
->prevFact_mag
[band
] = FL2FXCONST_DBL(0.5f
);
121 for (band
= 0; band
< SBRDEC_MAX_DRC_BANDS
; band
++) {
122 hDrcData
->currFact_mag
[band
] = FL2FXCONST_DBL(0.5f
);
123 hDrcData
->nextFact_mag
[band
] = FL2FXCONST_DBL(0.5f
);
126 hDrcData
->prevFact_exp
= 1;
127 hDrcData
->currFact_exp
= 1;
128 hDrcData
->nextFact_exp
= 1;
130 hDrcData
->numBandsCurr
= 1;
131 hDrcData
->numBandsNext
= 1;
133 hDrcData
->winSequenceCurr
= 0;
134 hDrcData
->winSequenceNext
= 0;
136 hDrcData
->drcInterpolationSchemeCurr
= 0;
137 hDrcData
->drcInterpolationSchemeNext
= 0;
139 hDrcData
->enable
= 0;
144 \brief Swap DRC QMF scaling factors after they have been applied.
146 \hDrcData Handle to DRC channel data.
150 void sbrDecoder_drcUpdateChannel (
151 HANDLE_SBR_DRC_CHANNEL hDrcData
)
153 if (hDrcData
== NULL
) {
156 if (hDrcData
->enable
!= 1) {
160 /* swap previous data */
161 FDKmemcpy( hDrcData
->currFact_mag
,
162 hDrcData
->nextFact_mag
,
163 SBRDEC_MAX_DRC_BANDS
* sizeof(FIXP_DBL
) );
165 hDrcData
->currFact_exp
= hDrcData
->nextFact_exp
;
167 hDrcData
->numBandsCurr
= hDrcData
->numBandsNext
;
169 FDKmemcpy( hDrcData
->bandTopCurr
,
170 hDrcData
->bandTopNext
,
171 SBRDEC_MAX_DRC_BANDS
* sizeof(USHORT
) );
173 hDrcData
->drcInterpolationSchemeCurr
= hDrcData
->drcInterpolationSchemeNext
;
175 hDrcData
->winSequenceCurr
= hDrcData
->winSequenceNext
;
180 \brief Apply DRC factors slot based.
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.
191 void sbrDecoder_drcApplySlot (
192 HANDLE_SBR_DRC_CHANNEL hDrcData
,
193 FIXP_DBL
*qmfRealSlot
,
194 FIXP_DBL
*qmfImagSlot
,
196 int numQmfSubSamples
,
202 int band
, bottomMdct
, topMdct
, bin
, useLP
;
203 int indx
= numQmfSubSamples
- (numQmfSubSamples
>> 1) - 10; /* l_border */
204 int frameLenFlag
= (numQmfSubSamples
== 30) ? 1 : 0;
206 const FIXP_DBL
*fact_mag
= NULL
;
209 USHORT
*bandTop
= NULL
;
212 FIXP_DBL alphaValue
= FL2FXCONST_DBL(0.0f
);
214 if (hDrcData
== NULL
) {
217 if (hDrcData
->enable
!= 1) {
221 offset
= offsetTab
[frameLenFlag
];
223 useLP
= (qmfImagSlot
== NULL
) ? 1 : 0;
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);
234 if (hDrcData
->drcInterpolationSchemeCurr
== 0) {
235 INT k
= (frameLenFlag
) ? 0x4444444 : 0x4000000;
237 alphaValue
= (FIXP_DBL
)(j
* k
);
240 if (j
>= offset
[hDrcData
->drcInterpolationSchemeCurr
- 1]) {
241 alphaValue
= (FIXP_DBL
)MAXVAL_DBL
;
245 else { /* short windows */
249 fact_mag
= hDrcData
->currFact_mag
;
250 fact_exp
= hDrcData
->currFact_exp
;
251 numBands
= hDrcData
->numBandsCurr
;
252 bandTop
= hDrcData
->bandTopCurr
;
254 else if (col
< numQmfSubSamples
) { /* second half of current frame */
255 if (hDrcData
->winSequenceNext
!= 2) { /* next: long window */
256 int j
= col
- (numQmfSubSamples
>>1);
258 if (hDrcData
->drcInterpolationSchemeNext
== 0) {
259 INT k
= (frameLenFlag
) ? 0x4444444 : 0x4000000;
261 alphaValue
= (FIXP_DBL
)(j
* k
);
264 if (j
>= offset
[hDrcData
->drcInterpolationSchemeNext
- 1]) {
265 alphaValue
= (FIXP_DBL
)MAXVAL_DBL
;
269 fact_mag
= hDrcData
->nextFact_mag
;
270 fact_exp
= hDrcData
->nextFact_exp
;
271 numBands
= hDrcData
->numBandsNext
;
272 bandTop
= hDrcData
->bandTopNext
;
274 else { /* next: short windows */
275 if (hDrcData
->winSequenceCurr
!= 2) { /* current: long window */
276 alphaValue
= (FIXP_DBL
)0;
278 fact_mag
= hDrcData
->nextFact_mag
;
279 fact_exp
= hDrcData
->nextFact_exp
;
280 numBands
= hDrcData
->numBandsNext
;
281 bandTop
= hDrcData
->bandTopNext
;
283 else { /* current: short windows */
286 fact_mag
= hDrcData
->currFact_mag
;
287 fact_exp
= hDrcData
->currFact_exp
;
288 numBands
= hDrcData
->numBandsCurr
;
289 bandTop
= hDrcData
->bandTopCurr
;
293 else { /* first half of next frame */
294 if (hDrcData
->winSequenceNext
!= 2) { /* long window */
295 int j
= col
- (numQmfSubSamples
>>1);
297 if (hDrcData
->drcInterpolationSchemeNext
== 0) {
298 INT k
= (frameLenFlag
) ? 0x4444444 : 0x4000000;
300 alphaValue
= (FIXP_DBL
)(j
* k
);
303 if (j
>= offset
[hDrcData
->drcInterpolationSchemeNext
- 1]) {
304 alphaValue
= (FIXP_DBL
)MAXVAL_DBL
;
308 else { /* short windows */
312 fact_mag
= hDrcData
->nextFact_mag
;
313 fact_exp
= hDrcData
->nextFact_exp
;
314 numBands
= hDrcData
->numBandsNext
;
315 bandTop
= hDrcData
->bandTopNext
;
317 col
-= numQmfSubSamples
;
322 for (band
= 0; band
< (int)numBands
; band
++) {
323 int bottomQmf
, topQmf
;
325 FIXP_DBL drcFact_mag
= (FIXP_DBL
)MAXVAL_DBL
;
327 topMdct
= (bandTop
[band
]+1) << 2;
329 if (!shortDrc
) { /* long window */
332 bottomMdct
= 30 * (bottomMdct
/ 30);
333 topMdct
= 30 * (topMdct
/ 30);
335 bottomQmf
= fMultIfloor((FIXP_DBL
)0x4444444, bottomMdct
);
336 topQmf
= fMultIfloor((FIXP_DBL
)0x4444444, topMdct
);
343 bottomQmf
= bottomMdct
>> 5;
344 topQmf
= topMdct
>> 5;
347 if (band
== ((int)numBands
-1)) {
351 for (bin
= bottomQmf
; bin
< topQmf
; bin
++) {
352 FIXP_DBL drcFact1_mag
= hDrcData
->prevFact_mag
[bin
];
353 FIXP_DBL drcFact2_mag
= fact_mag
[band
];
355 /* normalize scale factors */
356 if (hDrcData
->prevFact_exp
< maxShift
) {
357 drcFact1_mag
>>= maxShift
- hDrcData
->prevFact_exp
;
359 if (fact_exp
< maxShift
) {
360 drcFact2_mag
>>= maxShift
- fact_exp
;
364 if (alphaValue
== (FIXP_DBL
)0) {
365 drcFact_mag
= drcFact1_mag
;
366 } else if (alphaValue
== (FIXP_DBL
)MAXVAL_DBL
) {
367 drcFact_mag
= drcFact2_mag
;
369 drcFact_mag
= fMult(alphaValue
, drcFact2_mag
) + fMult(((FIXP_DBL
)MAXVAL_DBL
- alphaValue
), drcFact1_mag
);
373 qmfRealSlot
[bin
] = fMult(qmfRealSlot
[bin
], drcFact_mag
);
375 qmfImagSlot
[bin
] = fMult(qmfImagSlot
[bin
], drcFact_mag
);
378 /* save previous factors */
379 if (col
== (numQmfSubSamples
>>1)-1) {
380 hDrcData
->prevFact_mag
[bin
] = fact_mag
[band
];
384 else { /* short windows */
385 int startSample
, stopSample
;
386 FIXP_DBL invFrameSizeDiv8
= (frameLenFlag
) ? (FIXP_DBL
)0x1111111 : (FIXP_DBL
)0x1000000;
390 bottomMdct
= 30/8 * (bottomMdct
*8/30);
391 topMdct
= 30/8 * (topMdct
*8/30);
399 /* startSample is truncated to the nearest corresponding start subsample in
400 the QMF of the short window bottom is present in:*/
401 startSample
= ((fMultIfloor( invFrameSizeDiv8
, bottomMdct
) & 0x7) * numQmfSubSamples
) >> 3;
403 /* stopSample is rounded upwards to the nearest corresponding stop subsample
404 in the QMF of the short window top is present in. */
405 stopSample
= ((fMultIceil( invFrameSizeDiv8
, topMdct
) & 0xf) * numQmfSubSamples
) >> 3;
407 bottomQmf
= fMultIfloor( invFrameSizeDiv8
, ((bottomMdct
%(numQmfSubSamples
<<2)) << 5) );
408 topQmf
= fMultIfloor( invFrameSizeDiv8
, ((topMdct
%(numQmfSubSamples
<<2)) << 5) );
410 /* extend last band */
411 if (band
== ((int)numBands
-1)) {
413 stopSample
= numQmfSubSamples
;
420 /* save previous factors */
421 if (stopSample
== numQmfSubSamples
) {
422 int tmpBottom
= bottomQmf
;
424 if (((numQmfSubSamples
-1) & ~0x03) > startSample
) {
425 tmpBottom
= 0; /* band starts in previous short window */
428 for (bin
= tmpBottom
; bin
< topQmf
; bin
++) {
429 hDrcData
->prevFact_mag
[bin
] = fact_mag
[band
];
434 if ((col
>= startSample
) && (col
< stopSample
)) {
435 if ((col
& ~0x03) > startSample
) {
436 bottomQmf
= 0; /* band starts in previous short window */
438 if (col
< ((stopSample
-1) & ~0x03)) {
439 topQmf
= (64); /* band ends in next short window */
442 drcFact_mag
= fact_mag
[band
];
444 /* normalize scale factor */
445 if (fact_exp
< maxShift
) {
446 drcFact_mag
>>= maxShift
- fact_exp
;
450 for (bin
= bottomQmf
; bin
< topQmf
; bin
++) {
451 qmfRealSlot
[bin
] = fMult(qmfRealSlot
[bin
], drcFact_mag
);
453 qmfImagSlot
[bin
] = fMult(qmfImagSlot
[bin
], drcFact_mag
);
459 bottomMdct
= topMdct
;
460 } /* end of bands loop */
462 if (col
== (numQmfSubSamples
>>1)-1) {
463 hDrcData
->prevFact_exp
= fact_exp
;
469 \brief Apply DRC factors frame based.
471 \hDrcData Handle to DRC channel data.
472 \qmfRealSlot Pointer to real valued QMF data of the whole frame.
473 \qmfImagSlot Pointer to the imaginary QMF data of the whole frame.
474 \numQmfSubSamples Total number of time slots for one frame.
475 \scaleFactor Pointer to the out scale factor of the frame.
479 void sbrDecoder_drcApply (
480 HANDLE_SBR_DRC_CHANNEL hDrcData
,
481 FIXP_DBL
**QmfBufferReal
,
482 FIXP_DBL
**QmfBufferImag
,
483 int numQmfSubSamples
,
490 if (hDrcData
== NULL
) {
493 if (hDrcData
->enable
== 0) {
494 return; /* Avoid changing the scaleFactor even though the processing is disabled. */
497 /* get max scale factor */
498 if (hDrcData
->prevFact_exp
> maxShift
) {
499 maxShift
= hDrcData
->prevFact_exp
;
501 if (hDrcData
->currFact_exp
> maxShift
) {
502 maxShift
= hDrcData
->currFact_exp
;
504 if (hDrcData
->nextFact_exp
> maxShift
) {
505 maxShift
= hDrcData
->nextFact_exp
;
508 for (col
= 0; col
< numQmfSubSamples
; col
++)
510 FIXP_DBL
*qmfSlotReal
= QmfBufferReal
[col
];
511 FIXP_DBL
*qmfSlotImag
= (QmfBufferImag
== NULL
) ? NULL
: QmfBufferImag
[col
];
513 sbrDecoder_drcApplySlot (
523 *scaleFactor
+= maxShift
;