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