2
* Copyright (C) 2003-2009 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License 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.
20
#include "../decoder_api.h"
37
#define G_LOG_DOMAIN "mad"
39
#define FRAMES_CUSHION 2000
41
#define READ_BUFFER_SIZE 40960
56
/* the number of samples of silence the decoder inserts at start */
57
#define DECODERDELAY 529
59
#define DEFAULT_GAPLESS_MP3_PLAYBACK true
61
static bool gapless_playback;
64
mad_fixed_to_24_sample(mad_fixed_t sample)
73
sample = sample + (1L << (MAD_F_FRACBITS - bits));
78
else if (sample < MIN)
82
return sample >> (MAD_F_FRACBITS + 1 - bits);
86
mad_fixed_to_24_buffer(int32_t *dest, const struct mad_synth *synth,
87
unsigned int start, unsigned int end,
88
unsigned int num_channels)
92
for (i = start; i < end; ++i) {
93
for (c = 0; c < num_channels; ++c)
94
*dest++ = mad_fixed_to_24_sample(synth->pcm.samples[c][i]);
99
mp3_plugin_init(G_GNUC_UNUSED const struct config_param *param)
101
gapless_playback = config_get_bool(CONF_GAPLESS_MP3_PLAYBACK,
102
DEFAULT_GAPLESS_MP3_PLAYBACK);
106
#define MP3_DATA_OUTPUT_BUFFER_SIZE 2048
109
struct mad_stream stream;
110
struct mad_frame frame;
111
struct mad_synth synth;
113
unsigned char input_buffer[READ_BUFFER_SIZE];
114
int32_t output_buffer[MP3_DATA_OUTPUT_BUFFER_SIZE];
118
enum muteframe mute_frame;
121
unsigned long highest_frame;
122
unsigned long max_frames;
123
unsigned long current_frame;
124
unsigned int drop_start_frames;
125
unsigned int drop_end_frames;
126
unsigned int drop_start_samples;
127
unsigned int drop_end_samples;
129
bool found_first_frame;
130
bool decoded_first_frame;
131
unsigned long bit_rate;
132
struct decoder *decoder;
133
struct input_stream *input_stream;
134
enum mad_layer layer;
138
mp3_data_init(struct mp3_data *data, struct decoder *decoder,
139
struct input_stream *input_stream)
141
data->mute_frame = MUTEFRAME_NONE;
142
data->highest_frame = 0;
143
data->max_frames = 0;
144
data->frame_offsets = NULL;
146
data->current_frame = 0;
147
data->drop_start_frames = 0;
148
data->drop_end_frames = 0;
149
data->drop_start_samples = 0;
150
data->drop_end_samples = 0;
151
data->found_xing = false;
152
data->found_first_frame = false;
153
data->decoded_first_frame = false;
154
data->decoder = decoder;
155
data->input_stream = input_stream;
158
mad_stream_init(&data->stream);
159
mad_stream_options(&data->stream, MAD_OPTION_IGNORECRC);
160
mad_frame_init(&data->frame);
161
mad_synth_init(&data->synth);
162
mad_timer_reset(&data->timer);
165
static bool mp3_seek(struct mp3_data *data, long offset)
167
if (!input_stream_seek(data->input_stream, offset, SEEK_SET))
170
mad_stream_buffer(&data->stream, data->input_buffer, 0);
171
(data->stream).error = 0;
177
mp3_fill_buffer(struct mp3_data *data)
179
size_t remaining, length;
182
if (data->stream.next_frame != NULL) {
183
remaining = data->stream.bufend - data->stream.next_frame;
184
memmove(data->input_buffer, data->stream.next_frame,
186
dest = (data->input_buffer) + remaining;
187
length = READ_BUFFER_SIZE - remaining;
190
length = READ_BUFFER_SIZE;
191
dest = data->input_buffer;
194
/* we've exhausted the read buffer, so give up!, these potential
195
* mp3 frames are way too big, and thus unlikely to be mp3 frames */
199
length = decoder_read(data->decoder, data->input_stream, dest, length);
203
mad_stream_buffer(&data->stream, data->input_buffer,
205
(data->stream).error = 0;
211
/* Parse mp3 RVA2 frame. Shamelessly stolen from madplay. */
213
parse_rva2(struct id3_tag *tag, struct replay_gain_info *replay_gain_info)
215
struct id3_frame const * frame;
217
id3_latin1_t const *id;
218
id3_byte_t const *data;
222
CHANNEL_OTHER = 0x00,
223
CHANNEL_MASTER_VOLUME = 0x01,
224
CHANNEL_FRONT_RIGHT = 0x02,
225
CHANNEL_FRONT_LEFT = 0x03,
226
CHANNEL_BACK_RIGHT = 0x04,
227
CHANNEL_BACK_LEFT = 0x05,
228
CHANNEL_FRONT_CENTRE = 0x06,
229
CHANNEL_BACK_CENTRE = 0x07,
230
CHANNEL_SUBWOOFER = 0x08
233
/* relative volume adjustment information */
235
frame = id3_tag_findframe(tag, "RVA2", 0);
239
id = id3_field_getlatin1(id3_frame_field(frame, 0));
240
data = id3_field_getbinarydata(id3_frame_field(frame, 1),
243
if (id == NULL || data == NULL)
247
* "The 'identification' string is used to identify the
248
* situation and/or device where this adjustment should apply.
249
* The following is then repeated for every channel
251
* Type of channel $xx
252
* Volume adjustment $xx xx
253
* Bits representing peak $xx
254
* Peak volume $xx (xx ...)"
257
while (length >= 4) {
258
unsigned int peak_bytes;
260
peak_bytes = (data[3] + 7) / 8;
261
if (4 + peak_bytes > length)
264
if (data[0] == CHANNEL_MASTER_VOLUME) {
265
signed int voladj_fixed;
269
* "The volume adjustment is encoded as a fixed
270
* point decibel value, 16 bit signed integer
271
* representing (adjustment*512), giving +/- 64
272
* dB with a precision of 0.001953125 dB."
275
voladj_fixed = (data[1] << 8) | (data[2] << 0);
276
voladj_fixed |= -(voladj_fixed & 0x8000);
278
voladj_float = (double) voladj_fixed / 512;
280
replay_gain_info->tuples[REPLAY_GAIN_TRACK].gain = voladj_float;
281
replay_gain_info->tuples[REPLAY_GAIN_ALBUM].gain = voladj_float;
283
g_debug("parseRVA2: Relative Volume "
284
"%+.1f dB adjustment (%s)\n",
290
data += 4 + peak_bytes;
291
length -= 4 + peak_bytes;
299
static struct replay_gain_info *
300
parse_id3_replay_gain_info(struct id3_tag *tag)
305
struct id3_frame *frame;
307
struct replay_gain_info *replay_gain_info;
309
replay_gain_info = replay_gain_info_new();
311
for (i = 0; (frame = id3_tag_findframe(tag, "TXXX", i)); i++) {
312
if (frame->nfields < 3)
316
id3_ucs4_latin1duplicate(id3_field_getstring
317
(&frame->fields[1]));
319
id3_ucs4_latin1duplicate(id3_field_getstring
320
(&frame->fields[2]));
322
if (g_ascii_strcasecmp(key, "replaygain_track_gain") == 0) {
323
replay_gain_info->tuples[REPLAY_GAIN_TRACK].gain = atof(value);
325
} else if (g_ascii_strcasecmp(key, "replaygain_album_gain") == 0) {
326
replay_gain_info->tuples[REPLAY_GAIN_ALBUM].gain = atof(value);
328
} else if (g_ascii_strcasecmp(key, "replaygain_track_peak") == 0) {
329
replay_gain_info->tuples[REPLAY_GAIN_TRACK].peak = atof(value);
331
} else if (g_ascii_strcasecmp(key, "replaygain_album_peak") == 0) {
332
replay_gain_info->tuples[REPLAY_GAIN_ALBUM].peak = atof(value);
341
/* fall back on RVA2 if no replaygain tags found */
342
found = parse_rva2(tag, replay_gain_info);
346
return replay_gain_info;
347
replay_gain_info_free(replay_gain_info);
352
static void mp3_parse_id3(struct mp3_data *data, size_t tagsize,
353
struct tag **mpd_tag,
354
struct replay_gain_info **replay_gain_info_r)
357
struct id3_tag *id3_tag = NULL;
359
id3_byte_t const *id3_data;
360
id3_byte_t *allocated = NULL;
362
count = data->stream.bufend - data->stream.this_frame;
364
if (tagsize <= count) {
365
id3_data = data->stream.this_frame;
366
mad_stream_skip(&(data->stream), tagsize);
368
allocated = g_malloc(tagsize);
369
memcpy(allocated, data->stream.this_frame, count);
370
mad_stream_skip(&(data->stream), count);
372
while (count < tagsize) {
375
len = decoder_read(data->decoder, data->input_stream,
376
allocated + count, tagsize - count);
383
if (count != tagsize) {
384
g_debug("error parsing ID3 tag");
389
id3_data = allocated;
392
id3_tag = id3_tag_parse(id3_data, tagsize);
393
if (id3_tag == NULL) {
399
struct tag *tmp_tag = tag_id3_import(id3_tag);
400
if (tmp_tag != NULL) {
401
if (*mpd_tag != NULL)
407
if (replay_gain_info_r) {
408
struct replay_gain_info *tmp_rgi =
409
parse_id3_replay_gain_info(id3_tag);
410
if (tmp_rgi != NULL) {
411
if (*replay_gain_info_r)
412
replay_gain_info_free(*replay_gain_info_r);
413
*replay_gain_info_r = tmp_rgi;
417
id3_tag_delete(id3_tag);
420
#else /* !HAVE_ID3TAG */
422
(void)replay_gain_info_r;
424
/* This code is enabled when libid3tag is disabled. Instead
425
of parsing the ID3 frame, it just skips it. */
427
size_t count = data->stream.bufend - data->stream.this_frame;
429
if (tagsize <= count) {
430
mad_stream_skip(&data->stream, tagsize);
432
mad_stream_skip(&data->stream, count);
434
while (count < tagsize) {
435
size_t len = tagsize - count;
437
if (len > sizeof(ignored))
438
len = sizeof(ignored);
440
len = decoder_read(data->decoder, data->input_stream,
453
* This function emulates libid3tag when it is disabled. Instead of
454
* doing a real analyzation of the frame, it just checks whether the
455
* frame begins with the string "ID3". If so, it returns the length
459
id3_tag_query(const void *p0, size_t length)
463
return length >= 10 && memcmp(p, "ID3", 3) == 0
464
? (p[8] << 7) + p[9] + 10
467
#endif /* !HAVE_ID3TAG */
469
static enum mp3_action
470
decode_next_frame_header(struct mp3_data *data, G_GNUC_UNUSED struct tag **tag,
471
G_GNUC_UNUSED struct replay_gain_info **replay_gain_info_r)
473
enum mad_layer layer;
475
if ((data->stream).buffer == NULL
476
|| (data->stream).error == MAD_ERROR_BUFLEN) {
477
if (!mp3_fill_buffer(data))
480
if (mad_header_decode(&data->frame.header, &data->stream)) {
481
if ((data->stream).error == MAD_ERROR_LOSTSYNC &&
482
(data->stream).this_frame) {
483
signed long tagsize = id3_tag_query((data->stream).
491
if (tag && !(*tag)) {
492
mp3_parse_id3(data, (size_t)tagsize,
493
tag, replay_gain_info_r);
495
mad_stream_skip(&(data->stream),
501
if (MAD_RECOVERABLE((data->stream).error)) {
504
if ((data->stream).error == MAD_ERROR_BUFLEN)
507
g_warning("unrecoverable frame level error "
509
mad_stream_errorstr(&data->stream));
515
layer = data->frame.header.layer;
517
if (layer != MAD_LAYER_II && layer != MAD_LAYER_III) {
518
/* Only layer 2 and 3 have been tested to work */
522
} else if (layer != data->layer) {
523
/* Don't decode frames with a different layer than the first */
530
static enum mp3_action
531
decodeNextFrame(struct mp3_data *data)
533
if ((data->stream).buffer == NULL
534
|| (data->stream).error == MAD_ERROR_BUFLEN) {
535
if (!mp3_fill_buffer(data))
538
if (mad_frame_decode(&data->frame, &data->stream)) {
539
if ((data->stream).error == MAD_ERROR_LOSTSYNC) {
540
signed long tagsize = id3_tag_query((data->stream).
547
mad_stream_skip(&(data->stream), tagsize);
551
if (MAD_RECOVERABLE((data->stream).error)) {
554
if ((data->stream).error == MAD_ERROR_BUFLEN)
557
g_warning("unrecoverable frame level error "
559
mad_stream_errorstr(&data->stream));
568
/* xing stuff stolen from alsaplayer, and heavily modified by jat */
569
#define XI_MAGIC (('X' << 8) | 'i')
570
#define NG_MAGIC (('n' << 8) | 'g')
571
#define IN_MAGIC (('I' << 8) | 'n')
572
#define FO_MAGIC (('f' << 8) | 'o')
575
XING_MAGIC_XING, /* VBR */
576
XING_MAGIC_INFO /* CBR */
580
long flags; /* valid fields (see below) */
581
unsigned long frames; /* total number of frames */
582
unsigned long bytes; /* total number of bytes */
583
unsigned char toc[100]; /* 100-point seek table */
584
long scale; /* VBR quality */
585
enum xing_magic magic; /* header magic */
589
XING_FRAMES = 0x00000001L,
590
XING_BYTES = 0x00000002L,
591
XING_TOC = 0x00000004L,
592
XING_SCALE = 0x00000008L
601
char encoder[10]; /* 9 byte encoder name/version ("LAME3.97b") */
602
struct version version; /* struct containing just the version */
603
float peak; /* replaygain peak */
604
float track_gain; /* replaygain track gain */
605
float album_gain; /* replaygain album gain */
606
int encoder_delay; /* # of added samples at start of mp3 */
607
int encoder_padding; /* # of added samples at end of mp3 */
608
int crc; /* CRC of the first 190 bytes of this frame */
612
parse_xing(struct xing *xing, struct mad_bitptr *ptr, int *oldbitlen)
624
bits = mad_bit_read(ptr, 16);
627
if (bits == XI_MAGIC) {
631
if (mad_bit_read(ptr, 16) != NG_MAGIC)
635
xing->magic = XING_MAGIC_XING;
636
} else if (bits == IN_MAGIC) {
640
if (mad_bit_read(ptr, 16) != FO_MAGIC)
644
xing->magic = XING_MAGIC_INFO;
646
else if (bits == NG_MAGIC) xing->magic = XING_MAGIC_XING;
647
else if (bits == FO_MAGIC) xing->magic = XING_MAGIC_INFO;
653
xing->flags = mad_bit_read(ptr, 32);
656
if (xing->flags & XING_FRAMES) {
659
xing->frames = mad_bit_read(ptr, 32);
663
if (xing->flags & XING_BYTES) {
666
xing->bytes = mad_bit_read(ptr, 32);
670
if (xing->flags & XING_TOC) {
673
for (i = 0; i < 100; ++i) xing->toc[i] = mad_bit_read(ptr, 8);
677
if (xing->flags & XING_SCALE) {
680
xing->scale = mad_bit_read(ptr, 32);
684
/* Make sure we consume no less than 120 bytes (960 bits) in hopes that
685
* the LAME tag is found there, and not right after the Xing header */
686
bitsleft = 960 - ((*oldbitlen) - bitlen);
689
else if (bitsleft > 0) {
690
mad_bit_read(ptr, bitsleft);
700
parse_lame(struct lame *lame, struct mad_bitptr *ptr, int *bitlen)
709
/* Unlike the xing header, the lame tag has a fixed length. Fail if
710
* not all 36 bytes (288 bits) are there. */
714
for (i = 0; i < 9; i++)
715
lame->encoder[i] = (char)mad_bit_read(ptr, 8);
716
lame->encoder[9] = '\0';
720
/* This is technically incorrect, since the encoder might not be lame.
721
* But there's no other way to determine if this is a lame tag, and we
722
* wouldn't want to go reading a tag that's not there. */
723
if (!g_str_has_prefix(lame->encoder, "LAME"))
726
if (sscanf(lame->encoder+4, "%u.%u",
727
&lame->version.major, &lame->version.minor) != 2)
730
g_debug("detected LAME version %i.%i (\"%s\")\n",
731
lame->version.major, lame->version.minor, lame->encoder);
733
/* The reference volume was changed from the 83dB used in the
734
* ReplayGain spec to 89dB in lame 3.95.1. Bump the gain for older
735
* versions, since everyone else uses 89dB instead of 83dB.
736
* Unfortunately, lame didn't differentiate between 3.95 and 3.95.1, so
737
* it's impossible to make the proper adjustment for 3.95.
738
* Fortunately, 3.95 was only out for about a day before 3.95.1 was
739
* released. -- tmz */
740
if (lame->version.major < 3 ||
741
(lame->version.major == 3 && lame->version.minor < 95))
744
mad_bit_read(ptr, 16);
746
lame->peak = mad_f_todouble(mad_bit_read(ptr, 32) << 5); /* peak */
747
g_debug("LAME peak found: %f\n", lame->peak);
749
lame->track_gain = 0;
750
name = mad_bit_read(ptr, 3); /* gain name */
751
orig = mad_bit_read(ptr, 3); /* gain originator */
752
sign = mad_bit_read(ptr, 1); /* sign bit */
753
gain = mad_bit_read(ptr, 9); /* gain*10 */
754
if (gain && name == 1 && orig != 0) {
755
lame->track_gain = ((sign ? -gain : gain) / 10.0) + adj;
756
g_debug("LAME track gain found: %f\n", lame->track_gain);
759
/* tmz reports that this isn't currently written by any version of lame
760
* (as of 3.97). Since we have no way of testing it, don't use it.
761
* Wouldn't want to go blowing someone's ears just because we read it
762
* wrong. :P -- jat */
763
lame->album_gain = 0;
765
name = mad_bit_read(ptr, 3); /* gain name */
766
orig = mad_bit_read(ptr, 3); /* gain originator */
767
sign = mad_bit_read(ptr, 1); /* sign bit */
768
gain = mad_bit_read(ptr, 9); /* gain*10 */
769
if (gain && name == 2 && orig != 0) {
770
lame->album_gain = ((sign ? -gain : gain) / 10.0) + adj;
771
g_debug("LAME album gain found: %f\n", lame->track_gain);
774
mad_bit_read(ptr, 16);
777
mad_bit_read(ptr, 16);
779
lame->encoder_delay = mad_bit_read(ptr, 12);
780
lame->encoder_padding = mad_bit_read(ptr, 12);
782
g_debug("encoder delay is %i, encoder padding is %i\n",
783
lame->encoder_delay, lame->encoder_padding);
785
mad_bit_read(ptr, 80);
787
lame->crc = mad_bit_read(ptr, 16);
795
mp3_frame_duration(const struct mad_frame *frame)
797
return mad_timer_count(frame->header.duration,
798
MAD_UNITS_MILLISECONDS) / 1000.0;
802
mp3_this_frame_offset(const struct mp3_data *data)
804
off_t offset = data->input_stream->offset;
806
if (data->stream.this_frame != NULL)
807
offset -= data->stream.bufend - data->stream.this_frame;
809
offset -= data->stream.bufend - data->stream.buffer;
815
mp3_rest_including_this_frame(const struct mp3_data *data)
817
return data->input_stream->size - mp3_this_frame_offset(data);
821
* Attempt to calulcate the length of the song from filesize
824
mp3_filesize_to_song_length(struct mp3_data *data)
826
off_t rest = mp3_rest_including_this_frame(data);
829
float frame_duration = mp3_frame_duration(&data->frame);
831
data->total_time = (rest * 8.0) / (data->frame).header.bitrate;
832
data->max_frames = data->total_time / frame_duration +
835
data->max_frames = FRAMES_CUSHION;
836
data->total_time = 0;
841
mp3_decode_first_frame(struct mp3_data *data, struct tag **tag,
842
struct replay_gain_info **replay_gain_info_r)
846
struct mad_bitptr ptr;
851
memset(&xing, 0, sizeof(struct xing));
856
ret = decode_next_frame_header(data, tag,
858
} while (ret == DECODE_CONT);
859
if (ret == DECODE_BREAK)
861
if (ret == DECODE_SKIP) continue;
864
ret = decodeNextFrame(data);
865
} while (ret == DECODE_CONT);
866
if (ret == DECODE_BREAK)
868
if (ret == DECODE_OK) break;
871
ptr = data->stream.anc_ptr;
872
bitlen = data->stream.anc_bitlen;
874
mp3_filesize_to_song_length(data);
877
* if an xing tag exists, use that!
879
if (parse_xing(&xing, &ptr, &bitlen)) {
880
data->found_xing = true;
881
data->mute_frame = MUTEFRAME_SKIP;
883
if ((xing.flags & XING_FRAMES) && xing.frames) {
884
mad_timer_t duration = data->frame.header.duration;
885
mad_timer_multiply(&duration, xing.frames);
886
data->total_time = ((float)mad_timer_count(duration, MAD_UNITS_MILLISECONDS)) / 1000;
887
data->max_frames = xing.frames;
890
if (parse_lame(&lame, &ptr, &bitlen)) {
891
if (gapless_playback &&
892
data->input_stream->seekable) {
893
data->drop_start_samples = lame.encoder_delay +
895
data->drop_end_samples = lame.encoder_padding;
898
/* Album gain isn't currently used. See comment in
899
* parse_lame() for details. -- jat */
900
if (replay_gain_info_r && !*replay_gain_info_r &&
902
*replay_gain_info_r = replay_gain_info_new();
903
(*replay_gain_info_r)->tuples[REPLAY_GAIN_TRACK].gain = lame.track_gain;
904
(*replay_gain_info_r)->tuples[REPLAY_GAIN_TRACK].peak = lame.peak;
909
if (!data->max_frames)
912
if (data->max_frames > 8 * 1024 * 1024) {
913
g_warning("mp3 file header indicates too many frames: %lu\n",
918
data->frame_offsets = g_malloc(sizeof(long) * data->max_frames);
919
data->times = g_malloc(sizeof(mad_timer_t) * data->max_frames);
924
static void mp3_data_finish(struct mp3_data *data)
926
mad_synth_finish(&data->synth);
927
mad_frame_finish(&data->frame);
928
mad_stream_finish(&data->stream);
930
g_free(data->frame_offsets);
934
/* this is primarily used for getting total time for tags */
935
static int mp3_total_file_time(const char *file)
937
struct input_stream input_stream;
938
struct mp3_data data;
941
if (!input_stream_open(&input_stream, file))
943
mp3_data_init(&data, NULL, &input_stream);
944
if (!mp3_decode_first_frame(&data, NULL, NULL))
947
ret = data.total_time + 0.5;
948
mp3_data_finish(&data);
949
input_stream_close(&input_stream);
955
mp3_open(struct input_stream *is, struct mp3_data *data,
956
struct decoder *decoder, struct tag **tag,
957
struct replay_gain_info **replay_gain_info_r)
959
mp3_data_init(data, decoder, is);
961
if (!mp3_decode_first_frame(data, tag, replay_gain_info_r)) {
962
mp3_data_finish(data);
972
mp3_time_to_frame(const struct mp3_data *data, double t)
976
for (i = 0; i < data->highest_frame; ++i) {
978
mad_timer_count(data->times[i],
979
MAD_UNITS_MILLISECONDS) / 1000.;
988
mp3_update_timer_next_frame(struct mp3_data *data)
990
if (data->current_frame >= data->highest_frame) {
991
/* record this frame's properties in
992
data->frame_offsets (for seeking) and
994
data->bit_rate = (data->frame).header.bitrate;
996
if (data->current_frame >= data->max_frames)
997
/* cap data->current_frame */
998
data->current_frame = data->max_frames - 1;
1000
data->highest_frame++;
1002
data->frame_offsets[data->current_frame] =
1003
mp3_this_frame_offset(data);
1005
mad_timer_add(&data->timer, (data->frame).header.duration);
1006
data->times[data->current_frame] = data->timer;
1008
/* get the new timer value from data->times */
1009
data->timer = data->times[data->current_frame];
1011
data->current_frame++;
1012
data->elapsed_time =
1013
mad_timer_count(data->timer, MAD_UNITS_MILLISECONDS) / 1000.0;
1017
* Sends the synthesized current frame via decoder_data().
1019
static enum decoder_command
1020
mp3_send_pcm(struct mp3_data *data, unsigned i, unsigned pcm_length,
1021
struct replay_gain_info *replay_gain_info)
1023
unsigned max_samples;
1025
max_samples = sizeof(data->output_buffer) /
1026
sizeof(data->output_buffer[0]) /
1027
MAD_NCHANNELS(&(data->frame).header);
1029
while (i < pcm_length) {
1030
enum decoder_command cmd;
1031
unsigned int num_samples = pcm_length - i;
1032
if (num_samples > max_samples)
1033
num_samples = max_samples;
1037
mad_fixed_to_24_buffer(data->output_buffer,
1040
MAD_NCHANNELS(&(data->frame).header));
1041
num_samples *= MAD_NCHANNELS(&(data->frame).header);
1043
cmd = decoder_data(data->decoder, data->input_stream,
1044
data->output_buffer,
1045
sizeof(data->output_buffer[0]) * num_samples,
1047
data->bit_rate / 1000,
1049
if (cmd != DECODE_COMMAND_NONE)
1053
return DECODE_COMMAND_NONE;
1057
* Synthesize the current frame and send it via decoder_data().
1059
static enum decoder_command
1060
mp3_synth_and_send(struct mp3_data *data,
1061
struct replay_gain_info *replay_gain_info)
1063
unsigned i, pcm_length;
1064
enum decoder_command cmd;
1066
mad_synth_frame(&data->synth, &data->frame);
1068
if (!data->found_first_frame) {
1069
unsigned int samples_per_frame = data->synth.pcm.length;
1070
data->drop_start_frames = data->drop_start_samples / samples_per_frame;
1071
data->drop_end_frames = data->drop_end_samples / samples_per_frame;
1072
data->drop_start_samples = data->drop_start_samples % samples_per_frame;
1073
data->drop_end_samples = data->drop_end_samples % samples_per_frame;
1074
data->found_first_frame = true;
1077
if (data->drop_start_frames > 0) {
1078
data->drop_start_frames--;
1079
return DECODE_COMMAND_NONE;
1080
} else if ((data->drop_end_frames > 0) &&
1081
(data->current_frame == (data->max_frames + 1 - data->drop_end_frames))) {
1082
/* stop decoding, effectively dropping all remaining
1084
return DECODE_COMMAND_STOP;
1087
if (!data->decoded_first_frame) {
1088
i = data->drop_start_samples;
1089
data->decoded_first_frame = true;
1093
pcm_length = data->synth.pcm.length;
1094
if (data->drop_end_samples &&
1095
(data->current_frame == data->max_frames - data->drop_end_frames)) {
1096
if (data->drop_end_samples >= pcm_length)
1099
pcm_length -= data->drop_end_samples;
1102
cmd = mp3_send_pcm(data, i, pcm_length, replay_gain_info);
1103
if (cmd != DECODE_COMMAND_NONE)
1106
if (data->drop_end_samples &&
1107
(data->current_frame == data->max_frames - data->drop_end_frames))
1108
/* stop decoding, effectively dropping
1109
* all remaining samples */
1110
return DECODE_COMMAND_STOP;
1112
return DECODE_COMMAND_NONE;
1116
mp3_read(struct mp3_data *data, struct replay_gain_info **replay_gain_info_r)
1118
struct decoder *decoder = data->decoder;
1119
enum mp3_action ret;
1120
enum decoder_command cmd;
1122
mp3_update_timer_next_frame(data);
1124
switch (data->mute_frame) {
1125
case MUTEFRAME_SKIP:
1126
data->mute_frame = MUTEFRAME_NONE;
1128
case MUTEFRAME_SEEK:
1129
if (data->elapsed_time >= data->seek_where)
1130
data->mute_frame = MUTEFRAME_NONE;
1132
case MUTEFRAME_NONE:
1133
cmd = mp3_synth_and_send(data,
1134
replay_gain_info_r != NULL
1135
? *replay_gain_info_r : NULL);
1136
if (cmd == DECODE_COMMAND_SEEK) {
1139
assert(data->input_stream->seekable);
1141
j = mp3_time_to_frame(data,
1142
decoder_seek_where(decoder));
1143
if (j < data->highest_frame) {
1144
if (mp3_seek(data, data->frame_offsets[j])) {
1145
data->current_frame = j;
1146
decoder_command_finished(decoder);
1148
decoder_seek_error(decoder);
1150
data->seek_where = decoder_seek_where(decoder);
1151
data->mute_frame = MUTEFRAME_SEEK;
1152
decoder_command_finished(decoder);
1154
} else if (cmd != DECODE_COMMAND_NONE)
1162
struct tag *tag = NULL;
1164
ret = decode_next_frame_header(data, &tag,
1165
replay_gain_info_r);
1168
decoder_tag(decoder, data->input_stream, tag);
1171
} while (ret == DECODE_CONT);
1172
if (ret == DECODE_BREAK)
1174
else if (ret == DECODE_SKIP)
1177
if (data->mute_frame == MUTEFRAME_NONE) {
1179
ret = decodeNextFrame(data);
1180
} while (ret == DECODE_CONT);
1181
if (ret == DECODE_BREAK)
1185
if (!skip && ret == DECODE_OK)
1189
return ret != DECODE_BREAK;
1192
static void mp3_audio_format(struct mp3_data *data, struct audio_format *af)
1195
af->sample_rate = (data->frame).header.samplerate;
1196
af->channels = MAD_NCHANNELS(&(data->frame).header);
1200
mp3_decode(struct decoder *decoder, struct input_stream *input_stream)
1202
struct mp3_data data;
1203
struct tag *tag = NULL;
1204
struct replay_gain_info *replay_gain_info = NULL;
1205
struct audio_format audio_format;
1207
if (!mp3_open(input_stream, &data, decoder, &tag, &replay_gain_info)) {
1208
if (decoder_get_command(decoder) == DECODE_COMMAND_NONE)
1210
("Input does not appear to be a mp3 bit stream.\n");
1214
mp3_audio_format(&data, &audio_format);
1216
decoder_initialized(decoder, &audio_format,
1217
data.input_stream->seekable, data.total_time);
1220
decoder_tag(decoder, input_stream, tag);
1224
while (mp3_read(&data, &replay_gain_info)) ;
1226
if (replay_gain_info)
1227
replay_gain_info_free(replay_gain_info);
1229
mp3_data_finish(&data);
1232
static struct tag *mp3_tag_dup(const char *file)
1237
total_time = mp3_total_file_time(file);
1238
if (total_time < 0) {
1239
g_debug("Failed to get total song time from: %s", file);
1244
tag->time = total_time;
1248
static const char *const mp3_suffixes[] = { "mp3", "mp2", NULL };
1249
static const char *const mp3_mime_types[] = { "audio/mpeg", NULL };
1251
const struct decoder_plugin mad_decoder_plugin = {
1253
.init = mp3_plugin_init,
1254
.stream_decode = mp3_decode,
1255
.tag_dup = mp3_tag_dup,
1256
.suffixes = mp3_suffixes,
1257
.mime_types = mp3_mime_types