modplug_plugin.c 4.76 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19 20 21 22 23 24
 */

#include "../decoder_api.h"

#include <glib.h>
#include <modplug.h>

25 26 27
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "modplug"

28 29 30 31 32 33
enum {
	MODPLUG_FRAME_SIZE = 4096,
	MODPLUG_PREALLOC_BLOCK = 256 * 1024,
	MODPLUG_READ_BLOCK = 128 * 1024,
	MODPLUG_FILE_LIMIT = 100 * 1024 * 1024,
};
34 35 36 37 38

static GByteArray *mod_loadfile(struct decoder *decoder, struct input_stream *is)
{
	unsigned char *data;
	GByteArray *bdatas;
39
	size_t ret;
40

41 42 43 44 45 46 47 48 49 50
	if (is->size == 0) {
		g_warning("file is empty");
		return NULL;
	}

	if (is->size > MODPLUG_FILE_LIMIT) {
		g_warning("file too large");
		return NULL;
	}

51
	//known/unknown size, preallocate array, lets read in chunks
52
	if (is->size > 0) {
53 54 55 56
		bdatas = g_byte_array_sized_new(is->size);
	} else {
		bdatas = g_byte_array_sized_new(MODPLUG_PREALLOC_BLOCK);
	}
57

58
	data = g_malloc(MODPLUG_READ_BLOCK);
59 60

	while (true) {
61
		ret = decoder_read(decoder, is, data, MODPLUG_READ_BLOCK);
62
		if (ret == 0) {
63 64 65 66 67 68 69 70
			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;
71
		}
72

73
		if (bdatas->len + ret > MODPLUG_FILE_LIMIT) {
74 75 76 77 78
			g_warning("stream too large\n");
			g_free(data);
			g_byte_array_free(bdatas, TRUE);
			return NULL;
		}
79 80

		g_byte_array_append(bdatas, data, ret);
81 82
	}

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	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;
	}
107

108 109 110 111 112 113 114 115 116
	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);

117 118 119 120 121 122 123
	f = ModPlug_Load(bdatas->data, bdatas->len);
	g_byte_array_free(bdatas, TRUE);
	if (!f) {
		g_warning("could not decode stream\n");
		return;
	}

124
	audio_format_init(&audio_format, 44100, 16, 2);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

	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();
187
	ret->time = ModPlug_GetLength(f) / 1000;
188 189 190

	title = g_strdup(ModPlug_GetName(f));
	if (title)
191
		tag_add_item(ret, TAG_TITLE, title);
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	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
};

208
const struct decoder_plugin modplug_decoder_plugin = {
209 210 211 212 213
	.name = "modplug",
	.stream_decode = mod_decode,
	.tag_dup = mod_tagdup,
	.suffixes = mod_suffixes,
};