10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
12527
|
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
|
10725
|
4 **
|
|
5 ** This program is free software; you can redistribute it and/or modify
|
|
6 ** it under the terms of the GNU General Public License as published by
|
|
7 ** the Free Software Foundation; either version 2 of the License, or
|
|
8 ** (at your option) any later version.
|
|
9 **
|
|
10 ** This program is distributed in the hope that it will be useful,
|
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 ** GNU General Public License for more details.
|
|
14 **
|
|
15 ** You should have received a copy of the GNU General Public License
|
|
16 ** along with this program; if not, write to the Free Software
|
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 **
|
|
19 ** Any non-GPL usage of this software or parts of this software is strictly
|
|
20 ** forbidden.
|
|
21 **
|
|
22 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
12527
|
25 ** $Id: common.c,v 1.2 2003/10/03 22:22:27 alex Exp $
|
10725
|
26 **/
|
|
27
|
|
28 /* just some common functions that could be used anywhere */
|
|
29
|
|
30 #include "common.h"
|
|
31 #include "structs.h"
|
|
32
|
12527
|
33 #include <stdlib.h>
|
10725
|
34 #include "syntax.h"
|
|
35
|
12527
|
36 #ifdef USE_SSE
|
|
37 __declspec(naked) static int32_t __fastcall test_cpuid(void)
|
|
38 {
|
|
39 __asm
|
|
40 {
|
|
41 pushf
|
|
42 pop eax
|
|
43 mov ecx,eax
|
|
44 xor eax,(1<<21)
|
|
45 push eax
|
|
46 popf
|
|
47 pushf
|
|
48 pop eax
|
|
49 push ecx
|
|
50 popf
|
|
51 cmp eax,ecx
|
|
52 mov eax,0
|
|
53 setne al
|
|
54 ret
|
|
55 }
|
|
56 }
|
|
57
|
|
58 __declspec(naked) static void __fastcall run_cpuid(int32_t param, int32_t out[4])
|
|
59 {
|
|
60 __asm
|
|
61 {
|
|
62 pushad
|
|
63 push edx
|
|
64 mov eax,ecx
|
|
65 cpuid
|
|
66 pop edi
|
|
67 mov [edi+0],eax
|
|
68 mov [edi+4],ebx
|
|
69 mov [edi+8],ecx
|
|
70 mov [edi+12],edx
|
|
71 popad
|
|
72 ret
|
|
73 }
|
|
74 }
|
|
75
|
|
76 uint8_t cpu_has_sse()
|
|
77 {
|
|
78 int32_t features[4];
|
|
79
|
|
80 if (test_cpuid())
|
|
81 {
|
|
82 run_cpuid(1, features);
|
|
83 }
|
|
84
|
|
85 /* check for SSE */
|
|
86 if (features[3] & 0x02000000)
|
|
87 return 1;
|
|
88
|
|
89 return 0;
|
|
90 }
|
|
91 #else
|
|
92 uint8_t cpu_has_sse()
|
|
93 {
|
|
94 return 0;
|
|
95 }
|
|
96 #endif
|
|
97
|
10725
|
98 /* Returns the sample rate index based on the samplerate */
|
12527
|
99 uint8_t get_sr_index(const uint32_t samplerate)
|
10725
|
100 {
|
|
101 if (92017 <= samplerate) return 0;
|
|
102 if (75132 <= samplerate) return 1;
|
|
103 if (55426 <= samplerate) return 2;
|
|
104 if (46009 <= samplerate) return 3;
|
|
105 if (37566 <= samplerate) return 4;
|
|
106 if (27713 <= samplerate) return 5;
|
|
107 if (23004 <= samplerate) return 6;
|
|
108 if (18783 <= samplerate) return 7;
|
|
109 if (13856 <= samplerate) return 8;
|
|
110 if (11502 <= samplerate) return 9;
|
|
111 if (9391 <= samplerate) return 10;
|
10989
|
112 if (16428320 <= samplerate) return 11;
|
10725
|
113
|
|
114 return 11;
|
|
115 }
|
|
116
|
10989
|
117 /* Returns the sample rate based on the sample rate index */
|
12527
|
118 uint32_t get_sample_rate(const uint8_t sr_index)
|
10989
|
119 {
|
|
120 static const uint32_t sample_rates[] =
|
|
121 {
|
|
122 96000, 88200, 64000, 48000, 44100, 32000,
|
|
123 24000, 22050, 16000, 12000, 11025, 8000
|
|
124 };
|
|
125
|
|
126 if (sr_index < 12)
|
|
127 return sample_rates[sr_index];
|
|
128
|
|
129 return 0;
|
|
130 }
|
|
131
|
12527
|
132 uint8_t max_pred_sfb(const uint8_t sr_index)
|
|
133 {
|
|
134 static const uint8_t pred_sfb_max[] =
|
|
135 {
|
|
136 33, 33, 38, 40, 40, 40, 41, 41, 37, 37, 37, 34
|
|
137 };
|
|
138
|
|
139
|
|
140 if (sr_index < 12)
|
|
141 return pred_sfb_max[sr_index];
|
|
142
|
|
143 return 0;
|
|
144 }
|
|
145
|
|
146 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type,
|
|
147 const uint8_t is_short)
|
|
148 {
|
|
149 /* entry for each sampling rate
|
|
150 * 1 Main/LC long window
|
|
151 * 2 Main/LC short window
|
|
152 * 3 SSR long window
|
|
153 * 4 SSR short window
|
|
154 */
|
|
155 static const uint8_t tns_sbf_max[][4] =
|
|
156 {
|
|
157 {31, 9, 28, 7}, /* 96000 */
|
|
158 {31, 9, 28, 7}, /* 88200 */
|
|
159 {34, 10, 27, 7}, /* 64000 */
|
|
160 {40, 14, 26, 6}, /* 48000 */
|
|
161 {42, 14, 26, 6}, /* 44100 */
|
|
162 {51, 14, 26, 6}, /* 32000 */
|
|
163 {46, 14, 29, 7}, /* 24000 */
|
|
164 {46, 14, 29, 7}, /* 22050 */
|
|
165 {42, 14, 23, 8}, /* 16000 */
|
|
166 {42, 14, 23, 8}, /* 12000 */
|
|
167 {42, 14, 23, 8}, /* 11025 */
|
|
168 {39, 14, 19, 7}, /* 8000 */
|
|
169 {39, 14, 19, 7}, /* 7350 */
|
|
170 {0,0,0,0},
|
|
171 {0,0,0,0},
|
|
172 {0,0,0,0}
|
|
173 };
|
|
174 uint8_t i = 0;
|
|
175
|
|
176 if (is_short) i++;
|
|
177 if (object_type == SSR) i += 2;
|
|
178
|
|
179 return tns_sbf_max[sr_index][i];
|
|
180 }
|
|
181
|
10725
|
182 /* Returns 0 if an object type is decodable, otherwise returns -1 */
|
12527
|
183 int8_t can_decode_ot(const uint8_t object_type)
|
10725
|
184 {
|
|
185 switch (object_type)
|
|
186 {
|
|
187 case LC:
|
|
188 return 0;
|
|
189 case MAIN:
|
|
190 #ifdef MAIN_DEC
|
|
191 return 0;
|
|
192 #else
|
|
193 return -1;
|
|
194 #endif
|
|
195 case SSR:
|
|
196 #ifdef SSR_DEC
|
|
197 return 0;
|
|
198 #else
|
|
199 return -1;
|
|
200 #endif
|
|
201 case LTP:
|
|
202 #ifdef LTP_DEC
|
|
203 return 0;
|
|
204 #else
|
|
205 return -1;
|
|
206 #endif
|
|
207
|
|
208 /* ER object types */
|
|
209 #ifdef ERROR_RESILIENCE
|
|
210 case ER_LC:
|
|
211 #ifdef DRM
|
|
212 case DRM_ER_LC:
|
|
213 #endif
|
|
214 return 0;
|
|
215 case ER_LTP:
|
|
216 #ifdef LTP_DEC
|
|
217 return 0;
|
|
218 #else
|
|
219 return -1;
|
|
220 #endif
|
|
221 case LD:
|
|
222 #ifdef LD_DEC
|
|
223 return 0;
|
|
224 #else
|
|
225 return -1;
|
|
226 #endif
|
|
227 #endif
|
|
228 }
|
|
229
|
|
230 return -1;
|
|
231 }
|
|
232
|
12527
|
233 /* common malloc function */
|
|
234 void *faad_malloc(int32_t size)
|
|
235 {
|
|
236 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE)
|
|
237 return _aligned_malloc(size, 16);
|
|
238 #else
|
|
239 return malloc(size);
|
|
240 #endif
|
|
241 }
|
|
242
|
|
243 /* common free function */
|
|
244 void faad_free(void *b)
|
|
245 {
|
|
246 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE)
|
|
247 _aligned_free(b);
|
|
248 #else
|
|
249 free(b);
|
|
250 #endif
|
|
251 }
|
|
252
|
10725
|
253 static const uint8_t Parity [256] = { // parity
|
|
254 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
|
|
255 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
|
|
256 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
|
|
257 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
|
|
258 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
|
|
259 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
|
|
260 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
|
|
261 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0
|
|
262 };
|
|
263
|
|
264 static uint32_t __r1 = 1;
|
|
265 static uint32_t __r2 = 1;
|
|
266
|
|
267
|
|
268 /*
|
|
269 * This is a simple random number generator with good quality for audio purposes.
|
|
270 * It consists of two polycounters with opposite rotation direction and different
|
|
271 * periods. The periods are coprime, so the total period is the product of both.
|
|
272 *
|
|
273 * -------------------------------------------------------------------------------------------------
|
|
274 * +-> |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0|
|
|
275 * | -------------------------------------------------------------------------------------------------
|
|
276 * | | | | | | |
|
|
277 * | +--+--+--+-XOR-+--------+
|
|
278 * | |
|
|
279 * +--------------------------------------------------------------------------------------+
|
|
280 *
|
|
281 * -------------------------------------------------------------------------------------------------
|
|
282 * |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0| <-+
|
|
283 * ------------------------------------------------------------------------------------------------- |
|
|
284 * | | | | |
|
|
285 * +--+----XOR----+--+ |
|
|
286 * | |
|
|
287 * +----------------------------------------------------------------------------------------+
|
|
288 *
|
|
289 *
|
|
290 * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481,
|
|
291 * which gives a period of 18.410.713.077.675.721.215. The result is the
|
|
292 * XORed values of both generators.
|
|
293 */
|
|
294 uint32_t random_int(void)
|
|
295 {
|
|
296 uint32_t t1, t2, t3, t4;
|
|
297
|
|
298 t3 = t1 = __r1; t4 = t2 = __r2; // Parity calculation is done via table lookup, this is also available
|
|
299 t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable
|
|
300 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations.
|
|
301 t1 <<= 31; t2 = Parity [t2];
|
|
302
|
|
303 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
|
|
304 }
|