1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * OggFLAC support (half-stolen from flac_plugin.c :))
6 * (c) 2005 by Eric Wong <normalperson@yhbt.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "_flac_common.h"
26 #include "_ogg_common.h"
30 #include "../pcm_utils.h"
31 #include "../inputStream.h"
32 #include "../outputBuffer.h"
33 #include "../replayGain.h"
40 static void oggflac_cleanup(InputStream * inStream,
42 OggFLAC__SeekableStreamDecoder * decoder)
44 if (data->replayGainInfo)
45 freeReplayGainInfo(data->replayGainInfo);
47 OggFLAC__seekable_stream_decoder_delete(decoder);
48 closeInputStream(inStream);
51 static OggFLAC__SeekableStreamDecoderReadStatus of_read_cb(const
52 OggFLAC__SeekableStreamDecoder
58 FlacData *data = (FlacData *) fdata;
62 r = readFromInputStream(data->inStream, (void *)buf, 1, *bytes);
63 if (r == 0 && !inputStreamAtEOF(data->inStream) &&
71 if (r == 0 && !inputStreamAtEOF(data->inStream) && !data->dc->stop)
72 return OggFLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
74 return OggFLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
77 static OggFLAC__SeekableStreamDecoderSeekStatus of_seek_cb(const
78 OggFLAC__SeekableStreamDecoder
83 FlacData *data = (FlacData *) fdata;
85 if (seekInputStream(data->inStream, offset, SEEK_SET) < 0) {
86 return OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
89 return OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
92 static OggFLAC__SeekableStreamDecoderTellStatus of_tell_cb(const
93 OggFLAC__SeekableStreamDecoder
98 FlacData *data = (FlacData *) fdata;
100 *offset = (long)(data->inStream->offset);
102 return OggFLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
105 static OggFLAC__SeekableStreamDecoderLengthStatus of_length_cb(const
106 OggFLAC__SeekableStreamDecoder
112 FlacData *data = (FlacData *) fdata;
114 *length = (size_t) (data->inStream->size);
116 return OggFLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
119 static FLAC__bool of_EOF_cb(const OggFLAC__SeekableStreamDecoder * decoder,
122 FlacData *data = (FlacData *) fdata;
124 if (inputStreamAtEOF(data->inStream) == 1)
129 static void of_error_cb(const OggFLAC__SeekableStreamDecoder * decoder,
130 FLAC__StreamDecoderErrorStatus status, void *fdata)
132 flac_error_common_cb("oggflac", status, (FlacData *) fdata);
135 static void oggflacPrintErroredState(OggFLAC__SeekableStreamDecoderState state)
138 case OggFLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
139 ERROR("oggflac allocation error\n");
141 case OggFLAC__SEEKABLE_STREAM_DECODER_READ_ERROR:
142 ERROR("oggflac read error\n");
144 case OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR:
145 ERROR("oggflac seek error\n");
147 case OggFLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR:
148 ERROR("oggflac seekable stream error\n");
150 case OggFLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED:
151 ERROR("oggflac decoder already initialized\n");
153 case OggFLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK:
154 ERROR("invalid oggflac callback\n");
156 case OggFLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED:
157 ERROR("oggflac decoder uninitialized\n");
159 case OggFLAC__SEEKABLE_STREAM_DECODER_OK:
160 case OggFLAC__SEEKABLE_STREAM_DECODER_SEEKING:
161 case OggFLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM:
166 static FLAC__StreamDecoderWriteStatus oggflacWrite(const
167 OggFLAC__SeekableStreamDecoder
169 const FLAC__Frame * frame,
171 const buf[], void *vdata)
173 FlacData *data = (FlacData *) vdata;
174 FLAC__uint32 samples = frame->header.blocksize;
177 int c_samp, c_chan, d_samp;
181 timeChange = ((float)samples) / frame->header.sample_rate;
182 data->time += timeChange;
184 /* ogg123 uses a complicated method of calculating bitrate
185 * with averaging which I'm not too fond of.
186 * (waste of memory/CPU cycles, especially given this is _lossless_)
187 * a get_decode_position() is not available in OggFLAC, either
189 * this does not give an accurate bitrate:
190 * (bytes_last_read was set in the read callback)
191 data->bitRate = ((8.0 * data->bytes_last_read *
192 frame->header.sample_rate)
193 /((float)samples * 1000)) + 0.5;
196 for (c_samp = d_samp = 0; c_samp < frame->header.blocksize; c_samp++) {
197 for (c_chan = 0; c_chan < frame->header.channels;
198 c_chan++, d_samp++) {
199 u16 = buf[c_chan][c_samp];
200 uc = (unsigned char *)&u16;
201 for (i = 0; i < (data->dc->audioFormat.bits / 8); i++) {
202 if (data->chunk_length >= FLAC_CHUNK_SIZE) {
203 if (flacSendChunk(data) < 0) {
205 FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
207 data->chunk_length = 0;
208 if (data->dc->seek) {
210 FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
213 data->chunk[data->chunk_length++] = *(uc++);
218 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
222 static void of_metadata_dup_cb(const OggFLAC__SeekableStreamDecoder * decoder,
223 const FLAC__StreamMetadata * block, void *vdata)
225 FlacData *data = (FlacData *) vdata;
227 switch (block->type) {
228 case FLAC__METADATA_TYPE_STREAMINFO:
230 data->tag = newMpdTag();
231 data->tag->time = ((float)block->data.stream_info.
233 block->data.stream_info.sample_rate + 0.5;
235 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
236 copyVorbisCommentBlockToMpdTag(block, data->tag);
243 static void of_metadata_decode_cb(const OggFLAC__SeekableStreamDecoder * dec,
244 const FLAC__StreamMetadata * block,
247 flac_metadata_common_cb(block, (FlacData *) vdata);
250 static OggFLAC__SeekableStreamDecoder
251 * full_decoder_init_and_read_metadata(FlacData * data,
252 unsigned int metadata_only)
254 OggFLAC__SeekableStreamDecoder *decoder = NULL;
257 if (!(decoder = OggFLAC__seekable_stream_decoder_new()))
261 s &= OggFLAC__seekable_stream_decoder_set_metadata_callback
262 (decoder, of_metadata_dup_cb);
263 s &= OggFLAC__seekable_stream_decoder_set_metadata_respond
264 (decoder, FLAC__METADATA_TYPE_STREAMINFO);
266 s &= OggFLAC__seekable_stream_decoder_set_metadata_callback
267 (decoder, of_metadata_decode_cb);
270 s &= OggFLAC__seekable_stream_decoder_set_read_callback(decoder,
272 s &= OggFLAC__seekable_stream_decoder_set_seek_callback(decoder,
274 s &= OggFLAC__seekable_stream_decoder_set_tell_callback(decoder,
276 s &= OggFLAC__seekable_stream_decoder_set_length_callback(decoder,
278 s &= OggFLAC__seekable_stream_decoder_set_eof_callback(decoder,
280 s &= OggFLAC__seekable_stream_decoder_set_write_callback(decoder,
282 s &= OggFLAC__seekable_stream_decoder_set_metadata_respond(decoder,
283 FLAC__METADATA_TYPE_VORBIS_COMMENT);
284 s &= OggFLAC__seekable_stream_decoder_set_error_callback(decoder,
286 s &= OggFLAC__seekable_stream_decoder_set_client_data(decoder,
290 ERROR("oggflac problem before init()\n");
293 if (OggFLAC__seekable_stream_decoder_init(decoder) !=
294 OggFLAC__SEEKABLE_STREAM_DECODER_OK) {
295 ERROR("oggflac problem doing init()\n");
298 if (!OggFLAC__seekable_stream_decoder_process_until_end_of_metadata
300 ERROR("oggflac problem reading metadata\n");
307 oggflacPrintErroredState(OggFLAC__seekable_stream_decoder_get_state
309 OggFLAC__seekable_stream_decoder_delete(decoder);
313 /* public functions: */
314 static MpdTag *oggflac_TagDup(char *file)
316 InputStream inStream;
317 OggFLAC__SeekableStreamDecoder *decoder;
320 if (openInputStream(&inStream, file) < 0)
322 if (ogg_stream_type_detect(&inStream) != FLAC) {
323 closeInputStream(&inStream);
327 init_FlacData(&data, NULL, NULL, &inStream);
329 /* errors here won't matter,
330 * data.tag will be set or unset, that's all we care about */
331 decoder = full_decoder_init_and_read_metadata(&data, 1);
333 oggflac_cleanup(&inStream, &data, decoder);
338 static unsigned int oggflac_try_decode(InputStream * inStream)
340 return (ogg_stream_type_detect(inStream) == FLAC) ? 1 : 0;
343 static int oggflac_decode(OutputBuffer * cb, DecoderControl * dc,
344 InputStream * inStream)
346 OggFLAC__SeekableStreamDecoder *decoder = NULL;
350 init_FlacData(&data, cb, dc, inStream);
352 if (!(decoder = full_decoder_init_and_read_metadata(&data, 0))) {
357 dc->state = DECODE_STATE_DECODE;
360 OggFLAC__seekable_stream_decoder_process_single(decoder);
361 if (OggFLAC__seekable_stream_decoder_get_state(decoder) !=
362 OggFLAC__SEEKABLE_STREAM_DECODER_OK) {
366 FLAC__uint64 sampleToSeek = dc->seekWhere *
367 dc->audioFormat.sampleRate + 0.5;
368 if (OggFLAC__seekable_stream_decoder_seek_absolute
369 (decoder, sampleToSeek)) {
370 clearOutputBuffer(cb);
371 data.time = ((float)sampleToSeek) /
372 dc->audioFormat.sampleRate;
381 oggflacPrintErroredState
382 (OggFLAC__seekable_stream_decoder_get_state(decoder));
383 OggFLAC__seekable_stream_decoder_finish(decoder);
385 /* send last little bit */
386 if (data.chunk_length > 0 && !dc->stop) {
387 flacSendChunk(&data);
388 flushOutputBuffer(data.cb);
391 dc->state = DECODE_STATE_STOP;
395 oggflac_cleanup(inStream, &data, decoder);
400 static char *oggflac_Suffixes[] = { "ogg", NULL };
401 static char *oggflac_mime_types[] = { "audio/x-flac+ogg",
406 InputPlugin oggflacPlugin = {
414 INPUT_PLUGIN_STREAM_URL | INPUT_PLUGIN_STREAM_FILE,
419 #else /* !HAVE_FLAC */
421 InputPlugin oggflacPlugin;
423 #endif /* HAVE_OGGFLAC */