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 ----------------------------------------------------------------------------------------------------------- */
87 This module provides the actual decoder implementation. The SBR data (side information) is already
88 decoded. Only three functions are provided:
90 \li 1.) createSbrDec(): One time initialization
91 \li 2.) resetSbrDec(): Called by sbr_Apply() when the information contained in an SBR_HEADER_ELEMENT requires a reset
92 and recalculation of important SBR structures.
93 \li 3.) sbr_dec(): The actual decoder. Calls the different tools such as filterbanks, lppTransposer(), and calculateSbrEnvelope()
94 [the envelope adjuster].
96 \sa sbr_dec(), \ref documentationOverview
102 #include "env_extr.h"
103 #include "env_calc.h"
106 #include "genericStds.h"
108 #include "sbrdec_drc.h"
112 static void assignLcTimeSlots( HANDLE_SBR_DEC hSbrDec
, /*!< handle to Decoder channel */
113 FIXP_DBL
**QmfBufferReal
,
119 /* Number of QMF timeslots in the overlap buffer: */
120 ptr
= hSbrDec
->pSbrOverlapBuffer
;
121 for(slot
=0; slot
<hSbrDec
->LppTrans
.pSettings
->overlap
; slot
++) {
122 QmfBufferReal
[slot
] = ptr
; ptr
+= (64);
125 /* Assign timeslots to Workbuffer1 */
126 ptr
= hSbrDec
->WorkBuffer1
;
127 for(i
=0; i
<noCols
; i
++) {
128 QmfBufferReal
[slot
] = ptr
; ptr
+= (64);
134 static void assignHqTimeSlots( HANDLE_SBR_DEC hSbrDec
, /*!< handle to Decoder channel */
135 FIXP_DBL
**QmfBufferReal
,
136 FIXP_DBL
**QmfBufferImag
,
142 /* Number of QMF timeslots in one half of a frame (size of Workbuffer1 or 2): */
143 int halflen
= (noCols
>> 1) + hSbrDec
->LppTrans
.pSettings
->overlap
;
144 int totCols
= noCols
+ hSbrDec
->LppTrans
.pSettings
->overlap
;
146 /* Number of QMF timeslots in the overlap buffer: */
147 ptr
= hSbrDec
->pSbrOverlapBuffer
;
148 for(slot
=0; slot
<hSbrDec
->LppTrans
.pSettings
->overlap
; slot
++) {
149 QmfBufferReal
[slot
] = ptr
; ptr
+= (64);
150 QmfBufferImag
[slot
] = ptr
; ptr
+= (64);
153 /* Assign first half of timeslots to Workbuffer1 */
154 ptr
= hSbrDec
->WorkBuffer1
;
155 for(; slot
<halflen
; slot
++) {
156 QmfBufferReal
[slot
] = ptr
; ptr
+= (64);
157 QmfBufferImag
[slot
] = ptr
; ptr
+= (64);
160 /* Assign second half of timeslots to Workbuffer2 */
161 ptr
= hSbrDec
->WorkBuffer2
;
162 for(; slot
<totCols
; slot
++) {
163 QmfBufferReal
[slot
] = ptr
; ptr
+= (64);
164 QmfBufferImag
[slot
] = ptr
; ptr
+= (64);
169 static void assignTimeSlots( HANDLE_SBR_DEC hSbrDec
, /*!< handle to Decoder channel */
173 /* assign qmf time slots */
174 hSbrDec
->useLP
= useLP
;
176 hSbrDec
->SynthesisQMF
.flags
|= QMF_FLAG_LP
;
177 hSbrDec
->AnalysiscQMF
.flags
|= QMF_FLAG_LP
;
179 hSbrDec
->SynthesisQMF
.flags
&= ~QMF_FLAG_LP
;
180 hSbrDec
->AnalysiscQMF
.flags
&= ~QMF_FLAG_LP
;
183 assignHqTimeSlots( hSbrDec
, hSbrDec
->QmfBufferReal
, hSbrDec
->QmfBufferImag
, noCols
);
186 assignLcTimeSlots( hSbrDec
, hSbrDec
->QmfBufferReal
, noCols
);
190 static void changeQmfType( HANDLE_SBR_DEC hSbrDec
, /*!< handle to Decoder channel */
193 UINT synQmfFlags
= hSbrDec
->SynthesisQMF
.flags
;
194 UINT anaQmfFlags
= hSbrDec
->AnalysiscQMF
.flags
;
198 /* assign qmf type */
199 if (useLdTimeAlign
) {
200 if (synQmfFlags
& QMF_FLAG_CLDFB
) {
201 /* change the type to MPSLD */
202 synQmfFlags
&= ~QMF_FLAG_CLDFB
;
203 synQmfFlags
|= QMF_FLAG_MPSLDFB
;
206 if (anaQmfFlags
& QMF_FLAG_CLDFB
) {
207 /* change the type to MPSLD */
208 anaQmfFlags
&= ~QMF_FLAG_CLDFB
;
209 anaQmfFlags
|= QMF_FLAG_MPSLDFB
;
213 if (synQmfFlags
& QMF_FLAG_MPSLDFB
) {
214 /* change the type to CLDFB */
215 synQmfFlags
&= ~QMF_FLAG_MPSLDFB
;
216 synQmfFlags
|= QMF_FLAG_CLDFB
;
219 if (anaQmfFlags
& QMF_FLAG_MPSLDFB
) {
220 /* change the type to CLDFB */
221 anaQmfFlags
&= ~QMF_FLAG_MPSLDFB
;
222 anaQmfFlags
|= QMF_FLAG_CLDFB
;
228 int qmfErr
= qmfInitAnalysisFilterBank (
229 &hSbrDec
->AnalysiscQMF
,
230 hSbrDec
->anaQmfStates
,
231 hSbrDec
->AnalysiscQMF
.no_col
,
232 hSbrDec
->AnalysiscQMF
.lsb
,
233 hSbrDec
->AnalysiscQMF
.usb
,
234 hSbrDec
->AnalysiscQMF
.no_channels
,
235 anaQmfFlags
| QMF_FLAG_KEEP_STATES
243 int qmfErr
= qmfInitSynthesisFilterBank (
244 &hSbrDec
->SynthesisQMF
,
245 hSbrDec
->pSynQmfStates
,
246 hSbrDec
->SynthesisQMF
.no_col
,
247 hSbrDec
->SynthesisQMF
.lsb
,
248 hSbrDec
->SynthesisQMF
.usb
,
249 hSbrDec
->SynthesisQMF
.no_channels
,
250 synQmfFlags
| QMF_FLAG_KEEP_STATES
261 \brief SBR decoder core function for one channel
263 \image html BufferMgmtDetailed-1632.png
265 Besides the filter states of the QMF filter bank and the LPC-states of
266 the LPP-Transposer, processing is mainly based on four buffers:
267 #timeIn, #timeOut, #WorkBuffer2 and #OverlapBuffer. The #WorkBuffer2
268 is reused for all channels and might be used by the core decoder, a
269 static overlap buffer is required for each channel. Du to in-place
270 processing, #timeIn and #timeOut point to identical locations.
272 The spectral data is organized in so-called slots, each slot
273 containing 64 bands of complex data. The number of slots per frame is
274 dependend on the frame size. For mp3PRO, there are 18 slots per frame
275 and 6 slots per #OverlapBuffer. It is not necessary to have the slots
276 in located consecutive address ranges.
278 To optimize memory usage and to minimize the number of memory
279 accesses, the memory management is organized as follows (Slot numbers
282 1.) Input time domain signal is located in #timeIn, the last slots
283 (0..5) of the spectral data of the previous frame are located in the
284 #OverlapBuffer. In addition, #frameData of the current frame resides
285 in the upper part of #timeIn.
287 2.) During the cplxAnalysisQmfFiltering(), 32 samples from #timeIn are transformed
288 into a slot of up to 32 complex spectral low band values at a
289 time. The first spectral slot -- nr. 6 -- is written at slot number
290 zero of #WorkBuffer2. #WorkBuffer2 will be completely filled with
293 3.) LPP-Transposition in lppTransposer() is processed on 24 slots. During the
294 transposition, the high band part of the spectral data is replicated
295 based on the low band data.
297 Envelope Adjustment is processed on the high band part of the spectral
298 data only by calculateSbrEnvelope().
300 4.) The cplxSynthesisQmfFiltering() creates 64 time domain samples out
301 of a slot of 64 complex spectral values at a time. The first 6 slots
302 in #timeOut are filled from the results of spectral slots 0..5 in the
303 #OverlapBuffer. The consecutive slots in timeOut are now filled with
304 the results of spectral slots 6..17.
306 5.) The preprocessed slots 18..23 have to be stored in the
312 sbr_dec ( HANDLE_SBR_DEC hSbrDec
, /*!< handle to Decoder channel */
313 INT_PCM
*timeIn
, /*!< pointer to input time signal */
314 INT_PCM
*timeOut
, /*!< pointer to output time signal */
315 HANDLE_SBR_DEC hSbrDecRight
, /*!< handle to Decoder channel right */
316 INT_PCM
*timeOutRight
, /*!< pointer to output time signal */
317 const int strideIn
, /*!< Time data traversal strideIn */
318 const int strideOut
, /*!< Time data traversal strideOut */
319 HANDLE_SBR_HEADER_DATA hHeaderData
,/*!< Static control data */
320 HANDLE_SBR_FRAME_DATA hFrameData
, /*!< Control data of current frame */
321 HANDLE_SBR_PREV_FRAME_DATA hPrevFrameData
, /*!< Some control data of last frame */
322 const int applyProcessing
, /*!< Flag for SBR operation */
323 HANDLE_PS_DEC h_ps_d
,
327 int i
, slot
, reserve
;
333 /* 1+1/3 frames of spectral data: */
334 FIXP_DBL
**QmfBufferReal
= hSbrDec
->QmfBufferReal
;
335 FIXP_DBL
**QmfBufferImag
= hSbrDec
->QmfBufferImag
;
337 /* Number of QMF timeslots in the overlap buffer: */
338 ov_len
= hSbrDec
->LppTrans
.pSettings
->overlap
;
340 /* Number of QMF slots per frame */
341 int noCols
= hHeaderData
->numberTimeSlots
* hHeaderData
->timeStep
;
343 /* assign qmf time slots */
344 if ( ((flags
& SBRDEC_LOW_POWER
) ? 1 : 0) != ((hSbrDec
->SynthesisQMF
.flags
& QMF_FLAG_LP
) ? 1 : 0) ) {
345 assignTimeSlots( hSbrDec
, hHeaderData
->numberTimeSlots
* hHeaderData
->timeStep
, flags
& SBRDEC_LOW_POWER
);
348 if (flags
& SBRDEC_ELD_GRID
) {
349 /* Choose the right low delay filter bank */
350 changeQmfType( hSbrDec
, (flags
& SBRDEC_LD_MPS_QMF
) ? 1 : 0 );
354 low band codec signal subband filtering
358 C_AALLOC_SCRATCH_START(qmfTemp
, FIXP_DBL
, 2*(64));
360 qmfAnalysisFiltering( &hSbrDec
->AnalysiscQMF
,
361 QmfBufferReal
+ ov_len
,
362 QmfBufferImag
+ ov_len
,
363 &hSbrDec
->sbrScaleFactor
,
369 C_AALLOC_SCRATCH_END(qmfTemp
, FIXP_DBL
, 2*(64));
373 Clear upper half of spectrum
376 int nAnalysisBands
= hHeaderData
->numberOfAnalysisBands
;
378 if (! (flags
& SBRDEC_LOW_POWER
)) {
379 for (slot
= ov_len
; slot
< noCols
+ov_len
; slot
++) {
380 FDKmemclear(&QmfBufferReal
[slot
][nAnalysisBands
],((64)-nAnalysisBands
)*sizeof(FIXP_DBL
));
381 FDKmemclear(&QmfBufferImag
[slot
][nAnalysisBands
],((64)-nAnalysisBands
)*sizeof(FIXP_DBL
));
384 for (slot
= ov_len
; slot
< noCols
+ov_len
; slot
++) {
385 FDKmemclear(&QmfBufferReal
[slot
][nAnalysisBands
],((64)-nAnalysisBands
)*sizeof(FIXP_DBL
));
392 Shift spectral data left to gain accuracy in transposer and adjustor
394 maxVal
= maxSubbandSample( QmfBufferReal
,
395 (flags
& SBRDEC_LOW_POWER
) ? NULL
: QmfBufferImag
,
397 hSbrDec
->AnalysiscQMF
.lsb
,
401 reserve
= fixMax(0,CntLeadingZeros(maxVal
)-1) ;
402 reserve
= fixMin(reserve
,DFRACT_BITS
-1-hSbrDec
->sbrScaleFactor
.lb_scale
);
404 /* If all data is zero, lb_scale could become too large */
405 rescaleSubbandSamples( QmfBufferReal
,
406 (flags
& SBRDEC_LOW_POWER
) ? NULL
: QmfBufferImag
,
408 hSbrDec
->AnalysiscQMF
.lsb
,
413 hSbrDec
->sbrScaleFactor
.lb_scale
+= reserve
;
416 save low band scale, wavecoding or parametric stereo may modify it
418 saveLbScale
= hSbrDec
->sbrScaleFactor
.lb_scale
;
423 UCHAR
* borders
= hFrameData
->frameInfo
.borders
;
424 lastSlotOffs
= borders
[hFrameData
->frameInfo
.nEnvelopes
] - hHeaderData
->numberTimeSlots
;
426 FIXP_DBL degreeAlias
[(64)];
428 /* The transposer will override most values in degreeAlias[].
429 The array needs to be cleared at least from lowSubband to highSubband before. */
430 if (flags
& SBRDEC_LOW_POWER
)
431 FDKmemclear(°reeAlias
[hHeaderData
->freqBandData
.lowSubband
], (hHeaderData
->freqBandData
.highSubband
-hHeaderData
->freqBandData
.lowSubband
)*sizeof(FIXP_DBL
));
434 Inverse filtering of lowband and transposition into the SBR-frequency range
437 lppTransposer ( &hSbrDec
->LppTrans
,
438 &hSbrDec
->sbrScaleFactor
,
440 degreeAlias
, // only used if useLP = 1
442 flags
& SBRDEC_LOW_POWER
,
443 hHeaderData
->timeStep
,
446 hHeaderData
->freqBandData
.nInvfBands
,
447 hFrameData
->sbr_invf_mode
,
448 hPrevFrameData
->sbr_invf_mode
);
455 Adjust envelope of current frame.
458 calculateSbrEnvelope (&hSbrDec
->sbrScaleFactor
,
459 &hSbrDec
->SbrCalculateEnvelope
,
464 flags
& SBRDEC_LOW_POWER
,
468 (hHeaderData
->frameErrorFlag
|| hPrevFrameData
->frameErrorFlag
));
472 Update hPrevFrameData (to be used in the next frame)
474 for (i
=0; i
<hHeaderData
->freqBandData
.nInvfBands
; i
++) {
475 hPrevFrameData
->sbr_invf_mode
[i
] = hFrameData
->sbr_invf_mode
[i
];
477 hPrevFrameData
->coupling
= hFrameData
->coupling
;
478 hPrevFrameData
->stopPos
= borders
[hFrameData
->frameInfo
.nEnvelopes
];
479 hPrevFrameData
->ampRes
= hFrameData
->ampResolutionCurrentFrame
;
482 /* Reset hb_scale if no highband is present, because hb_scale is considered in the QMF-synthesis */
483 hSbrDec
->sbrScaleFactor
.hb_scale
= saveLbScale
;
487 for (i
=0; i
<LPC_ORDER
; i
++){
489 Store the unmodified qmf Slots values (required for LPC filtering)
491 if (! (flags
& SBRDEC_LOW_POWER
)) {
492 FDKmemcpy(hSbrDec
->LppTrans
.lpcFilterStatesReal
[i
], QmfBufferReal
[noCols
-LPC_ORDER
+i
], hSbrDec
->AnalysiscQMF
.lsb
*sizeof(FIXP_DBL
));
493 FDKmemcpy(hSbrDec
->LppTrans
.lpcFilterStatesImag
[i
], QmfBufferImag
[noCols
-LPC_ORDER
+i
], hSbrDec
->AnalysiscQMF
.lsb
*sizeof(FIXP_DBL
));
495 FDKmemcpy(hSbrDec
->LppTrans
.lpcFilterStatesReal
[i
], QmfBufferReal
[noCols
-LPC_ORDER
+i
], hSbrDec
->AnalysiscQMF
.lsb
*sizeof(FIXP_DBL
));
499 Synthesis subband filtering.
502 if ( ! (flags
& SBRDEC_PS_DECODED
) ) {
505 int outScalefactor
= 0;
507 if (h_ps_d
!= NULL
) {
508 h_ps_d
->procFrameBased
= 1; /* we here do frame based processing */
512 sbrDecoder_drcApply(&hSbrDec
->sbrDrcChannel
,
514 (flags
& SBRDEC_LOW_POWER
) ? NULL
: QmfBufferImag
,
515 hSbrDec
->SynthesisQMF
.no_col
,
521 qmfChangeOutScalefactor(&hSbrDec
->SynthesisQMF
, outScalefactor
);
524 C_AALLOC_SCRATCH_START(qmfTemp
, FIXP_DBL
, 2*(64));
526 qmfSynthesisFiltering( &hSbrDec
->SynthesisQMF
,
528 (flags
& SBRDEC_LOW_POWER
) ? NULL
: QmfBufferImag
,
529 &hSbrDec
->sbrScaleFactor
,
530 hSbrDec
->LppTrans
.pSettings
->overlap
,
535 C_AALLOC_SCRATCH_END(qmfTemp
, FIXP_DBL
, 2*(64));
540 } else { /* (flags & SBRDEC_PS_DECODED) */
541 INT i
, sdiff
, outScalefactor
, scaleFactorLowBand
, scaleFactorHighBand
;
542 SCHAR scaleFactorLowBand_ov
, scaleFactorLowBand_no_ov
;
544 HANDLE_QMF_FILTER_BANK synQmf
= &hSbrDec
->SynthesisQMF
;
545 HANDLE_QMF_FILTER_BANK synQmfRight
= &hSbrDecRight
->SynthesisQMF
;
548 sdiff
= hSbrDec
->sbrScaleFactor
.lb_scale
- reserve
; /* Scaling difference */
549 scaleFactorHighBand
= sdiff
- hSbrDec
->sbrScaleFactor
.hb_scale
; /* Scale of current high band */
550 scaleFactorLowBand_ov
= sdiff
- hSbrDec
->sbrScaleFactor
.ov_lb_scale
; /* Scale of low band overlapping QMF data */
551 scaleFactorLowBand_no_ov
= sdiff
- hSbrDec
->sbrScaleFactor
.lb_scale
; /* Scale of low band current QMF data */
552 outScalefactor
= 0; /* Initial output scale */
554 if (h_ps_d
->procFrameBased
== 1) /* If we have switched from frame to slot based processing copy filter states */
555 { /* procFrameBased will be unset later */
556 /* copy filter states from left to right */
557 FDKmemcpy(synQmfRight
->FilterStates
, synQmf
->FilterStates
, ((640)-(64))*sizeof(FIXP_QSS
));
560 /* scale ALL qmf vales ( real and imag ) of mono / left channel to the
561 same scale factor ( ov_lb_sf, lb_sf and hq_sf ) */
562 scalFilterBankValues( h_ps_d
, /* parametric stereo decoder handle */
563 QmfBufferReal
, /* qmf filterbank values */
564 QmfBufferImag
, /* qmf filterbank values */
565 synQmf
->lsb
, /* sbr start subband */
566 hSbrDec
->sbrScaleFactor
.ov_lb_scale
,
567 hSbrDec
->sbrScaleFactor
.lb_scale
,
568 &scaleFactorLowBand_ov
, /* adapt scaling values */
569 &scaleFactorLowBand_no_ov
, /* adapt scaling values */
570 hSbrDec
->sbrScaleFactor
.hb_scale
, /* current frame ( highband ) */
571 &scaleFactorHighBand
,
574 /* use the same synthese qmf values for left and right channel */
575 synQmfRight
->no_col
= synQmf
->no_col
;
576 synQmfRight
->lsb
= synQmf
->lsb
;
577 synQmfRight
->usb
= synQmf
->usb
;
581 outScalefactor
+= (SCAL_HEADROOM
+1); /* psDiffScale! */
584 C_AALLOC_SCRATCH_START(pWorkBuffer
, FIXP_DBL
, 2*(64));
588 if (hSbrDec
->sbrDrcChannel
.enable
!= 0) {
589 if (hSbrDec
->sbrDrcChannel
.prevFact_exp
> maxShift
) {
590 maxShift
= hSbrDec
->sbrDrcChannel
.prevFact_exp
;
592 if (hSbrDec
->sbrDrcChannel
.currFact_exp
> maxShift
) {
593 maxShift
= hSbrDec
->sbrDrcChannel
.currFact_exp
;
595 if (hSbrDec
->sbrDrcChannel
.nextFact_exp
> maxShift
) {
596 maxShift
= hSbrDec
->sbrDrcChannel
.nextFact_exp
;
600 /* copy DRC data to right channel (with PS both channels use the same DRC gains) */
601 FDKmemcpy(&hSbrDecRight
->sbrDrcChannel
, &hSbrDec
->sbrDrcChannel
, sizeof(SBRDEC_DRC_CHANNEL
));
603 for (i
= 0; i
< synQmf
->no_col
; i
++) { /* ----- no_col loop ----- */
605 INT outScalefactorR
, outScalefactorL
;
606 outScalefactorR
= outScalefactorL
= outScalefactor
;
608 /* qmf timeslot of right channel */
609 FIXP_DBL
* rQmfReal
= pWorkBuffer
;
610 FIXP_DBL
* rQmfImag
= pWorkBuffer
+ 64;
614 if ( i
== h_ps_d
->bsData
[h_ps_d
->processSlot
].mpeg
.aEnvStartStop
[env
] ) {
615 initSlotBasedRotation( h_ps_d
, env
, hHeaderData
->freqBandData
.highSubband
);
619 ApplyPsSlot( h_ps_d
, /* parametric stereo decoder handle */
620 (QmfBufferReal
+ i
), /* one timeslot of left/mono channel */
621 (QmfBufferImag
+ i
), /* one timeslot of left/mono channel */
622 rQmfReal
, /* one timeslot or right channel */
623 rQmfImag
); /* one timeslot or right channel */
627 scaleFactorLowBand
= (i
<(6)) ? scaleFactorLowBand_ov
: scaleFactorLowBand_no_ov
;
630 sbrDecoder_drcApplySlot ( /* right channel */
631 &hSbrDecRight
->sbrDrcChannel
,
639 outScalefactorR
+= maxShift
;
641 sbrDecoder_drcApplySlot ( /* left channel */
642 &hSbrDec
->sbrDrcChannel
,
643 *(QmfBufferReal
+ i
),
644 *(QmfBufferImag
+ i
),
650 outScalefactorL
+= maxShift
;
653 /* scale filter states for left and right channel */
654 qmfChangeOutScalefactor( synQmf
, outScalefactorL
);
655 qmfChangeOutScalefactor( synQmfRight
, outScalefactorR
);
659 qmfSynthesisFilteringSlot( synQmfRight
,
660 rQmfReal
, /* QMF real buffer */
661 rQmfImag
, /* QMF imag buffer */
664 timeOutRight
+(i
*synQmf
->no_channels
*strideOut
),
668 qmfSynthesisFilteringSlot( synQmf
,
669 *(QmfBufferReal
+ i
), /* QMF real buffer */
670 *(QmfBufferImag
+ i
), /* QMF imag buffer */
673 timeOut
+(i
*synQmf
->no_channels
*strideOut
),
678 } /* no_col loop i */
680 /* scale back (6) timeslots look ahead for hybrid filterbank to original value */
681 rescalFilterBankValues( h_ps_d
,
687 C_AALLOC_SCRATCH_END(pWorkBuffer
, FIXP_DBL
, 2*(64));
691 sbrDecoder_drcUpdateChannel( &hSbrDec
->sbrDrcChannel
);
695 Update overlap buffer
696 Even bands above usb are copied to avoid outdated spectral data in case
697 the stop frequency raises.
700 if (hSbrDec
->LppTrans
.pSettings
->overlap
> 0)
702 if (! (flags
& SBRDEC_LOW_POWER
)) {
703 for ( i
=0; i
<hSbrDec
->LppTrans
.pSettings
->overlap
; i
++ ) {
704 FDKmemcpy(QmfBufferReal
[i
], QmfBufferReal
[i
+noCols
], (64)*sizeof(FIXP_DBL
));
705 FDKmemcpy(QmfBufferImag
[i
], QmfBufferImag
[i
+noCols
], (64)*sizeof(FIXP_DBL
));
708 for ( i
=0; i
<hSbrDec
->LppTrans
.pSettings
->overlap
; i
++ ) {
709 FDKmemcpy(QmfBufferReal
[i
], QmfBufferReal
[i
+noCols
], (64)*sizeof(FIXP_DBL
));
713 hSbrDec
->sbrScaleFactor
.ov_lb_scale
= saveLbScale
;
715 /* Save current frame status */
716 hPrevFrameData
->frameErrorFlag
= hHeaderData
->frameErrorFlag
;
722 \brief Creates sbr decoder structure
723 \return errorCode, 0 if successful
726 createSbrDec (SBR_CHANNEL
* hSbrChannel
,
727 HANDLE_SBR_HEADER_DATA hHeaderData
, /*!< Static control data */
728 TRANSPOSER_SETTINGS
*pSettings
,
729 const int downsampleFac
, /*!< Downsampling factor */
730 const UINT qmfFlags
, /*!< flags -> 1: HQ/LP selector, 2: CLDFB */
733 int chan
) /*!< Channel for which to assign buffers etc. */
736 SBR_ERROR err
= SBRDEC_OK
;
737 int timeSlots
= hHeaderData
->numberTimeSlots
; /* Number of SBR slots per frame */
738 int noCols
= timeSlots
* hHeaderData
->timeStep
; /* Number of QMF slots per frame */
739 HANDLE_SBR_DEC hs
= &(hSbrChannel
->SbrDec
);
741 /* Initialize scale factors */
742 hs
->sbrScaleFactor
.ov_lb_scale
= 0;
743 hs
->sbrScaleFactor
.ov_hb_scale
= 0;
744 hs
->sbrScaleFactor
.hb_scale
= 0;
748 create envelope calculator
750 err
= createSbrEnvelopeCalc (&hs
->SbrCalculateEnvelope
,
754 if (err
!= SBRDEC_OK
) {
759 create QMF filter banks
763 /* Adapted QMF analysis post-twiddles for down-sampled HQ SBR */
764 const UINT downSampledFlag
= (downsampleFac
==2) ? QMF_FLAG_DOWNSAMPLED
: 0;
766 qmfErr
= qmfInitAnalysisFilterBank (
770 hHeaderData
->freqBandData
.lowSubband
,
771 hHeaderData
->freqBandData
.highSubband
,
772 hHeaderData
->numberOfAnalysisBands
,
773 (qmfFlags
& (~QMF_FLAG_KEEP_STATES
)) | downSampledFlag
776 return SBRDEC_UNSUPPORTED_CONFIG
;
779 if (hs
->pSynQmfStates
== NULL
) {
780 hs
->pSynQmfStates
= GetRam_sbr_QmfStatesSynthesis(chan
);
781 if (hs
->pSynQmfStates
== NULL
)
782 return SBRDEC_MEM_ALLOC_FAILED
;
788 qmfErr
= qmfInitSynthesisFilterBank (
792 hHeaderData
->freqBandData
.lowSubband
,
793 hHeaderData
->freqBandData
.highSubband
,
794 (64) / downsampleFac
,
795 qmfFlags
& (~QMF_FLAG_KEEP_STATES
)
799 return SBRDEC_UNSUPPORTED_CONFIG
;
802 initSbrPrevFrameData (&hSbrChannel
->prevFrameData
, timeSlots
);
807 err
= createLppTransposer (&hs
->LppTrans
,
809 hHeaderData
->freqBandData
.lowSubband
,
810 hHeaderData
->freqBandData
.v_k_master
,
811 hHeaderData
->freqBandData
.numMaster
,
812 hs
->SynthesisQMF
.usb
,
814 hs
->AnalysiscQMF
.no_col
,
815 hHeaderData
->freqBandData
.freqBandTableNoise
,
816 hHeaderData
->freqBandData
.nNfb
,
817 hHeaderData
->sbrProcSmplRate
,
820 if (err
!= SBRDEC_OK
) {
824 /* The CLDFB does not have overlap */
825 if ((qmfFlags
& QMF_FLAG_CLDFB
) == 0) {
826 if (hs
->pSbrOverlapBuffer
== NULL
) {
827 hs
->pSbrOverlapBuffer
= GetRam_sbr_OverlapBuffer(chan
);
828 if (hs
->pSbrOverlapBuffer
== NULL
) {
829 return SBRDEC_MEM_ALLOC_FAILED
;
832 /* Clear overlap buffer */
833 FDKmemclear( hs
->pSbrOverlapBuffer
,
834 sizeof(FIXP_DBL
) * 2 * (6) * (64)
839 /* assign qmf time slots */
840 assignTimeSlots( &hSbrChannel
->SbrDec
, hHeaderData
->numberTimeSlots
* hHeaderData
->timeStep
, qmfFlags
& QMF_FLAG_LP
);
846 \brief Delete sbr decoder structure
847 \return errorCode, 0 if successful
850 deleteSbrDec (SBR_CHANNEL
* hSbrChannel
)
852 HANDLE_SBR_DEC hs
= &hSbrChannel
->SbrDec
;
854 deleteSbrEnvelopeCalc (&hs
->SbrCalculateEnvelope
);
856 /* delete QMF filter states */
857 if (hs
->pSynQmfStates
!= NULL
) {
858 FreeRam_sbr_QmfStatesSynthesis(&hs
->pSynQmfStates
);
862 if (hs
->pSbrOverlapBuffer
!= NULL
) {
863 FreeRam_sbr_OverlapBuffer(&hs
->pSbrOverlapBuffer
);
871 \brief resets sbr decoder structure
872 \return errorCode, 0 if successful
875 resetSbrDec (HANDLE_SBR_DEC hSbrDec
,
876 HANDLE_SBR_HEADER_DATA hHeaderData
,
877 HANDLE_SBR_PREV_FRAME_DATA hPrevFrameData
,
879 const int downsampleFac
882 SBR_ERROR sbrError
= SBRDEC_OK
;
884 int old_lsb
= hSbrDec
->SynthesisQMF
.lsb
;
885 int new_lsb
= hHeaderData
->freqBandData
.lowSubband
;
886 int l
, startBand
, stopBand
, startSlot
, size
;
888 int source_scale
, target_scale
, delta_scale
, target_lsb
, target_usb
, reserve
;
891 /* overlapBuffer point to first (6) slots */
892 FIXP_DBL
**OverlapBufferReal
= hSbrDec
->QmfBufferReal
;
893 FIXP_DBL
**OverlapBufferImag
= hSbrDec
->QmfBufferImag
;
895 /* assign qmf time slots */
896 assignTimeSlots( hSbrDec
, hHeaderData
->numberTimeSlots
* hHeaderData
->timeStep
, useLP
);
900 resetSbrEnvelopeCalc (&hSbrDec
->SbrCalculateEnvelope
);
902 hSbrDec
->SynthesisQMF
.lsb
= hHeaderData
->freqBandData
.lowSubband
;
903 hSbrDec
->SynthesisQMF
.usb
= fixMin((INT
)hSbrDec
->SynthesisQMF
.no_channels
, (INT
)hHeaderData
->freqBandData
.highSubband
);
905 hSbrDec
->AnalysiscQMF
.lsb
= hSbrDec
->SynthesisQMF
.lsb
;
906 hSbrDec
->AnalysiscQMF
.usb
= hSbrDec
->SynthesisQMF
.usb
;
910 The following initialization of spectral data in the overlap buffer
911 is required for dynamic x-over or a change of the start-freq for 2 reasons:
913 1. If the lowband gets _wider_, unadjusted data would remain
915 2. If the lowband becomes _smaller_, the highest bands of the old lowband
916 must be cleared because the whitening would be affected
920 startSlot
= hHeaderData
->timeStep
* (hPrevFrameData
->stopPos
- hHeaderData
->numberTimeSlots
);
921 size
= fixMax(0,stopBand
-startBand
);
923 /* keep already adjusted data in the x-over-area */
925 for (l
=startSlot
; l
<hSbrDec
->LppTrans
.pSettings
->overlap
; l
++) {
926 FDKmemclear(&OverlapBufferReal
[l
][startBand
], size
*sizeof(FIXP_DBL
));
927 FDKmemclear(&OverlapBufferImag
[l
][startBand
], size
*sizeof(FIXP_DBL
));
930 for (l
=startSlot
; l
<hSbrDec
->LppTrans
.pSettings
->overlap
; l
++) {
931 FDKmemclear(&OverlapBufferReal
[l
][startBand
], size
*sizeof(FIXP_DBL
));
936 reset LPC filter states
938 startBand
= fixMin(old_lsb
,new_lsb
);
939 stopBand
= fixMax(old_lsb
,new_lsb
);
940 size
= fixMax(0,stopBand
-startBand
);
942 FDKmemclear(&hSbrDec
->LppTrans
.lpcFilterStatesReal
[0][startBand
], size
*sizeof(FIXP_DBL
));
943 FDKmemclear(&hSbrDec
->LppTrans
.lpcFilterStatesReal
[1][startBand
], size
*sizeof(FIXP_DBL
));
945 FDKmemclear(&hSbrDec
->LppTrans
.lpcFilterStatesImag
[0][startBand
], size
*sizeof(FIXP_DBL
));
946 FDKmemclear(&hSbrDec
->LppTrans
.lpcFilterStatesImag
[1][startBand
], size
*sizeof(FIXP_DBL
));
951 Rescale already processed spectral data between old and new x-over frequency.
952 This must be done because of the separate scalefactors for lowband and highband.
954 startBand
= fixMin(old_lsb
,new_lsb
);
955 stopBand
= fixMax(old_lsb
,new_lsb
);
957 if (new_lsb
> old_lsb
) {
958 /* The x-over-area was part of the highband before and will now belong to the lowband */
959 source_scale
= hSbrDec
->sbrScaleFactor
.ov_hb_scale
;
960 target_scale
= hSbrDec
->sbrScaleFactor
.ov_lb_scale
;
962 target_usb
= old_lsb
;
965 /* The x-over-area was part of the lowband before and will now belong to the highband */
966 source_scale
= hSbrDec
->sbrScaleFactor
.ov_lb_scale
;
967 target_scale
= hSbrDec
->sbrScaleFactor
.ov_hb_scale
;
968 /* jdr: The values old_lsb and old_usb might be wrong because the previous frame might have been "upsamling". */
969 target_lsb
= hSbrDec
->SynthesisQMF
.lsb
;
970 target_usb
= hSbrDec
->SynthesisQMF
.usb
;
973 /* Shift left all samples of the x-over-area as much as possible
974 An unnecessary coarse scale could cause ov_lb_scale or ov_hb_scale to be
975 adapted and the accuracy in the next frame would seriously suffer! */
977 maxVal
= maxSubbandSample( OverlapBufferReal
,
978 (useLP
) ? NULL
: OverlapBufferImag
,
984 reserve
= CntLeadingZeros(maxVal
)-1;
985 reserve
= fixMin(reserve
,DFRACT_BITS
-1-source_scale
);
987 rescaleSubbandSamples( OverlapBufferReal
,
988 (useLP
) ? NULL
: OverlapBufferImag
,
994 source_scale
+= reserve
;
996 delta_scale
= target_scale
- source_scale
;
998 if (delta_scale
> 0) { /* x-over-area is dominant */
999 delta_scale
= -delta_scale
;
1000 startBand
= target_lsb
;
1001 stopBand
= target_usb
;
1003 if (new_lsb
> old_lsb
) {
1004 /* The lowband has to be rescaled */
1005 hSbrDec
->sbrScaleFactor
.ov_lb_scale
= source_scale
;
1008 /* The highband has be be rescaled */
1009 hSbrDec
->sbrScaleFactor
.ov_hb_scale
= source_scale
;
1013 FDK_ASSERT(startBand
<= stopBand
);
1016 for (l
=0; l
<startSlot
; l
++) {
1017 scaleValues( OverlapBufferReal
[l
] + startBand
, stopBand
-startBand
, delta_scale
);
1018 scaleValues( OverlapBufferImag
[l
] + startBand
, stopBand
-startBand
, delta_scale
);
1021 for (l
=0; l
<startSlot
; l
++) {
1022 scaleValues( OverlapBufferReal
[l
] + startBand
, stopBand
-startBand
, delta_scale
);
1027 Initialize transposer and limiter
1029 sbrError
= resetLppTransposer (&hSbrDec
->LppTrans
,
1030 hHeaderData
->freqBandData
.lowSubband
,
1031 hHeaderData
->freqBandData
.v_k_master
,
1032 hHeaderData
->freqBandData
.numMaster
,
1033 hHeaderData
->freqBandData
.freqBandTableNoise
,
1034 hHeaderData
->freqBandData
.nNfb
,
1035 hHeaderData
->freqBandData
.highSubband
,
1036 hHeaderData
->sbrProcSmplRate
);
1037 if (sbrError
!= SBRDEC_OK
)
1040 sbrError
= ResetLimiterBands ( hHeaderData
->freqBandData
.limiterBandTable
,
1041 &hHeaderData
->freqBandData
.noLimiterBands
,
1042 hHeaderData
->freqBandData
.freqBandTable
[0],
1043 hHeaderData
->freqBandData
.nSfb
[0],
1044 hSbrDec
->LppTrans
.pSettings
->patchParam
,
1045 hSbrDec
->LppTrans
.pSettings
->noOfPatches
,
1046 hHeaderData
->bs_data
.limiterBands
);