1 /*
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 Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * $Id: xing.c,v 1.5 2002/11/18 16:32:21 daper Exp $
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <mad.h>
27
28 #include "xing.h"
29
30 #define XING_MAGIC (('X' << 24) | ('i' << 16) | ('n' << 8) | 'g')
31
32 /*
33 * NAME: xing->init()
34 * DESCRIPTION: initialize Xing structure
35 */
xing_init(struct xing * xing)36 void xing_init(struct xing *xing)
37 {
38 xing->flags = 0;
39 }
40
41 /*
42 * NAME: xing->parse()
43 * DESCRIPTION: parse a Xing VBR header
44 */
xing_parse(struct xing * xing,struct mad_bitptr ptr,unsigned int bitlen)45 int xing_parse(struct xing *xing, struct mad_bitptr ptr, unsigned int bitlen)
46 {
47 if (bitlen < 64 || mad_bit_read(&ptr, 32) != XING_MAGIC)
48 goto fail;
49
50 xing->flags = mad_bit_read(&ptr, 32);
51 bitlen -= 64;
52
53 if (xing->flags & XING_FRAMES) {
54 if (bitlen < 32)
55 goto fail;
56
57 xing->frames = mad_bit_read(&ptr, 32);
58 bitlen -= 32;
59 }
60
61 if (xing->flags & XING_BYTES) {
62 if (bitlen < 32)
63 goto fail;
64
65 xing->bytes = mad_bit_read(&ptr, 32);
66 bitlen -= 32;
67 }
68
69 if (xing->flags & XING_TOC) {
70 int i;
71
72 if (bitlen < 800)
73 goto fail;
74
75 for (i = 0; i < 100; ++i)
76 xing->toc[i] = mad_bit_read(&ptr, 8);
77
78 bitlen -= 800;
79 }
80
81 if (xing->flags & XING_SCALE) {
82 if (bitlen < 32)
83 goto fail;
84
85 xing->scale = mad_bit_read(&ptr, 32);
86 bitlen -= 32;
87 }
88
89 return 0;
90
91 fail:
92 xing->flags = 0;
93 return -1;
94 }
95