• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5 Forschung e.V. All rights reserved.
6 
7  1.    INTRODUCTION
8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
11 a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14 general perceptual audio codecs. AAC-ELD is considered the best-performing
15 full-bandwidth communications codec by independent studies and is widely
16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17 specifications.
18 
19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
20 those of Fraunhofer) may be obtained through Via Licensing
21 (www.vialicensing.com) or through the respective patent owners individually for
22 the purpose of encoding or decoding bit streams in products that are compliant
23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24 Android devices already license these patent claims through Via Licensing or
25 directly from the patent owners, and therefore FDK AAC Codec software may
26 already be covered under those patent licenses when it is used for those
27 licensed purposes only.
28 
29 Commercially-licensed AAC software libraries, including floating-point versions
30 with enhanced sound quality, are also available from Fraunhofer. Users are
31 encouraged to check the Fraunhofer website for additional applications
32 information and documentation.
33 
34 2.    COPYRIGHT LICENSE
35 
36 Redistribution and use in source and binary forms, with or without modification,
37 are permitted without payment of copyright license fees provided that you
38 satisfy the following conditions:
39 
40 You must retain the complete text of this software license in redistributions of
41 the FDK AAC Codec or your modifications thereto in source code form.
42 
43 You must retain the complete text of this software license in the documentation
44 and/or other materials provided with redistributions of the FDK AAC Codec or
45 your modifications thereto in binary form. You must make available free of
46 charge copies of the complete source code of the FDK AAC Codec and your
47 modifications thereto to recipients of copies in binary form.
48 
49 The name of Fraunhofer may not be used to endorse or promote products derived
50 from this library without prior written permission.
51 
52 You may not charge copyright license fees for anyone to use, copy or distribute
53 the FDK AAC Codec software or your modifications thereto.
54 
55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
56 that you changed the software and the date of any change. For modified versions
57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59 AAC Codec Library for Android."
60 
61 3.    NO PATENT LICENSE
62 
63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65 Fraunhofer provides no warranty of patent non-infringement with respect to this
66 software.
67 
68 You may use this FDK AAC Codec software or modifications thereto only for
69 purposes that are authorized by appropriate patent licenses.
70 
71 4.    DISCLAIMER
72 
73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75 including but not limited to the implied warranties of merchantability and
76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78 or consequential damages, including but not limited to procurement of substitute
79 goods or services; loss of use, data, or profits, or business interruption,
80 however caused and on any theory of liability, whether in contract, strict
81 liability, or tort (including negligence), arising in any way out of the use of
82 this software, even if advised of the possibility of such damage.
83 
84 5.    CONTACT INFORMATION
85 
86 Fraunhofer Institute for Integrated Circuits IIS
87 Attention: Audio and Multimedia Departments - FDK AAC LL
88 Am Wolfsmantel 33
89 91058 Erlangen, Germany
90 
91 www.iis.fraunhofer.de/amm
92 amm-info@iis.fraunhofer.de
93 ----------------------------------------------------------------------------- */
94 
95 /**************************** SBR encoder library ******************************
96 
97    Author(s):
98 
99    Description:
100 
101 *******************************************************************************/
102 
103 #include "code_env.h"
104 #include "sbrenc_rom.h"
105 
106 /*****************************************************************************
107 
108  functionname: FDKsbrEnc_InitSbrHuffmanTables
109  description:  initializes Huffman Tables dependent on chosen amp_res
110  returns:      error handle
111  input:
112  output:
113 
114 *****************************************************************************/
FDKsbrEnc_InitSbrHuffmanTables(HANDLE_SBR_ENV_DATA sbrEnvData,HANDLE_SBR_CODE_ENVELOPE henv,HANDLE_SBR_CODE_ENVELOPE hnoise,AMP_RES amp_res)115 INT FDKsbrEnc_InitSbrHuffmanTables(HANDLE_SBR_ENV_DATA sbrEnvData,
116                                    HANDLE_SBR_CODE_ENVELOPE henv,
117                                    HANDLE_SBR_CODE_ENVELOPE hnoise,
118                                    AMP_RES amp_res) {
119   if ((!henv) || (!hnoise) || (!sbrEnvData)) return (1); /* not init. */
120 
121   sbrEnvData->init_sbr_amp_res = amp_res;
122 
123   switch (amp_res) {
124     case SBR_AMP_RES_3_0:
125       /*envelope data*/
126 
127       /*Level/Pan - coding */
128       sbrEnvData->hufftableLevelTimeC = v_Huff_envelopeLevelC11T;
129       sbrEnvData->hufftableLevelTimeL = v_Huff_envelopeLevelL11T;
130       sbrEnvData->hufftableBalanceTimeC = bookSbrEnvBalanceC11T;
131       sbrEnvData->hufftableBalanceTimeL = bookSbrEnvBalanceL11T;
132 
133       sbrEnvData->hufftableLevelFreqC = v_Huff_envelopeLevelC11F;
134       sbrEnvData->hufftableLevelFreqL = v_Huff_envelopeLevelL11F;
135       sbrEnvData->hufftableBalanceFreqC = bookSbrEnvBalanceC11F;
136       sbrEnvData->hufftableBalanceFreqL = bookSbrEnvBalanceL11F;
137 
138       /*Right/Left - coding */
139       sbrEnvData->hufftableTimeC = v_Huff_envelopeLevelC11T;
140       sbrEnvData->hufftableTimeL = v_Huff_envelopeLevelL11T;
141       sbrEnvData->hufftableFreqC = v_Huff_envelopeLevelC11F;
142       sbrEnvData->hufftableFreqL = v_Huff_envelopeLevelL11F;
143 
144       sbrEnvData->codeBookScfLavBalance = CODE_BOOK_SCF_LAV_BALANCE11;
145       sbrEnvData->codeBookScfLav = CODE_BOOK_SCF_LAV11;
146 
147       sbrEnvData->si_sbr_start_env_bits = SI_SBR_START_ENV_BITS_AMP_RES_3_0;
148       sbrEnvData->si_sbr_start_env_bits_balance =
149           SI_SBR_START_ENV_BITS_BALANCE_AMP_RES_3_0;
150       break;
151 
152     case SBR_AMP_RES_1_5:
153       /*envelope data*/
154 
155       /*Level/Pan - coding */
156       sbrEnvData->hufftableLevelTimeC = v_Huff_envelopeLevelC10T;
157       sbrEnvData->hufftableLevelTimeL = v_Huff_envelopeLevelL10T;
158       sbrEnvData->hufftableBalanceTimeC = bookSbrEnvBalanceC10T;
159       sbrEnvData->hufftableBalanceTimeL = bookSbrEnvBalanceL10T;
160 
161       sbrEnvData->hufftableLevelFreqC = v_Huff_envelopeLevelC10F;
162       sbrEnvData->hufftableLevelFreqL = v_Huff_envelopeLevelL10F;
163       sbrEnvData->hufftableBalanceFreqC = bookSbrEnvBalanceC10F;
164       sbrEnvData->hufftableBalanceFreqL = bookSbrEnvBalanceL10F;
165 
166       /*Right/Left - coding */
167       sbrEnvData->hufftableTimeC = v_Huff_envelopeLevelC10T;
168       sbrEnvData->hufftableTimeL = v_Huff_envelopeLevelL10T;
169       sbrEnvData->hufftableFreqC = v_Huff_envelopeLevelC10F;
170       sbrEnvData->hufftableFreqL = v_Huff_envelopeLevelL10F;
171 
172       sbrEnvData->codeBookScfLavBalance = CODE_BOOK_SCF_LAV_BALANCE10;
173       sbrEnvData->codeBookScfLav = CODE_BOOK_SCF_LAV10;
174 
175       sbrEnvData->si_sbr_start_env_bits = SI_SBR_START_ENV_BITS_AMP_RES_1_5;
176       sbrEnvData->si_sbr_start_env_bits_balance =
177           SI_SBR_START_ENV_BITS_BALANCE_AMP_RES_1_5;
178       break;
179 
180     default:
181       return (1); /* undefined amp_res mode */
182   }
183 
184   /* these are common to both amp_res values */
185   /*Noise data*/
186 
187   /*Level/Pan - coding */
188   sbrEnvData->hufftableNoiseLevelTimeC = v_Huff_NoiseLevelC11T;
189   sbrEnvData->hufftableNoiseLevelTimeL = v_Huff_NoiseLevelL11T;
190   sbrEnvData->hufftableNoiseBalanceTimeC = bookSbrNoiseBalanceC11T;
191   sbrEnvData->hufftableNoiseBalanceTimeL = bookSbrNoiseBalanceL11T;
192 
193   sbrEnvData->hufftableNoiseLevelFreqC = v_Huff_envelopeLevelC11F;
194   sbrEnvData->hufftableNoiseLevelFreqL = v_Huff_envelopeLevelL11F;
195   sbrEnvData->hufftableNoiseBalanceFreqC = bookSbrEnvBalanceC11F;
196   sbrEnvData->hufftableNoiseBalanceFreqL = bookSbrEnvBalanceL11F;
197 
198   /*Right/Left - coding */
199   sbrEnvData->hufftableNoiseTimeC = v_Huff_NoiseLevelC11T;
200   sbrEnvData->hufftableNoiseTimeL = v_Huff_NoiseLevelL11T;
201   sbrEnvData->hufftableNoiseFreqC = v_Huff_envelopeLevelC11F;
202   sbrEnvData->hufftableNoiseFreqL = v_Huff_envelopeLevelL11F;
203 
204   sbrEnvData->si_sbr_start_noise_bits = SI_SBR_START_NOISE_BITS_AMP_RES_3_0;
205   sbrEnvData->si_sbr_start_noise_bits_balance =
206       SI_SBR_START_NOISE_BITS_BALANCE_AMP_RES_3_0;
207 
208   /* init envelope tables and codebooks */
209   henv->codeBookScfLavBalanceTime = sbrEnvData->codeBookScfLavBalance;
210   henv->codeBookScfLavBalanceFreq = sbrEnvData->codeBookScfLavBalance;
211   henv->codeBookScfLavLevelTime = sbrEnvData->codeBookScfLav;
212   henv->codeBookScfLavLevelFreq = sbrEnvData->codeBookScfLav;
213   henv->codeBookScfLavTime = sbrEnvData->codeBookScfLav;
214   henv->codeBookScfLavFreq = sbrEnvData->codeBookScfLav;
215 
216   henv->hufftableLevelTimeL = sbrEnvData->hufftableLevelTimeL;
217   henv->hufftableBalanceTimeL = sbrEnvData->hufftableBalanceTimeL;
218   henv->hufftableTimeL = sbrEnvData->hufftableTimeL;
219   henv->hufftableLevelFreqL = sbrEnvData->hufftableLevelFreqL;
220   henv->hufftableBalanceFreqL = sbrEnvData->hufftableBalanceFreqL;
221   henv->hufftableFreqL = sbrEnvData->hufftableFreqL;
222 
223   henv->codeBookScfLavFreq = sbrEnvData->codeBookScfLav;
224   henv->codeBookScfLavTime = sbrEnvData->codeBookScfLav;
225 
226   henv->start_bits = sbrEnvData->si_sbr_start_env_bits;
227   henv->start_bits_balance = sbrEnvData->si_sbr_start_env_bits_balance;
228 
229   /* init noise tables and codebooks */
230 
231   hnoise->codeBookScfLavBalanceTime = CODE_BOOK_SCF_LAV_BALANCE11;
232   hnoise->codeBookScfLavBalanceFreq = CODE_BOOK_SCF_LAV_BALANCE11;
233   hnoise->codeBookScfLavLevelTime = CODE_BOOK_SCF_LAV11;
234   hnoise->codeBookScfLavLevelFreq = CODE_BOOK_SCF_LAV11;
235   hnoise->codeBookScfLavTime = CODE_BOOK_SCF_LAV11;
236   hnoise->codeBookScfLavFreq = CODE_BOOK_SCF_LAV11;
237 
238   hnoise->hufftableLevelTimeL = sbrEnvData->hufftableNoiseLevelTimeL;
239   hnoise->hufftableBalanceTimeL = sbrEnvData->hufftableNoiseBalanceTimeL;
240   hnoise->hufftableTimeL = sbrEnvData->hufftableNoiseTimeL;
241   hnoise->hufftableLevelFreqL = sbrEnvData->hufftableNoiseLevelFreqL;
242   hnoise->hufftableBalanceFreqL = sbrEnvData->hufftableNoiseBalanceFreqL;
243   hnoise->hufftableFreqL = sbrEnvData->hufftableNoiseFreqL;
244 
245   hnoise->start_bits = sbrEnvData->si_sbr_start_noise_bits;
246   hnoise->start_bits_balance = sbrEnvData->si_sbr_start_noise_bits_balance;
247 
248   /* No delta coding in time from the previous frame due to 1.5dB FIx-FIX rule
249    */
250   henv->upDate = 0;
251   hnoise->upDate = 0;
252   return (0);
253 }
254 
255 /*******************************************************************************
256  Functionname:  indexLow2High
257  *******************************************************************************
258 
259  Description:   Nice small patch-functions in order to cope with non-factor-2
260                 ratios between high-res and low-res
261 
262  Arguments:     INT offset, INT index, FREQ_RES res
263 
264  Return:        INT
265 
266 *******************************************************************************/
indexLow2High(INT offset,INT index,FREQ_RES res)267 static INT indexLow2High(INT offset, INT index, FREQ_RES res) {
268   if (res == FREQ_RES_LOW) {
269     if (offset >= 0) {
270       if (index < offset)
271         return (index);
272       else
273         return (2 * index - offset);
274     } else {
275       offset = -offset;
276       if (index < offset)
277         return (2 * index + index);
278       else
279         return (2 * index + offset);
280     }
281   } else
282     return (index);
283 }
284 
285 /*******************************************************************************
286  Functionname:  mapLowResEnergyVal
287  *******************************************************************************
288 
289  Description:
290 
291  Arguments:     INT currVal,INT* prevData, INT offset, INT index, FREQ_RES res
292 
293  Return:        none
294 
295 *******************************************************************************/
mapLowResEnergyVal(SCHAR currVal,SCHAR * prevData,INT offset,INT index,FREQ_RES res)296 static void mapLowResEnergyVal(SCHAR currVal, SCHAR *prevData, INT offset,
297                                INT index, FREQ_RES res) {
298   if (res == FREQ_RES_LOW) {
299     if (offset >= 0) {
300       if (index < offset)
301         prevData[index] = currVal;
302       else {
303         prevData[2 * index - offset] = currVal;
304         prevData[2 * index + 1 - offset] = currVal;
305       }
306     } else {
307       offset = -offset;
308       if (index < offset) {
309         prevData[3 * index] = currVal;
310         prevData[3 * index + 1] = currVal;
311         prevData[3 * index + 2] = currVal;
312       } else {
313         prevData[2 * index + offset] = currVal;
314         prevData[2 * index + 1 + offset] = currVal;
315       }
316     }
317   } else
318     prevData[index] = currVal;
319 }
320 
321 /*******************************************************************************
322  Functionname:  computeBits
323  *******************************************************************************
324 
325  Description:
326 
327  Arguments:     INT delta,
328                 INT codeBookScfLavLevel,
329                 INT codeBookScfLavBalance,
330                 const UCHAR * hufftableLevel,
331                 const UCHAR * hufftableBalance, INT coupling, INT channel)
332 
333  Return:        INT
334 
335 *******************************************************************************/
computeBits(SCHAR * delta,INT codeBookScfLavLevel,INT codeBookScfLavBalance,const UCHAR * hufftableLevel,const UCHAR * hufftableBalance,INT coupling,INT channel)336 static INT computeBits(SCHAR *delta, INT codeBookScfLavLevel,
337                        INT codeBookScfLavBalance, const UCHAR *hufftableLevel,
338                        const UCHAR *hufftableBalance, INT coupling,
339                        INT channel) {
340   INT index;
341   INT delta_bits = 0;
342 
343   if (coupling) {
344     if (channel == 1) {
345       if (*delta < 0)
346         index = fixMax(*delta, -codeBookScfLavBalance);
347       else
348         index = fixMin(*delta, codeBookScfLavBalance);
349 
350       if (index != *delta) {
351         *delta = index;
352         return (10000);
353       }
354 
355       delta_bits = hufftableBalance[index + codeBookScfLavBalance];
356     } else {
357       if (*delta < 0)
358         index = fixMax(*delta, -codeBookScfLavLevel);
359       else
360         index = fixMin(*delta, codeBookScfLavLevel);
361 
362       if (index != *delta) {
363         *delta = index;
364         return (10000);
365       }
366       delta_bits = hufftableLevel[index + codeBookScfLavLevel];
367     }
368   } else {
369     if (*delta < 0)
370       index = fixMax(*delta, -codeBookScfLavLevel);
371     else
372       index = fixMin(*delta, codeBookScfLavLevel);
373 
374     if (index != *delta) {
375       *delta = index;
376       return (10000);
377     }
378     delta_bits = hufftableLevel[index + codeBookScfLavLevel];
379   }
380 
381   return (delta_bits);
382 }
383 
384 /*******************************************************************************
385  Functionname:  FDKsbrEnc_codeEnvelope
386  *******************************************************************************
387 
388  Description:
389 
390  Arguments:     INT *sfb_nrg,
391                 const FREQ_RES *freq_res,
392                 SBR_CODE_ENVELOPE * h_sbrCodeEnvelope,
393                 INT *directionVec, INT scalable, INT nEnvelopes, INT channel,
394                 INT headerActive)
395 
396  Return:        none
397                 h_sbrCodeEnvelope->sfb_nrg_prev is modified !
398                 sfb_nrg is modified
399                 h_sbrCodeEnvelope->update is modfied !
400                 *directionVec is modified
401 
402 *******************************************************************************/
FDKsbrEnc_codeEnvelope(SCHAR * sfb_nrg,const FREQ_RES * freq_res,SBR_CODE_ENVELOPE * h_sbrCodeEnvelope,INT * directionVec,INT coupling,INT nEnvelopes,INT channel,INT headerActive)403 void FDKsbrEnc_codeEnvelope(SCHAR *sfb_nrg, const FREQ_RES *freq_res,
404                             SBR_CODE_ENVELOPE *h_sbrCodeEnvelope,
405                             INT *directionVec, INT coupling, INT nEnvelopes,
406                             INT channel, INT headerActive) {
407   INT i, no_of_bands, band;
408   FIXP_DBL tmp1, tmp2, tmp3, dF_edge_1stEnv;
409   SCHAR *ptr_nrg;
410 
411   INT codeBookScfLavLevelTime;
412   INT codeBookScfLavLevelFreq;
413   INT codeBookScfLavBalanceTime;
414   INT codeBookScfLavBalanceFreq;
415   const UCHAR *hufftableLevelTimeL;
416   const UCHAR *hufftableBalanceTimeL;
417   const UCHAR *hufftableLevelFreqL;
418   const UCHAR *hufftableBalanceFreqL;
419 
420   INT offset = h_sbrCodeEnvelope->offset;
421   INT envDataTableCompFactor;
422 
423   INT delta_F_bits = 0, delta_T_bits = 0;
424   INT use_dT;
425 
426   SCHAR delta_F[MAX_FREQ_COEFFS];
427   SCHAR delta_T[MAX_FREQ_COEFFS];
428   SCHAR last_nrg, curr_nrg;
429 
430   tmp1 = FL2FXCONST_DBL(0.5f) >> (DFRACT_BITS - 16 - 1);
431   tmp2 = h_sbrCodeEnvelope->dF_edge_1stEnv >> (DFRACT_BITS - 16);
432   tmp3 = (FIXP_DBL)fMult(h_sbrCodeEnvelope->dF_edge_incr,
433                          ((FIXP_DBL)h_sbrCodeEnvelope->dF_edge_incr_fac) << 15);
434 
435   dF_edge_1stEnv = tmp1 + tmp2 + tmp3;
436 
437   if (coupling) {
438     codeBookScfLavLevelTime = h_sbrCodeEnvelope->codeBookScfLavLevelTime;
439     codeBookScfLavLevelFreq = h_sbrCodeEnvelope->codeBookScfLavLevelFreq;
440     codeBookScfLavBalanceTime = h_sbrCodeEnvelope->codeBookScfLavBalanceTime;
441     codeBookScfLavBalanceFreq = h_sbrCodeEnvelope->codeBookScfLavBalanceFreq;
442     hufftableLevelTimeL = h_sbrCodeEnvelope->hufftableLevelTimeL;
443     hufftableBalanceTimeL = h_sbrCodeEnvelope->hufftableBalanceTimeL;
444     hufftableLevelFreqL = h_sbrCodeEnvelope->hufftableLevelFreqL;
445     hufftableBalanceFreqL = h_sbrCodeEnvelope->hufftableBalanceFreqL;
446   } else {
447     codeBookScfLavLevelTime = h_sbrCodeEnvelope->codeBookScfLavTime;
448     codeBookScfLavLevelFreq = h_sbrCodeEnvelope->codeBookScfLavFreq;
449     codeBookScfLavBalanceTime = h_sbrCodeEnvelope->codeBookScfLavTime;
450     codeBookScfLavBalanceFreq = h_sbrCodeEnvelope->codeBookScfLavFreq;
451     hufftableLevelTimeL = h_sbrCodeEnvelope->hufftableTimeL;
452     hufftableBalanceTimeL = h_sbrCodeEnvelope->hufftableTimeL;
453     hufftableLevelFreqL = h_sbrCodeEnvelope->hufftableFreqL;
454     hufftableBalanceFreqL = h_sbrCodeEnvelope->hufftableFreqL;
455   }
456 
457   if (coupling == 1 && channel == 1)
458     envDataTableCompFactor =
459         1; /*should be one when the new huffman-tables are ready*/
460   else
461     envDataTableCompFactor = 0;
462 
463   if (h_sbrCodeEnvelope->deltaTAcrossFrames == 0) h_sbrCodeEnvelope->upDate = 0;
464 
465   /* no delta coding in time in case of a header */
466   if (headerActive) h_sbrCodeEnvelope->upDate = 0;
467 
468   for (i = 0; i < nEnvelopes; i++) {
469     if (freq_res[i] == FREQ_RES_HIGH)
470       no_of_bands = h_sbrCodeEnvelope->nSfb[FREQ_RES_HIGH];
471     else
472       no_of_bands = h_sbrCodeEnvelope->nSfb[FREQ_RES_LOW];
473 
474     ptr_nrg = sfb_nrg;
475     curr_nrg = *ptr_nrg;
476 
477     delta_F[0] = curr_nrg >> envDataTableCompFactor;
478 
479     if (coupling && channel == 1)
480       delta_F_bits = h_sbrCodeEnvelope->start_bits_balance;
481     else
482       delta_F_bits = h_sbrCodeEnvelope->start_bits;
483 
484     if (h_sbrCodeEnvelope->upDate != 0) {
485       delta_T[0] = (curr_nrg - h_sbrCodeEnvelope->sfb_nrg_prev[0]) >>
486                    envDataTableCompFactor;
487 
488       delta_T_bits = computeBits(&delta_T[0], codeBookScfLavLevelTime,
489                                  codeBookScfLavBalanceTime, hufftableLevelTimeL,
490                                  hufftableBalanceTimeL, coupling, channel);
491     }
492 
493     mapLowResEnergyVal(curr_nrg, h_sbrCodeEnvelope->sfb_nrg_prev, offset, 0,
494                        freq_res[i]);
495 
496     /* ensure that nrg difference is not higher than codeBookScfLavXXXFreq */
497     if (coupling && channel == 1) {
498       for (band = no_of_bands - 1; band > 0; band--) {
499         if (ptr_nrg[band] - ptr_nrg[band - 1] > codeBookScfLavBalanceFreq) {
500           ptr_nrg[band - 1] = ptr_nrg[band] - codeBookScfLavBalanceFreq;
501         }
502       }
503       for (band = 1; band < no_of_bands; band++) {
504         if (ptr_nrg[band - 1] - ptr_nrg[band] > codeBookScfLavBalanceFreq) {
505           ptr_nrg[band] = ptr_nrg[band - 1] - codeBookScfLavBalanceFreq;
506         }
507       }
508     } else {
509       for (band = no_of_bands - 1; band > 0; band--) {
510         if (ptr_nrg[band] - ptr_nrg[band - 1] > codeBookScfLavLevelFreq) {
511           ptr_nrg[band - 1] = ptr_nrg[band] - codeBookScfLavLevelFreq;
512         }
513       }
514       for (band = 1; band < no_of_bands; band++) {
515         if (ptr_nrg[band - 1] - ptr_nrg[band] > codeBookScfLavLevelFreq) {
516           ptr_nrg[band] = ptr_nrg[band - 1] - codeBookScfLavLevelFreq;
517         }
518       }
519     }
520 
521     /* Coding loop*/
522     for (band = 1; band < no_of_bands; band++) {
523       last_nrg = (*ptr_nrg);
524       ptr_nrg++;
525       curr_nrg = (*ptr_nrg);
526 
527       delta_F[band] = (curr_nrg - last_nrg) >> envDataTableCompFactor;
528 
529       delta_F_bits += computeBits(
530           &delta_F[band], codeBookScfLavLevelFreq, codeBookScfLavBalanceFreq,
531           hufftableLevelFreqL, hufftableBalanceFreqL, coupling, channel);
532 
533       if (h_sbrCodeEnvelope->upDate != 0) {
534         delta_T[band] =
535             curr_nrg -
536             h_sbrCodeEnvelope
537                 ->sfb_nrg_prev[indexLow2High(offset, band, freq_res[i])];
538         delta_T[band] = delta_T[band] >> envDataTableCompFactor;
539       }
540 
541       mapLowResEnergyVal(curr_nrg, h_sbrCodeEnvelope->sfb_nrg_prev, offset,
542                          band, freq_res[i]);
543 
544       if (h_sbrCodeEnvelope->upDate != 0) {
545         delta_T_bits += computeBits(
546             &delta_T[band], codeBookScfLavLevelTime, codeBookScfLavBalanceTime,
547             hufftableLevelTimeL, hufftableBalanceTimeL, coupling, channel);
548       }
549     }
550 
551     /* Replace sfb_nrg with deltacoded samples and set flag */
552     if (i == 0) {
553       INT tmp_bits;
554       tmp_bits = (((delta_T_bits * dF_edge_1stEnv) >> (DFRACT_BITS - 18)) +
555                   (FIXP_DBL)1) >>
556                  1;
557       use_dT = (h_sbrCodeEnvelope->upDate != 0 && (delta_F_bits > tmp_bits));
558     } else
559       use_dT = (delta_T_bits < delta_F_bits && h_sbrCodeEnvelope->upDate != 0);
560 
561     if (use_dT) {
562       directionVec[i] = TIME;
563       FDKmemcpy(sfb_nrg, delta_T, no_of_bands * sizeof(SCHAR));
564     } else {
565       h_sbrCodeEnvelope->upDate = 0;
566       directionVec[i] = FREQ;
567       FDKmemcpy(sfb_nrg, delta_F, no_of_bands * sizeof(SCHAR));
568     }
569     sfb_nrg += no_of_bands;
570     h_sbrCodeEnvelope->upDate = 1;
571   }
572 }
573 
574 /*******************************************************************************
575  Functionname:  FDKsbrEnc_InitSbrCodeEnvelope
576  *******************************************************************************
577 
578  Description:
579 
580  Arguments:
581 
582  Return:
583 
584 *******************************************************************************/
FDKsbrEnc_InitSbrCodeEnvelope(HANDLE_SBR_CODE_ENVELOPE h_sbrCodeEnvelope,INT * nSfb,INT deltaTAcrossFrames,FIXP_DBL dF_edge_1stEnv,FIXP_DBL dF_edge_incr)585 INT FDKsbrEnc_InitSbrCodeEnvelope(HANDLE_SBR_CODE_ENVELOPE h_sbrCodeEnvelope,
586                                   INT *nSfb, INT deltaTAcrossFrames,
587                                   FIXP_DBL dF_edge_1stEnv,
588                                   FIXP_DBL dF_edge_incr) {
589   FDKmemclear(h_sbrCodeEnvelope, sizeof(SBR_CODE_ENVELOPE));
590 
591   h_sbrCodeEnvelope->deltaTAcrossFrames = deltaTAcrossFrames;
592   h_sbrCodeEnvelope->dF_edge_1stEnv = dF_edge_1stEnv;
593   h_sbrCodeEnvelope->dF_edge_incr = dF_edge_incr;
594   h_sbrCodeEnvelope->dF_edge_incr_fac = 0;
595   h_sbrCodeEnvelope->upDate = 0;
596   h_sbrCodeEnvelope->nSfb[FREQ_RES_LOW] = nSfb[FREQ_RES_LOW];
597   h_sbrCodeEnvelope->nSfb[FREQ_RES_HIGH] = nSfb[FREQ_RES_HIGH];
598   h_sbrCodeEnvelope->offset = 2 * h_sbrCodeEnvelope->nSfb[FREQ_RES_LOW] -
599                               h_sbrCodeEnvelope->nSfb[FREQ_RES_HIGH];
600 
601   return (0);
602 }
603