MadDecoderPlugin.cxx 26.1 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
21
#include "MadDecoderPlugin.hxx"
22
#include "../DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "input/InputStream.hxx"
24
#include "config/ConfigGlobal.hxx"
25 26
#include "tag/TagId3.hxx"
#include "tag/TagRva2.hxx"
27
#include "tag/TagHandler.hxx"
28
#include "tag/ReplayGain.hxx"
29
#include "tag/MixRamp.hxx"
30
#include "CheckAudioFormat.hxx"
31
#include "util/StringUtil.hxx"
32
#include "util/ASCII.hxx"
33
#include "util/Error.hxx"
34 35
#include "util/Domain.hxx"
#include "Log.hxx"
Warren Dukes's avatar
Warren Dukes committed
36 37

#include <mad.h>
38

39
#ifdef ENABLE_ID3TAG
40 41
#include <id3tag.h>
#endif
42

43 44 45 46 47
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

48
static constexpr unsigned long FRAMES_CUSHION = 2000;
Warren Dukes's avatar
Warren Dukes committed
49

50 51 52 53 54 55
enum mp3_action {
	DECODE_SKIP = -3,
	DECODE_BREAK = -2,
	DECODE_CONT = -1,
	DECODE_OK = 0
};
Warren Dukes's avatar
Warren Dukes committed
56

57 58 59 60 61
enum muteframe {
	MUTEFRAME_NONE,
	MUTEFRAME_SKIP,
	MUTEFRAME_SEEK
};
62

63
/* the number of samples of silence the decoder inserts at start */
64
static constexpr unsigned DECODERDELAY = 529;
65

66
static constexpr bool DEFAULT_GAPLESS_MP3_PLAYBACK = true;
67

68 69
static constexpr Domain mad_domain("mad");

Max Kellermann's avatar
Max Kellermann committed
70
static bool gapless_playback;
71

72 73 74 75 76 77 78
gcc_const
static SongTime
ToSongTime(mad_timer_t t)
{
	return SongTime::FromMS(mad_timer_count(t, MAD_UNITS_MILLISECONDS));
}

79 80
static inline int32_t
mad_fixed_to_24_sample(mad_fixed_t sample)
Avuton Olrich's avatar
Avuton Olrich committed
81
{
82 83 84
	static constexpr unsigned bits = 24;
	static constexpr mad_fixed_t MIN = -MAD_F_ONE;
	static constexpr mad_fixed_t MAX = MAD_F_ONE - 1;
Warren Dukes's avatar
Warren Dukes committed
85

86 87
	/* round */
	sample = sample + (1L << (MAD_F_FRACBITS - bits));
Warren Dukes's avatar
Warren Dukes committed
88

89
	/* clip */
90
	if (gcc_unlikely(sample > MAX))
91
		sample = MAX;
92
	else if (gcc_unlikely(sample < MIN))
93
		sample = MIN;
Warren Dukes's avatar
Warren Dukes committed
94

95 96
	/* quantize */
	return sample >> (MAD_F_FRACBITS + 1 - bits);
Warren Dukes's avatar
Warren Dukes committed
97
}
Avuton Olrich's avatar
Avuton Olrich committed
98

99 100 101 102
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)
103
{
104 105
	for (unsigned i = start; i < end; ++i)
		for (unsigned c = 0; c < num_channels; ++c)
106
			*dest++ = mad_fixed_to_24_sample(synth->pcm.samples[c][i]);
107 108
}

109
static bool
110
mp3_plugin_init(gcc_unused const config_param &param)
111
{
112 113
	gapless_playback = config_get_bool(CONF_GAPLESS_MP3_PLAYBACK,
					   DEFAULT_GAPLESS_MP3_PLAYBACK);
114
	return true;
115 116
}

117
struct MadDecoder {
118 119 120
	static constexpr size_t READ_BUFFER_SIZE = 40960;
	static constexpr size_t MP3_DATA_OUTPUT_BUFFER_SIZE = 2048;

Warren Dukes's avatar
Warren Dukes committed
121 122 123 124
	struct mad_stream stream;
	struct mad_frame frame;
	struct mad_synth synth;
	mad_timer_t timer;
Max Kellermann's avatar
Max Kellermann committed
125 126
	unsigned char input_buffer[READ_BUFFER_SIZE];
	int32_t output_buffer[MP3_DATA_OUTPUT_BUFFER_SIZE];
127
	SignedSongTime total_time;
128 129
	SongTime elapsed_time;
	SongTime seek_time;
Max Kellermann's avatar
Max Kellermann committed
130 131
	enum muteframe mute_frame;
	long *frame_offsets;
Avuton Olrich's avatar
Avuton Olrich committed
132
	mad_timer_t *times;
Max Kellermann's avatar
Max Kellermann committed
133 134 135 136 137 138 139
	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;
140
	bool found_replay_gain;
Max Kellermann's avatar
Max Kellermann committed
141 142
	bool found_first_frame;
	bool decoded_first_frame;
Max Kellermann's avatar
Max Kellermann committed
143
	unsigned long bit_rate;
144
	Decoder *const decoder;
145
	InputStream &input_stream;
146
	enum mad_layer layer;
147

148
	MadDecoder(Decoder *decoder, InputStream &input_stream);
149 150 151 152
	~MadDecoder();

	bool Seek(long offset);
	bool FillBuffer();
Max Kellermann's avatar
Max Kellermann committed
153 154
	void ParseId3(size_t tagsize, Tag **mpd_tag);
	enum mp3_action DecodeNextFrameHeader(Tag **tag);
155 156 157
	enum mp3_action DecodeNextFrame();

	gcc_pure
158
	offset_type ThisFrameOffset() const;
159 160

	gcc_pure
161
	offset_type RestIncludingThisFrame() const;
162 163 164 165 166 167

	/**
	 * Attempt to calulcate the length of the song from filesize
	 */
	void FileSizeToSongLength();

Max Kellermann's avatar
Max Kellermann committed
168
	bool DecodeFirstFrame(Tag **tag);
169 170

	gcc_pure
171
	long TimeToFrame(SongTime t) const;
172 173 174 175 176 177

	void UpdateTimerNextFrame();

	/**
	 * Sends the synthesized current frame via decoder_data().
	 */
178
	DecoderCommand SendPCM(unsigned i, unsigned pcm_length);
179 180 181 182 183

	/**
	 * Synthesize the current frame and send it via
	 * decoder_data().
	 */
184
	DecoderCommand SyncAndSend();
185 186

	bool Read();
Max Kellermann's avatar
Max Kellermann committed
187
};
Warren Dukes's avatar
Warren Dukes committed
188

189
MadDecoder::MadDecoder(Decoder *_decoder,
190
		       InputStream &_input_stream)
191 192 193 194 195 196
	:mute_frame(MUTEFRAME_NONE),
	 frame_offsets(nullptr),
	 times(nullptr),
	 highest_frame(0), max_frames(0), current_frame(0),
	 drop_start_frames(0), drop_end_frames(0),
	 drop_start_samples(0), drop_end_samples(0),
197
	 found_replay_gain(false),
198 199 200
	 found_first_frame(false), decoded_first_frame(false),
	 decoder(_decoder), input_stream(_input_stream),
	 layer(mad_layer(0))
Avuton Olrich's avatar
Avuton Olrich committed
201
{
202 203 204 205 206
	mad_stream_init(&stream);
	mad_stream_options(&stream, MAD_OPTION_IGNORECRC);
	mad_frame_init(&frame);
	mad_synth_init(&synth);
	mad_timer_reset(&timer);
Warren Dukes's avatar
Warren Dukes committed
207 208
}

209 210
inline bool
MadDecoder::Seek(long offset)
Avuton Olrich's avatar
Avuton Olrich committed
211
{
212
	Error error;
213
	if (!input_stream.LockSeek(offset, error))
Max Kellermann's avatar
Max Kellermann committed
214
		return false;
215

216 217
	mad_stream_buffer(&stream, input_buffer, 0);
	stream.error = MAD_ERROR_NONE;
218

Max Kellermann's avatar
Max Kellermann committed
219
	return true;
220 221
}

222 223
inline bool
MadDecoder::FillBuffer()
Avuton Olrich's avatar
Avuton Olrich committed
224
{
Max Kellermann's avatar
Max Kellermann committed
225 226 227
	size_t remaining, length;
	unsigned char *dest;

228 229 230 231
	if (stream.next_frame != nullptr) {
		remaining = stream.bufend - stream.next_frame;
		memmove(input_buffer, stream.next_frame, remaining);
		dest = input_buffer + remaining;
Max Kellermann's avatar
Max Kellermann committed
232
		length = READ_BUFFER_SIZE - remaining;
Avuton Olrich's avatar
Avuton Olrich committed
233
	} else {
Max Kellermann's avatar
Max Kellermann committed
234 235
		remaining = 0;
		length = READ_BUFFER_SIZE;
236
		dest = input_buffer;
Warren Dukes's avatar
Warren Dukes committed
237 238
	}

239 240
	/* 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
241
	if (length == 0)
Max Kellermann's avatar
Max Kellermann committed
242
		return false;
243

244
	length = decoder_read(decoder, input_stream, dest, length);
Max Kellermann's avatar
Max Kellermann committed
245
	if (length == 0)
Max Kellermann's avatar
Max Kellermann committed
246
		return false;
247

248 249
	mad_stream_buffer(&stream, input_buffer, length + remaining);
	stream.error = MAD_ERROR_NONE;
Warren Dukes's avatar
Warren Dukes committed
250

Max Kellermann's avatar
Max Kellermann committed
251
	return true;
Warren Dukes's avatar
Warren Dukes committed
252 253
}

254
#ifdef ENABLE_ID3TAG
255
static bool
256
parse_id3_replay_gain_info(ReplayGainInfo &rgi,
257
			   struct id3_tag *tag)
Avuton Olrich's avatar
Avuton Olrich committed
258
{
259
	bool found = false;
260

261
	rgi.Clear();
262

263 264
	struct id3_frame *frame;
	for (unsigned i = 0; (frame = id3_tag_findframe(tag, "TXXX", i)); i++) {
Avuton Olrich's avatar
Avuton Olrich committed
265 266
		if (frame->nfields < 3)
			continue;
267

268
		char *const key = (char *)
Avuton Olrich's avatar
Avuton Olrich committed
269 270
		    id3_ucs4_latin1duplicate(id3_field_getstring
					     (&frame->fields[1]));
271
		char *const value = (char *)
Avuton Olrich's avatar
Avuton Olrich committed
272 273
		    id3_ucs4_latin1duplicate(id3_field_getstring
					     (&frame->fields[2]));
274

275
		if (ParseReplayGainTag(rgi, key, value))
276
			found = true;
277 278 279 280 281

		free(key);
		free(value);
	}

282
	return found ||
283
		/* fall back on RVA2 if no replaygain tags found */
284
		tag_rva2_parse(tag, rgi);
285
}
286
#endif
287

288
#ifdef ENABLE_ID3TAG
289 290 291
gcc_pure
static MixRampInfo
parse_id3_mixramp(struct id3_tag *tag)
292
{
293
	MixRampInfo result;
294

295 296
	struct id3_frame *frame;
	for (unsigned i = 0; (frame = id3_tag_findframe(tag, "TXXX", i)); i++) {
297 298 299
		if (frame->nfields < 3)
			continue;

300
		char *const key = (char *)
301 302
		    id3_ucs4_latin1duplicate(id3_field_getstring
					     (&frame->fields[1]));
303
		char *const value = (char *)
304 305 306
		    id3_ucs4_latin1duplicate(id3_field_getstring
					     (&frame->fields[2]));

307
		ParseMixRampTag(result, key, value);
308 309 310 311 312

		free(key);
		free(value);
	}

313
	return result;
314 315 316
}
#endif

317
inline void
Max Kellermann's avatar
Max Kellermann committed
318
MadDecoder::ParseId3(size_t tagsize, Tag **mpd_tag)
Avuton Olrich's avatar
Avuton Olrich committed
319
{
320
#ifdef ENABLE_ID3TAG
321
	id3_byte_t *allocated = nullptr;
322

323
	const id3_length_t count = stream.bufend - stream.this_frame;
324

325
	const id3_byte_t *id3_data;
Avuton Olrich's avatar
Avuton Olrich committed
326
	if (tagsize <= count) {
327 328
		id3_data = stream.this_frame;
		mad_stream_skip(&(stream), tagsize);
Avuton Olrich's avatar
Avuton Olrich committed
329
	} else {
330
		allocated = new id3_byte_t[tagsize];
331 332
		memcpy(allocated, stream.this_frame, count);
		mad_stream_skip(&(stream), count);
333

334 335
		if (!decoder_read_full(decoder, input_stream,
				       allocated + count, tagsize - count)) {
336
			LogDebug(mad_domain, "error parsing ID3 tag");
337
			delete[] allocated;
Max Kellermann's avatar
Max Kellermann committed
338
			return;
Warren Dukes's avatar
Warren Dukes committed
339
		}
340 341 342 343

		id3_data = allocated;
	}

344
	struct id3_tag *const id3_tag = id3_tag_parse(id3_data, tagsize);
345
	if (id3_tag == nullptr) {
346
		delete[] allocated;
Max Kellermann's avatar
Max Kellermann committed
347 348
		return;
	}
349

Max Kellermann's avatar
Max Kellermann committed
350
	if (mpd_tag) {
Max Kellermann's avatar
Max Kellermann committed
351
		Tag *tmp_tag = tag_id3_import(id3_tag);
352
		if (tmp_tag != nullptr) {
Max Kellermann's avatar
Max Kellermann committed
353
			delete *mpd_tag;
Max Kellermann's avatar
Max Kellermann committed
354
			*mpd_tag = tmp_tag;
355
		}
356
	}
357

358
	if (decoder != nullptr) {
359
		ReplayGainInfo rgi;
360

361
		if (parse_id3_replay_gain_info(rgi, id3_tag)) {
362
			decoder_replay_gain(*decoder, &rgi);
363
			found_replay_gain = true;
364
		}
365

366
		decoder_mixramp(*decoder, parse_id3_mixramp(id3_tag));
367 368
	}

Max Kellermann's avatar
Max Kellermann committed
369
	id3_tag_delete(id3_tag);
Max Kellermann's avatar
Max Kellermann committed
370

371
	delete[] allocated;
372
#else /* !ENABLE_ID3TAG */
373 374 375 376 377
	(void)mpd_tag;

	/* This code is enabled when libid3tag is disabled.  Instead
	   of parsing the ID3 frame, it just skips it. */

378
	size_t count = stream.bufend - stream.this_frame;
379 380

	if (tagsize <= count) {
381
		mad_stream_skip(&stream, tagsize);
382
	} else {
383
		mad_stream_skip(&stream, count);
384
		decoder_skip(decoder, input_stream, tagsize - count);
385
	}
386
#endif
387 388
}

389
#ifndef ENABLE_ID3TAG
390 391 392
/**
 * This function emulates libid3tag when it is disabled.  Instead of
 * doing a real analyzation of the frame, it just checks whether the
393 394
 * frame begins with the string "ID3".  If so, it returns the length
 * of the ID3 frame.
395 396 397 398
 */
static signed long
id3_tag_query(const void *p0, size_t length)
{
399
	const char *p = (const char *)p0;
400

401 402
	return length >= 10 && memcmp(p, "ID3", 3) == 0
		? (p[8] << 7) + p[9] + 10
403 404
		: 0;
}
405
#endif /* !ENABLE_ID3TAG */
406

407 408 409 410 411 412 413 414 415 416 417 418 419 420
static enum mp3_action
RecoverFrameError(struct mad_stream &stream)
{
	if (MAD_RECOVERABLE(stream.error))
		return DECODE_SKIP;
	else if (stream.error == MAD_ERROR_BUFLEN)
		return DECODE_CONT;

	FormatWarning(mad_domain,
		      "unrecoverable frame level error: %s",
		      mad_stream_errorstr(&stream));
	return DECODE_BREAK;
}

421
enum mp3_action
Max Kellermann's avatar
Max Kellermann committed
422
MadDecoder::DecodeNextFrameHeader(Tag **tag)
Avuton Olrich's avatar
Avuton Olrich committed
423
{
424 425 426
	if ((stream.buffer == nullptr || stream.error == MAD_ERROR_BUFLEN) &&
	    !FillBuffer())
		return DECODE_BREAK;
427

428 429 430 431 432
	if (mad_header_decode(&frame.header, &stream)) {
		if (stream.error == MAD_ERROR_LOSTSYNC && stream.this_frame) {
			signed long tagsize = id3_tag_query(stream.this_frame,
							    stream.bufend -
							    stream.this_frame);
Avuton Olrich's avatar
Avuton Olrich committed
433 434 435

			if (tagsize > 0) {
				if (tag && !(*tag)) {
436
					ParseId3((size_t)tagsize, tag);
Avuton Olrich's avatar
Avuton Olrich committed
437
				} else {
438
					mad_stream_skip(&stream, tagsize);
439
				}
440 441 442
				return DECODE_CONT;
			}
		}
443

444
		return RecoverFrameError(stream);
Warren Dukes's avatar
Warren Dukes committed
445
	}
446

447 448 449
	enum mad_layer new_layer = frame.header.layer;
	if (layer == (mad_layer)0) {
		if (new_layer != MAD_LAYER_II && new_layer != MAD_LAYER_III) {
450
			/* Only layer 2 and 3 have been tested to work */
451
			return DECODE_SKIP;
452
		}
453 454 455

		layer = new_layer;
	} else if (new_layer != layer) {
456
		/* Don't decode frames with a different layer than the first */
457 458
		return DECODE_SKIP;
	}
Warren Dukes's avatar
Warren Dukes committed
459 460 461 462

	return DECODE_OK;
}

463 464
enum mp3_action
MadDecoder::DecodeNextFrame()
Avuton Olrich's avatar
Avuton Olrich committed
465
{
466 467 468 469 470 471 472 473 474
	if ((stream.buffer == nullptr || stream.error == MAD_ERROR_BUFLEN) &&
	    !FillBuffer())
		return DECODE_BREAK;

	if (mad_frame_decode(&frame, &stream)) {
		if (stream.error == MAD_ERROR_LOSTSYNC) {
			signed long tagsize = id3_tag_query(stream.this_frame,
							    stream.bufend -
							    stream.this_frame);
Avuton Olrich's avatar
Avuton Olrich committed
475
			if (tagsize > 0) {
476
				mad_stream_skip(&stream, tagsize);
477 478 479
				return DECODE_CONT;
			}
		}
480

481
		return RecoverFrameError(stream);
Warren Dukes's avatar
Warren Dukes committed
482 483 484 485 486
	}

	return DECODE_OK;
}

487
/* xing stuff stolen from alsaplayer, and heavily modified by jat */
488 489 490 491
static constexpr unsigned XI_MAGIC = (('X' << 8) | 'i');
static constexpr unsigned NG_MAGIC = (('n' << 8) | 'g');
static constexpr unsigned IN_MAGIC = (('I' << 8) | 'n');
static constexpr unsigned FO_MAGIC = (('f' << 8) | 'o');
492 493

enum xing_magic {
494
	XING_MAGIC_XING, /* VBR */
495
	XING_MAGIC_INFO  /* CBR */
496
};
Warren Dukes's avatar
Warren Dukes committed
497 498

struct xing {
499 500 501 502 503 504
	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
505 506
};

507 508 509 510
static const unsigned XING_FRAMES = 1;
static const unsigned XING_BYTES = 2;
static const unsigned XING_TOC = 4;
static const unsigned XING_SCALE = 8;
Warren Dukes's avatar
Warren Dukes committed
511

512
struct lame_version {
513 514
	unsigned major;
	unsigned minor;
515 516
};

517
struct lame {
518
	char encoder[10];       /* 9 byte encoder name/version ("LAME3.97b") */
519
	struct lame_version version; /* struct containing just the version */
520
	float peak;             /* replaygain peak */
Max Kellermann's avatar
Max Kellermann committed
521 522 523 524
	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 */
525
	int crc;                /* CRC of the first 190 bytes of this frame */
526 527
};

Max Kellermann's avatar
Max Kellermann committed
528 529
static bool
parse_xing(struct xing *xing, struct mad_bitptr *ptr, int *oldbitlen)
Warren Dukes's avatar
Warren Dukes committed
530
{
531
	int bitlen = *oldbitlen;
Warren Dukes's avatar
Warren Dukes committed
532

Max Kellermann's avatar
Max Kellermann committed
533 534 535
	if (bitlen < 16)
		return false;

536
	const unsigned long bits = mad_bit_read(ptr, 16);
537 538
	bitlen -= 16;

539
	if (bits == XI_MAGIC) {
Max Kellermann's avatar
Max Kellermann committed
540 541 542 543 544 545
		if (bitlen < 16)
			return false;

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

546
		bitlen -= 16;
547 548
		xing->magic = XING_MAGIC_XING;
	} else if (bits == IN_MAGIC) {
Max Kellermann's avatar
Max Kellermann committed
549 550 551 552 553 554
		if (bitlen < 16)
			return false;

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

555 556
		bitlen -= 16;
		xing->magic = XING_MAGIC_INFO;
557 558 559
	}
	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
560 561
	else
		return false;
562

Max Kellermann's avatar
Max Kellermann committed
563 564
	if (bitlen < 32)
		return false;
565
	xing->flags = mad_bit_read(ptr, 32);
566 567 568
	bitlen -= 32;

	if (xing->flags & XING_FRAMES) {
Max Kellermann's avatar
Max Kellermann committed
569 570
		if (bitlen < 32)
			return false;
571
		xing->frames = mad_bit_read(ptr, 32);
572 573 574 575
		bitlen -= 32;
	}

	if (xing->flags & XING_BYTES) {
Max Kellermann's avatar
Max Kellermann committed
576 577
		if (bitlen < 32)
			return false;
578
		xing->bytes = mad_bit_read(ptr, 32);
579 580
		bitlen -= 32;
	}
Warren Dukes's avatar
Warren Dukes committed
581

582
	if (xing->flags & XING_TOC) {
Max Kellermann's avatar
Max Kellermann committed
583 584
		if (bitlen < 800)
			return false;
585 586
		for (unsigned i = 0; i < 100; ++i)
			xing->toc[i] = mad_bit_read(ptr, 8);
587 588 589 590
		bitlen -= 800;
	}

	if (xing->flags & XING_SCALE) {
Max Kellermann's avatar
Max Kellermann committed
591 592
		if (bitlen < 32)
			return false;
593
		xing->scale = mad_bit_read(ptr, 32);
594 595 596
		bitlen -= 32;
	}

597 598
	/* 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 */
599
	const int bitsleft = 960 - (*oldbitlen - bitlen);
Max Kellermann's avatar
Max Kellermann committed
600 601
	if (bitsleft < 0)
		return false;
602 603 604 605 606
	else if (bitsleft > 0) {
		mad_bit_read(ptr, bitsleft);
		bitlen -= bitsleft;
	}

607
	*oldbitlen = bitlen;
608

Max Kellermann's avatar
Max Kellermann committed
609
	return true;
Warren Dukes's avatar
Warren Dukes committed
610 611
}

Max Kellermann's avatar
Max Kellermann committed
612 613
static bool
parse_lame(struct lame *lame, struct mad_bitptr *ptr, int *bitlen)
614 615 616
{
	/* Unlike the xing header, the lame tag has a fixed length.  Fail if
	 * not all 36 bytes (288 bits) are there. */
617
	if (*bitlen < 288)
Max Kellermann's avatar
Max Kellermann committed
618
		return false;
619

620
	for (unsigned i = 0; i < 9; i++)
621
		lame->encoder[i] = (char)mad_bit_read(ptr, 8);
622 623
	lame->encoder[9] = '\0';

624 625
	*bitlen -= 72;

626 627 628
	/* 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. */
629
	if (!StringStartsWith(lame->encoder, "LAME"))
Max Kellermann's avatar
Max Kellermann committed
630
		return false;
631 632 633

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

636 637
	FormatDebug(mad_domain, "detected LAME version %i.%i (\"%s\")",
		    lame->version.major, lame->version.minor, lame->encoder);
638 639 640 641 642 643 644 645

	/* 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 */
646
	int adj = 0;
647 648 649
	if (lame->version.major < 3 ||
	    (lame->version.major == 3 && lame->version.minor < 95))
		adj = 6;
650 651 652

	mad_bit_read(ptr, 16);

653
	lame->peak = mad_f_todouble(mad_bit_read(ptr, 32) << 5); /* peak */
654
	FormatDebug(mad_domain, "LAME peak found: %f", lame->peak);
655

Max Kellermann's avatar
Max Kellermann committed
656
	lame->track_gain = 0;
657 658 659
	unsigned name = mad_bit_read(ptr, 3); /* gain name */
	unsigned orig = mad_bit_read(ptr, 3); /* gain originator */
	unsigned sign = mad_bit_read(ptr, 1); /* sign bit */
660
	int gain = mad_bit_read(ptr, 9); /* gain*10 */
661
	if (gain && name == 1 && orig != 0) {
Max Kellermann's avatar
Max Kellermann committed
662
		lame->track_gain = ((sign ? -gain : gain) / 10.0) + adj;
663 664
		FormatDebug(mad_domain, "LAME track gain found: %f",
			    lame->track_gain);
665
	}
666

667 668 669 670
	/* 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
671
	lame->album_gain = 0;
672 673 674 675 676 677
#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
678
		lame->album_gain = ((sign ? -gain : gain) / 10.0) + adj;
679 680
		FormatDebug(mad_domain, "LAME album gain found: %f",
			    lame->track_gain);
681
	}
682
#else
683
	mad_bit_read(ptr, 16);
684 685
#endif

686 687
	mad_bit_read(ptr, 16);

Max Kellermann's avatar
Max Kellermann committed
688 689
	lame->encoder_delay = mad_bit_read(ptr, 12);
	lame->encoder_padding = mad_bit_read(ptr, 12);
690

691 692
	FormatDebug(mad_domain, "encoder delay is %i, encoder padding is %i",
		    lame->encoder_delay, lame->encoder_padding);
693

694 695 696 697 698
	mad_bit_read(ptr, 80);

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

	*bitlen -= 216;
699

Max Kellermann's avatar
Max Kellermann committed
700
	return true;
701 702
}

703
static inline SongTime
704 705
mp3_frame_duration(const struct mad_frame *frame)
{
706
	return ToSongTime(frame->header.duration);
707 708
}

709
inline offset_type
710
MadDecoder::ThisFrameOffset() const
711
{
712
	auto offset = input_stream.GetOffset();
713

714 715
	if (stream.this_frame != nullptr)
		offset -= stream.bufend - stream.this_frame;
716
	else
717
		offset -= stream.bufend - stream.buffer;
718

719 720 721
	return offset;
}

722
inline offset_type
723
MadDecoder::RestIncludingThisFrame() const
724
{
725
	return input_stream.GetSize() - ThisFrameOffset();
726 727
}

728 729
inline void
MadDecoder::FileSizeToSongLength()
730
{
731
	if (input_stream.KnownSize()) {
732
		offset_type rest = RestIncludingThisFrame();
733

734 735 736 737 738
		const SongTime frame_duration = mp3_frame_duration(&frame);
		const SongTime duration =
			SongTime::FromScale<uint64_t>(rest,
						      frame.header.bitrate / 8);
		total_time = duration;
739

740 741 742 743
		max_frames = (frame_duration.IsPositive()
			      ? duration.count() / frame_duration.count()
			      : 0)
			+ FRAMES_CUSHION;
744
	} else {
745
		max_frames = FRAMES_CUSHION;
746
		total_time = SignedSongTime::Negative();
747 748 749
	}
}

750
inline bool
Max Kellermann's avatar
Max Kellermann committed
751
MadDecoder::DecodeFirstFrame(Tag **tag)
752
{
753
	struct xing xing;
754
	xing.frames = 0;
755

Max Kellermann's avatar
Max Kellermann committed
756
	while (true) {
757
		enum mp3_action ret;
758
		do {
759
			ret = DecodeNextFrameHeader(tag);
760 761
		} while (ret == DECODE_CONT);
		if (ret == DECODE_BREAK)
Max Kellermann's avatar
Max Kellermann committed
762
			return false;
763
		if (ret == DECODE_SKIP) continue;
764

765
		do {
766
			ret = DecodeNextFrame();
767 768
		} while (ret == DECODE_CONT);
		if (ret == DECODE_BREAK)
Max Kellermann's avatar
Max Kellermann committed
769
			return false;
770
		if (ret == DECODE_OK) break;
Avuton Olrich's avatar
Avuton Olrich committed
771 772
	}

773 774
	struct mad_bitptr ptr = stream.anc_ptr;
	int bitlen = stream.anc_bitlen;
775

776
	FileSizeToSongLength();
777

778 779 780
	/*
	 * if an xing tag exists, use that!
	 */
781
	if (parse_xing(&xing, &ptr, &bitlen)) {
782
		mute_frame = MUTEFRAME_SKIP;
783

784
		if ((xing.flags & XING_FRAMES) && xing.frames) {
785
			mad_timer_t duration = frame.header.duration;
Avuton Olrich's avatar
Avuton Olrich committed
786
			mad_timer_multiply(&duration, xing.frames);
787
			total_time = ToSongTime(duration);
788
			max_frames = xing.frames;
Warren Dukes's avatar
Warren Dukes committed
789
		}
790

791
		struct lame lame;
792
		if (parse_lame(&lame, &ptr, &bitlen)) {
793
			if (gapless_playback && input_stream.IsSeekable()) {
794
				drop_start_samples = lame.encoder_delay +
795
				                           DECODERDELAY;
796
				drop_end_samples = lame.encoder_padding;
797 798 799 800
			}

			/* Album gain isn't currently used.  See comment in
			 * parse_lame() for details. -- jat */
801
			if (decoder != nullptr && !found_replay_gain &&
Max Kellermann's avatar
Max Kellermann committed
802
			    lame.track_gain) {
803
				ReplayGainInfo rgi;
804
				rgi.Clear();
805 806
				rgi.tuples[REPLAY_GAIN_TRACK].gain = lame.track_gain;
				rgi.tuples[REPLAY_GAIN_TRACK].peak = lame.peak;
807
				decoder_replay_gain(*decoder, &rgi);
808 809
			}
		}
810
	}
Warren Dukes's avatar
Warren Dukes committed
811

812
	if (!max_frames)
Max Kellermann's avatar
Max Kellermann committed
813
		return false;
814

815
	if (max_frames > 8 * 1024 * 1024) {
816 817 818
		FormatWarning(mad_domain,
			      "mp3 file header indicates too many frames: %lu",
			      max_frames);
Max Kellermann's avatar
Max Kellermann committed
819
		return false;
820 821
	}

822 823
	frame_offsets = new long[max_frames];
	times = new mad_timer_t[max_frames];
Warren Dukes's avatar
Warren Dukes committed
824

Max Kellermann's avatar
Max Kellermann committed
825
	return true;
Warren Dukes's avatar
Warren Dukes committed
826 827
}

828
MadDecoder::~MadDecoder()
Avuton Olrich's avatar
Avuton Olrich committed
829
{
830 831 832
	mad_synth_finish(&synth);
	mad_frame_finish(&frame);
	mad_stream_finish(&stream);
Warren Dukes's avatar
Warren Dukes committed
833

834 835
	delete[] frame_offsets;
	delete[] times;
Warren Dukes's avatar
Warren Dukes committed
836 837 838
}

/* this is primarily used for getting total time for tags */
839
static std::pair<bool, SignedSongTime>
840
mad_decoder_total_file_time(InputStream &is)
Avuton Olrich's avatar
Avuton Olrich committed
841
{
842 843
	MadDecoder data(nullptr, is);
	return data.DecodeFirstFrame(nullptr)
844 845
		? std::make_pair(true, data.total_time)
		: std::make_pair(false, SignedSongTime::Negative());
Warren Dukes's avatar
Warren Dukes committed
846 847
}

848
long
849
MadDecoder::TimeToFrame(SongTime t) const
850 851 852
{
	unsigned long i;

853
	for (i = 0; i < highest_frame; ++i) {
854
		auto frame_time = ToSongTime(times[i]);
855 856 857 858 859 860 861
		if (frame_time >= t)
			break;
	}

	return i;
}

862 863
void
MadDecoder::UpdateTimerNextFrame()
Avuton Olrich's avatar
Avuton Olrich committed
864
{
865 866 867 868 869 870 871 872
	if (current_frame >= highest_frame) {
		/* record this frame's properties in frame_offsets
		   (for seeking) and times */
		bit_rate = frame.header.bitrate;

		if (current_frame >= max_frames)
			/* cap current_frame */
			current_frame = max_frames - 1;
873
		else
874
			highest_frame++;
875

876
		frame_offsets[current_frame] = ThisFrameOffset();
877

878 879
		mad_timer_add(&timer, frame.header.duration);
		times[current_frame] = timer;
880
	} else
881 882
		/* get the new timer value from "times" */
		timer = times[current_frame];
883

884
	current_frame++;
885
	elapsed_time = ToSongTime(timer);
886 887
}

888
DecoderCommand
889
MadDecoder::SendPCM(unsigned i, unsigned pcm_length)
890
{
891
	unsigned max_samples = sizeof(output_buffer) /
892 893
		sizeof(output_buffer[0]) /
		MAD_NCHANNELS(&frame.header);
894 895 896 897 898 899 900 901

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

		i += num_samples;

902
		mad_fixed_to_24_buffer(output_buffer, &synth,
903
				       i - num_samples, i,
904 905
				       MAD_NCHANNELS(&frame.header));
		num_samples *= MAD_NCHANNELS(&frame.header);
906

907
		auto cmd = decoder_data(*decoder, input_stream, output_buffer,
908 909 910
					sizeof(output_buffer[0]) * num_samples,
					bit_rate / 1000);
		if (cmd != DecoderCommand::NONE)
911 912 913
			return cmd;
	}

914
	return DecoderCommand::NONE;
915 916
}

917
inline DecoderCommand
918
MadDecoder::SyncAndSend()
919
{
920 921 922 923 924 925 926 927 928
	mad_synth_frame(&synth, &frame);

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

931 932
	if (drop_start_frames > 0) {
		drop_start_frames--;
933
		return DecoderCommand::NONE;
934 935
	} else if ((drop_end_frames > 0) &&
		   (current_frame == (max_frames + 1 - drop_end_frames))) {
936 937
		/* stop decoding, effectively dropping all remaining
		   frames */
938
		return DecoderCommand::STOP;
939 940
	}

941 942 943 944 945
	unsigned i = 0;
	if (!decoded_first_frame) {
		i = drop_start_samples;
		decoded_first_frame = true;
	}
946

947 948 949 950
	unsigned pcm_length = synth.pcm.length;
	if (drop_end_samples &&
	    (current_frame == max_frames - drop_end_frames)) {
		if (drop_end_samples >= pcm_length)
951 952
			pcm_length = 0;
		else
953
			pcm_length -= drop_end_samples;
954 955
	}

956 957
	auto cmd = SendPCM(i, pcm_length);
	if (cmd != DecoderCommand::NONE)
958 959
		return cmd;

960 961
	if (drop_end_samples &&
	    (current_frame == max_frames - drop_end_frames))
962 963
		/* stop decoding, effectively dropping
		 * all remaining samples */
964
		return DecoderCommand::STOP;
965

966
	return DecoderCommand::NONE;
967 968
}

969 970
inline bool
MadDecoder::Read()
971
{
972
	UpdateTimerNextFrame();
Warren Dukes's avatar
Warren Dukes committed
973

974
	switch (mute_frame) {
975 976
		DecoderCommand cmd;

Avuton Olrich's avatar
Avuton Olrich committed
977
	case MUTEFRAME_SKIP:
978
		mute_frame = MUTEFRAME_NONE;
Avuton Olrich's avatar
Avuton Olrich committed
979 980
		break;
	case MUTEFRAME_SEEK:
981
		if (elapsed_time >= seek_time)
982
			mute_frame = MUTEFRAME_NONE;
Avuton Olrich's avatar
Avuton Olrich committed
983
		break;
984
	case MUTEFRAME_NONE:
985
		cmd = SyncAndSend();
986
		if (cmd == DecoderCommand::SEEK) {
987
			assert(input_stream.IsSeekable());
988

989
			unsigned long j =
990
				TimeToFrame(decoder_seek_time(*decoder));
991 992 993
			if (j < highest_frame) {
				if (Seek(frame_offsets[j])) {
					current_frame = j;
994
					decoder_command_finished(*decoder);
Avuton Olrich's avatar
Avuton Olrich committed
995
				} else
996
					decoder_seek_error(*decoder);
Max Kellermann's avatar
Max Kellermann committed
997
			} else {
998
				seek_time = decoder_seek_time(*decoder);
999
				mute_frame = MUTEFRAME_SEEK;
1000
				decoder_command_finished(*decoder);
Max Kellermann's avatar
Max Kellermann committed
1001
			}
1002
		} else if (cmd != DecoderCommand::NONE)
1003
			return false;
Warren Dukes's avatar
Warren Dukes committed
1004 1005
	}

Max Kellermann's avatar
Max Kellermann committed
1006
	while (true) {
1007
		enum mp3_action ret;
1008
		do {
Max Kellermann's avatar
Max Kellermann committed
1009
			Tag *tag = nullptr;
1010

1011
			ret = DecodeNextFrameHeader(&tag);
1012

1013
			if (tag != nullptr) {
1014
				decoder_tag(*decoder, input_stream,
1015
					    std::move(*tag));
Max Kellermann's avatar
Max Kellermann committed
1016
				delete tag;
1017
			}
1018 1019
		} while (ret == DECODE_CONT);
		if (ret == DECODE_BREAK)
1020
			return false;
1021 1022

		const bool skip = ret == DECODE_SKIP;
1023

1024
		if (mute_frame == MUTEFRAME_NONE) {
1025
			do {
1026
				ret = DecodeNextFrame();
1027 1028
			} while (ret == DECODE_CONT);
			if (ret == DECODE_BREAK)
1029
				return false;
Warren Dukes's avatar
Warren Dukes committed
1030
		}
1031

Avuton Olrich's avatar
Avuton Olrich committed
1032
		if (!skip && ret == DECODE_OK)
1033
			return true;
Warren Dukes's avatar
Warren Dukes committed
1034 1035 1036
	}
}

1037
static void
1038
mp3_decode(Decoder &decoder, InputStream &input_stream)
Avuton Olrich's avatar
Avuton Olrich committed
1039
{
1040
	MadDecoder data(&decoder, input_stream);
1041

Max Kellermann's avatar
Max Kellermann committed
1042
	Tag *tag = nullptr;
1043
	if (!data.DecodeFirstFrame(&tag)) {
Max Kellermann's avatar
Max Kellermann committed
1044
		delete tag;
1045

1046
		if (decoder_get_command(decoder) == DecoderCommand::NONE)
1047
			LogError(mad_domain,
Max Kellermann's avatar
Max Kellermann committed
1048
				 "input/Input does not appear to be a mp3 bit stream");
1049
		return;
Warren Dukes's avatar
Warren Dukes committed
1050 1051
	}

1052
	Error error;
1053 1054
	AudioFormat audio_format;
	if (!audio_format_init_checked(audio_format,
1055
				       data.frame.header.samplerate,
1056
				       SampleFormat::S24_P32,
1057
				       MAD_NCHANNELS(&data.frame.header),
1058
				       error)) {
1059
		LogError(error);
Max Kellermann's avatar
Max Kellermann committed
1060
		delete tag;
1061 1062
		return;
	}
Avuton Olrich's avatar
Avuton Olrich committed
1063

1064
	decoder_initialized(decoder, audio_format,
1065
			    input_stream.IsSeekable(),
1066
			    data.total_time);
Warren Dukes's avatar
Warren Dukes committed
1067

1068
	if (tag != nullptr) {
1069
		decoder_tag(decoder, input_stream, std::move(*tag));
Max Kellermann's avatar
Max Kellermann committed
1070
		delete tag;
1071 1072
	}

1073
	while (data.Read()) {}
Warren Dukes's avatar
Warren Dukes committed
1074 1075
}

1076
static bool
1077
mad_decoder_scan_stream(InputStream &is,
1078
			const struct tag_handler *handler, void *handler_ctx)
Avuton Olrich's avatar
Avuton Olrich committed
1079
{
1080 1081
	const auto result = mad_decoder_total_file_time(is);
	if (!result.first)
1082
		return false;
Warren Dukes's avatar
Warren Dukes committed
1083

1084 1085 1086
	if (!result.second.IsNegative())
		tag_handler_invoke_duration(handler, handler_ctx,
					    SongTime(result.second));
1087
	return true;
Warren Dukes's avatar
Warren Dukes committed
1088 1089
}

1090 1091
static const char *const mp3_suffixes[] = { "mp3", "mp2", nullptr };
static const char *const mp3_mime_types[] = { "audio/mpeg", nullptr };
Warren Dukes's avatar
Warren Dukes committed
1092

1093
const struct DecoderPlugin mad_decoder_plugin = {
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
	"mad",
	mp3_plugin_init,
	nullptr,
	mp3_decode,
	nullptr,
	nullptr,
	mad_decoder_scan_stream,
	nullptr,
	mp3_suffixes,
	mp3_mime_types,
Warren Dukes's avatar
Warren Dukes committed
1104
};