mp3_plugin.c 29.7 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
Warren Dukes's avatar
Warren Dukes committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * This project's homepage is: http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

19
#include "../decoder_api.h"
20
#include "../conf.h"
21
#include "config.h"
Warren Dukes's avatar
Warren Dukes committed
22

23
#include <assert.h>
24 25 26
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
27
#include <glib.h>
Warren Dukes's avatar
Warren Dukes committed
28
#include <mad.h>
29

30 31 32
#ifdef HAVE_ID3TAG
#include <id3tag.h>
#endif
33

34
#define FRAMES_CUSHION    2000
Warren Dukes's avatar
Warren Dukes committed
35

36
#define READ_BUFFER_SIZE  40960
Warren Dukes's avatar
Warren Dukes committed
37

38 39 40 41 42 43
enum mp3_action {
	DECODE_SKIP = -3,
	DECODE_BREAK = -2,
	DECODE_CONT = -1,
	DECODE_OK = 0
};
Warren Dukes's avatar
Warren Dukes committed
44

45 46 47 48 49
enum muteframe {
	MUTEFRAME_NONE,
	MUTEFRAME_SKIP,
	MUTEFRAME_SEEK
};
50

51 52 53
/* the number of samples of silence the decoder inserts at start */
#define DECODERDELAY 529

Max Kellermann's avatar
Max Kellermann committed
54
#define DEFAULT_GAPLESS_MP3_PLAYBACK true
55

Max Kellermann's avatar
Max Kellermann committed
56
static bool gapless_playback;
57

58 59
static inline int32_t
mad_fixed_to_24_sample(mad_fixed_t sample)
Avuton Olrich's avatar
Avuton Olrich committed
60
{
Warren Dukes's avatar
Warren Dukes committed
61
	enum {
62
		bits = 24,
Warren Dukes's avatar
Warren Dukes committed
63
		MIN = -MAD_F_ONE,
Avuton Olrich's avatar
Avuton Olrich committed
64
		MAX = MAD_F_ONE - 1
Warren Dukes's avatar
Warren Dukes committed
65 66
	};

67 68
	/* round */
	sample = sample + (1L << (MAD_F_FRACBITS - bits));
Warren Dukes's avatar
Warren Dukes committed
69

70 71 72 73 74
	/* clip */
	if (sample > MAX)
		sample = MAX;
	else if (sample < MIN)
		sample = MIN;
Warren Dukes's avatar
Warren Dukes committed
75

76 77
	/* quantize */
	return sample >> (MAD_F_FRACBITS + 1 - bits);
Warren Dukes's avatar
Warren Dukes committed
78
}
Avuton Olrich's avatar
Avuton Olrich committed
79

80 81 82 83
static void
mad_fixed_to_24_buffer(int32_t *dest, const struct mad_synth *synth,
		       unsigned int start, unsigned int end,
		       unsigned int num_channels)
84
{
85
	unsigned int i, c;
86 87

	for (i = start; i < end; ++i) {
88
		for (c = 0; c < num_channels; ++c)
89
			*dest++ = mad_fixed_to_24_sample(synth->pcm.samples[c][i]);
90 91 92
	}
}

93
static bool mp3_plugin_init(void)
94
{
Max Kellermann's avatar
Max Kellermann committed
95 96 97 98
	int ret = getBoolConfigParam(CONF_GAPLESS_MP3_PLAYBACK, true);
	gapless_playback = ret != CONF_BOOL_UNSET
		? !!ret
		: DEFAULT_GAPLESS_MP3_PLAYBACK;
99
	return true;
100 101
}

102
#define MP3_DATA_OUTPUT_BUFFER_SIZE 2048
103

Max Kellermann's avatar
Max Kellermann committed
104
struct mp3_data {
Warren Dukes's avatar
Warren Dukes committed
105 106 107 108
	struct mad_stream stream;
	struct mad_frame frame;
	struct mad_synth synth;
	mad_timer_t timer;
Max Kellermann's avatar
Max Kellermann committed
109 110 111 112
	unsigned char input_buffer[READ_BUFFER_SIZE];
	int32_t output_buffer[MP3_DATA_OUTPUT_BUFFER_SIZE];
	float total_time;
	float elapsed_time;
Max Kellermann's avatar
Max Kellermann committed
113
	float seek_where;
Max Kellermann's avatar
Max Kellermann committed
114 115
	enum muteframe mute_frame;
	long *frame_offsets;
Avuton Olrich's avatar
Avuton Olrich committed
116
	mad_timer_t *times;
Max Kellermann's avatar
Max Kellermann committed
117 118 119 120 121 122 123
	unsigned long highest_frame;
	unsigned long max_frames;
	unsigned long current_frame;
	unsigned int drop_start_frames;
	unsigned int drop_end_frames;
	unsigned int drop_start_samples;
	unsigned int drop_end_samples;
Max Kellermann's avatar
Max Kellermann committed
124 125 126
	bool found_xing;
	bool found_first_frame;
	bool decoded_first_frame;
Max Kellermann's avatar
Max Kellermann committed
127
	unsigned long bit_rate;
128
	struct decoder *decoder;
Max Kellermann's avatar
Max Kellermann committed
129
	struct input_stream *input_stream;
130
	enum mad_layer layer;
Max Kellermann's avatar
Max Kellermann committed
131
};
Warren Dukes's avatar
Warren Dukes committed
132

Max Kellermann's avatar
Max Kellermann committed
133 134 135
static void
mp3_data_init(struct mp3_data *data, struct decoder *decoder,
	      struct input_stream *input_stream)
Avuton Olrich's avatar
Avuton Olrich committed
136
{
Max Kellermann's avatar
Max Kellermann committed
137 138 139 140
	data->mute_frame = MUTEFRAME_NONE;
	data->highest_frame = 0;
	data->max_frames = 0;
	data->frame_offsets = NULL;
Warren Dukes's avatar
Warren Dukes committed
141
	data->times = NULL;
Max Kellermann's avatar
Max Kellermann committed
142 143 144 145 146
	data->current_frame = 0;
	data->drop_start_frames = 0;
	data->drop_end_frames = 0;
	data->drop_start_samples = 0;
	data->drop_end_samples = 0;
Max Kellermann's avatar
Max Kellermann committed
147 148 149
	data->found_xing = false;
	data->found_first_frame = false;
	data->decoded_first_frame = false;
150
	data->decoder = decoder;
Max Kellermann's avatar
Max Kellermann committed
151
	data->input_stream = input_stream;
152
	data->layer = 0;
153

Warren Dukes's avatar
Warren Dukes committed
154
	mad_stream_init(&data->stream);
155
	mad_stream_options(&data->stream, MAD_OPTION_IGNORECRC);
Warren Dukes's avatar
Warren Dukes committed
156 157 158 159 160
	mad_frame_init(&data->frame);
	mad_synth_init(&data->synth);
	mad_timer_reset(&data->timer);
}

Max Kellermann's avatar
Max Kellermann committed
161
static bool mp3_seek(struct mp3_data *data, long offset)
Avuton Olrich's avatar
Avuton Olrich committed
162
{
Max Kellermann's avatar
Max Kellermann committed
163
	if (!input_stream_seek(data->input_stream, offset, SEEK_SET))
Max Kellermann's avatar
Max Kellermann committed
164
		return false;
165

Max Kellermann's avatar
Max Kellermann committed
166
	mad_stream_buffer(&data->stream, data->input_buffer, 0);
167
	(data->stream).error = 0;
168

Max Kellermann's avatar
Max Kellermann committed
169
	return true;
170 171
}

Max Kellermann's avatar
Max Kellermann committed
172 173
static bool
mp3_fill_buffer(struct mp3_data *data)
Avuton Olrich's avatar
Avuton Olrich committed
174
{
Max Kellermann's avatar
Max Kellermann committed
175 176 177 178 179 180 181 182 183
	size_t remaining, length;
	unsigned char *dest;

	if (data->stream.next_frame != NULL) {
		remaining = data->stream.bufend - data->stream.next_frame;
		memmove(data->input_buffer, data->stream.next_frame,
			remaining);
		dest = (data->input_buffer) + remaining;
		length = READ_BUFFER_SIZE - remaining;
Avuton Olrich's avatar
Avuton Olrich committed
184
	} else {
Max Kellermann's avatar
Max Kellermann committed
185 186 187
		remaining = 0;
		length = READ_BUFFER_SIZE;
		dest = data->input_buffer;
Warren Dukes's avatar
Warren Dukes committed
188 189
	}

190 191
	/* we've exhausted the read buffer, so give up!, these potential
	 * mp3 frames are way too big, and thus unlikely to be mp3 frames */
Max Kellermann's avatar
Max Kellermann committed
192
	if (length == 0)
Max Kellermann's avatar
Max Kellermann committed
193
		return false;
194

Max Kellermann's avatar
Max Kellermann committed
195 196
	length = decoder_read(data->decoder, data->input_stream, dest, length);
	if (length == 0)
Max Kellermann's avatar
Max Kellermann committed
197
		return false;
198

Max Kellermann's avatar
Max Kellermann committed
199 200
	mad_stream_buffer(&data->stream, data->input_buffer,
			  length + remaining);
Warren Dukes's avatar
Warren Dukes committed
201 202
	(data->stream).error = 0;

Max Kellermann's avatar
Max Kellermann committed
203
	return true;
Warren Dukes's avatar
Warren Dukes committed
204 205
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
#ifdef HAVE_ID3TAG
/* Parse mp3 RVA2 frame. Shamelessly stolen from madplay. */
static int parse_rva2(struct id3_tag * tag, struct replay_gain_info * replay_gain_info)
{
	struct id3_frame const * frame;

	id3_latin1_t const *id;
	id3_byte_t const *data;
	id3_length_t length;
	int found;

	enum {
		CHANNEL_OTHER         = 0x00,
		CHANNEL_MASTER_VOLUME = 0x01,
		CHANNEL_FRONT_RIGHT   = 0x02,
		CHANNEL_FRONT_LEFT    = 0x03,
		CHANNEL_BACK_RIGHT    = 0x04,
		CHANNEL_BACK_LEFT     = 0x05,
		CHANNEL_FRONT_CENTRE  = 0x06,
		CHANNEL_BACK_CENTRE   = 0x07,
		CHANNEL_SUBWOOFER     = 0x08
	};

	found = 0;

	/* relative volume adjustment information */

	frame = id3_tag_findframe(tag, "RVA2", 0);
	if (!frame) return 0;

	id   = id3_field_getlatin1(id3_frame_field(frame, 0));
	data = id3_field_getbinarydata(id3_frame_field(frame, 1),
					&length);

	if (!id || !data) return 0;

	/*
	 * "The 'identification' string is used to identify the
	 * situation and/or device where this adjustment should apply.
	 * The following is then repeated for every channel
	 *
	 *   Type of channel         $xx
	 *   Volume adjustment       $xx xx
	 *   Bits representing peak  $xx
	 *   Peak volume             $xx (xx ...)"
	 */

	while (length >= 4) {
		unsigned int peak_bytes;

		peak_bytes = (data[3] + 7) / 8;
		if (4 + peak_bytes > length)
			break;

		if (data[0] == CHANNEL_MASTER_VOLUME) {
			signed int voladj_fixed;
			double voladj_float;

			/*
			 * "The volume adjustment is encoded as a fixed
			 * point decibel value, 16 bit signed integer
			 * representing (adjustment*512), giving +/- 64
			 * dB with a precision of 0.001953125 dB."
			 */

			voladj_fixed  = (data[1] << 8) | (data[2] << 0);
			voladj_fixed |= -(voladj_fixed & 0x8000);

			voladj_float  = (double) voladj_fixed / 512;

			replay_gain_info->tuples[REPLAY_GAIN_TRACK].peak = voladj_float;
			replay_gain_info->tuples[REPLAY_GAIN_ALBUM].peak = voladj_float;

			g_debug("parseRVA2: Relative Volume "
				"%+.1f dB adjustment (%s)\n",
				voladj_float, id);

			found = 1;
			break;
		}

		data   += 4 + peak_bytes;
		length -= 4 + peak_bytes;
	}

	return found;
}
#endif

295
#ifdef HAVE_ID3TAG
296 297
static struct replay_gain_info *
parse_id3_replay_gain_info(struct id3_tag *tag)
Avuton Olrich's avatar
Avuton Olrich committed
298
{
299
	int i;
Avuton Olrich's avatar
Avuton Olrich committed
300 301 302
	char *key;
	char *value;
	struct id3_frame *frame;
303
	bool found = false;
304
	struct replay_gain_info *replay_gain_info;
305

306
	replay_gain_info = replay_gain_info_new();
307

Avuton Olrich's avatar
Avuton Olrich committed
308 309 310
	for (i = 0; (frame = id3_tag_findframe(tag, "TXXX", i)); i++) {
		if (frame->nfields < 3)
			continue;
311

Avuton Olrich's avatar
Avuton Olrich committed
312
		key = (char *)
Avuton Olrich's avatar
Avuton Olrich committed
313 314
		    id3_ucs4_latin1duplicate(id3_field_getstring
					     (&frame->fields[1]));
Avuton Olrich's avatar
Avuton Olrich committed
315
		value = (char *)
Avuton Olrich's avatar
Avuton Olrich committed
316 317
		    id3_ucs4_latin1duplicate(id3_field_getstring
					     (&frame->fields[2]));
318

319
		if (strcasecmp(key, "replaygain_track_gain") == 0) {
320
			replay_gain_info->tuples[REPLAY_GAIN_TRACK].gain = atof(value);
321
			found = true;
322
		} else if (strcasecmp(key, "replaygain_album_gain") == 0) {
323
			replay_gain_info->tuples[REPLAY_GAIN_ALBUM].gain = atof(value);
324
			found = true;
325
		} else if (strcasecmp(key, "replaygain_track_peak") == 0) {
326
			replay_gain_info->tuples[REPLAY_GAIN_TRACK].peak = atof(value);
327
			found = true;
328
		} else if (strcasecmp(key, "replaygain_album_peak") == 0) {
329
			replay_gain_info->tuples[REPLAY_GAIN_ALBUM].peak = atof(value);
330
			found = true;
331 332 333 334 335 336
		}

		free(key);
		free(value);
	}

337 338 339 340 341
	if (!found) {
		/* fall back on RVA2 if no replaygain tags found */
		found = parse_rva2(tag, replay_gain_info);
	}

Avuton Olrich's avatar
Avuton Olrich committed
342
	if (found)
Max Kellermann's avatar
Max Kellermann committed
343
		return replay_gain_info;
344
	replay_gain_info_free(replay_gain_info);
345
	return NULL;
346
}
347
#endif
348

349
#ifdef HAVE_ID3TAG
Max Kellermann's avatar
Max Kellermann committed
350 351
static void mp3_parse_id3(struct mp3_data *data, size_t tagsize,
			  struct tag **mpd_tag,
352
			  struct replay_gain_info **replay_gain_info_r)
Avuton Olrich's avatar
Avuton Olrich committed
353
{
Max Kellermann's avatar
Max Kellermann committed
354
	struct id3_tag *id3_tag = NULL;
355 356
	id3_length_t count;
	id3_byte_t const *id3_data;
Avuton Olrich's avatar
Avuton Olrich committed
357
	id3_byte_t *allocated = NULL;
358

359 360
	count = data->stream.bufend - data->stream.this_frame;

Avuton Olrich's avatar
Avuton Olrich committed
361
	if (tagsize <= count) {
362 363
		id3_data = data->stream.this_frame;
		mad_stream_skip(&(data->stream), tagsize);
Avuton Olrich's avatar
Avuton Olrich committed
364
	} else {
365
		allocated = g_malloc(tagsize);
366 367 368
		memcpy(allocated, data->stream.this_frame, count);
		mad_stream_skip(&(data->stream), count);

Avuton Olrich's avatar
Avuton Olrich committed
369
		while (count < tagsize) {
370
			size_t len;
371

Max Kellermann's avatar
Max Kellermann committed
372
			len = decoder_read(data->decoder, data->input_stream,
Max Kellermann's avatar
Max Kellermann committed
373 374
					   allocated + count, tagsize - count);
			if (len == 0)
375
				break;
Avuton Olrich's avatar
Avuton Olrich committed
376 377
			else
				count += len;
378 379
		}

Avuton Olrich's avatar
Avuton Olrich committed
380
		if (count != tagsize) {
Max Kellermann's avatar
Max Kellermann committed
381
			g_debug("mp3_decode: error parsing ID3 tag\n");
Max Kellermann's avatar
Max Kellermann committed
382 383
			g_free(allocated);
			return;
Warren Dukes's avatar
Warren Dukes committed
384
		}
385 386 387 388

		id3_data = allocated;
	}

Max Kellermann's avatar
Max Kellermann committed
389
	id3_tag = id3_tag_parse(id3_data, tagsize);
Max Kellermann's avatar
Max Kellermann committed
390 391 392 393
	if (id3_tag == NULL) {
		g_free(allocated);
		return;
	}
394

Max Kellermann's avatar
Max Kellermann committed
395 396 397 398 399 400
	if (mpd_tag) {
		struct tag *tmp_tag = tag_id3_import(id3_tag);
		if (tmp_tag != NULL) {
			if (*mpd_tag != NULL)
				tag_free(*mpd_tag);
			*mpd_tag = tmp_tag;
401
		}
402
	}
403

Max Kellermann's avatar
Max Kellermann committed
404
	if (replay_gain_info_r) {
405 406
		struct replay_gain_info *tmp_rgi =
			parse_id3_replay_gain_info(id3_tag);
Max Kellermann's avatar
Max Kellermann committed
407 408
		if (tmp_rgi != NULL) {
			if (*replay_gain_info_r)
409
				replay_gain_info_free(*replay_gain_info_r);
Max Kellermann's avatar
Max Kellermann committed
410
			*replay_gain_info_r = tmp_rgi;
411 412 413
		}
	}

Max Kellermann's avatar
Max Kellermann committed
414
	id3_tag_delete(id3_tag);
Max Kellermann's avatar
Max Kellermann committed
415

416
	g_free(allocated);
417 418 419
}
#endif

420
static enum mp3_action
421 422
decode_next_frame_header(struct mp3_data *data, G_GNUC_UNUSED struct tag **tag,
			 G_GNUC_UNUSED struct replay_gain_info **replay_gain_info_r)
Avuton Olrich's avatar
Avuton Olrich committed
423
{
424 425
	enum mad_layer layer;

Avuton Olrich's avatar
Avuton Olrich committed
426 427
	if ((data->stream).buffer == NULL
	    || (data->stream).error == MAD_ERROR_BUFLEN) {
Max Kellermann's avatar
Max Kellermann committed
428
		if (!mp3_fill_buffer(data))
Warren Dukes's avatar
Warren Dukes committed
429 430
			return DECODE_BREAK;
	}
Avuton Olrich's avatar
Avuton Olrich committed
431
	if (mad_header_decode(&data->frame.header, &data->stream)) {
432
#ifdef HAVE_ID3TAG
Avuton Olrich's avatar
Avuton Olrich committed
433 434 435 436 437 438 439 440 441 442 443
		if ((data->stream).error == MAD_ERROR_LOSTSYNC &&
		    (data->stream).this_frame) {
			signed long tagsize = id3_tag_query((data->stream).
							    this_frame,
							    (data->stream).
							    bufend -
							    (data->stream).
							    this_frame);

			if (tagsize > 0) {
				if (tag && !(*tag)) {
Max Kellermann's avatar
Max Kellermann committed
444 445
					mp3_parse_id3(data, (size_t)tagsize,
						      tag, replay_gain_info_r);
Avuton Olrich's avatar
Avuton Olrich committed
446
				} else {
447 448 449
					mad_stream_skip(&(data->stream),
							tagsize);
				}
450 451 452
				return DECODE_CONT;
			}
		}
453
#endif
Avuton Olrich's avatar
Avuton Olrich committed
454
		if (MAD_RECOVERABLE((data->stream).error)) {
455
			return DECODE_SKIP;
Avuton Olrich's avatar
Avuton Olrich committed
456 457 458 459
		} else {
			if ((data->stream).error == MAD_ERROR_BUFLEN)
				return DECODE_CONT;
			else {
Max Kellermann's avatar
Max Kellermann committed
460 461 462
				g_warning("unrecoverable frame level error "
					  "(%s).\n",
					  mad_stream_errorstr(&data->stream));
Warren Dukes's avatar
Warren Dukes committed
463 464 465 466
				return DECODE_BREAK;
			}
		}
	}
467 468 469 470 471

	layer = data->frame.header.layer;
	if (!data->layer) {
		if (layer != MAD_LAYER_II && layer != MAD_LAYER_III) {
			/* Only layer 2 and 3 have been tested to work */
472
			return DECODE_SKIP;
473 474 475 476
		}
		data->layer = layer;
	} else if (layer != data->layer) {
		/* Don't decode frames with a different layer than the first */
477 478
		return DECODE_SKIP;
	}
Warren Dukes's avatar
Warren Dukes committed
479 480 481 482

	return DECODE_OK;
}

483
static enum mp3_action
Max Kellermann's avatar
Max Kellermann committed
484
decodeNextFrame(struct mp3_data *data)
Avuton Olrich's avatar
Avuton Olrich committed
485 486 487
{
	if ((data->stream).buffer == NULL
	    || (data->stream).error == MAD_ERROR_BUFLEN) {
Max Kellermann's avatar
Max Kellermann committed
488
		if (!mp3_fill_buffer(data))
Warren Dukes's avatar
Warren Dukes committed
489 490
			return DECODE_BREAK;
	}
Avuton Olrich's avatar
Avuton Olrich committed
491
	if (mad_frame_decode(&data->frame, &data->stream)) {
Warren Dukes's avatar
Warren Dukes committed
492
#ifdef HAVE_ID3TAG
Avuton Olrich's avatar
Avuton Olrich committed
493 494 495 496 497 498 499 500 501
		if ((data->stream).error == MAD_ERROR_LOSTSYNC) {
			signed long tagsize = id3_tag_query((data->stream).
							    this_frame,
							    (data->stream).
							    bufend -
							    (data->stream).
							    this_frame);
			if (tagsize > 0) {
				mad_stream_skip(&(data->stream), tagsize);
502 503 504
				return DECODE_CONT;
			}
		}
505
#endif
Avuton Olrich's avatar
Avuton Olrich committed
506
		if (MAD_RECOVERABLE((data->stream).error)) {
507
			return DECODE_SKIP;
Avuton Olrich's avatar
Avuton Olrich committed
508 509 510 511
		} else {
			if ((data->stream).error == MAD_ERROR_BUFLEN)
				return DECODE_CONT;
			else {
Max Kellermann's avatar
Max Kellermann committed
512 513 514
				g_warning("unrecoverable frame level error "
					  "(%s).\n",
					  mad_stream_errorstr(&data->stream));
Warren Dukes's avatar
Warren Dukes committed
515 516 517 518 519 520 521 522
				return DECODE_BREAK;
			}
		}
	}

	return DECODE_OK;
}

523
/* xing stuff stolen from alsaplayer, and heavily modified by jat */
524 525
#define XI_MAGIC (('X' << 8) | 'i')
#define NG_MAGIC (('n' << 8) | 'g')
526 527 528 529
#define IN_MAGIC (('I' << 8) | 'n')
#define FO_MAGIC (('f' << 8) | 'o')

enum xing_magic {
530
	XING_MAGIC_XING, /* VBR */
531
	XING_MAGIC_INFO  /* CBR */
532
};
Warren Dukes's avatar
Warren Dukes committed
533 534

struct xing {
535 536 537 538 539 540
	long flags;             /* valid fields (see below) */
	unsigned long frames;   /* total number of frames */
	unsigned long bytes;    /* total number of bytes */
	unsigned char toc[100]; /* 100-point seek table */
	long scale;             /* VBR quality */
	enum xing_magic magic;  /* header magic */
Warren Dukes's avatar
Warren Dukes committed
541 542 543
};

enum {
Avuton Olrich's avatar
Avuton Olrich committed
544
	XING_FRAMES = 0x00000001L,
545 546
	XING_BYTES  = 0x00000002L,
	XING_TOC    = 0x00000004L,
547
	XING_SCALE  = 0x00000008L
Warren Dukes's avatar
Warren Dukes committed
548 549
};

550
struct version {
551 552
	unsigned major;
	unsigned minor;
553 554
};

555
struct lame {
556 557 558
	char encoder[10];       /* 9 byte encoder name/version ("LAME3.97b") */
	struct version version; /* struct containing just the version */
	float peak;             /* replaygain peak */
Max Kellermann's avatar
Max Kellermann committed
559 560 561 562
	float track_gain;       /* replaygain track gain */
	float album_gain;       /* replaygain album gain */
	int encoder_delay;      /* # of added samples at start of mp3 */
	int encoder_padding;    /* # of added samples at end of mp3 */
563
	int crc;                /* CRC of the first 190 bytes of this frame */
564 565
};

Max Kellermann's avatar
Max Kellermann committed
566 567
static bool
parse_xing(struct xing *xing, struct mad_bitptr *ptr, int *oldbitlen)
Warren Dukes's avatar
Warren Dukes committed
568
{
569
	unsigned long bits;
570
	int bitlen;
571 572 573
	int bitsleft;
	int i;

574
	bitlen = *oldbitlen;
Warren Dukes's avatar
Warren Dukes committed
575

Max Kellermann's avatar
Max Kellermann committed
576 577 578
	if (bitlen < 16)
		return false;

579
	bits = mad_bit_read(ptr, 16);
580 581
	bitlen -= 16;

582
	if (bits == XI_MAGIC) {
Max Kellermann's avatar
Max Kellermann committed
583 584 585 586 587 588
		if (bitlen < 16)
			return false;

		if (mad_bit_read(ptr, 16) != NG_MAGIC)
			return false;

589
		bitlen -= 16;
590 591
		xing->magic = XING_MAGIC_XING;
	} else if (bits == IN_MAGIC) {
Max Kellermann's avatar
Max Kellermann committed
592 593 594 595 596 597
		if (bitlen < 16)
			return false;

		if (mad_bit_read(ptr, 16) != FO_MAGIC)
			return false;

598 599
		bitlen -= 16;
		xing->magic = XING_MAGIC_INFO;
600 601 602
	}
	else if (bits == NG_MAGIC) xing->magic = XING_MAGIC_XING;
	else if (bits == FO_MAGIC) xing->magic = XING_MAGIC_INFO;
Max Kellermann's avatar
Max Kellermann committed
603 604
	else
		return false;
605

Max Kellermann's avatar
Max Kellermann committed
606 607
	if (bitlen < 32)
		return false;
608
	xing->flags = mad_bit_read(ptr, 32);
609 610 611
	bitlen -= 32;

	if (xing->flags & XING_FRAMES) {
Max Kellermann's avatar
Max Kellermann committed
612 613
		if (bitlen < 32)
			return false;
614
		xing->frames = mad_bit_read(ptr, 32);
615 616 617 618
		bitlen -= 32;
	}

	if (xing->flags & XING_BYTES) {
Max Kellermann's avatar
Max Kellermann committed
619 620
		if (bitlen < 32)
			return false;
621
		xing->bytes = mad_bit_read(ptr, 32);
622 623
		bitlen -= 32;
	}
Warren Dukes's avatar
Warren Dukes committed
624

625
	if (xing->flags & XING_TOC) {
Max Kellermann's avatar
Max Kellermann committed
626 627
		if (bitlen < 800)
			return false;
628
		for (i = 0; i < 100; ++i) xing->toc[i] = mad_bit_read(ptr, 8);
629 630 631 632
		bitlen -= 800;
	}

	if (xing->flags & XING_SCALE) {
Max Kellermann's avatar
Max Kellermann committed
633 634
		if (bitlen < 32)
			return false;
635
		xing->scale = mad_bit_read(ptr, 32);
636 637 638
		bitlen -= 32;
	}

639 640
	/* Make sure we consume no less than 120 bytes (960 bits) in hopes that
	 * the LAME tag is found there, and not right after the Xing header */
641
	bitsleft = 960 - ((*oldbitlen) - bitlen);
Max Kellermann's avatar
Max Kellermann committed
642 643
	if (bitsleft < 0)
		return false;
644 645 646 647 648
	else if (bitsleft > 0) {
		mad_bit_read(ptr, bitsleft);
		bitlen -= bitsleft;
	}

649
	*oldbitlen = bitlen;
650

Max Kellermann's avatar
Max Kellermann committed
651
	return true;
Warren Dukes's avatar
Warren Dukes committed
652 653
}

Max Kellermann's avatar
Max Kellermann committed
654 655
static bool
parse_lame(struct lame *lame, struct mad_bitptr *ptr, int *bitlen)
656
{
657 658 659 660 661
	int adj = 0;
	int name;
	int orig;
	int sign;
	int gain;
662 663 664 665
	int i;

	/* Unlike the xing header, the lame tag has a fixed length.  Fail if
	 * not all 36 bytes (288 bits) are there. */
666
	if (*bitlen < 288)
Max Kellermann's avatar
Max Kellermann committed
667
		return false;
668

669 670
	for (i = 0; i < 9; i++)
		lame->encoder[i] = (char)mad_bit_read(ptr, 8);
671 672
	lame->encoder[9] = '\0';

673 674
	*bitlen -= 72;

675 676 677
	/* This is technically incorrect, since the encoder might not be lame.
	 * But there's no other way to determine if this is a lame tag, and we
	 * wouldn't want to go reading a tag that's not there. */
678
	if (!g_str_has_prefix(lame->encoder, "LAME"))
Max Kellermann's avatar
Max Kellermann committed
679
		return false;
680 681 682

	if (sscanf(lame->encoder+4, "%u.%u",
	           &lame->version.major, &lame->version.minor) != 2)
Max Kellermann's avatar
Max Kellermann committed
683
		return false;
684

Max Kellermann's avatar
Max Kellermann committed
685 686
	g_debug("detected LAME version %i.%i (\"%s\")\n",
		lame->version.major, lame->version.minor, lame->encoder);
687 688 689 690 691 692 693 694 695 696 697

	/* The reference volume was changed from the 83dB used in the
	 * ReplayGain spec to 89dB in lame 3.95.1.  Bump the gain for older
	 * versions, since everyone else uses 89dB instead of 83dB.
	 * Unfortunately, lame didn't differentiate between 3.95 and 3.95.1, so
	 * it's impossible to make the proper adjustment for 3.95.
	 * Fortunately, 3.95 was only out for about a day before 3.95.1 was
	 * released. -- tmz */
	if (lame->version.major < 3 ||
	    (lame->version.major == 3 && lame->version.minor < 95))
		adj = 6;
698 699 700

	mad_bit_read(ptr, 16);

701
	lame->peak = mad_f_todouble(mad_bit_read(ptr, 32) << 5); /* peak */
Max Kellermann's avatar
Max Kellermann committed
702
	g_debug("LAME peak found: %f\n", lame->peak);
703

Max Kellermann's avatar
Max Kellermann committed
704
	lame->track_gain = 0;
705 706 707 708 709
	name = mad_bit_read(ptr, 3); /* gain name */
	orig = mad_bit_read(ptr, 3); /* gain originator */
	sign = mad_bit_read(ptr, 1); /* sign bit */
	gain = mad_bit_read(ptr, 9); /* gain*10 */
	if (gain && name == 1 && orig != 0) {
Max Kellermann's avatar
Max Kellermann committed
710
		lame->track_gain = ((sign ? -gain : gain) / 10.0) + adj;
Max Kellermann's avatar
Max Kellermann committed
711
		g_debug("LAME track gain found: %f\n", lame->track_gain);
712
	}
713

714 715 716 717
	/* tmz reports that this isn't currently written by any version of lame
	 * (as of 3.97).  Since we have no way of testing it, don't use it.
	 * Wouldn't want to go blowing someone's ears just because we read it
	 * wrong. :P -- jat */
Max Kellermann's avatar
Max Kellermann committed
718
	lame->album_gain = 0;
719 720 721 722 723 724
#if 0
	name = mad_bit_read(ptr, 3); /* gain name */
	orig = mad_bit_read(ptr, 3); /* gain originator */
	sign = mad_bit_read(ptr, 1); /* sign bit */
	gain = mad_bit_read(ptr, 9); /* gain*10 */
	if (gain && name == 2 && orig != 0) {
Max Kellermann's avatar
Max Kellermann committed
725
		lame->album_gain = ((sign ? -gain : gain) / 10.0) + adj;
Max Kellermann's avatar
Max Kellermann committed
726
		g_debug("LAME album gain found: %f\n", lame->track_gain);
727
	}
728
#else
729
	mad_bit_read(ptr, 16);
730 731
#endif

732 733
	mad_bit_read(ptr, 16);

Max Kellermann's avatar
Max Kellermann committed
734 735
	lame->encoder_delay = mad_bit_read(ptr, 12);
	lame->encoder_padding = mad_bit_read(ptr, 12);
736

Max Kellermann's avatar
Max Kellermann committed
737
	g_debug("encoder delay is %i, encoder padding is %i\n",
Max Kellermann's avatar
Max Kellermann committed
738
	      lame->encoder_delay, lame->encoder_padding);
739

740 741 742 743 744
	mad_bit_read(ptr, 80);

	lame->crc = mad_bit_read(ptr, 16);

	*bitlen -= 216;
745

Max Kellermann's avatar
Max Kellermann committed
746
	return true;
747 748
}

749 750 751 752 753 754 755 756
static inline float
mp3_frame_duration(const struct mad_frame *frame)
{
	return mad_timer_count(frame->header.duration,
			       MAD_UNITS_MILLISECONDS) / 1000.0;
}

static off_t
757
mp3_this_frame_offset(const struct mp3_data *data)
758 759 760 761 762 763 764 765
{
	off_t offset = data->input_stream->offset;

	if (data->stream.this_frame != NULL)
		offset -= data->stream.bufend - data->stream.this_frame;
	else
		offset -= data->stream.bufend - data->stream.buffer;

766 767 768 769 770 771 772
	return offset;
}

static off_t
mp3_rest_including_this_frame(const struct mp3_data *data)
{
	return data->input_stream->size - mp3_this_frame_offset(data);
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
}

/**
 * Attempt to calulcate the length of the song from filesize
 */
static void
mp3_filesize_to_song_length(struct mp3_data *data)
{
	off_t rest = mp3_rest_including_this_frame(data);

	if (rest > 0) {
		float frame_duration = mp3_frame_duration(&data->frame);

		data->total_time = (rest * 8.0) / (data->frame).header.bitrate;
		data->max_frames = data->total_time / frame_duration +
			FRAMES_CUSHION;
	} else {
		data->max_frames = FRAMES_CUSHION;
		data->total_time = 0;
	}
}

Max Kellermann's avatar
Max Kellermann committed
795
static bool
Max Kellermann's avatar
Max Kellermann committed
796
mp3_decode_first_frame(struct mp3_data *data, struct tag **tag,
797
		       struct replay_gain_info **replay_gain_info_r)
798
{
Warren Dukes's avatar
Warren Dukes committed
799
	struct xing xing;
800
	struct lame lame;
801 802
	struct mad_bitptr ptr;
	int bitlen;
803
	enum mp3_action ret;
Warren Dukes's avatar
Warren Dukes committed
804

805 806 807
	/* stfu gcc */
	memset(&xing, 0, sizeof(struct xing));
	xing.flags = 0;
808

Max Kellermann's avatar
Max Kellermann committed
809
	while (true) {
810 811 812 813 814
		do {
			ret = decode_next_frame_header(data, tag,
						       replay_gain_info_r);
		} while (ret == DECODE_CONT);
		if (ret == DECODE_BREAK)
Max Kellermann's avatar
Max Kellermann committed
815
			return false;
816
		if (ret == DECODE_SKIP) continue;
817

818 819 820 821
		do {
			ret = decodeNextFrame(data);
		} while (ret == DECODE_CONT);
		if (ret == DECODE_BREAK)
Max Kellermann's avatar
Max Kellermann committed
822
			return false;
823
		if (ret == DECODE_OK) break;
Avuton Olrich's avatar
Avuton Olrich committed
824 825
	}

826 827 828
	ptr = data->stream.anc_ptr;
	bitlen = data->stream.anc_bitlen;

829 830
	mp3_filesize_to_song_length(data);

831 832 833
	/*
	 * if an xing tag exists, use that!
	 */
834
	if (parse_xing(&xing, &ptr, &bitlen)) {
Max Kellermann's avatar
Max Kellermann committed
835
		data->found_xing = true;
Max Kellermann's avatar
Max Kellermann committed
836
		data->mute_frame = MUTEFRAME_SKIP;
837

838
		if ((xing.flags & XING_FRAMES) && xing.frames) {
Warren Dukes's avatar
Warren Dukes committed
839
			mad_timer_t duration = data->frame.header.duration;
Avuton Olrich's avatar
Avuton Olrich committed
840
			mad_timer_multiply(&duration, xing.frames);
Max Kellermann's avatar
Max Kellermann committed
841 842
			data->total_time = ((float)mad_timer_count(duration, MAD_UNITS_MILLISECONDS)) / 1000;
			data->max_frames = xing.frames;
Warren Dukes's avatar
Warren Dukes committed
843
		}
844 845

		if (parse_lame(&lame, &ptr, &bitlen)) {
Max Kellermann's avatar
Max Kellermann committed
846 847 848
			if (gapless_playback &&
			    data->input_stream->seekable) {
				data->drop_start_samples = lame.encoder_delay +
849
				                           DECODERDELAY;
Max Kellermann's avatar
Max Kellermann committed
850
				data->drop_end_samples = lame.encoder_padding;
851 852 853 854
			}

			/* Album gain isn't currently used.  See comment in
			 * parse_lame() for details. -- jat */
Max Kellermann's avatar
Max Kellermann committed
855 856
			if (replay_gain_info_r && !*replay_gain_info_r &&
			    lame.track_gain) {
857
				*replay_gain_info_r = replay_gain_info_new();
858 859
				(*replay_gain_info_r)->tuples[REPLAY_GAIN_TRACK].gain = lame.track_gain;
				(*replay_gain_info_r)->tuples[REPLAY_GAIN_TRACK].peak = lame.peak;
860 861
			}
		}
862
	} 
Warren Dukes's avatar
Warren Dukes committed
863

Max Kellermann's avatar
Max Kellermann committed
864 865
	if (!data->max_frames)
		return false;
866

Max Kellermann's avatar
Max Kellermann committed
867
	if (data->max_frames > 8 * 1024 * 1024) {
Max Kellermann's avatar
Max Kellermann committed
868 869
		g_warning("mp3 file header indicates too many frames: %lu\n",
			  data->max_frames);
Max Kellermann's avatar
Max Kellermann committed
870
		return false;
871 872
	}

873 874
	data->frame_offsets = g_malloc(sizeof(long) * data->max_frames);
	data->times = g_malloc(sizeof(mad_timer_t) * data->max_frames);
Warren Dukes's avatar
Warren Dukes committed
875

Max Kellermann's avatar
Max Kellermann committed
876
	return true;
Warren Dukes's avatar
Warren Dukes committed
877 878
}

Max Kellermann's avatar
Max Kellermann committed
879
static void mp3_data_finish(struct mp3_data *data)
Avuton Olrich's avatar
Avuton Olrich committed
880
{
Warren Dukes's avatar
Warren Dukes committed
881 882 883 884
	mad_synth_finish(&data->synth);
	mad_frame_finish(&data->frame);
	mad_stream_finish(&data->stream);

885 886
	g_free(data->frame_offsets);
	g_free(data->times);
Warren Dukes's avatar
Warren Dukes committed
887 888 889
}

/* this is primarily used for getting total time for tags */
890
static int mp3_total_file_time(const char *file)
Avuton Olrich's avatar
Avuton Olrich committed
891
{
Max Kellermann's avatar
Max Kellermann committed
892 893
	struct input_stream input_stream;
	struct mp3_data data;
Warren Dukes's avatar
Warren Dukes committed
894 895
	int ret;

Max Kellermann's avatar
Max Kellermann committed
896
	if (!input_stream_open(&input_stream, file))
Avuton Olrich's avatar
Avuton Olrich committed
897
		return -1;
Max Kellermann's avatar
Max Kellermann committed
898
	mp3_data_init(&data, NULL, &input_stream);
Max Kellermann's avatar
Max Kellermann committed
899
	if (!mp3_decode_first_frame(&data, NULL, NULL))
Avuton Olrich's avatar
Avuton Olrich committed
900 901
		ret = -1;
	else
Max Kellermann's avatar
Max Kellermann committed
902 903 904
		ret = data.total_time + 0.5;
	mp3_data_finish(&data);
	input_stream_close(&input_stream);
Warren Dukes's avatar
Warren Dukes committed
905 906 907 908

	return ret;
}

Max Kellermann's avatar
Max Kellermann committed
909
static bool
Max Kellermann's avatar
Max Kellermann committed
910 911
mp3_open(struct input_stream *is, struct mp3_data *data,
	 struct decoder *decoder, struct tag **tag,
912
	 struct replay_gain_info **replay_gain_info_r)
913
{
Max Kellermann's avatar
Max Kellermann committed
914
	mp3_data_init(data, decoder, is);
915
	*tag = NULL;
Max Kellermann's avatar
Max Kellermann committed
916
	if (!mp3_decode_first_frame(data, tag, replay_gain_info_r)) {
Max Kellermann's avatar
Max Kellermann committed
917
		mp3_data_finish(data);
Avuton Olrich's avatar
Avuton Olrich committed
918
		if (tag && *tag)
919
			tag_free(*tag);
Max Kellermann's avatar
Max Kellermann committed
920
		return false;
Warren Dukes's avatar
Warren Dukes committed
921 922
	}

Max Kellermann's avatar
Max Kellermann committed
923
	return true;
Warren Dukes's avatar
Warren Dukes committed
924 925
}

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
static long
mp3_time_to_frame(const struct mp3_data *data, double t)
{
	unsigned long i;

	for (i = 0; i < data->highest_frame; ++i) {
		double frame_time =
			mad_timer_count(data->times[i],
					MAD_UNITS_MILLISECONDS) / 1000.;
		if (frame_time >= t)
			break;
	}

	return i;
}

942 943
static void
mp3_update_timer_next_frame(struct mp3_data *data)
Avuton Olrich's avatar
Avuton Olrich committed
944
{
Max Kellermann's avatar
Max Kellermann committed
945
	if (data->current_frame >= data->highest_frame) {
946 947 948
		/* record this frame's properties in
		   data->frame_offsets (for seeking) and
		   data->times */
Max Kellermann's avatar
Max Kellermann committed
949
		data->bit_rate = (data->frame).header.bitrate;
950 951 952

		if (data->current_frame >= data->max_frames)
			/* cap data->current_frame */
Max Kellermann's avatar
Max Kellermann committed
953
			data->current_frame = data->max_frames - 1;
954
		else
Max Kellermann's avatar
Max Kellermann committed
955
			data->highest_frame++;
956

957 958
		data->frame_offsets[data->current_frame] =
			mp3_this_frame_offset(data);
959 960

		mad_timer_add(&data->timer, (data->frame).header.duration);
Max Kellermann's avatar
Max Kellermann committed
961
		data->times[data->current_frame] = data->timer;
962 963
	} else
		/* get the new timer value from data->times */
Max Kellermann's avatar
Max Kellermann committed
964
		data->timer = data->times[data->current_frame];
965

Max Kellermann's avatar
Max Kellermann committed
966 967
	data->current_frame++;
	data->elapsed_time =
968 969 970
		mad_timer_count(data->timer, MAD_UNITS_MILLISECONDS) / 1000.0;
}

971 972 973 974 975
/**
 * Sends the synthesized current frame via decoder_data().
 */
static enum decoder_command
mp3_send_pcm(struct mp3_data *data, unsigned i, unsigned pcm_length,
976
	     struct replay_gain_info *replay_gain_info)
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
{
	unsigned max_samples;

	max_samples = sizeof(data->output_buffer) /
		sizeof(data->output_buffer[0]) /
		MAD_NCHANNELS(&(data->frame).header);

	while (i < pcm_length) {
		enum decoder_command cmd;
		unsigned int num_samples = pcm_length - i;
		if (num_samples > max_samples)
			num_samples = max_samples;

		i += num_samples;

		mad_fixed_to_24_buffer(data->output_buffer,
				       &data->synth,
				       i - num_samples, i,
				       MAD_NCHANNELS(&(data->frame).header));
		num_samples *= MAD_NCHANNELS(&(data->frame).header);

		cmd = decoder_data(data->decoder, data->input_stream,
				   data->output_buffer,
				   sizeof(data->output_buffer[0]) * num_samples,
				   data->elapsed_time,
				   data->bit_rate / 1000,
				   replay_gain_info);
1004
		if (cmd != DECODE_COMMAND_NONE)
1005 1006 1007 1008 1009 1010
			return cmd;
	}

	return DECODE_COMMAND_NONE;
}

1011 1012 1013 1014
/**
 * Synthesize the current frame and send it via decoder_data().
 */
static enum decoder_command
1015 1016
mp3_synth_and_send(struct mp3_data *data,
		   struct replay_gain_info *replay_gain_info)
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
{
	unsigned i, pcm_length;
	enum decoder_command cmd;

	mad_synth_frame(&data->synth, &data->frame);

	if (!data->found_first_frame) {
		unsigned int samples_per_frame = data->synth.pcm.length;
		data->drop_start_frames = data->drop_start_samples / samples_per_frame;
		data->drop_end_frames = data->drop_end_samples / samples_per_frame;
		data->drop_start_samples = data->drop_start_samples % samples_per_frame;
		data->drop_end_samples = data->drop_end_samples % samples_per_frame;
		data->found_first_frame = true;
	}

	if (data->drop_start_frames > 0) {
		data->drop_start_frames--;
		return DECODE_COMMAND_NONE;
	} else if ((data->drop_end_frames > 0) &&
		   (data->current_frame == (data->max_frames + 1 - data->drop_end_frames))) {
		/* stop decoding, effectively dropping all remaining
		   frames */
		return DECODE_COMMAND_STOP;
	}

	if (!data->decoded_first_frame) {
		i = data->drop_start_samples;
		data->decoded_first_frame = true;
	} else
		i = 0;

	pcm_length = data->synth.pcm.length;
	if (data->drop_end_samples &&
	    (data->current_frame == data->max_frames - data->drop_end_frames)) {
		if (data->drop_end_samples >= pcm_length)
			pcm_length = 0;
		else
			pcm_length -= data->drop_end_samples;
	}

	cmd = mp3_send_pcm(data, i, pcm_length, replay_gain_info);
1058
	if (cmd != DECODE_COMMAND_NONE)
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
		return cmd;

	if (data->drop_end_samples &&
	    (data->current_frame == data->max_frames - data->drop_end_frames))
		/* stop decoding, effectively dropping
		 * all remaining samples */
		return DECODE_COMMAND_STOP;

	return DECODE_COMMAND_NONE;
}

1070
static bool
1071
mp3_read(struct mp3_data *data, struct replay_gain_info **replay_gain_info_r)
1072 1073
{
	struct decoder *decoder = data->decoder;
1074
	enum mp3_action ret;
1075
	enum decoder_command cmd;
1076 1077

	mp3_update_timer_next_frame(data);
Warren Dukes's avatar
Warren Dukes committed
1078

Max Kellermann's avatar
Max Kellermann committed
1079
	switch (data->mute_frame) {
Avuton Olrich's avatar
Avuton Olrich committed
1080
	case MUTEFRAME_SKIP:
Max Kellermann's avatar
Max Kellermann committed
1081
		data->mute_frame = MUTEFRAME_NONE;
Avuton Olrich's avatar
Avuton Olrich committed
1082 1083
		break;
	case MUTEFRAME_SEEK:
Max Kellermann's avatar
Max Kellermann committed
1084
		if (data->elapsed_time >= data->seek_where)
Max Kellermann's avatar
Max Kellermann committed
1085
			data->mute_frame = MUTEFRAME_NONE;
Avuton Olrich's avatar
Avuton Olrich committed
1086
		break;
1087
	case MUTEFRAME_NONE:
1088 1089 1090
		cmd = mp3_synth_and_send(data,
					 replay_gain_info_r != NULL
					 ? *replay_gain_info_r : NULL);
1091
		if (cmd == DECODE_COMMAND_SEEK) {
1092
			unsigned long j;
1093 1094 1095

			assert(data->input_stream->seekable);

1096 1097
			j = mp3_time_to_frame(data,
					      decoder_seek_where(decoder));
Max Kellermann's avatar
Max Kellermann committed
1098
			if (j < data->highest_frame) {
Max Kellermann's avatar
Max Kellermann committed
1099
				if (mp3_seek(data, data->frame_offsets[j])) {
Max Kellermann's avatar
Max Kellermann committed
1100
					data->current_frame = j;
1101
					decoder_command_finished(decoder);
Avuton Olrich's avatar
Avuton Olrich committed
1102
				} else
1103
					decoder_seek_error(decoder);
Max Kellermann's avatar
Max Kellermann committed
1104 1105
			} else {
				data->seek_where = decoder_seek_where(decoder);
1106
				data->mute_frame = MUTEFRAME_SEEK;
Max Kellermann's avatar
Max Kellermann committed
1107 1108
				decoder_command_finished(decoder);
			}
1109 1110
		} else if (cmd != DECODE_COMMAND_NONE)
			return false;
Warren Dukes's avatar
Warren Dukes committed
1111 1112
	}

Max Kellermann's avatar
Max Kellermann committed
1113
	while (true) {
1114 1115
		bool skip = false;

1116
		do {
1117 1118 1119
			struct tag *tag = NULL;

			ret = decode_next_frame_header(data, &tag,
1120
						       replay_gain_info_r);
1121 1122 1123 1124 1125

			if (tag != NULL) {
				decoder_tag(decoder, data->input_stream, tag);
				tag_free(tag);
			}
1126 1127
		} while (ret == DECODE_CONT);
		if (ret == DECODE_BREAK)
1128
			return false;
Avuton Olrich's avatar
Avuton Olrich committed
1129
		else if (ret == DECODE_SKIP)
1130
			skip = true;
1131

Max Kellermann's avatar
Max Kellermann committed
1132
		if (data->mute_frame == MUTEFRAME_NONE) {
1133 1134 1135 1136
			do {
				ret = decodeNextFrame(data);
			} while (ret == DECODE_CONT);
			if (ret == DECODE_BREAK)
1137
				return false;
Warren Dukes's avatar
Warren Dukes committed
1138
		}
1139

Avuton Olrich's avatar
Avuton Olrich committed
1140 1141
		if (!skip && ret == DECODE_OK)
			break;
Warren Dukes's avatar
Warren Dukes committed
1142
	}
Warren Dukes's avatar
Warren Dukes committed
1143

1144
	return ret != DECODE_BREAK;
Warren Dukes's avatar
Warren Dukes committed
1145 1146
}

Max Kellermann's avatar
Max Kellermann committed
1147
static void mp3_audio_format(struct mp3_data *data, struct audio_format *af)
Avuton Olrich's avatar
Avuton Olrich committed
1148
{
1149
	af->bits = 24;
1150
	af->sample_rate = (data->frame).header.samplerate;
Warren Dukes's avatar
Warren Dukes committed
1151 1152 1153
	af->channels = MAD_NCHANNELS(&(data->frame).header);
}

1154
static void
Max Kellermann's avatar
Max Kellermann committed
1155
mp3_decode(struct decoder *decoder, struct input_stream *input_stream)
Avuton Olrich's avatar
Avuton Olrich committed
1156
{
Max Kellermann's avatar
Max Kellermann committed
1157
	struct mp3_data data;
1158
	struct tag *tag = NULL;
1159
	struct replay_gain_info *replay_gain_info = NULL;
1160
	struct audio_format audio_format;
1161

Max Kellermann's avatar
Max Kellermann committed
1162
	if (!mp3_open(input_stream, &data, decoder, &tag, &replay_gain_info)) {
1163
		if (decoder_get_command(decoder) == DECODE_COMMAND_NONE)
Max Kellermann's avatar
Max Kellermann committed
1164
			g_warning
Avuton Olrich's avatar
Avuton Olrich committed
1165
			    ("Input does not appear to be a mp3 bit stream.\n");
1166
		return;
Warren Dukes's avatar
Warren Dukes committed
1167 1168
	}

Max Kellermann's avatar
Max Kellermann committed
1169
	mp3_audio_format(&data, &audio_format);
Avuton Olrich's avatar
Avuton Olrich committed
1170

1171 1172
	decoder_initialized(decoder, &audio_format,
			    data.input_stream->seekable, data.total_time);
Warren Dukes's avatar
Warren Dukes committed
1173

1174 1175 1176 1177 1178
	if (tag != NULL) {
		decoder_tag(decoder, input_stream, tag);
		tag_free(tag);
	}

1179
	while (mp3_read(&data, &replay_gain_info)) ;
Warren Dukes's avatar
Warren Dukes committed
1180

Max Kellermann's avatar
Max Kellermann committed
1181
	if (replay_gain_info)
1182
		replay_gain_info_free(replay_gain_info);
1183

1184
	if (decoder_get_command(decoder) == DECODE_COMMAND_SEEK &&
1185
	    data.mute_frame == MUTEFRAME_SEEK)
1186
		decoder_command_finished(decoder);
1187

Max Kellermann's avatar
Max Kellermann committed
1188
	mp3_data_finish(&data);
Warren Dukes's avatar
Warren Dukes committed
1189 1190
}

1191
static struct tag *mp3_tag_dup(const char *file)
Avuton Olrich's avatar
Avuton Olrich committed
1192
{
1193
	struct tag *ret = NULL;
Max Kellermann's avatar
Max Kellermann committed
1194
	int total_time;
Warren Dukes's avatar
Warren Dukes committed
1195

1196
	ret = tag_id3_load(file);
Warren Dukes's avatar
Warren Dukes committed
1197

Max Kellermann's avatar
Max Kellermann committed
1198
	total_time = mp3_total_file_time(file);
Max Kellermann's avatar
Max Kellermann committed
1199
	if (total_time >= 0) {
Avuton Olrich's avatar
Avuton Olrich committed
1200
		if (!ret)
1201
			ret = tag_new();
Max Kellermann's avatar
Max Kellermann committed
1202
		ret->time = total_time;
Avuton Olrich's avatar
Avuton Olrich committed
1203
	} else {
Max Kellermann's avatar
Max Kellermann committed
1204 1205
		g_debug("mp3_tag_dup: Failed to get total song time from: %s\n",
			file);
1206
	}
Warren Dukes's avatar
Warren Dukes committed
1207 1208 1209 1210

	return ret;
}

1211 1212
static const char *const mp3_suffixes[] = { "mp3", "mp2", NULL };
static const char *const mp3_mime_types[] = { "audio/mpeg", NULL };
Warren Dukes's avatar
Warren Dukes committed
1213

1214
const struct decoder_plugin mp3Plugin = {
1215 1216 1217
	.name = "mp3",
	.init = mp3_plugin_init,
	.stream_decode = mp3_decode,
Max Kellermann's avatar
Max Kellermann committed
1218
	.tag_dup = mp3_tag_dup,
1219
	.suffixes = mp3_suffixes,
Max Kellermann's avatar
Max Kellermann committed
1220
	.mime_types = mp3_mime_types
Warren Dukes's avatar
Warren Dukes committed
1221
};