AudiofileDecoderPlugin.cxx 6.72 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "util/ScopeExit.hxx"
27 28
#include "util/Domain.hxx"
#include "Log.hxx"
Warren Dukes's avatar
Warren Dukes committed
29 30

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

33 34
#include <stdexcept>

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

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

40 41 42 43 44 45 46
static void
audiofile_error_func(long, const char *msg)
{
	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 57
	InputStream &is;

	size_t Read(void *buffer, size_t size) {
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 70
audiofile_get_duration(AFfilehandle fh)
{
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)
77
{
78
	AudioFileInputStream &afis = *(AudioFileInputStream *)vfile->closure;
79

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

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

89
	return is.GetSize();
90 91
}

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

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

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

106
	vfile->closure = nullptr;
107 108
}

109
static AFfileoffset
110 111
audiofile_file_seek(AFvirtualfile *vfile, AFfileoffset _offset,
		    int is_relative)
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 (const std::runtime_error &e) {
		LogError(e, "Seek failed");
125 126 127 128 129
		return -1;
	}
}

static AFvirtualfile *
130
setup_virtual_fops(AudioFileInputStream &afis)
131
{
132
	AFvirtualfile *vf = new AFvirtualfile();
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
static SampleFormat
144 145 146 147
audiofile_bits_to_sample_format(int bits)
{
	switch (bits) {
	case 8:
148
		return SampleFormat::S8;
149 150

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

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

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

160
	return SampleFormat::UNDEFINED;
161 162
}

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

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

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

180
	return audiofile_bits_to_sample_format(bits);
181 182
}

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

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

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

198 199
	AtScopeExit(fh) { afCloseFile(fh); };

200 201 202 203
	const auto audio_format =
		CheckAudioFormat(afGetRate(fh, AF_DEFAULT_TRACK),
				 audiofile_setup_sample_format(fh),
				 afGetVirtualChannels(fh, AF_DEFAULT_TRACK));
204

205
	const auto total_time = audiofile_get_duration(fh);
Avuton Olrich's avatar
Avuton Olrich committed
206

207
	const uint16_t kbit_rate = (uint16_t)
208
		(is.GetSize() * uint64_t(8) / total_time.ToMS());
Avuton Olrich's avatar
Avuton Olrich committed
209

210 211
	const unsigned frame_size = (unsigned)
		afGetVirtualFrameSize(fh, AF_DEFAULT_TRACK, true);
Warren Dukes's avatar
Warren Dukes committed
212

213
	client.Ready(audio_format, true, total_time);
214

215
	DecoderCommand cmd;
216
	do {
217 218 219 220 221 222 223
		/* 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)
224 225
			break;

226 227 228
		cmd = client.SubmitData(nullptr,
					chunk, nframes * frame_size,
					kbit_rate);
229

230
		if (cmd == DecoderCommand::SEEK) {
231
			AFframecount frame = client.GetSeekFrame();
232
			afSeekFrame(fh, AF_DEFAULT_TRACK, frame);
233

234
			client.CommandFinished();
235
			cmd = DecoderCommand::NONE;
236
		}
237
	} while (cmd == DecoderCommand::NONE);
Warren Dukes's avatar
Warren Dukes committed
238 239
}

240
gcc_pure
241
static SignedSongTime
242
audiofile_get_duration(InputStream &is)
Avuton Olrich's avatar
Avuton Olrich committed
243
{
244
	if (!is.IsSeekable() || !is.KnownSize())
245
		return SignedSongTime::Negative();
246 247 248 249 250

	AudioFileInputStream afis{nullptr, is};
	AFvirtualfile *vf = setup_virtual_fops(afis);
	AFfilehandle fh = afOpenVirtualFile(vf, "r", nullptr);
	if (fh == AF_NULL_FILEHANDLE)
251
		return SignedSongTime::Negative();
Avuton Olrich's avatar
Avuton Olrich committed
252

253
	const auto duration = audiofile_get_duration(fh);
254 255 256 257 258 259
	afCloseFile(fh);
	return duration;
}

static bool
audiofile_scan_stream(InputStream &is,
260
		      const TagHandler &handler, void *handler_ctx)
261
{
262 263
	const auto duration = audiofile_get_duration(is);
	if (duration.IsNegative())
264
		return false;
Warren Dukes's avatar
Warren Dukes committed
265

266
	tag_handler_invoke_duration(handler, handler_ctx, SongTime(duration));
267
	return true;
Warren Dukes's avatar
Warren Dukes committed
268 269
}

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

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

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