flac_plugin.c 13.4 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
Warren Dukes's avatar
Warren Dukes committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

19
#include "_flac_common.h"
Warren Dukes's avatar
Warren Dukes committed
20

21 22
#include <glib.h>

23
#include <assert.h>
24
#include <unistd.h>
25

26
/* this code was based on flac123, from flac-tools */
Warren Dukes's avatar
Warren Dukes committed
27

Max Kellermann's avatar
Max Kellermann committed
28 29 30 31
static flac_read_status
flac_read_cb(G_GNUC_UNUSED const flac_decoder *fd,
	     FLAC__byte buf[], flac_read_status_size_t *bytes,
	     void *fdata)
Avuton Olrich's avatar
Avuton Olrich committed
32
{
Max Kellermann's avatar
Max Kellermann committed
33
	struct flac_data *data = fdata;
34 35
	size_t r;

Max Kellermann's avatar
Max Kellermann committed
36 37
	r = decoder_read(data->decoder, data->input_stream,
			 (void *)buf, *bytes);
38
	*bytes = r;
Avuton Olrich's avatar
Avuton Olrich committed
39

40 41
	if (r == 0) {
		if (decoder_get_command(data->decoder) != DECODE_COMMAND_NONE ||
Max Kellermann's avatar
Max Kellermann committed
42
		    input_stream_eof(data->input_stream))
43
			return flac_read_status_eof;
44
		else
45
			return flac_read_status_abort;
46
	}
47

48
	return flac_read_status_continue;
49 50
}

Max Kellermann's avatar
Max Kellermann committed
51 52 53
static flac_seek_status
flac_seek_cb(G_GNUC_UNUSED const flac_decoder *fd,
	     FLAC__uint64 offset, void *fdata)
54
{
Max Kellermann's avatar
Max Kellermann committed
55
	struct flac_data *data = (struct flac_data *) fdata;
56

Max Kellermann's avatar
Max Kellermann committed
57
	if (!input_stream_seek(data->input_stream, offset, SEEK_SET))
58
		return flac_seek_status_error;
59

60
	return flac_seek_status_ok;
61 62
}

Max Kellermann's avatar
Max Kellermann committed
63 64 65
static flac_tell_status
flac_tell_cb(G_GNUC_UNUSED const flac_decoder *fd,
	     FLAC__uint64 * offset, void *fdata)
66
{
Max Kellermann's avatar
Max Kellermann committed
67
	struct flac_data *data = (struct flac_data *) fdata;
68

Max Kellermann's avatar
Max Kellermann committed
69
	*offset = (long)(data->input_stream->offset);
70

71
	return flac_tell_status_ok;
72 73
}

Max Kellermann's avatar
Max Kellermann committed
74 75 76
static flac_length_status
flac_length_cb(G_GNUC_UNUSED const flac_decoder *fd,
	       FLAC__uint64 * length, void *fdata)
77
{
Max Kellermann's avatar
Max Kellermann committed
78
	struct flac_data *data = (struct flac_data *) fdata;
79

Max Kellermann's avatar
Max Kellermann committed
80
	if (data->input_stream->size < 0)
81 82
		return flac_length_status_unsupported;

Max Kellermann's avatar
Max Kellermann committed
83
	*length = (size_t) (data->input_stream->size);
84

85
	return flac_length_status_ok;
86 87
}

Max Kellermann's avatar
Max Kellermann committed
88 89
static FLAC__bool
flac_eof_cb(G_GNUC_UNUSED const flac_decoder *fd, void *fdata)
Avuton Olrich's avatar
Avuton Olrich committed
90
{
Max Kellermann's avatar
Max Kellermann committed
91
	struct flac_data *data = (struct flac_data *) fdata;
Avuton Olrich's avatar
Avuton Olrich committed
92

93 94
	return (decoder_get_command(data->decoder) != DECODE_COMMAND_NONE &&
		decoder_get_command(data->decoder) != DECODE_COMMAND_SEEK) ||
Max Kellermann's avatar
Max Kellermann committed
95
		input_stream_eof(data->input_stream);
96 97
}

Max Kellermann's avatar
Max Kellermann committed
98 99 100
static void
flac_error_cb(G_GNUC_UNUSED const flac_decoder *fd,
	      FLAC__StreamDecoderErrorStatus status, void *fdata)
101
{
Max Kellermann's avatar
Max Kellermann committed
102
	flac_error_common_cb("flac", status, (struct flac_data *) fdata);
Warren Dukes's avatar
Warren Dukes committed
103 104
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
static void flacPrintErroredState(FLAC__SeekableStreamDecoderState state)
{
	const char *str = ""; /* "" to silence compiler warning */
	switch (state) {
	case FLAC__SEEKABLE_STREAM_DECODER_OK:
	case FLAC__SEEKABLE_STREAM_DECODER_SEEKING:
	case FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM:
		return;
	case FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
		str = "allocation error";
		break;
	case FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR:
		str = "read error";
		break;
	case FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR:
		str = "seek error";
		break;
	case FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR:
		str = "seekable stream error";
		break;
	case FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED:
		str = "decoder already initialized";
		break;
	case FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK:
		str = "invalid callback";
		break;
	case FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED:
		str = "decoder uninitialized";
	}
135 136

	g_warning("%s\n", str);
137 138
}

139 140 141 142 143 144 145 146 147 148 149
static bool
flac_init(FLAC__SeekableStreamDecoder *dec,
	  FLAC__SeekableStreamDecoderReadCallback read_cb,
	  FLAC__SeekableStreamDecoderSeekCallback seek_cb,
	  FLAC__SeekableStreamDecoderTellCallback tell_cb,
	  FLAC__SeekableStreamDecoderLengthCallback length_cb,
	  FLAC__SeekableStreamDecoderEofCallback eof_cb,
	  FLAC__SeekableStreamDecoderWriteCallback write_cb,
	  FLAC__SeekableStreamDecoderMetadataCallback metadata_cb,
	  FLAC__SeekableStreamDecoderErrorCallback error_cb,
	  void *data)
150
{
151 152 153 154 155 156 157 158 159 160 161
	return FLAC__seekable_stream_decoder_set_read_callback(dec, read_cb) &&
		FLAC__seekable_stream_decoder_set_seek_callback(dec, seek_cb) &&
		FLAC__seekable_stream_decoder_set_tell_callback(dec, tell_cb) &&
		FLAC__seekable_stream_decoder_set_length_callback(dec, length_cb) &&
		FLAC__seekable_stream_decoder_set_eof_callback(dec, eof_cb) &&
		FLAC__seekable_stream_decoder_set_write_callback(dec, write_cb) &&
		FLAC__seekable_stream_decoder_set_metadata_callback(dec, metadata_cb) &&
		FLAC__seekable_stream_decoder_set_metadata_respond(dec, FLAC__METADATA_TYPE_VORBIS_COMMENT) &&
		FLAC__seekable_stream_decoder_set_error_callback(dec, error_cb) &&
		FLAC__seekable_stream_decoder_set_client_data(dec, data) &&
		FLAC__seekable_stream_decoder_init(dec) == FLAC__SEEKABLE_STREAM_DECODER_OK;
162 163
}
#else /* FLAC_API_VERSION_CURRENT >= 7 */
164
static void flacPrintErroredState(FLAC__StreamDecoderState state)
165
{
166
	const char *str = ""; /* "" to silence compiler warning */
Avuton Olrich's avatar
Avuton Olrich committed
167
	switch (state) {
168 169 170 171 172 173 174 175
	case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
	case FLAC__STREAM_DECODER_READ_METADATA:
	case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
	case FLAC__STREAM_DECODER_READ_FRAME:
	case FLAC__STREAM_DECODER_END_OF_STREAM:
		return;
	case FLAC__STREAM_DECODER_OGG_ERROR:
		str = "error in the Ogg layer";
176
		break;
177 178
	case FLAC__STREAM_DECODER_SEEK_ERROR:
		str = "seek error";
Warren Dukes's avatar
Warren Dukes committed
179
		break;
180 181
	case FLAC__STREAM_DECODER_ABORTED:
		str = "decoder aborted by read";
Warren Dukes's avatar
Warren Dukes committed
182
		break;
183 184
	case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
		str = "allocation error";
Warren Dukes's avatar
Warren Dukes committed
185
		break;
186 187
	case FLAC__STREAM_DECODER_UNINITIALIZED:
		str = "decoder uninitialized";
Warren Dukes's avatar
Warren Dukes committed
188
	}
189 190

	g_warning("%s\n", str);
Warren Dukes's avatar
Warren Dukes committed
191
}
192
#endif /* FLAC_API_VERSION_CURRENT >= 7 */
Warren Dukes's avatar
Warren Dukes committed
193

194
static void flacMetadata(G_GNUC_UNUSED const flac_decoder * dec,
Avuton Olrich's avatar
Avuton Olrich committed
195
			 const FLAC__StreamMetadata * block, void *vdata)
196
{
Max Kellermann's avatar
Max Kellermann committed
197
	flac_metadata_common_cb(block, (struct flac_data *) vdata);
Warren Dukes's avatar
Warren Dukes committed
198 199
}

Max Kellermann's avatar
Max Kellermann committed
200 201 202
static FLAC__StreamDecoderWriteStatus
flac_write_cb(const flac_decoder *dec, const FLAC__Frame *frame,
	      const FLAC__int32 *const buf[], void *vdata)
203
{
Warren Dukes's avatar
Warren Dukes committed
204
	FLAC__uint32 samples = frame->header.blocksize;
Max Kellermann's avatar
Max Kellermann committed
205
	struct flac_data *data = (struct flac_data *) vdata;
206 207
	float timeChange;
	FLAC__uint64 newPosition = 0;
Avuton Olrich's avatar
Avuton Olrich committed
208 209 210 211

	timeChange = ((float)samples) / frame->header.sample_rate;
	data->time += timeChange;

212
	flac_get_decode_position(dec, &newPosition);
213
	if (data->position && newPosition >= data->position) {
214 215
		assert(timeChange >= 0);

Max Kellermann's avatar
Max Kellermann committed
216
		data->bit_rate =
Avuton Olrich's avatar
Avuton Olrich committed
217 218
		    ((newPosition - data->position) * 8.0 / timeChange)
		    / 1000 + 0.5;
219 220
	}
	data->position = newPosition;
Warren Dukes's avatar
Warren Dukes committed
221

222
	return flac_common_write(data, frame, buf);
Warren Dukes's avatar
Warren Dukes committed
223 224
}

225
static struct tag *
226
flac_tag_load(const char *file)
Avuton Olrich's avatar
Avuton Olrich committed
227
{
228
	struct tag *tag;
Avuton Olrich's avatar
Avuton Olrich committed
229 230
	FLAC__Metadata_SimpleIterator *it;
	FLAC__StreamMetadata *block = NULL;
Warren Dukes's avatar
Warren Dukes committed
231 232

	it = FLAC__metadata_simple_iterator_new();
Avuton Olrich's avatar
Avuton Olrich committed
233
	if (!FLAC__metadata_simple_iterator_init(it, file, 1, 0)) {
234 235 236 237 238 239
		const char *err;
		FLAC_API FLAC__Metadata_SimpleIteratorStatus s;

		s = FLAC__metadata_simple_iterator_status(it);

		switch (s) { /* slightly more human-friendly messages: */
Avuton Olrich's avatar
Avuton Olrich committed
240
		case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT:
241
			err = "illegal input";
Avuton Olrich's avatar
Avuton Olrich committed
242 243
			break;
		case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE:
244
			err = "error opening file";
Avuton Olrich's avatar
Avuton Olrich committed
245 246
			break;
		case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE:
247
			err = "not a FLAC file";
Avuton Olrich's avatar
Avuton Olrich committed
248 249
			break;
		default:
250
			err = FLAC__Metadata_SimpleIteratorStatusString[s];
251
		}
252 253
		g_debug("Reading '%s' metadata gave the following error: %s\n",
			file, err);
Warren Dukes's avatar
Warren Dukes committed
254
		FLAC__metadata_simple_iterator_delete(it);
255
		return NULL;
Warren Dukes's avatar
Warren Dukes committed
256
	}
Avuton Olrich's avatar
Avuton Olrich committed
257

258
	tag = tag_new();
Warren Dukes's avatar
Warren Dukes committed
259 260
	do {
		block = FLAC__metadata_simple_iterator_get_block(it);
Avuton Olrich's avatar
Avuton Olrich committed
261 262 263
		if (!block)
			break;
		if (block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
264
			flac_vorbis_comments_to_tag(tag, block);
Avuton Olrich's avatar
Avuton Olrich committed
265
		} else if (block->type == FLAC__METADATA_TYPE_STREAMINFO) {
266
			tag->time = ((float)block->data.stream_info.total_samples) /
Avuton Olrich's avatar
Avuton Olrich committed
267
			    block->data.stream_info.sample_rate + 0.5;
Warren Dukes's avatar
Warren Dukes committed
268 269
		}
		FLAC__metadata_object_delete(block);
Avuton Olrich's avatar
Avuton Olrich committed
270
	} while (FLAC__metadata_simple_iterator_next(it));
Warren Dukes's avatar
Warren Dukes committed
271 272

	FLAC__metadata_simple_iterator_delete(it);
273 274 275 276 277 278 279

	if (!tag_is_defined(tag)) {
		tag_free(tag);
		tag = NULL;
	}

	return tag;
Warren Dukes's avatar
Warren Dukes committed
280 281
}

Max Kellermann's avatar
Max Kellermann committed
282 283
static struct tag *
flac_tag_dup(const char *file)
Avuton Olrich's avatar
Avuton Olrich committed
284
{
285
	return flac_tag_load(file);
Warren Dukes's avatar
Warren Dukes committed
286 287
}

288
static void
Max Kellermann's avatar
Max Kellermann committed
289 290
flac_decode_internal(struct decoder * decoder,
		     struct input_stream *input_stream,
291
		     bool is_ogg)
292
{
Max Kellermann's avatar
Max Kellermann committed
293 294
	flac_decoder *flac_dec;
	struct flac_data data;
295
	const char *err = NULL;
296

Max Kellermann's avatar
Max Kellermann committed
297
	if (!(flac_dec = flac_new()))
298
		return;
Max Kellermann's avatar
Max Kellermann committed
299
	flac_data_init(&data, decoder, input_stream);
300

301
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
Max Kellermann's avatar
Max Kellermann committed
302
        if(!FLAC__stream_decoder_set_metadata_respond(flac_dec, FLAC__METADATA_TYPE_VORBIS_COMMENT))
303
        {
304
                g_debug("Failed to set metadata respond\n");
305 306 307 308
        }
#endif


309
	if (is_ogg) {
Max Kellermann's avatar
Max Kellermann committed
310 311 312 313 314
		if (!flac_ogg_init(flac_dec, flac_read_cb,
				   flac_seek_cb, flac_tell_cb,
				   flac_length_cb, flac_eof_cb,
				   flac_write_cb, flacMetadata,
				   flac_error_cb, (void *)&data)) {
315 316 317 318
			err = "doing Ogg init()";
			goto fail;
		}
	} else {
Max Kellermann's avatar
Max Kellermann committed
319 320 321 322 323
		if (!flac_init(flac_dec, flac_read_cb,
			       flac_seek_cb, flac_tell_cb,
			       flac_length_cb, flac_eof_cb,
			       flac_write_cb, flacMetadata,
			       flac_error_cb, (void *)&data)) {
324 325 326
			err = "doing init()";
			goto fail;
		}
327 328
	}

Max Kellermann's avatar
Max Kellermann committed
329
	if (!flac_process_metadata(flac_dec)) {
330 331
		err = "problem reading metadata";
		goto fail;
332 333
	}

334 335 336 337 338 339 340 341
	if (!audio_format_valid(&data.audio_format)) {
		g_warning("Invalid audio format: %u:%u:%u\n",
			  data.audio_format.sample_rate,
			  data.audio_format.bits,
			  data.audio_format.channels);
		goto fail;
	}

342
	decoder_initialized(decoder, &data.audio_format,
Max Kellermann's avatar
Max Kellermann committed
343
			    input_stream->seekable, data.total_time);
344

345
	while (true) {
Max Kellermann's avatar
Max Kellermann committed
346
		if (!flac_process_single(flac_dec))
347
			break;
348
		if (decoder_get_command(decoder) == DECODE_COMMAND_SEEK) {
Max Kellermann's avatar
Max Kellermann committed
349
			FLAC__uint64 seek_sample = decoder_seek_where(decoder) *
350
			    data.audio_format.sample_rate + 0.5;
Max Kellermann's avatar
Max Kellermann committed
351 352
			if (flac_seek_absolute(flac_dec, seek_sample)) {
				data.time = ((float)seek_sample) /
353
				    data.audio_format.sample_rate;
354
				data.position = 0;
355
				decoder_command_finished(decoder);
356
			} else
357
				decoder_seek_error(decoder);
Max Kellermann's avatar
Max Kellermann committed
358
		} else if (flac_get_state(flac_dec) == flac_decoder_eof)
359
			break;
360
	}
361
	if (decoder_get_command(decoder) != DECODE_COMMAND_STOP) {
Max Kellermann's avatar
Max Kellermann committed
362 363
		flacPrintErroredState(flac_get_state(flac_dec));
		flac_finish(flac_dec);
364 365 366
	}

fail:
Max Kellermann's avatar
Max Kellermann committed
367 368
	if (data.replay_gain_info)
		replay_gain_info_free(data.replay_gain_info);
369

Max Kellermann's avatar
Max Kellermann committed
370 371
	if (flac_dec)
		flac_delete(flac_dec);
372

373 374
	if (err)
		g_warning("%s\n", err);
375 376
}

377
static void
Max Kellermann's avatar
Max Kellermann committed
378
flac_decode(struct decoder * decoder, struct input_stream *input_stream)
379
{
Max Kellermann's avatar
Max Kellermann committed
380
	flac_decode_internal(decoder, input_stream, false);
381 382
}

383 384
#ifndef HAVE_OGGFLAC

385
static bool
386
oggflac_init(G_GNUC_UNUSED const struct config_param *param)
387
{
388
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
389
	return !!FLAC_API_SUPPORTS_OGG_FLAC;
390 391 392 393
#else
	/* disable oggflac when libflac is too old */
	return false;
#endif
394 395
}

396 397
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7

Max Kellermann's avatar
Max Kellermann committed
398 399
static struct tag *
oggflac_tag_dup(const char *file)
400
{
401
	struct tag *ret = NULL;
402 403 404 405 406 407 408 409
	FLAC__Metadata_Iterator *it;
	FLAC__StreamMetadata *block;
	FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();

	if (!(FLAC__metadata_chain_read_ogg(chain, file)))
		goto out;
	it = FLAC__metadata_iterator_new();
	FLAC__metadata_iterator_init(it, chain);
410 411

	ret = tag_new();
412 413 414 415
	do {
		if (!(block = FLAC__metadata_iterator_get_block(it)))
			break;
		if (block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
416
			flac_vorbis_comments_to_tag(ret, block);
417 418 419 420 421 422 423
		} else if (block->type == FLAC__METADATA_TYPE_STREAMINFO) {
			ret->time = ((float)block->data.stream_info.
				     total_samples) /
			    block->data.stream_info.sample_rate + 0.5;
		}
	} while (FLAC__metadata_iterator_next(it));
	FLAC__metadata_iterator_delete(it);
424 425 426 427 428 429

	if (!tag_is_defined(ret)) {
		tag_free(ret);
		ret = NULL;
	}

430 431
out:
	FLAC__metadata_chain_delete(chain);
432 433 434
	return ret;
}

435
static void
Max Kellermann's avatar
Max Kellermann committed
436
oggflac_decode(struct decoder *decoder, struct input_stream *input_stream)
437
{
Max Kellermann's avatar
Max Kellermann committed
438
	if (ogg_stream_type_detect(input_stream) != FLAC)
439
		return;
440

441 442
	/* rewind the stream, because ogg_stream_type_detect() has
	   moved it */
Max Kellermann's avatar
Max Kellermann committed
443
	input_stream_seek(input_stream, 0, SEEK_SET);
444

Max Kellermann's avatar
Max Kellermann committed
445
	flac_decode_internal(decoder, input_stream, true);
446 447
}

448 449 450 451 452 453 454
static const char *const oggflac_suffixes[] = { "ogg", "oga", NULL };
static const char *const oggflac_mime_types[] = {
	"audio/x-flac+ogg",
	"application/ogg",
	"application/x-ogg",
	NULL
};
455

456 457
#endif /* FLAC_API_VERSION_CURRENT >= 7 */

Max Kellermann's avatar
Max Kellermann committed
458
const struct decoder_plugin oggflac_decoder_plugin = {
459
	.name = "oggflac",
460
	.init = oggflac_init,
461
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
462 463 464 465
	.stream_decode = oggflac_decode,
	.tag_dup = oggflac_tag_dup,
	.suffixes = oggflac_suffixes,
	.mime_types = oggflac_mime_types
466
#endif
467
};
468

469
#endif /* HAVE_OGGFLAC */
470

Max Kellermann's avatar
Max Kellermann committed
471
static const char *const flac_suffixes[] = { "flac", NULL };
472 473 474
static const char *const flac_mime_types[] = {
	"audio/x-flac", "application/x-flac", NULL
};
Warren Dukes's avatar
Warren Dukes committed
475

Max Kellermann's avatar
Max Kellermann committed
476
const struct decoder_plugin flac_decoder_plugin = {
477 478
	.name = "flac",
	.stream_decode = flac_decode,
Max Kellermann's avatar
Max Kellermann committed
479 480
	.tag_dup = flac_tag_dup,
	.suffixes = flac_suffixes,
481
	.mime_types = flac_mime_types
Warren Dukes's avatar
Warren Dukes committed
482
};