a921653c4459835645ea18cbe4d4f938f15c26cf
[platform/upstream/flac.git] / include / FLAC++ / decoder.h
1 /* libFLAC++ - Free Lossless Audio Codec library
2  * Copyright (C) 2002,2003,2004,2005,2006 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 <string>
38 #include "FLAC/stream_decoder.h"
39
40
41 /** \file include/FLAC++/decoder.h
42  *
43  *  \brief
44  *  This module contains the classes which implement the various
45  *  decoders.
46  *
47  *  See the detailed documentation in the
48  *  \link flacpp_decoder decoder \endlink module.
49  */
50
51 /** \defgroup flacpp_decoder FLAC++/decoder.h: decoder classes
52  *  \ingroup flacpp
53  *
54  *  \brief
55  *  This module describes the decoder layers provided by libFLAC++.
56  *
57  * The libFLAC++ decoder classes are object wrappers around their
58  * counterparts in libFLAC.  All decoding layers available in
59  * libFLAC are also provided here.  The interface is very similar;
60  * make sure to read the \link flac_decoder libFLAC decoder module \endlink.
61  *
62  * There are only two significant differences here.  First, instead of
63  * passing in C function pointers for callbacks, you inherit from the
64  * decoder class and provide implementations for the callbacks in your
65  * derived class; because of this there is no need for a 'client_data'
66  * property.
67  *
68  * Second, there are two stream decoder classes.  FLAC::Decoder::Stream
69  * is used for the same cases that FLAC__stream_decoder_init_stream() /
70  * FLAC__stream_decoder_init_ogg_stream() is used, and FLAC::Decoder::File
71  * is used for the same cases that
72  * FLAC__stream_decoder_init_FILE() and FLAC__stream_decoder_init_file() /
73  * FLAC__stream_decoder_init_ogg_FILE() and FLAC__stream_decoder_init_ogg_file()
74  * are used.
75  */
76
77 namespace FLAC {
78         namespace Decoder {
79
80                 /** \ingroup flacpp_decoder
81                  *  \brief
82                  *  This class wraps the ::FLAC__StreamDecoder.  If you are
83                  *  decoding from a file, FLAC::Decoder::File may be more
84                  *  convenient.
85                  *
86                  * The usage of this class is similar to FLAC__StreamDecoder,
87                  * except instead of providing callbacks to
88                  * FLAC__stream_decoder_init*_stream(), you will inherit from this
89                  * class and override the virtual callback functions with your
90                  * own implementations, then call init() or init_ogg().  The rest
91                  * of the calls work the same as in the C layer.
92                  *
93                  * Only the read, write, and error callbacks are mandatory.  The
94                  * others are optional; this class provides default
95                  * implementations that do nothing.  In order for seeking to work
96                  * you must overide seek_callback(), tell_callback(),
97                  * length_callback(), and eof_callback().
98                  */
99                 class FLACPP_API Stream {
100                 public:
101                         /** This class is a wrapper around FLAC__StreamDecoderState.
102                          */
103                         class FLACPP_API State {
104                         public:
105                                 inline State(::FLAC__StreamDecoderState state): state_(state) { }
106                                 inline operator ::FLAC__StreamDecoderState() const { return state_; }
107                                 inline const char *as_cstring() const { return ::FLAC__StreamDecoderStateString[state_]; }
108                                 inline const char *resolved_as_cstring(const Stream &decoder) const { return ::FLAC__stream_decoder_get_resolved_state_string(decoder.decoder_); }
109                         protected:
110                                 ::FLAC__StreamDecoderState state_;
111                         };
112
113                         Stream();
114                         virtual ~Stream();
115
116                         //@{
117                         /** Call after construction to check the that the object was created
118                          *  successfully.  If not, use get_state() to find out why not.
119                          */
120                         virtual bool is_valid() const;
121                         inline operator bool() const { return is_valid(); } ///< See is_valid()
122                         //@}
123
124                         virtual bool set_ogg_serial_number(long value);                        ///< See FLAC__stream_decoder_set_ogg_serial_number()
125                         virtual bool set_md5_checking(bool value);                             ///< See FLAC__stream_decoder_set_md5_checking()
126                         virtual bool set_metadata_respond(::FLAC__MetadataType type);          ///< See FLAC__stream_decoder_set_metadata_respond()
127                         virtual bool set_metadata_respond_application(const FLAC__byte id[4]); ///< See FLAC__stream_decoder_set_metadata_respond_application()
128                         virtual bool set_metadata_respond_all();                               ///< See FLAC__stream_decoder_set_metadata_respond_all()
129                         virtual bool set_metadata_ignore(::FLAC__MetadataType type);           ///< See FLAC__stream_decoder_set_metadata_ignore()
130                         virtual bool set_metadata_ignore_application(const FLAC__byte id[4]);  ///< See FLAC__stream_decoder_set_metadata_ignore_application()
131                         virtual bool set_metadata_ignore_all();                                ///< See FLAC__stream_decoder_set_metadata_ignore_all()
132
133                         /* get_state() is not virtual since we want subclasses to be able to return their own state */
134                         State get_state() const;                                          ///< See FLAC__stream_decoder_get_state()
135                         virtual bool get_md5_checking() const;                            ///< See FLAC__stream_decoder_get_md5_checking()
136                         virtual FLAC__uint64 get_total_samples() const;                   ///< See FLAC__stream_decoder_get_total_samples()
137                         virtual unsigned get_channels() const;                            ///< See FLAC__stream_decoder_get_channels()
138                         virtual ::FLAC__ChannelAssignment get_channel_assignment() const; ///< See FLAC__stream_decoder_get_channel_assignment()
139                         virtual unsigned get_bits_per_sample() const;                     ///< See FLAC__stream_decoder_get_bits_per_sample()
140                         virtual unsigned get_sample_rate() const;                         ///< See FLAC__stream_decoder_get_sample_rate()
141                         virtual unsigned get_blocksize() const;                           ///< See FLAC__stream_decoder_get_blocksize()
142
143                         virtual ::FLAC__StreamDecoderInitStatus init();      ///< Seek FLAC__stream_decoder_init_stream()
144                         virtual ::FLAC__StreamDecoderInitStatus init_ogg();  ///< Seek FLAC__stream_decoder_init_ogg_stream()
145
146                         virtual void finish(); ///< See FLAC__stream_decoder_finish()
147
148                         virtual bool flush(); ///< See FLAC__stream_decoder_flush()
149                         virtual bool reset(); ///< See FLAC__stream_decoder_reset()
150
151                         virtual bool process_single();                ///< See FLAC__stream_decoder_process_single()
152                         virtual bool process_until_end_of_metadata(); ///< See FLAC__stream_decoder_process_until_end_of_metadata()
153                         virtual bool process_until_end_of_stream();   ///< See FLAC__stream_decoder_process_until_end_of_stream()
154                         virtual bool skip_single_frame();             ///< See FLAC__stream_decoder_skip_single_frame()
155
156                         virtual bool seek_absolute(FLAC__uint64 sample); ///< See FLAC__stream_decoder_seek_absolute()
157                 protected:
158                         /// see FLAC__StreamDecoderReadCallback
159                         virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes) = 0;
160
161                         /// see FLAC__StreamDecoderSeekCallback
162                         virtual ::FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
163
164                         /// see FLAC__StreamDecoderTellCallback
165                         virtual ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
166
167                         /// see FLAC__StreamDecoderLengthCallback
168                         virtual ::FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length);
169
170                         /// see FLAC__StreamDecoderEofCallback
171                         virtual bool eof_callback();
172
173                         /// see FLAC__StreamDecoderWriteCallback
174                         virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) = 0;
175
176                         /// see FLAC__StreamDecoderMetadataCallback
177                         virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata);
178
179                         /// see FLAC__StreamDecoderErrorCallback
180                         virtual void error_callback(::FLAC__StreamDecoderErrorStatus status) = 0;
181
182 #if (defined _MSC_VER) || (defined __GNUG__ && (__GNUG__ < 2 || (__GNUG__ == 2 && __GNUC_MINOR__ < 96))) || (defined __SUNPRO_CC)
183                         // lame hack: some MSVC/GCC versions can't see a protected decoder_ from nested State::resolved_as_cstring()
184                         friend State;
185 #endif
186                         // hackery solely for the use of libOggFLAC++
187                         Stream(::FLAC__StreamDecoder *);
188
189                         ::FLAC__StreamDecoder *decoder_;
190
191                         static ::FLAC__StreamDecoderReadStatus read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
192                         static ::FLAC__StreamDecoderSeekStatus seek_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
193                         static ::FLAC__StreamDecoderTellStatus tell_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
194                         static ::FLAC__StreamDecoderLengthStatus length_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
195                         static FLAC__bool eof_callback_(const ::FLAC__StreamDecoder *decoder, void *client_data);
196                         static ::FLAC__StreamDecoderWriteStatus write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
197                         static void metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data);
198                         static void error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data);
199                 private:
200                         // Private and undefined so you can't use them:
201                         Stream(const Stream &);
202                         void operator=(const Stream &);
203                 };
204
205                 /** \ingroup flacpp_decoder
206                  *  \brief
207                  *  This class wraps the ::FLAC__StreamDecoder.  If you are
208                  *  not decoding from a file, you may need to use
209                  *  FLAC::Decoder::Stream.
210                  *
211                  * The usage of this class is similar to FLAC__StreamDecoder,
212                  * except instead of providing callbacks to
213                  * FLAC__stream_decoder_init*_FILE() or
214                  * FLAC__stream_decoder_init*_file(), you will inherit from this
215                  * class and override the virtual callback functions with your
216                  * own implementations, then call init() or init_off().  The rest
217                  * of the calls work the same as in the C layer.
218                  *
219                  * Only the write, and error callbacks from FLAC::Decoder::Stream
220                  * are mandatory.  The others are optional; this class provides
221                  * full working implementations for all other callbacks and
222                  * supports seeking.
223                  */
224                 class FLACPP_API File: public Stream {
225                 public:
226                         File();
227                         virtual ~File();
228
229                         virtual ::FLAC__StreamDecoderInitStatus init(FILE *file);                      ///< See FLAC__stream_decoder_init_FILE()
230                         virtual ::FLAC__StreamDecoderInitStatus init(const char *filename);            ///< See FLAC__stream_decoder_init_file()
231                         virtual ::FLAC__StreamDecoderInitStatus init(const std::string &filename);     ///< See FLAC__stream_decoder_init_file()
232                         virtual ::FLAC__StreamDecoderInitStatus init_ogg(FILE *file);                  ///< See FLAC__stream_decoder_init_ogg_FILE()
233                         virtual ::FLAC__StreamDecoderInitStatus init_ogg(const char *filename);        ///< See FLAC__stream_decoder_init_ogg_file()
234                         virtual ::FLAC__StreamDecoderInitStatus init_ogg(const std::string &filename); ///< See FLAC__stream_decoder_init_ogg_file()
235                 protected:
236                         // this is a dummy implementation to satisfy the pure virtual in Stream that is actually supplied internally by the C layer
237                         virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes);
238                 private:
239                         // Private and undefined so you can't use them:
240                         File(const File &);
241                         void operator=(const File &);
242                 };
243
244         }
245 }
246
247 #endif