196e9f5ee078b977bb92bb9e4a08d5eecc334558
[platform/upstream/flac.git] / include / FLAC++ / decoder.h
1 /* libFLAC++ - Free Lossless Audio Codec library
2  * Copyright (C) 2002,2003  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the Xiph.org Foundation nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef FLACPP__DECODER_H
33 #define FLACPP__DECODER_H
34
35 #include "export.h"
36
37 #include "FLAC/file_decoder.h"
38 #include "FLAC/seekable_stream_decoder.h"
39 #include "FLAC/stream_decoder.h"
40
41
42 /** \file include/FLAC++/decoder.h
43  *
44  *  \brief
45  *  This module contains the classes which implement the various
46  *  decoders.
47  *
48  *  See the detailed documentation in the
49  *  \link flacpp_decoder decoder \endlink module.
50  */
51
52 /** \defgroup flacpp_decoder FLAC++/decoder.h: decoder classes
53  *  \ingroup flacpp
54  *
55  *  \brief
56  *  This module describes the three decoder layers provided by libFLAC++.
57  *
58  * The libFLAC++ decoder classes are object wrappers around their
59  * counterparts in libFLAC.  All three decoding layers available in
60  * libFLAC are also provided here.  The interface is very similar;
61  * make sure to read the \link flac_decoder libFLAC decoder module \endlink.
62  *
63  * The only real difference here is that instead of passing in C function
64  * pointers for callbacks, you inherit from the decoder class and provide
65  * implementations for the callbacks in the derived class; because of this
66  * there is no need for a 'client_data' property.
67  */
68
69 namespace FLAC {
70         namespace Decoder {
71
72                 // ============================================================
73                 //
74                 //  Equivalent: FLAC__StreamDecoder
75                 //
76                 // ============================================================
77
78                 /** \defgroup flacpp_stream_decoder FLAC++/decoder.h: stream decoder class
79                  *  \ingroup flacpp_decoder
80                  *
81                  *  \brief
82                  *  This class wraps the ::FLAC__StreamDecoder.
83                  *
84                  * See the \link flac_stream_decoder libFLAC stream decoder module \endlink.
85                  *
86                  * \{
87                  */
88
89                 /** This class wraps the ::FLAC__StreamDecoder.
90                  */
91                 class FLACPP_API Stream {
92                 public:
93                         class FLACPP_API State {
94                         public:
95                                 inline State(::FLAC__StreamDecoderState state): state_(state) { }
96                                 inline operator ::FLAC__StreamDecoderState() const { return state_; }
97                                 inline const char *as_cstring() const { return ::FLAC__StreamDecoderStateString[state_]; }
98                                 inline const char *resolved_as_cstring(const Stream &decoder) const { return ::FLAC__stream_decoder_get_resolved_state_string(decoder.decoder_); }
99                         protected:
100                                 ::FLAC__StreamDecoderState state_;
101                         };
102
103                         Stream();
104                         virtual ~Stream();
105
106                         bool is_valid() const;
107                         inline operator bool() const { return is_valid(); }
108
109                         bool set_metadata_respond(::FLAC__MetadataType type);
110                         bool set_metadata_respond_application(const FLAC__byte id[4]);
111                         bool set_metadata_respond_all();
112                         bool set_metadata_ignore(::FLAC__MetadataType type);
113                         bool set_metadata_ignore_application(const FLAC__byte id[4]);
114                         bool set_metadata_ignore_all();
115
116                         State get_state() const;
117                         unsigned get_channels() const;
118                         ::FLAC__ChannelAssignment get_channel_assignment() const;
119                         unsigned get_bits_per_sample() const;
120                         unsigned get_sample_rate() const;
121                         unsigned get_blocksize() const;
122
123                         /** Initialize the instance; as with the C interface,
124                          *  init() should be called after construction and 'set'
125                          *  calls but before any of the 'process' calls.
126                          */
127                         State init();
128
129                         void finish();
130
131                         bool flush();
132                         bool reset();
133
134                         bool process_single();
135                         bool process_until_end_of_metadata();
136                         bool process_until_end_of_stream();
137                 protected:
138                         virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes) = 0;
139                         virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) = 0;
140                         virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata) = 0;
141                         virtual void error_callback(::FLAC__StreamDecoderErrorStatus status) = 0;
142
143 #ifdef _MSC_VER
144                 //@@@@@@ lame hack: some MSVC versions can't see a protected decoder_ from nested State::resolved_as_cstring(); fix properly later
145                 public:
146 #endif
147                         ::FLAC__StreamDecoder *decoder_;
148                 private:
149                         static ::FLAC__StreamDecoderReadStatus read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
150                         static ::FLAC__StreamDecoderWriteStatus write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
151                         static void metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data);
152                         static void error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data);
153
154                         // Private and undefined so you can't use them:
155                         Stream(const Stream &);
156                         void operator=(const Stream &);
157                 };
158
159                 /* \} */
160
161                 // ============================================================
162                 //
163                 //  Equivalent: FLAC__SeekableStreamDecoder
164                 //
165                 // ============================================================
166
167                 /** \defgroup flacpp_seekable_stream_decoder FLAC++/decoder.h: seekable stream decoder class
168                  *  \ingroup flacpp_decoder
169                  *
170                  *  \brief
171                  *  This class wraps the ::FLAC__SeekableStreamDecoder.
172                  *
173                  * See the \link flac_seekable_stream_decoder libFLAC seekable stream decoder module \endlink.
174                  *
175                  * \{
176                  */
177
178                 /** This class wraps the ::FLAC__SeekableStreamDecoder.
179                  */
180                 class FLACPP_API SeekableStream {
181                 public:
182                         class FLACPP_API State {
183                         public:
184                                 inline State(::FLAC__SeekableStreamDecoderState state): state_(state) { }
185                                 inline operator ::FLAC__SeekableStreamDecoderState() const { return state_; }
186                                 inline const char *as_cstring() const { return ::FLAC__SeekableStreamDecoderStateString[state_]; }
187                                 inline const char *resolved_as_cstring(const SeekableStream &decoder) const { return ::FLAC__seekable_stream_decoder_get_resolved_state_string(decoder.decoder_); }
188                         protected:
189                                 ::FLAC__SeekableStreamDecoderState state_;
190                         };
191
192                         SeekableStream();
193                         virtual ~SeekableStream();
194
195                         bool is_valid() const;
196                         inline operator bool() const { return is_valid(); }
197
198                         bool set_md5_checking(bool value);
199                         bool set_metadata_respond(::FLAC__MetadataType type);
200                         bool set_metadata_respond_application(const FLAC__byte id[4]);
201                         bool set_metadata_respond_all();
202                         bool set_metadata_ignore(::FLAC__MetadataType type);
203                         bool set_metadata_ignore_application(const FLAC__byte id[4]);
204                         bool set_metadata_ignore_all();
205
206                         State get_state() const;
207                         Stream::State get_stream_decoder_state() const;
208                         bool get_md5_checking() const;
209                         unsigned get_channels() const;
210                         ::FLAC__ChannelAssignment get_channel_assignment() const;
211                         unsigned get_bits_per_sample() const;
212                         unsigned get_sample_rate() const;
213                         unsigned get_blocksize() const;
214
215                         State init();
216
217                         bool finish();
218
219                         bool flush();
220                         bool reset();
221
222                         bool process_single();
223                         bool process_until_end_of_metadata();
224                         bool process_until_end_of_stream();
225
226                         bool seek_absolute(FLAC__uint64 sample);
227                 protected:
228                         virtual ::FLAC__SeekableStreamDecoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes) = 0;
229                         virtual ::FLAC__SeekableStreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset) = 0;
230                         virtual ::FLAC__SeekableStreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset) = 0;
231                         virtual ::FLAC__SeekableStreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length) = 0;
232                         virtual bool eof_callback() = 0;
233                         virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) = 0;
234                         virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata) = 0;
235                         virtual void error_callback(::FLAC__StreamDecoderErrorStatus status) = 0;
236
237 #ifdef _MSC_VER
238                 //@@@@@@ lame hack: some MSVC versions can't see a protected decoder_ from nested State::resolved_as_cstring(); fix properly later
239                 public:
240 #endif
241                         ::FLAC__SeekableStreamDecoder *decoder_;
242                 private:
243                         static ::FLAC__SeekableStreamDecoderReadStatus read_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
244                         static ::FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
245                         static ::FLAC__SeekableStreamDecoderTellStatus tell_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
246                         static ::FLAC__SeekableStreamDecoderLengthStatus length_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
247                         static FLAC__bool eof_callback_(const ::FLAC__SeekableStreamDecoder *decoder, void *client_data);
248                         static ::FLAC__StreamDecoderWriteStatus write_callback_(const ::FLAC__SeekableStreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
249                         static void metadata_callback_(const ::FLAC__SeekableStreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data);
250                         static void error_callback_(const ::FLAC__SeekableStreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data);
251
252                         // Private and undefined so you can't use them:
253                         SeekableStream(const SeekableStream &);
254                         void operator=(const SeekableStream &);
255                 };
256
257                 /* \} */
258
259                 // ============================================================
260                 //
261                 //  Equivalent: FLAC__FileDecoder
262                 //
263                 // ============================================================
264
265                 /** \defgroup flacpp_file_decoder FLAC++/decoder.h: file decoder class
266                  *  \ingroup flacpp_decoder
267                  *
268                  *  \brief
269                  *  This class wraps the ::FLAC__FileDecoder.
270                  *
271                  * See the \link flac_file_decoder libFLAC file decoder module \endlink.
272                  *
273                  * \{
274                  */
275
276                 /** This class wraps the ::FLAC__FileDecoder.
277                  */
278                 class FLACPP_API File {
279                 public:
280                         class FLACPP_API State {
281                         public:
282                                 inline State(::FLAC__FileDecoderState state): state_(state) { }
283                                 inline operator ::FLAC__FileDecoderState() const { return state_; }
284                                 inline const char *as_cstring() const { return ::FLAC__FileDecoderStateString[state_]; }
285                                 inline const char *resolved_as_cstring(const File &decoder) const { return ::FLAC__file_decoder_get_resolved_state_string(decoder.decoder_); }
286                         protected:
287                                 ::FLAC__FileDecoderState state_;
288                         };
289
290                         File();
291                         virtual ~File();
292
293                         bool is_valid() const;
294                         inline operator bool() const { return is_valid(); }
295
296                         bool set_md5_checking(bool value);
297                         bool set_filename(const char *value); //!< 'value' may not be \c NULL; use "-" for stdin
298                         bool set_metadata_respond(::FLAC__MetadataType type);
299                         bool set_metadata_respond_application(const FLAC__byte id[4]);
300                         bool set_metadata_respond_all();
301                         bool set_metadata_ignore(::FLAC__MetadataType type);
302                         bool set_metadata_ignore_application(const FLAC__byte id[4]);
303                         bool set_metadata_ignore_all();
304
305                         State get_state() const;
306                         SeekableStream::State get_seekable_stream_decoder_state() const;
307                         Stream::State get_stream_decoder_state() const;
308                         bool get_md5_checking() const;
309                         unsigned get_channels() const;
310                         ::FLAC__ChannelAssignment get_channel_assignment() const;
311                         unsigned get_bits_per_sample() const;
312                         unsigned get_sample_rate() const;
313                         unsigned get_blocksize() const;
314
315                         State init();
316
317                         bool finish();
318
319                         bool process_single();
320                         bool process_until_end_of_metadata();
321                         bool process_until_end_of_file();
322
323                         bool seek_absolute(FLAC__uint64 sample);
324                 protected:
325                         virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) = 0;
326                         virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata) = 0;
327                         virtual void error_callback(::FLAC__StreamDecoderErrorStatus status) = 0;
328
329 #ifdef _MSC_VER
330                 //@@@@@@ lame hack: some MSVC versions can't see a protected decoder_ from nested State::resolved_as_cstring(); fix properly later
331                 public:
332 #endif
333                         ::FLAC__FileDecoder *decoder_;
334                 private:
335                         static ::FLAC__StreamDecoderWriteStatus write_callback_(const ::FLAC__FileDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
336                         static void metadata_callback_(const ::FLAC__FileDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data);
337                         static void error_callback_(const ::FLAC__FileDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data);
338
339                         // Private and undefined so you can't use them:
340                         File(const File &);
341                         void operator=(const File &);
342                 };
343
344                 /* \} */
345
346         };
347 };
348
349 #endif