Initial revision 6759
[qball-mpd.git] / src / inputPlugins / .svn / text-base / oggflac_plugin.c.svn-base
blob58eb0a5f72c30efb025eed1e7126617e80b49fa8
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
4  *
5  * OggFLAC support (half-stolen from flac_plugin.c :))
6  * (c) 2005 by Eric Wong <normalperson@yhbt.net>
7  *
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.
12  *
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
20  */
22 #include "_flac_common.h"
24 #ifdef HAVE_OGGFLAC
26 #include "_ogg_common.h"
28 #include "../utils.h"
29 #include "../log.h"
30 #include "../pcm_utils.h"
31 #include "../inputStream.h"
32 #include "../outputBuffer.h"
33 #include "../replayGain.h"
34 #include "../audio.h"
36 #include <stdio.h>
37 #include <string.h>
38 #include <unistd.h>
40 static void oggflac_cleanup(InputStream * inStream,
41                             FlacData * data,
42                             OggFLAC__SeekableStreamDecoder * decoder)
44         if (data->replayGainInfo)
45                 freeReplayGainInfo(data->replayGainInfo);
46         if (decoder)
47                 OggFLAC__seekable_stream_decoder_delete(decoder);
48         closeInputStream(inStream);
51 static OggFLAC__SeekableStreamDecoderReadStatus of_read_cb(const
52                                                            OggFLAC__SeekableStreamDecoder
53                                                            * decoder,
54                                                            FLAC__byte buf[],
55                                                            unsigned *bytes,
56                                                            void *fdata)
58         FlacData *data = (FlacData *) fdata;
59         size_t r;
61         while (1) {
62                 r = readFromInputStream(data->inStream, (void *)buf, 1, *bytes);
63                 if (r == 0 && !inputStreamAtEOF(data->inStream) &&
64                     !data->dc->stop)
65                         my_usleep(10000);
66                 else
67                         break;
68         }
69         *bytes = r;
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
79                                                            * decoder,
80                                                            FLAC__uint64 offset,
81                                                            void *fdata)
83         FlacData *data = (FlacData *) fdata;
85         if (seekInputStream(data->inStream, offset, SEEK_SET) < 0) {
86                 return OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
87         }
89         return OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
92 static OggFLAC__SeekableStreamDecoderTellStatus of_tell_cb(const
93                                                            OggFLAC__SeekableStreamDecoder
94                                                            * decoder,
95                                                            FLAC__uint64 *
96                                                            offset, void *fdata)
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
107                                                                * decoder,
108                                                                FLAC__uint64 *
109                                                                length,
110                                                                void *fdata)
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,
120                             void *fdata)
122         FlacData *data = (FlacData *) fdata;
124         if (inputStreamAtEOF(data->inStream) == 1)
125                 return true;
126         return false;
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)
137         switch (state) {
138         case OggFLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
139                 ERROR("oggflac allocation error\n");
140                 break;
141         case OggFLAC__SEEKABLE_STREAM_DECODER_READ_ERROR:
142                 ERROR("oggflac read error\n");
143                 break;
144         case OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR:
145                 ERROR("oggflac seek error\n");
146                 break;
147         case OggFLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR:
148                 ERROR("oggflac seekable stream error\n");
149                 break;
150         case OggFLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED:
151                 ERROR("oggflac decoder already initialized\n");
152                 break;
153         case OggFLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK:
154                 ERROR("invalid oggflac callback\n");
155                 break;
156         case OggFLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED:
157                 ERROR("oggflac decoder uninitialized\n");
158                 break;
159         case OggFLAC__SEEKABLE_STREAM_DECODER_OK:
160         case OggFLAC__SEEKABLE_STREAM_DECODER_SEEKING:
161         case OggFLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM:
162                 break;
163         }
166 static FLAC__StreamDecoderWriteStatus oggflacWrite(const
167                                                    OggFLAC__SeekableStreamDecoder
168                                                    * decoder,
169                                                    const FLAC__Frame * frame,
170                                                    const FLAC__int32 *
171                                                    const buf[], void *vdata)
173         FlacData *data = (FlacData *) vdata;
174         FLAC__uint32 samples = frame->header.blocksize;
175         FLAC__uint16 u16;
176         unsigned char *uc;
177         int c_samp, c_chan, d_samp;
178         int i;
179         float timeChange;
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
188          *
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;
194          */
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) {
204                                                 return
205                                                     FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
206                                         }
207                                         data->chunk_length = 0;
208                                         if (data->dc->seek) {
209                                                 return
210                                                     FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
211                                         }
212                                 }
213                                 data->chunk[data->chunk_length++] = *(uc++);
214                         }
215                 }
216         }
218         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
221 /* used by TagDup */
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:
229                 if (!data->tag)
230                         data->tag = newMpdTag();
231                 data->tag->time = ((float)block->data.stream_info.
232                                    total_samples) /
233                     block->data.stream_info.sample_rate + 0.5;
234                 return;
235         case FLAC__METADATA_TYPE_VORBIS_COMMENT:
236                 copyVorbisCommentBlockToMpdTag(block, data->tag);
237         default:
238                 break;
239         }
242 /* used by decode */
243 static void of_metadata_decode_cb(const OggFLAC__SeekableStreamDecoder * dec,
244                                   const FLAC__StreamMetadata * block,
245                                   void *vdata)
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;
255         unsigned int s = 1;
257         if (!(decoder = OggFLAC__seekable_stream_decoder_new()))
258                 return NULL;
260         if (metadata_only) {
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);
265         } else {
266                 s &= OggFLAC__seekable_stream_decoder_set_metadata_callback
267                     (decoder, of_metadata_decode_cb);
268         }
270         s &= OggFLAC__seekable_stream_decoder_set_read_callback(decoder,
271                                                                 of_read_cb);
272         s &= OggFLAC__seekable_stream_decoder_set_seek_callback(decoder,
273                                                                 of_seek_cb);
274         s &= OggFLAC__seekable_stream_decoder_set_tell_callback(decoder,
275                                                                 of_tell_cb);
276         s &= OggFLAC__seekable_stream_decoder_set_length_callback(decoder,
277                                                                   of_length_cb);
278         s &= OggFLAC__seekable_stream_decoder_set_eof_callback(decoder,
279                                                                of_EOF_cb);
280         s &= OggFLAC__seekable_stream_decoder_set_write_callback(decoder,
281                                                                  oggflacWrite);
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,
285                                                                  of_error_cb);
286         s &= OggFLAC__seekable_stream_decoder_set_client_data(decoder,
287                                                               (void *)data);
289         if (!s) {
290                 ERROR("oggflac problem before init()\n");
291                 goto fail;
292         }
293         if (OggFLAC__seekable_stream_decoder_init(decoder) !=
294             OggFLAC__SEEKABLE_STREAM_DECODER_OK) {
295                 ERROR("oggflac problem doing init()\n");
296                 goto fail;
297         }
298         if (!OggFLAC__seekable_stream_decoder_process_until_end_of_metadata
299             (decoder)) {
300                 ERROR("oggflac problem reading metadata\n");
301                 goto fail;
302         }
304         return decoder;
306 fail:
307         oggflacPrintErroredState(OggFLAC__seekable_stream_decoder_get_state
308                                  (decoder));
309         OggFLAC__seekable_stream_decoder_delete(decoder);
310         return NULL;
313 /* public functions: */
314 static MpdTag *oggflac_TagDup(char *file)
316         InputStream inStream;
317         OggFLAC__SeekableStreamDecoder *decoder;
318         FlacData data;
320         if (openInputStream(&inStream, file) < 0)
321                 return NULL;
322         if (ogg_stream_type_detect(&inStream) != FLAC) {
323                 closeInputStream(&inStream);
324                 return NULL;
325         }
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);
335         return data.tag;
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;
347         FlacData data;
348         int ret = 0;
350         init_FlacData(&data, cb, dc, inStream);
352         if (!(decoder = full_decoder_init_and_read_metadata(&data, 0))) {
353                 ret = -1;
354                 goto fail;
355         }
357         dc->state = DECODE_STATE_DECODE;
359         while (1) {
360                 OggFLAC__seekable_stream_decoder_process_single(decoder);
361                 if (OggFLAC__seekable_stream_decoder_get_state(decoder) !=
362                     OggFLAC__SEEKABLE_STREAM_DECODER_OK) {
363                         break;
364                 }
365                 if (dc->seek) {
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;
373                                 data.position = 0;
374                         } else
375                                 dc->seekError = 1;
376                         dc->seek = 0;
377                 }
378         }
380         if (!dc->stop) {
381                 oggflacPrintErroredState
382                     (OggFLAC__seekable_stream_decoder_get_state(decoder));
383                 OggFLAC__seekable_stream_decoder_finish(decoder);
384         }
385         /* send last little bit */
386         if (data.chunk_length > 0 && !dc->stop) {
387                 flacSendChunk(&data);
388                 flushOutputBuffer(data.cb);
389         }
391         dc->state = DECODE_STATE_STOP;
392         dc->stop = 0;
394 fail:
395         oggflac_cleanup(inStream, &data, decoder);
397         return ret;
400 static char *oggflac_Suffixes[] = { "ogg", NULL };
401 static char *oggflac_mime_types[] = { "audio/x-flac+ogg",
402                                       "application/ogg",
403                                       "application/x-ogg",
404                                       NULL };
406 InputPlugin oggflacPlugin = {
407         "oggflac",
408         NULL,
409         NULL,
410         oggflac_try_decode,
411         oggflac_decode,
412         NULL,
413         oggflac_TagDup,
414         INPUT_PLUGIN_STREAM_URL | INPUT_PLUGIN_STREAM_FILE,
415         oggflac_Suffixes,
416         oggflac_mime_types
419 #else /* !HAVE_FLAC */
421 InputPlugin oggflacPlugin;
423 #endif /* HAVE_OGGFLAC */