Initial revision 6759
[qball-mpd.git] / src / inputPlugins / _flac_common.h
blobe04e70693683a46ab6f4ea466a1e15649da1a214
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 * Common data structures and functions used by FLAC and OggFLAC
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 #ifndef _FLAC_COMMON_H
23 #define _FLAC_COMMON_H
25 #include "../inputPlugin.h"
27 #if defined(HAVE_FLAC) || defined(HAVE_OGGFLAC)
29 #include "../tag.h"
30 #include "../inputStream.h"
31 #include "../outputBuffer.h"
32 #include "../decode.h"
33 #include <FLAC/export.h>
34 #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
35 # include <FLAC/seekable_stream_decoder.h>
36 # define flac_decoder FLAC__SeekableStreamDecoder
37 # define flac_new() FLAC__seekable_stream_decoder_new()
39 # define flac_ogg_init(a,b,c,d,e,f,g,h,i,j) (0)
41 # define flac_get_decode_position(x,y) \
42 FLAC__seekable_stream_decoder_get_decode_position(x,y)
43 # define flac_get_state(x) FLAC__seekable_stream_decoder_get_state(x)
44 # define flac_process_single(x) FLAC__seekable_stream_decoder_process_single(x)
45 # define flac_process_metadata(x) \
46 FLAC__seekable_stream_decoder_process_until_end_of_metadata(x)
47 # define flac_seek_absolute(x,y) \
48 FLAC__seekable_stream_decoder_seek_absolute(x,y)
49 # define flac_finish(x) FLAC__seekable_stream_decoder_finish(x)
50 # define flac_delete(x) FLAC__seekable_stream_decoder_delete(x)
52 # define flac_decoder_eof FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM
54 typedef unsigned flac_read_status_size_t;
55 # define flac_read_status FLAC__SeekableStreamDecoderReadStatus
56 # define flac_read_status_continue \
57 FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK
58 # define flac_read_status_eof FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK
59 # define flac_read_status_abort \
60 FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR
62 # define flac_seek_status FLAC__SeekableStreamDecoderSeekStatus
63 # define flac_seek_status_ok FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK
64 # define flac_seek_status_error FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR
66 # define flac_tell_status FLAC__SeekableStreamDecoderTellStatus
67 # define flac_tell_status_ok FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK
68 # define flac_tell_status_error \
69 FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR
70 # define flac_tell_status_unsupported \
71 FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR
73 # define flac_length_status FLAC__SeekableStreamDecoderLengthStatus
74 # define flac_length_status_ok FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK
75 # define flac_length_status_error \
76 FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR
77 # define flac_length_status_unsupported \
78 FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR
80 # ifdef HAVE_OGGFLAC
81 # include <OggFLAC/seekable_stream_decoder.h>
82 # endif
83 #else /* FLAC_API_VERSION_CURRENT >= 7 */
85 /* OggFLAC support is handled by our flac_plugin already, and
86 * thus we *can* always have it if libFLAC was compiled with it */
87 # ifndef HAVE_OGGFLAC
88 # define HAVE_OGGFLAC 1
89 # endif
90 # include "_ogg_common.h"
91 # undef HAVE_OGGFLAC /* we don't need this defined anymore */
93 # include <FLAC/stream_decoder.h>
94 # define flac_decoder FLAC__StreamDecoder
95 # define flac_new() FLAC__stream_decoder_new()
97 # define flac_init(a,b,c,d,e,f,g,h,i,j) \
98 (FLAC__stream_decoder_init_stream(a,b,c,d,e,f,g,h,i,j) \
99 == FLAC__STREAM_DECODER_INIT_STATUS_OK)
100 # define flac_ogg_init(a,b,c,d,e,f,g,h,i,j) \
101 (FLAC__stream_decoder_init_ogg_stream(a,b,c,d,e,f,g,h,i,j) \
102 == FLAC__STREAM_DECODER_INIT_STATUS_OK)
104 # define flac_get_decode_position(x,y) \
105 FLAC__stream_decoder_get_decode_position(x,y)
106 # define flac_get_state(x) FLAC__stream_decoder_get_state(x)
107 # define flac_process_single(x) FLAC__stream_decoder_process_single(x)
108 # define flac_process_metadata(x) \
109 FLAC__stream_decoder_process_until_end_of_metadata(x)
110 # define flac_seek_absolute(x,y) FLAC__stream_decoder_seek_absolute(x,y)
111 # define flac_finish(x) FLAC__stream_decoder_finish(x)
112 # define flac_delete(x) FLAC__stream_decoder_delete(x)
114 # define flac_decoder_eof FLAC__STREAM_DECODER_END_OF_STREAM
116 typedef size_t flac_read_status_size_t;
117 # define flac_read_status FLAC__StreamDecoderReadStatus
118 # define flac_read_status_continue \
119 FLAC__STREAM_DECODER_READ_STATUS_CONTINUE
120 # define flac_read_status_eof FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM
121 # define flac_read_status_abort FLAC__STREAM_DECODER_READ_STATUS_ABORT
123 # define flac_seek_status FLAC__StreamDecoderSeekStatus
124 # define flac_seek_status_ok FLAC__STREAM_DECODER_SEEK_STATUS_OK
125 # define flac_seek_status_error FLAC__STREAM_DECODER_SEEK_STATUS_ERROR
126 # define flac_seek_status_unsupported \
127 FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED
129 # define flac_tell_status FLAC__StreamDecoderTellStatus
130 # define flac_tell_status_ok FLAC__STREAM_DECODER_TELL_STATUS_OK
131 # define flac_tell_status_error FLAC__STREAM_DECODER_TELL_STATUS_ERROR
132 # define flac_tell_status_unsupported \
133 FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED
135 # define flac_length_status FLAC__StreamDecoderLengthStatus
136 # define flac_length_status_ok FLAC__STREAM_DECODER_LENGTH_STATUS_OK
137 # define flac_length_status_error FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR
138 # define flac_length_status_unsupported \
139 FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED
141 #endif /* FLAC_API_VERSION_CURRENT >= 7 */
143 #include <FLAC/metadata.h>
145 #define FLAC_CHUNK_SIZE 4080
147 typedef struct {
148 unsigned char chunk[FLAC_CHUNK_SIZE];
149 int chunk_length;
150 float time;
151 int bitRate;
152 FLAC__uint64 position;
153 OutputBuffer *cb;
154 DecoderControl *dc;
155 InputStream *inStream;
156 ReplayGainInfo *replayGainInfo;
157 MpdTag *tag;
158 } FlacData;
160 /* initializes a given FlacData struct */
161 void init_FlacData(FlacData * data, OutputBuffer * cb,
162 DecoderControl * dc, InputStream * inStream);
163 void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
164 FlacData * data);
165 void flac_error_common_cb(const char *plugin,
166 FLAC__StreamDecoderErrorStatus status,
167 FlacData * data);
169 MpdTag *copyVorbisCommentBlockToMpdTag(const FLAC__StreamMetadata * block,
170 MpdTag * tag);
172 /* keep this inlined, this is just macro but prettier :) */
173 static inline int flacSendChunk(FlacData * data)
175 if (sendDataToOutputBuffer(data->cb, NULL, data->dc, 1, data->chunk,
176 data->chunk_length, data->time,
177 data->bitRate,
178 data->replayGainInfo) ==
179 OUTPUT_BUFFER_DC_STOP)
180 return -1;
182 return 0;
185 #endif /* HAVE_FLAC || HAVE_OGGFLAC */
187 #endif /* _FLAC_COMMON_H */