1 /*
2  * Copyright 2003-2021 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include "config.h"
21 #include "MadDecoderPlugin.hxx"
22 #include "../DecoderAPI.hxx"
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 #include "util/Domain.hxx"
34 #include "Log.hxx"
35 
36 #include <mad.h>
37 
38 #ifdef ENABLE_ID3TAG
39 #include "tag/Id3Unique.hxx"
40 #include <id3tag.h>
41 #endif
42 
43 #include <cassert>
44 
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 
49 static constexpr unsigned long FRAMES_CUSHION = 2000;
50 
51 enum class MadDecoderAction {
52 	SKIP,
53 	BREAK,
54 	CONT,
55 	OK
56 };
57 
58 enum class MadDecoderMuteFrame {
59 	NONE,
60 	SKIP,
61 	SEEK
62 };
63 
64 /* the number of samples of silence the decoder inserts at start */
65 static constexpr unsigned DECODERDELAY = 529;
66 
67 static constexpr Domain mad_domain("mad");
68 
69 gcc_const
70 static SongTime
ToSongTime(mad_timer_t t)71 ToSongTime(mad_timer_t t) noexcept
72 {
73 	return SongTime::FromMS(mad_timer_count(t, MAD_UNITS_MILLISECONDS));
74 }
75 
76 static inline int32_t
mad_fixed_to_24_sample(mad_fixed_t sample)77 mad_fixed_to_24_sample(mad_fixed_t sample) noexcept
78 {
79 	static constexpr unsigned bits = 24;
80 	static constexpr mad_fixed_t MIN = -MAD_F_ONE;
81 	static constexpr mad_fixed_t MAX = MAD_F_ONE - 1;
82 
83 	/* round */
84 	sample = sample + (1L << (MAD_F_FRACBITS - bits));
85 
86 	/* quantize */
87 	return Clamp(sample, MIN, MAX)
88 		>> (MAD_F_FRACBITS + 1 - bits);
89 }
90 
91 static void
mad_fixed_to_24_buffer(int32_t * dest,const struct mad_pcm & src,size_t start,size_t end,unsigned int num_channels)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 	static constexpr size_t READ_BUFFER_SIZE = 40960;
103 
104 	struct mad_stream stream;
105 	struct mad_frame frame;
106 	struct mad_synth synth;
107 	mad_timer_t timer;
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 	SongTime elapsed_time;
112 	SongTime seek_time;
113 	MadDecoderMuteFrame mute_frame = MadDecoderMuteFrame::NONE;
114 	long *frame_offsets = nullptr;
115 	mad_timer_t *times = nullptr;
116 	size_t highest_frame = 0;
117 	size_t max_frames = 0;
118 	size_t current_frame = 0;
119 	unsigned int drop_start_frames;
120 	unsigned int drop_end_frames;
121 	unsigned int drop_start_samples = 0;
122 	unsigned int drop_end_samples = 0;
123 	bool found_replay_gain = false;
124 	bool found_first_frame = false;
125 	bool decoded_first_frame = false;
126 
127 	/**
128 	 * If this flag is true, then end-of-file was seen and a
129 	 * padding of 8 zero bytes were appended to #input_buffer, to
130 	 * allow libmad to decode the last frame.
131 	 */
132 	bool was_eof = false;
133 
134 	DecoderClient *const client;
135 	InputStream &input_stream;
136 	enum mad_layer layer = mad_layer(0);
137 
138 public:
139 	MadDecoder(DecoderClient *client, InputStream &input_stream) noexcept;
140 	~MadDecoder() noexcept;
141 
142 	MadDecoder(const MadDecoder &) = delete;
143 	MadDecoder &operator=(const MadDecoder &) = delete;
144 
145 	void RunDecoder() noexcept;
146 	bool RunScan(TagHandler &handler) noexcept;
147 
148 private:
149 	bool Seek(long offset) noexcept;
150 	bool FillBuffer() noexcept;
151 	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 	 * Attempt to calulcate the length of the song from filesize
162 	 */
163 	void FileSizeToSongLength() noexcept;
164 
165 	bool DecodeFirstFrame(Tag *tag) noexcept;
166 
AllocateBuffers()167 	void AllocateBuffers() noexcept {
168 		assert(max_frames > 0);
169 		assert(frame_offsets == nullptr);
170 		assert(times == nullptr);
171 
172 		frame_offsets = new long[max_frames];
173 		times = new mad_timer_t[max_frames];
174 	}
175 
176 	[[nodiscard]] gcc_pure
177 	size_t TimeToFrame(SongTime t) const noexcept;
178 
179 	/**
180 	 * Record the current frame's offset in the "frame_offsets"
181 	 * buffer and go forward to the next frame, updating the
182 	 * attributes "current_frame" and "timer".
183 	 */
184 	void UpdateTimerNextFrame() noexcept;
185 
186 	/**
187 	 * Sends the synthesized current frame via
188 	 * 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 	 * @return false to stop decoding
200 	 */
201 	bool HandleCurrentFrame() noexcept;
202 
203 	bool LoadNextFrame() noexcept;
204 
205 	bool Read() noexcept;
206 };
207 
MadDecoder(DecoderClient * _client,InputStream & _input_stream)208 MadDecoder::MadDecoder(DecoderClient *_client,
209 		       InputStream &_input_stream) noexcept
210 	:client(_client), input_stream(_input_stream)
211 {
212 	mad_stream_init(&stream);
213 	mad_stream_options(&stream, MAD_OPTION_IGNORECRC);
214 	mad_frame_init(&frame);
215 	mad_synth_init(&synth);
216 	mad_timer_reset(&timer);
217 }
218 
219 inline bool
Seek(long offset)220 MadDecoder::Seek(long offset) noexcept
221 {
222 	try {
223 		input_stream.LockSeek(offset);
224 	} catch (...) {
225 		return false;
226 	}
227 
228 	mad_stream_buffer(&stream, input_buffer, 0);
229 	stream.error = MAD_ERROR_NONE;
230 
231 	return true;
232 }
233 
234 inline bool
FillBuffer()235 MadDecoder::FillBuffer() noexcept
236 {
237 	/* amount of rest data still residing in the buffer */
238 	size_t rest_size = 0;
239 
240 	size_t max_read_size = sizeof(input_buffer);
241 	unsigned char *dest = input_buffer;
242 
243 	if (stream.next_frame != nullptr) {
244 		rest_size = stream.bufend - stream.next_frame;
245 		memmove(input_buffer, stream.next_frame, rest_size);
246 		dest += rest_size;
247 		max_read_size -= rest_size;
248 	}
249 
250 	/* we've exhausted the read buffer, so give up!, these potential
251 	 * mp3 frames are way too big, and thus unlikely to be mp3 frames */
252 	if (max_read_size == 0)
253 		return false;
254 
255 	size_t nbytes = decoder_read(client, input_stream,
256 				     dest, max_read_size);
257 	if (nbytes == 0) {
258 		if (was_eof || max_read_size < MAD_BUFFER_GUARD)
259 			return false;
260 
261 		was_eof = true;
262 		nbytes = MAD_BUFFER_GUARD;
263 		memset(dest, 0, nbytes);
264 	}
265 
266 	mad_stream_buffer(&stream, input_buffer, rest_size + nbytes);
267 	stream.error = MAD_ERROR_NONE;
268 
269 	return true;
270 }
271 
272 inline void
ParseId3(size_t tagsize,Tag * mpd_tag)273 MadDecoder::ParseId3(size_t tagsize, Tag *mpd_tag) noexcept
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;
281 	if (tagsize <= count) {
282 		id3_data = stream.this_frame;
283 		mad_stream_skip(&(stream), tagsize);
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");
292 			return;
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)
300 		return;
301 
302 	if (mpd_tag != nullptr)
303 		*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 		if (auto mix_ramp = Id3ToMixRampInfo(id3_tag.get());
314 		    mix_ramp.IsDefined())
315 			client->SubmitMixRamp(std::move(mix_ramp));
316 	}
317 
318 #else /* !ENABLE_ID3TAG */
319 	(void)mpd_tag;
320 
321 	/* This code is enabled when libid3tag is disabled.  Instead
322 	   of parsing the ID3 frame, it just skips it. */
323 
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  * This function emulates libid3tag when it is disabled.  Instead of
338  * doing a real analyzation of the frame, it just checks whether the
339  * frame begins with the string "ID3".  If so, it returns the length
340  * of the ID3 frame.
341  */
342 static signed long
id3_tag_query(const void * p0,size_t length)343 id3_tag_query(const void *p0, size_t length) noexcept
344 {
345 	const char *p = (const char *)p0;
346 
347 	return length >= 10 && memcmp(p, "ID3", 3) == 0
348 		? (p[8] << 7) + p[9] + 10
349 		: 0;
350 }
351 #endif /* !ENABLE_ID3TAG */
352 
353 static MadDecoderAction
RecoverFrameError(const struct mad_stream & stream)354 RecoverFrameError(const struct mad_stream &stream) noexcept
355 {
356 	if (MAD_RECOVERABLE(stream.error))
357 		return MadDecoderAction::SKIP;
358 
359 	FmtWarning(mad_domain,
360 		   "unrecoverable frame level error: {}",
361 		   mad_stream_errorstr(&stream));
362 	return MadDecoderAction::BREAK;
363 }
364 
365 MadDecoderAction
DecodeNextFrame(bool skip,Tag * tag)366 MadDecoder::DecodeNextFrame(bool skip, Tag *tag) noexcept
367 {
368 	if ((stream.buffer == nullptr || stream.error == MAD_ERROR_BUFLEN) &&
369 	    !FillBuffer())
370 		return MadDecoderAction::BREAK;
371 
372 	if (mad_header_decode(&frame.header, &stream)) {
373 		if (stream.error == MAD_ERROR_BUFLEN)
374 			return MadDecoderAction::CONT;
375 
376 		if (stream.error == MAD_ERROR_LOSTSYNC && stream.this_frame) {
377 			signed long tagsize = id3_tag_query(stream.this_frame,
378 							    stream.bufend -
379 							    stream.this_frame);
380 
381 			if (tagsize > 0) {
382 				ParseId3((size_t)tagsize, tag);
383 				return MadDecoderAction::CONT;
384 			}
385 		}
386 
387 		return RecoverFrameError(stream);
388 	}
389 
390 	enum mad_layer new_layer = frame.header.layer;
391 	if (layer == (mad_layer)0) {
392 		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 		layer = new_layer;
398 	} else if (new_layer != layer) {
399 		/* Don't decode frames with a different layer than the first */
400 		return MadDecoderAction::SKIP;
401 	}
402 
403 	if (!skip && mad_frame_decode(&frame, &stream))
404 		return RecoverFrameError(stream);
405 
406 	return MadDecoderAction::OK;
407 }
408 
409 /* xing stuff stolen from alsaplayer, and heavily modified by jat */
410 static constexpr unsigned XI_MAGIC = (('X' << 8) | 'i');
411 static constexpr unsigned NG_MAGIC = (('n' << 8) | 'g');
412 static constexpr unsigned IN_MAGIC = (('I' << 8) | 'n');
413 static constexpr unsigned FO_MAGIC = (('f' << 8) | 'o');
414 
415 struct xing {
416 	long flags;             /* valid fields (see below) */
417 	unsigned long frames;   /* total number of frames */
418 	unsigned long bytes;    /* total number of bytes */
419 	unsigned char toc[100]; /* 100-point seek table */
420 	long scale;             /* VBR quality */
421 };
422 
423 static constexpr unsigned XING_FRAMES = 1;
424 static constexpr unsigned XING_BYTES = 2;
425 static constexpr unsigned XING_TOC = 4;
426 static constexpr unsigned XING_SCALE = 8;
427 
428 struct lame_version {
429 	unsigned major;
430 	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 */
437 	float track_gain;       /* replaygain track gain */
438 	float album_gain;       /* replaygain album gain */
439 	int encoder_delay;      /* # of added samples at start of mp3 */
440 	int encoder_padding;    /* # of added samples at end of mp3 */
441 	int crc;                /* CRC of the first 190 bytes of this frame */
442 };
443 
444 static bool
parse_xing(struct xing * xing,struct mad_bitptr * ptr,int * oldbitlen)445 parse_xing(struct xing *xing, struct mad_bitptr *ptr, int *oldbitlen) noexcept
446 {
447 	int bitlen = *oldbitlen;
448 
449 	if (bitlen < 16)
450 		return false;
451 
452 	const unsigned long bits = mad_bit_read(ptr, 16);
453 	bitlen -= 16;
454 
455 	if (bits == XI_MAGIC) {
456 		if (bitlen < 16)
457 			return false;
458 
459 		if (mad_bit_read(ptr, 16) != NG_MAGIC)
460 			return false;
461 
462 		bitlen -= 16;
463 	} else if (bits == IN_MAGIC) {
464 		if (bitlen < 16)
465 			return false;
466 
467 		if (mad_bit_read(ptr, 16) != FO_MAGIC)
468 			return false;
469 
470 		bitlen -= 16;
471 	}
472 	else if (bits != NG_MAGIC && bits != FO_MAGIC)
473 		return false;
474 
475 	if (bitlen < 32)
476 		return false;
477 	xing->flags = mad_bit_read(ptr, 32);
478 	bitlen -= 32;
479 
480 	if (xing->flags & XING_FRAMES) {
481 		if (bitlen < 32)
482 			return false;
483 		xing->frames = mad_bit_read(ptr, 32);
484 		bitlen -= 32;
485 	}
486 
487 	if (xing->flags & XING_BYTES) {
488 		if (bitlen < 32)
489 			return false;
490 		xing->bytes = mad_bit_read(ptr, 32);
491 		bitlen -= 32;
492 	}
493 
494 	if (xing->flags & XING_TOC) {
495 		if (bitlen < 800)
496 			return false;
497 		for (unsigned char & i : xing->toc)
498 			i = mad_bit_read(ptr, 8);
499 		bitlen -= 800;
500 	}
501 
502 	if (xing->flags & XING_SCALE) {
503 		if (bitlen < 32)
504 			return false;
505 		xing->scale = mad_bit_read(ptr, 32);
506 		bitlen -= 32;
507 	}
508 
509 	/* Make sure we consume no less than 120 bytes (960 bits) in hopes that
510 	 * the LAME tag is found there, and not right after the Xing header */
511 	const int bitsleft = 960 - (*oldbitlen - bitlen);
512 	if (bitsleft < 0)
513 		return false;
514 	else if (bitsleft > 0) {
515 		mad_bit_skip(ptr, bitsleft);
516 		bitlen -= bitsleft;
517 	}
518 
519 	*oldbitlen = bitlen;
520 
521 	return true;
522 }
523 
524 static bool
parse_lame(struct lame * lame,struct mad_bitptr * ptr,int * bitlen)525 parse_lame(struct lame *lame, struct mad_bitptr *ptr, int *bitlen) noexcept
526 {
527 	/* Unlike the xing header, the lame tag has a fixed length.  Fail if
528 	 * not all 36 bytes (288 bits) are there. */
529 	if (*bitlen < 288)
530 		return false;
531 
532 	for (unsigned i = 0; i < 9; i++)
533 		lame->encoder[i] = (char)mad_bit_read(ptr, 8);
534 	lame->encoder[9] = '\0';
535 
536 	*bitlen -= 72;
537 
538 	/* This is technically incorrect, since the encoder might not be lame.
539 	 * But there's no other way to determine if this is a lame tag, and we
540 	 * wouldn't want to go reading a tag that's not there. */
541 	if (!StringStartsWith(lame->encoder, "LAME"))
542 		return false;
543 
544 	if (sscanf(lame->encoder+4, "%u.%u",
545 	           &lame->version.major, &lame->version.minor) != 2)
546 		return false;
547 
548 	FmtDebug(mad_domain, "detected LAME version {}.{} (\"{}\")",
549 		 lame->version.major, lame->version.minor, lame->encoder);
550 
551 	/* The reference volume was changed from the 83dB used in the
552 	 * ReplayGain spec to 89dB in lame 3.95.1.  Bump the gain for older
553 	 * versions, since everyone else uses 89dB instead of 83dB.
554 	 * Unfortunately, lame didn't differentiate between 3.95 and 3.95.1, so
555 	 * it's impossible to make the proper adjustment for 3.95.
556 	 * Fortunately, 3.95 was only out for about a day before 3.95.1 was
557 	 * released. -- tmz */
558 	int adj = 0;
559 	if (lame->version.major < 3 ||
560 	    (lame->version.major == 3 && lame->version.minor < 95))
561 		adj = 6;
562 
563 	mad_bit_skip(ptr, 16);
564 
565 	lame->peak = MAD_F(mad_bit_read(ptr, 32) << 5); /* peak */
566 	FmtDebug(mad_domain, "LAME peak found: {}", lame->peak);
567 
568 	lame->track_gain = 0;
569 	unsigned name = mad_bit_read(ptr, 3); /* gain name */
570 	unsigned orig = mad_bit_read(ptr, 3); /* gain originator */
571 	unsigned sign = mad_bit_read(ptr, 1); /* sign bit */
572 	int gain = mad_bit_read(ptr, 9); /* gain*10 */
573 	if (gain && name == 1 && orig != 0) {
574 		lame->track_gain = ((sign ? -gain : gain) / 10.0f) + adj;
575 		FmtDebug(mad_domain, "LAME track gain found: {}",
576 			 lame->track_gain);
577 	}
578 
579 	/* tmz reports that this isn't currently written by any version of lame
580 	 * (as of 3.97).  Since we have no way of testing it, don't use it.
581 	 * Wouldn't want to go blowing someone's ears just because we read it
582 	 * wrong. :P -- jat */
583 	lame->album_gain = 0;
584 #if 0
585 	name = mad_bit_read(ptr, 3); /* gain name */
586 	orig = mad_bit_read(ptr, 3); /* gain originator */
587 	sign = mad_bit_read(ptr, 1); /* sign bit */
588 	gain = mad_bit_read(ptr, 9); /* gain*10 */
589 	if (gain && name == 2 && orig != 0) {
590 		lame->album_gain = ((sign ? -gain : gain) / 10.0) + adj;
591 		FmtDebug(mad_domain, "LAME album gain found: {}",
592 			 lame->track_gain);
593 	}
594 #else
595 	mad_bit_skip(ptr, 16);
596 #endif
597 
598 	mad_bit_skip(ptr, 16);
599 
600 	lame->encoder_delay = mad_bit_read(ptr, 12);
601 	lame->encoder_padding = mad_bit_read(ptr, 12);
602 
603 	FmtDebug(mad_domain, "encoder delay is {}, encoder padding is {}",
604 		 lame->encoder_delay, lame->encoder_padding);
605 
606 	mad_bit_skip(ptr, 80);
607 
608 	lame->crc = mad_bit_read(ptr, 16);
609 
610 	*bitlen -= 216;
611 
612 	return true;
613 }
614 
615 static inline SongTime
mad_frame_duration(const struct mad_frame * frame)616 mad_frame_duration(const struct mad_frame *frame) noexcept
617 {
618 	return ToSongTime(frame->header.duration);
619 }
620 
621 inline offset_type
ThisFrameOffset() const622 MadDecoder::ThisFrameOffset() const noexcept
623 {
624 	auto offset = input_stream.GetOffset();
625 
626 	if (stream.this_frame != nullptr)
627 		offset -= stream.bufend - stream.this_frame;
628 	else
629 		offset -= stream.bufend - stream.buffer;
630 
631 	return offset;
632 }
633 
634 inline offset_type
RestIncludingThisFrame() const635 MadDecoder::RestIncludingThisFrame() const noexcept
636 {
637 	return input_stream.GetSize() - ThisFrameOffset();
638 }
639 
640 inline void
FileSizeToSongLength()641 MadDecoder::FileSizeToSongLength() noexcept
642 {
643 	if (input_stream.KnownSize()) {
644 		offset_type rest = RestIncludingThisFrame();
645 
646 		const SongTime frame_duration = mad_frame_duration(&frame);
647 		const SongTime duration =
648 			SongTime::FromScale<uint64_t>(rest,
649 						      frame.header.bitrate / 8);
650 		total_time = duration;
651 
652 		max_frames = (frame_duration.IsPositive()
653 			      ? duration.count() / frame_duration.count()
654 			      : 0)
655 			+ FRAMES_CUSHION;
656 	} else {
657 		max_frames = FRAMES_CUSHION;
658 		total_time = SignedSongTime::Negative();
659 	}
660 }
661 
662 inline bool
DecodeFirstFrame(Tag * tag)663 MadDecoder::DecodeFirstFrame(Tag *tag) noexcept
664 {
665 	struct xing xing;
666 
667 #if GCC_CHECK_VERSION(10,0)
668 	/* work around bogus -Wuninitialized in GCC 10 */
669 	xing.frames = 0;
670 #endif
671 
672 	while (true) {
673 		const auto action = DecodeNextFrame(false, tag);
674 		switch (action) {
675 		case MadDecoderAction::SKIP:
676 		case MadDecoderAction::CONT:
677 			continue;
678 
679 		case MadDecoderAction::BREAK:
680 			return false;
681 
682 		case MadDecoderAction::OK:
683 			break;
684 		}
685 
686 		break;
687 	}
688 
689 	struct mad_bitptr ptr = stream.anc_ptr;
690 	int bitlen = stream.anc_bitlen;
691 
692 	FileSizeToSongLength();
693 
694 	/*
695 	 * if an xing tag exists, use that!
696 	 */
697 	if (parse_xing(&xing, &ptr, &bitlen)) {
698 		mute_frame = MadDecoderMuteFrame::SKIP;
699 
700 		if ((xing.flags & XING_FRAMES) && xing.frames) {
701 			mad_timer_t duration = frame.header.duration;
702 			mad_timer_multiply(&duration, xing.frames);
703 			total_time = ToSongTime(duration);
704 			max_frames = xing.frames;
705 		}
706 
707 		struct lame lame;
708 		if (parse_lame(&lame, &ptr, &bitlen)) {
709 			if (input_stream.IsSeekable()) {
710 				/* libmad inserts 529 samples of
711 				   silence at the beginning and
712 				   removes those 529 samples at the
713 				   end */
714 				drop_start_samples = lame.encoder_delay +
715 				                           DECODERDELAY;
716 				drop_end_samples = lame.encoder_padding;
717 				if (drop_end_samples > DECODERDELAY)
718 					drop_end_samples -= DECODERDELAY;
719 				else
720 					drop_end_samples = 0;
721 			}
722 
723 			/* Album gain isn't currently used.  See comment in
724 			 * parse_lame() for details. -- jat */
725 			if (client != nullptr && !found_replay_gain &&
726 			    lame.track_gain > 0.0f) {
727 				ReplayGainInfo rgi;
728 				rgi.Clear();
729 				rgi.track.gain = lame.track_gain;
730 				rgi.track.peak = lame.peak;
731 				client->SubmitReplayGain(&rgi);
732 			}
733 		}
734 	}
735 
736 	if (!max_frames)
737 		return false;
738 
739 	if (max_frames > 8 * 1024 * 1024) {
740 		FmtWarning(mad_domain,
741 			   "mp3 file header indicates too many frames: {}",
742 			   max_frames);
743 		return false;
744 	}
745 
746 	return true;
747 }
748 
~MadDecoder()749 MadDecoder::~MadDecoder() noexcept
750 {
751 	mad_synth_finish(&synth);
752 	mad_frame_finish(&frame);
753 	mad_stream_finish(&stream);
754 
755 	delete[] frame_offsets;
756 	delete[] times;
757 }
758 
759 size_t
TimeToFrame(SongTime t) const760 MadDecoder::TimeToFrame(SongTime t) const noexcept
761 {
762 	size_t i;
763 
764 	for (i = 0; i < highest_frame; ++i) {
765 		auto frame_time = ToSongTime(times[i]);
766 		if (frame_time >= t)
767 			break;
768 	}
769 
770 	return i;
771 }
772 
773 void
UpdateTimerNextFrame()774 MadDecoder::UpdateTimerNextFrame() noexcept
775 {
776 	if (current_frame >= highest_frame) {
777 		/* record this frame's properties in frame_offsets
778 		   (for seeking) and times */
779 
780 		if (current_frame >= max_frames)
781 			/* cap current_frame */
782 			current_frame = max_frames - 1;
783 		else
784 			highest_frame++;
785 
786 		frame_offsets[current_frame] = ThisFrameOffset();
787 
788 		mad_timer_add(&timer, frame.header.duration);
789 		times[current_frame] = timer;
790 	} else
791 		/* get the new timer value from "times" */
792 		timer = times[current_frame];
793 
794 	current_frame++;
795 	elapsed_time = ToSongTime(timer);
796 }
797 
798 DecoderCommand
SubmitPCM(size_t i,size_t pcm_length)799 MadDecoder::SubmitPCM(size_t i, size_t pcm_length) noexcept
800 {
801 	size_t num_samples = pcm_length - i;
802 
803 	mad_fixed_to_24_buffer(output_buffer, synth.pcm,
804 			       i, i + num_samples,
805 			       MAD_NCHANNELS(&frame.header));
806 	num_samples *= MAD_NCHANNELS(&frame.header);
807 
808 	return client->SubmitData(input_stream, output_buffer,
809 				  sizeof(output_buffer[0]) * num_samples,
810 				  frame.header.bitrate / 1000);
811 }
812 
813 inline DecoderCommand
SynthAndSubmit()814 MadDecoder::SynthAndSubmit() noexcept
815 {
816 	mad_synth_frame(&synth, &frame);
817 
818 	if (!found_first_frame) {
819 		unsigned int samples_per_frame = synth.pcm.length;
820 		drop_start_frames = drop_start_samples / samples_per_frame;
821 		drop_end_frames = drop_end_samples / samples_per_frame;
822 		drop_start_samples = drop_start_samples % samples_per_frame;
823 		drop_end_samples = drop_end_samples % samples_per_frame;
824 		found_first_frame = true;
825 	}
826 
827 	if (drop_start_frames > 0) {
828 		drop_start_frames--;
829 		return DecoderCommand::NONE;
830 	} else if ((drop_end_frames > 0) &&
831 		   current_frame == max_frames - drop_end_frames) {
832 		/* stop decoding, effectively dropping all remaining
833 		   frames */
834 		return DecoderCommand::STOP;
835 	}
836 
837 	size_t i = 0;
838 	if (!decoded_first_frame) {
839 		i = drop_start_samples;
840 		decoded_first_frame = true;
841 	}
842 
843 	size_t pcm_length = synth.pcm.length;
844 	if (drop_end_samples &&
845 	    current_frame == max_frames - drop_end_frames - 1) {
846 		if (drop_end_samples >= pcm_length)
847 			return DecoderCommand::STOP;
848 
849 		pcm_length -= drop_end_samples;
850 	}
851 
852 	auto cmd = SubmitPCM(i, pcm_length);
853 	if (cmd != DecoderCommand::NONE)
854 		return cmd;
855 
856 	if (drop_end_samples &&
857 	    current_frame == max_frames - drop_end_frames - 1)
858 		/* stop decoding, effectively dropping
859 		 * all remaining samples */
860 		return DecoderCommand::STOP;
861 
862 	return DecoderCommand::NONE;
863 }
864 
865 inline bool
HandleCurrentFrame()866 MadDecoder::HandleCurrentFrame() noexcept
867 {
868 	switch (mute_frame) {
869 	case MadDecoderMuteFrame::SKIP:
870 		mute_frame = MadDecoderMuteFrame::NONE;
871 		break;
872 	case MadDecoderMuteFrame::SEEK:
873 		if (elapsed_time >= seek_time)
874 			mute_frame = MadDecoderMuteFrame::NONE;
875 		UpdateTimerNextFrame();
876 		break;
877 	case MadDecoderMuteFrame::NONE: {
878 		const auto cmd = SynthAndSubmit();
879 		UpdateTimerNextFrame();
880 		if (cmd == DecoderCommand::SEEK) {
881 			assert(input_stream.IsSeekable());
882 
883 			const auto t = client->GetSeekTime();
884 			size_t j = TimeToFrame(t);
885 			if (j < highest_frame) {
886 				if (Seek(frame_offsets[j])) {
887 					current_frame = j;
888 					was_eof = false;
889 					client->CommandFinished();
890 				} else
891 					client->SeekError();
892 			} else {
893 				seek_time = t;
894 				mute_frame = MadDecoderMuteFrame::SEEK;
895 				client->CommandFinished();
896 			}
897 		} else if (cmd != DecoderCommand::NONE)
898 			return false;
899 	}
900 	}
901 
902 	return true;
903 }
904 
905 inline bool
LoadNextFrame()906 MadDecoder::LoadNextFrame() noexcept
907 {
908 	while (true) {
909 		Tag tag;
910 
911 		const auto action =
912 			DecodeNextFrame(mute_frame != MadDecoderMuteFrame::NONE,
913 					&tag);
914 		if (!tag.IsEmpty())
915 			client->SubmitTag(input_stream, std::move(tag));
916 
917 		switch (action) {
918 		case MadDecoderAction::SKIP:
919 		case MadDecoderAction::CONT:
920 			continue;
921 
922 		case MadDecoderAction::BREAK:
923 			return false;
924 
925 		case MadDecoderAction::OK:
926 			return true;
927 		}
928 	}
929 }
930 
931 inline bool
Read()932 MadDecoder::Read() noexcept
933 {
934 	return HandleCurrentFrame() &&
935 		LoadNextFrame();
936 }
937 
938 inline void
RunDecoder()939 MadDecoder::RunDecoder() noexcept
940 {
941 	assert(client != nullptr);
942 
943 	Tag tag;
944 	if (!DecodeFirstFrame(&tag)) {
945 		if (client->GetCommand() == DecoderCommand::NONE)
946 			LogError(mad_domain,
947 				 "input does not appear to be a mp3 bit stream");
948 		return;
949 	}
950 
951 	AllocateBuffers();
952 
953 	client->Ready(CheckAudioFormat(frame.header.samplerate,
954 				       SampleFormat::S24_P32,
955 				       MAD_NCHANNELS(&frame.header)),
956 		      input_stream.IsSeekable(),
957 		      total_time);
958 
959 	if (!tag.IsEmpty())
960 		client->SubmitTag(input_stream, std::move(tag));
961 
962 	while (Read()) {}
963 }
964 
965 static void
mad_decode(DecoderClient & client,InputStream & input_stream)966 mad_decode(DecoderClient &client, InputStream &input_stream)
967 {
968 	MadDecoder data(&client, input_stream);
969 	data.RunDecoder();
970 }
971 
972 inline bool
RunScan(TagHandler & handler)973 MadDecoder::RunScan(TagHandler &handler) noexcept
974 {
975 	if (!DecodeFirstFrame(nullptr))
976 		return false;
977 
978 	if (!total_time.IsNegative())
979 		handler.OnDuration(SongTime(total_time));
980 
981 	try {
982 		handler.OnAudioFormat(CheckAudioFormat(frame.header.samplerate,
983 						       SampleFormat::S24_P32,
984 						       MAD_NCHANNELS(&frame.header)));
985 	} catch (...) {
986 	}
987 
988 	return true;
989 }
990 
991 static bool
mad_decoder_scan_stream(InputStream & is,TagHandler & handler)992 mad_decoder_scan_stream(InputStream &is, TagHandler &handler)
993 {
994 	MadDecoder data(nullptr, is);
995 	return data.RunScan(handler);
996 }
997 
998 static const char *const mad_suffixes[] = { "mp3", "mp2", nullptr };
999 static const char *const mad_mime_types[] = { "audio/mpeg", nullptr };
1000 
1001 constexpr DecoderPlugin mad_decoder_plugin =
1002 	DecoderPlugin("mad", mad_decode, mad_decoder_scan_stream)
1003 	.WithSuffixes(mad_suffixes)
1004 	.WithMimeTypes(mad_mime_types);
1005