2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2006-07-14 21:37:45 +02:00
|
|
|
* (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2004-05-31 03:21:17 +02:00
|
|
|
#include "../inputPlugin.h"
|
2004-03-18 04:29:25 +01:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#ifdef HAVE_MAD
|
|
|
|
|
2004-05-31 03:21:17 +02:00
|
|
|
#include "../pcm_utils.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
|
|
|
|
2004-05-31 03:21:17 +02:00
|
|
|
#include "../log.h"
|
|
|
|
#include "../utils.h"
|
2006-01-14 23:47:16 +01:00
|
|
|
#include "../replayGain.h"
|
2004-05-31 03:21:17 +02:00
|
|
|
#include "../tag.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2006-01-14 23:47:16 +01:00
|
|
|
#include <stdlib.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2004-04-11 06:49:27 +02:00
|
|
|
#include <errno.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#define FRAMES_CUSHION 2000
|
|
|
|
|
|
|
|
#define READ_BUFFER_SIZE 40960
|
|
|
|
|
2004-03-04 21:45:49 +01:00
|
|
|
#define DECODE_SKIP -3
|
2004-02-24 00:41:20 +01:00
|
|
|
#define DECODE_BREAK -2
|
|
|
|
#define DECODE_CONT -1
|
|
|
|
#define DECODE_OK 0
|
|
|
|
|
2004-05-20 06:00:17 +02:00
|
|
|
#define MUTEFRAME_SKIP 1
|
|
|
|
#define MUTEFRAME_SEEK 2
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
/* this is stolen from mpg321! */
|
|
|
|
struct audio_dither {
|
|
|
|
mad_fixed_t error[3];
|
|
|
|
mad_fixed_t random;
|
|
|
|
};
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static unsigned long prng(unsigned long state)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static signed long audio_linear_dither(unsigned int bits, mad_fixed_t sample,
|
|
|
|
struct audio_dither *dither)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
unsigned int scalebits;
|
|
|
|
mad_fixed_t output, mask, random;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
sample += dither->error[0] - dither->error[1] + dither->error[2];
|
|
|
|
|
|
|
|
dither->error[2] = dither->error[1];
|
|
|
|
dither->error[1] = dither->error[0] / 2;
|
|
|
|
|
|
|
|
output = sample + (1L << (MAD_F_FRACBITS + 1 - bits - 1));
|
|
|
|
|
|
|
|
scalebits = MAD_F_FRACBITS + 1 - bits;
|
|
|
|
mask = (1L << scalebits) - 1;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
random = prng(dither->random);
|
2004-02-24 00:41:20 +01:00
|
|
|
output += (random & mask) - (dither->random & mask);
|
|
|
|
|
|
|
|
dither->random = random;
|
|
|
|
|
|
|
|
if (output > MAX) {
|
|
|
|
output = MAX;
|
|
|
|
|
|
|
|
if (sample > MAX)
|
|
|
|
sample = MAX;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (output < MIN) {
|
|
|
|
output = MIN;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
if (sample < MIN)
|
|
|
|
sample = MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
output &= ~mask;
|
|
|
|
|
|
|
|
dither->error[0] = sample - output;
|
|
|
|
|
|
|
|
return output >> scalebits;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
/* end of stolen stuff from mpg321 */
|
|
|
|
|
|
|
|
/* decoder stuff is based on madlld */
|
|
|
|
|
2004-05-07 17:58:04 +02:00
|
|
|
#define MP3_DATA_OUTPUT_BUFFER_SIZE 4096
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
typedef struct _mp3DecodeData {
|
|
|
|
struct mad_stream stream;
|
|
|
|
struct mad_frame frame;
|
|
|
|
struct mad_synth synth;
|
|
|
|
mad_timer_t timer;
|
|
|
|
unsigned char readBuffer[READ_BUFFER_SIZE];
|
2004-05-07 17:58:04 +02:00
|
|
|
char outputBuffer[MP3_DATA_OUTPUT_BUFFER_SIZE];
|
2006-07-20 18:02:40 +02:00
|
|
|
char *outputPtr;
|
|
|
|
char *outputBufferEnd;
|
2004-02-24 00:41:20 +01:00
|
|
|
float totalTime;
|
|
|
|
float elapsedTime;
|
|
|
|
int muteFrame;
|
2006-07-20 18:02:40 +02:00
|
|
|
long *frameOffset;
|
|
|
|
mad_timer_t *times;
|
2004-02-24 00:41:20 +01:00
|
|
|
long highestFrame;
|
|
|
|
long maxFrames;
|
|
|
|
long currentFrame;
|
|
|
|
int flush;
|
|
|
|
unsigned long bitRate;
|
2006-07-20 18:02:40 +02:00
|
|
|
InputStream *inStream;
|
2004-06-20 03:37:18 +02:00
|
|
|
struct audio_dither dither;
|
2004-02-24 00:41:20 +01:00
|
|
|
} mp3DecodeData;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void initMp3DecodeData(mp3DecodeData * data, InputStream * inStream)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
data->outputPtr = data->outputBuffer;
|
2006-07-20 18:02:40 +02:00
|
|
|
data->outputBufferEnd =
|
|
|
|
data->outputBuffer + MP3_DATA_OUTPUT_BUFFER_SIZE;
|
2004-02-24 00:41:20 +01:00
|
|
|
data->muteFrame = 0;
|
|
|
|
data->highestFrame = 0;
|
|
|
|
data->maxFrames = 0;
|
|
|
|
data->frameOffset = NULL;
|
|
|
|
data->times = NULL;
|
|
|
|
data->currentFrame = 0;
|
|
|
|
data->flush = 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
data->inStream = inStream;
|
2004-06-20 03:37:18 +02:00
|
|
|
memset(&(data->dither), 0, sizeof(struct audio_dither));
|
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);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int seekMp3InputBuffer(mp3DecodeData * data, long offset)
|
|
|
|
{
|
|
|
|
if (seekInputStream(data->inStream, offset, SEEK_SET) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2004-05-20 14:11:28 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
mad_stream_buffer(&data->stream, data->readBuffer, 0);
|
2004-05-20 14:11:28 +02:00
|
|
|
(data->stream).error = 0;
|
2004-05-18 22:27:12 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int fillMp3InputBuffer(mp3DecodeData * data)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
size_t readSize;
|
|
|
|
size_t remaining;
|
2006-07-20 18:02:40 +02:00
|
|
|
size_t readed;
|
|
|
|
unsigned char *readStart;
|
|
|
|
|
|
|
|
if ((data->stream).next_frame != NULL) {
|
|
|
|
remaining = (data->stream).bufend - (data->stream).next_frame;
|
|
|
|
memmove(data->readBuffer, (data->stream).next_frame, remaining);
|
|
|
|
readStart = (data->readBuffer) + remaining;
|
|
|
|
readSize = READ_BUFFER_SIZE - remaining;
|
|
|
|
} else {
|
2004-02-24 00:41:20 +01:00
|
|
|
readSize = READ_BUFFER_SIZE;
|
2006-07-20 18:02:40 +02:00
|
|
|
readStart = data->readBuffer, remaining = 0;
|
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 */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (readSize == 0)
|
|
|
|
return -1;
|
2005-02-14 01:52:29 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
readed = readFromInputStream(data->inStream, readStart, (size_t) 1,
|
|
|
|
readSize);
|
|
|
|
if (readed <= 0 && inputStreamAtEOF(data->inStream))
|
|
|
|
return -1;
|
2004-05-18 22:27:12 +02:00
|
|
|
/* sleep for a fraction of a second! */
|
2006-07-20 18:02:40 +02:00
|
|
|
else if (readed <= 0) {
|
2004-06-20 03:37:18 +02:00
|
|
|
readed = 0;
|
|
|
|
my_usleep(10000);
|
|
|
|
}
|
2004-05-18 05:37:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
mad_stream_buffer(&data->stream, data->readBuffer, readed + remaining);
|
2004-02-24 00:41:20 +01:00
|
|
|
(data->stream).error = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:50:15 +02:00
|
|
|
#ifdef HAVE_ID3TAG
|
2006-07-20 18:02:40 +02:00
|
|
|
static ReplayGainInfo *parseId3ReplayGainInfo(struct id3_tag *tag)
|
|
|
|
{
|
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;
|
2006-01-14 23:47:16 +01:00
|
|
|
int found = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
ReplayGainInfo *replayGainInfo;
|
2006-01-14 23:47:16 +01:00
|
|
|
|
2006-06-21 18:38:18 +02:00
|
|
|
replayGainInfo = newReplayGainInfo();
|
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-07-20 18:02:40 +02:00
|
|
|
if (strcmp(key, "replaygain_track_gain") == 0) {
|
2006-06-21 18:38:18 +02:00
|
|
|
replayGainInfo->trackGain = atof(value);
|
2006-01-14 23:47:16 +01:00
|
|
|
found = 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (strcmp(key, "replaygain_album_gain") == 0) {
|
2006-06-21 18:38:18 +02:00
|
|
|
replayGainInfo->albumGain = atof(value);
|
2006-01-14 23:47:16 +01:00
|
|
|
found = 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (strcmp(key, "replaygain_track_peak") == 0) {
|
2006-06-21 18:38:18 +02:00
|
|
|
replayGainInfo->trackPeak = atof(value);
|
2006-01-14 23:47:16 +01:00
|
|
|
found = 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (strcmp(key, "replaygain_album_peak") == 0) {
|
2006-06-21 18:38:18 +02:00
|
|
|
replayGainInfo->albumPeak = atof(value);
|
2006-01-14 23:47:16 +01:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(key);
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (found)
|
|
|
|
return replayGainInfo;
|
2006-06-21 18:38:18 +02:00
|
|
|
freeReplayGainInfo(replayGainInfo);
|
|
|
|
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
|
2006-07-20 18:02:40 +02:00
|
|
|
static void mp3_parseId3Tag(mp3DecodeData * data, signed long tagsize,
|
|
|
|
MpdTag ** mpdTag, ReplayGainInfo ** replayGainInfo)
|
|
|
|
{
|
|
|
|
struct id3_tag *id3Tag = 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;
|
|
|
|
MpdTag *tmpMpdTag;
|
|
|
|
ReplayGainInfo *tmpReplayGainInfo;
|
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 {
|
2004-06-01 14:50:15 +02:00
|
|
|
allocated = malloc(tagsize);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!allocated)
|
|
|
|
goto fail;
|
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) {
|
2004-06-01 14:50:15 +02:00
|
|
|
int len;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
len = readFromInputStream(data->inStream,
|
|
|
|
allocated + count, (size_t) 1,
|
|
|
|
tagsize - count);
|
|
|
|
if (len <= 0 && inputStreamAtEOF(data->inStream)) {
|
2004-06-01 14:50:15 +02:00
|
|
|
break;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (len <= 0)
|
|
|
|
my_usleep(10000);
|
|
|
|
else
|
|
|
|
count += len;
|
2004-06-01 14:50:15 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (count != tagsize) {
|
2004-06-19 23:31:53 +02:00
|
|
|
DEBUG("mp3_decode: error parsing ID3 tag\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2004-06-01 14:50:15 +02:00
|
|
|
|
|
|
|
id3_data = allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
id3Tag = id3_tag_parse(id3_data, tagsize);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!id3Tag)
|
|
|
|
goto fail;
|
2006-06-21 18:11:07 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (mpdTag) {
|
2006-06-21 18:22:46 +02:00
|
|
|
tmpMpdTag = parseId3Tag(id3Tag);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tmpMpdTag) {
|
|
|
|
if (*mpdTag)
|
|
|
|
freeMpdTag(*mpdTag);
|
2006-06-21 18:22:46 +02:00
|
|
|
*mpdTag = tmpMpdTag;
|
2006-06-21 18:11:07 +02:00
|
|
|
}
|
2004-07-24 04:54:19 +02:00
|
|
|
}
|
2004-06-01 14:50:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (replayGainInfo) {
|
2006-06-21 18:38:18 +02:00
|
|
|
tmpReplayGainInfo = parseId3ReplayGainInfo(id3Tag);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tmpReplayGainInfo) {
|
|
|
|
if (*replayGainInfo)
|
|
|
|
freeReplayGainInfo(*replayGainInfo);
|
2006-06-21 18:38:18 +02:00
|
|
|
*replayGainInfo = tmpReplayGainInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-21 18:11:07 +02:00
|
|
|
id3_tag_delete(id3Tag);
|
2006-07-20 18:02:40 +02:00
|
|
|
fail:
|
|
|
|
if (allocated)
|
|
|
|
free(allocated);
|
2004-06-01 14:50:15 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int decodeNextFrameHeader(mp3DecodeData * data, MpdTag ** tag,
|
|
|
|
ReplayGainInfo ** replayGainInfo)
|
|
|
|
{
|
|
|
|
if ((data->stream).buffer == NULL
|
|
|
|
|| (data->stream).error == MAD_ERROR_BUFLEN) {
|
|
|
|
if (fillMp3InputBuffer(data) < 0) {
|
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)) {
|
|
|
|
mp3_parseId3Tag(data, tagsize, tag,
|
|
|
|
replayGainInfo);
|
|
|
|
} 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 {
|
2004-02-24 00:41:20 +01:00
|
|
|
ERROR("unrecoverable frame level error "
|
2006-07-20 18:02:40 +02:00
|
|
|
"(%s).\n",
|
|
|
|
mad_stream_errorstr(&data->stream));
|
2004-02-24 00:41:20 +01:00
|
|
|
data->flush = 0;
|
|
|
|
return DECODE_BREAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (data->frame.header.layer != MAD_LAYER_III) {
|
2004-07-03 16:33:21 +02:00
|
|
|
return DECODE_SKIP;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return DECODE_OK;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int decodeNextFrame(mp3DecodeData * data)
|
|
|
|
{
|
|
|
|
if ((data->stream).buffer == NULL
|
|
|
|
|| (data->stream).error == MAD_ERROR_BUFLEN) {
|
|
|
|
if (fillMp3InputBuffer(data) < 0) {
|
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 {
|
2004-02-24 00:41:20 +01:00
|
|
|
ERROR("unrecoverable frame level error "
|
2006-07-20 18:02:40 +02:00
|
|
|
"(%s).\n",
|
|
|
|
mad_stream_errorstr(&data->stream));
|
2004-02-24 00:41:20 +01:00
|
|
|
data->flush = 0;
|
|
|
|
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 */
|
|
|
|
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,
|
|
|
|
XING_SCALE = 0x00000008L,
|
2004-02-24 00:41:20 +01:00
|
|
|
};
|
|
|
|
|
2006-07-25 22:08:22 +02:00
|
|
|
static int 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
|
|
|
|
2006-07-25 21:08:46 +02:00
|
|
|
if (bitlen < 16) goto fail;
|
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) {
|
2006-07-25 21:08:46 +02:00
|
|
|
if (bitlen < 16) goto fail;
|
|
|
|
if (mad_bit_read(ptr, 16) != NG_MAGIC) goto fail;
|
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) {
|
2006-07-25 21:08:46 +02:00
|
|
|
if (bitlen < 16) goto fail;
|
|
|
|
if (mad_bit_read(ptr, 16) != FO_MAGIC) goto fail;
|
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;
|
|
|
|
else goto fail;
|
2006-07-18 18:28:58 +02:00
|
|
|
|
2006-07-25 21:08:46 +02:00
|
|
|
if (bitlen < 32) goto fail;
|
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) {
|
2006-07-25 21:08:46 +02:00
|
|
|
if (bitlen < 32) goto fail;
|
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) {
|
2006-07-25 21:08:46 +02:00
|
|
|
if (bitlen < 32) goto fail;
|
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) {
|
2006-07-25 21:08:46 +02:00
|
|
|
if (bitlen < 800) goto fail;
|
|
|
|
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) {
|
2006-07-25 21:08:46 +02:00
|
|
|
if (bitlen < 32) goto fail;
|
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);
|
2006-07-25 21:08:46 +02:00
|
|
|
if (bitsleft < 0) goto fail;
|
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
|
|
|
|
|
|
|
return 1;
|
2006-07-25 22:08:22 +02:00
|
|
|
fail:
|
|
|
|
xing->flags = 0;
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-17 03:47:32 +02:00
|
|
|
static int decodeFirstFrame(mp3DecodeData * data, DecoderControl * dc,
|
2006-07-25 21:08:46 +02:00
|
|
|
MpdTag ** tag, ReplayGainInfo ** replayGainInfo)
|
2004-06-01 14:50:15 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
struct xing xing;
|
2006-07-25 22:08:22 +02:00
|
|
|
struct mad_bitptr ptr;
|
|
|
|
int bitlen;
|
|
|
|
int found_xing;
|
2004-02-24 00:41:20 +01:00
|
|
|
int ret;
|
2004-03-04 21:45:49 +01:00
|
|
|
int skip;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (1) {
|
2004-03-04 21:50:32 +01:00
|
|
|
skip = 0;
|
2006-07-25 21:08:46 +02:00
|
|
|
while ((ret = decodeNextFrameHeader(data, tag, replayGainInfo)) == DECODE_CONT &&
|
|
|
|
(!dc || !dc->stop));
|
|
|
|
|
|
|
|
if (ret == DECODE_SKIP) skip = 1;
|
|
|
|
else if (ret == DECODE_BREAK || (dc && dc->stop)) return -1;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while ((ret = decodeNextFrame(data)) == DECODE_CONT &&
|
2006-07-25 21:08:46 +02:00
|
|
|
(!dc || !dc->stop));
|
|
|
|
|
|
|
|
if (ret == DECODE_BREAK || (dc && dc->stop)) return -1;
|
|
|
|
if (!skip && 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;
|
|
|
|
|
|
|
|
found_xing = parse_xing(&xing, &ptr, &bitlen);
|
|
|
|
|
|
|
|
if (found_xing) {
|
2006-07-20 18:02:40 +02:00
|
|
|
if (xing.flags & 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);
|
2004-05-20 06:00:17 +02:00
|
|
|
data->muteFrame = MUTEFRAME_SKIP;
|
2006-07-25 21:08:46 +02:00
|
|
|
data->totalTime = ((float)mad_timer_count(duration, MAD_UNITS_MILLISECONDS)) / 1000;
|
2004-02-24 00:41:20 +01:00
|
|
|
data->maxFrames = xing.frames;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2004-05-18 15:13:55 +02:00
|
|
|
size_t offset = data->inStream->offset;
|
2004-02-24 00:41:20 +01:00
|
|
|
mad_timer_t duration = data->frame.header.duration;
|
2006-07-25 21:08:46 +02:00
|
|
|
float frameTime = ((float)mad_timer_count(duration, MAD_UNITS_MILLISECONDS)) / 1000;
|
|
|
|
|
|
|
|
if (data->stream.this_frame != NULL)
|
2006-07-20 18:02:40 +02:00
|
|
|
offset -= data->stream.bufend - data->stream.this_frame;
|
2006-07-25 21:08:46 +02:00
|
|
|
else
|
2006-07-20 18:02:40 +02:00
|
|
|
offset -= data->stream.bufend - data->stream.buffer;
|
2006-07-25 21:08:46 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (data->inStream->size >= offset) {
|
2006-07-25 21:08:46 +02:00
|
|
|
data->totalTime = ((data->inStream->size - offset) * 8.0) / (data->frame).header.bitrate;
|
|
|
|
data->maxFrames = data->totalTime / frameTime + FRAMES_CUSHION;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2004-05-18 16:58:47 +02:00
|
|
|
data->maxFrames = FRAMES_CUSHION;
|
|
|
|
data->totalTime = 0;
|
|
|
|
}
|
2006-07-25 21:08:46 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
data->frameOffset = malloc(sizeof(long) * data->maxFrames);
|
|
|
|
data->times = malloc(sizeof(mad_timer_t) * data->maxFrames);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void mp3DecodeDataFinalize(mp3DecodeData * data)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
mad_synth_finish(&data->synth);
|
|
|
|
mad_frame_finish(&data->frame);
|
|
|
|
mad_stream_finish(&data->stream);
|
|
|
|
|
2006-07-25 21:08:46 +02:00
|
|
|
if (data->frameOffset) free(data->frameOffset);
|
|
|
|
if (data->times) free(data->times);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* this is primarily used for getting total time for tags */
|
2006-07-20 18:02:40 +02:00
|
|
|
static int getMp3TotalTime(char *file)
|
|
|
|
{
|
|
|
|
InputStream inStream;
|
2004-02-24 00:41:20 +01:00
|
|
|
mp3DecodeData data;
|
|
|
|
int ret;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (openInputStream(&inStream, file) < 0)
|
|
|
|
return -1;
|
|
|
|
initMp3DecodeData(&data, &inStream);
|
|
|
|
if (decodeFirstFrame(&data, NULL, NULL, NULL) < 0)
|
|
|
|
ret = -1;
|
|
|
|
else
|
|
|
|
ret = data.totalTime + 0.5;
|
2004-02-24 00:41:20 +01:00
|
|
|
mp3DecodeDataFinalize(&data);
|
2004-06-03 04:56:26 +02:00
|
|
|
closeInputStream(&inStream);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-17 03:47:32 +02:00
|
|
|
static int openMp3FromInputStream(InputStream * inStream, mp3DecodeData * data,
|
2006-07-20 18:02:40 +02:00
|
|
|
DecoderControl * dc, MpdTag ** tag,
|
|
|
|
ReplayGainInfo ** replayGainInfo)
|
2004-05-20 00:03:27 +02:00
|
|
|
{
|
2004-05-18 15:13:55 +02:00
|
|
|
initMp3DecodeData(data, inStream);
|
2004-06-01 14:50:15 +02:00
|
|
|
*tag = NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (decodeFirstFrame(data, dc, tag, replayGainInfo) < 0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
mp3DecodeDataFinalize(data);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag && *tag)
|
|
|
|
freeMpdTag(*tag);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int mp3Read(mp3DecodeData * data, OutputBuffer * cb, DecoderControl * dc,
|
|
|
|
ReplayGainInfo ** replayGainInfo)
|
|
|
|
{
|
2004-05-20 06:00:17 +02:00
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
int skip;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (data->currentFrame >= data->highestFrame) {
|
|
|
|
mad_timer_add(&data->timer, (data->frame).header.duration);
|
2004-02-24 00:41:20 +01:00
|
|
|
data->bitRate = (data->frame).header.bitrate;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (data->currentFrame >= data->maxFrames) {
|
2004-05-18 16:58:47 +02:00
|
|
|
data->currentFrame = data->maxFrames - 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
data->highestFrame++;
|
2004-05-18 15:13:55 +02:00
|
|
|
data->frameOffset[data->currentFrame] = data->inStream->offset;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (data->stream.this_frame != NULL) {
|
|
|
|
data->frameOffset[data->currentFrame] -=
|
|
|
|
data->stream.bufend - data->stream.this_frame;
|
|
|
|
} else {
|
|
|
|
data->frameOffset[data->currentFrame] -=
|
|
|
|
data->stream.bufend - data->stream.buffer;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
data->times[data->currentFrame] = data->timer;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
data->timer = data->times[data->currentFrame];
|
2004-02-24 00:41:20 +01:00
|
|
|
data->currentFrame++;
|
2006-07-20 18:02:40 +02:00
|
|
|
data->elapsedTime =
|
|
|
|
((float)mad_timer_count(data->timer, MAD_UNITS_MILLISECONDS)) /
|
|
|
|
1000;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (data->muteFrame) {
|
|
|
|
case MUTEFRAME_SKIP:
|
2004-05-20 06:00:17 +02:00
|
|
|
data->muteFrame = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
break;
|
|
|
|
case MUTEFRAME_SEEK:
|
|
|
|
if (dc->seekWhere <= data->elapsedTime) {
|
|
|
|
data->outputPtr = data->outputBuffer;
|
|
|
|
clearOutputBuffer(cb);
|
2004-02-24 00:41:20 +01:00
|
|
|
data->muteFrame = 0;
|
|
|
|
dc->seek = 0;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mad_synth_frame(&data->synth, &data->frame);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (data->inStream->metaTitle) {
|
|
|
|
MpdTag *tag = newMpdTag();
|
|
|
|
if (data->inStream->metaName) {
|
2004-11-10 22:58:27 +01:00
|
|
|
addItemToMpdTag(tag,
|
|
|
|
TAG_ITEM_NAME,
|
|
|
|
data->inStream->metaName);
|
2004-06-07 07:00:56 +02:00
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
addItemToMpdTag(tag, TAG_ITEM_TITLE,
|
|
|
|
data->inStream->metaTitle);
|
2004-06-07 07:00:56 +02:00
|
|
|
free(data->inStream->metaTitle);
|
|
|
|
data->inStream->metaTitle = NULL;
|
|
|
|
copyMpdTagToOutputBuffer(cb, tag);
|
|
|
|
freeMpdTag(tag);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < (data->synth).pcm.length; i++) {
|
|
|
|
mpd_sint16 *sample;
|
2004-03-20 17:18:47 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
sample = (mpd_sint16 *) data->outputPtr;
|
2004-03-20 17:18:47 +01:00
|
|
|
*sample = (mpd_sint16) audio_linear_dither(16,
|
2006-07-20 18:02:40 +02:00
|
|
|
(data->
|
|
|
|
synth).pcm.
|
|
|
|
samples[0]
|
|
|
|
[i],
|
|
|
|
&(data->
|
|
|
|
dither));
|
|
|
|
data->outputPtr += 2;
|
|
|
|
|
|
|
|
if (MAD_NCHANNELS(&(data->frame).header) == 2) {
|
|
|
|
sample = (mpd_sint16 *) data->outputPtr;
|
2004-03-20 17:18:47 +01:00
|
|
|
*sample = (mpd_sint16) audio_linear_dither(16,
|
2006-07-20 18:02:40 +02:00
|
|
|
(data->
|
|
|
|
synth).
|
|
|
|
pcm.
|
|
|
|
samples
|
|
|
|
[1]
|
|
|
|
[i],
|
|
|
|
&
|
|
|
|
(data->
|
|
|
|
dither));
|
|
|
|
data->outputPtr += 2;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (data->outputPtr >= data->outputBufferEnd) {
|
|
|
|
long ret;
|
|
|
|
ret = sendDataToOutputBuffer(cb,
|
|
|
|
data->inStream,
|
|
|
|
dc,
|
|
|
|
data->inStream->
|
|
|
|
seekable,
|
|
|
|
data->outputBuffer,
|
|
|
|
data->outputPtr -
|
|
|
|
data->outputBuffer,
|
|
|
|
data->elapsedTime,
|
|
|
|
data->bitRate /
|
|
|
|
1000,
|
|
|
|
(replayGainInfo !=
|
|
|
|
NULL) ?
|
|
|
|
*replayGainInfo :
|
|
|
|
NULL);
|
|
|
|
if (ret == OUTPUT_BUFFER_DC_STOP) {
|
2004-06-20 03:37:18 +02:00
|
|
|
data->flush = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
return DECODE_BREAK;
|
|
|
|
}
|
2004-05-07 21:11:43 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
data->outputPtr = data->outputBuffer;
|
2004-06-20 03:37:18 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ret == OUTPUT_BUFFER_DC_SEEK)
|
|
|
|
break;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (dc->seek && data->inStream->seekable) {
|
2004-02-24 00:41:20 +01:00
|
|
|
long i = 0;
|
2004-05-20 06:00:17 +02:00
|
|
|
data->muteFrame = MUTEFRAME_SEEK;
|
2006-07-20 18:02:40 +02:00
|
|
|
while (i < data->highestFrame && dc->seekWhere >
|
|
|
|
((float)mad_timer_count(data->times[i],
|
|
|
|
MAD_UNITS_MILLISECONDS))
|
|
|
|
/ 1000) {
|
2004-02-24 00:41:20 +01:00
|
|
|
i++;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (i < data->highestFrame) {
|
|
|
|
if (seekMp3InputBuffer(data,
|
|
|
|
data->frameOffset[i]) ==
|
|
|
|
0) {
|
|
|
|
data->outputPtr = data->outputBuffer;
|
|
|
|
clearOutputBuffer(cb);
|
|
|
|
data->currentFrame = i;
|
|
|
|
} else
|
|
|
|
dc->seekError = 1;
|
2004-05-20 06:04:10 +02:00
|
|
|
data->muteFrame = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
dc->seek = 0;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (dc->seek && !data->inStream->seekable) {
|
|
|
|
dc->seek = 0;
|
|
|
|
dc->seekError = 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (1) {
|
2004-03-05 14:06:31 +01:00
|
|
|
skip = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
while ((ret =
|
|
|
|
decodeNextFrameHeader(data, NULL,
|
|
|
|
replayGainInfo)) == DECODE_CONT
|
|
|
|
&& !dc->stop) ;
|
|
|
|
if (ret == DECODE_BREAK || dc->stop || dc->seek)
|
|
|
|
break;
|
|
|
|
else if (ret == DECODE_SKIP)
|
|
|
|
skip = 1;
|
|
|
|
if (!data->muteFrame) {
|
|
|
|
while ((ret = decodeNextFrame(data)) == DECODE_CONT &&
|
|
|
|
!dc->stop && !dc->seek) ;
|
|
|
|
if (ret == DECODE_BREAK || dc->stop || dc->seek)
|
|
|
|
break;
|
2004-03-05 14:06:31 +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
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (dc->stop)
|
|
|
|
return DECODE_BREAK;
|
2004-05-18 22:27:12 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void initAudioFormatFromMp3DecodeData(mp3DecodeData * data,
|
|
|
|
AudioFormat * af)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
af->bits = 16;
|
|
|
|
af->sampleRate = (data->frame).header.samplerate;
|
|
|
|
af->channels = MAD_NCHANNELS(&(data->frame).header);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int mp3_decode(OutputBuffer * cb, DecoderControl * dc,
|
|
|
|
InputStream * inStream)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
mp3DecodeData data;
|
2006-07-20 18:02:40 +02:00
|
|
|
MpdTag *tag = NULL;
|
|
|
|
ReplayGainInfo *replayGainInfo = NULL;
|
2004-02-29 09:10:52 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (openMp3FromInputStream(inStream, &data, dc, &tag, &replayGainInfo) <
|
|
|
|
0) {
|
2004-06-03 04:56:26 +02:00
|
|
|
closeInputStream(inStream);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!dc->stop) {
|
|
|
|
ERROR
|
|
|
|
("Input does not appear to be a mp3 bit stream.\n");
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
dc->state = DECODE_STATE_STOP;
|
|
|
|
dc->stop = 0;
|
|
|
|
}
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-05-10 14:35:18 +02:00
|
|
|
initAudioFormatFromMp3DecodeData(&data, &(dc->audioFormat));
|
2006-07-20 18:02:40 +02:00
|
|
|
getOutputAudioFormat(&(dc->audioFormat), &(cb->audioFormat));
|
|
|
|
|
2004-05-10 14:35:18 +02:00
|
|
|
dc->totalTime = data.totalTime;
|
2004-06-01 14:50:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (inStream->metaTitle) {
|
|
|
|
if (tag)
|
|
|
|
freeMpdTag(tag);
|
2004-06-07 07:00:56 +02:00
|
|
|
tag = newMpdTag();
|
2004-11-10 22:58:27 +01:00
|
|
|
addItemToMpdTag(tag, TAG_ITEM_TITLE, inStream->metaTitle);
|
2004-06-07 07:00:56 +02:00
|
|
|
free(inStream->metaTitle);
|
|
|
|
inStream->metaTitle = NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (inStream->metaName) {
|
|
|
|
addItemToMpdTag(tag, TAG_ITEM_NAME, inStream->metaName);
|
2004-06-07 07:00:56 +02:00
|
|
|
}
|
|
|
|
copyMpdTagToOutputBuffer(cb, tag);
|
|
|
|
freeMpdTag(tag);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (tag) {
|
|
|
|
if (inStream->metaName) {
|
2004-11-10 22:58:27 +01:00
|
|
|
clearItemsFromMpdTag(tag, TAG_ITEM_NAME);
|
2006-07-20 18:02:40 +02:00
|
|
|
addItemToMpdTag(tag, TAG_ITEM_NAME, inStream->metaName);
|
2004-06-07 07:00:56 +02:00
|
|
|
}
|
|
|
|
copyMpdTagToOutputBuffer(cb, tag);
|
|
|
|
freeMpdTag(tag);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (inStream->metaName) {
|
2004-06-07 07:00:56 +02:00
|
|
|
tag = newMpdTag();
|
2006-07-20 18:02:40 +02:00
|
|
|
if (inStream->metaName) {
|
|
|
|
addItemToMpdTag(tag, TAG_ITEM_NAME, inStream->metaName);
|
2004-06-06 18:42:14 +02:00
|
|
|
}
|
|
|
|
copyMpdTagToOutputBuffer(cb, tag);
|
2004-06-01 14:50:15 +02:00
|
|
|
freeMpdTag(tag);
|
2004-06-20 03:37:18 +02:00
|
|
|
}
|
2004-06-01 14:50:15 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
dc->state = DECODE_STATE_DECODE;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (mp3Read(&data, cb, dc, &replayGainInfo) != DECODE_BREAK) ;
|
2004-02-24 00:41:20 +01:00
|
|
|
/* send last little bit if not dc->stop */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!dc->stop && data.outputPtr != data.outputBuffer && data.flush) {
|
|
|
|
sendDataToOutputBuffer(cb, NULL, dc,
|
|
|
|
data.inStream->seekable,
|
|
|
|
data.outputBuffer,
|
|
|
|
data.outputPtr - data.outputBuffer,
|
|
|
|
data.elapsedTime, data.bitRate / 1000,
|
|
|
|
replayGainInfo);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (replayGainInfo)
|
|
|
|
freeReplayGainInfo(replayGainInfo);
|
2006-01-14 23:47:16 +01:00
|
|
|
|
2004-06-03 04:56:26 +02:00
|
|
|
closeInputStream(inStream);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (dc->seek && data.muteFrame == MUTEFRAME_SEEK) {
|
|
|
|
clearOutputBuffer(cb);
|
|
|
|
dc->seek = 0;
|
|
|
|
}
|
2004-07-03 16:13:43 +02:00
|
|
|
|
|
|
|
flushOutputBuffer(cb);
|
|
|
|
mp3DecodeDataFinalize(&data);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (dc->stop) {
|
2004-02-24 00:41:20 +01:00
|
|
|
dc->state = DECODE_STATE_STOP;
|
|
|
|
dc->stop = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
dc->state = DECODE_STATE_STOP;
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static MpdTag *mp3_tagDup(char *file)
|
|
|
|
{
|
|
|
|
MpdTag *ret = NULL;
|
2004-05-31 03:21:17 +02:00
|
|
|
int time;
|
|
|
|
|
2004-05-31 04:21:06 +02:00
|
|
|
ret = id3Dup(file);
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2004-05-31 04:21:06 +02:00
|
|
|
time = getMp3TotalTime(file);
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (time >= 0) {
|
|
|
|
if (!ret)
|
|
|
|
ret = newMpdTag();
|
2004-05-31 03:21:17 +02:00
|
|
|
ret->time = time;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
DEBUG("mp3_tagDup: Failed to get total song time from: %s\n",
|
|
|
|
file);
|
2005-09-08 23:08:02 +02:00
|
|
|
}
|
2004-05-31 03:21:17 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static char *mp3_suffixes[] = { "mp3", NULL };
|
|
|
|
static char *mp3_mimeTypes[] = { "audio/mpeg", NULL };
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
InputPlugin mp3Plugin = {
|
2004-05-31 03:21:17 +02:00
|
|
|
"mp3",
|
2004-05-31 22:59:55 +02:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2006-03-16 07:52:46 +01:00
|
|
|
NULL,
|
2004-05-31 03:21:17 +02:00
|
|
|
mp3_decode,
|
|
|
|
NULL,
|
|
|
|
mp3_tagDup,
|
|
|
|
INPUT_PLUGIN_STREAM_FILE | INPUT_PLUGIN_STREAM_URL,
|
|
|
|
mp3_suffixes,
|
|
|
|
mp3_mimeTypes
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
InputPlugin mp3Plugin = {
|
2004-05-31 03:21:17 +02:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2004-05-31 03:54:10 +02:00
|
|
|
NULL,
|
2004-05-31 22:59:55 +02:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2006-03-16 07:52:46 +01:00
|
|
|
NULL,
|
2004-05-31 03:21:17 +02:00
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|