AudiofileDecoderPlugin.cxx 6.18 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>
Warren Dukes's avatar
Warren Dukes committed
35

36 37 38
/* pick 1020 since its devisible for 8,16,24, and 32-bit audio */
#define CHUNK_SIZE		1020

39 40
static constexpr Domain audiofile_domain("audiofile");

41 42 43
gcc_pure
static int
audiofile_get_duration(Path path_fs)
Warren Dukes's avatar
Warren Dukes committed
44
{
Max Kellermann's avatar
Max Kellermann committed
45
	int total_time;
46
	AFfilehandle af_fp = afOpenFile(path_fs.c_str(), "r", nullptr);
Avuton Olrich's avatar
Avuton Olrich committed
47
	if (af_fp == AF_NULL_FILEHANDLE) {
Warren Dukes's avatar
Warren Dukes committed
48 49
		return -1;
	}
Max Kellermann's avatar
Max Kellermann committed
50
	total_time = (int)
Avuton Olrich's avatar
Avuton Olrich committed
51 52
	    ((double)afGetFrameCount(af_fp, AF_DEFAULT_TRACK)
	     / afGetRate(af_fp, AF_DEFAULT_TRACK));
Warren Dukes's avatar
Warren Dukes committed
53
	afCloseFile(af_fp);
Max Kellermann's avatar
Max Kellermann committed
54
	return total_time;
Warren Dukes's avatar
Warren Dukes committed
55 56
}

57
static ssize_t
58
audiofile_file_read(AFvirtualfile *vfile, void *data, size_t length)
59
{
60
	InputStream &is = *(InputStream *)vfile->closure;
61

62
	Error error;
63
	size_t nbytes = is.LockRead(data, length, error);
64
	if (nbytes == 0 && error.IsDefined()) {
65
		LogError(error);
66 67 68 69
		return -1;
	}

	return nbytes;
70 71
}

72
static AFfileoffset
73 74
audiofile_file_length(AFvirtualfile *vfile)
{
75 76
	InputStream &is = *(InputStream *)vfile->closure;
	return is.GetSize();
77 78
}

79
static AFfileoffset
80 81
audiofile_file_tell(AFvirtualfile *vfile)
{
82 83
	InputStream &is = *(InputStream *)vfile->closure;
	return is.GetOffset();
84 85 86 87 88
}

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

91
	vfile->closure = nullptr;
92 93
}

94 95
static AFfileoffset
audiofile_file_seek(AFvirtualfile *vfile, AFfileoffset offset, int is_relative)
96
{
97
	InputStream &is = *(InputStream *)vfile->closure;
98
	int whence = (is_relative ? SEEK_CUR : SEEK_SET);
99 100

	Error error;
101 102
	if (is.LockSeek(offset, whence, error)) {
		return is.GetOffset();
103 104 105 106 107 108
	} else {
		return -1;
	}
}

static AFvirtualfile *
109
setup_virtual_fops(InputStream &stream)
110
{
111
	AFvirtualfile *vf = new AFvirtualfile();
112
	vf->closure = &stream;
113
	vf->write = nullptr;
114 115 116 117 118 119 120 121
	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;
}

122
static SampleFormat
123 124 125 126
audiofile_bits_to_sample_format(int bits)
{
	switch (bits) {
	case 8:
127
		return SampleFormat::S8;
128 129

	case 16:
130
		return SampleFormat::S16;
131 132

	case 24:
133
		return SampleFormat::S24_P32;
134 135

	case 32:
136
		return SampleFormat::S32;
137 138
	}

139
	return SampleFormat::UNDEFINED;
140 141
}

142
static SampleFormat
143 144 145 146 147
audiofile_setup_sample_format(AFfilehandle af_fp)
{
	int fs, bits;

	afGetSampleFormat(af_fp, AF_DEFAULT_TRACK, &fs, &bits);
148
	if (!audio_valid_sample_format(audiofile_bits_to_sample_format(bits))) {
149 150 151
		FormatDebug(audiofile_domain,
			    "input file has %d bit samples, converting to 16",
			    bits);
152 153 154 155 156 157 158
		bits = 16;
	}

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

159
	return audiofile_bits_to_sample_format(bits);
160 161
}

162
static void
163
audiofile_stream_decode(Decoder &decoder, InputStream &is)
Avuton Olrich's avatar
Avuton Olrich committed
164
{
165
	AFvirtualfile *vf;
Warren Dukes's avatar
Warren Dukes committed
166 167
	int fs, frame_count;
	AFfilehandle af_fp;
168
	AudioFormat audio_format;
169
	float total_time;
Max Kellermann's avatar
Max Kellermann committed
170
	uint16_t bit_rate;
171
	int ret;
172
	char chunk[CHUNK_SIZE];
173

174
	if (!is.IsSeekable()) {
175
		LogWarning(audiofile_domain, "not seekable");
176 177 178
		return;
	}

Max Kellermann's avatar
Max Kellermann committed
179
	vf = setup_virtual_fops(is);
180

181
	af_fp = afOpenVirtualFile(vf, "r", nullptr);
Avuton Olrich's avatar
Avuton Olrich committed
182
	if (af_fp == AF_NULL_FILEHANDLE) {
183
		LogWarning(audiofile_domain, "failed to input stream");
184
		return;
Warren Dukes's avatar
Warren Dukes committed
185 186
	}

187
	Error error;
188
	if (!audio_format_init_checked(audio_format,
189
				       afGetRate(af_fp, AF_DEFAULT_TRACK),
190
				       audiofile_setup_sample_format(af_fp),
191
				       afGetVirtualChannels(af_fp, AF_DEFAULT_TRACK),
192
				       error)) {
193
		LogError(error);
194 195 196 197
		afCloseFile(af_fp);
		return;
	}

Avuton Olrich's avatar
Avuton Olrich committed
198 199
	frame_count = afGetFrameCount(af_fp, AF_DEFAULT_TRACK);

200
	total_time = ((float)frame_count / (float)audio_format.sample_rate);
Avuton Olrich's avatar
Avuton Olrich committed
201

202
	bit_rate = (uint16_t)(is.GetSize() * 8.0 / total_time / 1000.0 + 0.5);
Avuton Olrich's avatar
Avuton Olrich committed
203

204
	fs = (int)afGetVirtualFrameSize(af_fp, AF_DEFAULT_TRACK, 1);
Warren Dukes's avatar
Warren Dukes committed
205

206
	decoder_initialized(decoder, audio_format, true, total_time);
207

208
	DecoderCommand cmd;
209 210 211 212 213 214
	do {
		ret = afReadFrames(af_fp, AF_DEFAULT_TRACK, chunk,
				   CHUNK_SIZE / fs);
		if (ret <= 0)
			break;

215
		cmd = decoder_data(decoder, nullptr,
216
				   chunk, ret * fs,
217
				   bit_rate);
218

219
		if (cmd == DecoderCommand::SEEK) {
220
			AFframecount frame = decoder_seek_where(decoder) *
221
				audio_format.sample_rate;
222
			afSeekFrame(af_fp, AF_DEFAULT_TRACK, frame);
223 224

			decoder_command_finished(decoder);
225
			cmd = DecoderCommand::NONE;
226
		}
227
	} while (cmd == DecoderCommand::NONE);
228

Warren Dukes's avatar
Warren Dukes committed
229 230 231
	afCloseFile(af_fp);
}

232
static bool
233
audiofile_scan_file(Path path_fs,
234
		    const struct tag_handler *handler, void *handler_ctx)
Avuton Olrich's avatar
Avuton Olrich committed
235
{
236
	int total_time = audiofile_get_duration(path_fs);
Avuton Olrich's avatar
Avuton Olrich committed
237

238
	if (total_time < 0) {
239 240
		FormatWarning(audiofile_domain,
			      "Failed to get total song time from: %s",
241
			      path_fs.c_str());
242
		return false;
243
	}
Warren Dukes's avatar
Warren Dukes committed
244

245 246
	tag_handler_invoke_duration(handler, handler_ctx, total_time);
	return true;
Warren Dukes's avatar
Warren Dukes committed
247 248
}

249
static const char *const audiofile_suffixes[] = {
250
	"wav", "au", "aiff", "aif", nullptr
251
};
Warren Dukes's avatar
Warren Dukes committed
252

253 254 255
static const char *const audiofile_mime_types[] = {
	"audio/x-wav",
	"audio/x-aiff",
256
	nullptr
257 258
};

259
const struct DecoderPlugin audiofile_decoder_plugin = {
260 261 262 263 264 265 266 267 268 269
	"audiofile",
	nullptr,
	nullptr,
	audiofile_stream_decode,
	nullptr,
	audiofile_scan_file,
	nullptr,
	nullptr,
	audiofile_suffixes,
	audiofile_mime_types,
Warren Dukes's avatar
Warren Dukes committed
270
};