Coverage Report

Created: 2025-07-01 06:21

/src/aac/libSBRenc/src/code_env.cpp
Line
Count
Source (jump to first uncovered line)
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
*****************************************************************************/
115
INT FDKsbrEnc_InitSbrHuffmanTables(HANDLE_SBR_ENV_DATA sbrEnvData,
116
                                   HANDLE_SBR_CODE_ENVELOPE henv,
117
                                   HANDLE_SBR_CODE_ENVELOPE hnoise,
118
0
                                   AMP_RES amp_res) {
119
0
  if ((!henv) || (!hnoise) || (!sbrEnvData)) return (1); /* not init. */
120
121
0
  sbrEnvData->init_sbr_amp_res = amp_res;
122
123
0
  switch (amp_res) {
124
0
    case SBR_AMP_RES_3_0:
125
      /*envelope data*/
126
127
      /*Level/Pan - coding */
128
0
      sbrEnvData->hufftableLevelTimeC = v_Huff_envelopeLevelC11T;
129
0
      sbrEnvData->hufftableLevelTimeL = v_Huff_envelopeLevelL11T;
130
0
      sbrEnvData->hufftableBalanceTimeC = bookSbrEnvBalanceC11T;
131
0
      sbrEnvData->hufftableBalanceTimeL = bookSbrEnvBalanceL11T;
132
133
0
      sbrEnvData->hufftableLevelFreqC = v_Huff_envelopeLevelC11F;
134
0
      sbrEnvData->hufftableLevelFreqL = v_Huff_envelopeLevelL11F;
135
0
      sbrEnvData->hufftableBalanceFreqC = bookSbrEnvBalanceC11F;
136
0
      sbrEnvData->hufftableBalanceFreqL = bookSbrEnvBalanceL11F;
137
138
      /*Right/Left - coding */
139
0
      sbrEnvData->hufftableTimeC = v_Huff_envelopeLevelC11T;
140
0
      sbrEnvData->hufftableTimeL = v_Huff_envelopeLevelL11T;
141
0
      sbrEnvData->hufftableFreqC = v_Huff_envelopeLevelC11F;
142
0
      sbrEnvData->hufftableFreqL = v_Huff_envelopeLevelL11F;
143
144
0
      sbrEnvData->codeBookScfLavBalance = CODE_BOOK_SCF_LAV_BALANCE11;
145
0
      sbrEnvData->codeBookScfLav = CODE_BOOK_SCF_LAV11;
146
147
0
      sbrEnvData->si_sbr_start_env_bits = SI_SBR_START_ENV_BITS_AMP_RES_3_0;
148
0
      sbrEnvData->si_sbr_start_env_bits_balance =
149
0
          SI_SBR_START_ENV_BITS_BALANCE_AMP_RES_3_0;
150
0
      break;
151
152
0
    case SBR_AMP_RES_1_5:
153
      /*envelope data*/
154
155
      /*Level/Pan - coding */
156
0
      sbrEnvData->hufftableLevelTimeC = v_Huff_envelopeLevelC10T;
157
0
      sbrEnvData->hufftableLevelTimeL = v_Huff_envelopeLevelL10T;
158
0
      sbrEnvData->hufftableBalanceTimeC = bookSbrEnvBalanceC10T;
159
0
      sbrEnvData->hufftableBalanceTimeL = bookSbrEnvBalanceL10T;
160
161
0
      sbrEnvData->hufftableLevelFreqC = v_Huff_envelopeLevelC10F;
162
0
      sbrEnvData->hufftableLevelFreqL = v_Huff_envelopeLevelL10F;
163
0
      sbrEnvData->hufftableBalanceFreqC = bookSbrEnvBalanceC10F;
164
0
      sbrEnvData->hufftableBalanceFreqL = bookSbrEnvBalanceL10F;
165
166
      /*Right/Left - coding */
167
0
      sbrEnvData->hufftableTimeC = v_Huff_envelopeLevelC10T;
168
0
      sbrEnvData->hufftableTimeL = v_Huff_envelopeLevelL10T;
169
0
      sbrEnvData->hufftableFreqC = v_Huff_envelopeLevelC10F;
170
0
      sbrEnvData->hufftableFreqL = v_Huff_envelopeLevelL10F;
171
172
0
      sbrEnvData->codeBookScfLavBalance = CODE_BOOK_SCF_LAV_BALANCE10;
173
0
      sbrEnvData->codeBookScfLav = CODE_BOOK_SCF_LAV10;
174
175
0
      sbrEnvData->si_sbr_start_env_bits = SI_SBR_START_ENV_BITS_AMP_RES_1_5;
176
0
      sbrEnvData->si_sbr_start_env_bits_balance =
177
0
          SI_SBR_START_ENV_BITS_BALANCE_AMP_RES_1_5;
178
0
      break;
179
180
0
    default:
181
0
      return (1); /* undefined amp_res mode */
182
0
  }
183
184
  /* these are common to both amp_res values */
185
  /*Noise data*/
186
187
  /*Level/Pan - coding */
188
0
  sbrEnvData->hufftableNoiseLevelTimeC = v_Huff_NoiseLevelC11T;
189
0
  sbrEnvData->hufftableNoiseLevelTimeL = v_Huff_NoiseLevelL11T;
190
0
  sbrEnvData->hufftableNoiseBalanceTimeC = bookSbrNoiseBalanceC11T;
191
0
  sbrEnvData->hufftableNoiseBalanceTimeL = bookSbrNoiseBalanceL11T;
192
193
0
  sbrEnvData->hufftableNoiseLevelFreqC = v_Huff_envelopeLevelC11F;
194
0
  sbrEnvData->hufftableNoiseLevelFreqL = v_Huff_envelopeLevelL11F;
195
0
  sbrEnvData->hufftableNoiseBalanceFreqC = bookSbrEnvBalanceC11F;
196
0
  sbrEnvData->hufftableNoiseBalanceFreqL = bookSbrEnvBalanceL11F;
197
198
  /*Right/Left - coding */
199
0
  sbrEnvData->hufftableNoiseTimeC = v_Huff_NoiseLevelC11T;
200
0
  sbrEnvData->hufftableNoiseTimeL = v_Huff_NoiseLevelL11T;
201
0
  sbrEnvData->hufftableNoiseFreqC = v_Huff_envelopeLevelC11F;
202
0
  sbrEnvData->hufftableNoiseFreqL = v_Huff_envelopeLevelL11F;
203
204
0
  sbrEnvData->si_sbr_start_noise_bits = SI_SBR_START_NOISE_BITS_AMP_RES_3_0;
205
0
  sbrEnvData->si_sbr_start_noise_bits_balance =
206
0
      SI_SBR_START_NOISE_BITS_BALANCE_AMP_RES_3_0;
207
208
  /* init envelope tables and codebooks */
209
0
  henv->codeBookScfLavBalanceTime = sbrEnvData->codeBookScfLavBalance;
210
0
  henv->codeBookScfLavBalanceFreq = sbrEnvData->codeBookScfLavBalance;
211
0
  henv->codeBookScfLavLevelTime = sbrEnvData->codeBookScfLav;
212
0
  henv->codeBookScfLavLevelFreq = sbrEnvData->codeBookScfLav;
213
0
  henv->codeBookScfLavTime = sbrEnvData->codeBookScfLav;
214
0
  henv->codeBookScfLavFreq = sbrEnvData->codeBookScfLav;
215
216
0
  henv->hufftableLevelTimeL = sbrEnvData->hufftableLevelTimeL;
217
0
  henv->hufftableBalanceTimeL = sbrEnvData->hufftableBalanceTimeL;
218
0
  henv->hufftableTimeL = sbrEnvData->hufftableTimeL;
219
0
  henv->hufftableLevelFreqL = sbrEnvData->hufftableLevelFreqL;
220
0
  henv->hufftableBalanceFreqL = sbrEnvData->hufftableBalanceFreqL;
221
0
  henv->hufftableFreqL = sbrEnvData->hufftableFreqL;
222
223
0
  henv->codeBookScfLavFreq = sbrEnvData->codeBookScfLav;
224
0
  henv->codeBookScfLavTime = sbrEnvData->codeBookScfLav;
225
226
0
  henv->start_bits = sbrEnvData->si_sbr_start_env_bits;
227
0
  henv->start_bits_balance = sbrEnvData->si_sbr_start_env_bits_balance;
228
229
  /* init noise tables and codebooks */
230
231
0
  hnoise->codeBookScfLavBalanceTime = CODE_BOOK_SCF_LAV_BALANCE11;
232
0
  hnoise->codeBookScfLavBalanceFreq = CODE_BOOK_SCF_LAV_BALANCE11;
233
0
  hnoise->codeBookScfLavLevelTime = CODE_BOOK_SCF_LAV11;
234
0
  hnoise->codeBookScfLavLevelFreq = CODE_BOOK_SCF_LAV11;
235
0
  hnoise->codeBookScfLavTime = CODE_BOOK_SCF_LAV11;
236
0
  hnoise->codeBookScfLavFreq = CODE_BOOK_SCF_LAV11;
237
238
0
  hnoise->hufftableLevelTimeL = sbrEnvData->hufftableNoiseLevelTimeL;
239
0
  hnoise->hufftableBalanceTimeL = sbrEnvData->hufftableNoiseBalanceTimeL;
240
0
  hnoise->hufftableTimeL = sbrEnvData->hufftableNoiseTimeL;
241
0
  hnoise->hufftableLevelFreqL = sbrEnvData->hufftableNoiseLevelFreqL;
242
0
  hnoise->hufftableBalanceFreqL = sbrEnvData->hufftableNoiseBalanceFreqL;
243
0
  hnoise->hufftableFreqL = sbrEnvData->hufftableNoiseFreqL;
244
245
0
  hnoise->start_bits = sbrEnvData->si_sbr_start_noise_bits;
246
0
  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
0
  henv->upDate = 0;
251
0
  hnoise->upDate = 0;
252
0
  return (0);
253
0
}
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
*******************************************************************************/
267
0
static INT indexLow2High(INT offset, INT index, FREQ_RES res) {
268
0
  if (res == FREQ_RES_LOW) {
269
0
    if (offset >= 0) {
270
0
      if (index < offset)
271
0
        return (index);
272
0
      else
273
0
        return (2 * index - offset);
274
0
    } else {
275
0
      offset = -offset;
276
0
      if (index < offset)
277
0
        return (2 * index + index);
278
0
      else
279
0
        return (2 * index + offset);
280
0
    }
281
0
  } else
282
0
    return (index);
283
0
}
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
*******************************************************************************/
296
static void mapLowResEnergyVal(SCHAR currVal, SCHAR *prevData, INT offset,
297
0
                               INT index, FREQ_RES res) {
298
0
  if (res == FREQ_RES_LOW) {
299
0
    if (offset >= 0) {
300
0
      if (index < offset)
301
0
        prevData[index] = currVal;
302
0
      else {
303
0
        prevData[2 * index - offset] = currVal;
304
0
        prevData[2 * index + 1 - offset] = currVal;
305
0
      }
306
0
    } else {
307
0
      offset = -offset;
308
0
      if (index < offset) {
309
0
        prevData[3 * index] = currVal;
310
0
        prevData[3 * index + 1] = currVal;
311
0
        prevData[3 * index + 2] = currVal;
312
0
      } else {
313
0
        prevData[2 * index + offset] = currVal;
314
0
        prevData[2 * index + 1 + offset] = currVal;
315
0
      }
316
0
    }
317
0
  } else
318
0
    prevData[index] = currVal;
319
0
}
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
*******************************************************************************/
336
static INT computeBits(SCHAR *delta, INT codeBookScfLavLevel,
337
                       INT codeBookScfLavBalance, const UCHAR *hufftableLevel,
338
                       const UCHAR *hufftableBalance, INT coupling,
339
0
                       INT channel) {
340
0
  INT index;
341
0
  INT delta_bits = 0;
342
343
0
  if (coupling) {
344
0
    if (channel == 1) {
345
0
      if (*delta < 0)
346
0
        index = fixMax(*delta, -codeBookScfLavBalance);
347
0
      else
348
0
        index = fixMin(*delta, codeBookScfLavBalance);
349
350
0
      if (index != *delta) {
351
0
        *delta = index;
352
0
        return (10000);
353
0
      }
354
355
0
      delta_bits = hufftableBalance[index + codeBookScfLavBalance];
356
0
    } else {
357
0
      if (*delta < 0)
358
0
        index = fixMax(*delta, -codeBookScfLavLevel);
359
0
      else
360
0
        index = fixMin(*delta, codeBookScfLavLevel);
361
362
0
      if (index != *delta) {
363
0
        *delta = index;
364
0
        return (10000);
365
0
      }
366
0
      delta_bits = hufftableLevel[index + codeBookScfLavLevel];
367
0
    }
368
0
  } else {
369
0
    if (*delta < 0)
370
0
      index = fixMax(*delta, -codeBookScfLavLevel);
371
0
    else
372
0
      index = fixMin(*delta, codeBookScfLavLevel);
373
374
0
    if (index != *delta) {
375
0
      *delta = index;
376
0
      return (10000);
377
0
    }
378
0
    delta_bits = hufftableLevel[index + codeBookScfLavLevel];
379
0
  }
380
381
0
  return (delta_bits);
382
0
}
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
*******************************************************************************/
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
0
                            INT channel, INT headerActive) {
407
0
  INT i, no_of_bands, band;
408
0
  FIXP_DBL tmp1, tmp2, tmp3, dF_edge_1stEnv;
409
0
  SCHAR *ptr_nrg;
410
411
0
  INT codeBookScfLavLevelTime;
412
0
  INT codeBookScfLavLevelFreq;
413
0
  INT codeBookScfLavBalanceTime;
414
0
  INT codeBookScfLavBalanceFreq;
415
0
  const UCHAR *hufftableLevelTimeL;
416
0
  const UCHAR *hufftableBalanceTimeL;
417
0
  const UCHAR *hufftableLevelFreqL;
418
0
  const UCHAR *hufftableBalanceFreqL;
419
420
0
  INT offset = h_sbrCodeEnvelope->offset;
421
0
  INT envDataTableCompFactor;
422
423
0
  INT delta_F_bits = 0, delta_T_bits = 0;
424
0
  INT use_dT;
425
426
0
  SCHAR delta_F[MAX_FREQ_COEFFS];
427
0
  SCHAR delta_T[MAX_FREQ_COEFFS];
428
0
  SCHAR last_nrg, curr_nrg;
429
430
0
  tmp1 = FL2FXCONST_DBL(0.5f) >> (DFRACT_BITS - 16 - 1);
431
0
  tmp2 = h_sbrCodeEnvelope->dF_edge_1stEnv >> (DFRACT_BITS - 16);
432
0
  tmp3 = (FIXP_DBL)fMult(h_sbrCodeEnvelope->dF_edge_incr,
433
0
                         ((FIXP_DBL)h_sbrCodeEnvelope->dF_edge_incr_fac) << 15);
434
435
0
  dF_edge_1stEnv = tmp1 + tmp2 + tmp3;
436
437
0
  if (coupling) {
438
0
    codeBookScfLavLevelTime = h_sbrCodeEnvelope->codeBookScfLavLevelTime;
439
0
    codeBookScfLavLevelFreq = h_sbrCodeEnvelope->codeBookScfLavLevelFreq;
440
0
    codeBookScfLavBalanceTime = h_sbrCodeEnvelope->codeBookScfLavBalanceTime;
441
0
    codeBookScfLavBalanceFreq = h_sbrCodeEnvelope->codeBookScfLavBalanceFreq;
442
0
    hufftableLevelTimeL = h_sbrCodeEnvelope->hufftableLevelTimeL;
443
0
    hufftableBalanceTimeL = h_sbrCodeEnvelope->hufftableBalanceTimeL;
444
0
    hufftableLevelFreqL = h_sbrCodeEnvelope->hufftableLevelFreqL;
445
0
    hufftableBalanceFreqL = h_sbrCodeEnvelope->hufftableBalanceFreqL;
446
0
  } else {
447
0
    codeBookScfLavLevelTime = h_sbrCodeEnvelope->codeBookScfLavTime;
448
0
    codeBookScfLavLevelFreq = h_sbrCodeEnvelope->codeBookScfLavFreq;
449
0
    codeBookScfLavBalanceTime = h_sbrCodeEnvelope->codeBookScfLavTime;
450
0
    codeBookScfLavBalanceFreq = h_sbrCodeEnvelope->codeBookScfLavFreq;
451
0
    hufftableLevelTimeL = h_sbrCodeEnvelope->hufftableTimeL;
452
0
    hufftableBalanceTimeL = h_sbrCodeEnvelope->hufftableTimeL;
453
0
    hufftableLevelFreqL = h_sbrCodeEnvelope->hufftableFreqL;
454
0
    hufftableBalanceFreqL = h_sbrCodeEnvelope->hufftableFreqL;
455
0
  }
456
457
0
  if (coupling == 1 && channel == 1)
458
0
    envDataTableCompFactor =
459
0
        1; /*should be one when the new huffman-tables are ready*/
460
0
  else
461
0
    envDataTableCompFactor = 0;
462
463
0
  if (h_sbrCodeEnvelope->deltaTAcrossFrames == 0) h_sbrCodeEnvelope->upDate = 0;
464
465
  /* no delta coding in time in case of a header */
466
0
  if (headerActive) h_sbrCodeEnvelope->upDate = 0;
467
468
0
  for (i = 0; i < nEnvelopes; i++) {
469
0
    if (freq_res[i] == FREQ_RES_HIGH)
470
0
      no_of_bands = h_sbrCodeEnvelope->nSfb[FREQ_RES_HIGH];
471
0
    else
472
0
      no_of_bands = h_sbrCodeEnvelope->nSfb[FREQ_RES_LOW];
473
474
0
    ptr_nrg = sfb_nrg;
475
0
    curr_nrg = *ptr_nrg;
476
477
0
    delta_F[0] = curr_nrg >> envDataTableCompFactor;
478
479
0
    if (coupling && channel == 1)
480
0
      delta_F_bits = h_sbrCodeEnvelope->start_bits_balance;
481
0
    else
482
0
      delta_F_bits = h_sbrCodeEnvelope->start_bits;
483
484
0
    if (h_sbrCodeEnvelope->upDate != 0) {
485
0
      delta_T[0] = (curr_nrg - h_sbrCodeEnvelope->sfb_nrg_prev[0]) >>
486
0
                   envDataTableCompFactor;
487
488
0
      delta_T_bits = computeBits(&delta_T[0], codeBookScfLavLevelTime,
489
0
                                 codeBookScfLavBalanceTime, hufftableLevelTimeL,
490
0
                                 hufftableBalanceTimeL, coupling, channel);
491
0
    }
492
493
0
    mapLowResEnergyVal(curr_nrg, h_sbrCodeEnvelope->sfb_nrg_prev, offset, 0,
494
0
                       freq_res[i]);
495
496
    /* ensure that nrg difference is not higher than codeBookScfLavXXXFreq */
497
0
    if (coupling && channel == 1) {
498
0
      for (band = no_of_bands - 1; band > 0; band--) {
499
0
        if (ptr_nrg[band] - ptr_nrg[band - 1] > codeBookScfLavBalanceFreq) {
500
0
          ptr_nrg[band - 1] = ptr_nrg[band] - codeBookScfLavBalanceFreq;
501
0
        }
502
0
      }
503
0
      for (band = 1; band < no_of_bands; band++) {
504
0
        if (ptr_nrg[band - 1] - ptr_nrg[band] > codeBookScfLavBalanceFreq) {
505
0
          ptr_nrg[band] = ptr_nrg[band - 1] - codeBookScfLavBalanceFreq;
506
0
        }
507
0
      }
508
0
    } else {
509
0
      for (band = no_of_bands - 1; band > 0; band--) {
510
0
        if (ptr_nrg[band] - ptr_nrg[band - 1] > codeBookScfLavLevelFreq) {
511
0
          ptr_nrg[band - 1] = ptr_nrg[band] - codeBookScfLavLevelFreq;
512
0
        }
513
0
      }
514
0
      for (band = 1; band < no_of_bands; band++) {
515
0
        if (ptr_nrg[band - 1] - ptr_nrg[band] > codeBookScfLavLevelFreq) {
516
0
          ptr_nrg[band] = ptr_nrg[band - 1] - codeBookScfLavLevelFreq;
517
0
        }
518
0
      }
519
0
    }
520
521
    /* Coding loop*/
522
0
    for (band = 1; band < no_of_bands; band++) {
523
0
      last_nrg = (*ptr_nrg);
524
0
      ptr_nrg++;
525
0
      curr_nrg = (*ptr_nrg);
526
527
0
      delta_F[band] = (curr_nrg - last_nrg) >> envDataTableCompFactor;
528
529
0
      delta_F_bits += computeBits(
530
0
          &delta_F[band], codeBookScfLavLevelFreq, codeBookScfLavBalanceFreq,
531
0
          hufftableLevelFreqL, hufftableBalanceFreqL, coupling, channel);
532
533
0
      if (h_sbrCodeEnvelope->upDate != 0) {
534
0
        delta_T[band] =
535
0
            curr_nrg -
536
0
            h_sbrCodeEnvelope
537
0
                ->sfb_nrg_prev[indexLow2High(offset, band, freq_res[i])];
538
0
        delta_T[band] = delta_T[band] >> envDataTableCompFactor;
539
0
      }
540
541
0
      mapLowResEnergyVal(curr_nrg, h_sbrCodeEnvelope->sfb_nrg_prev, offset,
542
0
                         band, freq_res[i]);
543
544
0
      if (h_sbrCodeEnvelope->upDate != 0) {
545
0
        delta_T_bits += computeBits(
546
0
            &delta_T[band], codeBookScfLavLevelTime, codeBookScfLavBalanceTime,
547
0
            hufftableLevelTimeL, hufftableBalanceTimeL, coupling, channel);
548
0
      }
549
0
    }
550
551
    /* Replace sfb_nrg with deltacoded samples and set flag */
552
0
    if (i == 0) {
553
0
      INT tmp_bits;
554
0
      tmp_bits = (((delta_T_bits * dF_edge_1stEnv) >> (DFRACT_BITS - 18)) +
555
0
                  (FIXP_DBL)1) >>
556
0
                 1;
557
0
      use_dT = (h_sbrCodeEnvelope->upDate != 0 && (delta_F_bits > tmp_bits));
558
0
    } else
559
0
      use_dT = (delta_T_bits < delta_F_bits && h_sbrCodeEnvelope->upDate != 0);
560
561
0
    if (use_dT) {
562
0
      directionVec[i] = TIME;
563
0
      FDKmemcpy(sfb_nrg, delta_T, no_of_bands * sizeof(SCHAR));
564
0
    } else {
565
0
      h_sbrCodeEnvelope->upDate = 0;
566
0
      directionVec[i] = FREQ;
567
0
      FDKmemcpy(sfb_nrg, delta_F, no_of_bands * sizeof(SCHAR));
568
0
    }
569
0
    sfb_nrg += no_of_bands;
570
0
    h_sbrCodeEnvelope->upDate = 1;
571
0
  }
572
0
}
573
574
/*******************************************************************************
575
 Functionname:  FDKsbrEnc_InitSbrCodeEnvelope
576
 *******************************************************************************
577
578
 Description:
579
580
 Arguments:
581
582
 Return:
583
584
*******************************************************************************/
585
INT FDKsbrEnc_InitSbrCodeEnvelope(HANDLE_SBR_CODE_ENVELOPE h_sbrCodeEnvelope,
586
                                  INT *nSfb, INT deltaTAcrossFrames,
587
                                  FIXP_DBL dF_edge_1stEnv,
588
0
                                  FIXP_DBL dF_edge_incr) {
589
0
  FDKmemclear(h_sbrCodeEnvelope, sizeof(SBR_CODE_ENVELOPE));
590
591
0
  h_sbrCodeEnvelope->deltaTAcrossFrames = deltaTAcrossFrames;
592
0
  h_sbrCodeEnvelope->dF_edge_1stEnv = dF_edge_1stEnv;
593
0
  h_sbrCodeEnvelope->dF_edge_incr = dF_edge_incr;
594
0
  h_sbrCodeEnvelope->dF_edge_incr_fac = 0;
595
0
  h_sbrCodeEnvelope->upDate = 0;
596
0
  h_sbrCodeEnvelope->nSfb[FREQ_RES_LOW] = nSfb[FREQ_RES_LOW];
597
0
  h_sbrCodeEnvelope->nSfb[FREQ_RES_HIGH] = nSfb[FREQ_RES_HIGH];
598
0
  h_sbrCodeEnvelope->offset = 2 * h_sbrCodeEnvelope->nSfb[FREQ_RES_LOW] -
599
0
                              h_sbrCodeEnvelope->nSfb[FREQ_RES_HIGH];
600
601
0
  return (0);
602
0
}