~ubuntu-branches/ubuntu/trusty/mpd/trusty

« back to all changes in this revision

Viewing changes to src/decoder/mad_plugin.c

  • Committer: Bazaar Package Importer
  • Author(s): Angel Abad
  • Date: 2011-02-02 12:26:30 UTC
  • mfrom: (1.5.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20110202122630-bdyx8w4k94doz4fs
Tags: 0.16.1-1ubuntu1
* Merge from debian unstable. Remaining changes:
  - debian/control:
    + Don't build-depend on libmikmod2-dev (Debian bug #510675).
    + Move avahi-daemon from Suggests field to Recommends field.
  - debian/mpd.init.d:
    + Read mpd user from mpd.conf.
  - debian/control, debian/rules:
    + Add libmp3lame-dev to the build dependencies and enable lame.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2003-2009 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 "../decoder_api.h"
21
 
#include "../conf.h"
22
 
#include "config.h"
23
 
#include "tag_id3.h"
24
 
 
25
 
#include <assert.h>
26
 
#include <unistd.h>
27
 
#include <stdlib.h>
28
 
#include <stdio.h>
29
 
#include <glib.h>
30
 
#include <mad.h>
31
 
 
32
 
#ifdef HAVE_ID3TAG
33
 
#include <id3tag.h>
34
 
#endif
35
 
 
36
 
#undef G_LOG_DOMAIN
37
 
#define G_LOG_DOMAIN "mad"
38
 
 
39
 
#define FRAMES_CUSHION    2000
40
 
 
41
 
#define READ_BUFFER_SIZE  40960
42
 
 
43
 
enum mp3_action {
44
 
        DECODE_SKIP = -3,
45
 
        DECODE_BREAK = -2,
46
 
        DECODE_CONT = -1,
47
 
        DECODE_OK = 0
48
 
};
49
 
 
50
 
enum muteframe {
51
 
        MUTEFRAME_NONE,
52
 
        MUTEFRAME_SKIP,
53
 
        MUTEFRAME_SEEK
54
 
};
55
 
 
56
 
/* the number of samples of silence the decoder inserts at start */
57
 
#define DECODERDELAY 529
58
 
 
59
 
#define DEFAULT_GAPLESS_MP3_PLAYBACK true
60
 
 
61
 
static bool gapless_playback;
62
 
 
63
 
static inline int32_t
64
 
mad_fixed_to_24_sample(mad_fixed_t sample)
65
 
{
66
 
        enum {
67
 
                bits = 24,
68
 
                MIN = -MAD_F_ONE,
69
 
                MAX = MAD_F_ONE - 1
70
 
        };
71
 
 
72
 
        /* round */
73
 
        sample = sample + (1L << (MAD_F_FRACBITS - bits));
74
 
 
75
 
        /* clip */
76
 
        if (sample > MAX)
77
 
                sample = MAX;
78
 
        else if (sample < MIN)
79
 
                sample = MIN;
80
 
 
81
 
        /* quantize */
82
 
        return sample >> (MAD_F_FRACBITS + 1 - bits);
83
 
}
84
 
 
85
 
static void
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)
89
 
{
90
 
        unsigned int i, c;
91
 
 
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]);
95
 
        }
96
 
}
97
 
 
98
 
static bool
99
 
mp3_plugin_init(G_GNUC_UNUSED const struct config_param *param)
100
 
{
101
 
        gapless_playback = config_get_bool(CONF_GAPLESS_MP3_PLAYBACK,
102
 
                                           DEFAULT_GAPLESS_MP3_PLAYBACK);
103
 
        return true;
104
 
}
105
 
 
106
 
#define MP3_DATA_OUTPUT_BUFFER_SIZE 2048
107
 
 
108
 
struct mp3_data {
109
 
        struct mad_stream stream;
110
 
        struct mad_frame frame;
111
 
        struct mad_synth synth;
112
 
        mad_timer_t timer;
113
 
        unsigned char input_buffer[READ_BUFFER_SIZE];
114
 
        int32_t output_buffer[MP3_DATA_OUTPUT_BUFFER_SIZE];
115
 
        float total_time;
116
 
        float elapsed_time;
117
 
        float seek_where;
118
 
        enum muteframe mute_frame;
119
 
        long *frame_offsets;
120
 
        mad_timer_t *times;
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;
128
 
        bool found_xing;
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;
135
 
};
136
 
 
137
 
static void
138
 
mp3_data_init(struct mp3_data *data, struct decoder *decoder,
139
 
              struct input_stream *input_stream)
140
 
{
141
 
        data->mute_frame = MUTEFRAME_NONE;
142
 
        data->highest_frame = 0;
143
 
        data->max_frames = 0;
144
 
        data->frame_offsets = NULL;
145
 
        data->times = 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;
156
 
        data->layer = 0;
157
 
 
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);
163
 
}
164
 
 
165
 
static bool mp3_seek(struct mp3_data *data, long offset)
166
 
{
167
 
        if (!input_stream_seek(data->input_stream, offset, SEEK_SET))
168
 
                return false;
169
 
 
170
 
        mad_stream_buffer(&data->stream, data->input_buffer, 0);
171
 
        (data->stream).error = 0;
172
 
 
173
 
        return true;
174
 
}
175
 
 
176
 
static bool
177
 
mp3_fill_buffer(struct mp3_data *data)
178
 
{
179
 
        size_t remaining, length;
180
 
        unsigned char *dest;
181
 
 
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,
185
 
                        remaining);
186
 
                dest = (data->input_buffer) + remaining;
187
 
                length = READ_BUFFER_SIZE - remaining;
188
 
        } else {
189
 
                remaining = 0;
190
 
                length = READ_BUFFER_SIZE;
191
 
                dest = data->input_buffer;
192
 
        }
193
 
 
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 */
196
 
        if (length == 0)
197
 
                return false;
198
 
 
199
 
        length = decoder_read(data->decoder, data->input_stream, dest, length);
200
 
        if (length == 0)
201
 
                return false;
202
 
 
203
 
        mad_stream_buffer(&data->stream, data->input_buffer,
204
 
                          length + remaining);
205
 
        (data->stream).error = 0;
206
 
 
207
 
        return true;
208
 
}
209
 
 
210
 
#ifdef HAVE_ID3TAG
211
 
/* Parse mp3 RVA2 frame. Shamelessly stolen from madplay. */
212
 
static bool
213
 
parse_rva2(struct id3_tag *tag, struct replay_gain_info *replay_gain_info)
214
 
{
215
 
        struct id3_frame const * frame;
216
 
 
217
 
        id3_latin1_t const *id;
218
 
        id3_byte_t const *data;
219
 
        id3_length_t length;
220
 
 
221
 
        enum {
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
231
 
        };
232
 
 
233
 
        /* relative volume adjustment information */
234
 
 
235
 
        frame = id3_tag_findframe(tag, "RVA2", 0);
236
 
        if (frame == NULL)
237
 
                return false;
238
 
 
239
 
        id   = id3_field_getlatin1(id3_frame_field(frame, 0));
240
 
        data = id3_field_getbinarydata(id3_frame_field(frame, 1),
241
 
                                        &length);
242
 
 
243
 
        if (id == NULL || data == NULL)
244
 
                return false;
245
 
 
246
 
        /*
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
250
 
         *
251
 
         *   Type of channel         $xx
252
 
         *   Volume adjustment       $xx xx
253
 
         *   Bits representing peak  $xx
254
 
         *   Peak volume             $xx (xx ...)"
255
 
         */
256
 
 
257
 
        while (length >= 4) {
258
 
                unsigned int peak_bytes;
259
 
 
260
 
                peak_bytes = (data[3] + 7) / 8;
261
 
                if (4 + peak_bytes > length)
262
 
                        break;
263
 
 
264
 
                if (data[0] == CHANNEL_MASTER_VOLUME) {
265
 
                        signed int voladj_fixed;
266
 
                        double voladj_float;
267
 
 
268
 
                        /*
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."
273
 
                         */
274
 
 
275
 
                        voladj_fixed  = (data[1] << 8) | (data[2] << 0);
276
 
                        voladj_fixed |= -(voladj_fixed & 0x8000);
277
 
 
278
 
                        voladj_float  = (double) voladj_fixed / 512;
279
 
 
280
 
                        replay_gain_info->tuples[REPLAY_GAIN_TRACK].gain = voladj_float;
281
 
                        replay_gain_info->tuples[REPLAY_GAIN_ALBUM].gain = voladj_float;
282
 
 
283
 
                        g_debug("parseRVA2: Relative Volume "
284
 
                                "%+.1f dB adjustment (%s)\n",
285
 
                                voladj_float, id);
286
 
 
287
 
                        return true;
288
 
                }
289
 
 
290
 
                data   += 4 + peak_bytes;
291
 
                length -= 4 + peak_bytes;
292
 
        }
293
 
 
294
 
        return false;
295
 
}
296
 
#endif
297
 
 
298
 
#ifdef HAVE_ID3TAG
299
 
static struct replay_gain_info *
300
 
parse_id3_replay_gain_info(struct id3_tag *tag)
301
 
{
302
 
        int i;
303
 
        char *key;
304
 
        char *value;
305
 
        struct id3_frame *frame;
306
 
        bool found = false;
307
 
        struct replay_gain_info *replay_gain_info;
308
 
 
309
 
        replay_gain_info = replay_gain_info_new();
310
 
 
311
 
        for (i = 0; (frame = id3_tag_findframe(tag, "TXXX", i)); i++) {
312
 
                if (frame->nfields < 3)
313
 
                        continue;
314
 
 
315
 
                key = (char *)
316
 
                    id3_ucs4_latin1duplicate(id3_field_getstring
317
 
                                             (&frame->fields[1]));
318
 
                value = (char *)
319
 
                    id3_ucs4_latin1duplicate(id3_field_getstring
320
 
                                             (&frame->fields[2]));
321
 
 
322
 
                if (g_ascii_strcasecmp(key, "replaygain_track_gain") == 0) {
323
 
                        replay_gain_info->tuples[REPLAY_GAIN_TRACK].gain = atof(value);
324
 
                        found = true;
325
 
                } else if (g_ascii_strcasecmp(key, "replaygain_album_gain") == 0) {
326
 
                        replay_gain_info->tuples[REPLAY_GAIN_ALBUM].gain = atof(value);
327
 
                        found = true;
328
 
                } else if (g_ascii_strcasecmp(key, "replaygain_track_peak") == 0) {
329
 
                        replay_gain_info->tuples[REPLAY_GAIN_TRACK].peak = atof(value);
330
 
                        found = true;
331
 
                } else if (g_ascii_strcasecmp(key, "replaygain_album_peak") == 0) {
332
 
                        replay_gain_info->tuples[REPLAY_GAIN_ALBUM].peak = atof(value);
333
 
                        found = true;
334
 
                }
335
 
 
336
 
                free(key);
337
 
                free(value);
338
 
        }
339
 
 
340
 
        if (!found) {
341
 
                /* fall back on RVA2 if no replaygain tags found */
342
 
                found = parse_rva2(tag, replay_gain_info);
343
 
        }
344
 
 
345
 
        if (found)
346
 
                return replay_gain_info;
347
 
        replay_gain_info_free(replay_gain_info);
348
 
        return NULL;
349
 
}
350
 
#endif
351
 
 
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)
355
 
{
356
 
#ifdef HAVE_ID3TAG
357
 
        struct id3_tag *id3_tag = NULL;
358
 
        id3_length_t count;
359
 
        id3_byte_t const *id3_data;
360
 
        id3_byte_t *allocated = NULL;
361
 
 
362
 
        count = data->stream.bufend - data->stream.this_frame;
363
 
 
364
 
        if (tagsize <= count) {
365
 
                id3_data = data->stream.this_frame;
366
 
                mad_stream_skip(&(data->stream), tagsize);
367
 
        } else {
368
 
                allocated = g_malloc(tagsize);
369
 
                memcpy(allocated, data->stream.this_frame, count);
370
 
                mad_stream_skip(&(data->stream), count);
371
 
 
372
 
                while (count < tagsize) {
373
 
                        size_t len;
374
 
 
375
 
                        len = decoder_read(data->decoder, data->input_stream,
376
 
                                           allocated + count, tagsize - count);
377
 
                        if (len == 0)
378
 
                                break;
379
 
                        else
380
 
                                count += len;
381
 
                }
382
 
 
383
 
                if (count != tagsize) {
384
 
                        g_debug("error parsing ID3 tag");
385
 
                        g_free(allocated);
386
 
                        return;
387
 
                }
388
 
 
389
 
                id3_data = allocated;
390
 
        }
391
 
 
392
 
        id3_tag = id3_tag_parse(id3_data, tagsize);
393
 
        if (id3_tag == NULL) {
394
 
                g_free(allocated);
395
 
                return;
396
 
        }
397
 
 
398
 
        if (mpd_tag) {
399
 
                struct tag *tmp_tag = tag_id3_import(id3_tag);
400
 
                if (tmp_tag != NULL) {
401
 
                        if (*mpd_tag != NULL)
402
 
                                tag_free(*mpd_tag);
403
 
                        *mpd_tag = tmp_tag;
404
 
                }
405
 
        }
406
 
 
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;
414
 
                }
415
 
        }
416
 
 
417
 
        id3_tag_delete(id3_tag);
418
 
 
419
 
        g_free(allocated);
420
 
#else /* !HAVE_ID3TAG */
421
 
        (void)mpd_tag;
422
 
        (void)replay_gain_info_r;
423
 
 
424
 
        /* This code is enabled when libid3tag is disabled.  Instead
425
 
           of parsing the ID3 frame, it just skips it. */
426
 
 
427
 
        size_t count = data->stream.bufend - data->stream.this_frame;
428
 
 
429
 
        if (tagsize <= count) {
430
 
                mad_stream_skip(&data->stream, tagsize);
431
 
        } else {
432
 
                mad_stream_skip(&data->stream, count);
433
 
 
434
 
                while (count < tagsize) {
435
 
                        size_t len = tagsize - count;
436
 
                        char ignored[1024];
437
 
                        if (len > sizeof(ignored))
438
 
                                len = sizeof(ignored);
439
 
 
440
 
                        len = decoder_read(data->decoder, data->input_stream,
441
 
                                           ignored, len);
442
 
                        if (len == 0)
443
 
                                break;
444
 
                        else
445
 
                                count += len;
446
 
                }
447
 
        }
448
 
#endif
449
 
}
450
 
 
451
 
#ifndef HAVE_ID3TAG
452
 
/**
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
456
 
 * of the ID3 frame.
457
 
 */
458
 
static signed long
459
 
id3_tag_query(const void *p0, size_t length)
460
 
{
461
 
        const char *p = p0;
462
 
 
463
 
        return length >= 10 && memcmp(p, "ID3", 3) == 0
464
 
                ? (p[8] << 7) + p[9] + 10
465
 
                : 0;
466
 
}
467
 
#endif /* !HAVE_ID3TAG */
468
 
 
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)
472
 
{
473
 
        enum mad_layer layer;
474
 
 
475
 
        if ((data->stream).buffer == NULL
476
 
            || (data->stream).error == MAD_ERROR_BUFLEN) {
477
 
                if (!mp3_fill_buffer(data))
478
 
                        return DECODE_BREAK;
479
 
        }
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).
484
 
                                                            this_frame,
485
 
                                                            (data->stream).
486
 
                                                            bufend -
487
 
                                                            (data->stream).
488
 
                                                            this_frame);
489
 
 
490
 
                        if (tagsize > 0) {
491
 
                                if (tag && !(*tag)) {
492
 
                                        mp3_parse_id3(data, (size_t)tagsize,
493
 
                                                      tag, replay_gain_info_r);
494
 
                                } else {
495
 
                                        mad_stream_skip(&(data->stream),
496
 
                                                        tagsize);
497
 
                                }
498
 
                                return DECODE_CONT;
499
 
                        }
500
 
                }
501
 
                if (MAD_RECOVERABLE((data->stream).error)) {
502
 
                        return DECODE_SKIP;
503
 
                } else {
504
 
                        if ((data->stream).error == MAD_ERROR_BUFLEN)
505
 
                                return DECODE_CONT;
506
 
                        else {
507
 
                                g_warning("unrecoverable frame level error "
508
 
                                          "(%s).\n",
509
 
                                          mad_stream_errorstr(&data->stream));
510
 
                                return DECODE_BREAK;
511
 
                        }
512
 
                }
513
 
        }
514
 
 
515
 
        layer = data->frame.header.layer;
516
 
        if (!data->layer) {
517
 
                if (layer != MAD_LAYER_II && layer != MAD_LAYER_III) {
518
 
                        /* Only layer 2 and 3 have been tested to work */
519
 
                        return DECODE_SKIP;
520
 
                }
521
 
                data->layer = layer;
522
 
        } else if (layer != data->layer) {
523
 
                /* Don't decode frames with a different layer than the first */
524
 
                return DECODE_SKIP;
525
 
        }
526
 
 
527
 
        return DECODE_OK;
528
 
}
529
 
 
530
 
static enum mp3_action
531
 
decodeNextFrame(struct mp3_data *data)
532
 
{
533
 
        if ((data->stream).buffer == NULL
534
 
            || (data->stream).error == MAD_ERROR_BUFLEN) {
535
 
                if (!mp3_fill_buffer(data))
536
 
                        return DECODE_BREAK;
537
 
        }
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).
541
 
                                                            this_frame,
542
 
                                                            (data->stream).
543
 
                                                            bufend -
544
 
                                                            (data->stream).
545
 
                                                            this_frame);
546
 
                        if (tagsize > 0) {
547
 
                                mad_stream_skip(&(data->stream), tagsize);
548
 
                                return DECODE_CONT;
549
 
                        }
550
 
                }
551
 
                if (MAD_RECOVERABLE((data->stream).error)) {
552
 
                        return DECODE_SKIP;
553
 
                } else {
554
 
                        if ((data->stream).error == MAD_ERROR_BUFLEN)
555
 
                                return DECODE_CONT;
556
 
                        else {
557
 
                                g_warning("unrecoverable frame level error "
558
 
                                          "(%s).\n",
559
 
                                          mad_stream_errorstr(&data->stream));
560
 
                                return DECODE_BREAK;
561
 
                        }
562
 
                }
563
 
        }
564
 
 
565
 
        return DECODE_OK;
566
 
}
567
 
 
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')
573
 
 
574
 
enum xing_magic {
575
 
        XING_MAGIC_XING, /* VBR */
576
 
        XING_MAGIC_INFO  /* CBR */
577
 
};
578
 
 
579
 
struct xing {
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 */
586
 
};
587
 
 
588
 
enum {
589
 
        XING_FRAMES = 0x00000001L,
590
 
        XING_BYTES  = 0x00000002L,
591
 
        XING_TOC    = 0x00000004L,
592
 
        XING_SCALE  = 0x00000008L
593
 
};
594
 
 
595
 
struct version {
596
 
        unsigned major;
597
 
        unsigned minor;
598
 
};
599
 
 
600
 
struct lame {
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 */
609
 
};
610
 
 
611
 
static bool
612
 
parse_xing(struct xing *xing, struct mad_bitptr *ptr, int *oldbitlen)
613
 
{
614
 
        unsigned long bits;
615
 
        int bitlen;
616
 
        int bitsleft;
617
 
        int i;
618
 
 
619
 
        bitlen = *oldbitlen;
620
 
 
621
 
        if (bitlen < 16)
622
 
                return false;
623
 
 
624
 
        bits = mad_bit_read(ptr, 16);
625
 
        bitlen -= 16;
626
 
 
627
 
        if (bits == XI_MAGIC) {
628
 
                if (bitlen < 16)
629
 
                        return false;
630
 
 
631
 
                if (mad_bit_read(ptr, 16) != NG_MAGIC)
632
 
                        return false;
633
 
 
634
 
                bitlen -= 16;
635
 
                xing->magic = XING_MAGIC_XING;
636
 
        } else if (bits == IN_MAGIC) {
637
 
                if (bitlen < 16)
638
 
                        return false;
639
 
 
640
 
                if (mad_bit_read(ptr, 16) != FO_MAGIC)
641
 
                        return false;
642
 
 
643
 
                bitlen -= 16;
644
 
                xing->magic = XING_MAGIC_INFO;
645
 
        }
646
 
        else if (bits == NG_MAGIC) xing->magic = XING_MAGIC_XING;
647
 
        else if (bits == FO_MAGIC) xing->magic = XING_MAGIC_INFO;
648
 
        else
649
 
                return false;
650
 
 
651
 
        if (bitlen < 32)
652
 
                return false;
653
 
        xing->flags = mad_bit_read(ptr, 32);
654
 
        bitlen -= 32;
655
 
 
656
 
        if (xing->flags & XING_FRAMES) {
657
 
                if (bitlen < 32)
658
 
                        return false;
659
 
                xing->frames = mad_bit_read(ptr, 32);
660
 
                bitlen -= 32;
661
 
        }
662
 
 
663
 
        if (xing->flags & XING_BYTES) {
664
 
                if (bitlen < 32)
665
 
                        return false;
666
 
                xing->bytes = mad_bit_read(ptr, 32);
667
 
                bitlen -= 32;
668
 
        }
669
 
 
670
 
        if (xing->flags & XING_TOC) {
671
 
                if (bitlen < 800)
672
 
                        return false;
673
 
                for (i = 0; i < 100; ++i) xing->toc[i] = mad_bit_read(ptr, 8);
674
 
                bitlen -= 800;
675
 
        }
676
 
 
677
 
        if (xing->flags & XING_SCALE) {
678
 
                if (bitlen < 32)
679
 
                        return false;
680
 
                xing->scale = mad_bit_read(ptr, 32);
681
 
                bitlen -= 32;
682
 
        }
683
 
 
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);
687
 
        if (bitsleft < 0)
688
 
                return false;
689
 
        else if (bitsleft > 0) {
690
 
                mad_bit_read(ptr, bitsleft);
691
 
                bitlen -= bitsleft;
692
 
        }
693
 
 
694
 
        *oldbitlen = bitlen;
695
 
 
696
 
        return true;
697
 
}
698
 
 
699
 
static bool
700
 
parse_lame(struct lame *lame, struct mad_bitptr *ptr, int *bitlen)
701
 
{
702
 
        int adj = 0;
703
 
        int name;
704
 
        int orig;
705
 
        int sign;
706
 
        int gain;
707
 
        int i;
708
 
 
709
 
        /* Unlike the xing header, the lame tag has a fixed length.  Fail if
710
 
         * not all 36 bytes (288 bits) are there. */
711
 
        if (*bitlen < 288)
712
 
                return false;
713
 
 
714
 
        for (i = 0; i < 9; i++)
715
 
                lame->encoder[i] = (char)mad_bit_read(ptr, 8);
716
 
        lame->encoder[9] = '\0';
717
 
 
718
 
        *bitlen -= 72;
719
 
 
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"))
724
 
                return false;
725
 
 
726
 
        if (sscanf(lame->encoder+4, "%u.%u",
727
 
                   &lame->version.major, &lame->version.minor) != 2)
728
 
                return false;
729
 
 
730
 
        g_debug("detected LAME version %i.%i (\"%s\")\n",
731
 
                lame->version.major, lame->version.minor, lame->encoder);
732
 
 
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))
742
 
                adj = 6;
743
 
 
744
 
        mad_bit_read(ptr, 16);
745
 
 
746
 
        lame->peak = mad_f_todouble(mad_bit_read(ptr, 32) << 5); /* peak */
747
 
        g_debug("LAME peak found: %f\n", lame->peak);
748
 
 
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);
757
 
        }
758
 
 
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;
764
 
#if 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);
772
 
        }
773
 
#else
774
 
        mad_bit_read(ptr, 16);
775
 
#endif
776
 
 
777
 
        mad_bit_read(ptr, 16);
778
 
 
779
 
        lame->encoder_delay = mad_bit_read(ptr, 12);
780
 
        lame->encoder_padding = mad_bit_read(ptr, 12);
781
 
 
782
 
        g_debug("encoder delay is %i, encoder padding is %i\n",
783
 
              lame->encoder_delay, lame->encoder_padding);
784
 
 
785
 
        mad_bit_read(ptr, 80);
786
 
 
787
 
        lame->crc = mad_bit_read(ptr, 16);
788
 
 
789
 
        *bitlen -= 216;
790
 
 
791
 
        return true;
792
 
}
793
 
 
794
 
static inline float
795
 
mp3_frame_duration(const struct mad_frame *frame)
796
 
{
797
 
        return mad_timer_count(frame->header.duration,
798
 
                               MAD_UNITS_MILLISECONDS) / 1000.0;
799
 
}
800
 
 
801
 
static off_t
802
 
mp3_this_frame_offset(const struct mp3_data *data)
803
 
{
804
 
        off_t offset = data->input_stream->offset;
805
 
 
806
 
        if (data->stream.this_frame != NULL)
807
 
                offset -= data->stream.bufend - data->stream.this_frame;
808
 
        else
809
 
                offset -= data->stream.bufend - data->stream.buffer;
810
 
 
811
 
        return offset;
812
 
}
813
 
 
814
 
static off_t
815
 
mp3_rest_including_this_frame(const struct mp3_data *data)
816
 
{
817
 
        return data->input_stream->size - mp3_this_frame_offset(data);
818
 
}
819
 
 
820
 
/**
821
 
 * Attempt to calulcate the length of the song from filesize
822
 
 */
823
 
static void
824
 
mp3_filesize_to_song_length(struct mp3_data *data)
825
 
{
826
 
        off_t rest = mp3_rest_including_this_frame(data);
827
 
 
828
 
        if (rest > 0) {
829
 
                float frame_duration = mp3_frame_duration(&data->frame);
830
 
 
831
 
                data->total_time = (rest * 8.0) / (data->frame).header.bitrate;
832
 
                data->max_frames = data->total_time / frame_duration +
833
 
                        FRAMES_CUSHION;
834
 
        } else {
835
 
                data->max_frames = FRAMES_CUSHION;
836
 
                data->total_time = 0;
837
 
        }
838
 
}
839
 
 
840
 
static bool
841
 
mp3_decode_first_frame(struct mp3_data *data, struct tag **tag,
842
 
                       struct replay_gain_info **replay_gain_info_r)
843
 
{
844
 
        struct xing xing;
845
 
        struct lame lame;
846
 
        struct mad_bitptr ptr;
847
 
        int bitlen;
848
 
        enum mp3_action ret;
849
 
 
850
 
        /* stfu gcc */
851
 
        memset(&xing, 0, sizeof(struct xing));
852
 
        xing.flags = 0;
853
 
 
854
 
        while (true) {
855
 
                do {
856
 
                        ret = decode_next_frame_header(data, tag,
857
 
                                                       replay_gain_info_r);
858
 
                } while (ret == DECODE_CONT);
859
 
                if (ret == DECODE_BREAK)
860
 
                        return false;
861
 
                if (ret == DECODE_SKIP) continue;
862
 
 
863
 
                do {
864
 
                        ret = decodeNextFrame(data);
865
 
                } while (ret == DECODE_CONT);
866
 
                if (ret == DECODE_BREAK)
867
 
                        return false;
868
 
                if (ret == DECODE_OK) break;
869
 
        }
870
 
 
871
 
        ptr = data->stream.anc_ptr;
872
 
        bitlen = data->stream.anc_bitlen;
873
 
 
874
 
        mp3_filesize_to_song_length(data);
875
 
 
876
 
        /*
877
 
         * if an xing tag exists, use that!
878
 
         */
879
 
        if (parse_xing(&xing, &ptr, &bitlen)) {
880
 
                data->found_xing = true;
881
 
                data->mute_frame = MUTEFRAME_SKIP;
882
 
 
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;
888
 
                }
889
 
 
890
 
                if (parse_lame(&lame, &ptr, &bitlen)) {
891
 
                        if (gapless_playback &&
892
 
                            data->input_stream->seekable) {
893
 
                                data->drop_start_samples = lame.encoder_delay +
894
 
                                                           DECODERDELAY;
895
 
                                data->drop_end_samples = lame.encoder_padding;
896
 
                        }
897
 
 
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 &&
901
 
                            lame.track_gain) {
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;
905
 
                        }
906
 
                }
907
 
        } 
908
 
 
909
 
        if (!data->max_frames)
910
 
                return false;
911
 
 
912
 
        if (data->max_frames > 8 * 1024 * 1024) {
913
 
                g_warning("mp3 file header indicates too many frames: %lu\n",
914
 
                          data->max_frames);
915
 
                return false;
916
 
        }
917
 
 
918
 
        data->frame_offsets = g_malloc(sizeof(long) * data->max_frames);
919
 
        data->times = g_malloc(sizeof(mad_timer_t) * data->max_frames);
920
 
 
921
 
        return true;
922
 
}
923
 
 
924
 
static void mp3_data_finish(struct mp3_data *data)
925
 
{
926
 
        mad_synth_finish(&data->synth);
927
 
        mad_frame_finish(&data->frame);
928
 
        mad_stream_finish(&data->stream);
929
 
 
930
 
        g_free(data->frame_offsets);
931
 
        g_free(data->times);
932
 
}
933
 
 
934
 
/* this is primarily used for getting total time for tags */
935
 
static int mp3_total_file_time(const char *file)
936
 
{
937
 
        struct input_stream input_stream;
938
 
        struct mp3_data data;
939
 
        int ret;
940
 
 
941
 
        if (!input_stream_open(&input_stream, file))
942
 
                return -1;
943
 
        mp3_data_init(&data, NULL, &input_stream);
944
 
        if (!mp3_decode_first_frame(&data, NULL, NULL))
945
 
                ret = -1;
946
 
        else
947
 
                ret = data.total_time + 0.5;
948
 
        mp3_data_finish(&data);
949
 
        input_stream_close(&input_stream);
950
 
 
951
 
        return ret;
952
 
}
953
 
 
954
 
static bool
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)
958
 
{
959
 
        mp3_data_init(data, decoder, is);
960
 
        *tag = NULL;
961
 
        if (!mp3_decode_first_frame(data, tag, replay_gain_info_r)) {
962
 
                mp3_data_finish(data);
963
 
                if (tag && *tag)
964
 
                        tag_free(*tag);
965
 
                return false;
966
 
        }
967
 
 
968
 
        return true;
969
 
}
970
 
 
971
 
static long
972
 
mp3_time_to_frame(const struct mp3_data *data, double t)
973
 
{
974
 
        unsigned long i;
975
 
 
976
 
        for (i = 0; i < data->highest_frame; ++i) {
977
 
                double frame_time =
978
 
                        mad_timer_count(data->times[i],
979
 
                                        MAD_UNITS_MILLISECONDS) / 1000.;
980
 
                if (frame_time >= t)
981
 
                        break;
982
 
        }
983
 
 
984
 
        return i;
985
 
}
986
 
 
987
 
static void
988
 
mp3_update_timer_next_frame(struct mp3_data *data)
989
 
{
990
 
        if (data->current_frame >= data->highest_frame) {
991
 
                /* record this frame's properties in
992
 
                   data->frame_offsets (for seeking) and
993
 
                   data->times */
994
 
                data->bit_rate = (data->frame).header.bitrate;
995
 
 
996
 
                if (data->current_frame >= data->max_frames)
997
 
                        /* cap data->current_frame */
998
 
                        data->current_frame = data->max_frames - 1;
999
 
                else
1000
 
                        data->highest_frame++;
1001
 
 
1002
 
                data->frame_offsets[data->current_frame] =
1003
 
                        mp3_this_frame_offset(data);
1004
 
 
1005
 
                mad_timer_add(&data->timer, (data->frame).header.duration);
1006
 
                data->times[data->current_frame] = data->timer;
1007
 
        } else
1008
 
                /* get the new timer value from data->times */
1009
 
                data->timer = data->times[data->current_frame];
1010
 
 
1011
 
        data->current_frame++;
1012
 
        data->elapsed_time =
1013
 
                mad_timer_count(data->timer, MAD_UNITS_MILLISECONDS) / 1000.0;
1014
 
}
1015
 
 
1016
 
/**
1017
 
 * Sends the synthesized current frame via decoder_data().
1018
 
 */
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)
1022
 
{
1023
 
        unsigned max_samples;
1024
 
 
1025
 
        max_samples = sizeof(data->output_buffer) /
1026
 
                sizeof(data->output_buffer[0]) /
1027
 
                MAD_NCHANNELS(&(data->frame).header);
1028
 
 
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;
1034
 
 
1035
 
                i += num_samples;
1036
 
 
1037
 
                mad_fixed_to_24_buffer(data->output_buffer,
1038
 
                                       &data->synth,
1039
 
                                       i - num_samples, i,
1040
 
                                       MAD_NCHANNELS(&(data->frame).header));
1041
 
                num_samples *= MAD_NCHANNELS(&(data->frame).header);
1042
 
 
1043
 
                cmd = decoder_data(data->decoder, data->input_stream,
1044
 
                                   data->output_buffer,
1045
 
                                   sizeof(data->output_buffer[0]) * num_samples,
1046
 
                                   data->elapsed_time,
1047
 
                                   data->bit_rate / 1000,
1048
 
                                   replay_gain_info);
1049
 
                if (cmd != DECODE_COMMAND_NONE)
1050
 
                        return cmd;
1051
 
        }
1052
 
 
1053
 
        return DECODE_COMMAND_NONE;
1054
 
}
1055
 
 
1056
 
/**
1057
 
 * Synthesize the current frame and send it via decoder_data().
1058
 
 */
1059
 
static enum decoder_command
1060
 
mp3_synth_and_send(struct mp3_data *data,
1061
 
                   struct replay_gain_info *replay_gain_info)
1062
 
{
1063
 
        unsigned i, pcm_length;
1064
 
        enum decoder_command cmd;
1065
 
 
1066
 
        mad_synth_frame(&data->synth, &data->frame);
1067
 
 
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;
1075
 
        }
1076
 
 
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
1083
 
                   frames */
1084
 
                return DECODE_COMMAND_STOP;
1085
 
        }
1086
 
 
1087
 
        if (!data->decoded_first_frame) {
1088
 
                i = data->drop_start_samples;
1089
 
                data->decoded_first_frame = true;
1090
 
        } else
1091
 
                i = 0;
1092
 
 
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)
1097
 
                        pcm_length = 0;
1098
 
                else
1099
 
                        pcm_length -= data->drop_end_samples;
1100
 
        }
1101
 
 
1102
 
        cmd = mp3_send_pcm(data, i, pcm_length, replay_gain_info);
1103
 
        if (cmd != DECODE_COMMAND_NONE)
1104
 
                return cmd;
1105
 
 
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;
1111
 
 
1112
 
        return DECODE_COMMAND_NONE;
1113
 
}
1114
 
 
1115
 
static bool
1116
 
mp3_read(struct mp3_data *data, struct replay_gain_info **replay_gain_info_r)
1117
 
{
1118
 
        struct decoder *decoder = data->decoder;
1119
 
        enum mp3_action ret;
1120
 
        enum decoder_command cmd;
1121
 
 
1122
 
        mp3_update_timer_next_frame(data);
1123
 
 
1124
 
        switch (data->mute_frame) {
1125
 
        case MUTEFRAME_SKIP:
1126
 
                data->mute_frame = MUTEFRAME_NONE;
1127
 
                break;
1128
 
        case MUTEFRAME_SEEK:
1129
 
                if (data->elapsed_time >= data->seek_where)
1130
 
                        data->mute_frame = MUTEFRAME_NONE;
1131
 
                break;
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) {
1137
 
                        unsigned long j;
1138
 
 
1139
 
                        assert(data->input_stream->seekable);
1140
 
 
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);
1147
 
                                } else
1148
 
                                        decoder_seek_error(decoder);
1149
 
                        } else {
1150
 
                                data->seek_where = decoder_seek_where(decoder);
1151
 
                                data->mute_frame = MUTEFRAME_SEEK;
1152
 
                                decoder_command_finished(decoder);
1153
 
                        }
1154
 
                } else if (cmd != DECODE_COMMAND_NONE)
1155
 
                        return false;
1156
 
        }
1157
 
 
1158
 
        while (true) {
1159
 
                bool skip = false;
1160
 
 
1161
 
                do {
1162
 
                        struct tag *tag = NULL;
1163
 
 
1164
 
                        ret = decode_next_frame_header(data, &tag,
1165
 
                                                       replay_gain_info_r);
1166
 
 
1167
 
                        if (tag != NULL) {
1168
 
                                decoder_tag(decoder, data->input_stream, tag);
1169
 
                                tag_free(tag);
1170
 
                        }
1171
 
                } while (ret == DECODE_CONT);
1172
 
                if (ret == DECODE_BREAK)
1173
 
                        return false;
1174
 
                else if (ret == DECODE_SKIP)
1175
 
                        skip = true;
1176
 
 
1177
 
                if (data->mute_frame == MUTEFRAME_NONE) {
1178
 
                        do {
1179
 
                                ret = decodeNextFrame(data);
1180
 
                        } while (ret == DECODE_CONT);
1181
 
                        if (ret == DECODE_BREAK)
1182
 
                                return false;
1183
 
                }
1184
 
 
1185
 
                if (!skip && ret == DECODE_OK)
1186
 
                        break;
1187
 
        }
1188
 
 
1189
 
        return ret != DECODE_BREAK;
1190
 
}
1191
 
 
1192
 
static void mp3_audio_format(struct mp3_data *data, struct audio_format *af)
1193
 
{
1194
 
        af->bits = 24;
1195
 
        af->sample_rate = (data->frame).header.samplerate;
1196
 
        af->channels = MAD_NCHANNELS(&(data->frame).header);
1197
 
}
1198
 
 
1199
 
static void
1200
 
mp3_decode(struct decoder *decoder, struct input_stream *input_stream)
1201
 
{
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;
1206
 
 
1207
 
        if (!mp3_open(input_stream, &data, decoder, &tag, &replay_gain_info)) {
1208
 
                if (decoder_get_command(decoder) == DECODE_COMMAND_NONE)
1209
 
                        g_warning
1210
 
                            ("Input does not appear to be a mp3 bit stream.\n");
1211
 
                return;
1212
 
        }
1213
 
 
1214
 
        mp3_audio_format(&data, &audio_format);
1215
 
 
1216
 
        decoder_initialized(decoder, &audio_format,
1217
 
                            data.input_stream->seekable, data.total_time);
1218
 
 
1219
 
        if (tag != NULL) {
1220
 
                decoder_tag(decoder, input_stream, tag);
1221
 
                tag_free(tag);
1222
 
        }
1223
 
 
1224
 
        while (mp3_read(&data, &replay_gain_info)) ;
1225
 
 
1226
 
        if (replay_gain_info)
1227
 
                replay_gain_info_free(replay_gain_info);
1228
 
 
1229
 
        mp3_data_finish(&data);
1230
 
}
1231
 
 
1232
 
static struct tag *mp3_tag_dup(const char *file)
1233
 
{
1234
 
        struct tag *tag;
1235
 
        int total_time;
1236
 
 
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);
1240
 
                return NULL;
1241
 
        }
1242
 
 
1243
 
        tag = tag_new();
1244
 
        tag->time = total_time;
1245
 
        return tag;
1246
 
}
1247
 
 
1248
 
static const char *const mp3_suffixes[] = { "mp3", "mp2", NULL };
1249
 
static const char *const mp3_mime_types[] = { "audio/mpeg", NULL };
1250
 
 
1251
 
const struct decoder_plugin mad_decoder_plugin = {
1252
 
        .name = "mad",
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
1258
 
};