Coverage Report

Created: 2025-10-13 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libAACdec/src/usacdec_ace_d4t64.cpp
Line
Count
Source
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
/**************************** AAC decoder library ******************************
96
97
   Author(s):
98
99
   Description: ACELP
100
101
*******************************************************************************/
102
103
#include "usacdec_ace_d4t64.h"
104
105
356k
#define L_SUBFR 64 /* Subframe size              */
106
107
/*
108
 * D_ACELP_add_pulse
109
 *
110
 * Parameters:
111
 *    pos         I: position of pulse
112
 *    nb_pulse    I: number of pulses
113
 *    track       I: track
114
 *    code        O: fixed codebook
115
 *
116
 * Function:
117
 *    Add pulses to fixed codebook
118
 *
119
 * Returns:
120
 *    void
121
 */
122
static void D_ACELP_add_pulse(SHORT pos[], SHORT nb_pulse, SHORT track,
123
1.25M
                              FIXP_COD code[]) {
124
1.25M
  SHORT i, k;
125
3.24M
  for (k = 0; k < nb_pulse; k++) {
126
    /* i = ((pos[k] & (16-1))*NB_TRACK) + track; */
127
1.98M
    i = ((pos[k] & (16 - 1)) << 2) + track;
128
1.98M
    if ((pos[k] & 16) == 0) {
129
1.25M
      code[i] = code[i] + (FIXP_COD)(512 << (COD_BITS - FRACT_BITS));
130
1.25M
    } else {
131
734k
      code[i] = code[i] - (FIXP_COD)(512 << (COD_BITS - FRACT_BITS));
132
734k
    }
133
1.98M
  }
134
1.25M
  return;
135
1.25M
}
136
/*
137
 * D_ACELP_decode_1p_N1
138
 *
139
 * Parameters:
140
 *    index    I: pulse index
141
 *    N        I: number of bits for position
142
 *    offset   I: offset
143
 *    pos      O: position of the pulse
144
 *
145
 * Function:
146
 *    Decode 1 pulse with N+1 bits
147
 *
148
 * Returns:
149
 *    void
150
 */
151
static void D_ACELP_decode_1p_N1(LONG index, SHORT N, SHORT offset,
152
1.00M
                                 SHORT pos[]) {
153
1.00M
  SHORT pos1;
154
1.00M
  LONG i, mask;
155
156
1.00M
  mask = ((1 << N) - 1);
157
  /*
158
   * Decode 1 pulse with N+1 bits
159
   */
160
1.00M
  pos1 = (SHORT)((index & mask) + offset);
161
1.00M
  i = ((index >> N) & 1);
162
1.00M
  if (i == 1) {
163
357k
    pos1 += 16;
164
357k
  }
165
1.00M
  pos[0] = pos1;
166
1.00M
  return;
167
1.00M
}
168
/*
169
 * D_ACELP_decode_2p_2N1
170
 *
171
 * Parameters:
172
 *    index    I: pulse index
173
 *    N        I: number of bits for position
174
 *    offset   I: offset
175
 *    pos      O: position of the pulse
176
 *
177
 * Function:
178
 *    Decode 2 pulses with 2*N+1 bits
179
 *
180
 * Returns:
181
 *    void
182
 */
183
static void D_ACELP_decode_2p_2N1(LONG index, SHORT N, SHORT offset,
184
492k
                                  SHORT pos[]) {
185
492k
  SHORT pos1, pos2;
186
492k
  LONG mask, i;
187
492k
  mask = ((1 << N) - 1);
188
  /*
189
   * Decode 2 pulses with 2*N+1 bits
190
   */
191
492k
  pos1 = (SHORT)(((index >> N) & mask) + offset);
192
492k
  i = (index >> (2 * N)) & 1;
193
492k
  pos2 = (SHORT)((index & mask) + offset);
194
492k
  if ((pos2 - pos1) < 0) {
195
163k
    if (i == 1) {
196
78.9k
      pos1 += 16;
197
85.0k
    } else {
198
85.0k
      pos2 += 16;
199
85.0k
    }
200
328k
  } else {
201
328k
    if (i == 1) {
202
106k
      pos1 += 16;
203
106k
      pos2 += 16;
204
106k
    }
205
328k
  }
206
492k
  pos[0] = pos1;
207
492k
  pos[1] = pos2;
208
492k
  return;
209
492k
}
210
/*
211
 * D_ACELP_decode_3p_3N1
212
 *
213
 * Parameters:
214
 *    index    I: pulse index
215
 *    N        I: number of bits for position
216
 *    offset   I: offset
217
 *    pos      O: position of the pulse
218
 *
219
 * Function:
220
 *    Decode 3 pulses with 3*N+1 bits
221
 *
222
 * Returns:
223
 *    void
224
 */
225
static void D_ACELP_decode_3p_3N1(LONG index, SHORT N, SHORT offset,
226
91.2k
                                  SHORT pos[]) {
227
91.2k
  SHORT j;
228
91.2k
  LONG mask, idx;
229
230
  /*
231
   * Decode 3 pulses with 3*N+1 bits
232
   */
233
91.2k
  mask = ((1 << ((2 * N) - 1)) - 1);
234
91.2k
  idx = index & mask;
235
91.2k
  j = offset;
236
91.2k
  if (((index >> ((2 * N) - 1)) & 1) == 1) {
237
37.8k
    j += (1 << (N - 1));
238
37.8k
  }
239
91.2k
  D_ACELP_decode_2p_2N1(idx, N - 1, j, pos);
240
91.2k
  mask = ((1 << (N + 1)) - 1);
241
91.2k
  idx = (index >> (2 * N)) & mask;
242
91.2k
  D_ACELP_decode_1p_N1(idx, N, offset, pos + 2);
243
91.2k
  return;
244
91.2k
}
245
/*
246
 * D_ACELP_decode_4p_4N1
247
 *
248
 * Parameters:
249
 *    index    I: pulse index
250
 *    N        I: number of bits for position
251
 *    offset   I: offset
252
 *    pos      O: position of the pulse
253
 *
254
 * Function:
255
 *    Decode 4 pulses with 4*N+1 bits
256
 *
257
 * Returns:
258
 *    void
259
 */
260
static void D_ACELP_decode_4p_4N1(LONG index, SHORT N, SHORT offset,
261
73.3k
                                  SHORT pos[]) {
262
73.3k
  SHORT j;
263
73.3k
  LONG mask, idx;
264
  /*
265
   * Decode 4 pulses with 4*N+1 bits
266
   */
267
73.3k
  mask = ((1 << ((2 * N) - 1)) - 1);
268
73.3k
  idx = index & mask;
269
73.3k
  j = offset;
270
73.3k
  if (((index >> ((2 * N) - 1)) & 1) == 1) {
271
24.2k
    j += (1 << (N - 1));
272
24.2k
  }
273
73.3k
  D_ACELP_decode_2p_2N1(idx, N - 1, j, pos);
274
73.3k
  mask = ((1 << ((2 * N) + 1)) - 1);
275
73.3k
  idx = (index >> (2 * N)) & mask;
276
73.3k
  D_ACELP_decode_2p_2N1(idx, N, offset, pos + 2);
277
73.3k
  return;
278
73.3k
}
279
/*
280
 * D_ACELP_decode_4p_4N
281
 *
282
 * Parameters:
283
 *    index    I: pulse index
284
 *    N        I: number of bits for position
285
 *    offset   I: offset
286
 *    pos      O: position of the pulse
287
 *
288
 * Function:
289
 *    Decode 4 pulses with 4*N bits
290
 *
291
 * Returns:
292
 *    void
293
 */
294
static void D_ACELP_decode_4p_4N(LONG index, SHORT N, SHORT offset,
295
149k
                                 SHORT pos[]) {
296
149k
  SHORT j, n_1;
297
  /*
298
   * Decode 4 pulses with 4*N bits
299
   */
300
149k
  n_1 = N - 1;
301
149k
  j = offset + (1 << n_1);
302
149k
  switch ((index >> ((4 * N) - 2)) & 3) {
303
73.3k
    case 0:
304
73.3k
      if (((index >> ((4 * n_1) + 1)) & 1) == 0) {
305
50.0k
        D_ACELP_decode_4p_4N1(index, n_1, offset, pos);
306
50.0k
      } else {
307
23.2k
        D_ACELP_decode_4p_4N1(index, n_1, j, pos);
308
23.2k
      }
309
73.3k
      break;
310
28.4k
    case 1:
311
28.4k
      D_ACELP_decode_1p_N1((index >> ((3 * n_1) + 1)), n_1, offset, pos);
312
28.4k
      D_ACELP_decode_3p_3N1(index, n_1, j, pos + 1);
313
28.4k
      break;
314
25.6k
    case 2:
315
25.6k
      D_ACELP_decode_2p_2N1((index >> ((2 * n_1) + 1)), n_1, offset, pos);
316
25.6k
      D_ACELP_decode_2p_2N1(index, n_1, j, pos + 2);
317
25.6k
      break;
318
22.0k
    case 3:
319
22.0k
      D_ACELP_decode_3p_3N1((index >> (n_1 + 1)), n_1, offset, pos);
320
22.0k
      D_ACELP_decode_1p_N1(index, n_1, j, pos + 3);
321
22.0k
      break;
322
149k
  }
323
149k
  return;
324
149k
}
325
326
/*
327
 * D_ACELP_decode_4t
328
 *
329
 * Parameters:
330
 *    index          I: index
331
 *    mode           I: speech mode
332
 *    code           I: (Q9) algebraic (fixed) codebook excitation
333
 *
334
 * Function:
335
 *    20, 36, 44, 52, 64, 72, 88 bits algebraic codebook.
336
 *    4 tracks x 16 positions per track = 64 samples.
337
 *
338
 *    20 bits 5+5+5+5 --> 4 pulses in a frame of 64 samples.
339
 *    36 bits 9+9+9+9 --> 8 pulses in a frame of 64 samples.
340
 *    44 bits 13+9+13+9 --> 10 pulses in a frame of 64 samples.
341
 *    52 bits 13+13+13+13 --> 12 pulses in a frame of 64 samples.
342
 *    64 bits 2+2+2+2+14+14+14+14 --> 16 pulses in a frame of 64 samples.
343
 *    72 bits 10+2+10+2+10+14+10+14 --> 18 pulses in a frame of 64 samples.
344
 *    88 bits 11+11+11+11+11+11+11+11 --> 24 pulses in a frame of 64 samples.
345
 *
346
 *    All pulses can have two (2) possible amplitudes: +1 or -1.
347
 *    Each pulse can sixteen (16) possible positions.
348
 *
349
 *    codevector length    64
350
 *    number of track      4
351
 *    number of position   16
352
 *
353
 * Returns:
354
 *    void
355
 */
356
356k
void D_ACELP_decode_4t64(SHORT index[], int nbits, FIXP_COD code[]) {
357
356k
  LONG L_index;
358
356k
  SHORT k, pos[6];
359
360
356k
  FDKmemclear(code, L_SUBFR * sizeof(FIXP_COD));
361
362
  /* decode the positions and signs of pulses and build the codeword */
363
356k
  switch (nbits) {
364
38.8k
    case 12:
365
116k
      for (k = 0; k < 4; k += 2) {
366
77.6k
        L_index = index[2 * (k / 2) + 1];
367
77.6k
        D_ACELP_decode_1p_N1(L_index, 4, 0, pos);
368
77.6k
        D_ACELP_add_pulse(pos, 1, 2 * (index[2 * (k / 2)]) + k / 2, code);
369
77.6k
      }
370
38.8k
      break;
371
90.8k
    case 16: {
372
90.8k
      int i = 0;
373
90.8k
      int offset = index[i++];
374
90.8k
      offset = (offset == 0) ? 1 : 3;
375
454k
      for (k = 0; k < 4; k++) {
376
363k
        if (k != offset) {
377
272k
          L_index = index[i++];
378
272k
          D_ACELP_decode_1p_N1(L_index, 4, 0, pos);
379
272k
          D_ACELP_add_pulse(pos, 1, k, code);
380
272k
        }
381
363k
      }
382
90.8k
    } break;
383
114k
    case 20:
384
573k
      for (k = 0; k < 4; k++) {
385
458k
        L_index = (LONG)index[k];
386
458k
        D_ACELP_decode_1p_N1(L_index, 4, 0, pos);
387
458k
        D_ACELP_add_pulse(pos, 1, k, code);
388
458k
      }
389
114k
      break;
390
27.0k
    case 28:
391
81.0k
      for (k = 0; k < 4 - 2; k++) {
392
54.0k
        L_index = (LONG)index[k];
393
54.0k
        D_ACELP_decode_2p_2N1(L_index, 4, 0, pos);
394
54.0k
        D_ACELP_add_pulse(pos, 2, k, code);
395
54.0k
      }
396
81.0k
      for (k = 2; k < 4; k++) {
397
54.0k
        L_index = (LONG)index[k];
398
54.0k
        D_ACELP_decode_1p_N1(L_index, 4, 0, pos);
399
54.0k
        D_ACELP_add_pulse(pos, 1, k, code);
400
54.0k
      }
401
27.0k
      break;
402
35.0k
    case 36:
403
175k
      for (k = 0; k < 4; k++) {
404
140k
        L_index = (LONG)index[k];
405
140k
        D_ACELP_decode_2p_2N1(L_index, 4, 0, pos);
406
140k
        D_ACELP_add_pulse(pos, 2, k, code);
407
140k
      }
408
35.0k
      break;
409
4.19k
    case 44:
410
12.5k
      for (k = 0; k < 4 - 2; k++) {
411
8.38k
        L_index = (LONG)index[k];
412
8.38k
        D_ACELP_decode_3p_3N1(L_index, 4, 0, pos);
413
8.38k
        D_ACELP_add_pulse(pos, 3, k, code);
414
8.38k
      }
415
12.5k
      for (k = 2; k < 4; k++) {
416
8.38k
        L_index = (LONG)index[k];
417
8.38k
        D_ACELP_decode_2p_2N1(L_index, 4, 0, pos);
418
8.38k
        D_ACELP_add_pulse(pos, 2, k, code);
419
8.38k
      }
420
4.19k
      break;
421
8.08k
    case 52:
422
40.4k
      for (k = 0; k < 4; k++) {
423
32.3k
        L_index = (LONG)index[k];
424
32.3k
        D_ACELP_decode_3p_3N1(L_index, 4, 0, pos);
425
32.3k
        D_ACELP_add_pulse(pos, 3, k, code);
426
32.3k
      }
427
8.08k
      break;
428
37.4k
    case 64:
429
187k
      for (k = 0; k < 4; k++) {
430
149k
        L_index = (((LONG)index[k] << 14) + (LONG)index[k + 4]);
431
149k
        D_ACELP_decode_4p_4N(L_index, 4, 0, pos);
432
149k
        D_ACELP_add_pulse(pos, 4, k, code);
433
149k
      }
434
37.4k
      break;
435
0
    default:
436
0
      FDK_ASSERT(0);
437
356k
  }
438
356k
  return;
439
356k
}