1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5 � Copyright 1995 - 2015 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 /*************************** Fraunhofer IIS FDK Tools ***********************
85
86 Author(s): Andreas Ehret, Tobias Chalupka
87 Description: SBR encoder top level processing.
88
89 ******************************************************************************/
90
91 #include "sbr_encoder.h"
92
93 #include "sbr_ram.h"
94 #include "sbr_rom.h"
95 #include "sbrenc_freq_sca.h"
96 #include "env_bit.h"
97 #include "cmondata.h"
98 #include "sbr_misc.h"
99 #include "sbr.h"
100 #include "qmf.h"
101
102 #include "ps_main.h"
103
104 #define SBRENCODER_LIB_VL0 3
105 #define SBRENCODER_LIB_VL1 3
106 #define SBRENCODER_LIB_VL2 12
107
108
109
110 /***************************************************************************/
111 /*
112 * SBR Delay balancing definitions.
113 */
114
115 /*
116 input buffer (1ch)
117
118 |------------ 1537 -------------|-----|---------- 2048 -------------|
119 (core2sbr delay ) ds (read, core and ds area)
120 */
121
122 #define SFB(dwnsmp) (32 << (dwnsmp-1)) /* SBR Frequency bands: 64 for dual-rate, 32 for single-rate */
123 #define STS(fl) (((fl)==1024)?32:30) /* SBR Time Slots: 32 for core frame length 1024, 30 for core frame length 960 */
124
125 #define DELAY_QMF_ANA(dwnsmp) ((320<<((dwnsmp)-1)) - (32<<((dwnsmp)-1))) /* Full bandwidth */
126 #define DELAY_HYB_ANA (10*64) /* + 0.5 */ /* */
127 #define DELAY_HYB_SYN (6*64 - 32) /* */
128 #define DELAY_QMF_POSTPROC(dwnsmp) (32*(dwnsmp)) /* QMF postprocessing delay */
129 #define DELAY_DEC_QMF(dwnsmp) (6 * SFB(dwnsmp) ) /* Decoder QMF overlap */
130 #define DELAY_QMF_SYN (2) /* NO_POLY/2=2.5, rounded down to 2 */
131 #define DELAY_QMF_DS (32) /* QMF synthesis for downsampled time signal */
132
133 /* Delay in QMF paths */
134 #define DELAY_SBR(fl,dwnsmp) (DELAY_QMF_ANA(dwnsmp) + (SFB(dwnsmp)*STS(fl) - 1) + DELAY_QMF_SYN)
135 #define DELAY_PS(fl,dwnsmp) (DELAY_QMF_ANA(dwnsmp) + DELAY_HYB_ANA + DELAY_DEC_QMF(dwnsmp) + (SFB(dwnsmp)*STS(fl)-1) + DELAY_HYB_SYN + DELAY_QMF_SYN)
136 #define DELAY_ELDSBR(fl,dwnsmp) ( ( ((fl)/2)*(dwnsmp) ) - 1 + DELAY_QMF_POSTPROC(dwnsmp) )
137
138 /* Delay differences for SBR and SBR+PS */
139 #define MAX_DS_FILTER_DELAY (5) /* the additional max downsampler filter delay (source fs) */
140 #define DELAY_AAC2SBR(fl,dwnsmp) ((DELAY_QMF_ANA(dwnsmp) + DELAY_DEC_QMF(dwnsmp) + DELAY_QMF_SYN) - DELAY_SBR((fl),(dwnsmp)))
141 #define DELAY_ELD2SBR(fl,dwnsmp) ((DELAY_QMF_POSTPROC(dwnsmp)) - DELAY_ELDSBR(fl,dwnsmp))
142 #define DELAY_AAC2PS(fl,dwnsmp) ((DELAY_QMF_ANA(dwnsmp) + DELAY_QMF_DS + /*(DELAY_AAC(fl)*2) + */ DELAY_QMF_ANA(dwnsmp) + DELAY_DEC_QMF(dwnsmp) + DELAY_HYB_SYN + DELAY_QMF_SYN) - DELAY_PS(fl,dwnsmp)) /* 2048 - 463*2 */
143
144 /* Assumption: The sample delay resulting of of DELAY_AAC2PS is always smaller than the sample delay implied by DELAY_AAC2SBR */
145 #define MAX_SAMPLE_DELAY (DELAY_AAC2SBR(1024,2) + MAX_DS_FILTER_DELAY) /* maximum delay: frame length of 1024 and dual-rate sbr */
146
147 /***************************************************************************/
148
149
150
151 #define INVALID_TABLE_IDX -1
152
153 /***************************************************************************/
154 /*!
155
156 \brief Selects the SBR tuning settings to use dependent on number of
157 channels, bitrate, sample rate and core coder
158
159 \return Index to the appropriate table
160
161 ****************************************************************************/
162 #define DISTANCE_CEIL_VALUE 5000000
163 static INT
getSbrTuningTableIndex(UINT bitrate,UINT numChannels,UINT sampleRate,AUDIO_OBJECT_TYPE core,UINT * pBitRateClosest)164 getSbrTuningTableIndex(UINT bitrate, /*! the total bitrate in bits/sec */
165 UINT numChannels,/*! the number of channels for the core coder */
166 UINT sampleRate, /*! the sampling rate of the core coder */
167 AUDIO_OBJECT_TYPE core,
168 UINT *pBitRateClosest
169 )
170 {
171 int i, bitRateClosestLowerIndex=-1, bitRateClosestUpperIndex=-1, found = 0;
172 UINT bitRateClosestUpper = 0, bitRateClosestLower=DISTANCE_CEIL_VALUE;
173
174 #define isForThisCore(i) \
175 ( ( sbrTuningTable[i].coreCoder == CODEC_AACLD && core == AOT_ER_AAC_ELD ) || \
176 ( sbrTuningTable[i].coreCoder == CODEC_AAC && core != AOT_ER_AAC_ELD ) )
177
178 for (i=0; i < sbrTuningTableSize ; i++) {
179 if ( isForThisCore(i) ) /* tuning table is for this core codec */
180 {
181 if ( numChannels == sbrTuningTable [i].numChannels
182 && sampleRate == sbrTuningTable [i].sampleRate )
183 {
184 found = 1;
185 if ((bitrate >= sbrTuningTable [i].bitrateFrom) &&
186 (bitrate < sbrTuningTable [i].bitrateTo)) {
187 bitRateClosestLower = bitrate;
188 bitRateClosestUpper = bitrate;
189 //FDKprintf("entry %d\n", i);
190 return i ;
191 } else {
192 if ( sbrTuningTable [i].bitrateFrom > bitrate ) {
193 if (sbrTuningTable [i].bitrateFrom < bitRateClosestLower) {
194 bitRateClosestLower = sbrTuningTable [i].bitrateFrom;
195 bitRateClosestLowerIndex = i;
196 }
197 }
198 if ( sbrTuningTable [i].bitrateTo <= bitrate ) {
199 if (sbrTuningTable [i].bitrateTo > bitRateClosestUpper) {
200 bitRateClosestUpper = sbrTuningTable [i].bitrateTo-1;
201 bitRateClosestUpperIndex = i;
202 }
203 }
204 }
205 }
206 }
207 }
208
209 if (pBitRateClosest != NULL)
210 {
211 /* If there was at least one matching tuning entry found then pick the least distance bit rate */
212 if (found)
213 {
214 int distanceUpper=DISTANCE_CEIL_VALUE, distanceLower=DISTANCE_CEIL_VALUE;
215 if (bitRateClosestLowerIndex >= 0) {
216 distanceLower = sbrTuningTable [bitRateClosestLowerIndex].bitrateFrom - bitrate;
217 }
218 if (bitRateClosestUpperIndex >= 0) {
219 distanceUpper = bitrate - sbrTuningTable [bitRateClosestUpperIndex].bitrateTo;
220 }
221 if ( distanceUpper < distanceLower )
222 {
223 *pBitRateClosest = bitRateClosestUpper;
224 } else {
225 *pBitRateClosest = bitRateClosestLower;
226 }
227 } else {
228 *pBitRateClosest = 0;
229 }
230 }
231
232 return INVALID_TABLE_IDX;
233 }
234
235 /***************************************************************************/
236 /*!
237
238 \brief Selects the PS tuning settings to use dependent on bitrate
239 and core coder
240
241 \return Index to the appropriate table
242
243 ****************************************************************************/
244 static INT
getPsTuningTableIndex(UINT bitrate,UINT * pBitRateClosest)245 getPsTuningTableIndex(UINT bitrate, UINT *pBitRateClosest){
246
247 INT i, paramSets = sizeof (psTuningTable) / sizeof (psTuningTable [0]);
248 int bitRateClosestLowerIndex=-1, bitRateClosestUpperIndex=-1;
249 UINT bitRateClosestUpper = 0, bitRateClosestLower=DISTANCE_CEIL_VALUE;
250
251 for (i = 0 ; i < paramSets ; i++) {
252 if ((bitrate >= psTuningTable [i].bitrateFrom) &&
253 (bitrate < psTuningTable [i].bitrateTo)) {
254 return i ;
255 } else {
256 if ( psTuningTable [i].bitrateFrom > bitrate ) {
257 if (psTuningTable [i].bitrateFrom < bitRateClosestLower) {
258 bitRateClosestLower = psTuningTable [i].bitrateFrom;
259 bitRateClosestLowerIndex = i;
260 }
261 }
262 if ( psTuningTable [i].bitrateTo <= bitrate ) {
263 if (psTuningTable [i].bitrateTo > bitRateClosestUpper) {
264 bitRateClosestUpper = psTuningTable [i].bitrateTo-1;
265 bitRateClosestUpperIndex = i;
266 }
267 }
268 }
269 }
270
271 if (pBitRateClosest != NULL)
272 {
273 int distanceUpper=DISTANCE_CEIL_VALUE, distanceLower=DISTANCE_CEIL_VALUE;
274 if (bitRateClosestLowerIndex >= 0) {
275 distanceLower = sbrTuningTable [bitRateClosestLowerIndex].bitrateFrom - bitrate;
276 }
277 if (bitRateClosestUpperIndex >= 0) {
278 distanceUpper = bitrate - sbrTuningTable [bitRateClosestUpperIndex].bitrateTo;
279 }
280 if ( distanceUpper < distanceLower )
281 {
282 *pBitRateClosest = bitRateClosestUpper;
283 } else {
284 *pBitRateClosest = bitRateClosestLower;
285 }
286 }
287
288 return INVALID_TABLE_IDX;
289 }
290
291 /***************************************************************************/
292 /*!
293
294 \brief In case of downsampled SBR we may need to lower the stop freq
295 of a tuning setting to fit into the lower half of the
296 spectrum ( which is sampleRate/4 )
297
298 \return the adapted stop frequency index (-1 -> error)
299
300 \ingroup SbrEncCfg
301
302 ****************************************************************************/
303 static INT
FDKsbrEnc_GetDownsampledStopFreq(const INT sampleRateCore,const INT startFreq,INT stopFreq,const INT downSampleFactor)304 FDKsbrEnc_GetDownsampledStopFreq (
305 const INT sampleRateCore,
306 const INT startFreq,
307 INT stopFreq,
308 const INT downSampleFactor
309 )
310 {
311 INT maxStopFreqRaw = sampleRateCore / 2;
312 INT startBand, stopBand;
313 HANDLE_ERROR_INFO err;
314
315 while (stopFreq > 0 && FDKsbrEnc_getSbrStopFreqRAW(stopFreq, sampleRateCore) > maxStopFreqRaw) {
316 stopFreq--;
317 }
318
319 if (FDKsbrEnc_getSbrStopFreqRAW( stopFreq, sampleRateCore) > maxStopFreqRaw)
320 return -1;
321
322 err = FDKsbrEnc_FindStartAndStopBand (
323 sampleRateCore<<(downSampleFactor-1),
324 sampleRateCore,
325 32<<(downSampleFactor-1),
326 startFreq,
327 stopFreq,
328 &startBand,
329 &stopBand
330 );
331 if (err)
332 return -1;
333
334 return stopFreq;
335 }
336
337
338 /***************************************************************************/
339 /*!
340
341 \brief tells us, if for the given coreCoder, bitrate, number of channels
342 and input sampling rate an SBR setting is available. If yes, it
343 tells us also the core sampling rate we would need to run with
344
345 \return a flag indicating success: yes (1) or no (0)
346
347 ****************************************************************************/
348 static UINT
FDKsbrEnc_IsSbrSettingAvail(UINT bitrate,UINT vbrMode,UINT numOutputChannels,UINT sampleRateInput,UINT sampleRateCore,AUDIO_OBJECT_TYPE core)349 FDKsbrEnc_IsSbrSettingAvail (
350 UINT bitrate, /*! the total bitrate in bits/sec */
351 UINT vbrMode, /*! the vbr paramter, 0 means constant bitrate */
352 UINT numOutputChannels, /*! the number of channels for the core coder */
353 UINT sampleRateInput, /*! the input sample rate [in Hz] */
354 UINT sampleRateCore, /*! the core's sampling rate */
355 AUDIO_OBJECT_TYPE core
356 )
357 {
358 INT idx = INVALID_TABLE_IDX;
359
360 if (sampleRateInput < 16000)
361 return 0;
362
363 if (bitrate==0) {
364 /* map vbr quality to bitrate */
365 if (vbrMode < 30)
366 bitrate = 24000;
367 else if (vbrMode < 40)
368 bitrate = 28000;
369 else if (vbrMode < 60)
370 bitrate = 32000;
371 else if (vbrMode < 75)
372 bitrate = 40000;
373 else
374 bitrate = 48000;
375 bitrate *= numOutputChannels;
376 }
377
378 idx = getSbrTuningTableIndex(bitrate, numOutputChannels, sampleRateCore, core, NULL);
379
380 return (idx == INVALID_TABLE_IDX ? 0 : 1);
381 }
382
383
384 /***************************************************************************/
385 /*!
386
387 \brief Adjusts the SBR settings according to the chosen core coder
388 settings which are accessible via config->codecSettings
389
390 \return A flag indicating success: yes (1) or no (0)
391
392 ****************************************************************************/
393 static UINT
FDKsbrEnc_AdjustSbrSettings(const sbrConfigurationPtr config,UINT bitRate,UINT numChannels,UINT sampleRateCore,UINT sampleRateSbr,UINT transFac,UINT standardBitrate,UINT vbrMode,UINT useSpeechConfig,UINT lcsMode,UINT bParametricStereo,AUDIO_OBJECT_TYPE core)394 FDKsbrEnc_AdjustSbrSettings (const sbrConfigurationPtr config, /*! output, modified */
395 UINT bitRate, /*! the total bitrate in bits/sec */
396 UINT numChannels, /*! the core coder number of channels */
397 UINT sampleRateCore, /*! the core coder sampling rate in Hz */
398 UINT sampleRateSbr, /*! the sbr coder sampling rate in Hz */
399 UINT transFac, /*! the short block to long block ratio */
400 UINT standardBitrate, /*! the standard bitrate per channel in bits/sec */
401 UINT vbrMode, /*! the vbr paramter, 0 poor quality .. 100 high quality*/
402 UINT useSpeechConfig, /*!< adapt tuning parameters for speech ? */
403 UINT lcsMode, /*! the low complexity stereo mode */
404 UINT bParametricStereo, /*!< use parametric stereo */
405 AUDIO_OBJECT_TYPE core) /* Core audio codec object type */
406 {
407 INT idx = INVALID_TABLE_IDX;
408 /* set the core codec settings */
409 config->codecSettings.bitRate = bitRate;
410 config->codecSettings.nChannels = numChannels;
411 config->codecSettings.sampleFreq = sampleRateCore;
412 config->codecSettings.transFac = transFac;
413 config->codecSettings.standardBitrate = standardBitrate;
414
415 if (bitRate < 28000) {
416 config->threshold_AmpRes_FF_m = (FIXP_DBL)MAXVAL_DBL;
417 config->threshold_AmpRes_FF_e = 7;
418 }
419 else if (bitRate >= 28000 && bitRate <= 48000) {
420 /* The float threshold is 75
421 0.524288f is fractional part of RELAXATION, the quotaMatrix and therefore tonality are scaled by this
422 2/3 is because the original implementation divides the tonality values by 3, here it's divided by 2
423 128 compensates the necessary shiftfactor of 7 */
424 config->threshold_AmpRes_FF_m = FL2FXCONST_DBL(75.0f*0.524288f/(2.0f/3.0f)/128.0f);
425 config->threshold_AmpRes_FF_e = 7;
426 }
427 else if (bitRate > 48000) {
428 config->threshold_AmpRes_FF_m = FL2FXCONST_DBL(0);
429 config->threshold_AmpRes_FF_e = 0;
430 }
431
432 if (bitRate==0) {
433 /* map vbr quality to bitrate */
434 if (vbrMode < 30)
435 bitRate = 24000;
436 else if (vbrMode < 40)
437 bitRate = 28000;
438 else if (vbrMode < 60)
439 bitRate = 32000;
440 else if (vbrMode < 75)
441 bitRate = 40000;
442 else
443 bitRate = 48000;
444 bitRate *= numChannels;
445 /* fix to enable mono vbrMode<40 @ 44.1 of 48kHz */
446 if (numChannels==1) {
447 if (sampleRateSbr==44100 || sampleRateSbr==48000) {
448 if (vbrMode<40) bitRate = 32000;
449 }
450 }
451 }
452
453 idx = getSbrTuningTableIndex(bitRate,numChannels,sampleRateCore, core, NULL);
454
455 if (idx != INVALID_TABLE_IDX) {
456 config->startFreq = sbrTuningTable[idx].startFreq ;
457 config->stopFreq = sbrTuningTable[idx].stopFreq ;
458 if (useSpeechConfig) {
459 config->startFreq = sbrTuningTable[idx].startFreqSpeech;
460 config->stopFreq = sbrTuningTable[idx].stopFreqSpeech;
461 }
462
463 /* Adapt stop frequency in case of downsampled SBR - only 32 bands then */
464 if (1 == config->downSampleFactor) {
465 INT dsStopFreq = FDKsbrEnc_GetDownsampledStopFreq(
466 sampleRateCore,
467 config->startFreq,
468 config->stopFreq,
469 config->downSampleFactor
470 );
471 if (dsStopFreq < 0) {
472 return 0;
473 }
474
475 config->stopFreq = dsStopFreq;
476 }
477
478 config->sbr_noise_bands = sbrTuningTable[idx].numNoiseBands ;
479 if (core == AOT_ER_AAC_ELD)
480 config->init_amp_res_FF = SBR_AMP_RES_1_5;
481 config->noiseFloorOffset= sbrTuningTable[idx].noiseFloorOffset;
482
483 config->ana_max_level = sbrTuningTable[idx].noiseMaxLevel ;
484 config->stereoMode = sbrTuningTable[idx].stereoMode ;
485 config->freqScale = sbrTuningTable[idx].freqScale ;
486
487 if (numChannels == 1) {
488 /* stereo case */
489 switch (core) {
490 case AOT_AAC_LC:
491 if (bitRate <= (useSpeechConfig?24000U:20000U)) {
492 config->freq_res_fixfix[0] = FREQ_RES_LOW; /* set low frequency resolution for non-split frames */
493 config->freq_res_fixfix[1] = FREQ_RES_LOW; /* set low frequency resolution for split frames */
494 }
495 break;
496 case AOT_ER_AAC_ELD:
497 if (bitRate < 36000)
498 config->freq_res_fixfix[1] = FREQ_RES_LOW; /* set low frequency resolution for split frames */
499 if (bitRate < 26000) {
500 config->freq_res_fixfix[0] = FREQ_RES_LOW; /* set low frequency resolution for non-split frames */
501 config->fResTransIsLow = 1; /* for transient frames, set low frequency resolution */
502 }
503 break;
504 default:
505 break;
506 }
507 }
508 else {
509 /* stereo case */
510 switch (core) {
511 case AOT_AAC_LC:
512 if (bitRate <= 28000) {
513 config->freq_res_fixfix[0] = FREQ_RES_LOW; /* set low frequency resolution for non-split frames */
514 config->freq_res_fixfix[1] = FREQ_RES_LOW; /* set low frequency resolution for split frames */
515 }
516 break;
517 case AOT_ER_AAC_ELD:
518 if (bitRate < 72000) {
519 config->freq_res_fixfix[1] = FREQ_RES_LOW; /* set low frequency resolution for split frames */
520 }
521 if (bitRate < 52000) {
522 config->freq_res_fixfix[0] = FREQ_RES_LOW; /* set low frequency resolution for non-split frames */
523 config->fResTransIsLow = 1; /* for transient frames, set low frequency resolution */
524 }
525 break;
526 default:
527 break;
528 }
529 if (bitRate <= 28000) {
530 /*
531 additionally restrict frequency resolution in FIXFIX frames
532 to further reduce SBR payload size */
533 config->freq_res_fixfix[0] = FREQ_RES_LOW;
534 config->freq_res_fixfix[1] = FREQ_RES_LOW;
535 }
536 }
537
538 /* adjust usage of parametric coding dependent on bitrate and speech config flag */
539 if (useSpeechConfig)
540 config->parametricCoding = 0;
541
542 if (core == AOT_ER_AAC_ELD) {
543 if (bitRate < 28000)
544 config->init_amp_res_FF = SBR_AMP_RES_3_0;
545 config->SendHeaderDataTime = -1;
546 }
547
548 if (numChannels == 1) {
549 if (bitRate < 16000) {
550 config->parametricCoding = 0;
551 }
552 }
553 else {
554 if (bitRate < 20000) {
555 config->parametricCoding = 0;
556 }
557 }
558
559 config->useSpeechConfig = useSpeechConfig;
560
561 /* PS settings */
562 config->bParametricStereo = bParametricStereo;
563
564 return 1 ;
565 }
566 else {
567 return 0 ;
568 }
569 }
570
571 /*****************************************************************************
572
573 functionname: FDKsbrEnc_InitializeSbrDefaults
574 description: initializes the SBR confifuration
575 returns: error status
576 input: - core codec type,
577 - factor of SBR to core frame length,
578 - core frame length
579 output: initialized SBR configuration
580
581 *****************************************************************************/
582 static UINT
FDKsbrEnc_InitializeSbrDefaults(sbrConfigurationPtr config,INT downSampleFactor,UINT codecGranuleLen,const INT isLowDelay)583 FDKsbrEnc_InitializeSbrDefaults (sbrConfigurationPtr config,
584 INT downSampleFactor,
585 UINT codecGranuleLen
586 ,const INT isLowDelay
587 )
588 {
589 if ( (downSampleFactor < 1 || downSampleFactor > 2) ||
590 (codecGranuleLen*downSampleFactor > QMF_CHANNELS*QMF_MAX_TIME_SLOTS) )
591 return(0); /* error */
592
593 config->SendHeaderDataTime = 1000;
594 config->useWaveCoding = 0;
595 config->crcSbr = 0;
596 config->dynBwSupported = 1;
597 if (isLowDelay)
598 config->tran_thr = 6000;
599 else
600 config->tran_thr = 13000;
601
602 config->parametricCoding = 1;
603
604 config->sbrFrameSize = codecGranuleLen * downSampleFactor;
605 config->downSampleFactor = downSampleFactor;
606
607 /* sbr default parameters */
608 config->sbr_data_extra = 0;
609 config->amp_res = SBR_AMP_RES_3_0 ;
610 config->tran_fc = 0 ;
611 config->tran_det_mode = 1 ;
612 config->spread = 1 ;
613 config->stat = 0 ;
614 config->e = 1 ;
615 config->deltaTAcrossFrames = 1 ;
616 config->dF_edge_1stEnv = FL2FXCONST_DBL(0.3f) ;
617 config->dF_edge_incr = FL2FXCONST_DBL(0.3f) ;
618
619 config->sbr_invf_mode = INVF_SWITCHED;
620 config->sbr_xpos_mode = XPOS_LC;
621 config->sbr_xpos_ctrl = SBR_XPOS_CTRL_DEFAULT;
622 config->sbr_xpos_level = 0;
623 config->useSaPan = 0;
624 config->dynBwEnabled = 0;
625
626
627 /* the following parameters are overwritten by the FDKsbrEnc_AdjustSbrSettings() function since
628 they are included in the tuning table */
629 config->stereoMode = SBR_SWITCH_LRC;
630 config->ana_max_level = 6;
631 config->noiseFloorOffset = 0;
632 config->startFreq = 5; /* 5.9 respectively 6.0 kHz at fs = 44.1/48 kHz */
633 config->stopFreq = 9; /* 16.2 respectively 16.8 kHz at fs = 44.1/48 kHz */
634 config->freq_res_fixfix[0] = FREQ_RES_HIGH; /* non-split case */
635 config->freq_res_fixfix[1] = FREQ_RES_HIGH; /* split case */
636 config->fResTransIsLow = 0; /* for transient frames, set variable frequency resolution according to freqResTable */
637
638 /* header_extra_1 */
639 config->freqScale = SBR_FREQ_SCALE_DEFAULT;
640 config->alterScale = SBR_ALTER_SCALE_DEFAULT;
641 config->sbr_noise_bands = SBR_NOISE_BANDS_DEFAULT;
642
643 /* header_extra_2 */
644 config->sbr_limiter_bands = SBR_LIMITER_BANDS_DEFAULT;
645 config->sbr_limiter_gains = SBR_LIMITER_GAINS_DEFAULT;
646 config->sbr_interpol_freq = SBR_INTERPOL_FREQ_DEFAULT;
647 config->sbr_smoothing_length = SBR_SMOOTHING_LENGTH_DEFAULT;
648
649 return 1;
650 }
651
652
653 /*****************************************************************************
654
655 functionname: DeleteEnvChannel
656 description: frees memory of one SBR channel
657 returns: -
658 input: handle of channel
659 output: released handle
660
661 *****************************************************************************/
662 static void
deleteEnvChannel(HANDLE_ENV_CHANNEL hEnvCut)663 deleteEnvChannel (HANDLE_ENV_CHANNEL hEnvCut)
664 {
665 if (hEnvCut) {
666
667 FDKsbrEnc_DeleteTonCorrParamExtr(&hEnvCut->TonCorr);
668
669 FDKsbrEnc_deleteExtractSbrEnvelope (&hEnvCut->sbrExtractEnvelope);
670 }
671
672 }
673
674
675 /*****************************************************************************
676
677 functionname: sbrEncoder_ChannelClose
678 description: close the channel coding handle
679 returns:
680 input: phSbrChannel
681 output:
682
683 *****************************************************************************/
684 static void
sbrEncoder_ChannelClose(HANDLE_SBR_CHANNEL hSbrChannel)685 sbrEncoder_ChannelClose(HANDLE_SBR_CHANNEL hSbrChannel)
686 {
687 if (hSbrChannel != NULL)
688 {
689 deleteEnvChannel (&hSbrChannel->hEnvChannel);
690 }
691 }
692
693 /*****************************************************************************
694
695 functionname: sbrEncoder_ElementClose
696 description: close the channel coding handle
697 returns:
698 input: phSbrChannel
699 output:
700
701 *****************************************************************************/
702 static void
sbrEncoder_ElementClose(HANDLE_SBR_ELEMENT * phSbrElement)703 sbrEncoder_ElementClose(HANDLE_SBR_ELEMENT *phSbrElement)
704 {
705 HANDLE_SBR_ELEMENT hSbrElement = *phSbrElement;
706
707 if (hSbrElement!=NULL) {
708 if (hSbrElement->sbrConfigData.v_k_master)
709 FreeRam_Sbr_v_k_master(&hSbrElement->sbrConfigData.v_k_master);
710 if (hSbrElement->sbrConfigData.freqBandTable[LO])
711 FreeRam_Sbr_freqBandTableLO(&hSbrElement->sbrConfigData.freqBandTable[LO]);
712 if (hSbrElement->sbrConfigData.freqBandTable[HI])
713 FreeRam_Sbr_freqBandTableHI(&hSbrElement->sbrConfigData.freqBandTable[HI]);
714
715 FreeRam_SbrElement(phSbrElement);
716 }
717 return ;
718
719 }
720
721
sbrEncoder_Close(HANDLE_SBR_ENCODER * phSbrEncoder)722 void sbrEncoder_Close (HANDLE_SBR_ENCODER *phSbrEncoder)
723 {
724 HANDLE_SBR_ENCODER hSbrEncoder = *phSbrEncoder;
725
726 if (hSbrEncoder != NULL)
727 {
728 int el, ch;
729
730 for (el=0; el<(8); el++)
731 {
732 if (hSbrEncoder->sbrElement[el]!=NULL) {
733 sbrEncoder_ElementClose(&hSbrEncoder->sbrElement[el]);
734 }
735 }
736
737 /* Close sbr Channels */
738 for (ch=0; ch<(8); ch++)
739 {
740 if (hSbrEncoder->pSbrChannel[ch]) {
741 sbrEncoder_ChannelClose(hSbrEncoder->pSbrChannel[ch]);
742 FreeRam_SbrChannel(&hSbrEncoder->pSbrChannel[ch]);
743 }
744
745 if (hSbrEncoder->QmfAnalysis[ch].FilterStates)
746 FreeRam_Sbr_QmfStatesAnalysis((FIXP_QAS**)&hSbrEncoder->QmfAnalysis[ch].FilterStates);
747
748
749 }
750
751 if (hSbrEncoder->hParametricStereo)
752 PSEnc_Destroy(&hSbrEncoder->hParametricStereo);
753 if (hSbrEncoder->qmfSynthesisPS.FilterStates)
754 FreeRam_PsQmfStatesSynthesis((FIXP_DBL**)&hSbrEncoder->qmfSynthesisPS.FilterStates);
755
756 /* Release Overlay */
757 FreeRam_SbrDynamic_RAM((FIXP_DBL**)&hSbrEncoder->pSBRdynamic_RAM);
758
759
760 FreeRam_SbrEncoder(phSbrEncoder);
761 }
762
763 }
764
765 /*****************************************************************************
766
767 functionname: updateFreqBandTable
768 description: updates vk_master
769 returns: -
770 input: config handle
771 output: error info
772
773 *****************************************************************************/
updateFreqBandTable(HANDLE_SBR_CONFIG_DATA sbrConfigData,HANDLE_SBR_HEADER_DATA sbrHeaderData,const INT downSampleFactor)774 static INT updateFreqBandTable(
775 HANDLE_SBR_CONFIG_DATA sbrConfigData,
776 HANDLE_SBR_HEADER_DATA sbrHeaderData,
777 const INT downSampleFactor
778 )
779 {
780 INT k0, k2;
781
782 if( FDKsbrEnc_FindStartAndStopBand (
783 sbrConfigData->sampleFreq,
784 sbrConfigData->sampleFreq >> (downSampleFactor-1),
785 sbrConfigData->noQmfBands,
786 sbrHeaderData->sbr_start_frequency,
787 sbrHeaderData->sbr_stop_frequency,
788 &k0,
789 &k2
790 )
791 )
792 return(1);
793
794
795 if( FDKsbrEnc_UpdateFreqScale(
796 sbrConfigData->v_k_master,
797 &sbrConfigData->num_Master,
798 k0,
799 k2,
800 sbrHeaderData->freqScale,
801 sbrHeaderData->alterScale
802 )
803 )
804 return(1);
805
806
807 sbrHeaderData->sbr_xover_band=0;
808
809
810 if( FDKsbrEnc_UpdateHiRes(
811 sbrConfigData->freqBandTable[HI],
812 &sbrConfigData->nSfb[HI],
813 sbrConfigData->v_k_master,
814 sbrConfigData->num_Master,
815 &sbrHeaderData->sbr_xover_band
816 )
817 )
818 return(1);
819
820
821 FDKsbrEnc_UpdateLoRes(
822 sbrConfigData->freqBandTable[LO],
823 &sbrConfigData->nSfb[LO],
824 sbrConfigData->freqBandTable[HI],
825 sbrConfigData->nSfb[HI]
826 );
827
828
829 sbrConfigData->xOverFreq = (sbrConfigData->freqBandTable[LOW_RES][0] * sbrConfigData->sampleFreq / sbrConfigData->noQmfBands+1)>>1;
830
831 return (0);
832 }
833
834
835 /*****************************************************************************
836
837 functionname: resetEnvChannel
838 description: resets parameters and allocates memory
839 returns: error status
840 input:
841 output: hEnv
842
843 *****************************************************************************/
resetEnvChannel(HANDLE_SBR_CONFIG_DATA sbrConfigData,HANDLE_SBR_HEADER_DATA sbrHeaderData,HANDLE_ENV_CHANNEL hEnv)844 static INT resetEnvChannel (HANDLE_SBR_CONFIG_DATA sbrConfigData,
845 HANDLE_SBR_HEADER_DATA sbrHeaderData,
846 HANDLE_ENV_CHANNEL hEnv)
847 {
848 /* note !!! hEnv->encEnvData.noOfnoisebands will be updated later in function FDKsbrEnc_extractSbrEnvelope !!!*/
849 hEnv->TonCorr.sbrNoiseFloorEstimate.noiseBands = sbrHeaderData->sbr_noise_bands;
850
851
852 if(FDKsbrEnc_ResetTonCorrParamExtr(&hEnv->TonCorr,
853 sbrConfigData->xposCtrlSwitch,
854 sbrConfigData->freqBandTable[HI][0],
855 sbrConfigData->v_k_master,
856 sbrConfigData->num_Master,
857 sbrConfigData->sampleFreq,
858 sbrConfigData->freqBandTable,
859 sbrConfigData->nSfb,
860 sbrConfigData->noQmfBands))
861 return(1);
862
863 hEnv->sbrCodeNoiseFloor.nSfb[LO] = hEnv->TonCorr.sbrNoiseFloorEstimate.noNoiseBands;
864 hEnv->sbrCodeNoiseFloor.nSfb[HI] = hEnv->TonCorr.sbrNoiseFloorEstimate.noNoiseBands;
865
866 hEnv->sbrCodeEnvelope.nSfb[LO] = sbrConfigData->nSfb[LO];
867 hEnv->sbrCodeEnvelope.nSfb[HI] = sbrConfigData->nSfb[HI];
868
869 hEnv->encEnvData.noHarmonics = sbrConfigData->nSfb[HI];
870
871 hEnv->sbrCodeEnvelope.upDate = 0;
872 hEnv->sbrCodeNoiseFloor.upDate = 0;
873
874 return (0);
875 }
876
877 /* ****************************** FDKsbrEnc_SbrGetXOverFreq ******************************/
878 /**
879 * @fn
880 * @brief calculates the closest possible crossover frequency
881 * @return the crossover frequency SBR accepts
882 *
883 */
884 static INT
FDKsbrEnc_SbrGetXOverFreq(HANDLE_SBR_ELEMENT hEnv,INT xoverFreq)885 FDKsbrEnc_SbrGetXOverFreq(HANDLE_SBR_ELEMENT hEnv, /*!< handle to SBR encoder instance */
886 INT xoverFreq) /*!< from core coder suggested crossover frequency */
887 {
888 INT band;
889 INT lastDiff, newDiff;
890 INT cutoffSb;
891
892 UCHAR *RESTRICT pVKMaster = hEnv->sbrConfigData.v_k_master;
893
894 /* Check if there is a matching cutoff frequency in the master table */
895 cutoffSb = (4*xoverFreq * hEnv->sbrConfigData.noQmfBands / hEnv->sbrConfigData.sampleFreq + 1)>>1;
896 lastDiff = cutoffSb;
897 for (band = 0; band < hEnv->sbrConfigData.num_Master; band++) {
898
899 newDiff = fixp_abs((INT)pVKMaster[band] - cutoffSb);
900
901 if(newDiff >= lastDiff) {
902 band--;
903 break;
904 }
905
906 lastDiff = newDiff;
907 }
908
909 return ((pVKMaster[band] * hEnv->sbrConfigData.sampleFreq/hEnv->sbrConfigData.noQmfBands+1)>>1);
910 }
911
912 /*****************************************************************************
913
914 functionname: FDKsbrEnc_EnvEncodeFrame
915 description: performs the sbr envelope calculation for one element
916 returns:
917 input:
918 output:
919
920 *****************************************************************************/
921 INT
FDKsbrEnc_EnvEncodeFrame(HANDLE_SBR_ENCODER hEnvEncoder,int iElement,INT_PCM * samples,UINT timeInStride,UINT * sbrDataBits,UCHAR * sbrData,int clearOutput)922 FDKsbrEnc_EnvEncodeFrame(HANDLE_SBR_ENCODER hEnvEncoder,
923 int iElement,
924 INT_PCM *samples, /*!< time samples, always interleaved */
925 UINT timeInStride, /*!< time buffer channel interleaving stride */
926 UINT *sbrDataBits, /*!< Size of SBR payload */
927 UCHAR *sbrData, /*!< SBR payload */
928 int clearOutput /*!< Do not consider any input signal */
929 )
930 {
931 HANDLE_SBR_ELEMENT hSbrElement = NULL;
932 FDK_CRCINFO crcInfo;
933 INT crcReg;
934 INT ch;
935 INT band;
936 INT cutoffSb;
937 INT newXOver;
938
939 if (hEnvEncoder == NULL)
940 return -1;
941
942 hSbrElement = hEnvEncoder->sbrElement[iElement];
943
944 if (hSbrElement == NULL)
945 return -1;
946
947
948 /* header bitstream handling */
949 HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData = &hSbrElement->sbrBitstreamData;
950
951 INT psHeaderActive = 0;
952 sbrBitstreamData->HeaderActive = 0;
953
954 /* Anticipate PS header because of internal PS bitstream delay in order to be in sync with SBR header. */
955 if ( sbrBitstreamData->CountSendHeaderData==(sbrBitstreamData->NrSendHeaderData-1) )
956 {
957 psHeaderActive = 1;
958 }
959
960 /* Signal SBR header to be written into bitstream */
961 if ( sbrBitstreamData->CountSendHeaderData==0 )
962 {
963 sbrBitstreamData->HeaderActive = 1;
964 }
965
966 /* Increment header interval counter */
967 if (sbrBitstreamData->NrSendHeaderData == 0) {
968 sbrBitstreamData->CountSendHeaderData = 1;
969 }
970 else {
971 if (sbrBitstreamData->CountSendHeaderData >= 0) {
972 sbrBitstreamData->CountSendHeaderData++;
973 sbrBitstreamData->CountSendHeaderData %= sbrBitstreamData->NrSendHeaderData;
974 }
975 }
976
977 if (hSbrElement->CmonData.dynBwEnabled ) {
978 INT i;
979 for ( i = 4; i > 0; i-- )
980 hSbrElement->dynXOverFreqDelay[i] = hSbrElement->dynXOverFreqDelay[i-1];
981
982 hSbrElement->dynXOverFreqDelay[0] = hSbrElement->CmonData.dynXOverFreqEnc;
983 if (hSbrElement->dynXOverFreqDelay[1] > hSbrElement->dynXOverFreqDelay[2])
984 newXOver = hSbrElement->dynXOverFreqDelay[2];
985 else
986 newXOver = hSbrElement->dynXOverFreqDelay[1];
987
988 /* has the crossover frequency changed? */
989 if ( hSbrElement->sbrConfigData.dynXOverFreq != newXOver ) {
990
991 /* get corresponding master band */
992 cutoffSb = ((4* newXOver * hSbrElement->sbrConfigData.noQmfBands
993 / hSbrElement->sbrConfigData.sampleFreq)+1)>>1;
994
995 for ( band = 0; band < hSbrElement->sbrConfigData.num_Master; band++ ) {
996 if ( cutoffSb == hSbrElement->sbrConfigData.v_k_master[band] )
997 break;
998 }
999 FDK_ASSERT( band < hSbrElement->sbrConfigData.num_Master );
1000
1001 hSbrElement->sbrConfigData.dynXOverFreq = newXOver;
1002 hSbrElement->sbrHeaderData.sbr_xover_band = band;
1003 hSbrElement->sbrBitstreamData.HeaderActive=1;
1004 psHeaderActive = 1; /* ps header is one frame delayed */
1005
1006 /*
1007 update vk_master table
1008 */
1009 if(updateFreqBandTable(&hSbrElement->sbrConfigData,
1010 &hSbrElement->sbrHeaderData,
1011 hEnvEncoder->downSampleFactor
1012 ))
1013 return(1);
1014
1015
1016 /* reset SBR channels */
1017 INT nEnvCh = hSbrElement->sbrConfigData.nChannels;
1018 for ( ch = 0; ch < nEnvCh; ch++ ) {
1019 if(resetEnvChannel (&hSbrElement->sbrConfigData,
1020 &hSbrElement->sbrHeaderData,
1021 &hSbrElement->sbrChannel[ch]->hEnvChannel))
1022 return(1);
1023
1024 }
1025 }
1026 }
1027
1028 /*
1029 allocate space for dummy header and crc
1030 */
1031 crcReg = FDKsbrEnc_InitSbrBitstream(&hSbrElement->CmonData,
1032 hSbrElement->payloadDelayLine[hEnvEncoder->nBitstrDelay],
1033 MAX_PAYLOAD_SIZE*sizeof(UCHAR),
1034 &crcInfo,
1035 hSbrElement->sbrConfigData.sbrSyntaxFlags);
1036
1037 /* Temporal Envelope Data */
1038 SBR_FRAME_TEMP_DATA _fData;
1039 SBR_FRAME_TEMP_DATA *fData = &_fData;
1040 SBR_ENV_TEMP_DATA eData[MAX_NUM_CHANNELS];
1041
1042 /* Init Temporal Envelope Data */
1043 {
1044 int i;
1045
1046 FDKmemclear(&eData[0], sizeof(SBR_ENV_TEMP_DATA));
1047 FDKmemclear(&eData[1], sizeof(SBR_ENV_TEMP_DATA));
1048 FDKmemclear(fData, sizeof(SBR_FRAME_TEMP_DATA));
1049
1050 for(i=0; i<MAX_NUM_NOISE_VALUES; i++)
1051 fData->res[i] = FREQ_RES_HIGH;
1052 }
1053
1054
1055 if (!clearOutput)
1056 {
1057 /*
1058 * Transform audio data into QMF domain
1059 */
1060 for(ch = 0; ch < hSbrElement->sbrConfigData.nChannels; ch++)
1061 {
1062 HANDLE_ENV_CHANNEL h_envChan = &hSbrElement->sbrChannel[ch]->hEnvChannel;
1063 HANDLE_SBR_EXTRACT_ENVELOPE sbrExtrEnv = &h_envChan->sbrExtractEnvelope;
1064
1065 if(hSbrElement->elInfo.fParametricStereo == 0)
1066 {
1067 QMF_SCALE_FACTOR tmpScale;
1068 FIXP_DBL **pQmfReal, **pQmfImag;
1069 C_AALLOC_SCRATCH_START(qmfWorkBuffer, FIXP_DBL, QMF_CHANNELS*2)
1070
1071
1072 /* Obtain pointers to QMF buffers. */
1073 pQmfReal = sbrExtrEnv->rBuffer;
1074 pQmfImag = sbrExtrEnv->iBuffer;
1075
1076 qmfAnalysisFiltering( hSbrElement->hQmfAnalysis[ch],
1077 pQmfReal,
1078 pQmfImag,
1079 &tmpScale,
1080 samples + hSbrElement->elInfo.ChannelIndex[ch],
1081 timeInStride,
1082 qmfWorkBuffer );
1083
1084 h_envChan->qmfScale = tmpScale.lb_scale + 7;
1085
1086
1087 C_AALLOC_SCRATCH_END(qmfWorkBuffer, FIXP_DBL, QMF_CHANNELS*2)
1088
1089 } /* fParametricStereo == 0 */
1090
1091
1092 /*
1093 Parametric Stereo processing
1094 */
1095 if (hSbrElement->elInfo.fParametricStereo)
1096 {
1097 INT error = noError;
1098
1099
1100 /* Limit Parametric Stereo to one instance */
1101 FDK_ASSERT(ch == 0);
1102
1103
1104 if(error == noError){
1105 /* parametric stereo processing:
1106 - input:
1107 o left and right time domain samples
1108 - processing:
1109 o stereo qmf analysis
1110 o stereo hybrid analysis
1111 o ps parameter extraction
1112 o downmix + hybrid synthesis
1113 - output:
1114 o downmixed qmf data is written to sbrExtrEnv->rBuffer and sbrExtrEnv->iBuffer
1115 */
1116 SCHAR qmfScale;
1117 INT_PCM* pSamples[2] = {samples + hSbrElement->elInfo.ChannelIndex[0],samples + hSbrElement->elInfo.ChannelIndex[1]};
1118 error = FDKsbrEnc_PSEnc_ParametricStereoProcessing( hEnvEncoder->hParametricStereo,
1119 pSamples,
1120 timeInStride,
1121 hSbrElement->hQmfAnalysis,
1122 sbrExtrEnv->rBuffer,
1123 sbrExtrEnv->iBuffer,
1124 samples + hSbrElement->elInfo.ChannelIndex[ch],
1125 &hEnvEncoder->qmfSynthesisPS,
1126 &qmfScale,
1127 psHeaderActive );
1128 if (noError != error)
1129 {
1130 error = handBack(error);
1131 }
1132 h_envChan->qmfScale = (int)qmfScale;
1133 }
1134
1135
1136 } /* if (hEnvEncoder->hParametricStereo) */
1137
1138 /*
1139
1140 Extract Envelope relevant things from QMF data
1141
1142 */
1143 FDKsbrEnc_extractSbrEnvelope1(
1144 &hSbrElement->sbrConfigData,
1145 &hSbrElement->sbrHeaderData,
1146 &hSbrElement->sbrBitstreamData,
1147 h_envChan,
1148 &hSbrElement->CmonData,
1149 &eData[ch],
1150 fData
1151 );
1152
1153 } /* hEnvEncoder->sbrConfigData.nChannels */
1154 }
1155
1156 /*
1157 Process Envelope relevant things and calculate envelope data and write payload
1158 */
1159 FDKsbrEnc_extractSbrEnvelope2(
1160 &hSbrElement->sbrConfigData,
1161 &hSbrElement->sbrHeaderData,
1162 (hSbrElement->elInfo.fParametricStereo) ? hEnvEncoder->hParametricStereo : NULL,
1163 &hSbrElement->sbrBitstreamData,
1164 &hSbrElement->sbrChannel[0]->hEnvChannel,
1165 &hSbrElement->sbrChannel[1]->hEnvChannel,
1166 &hSbrElement->CmonData,
1167 eData,
1168 fData,
1169 clearOutput
1170 );
1171
1172 /*
1173 format payload, calculate crc
1174 */
1175 FDKsbrEnc_AssembleSbrBitstream(&hSbrElement->CmonData, &crcInfo, crcReg, hSbrElement->sbrConfigData.sbrSyntaxFlags);
1176
1177 /*
1178 save new payload, set to zero length if greater than MAX_PAYLOAD_SIZE
1179 */
1180 hSbrElement->payloadDelayLineSize[hEnvEncoder->nBitstrDelay] = FDKgetValidBits(&hSbrElement->CmonData.sbrBitbuf);
1181
1182 if(hSbrElement->payloadDelayLineSize[hEnvEncoder->nBitstrDelay] > (MAX_PAYLOAD_SIZE<<3))
1183 hSbrElement->payloadDelayLineSize[hEnvEncoder->nBitstrDelay]=0;
1184
1185 /* While filling the Delay lines, sbrData is NULL */
1186 if (sbrData) {
1187 *sbrDataBits = hSbrElement->payloadDelayLineSize[0];
1188 FDKmemcpy(sbrData, hSbrElement->payloadDelayLine[0], (hSbrElement->payloadDelayLineSize[0]+7)>>3);
1189
1190
1191 }
1192
1193
1194 /*******************************/
1195
1196 if (hEnvEncoder->fTimeDomainDownsampling)
1197 {
1198 int ch;
1199 int nChannels = hSbrElement->sbrConfigData.nChannels;
1200
1201 for (ch=0; ch < nChannels; ch++)
1202 {
1203 INT nOutSamples;
1204
1205 FDKaacEnc_Downsample(&hSbrElement->sbrChannel[ch]->downSampler,
1206 samples + hSbrElement->elInfo.ChannelIndex[ch] + hEnvEncoder->bufferOffset,
1207 hSbrElement->sbrConfigData.frameSize,
1208 timeInStride,
1209 samples + hSbrElement->elInfo.ChannelIndex[ch],
1210 &nOutSamples,
1211 hEnvEncoder->nChannels);
1212 }
1213 } /* downsample */
1214
1215
1216 return (0);
1217 }
1218
1219 /*****************************************************************************
1220
1221 functionname: createEnvChannel
1222 description: initializes parameters and allocates memory
1223 returns: error status
1224 input:
1225 output: hEnv
1226
1227 *****************************************************************************/
1228
1229 static INT
createEnvChannel(HANDLE_ENV_CHANNEL hEnv,INT channel,UCHAR * dynamic_RAM)1230 createEnvChannel (HANDLE_ENV_CHANNEL hEnv,
1231 INT channel
1232 ,UCHAR* dynamic_RAM
1233 )
1234 {
1235 FDKmemclear(hEnv,sizeof (struct ENV_CHANNEL));
1236
1237 if ( FDKsbrEnc_CreateTonCorrParamExtr(&hEnv->TonCorr,
1238 channel) )
1239 {
1240 return(1);
1241 }
1242
1243 if ( FDKsbrEnc_CreateExtractSbrEnvelope (&hEnv->sbrExtractEnvelope,
1244 channel
1245 ,/*chan*/0
1246 ,dynamic_RAM
1247 ) )
1248 {
1249 return(1);
1250 }
1251
1252 return 0;
1253 }
1254
1255 /*****************************************************************************
1256
1257 functionname: initEnvChannel
1258 description: initializes parameters
1259 returns: error status
1260 input:
1261 output:
1262
1263 *****************************************************************************/
1264 static INT
initEnvChannel(HANDLE_SBR_CONFIG_DATA sbrConfigData,HANDLE_SBR_HEADER_DATA sbrHeaderData,HANDLE_ENV_CHANNEL hEnv,sbrConfigurationPtr params,ULONG statesInitFlag,INT chanInEl,UCHAR * dynamic_RAM)1265 initEnvChannel ( HANDLE_SBR_CONFIG_DATA sbrConfigData,
1266 HANDLE_SBR_HEADER_DATA sbrHeaderData,
1267 HANDLE_ENV_CHANNEL hEnv,
1268 sbrConfigurationPtr params,
1269 ULONG statesInitFlag
1270 ,INT chanInEl
1271 ,UCHAR* dynamic_RAM
1272 )
1273 {
1274 int frameShift, tran_off=0;
1275 INT e;
1276 INT tran_fc;
1277 INT timeSlots, timeStep, startIndex;
1278 INT noiseBands[2] = { 3, 3 };
1279
1280 e = 1 << params->e;
1281
1282 FDK_ASSERT(params->e >= 0);
1283
1284 hEnv->encEnvData.freq_res_fixfix[0] = params->freq_res_fixfix[0];
1285 hEnv->encEnvData.freq_res_fixfix[1] = params->freq_res_fixfix[1];
1286 hEnv->encEnvData.fResTransIsLow = params->fResTransIsLow;
1287
1288 hEnv->fLevelProtect = 0;
1289
1290 hEnv->encEnvData.ldGrid = (sbrConfigData->sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY) ? 1 : 0;
1291
1292 hEnv->encEnvData.sbr_xpos_mode = (XPOS_MODE)params->sbr_xpos_mode;
1293
1294 if (hEnv->encEnvData.sbr_xpos_mode == XPOS_SWITCHED) {
1295 /*
1296 no other type than XPOS_MDCT or XPOS_SPEECH allowed,
1297 but enable switching
1298 */
1299 sbrConfigData->switchTransposers = TRUE;
1300 hEnv->encEnvData.sbr_xpos_mode = XPOS_MDCT;
1301 }
1302 else {
1303 sbrConfigData->switchTransposers = FALSE;
1304 }
1305
1306 hEnv->encEnvData.sbr_xpos_ctrl = params->sbr_xpos_ctrl;
1307
1308
1309 /* extended data */
1310 if(params->parametricCoding) {
1311 hEnv->encEnvData.extended_data = 1;
1312 }
1313 else {
1314 hEnv->encEnvData.extended_data = 0;
1315 }
1316
1317 hEnv->encEnvData.extension_size = 0;
1318
1319 startIndex = QMF_FILTER_PROTOTYPE_SIZE - sbrConfigData->noQmfBands;
1320
1321 switch (params->sbrFrameSize) {
1322 case 2304:
1323 timeSlots = 18;
1324 break;
1325 case 2048:
1326 case 1024:
1327 case 512:
1328 timeSlots = 16;
1329 break;
1330 case 1920:
1331 case 960:
1332 case 480:
1333 timeSlots = 15;
1334 break;
1335 case 1152:
1336 timeSlots = 9;
1337 break;
1338 default:
1339 return (1); /* Illegal frame size */
1340 }
1341
1342 timeStep = sbrConfigData->noQmfSlots / timeSlots;
1343
1344 if ( FDKsbrEnc_InitTonCorrParamExtr(params->sbrFrameSize,
1345 &hEnv->TonCorr,
1346 sbrConfigData,
1347 timeSlots,
1348 params->sbr_xpos_ctrl,
1349 params->ana_max_level,
1350 sbrHeaderData->sbr_noise_bands,
1351 params->noiseFloorOffset,
1352 params->useSpeechConfig) )
1353 return(1);
1354
1355 hEnv->encEnvData.noOfnoisebands = hEnv->TonCorr.sbrNoiseFloorEstimate.noNoiseBands;
1356
1357 noiseBands[0] = hEnv->encEnvData.noOfnoisebands;
1358 noiseBands[1] = hEnv->encEnvData.noOfnoisebands;
1359
1360 hEnv->encEnvData.sbr_invf_mode = (INVF_MODE)params->sbr_invf_mode;
1361
1362 if (hEnv->encEnvData.sbr_invf_mode == INVF_SWITCHED) {
1363 hEnv->encEnvData.sbr_invf_mode = INVF_MID_LEVEL;
1364 hEnv->TonCorr.switchInverseFilt = TRUE;
1365 }
1366 else {
1367 hEnv->TonCorr.switchInverseFilt = FALSE;
1368 }
1369
1370
1371 tran_fc = params->tran_fc;
1372
1373 if (tran_fc == 0) {
1374 tran_fc = fixMin (5000, FDKsbrEnc_getSbrStartFreqRAW (sbrHeaderData->sbr_start_frequency,params->codecSettings.sampleFreq));
1375 }
1376
1377 tran_fc = (tran_fc*4*sbrConfigData->noQmfBands/sbrConfigData->sampleFreq + 1)>>1;
1378
1379 if (sbrConfigData->sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY) {
1380 frameShift = LD_PRETRAN_OFF;
1381 tran_off = LD_PRETRAN_OFF + FRAME_MIDDLE_SLOT_512LD*timeStep;
1382 } else
1383 {
1384 frameShift = 0;
1385 switch (timeSlots) {
1386 /* The factor of 2 is by definition. */
1387 case NUMBER_TIME_SLOTS_2048: tran_off = 8 + FRAME_MIDDLE_SLOT_2048 * timeStep; break;
1388 case NUMBER_TIME_SLOTS_1920: tran_off = 7 + FRAME_MIDDLE_SLOT_1920 * timeStep; break;
1389 default: return 1;
1390 }
1391 }
1392 if ( FDKsbrEnc_InitExtractSbrEnvelope (&hEnv->sbrExtractEnvelope,
1393 sbrConfigData->noQmfSlots,
1394 sbrConfigData->noQmfBands, startIndex,
1395 timeSlots, timeStep, tran_off,
1396 statesInitFlag
1397 ,chanInEl
1398 ,dynamic_RAM
1399 ,sbrConfigData->sbrSyntaxFlags
1400 ) )
1401 return(1);
1402
1403 if(FDKsbrEnc_InitSbrCodeEnvelope (&hEnv->sbrCodeEnvelope,
1404 sbrConfigData->nSfb,
1405 params->deltaTAcrossFrames,
1406 params->dF_edge_1stEnv,
1407 params->dF_edge_incr))
1408 return(1);
1409
1410 if(FDKsbrEnc_InitSbrCodeEnvelope (&hEnv->sbrCodeNoiseFloor,
1411 noiseBands,
1412 params->deltaTAcrossFrames,
1413 0,0))
1414 return(1);
1415
1416 sbrConfigData->initAmpResFF = params->init_amp_res_FF;
1417
1418 if(FDKsbrEnc_InitSbrHuffmanTables (&hEnv->encEnvData,
1419 &hEnv->sbrCodeEnvelope,
1420 &hEnv->sbrCodeNoiseFloor,
1421 sbrHeaderData->sbr_amp_res))
1422 return(1);
1423
1424 FDKsbrEnc_initFrameInfoGenerator (&hEnv->SbrEnvFrame,
1425 params->spread,
1426 e,
1427 params->stat,
1428 timeSlots,
1429 hEnv->encEnvData.freq_res_fixfix,
1430 hEnv->encEnvData.fResTransIsLow,
1431 hEnv->encEnvData.ldGrid
1432 );
1433
1434 if(sbrConfigData->sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY)
1435 {
1436 INT bandwidth_qmf_slot = (sbrConfigData->sampleFreq>>1) / (sbrConfigData->noQmfBands);
1437 if(FDKsbrEnc_InitSbrFastTransientDetector(
1438 &hEnv->sbrFastTransientDetector,
1439 sbrConfigData->noQmfSlots,
1440 bandwidth_qmf_slot,
1441 sbrConfigData->noQmfBands,
1442 sbrConfigData->freqBandTable[0][0]
1443 ))
1444 return(1);
1445 }
1446
1447 /* The transient detector has to be initialized also if the fast transient
1448 detector was active, because the values from the transient detector
1449 structure are used. */
1450 if(FDKsbrEnc_InitSbrTransientDetector (&hEnv->sbrTransientDetector,
1451 sbrConfigData->sbrSyntaxFlags,
1452 sbrConfigData->frameSize,
1453 sbrConfigData->sampleFreq,
1454 params,
1455 tran_fc,
1456 sbrConfigData->noQmfSlots,
1457 sbrConfigData->noQmfBands,
1458 hEnv->sbrExtractEnvelope.YBufferWriteOffset,
1459 hEnv->sbrExtractEnvelope.YBufferSzShift,
1460 frameShift,
1461 tran_off
1462 ))
1463 return(1);
1464
1465
1466 sbrConfigData->xposCtrlSwitch = params->sbr_xpos_ctrl;
1467
1468 hEnv->encEnvData.noHarmonics = sbrConfigData->nSfb[HI];
1469 hEnv->encEnvData.addHarmonicFlag = 0;
1470
1471 return (0);
1472 }
1473
sbrEncoder_Open(HANDLE_SBR_ENCODER * phSbrEncoder,INT nElements,INT nChannels,INT supportPS)1474 INT sbrEncoder_Open(
1475 HANDLE_SBR_ENCODER *phSbrEncoder,
1476 INT nElements,
1477 INT nChannels,
1478 INT supportPS
1479 )
1480 {
1481 INT i;
1482 INT errorStatus = 1;
1483 HANDLE_SBR_ENCODER hSbrEncoder = NULL;
1484
1485 if (phSbrEncoder==NULL
1486 )
1487 {
1488 goto bail;
1489 }
1490
1491 hSbrEncoder = GetRam_SbrEncoder();
1492 if (hSbrEncoder==NULL) {
1493 goto bail;
1494 }
1495 FDKmemclear(hSbrEncoder, sizeof(SBR_ENCODER));
1496
1497 hSbrEncoder->pSBRdynamic_RAM = (UCHAR*)GetRam_SbrDynamic_RAM();
1498 hSbrEncoder->dynamicRam = hSbrEncoder->pSBRdynamic_RAM;
1499
1500 for (i=0; i<nElements; i++) {
1501 hSbrEncoder->sbrElement[i] = GetRam_SbrElement(i);
1502 if (hSbrEncoder->sbrElement[i]==NULL) {
1503 goto bail;
1504 }
1505 FDKmemclear(hSbrEncoder->sbrElement[i], sizeof(SBR_ELEMENT));
1506 hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[LO] = GetRam_Sbr_freqBandTableLO(i);
1507 hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[HI] = GetRam_Sbr_freqBandTableHI(i);
1508 hSbrEncoder->sbrElement[i]->sbrConfigData.v_k_master = GetRam_Sbr_v_k_master(i);
1509 if ( (hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[LO]==NULL) ||
1510 (hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[HI]==NULL) ||
1511 (hSbrEncoder->sbrElement[i]->sbrConfigData.v_k_master==NULL) )
1512 {
1513 goto bail;
1514 }
1515 }
1516
1517 for (i=0; i<nChannels; i++) {
1518 hSbrEncoder->pSbrChannel[i] = GetRam_SbrChannel(i);
1519 if (hSbrEncoder->pSbrChannel[i]==NULL) {
1520 goto bail;
1521 }
1522
1523 if ( createEnvChannel(&hSbrEncoder->pSbrChannel[i]->hEnvChannel,
1524 i
1525 ,hSbrEncoder->dynamicRam
1526 ) )
1527 {
1528 goto bail;
1529 }
1530
1531 }
1532
1533 for (i=0; i<fixMax(nChannels,(supportPS)?2:0); i++) {
1534 hSbrEncoder->QmfAnalysis[i].FilterStates = GetRam_Sbr_QmfStatesAnalysis(i);
1535 if (hSbrEncoder->QmfAnalysis[i].FilterStates==NULL) {
1536 goto bail;
1537 }
1538 }
1539
1540 if (supportPS) {
1541 if (PSEnc_Create(&hSbrEncoder->hParametricStereo))
1542 {
1543 goto bail;
1544 }
1545
1546 hSbrEncoder->qmfSynthesisPS.FilterStates = GetRam_PsQmfStatesSynthesis();
1547 if (hSbrEncoder->qmfSynthesisPS.FilterStates==NULL) {
1548 goto bail;
1549 }
1550 } /* supportPS */
1551
1552 *phSbrEncoder = hSbrEncoder;
1553
1554 errorStatus = 0;
1555 return errorStatus;
1556
1557 bail:
1558 /* Close SBR encoder instance */
1559 sbrEncoder_Close(&hSbrEncoder);
1560 return errorStatus;
1561 }
1562
1563 static
FDKsbrEnc_Reallocate(HANDLE_SBR_ENCODER hSbrEncoder,SBR_ELEMENT_INFO elInfo[(8)],const INT noElements)1564 INT FDKsbrEnc_Reallocate(
1565 HANDLE_SBR_ENCODER hSbrEncoder,
1566 SBR_ELEMENT_INFO elInfo[(8)],
1567 const INT noElements)
1568 {
1569 INT totalCh = 0;
1570 INT totalQmf = 0;
1571 INT coreEl;
1572 INT el=-1;
1573
1574 hSbrEncoder->lfeChIdx = -1; /* default value, until lfe found */
1575
1576 for (coreEl=0; coreEl<noElements; coreEl++)
1577 {
1578 /* SBR only handles SCE and CPE's */
1579 if (elInfo[coreEl].elType == ID_SCE || elInfo[coreEl].elType == ID_CPE) {
1580 el++;
1581 } else {
1582 if (elInfo[coreEl].elType == ID_LFE) {
1583 hSbrEncoder->lfeChIdx = elInfo[coreEl].ChannelIndex[0];
1584 }
1585 continue;
1586 }
1587
1588 SBR_ELEMENT_INFO *pelInfo = &elInfo[coreEl];
1589 HANDLE_SBR_ELEMENT hSbrElement = hSbrEncoder->sbrElement[el];
1590
1591 int ch;
1592 for ( ch = 0; ch < pelInfo->nChannelsInEl; ch++ ) {
1593 hSbrElement->sbrChannel[ch] = hSbrEncoder->pSbrChannel[totalCh];
1594 totalCh++;
1595 }
1596 /* analysis QMF */
1597 for ( ch = 0; ch < ((pelInfo->fParametricStereo)?2:pelInfo->nChannelsInEl); ch++ ) {
1598 hSbrElement->elInfo.ChannelIndex[ch] = pelInfo->ChannelIndex[ch];
1599 hSbrElement->hQmfAnalysis[ch] = &hSbrEncoder->QmfAnalysis[totalQmf++];
1600 }
1601
1602 /* Copy Element info */
1603 hSbrElement->elInfo.elType = pelInfo->elType;
1604 hSbrElement->elInfo.instanceTag = pelInfo->instanceTag;
1605 hSbrElement->elInfo.nChannelsInEl = pelInfo->nChannelsInEl;
1606 hSbrElement->elInfo.fParametricStereo = pelInfo->fParametricStereo;
1607 } /* coreEl */
1608
1609 return 0;
1610 }
1611
1612
1613
1614 /*****************************************************************************
1615
1616 functionname: FDKsbrEnc_EnvInit
1617 description: initializes parameters
1618 returns: error status
1619 input:
1620 output: hEnv
1621
1622 *****************************************************************************/
1623 static
FDKsbrEnc_EnvInit(HANDLE_SBR_ELEMENT hSbrElement,sbrConfigurationPtr params,INT * coreBandWith,AUDIO_OBJECT_TYPE aot,int nBitstrDelay,int nElement,const int headerPeriod,ULONG statesInitFlag,int fTimeDomainDownsampling,UCHAR * dynamic_RAM)1624 INT FDKsbrEnc_EnvInit (
1625 HANDLE_SBR_ELEMENT hSbrElement,
1626 sbrConfigurationPtr params,
1627 INT *coreBandWith,
1628 AUDIO_OBJECT_TYPE aot,
1629 int nBitstrDelay,
1630 int nElement,
1631 const int headerPeriod,
1632 ULONG statesInitFlag,
1633 int fTimeDomainDownsampling
1634 ,UCHAR *dynamic_RAM
1635 )
1636 {
1637 UCHAR *bitstreamBuffer;
1638 int ch, i;
1639
1640 if ((params->codecSettings.nChannels < 1) || (params->codecSettings.nChannels > MAX_NUM_CHANNELS)){
1641 return(1);
1642 }
1643
1644 /* initialize the encoder handle and structs*/
1645 bitstreamBuffer = hSbrElement->payloadDelayLine[nBitstrDelay];
1646
1647 /* init and set syntax flags */
1648 hSbrElement->sbrConfigData.sbrSyntaxFlags = 0;
1649
1650 switch (aot) {
1651 case AOT_ER_AAC_ELD:
1652 hSbrElement->sbrConfigData.sbrSyntaxFlags |= SBR_SYNTAX_LOW_DELAY;
1653 break;
1654 default:
1655 break;
1656 }
1657 if (params->crcSbr) {
1658 hSbrElement->sbrConfigData.sbrSyntaxFlags |= SBR_SYNTAX_CRC;
1659 }
1660
1661 hSbrElement->sbrConfigData.noQmfBands = QMF_CHANNELS>>(2-params->downSampleFactor);
1662 switch (hSbrElement->sbrConfigData.noQmfBands)
1663 {
1664 case 64: hSbrElement->sbrConfigData.noQmfSlots = params->sbrFrameSize>>6;
1665 break;
1666 case 32: hSbrElement->sbrConfigData.noQmfSlots = params->sbrFrameSize>>5;
1667 break;
1668 default: hSbrElement->sbrConfigData.noQmfSlots = params->sbrFrameSize>>6;
1669 return(2);
1670 }
1671
1672 FDKinitBitStream(&hSbrElement->CmonData.sbrBitbuf, bitstreamBuffer, MAX_PAYLOAD_SIZE*sizeof(UCHAR), 0, BS_WRITER);
1673
1674 /*
1675 now initialize sbrConfigData, sbrHeaderData and sbrBitstreamData,
1676 */
1677 hSbrElement->sbrConfigData.nChannels = params->codecSettings.nChannels;
1678
1679 if(params->codecSettings.nChannels == 2)
1680 hSbrElement->sbrConfigData.stereoMode = params->stereoMode;
1681 else
1682 hSbrElement->sbrConfigData.stereoMode = SBR_MONO;
1683
1684 hSbrElement->sbrConfigData.frameSize = params->sbrFrameSize;
1685
1686 hSbrElement->sbrConfigData.sampleFreq = params->downSampleFactor * params->codecSettings.sampleFreq;
1687
1688 hSbrElement->sbrBitstreamData.CountSendHeaderData = 0;
1689 if (params->SendHeaderDataTime > 0 ) {
1690
1691 if (headerPeriod==-1) {
1692
1693 hSbrElement->sbrBitstreamData.NrSendHeaderData = (INT)(params->SendHeaderDataTime * hSbrElement->sbrConfigData.sampleFreq
1694 / (1000 * hSbrElement->sbrConfigData.frameSize));
1695 hSbrElement->sbrBitstreamData.NrSendHeaderData = fixMax(hSbrElement->sbrBitstreamData.NrSendHeaderData,1);
1696 }
1697 else {
1698 /* assure header period at least once per second */
1699 hSbrElement->sbrBitstreamData.NrSendHeaderData = fixMin(fixMax(headerPeriod,1),(hSbrElement->sbrConfigData.sampleFreq/hSbrElement->sbrConfigData.frameSize));
1700 }
1701 }
1702 else {
1703 hSbrElement->sbrBitstreamData.NrSendHeaderData = 0;
1704 }
1705
1706 hSbrElement->sbrHeaderData.sbr_data_extra = params->sbr_data_extra;
1707 hSbrElement->sbrBitstreamData.HeaderActive = 0;
1708 hSbrElement->sbrHeaderData.sbr_start_frequency = params->startFreq;
1709 hSbrElement->sbrHeaderData.sbr_stop_frequency = params->stopFreq;
1710 hSbrElement->sbrHeaderData.sbr_xover_band = 0;
1711 hSbrElement->sbrHeaderData.sbr_lc_stereo_mode = 0;
1712
1713 /* data_extra */
1714 if (params->sbr_xpos_ctrl!= SBR_XPOS_CTRL_DEFAULT)
1715 hSbrElement->sbrHeaderData.sbr_data_extra = 1;
1716
1717 hSbrElement->sbrHeaderData.sbr_amp_res = (AMP_RES)params->amp_res;
1718
1719 /* header_extra_1 */
1720 hSbrElement->sbrHeaderData.freqScale = params->freqScale;
1721 hSbrElement->sbrHeaderData.alterScale = params->alterScale;
1722 hSbrElement->sbrHeaderData.sbr_noise_bands = params->sbr_noise_bands;
1723 hSbrElement->sbrHeaderData.header_extra_1 = 0;
1724
1725 if ((params->freqScale != SBR_FREQ_SCALE_DEFAULT) ||
1726 (params->alterScale != SBR_ALTER_SCALE_DEFAULT) ||
1727 (params->sbr_noise_bands != SBR_NOISE_BANDS_DEFAULT))
1728 {
1729 hSbrElement->sbrHeaderData.header_extra_1 = 1;
1730 }
1731
1732 /* header_extra_2 */
1733 hSbrElement->sbrHeaderData.sbr_limiter_bands = params->sbr_limiter_bands;
1734 hSbrElement->sbrHeaderData.sbr_limiter_gains = params->sbr_limiter_gains;
1735
1736 if ((hSbrElement->sbrConfigData.sampleFreq > 48000) &&
1737 (hSbrElement->sbrHeaderData.sbr_start_frequency >= 9))
1738 {
1739 hSbrElement->sbrHeaderData.sbr_limiter_gains = SBR_LIMITER_GAINS_INFINITE;
1740 }
1741
1742 hSbrElement->sbrHeaderData.sbr_interpol_freq = params->sbr_interpol_freq;
1743 hSbrElement->sbrHeaderData.sbr_smoothing_length = params->sbr_smoothing_length;
1744 hSbrElement->sbrHeaderData.header_extra_2 = 0;
1745
1746 if ((params->sbr_limiter_bands != SBR_LIMITER_BANDS_DEFAULT) ||
1747 (params->sbr_limiter_gains != SBR_LIMITER_GAINS_DEFAULT) ||
1748 (params->sbr_interpol_freq != SBR_INTERPOL_FREQ_DEFAULT) ||
1749 (params->sbr_smoothing_length != SBR_SMOOTHING_LENGTH_DEFAULT))
1750 {
1751 hSbrElement->sbrHeaderData.header_extra_2 = 1;
1752 }
1753
1754 /* other switches */
1755 hSbrElement->sbrConfigData.useWaveCoding = params->useWaveCoding;
1756 hSbrElement->sbrConfigData.useParametricCoding = params->parametricCoding;
1757 hSbrElement->sbrConfigData.thresholdAmpResFF_m = params->threshold_AmpRes_FF_m;
1758 hSbrElement->sbrConfigData.thresholdAmpResFF_e = params->threshold_AmpRes_FF_e;
1759
1760 /* init freq band table */
1761 if(updateFreqBandTable(&hSbrElement->sbrConfigData,
1762 &hSbrElement->sbrHeaderData,
1763 params->downSampleFactor
1764 ))
1765 {
1766 return(1);
1767 }
1768
1769 /* now create envelope ext and QMF for each available channel */
1770 for ( ch = 0; ch < hSbrElement->sbrConfigData.nChannels; ch++ ) {
1771
1772 if ( initEnvChannel(&hSbrElement->sbrConfigData,
1773 &hSbrElement->sbrHeaderData,
1774 &hSbrElement->sbrChannel[ch]->hEnvChannel,
1775 params,
1776 statesInitFlag
1777 ,ch
1778 ,dynamic_RAM
1779 ) )
1780 {
1781 return(1);
1782 }
1783
1784
1785 } /* nChannels */
1786
1787 /* reset and intialize analysis qmf */
1788 for ( ch = 0; ch < ((hSbrElement->elInfo.fParametricStereo)?2:hSbrElement->sbrConfigData.nChannels); ch++ )
1789 {
1790 int err;
1791 UINT qmfFlags = (hSbrElement->sbrConfigData.sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY) ? QMF_FLAG_CLDFB : 0;
1792 if (statesInitFlag)
1793 qmfFlags &= ~QMF_FLAG_KEEP_STATES;
1794 else
1795 qmfFlags |= QMF_FLAG_KEEP_STATES;
1796
1797 err = qmfInitAnalysisFilterBank( hSbrElement->hQmfAnalysis[ch],
1798 (FIXP_QAS*)hSbrElement->hQmfAnalysis[ch]->FilterStates,
1799 hSbrElement->sbrConfigData.noQmfSlots,
1800 hSbrElement->sbrConfigData.noQmfBands,
1801 hSbrElement->sbrConfigData.noQmfBands,
1802 hSbrElement->sbrConfigData.noQmfBands,
1803 qmfFlags );
1804 if (0!=err) {
1805 return err;
1806 }
1807 }
1808
1809 /* */
1810 hSbrElement->CmonData.xOverFreq = hSbrElement->sbrConfigData.xOverFreq;
1811 hSbrElement->CmonData.dynBwEnabled = (params->dynBwSupported && params->dynBwEnabled);
1812 hSbrElement->CmonData.dynXOverFreqEnc = FDKsbrEnc_SbrGetXOverFreq( hSbrElement, hSbrElement->CmonData.xOverFreq);
1813 for ( i = 0; i < 5; i++ )
1814 hSbrElement->dynXOverFreqDelay[i] = hSbrElement->CmonData.dynXOverFreqEnc;
1815 hSbrElement->CmonData.sbrNumChannels = hSbrElement->sbrConfigData.nChannels;
1816 hSbrElement->sbrConfigData.dynXOverFreq = hSbrElement->CmonData.xOverFreq;
1817
1818 /* Update Bandwith to be passed to the core encoder */
1819 *coreBandWith = hSbrElement->CmonData.xOverFreq;
1820
1821 return(0);
1822 }
1823
sbrEncoder_GetInBufferSize(int noChannels)1824 INT sbrEncoder_GetInBufferSize(int noChannels)
1825 {
1826 INT temp;
1827
1828 temp = (2048);
1829 temp += 1024 + MAX_SAMPLE_DELAY;
1830 temp *= noChannels;
1831 temp *= sizeof(INT_PCM);
1832 return temp;
1833 }
1834
1835 /*
1836 * Encode Dummy SBR payload frames to fill the delay lines.
1837 */
1838 static
FDKsbrEnc_DelayCompensation(HANDLE_SBR_ENCODER hEnvEnc,INT_PCM * timeBuffer)1839 INT FDKsbrEnc_DelayCompensation (
1840 HANDLE_SBR_ENCODER hEnvEnc,
1841 INT_PCM *timeBuffer
1842 )
1843 {
1844 int n, el;
1845
1846 for (n=hEnvEnc->nBitstrDelay; n>0; n--)
1847 {
1848 for (el=0; el<hEnvEnc->noElements; el++)
1849 {
1850 if (FDKsbrEnc_EnvEncodeFrame(
1851 hEnvEnc,
1852 el,
1853 timeBuffer + hEnvEnc->downsampledOffset,
1854 hEnvEnc->sbrElement[el]->sbrConfigData.nChannels,
1855 NULL,
1856 NULL,
1857 1
1858 ))
1859 return -1;
1860 }
1861 sbrEncoder_UpdateBuffers(hEnvEnc, timeBuffer);
1862 }
1863 return 0;
1864 }
1865
sbrEncoder_LimitBitRate(UINT bitRate,UINT numChannels,UINT coreSampleRate,AUDIO_OBJECT_TYPE aot)1866 UINT sbrEncoder_LimitBitRate(UINT bitRate, UINT numChannels, UINT coreSampleRate, AUDIO_OBJECT_TYPE aot)
1867 {
1868 UINT newBitRate;
1869 INT index;
1870
1871 FDK_ASSERT(numChannels > 0 && numChannels <= 2);
1872 if (aot == AOT_PS) {
1873 if (numChannels == 2) {
1874 index = getPsTuningTableIndex(bitRate, &newBitRate);
1875 if (index == INVALID_TABLE_IDX) {
1876 bitRate = newBitRate;
1877 }
1878 /* Set numChannels to 1 because for PS we need a SBR SCE (mono) element. */
1879 numChannels = 1;
1880 } else {
1881 return 0;
1882 }
1883 }
1884 index = getSbrTuningTableIndex(bitRate, numChannels, coreSampleRate, aot, &newBitRate);
1885 if (index != INVALID_TABLE_IDX) {
1886 newBitRate = bitRate;
1887 }
1888
1889 return newBitRate;
1890 }
1891
sbrEncoder_IsSingleRatePossible(AUDIO_OBJECT_TYPE aot)1892 UINT sbrEncoder_IsSingleRatePossible(AUDIO_OBJECT_TYPE aot)
1893 {
1894 UINT isPossible=(AOT_PS==aot)?0:1;
1895 return isPossible;
1896 }
1897
sbrEncoder_Init(HANDLE_SBR_ENCODER hSbrEncoder,SBR_ELEMENT_INFO elInfo[(8)],int noElements,INT_PCM * inputBuffer,INT * coreBandwidth,INT * inputBufferOffset,INT * numChannels,INT * coreSampleRate,UINT * downSampleFactor,INT * frameLength,AUDIO_OBJECT_TYPE aot,int * delay,int transformFactor,const int headerPeriod,ULONG statesInitFlag)1898 INT sbrEncoder_Init(
1899 HANDLE_SBR_ENCODER hSbrEncoder,
1900 SBR_ELEMENT_INFO elInfo[(8)],
1901 int noElements,
1902 INT_PCM *inputBuffer,
1903 INT *coreBandwidth,
1904 INT *inputBufferOffset,
1905 INT *numChannels,
1906 INT *coreSampleRate,
1907 UINT *downSampleFactor,
1908 INT *frameLength,
1909 AUDIO_OBJECT_TYPE aot,
1910 int *delay,
1911 int transformFactor,
1912 const int headerPeriod,
1913 ULONG statesInitFlag
1914 )
1915 {
1916 HANDLE_ERROR_INFO errorInfo = noError;
1917 sbrConfiguration sbrConfig[(8)];
1918 INT error = 0;
1919 INT lowestBandwidth;
1920 /* Save input parameters */
1921 INT inputSampleRate = *coreSampleRate;
1922 int coreFrameLength = *frameLength;
1923 int inputBandWidth = *coreBandwidth;
1924 int inputChannels = *numChannels;
1925
1926 int downsampledOffset = 0;
1927 int sbrOffset = 0;
1928 int downsamplerDelay = 0;
1929 int timeDomainDownsample = 0;
1930 int nBitstrDelay = 0;
1931 int highestSbrStartFreq, highestSbrStopFreq;
1932 int lowDelay = 0;
1933 int usePs = 0;
1934
1935 /* check whether SBR setting is available for the current encoder configuration (bitrate, samplerate) */
1936 if (!sbrEncoder_IsSingleRatePossible(aot)) {
1937 *downSampleFactor = 2;
1938 }
1939
1940
1941
1942 if ( aot==AOT_PS ) {
1943 usePs = 1;
1944 }
1945 if ( aot==AOT_ER_AAC_ELD ) {
1946 lowDelay = 1;
1947 }
1948 else if ( aot==AOT_ER_AAC_LD ) {
1949 error = 1;
1950 goto bail;
1951 }
1952
1953 /* Parametric Stereo */
1954 if ( usePs ) {
1955 if ( *numChannels == 2 && noElements == 1) {
1956 /* Override Element type in case of Parametric stereo */
1957 elInfo[0].elType = ID_SCE;
1958 elInfo[0].fParametricStereo = 1;
1959 elInfo[0].nChannelsInEl = 1;
1960 /* core encoder gets downmixed mono signal */
1961 *numChannels = 1;
1962 } else {
1963 error = 1;
1964 goto bail;
1965 }
1966 } /* usePs */
1967
1968 /* set the core's sample rate */
1969 switch (*downSampleFactor) {
1970 case 1:
1971 *coreSampleRate = inputSampleRate;
1972 break;
1973 case 2:
1974 *coreSampleRate = inputSampleRate>>1;
1975 break;
1976 default:
1977 *coreSampleRate = inputSampleRate>>1;
1978 return 0; /* return error */
1979 }
1980
1981 /* check whether SBR setting is available for the current encoder configuration (bitrate, coreSampleRate) */
1982 {
1983 int delayDiff = 0;
1984 int el, coreEl;
1985
1986 /* Check if every element config is feasible */
1987 for (coreEl=0; coreEl<noElements; coreEl++)
1988 {
1989 /* SBR only handles SCE and CPE's */
1990 if (elInfo[coreEl].elType != ID_SCE && elInfo[coreEl].elType != ID_CPE) {
1991 continue;
1992 }
1993 /* check if desired configuration is available */
1994 if ( !FDKsbrEnc_IsSbrSettingAvail (elInfo[coreEl].bitRate, 0, elInfo[coreEl].nChannelsInEl, inputSampleRate, *coreSampleRate, aot) )
1995 {
1996 error = 1;
1997 goto bail;
1998 }
1999 }
2000
2001 /* Determine Delay balancing and new encoder delay */
2002 if (lowDelay) {
2003 {
2004 delayDiff = (*delay * *downSampleFactor) + DELAY_ELD2SBR(coreFrameLength,*downSampleFactor);
2005 *delay = DELAY_ELDSBR(coreFrameLength,*downSampleFactor);
2006 }
2007 }
2008 else if (usePs) {
2009 delayDiff = (*delay * *downSampleFactor) + DELAY_AAC2PS(coreFrameLength,*downSampleFactor);
2010 *delay = DELAY_PS(coreFrameLength,*downSampleFactor);
2011 }
2012 else {
2013 delayDiff = DELAY_AAC2SBR(coreFrameLength,*downSampleFactor);
2014 delayDiff += (*delay * *downSampleFactor);
2015 *delay = DELAY_SBR(coreFrameLength,*downSampleFactor);
2016 }
2017
2018 if (!usePs) {
2019 timeDomainDownsample = *downSampleFactor-1; /* activate time domain downsampler when downSampleFactor is != 1 */
2020 }
2021
2022
2023 /* Take care about downsampled data bound to the SBR path */
2024 if (!timeDomainDownsample && delayDiff > 0) {
2025 /*
2026 * We must tweak the balancing into a situation where the downsampled path
2027 * is the one to be delayed, because delaying the QMF domain input, also delays
2028 * the downsampled audio, counteracting to the purpose of delay balancing.
2029 */
2030 while ( delayDiff > 0 )
2031 {
2032 /* Encoder delay increases */
2033 {
2034 *delay += coreFrameLength * *downSampleFactor;
2035 /* Add one frame delay to SBR path */
2036 delayDiff -= coreFrameLength * *downSampleFactor;
2037 }
2038 nBitstrDelay += 1;
2039 }
2040 } else
2041 {
2042 *delay += fixp_abs(delayDiff);
2043 }
2044
2045 if (delayDiff < 0) {
2046 /* Delay AAC data */
2047 delayDiff = -delayDiff;
2048 /* Multiply downsampled offset by AAC core channels. Divide by 2 because of half samplerate of downsampled data. */
2049 FDK_ASSERT(*downSampleFactor>0 && *downSampleFactor<=2);
2050 downsampledOffset = (delayDiff*(*numChannels))>>(*downSampleFactor-1);
2051 sbrOffset = 0;
2052 } else {
2053 /* Delay SBR input */
2054 if ( delayDiff > (int)coreFrameLength * (int)*downSampleFactor )
2055 {
2056 /* Do bitstream frame-wise delay balancing if we have more than SBR framelength samples delay difference */
2057 delayDiff -= coreFrameLength * *downSampleFactor;
2058 nBitstrDelay = 1;
2059 }
2060 /* Multiply input offset by input channels */
2061 sbrOffset = delayDiff*(*numChannels);
2062 downsampledOffset = 0;
2063 }
2064 hSbrEncoder->nBitstrDelay = nBitstrDelay;
2065 hSbrEncoder->nChannels = *numChannels;
2066 hSbrEncoder->frameSize = coreFrameLength * *downSampleFactor;
2067 hSbrEncoder->fTimeDomainDownsampling = timeDomainDownsample;
2068 hSbrEncoder->downSampleFactor = *downSampleFactor;
2069 hSbrEncoder->estimateBitrate = 0;
2070 hSbrEncoder->inputDataDelay = 0;
2071
2072
2073 /* Open SBR elements */
2074 el = -1;
2075 highestSbrStartFreq = highestSbrStopFreq = 0;
2076 lowestBandwidth = 99999;
2077
2078 /* Loop through each core encoder element and get a matching SBR element config */
2079 for (coreEl=0; coreEl<noElements; coreEl++)
2080 {
2081 /* SBR only handles SCE and CPE's */
2082 if (elInfo[coreEl].elType == ID_SCE || elInfo[coreEl].elType == ID_CPE) {
2083 el++;
2084 } else {
2085 continue;
2086 }
2087
2088 /* Set parametric Stereo Flag. */
2089 if (usePs) {
2090 elInfo[coreEl].fParametricStereo = 1;
2091 } else {
2092 elInfo[coreEl].fParametricStereo = 0;
2093 }
2094
2095 /*
2096 * Init sbrConfig structure
2097 */
2098 if ( ! FDKsbrEnc_InitializeSbrDefaults ( &sbrConfig[el],
2099 *downSampleFactor,
2100 coreFrameLength,
2101 IS_LOWDELAY(aot)
2102 ) )
2103 {
2104 error = 1;
2105 goto bail;
2106 }
2107
2108 /*
2109 * Modify sbrConfig structure according to Element parameters
2110 */
2111 if ( ! FDKsbrEnc_AdjustSbrSettings (&sbrConfig[el],
2112 elInfo[coreEl].bitRate,
2113 elInfo[coreEl].nChannelsInEl,
2114 *coreSampleRate,
2115 inputSampleRate,
2116 transformFactor,
2117 24000,
2118 0,
2119 0, /* useSpeechConfig */
2120 0, /* lcsMode */
2121 usePs, /* bParametricStereo */
2122 aot) )
2123 {
2124 error = 1;
2125 goto bail;
2126 }
2127
2128 /* Find common frequency border for all SBR elements */
2129 highestSbrStartFreq = fixMax(highestSbrStartFreq, sbrConfig[el].startFreq);
2130 highestSbrStopFreq = fixMax(highestSbrStopFreq, sbrConfig[el].stopFreq);
2131
2132 } /* first element loop */
2133
2134 /* Set element count (can be less than core encoder element count) */
2135 hSbrEncoder->noElements = el+1;
2136
2137 FDKsbrEnc_Reallocate(hSbrEncoder,
2138 elInfo,
2139 noElements);
2140
2141 for (el=0; el<hSbrEncoder->noElements; el++) {
2142
2143 int bandwidth = *coreBandwidth;
2144
2145 /* Use lowest common bandwidth */
2146 sbrConfig[el].startFreq = highestSbrStartFreq;
2147 sbrConfig[el].stopFreq = highestSbrStopFreq;
2148
2149 /* initialize SBR element, and get core bandwidth */
2150 error = FDKsbrEnc_EnvInit(hSbrEncoder->sbrElement[el],
2151 &sbrConfig[el],
2152 &bandwidth,
2153 aot,
2154 nBitstrDelay,
2155 el,
2156 headerPeriod,
2157 statesInitFlag,
2158 hSbrEncoder->fTimeDomainDownsampling
2159 ,hSbrEncoder->dynamicRam
2160 );
2161
2162 if (error != 0) {
2163 error = 2;
2164 goto bail;
2165 }
2166
2167 /* Get lowest core encoder bandwidth to be returned later. */
2168 lowestBandwidth = fixMin(lowestBandwidth, bandwidth);
2169
2170 } /* second element loop */
2171
2172 /* Initialize a downsampler for each channel in each SBR element */
2173 if (hSbrEncoder->fTimeDomainDownsampling)
2174 {
2175 for (el=0; el<hSbrEncoder->noElements; el++)
2176 {
2177 HANDLE_SBR_ELEMENT hSbrEl = hSbrEncoder->sbrElement[el];
2178 INT Wc, ch;
2179
2180 /* Calculated required normalized cutoff frequency (Wc = 1.0 -> lowestBandwidth = inputSampleRate/2) */
2181 Wc = (2*lowestBandwidth)*1000 / inputSampleRate;
2182
2183 for (ch=0; ch<hSbrEl->elInfo.nChannelsInEl; ch++)
2184 {
2185 FDKaacEnc_InitDownsampler (&hSbrEl->sbrChannel[ch]->downSampler, Wc, *downSampleFactor);
2186 FDK_ASSERT (hSbrEl->sbrChannel[ch]->downSampler.delay <=MAX_DS_FILTER_DELAY);
2187 }
2188
2189 downsamplerDelay = hSbrEl->sbrChannel[0]->downSampler.delay;
2190 } /* third element loop */
2191
2192 /* lfe */
2193 FDKaacEnc_InitDownsampler (&hSbrEncoder->lfeDownSampler, 0, *downSampleFactor);
2194
2195 /* Add the resampler additional delay to get the final delay and buffer offset values. */
2196 if (sbrOffset > 0 || downsampledOffset <= ((downsamplerDelay * (*numChannels))>>(*downSampleFactor-1))) {
2197 sbrOffset += (downsamplerDelay - downsampledOffset) * (*numChannels) ;
2198 *delay += downsamplerDelay - downsampledOffset;
2199 downsampledOffset = 0;
2200 } else {
2201 downsampledOffset -= (downsamplerDelay * (*numChannels))>>(*downSampleFactor-1);
2202 sbrOffset = 0;
2203 }
2204
2205 hSbrEncoder->inputDataDelay = downsamplerDelay;
2206 }
2207
2208 /* Assign core encoder Bandwidth */
2209 *coreBandwidth = lowestBandwidth;
2210
2211 /* Estimate sbr bitrate, 2.5 kBit/s per sbr channel */
2212 hSbrEncoder->estimateBitrate += 2500 * (*numChannels);
2213
2214 /* initialize parametric stereo */
2215 if (usePs)
2216 {
2217 PSENC_CONFIG psEncConfig;
2218 FDK_ASSERT(hSbrEncoder->noElements == 1);
2219 INT psTuningTableIdx = getPsTuningTableIndex(elInfo[0].bitRate, NULL);
2220
2221 psEncConfig.frameSize = coreFrameLength; //sbrConfig.sbrFrameSize;
2222 psEncConfig.qmfFilterMode = 0;
2223 psEncConfig.sbrPsDelay = 0;
2224
2225 /* tuning parameters */
2226 if (psTuningTableIdx != INVALID_TABLE_IDX) {
2227 psEncConfig.nStereoBands = psTuningTable[psTuningTableIdx].nStereoBands;
2228 psEncConfig.maxEnvelopes = psTuningTable[psTuningTableIdx].nEnvelopes;
2229 psEncConfig.iidQuantErrorThreshold = (FIXP_DBL)psTuningTable[psTuningTableIdx].iidQuantErrorThreshold;
2230
2231 /* calculation is not quite linear, increased number of envelopes causes more bits */
2232 /* assume avg. 50 bits per frame for 10 stereo bands / 1 envelope configuration */
2233 hSbrEncoder->estimateBitrate += ( (((*coreSampleRate) * 5 * psEncConfig.nStereoBands * psEncConfig.maxEnvelopes) / hSbrEncoder->frameSize));
2234
2235 } else {
2236 error = ERROR(CDI, "Invalid ps tuning table index.");
2237 goto bail;
2238 }
2239
2240 qmfInitSynthesisFilterBank(&hSbrEncoder->qmfSynthesisPS,
2241 (FIXP_DBL*)hSbrEncoder->qmfSynthesisPS.FilterStates,
2242 hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfSlots,
2243 hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands>>1,
2244 hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands>>1,
2245 hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands>>1,
2246 (statesInitFlag) ? 0 : QMF_FLAG_KEEP_STATES);
2247
2248 if(errorInfo == noError){
2249 /* update delay */
2250 psEncConfig.sbrPsDelay = FDKsbrEnc_GetEnvEstDelay(&hSbrEncoder->sbrElement[0]->sbrChannel[0]->hEnvChannel.sbrExtractEnvelope);
2251
2252 if(noError != (errorInfo = PSEnc_Init( hSbrEncoder->hParametricStereo,
2253 &psEncConfig,
2254 hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfSlots,
2255 hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands
2256 ,hSbrEncoder->dynamicRam
2257 )))
2258 {
2259 errorInfo = handBack(errorInfo);
2260 }
2261 }
2262
2263 /* QMF analysis + Hybrid analysis + Hybrid synthesis + QMF synthesis + downsampled input buffer delay */
2264 hSbrEncoder->inputDataDelay = (64*10/2) + (6*64) + (0) + (64*10/2-64+1) + ((*downSampleFactor)*downsampledOffset);
2265 }
2266
2267 hSbrEncoder->downsampledOffset = downsampledOffset;
2268 {
2269 hSbrEncoder->downmixSize = coreFrameLength*(*numChannels);
2270 }
2271
2272 hSbrEncoder->bufferOffset = sbrOffset;
2273 /* Delay Compensation: fill bitstream delay buffer with zero input signal */
2274 if ( hSbrEncoder->nBitstrDelay > 0 )
2275 {
2276 error = FDKsbrEnc_DelayCompensation (hSbrEncoder, inputBuffer);
2277 if (error != 0)
2278 goto bail;
2279 }
2280
2281 /* Set Output frame length */
2282 *frameLength = coreFrameLength * *downSampleFactor;
2283 /* Input buffer offset */
2284 *inputBufferOffset = fixMax(sbrOffset, downsampledOffset);
2285
2286
2287 }
2288
2289 return error;
2290
2291 bail:
2292 /* Restore input settings */
2293 *coreSampleRate = inputSampleRate;
2294 *frameLength = coreFrameLength;
2295 *numChannels = inputChannels;
2296 *coreBandwidth = inputBandWidth;
2297
2298 return error;
2299 }
2300
2301
2302 INT
sbrEncoder_EncodeFrame(HANDLE_SBR_ENCODER hSbrEncoder,INT_PCM * samples,UINT timeInStride,UINT sbrDataBits[(8)],UCHAR sbrData[(8)][MAX_PAYLOAD_SIZE])2303 sbrEncoder_EncodeFrame( HANDLE_SBR_ENCODER hSbrEncoder,
2304 INT_PCM *samples,
2305 UINT timeInStride,
2306 UINT sbrDataBits[(8)],
2307 UCHAR sbrData[(8)][MAX_PAYLOAD_SIZE]
2308 )
2309 {
2310 INT error;
2311 int el;
2312
2313 for (el=0; el<hSbrEncoder->noElements; el++)
2314 {
2315 if (hSbrEncoder->sbrElement[el] != NULL)
2316 {
2317 error = FDKsbrEnc_EnvEncodeFrame(
2318 hSbrEncoder,
2319 el,
2320 samples + hSbrEncoder->downsampledOffset,
2321 timeInStride,
2322 &sbrDataBits[el],
2323 sbrData[el],
2324 0
2325 );
2326 if (error)
2327 return error;
2328 }
2329 }
2330
2331 if ( ( hSbrEncoder->lfeChIdx!=-1) && (hSbrEncoder->downSampleFactor > 1) )
2332 { /* lfe downsampler */
2333 INT nOutSamples;
2334
2335 FDKaacEnc_Downsample(&hSbrEncoder->lfeDownSampler,
2336 samples + hSbrEncoder->downsampledOffset + hSbrEncoder->bufferOffset + hSbrEncoder->lfeChIdx,
2337 hSbrEncoder->frameSize,
2338 timeInStride,
2339 samples + hSbrEncoder->downsampledOffset + hSbrEncoder->lfeChIdx,
2340 &nOutSamples,
2341 hSbrEncoder->nChannels);
2342
2343
2344 }
2345
2346 return 0;
2347 }
2348
2349
sbrEncoder_UpdateBuffers(HANDLE_SBR_ENCODER hSbrEncoder,INT_PCM * timeBuffer)2350 INT sbrEncoder_UpdateBuffers(
2351 HANDLE_SBR_ENCODER hSbrEncoder,
2352 INT_PCM *timeBuffer
2353 )
2354 {
2355 if ( hSbrEncoder->downsampledOffset > 0 ) {
2356 /* Move delayed downsampled data */
2357 FDKmemcpy ( timeBuffer,
2358 timeBuffer + hSbrEncoder->downmixSize,
2359 sizeof(INT_PCM) * (hSbrEncoder->downsampledOffset) );
2360 } else {
2361 /* Move delayed input data */
2362 FDKmemcpy ( timeBuffer,
2363 timeBuffer + hSbrEncoder->nChannels * hSbrEncoder->frameSize,
2364 sizeof(INT_PCM) * hSbrEncoder->bufferOffset );
2365 }
2366 if ( hSbrEncoder->nBitstrDelay > 0 )
2367 {
2368 int el;
2369
2370 for (el=0; el<hSbrEncoder->noElements; el++)
2371 {
2372 FDKmemmove ( hSbrEncoder->sbrElement[el]->payloadDelayLine[0],
2373 hSbrEncoder->sbrElement[el]->payloadDelayLine[1],
2374 sizeof(UCHAR) * (hSbrEncoder->nBitstrDelay*MAX_PAYLOAD_SIZE) );
2375
2376 FDKmemmove( &hSbrEncoder->sbrElement[el]->payloadDelayLineSize[0],
2377 &hSbrEncoder->sbrElement[el]->payloadDelayLineSize[1],
2378 sizeof(UINT) * (hSbrEncoder->nBitstrDelay) );
2379 }
2380 }
2381 return 0;
2382 }
2383
2384
sbrEncoder_GetEstimateBitrate(HANDLE_SBR_ENCODER hSbrEncoder)2385 INT sbrEncoder_GetEstimateBitrate(HANDLE_SBR_ENCODER hSbrEncoder)
2386 {
2387 INT estimateBitrate = 0;
2388
2389 if(hSbrEncoder) {
2390 estimateBitrate += hSbrEncoder->estimateBitrate;
2391 }
2392
2393 return estimateBitrate;
2394 }
2395
sbrEncoder_GetInputDataDelay(HANDLE_SBR_ENCODER hSbrEncoder)2396 INT sbrEncoder_GetInputDataDelay(HANDLE_SBR_ENCODER hSbrEncoder)
2397 {
2398 INT delay = -1;
2399
2400 if(hSbrEncoder) {
2401 delay = hSbrEncoder->inputDataDelay;
2402 }
2403 return delay;
2404 }
2405
2406
sbrEncoder_GetLibInfo(LIB_INFO * info)2407 INT sbrEncoder_GetLibInfo( LIB_INFO *info )
2408 {
2409 int i;
2410
2411 if (info == NULL) {
2412 return -1;
2413 }
2414 /* search for next free tab */
2415 for (i = 0; i < FDK_MODULE_LAST; i++) {
2416 if (info[i].module_id == FDK_NONE) break;
2417 }
2418 if (i == FDK_MODULE_LAST) {
2419 return -1;
2420 }
2421 info += i;
2422
2423 info->module_id = FDK_SBRENC;
2424 info->version = LIB_VERSION(SBRENCODER_LIB_VL0, SBRENCODER_LIB_VL1, SBRENCODER_LIB_VL2);
2425 LIB_VERSION_STRING(info);
2426 #ifdef __ANDROID__
2427 info->build_date = "";
2428 info->build_time = "";
2429 #else
2430 info->build_date = __DATE__;
2431 info->build_time = __TIME__;
2432 #endif
2433 info->title = "SBR Encoder";
2434
2435 /* Set flags */
2436 info->flags = 0
2437 | CAPF_SBR_HQ
2438 | CAPF_SBR_PS_MPEG
2439 ;
2440 /* End of flags */
2441
2442 return 0;
2443 }
2444