Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fdk-aac/cpp/libAACdec/src/usacdec_lpc.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2019 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
/**************************** AAC decoder library ******************************
96
97
   Author(s):   Matthias Hildenbrand, Manuel Jander
98
99
   Description: USAC LPC/AVQ decode
100
101
*******************************************************************************/
102
103
#include "usacdec_lpc.h"
104
105
#include "usacdec_rom.h"
106
#include "FDK_trigFcts.h"
107
108
19.7M
#define NQ_MAX 36
109
110
/*
111
 * Helper functions.
112
 */
113
114
/**
115
 * \brief Read unary code.
116
 * \param hBs bitstream handle as data source.
117
 * \return decoded value.
118
 */
119
9.40M
static int get_vlclbf(HANDLE_FDK_BITSTREAM hBs) {
120
9.40M
  int result = 0;
121
122
18.7M
  while (FDKreadBits(hBs, 1) && result <= NQ_MAX) {
123
9.32M
    result++;
124
9.32M
  }
125
9.40M
  return result;
126
9.40M
}
127
128
/**
129
 * \brief Read bit count limited unary code.
130
 * \param hBs bitstream handle as data source
131
 * \param n max amount of bits to be read.
132
 * \return decoded value.
133
 */
134
307k
static int get_vlclbf_n(HANDLE_FDK_BITSTREAM hBs, int n) {
135
307k
  int result = 0;
136
137
521k
  while (FDKreadBits(hBs, 1)) {
138
293k
    result++;
139
293k
    n--;
140
293k
    if (n <= 0) {
141
78.8k
      break;
142
78.8k
    }
143
293k
  }
144
145
307k
  return result;
146
307k
}
147
148
/*
149
 * Algebraic Vector Quantizer
150
 */
151
152
/* ZF_SCALE must be greater than (number of FIXP_ZF)/2
153
   because the loss of precision caused by fPow2Div2 in RE8_PPV() */
154
//#define ZF_SCALE ((NQ_MAX-3)>>1)
155
75.6M
#define ZF_SCALE ((DFRACT_BITS / 2))
156
20.8M
#define FIXP_ZF FIXP_DBL
157
58.9M
#define INT2ZF(x, s) (FIXP_ZF)((x) << (ZF_SCALE - (s)))
158
16.6M
#define ZF2INT(x) (INT)((x) >> ZF_SCALE)
159
160
/* 1.0 in ZF format format */
161
25.0M
#define ONEZF ((FIXP_ZF)INT2ZF(1, 0))
162
163
/* static */
164
2.08M
void nearest_neighbor_2D8(FIXP_ZF x[8], int y[8]) {
165
2.08M
  FIXP_ZF s, em, e[8];
166
2.08M
  int i, j, sum;
167
168
  /* round x into 2Z^8 i.e. compute y=(y1,...,y8) such that yi = 2[xi/2]
169
     where [.] is the nearest integer operator
170
     in the mean time, compute sum = y1+...+y8
171
  */
172
2.08M
  sum = 0;
173
18.7M
  for (i = 0; i < 8; i++) {
174
16.6M
    FIXP_ZF tmp;
175
    /* round to ..., -2, 0, 2, ... ([-1..1[ --> 0) */
176
16.6M
    if (x[i] < (FIXP_ZF)0) {
177
3.91M
      tmp = ONEZF - x[i];
178
3.91M
      y[i] = -2 * ((ZF2INT(tmp)) >> 1);
179
12.7M
    } else {
180
12.7M
      tmp = ONEZF + x[i];
181
12.7M
      y[i] = 2 * ((ZF2INT(tmp)) >> 1);
182
12.7M
    }
183
16.6M
    sum += y[i];
184
16.6M
  }
185
  /* check if y1+...+y8 is a multiple of 4
186
     if not, y is not round xj in the wrong way where j is defined by
187
        j = arg max_i | xi -yi|
188
     (this is called the Wagner rule)
189
  */
190
2.08M
  if (sum % 4) {
191
    /* find j = arg max_i | xi -yi| */
192
1.11M
    em = (FIXP_SGL)0;
193
1.11M
    j = 0;
194
10.0M
    for (i = 0; i < 8; i++) {
195
      /* compute ei = xi-yi */
196
8.91M
      e[i] = x[i] - INT2ZF(y[i], 0);
197
8.91M
    }
198
10.0M
    for (i = 0; i < 8; i++) {
199
      /* compute |ei| = | xi-yi | */
200
8.91M
      if (e[i] < (FIXP_ZF)0) {
201
3.06M
        s = -e[i];
202
5.84M
      } else {
203
5.84M
        s = e[i];
204
5.84M
      }
205
      /* check if |ei| is maximal, if so, set j=i */
206
8.91M
      if (em < s) {
207
1.39M
        em = s;
208
1.39M
        j = i;
209
1.39M
      }
210
8.91M
    }
211
    /* round xj in the "wrong way" */
212
1.11M
    if (e[j] < (FIXP_ZF)0) {
213
596k
      y[j] -= 2;
214
596k
    } else {
215
517k
      y[j] += 2;
216
517k
    }
217
1.11M
  }
218
2.08M
}
219
220
/*--------------------------------------------------------------
221
  RE8_PPV(x,y)
222
  NEAREST NEIGHBOR SEARCH IN INFINITE LATTICE RE8
223
  the algorithm is based on the definition of RE8 as
224
      RE8 = (2D8) U (2D8+[1,1,1,1,1,1,1,1])
225
  it applies the coset decoding of Sloane and Conway
226
  (i) x: point in R^8 in 32-ZF_SCALE.ZF_SCALE format
227
  (o) y: point in RE8 (8-dimensional integer vector)
228
  --------------------------------------------------------------
229
*/
230
/* static */
231
1.04M
void RE8_PPV(FIXP_ZF x[], SHORT y[], int r) {
232
1.04M
  int i, y0[8], y1[8];
233
1.04M
  FIXP_ZF x1[8], tmp;
234
1.04M
  INT64 e;
235
236
  /* find the nearest neighbor y0 of x in 2D8 */
237
1.04M
  nearest_neighbor_2D8(x, y0);
238
  /* find the nearest neighbor y1 of x in 2D8+(1,...,1) (by coset decoding) */
239
9.38M
  for (i = 0; i < 8; i++) {
240
8.34M
    x1[i] = x[i] - ONEZF;
241
8.34M
  }
242
1.04M
  nearest_neighbor_2D8(x1, y1);
243
9.38M
  for (i = 0; i < 8; i++) {
244
8.34M
    y1[i] += 1;
245
8.34M
  }
246
247
  /* compute e0=||x-y0||^2 and e1=||x-y1||^2 */
248
1.04M
  e = 0;
249
9.38M
  for (i = 0; i < 8; i++) {
250
8.34M
    tmp = x[i] - INT2ZF(y0[i], 0);
251
8.34M
    e += (INT64)fPow2Div2(
252
8.34M
        tmp << r); /* shift left to ensure that no fract part bits get lost. */
253
8.34M
    tmp = x[i] - INT2ZF(y1[i], 0);
254
8.34M
    e -= (INT64)fPow2Div2(tmp << r);
255
8.34M
  }
256
  /* select best candidate y0 or y1 to minimize distortion */
257
1.04M
  if (e < 0) {
258
3.81M
    for (i = 0; i < 8; i++) {
259
3.39M
      y[i] = y0[i];
260
3.39M
    }
261
618k
  } else {
262
5.56M
    for (i = 0; i < 8; i++) {
263
4.94M
      y[i] = y1[i];
264
4.94M
    }
265
618k
  }
266
1.04M
}
267
268
/* table look-up of unsigned value: find i where index >= table[i]
269
   Note: range must be >= 2, index must be >= table[0] */
270
8.88M
static int table_lookup(const USHORT *table, unsigned int index, int range) {
271
8.88M
  int i;
272
273
12.6M
  for (i = 4; i < range; i += 4) {
274
11.0M
    if (index < table[i]) {
275
7.32M
      break;
276
7.32M
    }
277
11.0M
  }
278
8.88M
  if (i > range) {
279
1.14M
    i = range;
280
1.14M
  }
281
282
8.88M
  if (index < table[i - 2]) {
283
5.44M
    i -= 2;
284
5.44M
  }
285
8.88M
  if (index < table[i - 1]) {
286
4.17M
    i--;
287
4.17M
  }
288
8.88M
  i--;
289
290
8.88M
  return (i); /* index >= table[i] */
291
8.88M
}
292
293
/*--------------------------------------------------------------------------
294
  re8_decode_rank_of_permutation(rank, xs, x)
295
  DECODING OF THE RANK OF THE PERMUTATION OF xs
296
  (i) rank: index (rank) of a permutation
297
  (i) xs:   signed leader in RE8 (8-dimensional integer vector)
298
  (o) x:    point in RE8 (8-dimensional integer vector)
299
  --------------------------------------------------------------------------
300
 */
301
4.44M
static void re8_decode_rank_of_permutation(int rank, int *xs, SHORT x[8]) {
302
4.44M
  INT a[8], w[8], B, fac, fac_B, target;
303
4.44M
  int i, j;
304
305
  /* --- pre-processing based on the signed leader xs ---
306
     - compute the alphabet a=[a[0] ... a[q-1]] of x (q elements)
307
       such that a[0]!=...!=a[q-1]
308
       it is assumed that xs is sorted in the form of a signed leader
309
       which can be summarized in 2 requirements:
310
          a) |xs[0]| >= |xs[1]| >= |xs[2]| >= ... >= |xs[7]|
311
          b) if |xs[i]|=|xs[i-1]|, xs[i]>=xs[i+1]
312
       where |.| indicates the absolute value operator
313
     - compute q (the number of symbols in the alphabet)
314
     - compute w[0..q-1] where w[j] counts the number of occurences of
315
       the symbol a[j] in xs
316
     - compute B = prod_j=0..q-1 (w[j]!) where .! is the factorial */
317
  /* xs[i], xs[i-1] and ptr_w/a*/
318
4.44M
  j = 0;
319
4.44M
  w[j] = 1;
320
4.44M
  a[j] = xs[0];
321
4.44M
  B = 1;
322
35.5M
  for (i = 1; i < 8; i++) {
323
31.0M
    if (xs[i] != xs[i - 1]) {
324
7.83M
      j++;
325
7.83M
      w[j] = 1;
326
7.83M
      a[j] = xs[i];
327
23.2M
    } else {
328
23.2M
      w[j]++;
329
23.2M
      B *= w[j];
330
23.2M
    }
331
31.0M
  }
332
333
  /* --- actual rank decoding ---
334
     the rank of x (where x is a permutation of xs) is based on
335
     Schalkwijk's formula
336
     it is given by rank=sum_{k=0..7} (A_k * fac_k/B_k)
337
     the decoding of this rank is sequential and reconstructs x[0..7]
338
     element by element from x[0] to x[7]
339
     [the tricky part is the inference of A_k for each k...]
340
   */
341
342
4.44M
  if (w[0] == 8) {
343
2.88M
    for (i = 0; i < 8; i++) {
344
2.56M
      x[i] = a[0]; /* avoid fac of 40320 */
345
2.56M
    }
346
4.12M
  } else {
347
4.12M
    target = rank * B;
348
4.12M
    fac_B = 1;
349
    /* decode x element by element */
350
37.0M
    for (i = 0; i < 8; i++) {
351
32.9M
      fac = fac_B * fdk_dec_tab_factorial[i]; /* fac = 1..5040 */
352
32.9M
      j = -1;
353
75.0M
      do {
354
75.0M
        target -= w[++j] * fac;
355
75.0M
      } while (target >= 0); /* max of 30 tests / SV */
356
32.9M
      x[i] = a[j];
357
      /* update rank, denominator B (B_k) and counter w[j] */
358
32.9M
      target += w[j] * fac; /* target = fac_B*B*rank */
359
32.9M
      fac_B *= w[j];
360
32.9M
      w[j]--;
361
32.9M
    }
362
4.12M
  }
363
4.44M
}
364
365
/*--------------------------------------------------------------------------
366
  re8_decode_base_index(n, I, y)
367
  DECODING OF AN INDEX IN Qn (n=0,2,3 or 4)
368
  (i) n: codebook number (*n is an integer defined in {0,2,3,4})
369
  (i) I: index of c (pointer to unsigned 16-bit word)
370
  (o) y: point in RE8 (8-dimensional integer vector)
371
  note: the index I is defined as a 32-bit word, but only
372
  16 bits are required (long can be replaced by unsigned integer)
373
  --------------------------------------------------------------------------
374
 */
375
10.4M
static void re8_decode_base_index(int *n, UINT index, SHORT y[8]) {
376
10.4M
  int i, im, t, sign_code, ka, ks, rank, leader[8];
377
378
10.4M
  if (*n < 2) {
379
53.7M
    for (i = 0; i < 8; i++) {
380
47.7M
      y[i] = 0;
381
47.7M
    }
382
5.97M
  } else {
383
    // index = (unsigned int)*I;
384
    /* search for the identifier ka of the absolute leader (table-lookup)
385
       Q2 is a subset of Q3 - the two cases are considered in the same branch
386
     */
387
4.44M
    switch (*n) {
388
1.90M
      case 2:
389
3.33M
      case 3:
390
3.33M
        i = table_lookup(fdk_dec_I3, index, NB_LDQ3);
391
3.33M
        ka = fdk_dec_A3[i];
392
3.33M
        break;
393
1.10M
      case 4:
394
1.10M
        i = table_lookup(fdk_dec_I4, index, NB_LDQ4);
395
1.10M
        ka = fdk_dec_A4[i];
396
1.10M
        break;
397
0
      default:
398
0
        FDK_ASSERT(0);
399
0
        return;
400
4.44M
    }
401
    /* reconstruct the absolute leader */
402
39.9M
    for (i = 0; i < 8; i++) {
403
35.5M
      leader[i] = fdk_dec_Da[ka][i];
404
35.5M
    }
405
    /* search for the identifier ks of the signed leader (table look-up)
406
       (this search is focused based on the identifier ka of the absolute
407
        leader)*/
408
4.44M
    t = fdk_dec_Ia[ka];
409
4.44M
    im = fdk_dec_Ns[ka];
410
4.44M
    ks = table_lookup(fdk_dec_Is + t, index, im);
411
412
    /* reconstruct the signed leader from its sign code */
413
4.44M
    sign_code = 2 * fdk_dec_Ds[t + ks];
414
39.9M
    for (i = 7; i >= 0; i--) {
415
35.5M
      leader[i] *= (1 - (sign_code & 2));
416
35.5M
      sign_code >>= 1;
417
35.5M
    }
418
419
    /* compute and decode the rank of the permutation */
420
4.44M
    rank = index - fdk_dec_Is[t + ks]; /* rank = index - cardinality offset */
421
422
4.44M
    re8_decode_rank_of_permutation(rank, leader, y);
423
4.44M
  }
424
10.4M
  return;
425
10.4M
}
426
427
/* re8_y2k(y,m,k)
428
   VORONOI INDEXING (INDEX DECODING) k -> y
429
   (i) k: Voronoi index k[0..7]
430
   (i) m: Voronoi modulo (m = 2^r = 1<<r, where r is integer >=2)
431
   (i) r: Voronoi order  (m = 2^r = 1<<r, where r is integer >=2)
432
   (o) y: 8-dimensional point y[0..7] in RE8
433
 */
434
1.04M
static void re8_k2y(int *k, int r, SHORT *y) {
435
1.04M
  int i, tmp, sum;
436
1.04M
  SHORT v[8];
437
1.04M
  FIXP_ZF zf[8];
438
439
1.04M
  FDK_ASSERT(r <= ZF_SCALE);
440
441
  /* compute y = k M and z=(y-a)/m, where
442
     M = [4        ]
443
         [2 2      ]
444
         [|   \    ]
445
         [2     2  ]
446
         [1 1 _ 1 1]
447
     a=(2,0,...,0)
448
     m = 1<<r
449
  */
450
9.38M
  for (i = 0; i < 8; i++) {
451
8.34M
    y[i] = k[7];
452
8.34M
  }
453
1.04M
  zf[7] = INT2ZF(y[7], r);
454
1.04M
  sum = 0;
455
7.30M
  for (i = 6; i >= 1; i--) {
456
6.25M
    tmp = 2 * k[i];
457
6.25M
    sum += tmp;
458
6.25M
    y[i] += tmp;
459
6.25M
    zf[i] = INT2ZF(y[i], r);
460
6.25M
  }
461
1.04M
  y[0] += (4 * k[0] + sum);
462
1.04M
  zf[0] = INT2ZF(y[0] - 2, r);
463
  /* find nearest neighbor v of z in infinite RE8 */
464
1.04M
  RE8_PPV(zf, v, r);
465
  /* compute y -= m v */
466
9.38M
  for (i = 0; i < 8; i++) {
467
8.34M
    y[i] -= (SHORT)(v[i] << r);
468
8.34M
  }
469
1.04M
}
470
471
/*--------------------------------------------------------------------------
472
  RE8_dec(n, I, k, y)
473
  MULTI-RATE INDEXING OF A POINT y in THE LATTICE RE8 (INDEX DECODING)
474
  (i) n: codebook number (*n is an integer defined in {0,2,3,4,..,n_max}). n_max
475
  = 36 (i) I: index of c (pointer to unsigned 16-bit word) (i) k: index of v
476
  (8-dimensional vector of binary indices) = Voronoi index (o) y: point in RE8
477
  (8-dimensional integer vector) note: the index I is defined as a 32-bit word,
478
  but only 16 bits are required (long can be replaced by unsigned integer)
479
480
  return 0 on success, -1 on error.
481
  --------------------------------------------------------------------------
482
 */
483
10.4M
static int RE8_dec(int n, int I, int *k, FIXP_DBL *y) {
484
10.4M
  SHORT v[8];
485
10.4M
  SHORT _y[8];
486
10.4M
  UINT r;
487
10.4M
  int i;
488
489
  /* Check bound of codebook qn */
490
10.4M
  if (n > NQ_MAX) {
491
20.7k
    return -1;
492
20.7k
  }
493
494
  /* decode the sub-indices I and kv[] according to the codebook number n:
495
     if n=0,2,3,4, decode I (no Voronoi extension)
496
     if n>4, Voronoi extension is used, decode I and kv[] */
497
10.4M
  if (n <= 4) {
498
9.37M
    re8_decode_base_index(&n, I, _y);
499
84.3M
    for (i = 0; i < 8; i++) {
500
74.9M
      y[i] = (LONG)_y[i];
501
74.9M
    }
502
9.37M
  } else {
503
    /* compute the Voronoi modulo m = 2^r where r is extension order */
504
1.04M
    r = ((n - 3) >> 1);
505
506
2.70M
    while (n > 4) {
507
1.65M
      n -= 2;
508
1.65M
    }
509
    /* decode base codebook index I into c (c is an element of Q3 or Q4)
510
       [here c is stored in y to save memory] */
511
1.04M
    re8_decode_base_index(&n, I, _y);
512
    /* decode Voronoi index k[] into v */
513
1.04M
    re8_k2y(k, r, v);
514
    /* reconstruct y as y = m c + v (with m=2^r, r integer >=1) */
515
9.38M
    for (i = 0; i < 8; i++) {
516
8.34M
      y[i] = (LONG)((_y[i] << r) + v[i]);
517
8.34M
    }
518
1.04M
  }
519
10.4M
  return 0;
520
10.4M
}
521
522
/**************************/
523
/* start LPC decode stuff */
524
/**************************/
525
//#define M         16
526
#define FREQ_MAX 6400.0f
527
#define FREQ_DIV 400.0f
528
#define LSF_GAP 50.0f
529
530
/**
531
 * \brief calculate inverse weighting factor and add non-weighted residual
532
 *        LSF vector to first stage LSF approximation
533
 * \param lsfq first stage LSF approximation values.
534
 * \param xq weighted residual LSF vector
535
 * \param nk_mode code book number coding mode.
536
 */
537
788k
static void lsf_weight_2st(FIXP_LPC *lsfq, FIXP_DBL *xq, int nk_mode) {
538
788k
  FIXP_LPC d[M_LP_FILTER_ORDER + 1];
539
788k
  FIXP_SGL factor;
540
788k
  LONG w; /* inverse weight factor */
541
788k
  int i;
542
543
  /* compute lsf distance */
544
788k
  d[0] = lsfq[0];
545
788k
  d[M_LP_FILTER_ORDER] =
546
788k
      FL2FXCONST_LPC(FREQ_MAX / (1 << LSF_SCALE)) - lsfq[M_LP_FILTER_ORDER - 1];
547
12.6M
  for (i = 1; i < M_LP_FILTER_ORDER; i++) {
548
11.8M
    d[i] = lsfq[i] - lsfq[i - 1];
549
11.8M
  }
550
551
788k
  switch (nk_mode) {
552
429k
    case 0:
553
429k
      factor = FL2FXCONST_SGL(2.0f * 60.0f / FREQ_DIV);
554
429k
      break; /* abs */
555
81.8k
    case 1:
556
81.8k
      factor = FL2FXCONST_SGL(2.0f * 65.0f / FREQ_DIV);
557
81.8k
      break; /* mid */
558
123k
    case 2:
559
123k
      factor = FL2FXCONST_SGL(2.0f * 64.0f / FREQ_DIV);
560
123k
      break; /* rel1 */
561
152k
    default:
562
152k
      factor = FL2FXCONST_SGL(2.0f * 63.0f / FREQ_DIV);
563
152k
      break; /* rel2 */
564
788k
  }
565
  /* add non-weighted residual LSF vector to LSF1st */
566
13.4M
  for (i = 0; i < M_LP_FILTER_ORDER; i++) {
567
12.6M
    w = (LONG)fMultDiv2(factor, sqrtFixp(fMult(d[i], d[i + 1])));
568
12.6M
    lsfq[i] = fAddSaturate(lsfq[i],
569
12.6M
                           FX_DBL2FX_LPC((FIXP_DBL)((INT64)w * (LONG)xq[i])));
570
12.6M
  }
571
572
788k
  return;
573
788k
}
574
575
/**
576
 * \brief decode nqn amount of code book numbers. These values determine the
577
 * amount of following bits for nqn AVQ RE8 vectors.
578
 * \param nk_mode quantization mode.
579
 * \param nqn amount code book number to read.
580
 * \param qn pointer to output buffer to hold decoded code book numbers qn.
581
 */
582
static void decode_qn(HANDLE_FDK_BITSTREAM hBs, int nk_mode, int nqn,
583
9.64M
                      int qn[]) {
584
9.64M
  int n;
585
586
9.64M
  if (nk_mode == 1) { /* nk mode 1 */
587
    /* Unary code for mid LPC1/LPC3 */
588
    /* Q0=0, Q2=10, Q3=110, ... */
589
17.9M
    for (n = 0; n < nqn; n++) {
590
9.01M
      qn[n] = get_vlclbf(hBs);
591
9.01M
      if (qn[n] > 0) {
592
3.09M
        qn[n]++;
593
3.09M
      }
594
9.01M
    }
595
8.93M
  } else { /* nk_mode 0, 3 and 2 */
596
    /* 2 bits to specify Q2,Q3,Q4,ext */
597
2.13M
    for (n = 0; n < nqn; n++) {
598
1.42M
      qn[n] = 2 + FDKreadBits(hBs, 2);
599
1.42M
    }
600
712k
    if (nk_mode == 2) {
601
      /* Unary code for rel LPC1/LPC3 */
602
      /* Q0 = 0, Q5=10, Q6=110, ... */
603
374k
      for (n = 0; n < nqn; n++) {
604
249k
        if (qn[n] > 4) {
605
58.6k
          qn[n] = get_vlclbf(hBs);
606
58.6k
          if (qn[n] > 0) qn[n] += 4;
607
58.6k
        }
608
249k
      }
609
587k
    } else { /* nk_mode == (0 and 3) */
610
      /* Unary code for abs and rel LPC0/LPC2 */
611
      /* Q5 = 0, Q6=10, Q0=110, Q7=1110, ... */
612
1.76M
      for (n = 0; n < nqn; n++) {
613
1.17M
        if (qn[n] > 4) {
614
334k
          qn[n] = get_vlclbf(hBs);
615
334k
          switch (qn[n]) {
616
152k
            case 0:
617
152k
              qn[n] = 5;
618
152k
              break;
619
78.7k
            case 1:
620
78.7k
              qn[n] = 6;
621
78.7k
              break;
622
26.9k
            case 2:
623
26.9k
              qn[n] = 0;
624
26.9k
              break;
625
76.3k
            default:
626
76.3k
              qn[n] += 4;
627
76.3k
              break;
628
334k
          }
629
334k
        }
630
1.17M
      }
631
587k
    }
632
712k
  }
633
9.64M
}
634
635
/**
636
 * \brief reorder LSF coefficients to minimum distance.
637
 * \param lsf pointer to buffer containing LSF coefficients and where reordered
638
 * LSF coefficients will be stored into, scaled by LSF_SCALE.
639
 * \param min_dist min distance scaled by LSF_SCALE
640
 * \param n number of LSF/LSP coefficients.
641
 */
642
788k
static void reorder_lsf(FIXP_LPC *lsf, FIXP_LPC min_dist, int n) {
643
788k
  FIXP_LPC lsf_min;
644
788k
  int i;
645
646
788k
  lsf_min = min_dist;
647
13.4M
  for (i = 0; i < n; i++) {
648
12.6M
    if (lsf[i] < lsf_min) {
649
1.11M
      lsf[i] = lsf_min;
650
1.11M
    }
651
12.6M
    lsf_min = fAddSaturate(lsf[i], min_dist);
652
12.6M
  }
653
654
  /* reverse */
655
788k
  lsf_min = FL2FXCONST_LPC(FREQ_MAX / (1 << LSF_SCALE)) - min_dist;
656
13.4M
  for (i = n - 1; i >= 0; i--) {
657
12.6M
    if (lsf[i] > lsf_min) {
658
255k
      lsf[i] = lsf_min;
659
255k
    }
660
661
12.6M
    lsf_min = lsf[i] - min_dist;
662
12.6M
  }
663
788k
}
664
665
/**
666
 * \brief First stage approximation
667
 * \param hBs bitstream handle as data source
668
 * \param lsfq pointer to output buffer to hold LPC coefficients scaled by
669
 * LSF_SCALE.
670
 */
671
static void vlpc_1st_dec(
672
    HANDLE_FDK_BITSTREAM hBs, /* input:  codebook index                  */
673
    FIXP_LPC *lsfq            /* i/o:    i:prediction   o:quantized lsf  */
674
433k
) {
675
433k
  const FIXP_LPC *p_dico;
676
433k
  int i, index;
677
678
433k
  index = FDKreadBits(hBs, 8);
679
433k
  p_dico = &fdk_dec_dico_lsf_abs_8b[index * M_LP_FILTER_ORDER];
680
7.37M
  for (i = 0; i < M_LP_FILTER_ORDER; i++) {
681
6.94M
    lsfq[i] = p_dico[i];
682
6.94M
  }
683
433k
}
684
685
/**
686
 * \brief Do first stage approximation weighting and multiply with AVQ
687
 * refinement.
688
 * \param hBs bitstream handle data ssource.
689
 * \param lsfq buffer holding 1st stage approx, 2nd stage approx is added to
690
 * this values.
691
 * \param nk_mode quantization mode.
692
 * \return 0 on success, -1 on error.
693
 */
694
static int vlpc_2st_dec(
695
    HANDLE_FDK_BITSTREAM hBs,
696
    FIXP_LPC *lsfq, /* i/o:    i:1st stage   o:1st+2nd stage   */
697
    int nk_mode     /* input:  0=abs, >0=rel                   */
698
793k
) {
699
793k
  int err;
700
793k
  FIXP_DBL xq[M_LP_FILTER_ORDER]; /* weighted residual LSF vector */
701
702
  /* Decode AVQ refinement */
703
793k
  { err = CLpc_DecodeAVQ(hBs, xq, nk_mode, 2, 8); }
704
793k
  if (err != 0) {
705
5.52k
    return -1;
706
5.52k
  }
707
708
  /* add non-weighted residual LSF vector to LSF1st */
709
788k
  lsf_weight_2st(lsfq, xq, nk_mode);
710
711
  /* reorder */
712
788k
  reorder_lsf(lsfq, FL2FXCONST_LPC(LSF_GAP / (1 << LSF_SCALE)),
713
788k
              M_LP_FILTER_ORDER);
714
715
788k
  return 0;
716
793k
}
717
718
/*
719
 * Externally visible functions
720
 */
721
722
int CLpc_DecodeAVQ(HANDLE_FDK_BITSTREAM hBs, FIXP_DBL *pOutput, int nk_mode,
723
1.47M
                   int no_qn, int length) {
724
1.47M
  int i, l;
725
726
11.1M
  for (i = 0; i < length; i += 8 * no_qn) {
727
9.64M
    int qn[2], nk, n, I;
728
9.64M
    int kv[8] = {0};
729
730
9.64M
    decode_qn(hBs, nk_mode, no_qn, qn);
731
732
20.0M
    for (l = 0; l < no_qn; l++) {
733
10.4M
      if (qn[l] == 0) {
734
5.97M
        FDKmemclear(&pOutput[i + l * 8], 8 * sizeof(FIXP_DBL));
735
5.97M
      }
736
737
      /* Voronoi extension order ( nk ) */
738
10.4M
      nk = 0;
739
10.4M
      n = qn[l];
740
10.4M
      if (qn[l] > 4) {
741
1.06M
        nk = (qn[l] - 3) >> 1;
742
1.06M
        n = qn[l] - nk * 2;
743
1.06M
      }
744
745
      /* Base codebook index, in reverse bit group order (!) */
746
10.4M
      I = FDKreadBits(hBs, 4 * n);
747
748
10.4M
      if (nk > 0) {
749
1.06M
        int j;
750
751
9.57M
        for (j = 0; j < 8; j++) {
752
8.51M
          kv[j] = FDKreadBits(hBs, nk);
753
8.51M
        }
754
1.06M
      }
755
756
10.4M
      if (RE8_dec(qn[l], I, kv, &pOutput[i + l * 8]) != 0) {
757
20.7k
        return -1;
758
20.7k
      }
759
10.4M
    }
760
9.64M
  }
761
1.45M
  return 0;
762
1.47M
}
763
764
int CLpc_Read(HANDLE_FDK_BITSTREAM hBs, FIXP_LPC lsp[][M_LP_FILTER_ORDER],
765
              FIXP_LPC lpc4_lsf[M_LP_FILTER_ORDER],
766
              FIXP_LPC lsf_adaptive_mean_cand[M_LP_FILTER_ORDER],
767
              FIXP_SGL pStability[], UCHAR *mod, int first_lpd_flag,
768
196k
              int last_lpc_lost, int last_frame_ok) {
769
196k
  int i, k, err;
770
196k
  int mode_lpc_bin = 0; /* mode_lpc bitstream representation */
771
196k
  int lpc_present[5] = {0, 0, 0, 0, 0};
772
196k
  int lpc0_available = 1;
773
196k
  int s = 0;
774
196k
  int l = 3;
775
196k
  const int nbDiv = NB_DIV;
776
777
196k
  lpc_present[4 >> s] = 1; /* LPC4 */
778
779
  /* Decode LPC filters in the following order: LPC 4,0,2,1,3 */
780
781
  /*** Decode LPC4 ***/
782
196k
  vlpc_1st_dec(hBs, lsp[4 >> s]);
783
196k
  err = vlpc_2st_dec(hBs, lsp[4 >> s], 0); /* nk_mode = 0 */
784
196k
  if (err != 0) {
785
2.02k
    return err;
786
2.02k
  }
787
788
  /*** Decode LPC0 and LPC2 ***/
789
194k
  k = 0;
790
194k
  if (!first_lpd_flag) {
791
54.3k
    lpc_present[0] = 1;
792
54.3k
    lpc0_available = !last_lpc_lost;
793
    /* old LPC4 is new LPC0 */
794
923k
    for (i = 0; i < M_LP_FILTER_ORDER; i++) {
795
869k
      lsp[0][i] = lpc4_lsf[i];
796
869k
    }
797
    /* skip LPC0 and continue with LPC2 */
798
54.3k
    k = 2;
799
54.3k
  }
800
801
517k
  for (; k < l; k += 2) {
802
332k
    int nk_mode = 0;
803
804
332k
    if ((k == 2) && (mod[0] == 3)) {
805
7.35k
      break; /* skip LPC2 */
806
7.35k
    }
807
808
325k
    lpc_present[k >> s] = 1;
809
810
325k
    mode_lpc_bin = FDKreadBit(hBs);
811
812
325k
    if (mode_lpc_bin == 0) {
813
      /* LPC0/LPC2: Abs */
814
172k
      vlpc_1st_dec(hBs, lsp[k >> s]);
815
172k
    } else {
816
      /* LPC0/LPC2: RelR */
817
2.60M
      for (i = 0; i < M_LP_FILTER_ORDER; i++) {
818
2.45M
        lsp[k >> s][i] = lsp[4 >> s][i];
819
2.45M
      }
820
153k
      nk_mode = 3;
821
153k
    }
822
823
325k
    err = vlpc_2st_dec(hBs, lsp[k >> s], nk_mode);
824
325k
    if (err != 0) {
825
2.20k
      return err;
826
2.20k
    }
827
325k
  }
828
829
  /*** Decode LPC1 ***/
830
192k
  if (mod[0] < 2) { /* else: skip LPC1 */
831
134k
    lpc_present[1] = 1;
832
134k
    mode_lpc_bin = get_vlclbf_n(hBs, 2);
833
834
134k
    switch (mode_lpc_bin) {
835
31.1k
      case 1:
836
        /* LPC1: abs */
837
31.1k
        vlpc_1st_dec(hBs, lsp[1]);
838
31.1k
        err = vlpc_2st_dec(hBs, lsp[1], 0);
839
31.1k
        if (err != 0) {
840
78
          return err;
841
78
        }
842
31.0k
        break;
843
35.5k
      case 2:
844
        /* LPC1: mid0 (no second stage AVQ quantizer in this case) */
845
35.5k
        if (lpc0_available) { /* LPC0/lsf[0] might be zero some times */
846
600k
          for (i = 0; i < M_LP_FILTER_ORDER; i++) {
847
565k
            lsp[1][i] = (lsp[0][i] >> 1) + (lsp[2][i] >> 1);
848
565k
          }
849
35.3k
        } else {
850
2.92k
          for (i = 0; i < M_LP_FILTER_ORDER; i++) {
851
2.75k
            lsp[1][i] = lsp[2][i];
852
2.75k
          }
853
172
        }
854
35.5k
        break;
855
68.0k
      case 0:
856
        /* LPC1: RelR */
857
1.15M
        for (i = 0; i < M_LP_FILTER_ORDER; i++) {
858
1.08M
          lsp[1][i] = lsp[2][i];
859
1.08M
        }
860
68.0k
        err = vlpc_2st_dec(hBs, lsp[1], 2 << s);
861
68.0k
        if (err != 0) {
862
158
          return err;
863
158
        }
864
67.8k
        break;
865
134k
    }
866
134k
  }
867
868
  /*** Decode LPC3 ***/
869
192k
  if ((mod[2] < 2)) { /* else: skip LPC3 */
870
172k
    int nk_mode = 0;
871
172k
    lpc_present[3] = 1;
872
873
172k
    mode_lpc_bin = get_vlclbf_n(hBs, 3);
874
875
172k
    switch (mode_lpc_bin) {
876
33.7k
      case 1:
877
        /* LPC3: abs */
878
33.7k
        vlpc_1st_dec(hBs, lsp[3]);
879
33.7k
        break;
880
81.8k
      case 0:
881
        /* LPC3: mid */
882
1.39M
        for (i = 0; i < M_LP_FILTER_ORDER; i++) {
883
1.31M
          lsp[3][i] = (lsp[2][i] >> 1) + (lsp[4][i] >> 1);
884
1.31M
        }
885
81.8k
        nk_mode = 1;
886
81.8k
        break;
887
13.5k
      case 2:
888
        /* LPC3: relL */
889
230k
        for (i = 0; i < M_LP_FILTER_ORDER; i++) {
890
216k
          lsp[3][i] = lsp[2][i];
891
216k
        }
892
13.5k
        nk_mode = 2;
893
13.5k
        break;
894
43.3k
      case 3:
895
        /* LPC3: relR */
896
737k
        for (i = 0; i < M_LP_FILTER_ORDER; i++) {
897
694k
          lsp[3][i] = lsp[4][i];
898
694k
        }
899
43.3k
        nk_mode = 2;
900
43.3k
        break;
901
172k
    }
902
172k
    err = vlpc_2st_dec(hBs, lsp[3], nk_mode);
903
172k
    if (err != 0) {
904
1.06k
      return err;
905
1.06k
    }
906
172k
  }
907
908
191k
  if (!lpc0_available && !last_frame_ok) {
909
    /* LPC(0) was lost. Use next available LPC(k) instead */
910
2.10k
    for (k = 1; k < (nbDiv + 1); k++) {
911
2.10k
      if (lpc_present[k]) {
912
35.8k
        for (i = 0; i < M_LP_FILTER_ORDER; i++) {
913
33.6k
#define LSF_INIT_TILT (0.25f)
914
33.6k
          if (mod[0] > 0) {
915
0
            lsp[0][i] = FX_DBL2FX_LPC(
916
0
                fMult(lsp[k][i], FL2FXCONST_SGL(1.0f - LSF_INIT_TILT)) +
917
0
                fMult(fdk_dec_lsf_init[i], FL2FXCONST_SGL(LSF_INIT_TILT)));
918
33.6k
          } else {
919
33.6k
            lsp[0][i] = lsp[k][i];
920
33.6k
          }
921
33.6k
        }
922
2.10k
        break;
923
2.10k
      }
924
2.10k
    }
925
2.10k
  }
926
927
3.24M
  for (i = 0; i < M_LP_FILTER_ORDER; i++) {
928
3.05M
    lpc4_lsf[i] = lsp[4 >> s][i];
929
3.05M
  }
930
931
191k
  {
932
191k
    FIXP_DBL divFac;
933
191k
    int last, numLpc = 0;
934
935
191k
    i = nbDiv;
936
601k
    do {
937
601k
      numLpc += lpc_present[i--];
938
601k
    } while (i >= 0 && numLpc < 3);
939
940
191k
    last = i;
941
942
191k
    switch (numLpc) {
943
183k
      case 3:
944
183k
        divFac = FL2FXCONST_DBL(1.0f / 3.0f);
945
183k
        break;
946
7.35k
      case 2:
947
7.35k
        divFac = FL2FXCONST_DBL(1.0f / 2.0f);
948
7.35k
        break;
949
0
      default:
950
0
        divFac = FL2FXCONST_DBL(1.0f);
951
0
        break;
952
191k
    }
953
954
    /* get the adaptive mean for the next (bad) frame */
955
3.24M
    for (k = 0; k < M_LP_FILTER_ORDER; k++) {
956
3.05M
      FIXP_DBL tmp = (FIXP_DBL)0;
957
12.6M
      for (i = nbDiv; i > last; i--) {
958
9.62M
        if (lpc_present[i]) {
959
9.05M
          tmp = fMultAdd(tmp >> 1, lsp[i][k], divFac);
960
9.05M
        }
961
9.62M
      }
962
3.05M
      lsf_adaptive_mean_cand[k] = FX_DBL2FX_LPC(tmp);
963
3.05M
    }
964
191k
  }
965
966
  /* calculate stability factor Theta. Needed for ACELP decoder and concealment
967
   */
968
0
  {
969
191k
    FIXP_LPC *lsf_prev, *lsf_curr;
970
191k
    k = 0;
971
972
191k
    FDK_ASSERT(lpc_present[0] == 1 && lpc_present[4 >> s] == 1);
973
191k
    lsf_prev = lsp[0];
974
955k
    for (i = 1; i < (nbDiv + 1); i++) {
975
764k
      if (lpc_present[i]) {
976
680k
        FIXP_DBL tmp = (FIXP_DBL)0;
977
680k
        int j;
978
680k
        lsf_curr = lsp[i];
979
980
        /* sum = tmp * 2^(LSF_SCALE*2 + 4) */
981
11.5M
        for (j = 0; j < M_LP_FILTER_ORDER; j++) {
982
10.8M
          tmp += fPow2Div2((FIXP_SGL)(lsf_curr[j] - lsf_prev[j])) >> 3;
983
10.8M
        }
984
985
        /* tmp = (float)(FL2FXCONST_DBL(1.25f) - fMult(tmp,
986
         * FL2FXCONST_DBL(1/400000.0f))); */
987
680k
        tmp = FL2FXCONST_DBL(1.25f / (1 << LSF_SCALE)) -
988
680k
              fMult(tmp, FL2FXCONST_DBL((1 << (LSF_SCALE + 4)) / 400000.0f));
989
680k
        if (tmp >= FL2FXCONST_DBL(1.0f / (1 << LSF_SCALE))) {
990
133k
          pStability[k] = FL2FXCONST_SGL(1.0f / 2.0f);
991
547k
        } else if (tmp < FL2FXCONST_DBL(0.0f)) {
992
337k
          pStability[k] = FL2FXCONST_SGL(0.0f);
993
337k
        } else {
994
209k
          pStability[k] = FX_DBL2FX_SGL(tmp << (LSF_SCALE - 1));
995
209k
        }
996
997
680k
        lsf_prev = lsf_curr;
998
680k
        k = i;
999
680k
      } else {
1000
        /* Mark stability value as undefined. */
1001
83.7k
        pStability[i] = (FIXP_SGL)-1;
1002
83.7k
      }
1003
764k
    }
1004
191k
  }
1005
1006
  /* convert into LSP domain */
1007
1.14M
  for (i = 0; i < (nbDiv + 1); i++) {
1008
955k
    if (lpc_present[i]) {
1009
14.8M
      for (k = 0; k < M_LP_FILTER_ORDER; k++) {
1010
13.9M
        lsp[i][k] = FX_DBL2FX_LPC(
1011
13.9M
            fixp_cos(fMult(lsp[i][k],
1012
13.9M
                           FL2FXCONST_SGL((1 << LSPARG_SCALE) * M_PI / 6400.0)),
1013
13.9M
                     LSF_SCALE - LSPARG_SCALE));
1014
13.9M
      }
1015
872k
    }
1016
955k
  }
1017
1018
191k
  return 0;
1019
191k
}
1020
1021
void CLpc_Conceal(FIXP_LPC lsp[][M_LP_FILTER_ORDER],
1022
                  FIXP_LPC lpc4_lsf[M_LP_FILTER_ORDER],
1023
                  FIXP_LPC lsf_adaptive_mean[M_LP_FILTER_ORDER],
1024
1.26M
                  const int first_lpd_flag) {
1025
1.26M
  int i, j;
1026
1027
1.26M
#define BETA (FL2FXCONST_SGL(0.25f))
1028
1.26M
#define ONE_BETA (FL2FXCONST_SGL(0.75f))
1029
1.26M
#define BFI_FAC (FL2FXCONST_SGL(0.90f))
1030
1.26M
#define ONE_BFI_FAC (FL2FXCONST_SGL(0.10f))
1031
1032
  /* Frame loss concealment (could be improved) */
1033
1034
1.26M
  if (first_lpd_flag) {
1035
    /* Reset past LSF values */
1036
17.9M
    for (i = 0; i < M_LP_FILTER_ORDER; i++) {
1037
16.9M
      lsp[0][i] = lpc4_lsf[i] = fdk_dec_lsf_init[i];
1038
16.9M
    }
1039
1.05M
  } else {
1040
    /* old LPC4 is new LPC0 */
1041
3.50M
    for (i = 0; i < M_LP_FILTER_ORDER; i++) {
1042
3.30M
      lsp[0][i] = lpc4_lsf[i];
1043
3.30M
    }
1044
206k
  }
1045
1046
  /* LPC1 */
1047
21.5M
  for (i = 0; i < M_LP_FILTER_ORDER; i++) {
1048
20.2M
    FIXP_LPC lsf_mean = FX_DBL2FX_LPC(fMult(BETA, fdk_dec_lsf_init[i]) +
1049
20.2M
                                      fMult(ONE_BETA, lsf_adaptive_mean[i]));
1050
1051
20.2M
    lsp[1][i] = FX_DBL2FX_LPC(fMult(BFI_FAC, lpc4_lsf[i]) +
1052
20.2M
                              fMult(ONE_BFI_FAC, lsf_mean));
1053
20.2M
  }
1054
1055
  /* LPC2 - LPC4 */
1056
5.05M
  for (j = 2; j <= 4; j++) {
1057
64.5M
    for (i = 0; i < M_LP_FILTER_ORDER; i++) {
1058
      /* lsf_mean[i] =  FX_DBL2FX_LPC(fMult((FIXP_LPC)(BETA + j *
1059
         FL2FXCONST_LPC(0.1f)), fdk_dec_lsf_init[i])
1060
                                    + fMult((FIXP_LPC)(ONE_BETA - j *
1061
         FL2FXCONST_LPC(0.1f)), lsf_adaptive_mean[i])); */
1062
1063
60.7M
      FIXP_LPC lsf_mean = FX_DBL2FX_LPC(
1064
60.7M
          fMult((FIXP_SGL)(BETA + (FIXP_SGL)(j * (INT)FL2FXCONST_SGL(0.1f))),
1065
60.7M
                (FIXP_SGL)fdk_dec_lsf_init[i]) +
1066
60.7M
          fMult(
1067
60.7M
              (FIXP_SGL)(ONE_BETA - (FIXP_SGL)(j * (INT)FL2FXCONST_SGL(0.1f))),
1068
60.7M
              lsf_adaptive_mean[i]));
1069
1070
60.7M
      lsp[j][i] = FX_DBL2FX_LPC(fMult(BFI_FAC, lsp[j - 1][i]) +
1071
60.7M
                                fMult(ONE_BFI_FAC, lsf_mean));
1072
60.7M
    }
1073
3.79M
  }
1074
1075
  /* Update past values for the future */
1076
21.5M
  for (i = 0; i < M_LP_FILTER_ORDER; i++) {
1077
20.2M
    lpc4_lsf[i] = lsp[4][i];
1078
20.2M
  }
1079
1080
  /* convert into LSP domain */
1081
7.58M
  for (j = 0; j < 5; j++) {
1082
107M
    for (i = 0; i < M_LP_FILTER_ORDER; i++) {
1083
101M
      lsp[j][i] = FX_DBL2FX_LPC(fixp_cos(
1084
101M
          fMult(lsp[j][i], FL2FXCONST_SGL((1 << LSPARG_SCALE) * M_PI / 6400.0)),
1085
101M
          LSF_SCALE - LSPARG_SCALE));
1086
101M
    }
1087
6.32M
  }
1088
1.26M
}
1089
1090
829k
void E_LPC_a_weight(FIXP_LPC *wA, const FIXP_LPC *A, int m) {
1091
829k
  FIXP_DBL f;
1092
829k
  int i;
1093
1094
829k
  f = FL2FXCONST_DBL(0.92f);
1095
14.1M
  for (i = 0; i < m; i++) {
1096
13.2M
    wA[i] = FX_DBL2FX_LPC(fMult(A[i], f));
1097
13.2M
    f = fMult(f, FL2FXCONST_DBL(0.92f));
1098
13.2M
  }
1099
829k
}
1100
1101
335k
void CLpd_DecodeGain(FIXP_DBL *gain, INT *gain_e, int gain_code) {
1102
  /* gain * 2^(gain_e) = 10^(gain_code/28) */
1103
335k
  *gain = fLdPow(
1104
335k
      FL2FXCONST_DBL(3.3219280948873623478703194294894 / 4.0), /* log2(10)*/
1105
335k
      2,
1106
335k
      fMultDiv2((FIXP_DBL)gain_code << (DFRACT_BITS - 1 - 7),
1107
335k
                FL2FXCONST_DBL(2.0f / 28.0f)),
1108
335k
      7, gain_e);
1109
335k
}
1110
1111
  /**
1112
   * \brief *   Find the polynomial F1(z) or F2(z) from the LSPs.
1113
   * This is performed by expanding the product polynomials:
1114
   *
1115
   * F1(z) =   product   ( 1 - 2 LSP_i z^-1 + z^-2 )
1116
   *         i=0,2,4,6,8
1117
   * F2(z) =   product   ( 1 - 2 LSP_i z^-1 + z^-2 )
1118
   *         i=1,3,5,7,9
1119
   *
1120
   * where LSP_i are the LSPs in the cosine domain.
1121
   * R.A.Salami    October 1990
1122
   * \param lsp input, line spectral freq. (cosine domain)
1123
   * \param f output, the coefficients of F1 or F2, scaled by 8 bits
1124
   * \param n no of coefficients (m/2)
1125
   * \param flag 1 : F1(z) ; 2 : F2(z)
1126
   */
1127
1128
50.6M
#define SF_F 8
1129
1130
5.95M
static void get_lsppol(FIXP_LPC lsp[], FIXP_DBL f[], int n, int flag) {
1131
5.95M
  FIXP_DBL b;
1132
5.95M
  FIXP_LPC *plsp;
1133
5.95M
  int i, j;
1134
1135
5.95M
  plsp = lsp + flag - 1;
1136
5.95M
  f[0] = FL2FXCONST_DBL(1.0f / (1 << SF_F));
1137
5.95M
  b = -FX_LPC2FX_DBL(*plsp);
1138
5.95M
  f[1] = b >> (SF_F - 1);
1139
47.6M
  for (i = 2; i <= n; i++) {
1140
41.7M
    plsp += 2;
1141
41.7M
    b = -FX_LPC2FX_DBL(*plsp);
1142
41.7M
    f[i] = SATURATE_LEFT_SHIFT((fMultDiv2(b, f[i - 1]) + (f[i - 2] >> 1)), 2,
1143
41.7M
                               DFRACT_BITS);
1144
166M
    for (j = i - 1; j > 1; j--) {
1145
125M
      f[j] = SATURATE_LEFT_SHIFT(
1146
125M
          ((f[j] >> 2) + fMultDiv2(b, f[j - 1]) + (f[j - 2] >> 2)), 2,
1147
125M
          DFRACT_BITS);
1148
125M
    }
1149
41.7M
    f[1] = f[1] + (b >> (SF_F - 1));
1150
41.7M
  }
1151
5.95M
  return;
1152
5.95M
}
1153
1154
41.7M
#define NC M_LP_FILTER_ORDER / 2
1155
1156
/**
1157
 * \brief lsp input LSP vector
1158
 * \brief a output LP filter coefficient vector scaled by SF_A_COEFFS.
1159
 */
1160
2.97M
void E_LPC_f_lsp_a_conversion(FIXP_LPC *lsp, FIXP_LPC *a, INT *a_exp) {
1161
2.97M
  FIXP_DBL f1[NC + 1], f2[NC + 1];
1162
2.97M
  int i, k;
1163
1164
  /*-----------------------------------------------------*
1165
   *  Find the polynomials F1(z) and F2(z)               *
1166
   *-----------------------------------------------------*/
1167
1168
2.97M
  get_lsppol(lsp, f1, NC, 1);
1169
2.97M
  get_lsppol(lsp, f2, NC, 2);
1170
1171
  /*-----------------------------------------------------*
1172
   *  Multiply F1(z) by (1+z^-1) and F2(z) by (1-z^-1)   *
1173
   *-----------------------------------------------------*/
1174
2.97M
  scaleValues(f1, NC + 1, -2);
1175
2.97M
  scaleValues(f2, NC + 1, -2);
1176
1177
26.8M
  for (i = NC; i > 0; i--) {
1178
23.8M
    f1[i] += f1[i - 1];
1179
23.8M
    f2[i] -= f2[i - 1];
1180
23.8M
  }
1181
1182
2.97M
  FIXP_DBL aDBL[M_LP_FILTER_ORDER];
1183
1184
26.8M
  for (i = 1, k = M_LP_FILTER_ORDER - 1; i <= NC; i++, k--) {
1185
23.8M
    aDBL[i - 1] = f1[i] + f2[i];
1186
23.8M
    aDBL[k] = f1[i] - f2[i];
1187
23.8M
  }
1188
1189
2.97M
  int headroom_a = getScalefactor(aDBL, M_LP_FILTER_ORDER);
1190
1191
50.6M
  for (i = 0; i < M_LP_FILTER_ORDER; i++) {
1192
47.6M
    a[i] = FX_DBL2FX_LPC(aDBL[i] << headroom_a);
1193
47.6M
  }
1194
1195
2.97M
  *a_exp = SF_F + (2 - 1) - headroom_a;
1196
2.97M
}