1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5 � Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6 All rights reserved.
7
8 1. INTRODUCTION
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.
12
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.
17
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.
24
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.
28
29 2. COPYRIGHT LICENSE
30
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:
33
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.
36
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.
41
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44
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.
47
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."
52
53 3. NO PATENT LICENSE
54
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.
58
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61
62 4. DISCLAIMER
63
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.
72
73 5. CONTACT INFORMATION
74
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83
84 #include "env_est.h"
85 #include "tran_det.h"
86
87 #include "qmf.h"
88
89 #include "fram_gen.h"
90 #include "bit_sbr.h"
91 #include "cmondata.h"
92 #include "sbr_ram.h"
93
94
95 #include "genericStds.h"
96
97 #define QUANT_ERROR_THRES 200
98 #define Y_NRG_SCALE 5 /* noCols = 32 -> shift(5) */
99
100
101 static const UCHAR panTable[2][10] = { { 0, 2, 4, 6, 8,12,16,20,24},
102 { 0, 2, 4, 8,12, 0, 0, 0, 0 } };
103 static const UCHAR maxIndex[2] = {9, 5};
104
105
106 /***************************************************************************/
107 /*!
108
109 \brief Calculates energy form real and imaginary part of
110 the QMF subsamples
111
112 \return none
113
114 ****************************************************************************/
115 LNK_SECTION_CODE_L1
116 static void
FDKsbrEnc_getEnergyFromCplxQmfData(FIXP_DBL ** RESTRICT energyValues,FIXP_DBL ** RESTRICT realValues,FIXP_DBL ** RESTRICT imagValues,INT numberBands,INT numberCols,INT * qmfScale,INT * energyScale)117 FDKsbrEnc_getEnergyFromCplxQmfData(FIXP_DBL **RESTRICT energyValues,/*!< the result of the operation */
118 FIXP_DBL **RESTRICT realValues, /*!< the real part of the QMF subsamples */
119 FIXP_DBL **RESTRICT imagValues, /*!< the imaginary part of the QMF subsamples */
120 INT numberBands, /*!< number of QMF bands */
121 INT numberCols, /*!< number of QMF subsamples */
122 INT *qmfScale, /*!< sclefactor of QMF subsamples */
123 INT *energyScale) /*!< scalefactor of energies */
124 {
125 int j, k;
126 int scale;
127 FIXP_DBL max_val = FL2FXCONST_DBL(0.0f);
128
129 /* Get Scratch buffer */
130 C_ALLOC_SCRATCH_START(tmpNrg, FIXP_DBL, QMF_CHANNELS*QMF_MAX_TIME_SLOTS/2);
131
132 FDK_ASSERT(numberBands <= QMF_CHANNELS);
133 FDK_ASSERT(numberCols <= QMF_MAX_TIME_SLOTS);
134
135 /* Get max possible scaling of QMF data */
136 scale = DFRACT_BITS;
137 for (k=0; k<numberCols; k++) {
138 scale = fixMin(scale, fixMin(getScalefactor(realValues[k], numberBands), getScalefactor(imagValues[k], numberBands)));
139 }
140
141 /* Tweak scaling stability for zero signal to non-zero signal transitions */
142 if (scale >= DFRACT_BITS-1) {
143 scale = (FRACT_BITS-1-*qmfScale);
144 }
145 /* prevent scaling of QFM values to -1.f */
146 scale = fixMax(0,scale-1);
147
148 /* Update QMF scale */
149 *qmfScale += scale;
150
151 /*
152 Calculate energy of each time slot pair, max energy
153 and shift QMF values as far as possible to the left.
154 */
155 {
156 FIXP_DBL *nrgValues = tmpNrg;
157 for (k=0; k<numberCols; k+=2)
158 {
159 /* Load band vector addresses of 2 consecutive timeslots */
160 FIXP_DBL *RESTRICT r0 = realValues[k];
161 FIXP_DBL *RESTRICT i0 = imagValues[k];
162 FIXP_DBL *RESTRICT r1 = realValues[k+1];
163 FIXP_DBL *RESTRICT i1 = imagValues[k+1];
164 for (j=0; j<numberBands; j++)
165 {
166 FIXP_DBL energy;
167 FIXP_DBL tr0,tr1,ti0,ti1;
168
169 /* Read QMF values of 2 timeslots */
170 tr0 = r0[j]; tr1 = r1[j]; ti0 = i0[j]; ti1 = i1[j];
171
172 /* Scale QMF Values and Calc Energy of both timeslots */
173 tr0 <<= scale;
174 ti0 <<= scale;
175 energy = fPow2AddDiv2(fPow2Div2(tr0), ti0) >> 1;
176
177 tr1 <<= scale;
178 ti1 <<= scale;
179 energy += fPow2AddDiv2(fPow2Div2(tr1), ti1) >> 1;
180
181 /* Write timeslot pair energy to scratch */
182 *nrgValues++ = energy;
183 max_val = fixMax(max_val, energy);
184
185 /* Write back scaled QMF values */
186 r0[j] = tr0; r1[j] = tr1; i0[j] = ti0; i1[j] = ti1;
187 }
188 }
189 }
190 /* energyScale: scalefactor energies of current frame */
191 *energyScale = 2*(*qmfScale)-1; /* if qmfScale > 0: nr of right shifts otherwise nr of left shifts */
192
193 /* Scale timeslot pair energies and write to output buffer */
194 scale = CountLeadingBits(max_val);
195 {
196 FIXP_DBL *nrgValues = tmpNrg;
197 for (k=0; k<numberCols>>1; k++) {
198 scaleValues(energyValues[k], nrgValues, numberBands, scale);
199 nrgValues += numberBands;
200 }
201 *energyScale += scale;
202 }
203
204 /* Free Scratch buffer */
205 C_ALLOC_SCRATCH_END(tmpNrg, FIXP_DBL, QMF_CHANNELS*QMF_MAX_TIME_SLOTS/2);
206 }
207
208 LNK_SECTION_CODE_L1
209 static void
FDKsbrEnc_getEnergyFromCplxQmfDataFull(FIXP_DBL ** RESTRICT energyValues,FIXP_DBL ** RESTRICT realValues,FIXP_DBL ** RESTRICT imagValues,int numberBands,int numberCols,int * qmfScale,int * energyScale)210 FDKsbrEnc_getEnergyFromCplxQmfDataFull(FIXP_DBL **RESTRICT energyValues,/*!< the result of the operation */
211 FIXP_DBL **RESTRICT realValues, /*!< the real part of the QMF subsamples */
212 FIXP_DBL **RESTRICT imagValues, /*!< the imaginary part of the QMF subsamples */
213 int numberBands, /*!< number of QMF bands */
214 int numberCols, /*!< number of QMF subsamples */
215 int *qmfScale, /*!< sclefactor of QMF subsamples */
216 int *energyScale) /*!< scalefactor of energies */
217 {
218 int j, k;
219 int scale;
220 FIXP_DBL max_val = FL2FXCONST_DBL(0.0f);
221
222 /* Get Scratch buffer */
223 C_ALLOC_SCRATCH_START(tmpNrg, FIXP_DBL, QMF_MAX_TIME_SLOTS*QMF_CHANNELS/2);
224
225 FDK_ASSERT(numberBands <= QMF_CHANNELS);
226 FDK_ASSERT(numberCols <= QMF_MAX_TIME_SLOTS/2);
227
228 /* Get max possible scaling of QMF data */
229 scale = DFRACT_BITS;
230 for (k=0; k<numberCols; k++) {
231 scale = fixMin(scale, fixMin(getScalefactor(realValues[k], numberBands), getScalefactor(imagValues[k], numberBands)));
232 }
233
234 /* Tweak scaling stability for zero signal to non-zero signal transitions */
235 if (scale >= DFRACT_BITS-1) {
236 scale = (FRACT_BITS-1-*qmfScale);
237 }
238 /* prevent scaling of QFM values to -1.f */
239 scale = fixMax(0,scale-1);
240
241 /* Update QMF scale */
242 *qmfScale += scale;
243
244 /*
245 Calculate energy of each time slot pair, max energy
246 and shift QMF values as far as possible to the left.
247 */
248 {
249 FIXP_DBL *nrgValues = tmpNrg;
250 for (k=0; k<numberCols; k++)
251 {
252 /* Load band vector addresses of 2 consecutive timeslots */
253 FIXP_DBL *RESTRICT r0 = realValues[k];
254 FIXP_DBL *RESTRICT i0 = imagValues[k];
255 for (j=0; j<numberBands; j++)
256 {
257 FIXP_DBL energy;
258 FIXP_DBL tr0,ti0;
259
260 /* Read QMF values of 2 timeslots */
261 tr0 = r0[j]; ti0 = i0[j];
262
263 /* Scale QMF Values and Calc Energy of both timeslots */
264 tr0 <<= scale;
265 ti0 <<= scale;
266 energy = fPow2AddDiv2(fPow2Div2(tr0), ti0);
267 *nrgValues++ = energy;
268
269 max_val = fixMax(max_val, energy);
270
271 /* Write back scaled QMF values */
272 r0[j] = tr0; i0[j] = ti0;
273 }
274 }
275 }
276 /* energyScale: scalefactor energies of current frame */
277 *energyScale = 2*(*qmfScale)-1; /* if qmfScale > 0: nr of right shifts otherwise nr of left shifts */
278
279 /* Scale timeslot pair energies and write to output buffer */
280 scale = CountLeadingBits(max_val);
281 {
282 FIXP_DBL *nrgValues = tmpNrg;
283 for (k=0; k<numberCols; k++) {
284 scaleValues(energyValues[k], nrgValues, numberBands, scale);
285 nrgValues += numberBands;
286 }
287 *energyScale += scale;
288 }
289
290 /* Free Scratch buffer */
291 C_ALLOC_SCRATCH_END(tmpNrg, FIXP_DBL, QMF_MAX_TIME_SLOTS*QMF_CHANNELS/2);
292 }
293
294 /***************************************************************************/
295 /*!
296
297 \brief Quantisation of the panorama value (balance)
298
299 \return the quantized pan value
300
301 ****************************************************************************/
302 static INT
mapPanorama(INT nrgVal,INT ampRes,INT * quantError)303 mapPanorama(INT nrgVal, /*! integer value of the energy */
304 INT ampRes, /*! amplitude resolution [1.5/3dB] */
305 INT *quantError /*! quantization error of energy val*/
306 )
307 {
308 int i;
309 INT min_val, val;
310 UCHAR panIndex;
311 INT sign;
312
313 sign = nrgVal > 0 ? 1 : -1;
314
315 nrgVal *= sign;
316
317 min_val = FDK_INT_MAX;
318 panIndex = 0;
319 for (i = 0; i < maxIndex[ampRes]; i++) {
320 val = fixp_abs ((nrgVal - (INT)panTable[ampRes][i]));
321
322 if (val < min_val) {
323 min_val = val;
324 panIndex = i;
325 }
326 }
327
328 *quantError=min_val;
329
330 return panTable[ampRes][maxIndex[ampRes]-1] + sign * panTable[ampRes][panIndex];
331 }
332
333
334 /***************************************************************************/
335 /*!
336
337 \brief Quantisation of the noise floor levels
338
339 \return void
340
341 ****************************************************************************/
342 static void
sbrNoiseFloorLevelsQuantisation(SCHAR * RESTRICT iNoiseLevels,FIXP_DBL * RESTRICT NoiseLevels,INT coupling)343 sbrNoiseFloorLevelsQuantisation(SCHAR *RESTRICT iNoiseLevels, /*! quantized noise levels */
344 FIXP_DBL *RESTRICT NoiseLevels, /*! the noise levels */
345 INT coupling /*! the coupling flag */
346 )
347 {
348 INT i;
349 INT tmp, dummy;
350
351 /* Quantisation, similar to sfb quant... */
352 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++) {
353 /* tmp = NoiseLevels[i] > (PFLOAT)30.0f ? 30: (INT) (NoiseLevels[i] + (PFLOAT)0.5); */
354 /* 30>>6 = 0.46875 */
355 if ((FIXP_DBL)NoiseLevels[i] > FL2FXCONST_DBL(0.46875f)) {
356 tmp = 30;
357 }
358 else {
359 /* tmp = (INT)((FIXP_DBL)NoiseLevels[i] + (FL2FXCONST_DBL(0.5f)>>(*/ /* FRACT_BITS+ */ /* 6-1)));*/
360 /* tmp = tmp >> (DFRACT_BITS-1-6); */ /* conversion to integer happens here */
361 /* rounding is done by shifting one bit less than necessary to the right, adding '1' and then shifting the final bit */
362 tmp = ((((INT)NoiseLevels[i])>>(DFRACT_BITS-1-LD_DATA_SHIFT)) ); /* conversion to integer */
363 if (tmp != 0)
364 tmp += 1;
365 }
366
367 if (coupling) {
368 tmp = tmp < -30 ? -30 : tmp;
369 tmp = mapPanorama (tmp,1,&dummy);
370 }
371 iNoiseLevels[i] = tmp;
372 }
373 }
374
375 /***************************************************************************/
376 /*!
377
378 \brief Calculation of noise floor for coupling
379
380 \return void
381
382 ****************************************************************************/
383 static void
coupleNoiseFloor(FIXP_DBL * RESTRICT noise_level_left,FIXP_DBL * RESTRICT noise_level_right)384 coupleNoiseFloor(FIXP_DBL *RESTRICT noise_level_left, /*! noise level left (modified)*/
385 FIXP_DBL *RESTRICT noise_level_right /*! noise level right (modified)*/
386 )
387 {
388 FIXP_DBL cmpValLeft,cmpValRight;
389 INT i;
390 FIXP_DBL temp1,temp2;
391
392 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++) {
393
394 /* Calculation of the power function using ld64:
395 z = x^y;
396 z' = CalcLd64(z) = y*CalcLd64(x)/64;
397 z = CalcInvLd64(z');
398 */
399 cmpValLeft = NOISE_FLOOR_OFFSET_64 - noise_level_left[i];
400 cmpValRight = NOISE_FLOOR_OFFSET_64 - noise_level_right[i];
401
402 if (cmpValRight < FL2FXCONST_DBL(0.0f)) {
403 temp1 = CalcInvLdData(NOISE_FLOOR_OFFSET_64 - noise_level_right[i]);
404 }
405 else {
406 temp1 = CalcInvLdData(NOISE_FLOOR_OFFSET_64 - noise_level_right[i]);
407 temp1 = temp1 << (DFRACT_BITS-1-LD_DATA_SHIFT-1); /* INT to fract conversion of result, if input of CalcInvLdData is positiv */
408 }
409
410 if (cmpValLeft < FL2FXCONST_DBL(0.0f)) {
411 temp2 = CalcInvLdData(NOISE_FLOOR_OFFSET_64 - noise_level_left[i]);
412 }
413 else {
414 temp2 = CalcInvLdData(NOISE_FLOOR_OFFSET_64 - noise_level_left[i]);
415 temp2 = temp2 << (DFRACT_BITS-1-LD_DATA_SHIFT-1); /* INT to fract conversion of result, if input of CalcInvLdData is positiv */
416 }
417
418
419 if ((cmpValLeft < FL2FXCONST_DBL(0.0f)) && (cmpValRight < FL2FXCONST_DBL(0.0f))) {
420 noise_level_left[i] = NOISE_FLOOR_OFFSET_64 - (CalcLdData(((temp1>>1) + (temp2>>1)))); /* no scaling needed! both values are dfract */
421 noise_level_right[i] = CalcLdData(temp2) - CalcLdData(temp1);
422 }
423
424 if ((cmpValLeft >= FL2FXCONST_DBL(0.0f)) && (cmpValRight >= FL2FXCONST_DBL(0.0f))) {
425 noise_level_left[i] = NOISE_FLOOR_OFFSET_64 - (CalcLdData(((temp1>>1) + (temp2>>1))) + FL2FXCONST_DBL(0.109375f)); /* scaled with 7/64 */
426 noise_level_right[i] = CalcLdData(temp2) - CalcLdData(temp1);
427 }
428
429 if ((cmpValLeft >= FL2FXCONST_DBL(0.0f)) && (cmpValRight < FL2FXCONST_DBL(0.0f))) {
430 noise_level_left[i] = NOISE_FLOOR_OFFSET_64 - (CalcLdData(((temp1>>(7+1)) + (temp2>>1))) + FL2FXCONST_DBL(0.109375f)); /* scaled with 7/64 */
431 noise_level_right[i] = (CalcLdData(temp2) + FL2FXCONST_DBL(0.109375f)) - CalcLdData(temp1);
432 }
433
434 if ((cmpValLeft < FL2FXCONST_DBL(0.0f)) && (cmpValRight >= FL2FXCONST_DBL(0.0f))) {
435 noise_level_left[i] = NOISE_FLOOR_OFFSET_64 - (CalcLdData(((temp1>>1) + (temp2>>(7+1)))) + FL2FXCONST_DBL(0.109375f)); /* scaled with 7/64 */
436 noise_level_right[i] = CalcLdData(temp2) - (CalcLdData(temp1) + FL2FXCONST_DBL(0.109375f)); /* scaled with 7/64 */
437 }
438 }
439 }
440
441 /***************************************************************************/
442 /*!
443
444 \brief Calculation of energy starting in lower band (li) up to upper band (ui)
445 over slots (start_pos) to (stop_pos)
446
447 \return void
448
449 ****************************************************************************/
450 static FIXP_DBL
getEnvSfbEnergy(INT li,INT ui,INT start_pos,INT stop_pos,INT border_pos,FIXP_DBL ** YBuffer,INT YBufferSzShift,INT scaleNrg0,INT scaleNrg1)451 getEnvSfbEnergy(INT li, /*! lower band */
452 INT ui, /*! upper band */
453 INT start_pos, /*! start slot */
454 INT stop_pos, /*! stop slot */
455 INT border_pos, /*! slots scaling border */
456 FIXP_DBL **YBuffer, /*! sfb energy buffer */
457 INT YBufferSzShift, /*! Energy buffer index scale */
458 INT scaleNrg0, /*! scaling of lower slots */
459 INT scaleNrg1) /*! scaling of upper slots */
460 {
461 /* use dynamic scaling for outer energy loop;
462 energies are critical and every bit is important */
463 int sc0, sc1, k, l;
464
465 FIXP_DBL nrgSum, nrg1, nrg2, accu1, accu2;
466 INT dynScale, dynScale1, dynScale2;
467 if(ui-li==0) dynScale = DFRACT_BITS-1;
468 else
469 dynScale = CalcLdInt(ui-li)>>(DFRACT_BITS-1-LD_DATA_SHIFT);
470
471 sc0 = fixMin(scaleNrg0,Y_NRG_SCALE); sc1 = fixMin(scaleNrg1,Y_NRG_SCALE);
472 /* dynScale{1,2} is set such that the right shift below is positive */
473 dynScale1 = fixMin((scaleNrg0-sc0),dynScale);
474 dynScale2 = fixMin((scaleNrg1-sc1),dynScale);
475 nrgSum = accu1 = accu2 = (FIXP_DBL)0;
476
477 for (k = li; k < ui; k++) {
478 nrg1 = nrg2 = (FIXP_DBL)0;
479 for (l = start_pos; l < border_pos; l++) {
480 nrg1 += YBuffer[l>>YBufferSzShift][k] >> sc0;
481 }
482 for (; l < stop_pos; l++) {
483 nrg2 += YBuffer[l>>YBufferSzShift][k] >> sc1;
484 }
485 accu1 += (nrg1>>dynScale1);
486 accu2 += (nrg2>>dynScale2);
487 }
488 /* This shift factor is always positive. See comment above. */
489 nrgSum += ( accu1 >> fixMin((scaleNrg0-sc0-dynScale1),(DFRACT_BITS-1)) )
490 + ( accu2 >> fixMin((scaleNrg1-sc1-dynScale2),(DFRACT_BITS-1)) );
491
492 return nrgSum;
493 }
494
495 /***************************************************************************/
496 /*!
497
498 \brief Energy compensation in missing harmonic mode
499
500 \return void
501
502 ****************************************************************************/
503 static FIXP_DBL
mhLoweringEnergy(FIXP_DBL nrg,INT M)504 mhLoweringEnergy(FIXP_DBL nrg, INT M)
505 {
506 /*
507 Compensating for the fact that we in the decoder map the "average energy to every QMF
508 band, and use this when we calculate the boost-factor. Since the mapped energy isn't
509 the average energy but the maximum energy in case of missing harmonic creation, we will
510 in the boost function calculate that too much limiting has been applied and hence we will
511 boost the signal although it isn't called for. Hence we need to compensate for this by
512 lowering the transmitted energy values for the sines so they will get the correct level
513 after the boost is applied.
514 */
515 if(M > 2){
516 INT tmpScale;
517 tmpScale = CountLeadingBits(nrg);
518 nrg <<= tmpScale;
519 nrg = fMult(nrg, FL2FXCONST_DBL(0.398107267f)); /* The maximum boost is 1.584893, so the maximum attenuation should be square(1/1.584893) = 0.398107267 */
520 nrg >>= tmpScale;
521 }
522 else{
523 if(M > 1){
524 nrg >>= 1;
525 }
526 }
527
528 return nrg;
529 }
530
531 /***************************************************************************/
532 /*!
533
534 \brief Energy compensation in none missing harmonic mode
535
536 \return void
537
538 ****************************************************************************/
nmhLoweringEnergy(FIXP_DBL nrg,const FIXP_DBL nrgSum,const INT nrgSum_scale,const INT M)539 static FIXP_DBL nmhLoweringEnergy(
540 FIXP_DBL nrg,
541 const FIXP_DBL nrgSum,
542 const INT nrgSum_scale,
543 const INT M
544 )
545 {
546 if (nrg>FL2FXCONST_DBL(0)) {
547 int sc=0;
548 /* gain = nrgSum / (nrg*(M+1)) */
549 FIXP_DBL gain = fMult(fDivNorm(nrgSum, nrg, &sc), GetInvInt(M+1));
550 sc += nrgSum_scale;
551
552 /* reduce nrg if gain smaller 1.f */
553 if ( !((sc>=0) && ( gain > ((FIXP_DBL)MAXVAL_DBL>>sc) )) ) {
554 nrg = fMult(scaleValue(gain,sc), nrg);
555 }
556 }
557 return nrg;
558 }
559
560 /***************************************************************************/
561 /*!
562
563 \brief calculates the envelope values from the energies, depending on
564 framing and stereo mode
565
566 \return void
567
568 ****************************************************************************/
569 static void
calculateSbrEnvelope(FIXP_DBL ** RESTRICT YBufferLeft,FIXP_DBL ** RESTRICT YBufferRight,int * RESTRICT YBufferScaleLeft,int * RESTRICT YBufferScaleRight,const SBR_FRAME_INFO * frame_info,SCHAR * RESTRICT sfb_nrgLeft,SCHAR * RESTRICT sfb_nrgRight,HANDLE_SBR_CONFIG_DATA h_con,HANDLE_ENV_CHANNEL h_sbr,SBR_STEREO_MODE stereoMode,INT * maxQuantError,int YBufferSzShift)570 calculateSbrEnvelope (FIXP_DBL **RESTRICT YBufferLeft, /*! energy buffer left */
571 FIXP_DBL **RESTRICT YBufferRight, /*! energy buffer right */
572 int *RESTRICT YBufferScaleLeft, /*! scale energy buffer left */
573 int *RESTRICT YBufferScaleRight, /*! scale energy buffer right */
574 const SBR_FRAME_INFO *frame_info, /*! frame info vector */
575 SCHAR *RESTRICT sfb_nrgLeft, /*! sfb energy buffer left */
576 SCHAR *RESTRICT sfb_nrgRight, /*! sfb energy buffer right */
577 HANDLE_SBR_CONFIG_DATA h_con, /*! handle to config data */
578 HANDLE_ENV_CHANNEL h_sbr, /*! envelope channel handle */
579 SBR_STEREO_MODE stereoMode, /*! stereo coding mode */
580 INT* maxQuantError, /*! maximum quantization error, for panorama. */
581 int YBufferSzShift) /*! Energy buffer index scale */
582
583 {
584 int i, j, m = 0;
585 INT no_of_bands, start_pos, stop_pos, li, ui;
586 FREQ_RES freq_res;
587
588 INT ca = 2 - h_sbr->encEnvData.init_sbr_amp_res;
589 INT oneBitLess = 0;
590 if (ca == 2)
591 oneBitLess = 1; /* LD_DATA_SHIFT => ld64 scaling; one bit less for rounding */
592
593 INT quantError;
594 INT nEnvelopes = frame_info->nEnvelopes;
595 INT short_env = frame_info->shortEnv - 1;
596 INT timeStep = h_sbr->sbrExtractEnvelope.time_step;
597 INT commonScale,scaleLeft0,scaleLeft1;
598 INT scaleRight0=0,scaleRight1=0;
599
600 commonScale = fixMin(YBufferScaleLeft[0],YBufferScaleLeft[1]);
601
602 if (stereoMode == SBR_COUPLING) {
603 commonScale = fixMin(commonScale,YBufferScaleRight[0]);
604 commonScale = fixMin(commonScale,YBufferScaleRight[1]);
605 }
606
607 commonScale = commonScale - 7;
608
609 scaleLeft0 = YBufferScaleLeft[0] - commonScale;
610 scaleLeft1 = YBufferScaleLeft[1] - commonScale ;
611 FDK_ASSERT ((scaleLeft0 >= 0) && (scaleLeft1 >= 0));
612
613 if (stereoMode == SBR_COUPLING) {
614 scaleRight0 = YBufferScaleRight[0] - commonScale;
615 scaleRight1 = YBufferScaleRight[1] - commonScale;
616 FDK_ASSERT ((scaleRight0 >= 0) && (scaleRight1 >= 0));
617 *maxQuantError = 0;
618 }
619
620 for (i = 0; i < nEnvelopes; i++) {
621
622 FIXP_DBL pNrgLeft[QMF_MAX_TIME_SLOTS];
623 FIXP_DBL pNrgRight[QMF_MAX_TIME_SLOTS];
624 int envNrg_scale;
625 FIXP_DBL envNrgLeft = FL2FXCONST_DBL(0.0f);
626 FIXP_DBL envNrgRight = FL2FXCONST_DBL(0.0f);
627 int missingHarmonic[QMF_MAX_TIME_SLOTS];
628 int count[QMF_MAX_TIME_SLOTS];
629
630 start_pos = timeStep * frame_info->borders[i];
631 stop_pos = timeStep * frame_info->borders[i + 1];
632 freq_res = frame_info->freqRes[i];
633 no_of_bands = h_con->nSfb[freq_res];
634 envNrg_scale = DFRACT_BITS-fNormz((FIXP_DBL)no_of_bands);
635
636 if (i == short_env) {
637 stop_pos -= fixMax(2, timeStep); /* consider at least 2 QMF slots less for short envelopes (envelopes just before transients) */
638 }
639
640 for (j = 0; j < no_of_bands; j++) {
641 FIXP_DBL nrgLeft = FL2FXCONST_DBL(0.0f);
642 FIXP_DBL nrgRight = FL2FXCONST_DBL(0.0f);
643
644 li = h_con->freqBandTable[freq_res][j];
645 ui = h_con->freqBandTable[freq_res][j + 1];
646
647 if(freq_res == FREQ_RES_HIGH){
648 if(j == 0 && ui-li > 1){
649 li++;
650 }
651 }
652 else{
653 if(j == 0 && ui-li > 2){
654 li++;
655 }
656 }
657
658 /*
659 Find out whether a sine will be missing in the scale-factor
660 band that we're currently processing.
661 */
662 missingHarmonic[j] = 0;
663
664 if(h_sbr->encEnvData.addHarmonicFlag){
665
666 if(freq_res == FREQ_RES_HIGH){
667 if(h_sbr->encEnvData.addHarmonic[j]){ /*A missing sine in the current band*/
668 missingHarmonic[j] = 1;
669 }
670 }
671 else{
672 INT i;
673 INT startBandHigh = 0;
674 INT stopBandHigh = 0;
675
676 while(h_con->freqBandTable[FREQ_RES_HIGH][startBandHigh] < h_con->freqBandTable[FREQ_RES_LOW][j])
677 startBandHigh++;
678 while(h_con->freqBandTable[FREQ_RES_HIGH][stopBandHigh] < h_con->freqBandTable[FREQ_RES_LOW][j + 1])
679 stopBandHigh++;
680
681 for(i = startBandHigh; i<stopBandHigh; i++){
682 if(h_sbr->encEnvData.addHarmonic[i]){
683 missingHarmonic[j] = 1;
684 }
685 }
686 }
687 }
688
689 /*
690 If a sine is missing in a scalefactorband, with more than one qmf channel
691 use the nrg from the channel with the largest nrg rather than the mean.
692 Compensate for the boost calculation in the decdoder.
693 */
694 int border_pos = fixMin(stop_pos, h_sbr->sbrExtractEnvelope.YBufferWriteOffset<<YBufferSzShift);
695
696 if(missingHarmonic[j]){
697
698 int k;
699 count[j] = stop_pos - start_pos;
700 nrgLeft = FL2FXCONST_DBL(0.0f);
701
702 for (k = li; k < ui; k++) {
703 FIXP_DBL tmpNrg;
704 tmpNrg = getEnvSfbEnergy(k,
705 k+1,
706 start_pos,
707 stop_pos,
708 border_pos,
709 YBufferLeft,
710 YBufferSzShift,
711 scaleLeft0,
712 scaleLeft1);
713
714 nrgLeft = fixMax(nrgLeft, tmpNrg);
715 }
716
717 /* Energy lowering compensation */
718 nrgLeft = mhLoweringEnergy(nrgLeft, ui-li);
719
720 if (stereoMode == SBR_COUPLING) {
721
722 nrgRight = FL2FXCONST_DBL(0.0f);
723
724 for (k = li; k < ui; k++) {
725 FIXP_DBL tmpNrg;
726 tmpNrg = getEnvSfbEnergy(k,
727 k+1,
728 start_pos,
729 stop_pos,
730 border_pos,
731 YBufferRight,
732 YBufferSzShift,
733 scaleRight0,
734 scaleRight1);
735
736 nrgRight = fixMax(nrgRight, tmpNrg);
737 }
738
739 /* Energy lowering compensation */
740 nrgRight = mhLoweringEnergy(nrgRight, ui-li);
741 }
742 } /* end missingHarmonic */
743 else{
744 count[j] = (stop_pos - start_pos) * (ui - li);
745
746 nrgLeft = getEnvSfbEnergy(li,
747 ui,
748 start_pos,
749 stop_pos,
750 border_pos,
751 YBufferLeft,
752 YBufferSzShift,
753 scaleLeft0,
754 scaleLeft1);
755
756 if (stereoMode == SBR_COUPLING) {
757 nrgRight = getEnvSfbEnergy(li,
758 ui,
759 start_pos,
760 stop_pos,
761 border_pos,
762 YBufferRight,
763 YBufferSzShift,
764 scaleRight0,
765 scaleRight1);
766 }
767 } /* !missingHarmonic */
768
769 /* save energies */
770 pNrgLeft[j] = nrgLeft;
771 pNrgRight[j] = nrgRight;
772 envNrgLeft += (nrgLeft>>envNrg_scale);
773 envNrgRight += (nrgRight>>envNrg_scale);
774 } /* j */
775
776 for (j = 0; j < no_of_bands; j++) {
777
778 FIXP_DBL nrgLeft2 = FL2FXCONST_DBL(0.0f);
779 FIXP_DBL nrgLeft = pNrgLeft[j];
780 FIXP_DBL nrgRight = pNrgRight[j];
781
782 /* None missing harmonic Energy lowering compensation */
783 if(!missingHarmonic[j] && h_sbr->fLevelProtect) {
784 /* in case of missing energy in base band,
785 reduce reference energy to prevent overflows in decoder output */
786 nrgLeft = nmhLoweringEnergy(nrgLeft, envNrgLeft, envNrg_scale, no_of_bands);
787 if (stereoMode == SBR_COUPLING) {
788 nrgRight = nmhLoweringEnergy(nrgRight, envNrgRight, envNrg_scale, no_of_bands);
789 }
790 }
791
792 if (stereoMode == SBR_COUPLING) {
793 /* calc operation later with log */
794 nrgLeft2 = nrgLeft;
795 nrgLeft = (nrgRight + nrgLeft) >> 1;
796 }
797
798 /* nrgLeft = f20_log2(nrgLeft / (PFLOAT)(count * h_sbr->sbrQmf.no_channels))+(PFLOAT)44; */
799 /* If nrgLeft == 0 then the Log calculations below do fail. */
800 if (nrgLeft > FL2FXCONST_DBL(0.0f))
801 {
802 FIXP_DBL tmp0,tmp1,tmp2,tmp3;
803 INT tmpScale;
804
805 tmpScale = CountLeadingBits(nrgLeft);
806 nrgLeft = nrgLeft << tmpScale;
807
808 tmp0 = CalcLdData(nrgLeft); /* scaled by 1/64 */
809 tmp1 = ((FIXP_DBL) (commonScale+tmpScale)) << (DFRACT_BITS-1-LD_DATA_SHIFT-1); /* scaled by 1/64 */
810 tmp2 = ((FIXP_DBL)(count[j]*h_con->noQmfBands)) << (DFRACT_BITS-1-14-1);
811 tmp2 = CalcLdData(tmp2); /* scaled by 1/64 */
812 tmp3 = FL2FXCONST_DBL(0.6875f-0.21875f-0.015625f)>>1; /* scaled by 1/64 */
813
814 nrgLeft = ((tmp0-tmp2)>>1) + (tmp3 - tmp1);
815 } else {
816 nrgLeft = FL2FXCONST_DBL(-1.0f);
817 }
818
819 /* ld64 to integer conversion */
820 nrgLeft = fixMin(fixMax(nrgLeft,FL2FXCONST_DBL(0.0f)),FL2FXCONST_DBL(0.5f));
821 nrgLeft = (FIXP_DBL)(LONG)nrgLeft >> (DFRACT_BITS-1-LD_DATA_SHIFT-1-oneBitLess-1);
822 sfb_nrgLeft[m] = ((INT)nrgLeft+1)>>1; /* rounding */
823
824 if (stereoMode == SBR_COUPLING) {
825 FIXP_DBL scaleFract;
826
827 if (nrgRight != FL2FXCONST_DBL(0.0f)) {
828 int sc0 = CountLeadingBits(nrgLeft2);
829 int sc1 = CountLeadingBits(nrgRight);
830
831 scaleFract = ((FIXP_DBL)(sc0-sc1)) << (DFRACT_BITS-1-LD_DATA_SHIFT); /* scale value in ld64 representation */
832 nrgRight = CalcLdData(nrgLeft2<<sc0) - CalcLdData(nrgRight<<sc1) - scaleFract;
833 }
834 else
835 nrgRight = FL2FXCONST_DBL(0.5f); /* ld64(4294967296.0f) */
836
837 /* ld64 to integer conversion */
838 nrgRight = (FIXP_DBL)(LONG)(nrgRight) >> (DFRACT_BITS-1-LD_DATA_SHIFT-1-oneBitLess);
839 nrgRight = (nrgRight+(FIXP_DBL)1)>>1; /* rounding */
840
841 sfb_nrgRight[m] = mapPanorama (nrgRight,h_sbr->encEnvData.init_sbr_amp_res,&quantError);
842
843 *maxQuantError = fixMax(quantError, *maxQuantError);
844 }
845
846 m++;
847 } /* j */
848
849 /* Do energy compensation for sines that are present in two
850 QMF-bands in the original, but will only occur in one band in
851 the decoder due to the synthetic sine coding.*/
852 if (h_con->useParametricCoding) {
853 m-=no_of_bands;
854 for (j = 0; j < no_of_bands; j++) {
855 if (freq_res==FREQ_RES_HIGH && h_sbr->sbrExtractEnvelope.envelopeCompensation[j]){
856 sfb_nrgLeft[m] -= (ca * fixp_abs((INT)h_sbr->sbrExtractEnvelope.envelopeCompensation[j]));
857 }
858 sfb_nrgLeft[m] = fixMax(0, sfb_nrgLeft[m]);
859 m++;
860 }
861 } /* useParametricCoding */
862
863 } /* i*/
864 }
865
866 /***************************************************************************/
867 /*!
868
869 \brief calculates the noise floor and the envelope values from the
870 energies, depending on framing and stereo mode
871
872 FDKsbrEnc_extractSbrEnvelope is the main function for encoding and writing the
873 envelope and the noise floor. The function includes the following processes:
874
875 -Analysis subband filtering.
876 -Encoding SA and pan parameters (if enabled).
877 -Transient detection.
878
879 ****************************************************************************/
880
881 LNK_SECTION_CODE_L1
882 void
FDKsbrEnc_extractSbrEnvelope1(HANDLE_SBR_CONFIG_DATA h_con,HANDLE_SBR_HEADER_DATA sbrHeaderData,HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,HANDLE_ENV_CHANNEL hEnvChan,HANDLE_COMMON_DATA hCmonData,SBR_ENV_TEMP_DATA * eData,SBR_FRAME_TEMP_DATA * fData)883 FDKsbrEnc_extractSbrEnvelope1 (
884 HANDLE_SBR_CONFIG_DATA h_con, /*! handle to config data */
885 HANDLE_SBR_HEADER_DATA sbrHeaderData,
886 HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,
887 HANDLE_ENV_CHANNEL hEnvChan,
888 HANDLE_COMMON_DATA hCmonData,
889 SBR_ENV_TEMP_DATA *eData,
890 SBR_FRAME_TEMP_DATA *fData
891 )
892 {
893
894 HANDLE_SBR_EXTRACT_ENVELOPE sbrExtrEnv = &hEnvChan->sbrExtractEnvelope;
895
896 if (sbrExtrEnv->YBufferSzShift == 0)
897 FDKsbrEnc_getEnergyFromCplxQmfDataFull(&sbrExtrEnv->YBuffer[sbrExtrEnv->YBufferWriteOffset],
898 sbrExtrEnv->rBuffer + sbrExtrEnv->rBufferReadOffset,
899 sbrExtrEnv->iBuffer + sbrExtrEnv->rBufferReadOffset,
900 h_con->noQmfBands,
901 sbrExtrEnv->no_cols,
902 &hEnvChan->qmfScale,
903 &sbrExtrEnv->YBufferScale[1]);
904 else
905 FDKsbrEnc_getEnergyFromCplxQmfData(&sbrExtrEnv->YBuffer[sbrExtrEnv->YBufferWriteOffset],
906 sbrExtrEnv->rBuffer + sbrExtrEnv->rBufferReadOffset,
907 sbrExtrEnv->iBuffer + sbrExtrEnv->rBufferReadOffset,
908 h_con->noQmfBands,
909 sbrExtrEnv->no_cols,
910 &hEnvChan->qmfScale,
911 &sbrExtrEnv->YBufferScale[1]);
912
913
914
915 /*
916 Precalculation of Tonality Quotas COEFF Transform OK
917 */
918 FDKsbrEnc_CalculateTonalityQuotas(&hEnvChan->TonCorr,
919 sbrExtrEnv->rBuffer,
920 sbrExtrEnv->iBuffer,
921 h_con->freqBandTable[HI][h_con->nSfb[HI]],
922 hEnvChan->qmfScale);
923
924
925
926 /*
927 Transient detection COEFF Transform OK
928 */
929 FDKsbrEnc_transientDetect(&hEnvChan->sbrTransientDetector,
930 sbrExtrEnv->YBuffer,
931 sbrExtrEnv->YBufferScale,
932 eData->transient_info,
933 sbrExtrEnv->YBufferWriteOffset,
934 sbrExtrEnv->YBufferSzShift,
935 sbrExtrEnv->time_step,
936 hEnvChan->SbrEnvFrame.frameMiddleSlot);
937
938
939
940 /*
941 Generate flags for 2 env in a FIXFIX-frame.
942 Remove this function to get always 1 env per FIXFIX-frame.
943 */
944
945 /*
946 frame Splitter COEFF Transform OK
947 */
948 FDKsbrEnc_frameSplitter(sbrExtrEnv->YBuffer,
949 sbrExtrEnv->YBufferScale,
950 &hEnvChan->sbrTransientDetector,
951 h_con->freqBandTable[1],
952 eData->transient_info,
953 sbrExtrEnv->YBufferWriteOffset,
954 sbrExtrEnv->YBufferSzShift,
955 h_con->nSfb[1],
956 sbrExtrEnv->time_step,
957 sbrExtrEnv->no_cols);
958
959
960 }
961
962 /***************************************************************************/
963 /*!
964
965 \brief calculates the noise floor and the envelope values from the
966 energies, depending on framing and stereo mode
967
968 FDKsbrEnc_extractSbrEnvelope is the main function for encoding and writing the
969 envelope and the noise floor. The function includes the following processes:
970
971 -Determine time/frequency division of current granule.
972 -Sending transient info to bitstream.
973 -Set amp_res to 1.5 dB if the current frame contains only one envelope.
974 -Lock dynamic bandwidth frequency change if the next envelope not starts on a
975 frame boundary.
976 -MDCT transposer (needed to detect where harmonics will be missing).
977 -Spectrum Estimation (used for pulse train and missing harmonics detection).
978 -Pulse train detection.
979 -Inverse Filtering detection.
980 -Waveform Coding.
981 -Missing Harmonics detection.
982 -Extract envelope of current frame.
983 -Noise floor estimation.
984 -Noise floor quantisation and coding.
985 -Encode envelope of current frame.
986 -Send the encoded data to the bitstream.
987 -Write to bitstream.
988
989 ****************************************************************************/
990
991 LNK_SECTION_CODE_L1
992 void
FDKsbrEnc_extractSbrEnvelope2(HANDLE_SBR_CONFIG_DATA h_con,HANDLE_SBR_HEADER_DATA sbrHeaderData,HANDLE_PARAMETRIC_STEREO hParametricStereo,HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,HANDLE_ENV_CHANNEL h_envChan0,HANDLE_ENV_CHANNEL h_envChan1,HANDLE_COMMON_DATA hCmonData,SBR_ENV_TEMP_DATA * eData,SBR_FRAME_TEMP_DATA * fData,int clearOutput)993 FDKsbrEnc_extractSbrEnvelope2 (
994 HANDLE_SBR_CONFIG_DATA h_con, /*! handle to config data */
995 HANDLE_SBR_HEADER_DATA sbrHeaderData,
996 HANDLE_PARAMETRIC_STEREO hParametricStereo,
997 HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,
998 HANDLE_ENV_CHANNEL h_envChan0,
999 HANDLE_ENV_CHANNEL h_envChan1,
1000 HANDLE_COMMON_DATA hCmonData,
1001 SBR_ENV_TEMP_DATA *eData,
1002 SBR_FRAME_TEMP_DATA *fData,
1003 int clearOutput
1004 )
1005 {
1006 HANDLE_ENV_CHANNEL h_envChan[MAX_NUM_CHANNELS] = {h_envChan0, h_envChan1};
1007 int ch, i, j, c, YSzShift = h_envChan[0]->sbrExtractEnvelope.YBufferSzShift;
1008
1009 SBR_STEREO_MODE stereoMode = h_con->stereoMode;
1010 int nChannels = h_con->nChannels;
1011 const int *v_tuning;
1012 static const int v_tuningHEAAC[6] = { 0, 2, 4, 0, 0, 0 };
1013
1014 static const int v_tuningELD[6] = { 0, 2, 3, 0, 0, 0 };
1015
1016 if (h_con->sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY)
1017 v_tuning = v_tuningELD;
1018 else
1019 v_tuning = v_tuningHEAAC;
1020
1021
1022 /*
1023 Select stereo mode.
1024 */
1025 if (stereoMode == SBR_COUPLING) {
1026 if (eData[0].transient_info[1] && eData[1].transient_info[1]) {
1027 eData[0].transient_info[0] = fixMin(eData[1].transient_info[0], eData[0].transient_info[0]);
1028 eData[1].transient_info[0] = eData[0].transient_info[0];
1029 }
1030 else {
1031 if (eData[0].transient_info[1] && !eData[1].transient_info[1]) {
1032 eData[1].transient_info[0] = eData[0].transient_info[0];
1033 }
1034 else {
1035 if (!eData[0].transient_info[1] && eData[1].transient_info[1])
1036 eData[0].transient_info[0] = eData[1].transient_info[0];
1037 else {
1038 eData[0].transient_info[0] = fixMax(eData[1].transient_info[0], eData[0].transient_info[0]);
1039 eData[1].transient_info[0] = eData[0].transient_info[0];
1040 }
1041 }
1042 }
1043 }
1044
1045 /*
1046 Determine time/frequency division of current granule
1047 */
1048 eData[0].frame_info = FDKsbrEnc_frameInfoGenerator(&h_envChan[0]->SbrEnvFrame,
1049 eData[0].transient_info,
1050 h_envChan[0]->sbrExtractEnvelope.pre_transient_info,
1051 h_envChan[0]->encEnvData.ldGrid,
1052 v_tuning);
1053
1054 h_envChan[0]->encEnvData.hSbrBSGrid = &h_envChan[0]->SbrEnvFrame.SbrGrid;
1055
1056 /* AAC LD patch for transient prediction */
1057 if (h_envChan[0]->encEnvData.ldGrid && eData[0].transient_info[2]) {
1058 /* if next frame will start with transient, set shortEnv to numEnvelopes(shortend Envelope = shortEnv-1)*/
1059 h_envChan[0]->SbrEnvFrame.SbrFrameInfo.shortEnv = h_envChan[0]->SbrEnvFrame.SbrFrameInfo.nEnvelopes;
1060 }
1061
1062
1063 switch (stereoMode) {
1064 case SBR_LEFT_RIGHT:
1065 case SBR_SWITCH_LRC:
1066 eData[1].frame_info = FDKsbrEnc_frameInfoGenerator(&h_envChan[1]->SbrEnvFrame,
1067 eData[1].transient_info,
1068 h_envChan[1]->sbrExtractEnvelope.pre_transient_info,
1069 h_envChan[1]->encEnvData.ldGrid,
1070 v_tuning);
1071
1072 h_envChan[1]->encEnvData.hSbrBSGrid = &h_envChan[1]->SbrEnvFrame.SbrGrid;
1073
1074 if (h_envChan[1]->encEnvData.ldGrid && eData[1].transient_info[2]) {
1075 /* if next frame will start with transient, set shortEnv to numEnvelopes(shortend Envelope = shortEnv-1)*/
1076 h_envChan[1]->SbrEnvFrame.SbrFrameInfo.shortEnv = h_envChan[1]->SbrEnvFrame.SbrFrameInfo.nEnvelopes;
1077 }
1078
1079 /* compare left and right frame_infos */
1080 if (eData[0].frame_info->nEnvelopes != eData[1].frame_info->nEnvelopes) {
1081 stereoMode = SBR_LEFT_RIGHT;
1082 } else {
1083 for (i = 0; i < eData[0].frame_info->nEnvelopes + 1; i++) {
1084 if (eData[0].frame_info->borders[i] != eData[1].frame_info->borders[i]) {
1085 stereoMode = SBR_LEFT_RIGHT;
1086 break;
1087 }
1088 }
1089 for (i = 0; i < eData[0].frame_info->nEnvelopes; i++) {
1090 if (eData[0].frame_info->freqRes[i] != eData[1].frame_info->freqRes[i]) {
1091 stereoMode = SBR_LEFT_RIGHT;
1092 break;
1093 }
1094 }
1095 if (eData[0].frame_info->shortEnv != eData[1].frame_info->shortEnv) {
1096 stereoMode = SBR_LEFT_RIGHT;
1097 }
1098 }
1099 break;
1100 case SBR_COUPLING:
1101 eData[1].frame_info = eData[0].frame_info;
1102 h_envChan[1]->encEnvData.hSbrBSGrid = &h_envChan[0]->SbrEnvFrame.SbrGrid;
1103 break;
1104 case SBR_MONO:
1105 /* nothing to do */
1106 break;
1107 default:
1108 FDK_ASSERT (0);
1109 }
1110
1111
1112 for (ch = 0; ch < nChannels;ch++)
1113 {
1114 HANDLE_ENV_CHANNEL hEnvChan = h_envChan[ch];
1115 HANDLE_SBR_EXTRACT_ENVELOPE sbrExtrEnv = &hEnvChan->sbrExtractEnvelope;
1116 SBR_ENV_TEMP_DATA *ed = &eData[ch];
1117
1118
1119 /*
1120 Send transient info to bitstream and store for next call
1121 */
1122 sbrExtrEnv->pre_transient_info[0] = ed->transient_info[0];/* tran_pos */
1123 sbrExtrEnv->pre_transient_info[1] = ed->transient_info[1];/* tran_flag */
1124 hEnvChan->encEnvData.noOfEnvelopes = ed->nEnvelopes = ed->frame_info->nEnvelopes; /* number of envelopes of current frame */
1125
1126 /*
1127 Check if the current frame is divided into one envelope only. If so, set the amplitude
1128 resolution to 1.5 dB, otherwise may set back to chosen value
1129 */
1130 if( ( hEnvChan->encEnvData.hSbrBSGrid->frameClass == FIXFIX )
1131 && ( ed->nEnvelopes == 1 ) )
1132 {
1133
1134 if (hEnvChan->encEnvData.ldGrid)
1135 hEnvChan->encEnvData.currentAmpResFF = (AMP_RES)h_con->initAmpResFF;
1136 else
1137 hEnvChan->encEnvData.currentAmpResFF = SBR_AMP_RES_1_5;
1138
1139 if ( hEnvChan->encEnvData.currentAmpResFF != hEnvChan->encEnvData.init_sbr_amp_res) {
1140
1141 FDKsbrEnc_InitSbrHuffmanTables(&hEnvChan->encEnvData,
1142 &hEnvChan->sbrCodeEnvelope,
1143 &hEnvChan->sbrCodeNoiseFloor,
1144 hEnvChan->encEnvData.currentAmpResFF);
1145 }
1146 }
1147 else {
1148 if(sbrHeaderData->sbr_amp_res != hEnvChan->encEnvData.init_sbr_amp_res ) {
1149
1150 FDKsbrEnc_InitSbrHuffmanTables(&hEnvChan->encEnvData,
1151 &hEnvChan->sbrCodeEnvelope,
1152 &hEnvChan->sbrCodeNoiseFloor,
1153 sbrHeaderData->sbr_amp_res);
1154 }
1155 }
1156
1157 if (!clearOutput) {
1158
1159 /*
1160 Tonality correction parameter extraction (inverse filtering level, noise floor additional sines).
1161 */
1162 FDKsbrEnc_TonCorrParamExtr(&hEnvChan->TonCorr,
1163 hEnvChan->encEnvData.sbr_invf_mode_vec,
1164 ed->noiseFloor,
1165 &hEnvChan->encEnvData.addHarmonicFlag,
1166 hEnvChan->encEnvData.addHarmonic,
1167 sbrExtrEnv->envelopeCompensation,
1168 ed->frame_info,
1169 ed->transient_info,
1170 h_con->freqBandTable[HI],
1171 h_con->nSfb[HI],
1172 hEnvChan->encEnvData.sbr_xpos_mode,
1173 h_con->sbrSyntaxFlags);
1174
1175 }
1176
1177 /* Low energy in low band fix */
1178 if ( hEnvChan->sbrTransientDetector.prevLowBandEnergy < hEnvChan->sbrTransientDetector.prevHighBandEnergy && hEnvChan->sbrTransientDetector.prevHighBandEnergy > FL2FX_DBL(0.03))
1179 {
1180 int i;
1181
1182 hEnvChan->fLevelProtect = 1;
1183
1184 for (i=0; i<MAX_NUM_NOISE_VALUES; i++)
1185 hEnvChan->encEnvData.sbr_invf_mode_vec[i] = INVF_HIGH_LEVEL;
1186 } else {
1187 hEnvChan->fLevelProtect = 0;
1188 }
1189
1190 hEnvChan->encEnvData.sbr_invf_mode = hEnvChan->encEnvData.sbr_invf_mode_vec[0];
1191
1192 hEnvChan->encEnvData.noOfnoisebands = hEnvChan->TonCorr.sbrNoiseFloorEstimate.noNoiseBands;
1193
1194
1195 } /* ch */
1196
1197
1198
1199 /*
1200 Save number of scf bands per envelope
1201 */
1202 for (ch = 0; ch < nChannels;ch++) {
1203 for (i = 0; i < eData[ch].nEnvelopes; i++){
1204 h_envChan[ch]->encEnvData.noScfBands[i] =
1205 (eData[ch].frame_info->freqRes[i] == FREQ_RES_HIGH ? h_con->nSfb[FREQ_RES_HIGH] : h_con->nSfb[FREQ_RES_LOW]);
1206 }
1207 }
1208
1209 /*
1210 Extract envelope of current frame.
1211 */
1212 switch (stereoMode) {
1213 case SBR_MONO:
1214 calculateSbrEnvelope (h_envChan[0]->sbrExtractEnvelope.YBuffer, NULL,
1215 h_envChan[0]->sbrExtractEnvelope.YBufferScale, NULL,
1216 eData[0].frame_info, eData[0].sfb_nrg, NULL,
1217 h_con, h_envChan[0], SBR_MONO, NULL, YSzShift);
1218 break;
1219 case SBR_LEFT_RIGHT:
1220 calculateSbrEnvelope (h_envChan[0]->sbrExtractEnvelope.YBuffer, NULL,
1221 h_envChan[0]->sbrExtractEnvelope.YBufferScale, NULL,
1222 eData[0].frame_info, eData[0].sfb_nrg, NULL,
1223 h_con, h_envChan[0], SBR_MONO, NULL, YSzShift);
1224 calculateSbrEnvelope (h_envChan[1]->sbrExtractEnvelope.YBuffer, NULL,
1225 h_envChan[1]->sbrExtractEnvelope.YBufferScale, NULL,
1226 eData[1].frame_info,eData[1].sfb_nrg, NULL,
1227 h_con, h_envChan[1], SBR_MONO, NULL, YSzShift);
1228 break;
1229 case SBR_COUPLING:
1230 calculateSbrEnvelope (h_envChan[0]->sbrExtractEnvelope.YBuffer, h_envChan[1]->sbrExtractEnvelope.YBuffer,
1231 h_envChan[0]->sbrExtractEnvelope.YBufferScale, h_envChan[1]->sbrExtractEnvelope.YBufferScale,
1232 eData[0].frame_info, eData[0].sfb_nrg, eData[1].sfb_nrg,
1233 h_con, h_envChan[0], SBR_COUPLING, &fData->maxQuantError, YSzShift);
1234 break;
1235 case SBR_SWITCH_LRC:
1236 calculateSbrEnvelope (h_envChan[0]->sbrExtractEnvelope.YBuffer, NULL,
1237 h_envChan[0]->sbrExtractEnvelope.YBufferScale, NULL,
1238 eData[0].frame_info, eData[0].sfb_nrg, NULL,
1239 h_con, h_envChan[0], SBR_MONO, NULL, YSzShift);
1240 calculateSbrEnvelope (h_envChan[1]->sbrExtractEnvelope.YBuffer, NULL,
1241 h_envChan[1]->sbrExtractEnvelope.YBufferScale, NULL,
1242 eData[1].frame_info, eData[1].sfb_nrg, NULL,
1243 h_con, h_envChan[1], SBR_MONO,NULL, YSzShift);
1244 calculateSbrEnvelope (h_envChan[0]->sbrExtractEnvelope.YBuffer, h_envChan[1]->sbrExtractEnvelope.YBuffer,
1245 h_envChan[0]->sbrExtractEnvelope.YBufferScale, h_envChan[1]->sbrExtractEnvelope.YBufferScale,
1246 eData[0].frame_info, eData[0].sfb_nrg_coupling, eData[1].sfb_nrg_coupling,
1247 h_con, h_envChan[0], SBR_COUPLING, &fData->maxQuantError, YSzShift);
1248 break;
1249 }
1250
1251
1252
1253 /*
1254 Noise floor quantisation and coding.
1255 */
1256
1257 switch (stereoMode) {
1258 case SBR_MONO:
1259 sbrNoiseFloorLevelsQuantisation(eData[0].noise_level, eData[0].noiseFloor, 0);
1260
1261 FDKsbrEnc_codeEnvelope(eData[0].noise_level, fData->res,
1262 &h_envChan[0]->sbrCodeNoiseFloor,
1263 h_envChan[0]->encEnvData.domain_vec_noise, 0,
1264 (eData[0].frame_info->nEnvelopes > 1 ? 2 : 1), 0,
1265 sbrBitstreamData->HeaderActive);
1266
1267 break;
1268 case SBR_LEFT_RIGHT:
1269 sbrNoiseFloorLevelsQuantisation(eData[0].noise_level,eData[0].noiseFloor, 0);
1270
1271 FDKsbrEnc_codeEnvelope (eData[0].noise_level, fData->res,
1272 &h_envChan[0]->sbrCodeNoiseFloor,
1273 h_envChan[0]->encEnvData.domain_vec_noise, 0,
1274 (eData[0].frame_info->nEnvelopes > 1 ? 2 : 1), 0,
1275 sbrBitstreamData->HeaderActive);
1276
1277 sbrNoiseFloorLevelsQuantisation(eData[1].noise_level,eData[1].noiseFloor, 0);
1278
1279 FDKsbrEnc_codeEnvelope (eData[1].noise_level, fData->res,
1280 &h_envChan[1]->sbrCodeNoiseFloor,
1281 h_envChan[1]->encEnvData.domain_vec_noise, 0,
1282 (eData[1].frame_info->nEnvelopes > 1 ? 2 : 1), 0,
1283 sbrBitstreamData->HeaderActive);
1284
1285 break;
1286
1287 case SBR_COUPLING:
1288 coupleNoiseFloor(eData[0].noiseFloor,eData[1].noiseFloor);
1289
1290 sbrNoiseFloorLevelsQuantisation(eData[0].noise_level,eData[0].noiseFloor, 0);
1291
1292 FDKsbrEnc_codeEnvelope (eData[0].noise_level, fData->res,
1293 &h_envChan[0]->sbrCodeNoiseFloor,
1294 h_envChan[0]->encEnvData.domain_vec_noise, 1,
1295 (eData[0].frame_info->nEnvelopes > 1 ? 2 : 1), 0,
1296 sbrBitstreamData->HeaderActive);
1297
1298 sbrNoiseFloorLevelsQuantisation(eData[1].noise_level,eData[1].noiseFloor, 1);
1299
1300 FDKsbrEnc_codeEnvelope (eData[1].noise_level, fData->res,
1301 &h_envChan[1]->sbrCodeNoiseFloor,
1302 h_envChan[1]->encEnvData.domain_vec_noise, 1,
1303 (eData[1].frame_info->nEnvelopes > 1 ? 2 : 1), 1,
1304 sbrBitstreamData->HeaderActive);
1305
1306 break;
1307 case SBR_SWITCH_LRC:
1308 sbrNoiseFloorLevelsQuantisation(eData[0].noise_level,eData[0].noiseFloor, 0);
1309 sbrNoiseFloorLevelsQuantisation(eData[1].noise_level,eData[1].noiseFloor, 0);
1310 coupleNoiseFloor(eData[0].noiseFloor,eData[1].noiseFloor);
1311 sbrNoiseFloorLevelsQuantisation(eData[0].noise_level_coupling,eData[0].noiseFloor, 0);
1312 sbrNoiseFloorLevelsQuantisation(eData[1].noise_level_coupling,eData[1].noiseFloor, 1);
1313 break;
1314 }
1315
1316
1317
1318 /*
1319 Encode envelope of current frame.
1320 */
1321 switch (stereoMode) {
1322 case SBR_MONO:
1323 sbrHeaderData->coupling = 0;
1324 h_envChan[0]->encEnvData.balance = 0;
1325 FDKsbrEnc_codeEnvelope (eData[0].sfb_nrg, eData[0].frame_info->freqRes,
1326 &h_envChan[0]->sbrCodeEnvelope,
1327 h_envChan[0]->encEnvData.domain_vec,
1328 sbrHeaderData->coupling,
1329 eData[0].frame_info->nEnvelopes, 0,
1330 sbrBitstreamData->HeaderActive);
1331 break;
1332 case SBR_LEFT_RIGHT:
1333 sbrHeaderData->coupling = 0;
1334
1335 h_envChan[0]->encEnvData.balance = 0;
1336 h_envChan[1]->encEnvData.balance = 0;
1337
1338
1339 FDKsbrEnc_codeEnvelope (eData[0].sfb_nrg, eData[0].frame_info->freqRes,
1340 &h_envChan[0]->sbrCodeEnvelope,
1341 h_envChan[0]->encEnvData.domain_vec,
1342 sbrHeaderData->coupling,
1343 eData[0].frame_info->nEnvelopes, 0,
1344 sbrBitstreamData->HeaderActive);
1345 FDKsbrEnc_codeEnvelope (eData[1].sfb_nrg, eData[1].frame_info->freqRes,
1346 &h_envChan[1]->sbrCodeEnvelope,
1347 h_envChan[1]->encEnvData.domain_vec,
1348 sbrHeaderData->coupling,
1349 eData[1].frame_info->nEnvelopes, 0,
1350 sbrBitstreamData->HeaderActive);
1351 break;
1352 case SBR_COUPLING:
1353 sbrHeaderData->coupling = 1;
1354 h_envChan[0]->encEnvData.balance = 0;
1355 h_envChan[1]->encEnvData.balance = 1;
1356
1357 FDKsbrEnc_codeEnvelope (eData[0].sfb_nrg, eData[0].frame_info->freqRes,
1358 &h_envChan[0]->sbrCodeEnvelope,
1359 h_envChan[0]->encEnvData.domain_vec,
1360 sbrHeaderData->coupling,
1361 eData[0].frame_info->nEnvelopes, 0,
1362 sbrBitstreamData->HeaderActive);
1363 FDKsbrEnc_codeEnvelope (eData[1].sfb_nrg, eData[1].frame_info->freqRes,
1364 &h_envChan[1]->sbrCodeEnvelope,
1365 h_envChan[1]->encEnvData.domain_vec,
1366 sbrHeaderData->coupling,
1367 eData[1].frame_info->nEnvelopes, 1,
1368 sbrBitstreamData->HeaderActive);
1369 break;
1370 case SBR_SWITCH_LRC:
1371 {
1372 INT payloadbitsLR;
1373 INT payloadbitsCOUPLING;
1374
1375 SCHAR sfbNrgPrevTemp[MAX_NUM_CHANNELS][MAX_FREQ_COEFFS];
1376 SCHAR noisePrevTemp[MAX_NUM_CHANNELS][MAX_NUM_NOISE_COEFFS];
1377 INT upDateNrgTemp[MAX_NUM_CHANNELS];
1378 INT upDateNoiseTemp[MAX_NUM_CHANNELS];
1379 INT domainVecTemp[MAX_NUM_CHANNELS][MAX_ENVELOPES];
1380 INT domainVecNoiseTemp[MAX_NUM_CHANNELS][MAX_ENVELOPES];
1381
1382 INT tempFlagRight = 0;
1383 INT tempFlagLeft = 0;
1384
1385 /*
1386 Store previous values, in order to be able to "undo" what is being done.
1387 */
1388
1389 for(ch = 0; ch < nChannels;ch++){
1390 FDKmemcpy (sfbNrgPrevTemp[ch], h_envChan[ch]->sbrCodeEnvelope.sfb_nrg_prev,
1391 MAX_FREQ_COEFFS * sizeof (SCHAR));
1392
1393 FDKmemcpy (noisePrevTemp[ch], h_envChan[ch]->sbrCodeNoiseFloor.sfb_nrg_prev,
1394 MAX_NUM_NOISE_COEFFS * sizeof (SCHAR));
1395
1396 upDateNrgTemp[ch] = h_envChan[ch]->sbrCodeEnvelope.upDate;
1397 upDateNoiseTemp[ch] = h_envChan[ch]->sbrCodeNoiseFloor.upDate;
1398
1399 /*
1400 forbid time coding in the first envelope in case of a different
1401 previous stereomode
1402 */
1403 if(sbrHeaderData->prev_coupling){
1404 h_envChan[ch]->sbrCodeEnvelope.upDate = 0;
1405 h_envChan[ch]->sbrCodeNoiseFloor.upDate = 0;
1406 }
1407 } /* ch */
1408
1409
1410 /*
1411 Code ordinary Left/Right stereo
1412 */
1413 FDKsbrEnc_codeEnvelope (eData[0].sfb_nrg, eData[0].frame_info->freqRes,
1414 &h_envChan[0]->sbrCodeEnvelope,
1415 h_envChan[0]->encEnvData.domain_vec, 0,
1416 eData[0].frame_info->nEnvelopes, 0,
1417 sbrBitstreamData->HeaderActive);
1418 FDKsbrEnc_codeEnvelope (eData[1].sfb_nrg, eData[1].frame_info->freqRes,
1419 &h_envChan[1]->sbrCodeEnvelope,
1420 h_envChan[1]->encEnvData.domain_vec, 0,
1421 eData[1].frame_info->nEnvelopes, 0,
1422 sbrBitstreamData->HeaderActive);
1423
1424 c = 0;
1425 for (i = 0; i < eData[0].nEnvelopes; i++) {
1426 for (j = 0; j < h_envChan[0]->encEnvData.noScfBands[i]; j++)
1427 {
1428 h_envChan[0]->encEnvData.ienvelope[i][j] = eData[0].sfb_nrg[c];
1429 h_envChan[1]->encEnvData.ienvelope[i][j] = eData[1].sfb_nrg[c];
1430 c++;
1431 }
1432 }
1433
1434
1435
1436 FDKsbrEnc_codeEnvelope (eData[0].noise_level, fData->res,
1437 &h_envChan[0]->sbrCodeNoiseFloor,
1438 h_envChan[0]->encEnvData.domain_vec_noise, 0,
1439 (eData[0].frame_info->nEnvelopes > 1 ? 2 : 1), 0,
1440 sbrBitstreamData->HeaderActive);
1441
1442
1443 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++)
1444 h_envChan[0]->encEnvData.sbr_noise_levels[i] = eData[0].noise_level[i];
1445
1446
1447 FDKsbrEnc_codeEnvelope (eData[1].noise_level, fData->res,
1448 &h_envChan[1]->sbrCodeNoiseFloor,
1449 h_envChan[1]->encEnvData.domain_vec_noise, 0,
1450 (eData[1].frame_info->nEnvelopes > 1 ? 2 : 1), 0,
1451 sbrBitstreamData->HeaderActive);
1452
1453 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++)
1454 h_envChan[1]->encEnvData.sbr_noise_levels[i] = eData[1].noise_level[i];
1455
1456
1457 sbrHeaderData->coupling = 0;
1458 h_envChan[0]->encEnvData.balance = 0;
1459 h_envChan[1]->encEnvData.balance = 0;
1460
1461 payloadbitsLR = FDKsbrEnc_CountSbrChannelPairElement (sbrHeaderData,
1462 hParametricStereo,
1463 sbrBitstreamData,
1464 &h_envChan[0]->encEnvData,
1465 &h_envChan[1]->encEnvData,
1466 hCmonData,
1467 h_con->sbrSyntaxFlags);
1468
1469 /*
1470 swap saved stored with current values
1471 */
1472 for(ch = 0; ch < nChannels;ch++){
1473 INT itmp;
1474 for(i=0;i<MAX_FREQ_COEFFS;i++){
1475 /*
1476 swap sfb energies
1477 */
1478 itmp = h_envChan[ch]->sbrCodeEnvelope.sfb_nrg_prev[i];
1479 h_envChan[ch]->sbrCodeEnvelope.sfb_nrg_prev[i]=sfbNrgPrevTemp[ch][i];
1480 sfbNrgPrevTemp[ch][i]=itmp;
1481 }
1482 for(i=0;i<MAX_NUM_NOISE_COEFFS;i++){
1483 /*
1484 swap noise energies
1485 */
1486 itmp = h_envChan[ch]->sbrCodeNoiseFloor.sfb_nrg_prev[i];
1487 h_envChan[ch]->sbrCodeNoiseFloor.sfb_nrg_prev[i]=noisePrevTemp[ch][i];
1488 noisePrevTemp[ch][i]=itmp;
1489 }
1490 /* swap update flags */
1491 itmp = h_envChan[ch]->sbrCodeEnvelope.upDate;
1492 h_envChan[ch]->sbrCodeEnvelope.upDate=upDateNrgTemp[ch];
1493 upDateNrgTemp[ch] = itmp;
1494
1495 itmp = h_envChan[ch]->sbrCodeNoiseFloor.upDate;
1496 h_envChan[ch]->sbrCodeNoiseFloor.upDate=upDateNoiseTemp[ch];
1497 upDateNoiseTemp[ch]=itmp;
1498
1499 /*
1500 save domain vecs
1501 */
1502 FDKmemcpy(domainVecTemp[ch],h_envChan[ch]->encEnvData.domain_vec,sizeof(INT)*MAX_ENVELOPES);
1503 FDKmemcpy(domainVecNoiseTemp[ch],h_envChan[ch]->encEnvData.domain_vec_noise,sizeof(INT)*MAX_ENVELOPES);
1504
1505 /*
1506 forbid time coding in the first envelope in case of a different
1507 previous stereomode
1508 */
1509
1510 if(!sbrHeaderData->prev_coupling){
1511 h_envChan[ch]->sbrCodeEnvelope.upDate = 0;
1512 h_envChan[ch]->sbrCodeNoiseFloor.upDate = 0;
1513 }
1514 } /* ch */
1515
1516
1517 /*
1518 Coupling
1519 */
1520
1521 FDKsbrEnc_codeEnvelope (eData[0].sfb_nrg_coupling, eData[0].frame_info->freqRes,
1522 &h_envChan[0]->sbrCodeEnvelope,
1523 h_envChan[0]->encEnvData.domain_vec, 1,
1524 eData[0].frame_info->nEnvelopes, 0,
1525 sbrBitstreamData->HeaderActive);
1526
1527 FDKsbrEnc_codeEnvelope (eData[1].sfb_nrg_coupling, eData[1].frame_info->freqRes,
1528 &h_envChan[1]->sbrCodeEnvelope,
1529 h_envChan[1]->encEnvData.domain_vec, 1,
1530 eData[1].frame_info->nEnvelopes, 1,
1531 sbrBitstreamData->HeaderActive);
1532
1533
1534 c = 0;
1535 for (i = 0; i < eData[0].nEnvelopes; i++) {
1536 for (j = 0; j < h_envChan[0]->encEnvData.noScfBands[i]; j++) {
1537 h_envChan[0]->encEnvData.ienvelope[i][j] = eData[0].sfb_nrg_coupling[c];
1538 h_envChan[1]->encEnvData.ienvelope[i][j] = eData[1].sfb_nrg_coupling[c];
1539 c++;
1540 }
1541 }
1542
1543 FDKsbrEnc_codeEnvelope (eData[0].noise_level_coupling, fData->res,
1544 &h_envChan[0]->sbrCodeNoiseFloor,
1545 h_envChan[0]->encEnvData.domain_vec_noise, 1,
1546 (eData[0].frame_info->nEnvelopes > 1 ? 2 : 1), 0,
1547 sbrBitstreamData->HeaderActive);
1548
1549 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++)
1550 h_envChan[0]->encEnvData.sbr_noise_levels[i] = eData[0].noise_level_coupling[i];
1551
1552
1553 FDKsbrEnc_codeEnvelope (eData[1].noise_level_coupling, fData->res,
1554 &h_envChan[1]->sbrCodeNoiseFloor,
1555 h_envChan[1]->encEnvData.domain_vec_noise, 1,
1556 (eData[1].frame_info->nEnvelopes > 1 ? 2 : 1), 1,
1557 sbrBitstreamData->HeaderActive);
1558
1559 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++)
1560 h_envChan[1]->encEnvData.sbr_noise_levels[i] = eData[1].noise_level_coupling[i];
1561
1562 sbrHeaderData->coupling = 1;
1563
1564 h_envChan[0]->encEnvData.balance = 0;
1565 h_envChan[1]->encEnvData.balance = 1;
1566
1567 tempFlagLeft = h_envChan[0]->encEnvData.addHarmonicFlag;
1568 tempFlagRight = h_envChan[1]->encEnvData.addHarmonicFlag;
1569
1570 payloadbitsCOUPLING =
1571 FDKsbrEnc_CountSbrChannelPairElement (sbrHeaderData,
1572 hParametricStereo,
1573 sbrBitstreamData,
1574 &h_envChan[0]->encEnvData,
1575 &h_envChan[1]->encEnvData,
1576 hCmonData,
1577 h_con->sbrSyntaxFlags);
1578
1579
1580 h_envChan[0]->encEnvData.addHarmonicFlag = tempFlagLeft;
1581 h_envChan[1]->encEnvData.addHarmonicFlag = tempFlagRight;
1582
1583 if (payloadbitsCOUPLING < payloadbitsLR) {
1584
1585 /*
1586 copy coded coupling envelope and noise data to l/r
1587 */
1588 for(ch = 0; ch < nChannels;ch++){
1589 SBR_ENV_TEMP_DATA *ed = &eData[ch];
1590 FDKmemcpy (ed->sfb_nrg, ed->sfb_nrg_coupling,
1591 MAX_NUM_ENVELOPE_VALUES * sizeof (SCHAR));
1592 FDKmemcpy (ed->noise_level, ed->noise_level_coupling,
1593 MAX_NUM_NOISE_VALUES * sizeof (SCHAR));
1594 }
1595
1596 sbrHeaderData->coupling = 1;
1597 h_envChan[0]->encEnvData.balance = 0;
1598 h_envChan[1]->encEnvData.balance = 1;
1599 }
1600 else{
1601 /*
1602 restore saved l/r items
1603 */
1604 for(ch = 0; ch < nChannels;ch++){
1605
1606 FDKmemcpy (h_envChan[ch]->sbrCodeEnvelope.sfb_nrg_prev,
1607 sfbNrgPrevTemp[ch], MAX_FREQ_COEFFS * sizeof (SCHAR));
1608
1609 h_envChan[ch]->sbrCodeEnvelope.upDate = upDateNrgTemp[ch];
1610
1611 FDKmemcpy (h_envChan[ch]->sbrCodeNoiseFloor.sfb_nrg_prev,
1612 noisePrevTemp[ch], MAX_NUM_NOISE_COEFFS * sizeof (SCHAR));
1613
1614 FDKmemcpy (h_envChan[ch]->encEnvData.domain_vec,domainVecTemp[ch],sizeof(INT)*MAX_ENVELOPES);
1615 FDKmemcpy (h_envChan[ch]->encEnvData.domain_vec_noise,domainVecNoiseTemp[ch],sizeof(INT)*MAX_ENVELOPES);
1616
1617 h_envChan[ch]->sbrCodeNoiseFloor.upDate = upDateNoiseTemp[ch];
1618 }
1619
1620 sbrHeaderData->coupling = 0;
1621 h_envChan[0]->encEnvData.balance = 0;
1622 h_envChan[1]->encEnvData.balance = 0;
1623 }
1624 }
1625 break;
1626 } /* switch */
1627
1628
1629 /* tell the envelope encoders how long it has been, since we last sent
1630 a frame starting with a dF-coded envelope */
1631 if (stereoMode == SBR_MONO ) {
1632 if (h_envChan[0]->encEnvData.domain_vec[0] == TIME)
1633 h_envChan[0]->sbrCodeEnvelope.dF_edge_incr_fac++;
1634 else
1635 h_envChan[0]->sbrCodeEnvelope.dF_edge_incr_fac = 0;
1636 }
1637 else {
1638 if (h_envChan[0]->encEnvData.domain_vec[0] == TIME ||
1639 h_envChan[1]->encEnvData.domain_vec[0] == TIME) {
1640 h_envChan[0]->sbrCodeEnvelope.dF_edge_incr_fac++;
1641 h_envChan[1]->sbrCodeEnvelope.dF_edge_incr_fac++;
1642 }
1643 else {
1644 h_envChan[0]->sbrCodeEnvelope.dF_edge_incr_fac = 0;
1645 h_envChan[1]->sbrCodeEnvelope.dF_edge_incr_fac = 0;
1646 }
1647 }
1648
1649 /*
1650 Send the encoded data to the bitstream
1651 */
1652 for(ch = 0; ch < nChannels;ch++){
1653 SBR_ENV_TEMP_DATA *ed = &eData[ch];
1654 c = 0;
1655 for (i = 0; i < ed->nEnvelopes; i++) {
1656 for (j = 0; j < h_envChan[ch]->encEnvData.noScfBands[i]; j++) {
1657 h_envChan[ch]->encEnvData.ienvelope[i][j] = ed->sfb_nrg[c];
1658
1659 c++;
1660 }
1661 }
1662 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++){
1663 h_envChan[ch]->encEnvData.sbr_noise_levels[i] = ed->noise_level[i];
1664 }
1665 }/* ch */
1666
1667
1668 /*
1669 Write bitstream
1670 */
1671 if (nChannels == 2) {
1672 FDKsbrEnc_WriteEnvChannelPairElement(sbrHeaderData,
1673 hParametricStereo,
1674 sbrBitstreamData,
1675 &h_envChan[0]->encEnvData,
1676 &h_envChan[1]->encEnvData,
1677 hCmonData,
1678 h_con->sbrSyntaxFlags);
1679 }
1680 else {
1681 FDKsbrEnc_WriteEnvSingleChannelElement(sbrHeaderData,
1682 hParametricStereo,
1683 sbrBitstreamData,
1684 &h_envChan[0]->encEnvData,
1685 hCmonData,
1686 h_con->sbrSyntaxFlags);
1687 }
1688
1689 /*
1690 * Update buffers.
1691 */
1692 for (ch=0; ch<nChannels; ch++)
1693 {
1694 int YBufferLength = h_envChan[ch]->sbrExtractEnvelope.no_cols >> h_envChan[ch]->sbrExtractEnvelope.YBufferSzShift;
1695 for (i = 0; i < h_envChan[ch]->sbrExtractEnvelope.YBufferWriteOffset; i++) {
1696 FDKmemcpy(h_envChan[ch]->sbrExtractEnvelope.YBuffer[i],
1697 h_envChan[ch]->sbrExtractEnvelope.YBuffer[i + YBufferLength],
1698 sizeof(FIXP_DBL)*QMF_CHANNELS);
1699 }
1700 h_envChan[ch]->sbrExtractEnvelope.YBufferScale[0] = h_envChan[ch]->sbrExtractEnvelope.YBufferScale[1];
1701 }
1702
1703 sbrHeaderData->prev_coupling = sbrHeaderData->coupling;
1704 }
1705
1706 /***************************************************************************/
1707 /*!
1708
1709 \brief creates an envelope extractor handle
1710
1711 \return error status
1712
1713 ****************************************************************************/
1714 INT
FDKsbrEnc_CreateExtractSbrEnvelope(HANDLE_SBR_EXTRACT_ENVELOPE hSbrCut,INT channel,INT chInEl,UCHAR * dynamic_RAM)1715 FDKsbrEnc_CreateExtractSbrEnvelope (HANDLE_SBR_EXTRACT_ENVELOPE hSbrCut,
1716 INT channel
1717 ,INT chInEl
1718 ,UCHAR* dynamic_RAM
1719 )
1720 {
1721 INT i;
1722 FIXP_DBL* YBuffer = GetRam_Sbr_envYBuffer(channel);
1723
1724 FDKmemclear(hSbrCut,sizeof(SBR_EXTRACT_ENVELOPE));
1725 hSbrCut->p_YBuffer = YBuffer;
1726
1727
1728 for (i = 0; i < (QMF_MAX_TIME_SLOTS>>1); i++) {
1729 hSbrCut->YBuffer[i] = YBuffer + (i*QMF_CHANNELS);
1730 }
1731 FIXP_DBL *YBufferDyn = GetRam_Sbr_envYBuffer(chInEl, dynamic_RAM);
1732 INT n=0;
1733 for (; i < QMF_MAX_TIME_SLOTS; i++,n++) {
1734 hSbrCut->YBuffer[i] = YBufferDyn + (n*QMF_CHANNELS);
1735 }
1736
1737 FIXP_DBL* rBuffer = GetRam_Sbr_envRBuffer(0, dynamic_RAM);
1738 FIXP_DBL* iBuffer = GetRam_Sbr_envIBuffer(0, dynamic_RAM);
1739
1740 for (i = 0; i < QMF_MAX_TIME_SLOTS; i++) {
1741 hSbrCut->rBuffer[i] = rBuffer + (i*QMF_CHANNELS);
1742 hSbrCut->iBuffer[i] = iBuffer + (i*QMF_CHANNELS);
1743 }
1744
1745 return 0;
1746 }
1747
1748
1749 /***************************************************************************/
1750 /*!
1751
1752 \brief Initialize an envelope extractor instance.
1753
1754 \return error status
1755
1756 ****************************************************************************/
1757 INT
FDKsbrEnc_InitExtractSbrEnvelope(HANDLE_SBR_EXTRACT_ENVELOPE hSbrCut,int no_cols,int no_rows,int start_index,int time_slots,int time_step,int tran_off,ULONG statesInitFlag,int chInEl,UCHAR * dynamic_RAM,UINT sbrSyntaxFlags)1758 FDKsbrEnc_InitExtractSbrEnvelope (HANDLE_SBR_EXTRACT_ENVELOPE hSbrCut,
1759 int no_cols,
1760 int no_rows,
1761 int start_index,
1762 int time_slots,
1763 int time_step,
1764 int tran_off,
1765 ULONG statesInitFlag
1766 ,int chInEl
1767 ,UCHAR* dynamic_RAM
1768 ,UINT sbrSyntaxFlags
1769 )
1770 {
1771 int YBufferLength, rBufferLength;
1772 int i;
1773
1774 if (sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY) {
1775 int off = TRANSIENT_OFFSET_LD;
1776 #ifndef FULL_DELAY
1777 hSbrCut->YBufferWriteOffset = (no_cols>>1)+off*time_step;
1778 #else
1779 hSbrCut->YBufferWriteOffset = no_cols+off*time_step;
1780 #endif
1781 } else
1782 {
1783 hSbrCut->YBufferWriteOffset = tran_off*time_step;
1784 }
1785 hSbrCut->rBufferReadOffset = 0;
1786
1787
1788 YBufferLength = hSbrCut->YBufferWriteOffset + no_cols;
1789 rBufferLength = no_cols;
1790
1791 hSbrCut->pre_transient_info[0] = 0;
1792 hSbrCut->pre_transient_info[1] = 0;
1793
1794
1795 hSbrCut->no_cols = no_cols;
1796 hSbrCut->no_rows = no_rows;
1797 hSbrCut->start_index = start_index;
1798
1799 hSbrCut->time_slots = time_slots;
1800 hSbrCut->time_step = time_step;
1801
1802 FDK_ASSERT(no_rows <= QMF_CHANNELS);
1803
1804 /* Use half the Energy values if time step is 2 or greater */
1805 if (time_step >= 2)
1806 hSbrCut->YBufferSzShift = 1;
1807 else
1808 hSbrCut->YBufferSzShift = 0;
1809
1810 YBufferLength >>= hSbrCut->YBufferSzShift;
1811 hSbrCut->YBufferWriteOffset >>= hSbrCut->YBufferSzShift;
1812
1813 FDK_ASSERT(YBufferLength<=QMF_MAX_TIME_SLOTS);
1814
1815 FIXP_DBL *YBufferDyn = GetRam_Sbr_envYBuffer(chInEl, dynamic_RAM);
1816 INT n=0;
1817 for (i=(QMF_MAX_TIME_SLOTS>>1); i < QMF_MAX_TIME_SLOTS; i++,n++) {
1818 hSbrCut->YBuffer[i] = YBufferDyn + (n*QMF_CHANNELS);
1819 }
1820
1821 if(statesInitFlag) {
1822 for (i=0; i<YBufferLength; i++) {
1823 FDKmemclear( hSbrCut->YBuffer[i],QMF_CHANNELS*sizeof(FIXP_DBL));
1824 }
1825 }
1826
1827 for (i = 0; i < rBufferLength; i++) {
1828 FDKmemclear( hSbrCut->rBuffer[i],QMF_CHANNELS*sizeof(FIXP_DBL));
1829 FDKmemclear( hSbrCut->iBuffer[i],QMF_CHANNELS*sizeof(FIXP_DBL));
1830 }
1831
1832 FDKmemclear (hSbrCut->envelopeCompensation,sizeof(UCHAR)*MAX_FREQ_COEFFS);
1833
1834 if(statesInitFlag) {
1835 hSbrCut->YBufferScale[0] = hSbrCut->YBufferScale[1] = FRACT_BITS-1;
1836 }
1837
1838 return (0);
1839 }
1840
1841
1842
1843
1844 /***************************************************************************/
1845 /*!
1846
1847 \brief deinitializes an envelope extractor handle
1848
1849 \return void
1850
1851 ****************************************************************************/
1852
1853 void
FDKsbrEnc_deleteExtractSbrEnvelope(HANDLE_SBR_EXTRACT_ENVELOPE hSbrCut)1854 FDKsbrEnc_deleteExtractSbrEnvelope (HANDLE_SBR_EXTRACT_ENVELOPE hSbrCut)
1855 {
1856
1857 if (hSbrCut) {
1858 FreeRam_Sbr_envYBuffer(&hSbrCut->p_YBuffer);
1859 }
1860 }
1861
1862 INT
FDKsbrEnc_GetEnvEstDelay(HANDLE_SBR_EXTRACT_ENVELOPE hSbr)1863 FDKsbrEnc_GetEnvEstDelay(HANDLE_SBR_EXTRACT_ENVELOPE hSbr)
1864 {
1865 return hSbr->no_rows*((hSbr->YBufferWriteOffset)*2 /* mult 2 because nrg's are grouped half */
1866 - hSbr->rBufferReadOffset ); /* in reference hold half spec and calc nrg's on overlapped spec */
1867
1868 }
1869
1870
1871
1872
1873