1*600f14f4SXin Li /* libFLAC++ - Free Lossless Audio Codec library 2*600f14f4SXin Li * Copyright (C) 2002-2009 Josh Coalson 3*600f14f4SXin Li * Copyright (C) 2011-2023 Xiph.Org Foundation 4*600f14f4SXin Li * 5*600f14f4SXin Li * Redistribution and use in source and binary forms, with or without 6*600f14f4SXin Li * modification, are permitted provided that the following conditions 7*600f14f4SXin Li * are met: 8*600f14f4SXin Li * 9*600f14f4SXin Li * - Redistributions of source code must retain the above copyright 10*600f14f4SXin Li * notice, this list of conditions and the following disclaimer. 11*600f14f4SXin Li * 12*600f14f4SXin Li * - Redistributions in binary form must reproduce the above copyright 13*600f14f4SXin Li * notice, this list of conditions and the following disclaimer in the 14*600f14f4SXin Li * documentation and/or other materials provided with the distribution. 15*600f14f4SXin Li * 16*600f14f4SXin Li * - Neither the name of the Xiph.org Foundation nor the names of its 17*600f14f4SXin Li * contributors may be used to endorse or promote products derived from 18*600f14f4SXin Li * this software without specific prior written permission. 19*600f14f4SXin Li * 20*600f14f4SXin Li * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21*600f14f4SXin Li * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*600f14f4SXin Li * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23*600f14f4SXin Li * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 24*600f14f4SXin Li * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25*600f14f4SXin Li * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26*600f14f4SXin Li * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27*600f14f4SXin Li * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28*600f14f4SXin Li * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29*600f14f4SXin Li * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30*600f14f4SXin Li * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31*600f14f4SXin Li */ 32*600f14f4SXin Li 33*600f14f4SXin Li #ifndef FLACPP__DECODER_H 34*600f14f4SXin Li #define FLACPP__DECODER_H 35*600f14f4SXin Li 36*600f14f4SXin Li #include "export.h" 37*600f14f4SXin Li 38*600f14f4SXin Li #include <string> 39*600f14f4SXin Li #include "FLAC/stream_decoder.h" 40*600f14f4SXin Li 41*600f14f4SXin Li 42*600f14f4SXin Li /** \file include/FLAC++/decoder.h 43*600f14f4SXin Li * 44*600f14f4SXin Li * \brief 45*600f14f4SXin Li * This module contains the classes which implement the various 46*600f14f4SXin Li * decoders. 47*600f14f4SXin Li * 48*600f14f4SXin Li * See the detailed documentation in the 49*600f14f4SXin Li * \link flacpp_decoder decoder \endlink module. 50*600f14f4SXin Li */ 51*600f14f4SXin Li 52*600f14f4SXin Li /** \defgroup flacpp_decoder FLAC++/decoder.h: decoder classes 53*600f14f4SXin Li * \ingroup flacpp 54*600f14f4SXin Li * 55*600f14f4SXin Li * \brief 56*600f14f4SXin Li * This module describes the decoder layers provided by libFLAC++. 57*600f14f4SXin Li * 58*600f14f4SXin Li * The libFLAC++ decoder classes are object wrappers around their 59*600f14f4SXin Li * counterparts in libFLAC. All decoding layers available in 60*600f14f4SXin Li * libFLAC are also provided here. The interface is very similar; 61*600f14f4SXin Li * make sure to read the \link flac_decoder libFLAC decoder module \endlink. 62*600f14f4SXin Li * 63*600f14f4SXin Li * There are only two significant differences here. First, instead of 64*600f14f4SXin Li * passing in C function pointers for callbacks, you inherit from the 65*600f14f4SXin Li * decoder class and provide implementations for the callbacks in your 66*600f14f4SXin Li * derived class; because of this there is no need for a 'client_data' 67*600f14f4SXin Li * property. 68*600f14f4SXin Li * 69*600f14f4SXin Li * Second, there are two stream decoder classes. FLAC::Decoder::Stream 70*600f14f4SXin Li * is used for the same cases that FLAC__stream_decoder_init_stream() / 71*600f14f4SXin Li * FLAC__stream_decoder_init_ogg_stream() are used, and FLAC::Decoder::File 72*600f14f4SXin Li * is used for the same cases that 73*600f14f4SXin Li * FLAC__stream_decoder_init_FILE() and FLAC__stream_decoder_init_file() / 74*600f14f4SXin Li * FLAC__stream_decoder_init_ogg_FILE() and FLAC__stream_decoder_init_ogg_file() 75*600f14f4SXin Li * are used. 76*600f14f4SXin Li */ 77*600f14f4SXin Li 78*600f14f4SXin Li namespace FLAC { 79*600f14f4SXin Li namespace Decoder { 80*600f14f4SXin Li 81*600f14f4SXin Li /** \ingroup flacpp_decoder 82*600f14f4SXin Li * \brief 83*600f14f4SXin Li * This class wraps the ::FLAC__StreamDecoder. If you are 84*600f14f4SXin Li * decoding from a file, FLAC::Decoder::File may be more 85*600f14f4SXin Li * convenient. 86*600f14f4SXin Li * 87*600f14f4SXin Li * The usage of this class is similar to FLAC__StreamDecoder, 88*600f14f4SXin Li * except instead of providing callbacks to 89*600f14f4SXin Li * FLAC__stream_decoder_init*_stream(), you will inherit from this 90*600f14f4SXin Li * class and override the virtual callback functions with your 91*600f14f4SXin Li * own implementations, then call init() or init_ogg(). The rest 92*600f14f4SXin Li * of the calls work the same as in the C layer. 93*600f14f4SXin Li * 94*600f14f4SXin Li * Only the read, write, and error callbacks are mandatory. The 95*600f14f4SXin Li * others are optional; this class provides default 96*600f14f4SXin Li * implementations that do nothing. In order for seeking to work 97*600f14f4SXin Li * you must override seek_callback(), tell_callback(), 98*600f14f4SXin Li * length_callback(), and eof_callback(). 99*600f14f4SXin Li */ 100*600f14f4SXin Li class FLACPP_API Stream { 101*600f14f4SXin Li public: 102*600f14f4SXin Li /** This class is a wrapper around FLAC__StreamDecoderState. 103*600f14f4SXin Li */ 104*600f14f4SXin Li class FLACPP_API State { 105*600f14f4SXin Li public: State(::FLAC__StreamDecoderState state)106*600f14f4SXin Li inline State(::FLAC__StreamDecoderState state): state_(state) { } FLAC__StreamDecoderState()107*600f14f4SXin Li inline operator ::FLAC__StreamDecoderState() const { return state_; } as_cstring()108*600f14f4SXin Li inline const char *as_cstring() const { return ::FLAC__StreamDecoderStateString[state_]; } resolved_as_cstring(const Stream & decoder)109*600f14f4SXin Li inline const char *resolved_as_cstring(const Stream &decoder) const { return ::FLAC__stream_decoder_get_resolved_state_string(decoder.decoder_); } 110*600f14f4SXin Li protected: 111*600f14f4SXin Li ::FLAC__StreamDecoderState state_; 112*600f14f4SXin Li }; 113*600f14f4SXin Li 114*600f14f4SXin Li Stream(); 115*600f14f4SXin Li virtual ~Stream(); 116*600f14f4SXin Li 117*600f14f4SXin Li //@{ 118*600f14f4SXin Li /** Call after construction to check that the object was created 119*600f14f4SXin Li * successfully. If not, use get_state() to find out why not. 120*600f14f4SXin Li */ 121*600f14f4SXin Li virtual bool is_valid() const; 122*600f14f4SXin Li inline operator bool() const { return is_valid(); } ///< See is_valid() 123*600f14f4SXin Li //@} 124*600f14f4SXin Li 125*600f14f4SXin Li virtual bool set_ogg_serial_number(long value); ///< See FLAC__stream_decoder_set_ogg_serial_number() 126*600f14f4SXin Li virtual bool set_md5_checking(bool value); ///< See FLAC__stream_decoder_set_md5_checking() 127*600f14f4SXin Li virtual bool set_metadata_respond(::FLAC__MetadataType type); ///< See FLAC__stream_decoder_set_metadata_respond() 128*600f14f4SXin Li virtual bool set_metadata_respond_application(const FLAC__byte id[4]); ///< See FLAC__stream_decoder_set_metadata_respond_application() 129*600f14f4SXin Li virtual bool set_metadata_respond_all(); ///< See FLAC__stream_decoder_set_metadata_respond_all() 130*600f14f4SXin Li virtual bool set_metadata_ignore(::FLAC__MetadataType type); ///< See FLAC__stream_decoder_set_metadata_ignore() 131*600f14f4SXin Li virtual bool set_metadata_ignore_application(const FLAC__byte id[4]); ///< See FLAC__stream_decoder_set_metadata_ignore_application() 132*600f14f4SXin Li virtual bool set_metadata_ignore_all(); ///< See FLAC__stream_decoder_set_metadata_ignore_all() 133*600f14f4SXin Li 134*600f14f4SXin Li /* get_state() is not virtual since we want subclasses to be able to return their own state */ 135*600f14f4SXin Li State get_state() const; ///< See FLAC__stream_decoder_get_state() 136*600f14f4SXin Li virtual bool get_md5_checking() const; ///< See FLAC__stream_decoder_get_md5_checking() 137*600f14f4SXin Li virtual FLAC__uint64 get_total_samples() const; ///< See FLAC__stream_decoder_get_total_samples() 138*600f14f4SXin Li virtual uint32_t get_channels() const; ///< See FLAC__stream_decoder_get_channels() 139*600f14f4SXin Li virtual ::FLAC__ChannelAssignment get_channel_assignment() const; ///< See FLAC__stream_decoder_get_channel_assignment() 140*600f14f4SXin Li virtual uint32_t get_bits_per_sample() const; ///< See FLAC__stream_decoder_get_bits_per_sample() 141*600f14f4SXin Li virtual uint32_t get_sample_rate() const; ///< See FLAC__stream_decoder_get_sample_rate() 142*600f14f4SXin Li virtual uint32_t get_blocksize() const; ///< See FLAC__stream_decoder_get_blocksize() 143*600f14f4SXin Li virtual bool get_decode_position(FLAC__uint64 *position) const; ///< See FLAC__stream_decoder_get_decode_position() 144*600f14f4SXin Li 145*600f14f4SXin Li virtual ::FLAC__StreamDecoderInitStatus init(); ///< Seek FLAC__stream_decoder_init_stream() 146*600f14f4SXin Li virtual ::FLAC__StreamDecoderInitStatus init_ogg(); ///< Seek FLAC__stream_decoder_init_ogg_stream() 147*600f14f4SXin Li 148*600f14f4SXin Li virtual bool finish(); ///< See FLAC__stream_decoder_finish() 149*600f14f4SXin Li 150*600f14f4SXin Li virtual bool flush(); ///< See FLAC__stream_decoder_flush() 151*600f14f4SXin Li virtual bool reset(); ///< See FLAC__stream_decoder_reset() 152*600f14f4SXin Li 153*600f14f4SXin Li virtual bool process_single(); ///< See FLAC__stream_decoder_process_single() 154*600f14f4SXin Li virtual bool process_until_end_of_metadata(); ///< See FLAC__stream_decoder_process_until_end_of_metadata() 155*600f14f4SXin Li virtual bool process_until_end_of_stream(); ///< See FLAC__stream_decoder_process_until_end_of_stream() 156*600f14f4SXin Li virtual bool skip_single_frame(); ///< See FLAC__stream_decoder_skip_single_frame() 157*600f14f4SXin Li 158*600f14f4SXin Li virtual bool seek_absolute(FLAC__uint64 sample); ///< See FLAC__stream_decoder_seek_absolute() 159*600f14f4SXin Li protected: 160*600f14f4SXin Li /// see FLAC__StreamDecoderReadCallback 161*600f14f4SXin Li virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes) = 0; 162*600f14f4SXin Li 163*600f14f4SXin Li /// see FLAC__StreamDecoderSeekCallback 164*600f14f4SXin Li virtual ::FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset); 165*600f14f4SXin Li 166*600f14f4SXin Li /// see FLAC__StreamDecoderTellCallback 167*600f14f4SXin Li virtual ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset); 168*600f14f4SXin Li 169*600f14f4SXin Li /// see FLAC__StreamDecoderLengthCallback 170*600f14f4SXin Li virtual ::FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length); 171*600f14f4SXin Li 172*600f14f4SXin Li /// see FLAC__StreamDecoderEofCallback 173*600f14f4SXin Li virtual bool eof_callback(); 174*600f14f4SXin Li 175*600f14f4SXin Li /// see FLAC__StreamDecoderWriteCallback 176*600f14f4SXin Li virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) = 0; 177*600f14f4SXin Li 178*600f14f4SXin Li /// see FLAC__StreamDecoderMetadataCallback 179*600f14f4SXin Li virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata); 180*600f14f4SXin Li 181*600f14f4SXin Li /// see FLAC__StreamDecoderErrorCallback 182*600f14f4SXin Li virtual void error_callback(::FLAC__StreamDecoderErrorStatus status) = 0; 183*600f14f4SXin Li 184*600f14f4SXin Li #if (defined __BORLANDC__) || (defined __GNUG__ && (__GNUG__ < 2 || (__GNUG__ == 2 && __GNUC_MINOR__ < 96))) || (defined __SUNPRO_CC) 185*600f14f4SXin Li // lame hack: some compilers can't see a protected decoder_ from nested State::resolved_as_cstring() 186*600f14f4SXin Li friend State; 187*600f14f4SXin Li #endif 188*600f14f4SXin Li ::FLAC__StreamDecoder *decoder_; 189*600f14f4SXin Li 190*600f14f4SXin Li static ::FLAC__StreamDecoderReadStatus read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); 191*600f14f4SXin Li static ::FLAC__StreamDecoderSeekStatus seek_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data); 192*600f14f4SXin Li static ::FLAC__StreamDecoderTellStatus tell_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data); 193*600f14f4SXin Li static ::FLAC__StreamDecoderLengthStatus length_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data); 194*600f14f4SXin Li static FLAC__bool eof_callback_(const ::FLAC__StreamDecoder *decoder, void *client_data); 195*600f14f4SXin Li static ::FLAC__StreamDecoderWriteStatus write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); 196*600f14f4SXin Li static void metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data); 197*600f14f4SXin Li static void error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data); 198*600f14f4SXin Li private: 199*600f14f4SXin Li // Private and undefined so you can't use them: 200*600f14f4SXin Li Stream(const Stream &); 201*600f14f4SXin Li void operator=(const Stream &); 202*600f14f4SXin Li }; 203*600f14f4SXin Li 204*600f14f4SXin Li /** \ingroup flacpp_decoder 205*600f14f4SXin Li * \brief 206*600f14f4SXin Li * This class wraps the ::FLAC__StreamDecoder. If you are 207*600f14f4SXin Li * not decoding from a file, you may need to use 208*600f14f4SXin Li * FLAC::Decoder::Stream. 209*600f14f4SXin Li * 210*600f14f4SXin Li * The usage of this class is similar to FLAC__StreamDecoder, 211*600f14f4SXin Li * except instead of providing callbacks to 212*600f14f4SXin Li * FLAC__stream_decoder_init*_FILE() or 213*600f14f4SXin Li * FLAC__stream_decoder_init*_file(), you will inherit from this 214*600f14f4SXin Li * class and override the virtual callback functions with your 215*600f14f4SXin Li * own implementations, then call init() or init_off(). The rest 216*600f14f4SXin Li * of the calls work the same as in the C layer. 217*600f14f4SXin Li * 218*600f14f4SXin Li * Only the write, and error callbacks from FLAC::Decoder::Stream 219*600f14f4SXin Li * are mandatory. The others are optional; this class provides 220*600f14f4SXin Li * full working implementations for all other callbacks and 221*600f14f4SXin Li * supports seeking. 222*600f14f4SXin Li */ 223*600f14f4SXin Li class FLACPP_API File: public Stream { 224*600f14f4SXin Li public: 225*600f14f4SXin Li File(); 226*600f14f4SXin Li virtual ~File(); 227*600f14f4SXin Li 228*600f14f4SXin Li using Stream::init; 229*600f14f4SXin Li virtual ::FLAC__StreamDecoderInitStatus init(FILE *file); ///< See FLAC__stream_decoder_init_FILE() 230*600f14f4SXin Li virtual ::FLAC__StreamDecoderInitStatus init(const char *filename); ///< See FLAC__stream_decoder_init_file() 231*600f14f4SXin Li virtual ::FLAC__StreamDecoderInitStatus init(const std::string &filename); ///< See FLAC__stream_decoder_init_file() 232*600f14f4SXin Li using Stream::init_ogg; 233*600f14f4SXin Li virtual ::FLAC__StreamDecoderInitStatus init_ogg(FILE *file); ///< See FLAC__stream_decoder_init_ogg_FILE() 234*600f14f4SXin Li virtual ::FLAC__StreamDecoderInitStatus init_ogg(const char *filename); ///< See FLAC__stream_decoder_init_ogg_file() 235*600f14f4SXin Li virtual ::FLAC__StreamDecoderInitStatus init_ogg(const std::string &filename); ///< See FLAC__stream_decoder_init_ogg_file() 236*600f14f4SXin Li protected: 237*600f14f4SXin Li // this is a dummy implementation to satisfy the pure virtual in Stream that is actually supplied internally by the C layer 238*600f14f4SXin Li virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes); 239*600f14f4SXin Li private: 240*600f14f4SXin Li // Private and undefined so you can't use them: 241*600f14f4SXin Li File(const File &); 242*600f14f4SXin Li void operator=(const File &); 243*600f14f4SXin Li }; 244*600f14f4SXin Li 245*600f14f4SXin Li } 246*600f14f4SXin Li } 247*600f14f4SXin Li 248*600f14f4SXin Li #endif 249