OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [mp3/] [sw/] [mad-xess/] [libmad/] [mad.h] - Blame information for rev 1771

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.