Mpg123DecoderPlugin.cxx 5.7 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h" /* must be first for large file support */
21
#include "Mpg123DecoderPlugin.hxx"
22
#include "../DecoderAPI.hxx"
23
#include "CheckAudioFormat.hxx"
24
#include "tag/TagHandler.hxx"
25
#include "util/Error.hxx"
26 27
#include "util/Domain.hxx"
#include "Log.hxx"
28 29

#include <mpg123.h>
30

31
#include <stdio.h>
32

33
static constexpr Domain mpg123_domain("mpg123");
34 35

static bool
36
mpd_mpg123_init(gcc_unused const config_param &param)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
{
	mpg123_init();

	return true;
}

static void
mpd_mpg123_finish(void)
{
	mpg123_exit();
}

/**
 * Opens a file with an existing #mpg123_handle.
 *
 * @param handle a handle which was created before; on error, this
 * function will not free it
 * @param audio_format this parameter is filled after successful
 * return
 * @return true on success
 */
static bool
mpd_mpg123_open(mpg123_handle *handle, const char *path_fs,
60
		AudioFormat &audio_format)
61 62 63 64 65 66
{
	int error;
	int channels, encoding;
	long rate;

	/* mpg123_open() wants a writable string :-( */
67
	char *const path2 = const_cast<char *>(path_fs);
68

69
	error = mpg123_open(handle, path2);
70
	if (error != MPG123_OK) {
71 72 73
		FormatWarning(mpg123_domain,
			      "libmpg123 failed to open %s: %s",
			      path_fs, mpg123_plain_strerror(error));
74 75 76 77 78 79 80
		return false;
	}

	/* obtain the audio format */

	error = mpg123_getformat(handle, &rate, &channels, &encoding);
	if (error != MPG123_OK) {
81 82 83
		FormatWarning(mpg123_domain,
			      "mpg123_getformat() failed: %s",
			      mpg123_plain_strerror(error));
84 85 86 87 88
		return false;
	}

	if (encoding != MPG123_ENC_SIGNED_16) {
		/* other formats not yet implemented */
89 90 91
		FormatWarning(mpg123_domain,
			      "expected MPG123_ENC_SIGNED_16, got %d",
			      encoding);
92 93 94
		return false;
	}

95
	Error error2;
96
	if (!audio_format_init_checked(audio_format, rate, SampleFormat::S16,
97
				       channels, error2)) {
98
		LogError(error2);
99 100 101 102 103 104 105
		return false;
	}

	return true;
}

static void
106
mpd_mpg123_file_decode(Decoder &decoder, const char *path_fs)
107 108 109
{
	mpg123_handle *handle;
	int error;
110
	off_t num_samples;
111
	struct mpg123_frameinfo info;
112 113 114

	/* open the file */

115 116
	handle = mpg123_new(nullptr, &error);
	if (handle == nullptr) {
117 118 119
		FormatError(mpg123_domain,
			    "mpg123_new() failed: %s",
			    mpg123_plain_strerror(error));
120 121 122
		return;
	}

123 124
	AudioFormat audio_format;
	if (!mpd_mpg123_open(handle, path_fs, audio_format)) {
125 126 127 128 129 130 131 132
		mpg123_delete(handle);
		return;
	}

	num_samples = mpg123_length(handle);

	/* tell MPD core we're ready */

133
	decoder_initialized(decoder, audio_format, true,
134 135 136
			    (float)num_samples /
			    (float)audio_format.sample_rate);

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	if (mpg123_info(handle, &info) != MPG123_OK) {
		info.vbr = MPG123_CBR;
		info.bitrate = 0;
	}

	switch (info.vbr) {
	case MPG123_ABR:
		info.bitrate = info.abr_rate;
		break;
	case MPG123_CBR:
		break;
	default:
		info.bitrate = 0;
	}

152 153
	/* the decoder main loop */

154
	DecoderCommand cmd;
155 156 157 158 159 160 161 162 163
	do {
		unsigned char buffer[8192];
		size_t nbytes;

		/* decode */

		error = mpg123_read(handle, buffer, sizeof(buffer), &nbytes);
		if (error != MPG123_OK) {
			if (error != MPG123_DONE)
164 165 166
				FormatWarning(mpg123_domain,
					      "mpg123_read() failed: %s",
					      mpg123_plain_strerror(error));
167 168 169
			break;
		}

170 171 172 173 174 175 176 177
		/* update bitrate for ABR/VBR */
		if (info.vbr != MPG123_CBR) {
			/* FIXME: maybe skip, as too expensive? */
			/* FIXME: maybe, (info.vbr == MPG123_VBR) ? */
			if (mpg123_info (handle, &info) != MPG123_OK)
				info.bitrate = 0;
		}

178 179
		/* send to MPD */

180
		cmd = decoder_data(decoder, nullptr, buffer, nbytes, info.bitrate);
181

182
		if (cmd == DecoderCommand::SEEK) {
183 184 185 186 187 188 189 190 191
			off_t c = decoder_seek_where(decoder)*audio_format.sample_rate;
			c = mpg123_seek(handle, c, SEEK_SET);
			if (c < 0)
				decoder_seek_error(decoder);
			else {
				decoder_command_finished(decoder);
				decoder_timestamp(decoder, c/(double)audio_format.sample_rate);
			}

192
			cmd = DecoderCommand::NONE;
193
		}
194
	} while (cmd == DecoderCommand::NONE);
195 196 197 198 199 200

	/* cleanup */

	mpg123_delete(handle);
}

201 202 203
static bool
mpd_mpg123_scan_file(const char *path_fs,
		     const struct tag_handler *handler, void *handler_ctx)
204 205 206 207 208
{
	mpg123_handle *handle;
	int error;
	off_t num_samples;

209 210
	handle = mpg123_new(nullptr, &error);
	if (handle == nullptr) {
211 212 213
		FormatError(mpg123_domain,
			    "mpg123_new() failed: %s",
			    mpg123_plain_strerror(error));
214
		return false;
215 216
	}

217 218
	AudioFormat audio_format;
	if (!mpd_mpg123_open(handle, path_fs, audio_format)) {
219
		mpg123_delete(handle);
220
		return false;
221 222 223 224 225
	}

	num_samples = mpg123_length(handle);
	if (num_samples <= 0) {
		mpg123_delete(handle);
226
		return false;
227 228 229 230 231
	}

	/* ID3 tag support not yet implemented */

	mpg123_delete(handle);
232 233 234 235

	tag_handler_invoke_duration(handler, handler_ctx,
				    num_samples / audio_format.sample_rate);
	return true;
236 237 238 239
}

static const char *const mpg123_suffixes[] = {
	"mp3",
240
	nullptr
241 242
};

243
const struct DecoderPlugin mpg123_decoder_plugin = {
244 245 246
	"mpg123",
	mpd_mpg123_init,
	mpd_mpg123_finish,
247
	/* streaming not yet implemented */
248 249 250 251 252 253 254
	nullptr,
	mpd_mpg123_file_decode,
	mpd_mpg123_scan_file,
	nullptr,
	nullptr,
	mpg123_suffixes,
	nullptr,
255
};