2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-07-28 12:56:35 +02:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* 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"
|
2013-07-28 12:56:35 +02:00
|
|
|
#include "ModplugDecoderPlugin.hxx"
|
2013-07-28 13:18:48 +02:00
|
|
|
#include "DecoderAPI.hxx"
|
2013-09-05 00:06:31 +02:00
|
|
|
#include "InputStream.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/TagHandler.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
|
|
|
#include "Log.hxx"
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2013-09-26 17:14:25 +02:00
|
|
|
#include <libmodplug/modplug.h>
|
|
|
|
|
2008-12-28 17:11:18 +01:00
|
|
|
#include <glib.h>
|
2013-09-26 17:14:25 +02:00
|
|
|
|
2009-11-10 19:01:38 +01:00
|
|
|
#include <assert.h>
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain modplug_domain("modplug");
|
2009-01-24 19:15:53 +01:00
|
|
|
|
2013-07-28 12:56:35 +02:00
|
|
|
static constexpr size_t MODPLUG_FRAME_SIZE = 4096;
|
|
|
|
static constexpr size_t MODPLUG_PREALLOC_BLOCK = 256 * 1024;
|
|
|
|
static constexpr size_t MODPLUG_READ_BLOCK = 128 * 1024;
|
2013-10-17 09:43:55 +02:00
|
|
|
static constexpr input_stream::offset_type MODPLUG_FILE_LIMIT = 100 * 1024 * 1024;
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2013-07-28 12:56:35 +02:00
|
|
|
static GByteArray *
|
|
|
|
mod_loadfile(struct decoder *decoder, struct input_stream *is)
|
2008-12-28 17:11:18 +01:00
|
|
|
{
|
2013-10-17 09:43:55 +02:00
|
|
|
const input_stream::offset_type size = is->GetSize();
|
2013-01-24 19:14:40 +01:00
|
|
|
|
|
|
|
if (size == 0) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(modplug_domain, "file is empty");
|
2013-07-28 12:56:35 +02:00
|
|
|
return nullptr;
|
2009-01-24 19:16:07 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 19:14:40 +01:00
|
|
|
if (size > MODPLUG_FILE_LIMIT) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(modplug_domain, "file too large");
|
2013-07-28 12:56:35 +02:00
|
|
|
return nullptr;
|
2009-01-24 19:16:07 +01:00
|
|
|
}
|
|
|
|
|
2008-12-28 17:11:18 +01:00
|
|
|
//known/unknown size, preallocate array, lets read in chunks
|
2013-07-28 12:56:35 +02:00
|
|
|
GByteArray *bdatas;
|
2013-01-24 19:14:40 +01:00
|
|
|
if (size > 0) {
|
|
|
|
bdatas = g_byte_array_sized_new(size);
|
2008-12-28 17:11:18 +01:00
|
|
|
} else {
|
|
|
|
bdatas = g_byte_array_sized_new(MODPLUG_PREALLOC_BLOCK);
|
|
|
|
}
|
2009-01-24 19:16:07 +01:00
|
|
|
|
2013-07-28 12:56:35 +02:00
|
|
|
unsigned char *data = (unsigned char *)g_malloc(MODPLUG_READ_BLOCK);
|
2009-01-24 20:02:06 +01:00
|
|
|
|
|
|
|
while (true) {
|
2013-07-28 12:56:35 +02:00
|
|
|
size_t ret = decoder_read(decoder, is, data,
|
|
|
|
MODPLUG_READ_BLOCK);
|
2009-01-24 19:16:20 +01:00
|
|
|
if (ret == 0) {
|
2013-09-05 00:06:31 +02:00
|
|
|
if (is->LockIsEOF())
|
2009-01-24 19:16:33 +01:00
|
|
|
/* end of file */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* I/O error - skip this song */
|
|
|
|
g_free(data);
|
|
|
|
g_byte_array_free(bdatas, true);
|
2013-07-28 12:56:35 +02:00
|
|
|
return nullptr;
|
2008-12-28 17:11:18 +01:00
|
|
|
}
|
2009-01-24 19:16:10 +01:00
|
|
|
|
2013-10-17 09:43:55 +02:00
|
|
|
if (input_stream::offset_type(bdatas->len + ret) > MODPLUG_FILE_LIMIT) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(modplug_domain, "stream too large");
|
2008-12-28 17:11:18 +01:00
|
|
|
g_free(data);
|
|
|
|
g_byte_array_free(bdatas, TRUE);
|
2013-07-28 12:56:35 +02:00
|
|
|
return nullptr;
|
2008-12-28 17:11:18 +01:00
|
|
|
}
|
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;
|
2009-11-14 22:30:57 +01:00
|
|
|
int ret;
|
2008-12-28 17:11:18 +01:00
|
|
|
char audio_buffer[MODPLUG_FRAME_SIZE];
|
|
|
|
|
|
|
|
bdatas = mod_loadfile(decoder, is);
|
|
|
|
|
|
|
|
if (!bdatas) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(modplug_domain, "could not load stream");
|
2008-12-28 17:11:18 +01:00
|
|
|
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) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(modplug_domain, "could not decode stream");
|
2009-01-23 18:50:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
static constexpr AudioFormat audio_format(44100, SampleFormat::S16, 2);
|
|
|
|
assert(audio_format.IsValid());
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
decoder_initialized(decoder, audio_format,
|
2013-09-05 00:06:31 +02:00
|
|
|
is->IsSeekable(),
|
2013-01-24 19:14:40 +01:00
|
|
|
ModPlug_GetLength(f) / 1000.0);
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd;
|
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;
|
|
|
|
|
2013-07-28 12:56:35 +02:00
|
|
|
cmd = decoder_data(decoder, nullptr,
|
2008-12-28 17:11:18 +01:00
|
|
|
audio_buffer, ret,
|
2010-01-03 22:44:23 +01:00
|
|
|
0);
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
if (cmd == DecoderCommand::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);
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
} while (cmd != DecoderCommand::STOP);
|
2008-12-28 17:11:18 +01:00
|
|
|
|
|
|
|
ModPlug_Unload(f);
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
|
|
|
modplug_scan_stream(struct input_stream *is,
|
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
2008-12-28 17:11:18 +01:00
|
|
|
{
|
|
|
|
ModPlugFile *f;
|
|
|
|
GByteArray *bdatas;
|
|
|
|
|
2013-07-28 12:56:35 +02:00
|
|
|
bdatas = mod_loadfile(nullptr, is);
|
2009-12-31 18:20:08 +01:00
|
|
|
if (!bdatas)
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2008-12-28 17:11:18 +01:00
|
|
|
|
|
|
|
f = ModPlug_Load(bdatas->data, bdatas->len);
|
|
|
|
g_byte_array_free(bdatas, TRUE);
|
2013-07-28 12:56:35 +02:00
|
|
|
if (f == nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-12-31 18:20:08 +01:00
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_duration(handler, handler_ctx,
|
|
|
|
ModPlug_GetLength(f) / 1000);
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
const char *title = ModPlug_GetName(f);
|
2013-07-28 12:56:35 +02:00
|
|
|
if (title != nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_tag(handler, handler_ctx,
|
|
|
|
TAG_TITLE, title);
|
2008-12-28 17:11:18 +01:00
|
|
|
|
|
|
|
ModPlug_Unload(f);
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
return true;
|
2008-12-28 17:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2013-07-28 12:56:35 +02:00
|
|
|
nullptr
|
2008-12-28 17:11:18 +01:00
|
|
|
};
|
|
|
|
|
2009-04-03 00:52:13 +02:00
|
|
|
const struct decoder_plugin modplug_decoder_plugin = {
|
2013-07-28 12:56:35 +02:00
|
|
|
"modplug",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
mod_decode,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
modplug_scan_stream,
|
|
|
|
nullptr,
|
|
|
|
mod_suffixes,
|
|
|
|
nullptr,
|
2008-12-28 17:11:18 +01:00
|
|
|
};
|