AudiofileDecoderPlugin.cxx 6.66 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "AudiofileDecoderPlugin.hxx"
21
#include "../DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "input/InputStream.hxx"
23
#include "CheckAudioFormat.hxx"
24
#include "tag/Handler.hxx"
25
#include "util/ScopeExit.hxx"
26 27
#include "util/Domain.hxx"
#include "Log.hxx"
Warren Dukes's avatar
Warren Dukes committed
28 29

#include <audiofile.h>
30
#include <af_vfs.h>
31

32
#include <exception>
33

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

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

40
static void
41
audiofile_error_func(long, const char *msg) noexcept
42 43 44 45 46
{
	LogWarning(audiofile_domain, msg);
}

static bool
47
audiofile_init(const ConfigBlock &)
48 49 50 51 52
{
	afSetErrorHandler(audiofile_error_func);
	return true;
}

53
struct AudioFileInputStream {
54
	DecoderClient *const client;
55 56
	InputStream &is;

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

67
gcc_pure
68
static SongTime
69
audiofile_get_duration(AFfilehandle fh) noexcept
70
{
71 72
	return SongTime::FromScale<uint64_t>(afGetFrameCount(fh, AF_DEFAULT_TRACK),
					     afGetRate(fh, AF_DEFAULT_TRACK));
73 74
}

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

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

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

89
	return is.GetSize();
90 91
}

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

98
	return is.GetOffset();
99 100 101
}

static void
102
audiofile_file_destroy(AFvirtualfile *vfile) noexcept
103
{
104
	assert(vfile->closure != nullptr);
105

106
	vfile->closure = nullptr;
107 108
}

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

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

120 121
	try {
		is.LockSeek(offset);
122
		return is.GetOffset();
123 124
	} catch (...) {
		LogError(std::current_exception(), "Seek failed");
125 126 127 128 129
		return -1;
	}
}

static AFvirtualfile *
130
setup_virtual_fops(AudioFileInputStream &afis) noexcept
131
{
132
	AFvirtualfile *vf = (AFvirtualfile *)malloc(sizeof(*vf));
133
	vf->closure = &afis;
134
	vf->write = nullptr;
135 136 137 138 139 140 141 142
	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;
}

143
gcc_const
144
static SampleFormat
145
audiofile_bits_to_sample_format(int bits) noexcept
146 147 148
{
	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
audiofile_setup_sample_format(AFfilehandle af_fp) noexcept
166 167 168 169
{
	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
		bits = 16;
	}

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

181
	return audiofile_bits_to_sample_format(bits);
182 183
}

184 185 186 187 188 189 190 191
static AudioFormat
CheckAudioFormat(AFfilehandle fh)
{
	return CheckAudioFormat(afGetRate(fh, AF_DEFAULT_TRACK),
				audiofile_setup_sample_format(fh),
				afGetVirtualChannels(fh, AF_DEFAULT_TRACK));
}

192
static void
193
audiofile_stream_decode(DecoderClient &client, InputStream &is)
Avuton Olrich's avatar
Avuton Olrich committed
194
{
195
	if (!is.IsSeekable() || !is.KnownSize()) {
196
		LogWarning(audiofile_domain, "not seekable");
197 198 199
		return;
	}

200
	AudioFileInputStream afis{&client, is};
201
	AFvirtualfile *const vf = setup_virtual_fops(afis);
202

203
	const AFfilehandle fh = afOpenVirtualFile(vf, "r", nullptr);
204
	if (fh == AF_NULL_FILEHANDLE)
205
		return;
Warren Dukes's avatar
Warren Dukes committed
206

207 208
	AtScopeExit(fh) { afCloseFile(fh); };

209
	const auto audio_format = CheckAudioFormat(fh);
210
	const auto total_time = audiofile_get_duration(fh);
Avuton Olrich's avatar
Avuton Olrich committed
211

212
	const uint16_t kbit_rate = (uint16_t)
213
		(is.GetSize() * uint64_t(8) / total_time.ToMS());
Avuton Olrich's avatar
Avuton Olrich committed
214

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

218
	client.Ready(audio_format, true, total_time);
219

220
	DecoderCommand cmd;
221
	do {
222
		uint8_t chunk[8192];
223 224 225 226
		const int nframes =
			afReadFrames(fh, AF_DEFAULT_TRACK, chunk,
				     sizeof(chunk) / frame_size);
		if (nframes <= 0)
227 228
			break;

229 230 231
		cmd = client.SubmitData(nullptr,
					chunk, nframes * frame_size,
					kbit_rate);
232

233
		if (cmd == DecoderCommand::SEEK) {
234
			AFframecount frame = client.GetSeekFrame();
235
			afSeekFrame(fh, AF_DEFAULT_TRACK, frame);
236

237
			client.CommandFinished();
238
			cmd = DecoderCommand::NONE;
239
		}
240
	} while (cmd == DecoderCommand::NONE);
Warren Dukes's avatar
Warren Dukes committed
241 242
}

243 244
static bool
audiofile_scan_stream(InputStream &is, TagHandler &handler) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
245
{
246
	if (!is.IsSeekable() || !is.KnownSize())
247
		return false;
248 249 250 251 252

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

255
	AtScopeExit(fh) { afCloseFile(fh); };
256

257
	handler.OnDuration(audiofile_get_duration(fh));
Warren Dukes's avatar
Warren Dukes committed
258

259
	try {
260
		handler.OnAudioFormat(CheckAudioFormat(fh));
261 262 263
	} catch (...) {
	}

264
	return true;
Warren Dukes's avatar
Warren Dukes committed
265 266
}

267
static const char *const audiofile_suffixes[] = {
268
	"wav", "au", "aiff", "aif", nullptr
269
};
Warren Dukes's avatar
Warren Dukes committed
270

271
static const char *const audiofile_mime_types[] = {
272 273
	"audio/wav",
	"audio/aiff",
274 275
	"audio/x-wav",
	"audio/x-aiff",
276
	nullptr
277 278
};

279
const struct DecoderPlugin audiofile_decoder_plugin = {
280
	"audiofile",
281
	audiofile_init,
282 283 284 285
	nullptr,
	audiofile_stream_decode,
	nullptr,
	nullptr,
286
	audiofile_scan_stream,
287 288 289
	nullptr,
	audiofile_suffixes,
	audiofile_mime_types,
Warren Dukes's avatar
Warren Dukes committed
290
};