Ported to AROS.
[AROS-Contrib.git] / libs / mpega / tag.c
blobdaff27ce706619c0967df4c9ed158ad5eec6b4ed
1 /*
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
19 * $Id$
22 # ifdef HAVE_CONFIG_H
23 # include "config.h"
24 # endif
26 # include "global.h"
28 # include "bit.h"
29 # include "crc.h"
30 # include "tag.h"
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')
37 * NAME: tag->init()
38 * DESCRIPTION: initialize tag structure
40 void tag_init(struct tag *tag)
42 tag->flags = 0;
43 tag->encoder[0] = 0;
47 * NAME: parse_xing()
48 * DESCRIPTION: parse a Xing VBR tag
50 static
51 int parse_xing(struct tag_xing *xing,
52 struct mad_bitptr *ptr, unsigned int *bitlen)
54 if (*bitlen < 32)
55 goto fail;
57 xing->flags = mad_bit_read(ptr, 32);
58 *bitlen -= 32;
60 if (xing->flags & TAG_XING_FRAMES) {
61 if (*bitlen < 32)
62 goto fail;
64 xing->frames = mad_bit_read(ptr, 32);
65 *bitlen -= 32;
68 if (xing->flags & TAG_XING_BYTES) {
69 if (*bitlen < 32)
70 goto fail;
72 xing->bytes = mad_bit_read(ptr, 32);
73 *bitlen -= 32;
76 if (xing->flags & TAG_XING_TOC) {
77 int i;
79 if (*bitlen < 800)
80 goto fail;
82 for (i = 0; i < 100; ++i)
83 xing->toc[i] = mad_bit_read(ptr, 8);
85 *bitlen -= 800;
88 if (xing->flags & TAG_XING_SCALE) {
89 if (*bitlen < 32)
90 goto fail;
92 xing->scale = mad_bit_read(ptr, 32);
93 *bitlen -= 32;
96 return 0;
98 fail:
99 xing->flags = 0;
100 return -1;
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)
109 int negative;
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);
117 if (negative)
118 rgain->adjustment = -rgain->adjustment;
122 * NAME: parse_lame()
123 * DESCRIPTION: parse a LAME tag
125 static
126 int parse_lame(struct tag_lame *lame,
127 struct mad_bitptr *ptr, unsigned int *bitlen,
128 unsigned short crc)
130 struct mad_bitptr save = *ptr;
131 unsigned long magic;
132 char const *version;
134 if (*bitlen < 36 * 8)
135 goto fail;
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)
148 goto fail;
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);
198 /* byte $B4: Misc */
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)
230 goto fail;
232 *bitlen -= 36 * 8;
234 return 0;
236 fail:
237 *ptr = save;
238 return -1;
242 * NAME: tag->parse()
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;
250 unsigned long magic;
251 int i;
253 if (bitlen < 32)
254 return -1;
256 magic = mad_bit_read(&ptr, 32);
257 bitlen -= 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))
271 return -1;
273 magic >>= 16;
275 /* backtrack the bit pointer */
277 ptr = start;
278 mad_bit_skip(&ptr, 16);
279 bitlen += 16;
282 if ((magic & 0x0000ffffL) == (XING_MAGIC & 0x0000ffffL))
283 tag->flags |= TAG_VBR;
285 /* Xing tag */
287 if (magic == LAME_MAGIC) {
288 ptr = start;
289 bitlen += 32;
291 else if (parse_xing(&tag->xing, &ptr, &bitlen) == 0)
292 tag->flags |= TAG_XING;
294 /* encoder string */
296 if (bitlen >= 20 * 8) {
297 start = ptr;
299 for (i = 0; i < 20; ++i) {
300 tag->encoder[i] = mad_bit_read(&ptr, 8);
302 if (tag->encoder[i] == 0)
303 break;
305 /* keep only printable ASCII chars */
307 if (tag->encoder[i] < 0x20 || tag->encoder[i] >= 0x7f) {
308 tag->encoder[i] = 0;
309 break;
313 tag->encoder[20] = 0;
314 ptr = start;
317 /* LAME tag */
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;
323 tag->encoder[9] = 0;
325 else {
326 for (i = 0; i < 20; ++i) {
327 if (tag->encoder[i] == 0)
328 break;
330 /* stop at padding chars */
332 if (tag->encoder[i] == 0x55) {
333 tag->encoder[i] = 0;
334 break;
339 return 0;