Mercurial > mplayer.hg
annotate libfaad2/common.c @ 13032:2ba5eb44eabd
maybe more understandable?
author | michael |
---|---|
date | Tue, 17 Aug 2004 01:06:24 +0000 |
parents | d81145997036 |
children | 6d50ef45a058 |
rev | line source |
---|---|
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 ** | |
12625
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
25 ** Initially modified for use with MPlayer by Arpad Gereöffy on 2003/08/30 |
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
26 ** $Id: common.c,v 1.3 2004/06/02 22:59:02 diego Exp $ |
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ |
10725 | 28 **/ |
29 | |
30 /* just some common functions that could be used anywhere */ | |
31 | |
32 #include "common.h" | |
33 #include "structs.h" | |
34 | |
12527 | 35 #include <stdlib.h> |
10725 | 36 #include "syntax.h" |
37 | |
12527 | 38 #ifdef USE_SSE |
39 __declspec(naked) static int32_t __fastcall test_cpuid(void) | |
40 { | |
41 __asm | |
42 { | |
43 pushf | |
44 pop eax | |
45 mov ecx,eax | |
46 xor eax,(1<<21) | |
47 push eax | |
48 popf | |
49 pushf | |
50 pop eax | |
51 push ecx | |
52 popf | |
53 cmp eax,ecx | |
54 mov eax,0 | |
55 setne al | |
56 ret | |
57 } | |
58 } | |
59 | |
60 __declspec(naked) static void __fastcall run_cpuid(int32_t param, int32_t out[4]) | |
61 { | |
62 __asm | |
63 { | |
64 pushad | |
65 push edx | |
66 mov eax,ecx | |
67 cpuid | |
68 pop edi | |
69 mov [edi+0],eax | |
70 mov [edi+4],ebx | |
71 mov [edi+8],ecx | |
72 mov [edi+12],edx | |
73 popad | |
74 ret | |
75 } | |
76 } | |
77 | |
78 uint8_t cpu_has_sse() | |
79 { | |
80 int32_t features[4]; | |
81 | |
82 if (test_cpuid()) | |
83 { | |
84 run_cpuid(1, features); | |
85 } | |
86 | |
87 /* check for SSE */ | |
88 if (features[3] & 0x02000000) | |
89 return 1; | |
90 | |
91 return 0; | |
92 } | |
93 #else | |
94 uint8_t cpu_has_sse() | |
95 { | |
96 return 0; | |
97 } | |
98 #endif | |
99 | |
10725 | 100 /* Returns the sample rate index based on the samplerate */ |
12527 | 101 uint8_t get_sr_index(const uint32_t samplerate) |
10725 | 102 { |
103 if (92017 <= samplerate) return 0; | |
104 if (75132 <= samplerate) return 1; | |
105 if (55426 <= samplerate) return 2; | |
106 if (46009 <= samplerate) return 3; | |
107 if (37566 <= samplerate) return 4; | |
108 if (27713 <= samplerate) return 5; | |
109 if (23004 <= samplerate) return 6; | |
110 if (18783 <= samplerate) return 7; | |
111 if (13856 <= samplerate) return 8; | |
112 if (11502 <= samplerate) return 9; | |
113 if (9391 <= samplerate) return 10; | |
10989 | 114 if (16428320 <= samplerate) return 11; |
10725 | 115 |
116 return 11; | |
117 } | |
118 | |
10989 | 119 /* Returns the sample rate based on the sample rate index */ |
12527 | 120 uint32_t get_sample_rate(const uint8_t sr_index) |
10989 | 121 { |
122 static const uint32_t sample_rates[] = | |
123 { | |
124 96000, 88200, 64000, 48000, 44100, 32000, | |
125 24000, 22050, 16000, 12000, 11025, 8000 | |
126 }; | |
127 | |
128 if (sr_index < 12) | |
129 return sample_rates[sr_index]; | |
130 | |
131 return 0; | |
132 } | |
133 | |
12527 | 134 uint8_t max_pred_sfb(const uint8_t sr_index) |
135 { | |
136 static const uint8_t pred_sfb_max[] = | |
137 { | |
138 33, 33, 38, 40, 40, 40, 41, 41, 37, 37, 37, 34 | |
139 }; | |
140 | |
141 | |
142 if (sr_index < 12) | |
143 return pred_sfb_max[sr_index]; | |
144 | |
145 return 0; | |
146 } | |
147 | |
148 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type, | |
149 const uint8_t is_short) | |
150 { | |
151 /* entry for each sampling rate | |
152 * 1 Main/LC long window | |
153 * 2 Main/LC short window | |
154 * 3 SSR long window | |
155 * 4 SSR short window | |
156 */ | |
157 static const uint8_t tns_sbf_max[][4] = | |
158 { | |
159 {31, 9, 28, 7}, /* 96000 */ | |
160 {31, 9, 28, 7}, /* 88200 */ | |
161 {34, 10, 27, 7}, /* 64000 */ | |
162 {40, 14, 26, 6}, /* 48000 */ | |
163 {42, 14, 26, 6}, /* 44100 */ | |
164 {51, 14, 26, 6}, /* 32000 */ | |
165 {46, 14, 29, 7}, /* 24000 */ | |
166 {46, 14, 29, 7}, /* 22050 */ | |
167 {42, 14, 23, 8}, /* 16000 */ | |
168 {42, 14, 23, 8}, /* 12000 */ | |
169 {42, 14, 23, 8}, /* 11025 */ | |
170 {39, 14, 19, 7}, /* 8000 */ | |
171 {39, 14, 19, 7}, /* 7350 */ | |
172 {0,0,0,0}, | |
173 {0,0,0,0}, | |
174 {0,0,0,0} | |
175 }; | |
176 uint8_t i = 0; | |
177 | |
178 if (is_short) i++; | |
179 if (object_type == SSR) i += 2; | |
180 | |
181 return tns_sbf_max[sr_index][i]; | |
182 } | |
183 | |
10725 | 184 /* Returns 0 if an object type is decodable, otherwise returns -1 */ |
12527 | 185 int8_t can_decode_ot(const uint8_t object_type) |
10725 | 186 { |
187 switch (object_type) | |
188 { | |
189 case LC: | |
190 return 0; | |
191 case MAIN: | |
192 #ifdef MAIN_DEC | |
193 return 0; | |
194 #else | |
195 return -1; | |
196 #endif | |
197 case SSR: | |
198 #ifdef SSR_DEC | |
199 return 0; | |
200 #else | |
201 return -1; | |
202 #endif | |
203 case LTP: | |
204 #ifdef LTP_DEC | |
205 return 0; | |
206 #else | |
207 return -1; | |
208 #endif | |
209 | |
210 /* ER object types */ | |
211 #ifdef ERROR_RESILIENCE | |
212 case ER_LC: | |
213 #ifdef DRM | |
214 case DRM_ER_LC: | |
215 #endif | |
216 return 0; | |
217 case ER_LTP: | |
218 #ifdef LTP_DEC | |
219 return 0; | |
220 #else | |
221 return -1; | |
222 #endif | |
223 case LD: | |
224 #ifdef LD_DEC | |
225 return 0; | |
226 #else | |
227 return -1; | |
228 #endif | |
229 #endif | |
230 } | |
231 | |
232 return -1; | |
233 } | |
234 | |
12527 | 235 /* common malloc function */ |
236 void *faad_malloc(int32_t size) | |
237 { | |
238 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE) | |
239 return _aligned_malloc(size, 16); | |
240 #else | |
241 return malloc(size); | |
242 #endif | |
243 } | |
244 | |
245 /* common free function */ | |
246 void faad_free(void *b) | |
247 { | |
248 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE) | |
249 _aligned_free(b); | |
250 #else | |
251 free(b); | |
252 #endif | |
253 } | |
254 | |
10725 | 255 static const uint8_t Parity [256] = { // parity |
256 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, | |
257 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, | |
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 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, | |
261 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, | |
262 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, | |
263 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 | |
264 }; | |
265 | |
266 static uint32_t __r1 = 1; | |
267 static uint32_t __r2 = 1; | |
268 | |
269 | |
270 /* | |
271 * This is a simple random number generator with good quality for audio purposes. | |
272 * It consists of two polycounters with opposite rotation direction and different | |
273 * periods. The periods are coprime, so the total period is the product of both. | |
274 * | |
275 * ------------------------------------------------------------------------------------------------- | |
276 * +-> |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| | |
277 * | ------------------------------------------------------------------------------------------------- | |
278 * | | | | | | | | |
279 * | +--+--+--+-XOR-+--------+ | |
280 * | | | |
281 * +--------------------------------------------------------------------------------------+ | |
282 * | |
283 * ------------------------------------------------------------------------------------------------- | |
284 * |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| <-+ | |
285 * ------------------------------------------------------------------------------------------------- | | |
286 * | | | | | | |
287 * +--+----XOR----+--+ | | |
288 * | | | |
289 * +----------------------------------------------------------------------------------------+ | |
290 * | |
291 * | |
292 * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481, | |
293 * which gives a period of 18.410.713.077.675.721.215. The result is the | |
294 * XORed values of both generators. | |
295 */ | |
296 uint32_t random_int(void) | |
297 { | |
298 uint32_t t1, t2, t3, t4; | |
299 | |
300 t3 = t1 = __r1; t4 = t2 = __r2; // Parity calculation is done via table lookup, this is also available | |
301 t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable | |
302 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations. | |
303 t1 <<= 31; t2 = Parity [t2]; | |
304 | |
305 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 ); | |
306 } |