MadDecoderPlugin.cxx 24.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "tag/Id3Scan.hxx"
25
#include "tag/Id3ReplayGain.hxx"
26
#include "tag/Id3MixRamp.hxx"
27
#include "tag/Handler.hxx"
28
#include "tag/ReplayGain.hxx"
29
#include "tag/MixRampParser.hxx"
30
#include "pcm/CheckAudioFormat.hxx"
31
#include "util/Clamp.hxx"
32
#include "util/StringCompare.hxx"
33 34
#include "util/Domain.hxx"
#include "Log.hxx"
Warren Dukes's avatar
Warren Dukes committed
35 36

#include <mad.h>
37

38
#ifdef ENABLE_ID3TAG
39
#include "tag/Id3Unique.hxx"
40 41
#include <id3tag.h>
#endif
42

43 44
#include <cassert>

45 46 47 48
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

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

51 52 53 54 55
enum class MadDecoderAction {
	SKIP,
	BREAK,
	CONT,
	OK
56
};
Warren Dukes's avatar
Warren Dukes committed
57

58 59 60 61
enum class MadDecoderMuteFrame {
	NONE,
	SKIP,
	SEEK
62
};
63

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

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

69 70
gcc_const
static SongTime
71
ToSongTime(mad_timer_t t) noexcept
72 73 74 75
{
	return SongTime::FromMS(mad_timer_count(t, MAD_UNITS_MILLISECONDS));
}

76
static inline int32_t
77
mad_fixed_to_24_sample(mad_fixed_t sample) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
78
{
79
	static constexpr unsigned bits = 24;
80 81
	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
82

83 84
	/* round */
	sample = sample + (1L << (MAD_F_FRACBITS - bits));
Warren Dukes's avatar
Warren Dukes committed
85

86
	/* quantize */
87
	return Clamp(sample, MIN, MAX)
88
		>> (MAD_F_FRACBITS + 1 - bits);
Warren Dukes's avatar
Warren Dukes committed
89
}
Avuton Olrich's avatar
Avuton Olrich committed
90

91
static void
92
mad_fixed_to_24_buffer(int32_t *dest, const struct mad_pcm &src,
93
		       size_t start, size_t end,
94
		       unsigned int num_channels)
95
{
96
	for (size_t i = start; i < end; ++i)
97
		for (unsigned c = 0; c < num_channels; ++c)
98
			*dest++ = mad_fixed_to_24_sample(src.samples[c][i]);
99 100
}

101
class MadDecoder {
102 103
	static constexpr size_t READ_BUFFER_SIZE = 40960;

Warren Dukes's avatar
Warren Dukes committed
104 105 106 107
	struct mad_stream stream;
	struct mad_frame frame;
	struct mad_synth synth;
	mad_timer_t timer;
Max Kellermann's avatar
Max Kellermann committed
108
	unsigned char input_buffer[READ_BUFFER_SIZE];
109
	int32_t output_buffer[sizeof(mad_pcm::samples) / sizeof(mad_fixed_t)];
110
	SignedSongTime total_time;
111 112
	SongTime elapsed_time;
	SongTime seek_time;
113
	MadDecoderMuteFrame mute_frame = MadDecoderMuteFrame::NONE;
114 115
	long *frame_offsets = nullptr;
	mad_timer_t *times = nullptr;
116 117 118
	size_t highest_frame = 0;
	size_t max_frames = 0;
	size_t current_frame = 0;
119 120
	unsigned int drop_start_frames;
	unsigned int drop_end_frames;
121 122 123 124 125
	unsigned int drop_start_samples = 0;
	unsigned int drop_end_samples = 0;
	bool found_replay_gain = false;
	bool found_first_frame = false;
	bool decoded_first_frame = false;
126 127 128 129 130 131 132 133

	/**
	 * If this flag is true, then end-of-file was seen and a
	 * padding of 8 zero bytes were appended to #input_buffer, to
	 * allow libmad to decode the last frame.
	 */
	bool was_eof = false;

134
	DecoderClient *const client;
135
	InputStream &input_stream;
136
	enum mad_layer layer = mad_layer(0);
137

138
public:
139 140
	MadDecoder(DecoderClient *client, InputStream &input_stream) noexcept;
	~MadDecoder() noexcept;
141

142 143 144
	MadDecoder(const MadDecoder &) = delete;
	MadDecoder &operator=(const MadDecoder &) = delete;

145 146
	void RunDecoder() noexcept;
	bool RunScan(TagHandler &handler) noexcept;
147

148
private:
149 150 151
	bool Seek(long offset) noexcept;
	bool FillBuffer() noexcept;
	void ParseId3(size_t tagsize, Tag *tag) noexcept;
152
	MadDecoderAction DecodeNextFrame(bool skip, Tag *tag) noexcept;
153

154
	[[nodiscard]] gcc_pure
155
	offset_type ThisFrameOffset() const noexcept;
156

157
	[[nodiscard]] gcc_pure
158
	offset_type RestIncludingThisFrame() const noexcept;
159 160 161 162

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

165
	bool DecodeFirstFrame(Tag *tag) noexcept;
166

167
	void AllocateBuffers() noexcept {
168 169 170 171 172 173 174 175
		assert(max_frames > 0);
		assert(frame_offsets == nullptr);
		assert(times == nullptr);

		frame_offsets = new long[max_frames];
		times = new mad_timer_t[max_frames];
	}

176
	[[nodiscard]] gcc_pure
177
	size_t TimeToFrame(SongTime t) const noexcept;
178

179 180 181 182 183
	/**
	 * Record the current frame's offset in the "frame_offsets"
	 * buffer and go forward to the next frame, updating the
	 * attributes "current_frame" and "timer".
	 */
184
	void UpdateTimerNextFrame() noexcept;
185 186

	/**
187 188
	 * Sends the synthesized current frame via
	 * DecoderClient::SubmitData().
189
	 */
190
	DecoderCommand SubmitPCM(size_t start, size_t n) noexcept;
191 192 193

	/**
	 * Synthesize the current frame and send it via
194
	 * DecoderClient::SubmitData().
195
	 */
196
	DecoderCommand SynthAndSubmit() noexcept;
197

198 199 200 201
	/**
	 * @return false to stop decoding
	 */
	bool HandleCurrentFrame() noexcept;
202

203 204
	bool LoadNextFrame() noexcept;

205
	bool Read() noexcept;
Max Kellermann's avatar
Max Kellermann committed
206
};
Warren Dukes's avatar
Warren Dukes committed
207

208
MadDecoder::MadDecoder(DecoderClient *_client,
209
		       InputStream &_input_stream) noexcept
210
	:client(_client), input_stream(_input_stream)
Avuton Olrich's avatar
Avuton Olrich committed
211
{
212 213 214 215 216
	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
217 218
}

219
inline bool
220
MadDecoder::Seek(long offset) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
221
{
222 223
	try {
		input_stream.LockSeek(offset);
224
	} catch (...) {
Max Kellermann's avatar
Max Kellermann committed
225
		return false;
226
	}
227

228 229
	mad_stream_buffer(&stream, input_buffer, 0);
	stream.error = MAD_ERROR_NONE;
230

Max Kellermann's avatar
Max Kellermann committed
231
	return true;
232 233
}

234
inline bool
235
MadDecoder::FillBuffer() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
236
{
237 238 239 240
	/* amount of rest data still residing in the buffer */
	size_t rest_size = 0;

	size_t max_read_size = sizeof(input_buffer);
241
	unsigned char *dest = input_buffer;
Max Kellermann's avatar
Max Kellermann committed
242

243
	if (stream.next_frame != nullptr) {
244 245 246 247
		rest_size = stream.bufend - stream.next_frame;
		memmove(input_buffer, stream.next_frame, rest_size);
		dest += rest_size;
		max_read_size -= rest_size;
Warren Dukes's avatar
Warren Dukes committed
248 249
	}

250 251
	/* we've exhausted the read buffer, so give up!, these potential
	 * mp3 frames are way too big, and thus unlikely to be mp3 frames */
252
	if (max_read_size == 0)
Max Kellermann's avatar
Max Kellermann committed
253
		return false;
254

255 256 257
	size_t nbytes = decoder_read(client, input_stream,
				     dest, max_read_size);
	if (nbytes == 0) {
258 259 260 261 262 263
		if (was_eof || max_read_size < MAD_BUFFER_GUARD)
			return false;

		was_eof = true;
		nbytes = MAD_BUFFER_GUARD;
		memset(dest, 0, nbytes);
264
	}
265

266
	mad_stream_buffer(&stream, input_buffer, rest_size + nbytes);
267
	stream.error = MAD_ERROR_NONE;
Warren Dukes's avatar
Warren Dukes committed
268

Max Kellermann's avatar
Max Kellermann committed
269
	return true;
Warren Dukes's avatar
Warren Dukes committed
270 271
}

272
inline void
273
MadDecoder::ParseId3(size_t tagsize, Tag *mpd_tag) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
274
{
275
#ifdef ENABLE_ID3TAG
276
	std::unique_ptr<id3_byte_t[]> allocated;
277

278
	const id3_length_t count = stream.bufend - stream.this_frame;
279

280
	const id3_byte_t *id3_data;
Avuton Olrich's avatar
Avuton Olrich committed
281
	if (tagsize <= count) {
282 283
		id3_data = stream.this_frame;
		mad_stream_skip(&(stream), tagsize);
Avuton Olrich's avatar
Avuton Olrich committed
284
	} else {
285
		allocated = std::make_unique<id3_byte_t[]>(tagsize);
286
		memcpy(allocated.get(), stream.this_frame, count);
287
		mad_stream_skip(&(stream), count);
288

289
		if (!decoder_read_full(client, input_stream,
290
				       allocated.get() + count, tagsize - count)) {
291
			LogDebug(mad_domain, "error parsing ID3 tag");
Max Kellermann's avatar
Max Kellermann committed
292
			return;
Warren Dukes's avatar
Warren Dukes committed
293
		}
294

295
		id3_data = allocated.get();
296 297
	}

298
	const UniqueId3Tag id3_tag(id3_tag_parse(id3_data, tagsize));
299
	if (id3_tag == nullptr)
Max Kellermann's avatar
Max Kellermann committed
300
		return;
301

302 303
	if (mpd_tag != nullptr)
		*mpd_tag = tag_id3_import(id3_tag.get());
304

305
	if (client != nullptr) {
306
		ReplayGainInfo rgi;
307

308
		if (Id3ToReplayGainInfo(rgi, id3_tag.get())) {
309
			client->SubmitReplayGain(&rgi);
310
			found_replay_gain = true;
311
		}
312

313 314 315
		if (auto mix_ramp = Id3ToMixRampInfo(id3_tag.get());
		    mix_ramp.IsDefined())
			client->SubmitMixRamp(std::move(mix_ramp));
316 317
	}

318
#else /* !ENABLE_ID3TAG */
319 320 321 322 323
	(void)mpd_tag;

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

324
	size_t count = stream.bufend - stream.this_frame;
325 326

	if (tagsize <= count) {
327
		mad_stream_skip(&stream, tagsize);
328
	} else {
329
		mad_stream_skip(&stream, count);
330
		decoder_skip(client, input_stream, tagsize - count);
331
	}
332
#endif
333 334
}

335
#ifndef ENABLE_ID3TAG
336 337 338
/**
 * This function emulates libid3tag when it is disabled.  Instead of
 * doing a real analyzation of the frame, it just checks whether the
339 340
 * frame begins with the string "ID3".  If so, it returns the length
 * of the ID3 frame.
341 342
 */
static signed long
343
id3_tag_query(const void *p0, size_t length) noexcept
344
{
345
	const char *p = (const char *)p0;
346

347 348
	return length >= 10 && memcmp(p, "ID3", 3) == 0
		? (p[8] << 7) + p[9] + 10
349 350
		: 0;
}
351
#endif /* !ENABLE_ID3TAG */
352

353
static MadDecoderAction
354
RecoverFrameError(const struct mad_stream &stream) noexcept
355 356
{
	if (MAD_RECOVERABLE(stream.error))
357
		return MadDecoderAction::SKIP;
358

359 360 361
	FmtWarning(mad_domain,
		   "unrecoverable frame level error: {}",
		   mad_stream_errorstr(&stream));
362
	return MadDecoderAction::BREAK;
363 364
}

365
MadDecoderAction
366
MadDecoder::DecodeNextFrame(bool skip, Tag *tag) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
367
{
368 369
	if ((stream.buffer == nullptr || stream.error == MAD_ERROR_BUFLEN) &&
	    !FillBuffer())
370
		return MadDecoderAction::BREAK;
371

372
	if (mad_header_decode(&frame.header, &stream)) {
373 374 375
		if (stream.error == MAD_ERROR_BUFLEN)
			return MadDecoderAction::CONT;

376 377 378 379
		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
380 381

			if (tagsize > 0) {
382
				ParseId3((size_t)tagsize, tag);
383
				return MadDecoderAction::CONT;
384 385
			}
		}
386

387
		return RecoverFrameError(stream);
Warren Dukes's avatar
Warren Dukes committed
388
	}
389

390 391 392
	enum mad_layer new_layer = frame.header.layer;
	if (layer == (mad_layer)0) {
		if (new_layer != MAD_LAYER_II && new_layer != MAD_LAYER_III) {
393
			/* Only layer 2 and 3 have been tested to work */
394
			return MadDecoderAction::SKIP;
395
		}
396 397 398

		layer = new_layer;
	} else if (new_layer != layer) {
399
		/* Don't decode frames with a different layer than the first */
400
		return MadDecoderAction::SKIP;
401
	}
Warren Dukes's avatar
Warren Dukes committed
402

403
	if (!skip && mad_frame_decode(&frame, &stream))
404
		return RecoverFrameError(stream);
Warren Dukes's avatar
Warren Dukes committed
405

406
	return MadDecoderAction::OK;
Warren Dukes's avatar
Warren Dukes committed
407 408
}

409
/* xing stuff stolen from alsaplayer, and heavily modified by jat */
410 411 412 413
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');
414

Warren Dukes's avatar
Warren Dukes committed
415
struct xing {
416 417 418 419 420
	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 */
Warren Dukes's avatar
Warren Dukes committed
421 422
};

423 424 425 426
static constexpr unsigned XING_FRAMES = 1;
static constexpr unsigned XING_BYTES = 2;
static constexpr unsigned XING_TOC = 4;
static constexpr unsigned XING_SCALE = 8;
Warren Dukes's avatar
Warren Dukes committed
427

428
struct lame_version {
429 430
	unsigned major;
	unsigned minor;
431 432
};

433
struct lame {
434
	char encoder[10];       /* 9 byte encoder name/version ("LAME3.97b") */
435
	struct lame_version version; /* struct containing just the version */
436
	float peak;             /* replaygain peak */
Max Kellermann's avatar
Max Kellermann committed
437 438 439 440
	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 */
441
	int crc;                /* CRC of the first 190 bytes of this frame */
442 443
};

Max Kellermann's avatar
Max Kellermann committed
444
static bool
445
parse_xing(struct xing *xing, struct mad_bitptr *ptr, int *oldbitlen) noexcept
Warren Dukes's avatar
Warren Dukes committed
446
{
447
	int bitlen = *oldbitlen;
Warren Dukes's avatar
Warren Dukes committed
448

Max Kellermann's avatar
Max Kellermann committed
449 450 451
	if (bitlen < 16)
		return false;

452
	const unsigned long bits = mad_bit_read(ptr, 16);
453 454
	bitlen -= 16;

455
	if (bits == XI_MAGIC) {
Max Kellermann's avatar
Max Kellermann committed
456 457 458 459 460 461
		if (bitlen < 16)
			return false;

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

462
		bitlen -= 16;
463
	} else if (bits == IN_MAGIC) {
Max Kellermann's avatar
Max Kellermann committed
464 465 466 467 468 469
		if (bitlen < 16)
			return false;

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

470
		bitlen -= 16;
471
	}
472
	else if (bits != NG_MAGIC && bits != FO_MAGIC)
Max Kellermann's avatar
Max Kellermann committed
473
		return false;
474

Max Kellermann's avatar
Max Kellermann committed
475 476
	if (bitlen < 32)
		return false;
477
	xing->flags = mad_bit_read(ptr, 32);
478 479 480
	bitlen -= 32;

	if (xing->flags & XING_FRAMES) {
Max Kellermann's avatar
Max Kellermann committed
481 482
		if (bitlen < 32)
			return false;
483
		xing->frames = mad_bit_read(ptr, 32);
484 485 486 487
		bitlen -= 32;
	}

	if (xing->flags & XING_BYTES) {
Max Kellermann's avatar
Max Kellermann committed
488 489
		if (bitlen < 32)
			return false;
490
		xing->bytes = mad_bit_read(ptr, 32);
491 492
		bitlen -= 32;
	}
Warren Dukes's avatar
Warren Dukes committed
493

494
	if (xing->flags & XING_TOC) {
Max Kellermann's avatar
Max Kellermann committed
495 496
		if (bitlen < 800)
			return false;
497 498
		for (unsigned char & i : xing->toc)
			i = mad_bit_read(ptr, 8);
499 500 501 502
		bitlen -= 800;
	}

	if (xing->flags & XING_SCALE) {
Max Kellermann's avatar
Max Kellermann committed
503 504
		if (bitlen < 32)
			return false;
505
		xing->scale = mad_bit_read(ptr, 32);
506 507 508
		bitlen -= 32;
	}

509 510
	/* 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 */
511
	const int bitsleft = 960 - (*oldbitlen - bitlen);
Max Kellermann's avatar
Max Kellermann committed
512 513
	if (bitsleft < 0)
		return false;
514
	else if (bitsleft > 0) {
515
		mad_bit_skip(ptr, bitsleft);
516 517 518
		bitlen -= bitsleft;
	}

519
	*oldbitlen = bitlen;
520

Max Kellermann's avatar
Max Kellermann committed
521
	return true;
Warren Dukes's avatar
Warren Dukes committed
522 523
}

Max Kellermann's avatar
Max Kellermann committed
524
static bool
525
parse_lame(struct lame *lame, struct mad_bitptr *ptr, int *bitlen) noexcept
526 527 528
{
	/* Unlike the xing header, the lame tag has a fixed length.  Fail if
	 * not all 36 bytes (288 bits) are there. */
529
	if (*bitlen < 288)
Max Kellermann's avatar
Max Kellermann committed
530
		return false;
531

532
	for (unsigned i = 0; i < 9; i++)
533
		lame->encoder[i] = (char)mad_bit_read(ptr, 8);
534 535
	lame->encoder[9] = '\0';

536 537
	*bitlen -= 72;

538 539 540
	/* 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. */
541
	if (!StringStartsWith(lame->encoder, "LAME"))
Max Kellermann's avatar
Max Kellermann committed
542
		return false;
543 544 545

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

548 549
	FmtDebug(mad_domain, "detected LAME version {}.{} (\"{}\")",
		 lame->version.major, lame->version.minor, lame->encoder);
550 551 552 553 554 555 556 557

	/* 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 */
558
	int adj = 0;
559 560 561
	if (lame->version.major < 3 ||
	    (lame->version.major == 3 && lame->version.minor < 95))
		adj = 6;
562

563
	mad_bit_skip(ptr, 16);
564

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
	/* The lame peak value is a float multiplied by 2^23 and stored as an
	 * unsigned integer (it is always positive). MAD's fixed-point format uses
	 * 28 bits for the fractional part, so shift the 23 bit fraction up before
	 * converting to a float.
	 */
	unsigned long peak_int = mad_bit_read(ptr, 32);

#define LAME_PEAK_FRACBITS 23
#if MAD_F_FRACBITS > LAME_PEAK_FRACBITS
	peak_int <<= (MAD_F_FRACBITS - LAME_PEAK_FRACBITS);
#elif LAME_PEAK_FRACBITS > MAD_F_FRACBITS
	peak_int >>= (LAME_PEAK_FRACBITS - MAD_F_FRACBITS);
#endif

	lame->peak = mad_f_todouble(peak_int); /* peak */
580
	FmtDebug(mad_domain, "LAME peak found: {}", lame->peak);
581

Max Kellermann's avatar
Max Kellermann committed
582
	lame->track_gain = 0;
583 584 585
	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 */
586
	int gain = mad_bit_read(ptr, 9); /* gain*10 */
587
	if (gain && name == 1 && orig != 0) {
Rosen Penev's avatar
Rosen Penev committed
588
		lame->track_gain = ((sign ? -gain : gain) / 10.0f) + adj;
589 590
		FmtDebug(mad_domain, "LAME track gain found: {}",
			 lame->track_gain);
591
	}
592

593 594 595 596
	/* 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
597
	lame->album_gain = 0;
598 599 600 601 602 603
#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
604
		lame->album_gain = ((sign ? -gain : gain) / 10.0) + adj;
605 606
		FmtDebug(mad_domain, "LAME album gain found: {}",
			 lame->track_gain);
607
	}
608
#else
609
	mad_bit_skip(ptr, 16);
610 611
#endif

612
	mad_bit_skip(ptr, 16);
613

Max Kellermann's avatar
Max Kellermann committed
614 615
	lame->encoder_delay = mad_bit_read(ptr, 12);
	lame->encoder_padding = mad_bit_read(ptr, 12);
616

617 618
	FmtDebug(mad_domain, "encoder delay is {}, encoder padding is {}",
		 lame->encoder_delay, lame->encoder_padding);
619

620
	mad_bit_skip(ptr, 80);
621 622 623 624

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

	*bitlen -= 216;
625

Max Kellermann's avatar
Max Kellermann committed
626
	return true;
627 628
}

629
static inline SongTime
630
mad_frame_duration(const struct mad_frame *frame) noexcept
631
{
632
	return ToSongTime(frame->header.duration);
633 634
}

635
inline offset_type
636
MadDecoder::ThisFrameOffset() const noexcept
637
{
638
	auto offset = input_stream.GetOffset();
639

640 641
	if (stream.this_frame != nullptr)
		offset -= stream.bufend - stream.this_frame;
642
	else
643
		offset -= stream.bufend - stream.buffer;
644

645 646 647
	return offset;
}

648
inline offset_type
649
MadDecoder::RestIncludingThisFrame() const noexcept
650
{
651
	return input_stream.GetSize() - ThisFrameOffset();
652 653
}

654
inline void
655
MadDecoder::FileSizeToSongLength() noexcept
656
{
657
	if (input_stream.KnownSize()) {
658
		offset_type rest = RestIncludingThisFrame();
659

660
		const SongTime frame_duration = mad_frame_duration(&frame);
661 662 663 664
		const SongTime duration =
			SongTime::FromScale<uint64_t>(rest,
						      frame.header.bitrate / 8);
		total_time = duration;
665

666 667 668 669
		max_frames = (frame_duration.IsPositive()
			      ? duration.count() / frame_duration.count()
			      : 0)
			+ FRAMES_CUSHION;
670
	} else {
671
		max_frames = FRAMES_CUSHION;
672
		total_time = SignedSongTime::Negative();
673 674 675
	}
}

676
inline bool
677
MadDecoder::DecodeFirstFrame(Tag *tag) noexcept
678
{
679
	struct xing xing;
680

681 682 683 684 685
#if GCC_CHECK_VERSION(10,0)
	/* work around bogus -Wuninitialized in GCC 10 */
	xing.frames = 0;
#endif

Max Kellermann's avatar
Max Kellermann committed
686
	while (true) {
687 688 689 690 691
		const auto action = DecodeNextFrame(false, tag);
		switch (action) {
		case MadDecoderAction::SKIP:
		case MadDecoderAction::CONT:
			continue;
692

693
		case MadDecoderAction::BREAK:
Max Kellermann's avatar
Max Kellermann committed
694
			return false;
695 696 697 698 699 700

		case MadDecoderAction::OK:
			break;
		}

		break;
Avuton Olrich's avatar
Avuton Olrich committed
701 702
	}

703 704
	struct mad_bitptr ptr = stream.anc_ptr;
	int bitlen = stream.anc_bitlen;
705

706
	FileSizeToSongLength();
707

708 709 710
	/*
	 * if an xing tag exists, use that!
	 */
711
	if (parse_xing(&xing, &ptr, &bitlen)) {
712
		mute_frame = MadDecoderMuteFrame::SKIP;
713

714
		if ((xing.flags & XING_FRAMES) && xing.frames) {
715
			mad_timer_t duration = frame.header.duration;
Avuton Olrich's avatar
Avuton Olrich committed
716
			mad_timer_multiply(&duration, xing.frames);
717
			total_time = ToSongTime(duration);
718
			max_frames = xing.frames;
Warren Dukes's avatar
Warren Dukes committed
719
		}
720

721
		struct lame lame;
722
		if (parse_lame(&lame, &ptr, &bitlen)) {
723
			if (input_stream.IsSeekable()) {
724 725 726 727
				/* libmad inserts 529 samples of
				   silence at the beginning and
				   removes those 529 samples at the
				   end */
728
				drop_start_samples = lame.encoder_delay +
729
				                           DECODERDELAY;
730
				drop_end_samples = lame.encoder_padding;
731 732 733 734
				if (drop_end_samples > DECODERDELAY)
					drop_end_samples -= DECODERDELAY;
				else
					drop_end_samples = 0;
735 736 737 738
			}

			/* Album gain isn't currently used.  See comment in
			 * parse_lame() for details. -- jat */
739
			if (client != nullptr && !found_replay_gain &&
Rosen Penev's avatar
Rosen Penev committed
740
			    lame.track_gain > 0.0f) {
741
				ReplayGainInfo rgi;
742
				rgi.Clear();
743 744
				rgi.track.gain = lame.track_gain;
				rgi.track.peak = lame.peak;
745
				client->SubmitReplayGain(&rgi);
746 747
			}
		}
748
	}
Warren Dukes's avatar
Warren Dukes committed
749

750
	if (!max_frames)
Max Kellermann's avatar
Max Kellermann committed
751
		return false;
752

753
	if (max_frames > 8 * 1024 * 1024) {
754 755 756
		FmtWarning(mad_domain,
			   "mp3 file header indicates too many frames: {}",
			   max_frames);
Max Kellermann's avatar
Max Kellermann committed
757
		return false;
758 759
	}

Max Kellermann's avatar
Max Kellermann committed
760
	return true;
Warren Dukes's avatar
Warren Dukes committed
761 762
}

763
MadDecoder::~MadDecoder() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
764
{
765 766 767
	mad_synth_finish(&synth);
	mad_frame_finish(&frame);
	mad_stream_finish(&stream);
Warren Dukes's avatar
Warren Dukes committed
768

769 770
	delete[] frame_offsets;
	delete[] times;
Warren Dukes's avatar
Warren Dukes committed
771 772
}

773
size_t
774
MadDecoder::TimeToFrame(SongTime t) const noexcept
775
{
776
	size_t i;
777

778
	for (i = 0; i < highest_frame; ++i) {
779
		auto frame_time = ToSongTime(times[i]);
780 781 782 783 784 785 786
		if (frame_time >= t)
			break;
	}

	return i;
}

787
void
788
MadDecoder::UpdateTimerNextFrame() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
789
{
790 791 792 793 794 795 796
	if (current_frame >= highest_frame) {
		/* record this frame's properties in frame_offsets
		   (for seeking) and times */

		if (current_frame >= max_frames)
			/* cap current_frame */
			current_frame = max_frames - 1;
797
		else
798
			highest_frame++;
799

800
		frame_offsets[current_frame] = ThisFrameOffset();
801

802 803
		mad_timer_add(&timer, frame.header.duration);
		times[current_frame] = timer;
804
	} else
805 806
		/* get the new timer value from "times" */
		timer = times[current_frame];
807

808
	current_frame++;
809
	elapsed_time = ToSongTime(timer);
810 811
}

812
DecoderCommand
813
MadDecoder::SubmitPCM(size_t i, size_t pcm_length) noexcept
814
{
815 816
	assert(i <= pcm_length);

817
	size_t num_samples = pcm_length - i;
818

819
	mad_fixed_to_24_buffer(output_buffer, synth.pcm,
820 821 822
			       i, i + num_samples,
			       MAD_NCHANNELS(&frame.header));
	num_samples *= MAD_NCHANNELS(&frame.header);
823

824 825 826
	return client->SubmitData(input_stream, output_buffer,
				  sizeof(output_buffer[0]) * num_samples,
				  frame.header.bitrate / 1000);
827 828
}

829
inline DecoderCommand
830
MadDecoder::SynthAndSubmit() noexcept
831
{
832 833 834 835 836 837 838 839 840
	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;
841 842
	}

843 844
	if (drop_start_frames > 0) {
		drop_start_frames--;
845
		return DecoderCommand::NONE;
846
	} else if ((drop_end_frames > 0) &&
847
		   current_frame == max_frames - drop_end_frames) {
848 849
		/* stop decoding, effectively dropping all remaining
		   frames */
850
		return DecoderCommand::STOP;
851 852
	}

853
	size_t i = 0;
854 855 856 857
	if (!decoded_first_frame) {
		i = drop_start_samples;
		decoded_first_frame = true;
	}
858

859
	size_t pcm_length = synth.pcm.length;
860
	if (drop_end_samples &&
861
	    current_frame == max_frames - drop_end_frames - 1) {
862
		if (i + drop_end_samples >= pcm_length)
863 864 865
			return DecoderCommand::STOP;

		pcm_length -= drop_end_samples;
866 867
	}

868
	auto cmd = SubmitPCM(i, pcm_length);
869
	if (cmd != DecoderCommand::NONE)
870 871
		return cmd;

872
	if (drop_end_samples &&
873
	    current_frame == max_frames - drop_end_frames - 1)
874 875
		/* stop decoding, effectively dropping
		 * all remaining samples */
876
		return DecoderCommand::STOP;
877

878
	return DecoderCommand::NONE;
879 880
}

881
inline bool
882
MadDecoder::HandleCurrentFrame() noexcept
883
{
884
	switch (mute_frame) {
885 886
	case MadDecoderMuteFrame::SKIP:
		mute_frame = MadDecoderMuteFrame::NONE;
Avuton Olrich's avatar
Avuton Olrich committed
887
		break;
888
	case MadDecoderMuteFrame::SEEK:
889
		if (elapsed_time >= seek_time)
890
			mute_frame = MadDecoderMuteFrame::NONE;
891
		UpdateTimerNextFrame();
Avuton Olrich's avatar
Avuton Olrich committed
892
		break;
893 894
	case MadDecoderMuteFrame::NONE: {
		const auto cmd = SynthAndSubmit();
895
		UpdateTimerNextFrame();
896
		if (cmd == DecoderCommand::SEEK) {
897
			assert(input_stream.IsSeekable());
898

899
			const auto t = client->GetSeekTime();
900
			size_t j = TimeToFrame(t);
901 902 903
			if (j < highest_frame) {
				if (Seek(frame_offsets[j])) {
					current_frame = j;
904
					was_eof = false;
905
					client->CommandFinished();
Avuton Olrich's avatar
Avuton Olrich committed
906
				} else
907
					client->SeekError();
Max Kellermann's avatar
Max Kellermann committed
908
			} else {
909
				seek_time = t;
910
				mute_frame = MadDecoderMuteFrame::SEEK;
911
				client->CommandFinished();
Max Kellermann's avatar
Max Kellermann committed
912
			}
913
		} else if (cmd != DecoderCommand::NONE)
914
			return false;
Warren Dukes's avatar
Warren Dukes committed
915
	}
916
	}
Warren Dukes's avatar
Warren Dukes committed
917

918 919 920 921
	return true;
}

inline bool
922
MadDecoder::LoadNextFrame() noexcept
923
{
Max Kellermann's avatar
Max Kellermann committed
924
	while (true) {
925
		Tag tag;
926

927 928 929 930 931
		const auto action =
			DecodeNextFrame(mute_frame != MadDecoderMuteFrame::NONE,
					&tag);
		if (!tag.IsEmpty())
			client->SubmitTag(input_stream, std::move(tag));
932

933 934 935 936
		switch (action) {
		case MadDecoderAction::SKIP:
		case MadDecoderAction::CONT:
			continue;
937

938 939
		case MadDecoderAction::BREAK:
			return false;
940

941
		case MadDecoderAction::OK:
942
			return true;
943
		}
Warren Dukes's avatar
Warren Dukes committed
944 945 946
	}
}

947 948
inline bool
MadDecoder::Read() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
949
{
950 951 952 953
	return HandleCurrentFrame() &&
		LoadNextFrame();
}

954 955
inline void
MadDecoder::RunDecoder() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
956
{
957
	assert(client != nullptr);
958

959
	Tag tag;
960 961
	if (!DecodeFirstFrame(&tag)) {
		if (client->GetCommand() == DecoderCommand::NONE)
962
			LogError(mad_domain,
963
				 "input does not appear to be a mp3 bit stream");
964
		return;
Warren Dukes's avatar
Warren Dukes committed
965 966
	}

967
	AllocateBuffers();
968

969 970 971 972 973
	client->Ready(CheckAudioFormat(frame.header.samplerate,
				       SampleFormat::S24_P32,
				       MAD_NCHANNELS(&frame.header)),
		      input_stream.IsSeekable(),
		      total_time);
Warren Dukes's avatar
Warren Dukes committed
974

975
	if (!tag.IsEmpty())
976
		client->SubmitTag(input_stream, std::move(tag));
977

978
	while (Read()) {}
Warren Dukes's avatar
Warren Dukes committed
979 980
}

981 982
static void
mad_decode(DecoderClient &client, InputStream &input_stream)
Avuton Olrich's avatar
Avuton Olrich committed
983
{
984 985 986 987 988 989 990 991
	MadDecoder data(&client, input_stream);
	data.RunDecoder();
}

inline bool
MadDecoder::RunScan(TagHandler &handler) noexcept
{
	if (!DecodeFirstFrame(nullptr))
992
		return false;
Warren Dukes's avatar
Warren Dukes committed
993

994 995
	if (!total_time.IsNegative())
		handler.OnDuration(SongTime(total_time));
996 997

	try {
998
		handler.OnAudioFormat(CheckAudioFormat(frame.header.samplerate,
999
						       SampleFormat::S24_P32,
1000
						       MAD_NCHANNELS(&frame.header)));
1001 1002 1003
	} catch (...) {
	}

1004
	return true;
Warren Dukes's avatar
Warren Dukes committed
1005 1006
}

1007
static bool
1008
mad_decoder_scan_stream(InputStream &is, TagHandler &handler)
1009 1010 1011 1012 1013
{
	MadDecoder data(nullptr, is);
	return data.RunScan(handler);
}

1014 1015
static const char *const mad_suffixes[] = { "mp3", "mp2", nullptr };
static const char *const mad_mime_types[] = { "audio/mpeg", nullptr };
Warren Dukes's avatar
Warren Dukes committed
1016

1017
constexpr DecoderPlugin mad_decoder_plugin =
1018 1019 1020
	DecoderPlugin("mad", mad_decode, mad_decoder_scan_stream)
	.WithSuffixes(mad_suffixes)
	.WithMimeTypes(mad_mime_types);