oggflac_plugin.c 10.4 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * This project's homepage is: http://www.musicpd.org
 *
 * OggFLAC support (half-stolen from flac_plugin.c :))
 * (c) 2005 by Eric Wong <normalperson@yhbt.net>
 *
 * 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
 */

22
#include "_flac_common.h"
23 24
#include "_ogg_common.h"

25
#include <glib.h>
26
#include <OggFLAC/seekable_stream_decoder.h>
27
#include <unistd.h>
28

29
static void oggflac_cleanup(FlacData * data,
Avuton Olrich's avatar
Avuton Olrich committed
30
			    OggFLAC__SeekableStreamDecoder * decoder)
31 32
{
	if (data->replayGainInfo)
33
		replay_gain_info_free(data->replayGainInfo);
34 35 36 37
	if (decoder)
		OggFLAC__seekable_stream_decoder_delete(decoder);
}

38
static OggFLAC__SeekableStreamDecoderReadStatus of_read_cb(G_GNUC_UNUSED const
Avuton Olrich's avatar
Avuton Olrich committed
39 40 41 42 43 44 45
							   OggFLAC__SeekableStreamDecoder
							   * decoder,
							   FLAC__byte buf[],
							   unsigned *bytes,
							   void *fdata)
{
	FlacData *data = (FlacData *) fdata;
46 47
	size_t r;

Max Kellermann's avatar
Max Kellermann committed
48
	r = decoder_read(data->decoder, data->inStream, (void *)buf, *bytes);
49
	*bytes = r;
Avuton Olrich's avatar
Avuton Olrich committed
50

51
	if (r == 0 && !input_stream_eof(data->inStream) &&
52
	    decoder_get_command(data->decoder) == DECODE_COMMAND_NONE)
53
		return OggFLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
Avuton Olrich's avatar
Avuton Olrich committed
54 55

	return OggFLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
56 57
}

58
static OggFLAC__SeekableStreamDecoderSeekStatus of_seek_cb(G_GNUC_UNUSED const
Avuton Olrich's avatar
Avuton Olrich committed
59 60 61 62
							   OggFLAC__SeekableStreamDecoder
							   * decoder,
							   FLAC__uint64 offset,
							   void *fdata)
63
{
Avuton Olrich's avatar
Avuton Olrich committed
64
	FlacData *data = (FlacData *) fdata;
65

66
	if (!input_stream_seek(data->inStream, offset, SEEK_SET))
Avuton Olrich's avatar
Avuton Olrich committed
67
		return OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
68

Avuton Olrich's avatar
Avuton Olrich committed
69
	return OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
70 71
}

72
static OggFLAC__SeekableStreamDecoderTellStatus of_tell_cb(G_GNUC_UNUSED const
Avuton Olrich's avatar
Avuton Olrich committed
73 74 75 76
							   OggFLAC__SeekableStreamDecoder
							   * decoder,
							   FLAC__uint64 *
							   offset, void *fdata)
77
{
Avuton Olrich's avatar
Avuton Olrich committed
78
	FlacData *data = (FlacData *) fdata;
79

Avuton Olrich's avatar
Avuton Olrich committed
80
	*offset = (long)(data->inStream->offset);
81

Avuton Olrich's avatar
Avuton Olrich committed
82
	return OggFLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
83 84
}

85
static OggFLAC__SeekableStreamDecoderLengthStatus of_length_cb(G_GNUC_UNUSED const
Avuton Olrich's avatar
Avuton Olrich committed
86 87 88 89 90
							       OggFLAC__SeekableStreamDecoder
							       * decoder,
							       FLAC__uint64 *
							       length,
							       void *fdata)
91
{
Avuton Olrich's avatar
Avuton Olrich committed
92
	FlacData *data = (FlacData *) fdata;
93

94 95 96
	if (data->inStream->size < 0)
		return OggFLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;

Avuton Olrich's avatar
Avuton Olrich committed
97
	*length = (size_t) (data->inStream->size);
98

Avuton Olrich's avatar
Avuton Olrich committed
99
	return OggFLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
100 101
}

102
static FLAC__bool of_EOF_cb(G_GNUC_UNUSED const OggFLAC__SeekableStreamDecoder * decoder,
Avuton Olrich's avatar
Avuton Olrich committed
103 104 105
			    void *fdata)
{
	FlacData *data = (FlacData *) fdata;
106

107 108
	return (decoder_get_command(data->decoder) != DECODE_COMMAND_NONE &&
		decoder_get_command(data->decoder) != DECODE_COMMAND_SEEK) ||
109
		input_stream_eof(data->inStream);
110 111
}

112
static void of_error_cb(G_GNUC_UNUSED const OggFLAC__SeekableStreamDecoder * decoder,
Avuton Olrich's avatar
Avuton Olrich committed
113
			FLAC__StreamDecoderErrorStatus status, void *fdata)
114
{
Avuton Olrich's avatar
Avuton Olrich committed
115
	flac_error_common_cb("oggflac", status, (FlacData *) fdata);
116 117
}

Avuton Olrich's avatar
Avuton Olrich committed
118
static void oggflacPrintErroredState(OggFLAC__SeekableStreamDecoderState state)
119
{
Avuton Olrich's avatar
Avuton Olrich committed
120
	switch (state) {
121
	case OggFLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
122
		g_warning("oggflac allocation error\n");
123 124
		break;
	case OggFLAC__SEEKABLE_STREAM_DECODER_READ_ERROR:
125
		g_warning("oggflac read error\n");
126 127
		break;
	case OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR:
128
		g_warning("oggflac seek error\n");
129 130
		break;
	case OggFLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR:
131
		g_warning("oggflac seekable stream error\n");
132 133
		break;
	case OggFLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED:
134
		g_warning("oggflac decoder already initialized\n");
135 136
		break;
	case OggFLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK:
137
		g_warning("invalid oggflac callback\n");
138 139
		break;
	case OggFLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED:
140
		g_warning("oggflac decoder uninitialized\n");
141 142 143 144 145 146 147 148
		break;
	case OggFLAC__SEEKABLE_STREAM_DECODER_OK:
	case OggFLAC__SEEKABLE_STREAM_DECODER_SEEKING:
	case OggFLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM:
		break;
	}
}

149
static FLAC__StreamDecoderWriteStatus oggflacWrite(G_GNUC_UNUSED const
Avuton Olrich's avatar
Avuton Olrich committed
150 151 152 153 154
						   OggFLAC__SeekableStreamDecoder
						   * decoder,
						   const FLAC__Frame * frame,
						   const FLAC__int32 *
						   const buf[], void *vdata)
155
{
Avuton Olrich's avatar
Avuton Olrich committed
156
	FlacData *data = (FlacData *) vdata;
157 158
	FLAC__uint32 samples = frame->header.blocksize;
	float timeChange;
Avuton Olrich's avatar
Avuton Olrich committed
159 160 161

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

163
	return flac_common_write(data, frame, buf);
164 165 166
}

/* used by TagDup */
167
static void of_metadata_dup_cb(G_GNUC_UNUSED const OggFLAC__SeekableStreamDecoder * decoder,
Avuton Olrich's avatar
Avuton Olrich committed
168
			       const FLAC__StreamMetadata * block, void *vdata)
169
{
Avuton Olrich's avatar
Avuton Olrich committed
170
	FlacData *data = (FlacData *) vdata;
171

Avuton Olrich's avatar
Avuton Olrich committed
172 173 174
	switch (block->type) {
	case FLAC__METADATA_TYPE_STREAMINFO:
		if (!data->tag)
175
			data->tag = tag_new();
176
		data->tag->time = ((float)block->data.stream_info.
Avuton Olrich's avatar
Avuton Olrich committed
177 178
				   total_samples) /
		    block->data.stream_info.sample_rate + 0.5;
179
		return;
Avuton Olrich's avatar
Avuton Olrich committed
180 181
	case FLAC__METADATA_TYPE_VORBIS_COMMENT:
		copyVorbisCommentBlockToMpdTag(block, data->tag);
182 183
	default:
		break;
Avuton Olrich's avatar
Avuton Olrich committed
184
	}
185 186 187
}

/* used by decode */
188
static void of_metadata_decode_cb(G_GNUC_UNUSED const OggFLAC__SeekableStreamDecoder * dec,
Avuton Olrich's avatar
Avuton Olrich committed
189 190
				  const FLAC__StreamMetadata * block,
				  void *vdata)
191
{
Avuton Olrich's avatar
Avuton Olrich committed
192
	flac_metadata_common_cb(block, (FlacData *) vdata);
193 194
}

Avuton Olrich's avatar
Avuton Olrich committed
195
static OggFLAC__SeekableStreamDecoder
Avuton Olrich's avatar
Avuton Olrich committed
196 197
    * full_decoder_init_and_read_metadata(FlacData * data,
					  unsigned int metadata_only)
198
{
Avuton Olrich's avatar
Avuton Olrich committed
199
	OggFLAC__SeekableStreamDecoder *decoder = NULL;
200 201 202 203 204 205
	unsigned int s = 1;

	if (!(decoder = OggFLAC__seekable_stream_decoder_new()))
		return NULL;

	if (metadata_only) {
Avuton Olrich's avatar
Avuton Olrich committed
206 207 208 209
		s &= OggFLAC__seekable_stream_decoder_set_metadata_callback
		    (decoder, of_metadata_dup_cb);
		s &= OggFLAC__seekable_stream_decoder_set_metadata_respond
		    (decoder, FLAC__METADATA_TYPE_STREAMINFO);
210
	} else {
Avuton Olrich's avatar
Avuton Olrich committed
211 212
		s &= OggFLAC__seekable_stream_decoder_set_metadata_callback
		    (decoder, of_metadata_decode_cb);
213
	}
Avuton Olrich's avatar
Avuton Olrich committed
214

215
	s &= OggFLAC__seekable_stream_decoder_set_read_callback(decoder,
Avuton Olrich's avatar
Avuton Olrich committed
216
								of_read_cb);
217
	s &= OggFLAC__seekable_stream_decoder_set_seek_callback(decoder,
Avuton Olrich's avatar
Avuton Olrich committed
218
								of_seek_cb);
219
	s &= OggFLAC__seekable_stream_decoder_set_tell_callback(decoder,
Avuton Olrich's avatar
Avuton Olrich committed
220
								of_tell_cb);
221
	s &= OggFLAC__seekable_stream_decoder_set_length_callback(decoder,
Avuton Olrich's avatar
Avuton Olrich committed
222
								  of_length_cb);
223
	s &= OggFLAC__seekable_stream_decoder_set_eof_callback(decoder,
Avuton Olrich's avatar
Avuton Olrich committed
224
							       of_EOF_cb);
225
	s &= OggFLAC__seekable_stream_decoder_set_write_callback(decoder,
Avuton Olrich's avatar
Avuton Olrich committed
226
								 oggflacWrite);
227
	s &= OggFLAC__seekable_stream_decoder_set_metadata_respond(decoder,
Avuton Olrich's avatar
Avuton Olrich committed
228
								   FLAC__METADATA_TYPE_VORBIS_COMMENT);
229
	s &= OggFLAC__seekable_stream_decoder_set_error_callback(decoder,
Avuton Olrich's avatar
Avuton Olrich committed
230
								 of_error_cb);
231
	s &= OggFLAC__seekable_stream_decoder_set_client_data(decoder,
Avuton Olrich's avatar
Avuton Olrich committed
232
							      (void *)data);
233 234

	if (!s) {
235
		g_warning("oggflac problem before init()\n");
236 237 238
		goto fail;
	}
	if (OggFLAC__seekable_stream_decoder_init(decoder) !=
Avuton Olrich's avatar
Avuton Olrich committed
239
	    OggFLAC__SEEKABLE_STREAM_DECODER_OK) {
240
		g_warning("oggflac problem doing init()\n");
241 242
		goto fail;
	}
Avuton Olrich's avatar
Avuton Olrich committed
243 244
	if (!OggFLAC__seekable_stream_decoder_process_until_end_of_metadata
	    (decoder)) {
245
		g_warning("oggflac problem reading metadata\n");
246 247 248
		goto fail;
	}

Avuton Olrich's avatar
Avuton Olrich committed
249
	return decoder;
250

251
fail:
Avuton Olrich's avatar
Avuton Olrich committed
252 253
	oggflacPrintErroredState(OggFLAC__seekable_stream_decoder_get_state
				 (decoder));
254 255 256 257 258
	OggFLAC__seekable_stream_decoder_delete(decoder);
	return NULL;
}

/* public functions: */
259
static struct tag *oggflac_TagDup(const char *file)
260
{
261
	struct input_stream inStream;
Avuton Olrich's avatar
Avuton Olrich committed
262
	OggFLAC__SeekableStreamDecoder *decoder;
263
	FlacData data;
Avuton Olrich's avatar
Avuton Olrich committed
264

265
	if (!input_stream_open(&inStream, file))
266 267
		return NULL;
	if (ogg_stream_type_detect(&inStream) != FLAC) {
268
		input_stream_close(&inStream);
269 270 271
		return NULL;
	}

Max Kellermann's avatar
Max Kellermann committed
272
	init_FlacData(&data, NULL, &inStream);
273 274 275

	/* errors here won't matter,
	 * data.tag will be set or unset, that's all we care about */
Avuton Olrich's avatar
Avuton Olrich committed
276 277
	decoder = full_decoder_init_and_read_metadata(&data, 1);

278
	oggflac_cleanup(&data, decoder);
279
	input_stream_close(&inStream);
Avuton Olrich's avatar
Avuton Olrich committed
280

281 282 283
	return data.tag;
}

284
static void
285
oggflac_decode(struct decoder * mpd_decoder, struct input_stream *inStream)
286
{
Avuton Olrich's avatar
Avuton Olrich committed
287
	OggFLAC__SeekableStreamDecoder *decoder = NULL;
288 289
	FlacData data;

290 291 292
	if (ogg_stream_type_detect(inStream) != FLAC)
		return;

Max Kellermann's avatar
Max Kellermann committed
293
	init_FlacData(&data, mpd_decoder, inStream);
294

Avuton Olrich's avatar
Avuton Olrich committed
295 296 297
	if (!(decoder = full_decoder_init_and_read_metadata(&data, 0))) {
		goto fail;
	}
298

299 300 301 302 303 304 305 306
	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;
	}

307 308
	decoder_initialized(mpd_decoder, &data.audio_format,
			    inStream->seekable, data.total_time);
309

310
	while (true) {
311
		OggFLAC__seekable_stream_decoder_process_single(decoder);
Avuton Olrich's avatar
Avuton Olrich committed
312 313
		if (OggFLAC__seekable_stream_decoder_get_state(decoder) !=
		    OggFLAC__SEEKABLE_STREAM_DECODER_OK) {
314 315
			break;
		}
316
		if (decoder_get_command(mpd_decoder) == DECODE_COMMAND_SEEK) {
317
			FLAC__uint64 sampleToSeek = decoder_seek_where(mpd_decoder) *
318
			    data.audio_format.sample_rate + 0.5;
Avuton Olrich's avatar
Avuton Olrich committed
319 320 321
			if (OggFLAC__seekable_stream_decoder_seek_absolute
			    (decoder, sampleToSeek)) {
				data.time = ((float)sampleToSeek) /
322
				    data.audio_format.sample_rate;
323
				data.position = 0;
324
				decoder_command_finished(mpd_decoder);
Avuton Olrich's avatar
Avuton Olrich committed
325
			} else
326
				decoder_seek_error(mpd_decoder);
327 328
		}
	}
Avuton Olrich's avatar
Avuton Olrich committed
329

330
	if (decoder_get_command(mpd_decoder) == DECODE_COMMAND_NONE) {
Avuton Olrich's avatar
Avuton Olrich committed
331 332
		oggflacPrintErroredState
		    (OggFLAC__seekable_stream_decoder_get_state(decoder));
333 334 335
		OggFLAC__seekable_stream_decoder_finish(decoder);
	}

336
fail:
337
	oggflac_cleanup(&data, decoder);
338 339
}

340 341 342 343 344 345 346
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
};
347

348
const struct decoder_plugin oggflacPlugin = {
349 350 351 352 353
	.name = "oggflac",
	.stream_decode = oggflac_decode,
	.tag_dup = oggflac_TagDup,
	.suffixes = oggflac_Suffixes,
	.mime_types = oggflac_mime_types
354
};