Chris@5: /* libFLAC++ - Free Lossless Audio Codec library Chris@5: * Copyright (C) 2002,2003,2004,2005,2006,2007 Josh Coalson Chris@5: * Chris@5: * Redistribution and use in source and binary forms, with or without Chris@5: * modification, are permitted provided that the following conditions Chris@5: * are met: Chris@5: * Chris@5: * - Redistributions of source code must retain the above copyright Chris@5: * notice, this list of conditions and the following disclaimer. Chris@5: * Chris@5: * - Redistributions in binary form must reproduce the above copyright Chris@5: * notice, this list of conditions and the following disclaimer in the Chris@5: * documentation and/or other materials provided with the distribution. Chris@5: * Chris@5: * - Neither the name of the Xiph.org Foundation nor the names of its Chris@5: * contributors may be used to endorse or promote products derived from Chris@5: * this software without specific prior written permission. Chris@5: * Chris@5: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@5: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT Chris@5: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR Chris@5: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR Chris@5: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@5: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@5: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR Chris@5: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF Chris@5: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING Chris@5: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS Chris@5: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@5: */ Chris@5: Chris@5: #ifndef FLACPP__DECODER_H Chris@5: #define FLACPP__DECODER_H Chris@5: Chris@5: #include "export.h" Chris@5: Chris@5: #include Chris@5: #include "FLAC/stream_decoder.h" Chris@5: Chris@5: Chris@5: /** \file include/FLAC++/decoder.h Chris@5: * Chris@5: * \brief Chris@5: * This module contains the classes which implement the various Chris@5: * decoders. Chris@5: * Chris@5: * See the detailed documentation in the Chris@5: * \link flacpp_decoder decoder \endlink module. Chris@5: */ Chris@5: Chris@5: /** \defgroup flacpp_decoder FLAC++/decoder.h: decoder classes Chris@5: * \ingroup flacpp Chris@5: * Chris@5: * \brief Chris@5: * This module describes the decoder layers provided by libFLAC++. Chris@5: * Chris@5: * The libFLAC++ decoder classes are object wrappers around their Chris@5: * counterparts in libFLAC. All decoding layers available in Chris@5: * libFLAC are also provided here. The interface is very similar; Chris@5: * make sure to read the \link flac_decoder libFLAC decoder module \endlink. Chris@5: * Chris@5: * There are only two significant differences here. First, instead of Chris@5: * passing in C function pointers for callbacks, you inherit from the Chris@5: * decoder class and provide implementations for the callbacks in your Chris@5: * derived class; because of this there is no need for a 'client_data' Chris@5: * property. Chris@5: * Chris@5: * Second, there are two stream decoder classes. FLAC::Decoder::Stream Chris@5: * is used for the same cases that FLAC__stream_decoder_init_stream() / Chris@5: * FLAC__stream_decoder_init_ogg_stream() are used, and FLAC::Decoder::File Chris@5: * is used for the same cases that Chris@5: * FLAC__stream_decoder_init_FILE() and FLAC__stream_decoder_init_file() / Chris@5: * FLAC__stream_decoder_init_ogg_FILE() and FLAC__stream_decoder_init_ogg_file() Chris@5: * are used. Chris@5: */ Chris@5: Chris@5: namespace FLAC { Chris@5: namespace Decoder { Chris@5: Chris@5: /** \ingroup flacpp_decoder Chris@5: * \brief Chris@5: * This class wraps the ::FLAC__StreamDecoder. If you are Chris@5: * decoding from a file, FLAC::Decoder::File may be more Chris@5: * convenient. Chris@5: * Chris@5: * The usage of this class is similar to FLAC__StreamDecoder, Chris@5: * except instead of providing callbacks to Chris@5: * FLAC__stream_decoder_init*_stream(), you will inherit from this Chris@5: * class and override the virtual callback functions with your Chris@5: * own implementations, then call init() or init_ogg(). The rest Chris@5: * of the calls work the same as in the C layer. Chris@5: * Chris@5: * Only the read, write, and error callbacks are mandatory. The Chris@5: * others are optional; this class provides default Chris@5: * implementations that do nothing. In order for seeking to work Chris@5: * you must overide seek_callback(), tell_callback(), Chris@5: * length_callback(), and eof_callback(). Chris@5: */ Chris@5: class FLACPP_API Stream { Chris@5: public: Chris@5: /** This class is a wrapper around FLAC__StreamDecoderState. Chris@5: */ Chris@5: class FLACPP_API State { Chris@5: public: Chris@5: inline State(::FLAC__StreamDecoderState state): state_(state) { } Chris@5: inline operator ::FLAC__StreamDecoderState() const { return state_; } Chris@5: inline const char *as_cstring() const { return ::FLAC__StreamDecoderStateString[state_]; } Chris@5: inline const char *resolved_as_cstring(const Stream &decoder) const { return ::FLAC__stream_decoder_get_resolved_state_string(decoder.decoder_); } Chris@5: protected: Chris@5: ::FLAC__StreamDecoderState state_; Chris@5: }; Chris@5: Chris@5: Stream(); Chris@5: virtual ~Stream(); Chris@5: Chris@5: //@{ Chris@5: /** Call after construction to check the that the object was created Chris@5: * successfully. If not, use get_state() to find out why not. Chris@5: */ Chris@5: virtual bool is_valid() const; Chris@5: inline operator bool() const { return is_valid(); } ///< See is_valid() Chris@5: //@} Chris@5: Chris@5: virtual bool set_ogg_serial_number(long value); ///< See FLAC__stream_decoder_set_ogg_serial_number() Chris@5: virtual bool set_md5_checking(bool value); ///< See FLAC__stream_decoder_set_md5_checking() Chris@5: virtual bool set_metadata_respond(::FLAC__MetadataType type); ///< See FLAC__stream_decoder_set_metadata_respond() Chris@5: virtual bool set_metadata_respond_application(const FLAC__byte id[4]); ///< See FLAC__stream_decoder_set_metadata_respond_application() Chris@5: virtual bool set_metadata_respond_all(); ///< See FLAC__stream_decoder_set_metadata_respond_all() Chris@5: virtual bool set_metadata_ignore(::FLAC__MetadataType type); ///< See FLAC__stream_decoder_set_metadata_ignore() Chris@5: virtual bool set_metadata_ignore_application(const FLAC__byte id[4]); ///< See FLAC__stream_decoder_set_metadata_ignore_application() Chris@5: virtual bool set_metadata_ignore_all(); ///< See FLAC__stream_decoder_set_metadata_ignore_all() Chris@5: Chris@5: /* get_state() is not virtual since we want subclasses to be able to return their own state */ Chris@5: State get_state() const; ///< See FLAC__stream_decoder_get_state() Chris@5: virtual bool get_md5_checking() const; ///< See FLAC__stream_decoder_get_md5_checking() Chris@5: virtual FLAC__uint64 get_total_samples() const; ///< See FLAC__stream_decoder_get_total_samples() Chris@5: virtual unsigned get_channels() const; ///< See FLAC__stream_decoder_get_channels() Chris@5: virtual ::FLAC__ChannelAssignment get_channel_assignment() const; ///< See FLAC__stream_decoder_get_channel_assignment() Chris@5: virtual unsigned get_bits_per_sample() const; ///< See FLAC__stream_decoder_get_bits_per_sample() Chris@5: virtual unsigned get_sample_rate() const; ///< See FLAC__stream_decoder_get_sample_rate() Chris@5: virtual unsigned get_blocksize() const; ///< See FLAC__stream_decoder_get_blocksize() Chris@5: virtual bool get_decode_position(FLAC__uint64 *position) const; ///< See FLAC__stream_decoder_get_decode_position() Chris@5: Chris@5: virtual ::FLAC__StreamDecoderInitStatus init(); ///< Seek FLAC__stream_decoder_init_stream() Chris@5: virtual ::FLAC__StreamDecoderInitStatus init_ogg(); ///< Seek FLAC__stream_decoder_init_ogg_stream() Chris@5: Chris@5: virtual bool finish(); ///< See FLAC__stream_decoder_finish() Chris@5: Chris@5: virtual bool flush(); ///< See FLAC__stream_decoder_flush() Chris@5: virtual bool reset(); ///< See FLAC__stream_decoder_reset() Chris@5: Chris@5: virtual bool process_single(); ///< See FLAC__stream_decoder_process_single() Chris@5: virtual bool process_until_end_of_metadata(); ///< See FLAC__stream_decoder_process_until_end_of_metadata() Chris@5: virtual bool process_until_end_of_stream(); ///< See FLAC__stream_decoder_process_until_end_of_stream() Chris@5: virtual bool skip_single_frame(); ///< See FLAC__stream_decoder_skip_single_frame() Chris@5: Chris@5: virtual bool seek_absolute(FLAC__uint64 sample); ///< See FLAC__stream_decoder_seek_absolute() Chris@5: protected: Chris@5: /// see FLAC__StreamDecoderReadCallback Chris@5: virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes) = 0; Chris@5: Chris@5: /// see FLAC__StreamDecoderSeekCallback Chris@5: virtual ::FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset); Chris@5: Chris@5: /// see FLAC__StreamDecoderTellCallback Chris@5: virtual ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset); Chris@5: Chris@5: /// see FLAC__StreamDecoderLengthCallback Chris@5: virtual ::FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length); Chris@5: Chris@5: /// see FLAC__StreamDecoderEofCallback Chris@5: virtual bool eof_callback(); Chris@5: Chris@5: /// see FLAC__StreamDecoderWriteCallback Chris@5: virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) = 0; Chris@5: Chris@5: /// see FLAC__StreamDecoderMetadataCallback Chris@5: virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata); Chris@5: Chris@5: /// see FLAC__StreamDecoderErrorCallback Chris@5: virtual void error_callback(::FLAC__StreamDecoderErrorStatus status) = 0; Chris@5: Chris@5: #if (defined _MSC_VER) || (defined __BORLANDC__) || (defined __GNUG__ && (__GNUG__ < 2 || (__GNUG__ == 2 && __GNUC_MINOR__ < 96))) || (defined __SUNPRO_CC) Chris@5: // lame hack: some MSVC/GCC versions can't see a protected decoder_ from nested State::resolved_as_cstring() Chris@5: friend State; Chris@5: #endif Chris@5: ::FLAC__StreamDecoder *decoder_; Chris@5: Chris@5: static ::FLAC__StreamDecoderReadStatus read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); Chris@5: static ::FLAC__StreamDecoderSeekStatus seek_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data); Chris@5: static ::FLAC__StreamDecoderTellStatus tell_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data); Chris@5: static ::FLAC__StreamDecoderLengthStatus length_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data); Chris@5: static FLAC__bool eof_callback_(const ::FLAC__StreamDecoder *decoder, void *client_data); Chris@5: static ::FLAC__StreamDecoderWriteStatus write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); Chris@5: static void metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data); Chris@5: static void error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data); Chris@5: private: Chris@5: // Private and undefined so you can't use them: Chris@5: Stream(const Stream &); Chris@5: void operator=(const Stream &); Chris@5: }; Chris@5: Chris@5: /** \ingroup flacpp_decoder Chris@5: * \brief Chris@5: * This class wraps the ::FLAC__StreamDecoder. If you are Chris@5: * not decoding from a file, you may need to use Chris@5: * FLAC::Decoder::Stream. Chris@5: * Chris@5: * The usage of this class is similar to FLAC__StreamDecoder, Chris@5: * except instead of providing callbacks to Chris@5: * FLAC__stream_decoder_init*_FILE() or Chris@5: * FLAC__stream_decoder_init*_file(), you will inherit from this Chris@5: * class and override the virtual callback functions with your Chris@5: * own implementations, then call init() or init_off(). The rest Chris@5: * of the calls work the same as in the C layer. Chris@5: * Chris@5: * Only the write, and error callbacks from FLAC::Decoder::Stream Chris@5: * are mandatory. The others are optional; this class provides Chris@5: * full working implementations for all other callbacks and Chris@5: * supports seeking. Chris@5: */ Chris@5: class FLACPP_API File: public Stream { Chris@5: public: Chris@5: File(); Chris@5: virtual ~File(); Chris@5: Chris@5: virtual ::FLAC__StreamDecoderInitStatus init(FILE *file); ///< See FLAC__stream_decoder_init_FILE() Chris@5: virtual ::FLAC__StreamDecoderInitStatus init(const char *filename); ///< See FLAC__stream_decoder_init_file() Chris@5: virtual ::FLAC__StreamDecoderInitStatus init(const std::string &filename); ///< See FLAC__stream_decoder_init_file() Chris@5: virtual ::FLAC__StreamDecoderInitStatus init_ogg(FILE *file); ///< See FLAC__stream_decoder_init_ogg_FILE() Chris@5: virtual ::FLAC__StreamDecoderInitStatus init_ogg(const char *filename); ///< See FLAC__stream_decoder_init_ogg_file() Chris@5: virtual ::FLAC__StreamDecoderInitStatus init_ogg(const std::string &filename); ///< See FLAC__stream_decoder_init_ogg_file() Chris@5: protected: Chris@5: // this is a dummy implementation to satisfy the pure virtual in Stream that is actually supplied internally by the C layer Chris@5: virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes); Chris@5: private: Chris@5: // Private and undefined so you can't use them: Chris@5: File(const File &); Chris@5: void operator=(const File &); Chris@5: }; Chris@5: Chris@5: } Chris@5: } Chris@5: Chris@5: #endif