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