2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
2008-12-28 17:11:18 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-12-28 17:11:18 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "decoder_api.h"
|
2008-12-28 17:11:18 +01:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <modplug.h>
|
2009-11-10 19:01:38 +01:00
|
|
|
#include <assert.h>
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2009-01-24 19:15:53 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "modplug"
|
|
|
|
|
2009-01-24 19:15:50 +01:00
|
|
|
enum {
|
|
|
|
MODPLUG_FRAME_SIZE = 4096,
|
|
|
|
MODPLUG_PREALLOC_BLOCK = 256 * 1024,
|
|
|
|
MODPLUG_READ_BLOCK = 128 * 1024,
|
|
|
|
MODPLUG_FILE_LIMIT = 100 * 1024 * 1024,
|
|
|
|
};
|
2008-12-28 17:11:18 +01:00
|
|
|
|
|
|
|
static GByteArray *mod_loadfile(struct decoder *decoder, struct input_stream *is)
|
|
|
|
{
|
|
|
|
unsigned char *data;
|
|
|
|
GByteArray *bdatas;
|
2009-01-24 19:16:31 +01:00
|
|
|
size_t ret;
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2009-01-24 19:16:07 +01:00
|
|
|
if (is->size == 0) {
|
|
|
|
g_warning("file is empty");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is->size > MODPLUG_FILE_LIMIT) {
|
|
|
|
g_warning("file too large");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-28 17:11:18 +01:00
|
|
|
//known/unknown size, preallocate array, lets read in chunks
|
2009-01-24 19:16:07 +01:00
|
|
|
if (is->size > 0) {
|
2008-12-28 17:11:18 +01:00
|
|
|
bdatas = g_byte_array_sized_new(is->size);
|
|
|
|
} else {
|
|
|
|
bdatas = g_byte_array_sized_new(MODPLUG_PREALLOC_BLOCK);
|
|
|
|
}
|
2009-01-24 19:16:07 +01:00
|
|
|
|
2008-12-28 17:11:18 +01:00
|
|
|
data = g_malloc(MODPLUG_READ_BLOCK);
|
2009-01-24 20:02:06 +01:00
|
|
|
|
|
|
|
while (true) {
|
2009-01-24 19:15:48 +01:00
|
|
|
ret = decoder_read(decoder, is, data, MODPLUG_READ_BLOCK);
|
2009-01-24 19:16:20 +01:00
|
|
|
if (ret == 0) {
|
2009-01-24 19:16:33 +01:00
|
|
|
if (input_stream_eof(is))
|
|
|
|
/* end of file */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* I/O error - skip this song */
|
|
|
|
g_free(data);
|
|
|
|
g_byte_array_free(bdatas, true);
|
|
|
|
return NULL;
|
2008-12-28 17:11:18 +01:00
|
|
|
}
|
2009-01-24 19:16:10 +01:00
|
|
|
|
2009-01-24 19:16:20 +01:00
|
|
|
if (bdatas->len + ret > MODPLUG_FILE_LIMIT) {
|
2008-12-28 17:11:18 +01:00
|
|
|
g_warning("stream too large\n");
|
|
|
|
g_free(data);
|
|
|
|
g_byte_array_free(bdatas, TRUE);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-01-24 19:16:20 +01:00
|
|
|
|
|
|
|
g_byte_array_append(bdatas, data, ret);
|
2009-01-24 20:02:06 +01:00
|
|
|
}
|
|
|
|
|
2008-12-28 17:11:18 +01:00
|
|
|
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;
|
2009-11-14 22:30:57 +01:00
|
|
|
int ret;
|
2008-12-28 17:11:18 +01:00
|
|
|
char audio_buffer[MODPLUG_FRAME_SIZE];
|
|
|
|
enum decoder_command cmd = DECODE_COMMAND_NONE;
|
|
|
|
|
|
|
|
bdatas = mod_loadfile(decoder, is);
|
|
|
|
|
|
|
|
if (!bdatas) {
|
|
|
|
g_warning("could not load stream\n");
|
|
|
|
return;
|
|
|
|
}
|
2009-01-23 18:50:13 +01:00
|
|
|
|
2008-12-28 17:11:18 +01:00
|
|
|
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);
|
|
|
|
|
2009-01-23 18:50:13 +01:00
|
|
|
f = ModPlug_Load(bdatas->data, bdatas->len);
|
|
|
|
g_byte_array_free(bdatas, TRUE);
|
|
|
|
if (!f) {
|
|
|
|
g_warning("could not decode stream\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2);
|
2009-11-10 19:01:38 +01:00
|
|
|
assert(audio_format_valid(&audio_format));
|
2008-12-28 17:11:18 +01:00
|
|
|
|
|
|
|
decoder_initialized(decoder, &audio_format,
|
2009-11-14 22:27:27 +01:00
|
|
|
is->seekable, ModPlug_GetLength(f) / 1000.0);
|
2008-12-28 17:11:18 +01:00
|
|
|
|
|
|
|
do {
|
|
|
|
ret = ModPlug_Read(f, audio_buffer, MODPLUG_FRAME_SIZE);
|
2009-11-14 22:27:04 +01:00
|
|
|
if (ret <= 0)
|
2008-12-28 17:11:18 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
cmd = decoder_data(decoder, NULL,
|
|
|
|
audio_buffer, ret,
|
2009-11-14 22:30:57 +01:00
|
|
|
0, NULL);
|
2008-12-28 17:11:18 +01:00
|
|
|
|
|
|
|
if (cmd == DECODE_COMMAND_SEEK) {
|
2009-11-14 22:30:57 +01:00
|
|
|
float where = decoder_seek_where(decoder);
|
|
|
|
|
|
|
|
ModPlug_Seek(f, (int)(where * 1000.0));
|
|
|
|
|
2008-12-28 17:11:18 +01:00
|
|
|
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;
|
|
|
|
|
2009-11-14 23:53:04 +01:00
|
|
|
if (!input_stream_open(&is, file, NULL)) {
|
2008-12-28 17:11:18 +01:00
|
|
|
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();
|
2009-07-15 11:13:02 +02:00
|
|
|
ret->time = ModPlug_GetLength(f) / 1000;
|
2008-12-28 17:11:18 +01:00
|
|
|
|
|
|
|
title = g_strdup(ModPlug_GetName(f));
|
|
|
|
if (title)
|
2009-10-13 16:12:45 +02:00
|
|
|
tag_add_item(ret, TAG_TITLE, title);
|
2008-12-28 17:11:18 +01:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2009-04-03 00:52:13 +02:00
|
|
|
const struct decoder_plugin modplug_decoder_plugin = {
|
2008-12-28 17:11:18 +01:00
|
|
|
.name = "modplug",
|
|
|
|
.stream_decode = mod_decode,
|
|
|
|
.tag_dup = mod_tagdup,
|
|
|
|
.suffixes = mod_suffixes,
|
|
|
|
};
|