1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2020 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   if (FDKsbrEnc_InitSbrHuffmanTables(&hEnv->encEnvData, &hEnv->sbrCodeEnvelope,
1454                                      &hEnv->sbrCodeNoiseFloor,
1455                                      sbrHeaderData->sbr_amp_res))
1456     return (1);
1457 
1458   FDKsbrEnc_initFrameInfoGenerator(
1459       &hEnv->SbrEnvFrame, params->spread, e, params->stat, timeSlots,
1460       hEnv->encEnvData.freq_res_fixfix, hEnv->encEnvData.fResTransIsLow,
1461       hEnv->encEnvData.ldGrid);
1462 
1463   if (sbrConfigData->sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY)
1464 
1465   {
1466     INT bandwidth_qmf_slot =
1467         (sbrConfigData->sampleFreq >> 1) / (sbrConfigData->noQmfBands);
1468     if (FDKsbrEnc_InitSbrFastTransientDetector(
1469             &hEnv->sbrFastTransientDetector, sbrConfigData->noQmfSlots,
1470             bandwidth_qmf_slot, sbrConfigData->noQmfBands,
1471             sbrConfigData->freqBandTable[0][0]))
1472       return (1);
1473   }
1474 
1475   /* The transient detector has to be initialized also if the fast transient
1476      detector was active, because the values from the transient detector
1477      structure are used. */
1478   if (FDKsbrEnc_InitSbrTransientDetector(
1479           &hEnv->sbrTransientDetector, sbrConfigData->sbrSyntaxFlags,
1480           sbrConfigData->frameSize, sbrConfigData->sampleFreq, params, tran_fc,
1481           sbrConfigData->noQmfSlots, sbrConfigData->noQmfBands,
1482           hEnv->sbrExtractEnvelope.YBufferWriteOffset,
1483           hEnv->sbrExtractEnvelope.YBufferSzShift, frameShift, tran_off))
1484     return (1);
1485 
1486   sbrConfigData->xposCtrlSwitch = params->sbr_xpos_ctrl;
1487 
1488   hEnv->encEnvData.noHarmonics = sbrConfigData->nSfb[HI];
1489   hEnv->encEnvData.addHarmonicFlag = 0;
1490 
1491   return (0);
1492 }
1493 
sbrEncoder_Open(HANDLE_SBR_ENCODER * phSbrEncoder,INT nElements,INT nChannels,INT supportPS)1494 INT sbrEncoder_Open(HANDLE_SBR_ENCODER *phSbrEncoder, INT nElements,
1495                     INT nChannels, INT supportPS) {
1496   INT i;
1497   INT errorStatus = 1;
1498   HANDLE_SBR_ENCODER hSbrEncoder = NULL;
1499 
1500   if (phSbrEncoder == NULL) {
1501     goto bail;
1502   }
1503 
1504   hSbrEncoder = GetRam_SbrEncoder();
1505   if (hSbrEncoder == NULL) {
1506     goto bail;
1507   }
1508   FDKmemclear(hSbrEncoder, sizeof(SBR_ENCODER));
1509 
1510   if (NULL ==
1511       (hSbrEncoder->pSBRdynamic_RAM = (UCHAR *)GetRam_SbrDynamic_RAM())) {
1512     goto bail;
1513   }
1514   hSbrEncoder->dynamicRam = hSbrEncoder->pSBRdynamic_RAM;
1515 
1516   /* Create SBR elements */
1517   for (i = 0; i < nElements; i++) {
1518     hSbrEncoder->sbrElement[i] = GetRam_SbrElement(i);
1519     if (hSbrEncoder->sbrElement[i] == NULL) {
1520       goto bail;
1521     }
1522     FDKmemclear(hSbrEncoder->sbrElement[i], sizeof(SBR_ELEMENT));
1523     hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[LO] =
1524         GetRam_Sbr_freqBandTableLO(i);
1525     hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[HI] =
1526         GetRam_Sbr_freqBandTableHI(i);
1527     hSbrEncoder->sbrElement[i]->sbrConfigData.v_k_master =
1528         GetRam_Sbr_v_k_master(i);
1529     if ((hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[LO] == NULL) ||
1530         (hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[HI] == NULL) ||
1531         (hSbrEncoder->sbrElement[i]->sbrConfigData.v_k_master == NULL)) {
1532       goto bail;
1533     }
1534   }
1535 
1536   /* Create SBR channels */
1537   for (i = 0; i < nChannels; i++) {
1538     hSbrEncoder->pSbrChannel[i] = GetRam_SbrChannel(i);
1539     if (hSbrEncoder->pSbrChannel[i] == NULL) {
1540       goto bail;
1541     }
1542 
1543     if (createEnvChannel(&hSbrEncoder->pSbrChannel[i]->hEnvChannel, i,
1544                          hSbrEncoder->dynamicRam)) {
1545       goto bail;
1546     }
1547   }
1548 
1549   /* Create QMF States */
1550   for (i = 0; i < fixMax(nChannels, (supportPS) ? 2 : 0); i++) {
1551     hSbrEncoder->QmfAnalysis[i].FilterStates = GetRam_Sbr_QmfStatesAnalysis(i);
1552     if (hSbrEncoder->QmfAnalysis[i].FilterStates == NULL) {
1553       goto bail;
1554     }
1555   }
1556 
1557   /* Create Parametric Stereo handle */
1558   if (supportPS) {
1559     if (PSEnc_Create(&hSbrEncoder->hParametricStereo)) {
1560       goto bail;
1561     }
1562 
1563     hSbrEncoder->qmfSynthesisPS.FilterStates = GetRam_PsQmfStatesSynthesis();
1564     if (hSbrEncoder->qmfSynthesisPS.FilterStates == NULL) {
1565       goto bail;
1566     }
1567   } /* supportPS */
1568 
1569   *phSbrEncoder = hSbrEncoder;
1570 
1571   errorStatus = 0;
1572   return errorStatus;
1573 
1574 bail:
1575   /* Close SBR encoder instance */
1576   sbrEncoder_Close(&hSbrEncoder);
1577   return errorStatus;
1578 }
1579 
FDKsbrEnc_Reallocate(HANDLE_SBR_ENCODER hSbrEncoder,SBR_ELEMENT_INFO elInfo[(8)],const INT noElements)1580 static INT FDKsbrEnc_Reallocate(HANDLE_SBR_ENCODER hSbrEncoder,
1581                                 SBR_ELEMENT_INFO elInfo[(8)],
1582                                 const INT noElements) {
1583   INT totalCh = 0;
1584   INT totalQmf = 0;
1585   INT coreEl;
1586   INT el = -1;
1587 
1588   hSbrEncoder->lfeChIdx = -1; /* default value, until lfe found */
1589 
1590   for (coreEl = 0; coreEl < noElements; coreEl++) {
1591     /* SBR only handles SCE and CPE's */
1592     if (elInfo[coreEl].elType == ID_SCE || elInfo[coreEl].elType == ID_CPE) {
1593       el++;
1594     } else {
1595       if (elInfo[coreEl].elType == ID_LFE) {
1596         hSbrEncoder->lfeChIdx = elInfo[coreEl].ChannelIndex[0];
1597       }
1598       continue;
1599     }
1600 
1601     SBR_ELEMENT_INFO *pelInfo = &elInfo[coreEl];
1602     HANDLE_SBR_ELEMENT hSbrElement = hSbrEncoder->sbrElement[el];
1603 
1604     int ch;
1605     for (ch = 0; ch < pelInfo->nChannelsInEl; ch++) {
1606       hSbrElement->sbrChannel[ch] = hSbrEncoder->pSbrChannel[totalCh];
1607       totalCh++;
1608     }
1609     /* analysis QMF */
1610     for (ch = 0;
1611          ch < ((pelInfo->fParametricStereo) ? 2 : pelInfo->nChannelsInEl);
1612          ch++) {
1613       hSbrElement->elInfo.ChannelIndex[ch] = pelInfo->ChannelIndex[ch];
1614       hSbrElement->hQmfAnalysis[ch] = &hSbrEncoder->QmfAnalysis[totalQmf++];
1615     }
1616 
1617     /* Copy Element info */
1618     hSbrElement->elInfo.elType = pelInfo->elType;
1619     hSbrElement->elInfo.instanceTag = pelInfo->instanceTag;
1620     hSbrElement->elInfo.nChannelsInEl = pelInfo->nChannelsInEl;
1621     hSbrElement->elInfo.fParametricStereo = pelInfo->fParametricStereo;
1622     hSbrElement->elInfo.fDualMono = pelInfo->fDualMono;
1623   } /* coreEl */
1624 
1625   return 0;
1626 }
1627 
1628 /*****************************************************************************
1629 
1630  functionname: FDKsbrEnc_bsBufInit
1631  description:  initializes bitstream buffer
1632  returns:      initialized bitstream buffer in env encoder
1633  input:
1634  output:       hEnv
1635 
1636 *****************************************************************************/
FDKsbrEnc_bsBufInit(HANDLE_SBR_ELEMENT hSbrElement,int nBitstrDelay)1637 static INT FDKsbrEnc_bsBufInit(HANDLE_SBR_ELEMENT hSbrElement,
1638                                int nBitstrDelay) {
1639   UCHAR *bitstreamBuffer;
1640 
1641   /* initialize the bitstream buffer */
1642   bitstreamBuffer = hSbrElement->payloadDelayLine[nBitstrDelay];
1643   FDKinitBitStream(&hSbrElement->CmonData.sbrBitbuf, bitstreamBuffer,
1644                    MAX_PAYLOAD_SIZE * sizeof(UCHAR), 0, BS_WRITER);
1645 
1646   return (0);
1647 }
1648 
1649 /*****************************************************************************
1650 
1651  functionname: FDKsbrEnc_EnvInit
1652  description:  initializes parameters
1653  returns:      error status
1654  input:
1655  output:       hEnv
1656 
1657 *****************************************************************************/
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)1658 static INT FDKsbrEnc_EnvInit(HANDLE_SBR_ELEMENT hSbrElement,
1659                              sbrConfigurationPtr params, INT *coreBandWith,
1660                              AUDIO_OBJECT_TYPE aot, int nElement,
1661                              const int headerPeriod, ULONG statesInitFlag,
1662                              const SBRENC_DS_TYPE downsamplingMethod,
1663                              UCHAR *dynamic_RAM) {
1664   int ch, i;
1665 
1666   if ((params->codecSettings.nChannels < 1) ||
1667       (params->codecSettings.nChannels > MAX_NUM_CHANNELS)) {
1668     return (1);
1669   }
1670 
1671   /* init and set syntax flags */
1672   hSbrElement->sbrConfigData.sbrSyntaxFlags = 0;
1673 
1674   switch (aot) {
1675     case AOT_ER_AAC_ELD:
1676       hSbrElement->sbrConfigData.sbrSyntaxFlags |= SBR_SYNTAX_LOW_DELAY;
1677       break;
1678     default:
1679       break;
1680   }
1681   if (params->crcSbr) {
1682     hSbrElement->sbrConfigData.sbrSyntaxFlags |= SBR_SYNTAX_CRC;
1683   }
1684 
1685   hSbrElement->sbrConfigData.noQmfBands = 64 >> (2 - params->downSampleFactor);
1686   switch (hSbrElement->sbrConfigData.noQmfBands) {
1687     case 64:
1688       hSbrElement->sbrConfigData.noQmfSlots = params->sbrFrameSize >> 6;
1689       break;
1690     case 32:
1691       hSbrElement->sbrConfigData.noQmfSlots = params->sbrFrameSize >> 5;
1692       break;
1693     default:
1694       hSbrElement->sbrConfigData.noQmfSlots = params->sbrFrameSize >> 6;
1695       return (2);
1696   }
1697 
1698   /*
1699     now initialize sbrConfigData, sbrHeaderData and sbrBitstreamData,
1700   */
1701   hSbrElement->sbrConfigData.nChannels = params->codecSettings.nChannels;
1702 
1703   if (params->codecSettings.nChannels == 2) {
1704     if ((hSbrElement->elInfo.elType == ID_CPE) &&
1705         ((hSbrElement->elInfo.fDualMono == 1))) {
1706       hSbrElement->sbrConfigData.stereoMode = SBR_LEFT_RIGHT;
1707     } else {
1708       hSbrElement->sbrConfigData.stereoMode = params->stereoMode;
1709     }
1710   } else {
1711     hSbrElement->sbrConfigData.stereoMode = SBR_MONO;
1712   }
1713 
1714   hSbrElement->sbrConfigData.frameSize = params->sbrFrameSize;
1715 
1716   hSbrElement->sbrConfigData.sampleFreq =
1717       params->downSampleFactor * params->codecSettings.sampleFreq;
1718 
1719   hSbrElement->sbrBitstreamData.CountSendHeaderData = 0;
1720   if (params->SendHeaderDataTime > 0) {
1721     if (headerPeriod == -1) {
1722       hSbrElement->sbrBitstreamData.NrSendHeaderData = (INT)(
1723           params->SendHeaderDataTime * hSbrElement->sbrConfigData.sampleFreq /
1724           (1000 * hSbrElement->sbrConfigData.frameSize));
1725       hSbrElement->sbrBitstreamData.NrSendHeaderData =
1726           fixMax(hSbrElement->sbrBitstreamData.NrSendHeaderData, 1);
1727     } else {
1728       /* assure header period at least once per second */
1729       hSbrElement->sbrBitstreamData.NrSendHeaderData = fixMin(
1730           fixMax(headerPeriod, 1), (hSbrElement->sbrConfigData.sampleFreq /
1731                                     hSbrElement->sbrConfigData.frameSize));
1732     }
1733   } else {
1734     hSbrElement->sbrBitstreamData.NrSendHeaderData = 0;
1735   }
1736 
1737   hSbrElement->sbrHeaderData.sbr_data_extra = params->sbr_data_extra;
1738   hSbrElement->sbrBitstreamData.HeaderActive = 0;
1739   hSbrElement->sbrBitstreamData.rightBorderFIX = 0;
1740   hSbrElement->sbrHeaderData.sbr_start_frequency = params->startFreq;
1741   hSbrElement->sbrHeaderData.sbr_stop_frequency = params->stopFreq;
1742   hSbrElement->sbrHeaderData.sbr_xover_band = 0;
1743   hSbrElement->sbrHeaderData.sbr_lc_stereo_mode = 0;
1744 
1745   /* data_extra */
1746   if (params->sbr_xpos_ctrl != SBR_XPOS_CTRL_DEFAULT)
1747     hSbrElement->sbrHeaderData.sbr_data_extra = 1;
1748 
1749   hSbrElement->sbrHeaderData.sbr_amp_res = (AMP_RES)params->amp_res;
1750   hSbrElement->sbrConfigData.initAmpResFF = params->init_amp_res_FF;
1751 
1752   /* header_extra_1 */
1753   hSbrElement->sbrHeaderData.freqScale = params->freqScale;
1754   hSbrElement->sbrHeaderData.alterScale = params->alterScale;
1755   hSbrElement->sbrHeaderData.sbr_noise_bands = params->sbr_noise_bands;
1756   hSbrElement->sbrHeaderData.header_extra_1 = 0;
1757 
1758   if ((params->freqScale != SBR_FREQ_SCALE_DEFAULT) ||
1759       (params->alterScale != SBR_ALTER_SCALE_DEFAULT) ||
1760       (params->sbr_noise_bands != SBR_NOISE_BANDS_DEFAULT)) {
1761     hSbrElement->sbrHeaderData.header_extra_1 = 1;
1762   }
1763 
1764   /* header_extra_2 */
1765   hSbrElement->sbrHeaderData.sbr_limiter_bands = params->sbr_limiter_bands;
1766   hSbrElement->sbrHeaderData.sbr_limiter_gains = params->sbr_limiter_gains;
1767 
1768   if ((hSbrElement->sbrConfigData.sampleFreq > 48000) &&
1769       (hSbrElement->sbrHeaderData.sbr_start_frequency >= 9)) {
1770     hSbrElement->sbrHeaderData.sbr_limiter_gains = SBR_LIMITER_GAINS_INFINITE;
1771   }
1772 
1773   hSbrElement->sbrHeaderData.sbr_interpol_freq = params->sbr_interpol_freq;
1774   hSbrElement->sbrHeaderData.sbr_smoothing_length =
1775       params->sbr_smoothing_length;
1776   hSbrElement->sbrHeaderData.header_extra_2 = 0;
1777 
1778   if ((params->sbr_limiter_bands != SBR_LIMITER_BANDS_DEFAULT) ||
1779       (params->sbr_limiter_gains != SBR_LIMITER_GAINS_DEFAULT) ||
1780       (params->sbr_interpol_freq != SBR_INTERPOL_FREQ_DEFAULT) ||
1781       (params->sbr_smoothing_length != SBR_SMOOTHING_LENGTH_DEFAULT)) {
1782     hSbrElement->sbrHeaderData.header_extra_2 = 1;
1783   }
1784 
1785   /* other switches */
1786   hSbrElement->sbrConfigData.useWaveCoding = params->useWaveCoding;
1787   hSbrElement->sbrConfigData.useParametricCoding = params->parametricCoding;
1788   hSbrElement->sbrConfigData.thresholdAmpResFF_m =
1789       params->threshold_AmpRes_FF_m;
1790   hSbrElement->sbrConfigData.thresholdAmpResFF_e =
1791       params->threshold_AmpRes_FF_e;
1792 
1793   /* init freq band table */
1794   if (updateFreqBandTable(&hSbrElement->sbrConfigData,
1795                           &hSbrElement->sbrHeaderData,
1796                           params->downSampleFactor)) {
1797     return (1);
1798   }
1799 
1800   /* now create envelope ext and QMF for each available channel */
1801   for (ch = 0; ch < hSbrElement->sbrConfigData.nChannels; ch++) {
1802     if (initEnvChannel(&hSbrElement->sbrConfigData, &hSbrElement->sbrHeaderData,
1803                        &hSbrElement->sbrChannel[ch]->hEnvChannel, params,
1804                        statesInitFlag, ch, dynamic_RAM)) {
1805       return (1);
1806     }
1807 
1808   } /* nChannels */
1809 
1810   /* reset and intialize analysis qmf */
1811   for (ch = 0; ch < ((hSbrElement->elInfo.fParametricStereo)
1812                          ? 2
1813                          : hSbrElement->sbrConfigData.nChannels);
1814        ch++) {
1815     int err;
1816     UINT qmfFlags =
1817         (hSbrElement->sbrConfigData.sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY)
1818             ? QMF_FLAG_CLDFB
1819             : 0;
1820     if (statesInitFlag)
1821       qmfFlags &= ~QMF_FLAG_KEEP_STATES;
1822     else
1823       qmfFlags |= QMF_FLAG_KEEP_STATES;
1824 
1825     err = qmfInitAnalysisFilterBank(
1826         hSbrElement->hQmfAnalysis[ch],
1827         (FIXP_QAS *)hSbrElement->hQmfAnalysis[ch]->FilterStates,
1828         hSbrElement->sbrConfigData.noQmfSlots,
1829         hSbrElement->sbrConfigData.noQmfBands,
1830         hSbrElement->sbrConfigData.noQmfBands,
1831         hSbrElement->sbrConfigData.noQmfBands, qmfFlags);
1832     if (0 != err) {
1833       return err;
1834     }
1835   }
1836 
1837   /*  */
1838   hSbrElement->CmonData.xOverFreq = hSbrElement->sbrConfigData.xOverFreq;
1839   hSbrElement->CmonData.dynBwEnabled =
1840       (params->dynBwSupported && params->dynBwEnabled);
1841   hSbrElement->CmonData.dynXOverFreqEnc =
1842       FDKsbrEnc_SbrGetXOverFreq(hSbrElement, hSbrElement->CmonData.xOverFreq);
1843   for (i = 0; i < 5; i++)
1844     hSbrElement->dynXOverFreqDelay[i] = hSbrElement->CmonData.dynXOverFreqEnc;
1845   hSbrElement->CmonData.sbrNumChannels = hSbrElement->sbrConfigData.nChannels;
1846   hSbrElement->sbrConfigData.dynXOverFreq = hSbrElement->CmonData.xOverFreq;
1847 
1848   /* Update Bandwith to be passed to the core encoder */
1849   *coreBandWith = hSbrElement->CmonData.xOverFreq;
1850 
1851   return (0);
1852 }
1853 
sbrEncoder_GetInBufferSize(int noChannels)1854 INT sbrEncoder_GetInBufferSize(int noChannels) {
1855   INT temp;
1856 
1857   temp = (2048);
1858   temp += 1024 + MAX_SAMPLE_DELAY;
1859   temp *= noChannels;
1860   temp *= sizeof(INT_PCM);
1861   return temp;
1862 }
1863 
1864 /*
1865  * Encode Dummy SBR payload frames to fill the delay lines.
1866  */
FDKsbrEnc_DelayCompensation(HANDLE_SBR_ENCODER hEnvEnc,INT_PCM * timeBuffer,UINT timeBufferBufSize)1867 static INT FDKsbrEnc_DelayCompensation(HANDLE_SBR_ENCODER hEnvEnc,
1868                                        INT_PCM *timeBuffer,
1869                                        UINT timeBufferBufSize) {
1870   int n, el;
1871 
1872   for (n = hEnvEnc->nBitstrDelay; n > 0; n--) {
1873     for (el = 0; el < hEnvEnc->noElements; el++) {
1874       if (FDKsbrEnc_EnvEncodeFrame(
1875               hEnvEnc, el,
1876               timeBuffer + hEnvEnc->downsampledOffset / hEnvEnc->nChannels,
1877               timeBufferBufSize, NULL, NULL, 1))
1878         return -1;
1879     }
1880     sbrEncoder_UpdateBuffers(hEnvEnc, timeBuffer, timeBufferBufSize);
1881   }
1882   return 0;
1883 }
1884 
sbrEncoder_LimitBitRate(UINT bitRate,UINT numChannels,UINT coreSampleRate,AUDIO_OBJECT_TYPE aot)1885 UINT sbrEncoder_LimitBitRate(UINT bitRate, UINT numChannels,
1886                              UINT coreSampleRate, AUDIO_OBJECT_TYPE aot) {
1887   UINT newBitRate = bitRate;
1888   INT index;
1889 
1890   FDK_ASSERT(numChannels > 0 && numChannels <= 2);
1891   if (aot == AOT_PS) {
1892     if (numChannels == 1) {
1893       index = getPsTuningTableIndex(bitRate, &newBitRate);
1894       if (index == INVALID_TABLE_IDX) {
1895         bitRate = newBitRate;
1896       }
1897     } else {
1898       return 0;
1899     }
1900   }
1901   index = getSbrTuningTableIndex(bitRate, numChannels, coreSampleRate, aot,
1902                                  &newBitRate);
1903   if (index != INVALID_TABLE_IDX) {
1904     newBitRate = bitRate;
1905   }
1906 
1907   return newBitRate;
1908 }
1909 
sbrEncoder_IsSingleRatePossible(AUDIO_OBJECT_TYPE aot)1910 UINT sbrEncoder_IsSingleRatePossible(AUDIO_OBJECT_TYPE aot) {
1911   UINT isPossible = (AOT_PS == aot) ? 0 : 1;
1912   return isPossible;
1913 }
1914 
1915 /*****************************************************************************/
1916 /*                                                                           */
1917 /*functionname: sbrEncoder_Init_delay                                        */
1918 /*description:  Determine Delay balancing and new encoder delay              */
1919 /*                                                                           */
1920 /*returns:      - error status                                               */
1921 /*input:        - frame length of the core (i.e. e.g. AAC)                   */
1922 /*              - number of channels                                         */
1923 /*              - downsample factor (1 for downsampled, 2 for dual-rate SBR) */
1924 /*              - low delay presence                                         */
1925 /*              - ps presence                                                */
1926 /*              - downsampling method: QMF-, time domain or no downsampling  */
1927 /*              - various delay values (see DELAY_PARAM struct description)  */
1928 /*                                                                           */
1929 /*Example: Delay balancing for a HE-AACv1 encoder (time-domain downsampling) */
1930 /*========================================================================== */
1931 /*                                                                           */
1932 /*    +--------+            +--------+ +--------+ +--------+ +--------+      */
1933 /*    |core    |            |ds 2:1  | |AAC     | |QMF     | |QMF     |      */
1934 /*  +-+path    +------------+        +-+core    +-+analysis+-+overlap +-+    */
1935 /*  | |offset  |            |        | |        | |32 bands| |        | |    */
1936 /*  | +--------+            +--------+ +--------+ +--------+ +--------+ |    */
1937 /*  |                              core path                    +-------++   */
1938 /*  |                                                           |QMF     |   */
1939 /*->+                                                           +synth.  +-> */
1940 /*  |                                                           |64 bands|   */
1941 /*  |                                                           +-------++   */
1942 /*  | +--------+ +--------+ +--------+ +--------+                       |    */
1943 /*  | |SBR path| |QMF     | |subband | |bs delay|                       |    */
1944 /*  +-+offset  +-+analysis+-+sample  +-+(full   +-----------------------+    */
1945 /*    |        | |64 bands| |buffer  | | frames)|                            */
1946 /*    +--------+ +--------+ +--------+ +--------+                            */
1947 /*                                 SBR path                                  */
1948 /*                                                                           */
1949 /*****************************************************************************/
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)1950 static INT sbrEncoder_Init_delay(
1951     const int coreFrameLength,               /* input */
1952     const int numChannels,                   /* input */
1953     const int downSampleFactor,              /* input */
1954     const int lowDelay,                      /* input */
1955     const int usePs,                         /* input */
1956     const int is212,                         /* input */
1957     const SBRENC_DS_TYPE downsamplingMethod, /* input */
1958     DELAY_PARAM *hDelayParam                 /* input/output */
1959 ) {
1960   int delayCorePath = 0;   /* delay in core path */
1961   int delaySbrPath = 0;    /* delay difference in QMF aka SBR path */
1962   int delayInput2Core = 0; /* delay from the input to the core */
1963   int delaySbrDec = 0;     /* delay of the decoder's SBR module */
1964 
1965   int delayCore = hDelayParam->delay; /* delay of the core */
1966 
1967   /* Added delay by the SBR delay initialization */
1968   int corePathOffset = 0; /* core path */
1969   int sbrPathOffset = 0;  /* sbr path */
1970   int bitstreamDelay = 0; /* sbr path, framewise */
1971 
1972   int flCore = coreFrameLength; /* core frame length */
1973 
1974   int returnValue = 0; /* return value - 0 means: no error */
1975 
1976   /* 1) Calculate actual delay  for core and SBR path */
1977   if (is212) {
1978     delayCorePath = DELAY_COREPATH_ELDv2SBR(flCore, downSampleFactor);
1979     delaySbrPath = DELAY_ELDv2SBR(flCore, downSampleFactor);
1980     delaySbrDec = ((flCore) / 2) * (downSampleFactor);
1981   } else if (lowDelay) {
1982     delayCorePath = DELAY_COREPATH_ELDSBR(flCore, downSampleFactor);
1983     delaySbrPath = DELAY_ELDSBR(flCore, downSampleFactor);
1984     delaySbrDec = DELAY_QMF_POSTPROC(downSampleFactor);
1985   } else if (usePs) {
1986     delayCorePath = DELAY_COREPATH_PS(flCore, downSampleFactor);
1987     delaySbrPath = DELAY_PS(flCore, downSampleFactor);
1988     delaySbrDec = DELAY_COREPATH_SBR(flCore, downSampleFactor);
1989   } else {
1990     delayCorePath = DELAY_COREPATH_SBR(flCore, downSampleFactor);
1991     delaySbrPath = DELAY_SBR(flCore, downSampleFactor);
1992     delaySbrDec = DELAY_COREPATH_SBR(flCore, downSampleFactor);
1993   }
1994   delayCorePath += delayCore * downSampleFactor;
1995   delayCorePath +=
1996       (downsamplingMethod == SBRENC_DS_TIME) ? hDelayParam->dsDelay : 0;
1997 
1998   /* 2) Manage coupling of paths */
1999   if (downsamplingMethod == SBRENC_DS_QMF && delayCorePath > delaySbrPath) {
2000     /* In case of QMF downsampling, both paths are coupled, i.e. the SBR path
2001        offset would be added to both the SBR path and to the core path
2002        as well, thus making it impossible to achieve delay balancing.
2003        To overcome that problem, a framewise delay is added to the SBR path
2004        first, until the overall delay of the core path is shorter than
2005        the delay of the SBR path. When this is achieved, the missing delay
2006        difference can be added as downsampled offset to the core path.
2007     */
2008     while (delayCorePath > delaySbrPath) {
2009       /* Add one frame delay to SBR path */
2010       delaySbrPath += flCore * downSampleFactor;
2011       bitstreamDelay += 1;
2012     }
2013   }
2014 
2015   /* 3) Calculate necessary additional delay to balance the paths */
2016   if (delayCorePath > delaySbrPath) {
2017     /* Delay QMF input */
2018     while (delayCorePath > delaySbrPath + (int)flCore * (int)downSampleFactor) {
2019       /* Do bitstream frame-wise delay balancing if there are
2020          more than SBR framelength samples delay difference */
2021       delaySbrPath += flCore * downSampleFactor;
2022       bitstreamDelay += 1;
2023     }
2024     /* Multiply input offset by input channels */
2025     corePathOffset = 0;
2026     sbrPathOffset = (delayCorePath - delaySbrPath) * numChannels;
2027   } else {
2028     /* Delay AAC data */
2029     /* Multiply downsampled offset by AAC core channels. Divide by 2 because of
2030        half samplerate of downsampled data. */
2031     corePathOffset = ((delaySbrPath - delayCorePath) * numChannels) >>
2032                      (downSampleFactor - 1);
2033     sbrPathOffset = 0;
2034   }
2035 
2036   /* 4) Calculate delay from input to core */
2037   if (usePs) {
2038     delayInput2Core =
2039         (DELAY_QMF_ANA(downSampleFactor) + DELAY_QMF_DS + DELAY_HYB_SYN) +
2040         (downSampleFactor * corePathOffset) + 1;
2041   } else if (downsamplingMethod == SBRENC_DS_TIME) {
2042     delayInput2Core = corePathOffset + hDelayParam->dsDelay;
2043   } else {
2044     delayInput2Core = corePathOffset;
2045   }
2046 
2047   /* 6) Set output parameters */
2048   hDelayParam->delay = FDKmax(delayCorePath, delaySbrPath); /* overall delay */
2049   hDelayParam->sbrDecDelay = delaySbrDec;         /* SBR decoder delay */
2050   hDelayParam->delayInput2Core = delayInput2Core; /* delay input - core */
2051   hDelayParam->bitstrDelay = bitstreamDelay;    /* bitstream delay, in frames */
2052   hDelayParam->corePathOffset = corePathOffset; /* offset added to core path */
2053   hDelayParam->sbrPathOffset = sbrPathOffset;   /* offset added to SBR path */
2054 
2055   return returnValue;
2056 }
2057 
2058 /*****************************************************************************
2059 
2060  functionname: sbrEncoder_Init
2061  description:  initializes the SBR encoder
2062  returns:      error status
2063 
2064 *****************************************************************************/
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)2065 INT sbrEncoder_Init(HANDLE_SBR_ENCODER hSbrEncoder,
2066                     SBR_ELEMENT_INFO elInfo[(8)], int noElements,
2067                     INT_PCM *inputBuffer, UINT inputBufferBufSize,
2068                     INT *coreBandwidth, INT *inputBufferOffset,
2069                     INT *numChannels, const UINT syntaxFlags,
2070                     INT *coreSampleRate, UINT *downSampleFactor,
2071                     INT *frameLength, AUDIO_OBJECT_TYPE aot, int *delay,
2072                     int transformFactor, const int headerPeriod,
2073                     ULONG statesInitFlag) {
2074   HANDLE_ERROR_INFO errorInfo = noError;
2075   sbrConfiguration sbrConfig[(8)];
2076   INT error = 0;
2077   INT lowestBandwidth;
2078   /* Save input parameters */
2079   INT inputSampleRate = *coreSampleRate;
2080   int coreFrameLength = *frameLength;
2081   int inputBandWidth = *coreBandwidth;
2082   int inputChannels = *numChannels;
2083 
2084   SBRENC_DS_TYPE downsamplingMethod = SBRENC_DS_NONE;
2085   int highestSbrStartFreq, highestSbrStopFreq;
2086   int lowDelay = 0;
2087   int usePs = 0;
2088   int is212 = 0;
2089 
2090   DELAY_PARAM delayParam;
2091 
2092   /* check whether SBR setting is available for the current encoder
2093    * configuration (bitrate, samplerate) */
2094   if (!sbrEncoder_IsSingleRatePossible(aot)) {
2095     *downSampleFactor = 2;
2096   }
2097 
2098   if (aot == AOT_PS) {
2099     usePs = 1;
2100   }
2101   if (aot == AOT_ER_AAC_ELD) {
2102     lowDelay = 1;
2103   } else if (aot == AOT_ER_AAC_LD) {
2104     error = 1;
2105     goto bail;
2106   }
2107 
2108   /* Parametric Stereo */
2109   if (usePs) {
2110     if (*numChannels == 2 && noElements == 1) {
2111       /* Override Element type in case of Parametric stereo */
2112       elInfo[0].elType = ID_SCE;
2113       elInfo[0].fParametricStereo = 1;
2114       elInfo[0].nChannelsInEl = 1;
2115       /* core encoder gets downmixed mono signal */
2116       *numChannels = 1;
2117     } else {
2118       error = 1;
2119       goto bail;
2120     }
2121   } /* usePs */
2122 
2123   /* set the core's sample rate */
2124   switch (*downSampleFactor) {
2125     case 1:
2126       *coreSampleRate = inputSampleRate;
2127       downsamplingMethod = SBRENC_DS_NONE;
2128       break;
2129     case 2:
2130       *coreSampleRate = inputSampleRate >> 1;
2131       downsamplingMethod = usePs ? SBRENC_DS_QMF : SBRENC_DS_TIME;
2132       break;
2133     default:
2134       *coreSampleRate = inputSampleRate >> 1;
2135       return 0; /* return error */
2136   }
2137 
2138   /* check whether SBR setting is available for the current encoder
2139    * configuration (bitrate, coreSampleRate) */
2140   {
2141     int el, coreEl;
2142 
2143     /* Check if every element config is feasible */
2144     for (coreEl = 0; coreEl < noElements; coreEl++) {
2145       /* SBR only handles SCE and CPE's */
2146       if (elInfo[coreEl].elType != ID_SCE && elInfo[coreEl].elType != ID_CPE) {
2147         continue;
2148       }
2149       /* check if desired configuration is available */
2150       if (!FDKsbrEnc_IsSbrSettingAvail(elInfo[coreEl].bitRate, 0,
2151                                        elInfo[coreEl].nChannelsInEl,
2152                                        inputSampleRate, *coreSampleRate, aot)) {
2153         error = 1;
2154         goto bail;
2155       }
2156     }
2157 
2158     hSbrEncoder->nChannels = *numChannels;
2159     hSbrEncoder->frameSize = coreFrameLength * *downSampleFactor;
2160     hSbrEncoder->downsamplingMethod = downsamplingMethod;
2161     hSbrEncoder->downSampleFactor = *downSampleFactor;
2162     hSbrEncoder->estimateBitrate = 0;
2163     hSbrEncoder->inputDataDelay = 0;
2164     is212 = ((aot == AOT_ER_AAC_ELD) && (syntaxFlags & AC_LD_MPS)) ? 1 : 0;
2165 
2166     /* Open SBR elements */
2167     el = -1;
2168     highestSbrStartFreq = highestSbrStopFreq = 0;
2169     lowestBandwidth = 99999;
2170 
2171     /* Loop through each core encoder element and get a matching SBR element
2172      * config */
2173     for (coreEl = 0; coreEl < noElements; coreEl++) {
2174       /* SBR only handles SCE and CPE's */
2175       if (elInfo[coreEl].elType == ID_SCE || elInfo[coreEl].elType == ID_CPE) {
2176         el++;
2177       } else {
2178         continue;
2179       }
2180 
2181       /* Set parametric Stereo Flag. */
2182       if (usePs) {
2183         elInfo[coreEl].fParametricStereo = 1;
2184       } else {
2185         elInfo[coreEl].fParametricStereo = 0;
2186       }
2187 
2188       /*
2189        * Init sbrConfig structure
2190        */
2191       if (!FDKsbrEnc_InitializeSbrDefaults(&sbrConfig[el], *downSampleFactor,
2192                                            coreFrameLength, IS_LOWDELAY(aot))) {
2193         error = 1;
2194         goto bail;
2195       }
2196 
2197       /*
2198        * Modify sbrConfig structure according to Element parameters
2199        */
2200       if (!FDKsbrEnc_AdjustSbrSettings(
2201               &sbrConfig[el], elInfo[coreEl].bitRate,
2202               elInfo[coreEl].nChannelsInEl, *coreSampleRate, inputSampleRate,
2203               transformFactor, 24000, 0, 0, /* useSpeechConfig */
2204               0,                            /* lcsMode */
2205               usePs,                        /* bParametricStereo */
2206               aot)) {
2207         error = 1;
2208         goto bail;
2209       }
2210 
2211       /* Find common frequency border for all SBR elements */
2212       highestSbrStartFreq =
2213           fixMax(highestSbrStartFreq, sbrConfig[el].startFreq);
2214       highestSbrStopFreq = fixMax(highestSbrStopFreq, sbrConfig[el].stopFreq);
2215 
2216     } /* first element loop */
2217 
2218     /* Set element count (can be less than core encoder element count) */
2219     hSbrEncoder->noElements = el + 1;
2220 
2221     FDKsbrEnc_Reallocate(hSbrEncoder, elInfo, noElements);
2222 
2223     for (el = 0; el < hSbrEncoder->noElements; el++) {
2224       int bandwidth = *coreBandwidth;
2225 
2226       /* Use lowest common bandwidth */
2227       sbrConfig[el].startFreq = highestSbrStartFreq;
2228       sbrConfig[el].stopFreq = highestSbrStopFreq;
2229 
2230       /* initialize SBR element, and get core bandwidth */
2231       error = FDKsbrEnc_EnvInit(hSbrEncoder->sbrElement[el], &sbrConfig[el],
2232                                 &bandwidth, aot, el, headerPeriod,
2233                                 statesInitFlag, hSbrEncoder->downsamplingMethod,
2234                                 hSbrEncoder->dynamicRam);
2235 
2236       if (error != 0) {
2237         error = 2;
2238         goto bail;
2239       }
2240 
2241       /* Get lowest core encoder bandwidth to be returned later. */
2242       lowestBandwidth = fixMin(lowestBandwidth, bandwidth);
2243 
2244     } /* second element loop */
2245 
2246     /* Initialize a downsampler for each channel in each SBR element */
2247     if (hSbrEncoder->downsamplingMethod == SBRENC_DS_TIME) {
2248       for (el = 0; el < hSbrEncoder->noElements; el++) {
2249         HANDLE_SBR_ELEMENT hSbrEl = hSbrEncoder->sbrElement[el];
2250         INT Wc, ch;
2251 
2252         Wc = 500; /* Cutoff frequency with full bandwidth */
2253 
2254         for (ch = 0; ch < hSbrEl->elInfo.nChannelsInEl; ch++) {
2255           FDKaacEnc_InitDownsampler(&hSbrEl->sbrChannel[ch]->downSampler, Wc,
2256                                     *downSampleFactor);
2257           FDK_ASSERT(hSbrEl->sbrChannel[ch]->downSampler.delay <=
2258                      MAX_DS_FILTER_DELAY);
2259         }
2260       } /* third element loop */
2261 
2262       /* lfe */
2263       FDKaacEnc_InitDownsampler(&hSbrEncoder->lfeDownSampler, 0,
2264                                 *downSampleFactor);
2265     }
2266 
2267     /* Get delay information */
2268     delayParam.dsDelay =
2269         hSbrEncoder->sbrElement[0]->sbrChannel[0]->downSampler.delay;
2270     delayParam.delay = *delay;
2271 
2272     error = sbrEncoder_Init_delay(coreFrameLength, *numChannels,
2273                                   *downSampleFactor, lowDelay, usePs, is212,
2274                                   downsamplingMethod, &delayParam);
2275 
2276     if (error != 0) {
2277       error = 3;
2278       goto bail;
2279     }
2280 
2281     hSbrEncoder->nBitstrDelay = delayParam.bitstrDelay;
2282     hSbrEncoder->sbrDecDelay = delayParam.sbrDecDelay;
2283     hSbrEncoder->inputDataDelay = delayParam.delayInput2Core;
2284 
2285     /* Assign core encoder Bandwidth */
2286     *coreBandwidth = lowestBandwidth;
2287 
2288     /* Estimate sbr bitrate, 2.5 kBit/s per sbr channel */
2289     hSbrEncoder->estimateBitrate += 2500 * (*numChannels);
2290 
2291     /* Initialize bitstream buffer for each element */
2292     for (el = 0; el < hSbrEncoder->noElements; el++) {
2293       FDKsbrEnc_bsBufInit(hSbrEncoder->sbrElement[el], delayParam.bitstrDelay);
2294     }
2295 
2296     /* initialize parametric stereo */
2297     if (usePs) {
2298       PSENC_CONFIG psEncConfig;
2299       FDK_ASSERT(hSbrEncoder->noElements == 1);
2300       INT psTuningTableIdx = getPsTuningTableIndex(elInfo[0].bitRate, NULL);
2301 
2302       psEncConfig.frameSize = coreFrameLength;  // sbrConfig.sbrFrameSize;
2303       psEncConfig.qmfFilterMode = 0;
2304       psEncConfig.sbrPsDelay = 0;
2305 
2306       /* tuning parameters */
2307       if (psTuningTableIdx != INVALID_TABLE_IDX) {
2308         psEncConfig.nStereoBands = psTuningTable[psTuningTableIdx].nStereoBands;
2309         psEncConfig.maxEnvelopes = psTuningTable[psTuningTableIdx].nEnvelopes;
2310         psEncConfig.iidQuantErrorThreshold =
2311             (FIXP_DBL)psTuningTable[psTuningTableIdx].iidQuantErrorThreshold;
2312 
2313         /* calculation is not quite linear, increased number of envelopes causes
2314          * more bits */
2315         /* assume avg. 50 bits per frame for 10 stereo bands / 1 envelope
2316          * configuration */
2317         hSbrEncoder->estimateBitrate +=
2318             ((((*coreSampleRate) * 5 * psEncConfig.nStereoBands *
2319                psEncConfig.maxEnvelopes) /
2320               hSbrEncoder->frameSize));
2321 
2322       } else {
2323         error = ERROR(CDI, "Invalid ps tuning table index.");
2324         goto bail;
2325       }
2326 
2327       qmfInitSynthesisFilterBank(
2328           &hSbrEncoder->qmfSynthesisPS,
2329           (FIXP_DBL *)hSbrEncoder->qmfSynthesisPS.FilterStates,
2330           hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfSlots,
2331           hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands >> 1,
2332           hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands >> 1,
2333           hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands >> 1,
2334           (statesInitFlag) ? 0 : QMF_FLAG_KEEP_STATES);
2335 
2336       if (errorInfo == noError) {
2337         /* update delay */
2338         psEncConfig.sbrPsDelay =
2339             FDKsbrEnc_GetEnvEstDelay(&hSbrEncoder->sbrElement[0]
2340                                           ->sbrChannel[0]
2341                                           ->hEnvChannel.sbrExtractEnvelope);
2342 
2343         errorInfo =
2344             PSEnc_Init(hSbrEncoder->hParametricStereo, &psEncConfig,
2345                        hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfSlots,
2346                        hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands,
2347                        hSbrEncoder->dynamicRam);
2348       }
2349     }
2350 
2351     hSbrEncoder->downsampledOffset = delayParam.corePathOffset;
2352     hSbrEncoder->bufferOffset = delayParam.sbrPathOffset;
2353     *delay = delayParam.delay;
2354 
2355     { hSbrEncoder->downmixSize = coreFrameLength * (*numChannels); }
2356 
2357     /* Delay Compensation: fill bitstream delay buffer with zero input signal */
2358     if (hSbrEncoder->nBitstrDelay > 0) {
2359       error = FDKsbrEnc_DelayCompensation(hSbrEncoder, inputBuffer,
2360                                           inputBufferBufSize);
2361       if (error != 0) goto bail;
2362     }
2363 
2364     /* Set Output frame length */
2365     *frameLength = coreFrameLength * *downSampleFactor;
2366     /* Input buffer offset */
2367     *inputBufferOffset =
2368         fixMax(delayParam.sbrPathOffset, delayParam.corePathOffset);
2369   }
2370 
2371   return error;
2372 
2373 bail:
2374   /* Restore input settings */
2375   *coreSampleRate = inputSampleRate;
2376   *frameLength = coreFrameLength;
2377   *numChannels = inputChannels;
2378   *coreBandwidth = inputBandWidth;
2379 
2380   return error;
2381 }
2382 
sbrEncoder_EncodeFrame(HANDLE_SBR_ENCODER hSbrEncoder,INT_PCM * samples,UINT samplesBufSize,UINT sbrDataBits[(8)],UCHAR sbrData[(8)][MAX_PAYLOAD_SIZE])2383 INT sbrEncoder_EncodeFrame(HANDLE_SBR_ENCODER hSbrEncoder, INT_PCM *samples,
2384                            UINT samplesBufSize, UINT sbrDataBits[(8)],
2385                            UCHAR sbrData[(8)][MAX_PAYLOAD_SIZE]) {
2386   INT error;
2387   int el;
2388 
2389   for (el = 0; el < hSbrEncoder->noElements; el++) {
2390     if (hSbrEncoder->sbrElement[el] != NULL) {
2391       error = FDKsbrEnc_EnvEncodeFrame(
2392           hSbrEncoder, el,
2393           samples + hSbrEncoder->downsampledOffset / hSbrEncoder->nChannels,
2394           samplesBufSize, &sbrDataBits[el], sbrData[el], 0);
2395       if (error) return error;
2396     }
2397   }
2398 
2399   error = FDKsbrEnc_Downsample(
2400       hSbrEncoder,
2401       samples + hSbrEncoder->downsampledOffset / hSbrEncoder->nChannels,
2402       samplesBufSize, hSbrEncoder->nChannels, &sbrDataBits[el], sbrData[el], 0);
2403   if (error) return error;
2404 
2405   return 0;
2406 }
2407 
sbrEncoder_UpdateBuffers(HANDLE_SBR_ENCODER hSbrEncoder,INT_PCM * timeBuffer,UINT timeBufferBufSize)2408 INT sbrEncoder_UpdateBuffers(HANDLE_SBR_ENCODER hSbrEncoder,
2409                              INT_PCM *timeBuffer, UINT timeBufferBufSize) {
2410   if (hSbrEncoder->downsampledOffset > 0) {
2411     int c;
2412     int nd = hSbrEncoder->downmixSize / hSbrEncoder->nChannels;
2413 
2414     for (c = 0; c < hSbrEncoder->nChannels; c++) {
2415       /* Move delayed downsampled data */
2416       FDKmemcpy(timeBuffer + timeBufferBufSize * c,
2417                 timeBuffer + timeBufferBufSize * c + nd,
2418                 sizeof(INT_PCM) *
2419                     (hSbrEncoder->downsampledOffset / hSbrEncoder->nChannels));
2420     }
2421   } else {
2422     int c;
2423 
2424     for (c = 0; c < hSbrEncoder->nChannels; c++) {
2425       /* Move delayed input data */
2426       FDKmemcpy(
2427           timeBuffer + timeBufferBufSize * c,
2428           timeBuffer + timeBufferBufSize * c + hSbrEncoder->frameSize,
2429           sizeof(INT_PCM) * hSbrEncoder->bufferOffset / hSbrEncoder->nChannels);
2430     }
2431   }
2432   if (hSbrEncoder->nBitstrDelay > 0) {
2433     int el;
2434 
2435     for (el = 0; el < hSbrEncoder->noElements; el++) {
2436       FDKmemmove(
2437           hSbrEncoder->sbrElement[el]->payloadDelayLine[0],
2438           hSbrEncoder->sbrElement[el]->payloadDelayLine[1],
2439           sizeof(UCHAR) * (hSbrEncoder->nBitstrDelay * MAX_PAYLOAD_SIZE));
2440 
2441       FDKmemmove(&hSbrEncoder->sbrElement[el]->payloadDelayLineSize[0],
2442                  &hSbrEncoder->sbrElement[el]->payloadDelayLineSize[1],
2443                  sizeof(UINT) * (hSbrEncoder->nBitstrDelay));
2444     }
2445   }
2446   return 0;
2447 }
2448 
sbrEncoder_SendHeader(HANDLE_SBR_ENCODER hSbrEncoder)2449 INT sbrEncoder_SendHeader(HANDLE_SBR_ENCODER hSbrEncoder) {
2450   INT error = -1;
2451   if (hSbrEncoder) {
2452     int el;
2453     for (el = 0; el < hSbrEncoder->noElements; el++) {
2454       if ((hSbrEncoder->noElements == 1) &&
2455           (hSbrEncoder->sbrElement[0]->elInfo.fParametricStereo == 1)) {
2456         hSbrEncoder->sbrElement[el]->sbrBitstreamData.CountSendHeaderData =
2457             hSbrEncoder->sbrElement[el]->sbrBitstreamData.NrSendHeaderData - 1;
2458       } else {
2459         hSbrEncoder->sbrElement[el]->sbrBitstreamData.CountSendHeaderData = 0;
2460       }
2461     }
2462     error = 0;
2463   }
2464   return error;
2465 }
2466 
sbrEncoder_ContainsHeader(HANDLE_SBR_ENCODER hSbrEncoder)2467 INT sbrEncoder_ContainsHeader(HANDLE_SBR_ENCODER hSbrEncoder) {
2468   INT sbrHeader = 1;
2469   if (hSbrEncoder) {
2470     int el;
2471     for (el = 0; el < hSbrEncoder->noElements; el++) {
2472       sbrHeader &=
2473           (hSbrEncoder->sbrElement[el]->sbrBitstreamData.HeaderActiveDelay == 1)
2474               ? 1
2475               : 0;
2476     }
2477   }
2478   return sbrHeader;
2479 }
2480 
sbrEncoder_GetHeaderDelay(HANDLE_SBR_ENCODER hSbrEncoder)2481 INT sbrEncoder_GetHeaderDelay(HANDLE_SBR_ENCODER hSbrEncoder) {
2482   INT delay = -1;
2483 
2484   if (hSbrEncoder) {
2485     if ((hSbrEncoder->noElements == 1) &&
2486         (hSbrEncoder->sbrElement[0]->elInfo.fParametricStereo == 1)) {
2487       delay = hSbrEncoder->nBitstrDelay + 1;
2488     } else {
2489       delay = hSbrEncoder->nBitstrDelay;
2490     }
2491   }
2492   return delay;
2493 }
sbrEncoder_GetBsDelay(HANDLE_SBR_ENCODER hSbrEncoder)2494 INT sbrEncoder_GetBsDelay(HANDLE_SBR_ENCODER hSbrEncoder) {
2495   INT delay = -1;
2496 
2497   if (hSbrEncoder) {
2498     delay = hSbrEncoder->nBitstrDelay;
2499   }
2500   return delay;
2501 }
2502 
sbrEncoder_SAPPrepare(HANDLE_SBR_ENCODER hSbrEncoder)2503 INT sbrEncoder_SAPPrepare(HANDLE_SBR_ENCODER hSbrEncoder) {
2504   INT error = -1;
2505   if (hSbrEncoder) {
2506     int el;
2507     for (el = 0; el < hSbrEncoder->noElements; el++) {
2508       hSbrEncoder->sbrElement[el]->sbrBitstreamData.rightBorderFIX = 1;
2509     }
2510     error = 0;
2511   }
2512   return error;
2513 }
2514 
sbrEncoder_GetEstimateBitrate(HANDLE_SBR_ENCODER hSbrEncoder)2515 INT sbrEncoder_GetEstimateBitrate(HANDLE_SBR_ENCODER hSbrEncoder) {
2516   INT estimateBitrate = 0;
2517 
2518   if (hSbrEncoder) {
2519     estimateBitrate += hSbrEncoder->estimateBitrate;
2520   }
2521 
2522   return estimateBitrate;
2523 }
2524 
sbrEncoder_GetInputDataDelay(HANDLE_SBR_ENCODER hSbrEncoder)2525 INT sbrEncoder_GetInputDataDelay(HANDLE_SBR_ENCODER hSbrEncoder) {
2526   INT delay = -1;
2527 
2528   if (hSbrEncoder) {
2529     delay = hSbrEncoder->inputDataDelay;
2530   }
2531   return delay;
2532 }
2533 
sbrEncoder_GetSbrDecDelay(HANDLE_SBR_ENCODER hSbrEncoder)2534 INT sbrEncoder_GetSbrDecDelay(HANDLE_SBR_ENCODER hSbrEncoder) {
2535   INT delay = -1;
2536 
2537   if (hSbrEncoder) {
2538     delay = hSbrEncoder->sbrDecDelay;
2539   }
2540   return delay;
2541 }
2542 
sbrEncoder_GetLibInfo(LIB_INFO * info)2543 INT sbrEncoder_GetLibInfo(LIB_INFO *info) {
2544   int i;
2545 
2546   if (info == NULL) {
2547     return -1;
2548   }
2549   /* search for next free tab */
2550   for (i = 0; i < FDK_MODULE_LAST; i++) {
2551     if (info[i].module_id == FDK_NONE) break;
2552   }
2553   if (i == FDK_MODULE_LAST) {
2554     return -1;
2555   }
2556   info += i;
2557 
2558   info->module_id = FDK_SBRENC;
2559   info->version =
2560       LIB_VERSION(SBRENCODER_LIB_VL0, SBRENCODER_LIB_VL1, SBRENCODER_LIB_VL2);
2561   LIB_VERSION_STRING(info);
2562 #ifdef SUPPRESS_BUILD_DATE_INFO
2563   info->build_date = "";
2564   info->build_time = "";
2565 #else
2566   info->build_date = __DATE__;
2567   info->build_time = __TIME__;
2568 #endif
2569   info->title = "SBR Encoder";
2570 
2571   /* Set flags */
2572   info->flags = 0 | CAPF_SBR_HQ | CAPF_SBR_PS_MPEG;
2573   /* End of flags */
2574 
2575   return 0;
2576 }
2577