2 * madplay - MPEG audio decoder and player
3 * Copyright (C) 2000-2004 Robert Leslie
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.
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.
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
32 # define XING_MAGIC (('X' << 24) | ('i' << 16) | ('n' << 8) | 'g')
33 # define INFO_MAGIC (('I' << 24) | ('n' << 16) | ('f' << 8) | 'o')
34 # define LAME_MAGIC (('L' << 24) | ('A' << 16) | ('M' << 8) | 'E')
38 * DESCRIPTION: initialize tag structure
40 void tag_init(struct tag
*tag
)
48 * DESCRIPTION: parse a Xing VBR tag
51 int parse_xing(struct tag_xing
*xing
,
52 struct mad_bitptr
*ptr
, unsigned int *bitlen
)
57 xing
->flags
= mad_bit_read(ptr
, 32);
60 if (xing
->flags
& TAG_XING_FRAMES
) {
64 xing
->frames
= mad_bit_read(ptr
, 32);
68 if (xing
->flags
& TAG_XING_BYTES
) {
72 xing
->bytes
= mad_bit_read(ptr
, 32);
76 if (xing
->flags
& TAG_XING_TOC
) {
82 for (i
= 0; i
< 100; ++i
)
83 xing
->toc
[i
] = mad_bit_read(ptr
, 8);
88 if (xing
->flags
& TAG_XING_SCALE
) {
92 xing
->scale
= mad_bit_read(ptr
, 32);
104 * NAME: tag->parse_rgain()
105 * DESCRIPTION: parse a 16-bit Replay Gain field
107 void tag_parse_rgain(struct tag_rgain
*rgain
, struct mad_bitptr
*ptr
)
111 rgain
->name
= mad_bit_read(ptr
, 3);
112 rgain
->originator
= mad_bit_read(ptr
, 3);
114 negative
= mad_bit_read(ptr
, 1);
115 rgain
->adjustment
= mad_bit_read(ptr
, 9);
118 rgain
->adjustment
= -rgain
->adjustment
;
123 * DESCRIPTION: parse a LAME tag
126 int parse_lame(struct tag_lame
*lame
,
127 struct mad_bitptr
*ptr
, unsigned int *bitlen
,
130 struct mad_bitptr save
= *ptr
;
134 if (*bitlen
< 36 * 8)
137 /* bytes $9A-$A4: Encoder short VersionString */
139 magic
= mad_bit_read(ptr
, 4 * 8);
140 version
= mad_bit_nextbyte(ptr
);
142 mad_bit_skip(ptr
, 5 * 8);
144 /* byte $A5: Info Tag revision + VBR method */
146 lame
->revision
= mad_bit_read(ptr
, 4);
147 if (lame
->revision
== 15)
150 lame
->vbr_method
= mad_bit_read(ptr
, 4);
152 /* byte $A6: Lowpass filter value (Hz) */
154 lame
->lowpass_filter
= mad_bit_read(ptr
, 8) * 100;
156 /* bytes $A7-$AA: 32 bit "Peak signal amplitude" */
158 lame
->peak
= mad_bit_read(ptr
, 32) << 5;
160 /* bytes $AB-$AC: 16 bit "Radio Replay Gain" */
162 tag_parse_rgain(&lame
->replay_gain
[0], ptr
);
164 /* bytes $AD-$AE: 16 bit "Audiophile Replay Gain" */
166 tag_parse_rgain(&lame
->replay_gain
[1], ptr
);
169 * As of version 3.95.1, LAME writes Replay Gain values with a reference of
170 * 89 dB SPL instead of the 83 dB specified in the Replay Gain proposed
171 * standard. Here we compensate for the heresy.
173 if (magic
== LAME_MAGIC
)
175 if (version
[2] == '.' || (version
[0] > '3' && version
[1] == '.') || (version
[0] == '3' && version
[1] == '.' && ((version
[2] == '9' && ((version
[3] == '5' && version
[4] == '.') || version
[3] > '5')) || (version
[2] >= '1' && version
[4] >= '0' && version
[4] <= '9'))))
177 if (lame
->replay_gain
[0].name
!= TAG_RGAIN_NAME_NOT_SET
)
178 lame
->replay_gain
[0].adjustment
-= 60; /* 6.0 dB */
179 if (lame
->replay_gain
[1].name
!= TAG_RGAIN_NAME_NOT_SET
)
180 lame
->replay_gain
[1].adjustment
-= 60; /* 6.0 dB */
184 /* byte $AF: Encoding flags + ATH Type */
186 lame
->flags
= mad_bit_read(ptr
, 4);
187 lame
->ath_type
= mad_bit_read(ptr
, 4);
189 /* byte $B0: if ABR {specified bitrate} else {minimal bitrate} */
191 lame
->bitrate
= mad_bit_read(ptr
, 8);
193 /* bytes $B1-$B3: Encoder delays */
195 lame
->start_delay
= mad_bit_read(ptr
, 12);
196 lame
->end_padding
= mad_bit_read(ptr
, 12);
200 lame
->source_samplerate
= mad_bit_read(ptr
, 2);
202 if (mad_bit_read(ptr
, 1))
203 lame
->flags
|= TAG_LAME_UNWISE
;
205 lame
->stereo_mode
= mad_bit_read(ptr
, 3);
206 lame
->noise_shaping
= mad_bit_read(ptr
, 2);
208 /* byte $B5: MP3 Gain */
210 lame
->gain
= mad_bit_read(ptr
, 8);
212 /* bytes $B6-B7: Preset and surround info */
214 mad_bit_skip(ptr
, 2);
216 lame
->surround
= mad_bit_read(ptr
, 3);
217 lame
->preset
= mad_bit_read(ptr
, 11);
219 /* bytes $B8-$BB: MusicLength */
221 lame
->music_length
= mad_bit_read(ptr
, 32);
223 /* bytes $BC-$BD: MusicCRC */
225 lame
->music_crc
= mad_bit_read(ptr
, 16);
227 /* bytes $BE-$BF: CRC-16 of Info Tag */
229 if (mad_bit_read(ptr
, 16) != crc
)
243 * DESCRIPTION: parse Xing/LAME tag(s)
245 int tag_parse(struct tag
*tag
, struct mad_stream
const *stream
)
247 struct mad_bitptr ptr
= stream
->anc_ptr
;
248 struct mad_bitptr start
= ptr
;
249 unsigned int bitlen
= stream
->anc_bitlen
;
256 magic
= mad_bit_read(&ptr
, 32);
259 if (magic
!= XING_MAGIC
&&
260 magic
!= INFO_MAGIC
&&
261 magic
!= LAME_MAGIC
) {
263 * Due to an unfortunate historical accident, a Xing VBR tag may be
264 * misplaced in a stream with CRC protection. We check for this by
265 * assuming the tag began two octets prior and the high bits of the
266 * following flags field are always zero.
269 if (magic
!= ((XING_MAGIC
<< 16) & 0xffffffffL
) &&
270 magic
!= ((INFO_MAGIC
<< 16) & 0xffffffffL
))
275 /* backtrack the bit pointer */
278 mad_bit_skip(&ptr
, 16);
282 if ((magic
& 0x0000ffffL
) == (XING_MAGIC
& 0x0000ffffL
))
283 tag
->flags
|= TAG_VBR
;
287 if (magic
== LAME_MAGIC
) {
291 else if (parse_xing(&tag
->xing
, &ptr
, &bitlen
) == 0)
292 tag
->flags
|= TAG_XING
;
296 if (bitlen
>= 20 * 8) {
299 for (i
= 0; i
< 20; ++i
) {
300 tag
->encoder
[i
] = mad_bit_read(&ptr
, 8);
302 if (tag
->encoder
[i
] == 0)
305 /* keep only printable ASCII chars */
307 if (tag
->encoder
[i
] < 0x20 || tag
->encoder
[i
] >= 0x7f) {
313 tag
->encoder
[20] = 0;
319 if (stream
->next_frame
- stream
->this_frame
>= 192 &&
320 parse_lame(&tag
->lame
, &ptr
, &bitlen
,
321 crc_compute(stream
->this_frame
, 190, 0x0000)) == 0) {
322 tag
->flags
|= TAG_LAME
;
326 for (i
= 0; i
< 20; ++i
) {
327 if (tag
->encoder
[i
] == 0)
330 /* stop at padding chars */
332 if (tag
->encoder
[i
] == 0x55) {