2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-07-26 11:59:17 +02:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2009-01-08 21:37:02 +01:00
|
|
|
#include "config.h"
|
2013-07-26 11:59:17 +02:00
|
|
|
#include "MadDecoderPlugin.hxx"
|
2013-07-28 13:18:48 +02:00
|
|
|
#include "DecoderAPI.hxx"
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "conf.h"
|
2013-07-28 20:25:45 +02:00
|
|
|
#include "TagId3.hxx"
|
2013-07-26 12:09:17 +02:00
|
|
|
#include "TagRva2.hxx"
|
2013-07-29 07:32:36 +02:00
|
|
|
#include "TagHandler.hxx"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
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>
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.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 */
|
2012-08-28 20:01:03 +02:00
|
|
|
if (gcc_unlikely(sample > MAX))
|
2008-10-23 16:58:38 +02:00
|
|
|
sample = MAX;
|
2012-08-28 20:01:03 +02:00
|
|
|
else if (gcc_unlikely(sample < MIN))
|
2008-10-23 16:58:38 +02:00
|
|
|
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
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
struct MadDecoder {
|
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;
|
2010-01-03 22:44:23 +01:00
|
|
|
bool found_replay_gain;
|
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;
|
2013-07-26 11:59:17 +02:00
|
|
|
|
|
|
|
MadDecoder(struct decoder *decoder, struct input_stream *input_stream);
|
|
|
|
~MadDecoder();
|
|
|
|
|
|
|
|
bool Seek(long offset);
|
|
|
|
bool FillBuffer();
|
2013-07-30 20:11:57 +02:00
|
|
|
void ParseId3(size_t tagsize, Tag **mpd_tag);
|
|
|
|
enum mp3_action DecodeNextFrameHeader(Tag **tag);
|
2013-07-26 11:59:17 +02:00
|
|
|
enum mp3_action DecodeNextFrame();
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
goffset ThisFrameOffset() const;
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
goffset RestIncludingThisFrame() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to calulcate the length of the song from filesize
|
|
|
|
*/
|
|
|
|
void FileSizeToSongLength();
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
bool DecodeFirstFrame(Tag **tag);
|
2013-07-26 11:59:17 +02:00
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
long TimeToFrame(double t) const;
|
|
|
|
|
|
|
|
void UpdateTimerNextFrame();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the synthesized current frame via decoder_data().
|
|
|
|
*/
|
|
|
|
enum decoder_command SendPCM(unsigned i, unsigned pcm_length);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesize the current frame and send it via
|
|
|
|
* decoder_data().
|
|
|
|
*/
|
|
|
|
enum decoder_command SyncAndSend();
|
|
|
|
|
|
|
|
bool Read();
|
2008-10-28 20:42:01 +01:00
|
|
|
};
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
MadDecoder::MadDecoder(struct decoder *_decoder,
|
|
|
|
struct input_stream *_input_stream)
|
|
|
|
:mute_frame(MUTEFRAME_NONE),
|
|
|
|
frame_offsets(nullptr),
|
|
|
|
times(nullptr),
|
|
|
|
highest_frame(0), max_frames(0), current_frame(0),
|
|
|
|
drop_start_frames(0), drop_end_frames(0),
|
|
|
|
drop_start_samples(0), drop_end_samples(0),
|
|
|
|
found_replay_gain(false), found_xing(false),
|
|
|
|
found_first_frame(false), decoded_first_frame(false),
|
|
|
|
decoder(_decoder), input_stream(_input_stream),
|
|
|
|
layer(mad_layer(0))
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_stream_init(&stream);
|
|
|
|
mad_stream_options(&stream, MAD_OPTION_IGNORECRC);
|
|
|
|
mad_frame_init(&frame);
|
|
|
|
mad_synth_init(&synth);
|
|
|
|
mad_timer_reset(&timer);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
inline bool
|
|
|
|
MadDecoder::Seek(long offset)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
if (!input_stream_lock_seek(input_stream, offset, SEEK_SET,
|
|
|
|
nullptr))
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2004-05-20 14:11:28 +02:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_stream_buffer(&stream, input_buffer, 0);
|
|
|
|
stream.error = MAD_ERROR_NONE;
|
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
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
inline bool
|
|
|
|
MadDecoder::FillBuffer()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-28 20:42:01 +01:00
|
|
|
size_t remaining, length;
|
|
|
|
unsigned char *dest;
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
if (stream.next_frame != nullptr) {
|
|
|
|
remaining = stream.bufend - stream.next_frame;
|
|
|
|
memmove(input_buffer, stream.next_frame, remaining);
|
|
|
|
dest = input_buffer + remaining;
|
2008-10-28 20:42:01 +01:00
|
|
|
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;
|
2013-07-26 11:59:17 +02:00
|
|
|
dest = 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
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
length = decoder_read(decoder, input_stream, dest, length);
|
2008-10-28 20:42:01 +01:00
|
|
|
if (length == 0)
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2004-05-18 05:37:55 +02:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_stream_buffer(&stream, input_buffer, length + remaining);
|
|
|
|
stream.error = MAD_ERROR_NONE;
|
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
|
|
|
}
|
|
|
|
|
2004-06-01 14:50:15 +02:00
|
|
|
#ifdef HAVE_ID3TAG
|
2010-02-14 20:36:31 +01:00
|
|
|
static bool
|
|
|
|
parse_id3_replay_gain_info(struct replay_gain_info *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;
|
2006-01-14 23:47:16 +01:00
|
|
|
|
2010-02-14 20:36:31 +01:00
|
|
|
replay_gain_info_init(replay_gain_info);
|
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
|
|
|
|
2009-04-28 09:29:39 +02:00
|
|
|
if (g_ascii_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;
|
2009-04-28 09:29:39 +02:00
|
|
|
} else if (g_ascii_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;
|
2009-04-28 09:29:39 +02:00
|
|
|
} else if (g_ascii_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;
|
2009-04-28 09:29:39 +02:00
|
|
|
} else if (g_ascii_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);
|
|
|
|
}
|
|
|
|
|
2010-02-14 20:36:31 +01:00
|
|
|
return found ||
|
2008-12-28 13:02:34 +01:00
|
|
|
/* fall back on RVA2 if no replaygain tags found */
|
2010-07-20 18:38:10 +02:00
|
|
|
tag_rva2_parse(tag, replay_gain_info);
|
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
|
|
|
|
2010-03-21 18:21:47 +01:00
|
|
|
#ifdef HAVE_ID3TAG
|
|
|
|
static bool
|
|
|
|
parse_id3_mixramp(char **mixramp_start, char **mixramp_end,
|
|
|
|
struct id3_tag *tag)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
struct id3_frame *frame;
|
|
|
|
bool found = false;
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
*mixramp_start = nullptr;
|
|
|
|
*mixramp_end = nullptr;
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
for (i = 0; (frame = id3_tag_findframe(tag, "TXXX", i)); i++) {
|
|
|
|
if (frame->nfields < 3)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
key = (char *)
|
|
|
|
id3_ucs4_latin1duplicate(id3_field_getstring
|
|
|
|
(&frame->fields[1]));
|
|
|
|
value = (char *)
|
|
|
|
id3_ucs4_latin1duplicate(id3_field_getstring
|
|
|
|
(&frame->fields[2]));
|
|
|
|
|
|
|
|
if (g_ascii_strcasecmp(key, "mixramp_start") == 0) {
|
2010-09-23 08:49:21 +02:00
|
|
|
*mixramp_start = g_strdup(value);
|
2010-03-21 18:21:47 +01:00
|
|
|
found = true;
|
|
|
|
} else if (g_ascii_strcasecmp(key, "mixramp_end") == 0) {
|
2010-09-23 08:49:21 +02:00
|
|
|
*mixramp_end = g_strdup(value);
|
2010-03-21 18:21:47 +01:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(key);
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
inline void
|
2013-07-30 20:11:57 +02:00
|
|
|
MadDecoder::ParseId3(size_t tagsize, Tag **mpd_tag)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-07-22 12:57:03 +02:00
|
|
|
#ifdef HAVE_ID3TAG
|
2013-07-26 11:59:17 +02:00
|
|
|
struct id3_tag *id3_tag = nullptr;
|
2004-06-01 14:50:15 +02:00
|
|
|
id3_length_t count;
|
|
|
|
id3_byte_t const *id3_data;
|
2013-07-26 11:59:17 +02:00
|
|
|
id3_byte_t *allocated = nullptr;
|
2006-01-14 23:47:16 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
count = stream.bufend - stream.this_frame;
|
2004-06-01 14:50:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tagsize <= count) {
|
2013-07-26 11:59:17 +02:00
|
|
|
id3_data = stream.this_frame;
|
|
|
|
mad_stream_skip(&(stream), tagsize);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2013-07-26 11:59:17 +02:00
|
|
|
allocated = (id3_byte_t *)g_malloc(tagsize);
|
|
|
|
memcpy(allocated, stream.this_frame, count);
|
|
|
|
mad_stream_skip(&(stream), count);
|
2004-06-01 14:50:15 +02:00
|
|
|
|
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
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
len = decoder_read(decoder, 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);
|
2013-07-26 11:59:17 +02:00
|
|
|
if (id3_tag == nullptr) {
|
2008-10-28 20:43:17 +01:00
|
|
|
g_free(allocated);
|
|
|
|
return;
|
|
|
|
}
|
2006-06-21 18:11:07 +02:00
|
|
|
|
2008-10-28 20:42:01 +01:00
|
|
|
if (mpd_tag) {
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *tmp_tag = tag_id3_import(id3_tag);
|
2013-07-26 11:59:17 +02:00
|
|
|
if (tmp_tag != nullptr) {
|
2013-07-30 20:11:57 +02:00
|
|
|
delete *mpd_tag;
|
2008-10-28 20:42:01 +01:00
|
|
|
*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
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
if (decoder != nullptr) {
|
2010-02-14 20:36:31 +01:00
|
|
|
struct replay_gain_info rgi;
|
2010-03-21 18:21:47 +01:00
|
|
|
char *mixramp_start;
|
|
|
|
char *mixramp_end;
|
2010-05-08 09:19:44 +02:00
|
|
|
|
2010-02-14 20:36:31 +01:00
|
|
|
if (parse_id3_replay_gain_info(&rgi, id3_tag)) {
|
2013-07-26 11:59:17 +02:00
|
|
|
decoder_replay_gain(decoder, &rgi);
|
|
|
|
found_replay_gain = true;
|
2006-06-21 18:38:18 +02:00
|
|
|
}
|
2012-09-25 11:08:16 +02:00
|
|
|
|
|
|
|
if (parse_id3_mixramp(&mixramp_start, &mixramp_end, id3_tag))
|
2013-07-26 11:59:17 +02:00
|
|
|
decoder_mixramp(decoder, mixramp_start, mixramp_end);
|
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);
|
2009-07-22 12:57:03 +02:00
|
|
|
#else /* !HAVE_ID3TAG */
|
|
|
|
(void)mpd_tag;
|
|
|
|
|
|
|
|
/* This code is enabled when libid3tag is disabled. Instead
|
|
|
|
of parsing the ID3 frame, it just skips it. */
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
size_t count = stream.bufend - stream.this_frame;
|
2010-04-13 07:51:29 +02:00
|
|
|
|
|
|
|
if (tagsize <= count) {
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_stream_skip(&stream, tagsize);
|
2010-04-13 07:51:29 +02:00
|
|
|
} else {
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_stream_skip(&stream, count);
|
2010-04-13 07:51:29 +02:00
|
|
|
|
|
|
|
while (count < tagsize) {
|
|
|
|
size_t len = tagsize - count;
|
|
|
|
char ignored[1024];
|
2010-05-30 17:27:03 +02:00
|
|
|
if (len > sizeof(ignored))
|
|
|
|
len = sizeof(ignored);
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
len = decoder_read(decoder, input_stream,
|
2010-04-13 07:51:29 +02:00
|
|
|
ignored, len);
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
count += len;
|
|
|
|
}
|
|
|
|
}
|
2004-06-01 14:50:15 +02:00
|
|
|
#endif
|
2009-07-22 12:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef HAVE_ID3TAG
|
|
|
|
/**
|
|
|
|
* This function emulates libid3tag when it is disabled. Instead of
|
|
|
|
* doing a real analyzation of the frame, it just checks whether the
|
2010-04-13 07:51:29 +02:00
|
|
|
* frame begins with the string "ID3". If so, it returns the length
|
|
|
|
* of the ID3 frame.
|
2009-07-22 12:57:03 +02:00
|
|
|
*/
|
|
|
|
static signed long
|
|
|
|
id3_tag_query(const void *p0, size_t length)
|
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
const char *p = (const char *)p0;
|
2009-07-22 12:57:03 +02:00
|
|
|
|
2010-04-13 07:51:29 +02:00
|
|
|
return length >= 10 && memcmp(p, "ID3", 3) == 0
|
|
|
|
? (p[8] << 7) + p[9] + 10
|
2009-07-22 12:57:03 +02:00
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
#endif /* !HAVE_ID3TAG */
|
2004-06-01 14:50:15 +02:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
enum mp3_action
|
2013-07-30 20:11:57 +02:00
|
|
|
MadDecoder::DecodeNextFrameHeader(Tag **tag)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
if ((stream.buffer == nullptr || stream.error == MAD_ERROR_BUFLEN) &&
|
|
|
|
!FillBuffer())
|
|
|
|
return DECODE_BREAK;
|
2006-08-23 15:20:24 +02:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
if (mad_header_decode(&frame.header, &stream)) {
|
|
|
|
if (stream.error == MAD_ERROR_LOSTSYNC && stream.this_frame) {
|
|
|
|
signed long tagsize = id3_tag_query(stream.this_frame,
|
|
|
|
stream.bufend -
|
|
|
|
stream.this_frame);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (tagsize > 0) {
|
|
|
|
if (tag && !(*tag)) {
|
2013-07-26 11:59:17 +02:00
|
|
|
ParseId3((size_t)tagsize, tag);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_stream_skip(&stream, tagsize);
|
2004-06-01 14:50:15 +02:00
|
|
|
}
|
2004-03-05 02:29:08 +01:00
|
|
|
return DECODE_CONT;
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 11:59:17 +02:00
|
|
|
if (MAD_RECOVERABLE(stream.error)) {
|
2004-05-18 21:32:05 +02:00
|
|
|
return DECODE_SKIP;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2013-07-26 11:59:17 +02:00
|
|
|
if (stream.error == MAD_ERROR_BUFLEN)
|
2006-07-20 18:02:40 +02:00
|
|
|
return DECODE_CONT;
|
|
|
|
else {
|
2008-11-27 19:19:05 +01:00
|
|
|
g_warning("unrecoverable frame level error "
|
|
|
|
"(%s).\n",
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_stream_errorstr(&stream));
|
2004-02-24 00:41:20 +01:00
|
|
|
return DECODE_BREAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-08-23 15:20:24 +02:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
enum mad_layer new_layer = frame.header.layer;
|
|
|
|
if (layer == (mad_layer)0) {
|
|
|
|
if (new_layer != MAD_LAYER_II && new_layer != MAD_LAYER_III) {
|
2006-08-23 15:20:24 +02:00
|
|
|
/* 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
|
|
|
}
|
2013-07-26 11:59:17 +02:00
|
|
|
|
|
|
|
layer = new_layer;
|
|
|
|
} else if (new_layer != layer) {
|
2006-08-23 15:20:24 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
enum mp3_action
|
|
|
|
MadDecoder::DecodeNextFrame()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
if ((stream.buffer == nullptr || stream.error == MAD_ERROR_BUFLEN) &&
|
|
|
|
!FillBuffer())
|
|
|
|
return DECODE_BREAK;
|
|
|
|
|
|
|
|
if (mad_frame_decode(&frame, &stream)) {
|
|
|
|
if (stream.error == MAD_ERROR_LOSTSYNC) {
|
|
|
|
signed long tagsize = id3_tag_query(stream.this_frame,
|
|
|
|
stream.bufend -
|
|
|
|
stream.this_frame);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tagsize > 0) {
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_stream_skip(&stream, tagsize);
|
2004-03-05 02:29:08 +01:00
|
|
|
return DECODE_CONT;
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 11:59:17 +02:00
|
|
|
if (MAD_RECOVERABLE(stream.error)) {
|
2004-05-18 21:32:05 +02:00
|
|
|
return DECODE_SKIP;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2013-07-26 11:59:17 +02:00
|
|
|
if (stream.error == MAD_ERROR_BUFLEN)
|
2006-07-20 18:02:40 +02:00
|
|
|
return DECODE_CONT;
|
|
|
|
else {
|
2008-11-27 19:19:05 +01:00
|
|
|
g_warning("unrecoverable frame level error "
|
|
|
|
"(%s).\n",
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_stream_errorstr(&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
|
|
|
};
|
|
|
|
|
2010-12-21 04:21:53 +01:00
|
|
|
struct lame_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") */
|
2010-12-21 04:21:53 +01:00
|
|
|
struct lame_version version; /* struct containing just the version */
|
2007-08-22 18:42:08 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
inline goffset
|
|
|
|
MadDecoder::ThisFrameOffset() const
|
2008-10-28 20:44:08 +01:00
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
goffset offset = input_stream_get_offset(input_stream);
|
2008-10-28 20:44:08 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
if (stream.this_frame != nullptr)
|
|
|
|
offset -= stream.bufend - stream.this_frame;
|
2008-10-28 20:44:08 +01:00
|
|
|
else
|
2013-07-26 11:59:17 +02:00
|
|
|
offset -= stream.bufend - stream.buffer;
|
2008-10-28 20:44:08 +01:00
|
|
|
|
2008-10-29 16:13:01 +01:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
inline goffset
|
|
|
|
MadDecoder::RestIncludingThisFrame() const
|
2008-10-29 16:13:01 +01:00
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
return input_stream_get_size(input_stream) - ThisFrameOffset();
|
2008-10-28 20:44:08 +01:00
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
inline void
|
|
|
|
MadDecoder::FileSizeToSongLength()
|
2008-10-28 20:44:08 +01:00
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
goffset rest = RestIncludingThisFrame();
|
2008-10-28 20:44:08 +01:00
|
|
|
|
|
|
|
if (rest > 0) {
|
2013-07-26 11:59:17 +02:00
|
|
|
float frame_duration = mp3_frame_duration(&frame);
|
2008-10-28 20:44:08 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
total_time = (rest * 8.0) / frame.header.bitrate;
|
|
|
|
max_frames = total_time / frame_duration + FRAMES_CUSHION;
|
2008-10-28 20:44:08 +01:00
|
|
|
} else {
|
2013-07-26 11:59:17 +02:00
|
|
|
max_frames = FRAMES_CUSHION;
|
|
|
|
total_time = 0;
|
2008-10-28 20:44:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
inline bool
|
2013-07-30 20:11:57 +02:00
|
|
|
MadDecoder::DecodeFirstFrame(Tag **tag)
|
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 {
|
2013-07-26 11:59:17 +02:00
|
|
|
ret = DecodeNextFrameHeader(tag);
|
2008-11-11 20:50:37 +01:00
|
|
|
} 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 {
|
2013-07-26 11:59:17 +02:00
|
|
|
ret = DecodeNextFrame();
|
2008-11-11 20:50:37 +01:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
ptr = stream.anc_ptr;
|
|
|
|
bitlen = stream.anc_bitlen;
|
2006-07-25 22:08:22 +02:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
FileSizeToSongLength();
|
2008-10-28 20:44:08 +01:00
|
|
|
|
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)) {
|
2013-07-26 11:59:17 +02:00
|
|
|
found_xing = true;
|
|
|
|
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) {
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_timer_t duration = frame.header.duration;
|
2006-07-20 18:02:40 +02:00
|
|
|
mad_timer_multiply(&duration, xing.frames);
|
2013-07-26 11:59:17 +02:00
|
|
|
total_time = ((float)mad_timer_count(duration, MAD_UNITS_MILLISECONDS)) / 1000;
|
|
|
|
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 &&
|
2013-07-26 11:59:17 +02:00
|
|
|
input_stream_is_seekable(input_stream)) {
|
|
|
|
drop_start_samples = lame.encoder_delay +
|
2007-08-22 18:42:08 +02:00
|
|
|
DECODERDELAY;
|
2013-07-26 11:59:17 +02:00
|
|
|
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 */
|
2013-07-26 11:59:17 +02:00
|
|
|
if (decoder != nullptr && !found_replay_gain &&
|
2008-10-28 20:42:01 +01:00
|
|
|
lame.track_gain) {
|
2010-02-14 20:36:31 +01:00
|
|
|
struct replay_gain_info rgi;
|
|
|
|
replay_gain_info_init(&rgi);
|
|
|
|
rgi.tuples[REPLAY_GAIN_TRACK].gain = lame.track_gain;
|
|
|
|
rgi.tuples[REPLAY_GAIN_TRACK].peak = lame.peak;
|
2013-07-26 11:59:17 +02:00
|
|
|
decoder_replay_gain(decoder, &rgi);
|
2007-08-22 18:42:08 +02:00
|
|
|
}
|
|
|
|
}
|
2010-02-14 20:36:31 +01:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
if (!max_frames)
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2006-10-03 02:58:20 +02:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
if (max_frames > 8 * 1024 * 1024) {
|
2008-11-27 19:19:05 +01:00
|
|
|
g_warning("mp3 file header indicates too many frames: %lu\n",
|
2013-07-26 11:59:17 +02:00
|
|
|
max_frames);
|
2008-10-28 20:42:33 +01:00
|
|
|
return false;
|
2008-09-17 22:30:34 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
frame_offsets = new long[max_frames];
|
|
|
|
times = new mad_timer_t[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
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
MadDecoder::~MadDecoder()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_synth_finish(&synth);
|
|
|
|
mad_frame_finish(&frame);
|
|
|
|
mad_stream_finish(&stream);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
delete[] frame_offsets;
|
|
|
|
delete[] times;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* this is primarily used for getting total time for tags */
|
2009-12-31 18:20:08 +01:00
|
|
|
static int
|
|
|
|
mad_decoder_total_file_time(struct input_stream *is)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
MadDecoder data(nullptr, is);
|
|
|
|
return data.DecodeFirstFrame(nullptr)
|
|
|
|
? data.total_time + 0.5
|
|
|
|
: -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
long
|
|
|
|
MadDecoder::TimeToFrame(double t) const
|
2008-10-29 16:12:26 +01:00
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
for (i = 0; i < highest_frame; ++i) {
|
2008-10-29 16:12:26 +01:00
|
|
|
double frame_time =
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_timer_count(times[i],
|
2008-10-29 16:12:26 +01:00
|
|
|
MAD_UNITS_MILLISECONDS) / 1000.;
|
|
|
|
if (frame_time >= t)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
void
|
|
|
|
MadDecoder::UpdateTimerNextFrame()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
if (current_frame >= highest_frame) {
|
|
|
|
/* record this frame's properties in frame_offsets
|
|
|
|
(for seeking) and times */
|
|
|
|
bit_rate = frame.header.bitrate;
|
|
|
|
|
|
|
|
if (current_frame >= max_frames)
|
|
|
|
/* cap current_frame */
|
|
|
|
current_frame = max_frames - 1;
|
2008-10-29 16:13:51 +01:00
|
|
|
else
|
2013-07-26 11:59:17 +02:00
|
|
|
highest_frame++;
|
2008-10-29 16:13:51 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
frame_offsets[current_frame] = ThisFrameOffset();
|
2008-10-29 16:13:51 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_timer_add(&timer, frame.header.duration);
|
|
|
|
times[current_frame] = timer;
|
2008-10-29 16:13:51 +01:00
|
|
|
} else
|
2013-07-26 11:59:17 +02:00
|
|
|
/* get the new timer value from "times" */
|
|
|
|
timer = times[current_frame];
|
2008-10-29 16:13:51 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
current_frame++;
|
|
|
|
elapsed_time = mad_timer_count(timer, MAD_UNITS_MILLISECONDS) / 1000.0;
|
2008-10-29 16:13:51 +01:00
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
enum decoder_command
|
|
|
|
MadDecoder::SendPCM(unsigned i, unsigned pcm_length)
|
2008-10-29 16:14:02 +01:00
|
|
|
{
|
|
|
|
unsigned max_samples;
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
max_samples = sizeof(output_buffer) /
|
|
|
|
sizeof(output_buffer[0]) /
|
|
|
|
MAD_NCHANNELS(&frame.header);
|
2008-10-29 16:14:02 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_fixed_to_24_buffer(output_buffer, &synth,
|
2008-10-29 16:14:02 +01:00
|
|
|
i - num_samples, i,
|
2013-07-26 11:59:17 +02:00
|
|
|
MAD_NCHANNELS(&frame.header));
|
|
|
|
num_samples *= MAD_NCHANNELS(&frame.header);
|
2008-10-29 16:14:02 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
cmd = decoder_data(decoder, input_stream, output_buffer,
|
|
|
|
sizeof(output_buffer[0]) * num_samples,
|
|
|
|
bit_rate / 1000);
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
inline enum decoder_command
|
|
|
|
MadDecoder::SyncAndSend()
|
2008-10-29 16:14:08 +01:00
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
mad_synth_frame(&synth, &frame);
|
|
|
|
|
|
|
|
if (!found_first_frame) {
|
|
|
|
unsigned int samples_per_frame = synth.pcm.length;
|
|
|
|
drop_start_frames = drop_start_samples / samples_per_frame;
|
|
|
|
drop_end_frames = drop_end_samples / samples_per_frame;
|
|
|
|
drop_start_samples = drop_start_samples % samples_per_frame;
|
|
|
|
drop_end_samples = drop_end_samples % samples_per_frame;
|
|
|
|
found_first_frame = true;
|
2008-10-29 16:14:08 +01:00
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
if (drop_start_frames > 0) {
|
|
|
|
drop_start_frames--;
|
2008-10-29 16:14:08 +01:00
|
|
|
return DECODE_COMMAND_NONE;
|
2013-07-26 11:59:17 +02:00
|
|
|
} else if ((drop_end_frames > 0) &&
|
|
|
|
(current_frame == (max_frames + 1 - drop_end_frames))) {
|
2008-10-29 16:14:08 +01:00
|
|
|
/* stop decoding, effectively dropping all remaining
|
|
|
|
frames */
|
|
|
|
return DECODE_COMMAND_STOP;
|
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
unsigned i = 0;
|
|
|
|
if (!decoded_first_frame) {
|
|
|
|
i = drop_start_samples;
|
|
|
|
decoded_first_frame = true;
|
|
|
|
}
|
2008-10-29 16:14:08 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
unsigned pcm_length = synth.pcm.length;
|
|
|
|
if (drop_end_samples &&
|
|
|
|
(current_frame == max_frames - drop_end_frames)) {
|
|
|
|
if (drop_end_samples >= pcm_length)
|
2008-10-29 16:14:08 +01:00
|
|
|
pcm_length = 0;
|
|
|
|
else
|
2013-07-26 11:59:17 +02:00
|
|
|
pcm_length -= drop_end_samples;
|
2008-10-29 16:14:08 +01:00
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
enum decoder_command cmd = SendPCM(i, pcm_length);
|
2008-10-29 16:17:21 +01:00
|
|
|
if (cmd != DECODE_COMMAND_NONE)
|
2008-10-29 16:14:08 +01:00
|
|
|
return cmd;
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
if (drop_end_samples &&
|
|
|
|
(current_frame == max_frames - drop_end_frames))
|
2008-10-29 16:14:08 +01:00
|
|
|
/* stop decoding, effectively dropping
|
|
|
|
* all remaining samples */
|
|
|
|
return DECODE_COMMAND_STOP;
|
|
|
|
|
|
|
|
return DECODE_COMMAND_NONE;
|
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
inline bool
|
|
|
|
MadDecoder::Read()
|
2008-10-29 16:13:51 +01:00
|
|
|
{
|
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
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
UpdateTimerNextFrame();
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
switch (mute_frame) {
|
2006-07-20 18:02:40 +02:00
|
|
|
case MUTEFRAME_SKIP:
|
2013-07-26 11:59:17 +02:00
|
|
|
mute_frame = MUTEFRAME_NONE;
|
2006-07-20 18:02:40 +02:00
|
|
|
break;
|
|
|
|
case MUTEFRAME_SEEK:
|
2013-07-26 11:59:17 +02:00
|
|
|
if (elapsed_time >= seek_where)
|
|
|
|
mute_frame = MUTEFRAME_NONE;
|
2006-07-20 18:02:40 +02:00
|
|
|
break;
|
2008-08-26 08:27:14 +02:00
|
|
|
case MUTEFRAME_NONE:
|
2013-07-26 11:59:17 +02:00
|
|
|
cmd = SyncAndSend();
|
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
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
assert(input_stream_is_seekable(input_stream));
|
2008-10-29 16:11:16 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
j = TimeToFrame(decoder_seek_where(decoder));
|
|
|
|
if (j < highest_frame) {
|
|
|
|
if (Seek(frame_offsets[j])) {
|
|
|
|
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 {
|
2013-07-26 11:59:17 +02:00
|
|
|
seek_where = decoder_seek_where(decoder);
|
|
|
|
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 {
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *tag = nullptr;
|
2008-11-11 20:53:24 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
ret = DecodeNextFrameHeader(&tag);
|
2008-11-11 20:53:24 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
if (tag != nullptr) {
|
2013-07-31 00:34:22 +02:00
|
|
|
decoder_tag(decoder, input_stream,
|
|
|
|
std::move(*tag));
|
2013-07-30 20:11:57 +02:00
|
|
|
delete tag;
|
2008-11-11 20:53:24 +01:00
|
|
|
}
|
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
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
if (mute_frame == MUTEFRAME_NONE) {
|
2008-11-11 20:50:37 +01:00
|
|
|
do {
|
2013-07-26 11:59:17 +02:00
|
|
|
ret = DecodeNextFrame();
|
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;
|
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-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
|
|
|
{
|
2013-07-26 11:59:17 +02:00
|
|
|
MadDecoder data(decoder, input_stream);
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *tag = nullptr;
|
2013-07-26 11:59:17 +02:00
|
|
|
if (!data.DecodeFirstFrame(&tag)) {
|
2013-07-30 20:11:57 +02:00
|
|
|
delete tag;
|
2004-02-29 09:10:52 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
struct audio_format audio_format;
|
|
|
|
GError *error = nullptr;
|
2009-11-10 19:01:38 +01:00
|
|
|
if (!audio_format_init_checked(&audio_format,
|
2009-11-10 17:11:34 +01:00
|
|
|
data.frame.header.samplerate,
|
|
|
|
SAMPLE_FORMAT_S24_P32,
|
2009-11-10 19:01:38 +01:00
|
|
|
MAD_NCHANNELS(&data.frame.header),
|
|
|
|
&error)) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
delete tag;
|
2009-11-10 19:01:38 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-11-02 17:01:51 +01:00
|
|
|
decoder_initialized(decoder, &audio_format,
|
2013-01-24 19:14:40 +01:00
|
|
|
input_stream_is_seekable(input_stream),
|
|
|
|
data.total_time);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
if (tag != nullptr) {
|
2013-07-31 00:34:22 +02:00
|
|
|
decoder_tag(decoder, input_stream, std::move(*tag));
|
2013-07-30 20:11:57 +02:00
|
|
|
delete tag;
|
2008-11-03 18:24:01 +01:00
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
while (data.Read()) {}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
|
|
|
mad_decoder_scan_stream(struct input_stream *is,
|
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-01-26 13:46:21 +01:00
|
|
|
int total_time;
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2009-12-31 18:20:08 +01:00
|
|
|
total_time = mad_decoder_total_file_time(is);
|
|
|
|
if (total_time < 0)
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_duration(handler, handler_ctx, total_time);
|
|
|
|
return true;
|
2004-05-31 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 11:59:17 +02:00
|
|
|
static const char *const mp3_suffixes[] = { "mp3", "mp2", nullptr };
|
|
|
|
static const char *const mp3_mime_types[] = { "audio/mpeg", nullptr };
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2009-02-16 19:30:45 +01:00
|
|
|
const struct decoder_plugin mad_decoder_plugin = {
|
2013-07-26 11:59:17 +02:00
|
|
|
"mad",
|
|
|
|
mp3_plugin_init,
|
|
|
|
nullptr,
|
|
|
|
mp3_decode,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
mad_decoder_scan_stream,
|
|
|
|
nullptr,
|
|
|
|
mp3_suffixes,
|
|
|
|
mp3_mime_types,
|
2004-05-31 03:21:17 +02:00
|
|
|
};
|