1 | /* |
2 | * libmad - MPEG audio decoder library |
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc. |
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 | * If you would like to negotiate alternate licensing terms, you may do |
20 | * so by contacting: Underbit Technologies, Inc. <info@underbit.com> |
21 | */ |
22 | |
23 | # ifdef __cplusplus |
24 | extern "C" { |
25 | # endif |
26 | |
27 | # define FPM_64BIT |
28 | |
29 | |
30 | |
31 | # define SIZEOF_INT 4 |
32 | # define SIZEOF_LONG 8 |
33 | # define SIZEOF_LONG_LONG 8 |
34 | |
35 | |
36 | /* Id: version.h,v 1.26 2004/01/23 09:41:33 rob Exp */ |
37 | |
38 | # ifndef LIBMAD_VERSION_H |
39 | # define LIBMAD_VERSION_H |
40 | |
41 | # define MAD_VERSION_MAJOR 0 |
42 | # define MAD_VERSION_MINOR 15 |
43 | # define MAD_VERSION_PATCH 1 |
44 | # define MAD_VERSION_EXTRA " (beta)" |
45 | |
46 | # define MAD_VERSION_STRINGIZE(str) #str |
47 | # define MAD_VERSION_STRING(num) MAD_VERSION_STRINGIZE(num) |
48 | |
49 | # define MAD_VERSION MAD_VERSION_STRING(MAD_VERSION_MAJOR) "." \ |
50 | MAD_VERSION_STRING(MAD_VERSION_MINOR) "." \ |
51 | MAD_VERSION_STRING(MAD_VERSION_PATCH) \ |
52 | MAD_VERSION_EXTRA |
53 | |
54 | # define MAD_PUBLISHYEAR "2000-2004" |
55 | # define MAD_AUTHOR "Underbit Technologies, Inc." |
56 | # define MAD_EMAIL "info@underbit.com" |
57 | |
58 | extern char const mad_version[]; |
59 | extern char const mad_copyright[]; |
60 | extern char const mad_author[]; |
61 | extern char const mad_build[]; |
62 | |
63 | # endif |
64 | |
65 | /* Id: fixed.h,v 1.38 2004/02/17 02:02:03 rob Exp */ |
66 | |
67 | # ifndef LIBMAD_FIXED_H |
68 | # define LIBMAD_FIXED_H |
69 | |
70 | # if SIZEOF_INT >= 4 |
71 | typedef signed int mad_fixed_t; |
72 | |
73 | typedef signed int mad_fixed64hi_t; |
74 | typedef unsigned int mad_fixed64lo_t; |
75 | # else |
76 | typedef signed long mad_fixed_t; |
77 | |
78 | typedef signed long mad_fixed64hi_t; |
79 | typedef unsigned long mad_fixed64lo_t; |
80 | # endif |
81 | |
82 | # if defined(_MSC_VER) |
83 | # define mad_fixed64_t signed __int64 |
84 | # elif 1 || defined(__GNUC__) |
85 | # define mad_fixed64_t signed long long |
86 | # endif |
87 | |
88 | # if defined(FPM_FLOAT) |
89 | typedef double mad_sample_t; |
90 | # else |
91 | typedef mad_fixed_t mad_sample_t; |
92 | # endif |
93 | |
94 | /* |
95 | * Fixed-point format: 0xABBBBBBB |
96 | * A == whole part (sign + 3 bits) |
97 | * B == fractional part (28 bits) |
98 | * |
99 | * Values are signed two's complement, so the effective range is: |
100 | * 0x80000000 to 0x7fffffff |
101 | * -8.0 to +7.9999999962747097015380859375 |
102 | * |
103 | * The smallest representable value is: |
104 | * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9) |
105 | * |
106 | * 28 bits of fractional accuracy represent about |
107 | * 8.6 digits of decimal accuracy. |
108 | * |
109 | * Fixed-point numbers can be added or subtracted as normal |
110 | * integers, but multiplication requires shifting the 64-bit result |
111 | * from 56 fractional bits back to 28 (and rounding.) |
112 | * |
113 | * Changing the definition of MAD_F_FRACBITS is only partially |
114 | * supported, and must be done with care. |
115 | */ |
116 | |
117 | # define MAD_F_FRACBITS 28 |
118 | |
119 | # if MAD_F_FRACBITS == 28 |
120 | # define MAD_F(x) ((mad_fixed_t) (x##L)) |
121 | # else |
122 | # if MAD_F_FRACBITS < 28 |
123 | # warning "MAD_F_FRACBITS < 28" |
124 | # define MAD_F(x) ((mad_fixed_t) \ |
125 | (((x##L) + \ |
126 | (1L << (28 - MAD_F_FRACBITS - 1))) >> \ |
127 | (28 - MAD_F_FRACBITS))) |
128 | # elif MAD_F_FRACBITS > 28 |
129 | # error "MAD_F_FRACBITS > 28 not currently supported" |
130 | # define MAD_F(x) ((mad_fixed_t) \ |
131 | ((x##L) << (MAD_F_FRACBITS - 28))) |
132 | # endif |
133 | # endif |
134 | |
135 | # define MAD_F_MIN ((mad_fixed_t) -0x80000000L) |
136 | # define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL) |
137 | |
138 | # define MAD_F_ONE MAD_F(0x10000000) |
139 | |
140 | # define mad_f_tofixed(x) ((mad_fixed_t) \ |
141 | ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5)) |
142 | # define mad_f_todouble(x) ((double) \ |
143 | ((x) / (double) (1L << MAD_F_FRACBITS))) |
144 | |
145 | # define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS) |
146 | # define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1)) |
147 | /* (x should be positive) */ |
148 | |
149 | # define mad_f_fromint(x) ((x) << MAD_F_FRACBITS) |
150 | |
151 | # define mad_f_add(x, y) ((x) + (y)) |
152 | # define mad_f_sub(x, y) ((x) - (y)) |
153 | |
154 | # if defined(FPM_FLOAT) |
155 | # error "FPM_FLOAT not yet supported" |
156 | |
157 | # undef MAD_F |
158 | # define MAD_F(x) mad_f_todouble(x) |
159 | |
160 | # define mad_f_mul(x, y) ((x) * (y)) |
161 | # define mad_f_scale64 |
162 | |
163 | # undef ASO_ZEROCHECK |
164 | |
165 | # elif defined(FPM_64BIT) |
166 | |
167 | /* |
168 | * This version should be the most accurate if 64-bit types are supported by |
169 | * the compiler, although it may not be the most efficient. |
170 | */ |
171 | # if defined(OPT_ACCURACY) |
172 | # define mad_f_mul(x, y) \ |
173 | ((mad_fixed_t) \ |
174 | ((((mad_fixed64_t) (x) * (y)) + \ |
175 | (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS)) |
176 | # else |
177 | # define mad_f_mul(x, y) \ |
178 | ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS)) |
179 | # endif |
180 | |
181 | # define MAD_F_SCALEBITS MAD_F_FRACBITS |
182 | |
183 | /* --- Intel --------------------------------------------------------------- */ |
184 | |
185 | # elif defined(FPM_INTEL) |
186 | |
187 | # if defined(_MSC_VER) |
188 | # pragma warning(push) |
189 | # pragma warning(disable: 4035) /* no return value */ |
190 | static __forceinline |
191 | mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y) |
192 | { |
193 | enum { |
194 | fracbits = MAD_F_FRACBITS |
195 | }; |
196 | |
197 | __asm { |
198 | mov eax, x |
199 | imul y |
200 | shrd eax, edx, fracbits |
201 | } |
202 | |
203 | /* implicit return of eax */ |
204 | } |
205 | # pragma warning(pop) |
206 | |
207 | # define mad_f_mul mad_f_mul_inline |
208 | # define mad_f_scale64 |
209 | # else |
210 | /* |
211 | * This Intel version is fast and accurate; the disposition of the least |
212 | * significant bit depends on OPT_ACCURACY via mad_f_scale64(). |
213 | */ |
214 | # define MAD_F_MLX(hi, lo, x, y) \ |
215 | asm ("imull %3" \ |
216 | : "=a" (lo), "=d" (hi) \ |
217 | : "%a" (x), "rm" (y) \ |
218 | : "cc") |
219 | |
220 | # if defined(OPT_ACCURACY) |
221 | /* |
222 | * This gives best accuracy but is not very fast. |
223 | */ |
224 | # define MAD_F_MLA(hi, lo, x, y) \ |
225 | ({ mad_fixed64hi_t __hi; \ |
226 | mad_fixed64lo_t __lo; \ |
227 | MAD_F_MLX(__hi, __lo, (x), (y)); \ |
228 | asm ("addl %2,%0\n\t" \ |
229 | "adcl %3,%1" \ |
230 | : "=rm" (lo), "=rm" (hi) \ |
231 | : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \ |
232 | : "cc"); \ |
233 | }) |
234 | # endif /* OPT_ACCURACY */ |
235 | |
236 | # if defined(OPT_ACCURACY) |
237 | /* |
238 | * Surprisingly, this is faster than SHRD followed by ADC. |
239 | */ |
240 | # define mad_f_scale64(hi, lo) \ |
241 | ({ mad_fixed64hi_t __hi_; \ |
242 | mad_fixed64lo_t __lo_; \ |
243 | mad_fixed_t __result; \ |
244 | asm ("addl %4,%2\n\t" \ |
245 | "adcl %5,%3" \ |
246 | : "=rm" (__lo_), "=rm" (__hi_) \ |
247 | : "0" (lo), "1" (hi), \ |
248 | "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \ |
249 | : "cc"); \ |
250 | asm ("shrdl %3,%2,%1" \ |
251 | : "=rm" (__result) \ |
252 | : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \ |
253 | : "cc"); \ |
254 | __result; \ |
255 | }) |
256 | # elif defined(OPT_INTEL) |
257 | /* |
258 | * Alternate Intel scaling that may or may not perform better. |
259 | */ |
260 | # define mad_f_scale64(hi, lo) \ |
261 | ({ mad_fixed_t __result; \ |
262 | asm ("shrl %3,%1\n\t" \ |
263 | "shll %4,%2\n\t" \ |
264 | "orl %2,%1" \ |
265 | : "=rm" (__result) \ |
266 | : "0" (lo), "r" (hi), \ |
267 | "I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS) \ |
268 | : "cc"); \ |
269 | __result; \ |
270 | }) |
271 | # else |
272 | # define mad_f_scale64(hi, lo) \ |
273 | ({ mad_fixed_t __result; \ |
274 | asm ("shrdl %3,%2,%1" \ |
275 | : "=rm" (__result) \ |
276 | : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \ |
277 | : "cc"); \ |
278 | __result; \ |
279 | }) |
280 | # endif /* OPT_ACCURACY */ |
281 | |
282 | # define MAD_F_SCALEBITS MAD_F_FRACBITS |
283 | # endif |
284 | |
285 | /* --- ARM ----------------------------------------------------------------- */ |
286 | |
287 | # elif defined(FPM_ARM) |
288 | |
289 | /* |
290 | * This ARM V4 version is as accurate as FPM_64BIT but much faster. The |
291 | * least significant bit is properly rounded at no CPU cycle cost! |
292 | */ |
293 | # if 1 |
294 | /* |
295 | * This is faster than the default implementation via MAD_F_MLX() and |
296 | * mad_f_scale64(). |
297 | */ |
298 | # define mad_f_mul(x, y) \ |
299 | ({ mad_fixed64hi_t __hi; \ |
300 | mad_fixed64lo_t __lo; \ |
301 | mad_fixed_t __result; \ |
302 | asm ("smull %0, %1, %3, %4\n\t" \ |
303 | "movs %0, %0, lsr %5\n\t" \ |
304 | "adc %2, %0, %1, lsl %6" \ |
305 | : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \ |
306 | : "%r" (x), "r" (y), \ |
307 | "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \ |
308 | : "cc"); \ |
309 | __result; \ |
310 | }) |
311 | # endif |
312 | |
313 | # define MAD_F_MLX(hi, lo, x, y) \ |
314 | asm ("smull %0, %1, %2, %3" \ |
315 | : "=&r" (lo), "=&r" (hi) \ |
316 | : "%r" (x), "r" (y)) |
317 | |
318 | # define MAD_F_MLA(hi, lo, x, y) \ |
319 | asm ("smlal %0, %1, %2, %3" \ |
320 | : "+r" (lo), "+r" (hi) \ |
321 | : "%r" (x), "r" (y)) |
322 | |
323 | # define MAD_F_MLN(hi, lo) \ |
324 | asm ("rsbs %0, %2, #0\n\t" \ |
325 | "rsc %1, %3, #0" \ |
326 | : "=r" (lo), "=r" (hi) \ |
327 | : "0" (lo), "1" (hi) \ |
328 | : "cc") |
329 | |
330 | # define mad_f_scale64(hi, lo) \ |
331 | ({ mad_fixed_t __result; \ |
332 | asm ("movs %0, %1, lsr %3\n\t" \ |
333 | "adc %0, %0, %2, lsl %4" \ |
334 | : "=&r" (__result) \ |
335 | : "r" (lo), "r" (hi), \ |
336 | "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \ |
337 | : "cc"); \ |
338 | __result; \ |
339 | }) |
340 | |
341 | # define MAD_F_SCALEBITS MAD_F_FRACBITS |
342 | |
343 | /* --- MIPS ---------------------------------------------------------------- */ |
344 | |
345 | # elif defined(FPM_MIPS) |
346 | |
347 | /* |
348 | * This MIPS version is fast and accurate; the disposition of the least |
349 | * significant bit depends on OPT_ACCURACY via mad_f_scale64(). |
350 | */ |
351 | # define MAD_F_MLX(hi, lo, x, y) \ |
352 | asm ("mult %2,%3" \ |
353 | : "=l" (lo), "=h" (hi) \ |
354 | : "%r" (x), "r" (y)) |
355 | |
356 | # if defined(HAVE_MADD_ASM) |
357 | # define MAD_F_MLA(hi, lo, x, y) \ |
358 | asm ("madd %2,%3" \ |
359 | : "+l" (lo), "+h" (hi) \ |
360 | : "%r" (x), "r" (y)) |
361 | # elif defined(HAVE_MADD16_ASM) |
362 | /* |
363 | * This loses significant accuracy due to the 16-bit integer limit in the |
364 | * multiply/accumulate instruction. |
365 | */ |
366 | # define MAD_F_ML0(hi, lo, x, y) \ |
367 | asm ("mult %2,%3" \ |
368 | : "=l" (lo), "=h" (hi) \ |
369 | : "%r" ((x) >> 12), "r" ((y) >> 16)) |
370 | # define MAD_F_MLA(hi, lo, x, y) \ |
371 | asm ("madd16 %2,%3" \ |
372 | : "+l" (lo), "+h" (hi) \ |
373 | : "%r" ((x) >> 12), "r" ((y) >> 16)) |
374 | # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo)) |
375 | # endif |
376 | |
377 | # if defined(OPT_SPEED) |
378 | # define mad_f_scale64(hi, lo) \ |
379 | ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS))) |
380 | # define MAD_F_SCALEBITS MAD_F_FRACBITS |
381 | # endif |
382 | |
383 | /* --- SPARC --------------------------------------------------------------- */ |
384 | |
385 | # elif defined(FPM_SPARC) |
386 | |
387 | /* |
388 | * This SPARC V8 version is fast and accurate; the disposition of the least |
389 | * significant bit depends on OPT_ACCURACY via mad_f_scale64(). |
390 | */ |
391 | # define MAD_F_MLX(hi, lo, x, y) \ |
392 | asm ("smul %2, %3, %0\n\t" \ |
393 | "rd %%y, %1" \ |
394 | : "=r" (lo), "=r" (hi) \ |
395 | : "%r" (x), "rI" (y)) |
396 | |
397 | /* --- PowerPC ------------------------------------------------------------- */ |
398 | |
399 | # elif defined(FPM_PPC) |
400 | |
401 | /* |
402 | * This PowerPC version is fast and accurate; the disposition of the least |
403 | * significant bit depends on OPT_ACCURACY via mad_f_scale64(). |
404 | */ |
405 | # define MAD_F_MLX(hi, lo, x, y) \ |
406 | do { \ |
407 | asm ("mullw %0,%1,%2" \ |
408 | : "=r" (lo) \ |
409 | : "%r" (x), "r" (y)); \ |
410 | asm ("mulhw %0,%1,%2" \ |
411 | : "=r" (hi) \ |
412 | : "%r" (x), "r" (y)); \ |
413 | } \ |
414 | while (0) |
415 | |
416 | # if defined(OPT_ACCURACY) |
417 | /* |
418 | * This gives best accuracy but is not very fast. |
419 | */ |
420 | # define MAD_F_MLA(hi, lo, x, y) \ |
421 | ({ mad_fixed64hi_t __hi; \ |
422 | mad_fixed64lo_t __lo; \ |
423 | MAD_F_MLX(__hi, __lo, (x), (y)); \ |
424 | asm ("addc %0,%2,%3\n\t" \ |
425 | "adde %1,%4,%5" \ |
426 | : "=r" (lo), "=r" (hi) \ |
427 | : "%r" (lo), "r" (__lo), \ |
428 | "%r" (hi), "r" (__hi) \ |
429 | : "xer"); \ |
430 | }) |
431 | # endif |
432 | |
433 | # if defined(OPT_ACCURACY) |
434 | /* |
435 | * This is slower than the truncating version below it. |
436 | */ |
437 | # define mad_f_scale64(hi, lo) \ |
438 | ({ mad_fixed_t __result, __round; \ |
439 | asm ("rotrwi %0,%1,%2" \ |
440 | : "=r" (__result) \ |
441 | : "r" (lo), "i" (MAD_F_SCALEBITS)); \ |
442 | asm ("extrwi %0,%1,1,0" \ |
443 | : "=r" (__round) \ |
444 | : "r" (__result)); \ |
445 | asm ("insrwi %0,%1,%2,0" \ |
446 | : "+r" (__result) \ |
447 | : "r" (hi), "i" (MAD_F_SCALEBITS)); \ |
448 | asm ("add %0,%1,%2" \ |
449 | : "=r" (__result) \ |
450 | : "%r" (__result), "r" (__round)); \ |
451 | __result; \ |
452 | }) |
453 | # else |
454 | # define mad_f_scale64(hi, lo) \ |
455 | ({ mad_fixed_t __result; \ |
456 | asm ("rotrwi %0,%1,%2" \ |
457 | : "=r" (__result) \ |
458 | : "r" (lo), "i" (MAD_F_SCALEBITS)); \ |
459 | asm ("insrwi %0,%1,%2,0" \ |
460 | : "+r" (__result) \ |
461 | : "r" (hi), "i" (MAD_F_SCALEBITS)); \ |
462 | __result; \ |
463 | }) |
464 | # endif |
465 | |
466 | # define MAD_F_SCALEBITS MAD_F_FRACBITS |
467 | |
468 | /* --- Default ------------------------------------------------------------- */ |
469 | |
470 | # elif defined(FPM_DEFAULT) |
471 | |
472 | /* |
473 | * This version is the most portable but it loses significant accuracy. |
474 | * Furthermore, accuracy is biased against the second argument, so care |
475 | * should be taken when ordering operands. |
476 | * |
477 | * The scale factors are constant as this is not used with SSO. |
478 | * |
479 | * Pre-rounding is required to stay within the limits of compliance. |
480 | */ |
481 | # if defined(OPT_SPEED) |
482 | # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16)) |
483 | # else |
484 | # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \ |
485 | (((y) + (1L << 15)) >> 16)) |
486 | # endif |
487 | |
488 | /* ------------------------------------------------------------------------- */ |
489 | |
490 | # else |
491 | # error "no FPM selected" |
492 | # endif |
493 | |
494 | /* default implementations */ |
495 | |
496 | # if !defined(mad_f_mul) |
497 | # define mad_f_mul(x, y) \ |
498 | ({ register mad_fixed64hi_t __hi; \ |
499 | register mad_fixed64lo_t __lo; \ |
500 | MAD_F_MLX(__hi, __lo, (x), (y)); \ |
501 | mad_f_scale64(__hi, __lo); \ |
502 | }) |
503 | # endif |
504 | |
505 | # if !defined(MAD_F_MLA) |
506 | # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y))) |
507 | # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y))) |
508 | # define MAD_F_MLN(hi, lo) ((lo) = -(lo)) |
509 | # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo)) |
510 | # endif |
511 | |
512 | # if !defined(MAD_F_ML0) |
513 | # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y)) |
514 | # endif |
515 | |
516 | # if !defined(MAD_F_MLN) |
517 | # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi)) |
518 | # endif |
519 | |
520 | # if !defined(MAD_F_MLZ) |
521 | # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo)) |
522 | # endif |
523 | |
524 | # if !defined(mad_f_scale64) |
525 | # if defined(OPT_ACCURACY) |
526 | # define mad_f_scale64(hi, lo) \ |
527 | ((((mad_fixed_t) \ |
528 | (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \ |
529 | ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1) |
530 | # else |
531 | # define mad_f_scale64(hi, lo) \ |
532 | ((mad_fixed_t) \ |
533 | (((hi) << (32 - MAD_F_SCALEBITS)) | \ |
534 | ((lo) >> MAD_F_SCALEBITS))) |
535 | # endif |
536 | # define MAD_F_SCALEBITS MAD_F_FRACBITS |
537 | # endif |
538 | |
539 | /* C routines */ |
540 | |
541 | mad_fixed_t mad_f_abs(mad_fixed_t); |
542 | mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t); |
543 | |
544 | # endif |
545 | |
546 | /* Id: bit.h,v 1.12 2004/01/23 09:41:32 rob Exp */ |
547 | |
548 | # ifndef LIBMAD_BIT_H |
549 | # define LIBMAD_BIT_H |
550 | |
551 | struct mad_bitptr { |
552 | unsigned char const *byte; |
553 | unsigned short cache; |
554 | unsigned short left; |
555 | }; |
556 | |
557 | void mad_bit_init(struct mad_bitptr *, unsigned char const *); |
558 | |
559 | # define mad_bit_finish(bitptr) /* nothing */ |
560 | |
561 | unsigned int mad_bit_length(struct mad_bitptr const *, |
562 | struct mad_bitptr const *); |
563 | |
564 | # define mad_bit_bitsleft(bitptr) ((bitptr)->left) |
565 | unsigned char const *mad_bit_nextbyte(struct mad_bitptr const *); |
566 | |
567 | void mad_bit_skip(struct mad_bitptr *, unsigned int); |
568 | unsigned long mad_bit_read(struct mad_bitptr *, unsigned int); |
569 | void mad_bit_write(struct mad_bitptr *, unsigned int, unsigned long); |
570 | |
571 | unsigned short mad_bit_crc(struct mad_bitptr, unsigned int, unsigned short); |
572 | |
573 | # endif |
574 | |
575 | /* Id: timer.h,v 1.16 2004/01/23 09:41:33 rob Exp */ |
576 | |
577 | # ifndef LIBMAD_TIMER_H |
578 | # define LIBMAD_TIMER_H |
579 | |
580 | typedef struct { |
581 | signed long seconds; /* whole seconds */ |
582 | unsigned long fraction; /* 1/MAD_TIMER_RESOLUTION seconds */ |
583 | } mad_timer_t; |
584 | |
585 | extern mad_timer_t const mad_timer_zero; |
586 | |
587 | # define MAD_TIMER_RESOLUTION 352800000UL |
588 | |
589 | enum mad_units { |
590 | MAD_UNITS_HOURS = -2, |
591 | MAD_UNITS_MINUTES = -1, |
592 | MAD_UNITS_SECONDS = 0, |
593 | |
594 | /* metric units */ |
595 | |
596 | MAD_UNITS_DECISECONDS = 10, |
597 | MAD_UNITS_CENTISECONDS = 100, |
598 | MAD_UNITS_MILLISECONDS = 1000, |
599 | |
600 | /* audio sample units */ |
601 | |
602 | MAD_UNITS_8000_HZ = 8000, |
603 | MAD_UNITS_11025_HZ = 11025, |
604 | MAD_UNITS_12000_HZ = 12000, |
605 | |
606 | MAD_UNITS_16000_HZ = 16000, |
607 | MAD_UNITS_22050_HZ = 22050, |
608 | MAD_UNITS_24000_HZ = 24000, |
609 | |
610 | MAD_UNITS_32000_HZ = 32000, |
611 | MAD_UNITS_44100_HZ = 44100, |
612 | MAD_UNITS_48000_HZ = 48000, |
613 | |
614 | /* video frame/field units */ |
615 | |
616 | MAD_UNITS_24_FPS = 24, |
617 | MAD_UNITS_25_FPS = 25, |
618 | MAD_UNITS_30_FPS = 30, |
619 | MAD_UNITS_48_FPS = 48, |
620 | MAD_UNITS_50_FPS = 50, |
621 | MAD_UNITS_60_FPS = 60, |
622 | |
623 | /* CD audio frames */ |
624 | |
625 | MAD_UNITS_75_FPS = 75, |
626 | |
627 | /* video drop-frame units */ |
628 | |
629 | MAD_UNITS_23_976_FPS = -24, |
630 | MAD_UNITS_24_975_FPS = -25, |
631 | MAD_UNITS_29_97_FPS = -30, |
632 | MAD_UNITS_47_952_FPS = -48, |
633 | MAD_UNITS_49_95_FPS = -50, |
634 | MAD_UNITS_59_94_FPS = -60 |
635 | }; |
636 | |
637 | # define mad_timer_reset(timer) ((void) (*(timer) = mad_timer_zero)) |
638 | |
639 | int mad_timer_compare(mad_timer_t, mad_timer_t); |
640 | |
641 | # define mad_timer_sign(timer) mad_timer_compare((timer), mad_timer_zero) |
642 | |
643 | void mad_timer_negate(mad_timer_t *); |
644 | mad_timer_t mad_timer_abs(mad_timer_t); |
645 | |
646 | void mad_timer_set(mad_timer_t *, unsigned long, unsigned long, unsigned long); |
647 | void mad_timer_add(mad_timer_t *, mad_timer_t); |
648 | void mad_timer_multiply(mad_timer_t *, signed long); |
649 | |
650 | signed long mad_timer_count(mad_timer_t, enum mad_units); |
651 | unsigned long mad_timer_fraction(mad_timer_t, unsigned long); |
652 | void mad_timer_string(mad_timer_t, char *, char const *, |
653 | enum mad_units, enum mad_units, unsigned long); |
654 | |
655 | # endif |
656 | |
657 | /* Id: stream.h,v 1.20 2004/02/05 09:02:39 rob Exp */ |
658 | |
659 | # ifndef LIBMAD_STREAM_H |
660 | # define LIBMAD_STREAM_H |
661 | |
662 | |
663 | # define MAD_BUFFER_GUARD 8 |
664 | # define MAD_BUFFER_MDLEN (511 + 2048 + MAD_BUFFER_GUARD) |
665 | |
666 | enum mad_error { |
667 | MAD_ERROR_NONE = 0x0000, /* no error */ |
668 | |
669 | MAD_ERROR_BUFLEN = 0x0001, /* input buffer too small (or EOF) */ |
670 | MAD_ERROR_BUFPTR = 0x0002, /* invalid (null) buffer pointer */ |
671 | |
672 | MAD_ERROR_NOMEM = 0x0031, /* not enough memory */ |
673 | |
674 | MAD_ERROR_LOSTSYNC = 0x0101, /* lost synchronization */ |
675 | MAD_ERROR_BADLAYER = 0x0102, /* reserved header layer value */ |
676 | MAD_ERROR_BADBITRATE = 0x0103, /* forbidden bitrate value */ |
677 | MAD_ERROR_BADSAMPLERATE = 0x0104, /* reserved sample frequency value */ |
678 | MAD_ERROR_BADEMPHASIS = 0x0105, /* reserved emphasis value */ |
679 | |
680 | MAD_ERROR_BADCRC = 0x0201, /* CRC check failed */ |
681 | MAD_ERROR_BADBITALLOC = 0x0211, /* forbidden bit allocation value */ |
682 | MAD_ERROR_BADSCALEFACTOR = 0x0221, /* bad scalefactor index */ |
683 | MAD_ERROR_BADMODE = 0x0222, /* bad bitrate/mode combination */ |
684 | MAD_ERROR_BADFRAMELEN = 0x0231, /* bad frame length */ |
685 | MAD_ERROR_BADBIGVALUES = 0x0232, /* bad big_values count */ |
686 | MAD_ERROR_BADBLOCKTYPE = 0x0233, /* reserved block_type */ |
687 | MAD_ERROR_BADSCFSI = 0x0234, /* bad scalefactor selection info */ |
688 | MAD_ERROR_BADDATAPTR = 0x0235, /* bad main_data_begin pointer */ |
689 | MAD_ERROR_BADPART3LEN = 0x0236, /* bad audio data length */ |
690 | MAD_ERROR_BADHUFFTABLE = 0x0237, /* bad Huffman table select */ |
691 | MAD_ERROR_BADHUFFDATA = 0x0238, /* Huffman data overrun */ |
692 | MAD_ERROR_BADSTEREO = 0x0239 /* incompatible block_type for JS */ |
693 | }; |
694 | |
695 | # define MAD_RECOVERABLE(error) ((error) & 0xff00) |
696 | |
697 | struct mad_stream { |
698 | unsigned char const *buffer; /* input bitstream buffer */ |
699 | unsigned char const *bufend; /* end of buffer */ |
700 | unsigned long skiplen; /* bytes to skip before next frame */ |
701 | |
702 | int sync; /* stream sync found */ |
703 | unsigned long freerate; /* free bitrate (fixed) */ |
704 | |
705 | unsigned char const *this_frame; /* start of current frame */ |
706 | unsigned char const *next_frame; /* start of next frame */ |
707 | struct mad_bitptr ptr; /* current processing bit pointer */ |
708 | |
709 | struct mad_bitptr anc_ptr; /* ancillary bits pointer */ |
710 | unsigned int anc_bitlen; /* number of ancillary bits */ |
711 | |
712 | unsigned char (*main_data)[MAD_BUFFER_MDLEN]; |
713 | /* Layer III main_data() */ |
714 | unsigned int md_len; /* bytes in main_data */ |
715 | |
716 | int options; /* decoding options (see below) */ |
717 | enum mad_error error; /* error code (see above) */ |
718 | }; |
719 | |
720 | enum { |
721 | MAD_OPTION_IGNORECRC = 0x0001, /* ignore CRC errors */ |
722 | MAD_OPTION_HALFSAMPLERATE = 0x0002 /* generate PCM at 1/2 sample rate */ |
723 | # if 0 /* not yet implemented */ |
724 | MAD_OPTION_LEFTCHANNEL = 0x0010, /* decode left channel only */ |
725 | MAD_OPTION_RIGHTCHANNEL = 0x0020, /* decode right channel only */ |
726 | MAD_OPTION_SINGLECHANNEL = 0x0030 /* combine channels */ |
727 | # endif |
728 | }; |
729 | |
730 | void mad_stream_init(struct mad_stream *); |
731 | void mad_stream_finish(struct mad_stream *); |
732 | |
733 | # define mad_stream_options(stream, opts) \ |
734 | ((void) ((stream)->options = (opts))) |
735 | |
736 | void mad_stream_buffer(struct mad_stream *, |
737 | unsigned char const *, unsigned long); |
738 | void mad_stream_skip(struct mad_stream *, unsigned long); |
739 | |
740 | int mad_stream_sync(struct mad_stream *); |
741 | |
742 | char const *mad_stream_errorstr(struct mad_stream const *); |
743 | |
744 | # endif |
745 | |
746 | /* Id: frame.h,v 1.20 2004/01/23 09:41:32 rob Exp */ |
747 | |
748 | # ifndef LIBMAD_FRAME_H |
749 | # define LIBMAD_FRAME_H |
750 | |
751 | |
752 | enum mad_layer { |
753 | MAD_LAYER_I = 1, /* Layer I */ |
754 | MAD_LAYER_II = 2, /* Layer II */ |
755 | MAD_LAYER_III = 3 /* Layer III */ |
756 | }; |
757 | |
758 | enum mad_mode { |
759 | MAD_MODE_SINGLE_CHANNEL = 0, /* single channel */ |
760 | MAD_MODE_DUAL_CHANNEL = 1, /* dual channel */ |
761 | MAD_MODE_JOINT_STEREO = 2, /* joint (MS/intensity) stereo */ |
762 | MAD_MODE_STEREO = 3 /* normal LR stereo */ |
763 | }; |
764 | |
765 | enum mad_emphasis { |
766 | MAD_EMPHASIS_NONE = 0, /* no emphasis */ |
767 | MAD_EMPHASIS_50_15_US = 1, /* 50/15 microseconds emphasis */ |
768 | MAD_EMPHASIS_CCITT_J_17 = 3, /* CCITT J.17 emphasis */ |
769 | MAD_EMPHASIS_RESERVED = 2 /* unknown emphasis */ |
770 | }; |
771 | |
772 | struct { |
773 | enum mad_layer ; /* audio layer (1, 2, or 3) */ |
774 | enum mad_mode ; /* channel mode (see above) */ |
775 | int ; /* additional mode info */ |
776 | enum mad_emphasis ; /* de-emphasis to use (see above) */ |
777 | |
778 | unsigned long ; /* stream bitrate (bps) */ |
779 | unsigned int ; /* sampling frequency (Hz) */ |
780 | |
781 | unsigned short ; /* frame CRC accumulator */ |
782 | unsigned short ; /* final target CRC checksum */ |
783 | |
784 | int ; /* flags (see below) */ |
785 | int ; /* private bits (see below) */ |
786 | |
787 | mad_timer_t ; /* audio playing time of frame */ |
788 | }; |
789 | |
790 | struct mad_frame { |
791 | struct mad_header ; /* MPEG audio header */ |
792 | |
793 | int options; /* decoding options (from stream) */ |
794 | |
795 | mad_fixed_t sbsample[2][36][32]; /* synthesis subband filter samples */ |
796 | mad_fixed_t (*overlap)[2][32][18]; /* Layer III block overlap data */ |
797 | }; |
798 | |
799 | # define MAD_NCHANNELS(header) ((header)->mode ? 2 : 1) |
800 | # define MAD_NSBSAMPLES(header) \ |
801 | ((header)->layer == MAD_LAYER_I ? 12 : \ |
802 | (((header)->layer == MAD_LAYER_III && \ |
803 | ((header)->flags & MAD_FLAG_LSF_EXT)) ? 18 : 36)) |
804 | |
805 | enum { |
806 | MAD_FLAG_NPRIVATE_III = 0x0007, /* number of Layer III private bits */ |
807 | MAD_FLAG_INCOMPLETE = 0x0008, /* header but not data is decoded */ |
808 | |
809 | MAD_FLAG_PROTECTION = 0x0010, /* frame has CRC protection */ |
810 | MAD_FLAG_COPYRIGHT = 0x0020, /* frame is copyright */ |
811 | MAD_FLAG_ORIGINAL = 0x0040, /* frame is original (else copy) */ |
812 | MAD_FLAG_PADDING = 0x0080, /* frame has additional slot */ |
813 | |
814 | MAD_FLAG_I_STEREO = 0x0100, /* uses intensity joint stereo */ |
815 | MAD_FLAG_MS_STEREO = 0x0200, /* uses middle/side joint stereo */ |
816 | MAD_FLAG_FREEFORMAT = 0x0400, /* uses free format bitrate */ |
817 | |
818 | MAD_FLAG_LSF_EXT = 0x1000, /* lower sampling freq. extension */ |
819 | MAD_FLAG_MC_EXT = 0x2000, /* multichannel audio extension */ |
820 | MAD_FLAG_MPEG_2_5_EXT = 0x4000 /* MPEG 2.5 (unofficial) extension */ |
821 | }; |
822 | |
823 | enum { |
824 | = 0x0100, /* header private bit */ |
825 | MAD_PRIVATE_III = 0x001f /* Layer III private bits (up to 5) */ |
826 | }; |
827 | |
828 | void (struct mad_header *); |
829 | |
830 | # define mad_header_finish(header) /* nothing */ |
831 | |
832 | int (struct mad_header *, struct mad_stream *); |
833 | |
834 | void mad_frame_init(struct mad_frame *); |
835 | void mad_frame_finish(struct mad_frame *); |
836 | |
837 | int mad_frame_decode(struct mad_frame *, struct mad_stream *); |
838 | |
839 | void mad_frame_mute(struct mad_frame *); |
840 | |
841 | # endif |
842 | |
843 | /* Id: synth.h,v 1.15 2004/01/23 09:41:33 rob Exp */ |
844 | |
845 | # ifndef LIBMAD_SYNTH_H |
846 | # define LIBMAD_SYNTH_H |
847 | |
848 | |
849 | struct mad_pcm { |
850 | unsigned int samplerate; /* sampling frequency (Hz) */ |
851 | unsigned short channels; /* number of channels */ |
852 | unsigned short length; /* number of samples per channel */ |
853 | mad_fixed_t samples[2][1152]; /* PCM output samples [ch][sample] */ |
854 | }; |
855 | |
856 | struct mad_synth { |
857 | mad_fixed_t filter[2][2][2][16][8]; /* polyphase filterbank outputs */ |
858 | /* [ch][eo][peo][s][v] */ |
859 | |
860 | unsigned int phase; /* current processing phase */ |
861 | |
862 | struct mad_pcm pcm; /* PCM output */ |
863 | }; |
864 | |
865 | /* single channel PCM selector */ |
866 | enum { |
867 | MAD_PCM_CHANNEL_SINGLE = 0 |
868 | }; |
869 | |
870 | /* dual channel PCM selector */ |
871 | enum { |
872 | MAD_PCM_CHANNEL_DUAL_1 = 0, |
873 | MAD_PCM_CHANNEL_DUAL_2 = 1 |
874 | }; |
875 | |
876 | /* stereo PCM selector */ |
877 | enum { |
878 | MAD_PCM_CHANNEL_STEREO_LEFT = 0, |
879 | MAD_PCM_CHANNEL_STEREO_RIGHT = 1 |
880 | }; |
881 | |
882 | void mad_synth_init(struct mad_synth *); |
883 | |
884 | # define mad_synth_finish(synth) /* nothing */ |
885 | |
886 | void mad_synth_mute(struct mad_synth *); |
887 | |
888 | void mad_synth_frame(struct mad_synth *, struct mad_frame const *); |
889 | |
890 | # endif |
891 | |
892 | /* Id: decoder.h,v 1.17 2004/01/23 09:41:32 rob Exp */ |
893 | |
894 | # ifndef LIBMAD_DECODER_H |
895 | # define LIBMAD_DECODER_H |
896 | |
897 | |
898 | enum mad_decoder_mode { |
899 | MAD_DECODER_MODE_SYNC = 0, |
900 | MAD_DECODER_MODE_ASYNC |
901 | }; |
902 | |
903 | enum mad_flow { |
904 | MAD_FLOW_CONTINUE = 0x0000, /* continue normally */ |
905 | MAD_FLOW_STOP = 0x0010, /* stop decoding normally */ |
906 | MAD_FLOW_BREAK = 0x0011, /* stop decoding and signal an error */ |
907 | MAD_FLOW_IGNORE = 0x0020 /* ignore the current frame */ |
908 | }; |
909 | |
910 | struct mad_decoder { |
911 | enum mad_decoder_mode mode; |
912 | |
913 | int options; |
914 | |
915 | struct { |
916 | long pid; |
917 | int in; |
918 | int out; |
919 | } async; |
920 | |
921 | struct { |
922 | struct mad_stream stream; |
923 | struct mad_frame frame; |
924 | struct mad_synth synth; |
925 | } *sync; |
926 | |
927 | void *cb_data; |
928 | |
929 | enum mad_flow (*input_func)(void *, struct mad_stream *); |
930 | enum mad_flow (*)(void *, struct mad_header const *); |
931 | enum mad_flow (*filter_func)(void *, |
932 | struct mad_stream const *, struct mad_frame *); |
933 | enum mad_flow (*output_func)(void *, |
934 | struct mad_header const *, struct mad_pcm *); |
935 | enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *); |
936 | enum mad_flow (*message_func)(void *, void *, unsigned int *); |
937 | }; |
938 | |
939 | void mad_decoder_init(struct mad_decoder *, void *, |
940 | enum mad_flow (*)(void *, struct mad_stream *), |
941 | enum mad_flow (*)(void *, struct mad_header const *), |
942 | enum mad_flow (*)(void *, |
943 | struct mad_stream const *, |
944 | struct mad_frame *), |
945 | enum mad_flow (*)(void *, |
946 | struct mad_header const *, |
947 | struct mad_pcm *), |
948 | enum mad_flow (*)(void *, |
949 | struct mad_stream *, |
950 | struct mad_frame *), |
951 | enum mad_flow (*)(void *, void *, unsigned int *)); |
952 | int mad_decoder_finish(struct mad_decoder *); |
953 | |
954 | # define mad_decoder_options(decoder, opts) \ |
955 | ((void) ((decoder)->options = (opts))) |
956 | |
957 | int mad_decoder_run(struct mad_decoder *, enum mad_decoder_mode); |
958 | int mad_decoder_message(struct mad_decoder *, void *, unsigned int *); |
959 | |
960 | # endif |
961 | |
962 | # ifdef __cplusplus |
963 | } |
964 | # endif |
965 | |