2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2008-08-26 08:27:04 +02:00
|
|
|
#include "../decoder_api.h"
|
2008-10-17 21:57:09 +02:00
|
|
|
#include "../conf.h"
|
2009-01-08 21:37:02 +01:00
|
|
|
#include "config.h"
|
2009-01-17 13:23:42 +01:00
|
|
|
#include "tag_id3.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-29 16:11:16 +01:00
|
|
|
#include <assert.h>
|
2008-10-28 20:43:15 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2008-10-28 20:33:56 +01:00
|
|
|
#include <glib.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
#include <mad.h>
|
2006-07-16 19:49:15 +02:00
|
|
|
|
2004-03-05 02:29:08 +01:00
|
|
|
#ifdef HAVE_ID3TAG
|
|
|
|
#include <id3tag.h>
|
|
|
|
#endif
|
2006-07-16 19:49:15 +02:00
|
|
|
|
2009-02-17 08:51:34 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "mad"
|
|
|
|
|
2007-08-22 18:42:08 +02:00
|
|
|
#define FRAMES_CUSHION 2000
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-08-22 18:42:08 +02:00
|
|
|
#define READ_BUFFER_SIZE 40960
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-26 08:27:14 +02:00
|
|
|
enum mp3_action {
|
|
|
|
DECODE_SKIP = -3,
|
|
|
|
DECODE_BREAK = -2,
|
|
|
|
DECODE_CONT = -1,
|
|
|
|
DECODE_OK = 0
|
|
|
|
};
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-26 08:27:14 +02:00
|
|
|
enum muteframe {
|
|
|
|
MUTEFRAME_NONE,
|
|
|
|
MUTEFRAME_SKIP,
|
|
|
|
MUTEFRAME_SEEK
|
|
|
|
};
|
2004-05-20 06:00:17 +02:00
|
|
|
|
2006-07-26 05:10:19 +02:00
|
|
|
/* the number of samples of silence the decoder inserts at start */
|
|
|
|
#define DECODERDELAY 529
|
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
#define DEFAULT_GAPLESS_MP3_PLAYBACK true
|
2006-12-23 19:00:15 +01:00
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
static bool gapless_playback;
|
2006-12-23 19:00:15 +01:00
|
|
|
|
2008-10-23 16:58:38 +02:00
|
|
|
static inline int32_t
|
|
|
|
mad_fixed_to_24_sample(mad_fixed_t sample)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
enum {
|
2008-10-23 16:58:38 +02:00
|
|
|
bits = 24,
|
2004-02-24 00:41:20 +01:00
|
|
|
MIN = -MAD_F_ONE,
|
2006-07-20 18:02:40 +02:00
|
|
|
MAX = MAD_F_ONE - 1
|
2004-02-24 00:41:20 +01:00
|
|
|
};
|
|
|
|
|
2008-10-23 16:58:38 +02:00
|
|
|
/* round */
|
|
|
|
sample = sample + (1L << (MAD_F_FRACBITS - bits));
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-23 16:58:38 +02:00
|
|
|
/* clip */
|
|
|
|
if (sample > MAX)
|
|
|
|
sample = MAX;
|
|
|
|
else if (sample < MIN)
|
|
|
|
sample = MIN;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-23 16:58:38 +02:00
|
|
|
/* quantize */
|
|
|
|
return sample >> (MAD_F_FRACBITS + 1 - bits);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-10-23 16:58:38 +02:00
|
|
|
static void
|
|
|
|
mad_fixed_to_24_buffer(int32_t *dest, const struct mad_synth *synth,
|
|
|
|
unsigned int start, unsigned int end,
|
|
|
|
unsigned int num_channels)
|
2008-08-26 08:27:12 +02:00
|
|
|
{
|
2008-10-10 16:40:48 +02:00
|
|
|
unsigned int i, c;
|
2008-08-26 08:27:12 +02:00
|
|
|
|
|
|
|
for (i = start; i < end; ++i) {
|
2008-10-10 16:40:48 +02:00
|
|
|
for (c = 0; c < num_channels; ++c)
|
2008-10-23 16:58:38 +02:00
|
|
|
*dest++ = mad_fixed_to_24_sample(synth->pcm.samples[c][i]);
|
2008-08-26 08:27:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-15 18:34:14 +01:00
|
|
|
static bool
|
|
|
|
mp3_plugin_init(G_GNUC_UNUSED const struct config_param *param)
|
2006-12-23 19:00:15 +01:00
|
|
|
{
|
2009-01-17 20:23:33 +01:00
|
|
|
gapless_playback = config_get_bool(CONF_GAPLESS_MP3_PLAYBACK,
|
|
|
|
DEFAULT_GAPLESS_MP3_PLAYBACK);
|
2008-10-30 08:38:54 +01:00
|
|
|
return true;
|
2006-12-23 19:00:15 +01:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:13 +02:00
|
|
|
#define MP3_DATA_OUTPUT_BUFFER_SIZE 2048
|
2004-05-07 17:58:04 +02:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
struct mp3_data {
|
2004-02-24 00:41:20 +01:00
|
|
|
struct mad_stream stream;
|
|
|
|
struct mad_frame frame;
|
|
|
|
struct mad_synth synth;
|
|
|
|
mad_timer_t timer;
|
2008-10-28 20:42:01 +01:00
|
|
|
unsigned char input_buffer[READ_BUFFER_SIZE];
|
|
|
|
int32_t output_buffer[MP3_DATA_OUTPUT_BUFFER_SIZE];
|
|
|
|
float total_time;
|
|
|
|
float elapsed_time;
|
2008-10-29 17:22:56 +01:00
|
|
|
float seek_where;
|
2008-10-28 20:42:01 +01:00
|
|
|
enum muteframe mute_frame;
|
|
|
|
long *frame_offsets;
|
2006-07-20 18:02:40 +02:00
|
|
|
mad_timer_t *times;
|
2008-10-28 20:42:01 +01:00
|
|
|
unsigned long highest_frame;
|
|
|
|
unsigned long max_frames;
|
|
|
|
unsigned long current_frame;
|
|
|
|
unsigned int drop_start_frames;
|
|
|
|
unsigned int drop_end_frames;
|
|
|
|
unsigned int drop_start_samples;
|
|
|
|
unsigned int drop_end_samples;
|
2008-10-28 20:42:33 +01:00
|
|
|
bool found_xing;
|
|
|
|
bool found_first_frame;
|
|
|
|
bool decoded_first_frame;
|
2008-10-28 20:42:01 +01:00
|
|
|
unsigned long bit_rate;
|
2008-08-26 08:27:13 +02:00
|
|
|
struct decoder *decoder;
|
2008-10-28 20:42:01 +01:00
|
|
|
struct input_stream *input_stream;
|
2006-08-23 15:20:24 +02:00
|
|
|
enum mad_layer layer;
|
2008-10-28 20:42:01 +01:00
|
|
|
};
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
static void
|
|
|
|
mp3_data_init(struct mp3_data *data, struct decoder *decoder,
|
|
|
|
struct input_stream *input_stream)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-28 20:42:01 +01:00
|
|
|
data->mute_frame = MUTEFRAME_NONE;
|
|
|
|
data->highest_frame = 0;
|
|
|
|
data->max_frames = 0;
|
|
|
|
data->frame_offsets = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
data->times = NULL;
|
2008-10-28 20:42:01 +01:00
|
|
|
data->current_frame = 0;
|
|
|
|
data->drop_start_frames = 0;
|
|
|
|
data->drop_end_frames = 0;
|
|
|
|
data->drop_start_samples = 0;
|
|
|
|
data->drop_end_samples = 0;
|
2008-10-28 20:42:33 +01:00
|
|
|
data->found_xing = false;
|
|
|
|
data->found_first_frame = false;
|
|
|
|
data->decoded_first_frame = false;
|
2008-08-26 08:27:13 +02:00
|
|
|
data->decoder = decoder;
|
2008-10-28 20:42:01 +01:00
|
|
|
data->input_stream = input_stream;
|
2006-08-23 15:20:24 +02:00
|
|
|
data->layer = 0;
|
2004-05-04 21:08:46 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
mad_stream_init(&data->stream);
|
2006-05-15 15:48:47 +02:00
|
|
|
mad_stream_options(&data->stream, MAD_OPTION_IGNORECRC);
|
2004-02-24 00:41:20 +01:00
|
|
|
mad_frame_init(&data->frame);
|
|
|
|
mad_synth_init(&data->synth);
|
|
|
|
mad_timer_reset(&data->timer);
|
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
static bool mp3_seek(struct mp3_data *data, long offset)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-28 20:42:01 +01:00
|
|
|
if (!input_stream_seek(data->input_stream, offset, SEEK_SET))
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2004-05-20 14:11:28 +02:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
mad_stream_buffer(&data->stream, data->input_buffer, 0);
|
2004-05-20 14:11:28 +02:00
|
|
|
(data->stream).error = 0;
|
2004-05-18 22:27:12 +02:00
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
return true;
|
2004-05-18 22:27:12 +02:00
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
static bool
|
|
|
|
mp3_fill_buffer(struct mp3_data *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-28 20:42:01 +01:00
|
|
|
size_t remaining, length;
|
|
|
|
unsigned char *dest;
|
|
|
|
|
|
|
|
if (data->stream.next_frame != NULL) {
|
|
|
|
remaining = data->stream.bufend - data->stream.next_frame;
|
|
|
|
memmove(data->input_buffer, data->stream.next_frame,
|
|
|
|
remaining);
|
|
|
|
dest = (data->input_buffer) + remaining;
|
|
|
|
length = READ_BUFFER_SIZE - remaining;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2008-10-28 20:42:01 +01:00
|
|
|
remaining = 0;
|
|
|
|
length = READ_BUFFER_SIZE;
|
|
|
|
dest = data->input_buffer;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2005-02-14 01:52:29 +01:00
|
|
|
/* we've exhausted the read buffer, so give up!, these potential
|
|
|
|
* mp3 frames are way too big, and thus unlikely to be mp3 frames */
|
2008-10-28 20:42:01 +01:00
|
|
|
if (length == 0)
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2005-02-14 01:52:29 +01:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
length = decoder_read(data->decoder, data->input_stream, dest, length);
|
|
|
|
if (length == 0)
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2004-05-18 05:37:55 +02:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
mad_stream_buffer(&data->stream, data->input_buffer,
|
|
|
|
length + remaining);
|
2004-02-24 00:41:20 +01:00
|
|
|
(data->stream).error = 0;
|
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-28 13:02:34 +01:00
|
|
|
#ifdef HAVE_ID3TAG
|
|
|
|
/* Parse mp3 RVA2 frame. Shamelessly stolen from madplay. */
|
|
|
|
static int parse_rva2(struct id3_tag * tag, struct replay_gain_info * replay_gain_info)
|
|
|
|
{
|
|
|
|
struct id3_frame const * frame;
|
|
|
|
|
|
|
|
id3_latin1_t const *id;
|
|
|
|
id3_byte_t const *data;
|
|
|
|
id3_length_t length;
|
|
|
|
int found;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CHANNEL_OTHER = 0x00,
|
|
|
|
CHANNEL_MASTER_VOLUME = 0x01,
|
|
|
|
CHANNEL_FRONT_RIGHT = 0x02,
|
|
|
|
CHANNEL_FRONT_LEFT = 0x03,
|
|
|
|
CHANNEL_BACK_RIGHT = 0x04,
|
|
|
|
CHANNEL_BACK_LEFT = 0x05,
|
|
|
|
CHANNEL_FRONT_CENTRE = 0x06,
|
|
|
|
CHANNEL_BACK_CENTRE = 0x07,
|
|
|
|
CHANNEL_SUBWOOFER = 0x08
|
|
|
|
};
|
|
|
|
|
|
|
|
found = 0;
|
|
|
|
|
|
|
|
/* relative volume adjustment information */
|
|
|
|
|
|
|
|
frame = id3_tag_findframe(tag, "RVA2", 0);
|
|
|
|
if (!frame) return 0;
|
|
|
|
|
|
|
|
id = id3_field_getlatin1(id3_frame_field(frame, 0));
|
|
|
|
data = id3_field_getbinarydata(id3_frame_field(frame, 1),
|
|
|
|
&length);
|
|
|
|
|
|
|
|
if (!id || !data) return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "The 'identification' string is used to identify the
|
|
|
|
* situation and/or device where this adjustment should apply.
|
|
|
|
* The following is then repeated for every channel
|
|
|
|
*
|
|
|
|
* Type of channel $xx
|
|
|
|
* Volume adjustment $xx xx
|
|
|
|
* Bits representing peak $xx
|
|
|
|
* Peak volume $xx (xx ...)"
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (length >= 4) {
|
|
|
|
unsigned int peak_bytes;
|
|
|
|
|
|
|
|
peak_bytes = (data[3] + 7) / 8;
|
|
|
|
if (4 + peak_bytes > length)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (data[0] == CHANNEL_MASTER_VOLUME) {
|
|
|
|
signed int voladj_fixed;
|
|
|
|
double voladj_float;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "The volume adjustment is encoded as a fixed
|
|
|
|
* point decibel value, 16 bit signed integer
|
|
|
|
* representing (adjustment*512), giving +/- 64
|
|
|
|
* dB with a precision of 0.001953125 dB."
|
|
|
|
*/
|
|
|
|
|
|
|
|
voladj_fixed = (data[1] << 8) | (data[2] << 0);
|
|
|
|
voladj_fixed |= -(voladj_fixed & 0x8000);
|
|
|
|
|
|
|
|
voladj_float = (double) voladj_fixed / 512;
|
|
|
|
|
|
|
|
replay_gain_info->tuples[REPLAY_GAIN_TRACK].peak = voladj_float;
|
|
|
|
replay_gain_info->tuples[REPLAY_GAIN_ALBUM].peak = voladj_float;
|
|
|
|
|
|
|
|
g_debug("parseRVA2: Relative Volume "
|
|
|
|
"%+.1f dB adjustment (%s)\n",
|
|
|
|
voladj_float, id);
|
|
|
|
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
data += 4 + peak_bytes;
|
|
|
|
length -= 4 + peak_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-01 14:50:15 +02:00
|
|
|
#ifdef HAVE_ID3TAG
|
2008-11-11 15:55:34 +01:00
|
|
|
static struct replay_gain_info *
|
|
|
|
parse_id3_replay_gain_info(struct id3_tag *tag)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-01-14 23:47:16 +01:00
|
|
|
int i;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
struct id3_frame *frame;
|
2008-10-30 08:38:54 +01:00
|
|
|
bool found = false;
|
2008-11-11 15:55:34 +01:00
|
|
|
struct replay_gain_info *replay_gain_info;
|
2006-01-14 23:47:16 +01:00
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_info = replay_gain_info_new();
|
2006-01-14 23:47:16 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; (frame = id3_tag_findframe(tag, "TXXX", i)); i++) {
|
|
|
|
if (frame->nfields < 3)
|
|
|
|
continue;
|
2006-01-14 23:47:16 +01:00
|
|
|
|
2006-07-20 20:53:56 +02:00
|
|
|
key = (char *)
|
2006-07-20 18:02:40 +02:00
|
|
|
id3_ucs4_latin1duplicate(id3_field_getstring
|
|
|
|
(&frame->fields[1]));
|
2006-07-20 20:53:56 +02:00
|
|
|
value = (char *)
|
2006-07-20 18:02:40 +02:00
|
|
|
id3_ucs4_latin1duplicate(id3_field_getstring
|
|
|
|
(&frame->fields[2]));
|
2006-01-14 23:47:16 +01:00
|
|
|
|
2006-08-23 21:10:47 +02:00
|
|
|
if (strcasecmp(key, "replaygain_track_gain") == 0) {
|
2008-11-11 16:24:27 +01:00
|
|
|
replay_gain_info->tuples[REPLAY_GAIN_TRACK].gain = atof(value);
|
2008-10-30 08:38:54 +01:00
|
|
|
found = true;
|
2006-08-23 21:10:47 +02:00
|
|
|
} else if (strcasecmp(key, "replaygain_album_gain") == 0) {
|
2008-11-11 16:24:27 +01:00
|
|
|
replay_gain_info->tuples[REPLAY_GAIN_ALBUM].gain = atof(value);
|
2008-10-30 08:38:54 +01:00
|
|
|
found = true;
|
2006-08-23 21:10:47 +02:00
|
|
|
} else if (strcasecmp(key, "replaygain_track_peak") == 0) {
|
2008-11-11 16:24:27 +01:00
|
|
|
replay_gain_info->tuples[REPLAY_GAIN_TRACK].peak = atof(value);
|
2008-10-30 08:38:54 +01:00
|
|
|
found = true;
|
2006-08-23 21:10:47 +02:00
|
|
|
} else if (strcasecmp(key, "replaygain_album_peak") == 0) {
|
2008-11-11 16:24:27 +01:00
|
|
|
replay_gain_info->tuples[REPLAY_GAIN_ALBUM].peak = atof(value);
|
2008-10-30 08:38:54 +01:00
|
|
|
found = true;
|
2006-01-14 23:47:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(key);
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
|
2008-12-28 13:02:34 +01:00
|
|
|
if (!found) {
|
|
|
|
/* fall back on RVA2 if no replaygain tags found */
|
|
|
|
found = parse_rva2(tag, replay_gain_info);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (found)
|
2008-10-28 20:42:01 +01:00
|
|
|
return replay_gain_info;
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_info_free(replay_gain_info);
|
2006-06-21 18:38:18 +02:00
|
|
|
return NULL;
|
2006-01-14 23:47:16 +01:00
|
|
|
}
|
2006-06-21 17:12:41 +02:00
|
|
|
#endif
|
2006-01-14 23:47:16 +01:00
|
|
|
|
2006-06-21 17:12:41 +02:00
|
|
|
#ifdef HAVE_ID3TAG
|
2008-10-28 20:42:01 +01:00
|
|
|
static void mp3_parse_id3(struct mp3_data *data, size_t tagsize,
|
|
|
|
struct tag **mpd_tag,
|
2008-11-11 15:55:34 +01:00
|
|
|
struct replay_gain_info **replay_gain_info_r)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-28 20:42:01 +01:00
|
|
|
struct id3_tag *id3_tag = NULL;
|
2004-06-01 14:50:15 +02:00
|
|
|
id3_length_t count;
|
|
|
|
id3_byte_t const *id3_data;
|
2006-07-20 18:02:40 +02:00
|
|
|
id3_byte_t *allocated = NULL;
|
2006-01-14 23:47:16 +01:00
|
|
|
|
2004-06-01 14:50:15 +02:00
|
|
|
count = data->stream.bufend - data->stream.this_frame;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tagsize <= count) {
|
2004-06-01 14:50:15 +02:00
|
|
|
id3_data = data->stream.this_frame;
|
|
|
|
mad_stream_skip(&(data->stream), tagsize);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2008-10-28 20:43:15 +01:00
|
|
|
allocated = g_malloc(tagsize);
|
2004-06-01 14:50:15 +02:00
|
|
|
memcpy(allocated, data->stream.this_frame, count);
|
|
|
|
mad_stream_skip(&(data->stream), count);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (count < tagsize) {
|
2008-04-12 06:15:10 +02:00
|
|
|
size_t len;
|
2004-06-01 14:50:15 +02:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
len = decoder_read(data->decoder, data->input_stream,
|
2008-08-26 08:27:14 +02:00
|
|
|
allocated + count, tagsize - count);
|
|
|
|
if (len == 0)
|
2004-06-01 14:50:15 +02:00
|
|
|
break;
|
2006-07-20 18:02:40 +02:00
|
|
|
else
|
|
|
|
count += len;
|
2004-06-01 14:50:15 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (count != tagsize) {
|
2009-02-17 08:51:34 +01:00
|
|
|
g_debug("error parsing ID3 tag");
|
2008-10-28 20:43:17 +01:00
|
|
|
g_free(allocated);
|
|
|
|
return;
|
2004-06-19 23:31:53 +02:00
|
|
|
}
|
2004-06-01 14:50:15 +02:00
|
|
|
|
|
|
|
id3_data = allocated;
|
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
id3_tag = id3_tag_parse(id3_data, tagsize);
|
2008-10-28 20:43:17 +01:00
|
|
|
if (id3_tag == NULL) {
|
|
|
|
g_free(allocated);
|
|
|
|
return;
|
|
|
|
}
|
2006-06-21 18:11:07 +02:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
if (mpd_tag) {
|
|
|
|
struct tag *tmp_tag = tag_id3_import(id3_tag);
|
|
|
|
if (tmp_tag != NULL) {
|
|
|
|
if (*mpd_tag != NULL)
|
|
|
|
tag_free(*mpd_tag);
|
|
|
|
*mpd_tag = tmp_tag;
|
2006-06-21 18:11:07 +02:00
|
|
|
}
|
2004-07-24 04:54:19 +02:00
|
|
|
}
|
2004-06-01 14:50:15 +02:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
if (replay_gain_info_r) {
|
2008-11-11 15:55:34 +01:00
|
|
|
struct replay_gain_info *tmp_rgi =
|
|
|
|
parse_id3_replay_gain_info(id3_tag);
|
2008-10-28 20:42:01 +01:00
|
|
|
if (tmp_rgi != NULL) {
|
|
|
|
if (*replay_gain_info_r)
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_info_free(*replay_gain_info_r);
|
2008-10-28 20:42:01 +01:00
|
|
|
*replay_gain_info_r = tmp_rgi;
|
2006-06-21 18:38:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
id3_tag_delete(id3_tag);
|
2008-10-28 20:43:17 +01:00
|
|
|
|
2008-10-28 20:43:15 +01:00
|
|
|
g_free(allocated);
|
2004-06-01 14:50:15 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-08-26 08:27:14 +02:00
|
|
|
static enum mp3_action
|
2008-12-24 11:07:58 +01:00
|
|
|
decode_next_frame_header(struct mp3_data *data, G_GNUC_UNUSED struct tag **tag,
|
|
|
|
G_GNUC_UNUSED struct replay_gain_info **replay_gain_info_r)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-08-23 15:20:24 +02:00
|
|
|
enum mad_layer layer;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((data->stream).buffer == NULL
|
|
|
|
|| (data->stream).error == MAD_ERROR_BUFLEN) {
|
2008-10-28 20:42:33 +01:00
|
|
|
if (!mp3_fill_buffer(data))
|
2004-02-24 00:41:20 +01:00
|
|
|
return DECODE_BREAK;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (mad_header_decode(&data->frame.header, &data->stream)) {
|
2004-03-05 02:29:08 +01:00
|
|
|
#ifdef HAVE_ID3TAG
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((data->stream).error == MAD_ERROR_LOSTSYNC &&
|
|
|
|
(data->stream).this_frame) {
|
|
|
|
signed long tagsize = id3_tag_query((data->stream).
|
|
|
|
this_frame,
|
|
|
|
(data->stream).
|
|
|
|
bufend -
|
|
|
|
(data->stream).
|
|
|
|
this_frame);
|
|
|
|
|
|
|
|
if (tagsize > 0) {
|
|
|
|
if (tag && !(*tag)) {
|
2008-10-28 20:42:01 +01:00
|
|
|
mp3_parse_id3(data, (size_t)tagsize,
|
|
|
|
tag, replay_gain_info_r);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2004-06-01 14:50:15 +02:00
|
|
|
mad_stream_skip(&(data->stream),
|
|
|
|
tagsize);
|
|
|
|
}
|
2004-03-05 02:29:08 +01:00
|
|
|
return DECODE_CONT;
|
|
|
|
}
|
|
|
|
}
|
2004-03-05 13:48:57 +01:00
|
|
|
#endif
|
2006-07-20 18:02:40 +02:00
|
|
|
if (MAD_RECOVERABLE((data->stream).error)) {
|
2004-05-18 21:32:05 +02:00
|
|
|
return DECODE_SKIP;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
if ((data->stream).error == MAD_ERROR_BUFLEN)
|
|
|
|
return DECODE_CONT;
|
|
|
|
else {
|
2008-11-27 19:19:05 +01:00
|
|
|
g_warning("unrecoverable frame level error "
|
|
|
|
"(%s).\n",
|
|
|
|
mad_stream_errorstr(&data->stream));
|
2004-02-24 00:41:20 +01:00
|
|
|
return DECODE_BREAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-08-23 15:20:24 +02:00
|
|
|
|
|
|
|
layer = data->frame.header.layer;
|
|
|
|
if (!data->layer) {
|
|
|
|
if (layer != MAD_LAYER_II && layer != MAD_LAYER_III) {
|
|
|
|
/* Only layer 2 and 3 have been tested to work */
|
2006-08-23 16:06:16 +02:00
|
|
|
return DECODE_SKIP;
|
2006-08-23 15:20:24 +02:00
|
|
|
}
|
|
|
|
data->layer = layer;
|
|
|
|
} else if (layer != data->layer) {
|
|
|
|
/* Don't decode frames with a different layer than the first */
|
2004-07-03 16:33:21 +02:00
|
|
|
return DECODE_SKIP;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return DECODE_OK;
|
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:14 +02:00
|
|
|
static enum mp3_action
|
2008-10-28 20:42:01 +01:00
|
|
|
decodeNextFrame(struct mp3_data *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if ((data->stream).buffer == NULL
|
|
|
|
|| (data->stream).error == MAD_ERROR_BUFLEN) {
|
2008-10-28 20:42:33 +01:00
|
|
|
if (!mp3_fill_buffer(data))
|
2004-02-24 00:41:20 +01:00
|
|
|
return DECODE_BREAK;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (mad_frame_decode(&data->frame, &data->stream)) {
|
2004-05-06 15:43:09 +02:00
|
|
|
#ifdef HAVE_ID3TAG
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((data->stream).error == MAD_ERROR_LOSTSYNC) {
|
|
|
|
signed long tagsize = id3_tag_query((data->stream).
|
|
|
|
this_frame,
|
|
|
|
(data->stream).
|
|
|
|
bufend -
|
|
|
|
(data->stream).
|
|
|
|
this_frame);
|
|
|
|
if (tagsize > 0) {
|
|
|
|
mad_stream_skip(&(data->stream), tagsize);
|
2004-03-05 02:29:08 +01:00
|
|
|
return DECODE_CONT;
|
|
|
|
}
|
|
|
|
}
|
2004-03-05 13:48:57 +01:00
|
|
|
#endif
|
2006-07-20 18:02:40 +02:00
|
|
|
if (MAD_RECOVERABLE((data->stream).error)) {
|
2004-05-18 21:32:05 +02:00
|
|
|
return DECODE_SKIP;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
if ((data->stream).error == MAD_ERROR_BUFLEN)
|
|
|
|
return DECODE_CONT;
|
|
|
|
else {
|
2008-11-27 19:19:05 +01:00
|
|
|
g_warning("unrecoverable frame level error "
|
|
|
|
"(%s).\n",
|
|
|
|
mad_stream_errorstr(&data->stream));
|
2004-02-24 00:41:20 +01:00
|
|
|
return DECODE_BREAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DECODE_OK;
|
|
|
|
}
|
|
|
|
|
2006-07-18 20:18:02 +02:00
|
|
|
/* xing stuff stolen from alsaplayer, and heavily modified by jat */
|
2006-05-15 15:48:47 +02:00
|
|
|
#define XI_MAGIC (('X' << 8) | 'i')
|
|
|
|
#define NG_MAGIC (('n' << 8) | 'g')
|
2006-07-18 20:18:02 +02:00
|
|
|
#define IN_MAGIC (('I' << 8) | 'n')
|
|
|
|
#define FO_MAGIC (('f' << 8) | 'o')
|
|
|
|
|
|
|
|
enum xing_magic {
|
2006-07-25 21:08:46 +02:00
|
|
|
XING_MAGIC_XING, /* VBR */
|
2006-08-01 06:18:41 +02:00
|
|
|
XING_MAGIC_INFO /* CBR */
|
2006-07-18 20:18:02 +02:00
|
|
|
};
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
struct xing {
|
2006-07-25 21:08:46 +02:00
|
|
|
long flags; /* valid fields (see below) */
|
|
|
|
unsigned long frames; /* total number of frames */
|
|
|
|
unsigned long bytes; /* total number of bytes */
|
|
|
|
unsigned char toc[100]; /* 100-point seek table */
|
|
|
|
long scale; /* VBR quality */
|
|
|
|
enum xing_magic magic; /* header magic */
|
2004-02-24 00:41:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2006-07-20 18:02:40 +02:00
|
|
|
XING_FRAMES = 0x00000001L,
|
2006-07-25 21:08:46 +02:00
|
|
|
XING_BYTES = 0x00000002L,
|
|
|
|
XING_TOC = 0x00000004L,
|
2006-08-01 06:18:41 +02:00
|
|
|
XING_SCALE = 0x00000008L
|
2004-02-24 00:41:20 +01:00
|
|
|
};
|
|
|
|
|
2007-08-22 18:42:08 +02:00
|
|
|
struct version {
|
2008-01-26 13:46:37 +01:00
|
|
|
unsigned major;
|
|
|
|
unsigned minor;
|
2007-08-22 18:42:08 +02:00
|
|
|
};
|
|
|
|
|
2006-07-26 00:49:10 +02:00
|
|
|
struct lame {
|
2007-08-22 18:42:08 +02:00
|
|
|
char encoder[10]; /* 9 byte encoder name/version ("LAME3.97b") */
|
|
|
|
struct version version; /* struct containing just the version */
|
|
|
|
float peak; /* replaygain peak */
|
2008-10-28 20:42:01 +01:00
|
|
|
float track_gain; /* replaygain track gain */
|
|
|
|
float album_gain; /* replaygain album gain */
|
|
|
|
int encoder_delay; /* # of added samples at start of mp3 */
|
|
|
|
int encoder_padding; /* # of added samples at end of mp3 */
|
2007-08-22 18:42:08 +02:00
|
|
|
int crc; /* CRC of the first 190 bytes of this frame */
|
2006-07-26 00:49:10 +02:00
|
|
|
};
|
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
static bool
|
|
|
|
parse_xing(struct xing *xing, struct mad_bitptr *ptr, int *oldbitlen)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2006-07-18 18:28:58 +02:00
|
|
|
unsigned long bits;
|
2006-07-25 22:08:22 +02:00
|
|
|
int bitlen;
|
2006-07-18 20:18:02 +02:00
|
|
|
int bitsleft;
|
|
|
|
int i;
|
|
|
|
|
2006-07-25 22:08:22 +02:00
|
|
|
bitlen = *oldbitlen;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-28 20:43:17 +01:00
|
|
|
if (bitlen < 16)
|
|
|
|
return false;
|
|
|
|
|
2006-07-18 20:18:02 +02:00
|
|
|
bits = mad_bit_read(ptr, 16);
|
2006-05-15 15:48:47 +02:00
|
|
|
bitlen -= 16;
|
|
|
|
|
2006-07-18 18:28:58 +02:00
|
|
|
if (bits == XI_MAGIC) {
|
2008-10-28 20:43:17 +01:00
|
|
|
if (bitlen < 16)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (mad_bit_read(ptr, 16) != NG_MAGIC)
|
|
|
|
return false;
|
|
|
|
|
2006-07-18 18:28:58 +02:00
|
|
|
bitlen -= 16;
|
2006-07-18 20:18:02 +02:00
|
|
|
xing->magic = XING_MAGIC_XING;
|
|
|
|
} else if (bits == IN_MAGIC) {
|
2008-10-28 20:43:17 +01:00
|
|
|
if (bitlen < 16)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (mad_bit_read(ptr, 16) != FO_MAGIC)
|
|
|
|
return false;
|
|
|
|
|
2006-07-18 20:18:02 +02:00
|
|
|
bitlen -= 16;
|
|
|
|
xing->magic = XING_MAGIC_INFO;
|
2006-07-25 21:08:46 +02:00
|
|
|
}
|
|
|
|
else if (bits == NG_MAGIC) xing->magic = XING_MAGIC_XING;
|
|
|
|
else if (bits == FO_MAGIC) xing->magic = XING_MAGIC_INFO;
|
2008-10-28 20:43:17 +01:00
|
|
|
else
|
|
|
|
return false;
|
2006-07-18 18:28:58 +02:00
|
|
|
|
2008-10-28 20:43:17 +01:00
|
|
|
if (bitlen < 32)
|
|
|
|
return false;
|
2006-07-18 20:18:02 +02:00
|
|
|
xing->flags = mad_bit_read(ptr, 32);
|
2006-07-18 18:28:58 +02:00
|
|
|
bitlen -= 32;
|
|
|
|
|
|
|
|
if (xing->flags & XING_FRAMES) {
|
2008-10-28 20:43:17 +01:00
|
|
|
if (bitlen < 32)
|
|
|
|
return false;
|
2006-07-18 20:18:02 +02:00
|
|
|
xing->frames = mad_bit_read(ptr, 32);
|
2006-07-18 18:28:58 +02:00
|
|
|
bitlen -= 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xing->flags & XING_BYTES) {
|
2008-10-28 20:43:17 +01:00
|
|
|
if (bitlen < 32)
|
|
|
|
return false;
|
2006-07-18 20:18:02 +02:00
|
|
|
xing->bytes = mad_bit_read(ptr, 32);
|
2006-07-18 18:28:58 +02:00
|
|
|
bitlen -= 32;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-18 18:28:58 +02:00
|
|
|
if (xing->flags & XING_TOC) {
|
2008-10-28 20:43:17 +01:00
|
|
|
if (bitlen < 800)
|
|
|
|
return false;
|
2006-07-25 21:08:46 +02:00
|
|
|
for (i = 0; i < 100; ++i) xing->toc[i] = mad_bit_read(ptr, 8);
|
2006-07-18 18:28:58 +02:00
|
|
|
bitlen -= 800;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xing->flags & XING_SCALE) {
|
2008-10-28 20:43:17 +01:00
|
|
|
if (bitlen < 32)
|
|
|
|
return false;
|
2006-07-18 20:18:02 +02:00
|
|
|
xing->scale = mad_bit_read(ptr, 32);
|
2006-07-18 18:28:58 +02:00
|
|
|
bitlen -= 32;
|
|
|
|
}
|
|
|
|
|
2006-07-18 20:18:02 +02:00
|
|
|
/* Make sure we consume no less than 120 bytes (960 bits) in hopes that
|
|
|
|
* the LAME tag is found there, and not right after the Xing header */
|
2006-07-25 22:08:22 +02:00
|
|
|
bitsleft = 960 - ((*oldbitlen) - bitlen);
|
2008-10-28 20:43:17 +01:00
|
|
|
if (bitsleft < 0)
|
|
|
|
return false;
|
2006-07-18 20:18:02 +02:00
|
|
|
else if (bitsleft > 0) {
|
|
|
|
mad_bit_read(ptr, bitsleft);
|
|
|
|
bitlen -= bitsleft;
|
|
|
|
}
|
|
|
|
|
2006-07-25 22:08:22 +02:00
|
|
|
*oldbitlen = bitlen;
|
2006-07-18 20:18:02 +02:00
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
static bool
|
|
|
|
parse_lame(struct lame *lame, struct mad_bitptr *ptr, int *bitlen)
|
2006-07-26 00:49:10 +02:00
|
|
|
{
|
2007-08-22 18:42:08 +02:00
|
|
|
int adj = 0;
|
|
|
|
int name;
|
|
|
|
int orig;
|
|
|
|
int sign;
|
|
|
|
int gain;
|
2006-07-26 00:49:10 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Unlike the xing header, the lame tag has a fixed length. Fail if
|
|
|
|
* not all 36 bytes (288 bits) are there. */
|
2007-08-22 18:42:08 +02:00
|
|
|
if (*bitlen < 288)
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2006-07-26 00:49:10 +02:00
|
|
|
|
2007-08-22 18:42:08 +02:00
|
|
|
for (i = 0; i < 9; i++)
|
|
|
|
lame->encoder[i] = (char)mad_bit_read(ptr, 8);
|
2006-07-26 00:49:10 +02:00
|
|
|
lame->encoder[9] = '\0';
|
|
|
|
|
2007-08-22 18:42:08 +02:00
|
|
|
*bitlen -= 72;
|
|
|
|
|
2006-07-26 00:49:10 +02:00
|
|
|
/* This is technically incorrect, since the encoder might not be lame.
|
|
|
|
* But there's no other way to determine if this is a lame tag, and we
|
|
|
|
* wouldn't want to go reading a tag that's not there. */
|
2008-10-28 20:33:56 +01:00
|
|
|
if (!g_str_has_prefix(lame->encoder, "LAME"))
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2007-08-22 18:42:08 +02:00
|
|
|
|
|
|
|
if (sscanf(lame->encoder+4, "%u.%u",
|
|
|
|
&lame->version.major, &lame->version.minor) != 2)
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2007-08-22 18:42:08 +02:00
|
|
|
|
2008-11-27 19:19:05 +01:00
|
|
|
g_debug("detected LAME version %i.%i (\"%s\")\n",
|
|
|
|
lame->version.major, lame->version.minor, lame->encoder);
|
2007-08-22 18:42:08 +02:00
|
|
|
|
|
|
|
/* The reference volume was changed from the 83dB used in the
|
|
|
|
* ReplayGain spec to 89dB in lame 3.95.1. Bump the gain for older
|
|
|
|
* versions, since everyone else uses 89dB instead of 83dB.
|
|
|
|
* Unfortunately, lame didn't differentiate between 3.95 and 3.95.1, so
|
|
|
|
* it's impossible to make the proper adjustment for 3.95.
|
|
|
|
* Fortunately, 3.95 was only out for about a day before 3.95.1 was
|
|
|
|
* released. -- tmz */
|
|
|
|
if (lame->version.major < 3 ||
|
|
|
|
(lame->version.major == 3 && lame->version.minor < 95))
|
|
|
|
adj = 6;
|
2006-07-26 00:49:10 +02:00
|
|
|
|
|
|
|
mad_bit_read(ptr, 16);
|
|
|
|
|
2007-08-22 18:42:08 +02:00
|
|
|
lame->peak = mad_f_todouble(mad_bit_read(ptr, 32) << 5); /* peak */
|
2008-11-27 19:19:05 +01:00
|
|
|
g_debug("LAME peak found: %f\n", lame->peak);
|
2007-08-22 18:42:08 +02:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
lame->track_gain = 0;
|
2007-08-22 18:42:08 +02:00
|
|
|
name = mad_bit_read(ptr, 3); /* gain name */
|
|
|
|
orig = mad_bit_read(ptr, 3); /* gain originator */
|
|
|
|
sign = mad_bit_read(ptr, 1); /* sign bit */
|
|
|
|
gain = mad_bit_read(ptr, 9); /* gain*10 */
|
|
|
|
if (gain && name == 1 && orig != 0) {
|
2008-10-28 20:42:01 +01:00
|
|
|
lame->track_gain = ((sign ? -gain : gain) / 10.0) + adj;
|
2008-11-27 19:19:05 +01:00
|
|
|
g_debug("LAME track gain found: %f\n", lame->track_gain);
|
2007-08-22 18:42:08 +02:00
|
|
|
}
|
2006-07-26 00:49:10 +02:00
|
|
|
|
2007-08-22 18:42:08 +02:00
|
|
|
/* tmz reports that this isn't currently written by any version of lame
|
|
|
|
* (as of 3.97). Since we have no way of testing it, don't use it.
|
|
|
|
* Wouldn't want to go blowing someone's ears just because we read it
|
|
|
|
* wrong. :P -- jat */
|
2008-10-28 20:42:01 +01:00
|
|
|
lame->album_gain = 0;
|
2007-08-22 18:42:08 +02:00
|
|
|
#if 0
|
|
|
|
name = mad_bit_read(ptr, 3); /* gain name */
|
|
|
|
orig = mad_bit_read(ptr, 3); /* gain originator */
|
|
|
|
sign = mad_bit_read(ptr, 1); /* sign bit */
|
|
|
|
gain = mad_bit_read(ptr, 9); /* gain*10 */
|
|
|
|
if (gain && name == 2 && orig != 0) {
|
2008-10-28 20:42:01 +01:00
|
|
|
lame->album_gain = ((sign ? -gain : gain) / 10.0) + adj;
|
2008-11-27 19:19:05 +01:00
|
|
|
g_debug("LAME album gain found: %f\n", lame->track_gain);
|
2007-08-22 18:42:08 +02:00
|
|
|
}
|
2006-07-26 00:49:10 +02:00
|
|
|
#else
|
2007-08-22 18:42:08 +02:00
|
|
|
mad_bit_read(ptr, 16);
|
2006-07-26 00:49:10 +02:00
|
|
|
#endif
|
|
|
|
|
2007-08-22 18:42:08 +02:00
|
|
|
mad_bit_read(ptr, 16);
|
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
lame->encoder_delay = mad_bit_read(ptr, 12);
|
|
|
|
lame->encoder_padding = mad_bit_read(ptr, 12);
|
2006-07-26 00:49:10 +02:00
|
|
|
|
2008-11-27 19:19:05 +01:00
|
|
|
g_debug("encoder delay is %i, encoder padding is %i\n",
|
2008-10-28 20:42:01 +01:00
|
|
|
lame->encoder_delay, lame->encoder_padding);
|
2006-07-26 00:49:10 +02:00
|
|
|
|
2007-08-22 18:42:08 +02:00
|
|
|
mad_bit_read(ptr, 80);
|
|
|
|
|
|
|
|
lame->crc = mad_bit_read(ptr, 16);
|
|
|
|
|
|
|
|
*bitlen -= 216;
|
2006-07-26 00:49:10 +02:00
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
return true;
|
2006-07-26 00:49:10 +02:00
|
|
|
}
|
|
|
|
|
2008-10-28 20:44:08 +01:00
|
|
|
static inline float
|
|
|
|
mp3_frame_duration(const struct mad_frame *frame)
|
|
|
|
{
|
|
|
|
return mad_timer_count(frame->header.duration,
|
|
|
|
MAD_UNITS_MILLISECONDS) / 1000.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static off_t
|
2008-10-29 16:13:01 +01:00
|
|
|
mp3_this_frame_offset(const struct mp3_data *data)
|
2008-10-28 20:44:08 +01:00
|
|
|
{
|
|
|
|
off_t offset = data->input_stream->offset;
|
|
|
|
|
|
|
|
if (data->stream.this_frame != NULL)
|
|
|
|
offset -= data->stream.bufend - data->stream.this_frame;
|
|
|
|
else
|
|
|
|
offset -= data->stream.bufend - data->stream.buffer;
|
|
|
|
|
2008-10-29 16:13:01 +01:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static off_t
|
|
|
|
mp3_rest_including_this_frame(const struct mp3_data *data)
|
|
|
|
{
|
|
|
|
return data->input_stream->size - mp3_this_frame_offset(data);
|
2008-10-28 20:44:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to calulcate the length of the song from filesize
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
mp3_filesize_to_song_length(struct mp3_data *data)
|
|
|
|
{
|
|
|
|
off_t rest = mp3_rest_including_this_frame(data);
|
|
|
|
|
|
|
|
if (rest > 0) {
|
|
|
|
float frame_duration = mp3_frame_duration(&data->frame);
|
|
|
|
|
|
|
|
data->total_time = (rest * 8.0) / (data->frame).header.bitrate;
|
|
|
|
data->max_frames = data->total_time / frame_duration +
|
|
|
|
FRAMES_CUSHION;
|
|
|
|
} else {
|
|
|
|
data->max_frames = FRAMES_CUSHION;
|
|
|
|
data->total_time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
static bool
|
2008-10-28 20:42:01 +01:00
|
|
|
mp3_decode_first_frame(struct mp3_data *data, struct tag **tag,
|
2008-11-11 15:55:34 +01:00
|
|
|
struct replay_gain_info **replay_gain_info_r)
|
2004-06-01 14:50:15 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
struct xing xing;
|
2006-07-26 00:49:10 +02:00
|
|
|
struct lame lame;
|
2006-07-25 22:08:22 +02:00
|
|
|
struct mad_bitptr ptr;
|
|
|
|
int bitlen;
|
2008-12-08 16:49:19 +01:00
|
|
|
enum mp3_action ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-26 05:31:21 +02:00
|
|
|
/* stfu gcc */
|
|
|
|
memset(&xing, 0, sizeof(struct xing));
|
|
|
|
xing.flags = 0;
|
2006-07-26 05:28:43 +02:00
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
while (true) {
|
2008-11-11 20:50:37 +01:00
|
|
|
do {
|
|
|
|
ret = decode_next_frame_header(data, tag,
|
|
|
|
replay_gain_info_r);
|
|
|
|
} while (ret == DECODE_CONT);
|
|
|
|
if (ret == DECODE_BREAK)
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2006-08-23 15:43:23 +02:00
|
|
|
if (ret == DECODE_SKIP) continue;
|
2006-07-25 21:08:46 +02:00
|
|
|
|
2008-11-11 20:50:37 +01:00
|
|
|
do {
|
|
|
|
ret = decodeNextFrame(data);
|
|
|
|
} while (ret == DECODE_CONT);
|
|
|
|
if (ret == DECODE_BREAK)
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2006-08-23 15:43:23 +02:00
|
|
|
if (ret == DECODE_OK) break;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
2006-07-25 22:08:22 +02:00
|
|
|
ptr = data->stream.anc_ptr;
|
|
|
|
bitlen = data->stream.anc_bitlen;
|
|
|
|
|
2008-10-28 20:44:08 +01:00
|
|
|
mp3_filesize_to_song_length(data);
|
|
|
|
|
2006-10-03 02:58:20 +02:00
|
|
|
/*
|
|
|
|
* if an xing tag exists, use that!
|
|
|
|
*/
|
2006-07-26 05:17:30 +02:00
|
|
|
if (parse_xing(&xing, &ptr, &bitlen)) {
|
2008-10-28 20:42:33 +01:00
|
|
|
data->found_xing = true;
|
2008-10-28 20:42:01 +01:00
|
|
|
data->mute_frame = MUTEFRAME_SKIP;
|
2006-07-26 05:14:19 +02:00
|
|
|
|
2006-10-03 02:58:20 +02:00
|
|
|
if ((xing.flags & XING_FRAMES) && xing.frames) {
|
2004-02-24 00:41:20 +01:00
|
|
|
mad_timer_t duration = data->frame.header.duration;
|
2006-07-20 18:02:40 +02:00
|
|
|
mad_timer_multiply(&duration, xing.frames);
|
2008-10-28 20:42:01 +01:00
|
|
|
data->total_time = ((float)mad_timer_count(duration, MAD_UNITS_MILLISECONDS)) / 1000;
|
|
|
|
data->max_frames = xing.frames;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2007-08-22 18:42:08 +02:00
|
|
|
|
|
|
|
if (parse_lame(&lame, &ptr, &bitlen)) {
|
2008-10-28 20:42:01 +01:00
|
|
|
if (gapless_playback &&
|
|
|
|
data->input_stream->seekable) {
|
|
|
|
data->drop_start_samples = lame.encoder_delay +
|
2007-08-22 18:42:08 +02:00
|
|
|
DECODERDELAY;
|
2008-10-28 20:42:01 +01:00
|
|
|
data->drop_end_samples = lame.encoder_padding;
|
2007-08-22 18:42:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Album gain isn't currently used. See comment in
|
|
|
|
* parse_lame() for details. -- jat */
|
2008-10-28 20:42:01 +01:00
|
|
|
if (replay_gain_info_r && !*replay_gain_info_r &&
|
|
|
|
lame.track_gain) {
|
2008-11-11 15:55:34 +01:00
|
|
|
*replay_gain_info_r = replay_gain_info_new();
|
2008-11-11 16:24:27 +01:00
|
|
|
(*replay_gain_info_r)->tuples[REPLAY_GAIN_TRACK].gain = lame.track_gain;
|
|
|
|
(*replay_gain_info_r)->tuples[REPLAY_GAIN_TRACK].peak = lame.peak;
|
2007-08-22 18:42:08 +02:00
|
|
|
}
|
|
|
|
}
|
2006-07-25 21:08:46 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
if (!data->max_frames)
|
|
|
|
return false;
|
2006-10-03 02:58:20 +02:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
if (data->max_frames > 8 * 1024 * 1024) {
|
2008-11-27 19:19:05 +01:00
|
|
|
g_warning("mp3 file header indicates too many frames: %lu\n",
|
|
|
|
data->max_frames);
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2008-09-17 22:30:34 +02:00
|
|
|
}
|
|
|
|
|
2008-10-28 20:43:15 +01:00
|
|
|
data->frame_offsets = g_malloc(sizeof(long) * data->max_frames);
|
|
|
|
data->times = g_malloc(sizeof(mad_timer_t) * data->max_frames);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
static void mp3_data_finish(struct mp3_data *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
mad_synth_finish(&data->synth);
|
|
|
|
mad_frame_finish(&data->frame);
|
|
|
|
mad_stream_finish(&data->stream);
|
|
|
|
|
2008-10-28 20:43:15 +01:00
|
|
|
g_free(data->frame_offsets);
|
|
|
|
g_free(data->times);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* this is primarily used for getting total time for tags */
|
2008-10-31 15:56:43 +01:00
|
|
|
static int mp3_total_file_time(const char *file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-28 20:42:01 +01:00
|
|
|
struct input_stream input_stream;
|
|
|
|
struct mp3_data data;
|
2004-02-24 00:41:20 +01:00
|
|
|
int ret;
|
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
if (!input_stream_open(&input_stream, file))
|
2006-07-20 18:02:40 +02:00
|
|
|
return -1;
|
2008-10-28 20:42:01 +01:00
|
|
|
mp3_data_init(&data, NULL, &input_stream);
|
2008-10-28 20:42:33 +01:00
|
|
|
if (!mp3_decode_first_frame(&data, NULL, NULL))
|
2006-07-20 18:02:40 +02:00
|
|
|
ret = -1;
|
|
|
|
else
|
2008-10-28 20:42:01 +01:00
|
|
|
ret = data.total_time + 0.5;
|
|
|
|
mp3_data_finish(&data);
|
|
|
|
input_stream_close(&input_stream);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
static bool
|
2008-10-28 20:42:01 +01:00
|
|
|
mp3_open(struct input_stream *is, struct mp3_data *data,
|
|
|
|
struct decoder *decoder, struct tag **tag,
|
2008-11-11 15:55:34 +01:00
|
|
|
struct replay_gain_info **replay_gain_info_r)
|
2004-05-20 00:03:27 +02:00
|
|
|
{
|
2008-10-28 20:42:01 +01:00
|
|
|
mp3_data_init(data, decoder, is);
|
2004-06-01 14:50:15 +02:00
|
|
|
*tag = NULL;
|
2008-10-28 20:42:33 +01:00
|
|
|
if (!mp3_decode_first_frame(data, tag, replay_gain_info_r)) {
|
2008-10-28 20:42:01 +01:00
|
|
|
mp3_data_finish(data);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag && *tag)
|
2008-08-29 09:38:21 +02:00
|
|
|
tag_free(*tag);
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-29 16:12:26 +01:00
|
|
|
static long
|
|
|
|
mp3_time_to_frame(const struct mp3_data *data, double t)
|
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
|
|
|
|
for (i = 0; i < data->highest_frame; ++i) {
|
|
|
|
double frame_time =
|
|
|
|
mad_timer_count(data->times[i],
|
|
|
|
MAD_UNITS_MILLISECONDS) / 1000.;
|
|
|
|
if (frame_time >= t)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2008-10-29 16:13:51 +01:00
|
|
|
static void
|
|
|
|
mp3_update_timer_next_frame(struct mp3_data *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-28 20:42:01 +01:00
|
|
|
if (data->current_frame >= data->highest_frame) {
|
2008-10-29 16:13:51 +01:00
|
|
|
/* record this frame's properties in
|
|
|
|
data->frame_offsets (for seeking) and
|
|
|
|
data->times */
|
2008-10-28 20:42:01 +01:00
|
|
|
data->bit_rate = (data->frame).header.bitrate;
|
2008-10-29 16:13:51 +01:00
|
|
|
|
|
|
|
if (data->current_frame >= data->max_frames)
|
|
|
|
/* cap data->current_frame */
|
2008-10-28 20:42:01 +01:00
|
|
|
data->current_frame = data->max_frames - 1;
|
2008-10-29 16:13:51 +01:00
|
|
|
else
|
2008-10-28 20:42:01 +01:00
|
|
|
data->highest_frame++;
|
2008-10-29 16:13:51 +01:00
|
|
|
|
2008-10-29 16:13:01 +01:00
|
|
|
data->frame_offsets[data->current_frame] =
|
|
|
|
mp3_this_frame_offset(data);
|
2008-10-29 16:13:51 +01:00
|
|
|
|
|
|
|
mad_timer_add(&data->timer, (data->frame).header.duration);
|
2008-10-28 20:42:01 +01:00
|
|
|
data->times[data->current_frame] = data->timer;
|
2008-10-29 16:13:51 +01:00
|
|
|
} else
|
|
|
|
/* get the new timer value from data->times */
|
2008-10-28 20:42:01 +01:00
|
|
|
data->timer = data->times[data->current_frame];
|
2008-10-29 16:13:51 +01:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
data->current_frame++;
|
|
|
|
data->elapsed_time =
|
2008-10-29 16:13:51 +01:00
|
|
|
mad_timer_count(data->timer, MAD_UNITS_MILLISECONDS) / 1000.0;
|
|
|
|
}
|
|
|
|
|
2008-10-29 16:14:02 +01:00
|
|
|
/**
|
|
|
|
* Sends the synthesized current frame via decoder_data().
|
|
|
|
*/
|
|
|
|
static enum decoder_command
|
|
|
|
mp3_send_pcm(struct mp3_data *data, unsigned i, unsigned pcm_length,
|
2008-11-11 15:55:34 +01:00
|
|
|
struct replay_gain_info *replay_gain_info)
|
2008-10-29 16:14:02 +01:00
|
|
|
{
|
|
|
|
unsigned max_samples;
|
|
|
|
|
|
|
|
max_samples = sizeof(data->output_buffer) /
|
|
|
|
sizeof(data->output_buffer[0]) /
|
|
|
|
MAD_NCHANNELS(&(data->frame).header);
|
|
|
|
|
|
|
|
while (i < pcm_length) {
|
|
|
|
enum decoder_command cmd;
|
|
|
|
unsigned int num_samples = pcm_length - i;
|
|
|
|
if (num_samples > max_samples)
|
|
|
|
num_samples = max_samples;
|
|
|
|
|
|
|
|
i += num_samples;
|
|
|
|
|
|
|
|
mad_fixed_to_24_buffer(data->output_buffer,
|
|
|
|
&data->synth,
|
|
|
|
i - num_samples, i,
|
|
|
|
MAD_NCHANNELS(&(data->frame).header));
|
|
|
|
num_samples *= MAD_NCHANNELS(&(data->frame).header);
|
|
|
|
|
|
|
|
cmd = decoder_data(data->decoder, data->input_stream,
|
|
|
|
data->output_buffer,
|
|
|
|
sizeof(data->output_buffer[0]) * num_samples,
|
|
|
|
data->elapsed_time,
|
|
|
|
data->bit_rate / 1000,
|
|
|
|
replay_gain_info);
|
2008-10-29 16:17:21 +01:00
|
|
|
if (cmd != DECODE_COMMAND_NONE)
|
2008-10-29 16:14:02 +01:00
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DECODE_COMMAND_NONE;
|
|
|
|
}
|
|
|
|
|
2008-10-29 16:14:08 +01:00
|
|
|
/**
|
|
|
|
* Synthesize the current frame and send it via decoder_data().
|
|
|
|
*/
|
|
|
|
static enum decoder_command
|
2008-11-11 15:55:34 +01:00
|
|
|
mp3_synth_and_send(struct mp3_data *data,
|
|
|
|
struct replay_gain_info *replay_gain_info)
|
2008-10-29 16:14:08 +01:00
|
|
|
{
|
|
|
|
unsigned i, pcm_length;
|
|
|
|
enum decoder_command cmd;
|
|
|
|
|
|
|
|
mad_synth_frame(&data->synth, &data->frame);
|
|
|
|
|
|
|
|
if (!data->found_first_frame) {
|
|
|
|
unsigned int samples_per_frame = data->synth.pcm.length;
|
|
|
|
data->drop_start_frames = data->drop_start_samples / samples_per_frame;
|
|
|
|
data->drop_end_frames = data->drop_end_samples / samples_per_frame;
|
|
|
|
data->drop_start_samples = data->drop_start_samples % samples_per_frame;
|
|
|
|
data->drop_end_samples = data->drop_end_samples % samples_per_frame;
|
|
|
|
data->found_first_frame = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->drop_start_frames > 0) {
|
|
|
|
data->drop_start_frames--;
|
|
|
|
return DECODE_COMMAND_NONE;
|
|
|
|
} else if ((data->drop_end_frames > 0) &&
|
|
|
|
(data->current_frame == (data->max_frames + 1 - data->drop_end_frames))) {
|
|
|
|
/* stop decoding, effectively dropping all remaining
|
|
|
|
frames */
|
|
|
|
return DECODE_COMMAND_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data->decoded_first_frame) {
|
|
|
|
i = data->drop_start_samples;
|
|
|
|
data->decoded_first_frame = true;
|
|
|
|
} else
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
pcm_length = data->synth.pcm.length;
|
|
|
|
if (data->drop_end_samples &&
|
|
|
|
(data->current_frame == data->max_frames - data->drop_end_frames)) {
|
|
|
|
if (data->drop_end_samples >= pcm_length)
|
|
|
|
pcm_length = 0;
|
|
|
|
else
|
|
|
|
pcm_length -= data->drop_end_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = mp3_send_pcm(data, i, pcm_length, replay_gain_info);
|
2008-10-29 16:17:21 +01:00
|
|
|
if (cmd != DECODE_COMMAND_NONE)
|
2008-10-29 16:14:08 +01:00
|
|
|
return cmd;
|
|
|
|
|
|
|
|
if (data->drop_end_samples &&
|
|
|
|
(data->current_frame == data->max_frames - data->drop_end_frames))
|
|
|
|
/* stop decoding, effectively dropping
|
|
|
|
* all remaining samples */
|
|
|
|
return DECODE_COMMAND_STOP;
|
|
|
|
|
|
|
|
return DECODE_COMMAND_NONE;
|
|
|
|
}
|
|
|
|
|
2008-10-30 06:09:28 +01:00
|
|
|
static bool
|
2008-11-11 15:55:34 +01:00
|
|
|
mp3_read(struct mp3_data *data, struct replay_gain_info **replay_gain_info_r)
|
2008-10-29 16:13:51 +01:00
|
|
|
{
|
|
|
|
struct decoder *decoder = data->decoder;
|
2008-12-08 16:49:19 +01:00
|
|
|
enum mp3_action ret;
|
2008-10-29 16:14:02 +01:00
|
|
|
enum decoder_command cmd;
|
2008-10-29 16:13:51 +01:00
|
|
|
|
|
|
|
mp3_update_timer_next_frame(data);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
switch (data->mute_frame) {
|
2006-07-20 18:02:40 +02:00
|
|
|
case MUTEFRAME_SKIP:
|
2008-10-28 20:42:01 +01:00
|
|
|
data->mute_frame = MUTEFRAME_NONE;
|
2006-07-20 18:02:40 +02:00
|
|
|
break;
|
|
|
|
case MUTEFRAME_SEEK:
|
2008-10-29 17:22:56 +01:00
|
|
|
if (data->elapsed_time >= data->seek_where)
|
2008-10-28 20:42:01 +01:00
|
|
|
data->mute_frame = MUTEFRAME_NONE;
|
2006-07-20 18:02:40 +02:00
|
|
|
break;
|
2008-08-26 08:27:14 +02:00
|
|
|
case MUTEFRAME_NONE:
|
2008-10-29 16:14:08 +01:00
|
|
|
cmd = mp3_synth_and_send(data,
|
|
|
|
replay_gain_info_r != NULL
|
|
|
|
? *replay_gain_info_r : NULL);
|
2008-11-13 14:43:19 +01:00
|
|
|
if (cmd == DECODE_COMMAND_SEEK) {
|
2008-10-29 16:12:26 +01:00
|
|
|
unsigned long j;
|
2008-10-29 16:11:16 +01:00
|
|
|
|
|
|
|
assert(data->input_stream->seekable);
|
|
|
|
|
2008-10-29 16:12:26 +01:00
|
|
|
j = mp3_time_to_frame(data,
|
|
|
|
decoder_seek_where(decoder));
|
2008-10-28 20:42:01 +01:00
|
|
|
if (j < data->highest_frame) {
|
2008-10-28 20:42:33 +01:00
|
|
|
if (mp3_seek(data, data->frame_offsets[j])) {
|
2008-10-28 20:42:01 +01:00
|
|
|
data->current_frame = j;
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_command_finished(decoder);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_seek_error(decoder);
|
2008-10-29 17:22:56 +01:00
|
|
|
} else {
|
|
|
|
data->seek_where = decoder_seek_where(decoder);
|
2008-10-29 16:12:26 +01:00
|
|
|
data->mute_frame = MUTEFRAME_SEEK;
|
2008-10-29 17:22:56 +01:00
|
|
|
decoder_command_finished(decoder);
|
|
|
|
}
|
2008-11-13 14:43:19 +01:00
|
|
|
} else if (cmd != DECODE_COMMAND_NONE)
|
|
|
|
return false;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
while (true) {
|
2008-10-30 08:38:54 +01:00
|
|
|
bool skip = false;
|
|
|
|
|
2008-11-11 20:50:37 +01:00
|
|
|
do {
|
2008-11-11 20:53:24 +01:00
|
|
|
struct tag *tag = NULL;
|
|
|
|
|
|
|
|
ret = decode_next_frame_header(data, &tag,
|
2008-11-11 20:50:37 +01:00
|
|
|
replay_gain_info_r);
|
2008-11-11 20:53:24 +01:00
|
|
|
|
|
|
|
if (tag != NULL) {
|
|
|
|
decoder_tag(decoder, data->input_stream, tag);
|
|
|
|
tag_free(tag);
|
|
|
|
}
|
2008-11-11 20:50:37 +01:00
|
|
|
} while (ret == DECODE_CONT);
|
|
|
|
if (ret == DECODE_BREAK)
|
2008-10-30 06:09:28 +01:00
|
|
|
return false;
|
2006-07-20 18:02:40 +02:00
|
|
|
else if (ret == DECODE_SKIP)
|
2008-10-30 08:38:54 +01:00
|
|
|
skip = true;
|
2008-11-11 20:50:37 +01:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
if (data->mute_frame == MUTEFRAME_NONE) {
|
2008-11-11 20:50:37 +01:00
|
|
|
do {
|
|
|
|
ret = decodeNextFrame(data);
|
|
|
|
} while (ret == DECODE_CONT);
|
|
|
|
if (ret == DECODE_BREAK)
|
2008-10-30 06:09:28 +01:00
|
|
|
return false;
|
2004-03-05 14:06:31 +01:00
|
|
|
}
|
2008-11-11 20:50:37 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!skip && ret == DECODE_OK)
|
|
|
|
break;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-03-05 14:06:31 +01:00
|
|
|
|
2008-10-30 06:09:28 +01:00
|
|
|
return ret != DECODE_BREAK;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
static void mp3_audio_format(struct mp3_data *data, struct audio_format *af)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-23 16:58:38 +02:00
|
|
|
af->bits = 24;
|
2008-10-10 14:40:54 +02:00
|
|
|
af->sample_rate = (data->frame).header.samplerate;
|
2004-02-24 00:41:20 +01:00
|
|
|
af->channels = MAD_NCHANNELS(&(data->frame).header);
|
|
|
|
}
|
|
|
|
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2008-10-28 20:42:01 +01:00
|
|
|
mp3_decode(struct decoder *decoder, struct input_stream *input_stream)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-28 20:42:01 +01:00
|
|
|
struct mp3_data data;
|
2008-08-29 09:38:11 +02:00
|
|
|
struct tag *tag = NULL;
|
2008-11-11 15:55:34 +01:00
|
|
|
struct replay_gain_info *replay_gain_info = NULL;
|
2008-09-07 19:19:55 +02:00
|
|
|
struct audio_format audio_format;
|
2004-02-29 09:10:52 +01:00
|
|
|
|
2008-10-28 20:42:33 +01:00
|
|
|
if (!mp3_open(input_stream, &data, decoder, &tag, &replay_gain_info)) {
|
2008-11-11 17:13:44 +01:00
|
|
|
if (decoder_get_command(decoder) == DECODE_COMMAND_NONE)
|
2008-11-27 19:19:05 +01:00
|
|
|
g_warning
|
2006-07-20 18:02:40 +02:00
|
|
|
("Input does not appear to be a mp3 bit stream.\n");
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
mp3_audio_format(&data, &audio_format);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-11-02 17:01:51 +01:00
|
|
|
decoder_initialized(decoder, &audio_format,
|
|
|
|
data.input_stream->seekable, data.total_time);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-11-03 18:24:01 +01:00
|
|
|
if (tag != NULL) {
|
|
|
|
decoder_tag(decoder, input_stream, tag);
|
|
|
|
tag_free(tag);
|
|
|
|
}
|
|
|
|
|
2008-10-30 06:09:28 +01:00
|
|
|
while (mp3_read(&data, &replay_gain_info)) ;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
if (replay_gain_info)
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_info_free(replay_gain_info);
|
2006-01-14 23:47:16 +01:00
|
|
|
|
2008-08-26 08:27:07 +02:00
|
|
|
if (decoder_get_command(decoder) == DECODE_COMMAND_SEEK &&
|
2008-10-29 17:28:47 +01:00
|
|
|
data.mute_frame == MUTEFRAME_SEEK)
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_command_finished(decoder);
|
2004-07-03 16:13:43 +02:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
mp3_data_finish(&data);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-31 15:56:43 +01:00
|
|
|
static struct tag *mp3_tag_dup(const char *file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-17 13:23:42 +01:00
|
|
|
struct tag *tag;
|
2008-01-26 13:46:21 +01:00
|
|
|
int total_time;
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
total_time = mp3_total_file_time(file);
|
2009-01-17 13:23:42 +01:00
|
|
|
if (total_time < 0) {
|
2009-02-17 08:51:34 +01:00
|
|
|
g_debug("Failed to get total song time from: %s", file);
|
2009-01-17 13:23:42 +01:00
|
|
|
return NULL;
|
2005-09-08 23:08:02 +02:00
|
|
|
}
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2009-01-17 13:23:42 +01:00
|
|
|
tag = tag_new();
|
|
|
|
tag->time = total_time;
|
|
|
|
return tag;
|
2004-05-31 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2008-11-01 14:55:23 +01:00
|
|
|
static const char *const mp3_suffixes[] = { "mp3", "mp2", NULL };
|
|
|
|
static const char *const mp3_mime_types[] = { "audio/mpeg", NULL };
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2009-02-16 19:30:45 +01:00
|
|
|
const struct decoder_plugin mad_decoder_plugin = {
|
|
|
|
.name = "mad",
|
2008-09-29 15:55:17 +02:00
|
|
|
.init = mp3_plugin_init,
|
|
|
|
.stream_decode = mp3_decode,
|
2008-10-28 20:42:01 +01:00
|
|
|
.tag_dup = mp3_tag_dup,
|
2008-09-29 15:55:17 +02:00
|
|
|
.suffixes = mp3_suffixes,
|
2008-10-28 20:42:01 +01:00
|
|
|
.mime_types = mp3_mime_types
|
2004-05-31 03:21:17 +02:00
|
|
|
};
|