diff --git a/configure.ac b/configure.ac index b1c71f964..37085d475 100644 --- a/configure.ac +++ b/configure.ac @@ -280,6 +280,12 @@ AC_ARG_ENABLE(mod, enable_mod=$enableval, enable_mod=no) +AC_ARG_ENABLE(modplug, + AS_HELP_STRING([--enable-modplug], + [enable MODPLUG support (default: disable)]), + enable_modplug=$enableval, + enable_modplug=no) + AC_ARG_ENABLE(mpc, AS_HELP_STRING([--disable-mpc], [disable musepack (MPC) support (default: enable)]), @@ -837,6 +843,14 @@ fi AM_CONDITIONAL(HAVE_MIKMOD, test x$enable_mod = xyes) +if test x$enable_modplug = xyes; then + PKG_CHECK_MODULES(MODPLUG, [libmodplug], + AC_DEFINE(HAVE_MODPLUG, 1, [Define for modplug support]), + enable_modplug=no) +fi + +AM_CONDITIONAL(HAVE_MODPLUG, test x$enable_modplug = xyes) + if test x$enable_ffmpeg = xyes; then PKG_CHECK_MODULES(FFMPEG, [libavformat libavcodec libavutil], AC_DEFINE(HAVE_FFMPEG, 1, [Define for FFMPEG support]), @@ -1160,6 +1174,12 @@ else echo " MOD support ...................disabled" fi +if test x$enable_modplug = xyes; then + echo " MODPLUG support ...............enabled" +else + echo " MODPLUG support ...............disabled" +fi + if test x$enable_ffmpeg = xyes; then echo " FFMPEG support ................enabled" else @@ -1176,6 +1196,7 @@ if test x$enable_mpc = xno && test x$enable_wavpack = xno && test x$enable_ffmpeg = xno && + test x$enable_modplug = xno && test x$enable_mod = xno; then AC_MSG_ERROR([No input plugins supported!]) fi diff --git a/src/Makefile.am b/src/Makefile.am index 4c1a58cc4..58f1bf02d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -244,6 +244,10 @@ if HAVE_MIKMOD mpd_SOURCES += decoder/mod_plugin.c endif +if HAVE_MODPLUG +mpd_SOURCES += decoder/modplug_plugin.c +endif + if HAVE_FFMPEG mpd_SOURCES += decoder/ffmpeg_plugin.c endif @@ -312,6 +316,7 @@ mpd_CPPFLAGS = \ $(OGGVORBIS_CFLAGS) $(VORBISENC_CFLAGS) \ $(patsubst -I%/FLAC,-I%,$(FLAC_CFLAGS)) \ $(AUDIOFILE_CFLAGS) $(LIBMIKMOD_CFLAGS) \ + $(MODPLUG_CFLAGS) \ $(ID3TAG_CFLAGS) \ $(MAD_CFLAGS) \ $(FFMPEG_CFLAGS) \ @@ -322,6 +327,7 @@ mpd_LDADD = $(MPD_LIBS) \ $(SHOUT_LIBS) \ $(OGGVORBIS_LIBS) $(VORBISENC_LIBS) $(FLAC_LIBS) \ $(AUDIOFILE_LIBS) $(LIBMIKMOD_LIBS) \ + $(MODPLUG_LIBS) \ $(ID3TAG_LIBS) \ $(MAD_LIBS) \ $(MP4FF_LIBS) \ diff --git a/src/decoder/modplug_plugin.c b/src/decoder/modplug_plugin.c new file mode 100644 index 000000000..e02282c04 --- /dev/null +++ b/src/decoder/modplug_plugin.c @@ -0,0 +1,200 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka + * 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 "../decoder_api.h" +#include "../utils.h" +#include "../log.h" + +#include +#include + +#define MODPLUG_FRAME_SIZE (4096) +#define MODPLUG_PREALLOC_BLOCK (256*1024) +#define MODPLUG_READ_BLOCK (128*1024) +#define MODPLUG_FILE_LIMIT (1024*1024*100) + +static GByteArray *mod_loadfile(struct decoder *decoder, struct input_stream *is) +{ + unsigned char *data; + GByteArray *bdatas; + int total_len; + int ret; + + //known/unknown size, preallocate array, lets read in chunks + if (is->size) { + if (is->size > MODPLUG_FILE_LIMIT) { + g_warning("file too large\n"); + return NULL; + } + bdatas = g_byte_array_sized_new(is->size); + } else { + bdatas = g_byte_array_sized_new(MODPLUG_PREALLOC_BLOCK); + } + data = g_malloc(MODPLUG_READ_BLOCK); + total_len = 0; + do { + if (decoder) { + ret = decoder_read(decoder, is, data, MODPLUG_READ_BLOCK); + } else { + ret = input_stream_read(is, data, MODPLUG_READ_BLOCK); + } + if (ret > 0) { + g_byte_array_append(bdatas, data, ret); + total_len += ret; + } else { + //end of file, or read error + break; + } + if (total_len > MODPLUG_FILE_LIMIT) { + g_warning("stream too large\n"); + g_free(data); + g_byte_array_free(bdatas, TRUE); + return NULL; + } + } while (input_stream_eof(is)); + g_free(data); + + return bdatas; +} + +static void +mod_decode(struct decoder *decoder, struct input_stream *is) +{ + ModPlugFile *f; + ModPlug_Settings settings; + GByteArray *bdatas; + struct audio_format audio_format; + float total_time = 0.0; + int ret, current; + char audio_buffer[MODPLUG_FRAME_SIZE]; + float sec_perbyte; + enum decoder_command cmd = DECODE_COMMAND_NONE; + + bdatas = mod_loadfile(decoder, is); + + if (!bdatas) { + g_warning("could not load stream\n"); + return; + } + f = ModPlug_Load(bdatas->data, bdatas->len); + g_byte_array_free(bdatas, TRUE); + if (!f) { + g_warning("could not decode stream\n"); + return; + } + ModPlug_GetSettings(&settings); + /* alter setting */ + settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; /* RESAMP */ + settings.mChannels = 2; + settings.mBits = 16; + settings.mFrequency = 44100; + /* insert more setting changes here */ + ModPlug_SetSettings(&settings); + + audio_format.bits = 16; + audio_format.sample_rate = 44100; + audio_format.channels = 2; + + sec_perbyte = + 1.0 / ((audio_format.bits * audio_format.channels / 8.0) * + (float)audio_format.sample_rate); + + total_time = ModPlug_GetLength(f) / 1000; + + decoder_initialized(decoder, &audio_format, + is->seekable, total_time); + + total_time = 0; + + do { + ret = ModPlug_Read(f, audio_buffer, MODPLUG_FRAME_SIZE); + + if (ret == 0) { + break; + } + + total_time += ret * sec_perbyte; + cmd = decoder_data(decoder, NULL, + audio_buffer, ret, + total_time, 0, NULL); + + if (cmd == DECODE_COMMAND_SEEK) { + total_time = decoder_seek_where(decoder); + current = total_time * 1000; + ModPlug_Seek(f, current); + decoder_command_finished(decoder); + } + + } while (cmd != DECODE_COMMAND_STOP); + + ModPlug_Unload(f); +} + +static struct tag *mod_tagdup(const char *file) +{ + ModPlugFile *f; + struct tag *ret = NULL; + GByteArray *bdatas; + char *title; + struct input_stream is; + + if (!input_stream_open(&is, file)) { + g_warning("cant open file %s\n", file); + return NULL; + } + + bdatas = mod_loadfile(NULL, &is); + if (!bdatas) { + g_warning("cant load file %s\n", file); + return NULL; + } + + f = ModPlug_Load(bdatas->data, bdatas->len); + g_byte_array_free(bdatas, TRUE); + if (!f) { + g_warning("could not decode file %s\n", file); + return NULL; + } + ret = tag_new(); + ret->time = 0; + + title = g_strdup(ModPlug_GetName(f)); + if (title) + tag_add_item(ret, TAG_ITEM_TITLE, title); + g_free(title); + + ModPlug_Unload(f); + + input_stream_close(&is); + + return ret; +} + +static const char *const mod_suffixes[] = { + "669", "amf", "ams", "dbm", "dfm", "dsm", "far", "it", + "med", "mdl", "mod", "mtm", "mt2", "okt", "s3m", "stm", + "ult", "umx", "xm", + NULL +}; + +const struct decoder_plugin modplug_plugin = { + .name = "modplug", + .stream_decode = mod_decode, + .tag_dup = mod_tagdup, + .suffixes = mod_suffixes, +}; diff --git a/src/decoder_list.c b/src/decoder_list.c index c70d6334c..6fe434e03 100644 --- a/src/decoder_list.c +++ b/src/decoder_list.c @@ -31,6 +31,7 @@ extern const struct decoder_plugin mp4_plugin; extern const struct decoder_plugin aacPlugin; extern const struct decoder_plugin mpcPlugin; extern const struct decoder_plugin wavpack_plugin; +extern const struct decoder_plugin modplug_plugin; extern const struct decoder_plugin modPlugin; extern const struct decoder_plugin ffmpeg_plugin; @@ -62,6 +63,9 @@ static const struct decoder_plugin *const decoder_plugins[] = { #ifdef HAVE_WAVPACK &wavpack_plugin, #endif +#ifdef HAVE_MODPLUG + &modplug_plugin, +#endif #ifdef HAVE_MIKMOD &modPlugin, #endif