From fd6aa253594e18877ca2380961c0425a7de21b2e Mon Sep 17 00:00:00 2001 From: Warren Dukes Date: Mon, 31 May 2004 01:21:17 +0000 Subject: mp3 and ogg plugin stuff git-svn-id: https://svn.musicpd.org/mpd/trunk@1245 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/Makefile.am | 13 +- src/decode.c | 100 ++----- src/inputPlugin.c | 82 +----- src/inputPlugin.h | 17 +- src/inputPlugins/mp3_plugin.c | 659 ++++++++++++++++++++++++++++++++++++++++++ src/inputPlugins/ogg_plugin.c | 364 +++++++++++++++++++++++ src/inputStream_http.c | 4 +- src/ls.c | 24 +- src/ls.h | 18 +- src/main.c | 3 + src/mp3_decode.c | 620 --------------------------------------- src/mp3_decode.h | 38 --- src/ogg_decode.c | 278 ------------------ src/ogg_decode.h | 34 --- src/player.c | 53 ---- src/player.h | 2 - src/song.c | 74 +---- src/tag.c | 84 ------ src/tag.h | 4 + 19 files changed, 1102 insertions(+), 1369 deletions(-) create mode 100644 src/inputPlugins/mp3_plugin.c create mode 100644 src/inputPlugins/ogg_plugin.c delete mode 100644 src/mp3_decode.c delete mode 100644 src/mp3_decode.h delete mode 100644 src/ogg_decode.c delete mode 100644 src/ogg_decode.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 5f783e6ac..ead005df4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,21 +1,24 @@ bin_PROGRAMS = mpd SUBDIRS = $(ID3_SUBDIR) $(MAD_SUBDIR) $(MP4FF_SUBDIR) + +mpd_inputPlugins = inputPlugins/mp3_plugin.c inputPlugins/ogg_plugin.c + mpd_headers = buffer2array.h interface.h command.h playlist.h ls.h \ - song.h list.h directory.h tables.h utils.h path.h mp3_decode.h \ - tag.h player.h listen.h conf.h ogg_decode.h volume.h flac_decode.h \ + song.h list.h directory.h tables.h utils.h path.h \ + tag.h player.h listen.h conf.h volume.h flac_decode.h \ audio.h playerData.h stats.h myfprintf.h sig_handlers.h decode.h log.h \ audiofile_decode.h charConv.h permission.h mpd_types.h pcm_utils.h \ mp4_decode.h aac_decode.h signal_check.h utf8.h inputStream.h \ outputBuffer.h replayGain.h inputStream_file.h inputStream_http.h \ inputPlugin.h mpd_SOURCES = main.c buffer2array.c interface.c command.c playlist.c ls.c \ - song.c list.c directory.c tables.c utils.c path.c mp3_decode.c \ - tag.c player.c listen.c conf.c ogg_decode.c volume.c flac_decode.c \ + song.c list.c directory.c tables.c utils.c path.c \ + tag.c player.c listen.c conf.c volume.c flac_decode.c \ audio.c playerData.c stats.c myfprintf.c sig_handlers.c decode.c log.c \ audiofile_decode.c charConv.c permission.c pcm_utils.c mp4_decode.c \ aac_decode.c signal_check.c utf8.c inputStream.c outputBuffer.c \ replayGain.c inputStream_file.c inputStream_http.c inputPlugin.c \ - $(mpd_headers) + $(mpd_headers) $(mpd_inputPlugins) mpd_CFLAGS = $(MPD_CFLAGS) mpd_LDADD = $(MPD_LIBS) $(ID3_LIB) $(MAD_LIB) $(MP4FF_LIB) diff --git a/src/decode.c b/src/decode.c index d5d3ab41b..ec4774fa9 100644 --- a/src/decode.c +++ b/src/decode.c @@ -28,23 +28,6 @@ #include "sig_handlers.h" #include "ls.h" -#ifdef HAVE_MAD -#include "mp3_decode.h" -#endif -#ifdef HAVE_OGG -#include "ogg_decode.h" -#endif -#ifdef HAVE_FLAC -#include "flac_decode.h" -#endif -#ifdef HAVE_AUDIOFILE -#include "audiofile_decode.h" -#endif -#ifdef HAVE_FAAD -#include "mp4_decode.h" -#include "aac_decode.h" -#endif - #include #include #include @@ -255,8 +238,7 @@ int decodeSeek(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb, void decodeStart(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) { int ret; InputStream inStream; - int suffix = pc->fileSuffix; - int decodeType = pc->decodeType; + InputPlugin * plugin; strncpy(dc->file,pc->file,MAXPATHLEN); dc->file[MAXPATHLEN] = '\0'; @@ -282,67 +264,33 @@ void decodeStart(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) { return; } - switch(decodeType) { - case DECODE_TYPE_URL: -#ifdef HAVE_OGG - if(suffix == DECODE_SUFFIX_OGG || (inStream.mime && - 0 == strcmp(inStream.mime, "application/ogg"))) - { - ret = ogg_decode(cb, dc, &inStream); - break; + ret = DECODE_ERROR_UNKTYPE; + if(isRemoteUrl(pc->file)) { + plugin = getInputPluginFromMimeType(inStream.mime); + if(plugin == NULL) { + plugin = getInputPluginFromSuffix(getSuffix(dc->file)); } -#endif -#ifdef HAVE_MAD - /*if(fileSuffix == DECODE_SUFFIX_MP3 || (inStream.mime && - 0 == strcmp(inStream.mime, "audio/mpeg")))*/ + if(plugin && (plugin->streamTypes & INPUT_PLUGIN_STREAM_URL) && + plugin->streamDecodeFunc) { - ret = mp3_decode(cb, dc, &inStream, 0); - break; - } - ret = DECODE_ERROR_UNKTYPE; -#endif - case DECODE_TYPE_FILE: -#ifdef HAVE_MAD - if(suffix == DECODE_SUFFIX_MP3) { - ret = mp3_decode(cb, dc, &inStream, 1); - break; - } -#endif -#ifdef HAVE_OGG - if(suffix == DECODE_SUFFIX_OGG) { - ret = ogg_decode(cb, dc, &inStream); - break; - } -#endif -#ifdef HAVE_FAAD - if(suffix == DECODE_SUFFIX_AAC) { - closeInputStream(&inStream); - ret = aac_decode(cb,dc); - break; - } - if(suffix == DECODE_SUFFIX_MP4) { - closeInputStream(&inStream); - ret = mp4_decode(cb,dc); - break; - } -#endif -#ifdef HAVE_FLAC - if(suffix == DECODE_SUFFIX_FLAC) { - closeInputStream(&inStream); - ret = flac_decode(cb,dc); - break; - } -#endif -#ifdef HAVE_AUDIOFILE - if(suffix == DECODE_SUFFIX_WAVE) { - closeInputStream(&inStream); - ret = audiofile_decode(cb,dc); - break; + ret = plugin->streamDecodeFunc(cb, dc, &inStream); } -#endif - default: - ret = DECODE_ERROR_UNKTYPE; } + else { + plugin = getInputPluginFromSuffix(getSuffix(dc->file)); + if(plugin && (plugin->streamTypes && INPUT_PLUGIN_STREAM_FILE)) + { + if(plugin->streamDecodeFunc) { + ret = plugin->streamDecodeFunc(cb, dc, + &inStream); + } + else if(plugin->fileDecodeFunc) { + closeInputStream(&inStream); + ret = plugin->fileDecodeFunc(cb, dc); + } + } + } + if(ret<0 || ret == DECODE_ERROR_UNKTYPE) { strncpy(pc->erroredFile, dc->file, MAXPATHLEN); pc->erroredFile[MAXPATHLEN] = '\0'; diff --git a/src/inputPlugin.c b/src/inputPlugin.c index 2afe17719..d90a0e944 100644 --- a/src/inputPlugin.c +++ b/src/inputPlugin.c @@ -7,75 +7,10 @@ static List * inputPlugin_list = NULL; -InputPlugin * newInputPlugin(char * name, InputPlugin_streamDecodeFunc - streamDecodeFunc, InputPlugin_fileDecodeFunc fileDecodeFunc, - InputPlugin_tagDupFunc tagDupFunc, unsigned char streamTypes) -{ - InputPlugin * ret = malloc(sizeof(InputPlugin)); - - memset(ret->name,0,INPUT_PLUGIN_NAME_LENGTH); - strncpy(ret->name, name, INPUT_PLUGIN_NAME_LENGTH-1); - - ret->suffixes = NULL; - ret->mimeTypes = NULL; - - ret->streamTypes = streamTypes; - - ret->streamDecodeFunc = streamDecodeFunc; - ret->fileDecodeFunc = fileDecodeFunc; - ret->tagDupFunc = tagDupFunc; - - return ret; -} - -static void freeStringArray(char ** ptr) { - if(ptr) { - char ** tmp = ptr; - - while(*tmp) { - if(*tmp) free(*tmp); - tmp++; - } - - free (ptr); - } -} - -void freeInputPlugin(InputPlugin * inPlugin) { - freeStringArray(inPlugin->suffixes); - freeStringArray(inPlugin->mimeTypes); - - free(inPlugin); -} - -static char ** AddStringToArray(char ** array, char * string) { - int arraySize = 1; - - if(array) { - char ** tmp = array; - while(*tmp) { - arraySize++; - tmp++; - } - } - - array = realloc(array, (arraySize+1)*sizeof(char *)); - - array[arraySize-1] = strdup(string); - array[arraySize] = NULL; - - return array; -} - -void addSuffixToInputPlugin(InputPlugin * inPlugin, char * suffix) { - inPlugin->suffixes = AddStringToArray(inPlugin->suffixes, suffix); -} - -void addMimeTypeToInputPlugin(InputPlugin * inPlugin, char * mimeType) { - inPlugin->mimeTypes = AddStringToArray(inPlugin->mimeTypes, mimeType); -} - void loadInputPlugin(InputPlugin * inputPlugin) { + if(!inputPlugin) return; + if(!inputPlugin->name) return; + insertInList(inputPlugin_list, inputPlugin->name, (void *)inputPlugin); } @@ -96,11 +31,14 @@ InputPlugin * getInputPluginFromSuffix(char * suffix) { ListNode * node = inputPlugin_list->firstNode; InputPlugin * plugin = NULL; + if(suffix == NULL) return NULL; + while(node != NULL) { plugin = node->data; if(stringFoundInStringArray(plugin->suffixes, suffix)) { return plugin; } + node = node->nextNode; } return NULL; @@ -115,6 +53,7 @@ InputPlugin * getInputPluginFromMimeType(char * mimeType) { if(stringFoundInStringArray(plugin->mimeTypes, mimeType)) { return plugin; } + node = node->nextNode; } return NULL; @@ -128,10 +67,15 @@ InputPlugin * getInputPluginFromName(char * name) { return (InputPlugin *)plugin; } +extern InputPlugin mp3Plugin; +extern InputPlugin oggPlugin; + void initInputPlugins() { - inputPlugin_list = makeList((ListFreeDataFunc *)freeInputPlugin); + inputPlugin_list = makeList(NULL); /* load plugins here */ + loadInputPlugin(&mp3Plugin); + loadInputPlugin(&oggPlugin); } void finishInputPlugins() { diff --git a/src/inputPlugin.h b/src/inputPlugin.h index 9f57037fb..9e1ff395d 100644 --- a/src/inputPlugin.h +++ b/src/inputPlugin.h @@ -1,6 +1,7 @@ #ifndef INPUT_PLUGIN_H #define INPUT_PLUGIN_H +#include "../config.h" #include "inputStream.h" #include "decode.h" #include "outputBuffer.h" @@ -9,8 +10,6 @@ #define INPUT_PLUGIN_STREAM_FILE 0x01 #define INPUT_PLUGIN_STREAM_URL 0x02 -#define INPUT_PLUGIN_NAME_LENGTH 64 - typedef int (* InputPlugin_streamDecodeFunc) (OutputBuffer *, DecoderControl *, InputStream *); @@ -19,7 +18,7 @@ typedef int (* InputPlugin_fileDecodeFunc) (OutputBuffer *, DecoderControl *); typedef MpdTag * (* InputPlugin_tagDupFunc) (char * utf8file); typedef struct _InputPlugin { - char name[INPUT_PLUGIN_NAME_LENGTH]; + char * name; InputPlugin_streamDecodeFunc streamDecodeFunc; InputPlugin_fileDecodeFunc fileDecodeFunc; InputPlugin_tagDupFunc tagDupFunc; @@ -28,25 +27,15 @@ typedef struct _InputPlugin { char ** mimeTypes; } InputPlugin; -/* interface for constructing a plugin */ - -InputPlugin * newInputPlugin(char * name, InputPlugin_streamDecodeFunc - streamDecodeFunc, InputPlugin_fileDecodeFunc fileDecodeFunc, - InputPlugin_tagDupFunc tagDupFunc, unsigned char streamTypes); -void addSuffixToInputPlugin(InputPlugin * inPlugin, char * suffix); -void addMimeTypeToInputPlugin(InputPlugin * inPlugin, char * suffix); -void freeInputPlugin(InputPlugin * inputPlugin); - /* individual functions to load/unload plugins */ void loadInputPlugin(InputPlugin * inputPlugin); -/* this free's inputPlugin as well! */ void unloadInputPlugin(InputPlugin * inputPlugin); /* interface for using plugins */ InputPlugin * getInputPluginFromSuffix(char * suffix); -InputPlugin * getInputPluginFromMimeTypes(char * mimeType); +InputPlugin * getInputPluginFromMimeType(char * mimeType); InputPlugin * getInputPluginFromName(char * name); diff --git a/src/inputPlugins/mp3_plugin.c b/src/inputPlugins/mp3_plugin.c new file mode 100644 index 000000000..5d34a1068 --- /dev/null +++ b/src/inputPlugins/mp3_plugin.c @@ -0,0 +1,659 @@ +/* the Music Player Daemon (MPD) + * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu) + * 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 + */ + +#include "../inputPlugin.h" + +#ifdef HAVE_MAD + +#include "../pcm_utils.h" +#ifdef USE_MPD_MAD +#include "../libmad/mad.h" +#else +#include +#endif +#ifdef HAVE_ID3TAG +#ifdef USE_MPD_ID3TAG +#include "../libid3tag/id3tag.h" +#else +#include +#endif +#endif +#include "../log.h" +#include "../utils.h" +#include "../tag.h" +#include "../path.h" + +#include +#include +#include +#include +#include +#include + +#define FRAMES_CUSHION 2000 + +#define READ_BUFFER_SIZE 40960 + +#define DECODE_SKIP -3 +#define DECODE_BREAK -2 +#define DECODE_CONT -1 +#define DECODE_OK 0 + +#define MUTEFRAME_SKIP 1 +#define MUTEFRAME_SEEK 2 + +/* this is stolen from mpg321! */ +struct audio_dither { + mad_fixed_t error[3]; + mad_fixed_t random; +}; + +unsigned long prng(unsigned long state) { + return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL; +} + +signed long audio_linear_dither(unsigned int bits, mad_fixed_t sample, struct audio_dither *dither) { + unsigned int scalebits; + mad_fixed_t output, mask, random; + + enum { + MIN = -MAD_F_ONE, + MAX = MAD_F_ONE - 1 + }; + + 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; + + random = prng(dither->random); + output += (random & mask) - (dither->random & mask); + + dither->random = random; + + if (output > MAX) { + output = MAX; + + if (sample > MAX) + sample = MAX; + } + else if (output < MIN) { + output = MIN; + + if (sample < MIN) + sample = MIN; + } + + output &= ~mask; + + dither->error[0] = sample - output; + + return output >> scalebits; +} +/* end of stolen stuff from mpg321 */ + +/* decoder stuff is based on madlld */ + +#define MP3_DATA_OUTPUT_BUFFER_SIZE 4096 + +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]; + char outputBuffer[MP3_DATA_OUTPUT_BUFFER_SIZE]; + char * outputPtr; + char * outputBufferEnd; + float totalTime; + float elapsedTime; + int muteFrame; + long * frameOffset; + mad_timer_t * times; + long highestFrame; + long maxFrames; + long currentFrame; + int flush; + unsigned long bitRate; + InputStream * inStream; +} mp3DecodeData; + +void initMp3DecodeData(mp3DecodeData * data, InputStream * inStream) { + data->outputPtr = data->outputBuffer; + data->outputBufferEnd = data->outputBuffer+MP3_DATA_OUTPUT_BUFFER_SIZE; + data->muteFrame = 0; + data->highestFrame = 0; + data->maxFrames = 0; + data->frameOffset = NULL; + data->times = NULL; + data->currentFrame = 0; + data->flush = 1; + data->inStream = inStream; + + mad_stream_init(&data->stream); + mad_frame_init(&data->frame); + mad_synth_init(&data->synth); + mad_timer_reset(&data->timer); +} + +int seekMp3InputBuffer(mp3DecodeData * data, long offset) { + if(seekInputStream(data->inStream,offset,SEEK_SET) < 0) { + return -1; + } + + mad_stream_buffer(&data->stream,data->readBuffer,0); + (data->stream).error = 0; + + return 0; +} + +int fillMp3InputBuffer(mp3DecodeData * data) { + size_t readSize; + size_t remaining; + 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 { + readSize = READ_BUFFER_SIZE; + readStart = data->readBuffer, + remaining = 0; + } + + readed = readFromInputStream(data->inStream, readStart, (size_t)1, + readSize); + if(readed <= 0 && inputStreamAtEOF(data->inStream)) return -1; + /* sleep for a fraction of a second! */ + else if(readed <= 0) my_usleep(10000); + + mad_stream_buffer(&data->stream,data->readBuffer,readed+remaining); + (data->stream).error = 0; + + return 0; +} + +int decodeNextFrameHeader(mp3DecodeData * data) { + if((data->stream).buffer==NULL || (data->stream).error==MAD_ERROR_BUFLEN) { + if(fillMp3InputBuffer(data) < 0) { + return DECODE_BREAK; + } + } + if(mad_header_decode(&data->frame.header,&data->stream)) { +#ifdef HAVE_ID3TAG + 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) { + mad_stream_skip(&(data->stream),tagsize); + return DECODE_CONT; + } + } +#endif + if(MAD_RECOVERABLE((data->stream).error)) { + return DECODE_SKIP; + } + else { + if((data->stream).error==MAD_ERROR_BUFLEN) return DECODE_CONT; + else + { + ERROR("unrecoverable frame level error " + "(%s).\n", + mad_stream_errorstr(&data->stream)); + data->flush = 0; + return DECODE_BREAK; + } + } + } + + return DECODE_OK; +} + +int decodeNextFrame(mp3DecodeData * data) { + if((data->stream).buffer==NULL || (data->stream).error==MAD_ERROR_BUFLEN) { + if(fillMp3InputBuffer(data) < 0) { + return DECODE_BREAK; + } + } + if(mad_frame_decode(&data->frame,&data->stream)) { +#ifdef HAVE_ID3TAG + 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); + return DECODE_CONT; + } + } +#endif + if(MAD_RECOVERABLE((data->stream).error)) { + return DECODE_SKIP; + } + else { + if((data->stream).error==MAD_ERROR_BUFLEN) return DECODE_CONT; + else + { + ERROR("unrecoverable frame level error " + "(%s).\n", + mad_stream_errorstr(&data->stream)); + data->flush = 0; + return DECODE_BREAK; + } + } + } + + return DECODE_OK; +} + +/* xing stuff stolen from alsaplayer */ +# define XING_MAGIC (('X' << 24) | ('i' << 16) | ('n' << 8) | 'g') + +struct xing { + 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; /* ?? */ +}; + +enum { + XING_FRAMES = 0x00000001L, + XING_BYTES = 0x00000002L, + XING_TOC = 0x00000004L, + XING_SCALE = 0x00000008L +}; + +int parse_xing(struct xing *xing, struct mad_bitptr ptr, unsigned int bitlen) +{ + if (bitlen < 64 || mad_bit_read(&ptr, 32) != XING_MAGIC) goto fail; + + xing->flags = mad_bit_read(&ptr, 32); + bitlen -= 64; + + if (xing->flags & XING_FRAMES) { + if (bitlen < 32) goto fail; + xing->frames = mad_bit_read(&ptr, 32); + bitlen -= 32; + } + + if (xing->flags & XING_BYTES) { + if (bitlen < 32) goto fail; + xing->bytes = mad_bit_read(&ptr, 32); + bitlen -= 32; + } + + if (xing->flags & XING_TOC) { + int i; + if (bitlen < 800) goto fail; + for (i = 0; i < 100; ++i) xing->toc[i] = mad_bit_read(&ptr, 8); + bitlen -= 800; + } + + if (xing->flags & XING_SCALE) { + if (bitlen < 32) goto fail; + xing->scale = mad_bit_read(&ptr, 32); + bitlen -= 32; + } + + return 1; + +fail: + xing->flags = 0; + return 0; +} + +int decodeFirstFrame(mp3DecodeData * data, DecoderControl * dc) { + struct xing xing; + int ret; + int skip; + + memset(&xing,0,sizeof(struct xing)); + xing.flags = 0; + + while(1) { + skip = 0; + while((ret = decodeNextFrameHeader(data))==DECODE_CONT && + (!dc || !dc->stop)); + if(ret==DECODE_SKIP) skip = 1; + else if(ret==DECODE_BREAK || (dc && dc->stop)) return -1; + while((ret = decodeNextFrame(data))==DECODE_CONT && + (!dc || !dc->stop)); + if(ret==DECODE_BREAK || (dc && dc->stop)) return -1; + if(!skip && ret==DECODE_OK) break; + } + + if(parse_xing(&xing,data->stream.anc_ptr,data->stream.anc_bitlen)) { + if(xing.flags & XING_FRAMES) { + mad_timer_t duration = data->frame.header.duration; + mad_timer_multiply(&duration,xing.frames); + data->muteFrame = MUTEFRAME_SKIP; + data->totalTime = ((float)mad_timer_count(duration, + MAD_UNITS_MILLISECONDS))/1000; + data->maxFrames = xing.frames; + } + } + else { + size_t offset = data->inStream->offset; + mad_timer_t duration = data->frame.header.duration; + float frameTime = ((float)mad_timer_count(duration, + MAD_UNITS_MILLISECONDS))/1000; + if(data->stream.this_frame!=NULL) { + offset-= data->stream.bufend-data->stream.this_frame; + } + else { + offset-= data->stream.bufend-data->stream.buffer; + } + if(data->inStream->size >= offset) { + data->totalTime = ((data->inStream->size-offset)*8.0)/ + (data->frame).header.bitrate; + data->maxFrames = + data->totalTime/frameTime+FRAMES_CUSHION; + } + else { + data->maxFrames = FRAMES_CUSHION; + data->totalTime = 0; + } + } + + data->frameOffset = malloc(sizeof(long)*data->maxFrames); + data->times = malloc(sizeof(mad_timer_t)*data->maxFrames); + + return 0; +} + +void mp3DecodeDataFinalize(mp3DecodeData * data) { + mad_synth_finish(&data->synth); + mad_frame_finish(&data->frame); + mad_stream_finish(&data->stream); + + closeInputStream(data->inStream); + if(data->frameOffset) free(data->frameOffset); + if(data->times) free(data->times); +} + +/* this is primarily used for getting total time for tags */ +int getMp3TotalTime(char * file) { + InputStream inStream; + mp3DecodeData data; + int ret; + + if(openInputStream(&inStream, file) < 0) return -1; + initMp3DecodeData(&data,&inStream); + data.stream.options |= MAD_OPTION_IGNORECRC; + if(decodeFirstFrame(&data, NULL)<0) ret = -1; + else ret = data.totalTime+0.5; + mp3DecodeDataFinalize(&data); + + return ret; +} + +int openMp3FromInputStream(InputStream * inStream, mp3DecodeData * data, + DecoderControl * dc) +{ + initMp3DecodeData(data, inStream); + data->stream.options |= MAD_OPTION_IGNORECRC; + if(decodeFirstFrame(data, dc)<0) { + mp3DecodeDataFinalize(data); + return -1; + } + + return 0; +} + +int mp3Read(mp3DecodeData * data, OutputBuffer * cb, DecoderControl * dc) { + int i; + int ret; + struct audio_dither dither; + int skip; + + if(data->currentFrame>=data->highestFrame) { + mad_timer_add(&data->timer,(data->frame).header.duration); + data->bitRate = (data->frame).header.bitrate; + if(data->currentFrame>=data->maxFrames) { + data->currentFrame = data->maxFrames - 1; + } + else data->highestFrame++; + data->frameOffset[data->currentFrame] = data->inStream->offset; + 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; + } + data->times[data->currentFrame] = data->timer; + } + else data->timer = data->times[data->currentFrame]; + data->currentFrame++; + data->elapsedTime = ((float)mad_timer_count(data->timer,MAD_UNITS_MILLISECONDS))/1000; + + switch(data->muteFrame) { + case MUTEFRAME_SKIP: + data->muteFrame = 0; + break; + case MUTEFRAME_SEEK: + if(dc->seekWhere<=data->elapsedTime) { + data->outputPtr = data->outputBuffer; + clearOutputBuffer(cb); + data->muteFrame = 0; + dc->seek = 0; + } + break; + default: + mad_synth_frame(&data->synth,&data->frame); + + for(i=0;i<(data->synth).pcm.length;i++) { + mpd_sint16 * sample; + + sample = (mpd_sint16 *)data->outputPtr; + *sample = (mpd_sint16) audio_linear_dither(16, + (data->synth).pcm.samples[0][i], + &dither); + data->outputPtr+=2; + + if(MAD_NCHANNELS(&(data->frame).header)==2) { + sample = (mpd_sint16 *)data->outputPtr; + *sample = (mpd_sint16) audio_linear_dither(16, + (data->synth).pcm.samples[1][i], + &dither); + data->outputPtr+=2; + } + + if(data->outputPtr==data->outputBufferEnd) { + long ret; + ret = sendDataToOutputBuffer(cb, + data->inStream, + dc, + data->inStream->seekable, + data->outputBuffer, + MP3_DATA_OUTPUT_BUFFER_SIZE, + data->elapsedTime, + data->bitRate/1000); + if(ret == OUTPUT_BUFFER_DC_STOP) { + return DECODE_BREAK; + } + + data->outputPtr = data->outputBuffer; + + if(ret == OUTPUT_BUFFER_DC_SEEK) break; + } + } + + if(dc->seek && data->inStream->seekable) { + long i = 0; + data->muteFrame = MUTEFRAME_SEEK; + while(ihighestFrame && dc->seekWhere > + ((float)mad_timer_count(data->times[i], + MAD_UNITS_MILLISECONDS))/1000) + { + i++; + } + if(ihighestFrame) { + if(seekMp3InputBuffer(data, + data->frameOffset[i]) == 0) + { + data->outputPtr = data->outputBuffer; + clearOutputBuffer(cb); + data->currentFrame = i; + } + else dc->seekError = 1; + data->muteFrame = 0; + dc->seek = 0; + } + } + else if(dc->seek && !data->inStream->seekable) { + dc->seek = 0; + dc->seekError = 1; + } + } + + while(1) { + skip = 0; + while((ret = decodeNextFrameHeader(data))==DECODE_CONT && + !dc->stop && !dc->seek); + 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; + } + if(!skip && ret==DECODE_OK) break; + } + + if(dc->stop) return DECODE_BREAK; + + return ret; +} + +void initAudioFormatFromMp3DecodeData(mp3DecodeData * data, AudioFormat * af) { + af->bits = 16; + af->sampleRate = (data->frame).header.samplerate; + af->channels = MAD_NCHANNELS(&(data->frame).header); +} + +int mp3_decode(OutputBuffer * cb, DecoderControl * dc, InputStream * inStream) { + mp3DecodeData data; + + if(openMp3FromInputStream(inStream, &data, dc) < 0) { + closeInputStream(inStream); + 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; + } + + initAudioFormatFromMp3DecodeData(&data, &(dc->audioFormat)); + getOutputAudioFormat(&(dc->audioFormat), &(cb->audioFormat)); + + dc->totalTime = data.totalTime; + dc->state = DECODE_STATE_DECODE; + + while(mp3Read(&data,cb,dc)!=DECODE_BREAK); + /* send last little bit if not dc->stop */ + if(data.outputPtr!=data.outputBuffer && data.flush) { + sendDataToOutputBuffer(cb, NULL, dc, + data.inStream->seekable, + data.outputBuffer, + data.outputPtr-data.outputBuffer, + data.elapsedTime,data.bitRate/1000); + } + + flushOutputBuffer(cb); + mp3DecodeDataFinalize(&data); + + /*if(dc->seek) { + dc->seekError = 1; + dc->seek = 0; + }*/ + + if(dc->stop) { + dc->state = DECODE_STATE_STOP; + dc->stop = 0; + } + else dc->state = DECODE_STATE_STOP; + + return 0; +} + +MpdTag * mp3_tagDup(char * utf8file) { + MpdTag * ret = NULL; + int time; + + ret = id3Dup(utf8file); + + time = getMp3TotalTime(rmp2amp(utf8ToFsCharset(utf8file))); + + if(time>=0) { + if(!ret) ret = newMpdTag(); + ret->time = time; + } + + if(ret) validateUtf8Tag(ret); + + return ret; +} + +char * mp3_suffixes[] = {"mp3", NULL}; +char * mp3_mimeTypes[] = {"audio/mpeg", NULL}; + +InputPlugin mp3Plugin = +{ + "mp3", + mp3_decode, + NULL, + mp3_tagDup, + INPUT_PLUGIN_STREAM_FILE | INPUT_PLUGIN_STREAM_URL, + mp3_suffixes, + mp3_mimeTypes +}; +#else + +InputPlugin mp3Plugin = +{ + NULL, + NULL, + NULL, + 0, + NULL, + NULL +}; + +#endif diff --git a/src/inputPlugins/ogg_plugin.c b/src/inputPlugins/ogg_plugin.c new file mode 100644 index 000000000..af617378e --- /dev/null +++ b/src/inputPlugins/ogg_plugin.c @@ -0,0 +1,364 @@ +/* the Music Player Daemon (MPD) + * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu) + * 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 + */ + +#include "../inputPlugin.h" + +#ifdef HAVE_OGG + +#include "../command.h" +#include "../utils.h" +#include "../audio.h" +#include "../log.h" +#include "../pcm_utils.h" +#include "../inputStream.h" +#include "../outputBuffer.h" +#include "../replayGain.h" + +#include +#include +#include +#include +#include +#include + +#ifdef WORDS_BIGENDIAN +#define OGG_DECODE_USE_BIGENDIAN 1 +#else +#define OGG_DECODE_USE_BIGENDIAN 0 +#endif + +typedef struct _OggCallbackData { + InputStream * inStream; + DecoderControl * dc; +} OggCallbackData; + +/* this is just for tag parsing for db import! */ +int getOggTotalTime(char * file) { + OggVorbis_File vf; + FILE * oggfp; + int totalTime; + + if(!(oggfp = fopen(file,"r"))) return -1; + + if(ov_open(oggfp, &vf, NULL, 0) < 0) { + fclose(oggfp); + return -1; + } + + totalTime = ov_time_total(&vf,-1)+0.5; + + ov_clear(&vf); + + return totalTime; +} + +size_t ogg_read_cb(void * ptr, size_t size, size_t nmemb, void * vdata) +{ + size_t ret = 0; + OggCallbackData * data = (OggCallbackData *)vdata; + + while(1) { + ret = readFromInputStream(data->inStream,ptr,size,nmemb); + if(ret == 0 && !inputStreamAtEOF(data->inStream) && + !data->dc->stop) + { + my_usleep(10000); + } + else break; + } + errno = 0; + /*if(ret<0) errno = ((InputStream *)inStream)->error;*/ + + return ret; +} + +int ogg_seek_cb(void * vdata, ogg_int64_t offset, int whence) { + OggCallbackData * data = (OggCallbackData *)vdata; + + return seekInputStream(data->inStream,offset,whence); +} + +int ogg_close_cb(void * vdata) { + OggCallbackData * data = (OggCallbackData *)vdata; + + return closeInputStream(data->inStream); +} + +long ogg_tell_cb(void * vdata) { + OggCallbackData * data = (OggCallbackData *)vdata; + + return (long)(data->inStream->offset); +} + +char * ogg_parseComment(char * comment, char * needle) { + int len = strlen(needle); + + if(strncasecmp(comment,needle,len)) return comment+len; + + return NULL; +} + +float ogg_getReplayGainScale(char ** comments) { + int trackGainFound = 0; + int albumGainFound = 0; + float trackGain = 1.0; + float albumGain = 1.0; + float trackPeak = 0.0; + float albumPeak = 0.0; + char * temp; + int replayGainState = getReplayGainState(); + + if(replayGainState == REPLAYGAIN_OFF) return 1.0; + + while(*comments) { + if((temp = ogg_parseComment(*comments,"replaygain_track_gain"))) + { + trackGain = atof(temp); + trackGainFound = 1; + } + else if((temp = ogg_parseComment(*comments, + "replaygain_album_gain"))) + { + albumGain = atof(temp); + albumGainFound = 1; + } + else if((temp = ogg_parseComment(*comments, + "replaygain_track_peak"))) + { + trackPeak = atof(temp); + } + else if((temp = ogg_parseComment(*comments, + "replaygain_album_peak"))) + { + albumPeak = atof(temp); + } + + comments++; + } + + switch(replayGainState) { + case REPLAYGAIN_ALBUM: + if(albumGainFound) { + return computeReplayGainScale(albumGain,albumPeak); + } + default: + return computeReplayGainScale(trackGain,trackPeak); + } + + return 1.0; +} + +int ogg_decode(OutputBuffer * cb, DecoderControl * dc, InputStream * inStream) +{ + OggVorbis_File vf; + ov_callbacks callbacks; + OggCallbackData data; + + data.inStream = inStream; + data.dc = dc; + + callbacks.read_func = ogg_read_cb; + callbacks.seek_func = ogg_seek_cb; + callbacks.close_func = ogg_close_cb; + callbacks.tell_func = ogg_tell_cb; + + if(ov_open_callbacks(&data, &vf, NULL, 0, callbacks) < 0) { + closeInputStream(inStream); + if(!dc->stop) { + ERROR("Input does not appear to be an Ogg Vorbis stream.\n"); + return -1; + } + else { + dc->state = DECODE_STATE_STOP; + dc->stop = 0; + } + return 0; + } + + { + vorbis_info *vi=ov_info(&vf,-1); + dc->audioFormat.bits = 16; + dc->audioFormat.channels = vi->channels; + dc->audioFormat.sampleRate = vi->rate; + getOutputAudioFormat(&(dc->audioFormat),&(cb->audioFormat)); + } + + dc->totalTime = ov_time_total(&vf,-1); + if(dc->totalTime < 0) dc->totalTime = 0; + dc->state = DECODE_STATE_DECODE; + + { + int current_section; + int eof = 0; + long ret; +#define OGG_CHUNK_SIZE 4096 + char chunk[OGG_CHUNK_SIZE]; + int chunkpos = 0; + long bitRate = 0; + long test; + float replayGainScale = ogg_getReplayGainScale( + ov_comment(&vf,-1)->user_comments); + + while(!eof) { + if(dc->seek) { + if(0 == ov_time_seek_page(&vf,dc->seekWhere)) { + clearOutputBuffer(cb); + chunkpos = 0; + } + else dc->seekError = 1; + dc->seek = 0; + } + ret = ov_read(&vf, chunk+chunkpos, + OGG_CHUNK_SIZE-chunkpos, + OGG_DECODE_USE_BIGENDIAN, + 2, 1, ¤t_section); + + if(ret <= 0 && ret != OV_HOLE) { + eof = 1; + break; + } + if(ret == OV_HOLE) ret = 0; + + chunkpos+=ret; + + if(chunkpos >= OGG_CHUNK_SIZE) { + if((test = ov_bitrate_instant(&vf))>0) { + bitRate = test/1000; + } + doReplayGain(chunk,ret,&(dc->audioFormat), + replayGainScale); + sendDataToOutputBuffer(cb, inStream, dc, + inStream->seekable, chunk, chunkpos, + ov_time_tell(&vf), bitRate); + if(dc->stop) break; + chunkpos = 0; + } + } + + if(!dc->stop && chunkpos > 0) { + sendDataToOutputBuffer(cb, NULL, dc, inStream->seekable, chunk, chunkpos, + ov_time_tell(&vf), bitRate); + } + + ov_clear(&vf); + + flushOutputBuffer(cb); + + /*if(dc->seek) { + dc->seekError = 1; + dc->seek = 0; + }*/ + + if(dc->stop) { + dc->state = DECODE_STATE_STOP; + dc->stop = 0; + } + else dc->state = DECODE_STATE_STOP; + } + + return 0; +} + +MpdTag * oggTagDup(char * utf8file) { + MpdTag * ret = NULL; + FILE * fp; + OggVorbis_File vf; + char ** comments; + char * temp; + char * s1; + char * s2; + + fp = fopen(rmp2amp(utf8ToFsCharset(utf8file)),"r"); + if(!fp) return NULL; + if(ov_open(fp,&vf,NULL,0)<0) { + fclose(fp); + return NULL; + } + + ret = newMpdTag(); + ret->time = (int)(ov_time_total(&vf,-1)+0.5); + + comments = ov_comment(&vf,-1)->user_comments; + + while(*comments) { + temp = strdup(*comments); + ++comments; + if(!(s1 = strtok(temp,"="))) continue; + s2 = strtok(NULL,""); + if(!s1 || !s2); + else if(0==strcasecmp(s1,"artist")) { + if(!ret->artist) { + stripReturnChar(s2); + ret->artist = strdup(s2); + } + } + else if(0==strcasecmp(s1,"title")) { + if(!ret->title) { + stripReturnChar(s2); + ret->title = strdup(s2); + } + } + else if(0==strcasecmp(s1,"album")) { + if(!ret->album) { + stripReturnChar(s2); + ret->album = strdup(s2); + } + } + else if(0==strcasecmp(s1,"tracknumber")) { + if(!ret->track) { + stripReturnChar(s2); + ret->track = strdup(s2); + } + } + free(temp); + } + + ov_clear(&vf); + + if(ret) validateUtf8Tag(ret); + + return ret; +} + +char * oggSuffixes[] = {"ogg", NULL}; +char * oggMimeTypes[] = {"application/ogg", NULL}; + +InputPlugin oggPlugin = +{ + "ogg", + ogg_decode, + NULL, + oggTagDup, + INPUT_PLUGIN_STREAM_URL | INPUT_PLUGIN_STREAM_FILE, + oggSuffixes, + oggMimeTypes +}; + +#else + +InputPlugin oggPlugin = +{ + NULL, + NULL, + NULL, + 0, + NULL, + NULL +}; + +#endif diff --git a/src/inputStream_http.c b/src/inputStream_http.c index f60c5240d..ab06a3c89 100644 --- a/src/inputStream_http.c +++ b/src/inputStream_http.c @@ -389,10 +389,12 @@ static int getHTTPHello(InputStream * inStream) { DEBUG("stream audiocast-name: %s\n", data->icyName); } else if(0 == strncmp(cur, "\r\nContent-Type:", 15)) { - char * temp = strstr(cur+15,"\r\n"); + int incr = 15; + char * temp = strstr(cur+incr,"\r\n"); if(!temp) break; *temp = '\0'; if(inStream->mime) free(inStream->mime); + while(*(incr+cur) == ' ') incr++; inStream->mime = strdup(cur+15); *temp = '\r'; } diff --git a/src/ls.c b/src/ls.c index 52ca5b782..285874585 100644 --- a/src/ls.c +++ b/src/ls.c @@ -216,27 +216,15 @@ int isDir(char * utf8name) { return 0; } -int isMusic(char * utf8file, time_t * mtime) { +InputPlugin * isMusic(char * utf8file, time_t * mtime) { + InputPlugin * ret = NULL; + if(isFile(utf8file,mtime)) { -#ifdef HAVE_OGG - if(hasOggSuffix(utf8file)) return 1; -#endif -#ifdef HAVE_FLAC - if(hasFlacSuffix(utf8file)) return 1; -#endif -#ifdef HAVE_MAD - if(hasMp3Suffix(utf8file)) return 1; -#endif -#ifdef HAVE_AUDIOFILE - if(hasWaveSuffix(utf8file)) return 1; -#endif -#ifdef HAVE_FAAD - if(hasMp4Suffix(utf8file)) return 1; - if(hasAacSuffix(utf8file)) return 1; -#endif + char * s = getSuffix(utf8file); + if(s) ret = getInputPluginFromSuffix(s); } - return 0; + return ret; } /* vim:set shiftwidth=4 tabstop=8 expandtab: */ diff --git a/src/ls.h b/src/ls.h index cb99df299..ef19676b1 100644 --- a/src/ls.h +++ b/src/ls.h @@ -21,11 +21,15 @@ #include "../config.h" +#include "inputPlugin.h" + #include #include int lsPlaylists(FILE * fp, char * utf8path); +char * getSuffix(char * utf8file); + int isRemoteUrl(char * url); int isFile(char * utf8file, time_t * mtime); @@ -34,19 +38,7 @@ int isDir(char * utf8name); int isPlaylist(char * utf8file); -int isMusic(char * utf8file, time_t * mtime); - -int hasWaveSuffix(char * utf8file); - -int hasMp3Suffix(char * utf8file); - -int hasAacSuffix(char * utf8file); - -int hasMp4Suffix(char * utf8file); - -int hasOggSuffix(char * utf8file); - -int hasFlacSuffix(char * utf8file); +InputPlugin * isMusic(char * utf8file, time_t * mtime); char * dupAndStripPlaylistSuffix(char * file); diff --git a/src/main.c b/src/main.c index c19e80f31..1bbf11531 100644 --- a/src/main.c +++ b/src/main.c @@ -33,6 +33,7 @@ #include "log.h" #include "permission.h" #include "replayGain.h" +#include "inputPlugin.h" #include "../config.h" #include @@ -327,6 +328,7 @@ int main(int argc, char * argv[]) { initTables(); initPlaylist(); + initInputPlugins(); if(!options.dbFile) directory_db = strdup(rpp2app(".mpddb")); else directory_db = strdup(options.dbFile); @@ -448,6 +450,7 @@ int main(int argc, char * argv[]) { finishPaths(); finishPermissions(); finishCommands(); + finishInputPlugins(); return EXIT_SUCCESS; } diff --git a/src/mp3_decode.c b/src/mp3_decode.c deleted file mode 100644 index 467a656df..000000000 --- a/src/mp3_decode.c +++ /dev/null @@ -1,620 +0,0 @@ -/* the Music Player Daemon (MPD) - * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu) - * 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 - */ - -#include "mp3_decode.h" - -#ifdef HAVE_MAD - -#include "pcm_utils.h" -#ifdef USE_MPD_MAD -#include "libmad/mad.h" -#else -#include -#endif -#ifdef HAVE_ID3TAG -#ifdef USE_MPD_ID3TAG -#include "libid3tag/id3tag.h" -#else -#include -#endif -#endif -#include "playerData.h" -#include "log.h" -#include "utils.h" -#include "inputStream.h" -#include "outputBuffer.h" - -#include -#include -#include -#include -#include -#include - -#define FRAMES_CUSHION 2000 - -#define READ_BUFFER_SIZE 40960 - -#define DECODE_SKIP -3 -#define DECODE_BREAK -2 -#define DECODE_CONT -1 -#define DECODE_OK 0 - -#define MUTEFRAME_SKIP 1 -#define MUTEFRAME_SEEK 2 - -/* this is stolen from mpg321! */ -struct audio_dither { - mad_fixed_t error[3]; - mad_fixed_t random; -}; - -unsigned long prng(unsigned long state) { - return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL; -} - -signed long audio_linear_dither(unsigned int bits, mad_fixed_t sample, struct audio_dither *dither) { - unsigned int scalebits; - mad_fixed_t output, mask, random; - - enum { - MIN = -MAD_F_ONE, - MAX = MAD_F_ONE - 1 - }; - - 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; - - random = prng(dither->random); - output += (random & mask) - (dither->random & mask); - - dither->random = random; - - if (output > MAX) { - output = MAX; - - if (sample > MAX) - sample = MAX; - } - else if (output < MIN) { - output = MIN; - - if (sample < MIN) - sample = MIN; - } - - output &= ~mask; - - dither->error[0] = sample - output; - - return output >> scalebits; -} -/* end of stolen stuff from mpg321 */ - -/* decoder stuff is based on madlld */ - -#define MP3_DATA_OUTPUT_BUFFER_SIZE 4096 - -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]; - char outputBuffer[MP3_DATA_OUTPUT_BUFFER_SIZE]; - char * outputPtr; - char * outputBufferEnd; - float totalTime; - float elapsedTime; - int muteFrame; - long * frameOffset; - mad_timer_t * times; - long highestFrame; - long maxFrames; - long currentFrame; - int flush; - unsigned long bitRate; - InputStream * inStream; -} mp3DecodeData; - -void initMp3DecodeData(mp3DecodeData * data, InputStream * inStream) { - data->outputPtr = data->outputBuffer; - data->outputBufferEnd = data->outputBuffer+MP3_DATA_OUTPUT_BUFFER_SIZE; - data->muteFrame = 0; - data->highestFrame = 0; - data->maxFrames = 0; - data->frameOffset = NULL; - data->times = NULL; - data->currentFrame = 0; - data->flush = 1; - data->inStream = inStream; - - mad_stream_init(&data->stream); - mad_frame_init(&data->frame); - mad_synth_init(&data->synth); - mad_timer_reset(&data->timer); -} - -int seekMp3InputBuffer(mp3DecodeData * data, long offset) { - if(seekInputStream(data->inStream,offset,SEEK_SET) < 0) { - return -1; - } - - mad_stream_buffer(&data->stream,data->readBuffer,0); - (data->stream).error = 0; - - return 0; -} - -int fillMp3InputBuffer(mp3DecodeData * data) { - size_t readSize; - size_t remaining; - 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 { - readSize = READ_BUFFER_SIZE; - readStart = data->readBuffer, - remaining = 0; - } - - readed = readFromInputStream(data->inStream, readStart, (size_t)1, - readSize); - if(readed <= 0 && inputStreamAtEOF(data->inStream)) return -1; - /* sleep for a fraction of a second! */ - else if(readed <= 0) my_usleep(10000); - - mad_stream_buffer(&data->stream,data->readBuffer,readed+remaining); - (data->stream).error = 0; - - return 0; -} - -int decodeNextFrameHeader(mp3DecodeData * data) { - if((data->stream).buffer==NULL || (data->stream).error==MAD_ERROR_BUFLEN) { - if(fillMp3InputBuffer(data) < 0) { - return DECODE_BREAK; - } - } - if(mad_header_decode(&data->frame.header,&data->stream)) { -#ifdef HAVE_ID3TAG - 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) { - mad_stream_skip(&(data->stream),tagsize); - return DECODE_CONT; - } - } -#endif - if(MAD_RECOVERABLE((data->stream).error)) { - return DECODE_SKIP; - } - else { - if((data->stream).error==MAD_ERROR_BUFLEN) return DECODE_CONT; - else - { - ERROR("unrecoverable frame level error " - "(%s).\n", - mad_stream_errorstr(&data->stream)); - data->flush = 0; - return DECODE_BREAK; - } - } - } - - return DECODE_OK; -} - -int decodeNextFrame(mp3DecodeData * data) { - if((data->stream).buffer==NULL || (data->stream).error==MAD_ERROR_BUFLEN) { - if(fillMp3InputBuffer(data) < 0) { - return DECODE_BREAK; - } - } - if(mad_frame_decode(&data->frame,&data->stream)) { -#ifdef HAVE_ID3TAG - 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); - return DECODE_CONT; - } - } -#endif - if(MAD_RECOVERABLE((data->stream).error)) { - return DECODE_SKIP; - } - else { - if((data->stream).error==MAD_ERROR_BUFLEN) return DECODE_CONT; - else - { - ERROR("unrecoverable frame level error " - "(%s).\n", - mad_stream_errorstr(&data->stream)); - data->flush = 0; - return DECODE_BREAK; - } - } - } - - return DECODE_OK; -} - -/* xing stuff stolen from alsaplayer */ -# define XING_MAGIC (('X' << 24) | ('i' << 16) | ('n' << 8) | 'g') - -struct xing { - 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; /* ?? */ -}; - -enum { - XING_FRAMES = 0x00000001L, - XING_BYTES = 0x00000002L, - XING_TOC = 0x00000004L, - XING_SCALE = 0x00000008L -}; - -int parse_xing(struct xing *xing, struct mad_bitptr ptr, unsigned int bitlen) -{ - if (bitlen < 64 || mad_bit_read(&ptr, 32) != XING_MAGIC) goto fail; - - xing->flags = mad_bit_read(&ptr, 32); - bitlen -= 64; - - if (xing->flags & XING_FRAMES) { - if (bitlen < 32) goto fail; - xing->frames = mad_bit_read(&ptr, 32); - bitlen -= 32; - } - - if (xing->flags & XING_BYTES) { - if (bitlen < 32) goto fail; - xing->bytes = mad_bit_read(&ptr, 32); - bitlen -= 32; - } - - if (xing->flags & XING_TOC) { - int i; - if (bitlen < 800) goto fail; - for (i = 0; i < 100; ++i) xing->toc[i] = mad_bit_read(&ptr, 8); - bitlen -= 800; - } - - if (xing->flags & XING_SCALE) { - if (bitlen < 32) goto fail; - xing->scale = mad_bit_read(&ptr, 32); - bitlen -= 32; - } - - return 1; - -fail: - xing->flags = 0; - return 0; -} - -int decodeFirstFrame(mp3DecodeData * data, DecoderControl * dc) { - struct xing xing; - int ret; - int skip; - - memset(&xing,0,sizeof(struct xing)); - xing.flags = 0; - - while(1) { - skip = 0; - while((ret = decodeNextFrameHeader(data))==DECODE_CONT && - (!dc || !dc->stop)); - if(ret==DECODE_SKIP) skip = 1; - else if(ret==DECODE_BREAK || (dc && dc->stop)) return -1; - while((ret = decodeNextFrame(data))==DECODE_CONT && - (!dc || !dc->stop)); - if(ret==DECODE_BREAK || (dc && dc->stop)) return -1; - if(!skip && ret==DECODE_OK) break; - } - - if(parse_xing(&xing,data->stream.anc_ptr,data->stream.anc_bitlen)) { - if(xing.flags & XING_FRAMES) { - mad_timer_t duration = data->frame.header.duration; - mad_timer_multiply(&duration,xing.frames); - data->muteFrame = MUTEFRAME_SKIP; - data->totalTime = ((float)mad_timer_count(duration, - MAD_UNITS_MILLISECONDS))/1000; - data->maxFrames = xing.frames; - } - } - else { - size_t offset = data->inStream->offset; - mad_timer_t duration = data->frame.header.duration; - float frameTime = ((float)mad_timer_count(duration, - MAD_UNITS_MILLISECONDS))/1000; - if(data->stream.this_frame!=NULL) { - offset-= data->stream.bufend-data->stream.this_frame; - } - else { - offset-= data->stream.bufend-data->stream.buffer; - } - if(data->inStream->size >= offset) { - data->totalTime = ((data->inStream->size-offset)*8.0)/ - (data->frame).header.bitrate; - data->maxFrames = - data->totalTime/frameTime+FRAMES_CUSHION; - } - else { - data->maxFrames = FRAMES_CUSHION; - data->totalTime = 0; - } - } - - data->frameOffset = malloc(sizeof(long)*data->maxFrames); - data->times = malloc(sizeof(mad_timer_t)*data->maxFrames); - - return 0; -} - -void mp3DecodeDataFinalize(mp3DecodeData * data) { - mad_synth_finish(&data->synth); - mad_frame_finish(&data->frame); - mad_stream_finish(&data->stream); - - closeInputStream(data->inStream); - if(data->frameOffset) free(data->frameOffset); - if(data->times) free(data->times); -} - -/* this is primarily used for getting total time for tags */ -int getMp3TotalTime(char * file) { - InputStream inStream; - mp3DecodeData data; - int ret; - - if(openInputStream(&inStream, file) < 0) return -1; - initMp3DecodeData(&data,&inStream); - data.stream.options |= MAD_OPTION_IGNORECRC; - if(decodeFirstFrame(&data, NULL)<0) ret = -1; - else ret = data.totalTime+0.5; - mp3DecodeDataFinalize(&data); - - return ret; -} - -int openMp3FromInputStream(InputStream * inStream, mp3DecodeData * data, - DecoderControl * dc, int ignoreCrc) -{ - initMp3DecodeData(data, inStream); - if(ignoreCrc) data->stream.options |= MAD_OPTION_IGNORECRC; - if(decodeFirstFrame(data, dc)<0) { - mp3DecodeDataFinalize(data); - return -1; - } - - return 0; -} - -int mp3Read(mp3DecodeData * data, OutputBuffer * cb, DecoderControl * dc) { - int i; - int ret; - struct audio_dither dither; - int skip; - - if(data->currentFrame>=data->highestFrame) { - mad_timer_add(&data->timer,(data->frame).header.duration); - data->bitRate = (data->frame).header.bitrate; - if(data->currentFrame>=data->maxFrames) { - data->currentFrame = data->maxFrames - 1; - } - else data->highestFrame++; - data->frameOffset[data->currentFrame] = data->inStream->offset; - 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; - } - data->times[data->currentFrame] = data->timer; - } - else data->timer = data->times[data->currentFrame]; - data->currentFrame++; - data->elapsedTime = ((float)mad_timer_count(data->timer,MAD_UNITS_MILLISECONDS))/1000; - - switch(data->muteFrame) { - case MUTEFRAME_SKIP: - data->muteFrame = 0; - break; - case MUTEFRAME_SEEK: - if(dc->seekWhere<=data->elapsedTime) { - data->outputPtr = data->outputBuffer; - clearOutputBuffer(cb); - data->muteFrame = 0; - dc->seek = 0; - } - break; - default: - mad_synth_frame(&data->synth,&data->frame); - - for(i=0;i<(data->synth).pcm.length;i++) { - mpd_sint16 * sample; - - sample = (mpd_sint16 *)data->outputPtr; - *sample = (mpd_sint16) audio_linear_dither(16, - (data->synth).pcm.samples[0][i], - &dither); - data->outputPtr+=2; - - if(MAD_NCHANNELS(&(data->frame).header)==2) { - sample = (mpd_sint16 *)data->outputPtr; - *sample = (mpd_sint16) audio_linear_dither(16, - (data->synth).pcm.samples[1][i], - &dither); - data->outputPtr+=2; - } - - if(data->outputPtr==data->outputBufferEnd) { - long ret; - ret = sendDataToOutputBuffer(cb, - data->inStream, - dc, - data->inStream->seekable, - data->outputBuffer, - MP3_DATA_OUTPUT_BUFFER_SIZE, - data->elapsedTime, - data->bitRate/1000); - if(ret == OUTPUT_BUFFER_DC_STOP) { - return DECODE_BREAK; - } - - data->outputPtr = data->outputBuffer; - - if(ret == OUTPUT_BUFFER_DC_SEEK) break; - } - } - - if(dc->seek && data->inStream->seekable) { - long i = 0; - data->muteFrame = MUTEFRAME_SEEK; - while(ihighestFrame && dc->seekWhere > - ((float)mad_timer_count(data->times[i], - MAD_UNITS_MILLISECONDS))/1000) - { - i++; - } - if(ihighestFrame) { - if(seekMp3InputBuffer(data, - data->frameOffset[i]) == 0) - { - data->outputPtr = data->outputBuffer; - clearOutputBuffer(cb); - data->currentFrame = i; - } - else dc->seekError = 1; - data->muteFrame = 0; - dc->seek = 0; - } - } - else if(dc->seek && !data->inStream->seekable) { - dc->seek = 0; - dc->seekError = 1; - } - } - - while(1) { - skip = 0; - while((ret = decodeNextFrameHeader(data))==DECODE_CONT && - !dc->stop && !dc->seek); - 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; - } - if(!skip && ret==DECODE_OK) break; - } - - if(dc->stop) return DECODE_BREAK; - - return ret; -} - -void initAudioFormatFromMp3DecodeData(mp3DecodeData * data, AudioFormat * af) { - af->bits = 16; - af->sampleRate = (data->frame).header.samplerate; - af->channels = MAD_NCHANNELS(&(data->frame).header); -} - -int mp3_decode(OutputBuffer * cb, DecoderControl * dc, InputStream * inStream, - int ignoreCrc) -{ - mp3DecodeData data; - - if(openMp3FromInputStream(inStream, &data, dc, ignoreCrc) < 0) { - closeInputStream(inStream); - 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; - } - - initAudioFormatFromMp3DecodeData(&data, &(dc->audioFormat)); - getOutputAudioFormat(&(dc->audioFormat), &(cb->audioFormat)); - - dc->totalTime = data.totalTime; - dc->state = DECODE_STATE_DECODE; - - while(mp3Read(&data,cb,dc)!=DECODE_BREAK); - /* send last little bit if not dc->stop */ - if(data.outputPtr!=data.outputBuffer && data.flush) { - sendDataToOutputBuffer(cb, NULL, dc, - data.inStream->seekable, - data.outputBuffer, - data.outputPtr-data.outputBuffer, - data.elapsedTime,data.bitRate/1000); - } - - flushOutputBuffer(cb); - mp3DecodeDataFinalize(&data); - - /*if(dc->seek) { - dc->seekError = 1; - dc->seek = 0; - }*/ - - if(dc->stop) { - dc->state = DECODE_STATE_STOP; - dc->stop = 0; - } - else dc->state = DECODE_STATE_STOP; - - return 0; -} - -#endif -/* vim:set shiftwidth=8 tabstop=8 expandtab: */ diff --git a/src/mp3_decode.h b/src/mp3_decode.h deleted file mode 100644 index 7cc1ecfb4..000000000 --- a/src/mp3_decode.h +++ /dev/null @@ -1,38 +0,0 @@ -/* the Music Player Daemon (MPD) - * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu) - * 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 - */ - -#ifndef MP3_DECODE_H -#define MP3_DECODE_H - -#include "../config.h" - -#ifdef HAVE_MAD - -#include "playerData.h" -#include "inputStream.h" - -/* this is primarily used in tag.c */ -int getMp3TotalTime(char * file); - -int mp3_decode(OutputBuffer * cb, DecoderControl * dc, InputStream * inStream, - int ignoreCrc); - -#endif - -#endif -/* vim:set shiftwidth=4 tabstop=8 expandtab: */ diff --git a/src/ogg_decode.c b/src/ogg_decode.c deleted file mode 100644 index 22ae07727..000000000 --- a/src/ogg_decode.c +++ /dev/null @@ -1,278 +0,0 @@ -/* the Music Player Daemon (MPD) - * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu) - * 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 - */ - -#include "ogg_decode.h" - -#ifdef HAVE_OGG - -#include "command.h" -#include "utils.h" -#include "audio.h" -#include "log.h" -#include "pcm_utils.h" -#include "inputStream.h" -#include "outputBuffer.h" -#include "replayGain.h" - -#include -#include -#include -#include -#include -#include - -#ifdef WORDS_BIGENDIAN -#define OGG_DECODE_USE_BIGENDIAN 1 -#else -#define OGG_DECODE_USE_BIGENDIAN 0 -#endif - -typedef struct _OggCallbackData { - InputStream * inStream; - DecoderControl * dc; -} OggCallbackData; - -/* this is just for tag parsing for db import! */ -int getOggTotalTime(char * file) { - OggVorbis_File vf; - FILE * oggfp; - int totalTime; - - if(!(oggfp = fopen(file,"r"))) return -1; - - if(ov_open(oggfp, &vf, NULL, 0) < 0) { - fclose(oggfp); - return -1; - } - - totalTime = ov_time_total(&vf,-1)+0.5; - - ov_clear(&vf); - - return totalTime; -} - -size_t ogg_read_cb(void * ptr, size_t size, size_t nmemb, void * vdata) -{ - size_t ret = 0; - OggCallbackData * data = (OggCallbackData *)vdata; - - while(1) { - ret = readFromInputStream(data->inStream,ptr,size,nmemb); - if(ret == 0 && !inputStreamAtEOF(data->inStream) && - !data->dc->stop) - { - my_usleep(10000); - } - else break; - } - errno = 0; - /*if(ret<0) errno = ((InputStream *)inStream)->error;*/ - - return ret; -} - -int ogg_seek_cb(void * vdata, ogg_int64_t offset, int whence) { - OggCallbackData * data = (OggCallbackData *)vdata; - - return seekInputStream(data->inStream,offset,whence); -} - -int ogg_close_cb(void * vdata) { - OggCallbackData * data = (OggCallbackData *)vdata; - - return closeInputStream(data->inStream); -} - -long ogg_tell_cb(void * vdata) { - OggCallbackData * data = (OggCallbackData *)vdata; - - return (long)(data->inStream->offset); -} - -char * ogg_parseComment(char * comment, char * needle) { - int len = strlen(needle); - - if(strncasecmp(comment,needle,len)) return comment+len; - - return NULL; -} - -float ogg_getReplayGainScale(char ** comments) { - int trackGainFound = 0; - int albumGainFound = 0; - float trackGain = 1.0; - float albumGain = 1.0; - float trackPeak = 0.0; - float albumPeak = 0.0; - char * temp; - int replayGainState = getReplayGainState(); - - if(replayGainState == REPLAYGAIN_OFF) return 1.0; - - while(*comments) { - if((temp = ogg_parseComment(*comments,"replaygain_track_gain"))) - { - trackGain = atof(temp); - trackGainFound = 1; - } - else if((temp = ogg_parseComment(*comments, - "replaygain_album_gain"))) - { - albumGain = atof(temp); - albumGainFound = 1; - } - else if((temp = ogg_parseComment(*comments, - "replaygain_track_peak"))) - { - trackPeak = atof(temp); - } - else if((temp = ogg_parseComment(*comments, - "replaygain_album_peak"))) - { - albumPeak = atof(temp); - } - - comments++; - } - - switch(replayGainState) { - case REPLAYGAIN_ALBUM: - if(albumGainFound) { - return computeReplayGainScale(albumGain,albumPeak); - } - default: - return computeReplayGainScale(trackGain,trackPeak); - } - - return 1.0; -} - -int ogg_decode(OutputBuffer * cb, DecoderControl * dc, InputStream * inStream) -{ - OggVorbis_File vf; - ov_callbacks callbacks; - OggCallbackData data; - - data.inStream = inStream; - data.dc = dc; - - callbacks.read_func = ogg_read_cb; - callbacks.seek_func = ogg_seek_cb; - callbacks.close_func = ogg_close_cb; - callbacks.tell_func = ogg_tell_cb; - - if(ov_open_callbacks(&data, &vf, NULL, 0, callbacks) < 0) { - closeInputStream(inStream); - if(!dc->stop) { - ERROR("Input does not appear to be an Ogg Vorbis stream.\n"); - return -1; - } - else { - dc->state = DECODE_STATE_STOP; - dc->stop = 0; - } - return 0; - } - - { - vorbis_info *vi=ov_info(&vf,-1); - dc->audioFormat.bits = 16; - dc->audioFormat.channels = vi->channels; - dc->audioFormat.sampleRate = vi->rate; - getOutputAudioFormat(&(dc->audioFormat),&(cb->audioFormat)); - } - - dc->totalTime = ov_time_total(&vf,-1); - if(dc->totalTime < 0) dc->totalTime = 0; - dc->state = DECODE_STATE_DECODE; - - { - int current_section; - int eof = 0; - long ret; -#define OGG_CHUNK_SIZE 4096 - char chunk[OGG_CHUNK_SIZE]; - int chunkpos = 0; - long bitRate = 0; - long test; - float replayGainScale = ogg_getReplayGainScale( - ov_comment(&vf,-1)->user_comments); - - while(!eof) { - if(dc->seek) { - if(0 == ov_time_seek_page(&vf,dc->seekWhere)) { - clearOutputBuffer(cb); - chunkpos = 0; - } - else dc->seekError = 1; - dc->seek = 0; - } - ret = ov_read(&vf, chunk+chunkpos, - OGG_CHUNK_SIZE-chunkpos, - OGG_DECODE_USE_BIGENDIAN, - 2, 1, ¤t_section); - - if(ret <= 0 && ret != OV_HOLE) { - eof = 1; - break; - } - if(ret == OV_HOLE) ret = 0; - - chunkpos+=ret; - - if(chunkpos >= OGG_CHUNK_SIZE) { - if((test = ov_bitrate_instant(&vf))>0) { - bitRate = test/1000; - } - doReplayGain(chunk,ret,&(dc->audioFormat), - replayGainScale); - sendDataToOutputBuffer(cb, inStream, dc, - inStream->seekable, chunk, chunkpos, - ov_time_tell(&vf), bitRate); - if(dc->stop) break; - chunkpos = 0; - } - } - - if(!dc->stop && chunkpos > 0) { - sendDataToOutputBuffer(cb, NULL, dc, inStream->seekable, chunk, chunkpos, - ov_time_tell(&vf), bitRate); - } - - ov_clear(&vf); - - flushOutputBuffer(cb); - - /*if(dc->seek) { - dc->seekError = 1; - dc->seek = 0; - }*/ - - if(dc->stop) { - dc->state = DECODE_STATE_STOP; - dc->stop = 0; - } - else dc->state = DECODE_STATE_STOP; - } - - return 0; -} - -#endif -/* vim:set shiftwidth=4 tabstop=8 expandtab: */ diff --git a/src/ogg_decode.h b/src/ogg_decode.h deleted file mode 100644 index 03f1364a5..000000000 --- a/src/ogg_decode.h +++ /dev/null @@ -1,34 +0,0 @@ -/* the Music Player Daemon (MPD) - * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu) - * 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 - */ - -#ifndef OGG_DECODE_H -#define OGG_DECODE_H - -#include "../config.h" - -#include "playerData.h" -#include "inputStream.h" - -#include - -int ogg_decode(OutputBuffer * cb, DecoderControl * dc, InputStream * inStream); - -int getOggTotalTime(char * file); - -#endif -/* vim:set shiftwidth=4 tabstop=8 expandtab: */ diff --git a/src/player.c b/src/player.c index 82f3101cd..33184f72c 100644 --- a/src/player.c +++ b/src/player.c @@ -117,7 +117,6 @@ int playerInit() { closeMp3Directory(); finishPlaylist(); closeTables(); - finishPaths(); finishPermissions(); finishCommands(); finishVolume(); @@ -160,35 +159,8 @@ int playerInit() { return 0; } -int playerGetDecodeType(char * utf8file) { - if(isRemoteUrl(utf8file)) return DECODE_TYPE_URL; - if(isFile(utf8file,NULL)) return DECODE_TYPE_FILE; - return -1; -} - -int playerGetSuffix(char * utf8file) { -#ifdef HAVE_MAD - if(hasMp3Suffix(utf8file)) return DECODE_SUFFIX_MP3; -#endif -#ifdef HAVE_OGG - if(hasOggSuffix(utf8file)) return DECODE_SUFFIX_OGG; -#endif -#ifdef HAVE_FAAD - if(hasAacSuffix(utf8file)) return DECODE_SUFFIX_AAC; - if(hasMp4Suffix(utf8file)) return DECODE_SUFFIX_MP4; -#endif -#ifdef HAVE_FLAC - if(hasFlacSuffix(utf8file)) return DECODE_SUFFIX_FLAC; -#endif -#ifdef HAVE_AUDIOFILE - if(hasWaveSuffix(utf8file)) return DECODE_SUFFIX_WAVE; -#endif - return -1; -} - int playerPlay(FILE * fp, Song * song) { PlayerControl * pc = &(getPlayerData()->playerControl); - int decodeType; if(fp==NULL) fp = stderr; @@ -204,15 +176,6 @@ int playerPlay(FILE * fp, Song * song) { } }*/ - decodeType = playerGetDecodeType(song->utf8url); - if(decodeType < 0) { - strncpy(pc->erroredFile,pc->file,MAXPATHLEN); - pc->erroredFile[MAXPATHLEN] = '\0'; - pc->error = PLAYER_ERROR_UNKTYPE; - return 0; - } - pc->decodeType = decodeType; - pc->fileSuffix = playerGetSuffix(song->utf8url); if(song->tag) pc->fileTime = song->tag->time; else pc->fileTime = 0; @@ -362,7 +325,6 @@ void playerCloseAudio() { int queueSong(Song * song) { PlayerControl * pc = &(getPlayerData()->playerControl); - int decodeType; if(pc->queueState==PLAYER_QUEUE_BLANK) { if(isRemoteUrl(song->utf8url)) { @@ -372,10 +334,6 @@ int queueSong(Song * song) { MAXPATHLEN); pc->file[MAXPATHLEN] = '\0'; - decodeType = playerGetDecodeType(song->utf8url); - if(decodeType < 0) return -1; - pc->decodeType = decodeType; - pc->fileSuffix = playerGetSuffix(song->utf8url); if(song->tag) pc->fileTime = song->tag->time; else pc->fileTime = 0; @@ -421,7 +379,6 @@ void playerQueueUnlock() { int playerSeek(FILE * fp, Song * song, float time) { PlayerControl * pc = &(getPlayerData()->playerControl); char * file; - int decodeType; if(pc->state==PLAYER_STATE_STOP) { myfprintf(fp,"%s player not currently playing\n", @@ -432,16 +389,6 @@ int playerSeek(FILE * fp, Song * song, float time) { if(isRemoteUrl(song->utf8url)) file = song->utf8url; else file = rmp2amp(utf8ToFsCharset(song->utf8url)); if(strcmp(pc->file,file)!=0) { - decodeType = playerGetDecodeType(song->utf8url); - if(decodeType < 0) { - myfprintf(fp,"%s unknown file type: %s\n", - COMMAND_RESPOND_ERROR, song->utf8url); - ERROR("playerSeek: unknown file type: %s\n", - song->utf8url); - return -1; - } - pc->decodeType = decodeType; - pc->fileSuffix = playerGetSuffix(song->utf8url); if(song->tag) pc->fileTime = song->tag->time; else pc->fileTime = 0; diff --git a/src/player.h b/src/player.h index a0b5515c1..75bc71edf 100644 --- a/src/player.h +++ b/src/player.h @@ -52,8 +52,6 @@ #define PLAYER_QUEUE_LOCKED 1 typedef struct _PlayerControl { - volatile mpd_sint8 decodeType; - volatile mpd_sint8 fileSuffix; volatile mpd_sint8 stop; volatile mpd_sint8 play; volatile mpd_sint8 pause; diff --git a/src/song.c b/src/song.c index 5991a2612..9d2a09e54 100644 --- a/src/song.c +++ b/src/song.c @@ -23,13 +23,10 @@ #include "utils.h" #include "tag.h" #include "log.h" -#include "mp3_decode.h" -#include "audiofile_decode.h" -#include "ogg_decode.h" -#include "flac_decode.h" #include "path.h" #include "playlist.h" #include "tables.h" +#include "inputPlugin.h" #define SONG_KEY "key: " #define SONG_FILE "file: " @@ -60,36 +57,10 @@ Song * newSong(char * utf8url, SONG_TYPE type) { song->type = type; if(song->type == SONG_TYPE_FILE) { - if(!isFile(utf8url,&(song->mtime))); -#ifdef HAVE_OGG - else if(hasOggSuffix(utf8url)) { - song->tag = oggTagDup(utf8url); - } -#endif -#ifdef HAVE_FLAC - else if((hasFlacSuffix(utf8url))) { - song->tag = flacTagDup(utf8url); - } -#endif -#ifdef HAVE_MAD - else if(hasMp3Suffix(utf8url)) { - song->tag = mp3TagDup(utf8url); - } -#endif -#ifdef HAVE_AUDIOFILE - else if(hasWaveSuffix(utf8url)) { - song->tag = audiofileTagDup(utf8url); - } -#endif -#ifdef HAVE_FAAD - else if(hasAacSuffix(utf8url)) { - song->tag = aacTagDup(utf8url); - } - else if(hasMp4Suffix(utf8url)) { - song->tag = mp4TagDup(utf8url); - } -#endif - + InputPlugin * plugin; + if((plugin = isMusic(utf8url,&(song->mtime)))) { + song->tag = plugin->tagDupFunc(utf8url); + } if(!song->tag || song->tag->time<0) { freeSong(song); song = NULL; @@ -288,41 +259,16 @@ int updateSongInfo(Song * song) { char * utf8url = song->utf8url; if(song->type == SONG_TYPE_FILE) { + InputPlugin * plugin; + removeASongFromTables(song); if(song->tag) freeMpdTag(song->tag); song->tag = NULL; - if(!isFile(utf8url,&(song->mtime))); -#ifdef HAVE_OGG - else if(hasOggSuffix(utf8url)) { - song->tag = oggTagDup(utf8url); - } -#endif -#ifdef HAVE_FLAC - else if((hasFlacSuffix(utf8url))) { - song->tag = flacTagDup(utf8url); - } -#endif -#ifdef HAVE_MAD - else if(hasMp3Suffix(utf8url)) { - song->tag = mp3TagDup(utf8url); - } -#endif -#ifdef HAVE_AUDIOFILE - else if(hasWaveSuffix(utf8url)) { - song->tag = audiofileTagDup(utf8url); - } -#endif -#ifdef HAVE_FAAD - else if(hasAacSuffix(utf8url)) { - song->tag = aacTagDup(utf8url); - } - else if(hasMp4Suffix(utf8url)) { - song->tag = mp4TagDup(utf8url); - } -#endif - + if((plugin = isMusic(utf8url,&(song->mtime)))) { + song->tag = plugin->tagDupFunc(utf8url); + } if(!song->tag || song->tag->time<0) return -1; else addSongToTables(song); } diff --git a/src/tag.c b/src/tag.c index 3a968fe8e..c540fa06e 100644 --- a/src/tag.c +++ b/src/tag.c @@ -19,7 +19,6 @@ #include "tag.h" #include "path.h" #include "myfprintf.h" -#include "mp3_decode.h" #include "audiofile_decode.h" #include "mp4_decode.h" #include "aac_decode.h" @@ -171,26 +170,6 @@ MpdTag * audiofileTagDup(char * utf8file) { } #endif -#ifdef HAVE_MAD -MpdTag * mp3TagDup(char * utf8file) { - MpdTag * ret = NULL; - int time; - - ret = id3Dup(utf8file); - - time = getMp3TotalTime(rmp2amp(utf8ToFsCharset(utf8file))); - - if(time>=0) { - if(!ret) ret = newMpdTag(); - ret->time = time; - } - - if(ret) validateUtf8Tag(ret); - - return ret; -} -#endif - #ifdef HAVE_FAAD MpdTag * aacTagDup(char * utf8file) { MpdTag * ret = NULL; @@ -300,69 +279,6 @@ MpdTag * mp4TagDup(char * utf8file) { } #endif -#ifdef HAVE_OGG -MpdTag * oggTagDup(char * utf8file) { - MpdTag * ret = NULL; - FILE * fp; - OggVorbis_File vf; - char ** comments; - char * temp; - char * s1; - char * s2; - - fp = fopen(rmp2amp(utf8ToFsCharset(utf8file)),"r"); - if(!fp) return NULL; - if(ov_open(fp,&vf,NULL,0)<0) { - fclose(fp); - return NULL; - } - - ret = newMpdTag(); - ret->time = (int)(ov_time_total(&vf,-1)+0.5); - - comments = ov_comment(&vf,-1)->user_comments; - - while(*comments) { - temp = strdup(*comments); - ++comments; - if(!(s1 = strtok(temp,"="))) continue; - s2 = strtok(NULL,""); - if(!s1 || !s2); - else if(0==strcasecmp(s1,"artist")) { - if(!ret->artist) { - stripReturnChar(s2); - ret->artist = strdup(s2); - } - } - else if(0==strcasecmp(s1,"title")) { - if(!ret->title) { - stripReturnChar(s2); - ret->title = strdup(s2); - } - } - else if(0==strcasecmp(s1,"album")) { - if(!ret->album) { - stripReturnChar(s2); - ret->album = strdup(s2); - } - } - else if(0==strcasecmp(s1,"tracknumber")) { - if(!ret->track) { - stripReturnChar(s2); - ret->track = strdup(s2); - } - } - free(temp); - } - - ov_clear(&vf); - - if(ret) validateUtf8Tag(ret); - - return ret; -} -#endif - #ifdef HAVE_FLAC MpdTag * flacMetadataDup(char * utf8file, int * vorbisCommentFound) { MpdTag * ret = NULL; diff --git a/src/tag.h b/src/tag.h index a406a3cae..89e60adfe 100644 --- a/src/tag.h +++ b/src/tag.h @@ -31,6 +31,8 @@ typedef struct _MpdTag { int time; } MpdTag; +MpdTag * id3Dup(char * utf8filename); + MpdTag * newMpdTag(); void freeMpdTag(MpdTag * tag); @@ -61,5 +63,7 @@ void printMpdTag(FILE * fp, MpdTag * tag); MpdTag * mpdTagDup(MpdTag * tag); +void validateUtf8Tag(MpdTag * tag); + #endif /* vim:set shiftwidth=4 tabstop=8 expandtab: */ -- cgit v1.2.3