AudiofileDecoderPlugin.cxx 6.85 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4
 * http://www.musicpd.org
 *
Warren Dukes's avatar
Warren Dukes committed
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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
21
#include "AudiofileDecoderPlugin.hxx"
22
#include "../DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "input/InputStream.hxx"
24
#include "CheckAudioFormat.hxx"
25
#include "tag/TagHandler.hxx"
26
#include "fs/Path.hxx"
27
#include "util/Error.hxx"
28 29
#include "util/Domain.hxx"
#include "Log.hxx"
Warren Dukes's avatar
Warren Dukes committed
30 31

#include <audiofile.h>
32
#include <af_vfs.h>
33

34
#include <assert.h>
35
#include <stdio.h>
Warren Dukes's avatar
Warren Dukes committed
36

37 38
static constexpr Domain audiofile_domain("audiofile");

39 40 41 42 43 44 45 46 47 48 49 50 51
static void
audiofile_error_func(long, const char *msg)
{
	LogWarning(audiofile_domain, msg);
}

static bool
audiofile_init(const config_param &)
{
	afSetErrorHandler(audiofile_error_func);
	return true;
}

52 53 54 55 56
struct AudioFileInputStream {
	Decoder *const decoder;
	InputStream &is;

	size_t Read(void *buffer, size_t size) {
57
		/* libaudiofile does not like partial reads at all,
58
		   and will abort playback; therefore always force full
59 60 61 62
		   reads */
		return decoder_read_full(decoder, is, buffer, size)
			? size
			: 0;
63 64 65
	}
};

66 67 68 69 70 71 72 73 74 75
gcc_pure
static double
audiofile_get_duration(AFfilehandle fh)
{
	double frame_count = afGetFrameCount(fh, AF_DEFAULT_TRACK);
	double rate = afGetRate(fh, AF_DEFAULT_TRACK);

	return frame_count / rate;
}

76
static ssize_t
77
audiofile_file_read(AFvirtualfile *vfile, void *data, size_t length)
78
{
79
	AudioFileInputStream &afis = *(AudioFileInputStream *)vfile->closure;
80

81
	return afis.Read(data, length);
82 83
}

84
static AFfileoffset
85 86
audiofile_file_length(AFvirtualfile *vfile)
{
87 88 89
	AudioFileInputStream &afis = *(AudioFileInputStream *)vfile->closure;
	InputStream &is = afis.is;

90
	return is.GetSize();
91 92
}

93
static AFfileoffset
94 95
audiofile_file_tell(AFvirtualfile *vfile)
{
96 97 98
	AudioFileInputStream &afis = *(AudioFileInputStream *)vfile->closure;
	InputStream &is = afis.is;

99
	return is.GetOffset();
100 101 102 103 104
}

static void
audiofile_file_destroy(AFvirtualfile *vfile)
{
105
	assert(vfile->closure != nullptr);
106

107
	vfile->closure = nullptr;
108 109
}

110
static AFfileoffset
111 112
audiofile_file_seek(AFvirtualfile *vfile, AFfileoffset _offset,
		    int is_relative)
113
{
114 115
	AudioFileInputStream &afis = *(AudioFileInputStream *)vfile->closure;
	InputStream &is = afis.is;
116

117
	offset_type offset = _offset;
118 119
	if (is_relative)
		offset += is.GetOffset();
120 121

	Error error;
Max Kellermann's avatar
Max Kellermann committed
122
	if (is.LockSeek(offset, error)) {
123
		return is.GetOffset();
124
	} else {
Max Kellermann's avatar
Max Kellermann committed
125
		LogError(error, "Seek failed");
126 127 128 129 130
		return -1;
	}
}

static AFvirtualfile *
131
setup_virtual_fops(AudioFileInputStream &afis)
132
{
133
	AFvirtualfile *vf = new AFvirtualfile();
134
	vf->closure = &afis;
135
	vf->write = nullptr;
136 137 138 139 140 141 142 143
	vf->read    = audiofile_file_read;
	vf->length  = audiofile_file_length;
	vf->destroy = audiofile_file_destroy;
	vf->seek    = audiofile_file_seek;
	vf->tell    = audiofile_file_tell;
	return vf;
}

144
static SampleFormat
145 146 147 148
audiofile_bits_to_sample_format(int bits)
{
	switch (bits) {
	case 8:
149
		return SampleFormat::S8;
150 151

	case 16:
152
		return SampleFormat::S16;
153 154

	case 24:
155
		return SampleFormat::S24_P32;
156 157

	case 32:
158
		return SampleFormat::S32;
159 160
	}

161
	return SampleFormat::UNDEFINED;
162 163
}

164
static SampleFormat
165 166 167 168 169
audiofile_setup_sample_format(AFfilehandle af_fp)
{
	int fs, bits;

	afGetSampleFormat(af_fp, AF_DEFAULT_TRACK, &fs, &bits);
170
	if (!audio_valid_sample_format(audiofile_bits_to_sample_format(bits))) {
171 172 173
		FormatDebug(audiofile_domain,
			    "input file has %d bit samples, converting to 16",
			    bits);
174 175 176 177 178 179 180
		bits = 16;
	}

	afSetVirtualSampleFormat(af_fp, AF_DEFAULT_TRACK,
	                         AF_SAMPFMT_TWOSCOMP, bits);
	afGetVirtualSampleFormat(af_fp, AF_DEFAULT_TRACK, &fs, &bits);

181
	return audiofile_bits_to_sample_format(bits);
182 183
}

184
static void
185
audiofile_stream_decode(Decoder &decoder, InputStream &is)
Avuton Olrich's avatar
Avuton Olrich committed
186
{
187
	if (!is.IsSeekable() || !is.KnownSize()) {
188
		LogWarning(audiofile_domain, "not seekable");
189 190 191
		return;
	}

192
	AudioFileInputStream afis{&decoder, is};
193
	AFvirtualfile *const vf = setup_virtual_fops(afis);
194

195
	const AFfilehandle fh = afOpenVirtualFile(vf, "r", nullptr);
196
	if (fh == AF_NULL_FILEHANDLE)
197
		return;
Warren Dukes's avatar
Warren Dukes committed
198

199
	Error error;
200
	AudioFormat audio_format;
201
	if (!audio_format_init_checked(audio_format,
202 203 204
				       afGetRate(fh, AF_DEFAULT_TRACK),
				       audiofile_setup_sample_format(fh),
				       afGetVirtualChannels(fh, AF_DEFAULT_TRACK),
205
				       error)) {
206
		LogError(error);
207
		afCloseFile(fh);
208 209 210
		return;
	}

211
	const double total_time = audiofile_get_duration(fh);
Avuton Olrich's avatar
Avuton Olrich committed
212

213 214
	const uint16_t kbit_rate = (uint16_t)
		(is.GetSize() * 8.0 / total_time / 1000.0 + 0.5);
Avuton Olrich's avatar
Avuton Olrich committed
215

216 217
	const unsigned frame_size = (unsigned)
		afGetVirtualFrameSize(fh, AF_DEFAULT_TRACK, true);
Warren Dukes's avatar
Warren Dukes committed
218

219
	decoder_initialized(decoder, audio_format, true, total_time);
220

221
	DecoderCommand cmd;
222
	do {
223 224 225 226 227 228 229
		/* pick 1020 since its divisible for 8,16,24, and
		   32-bit audio */
		char chunk[1020];
		const int nframes =
			afReadFrames(fh, AF_DEFAULT_TRACK, chunk,
				     sizeof(chunk) / frame_size);
		if (nframes <= 0)
230 231
			break;

232
		cmd = decoder_data(decoder, nullptr,
233 234
				   chunk, nframes * frame_size,
				   kbit_rate);
235

236
		if (cmd == DecoderCommand::SEEK) {
237
			AFframecount frame = decoder_seek_where(decoder) *
238
				audio_format.sample_rate;
239
			afSeekFrame(fh, AF_DEFAULT_TRACK, frame);
240 241

			decoder_command_finished(decoder);
242
			cmd = DecoderCommand::NONE;
243
		}
244
	} while (cmd == DecoderCommand::NONE);
245

246
	afCloseFile(fh);
Warren Dukes's avatar
Warren Dukes committed
247 248
}

249 250 251
gcc_pure
static int
audiofile_get_duration(InputStream &is)
Avuton Olrich's avatar
Avuton Olrich committed
252
{
253
	if (!is.IsSeekable() || !is.KnownSize())
254 255 256 257 258 259 260
		return -1;

	AudioFileInputStream afis{nullptr, is};
	AFvirtualfile *vf = setup_virtual_fops(afis);
	AFfilehandle fh = afOpenVirtualFile(vf, "r", nullptr);
	if (fh == AF_NULL_FILEHANDLE)
		return -1;
Avuton Olrich's avatar
Avuton Olrich committed
261

262 263 264 265 266 267 268 269 270 271 272
	int duration = int(audiofile_get_duration(fh));
	afCloseFile(fh);
	return duration;
}

static bool
audiofile_scan_stream(InputStream &is,
		      const struct tag_handler *handler, void *handler_ctx)
{
	int total_time = audiofile_get_duration(is);
	if (total_time < 0)
273
		return false;
Warren Dukes's avatar
Warren Dukes committed
274

275 276
	tag_handler_invoke_duration(handler, handler_ctx, total_time);
	return true;
Warren Dukes's avatar
Warren Dukes committed
277 278
}

279
static const char *const audiofile_suffixes[] = {
280
	"wav", "au", "aiff", "aif", nullptr
281
};
Warren Dukes's avatar
Warren Dukes committed
282

283 284 285
static const char *const audiofile_mime_types[] = {
	"audio/x-wav",
	"audio/x-aiff",
286
	nullptr
287 288
};

289
const struct DecoderPlugin audiofile_decoder_plugin = {
290
	"audiofile",
291
	audiofile_init,
292 293 294 295
	nullptr,
	audiofile_stream_decode,
	nullptr,
	nullptr,
296
	audiofile_scan_stream,
297 298 299
	nullptr,
	audiofile_suffixes,
	audiofile_mime_types,
Warren Dukes's avatar
Warren Dukes committed
300
};