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 Audio Encoder ***************************
86 Initial Authors: M. Multrus
87 Contents/Description: PS Wrapper, Downmix
89 ******************************************************************************/
94 /* Includes ******************************************************************/
97 #include "ps_bitenc.h"
101 /*--------------- function declarations --------------------*/
102 static void psFindBestScaling(
103 HANDLE_PARAMETRIC_STEREO hParametricStereo
,
104 FIXP_DBL
*hybridData
[HYBRID_FRAMESIZE
][MAX_PS_CHANNELS
][2],
106 FIXP_QMF
*maxBandValue
,
110 /*------------- function definitions ----------------*/
111 FDK_PSENC_ERROR
PSEnc_Create(
112 HANDLE_PARAMETRIC_STEREO
*phParametricStereo
115 FDK_PSENC_ERROR error
= PSENC_OK
;
117 if (phParametricStereo
==NULL
) {
118 error
= PSENC_INVALID_HANDLE
;
122 HANDLE_PARAMETRIC_STEREO hParametricStereo
= NULL
;
124 if (NULL
==(hParametricStereo
= GetRam_ParamStereo())) {
125 error
= PSENC_MEMORY_ERROR
;
128 FDKmemclear(hParametricStereo
, sizeof(PARAMETRIC_STEREO
));
130 if (PSENC_OK
!= (error
= FDKsbrEnc_CreatePSEncode(&hParametricStereo
->hPsEncode
))) {
134 for (i
=0; i
<MAX_PS_CHANNELS
; i
++) {
135 if (FDKhybridAnalysisOpen(
136 &hParametricStereo
->fdkHybAnaFilter
[i
],
137 hParametricStereo
->__staticHybAnaStatesLF
[i
],
138 sizeof(hParametricStereo
->__staticHybAnaStatesLF
[i
]),
139 hParametricStereo
->__staticHybAnaStatesHF
[i
],
140 sizeof(hParametricStereo
->__staticHybAnaStatesHF
[i
])
143 error
= PSENC_MEMORY_ERROR
;
148 *phParametricStereo
= hParametricStereo
; /* return allocated handle */
154 FDK_PSENC_ERROR
PSEnc_Init(
155 HANDLE_PARAMETRIC_STEREO hParametricStereo
,
156 const HANDLE_PSENC_CONFIG hPsEncConfig
,
162 FDK_PSENC_ERROR error
= PSENC_OK
;
164 if ( (NULL
==hParametricStereo
) || (NULL
==hPsEncConfig
) ) {
165 error
= PSENC_INVALID_HANDLE
;
170 hParametricStereo
->initPS
= 1;
171 hParametricStereo
->noQmfSlots
= noQmfSlots
;
172 hParametricStereo
->noQmfBands
= noQmfBands
;
174 /* clear delay lines */
175 FDKmemclear(hParametricStereo
->qmfDelayLines
, sizeof(hParametricStereo
->qmfDelayLines
));
177 hParametricStereo
->qmfDelayScale
= FRACT_BITS
-1;
179 /* create configuration for hybrid filter bank */
180 for (ch
=0; ch
<MAX_PS_CHANNELS
; ch
++) {
181 FDKhybridAnalysisInit(
182 &hParametricStereo
->fdkHybAnaFilter
[ch
],
190 FDKhybridSynthesisInit(
191 &hParametricStereo
->fdkHybSynFilter
,
197 /* determine average delay */
198 hParametricStereo
->psDelay
= (HYBRID_FILTER_DELAY
*hParametricStereo
->noQmfBands
);
200 if ( (hPsEncConfig
->maxEnvelopes
< PSENC_NENV_1
) || (hPsEncConfig
->maxEnvelopes
> PSENC_NENV_MAX
) ) {
201 hPsEncConfig
->maxEnvelopes
= PSENC_NENV_DEFAULT
;
203 hParametricStereo
->maxEnvelopes
= hPsEncConfig
->maxEnvelopes
;
205 if (PSENC_OK
!= (error
= FDKsbrEnc_InitPSEncode(hParametricStereo
->hPsEncode
, (PS_BANDS
) hPsEncConfig
->nStereoBands
, hPsEncConfig
->iidQuantErrorThreshold
))){
209 for (ch
= 0; ch
<MAX_PS_CHANNELS
; ch
++) {
210 FIXP_DBL
*pDynReal
= GetRam_Sbr_envRBuffer (ch
, dynamic_RAM
);
211 FIXP_DBL
*pDynImag
= GetRam_Sbr_envIBuffer (ch
, dynamic_RAM
);
213 for (i
=0; i
<HYBRID_FRAMESIZE
; i
++) {
214 hParametricStereo
->pHybridData
[i
+HYBRID_READ_OFFSET
][ch
][0] = &pDynReal
[i
*MAX_HYBRID_BANDS
];
215 hParametricStereo
->pHybridData
[i
+HYBRID_READ_OFFSET
][ch
][1] = &pDynImag
[i
*MAX_HYBRID_BANDS
];;
218 for (i
=0; i
<HYBRID_READ_OFFSET
; i
++) {
219 hParametricStereo
->pHybridData
[i
][ch
][0] = hParametricStereo
->__staticHybridData
[i
][ch
][0];
220 hParametricStereo
->pHybridData
[i
][ch
][1] = hParametricStereo
->__staticHybridData
[i
][ch
][1];
224 /* clear static hybrid buffer */
225 FDKmemclear(hParametricStereo
->__staticHybridData
, sizeof(hParametricStereo
->__staticHybridData
));
227 /* clear bs buffer */
228 FDKmemclear(hParametricStereo
->psOut
, sizeof(hParametricStereo
->psOut
));
230 hParametricStereo
->psOut
[0].enablePSHeader
= 1; /* write ps header in first frame */
232 /* clear scaling buffer */
233 FDKmemclear(hParametricStereo
->dynBandScale
, sizeof(UCHAR
)*PS_MAX_BANDS
);
234 FDKmemclear(hParametricStereo
->maxBandValue
, sizeof(FIXP_QMF
)*PS_MAX_BANDS
);
242 FDK_PSENC_ERROR
PSEnc_Destroy(
243 HANDLE_PARAMETRIC_STEREO
*phParametricStereo
246 FDK_PSENC_ERROR error
= PSENC_OK
;
248 if (NULL
!=phParametricStereo
) {
249 HANDLE_PARAMETRIC_STEREO hParametricStereo
= *phParametricStereo
;
250 if(hParametricStereo
!= NULL
){
251 FDKsbrEnc_DestroyPSEncode(&hParametricStereo
->hPsEncode
);
252 FreeRam_ParamStereo(phParametricStereo
);
259 static FDK_PSENC_ERROR
ExtractPSParameters(
260 HANDLE_PARAMETRIC_STEREO hParametricStereo
,
261 const int sendHeader
,
262 FIXP_DBL
*hybridData
[HYBRID_FRAMESIZE
][MAX_PS_CHANNELS
][2]
265 FDK_PSENC_ERROR error
= PSENC_OK
;
267 if (hParametricStereo
== NULL
) {
268 error
= PSENC_INVALID_HANDLE
;
271 /* call ps encode function */
272 if (hParametricStereo
->initPS
){
273 hParametricStereo
->psOut
[1] = hParametricStereo
->psOut
[0];
275 hParametricStereo
->psOut
[0] = hParametricStereo
->psOut
[1];
277 if (PSENC_OK
!= (error
= FDKsbrEnc_PSEncode(
278 hParametricStereo
->hPsEncode
,
279 &hParametricStereo
->psOut
[1],
280 hParametricStereo
->dynBandScale
,
281 hParametricStereo
->maxEnvelopes
,
283 hParametricStereo
->noQmfSlots
,
289 if (hParametricStereo
->initPS
) {
290 hParametricStereo
->psOut
[0] = hParametricStereo
->psOut
[1];
291 hParametricStereo
->initPS
= 0;
299 static FDK_PSENC_ERROR
DownmixPSQmfData(
300 HANDLE_PARAMETRIC_STEREO hParametricStereo
,
301 HANDLE_QMF_FILTER_BANK sbrSynthQmf
,
302 FIXP_QMF
**RESTRICT mixRealQmfData
,
303 FIXP_QMF
**RESTRICT mixImagQmfData
,
304 INT_PCM
*downsampledOutSignal
,
305 FIXP_DBL
*hybridData
[HYBRID_FRAMESIZE
][MAX_PS_CHANNELS
][2],
306 const INT noQmfSlots
,
307 const INT psQmfScale
[MAX_PS_CHANNELS
],
311 FDK_PSENC_ERROR error
= PSENC_OK
;
313 if(hParametricStereo
== NULL
){
314 error
= PSENC_INVALID_HANDLE
;
318 C_AALLOC_SCRATCH_START(pWorkBuffer
, FIXP_QMF
, 2*QMF_CHANNELS
)
320 /* define scalings */
321 int dynQmfScale
= fixMax(0, hParametricStereo
->dmxScale
-1); /* scale one bit more for addition of left and right */
322 int downmixScale
= psQmfScale
[0] - dynQmfScale
;
323 const FIXP_DBL maxStereoScaleFactor
= MAXVAL_DBL
; /* 2.f/2.f */
325 for (n
= 0; n
<noQmfSlots
; n
++) {
327 FIXP_DBL tmpHybrid
[2][MAX_HYBRID_BANDS
];
329 for(k
= 0; k
<71; k
++){
330 int dynScale
, sc
; /* scaling */
331 FIXP_QMF tmpLeftReal
, tmpRightReal
, tmpLeftImag
, tmpRightImag
;
332 FIXP_DBL tmpScaleFactor
, stereoScaleFactor
;
334 tmpLeftReal
= hybridData
[n
][0][0][k
];
335 tmpLeftImag
= hybridData
[n
][0][1][k
];
336 tmpRightReal
= hybridData
[n
][1][0][k
];
337 tmpRightImag
= hybridData
[n
][1][1][k
];
339 sc
= fixMax(0,CntLeadingZeros( fixMax(fixMax(fixp_abs(tmpLeftReal
),fixp_abs(tmpLeftImag
)),fixMax(fixp_abs(tmpRightReal
),fixp_abs(tmpRightImag
))) )-2);
341 tmpLeftReal
<<= sc
; tmpLeftImag
<<= sc
;
342 tmpRightReal
<<= sc
; tmpRightImag
<<= sc
;
343 dynScale
= fixMin(sc
-dynQmfScale
,DFRACT_BITS
-1);
345 /* calc stereo scale factor to avoid loss of energy in bands */
346 /* stereo scale factor = min(2.0f, sqrt( (abs(l(k, n)^2 + abs(r(k, n)^2 )))/(0.5f*abs(l(k, n) + r(k, n))) )) */
347 stereoScaleFactor
= fPow2Div2(tmpLeftReal
) + fPow2Div2(tmpLeftImag
)
348 + fPow2Div2(tmpRightReal
) + fPow2Div2(tmpRightImag
) ;
350 /* might be that tmpScaleFactor becomes negative, so fabs(.) */
351 tmpScaleFactor
= fixp_abs(stereoScaleFactor
+ fMult(tmpLeftReal
,tmpRightReal
) + fMult(tmpLeftImag
,tmpRightImag
));
353 /* min(2.0f, sqrt(stereoScaleFactor/(0.5f*tmpScaleFactor))) */
354 if ( (stereoScaleFactor
>>1) < fMult(maxStereoScaleFactor
,tmpScaleFactor
) ) {
356 int sc_num
= CountLeadingBits(stereoScaleFactor
) ;
357 int sc_denum
= CountLeadingBits(tmpScaleFactor
) ;
358 sc
= -(sc_num
-sc_denum
);
360 tmpScaleFactor
= schur_div((stereoScaleFactor
<<(sc_num
))>>1,
361 tmpScaleFactor
<<sc_denum
,
364 /* prevent odd scaling for next sqrt calculation */
369 stereoScaleFactor
= sqrtFixp(tmpScaleFactor
);
370 stereoScaleFactor
<<= (sc
>>1);
373 stereoScaleFactor
= maxStereoScaleFactor
;
376 /* write data to hybrid output */
377 tmpHybrid
[0][k
] = fMultDiv2(stereoScaleFactor
, (FIXP_QMF
)(tmpLeftReal
+ tmpRightReal
))>>dynScale
;
378 tmpHybrid
[1][k
] = fMultDiv2(stereoScaleFactor
, (FIXP_QMF
)(tmpLeftImag
+ tmpRightImag
))>>dynScale
;
380 } /* hybrid bands - k */
382 FDKhybridSynthesisApply(
383 &hParametricStereo
->fdkHybSynFilter
,
389 qmfSynthesisFilteringSlot(
395 downsampledOutSignal
+(n
*sbrSynthQmf
->no_channels
),
401 *qmfScale
= -downmixScale
+ 7;
403 C_AALLOC_SCRATCH_END(pWorkBuffer
, FIXP_QMF
, 2*QMF_CHANNELS
)
406 const INT noQmfSlots2
= hParametricStereo
->noQmfSlots
>>1;
407 const int noQmfBands
= hParametricStereo
->noQmfBands
;
409 INT scale
, i
, j
, slotOffset
;
411 FIXP_QMF tmp
[2][QMF_CHANNELS
];
413 for (i
=0; i
<noQmfSlots2
; i
++) {
414 FDKmemcpy(tmp
[0], hParametricStereo
->qmfDelayLines
[0][i
], noQmfBands
*sizeof(FIXP_QMF
));
415 FDKmemcpy(tmp
[1], hParametricStereo
->qmfDelayLines
[1][i
], noQmfBands
*sizeof(FIXP_QMF
));
417 FDKmemcpy(hParametricStereo
->qmfDelayLines
[0][i
], mixRealQmfData
[i
+noQmfSlots2
], noQmfBands
*sizeof(FIXP_QMF
));
418 FDKmemcpy(hParametricStereo
->qmfDelayLines
[1][i
], mixImagQmfData
[i
+noQmfSlots2
], noQmfBands
*sizeof(FIXP_QMF
));
420 FDKmemcpy(mixRealQmfData
[i
+noQmfSlots2
], mixRealQmfData
[i
], noQmfBands
*sizeof(FIXP_QMF
));
421 FDKmemcpy(mixImagQmfData
[i
+noQmfSlots2
], mixImagQmfData
[i
], noQmfBands
*sizeof(FIXP_QMF
));
423 FDKmemcpy(mixRealQmfData
[i
], tmp
[0], noQmfBands
*sizeof(FIXP_QMF
));
424 FDKmemcpy(mixImagQmfData
[i
], tmp
[1], noQmfBands
*sizeof(FIXP_QMF
));
427 if (hParametricStereo
->qmfDelayScale
> *qmfScale
) {
428 scale
= hParametricStereo
->qmfDelayScale
- *qmfScale
;
432 scale
= *qmfScale
- hParametricStereo
->qmfDelayScale
;
433 slotOffset
= noQmfSlots2
;
436 for (i
=0; i
<noQmfSlots2
; i
++) {
437 for (j
=0; j
<noQmfBands
; j
++) {
438 mixRealQmfData
[i
+slotOffset
][j
] >>= scale
;
439 mixImagQmfData
[i
+slotOffset
][j
] >>= scale
;
444 *qmfScale
= FDKmin(*qmfScale
, hParametricStereo
->qmfDelayScale
);
445 hParametricStereo
->qmfDelayScale
= scale
;
454 INT
FDKsbrEnc_PSEnc_WritePSData(
455 HANDLE_PARAMETRIC_STEREO hParametricStereo
,
456 HANDLE_FDK_BITSTREAM hBitstream
459 return ( (hParametricStereo
!=NULL
) ? FDKsbrEnc_WritePSBitstream(&hParametricStereo
->psOut
[0], hBitstream
) : 0 );
463 FDK_PSENC_ERROR
FDKsbrEnc_PSEnc_ParametricStereoProcessing(
464 HANDLE_PARAMETRIC_STEREO hParametricStereo
,
467 QMF_FILTER_BANK
**hQmfAnalysis
,
468 FIXP_QMF
**RESTRICT downmixedRealQmfData
,
469 FIXP_QMF
**RESTRICT downmixedImagQmfData
,
470 INT_PCM
*downsampledOutSignal
,
471 HANDLE_QMF_FILTER_BANK sbrSynthQmf
,
476 FDK_PSENC_ERROR error
= PSENC_OK
;
477 INT psQmfScale
[MAX_PS_CHANNELS
] = {0};
479 C_AALLOC_SCRATCH_START(pWorkBuffer
, FIXP_QMF
, 4*QMF_CHANNELS
)
481 for (psCh
= 0; psCh
<MAX_PS_CHANNELS
; psCh
++) {
483 for (i
= 0; i
< hQmfAnalysis
[psCh
]->no_col
; i
++) {
485 qmfAnalysisFilteringSlot(
487 &pWorkBuffer
[2*QMF_CHANNELS
], /* qmfReal[QMF_CHANNELS] */
488 &pWorkBuffer
[3*QMF_CHANNELS
], /* qmfImag[QMF_CHANNELS] */
489 samples
[psCh
]+i
*(hQmfAnalysis
[psCh
]->no_channels
*timeInStride
),
491 &pWorkBuffer
[0*QMF_CHANNELS
] /* qmf workbuffer 2*QMF_CHANNELS */
494 FDKhybridAnalysisApply(
495 &hParametricStereo
->fdkHybAnaFilter
[psCh
],
496 &pWorkBuffer
[2*QMF_CHANNELS
], /* qmfReal[QMF_CHANNELS] */
497 &pWorkBuffer
[3*QMF_CHANNELS
], /* qmfImag[QMF_CHANNELS] */
498 hParametricStereo
->pHybridData
[i
+HYBRID_READ_OFFSET
][psCh
][0],
499 hParametricStereo
->pHybridData
[i
+HYBRID_READ_OFFSET
][psCh
][1]
502 } /* no_col loop i */
504 psQmfScale
[psCh
] = hQmfAnalysis
[psCh
]->outScalefactor
;
508 C_AALLOC_SCRATCH_END(pWorkBuffer
, FIXP_QMF
, 4*QMF_CHANNELS
)
510 /* find best scaling in new QMF and Hybrid data */
511 psFindBestScaling( hParametricStereo
,
512 &hParametricStereo
->pHybridData
[HYBRID_READ_OFFSET
],
513 hParametricStereo
->dynBandScale
,
514 hParametricStereo
->maxBandValue
,
515 &hParametricStereo
->dmxScale
) ;
518 /* extract the ps parameters */
519 if(PSENC_OK
!= (error
= ExtractPSParameters(hParametricStereo
, sendHeader
, &hParametricStereo
->pHybridData
[0]))){
523 /* save hybrid date for next frame */
524 for (i
=0; i
<HYBRID_READ_OFFSET
; i
++) {
525 FDKmemcpy(hParametricStereo
->pHybridData
[i
][0][0], hParametricStereo
->pHybridData
[HYBRID_FRAMESIZE
+i
][0][0], MAX_HYBRID_BANDS
*sizeof(FIXP_DBL
)); /* left, real */
526 FDKmemcpy(hParametricStereo
->pHybridData
[i
][0][1], hParametricStereo
->pHybridData
[HYBRID_FRAMESIZE
+i
][0][1], MAX_HYBRID_BANDS
*sizeof(FIXP_DBL
)); /* left, imag */
527 FDKmemcpy(hParametricStereo
->pHybridData
[i
][1][0], hParametricStereo
->pHybridData
[HYBRID_FRAMESIZE
+i
][1][0], MAX_HYBRID_BANDS
*sizeof(FIXP_DBL
)); /* right, real */
528 FDKmemcpy(hParametricStereo
->pHybridData
[i
][1][1], hParametricStereo
->pHybridData
[HYBRID_FRAMESIZE
+i
][1][1], MAX_HYBRID_BANDS
*sizeof(FIXP_DBL
)); /* right, imag */
531 /* downmix and hybrid synthesis */
532 if (PSENC_OK
!= (error
= DownmixPSQmfData(hParametricStereo
, sbrSynthQmf
, downmixedRealQmfData
, downmixedImagQmfData
, downsampledOutSignal
, &hParametricStereo
->pHybridData
[HYBRID_READ_OFFSET
], hParametricStereo
->noQmfSlots
, psQmfScale
, qmfScale
))) {
541 static void psFindBestScaling(
542 HANDLE_PARAMETRIC_STEREO hParametricStereo
,
543 FIXP_DBL
*hybridData
[HYBRID_FRAMESIZE
][MAX_PS_CHANNELS
][2],
545 FIXP_QMF
*maxBandValue
,
549 HANDLE_PS_ENCODE hPsEncode
= hParametricStereo
->hPsEncode
;
551 INT group
, bin
, col
, band
;
552 const INT frameSize
= hParametricStereo
->noQmfSlots
;
553 const INT psBands
= (INT
) hPsEncode
->psEncMode
;
554 const INT nIidGroups
= hPsEncode
->nQmfIidGroups
+ hPsEncode
->nSubQmfIidGroups
;
556 /* group wise scaling */
557 FIXP_QMF maxVal
[2][PS_MAX_BANDS
];
558 FIXP_QMF maxValue
= FL2FXCONST_DBL(0.f
);
560 FDKmemclear(maxVal
, sizeof(maxVal
));
562 /* start with hybrid data */
563 for (group
=0; group
< nIidGroups
; group
++) {
564 /* Translate group to bin */
565 bin
= hPsEncode
->subband2parameterIndex
[group
];
567 /* Translate from 20 bins to 10 bins */
568 if (hPsEncode
->psEncMode
== PS_BANDS_COARSE
) {
572 /* QMF downmix scaling */
574 FIXP_QMF tmp
= maxVal
[0][bin
];
576 for (col
=0; col
<frameSize
-HYBRID_READ_OFFSET
; col
++) {
577 for (i
= hPsEncode
->iidGroupBorders
[group
]; i
< hPsEncode
->iidGroupBorders
[group
+1]; i
++) {
578 tmp
= fixMax(tmp
, (FIXP_QMF
)fixp_abs(hybridData
[col
][0][0][i
]));
579 tmp
= fixMax(tmp
, (FIXP_QMF
)fixp_abs(hybridData
[col
][0][1][i
]));
580 tmp
= fixMax(tmp
, (FIXP_QMF
)fixp_abs(hybridData
[col
][1][0][i
]));
581 tmp
= fixMax(tmp
, (FIXP_QMF
)fixp_abs(hybridData
[col
][1][1][i
]));
584 maxVal
[0][bin
] = tmp
;
586 tmp
= maxVal
[1][bin
];
587 for (col
=frameSize
-HYBRID_READ_OFFSET
; col
<frameSize
; col
++) {
588 for (i
= hPsEncode
->iidGroupBorders
[group
]; i
< hPsEncode
->iidGroupBorders
[group
+1]; i
++) {
589 tmp
= fixMax(tmp
, (FIXP_QMF
)fixp_abs(hybridData
[col
][0][0][i
]));
590 tmp
= fixMax(tmp
, (FIXP_QMF
)fixp_abs(hybridData
[col
][0][1][i
]));
591 tmp
= fixMax(tmp
, (FIXP_QMF
)fixp_abs(hybridData
[col
][1][0][i
]));
592 tmp
= fixMax(tmp
, (FIXP_QMF
)fixp_abs(hybridData
[col
][1][1][i
]));
595 maxVal
[1][bin
] = tmp
;
599 /* convert maxSpec to maxScaling, find scaling space */
600 for (band
=0; band
<psBands
; band
++) {
602 dynBandScale
[band
] = CountLeadingBits(fixMax(maxVal
[0][band
],maxBandValue
[band
]));
604 dynBandScale
[band
] = fixMax(0,CountLeadingBits(fixMax(maxVal
[0][band
],maxBandValue
[band
]))-FRACT_BITS
);
606 maxValue
= fixMax(maxValue
,fixMax(maxVal
[0][band
],maxVal
[1][band
]));
607 maxBandValue
[band
] = fixMax(maxVal
[0][band
], maxVal
[1][band
]);
610 /* calculate maximal scaling for QMF downmix */
612 *dmxScale
= fixMin(DFRACT_BITS
, CountLeadingBits(maxValue
));
614 *dmxScale
= fixMax(0,fixMin(FRACT_BITS
, CountLeadingBits(FX_QMF2FX_DBL(maxValue
))));