• 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 /**************************** AAC decoder library ******************************
96 
97    Author(s):
98 
99    Description: ACELP
100 
101 *******************************************************************************/
102 
103 #include "usacdec_ace_d4t64.h"
104 
105 #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  */
D_ACELP_add_pulse(SHORT pos[],SHORT nb_pulse,SHORT track,FIXP_COD code[])122 static void D_ACELP_add_pulse(SHORT pos[], SHORT nb_pulse, SHORT track,
123                               FIXP_COD code[]) {
124   SHORT i, k;
125   for (k = 0; k < nb_pulse; k++) {
126     /* i = ((pos[k] & (16-1))*NB_TRACK) + track; */
127     i = ((pos[k] & (16 - 1)) << 2) + track;
128     if ((pos[k] & 16) == 0) {
129       code[i] = code[i] + (FIXP_COD)(512 << (COD_BITS - FRACT_BITS));
130     } else {
131       code[i] = code[i] - (FIXP_COD)(512 << (COD_BITS - FRACT_BITS));
132     }
133   }
134   return;
135 }
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  */
D_ACELP_decode_1p_N1(LONG index,SHORT N,SHORT offset,SHORT pos[])151 static void D_ACELP_decode_1p_N1(LONG index, SHORT N, SHORT offset,
152                                  SHORT pos[]) {
153   SHORT pos1;
154   LONG i, mask;
155 
156   mask = ((1 << N) - 1);
157   /*
158    * Decode 1 pulse with N+1 bits
159    */
160   pos1 = (SHORT)((index & mask) + offset);
161   i = ((index >> N) & 1);
162   if (i == 1) {
163     pos1 += 16;
164   }
165   pos[0] = pos1;
166   return;
167 }
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  */
D_ACELP_decode_2p_2N1(LONG index,SHORT N,SHORT offset,SHORT pos[])183 static void D_ACELP_decode_2p_2N1(LONG index, SHORT N, SHORT offset,
184                                   SHORT pos[]) {
185   SHORT pos1, pos2;
186   LONG mask, i;
187   mask = ((1 << N) - 1);
188   /*
189    * Decode 2 pulses with 2*N+1 bits
190    */
191   pos1 = (SHORT)(((index >> N) & mask) + offset);
192   i = (index >> (2 * N)) & 1;
193   pos2 = (SHORT)((index & mask) + offset);
194   if ((pos2 - pos1) < 0) {
195     if (i == 1) {
196       pos1 += 16;
197     } else {
198       pos2 += 16;
199     }
200   } else {
201     if (i == 1) {
202       pos1 += 16;
203       pos2 += 16;
204     }
205   }
206   pos[0] = pos1;
207   pos[1] = pos2;
208   return;
209 }
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  */
D_ACELP_decode_3p_3N1(LONG index,SHORT N,SHORT offset,SHORT pos[])225 static void D_ACELP_decode_3p_3N1(LONG index, SHORT N, SHORT offset,
226                                   SHORT pos[]) {
227   SHORT j;
228   LONG mask, idx;
229 
230   /*
231    * Decode 3 pulses with 3*N+1 bits
232    */
233   mask = ((1 << ((2 * N) - 1)) - 1);
234   idx = index & mask;
235   j = offset;
236   if (((index >> ((2 * N) - 1)) & 1) == 1) {
237     j += (1 << (N - 1));
238   }
239   D_ACELP_decode_2p_2N1(idx, N - 1, j, pos);
240   mask = ((1 << (N + 1)) - 1);
241   idx = (index >> (2 * N)) & mask;
242   D_ACELP_decode_1p_N1(idx, N, offset, pos + 2);
243   return;
244 }
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  */
D_ACELP_decode_4p_4N1(LONG index,SHORT N,SHORT offset,SHORT pos[])260 static void D_ACELP_decode_4p_4N1(LONG index, SHORT N, SHORT offset,
261                                   SHORT pos[]) {
262   SHORT j;
263   LONG mask, idx;
264   /*
265    * Decode 4 pulses with 4*N+1 bits
266    */
267   mask = ((1 << ((2 * N) - 1)) - 1);
268   idx = index & mask;
269   j = offset;
270   if (((index >> ((2 * N) - 1)) & 1) == 1) {
271     j += (1 << (N - 1));
272   }
273   D_ACELP_decode_2p_2N1(idx, N - 1, j, pos);
274   mask = ((1 << ((2 * N) + 1)) - 1);
275   idx = (index >> (2 * N)) & mask;
276   D_ACELP_decode_2p_2N1(idx, N, offset, pos + 2);
277   return;
278 }
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  */
D_ACELP_decode_4p_4N(LONG index,SHORT N,SHORT offset,SHORT pos[])294 static void D_ACELP_decode_4p_4N(LONG index, SHORT N, SHORT offset,
295                                  SHORT pos[]) {
296   SHORT j, n_1;
297   /*
298    * Decode 4 pulses with 4*N bits
299    */
300   n_1 = N - 1;
301   j = offset + (1 << n_1);
302   switch ((index >> ((4 * N) - 2)) & 3) {
303     case 0:
304       if (((index >> ((4 * n_1) + 1)) & 1) == 0) {
305         D_ACELP_decode_4p_4N1(index, n_1, offset, pos);
306       } else {
307         D_ACELP_decode_4p_4N1(index, n_1, j, pos);
308       }
309       break;
310     case 1:
311       D_ACELP_decode_1p_N1((index >> ((3 * n_1) + 1)), n_1, offset, pos);
312       D_ACELP_decode_3p_3N1(index, n_1, j, pos + 1);
313       break;
314     case 2:
315       D_ACELP_decode_2p_2N1((index >> ((2 * n_1) + 1)), n_1, offset, pos);
316       D_ACELP_decode_2p_2N1(index, n_1, j, pos + 2);
317       break;
318     case 3:
319       D_ACELP_decode_3p_3N1((index >> (n_1 + 1)), n_1, offset, pos);
320       D_ACELP_decode_1p_N1(index, n_1, j, pos + 3);
321       break;
322   }
323   return;
324 }
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  */
D_ACELP_decode_4t64(SHORT index[],int nbits,FIXP_COD code[])356 void D_ACELP_decode_4t64(SHORT index[], int nbits, FIXP_COD code[]) {
357   LONG L_index;
358   SHORT k, pos[6];
359 
360   FDKmemclear(code, L_SUBFR * sizeof(FIXP_COD));
361 
362   /* decode the positions and signs of pulses and build the codeword */
363   switch (nbits) {
364     case 12:
365       for (k = 0; k < 4; k += 2) {
366         L_index = index[2 * (k / 2) + 1];
367         D_ACELP_decode_1p_N1(L_index, 4, 0, pos);
368         D_ACELP_add_pulse(pos, 1, 2 * (index[2 * (k / 2)]) + k / 2, code);
369       }
370       break;
371     case 16: {
372       int i = 0;
373       int offset = index[i++];
374       offset = (offset == 0) ? 1 : 3;
375       for (k = 0; k < 4; k++) {
376         if (k != offset) {
377           L_index = index[i++];
378           D_ACELP_decode_1p_N1(L_index, 4, 0, pos);
379           D_ACELP_add_pulse(pos, 1, k, code);
380         }
381       }
382     } break;
383     case 20:
384       for (k = 0; k < 4; k++) {
385         L_index = (LONG)index[k];
386         D_ACELP_decode_1p_N1(L_index, 4, 0, pos);
387         D_ACELP_add_pulse(pos, 1, k, code);
388       }
389       break;
390     case 28:
391       for (k = 0; k < 4 - 2; k++) {
392         L_index = (LONG)index[k];
393         D_ACELP_decode_2p_2N1(L_index, 4, 0, pos);
394         D_ACELP_add_pulse(pos, 2, k, code);
395       }
396       for (k = 2; k < 4; k++) {
397         L_index = (LONG)index[k];
398         D_ACELP_decode_1p_N1(L_index, 4, 0, pos);
399         D_ACELP_add_pulse(pos, 1, k, code);
400       }
401       break;
402     case 36:
403       for (k = 0; k < 4; k++) {
404         L_index = (LONG)index[k];
405         D_ACELP_decode_2p_2N1(L_index, 4, 0, pos);
406         D_ACELP_add_pulse(pos, 2, k, code);
407       }
408       break;
409     case 44:
410       for (k = 0; k < 4 - 2; k++) {
411         L_index = (LONG)index[k];
412         D_ACELP_decode_3p_3N1(L_index, 4, 0, pos);
413         D_ACELP_add_pulse(pos, 3, k, code);
414       }
415       for (k = 2; k < 4; k++) {
416         L_index = (LONG)index[k];
417         D_ACELP_decode_2p_2N1(L_index, 4, 0, pos);
418         D_ACELP_add_pulse(pos, 2, k, code);
419       }
420       break;
421     case 52:
422       for (k = 0; k < 4; k++) {
423         L_index = (LONG)index[k];
424         D_ACELP_decode_3p_3N1(L_index, 4, 0, pos);
425         D_ACELP_add_pulse(pos, 3, k, code);
426       }
427       break;
428     case 64:
429       for (k = 0; k < 4; k++) {
430         L_index = (((LONG)index[k] << 14) + (LONG)index[k + 4]);
431         D_ACELP_decode_4p_4N(L_index, 4, 0, pos);
432         D_ACELP_add_pulse(pos, 4, k, code);
433       }
434       break;
435     default:
436       FDK_ASSERT(0);
437   }
438   return;
439 }
440