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 ----------------------------------------------------------------------------------------------------------- */
86 \brief Envelope extraction
87 The functions provided by this module are mostly called by applySBR(). After it is
88 determined that there is valid SBR data, sbrGetHeaderData() might be called if the current
89 SBR data contains an \ref SBR_HEADER_ELEMENT as opposed to a \ref SBR_STANDARD_ELEMENT. This function
90 may return various error codes as defined in #SBR_HEADER_STATUS . Most importantly it returns HEADER_RESET when decoder
91 settings need to be recalculated according to the SBR specifications. In that case applySBR()
92 will initiatite the required re-configuration.
94 The header data is stored in a #SBR_HEADER_DATA structure.
96 The actual SBR data for the current frame is decoded into SBR_FRAME_DATA stuctures by sbrGetChannelPairElement()
97 [for stereo streams] and sbrGetSingleChannelElement() [for mono streams]. There is no fractional arithmetic involved.
99 Once the information is extracted, the data needs to be further prepared before the actual decoding process.
100 This is done in decodeSbrData().
102 \sa Description of buffer management in applySBR(). \ref documentationOverview
104 <h1>About the SBR data format:</h1>
106 Each frame includes SBR data (side chain information), and can be either the \ref SBR_HEADER_ELEMENT or the \ref SBR_STANDARD_ELEMENT.
107 Parts of the data can be protected by a CRC checksum.
109 \anchor SBR_HEADER_ELEMENT <h2>The SBR_HEADER_ELEMENT</h2>
111 The SBR_HEADER_ELEMENT can be transmitted with every frame, however, it typically is send every second or so. It contains fundamental
112 information such as SBR sampling frequency and frequency range as well as control signals that do not require frequent changes. It also
113 includes the \ref SBR_STANDARD_ELEMENT.
115 Depending on the changes between the information in a current SBR_HEADER_ELEMENT and the previous SBR_HEADER_ELEMENT, the SBR decoder might need
116 to be reset and reconfigured (e.g. new tables need to be calculated).
118 \anchor SBR_STANDARD_ELEMENT <h2>The SBR_STANDARD_ELEMENT</h2>
120 This data can be subdivided into "side info" and "raw data", where side info is defined as signals needed to decode the raw data
121 and some decoder tuning signals. Raw data is referred to as PCM and Huffman coded envelope and noise floor estimates. The side info also
122 includes information about the time-frequency grid for the current frame.
124 \sa \ref documentationOverview
127 #include "env_extr.h"
131 #include "huff_dec.h"
134 #include "psbitdec.h"
136 #define DRM_PARAMETRIC_STEREO 0
137 #define EXTENSION_ID_PS_CODING 2
140 static int extractFrameInfo (HANDLE_FDK_BITSTREAM hBs
,
141 HANDLE_SBR_HEADER_DATA hHeaderData
,
142 HANDLE_SBR_FRAME_DATA h_frame_data
,
143 const UINT nrOfChannels
,
148 static int sbrGetEnvelope (HANDLE_SBR_HEADER_DATA hHeaderData
,
149 HANDLE_SBR_FRAME_DATA h_frame_data
,
150 HANDLE_FDK_BITSTREAM hBs
,
153 static void sbrGetDirectionControlData (HANDLE_SBR_FRAME_DATA hFrameData
,
154 HANDLE_FDK_BITSTREAM hBs
);
156 static void sbrGetNoiseFloorData (HANDLE_SBR_HEADER_DATA hHeaderData
,
157 HANDLE_SBR_FRAME_DATA h_frame_data
,
158 HANDLE_FDK_BITSTREAM hBs
);
160 static int checkFrameInfo (FRAME_INFO
*pFrameInfo
, int numberOfTimeSlots
, int overlap
, int timeStep
);
164 HANDLE_SBR_HEADER_DATA hHeaderData
,
165 const int sampleRateIn
,
166 const int sampleRateOut
,
167 const int samplesPerFrame
,
171 HANDLE_FREQ_BAND_DATA hFreq
= &hHeaderData
->freqBandData
;
172 SBR_ERROR sbrError
= SBRDEC_OK
;
173 int numAnalysisBands
;
175 if ( sampleRateIn
== sampleRateOut
) {
176 hHeaderData
->sbrProcSmplRate
= sampleRateOut
<<1;
177 numAnalysisBands
= 32;
179 hHeaderData
->sbrProcSmplRate
= sampleRateOut
;
180 if ( (sampleRateOut
>>1) == sampleRateIn
) {
182 numAnalysisBands
= 32;
183 } else if ( (sampleRateOut
>>2) == sampleRateIn
) {
185 numAnalysisBands
= 32;
186 } else if ( (sampleRateOut
*3)>>3 == (sampleRateIn
*8)>>3 ) {
187 /* 3:8, 3/4 core frame length */
188 numAnalysisBands
= 24;
190 sbrError
= SBRDEC_UNSUPPORTED_CONFIG
;
195 /* Fill in default values first */
196 hHeaderData
->syncState
= SBR_NOT_INITIALIZED
;
197 hHeaderData
->status
= 0;
198 hHeaderData
->frameErrorFlag
= 0;
200 hHeaderData
->bs_info
.ampResolution
= 1;
201 hHeaderData
->bs_info
.xover_band
= 0;
202 hHeaderData
->bs_info
.sbr_preprocessing
= 0;
204 hHeaderData
->bs_data
.startFreq
= 5;
205 hHeaderData
->bs_data
.stopFreq
= 0;
206 hHeaderData
->bs_data
.freqScale
= 2;
207 hHeaderData
->bs_data
.alterScale
= 1;
208 hHeaderData
->bs_data
.noise_bands
= 2;
209 hHeaderData
->bs_data
.limiterBands
= 2;
210 hHeaderData
->bs_data
.limiterGains
= 2;
211 hHeaderData
->bs_data
.interpolFreq
= 1;
212 hHeaderData
->bs_data
.smoothingLength
= 1;
214 hHeaderData
->timeStep
= (flags
& SBRDEC_ELD_GRID
) ? 1 : 2;
216 /* Setup pointers to frequency band tables */
217 hFreq
->freqBandTable
[0] = hFreq
->freqBandTableLo
;
218 hFreq
->freqBandTable
[1] = hFreq
->freqBandTableHi
;
220 /* Patch some entries */
221 if (sampleRateOut
> 24000) { /* Trigger an error if SBR is going to be processed without */
222 hHeaderData
->bs_data
.startFreq
= 7; /* having read these frequency values from bit stream before. */
223 hHeaderData
->bs_data
.stopFreq
= 3;
226 /* One SBR timeslot corresponds to the amount of samples equal to the amount of analysis bands, divided by the timestep. */
227 hHeaderData
->numberTimeSlots
= (samplesPerFrame
/numAnalysisBands
) >> (hHeaderData
->timeStep
- 1);
228 if (hHeaderData
->numberTimeSlots
> (16)) {
229 sbrError
= SBRDEC_UNSUPPORTED_CONFIG
;
232 hHeaderData
->numberOfAnalysisBands
= numAnalysisBands
;
240 \brief Initialize the SBR_PREV_FRAME_DATA struct
243 initSbrPrevFrameData (HANDLE_SBR_PREV_FRAME_DATA h_prev_data
, /*!< handle to struct SBR_PREV_FRAME_DATA */
244 int timeSlots
) /*!< Framelength in SBR-timeslots */
248 /* Set previous energy and noise levels to 0 for the case
249 that decoding starts in the middle of a bitstream */
250 for (i
=0; i
< MAX_FREQ_COEFFS
; i
++)
251 h_prev_data
->sfb_nrg_prev
[i
] = (FIXP_DBL
)0;
252 for (i
=0; i
< MAX_NOISE_COEFFS
; i
++)
253 h_prev_data
->prevNoiseLevel
[i
] = (FIXP_DBL
)0;
254 for (i
=0; i
< MAX_INVF_BANDS
; i
++)
255 h_prev_data
->sbr_invf_mode
[i
] = INVF_OFF
;
257 h_prev_data
->stopPos
= timeSlots
;
258 h_prev_data
->coupling
= COUPLING_OFF
;
259 h_prev_data
->ampRes
= 0;
264 \brief Read header data from bitstream
266 \return error status - 0 if ok
269 sbrGetHeaderData (HANDLE_SBR_HEADER_DATA hHeaderData
,
270 HANDLE_FDK_BITSTREAM hBs
,
272 const int fIsSbrData
)
274 SBR_HEADER_DATA_BS
*pBsData
;
275 SBR_HEADER_DATA_BS lastHeader
;
276 SBR_HEADER_DATA_BS_INFO lastInfo
;
277 int headerExtra1
=0, headerExtra2
=0;
279 /* Copy SBR bit stream header to temporary header */
280 lastHeader
= hHeaderData
->bs_data
;
281 lastInfo
= hHeaderData
->bs_info
;
283 /* Read new header from bitstream */
285 pBsData
= &hHeaderData
->bs_data
;
289 hHeaderData
->bs_info
.ampResolution
= FDKreadBits (hBs
, 1);
292 pBsData
->startFreq
= FDKreadBits (hBs
, 4);
293 pBsData
->stopFreq
= FDKreadBits (hBs
, 4);
296 hHeaderData
->bs_info
.xover_band
= FDKreadBits (hBs
, 3);
297 FDKreadBits (hBs
, 2);
300 headerExtra1
= FDKreadBits (hBs
, 1);
301 headerExtra2
= FDKreadBits (hBs
, 1);
303 /* Handle extra header information */
306 pBsData
->freqScale
= FDKreadBits (hBs
, 2);
307 pBsData
->alterScale
= FDKreadBits (hBs
, 1);
308 pBsData
->noise_bands
= FDKreadBits (hBs
, 2);
311 pBsData
->freqScale
= 2;
312 pBsData
->alterScale
= 1;
313 pBsData
->noise_bands
= 2;
317 pBsData
->limiterBands
= FDKreadBits (hBs
, 2);
318 pBsData
->limiterGains
= FDKreadBits (hBs
, 2);
319 pBsData
->interpolFreq
= FDKreadBits (hBs
, 1);
320 pBsData
->smoothingLength
= FDKreadBits (hBs
, 1);
323 pBsData
->limiterBands
= 2;
324 pBsData
->limiterGains
= 2;
325 pBsData
->interpolFreq
= 1;
326 pBsData
->smoothingLength
= 1;
329 /* Look for new settings. IEC 14496-3, 4.6.18.3.1 */
330 if(hHeaderData
->syncState
!= SBR_ACTIVE
||
331 lastHeader
.startFreq
!= pBsData
->startFreq
||
332 lastHeader
.stopFreq
!= pBsData
->stopFreq
||
333 lastHeader
.freqScale
!= pBsData
->freqScale
||
334 lastHeader
.alterScale
!= pBsData
->alterScale
||
335 lastHeader
.noise_bands
!= pBsData
->noise_bands
||
336 lastInfo
.xover_band
!= hHeaderData
->bs_info
.xover_band
) {
337 return HEADER_RESET
; /* New settings */
344 \brief Get missing harmonics parameters (only used for AAC+SBR)
346 \return error status - 0 if ok
349 sbrGetSyntheticCodedData(HANDLE_SBR_HEADER_DATA hHeaderData
,
350 HANDLE_SBR_FRAME_DATA hFrameData
,
351 HANDLE_FDK_BITSTREAM hBs
)
355 int flag
= FDKreadBits(hBs
,1);
359 for(i
=0;i
<hHeaderData
->freqBandData
.nSfb
[1];i
++){
360 hFrameData
->addHarmonics
[i
] = FDKreadBits (hBs
, 1 );
365 for(i
=0; i
<MAX_FREQ_COEFFS
; i
++)
366 hFrameData
->addHarmonics
[i
] = 0;
372 \brief Reads extension data from the bitstream
374 The bitstream format allows up to 4 kinds of extended data element.
375 Extended data may contain several elements, each identified by a 2-bit-ID.
376 So far, no extended data elements are defined hence the first 2 parameters
377 are unused. The data should be skipped in order to update the number
378 of read bits for the consistency check in applySBR().
380 static int extractExtendedData(
381 HANDLE_SBR_HEADER_DATA hHeaderData
, /*!< handle to SBR header */
382 HANDLE_FDK_BITSTREAM hBs
/*!< Handle to the bit buffer */
383 ,HANDLE_PS_DEC hParametricStereoDec
/*!< Parametric Stereo Decoder */
390 extended_data
= FDKreadBits(hBs
, 1);
396 cnt
= FDKreadBits(hBs
, 4);
398 cnt
+= FDKreadBits(hBs
, 8);
403 /* sanity check for cnt */
404 if (nBitsLeft
> (INT
)FDKgetValidBits(hBs
)) {
405 /* limit nBitsLeft */
406 nBitsLeft
= (INT
)FDKgetValidBits(hBs
);
407 /* set frame error */
411 while (nBitsLeft
> 7) {
412 int extension_id
= FDKreadBits(hBs
, 2);
415 switch(extension_id
) {
419 case EXTENSION_ID_PS_CODING
:
421 /* Read PS data from bitstream */
423 if (hParametricStereoDec
!= NULL
) {
424 if(bPsRead
&& !hParametricStereoDec
->bsData
[hParametricStereoDec
->bsReadSlot
].mpeg
.bPsHeaderValid
) {
425 cnt
= nBitsLeft
>> 3; /* number of remaining bytes */
426 for (i
=0; i
<cnt
; i
++)
428 nBitsLeft
-= cnt
* 8;
430 nBitsLeft
-= ReadPsData(hParametricStereoDec
, hBs
, nBitsLeft
);
435 /* parametric stereo detected, could set channelMode accordingly here */
437 /* "The usage of this parametric stereo extension to HE-AAC is */
438 /* signalled implicitly in the bitstream. Hence, if an sbr_extension() */
439 /* with bs_extension_id==EXTENSION_ID_PS is found in the SBR part of */
440 /* the bitstream, a decoder supporting the combination of SBR and PS */
441 /* shall operate the PS tool to generate a stereo output signal." */
442 /* source: ISO/IEC 14496-3:2001/FDAM 2:2004(E) */
448 cnt
= nBitsLeft
>> 3; /* number of remaining bytes */
449 for (i
=0; i
<cnt
; i
++)
451 nBitsLeft
-= cnt
* 8;
461 /* Read fill bits for byte alignment */
462 FDKreadBits(hBs
, nBitsLeft
);
472 \brief Read bitstream elements of one channel
474 \return SbrFrameOK: 1=ok, 0=error
477 sbrGetSingleChannelElement (HANDLE_SBR_HEADER_DATA hHeaderData
, /*!< Static control data */
478 HANDLE_SBR_FRAME_DATA hFrameData
, /*!< Control data of current frame */
479 HANDLE_FDK_BITSTREAM hBs
, /*!< Handle to struct BIT_BUF */
480 HANDLE_PS_DEC hParametricStereoDec
, /*!< Handle to PS decoder */
488 hFrameData
->coupling
= COUPLING_OFF
;
492 if (FDKreadBits(hBs
, 1)) { /* bs_data_extra */
494 if (flags
& SBRDEC_SYNTAX_SCAL
) {
500 if (flags
& SBRDEC_SYNTAX_SCAL
) {
501 FDKreadBits (hBs
, 1); /* bs_coupling */
507 if ( !extractFrameInfo ( hBs
, hHeaderData
, hFrameData
, 1, flags
) )
510 if ( !checkFrameInfo (&hFrameData
->frameInfo
, hHeaderData
->numberTimeSlots
, overlap
, hHeaderData
->timeStep
) )
515 Fetch domain vectors (time or frequency direction for delta-coding)
517 sbrGetDirectionControlData (hFrameData
, hBs
);
519 for (i
=0; i
<hHeaderData
->freqBandData
.nInvfBands
; i
++) {
520 hFrameData
->sbr_invf_mode
[i
] =
521 (INVF_MODE
) FDKreadBits (hBs
, 2);
527 if ( !sbrGetEnvelope (hHeaderData
, hFrameData
, hBs
, flags
) )
531 sbrGetNoiseFloorData (hHeaderData
, hFrameData
, hBs
);
533 sbrGetSyntheticCodedData(hHeaderData
, hFrameData
, hBs
);
536 /* sbr extended data */
537 if (! extractExtendedData(
540 ,hParametricStereoDec
552 \brief Read bitstream elements of a channel pair
556 sbrGetChannelPairElement (HANDLE_SBR_HEADER_DATA hHeaderData
, /*!< Static control data */
557 HANDLE_SBR_FRAME_DATA hFrameDataLeft
, /*!< Dynamic control data for first channel */
558 HANDLE_SBR_FRAME_DATA hFrameDataRight
,/*!< Dynamic control data for second channel */
559 HANDLE_FDK_BITSTREAM hBs
, /*!< handle to struct BIT_BUF */
567 if (FDKreadBits(hBs
, 1)) { /* bs_data_extra */
572 /* Read coupling flag */
573 bit
= FDKreadBits (hBs
, 1);
576 hFrameDataLeft
->coupling
= COUPLING_LEVEL
;
577 hFrameDataRight
->coupling
= COUPLING_BAL
;
580 hFrameDataLeft
->coupling
= COUPLING_OFF
;
581 hFrameDataRight
->coupling
= COUPLING_OFF
;
588 if ( !extractFrameInfo (hBs
, hHeaderData
, hFrameDataLeft
, 2, flags
) )
591 if ( !checkFrameInfo (&hFrameDataLeft
->frameInfo
, hHeaderData
->numberTimeSlots
, overlap
, hHeaderData
->timeStep
) )
594 if (hFrameDataLeft
->coupling
) {
595 FDKmemcpy (&hFrameDataRight
->frameInfo
, &hFrameDataLeft
->frameInfo
, sizeof(FRAME_INFO
));
596 hFrameDataRight
->ampResolutionCurrentFrame
= hFrameDataLeft
->ampResolutionCurrentFrame
;
599 if ( !extractFrameInfo (hBs
, hHeaderData
, hFrameDataRight
, 2, flags
) )
602 if ( !checkFrameInfo (&hFrameDataRight
->frameInfo
, hHeaderData
->numberTimeSlots
, overlap
, hHeaderData
->timeStep
) )
607 Fetch domain vectors (time or frequency direction for delta-coding)
609 sbrGetDirectionControlData (hFrameDataLeft
, hBs
);
610 sbrGetDirectionControlData (hFrameDataRight
, hBs
);
612 for (i
=0; i
<hHeaderData
->freqBandData
.nInvfBands
; i
++) {
613 hFrameDataLeft
->sbr_invf_mode
[i
] = (INVF_MODE
) FDKreadBits (hBs
, 2);
616 if (hFrameDataLeft
->coupling
) {
617 for (i
=0; i
<hHeaderData
->freqBandData
.nInvfBands
; i
++) {
618 hFrameDataRight
->sbr_invf_mode
[i
] = hFrameDataLeft
->sbr_invf_mode
[i
];
622 if ( !sbrGetEnvelope (hHeaderData
, hFrameDataLeft
, hBs
, flags
) ) {
626 sbrGetNoiseFloorData (hHeaderData
, hFrameDataLeft
, hBs
);
628 if ( !sbrGetEnvelope (hHeaderData
, hFrameDataRight
, hBs
, flags
) ) {
634 for (i
=0; i
<hHeaderData
->freqBandData
.nInvfBands
; i
++) {
635 hFrameDataRight
->sbr_invf_mode
[i
] = (INVF_MODE
) FDKreadBits (hBs
, 2);
640 if ( !sbrGetEnvelope (hHeaderData
, hFrameDataLeft
, hBs
, flags
) )
643 if ( !sbrGetEnvelope (hHeaderData
, hFrameDataRight
, hBs
, flags
) )
646 sbrGetNoiseFloorData (hHeaderData
, hFrameDataLeft
, hBs
);
649 sbrGetNoiseFloorData (hHeaderData
, hFrameDataRight
, hBs
);
651 sbrGetSyntheticCodedData(hHeaderData
, hFrameDataLeft
, hBs
);
652 sbrGetSyntheticCodedData(hHeaderData
, hFrameDataRight
, hBs
);
655 if (! extractExtendedData(
671 \brief Read direction control data from bitstream
674 sbrGetDirectionControlData (HANDLE_SBR_FRAME_DATA h_frame_data
, /*!< handle to struct SBR_FRAME_DATA */
675 HANDLE_FDK_BITSTREAM hBs
) /*!< handle to struct BIT_BUF */
679 for (i
= 0; i
< h_frame_data
->frameInfo
.nEnvelopes
; i
++) {
680 h_frame_data
->domain_vec
[i
] = FDKreadBits (hBs
, 1);
683 for (i
= 0; i
< h_frame_data
->frameInfo
.nNoiseEnvelopes
; i
++) {
684 h_frame_data
->domain_vec_noise
[i
] = FDKreadBits (hBs
, 1);
691 \brief Read noise-floor-level data from bitstream
694 sbrGetNoiseFloorData (HANDLE_SBR_HEADER_DATA hHeaderData
, /*!< Static control data */
695 HANDLE_SBR_FRAME_DATA h_frame_data
, /*!< handle to struct SBR_FRAME_DATA */
696 HANDLE_FDK_BITSTREAM hBs
) /*!< handle to struct BIT_BUF */
700 COUPLING_MODE coupling
;
701 int noNoiseBands
= hHeaderData
->freqBandData
.nNfb
;
705 int envDataTableCompFactor
;
707 coupling
= h_frame_data
->coupling
;
711 Select huffman codebook depending on coupling mode
713 if (coupling
== COUPLING_BAL
) {
714 hcb_noise
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_NoiseBalance11T
;
715 hcb_noiseF
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_EnvBalance11F
; /* "sbr_huffBook_NoiseBalance11F" */
716 envDataTableCompFactor
= 1;
719 hcb_noise
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_NoiseLevel11T
;
720 hcb_noiseF
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_EnvLevel11F
; /* "sbr_huffBook_NoiseLevel11F" */
721 envDataTableCompFactor
= 0;
725 Read raw noise-envelope data
727 for (i
=0; i
<h_frame_data
->frameInfo
.nNoiseEnvelopes
; i
++) {
730 if (h_frame_data
->domain_vec_noise
[i
] == 0) {
731 if (coupling
== COUPLING_BAL
) {
732 h_frame_data
->sbrNoiseFloorLevel
[i
*noNoiseBands
] =
733 (FIXP_SGL
) (((int)FDKreadBits (hBs
, 5)) << envDataTableCompFactor
);
736 h_frame_data
->sbrNoiseFloorLevel
[i
*noNoiseBands
] =
737 (FIXP_SGL
) (int)FDKreadBits (hBs
, 5);
740 for (j
= 1; j
< noNoiseBands
; j
++) {
741 delta
= DecodeHuffmanCW(hcb_noiseF
, hBs
);
742 h_frame_data
->sbrNoiseFloorLevel
[i
*noNoiseBands
+j
] = (FIXP_SGL
) (delta
<< envDataTableCompFactor
);
746 for (j
= 0; j
< noNoiseBands
; j
++) {
747 delta
= DecodeHuffmanCW(hcb_noise
, hBs
);
748 h_frame_data
->sbrNoiseFloorLevel
[i
*noNoiseBands
+j
] = (FIXP_SGL
) (delta
<< envDataTableCompFactor
);
756 \brief Read envelope data from bitstream
759 sbrGetEnvelope (HANDLE_SBR_HEADER_DATA hHeaderData
, /*!< Static control data */
760 HANDLE_SBR_FRAME_DATA h_frame_data
, /*!< handle to struct SBR_FRAME_DATA */
761 HANDLE_FDK_BITSTREAM hBs
, /*!< handle to struct BIT_BUF */
765 UCHAR no_band
[MAX_ENVELOPES
];
768 COUPLING_MODE coupling
= h_frame_data
->coupling
;
769 int ampRes
= hHeaderData
->bs_info
.ampResolution
;
770 int nEnvelopes
= h_frame_data
->frameInfo
.nEnvelopes
;
771 int envDataTableCompFactor
;
772 int start_bits
, start_bits_balance
;
773 Huffman hcb_t
, hcb_f
;
775 h_frame_data
->nScaleFactors
= 0;
777 if ( (h_frame_data
->frameInfo
.frameClass
== 0) && (nEnvelopes
== 1) ) {
778 if (flags
& SBRDEC_ELD_GRID
)
779 ampRes
= h_frame_data
->ampResolutionCurrentFrame
;
783 h_frame_data
->ampResolutionCurrentFrame
= ampRes
;
786 Set number of bits for first value depending on amplitude resolution
791 start_bits_balance
= 5;
796 start_bits_balance
= 6;
800 Calculate number of values for each envelope and alltogether
802 for (i
= 0; i
< nEnvelopes
; i
++) {
803 no_band
[i
] = hHeaderData
->freqBandData
.nSfb
[h_frame_data
->frameInfo
.freqRes
[i
]];
804 h_frame_data
->nScaleFactors
+= no_band
[i
];
806 if (h_frame_data
->nScaleFactors
> MAX_NUM_ENVELOPE_VALUES
)
810 Select Huffman codebook depending on coupling mode and amplitude resolution
812 if (coupling
== COUPLING_BAL
) {
813 envDataTableCompFactor
= 1;
815 hcb_t
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_EnvBalance10T
;
816 hcb_f
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_EnvBalance10F
;
819 hcb_t
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_EnvBalance11T
;
820 hcb_f
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_EnvBalance11F
;
824 envDataTableCompFactor
= 0;
826 hcb_t
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_EnvLevel10T
;
827 hcb_f
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_EnvLevel10F
;
830 hcb_t
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_EnvLevel11T
;
831 hcb_f
= (Huffman
)&FDK_sbrDecoder_sbr_huffBook_EnvLevel11F
;
836 Now read raw envelope data
838 for (j
= 0, offset
= 0; j
< nEnvelopes
; j
++) {
841 if (h_frame_data
->domain_vec
[j
] == 0) {
842 if (coupling
== COUPLING_BAL
) {
843 h_frame_data
->iEnvelope
[offset
] =
844 (FIXP_SGL
) (( (int)FDKreadBits(hBs
, start_bits_balance
)) << envDataTableCompFactor
);
847 h_frame_data
->iEnvelope
[offset
] =
848 (FIXP_SGL
) (int)FDKreadBits (hBs
, start_bits
);
852 for (i
= (1 - h_frame_data
->domain_vec
[j
]); i
< no_band
[j
]; i
++) {
854 if (h_frame_data
->domain_vec
[j
] == 0) {
855 delta
= DecodeHuffmanCW(hcb_f
, hBs
);
858 delta
= DecodeHuffmanCW(hcb_t
, hBs
);
861 h_frame_data
->iEnvelope
[offset
+ i
] = (FIXP_SGL
) (delta
<< envDataTableCompFactor
);
863 offset
+= no_band
[j
];
867 /* Convert from int to scaled fract (ENV_EXP_FRACT bits for the fractional part) */
868 for (i
= 0; i
< h_frame_data
->nScaleFactors
; i
++) {
869 h_frame_data
->iEnvelope
[i
] <<= ENV_EXP_FRACT
;
877 //static const FRAME_INFO v_frame_info1_8 = { 0, 1, {0, 8}, {1}, -1, 1, {0, 8} };
878 static const FRAME_INFO v_frame_info2_8
= { 0, 2, {0, 4, 8}, {1, 1}, -1, 2, {0, 4, 8} };
879 static const FRAME_INFO v_frame_info4_8
= { 0, 4, {0, 2, 4, 6, 8}, {1, 1, 1, 1}, -1, 2, {0, 4, 8} };
881 /***************************************************************************/
883 \brief Generates frame info for FIXFIXonly frame class used for low delay version
886 ****************************************************************************/
887 static void generateFixFixOnly ( FRAME_INFO
*hSbrFrameInfo
,
892 int nEnv
, i
, tranIdx
;
895 switch (numberTimeSlots
) {
897 pTable
= FDK_sbrDecoder_envelopeTable_8
[tranPosInternal
];
900 pTable
= FDK_sbrDecoder_envelopeTable_15
[tranPosInternal
];
903 pTable
= FDK_sbrDecoder_envelopeTable_16
[tranPosInternal
];
909 /* look number of envelopes in table */
911 /* look up envelope distribution in table */
912 for (i
=1; i
<nEnv
; i
++)
913 hSbrFrameInfo
->borders
[i
] = pTable
[i
+2];
914 /* open and close frame border */
915 hSbrFrameInfo
->borders
[0] = 0;
916 hSbrFrameInfo
->borders
[nEnv
] = numberTimeSlots
;
917 hSbrFrameInfo
->nEnvelopes
= nEnv
;
920 tranIdx
= hSbrFrameInfo
->tranEnv
= pTable
[1];
922 /* add noise floors */
923 hSbrFrameInfo
->bordersNoise
[0] = 0;
924 hSbrFrameInfo
->bordersNoise
[1] = hSbrFrameInfo
->borders
[tranIdx
?tranIdx
:1];
925 hSbrFrameInfo
->bordersNoise
[2] = numberTimeSlots
;
926 /* nEnv is always > 1, so nNoiseEnvelopes is always 2 (IEC 14496-3 4.6.19.3.2) */
927 hSbrFrameInfo
->nNoiseEnvelopes
= 2;
931 \brief Extracts LowDelaySBR control data from the bitstream.
933 \return zero for bitstream error, one for correct.
936 extractLowDelayGrid (HANDLE_FDK_BITSTREAM hBitBuf
, /*!< bitbuffer handle */
937 HANDLE_SBR_HEADER_DATA hHeaderData
,
938 HANDLE_SBR_FRAME_DATA h_frame_data
, /*!< contains the FRAME_INFO struct to be filled */
942 FRAME_INFO
* pFrameInfo
= &h_frame_data
->frameInfo
;
943 INT numberTimeSlots
= hHeaderData
->numberTimeSlots
;
946 /* FIXFIXonly framing case */
947 h_frame_data
->frameInfo
.frameClass
= 0;
949 /* get the transient position from the bitstream */
952 /* 3bit transient position (temp={0;..;7}) */
953 temp
= FDKreadBits( hBitBuf
, 3);
958 /* 4bit transient position (temp={0;..;15}) */
959 temp
= FDKreadBits( hBitBuf
, 4);
966 /* calculate borders according to the transient position */
967 generateFixFixOnly ( pFrameInfo
,
972 /* decode freq res: */
973 for (k
= 0; k
< pFrameInfo
->nEnvelopes
; k
++) {
974 pFrameInfo
->freqRes
[k
] = (UCHAR
) FDKreadBits (hBitBuf
, 1); /* f = F [1 bits] */
982 \brief Extract the frame information (structure FRAME_INFO) from the bitstream
983 \return Zero for bitstream error, one for correct.
986 extractFrameInfo ( HANDLE_FDK_BITSTREAM hBs
, /*!< bitbuffer handle */
987 HANDLE_SBR_HEADER_DATA hHeaderData
, /*!< Static control data */
988 HANDLE_SBR_FRAME_DATA h_frame_data
, /*!< pointer to memory where the frame-info will be stored */
989 const UINT nrOfChannels
,
993 FRAME_INFO
* pFrameInfo
= &h_frame_data
->frameInfo
;
994 int numberTimeSlots
= hHeaderData
->numberTimeSlots
;
995 int pointer_bits
= 0, nEnv
= 0, b
= 0, border
, i
, n
= 0,
996 k
, p
, aL
, aR
, nL
, nR
,
997 temp
= 0, staticFreqRes
;
1000 if (flags
& SBRDEC_ELD_GRID
) {
1001 /* CODEC_AACLD (LD+SBR) only uses the normal 0 Grid for non-transient Frames and the LowDelayGrid for transient Frames */
1002 frameClass
= FDKreadBits (hBs
, 1); /* frameClass = [1 bit] */
1003 if ( frameClass
== 1 ) {
1004 /* if frameClass == 1, extract LowDelaySbrGrid, otherwise extract normal SBR-Grid for FIXIFX */
1005 /* extract the AACLD-Sbr-Grid */
1006 pFrameInfo
->frameClass
= frameClass
;
1007 extractLowDelayGrid (hBs
, hHeaderData
, h_frame_data
, numberTimeSlots
);
1012 frameClass
= FDKreadBits (hBs
, 2); /* frameClass = C [2 bits] */
1016 switch (frameClass
) {
1018 temp
= FDKreadBits (hBs
, 2); /* E [2 bits ] */
1019 nEnv
= (int) (1 << temp
); /* E -> e */
1021 if ((flags
& SBRDEC_ELD_GRID
) && (nEnv
== 1))
1022 h_frame_data
->ampResolutionCurrentFrame
= FDKreadBits( hBs
, 1); /* new ELD Syntax 07-11-09 */
1024 staticFreqRes
= FDKreadBits (hBs
, 1);
1027 if (nEnv
> MAX_ENVELOPES_HEAAC
)
1034 switch (numberTimeSlots
) {
1036 FDKmemcpy (pFrameInfo
, &FDK_sbrDecoder_sbr_frame_info1_15
, sizeof(FRAME_INFO
));
1039 FDKmemcpy (pFrameInfo
, &FDK_sbrDecoder_sbr_frame_info1_16
, sizeof(FRAME_INFO
));
1046 switch (numberTimeSlots
) {
1048 FDKmemcpy (pFrameInfo
, &FDK_sbrDecoder_sbr_frame_info2_15
, sizeof(FRAME_INFO
));
1051 FDKmemcpy (pFrameInfo
, &FDK_sbrDecoder_sbr_frame_info2_16
, sizeof(FRAME_INFO
));
1058 switch (numberTimeSlots
) {
1060 FDKmemcpy (pFrameInfo
, &FDK_sbrDecoder_sbr_frame_info4_15
, sizeof(FRAME_INFO
));
1063 FDKmemcpy (pFrameInfo
, &FDK_sbrDecoder_sbr_frame_info4_16
, sizeof(FRAME_INFO
));
1070 #if (MAX_ENVELOPES >= 8)
1071 switch (numberTimeSlots
) {
1073 FDKmemcpy (pFrameInfo
, &FDK_sbrDecoder_sbr_frame_info8_15
, sizeof(FRAME_INFO
));
1076 FDKmemcpy (pFrameInfo
, &FDK_sbrDecoder_sbr_frame_info8_16
, sizeof(FRAME_INFO
));
1086 /* Apply correct freqRes (High is default) */
1087 if (!staticFreqRes
) {
1088 for (i
= 0; i
< nEnv
; i
++)
1089 pFrameInfo
->freqRes
[i
] = 0;
1095 temp
= FDKreadBits (hBs
, 2); /* A [2 bits] */
1097 n
= FDKreadBits (hBs
, 2); /* n = N [2 bits] */
1099 nEnv
= n
+ 1; /* # envelopes */
1100 b
= nEnv
+ 1; /* # borders */
1105 switch (frameClass
) {
1107 /* Decode borders: */
1108 pFrameInfo
->borders
[0] = 0; /* first border */
1109 border
= temp
+ numberTimeSlots
; /* A -> aR */
1110 i
= b
-1; /* frame info index for last border */
1111 pFrameInfo
->borders
[i
] = border
; /* last border */
1113 for (k
= 0; k
< n
; k
++) {
1114 temp
= FDKreadBits (hBs
, 2);/* R [2 bits] */
1115 border
-= (2 * temp
+ 2); /* R -> r */
1116 pFrameInfo
->borders
[--i
] = border
;
1120 /* Decode pointer: */
1121 pointer_bits
= DFRACT_BITS
- 1 - CountLeadingBits((FIXP_DBL
)(n
+1));
1122 p
= FDKreadBits (hBs
, pointer_bits
); /* p = P [pointer_bits bits] */
1127 pFrameInfo
->tranEnv
= p
? n
+ 2 - p
: -1;
1130 /* Decode freq res: */
1131 for (k
= n
; k
>= 0; k
--) {
1132 pFrameInfo
->freqRes
[k
] = FDKreadBits (hBs
, 1); /* f = F [1 bits] */
1136 /* Calculate noise floor middle border: */
1137 if (p
== 0 || p
== 1)
1138 pFrameInfo
->bordersNoise
[1] = pFrameInfo
->borders
[n
];
1140 pFrameInfo
->bordersNoise
[1] = pFrameInfo
->borders
[pFrameInfo
->tranEnv
];
1145 /* Decode borders: */
1146 border
= temp
; /* A -> aL */
1147 pFrameInfo
->borders
[0] = border
; /* first border */
1149 for (k
= 1; k
<= n
; k
++) {
1150 temp
= FDKreadBits (hBs
, 2);/* R [2 bits] */
1151 border
+= (2 * temp
+ 2); /* R -> r */
1152 pFrameInfo
->borders
[k
] = border
;
1154 pFrameInfo
->borders
[k
] = numberTimeSlots
; /* last border */
1157 /* Decode pointer: */
1158 pointer_bits
= DFRACT_BITS
- 1 - CountLeadingBits((FIXP_DBL
)(n
+1));
1159 p
= FDKreadBits (hBs
, pointer_bits
); /* p = P [pointer_bits bits] */
1163 if (p
== 0 || p
== 1)
1164 pFrameInfo
->tranEnv
= -1;
1166 pFrameInfo
->tranEnv
= p
- 1;
1170 /* Decode freq res: */
1171 for (k
= 0; k
<= n
; k
++) {
1172 pFrameInfo
->freqRes
[k
] = FDKreadBits(hBs
, 1); /* f = F [1 bits] */
1177 /* Calculate noise floor middle border: */
1180 pFrameInfo
->bordersNoise
[1] = pFrameInfo
->borders
[1];
1183 pFrameInfo
->bordersNoise
[1] = pFrameInfo
->borders
[n
];
1186 pFrameInfo
->bordersNoise
[1] = pFrameInfo
->borders
[pFrameInfo
->tranEnv
];
1193 /* v_ctrlSignal = [frameClass,aL,aR,nL,nR,v_rL,v_rR,p,v_fLR]; */
1195 aL
= FDKreadBits (hBs
, 2); /* AL [2 bits], AL -> aL */
1197 aR
= FDKreadBits (hBs
, 2) + numberTimeSlots
; /* AR [2 bits], AR -> aR */
1199 nL
= FDKreadBits (hBs
, 2); /* nL = NL [2 bits] */
1201 nR
= FDKreadBits (hBs
, 2); /* nR = NR [2 bits] */
1205 /*-------------------------------------------------------------------------
1206 Calculate help variables
1207 --------------------------------------------------------------------------*/
1210 nEnv
= nL
+ nR
+ 1; /* # envelopes */
1211 if (nEnv
> MAX_ENVELOPES
)
1213 b
= nEnv
+ 1; /* # borders */
1217 /*-------------------------------------------------------------------------
1219 --------------------------------------------------------------------------*/
1223 border
= aL
; /* first border */
1224 pFrameInfo
->borders
[0] = border
;
1226 for (k
= 1; k
<= nL
; k
++) {
1227 temp
= FDKreadBits (hBs
, 2);/* R [2 bits] */
1228 border
+= (2 * temp
+ 2); /* R -> r */
1229 pFrameInfo
->borders
[k
] = border
;
1234 border
= aR
; /* last border */
1237 pFrameInfo
->borders
[i
] = border
;
1239 for (k
= 0; k
< nR
; k
++) {
1240 temp
= FDKreadBits (hBs
, 2);/* R [2 bits] */
1241 border
-= (2 * temp
+ 2); /* R -> r */
1242 pFrameInfo
->borders
[--i
] = border
;
1246 /* decode pointer: */
1247 pointer_bits
= DFRACT_BITS
- 1 - CountLeadingBits((FIXP_DBL
)(nL
+nR
+1));
1248 p
= FDKreadBits (hBs
, pointer_bits
); /* p = P [pointer_bits bits] */
1253 pFrameInfo
->tranEnv
= p
? b
- p
: -1;
1257 /* decode freq res: */
1258 for (k
= 0; k
< nEnv
; k
++) {
1259 pFrameInfo
->freqRes
[k
] = FDKreadBits(hBs
, 1); /* f = F [1 bits] */
1264 /*-------------------------------------------------------------------------
1266 --------------------------------------------------------------------------*/
1267 pFrameInfo
->bordersNoise
[0] = aL
;
1270 /* 1 noise floor envelope: */
1271 pFrameInfo
->bordersNoise
[1] = aR
;
1274 /* 2 noise floor envelopes */
1275 if (p
== 0 || p
== 1)
1276 pFrameInfo
->bordersNoise
[1] = pFrameInfo
->borders
[nEnv
- 1];
1278 pFrameInfo
->bordersNoise
[1] = pFrameInfo
->borders
[pFrameInfo
->tranEnv
];
1279 pFrameInfo
->bordersNoise
[2] = aR
;
1286 Store number of envelopes, noise floor envelopes and frame class
1288 pFrameInfo
->nEnvelopes
= nEnv
;
1291 pFrameInfo
->nNoiseEnvelopes
= 1;
1293 pFrameInfo
->nNoiseEnvelopes
= 2;
1295 pFrameInfo
->frameClass
= frameClass
;
1297 if (pFrameInfo
->frameClass
== 2 || pFrameInfo
->frameClass
== 1) {
1298 /* calculate noise floor first and last borders: */
1299 pFrameInfo
->bordersNoise
[0] = pFrameInfo
->borders
[0];
1300 pFrameInfo
->bordersNoise
[pFrameInfo
->nNoiseEnvelopes
] = pFrameInfo
->borders
[nEnv
];
1309 \brief Check if the frameInfo vector has reasonable values.
1310 \return Zero for error, one for correct
1313 checkFrameInfo (FRAME_INFO
* pFrameInfo
, /*!< pointer to frameInfo */
1314 int numberOfTimeSlots
, /*!< QMF time slots per frame */
1315 int overlap
, /*!< Amount of overlap QMF time slots */
1316 int timeStep
) /*!< QMF slots to SBR slots step factor */
1324 int nEnvelopes
= pFrameInfo
->nEnvelopes
;
1325 int nNoiseEnvelopes
= pFrameInfo
->nNoiseEnvelopes
;
1327 if(nEnvelopes
< 1 || nEnvelopes
> MAX_ENVELOPES
)
1330 if(nNoiseEnvelopes
> MAX_NOISE_ENVELOPES
)
1333 startPos
= pFrameInfo
->borders
[0];
1334 stopPos
= pFrameInfo
->borders
[nEnvelopes
];
1335 tranEnv
= pFrameInfo
->tranEnv
;
1336 startPosNoise
= pFrameInfo
->bordersNoise
[0];
1337 stopPosNoise
= pFrameInfo
->bordersNoise
[nNoiseEnvelopes
];
1339 if (overlap
< 0 || overlap
> (6)) {
1342 if (timeStep
< 1 || timeStep
> 2) {
1345 maxPos
= numberOfTimeSlots
+ (overlap
/timeStep
);
1347 /* Check that the start and stop positions of the frame are reasonable values. */
1348 if( (startPos
< 0) || (startPos
>= stopPos
) )
1350 if( startPos
> maxPos
-numberOfTimeSlots
) /* First env. must start in or directly after the overlap buffer */
1352 if( stopPos
< numberOfTimeSlots
) /* One complete frame must be ready for output after processing */
1354 if(stopPos
> maxPos
)
1357 /* Check that the start border for every envelope is strictly later in time */
1358 for(i
=0;i
<nEnvelopes
;i
++) {
1359 if(pFrameInfo
->borders
[i
] >= pFrameInfo
->borders
[i
+1])
1363 /* Check that the envelope to be shortened is actually among the envelopes */
1364 if(tranEnv
>nEnvelopes
)
1368 /* Check the noise borders */
1369 if(nEnvelopes
==1 && nNoiseEnvelopes
>1)
1372 if(startPos
!= startPosNoise
|| stopPos
!= stopPosNoise
)
1376 /* Check that the start border for every noise-envelope is strictly later in time*/
1377 for(i
=0; i
<nNoiseEnvelopes
; i
++) {
1378 if(pFrameInfo
->bordersNoise
[i
] >= pFrameInfo
->bordersNoise
[i
+1])
1382 /* Check that every noise border is the same as an envelope border*/
1383 for(i
=0; i
<nNoiseEnvelopes
; i
++) {
1384 startPosNoise
= pFrameInfo
->bordersNoise
[i
];
1386 for(j
=0; j
<nEnvelopes
; j
++) {
1387 if(pFrameInfo
->borders
[j
] == startPosNoise
)