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__ENCODER_H Chris@5: #define FLACPP__ENCODER_H Chris@5: Chris@5: #include "export.h" Chris@5: Chris@5: #include "FLAC/stream_encoder.h" Chris@5: #include "decoder.h" Chris@5: #include "metadata.h" Chris@5: Chris@5: Chris@5: /** \file include/FLAC++/encoder.h Chris@5: * Chris@5: * \brief Chris@5: * This module contains the classes which implement the various Chris@5: * encoders. Chris@5: * Chris@5: * See the detailed documentation in the Chris@5: * \link flacpp_encoder encoder \endlink module. Chris@5: */ Chris@5: Chris@5: /** \defgroup flacpp_encoder FLAC++/encoder.h: encoder classes Chris@5: * \ingroup flacpp Chris@5: * Chris@5: * \brief Chris@5: * This module describes the encoder layers provided by libFLAC++. Chris@5: * Chris@5: * The libFLAC++ encoder classes are object wrappers around their Chris@5: * counterparts in libFLAC. All encoding layers available in Chris@5: * libFLAC are also provided here. The interface is very similar; Chris@5: * make sure to read the \link flac_encoder libFLAC encoder 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: * encoder 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 encoder classes. FLAC::Encoder::Stream Chris@5: * is used for the same cases that FLAC__stream_encoder_init_stream() / Chris@5: * FLAC__stream_encoder_init_ogg_stream() are used, and FLAC::Encoder::File Chris@5: * is used for the same cases that Chris@5: * FLAC__stream_encoder_init_FILE() and FLAC__stream_encoder_init_file() / Chris@5: * FLAC__stream_encoder_init_ogg_FILE() and FLAC__stream_encoder_init_ogg_file() Chris@5: * are used. Chris@5: */ Chris@5: Chris@5: namespace FLAC { Chris@5: namespace Encoder { Chris@5: Chris@5: /** \ingroup flacpp_encoder Chris@5: * \brief Chris@5: * This class wraps the ::FLAC__StreamEncoder. If you are Chris@5: * encoding to a file, FLAC::Encoder::File may be more Chris@5: * convenient. Chris@5: * Chris@5: * The usage of this class is similar to FLAC__StreamEncoder, Chris@5: * except instead of providing callbacks to Chris@5: * FLAC__stream_encoder_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 of Chris@5: * the calls work the same as in the C layer. Chris@5: * Chris@5: * Only the write callback is mandatory. The others are Chris@5: * optional; this class provides default implementations that do Chris@5: * nothing. In order for some STREAMINFO and SEEKTABLE data to Chris@5: * be written properly, you must overide seek_callback() and Chris@5: * tell_callback(); see FLAC__stream_encoder_init_stream() as to Chris@5: * why. Chris@5: */ Chris@5: class FLACPP_API Stream { Chris@5: public: Chris@5: /** This class is a wrapper around FLAC__StreamEncoderState. Chris@5: */ Chris@5: class FLACPP_API State { Chris@5: public: Chris@5: inline State(::FLAC__StreamEncoderState state): state_(state) { } Chris@5: inline operator ::FLAC__StreamEncoderState() const { return state_; } Chris@5: inline const char *as_cstring() const { return ::FLAC__StreamEncoderStateString[state_]; } Chris@5: inline const char *resolved_as_cstring(const Stream &encoder) const { return ::FLAC__stream_encoder_get_resolved_state_string(encoder.encoder_); } Chris@5: protected: Chris@5: ::FLAC__StreamEncoderState 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: */ 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_encoder_set_ogg_serial_number() Chris@5: virtual bool set_verify(bool value); ///< See FLAC__stream_encoder_set_verify() Chris@5: virtual bool set_streamable_subset(bool value); ///< See FLAC__stream_encoder_set_streamable_subset() Chris@5: virtual bool set_channels(unsigned value); ///< See FLAC__stream_encoder_set_channels() Chris@5: virtual bool set_bits_per_sample(unsigned value); ///< See FLAC__stream_encoder_set_bits_per_sample() Chris@5: virtual bool set_sample_rate(unsigned value); ///< See FLAC__stream_encoder_set_sample_rate() Chris@5: virtual bool set_compression_level(unsigned value); ///< See FLAC__stream_encoder_set_compression_level() Chris@5: virtual bool set_blocksize(unsigned value); ///< See FLAC__stream_encoder_set_blocksize() Chris@5: virtual bool set_do_mid_side_stereo(bool value); ///< See FLAC__stream_encoder_set_do_mid_side_stereo() Chris@5: virtual bool set_loose_mid_side_stereo(bool value); ///< See FLAC__stream_encoder_set_loose_mid_side_stereo() Chris@5: virtual bool set_apodization(const char *specification); ///< See FLAC__stream_encoder_set_apodization() Chris@5: virtual bool set_max_lpc_order(unsigned value); ///< See FLAC__stream_encoder_set_max_lpc_order() Chris@5: virtual bool set_qlp_coeff_precision(unsigned value); ///< See FLAC__stream_encoder_set_qlp_coeff_precision() Chris@5: virtual bool set_do_qlp_coeff_prec_search(bool value); ///< See FLAC__stream_encoder_set_do_qlp_coeff_prec_search() Chris@5: virtual bool set_do_escape_coding(bool value); ///< See FLAC__stream_encoder_set_do_escape_coding() Chris@5: virtual bool set_do_exhaustive_model_search(bool value); ///< See FLAC__stream_encoder_set_do_exhaustive_model_search() Chris@5: virtual bool set_min_residual_partition_order(unsigned value); ///< See FLAC__stream_encoder_set_min_residual_partition_order() Chris@5: virtual bool set_max_residual_partition_order(unsigned value); ///< See FLAC__stream_encoder_set_max_residual_partition_order() Chris@5: virtual bool set_rice_parameter_search_dist(unsigned value); ///< See FLAC__stream_encoder_set_rice_parameter_search_dist() Chris@5: virtual bool set_total_samples_estimate(FLAC__uint64 value); ///< See FLAC__stream_encoder_set_total_samples_estimate() Chris@5: virtual bool set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks); ///< See FLAC__stream_encoder_set_metadata() Chris@5: virtual bool set_metadata(FLAC::Metadata::Prototype **metadata, unsigned num_blocks); ///< See FLAC__stream_encoder_set_metadata() 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_encoder_get_state() Chris@5: virtual Decoder::Stream::State get_verify_decoder_state() const; ///< See FLAC__stream_encoder_get_verify_decoder_state() Chris@5: virtual void get_verify_decoder_error_stats(FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got); ///< See FLAC__stream_encoder_get_verify_decoder_error_stats() Chris@5: virtual bool get_verify() const; ///< See FLAC__stream_encoder_get_verify() Chris@5: virtual bool get_streamable_subset() const; ///< See FLAC__stream_encoder_get_streamable_subset() Chris@5: virtual bool get_do_mid_side_stereo() const; ///< See FLAC__stream_encoder_get_do_mid_side_stereo() Chris@5: virtual bool get_loose_mid_side_stereo() const; ///< See FLAC__stream_encoder_get_loose_mid_side_stereo() Chris@5: virtual unsigned get_channels() const; ///< See FLAC__stream_encoder_get_channels() Chris@5: virtual unsigned get_bits_per_sample() const; ///< See FLAC__stream_encoder_get_bits_per_sample() Chris@5: virtual unsigned get_sample_rate() const; ///< See FLAC__stream_encoder_get_sample_rate() Chris@5: virtual unsigned get_blocksize() const; ///< See FLAC__stream_encoder_get_blocksize() Chris@5: virtual unsigned get_max_lpc_order() const; ///< See FLAC__stream_encoder_get_max_lpc_order() Chris@5: virtual unsigned get_qlp_coeff_precision() const; ///< See FLAC__stream_encoder_get_qlp_coeff_precision() Chris@5: virtual bool get_do_qlp_coeff_prec_search() const; ///< See FLAC__stream_encoder_get_do_qlp_coeff_prec_search() Chris@5: virtual bool get_do_escape_coding() const; ///< See FLAC__stream_encoder_get_do_escape_coding() Chris@5: virtual bool get_do_exhaustive_model_search() const; ///< See FLAC__stream_encoder_get_do_exhaustive_model_search() Chris@5: virtual unsigned get_min_residual_partition_order() const; ///< See FLAC__stream_encoder_get_min_residual_partition_order() Chris@5: virtual unsigned get_max_residual_partition_order() const; ///< See FLAC__stream_encoder_get_max_residual_partition_order() Chris@5: virtual unsigned get_rice_parameter_search_dist() const; ///< See FLAC__stream_encoder_get_rice_parameter_search_dist() Chris@5: virtual FLAC__uint64 get_total_samples_estimate() const; ///< See FLAC__stream_encoder_get_total_samples_estimate() Chris@5: Chris@5: virtual ::FLAC__StreamEncoderInitStatus init(); ///< See FLAC__stream_encoder_init_stream() Chris@5: virtual ::FLAC__StreamEncoderInitStatus init_ogg(); ///< See FLAC__stream_encoder_init_ogg_stream() Chris@5: Chris@5: virtual bool finish(); ///< See FLAC__stream_encoder_finish() Chris@5: Chris@5: virtual bool process(const FLAC__int32 * const buffer[], unsigned samples); ///< See FLAC__stream_encoder_process() Chris@5: virtual bool process_interleaved(const FLAC__int32 buffer[], unsigned samples); ///< See FLAC__stream_encoder_process_interleaved() Chris@5: protected: Chris@5: /// See FLAC__StreamEncoderReadCallback Chris@5: virtual ::FLAC__StreamEncoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes); Chris@5: Chris@5: /// See FLAC__StreamEncoderWriteCallback Chris@5: virtual ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame) = 0; Chris@5: Chris@5: /// See FLAC__StreamEncoderSeekCallback Chris@5: virtual ::FLAC__StreamEncoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset); Chris@5: Chris@5: /// See FLAC__StreamEncoderTellCallback Chris@5: virtual ::FLAC__StreamEncoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset); Chris@5: Chris@5: /// See FLAC__StreamEncoderMetadataCallback Chris@5: virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata); 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 encoder_ from nested State::resolved_as_cstring() Chris@5: friend State; Chris@5: #endif Chris@5: ::FLAC__StreamEncoder *encoder_; Chris@5: Chris@5: static ::FLAC__StreamEncoderReadStatus read_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data); Chris@5: static ::FLAC__StreamEncoderWriteStatus write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data); Chris@5: static ::FLAC__StreamEncoderSeekStatus seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data); Chris@5: static ::FLAC__StreamEncoderTellStatus tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data); Chris@5: static void metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetadata *metadata, 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_encoder Chris@5: * \brief Chris@5: * This class wraps the ::FLAC__StreamEncoder. If you are Chris@5: * not encoding to a file, you may need to use Chris@5: * FLAC::Encoder::Stream. Chris@5: * Chris@5: * The usage of this class is similar to FLAC__StreamEncoder, Chris@5: * except instead of providing callbacks to Chris@5: * FLAC__stream_encoder_init*_FILE() or Chris@5: * FLAC__stream_encoder_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_ogg(). The rest Chris@5: * of the calls work the same as in the C layer. Chris@5: * Chris@5: * There are no mandatory callbacks; all the callbacks from Chris@5: * FLAC::Encoder::Stream are implemented here fully and support Chris@5: * full post-encode STREAMINFO and SEEKTABLE updating. There is Chris@5: * only an optional progress callback which you may override to Chris@5: * get periodic reports on the progress of the encode. 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__StreamEncoderInitStatus init(FILE *file); ///< See FLAC__stream_encoder_init_FILE() Chris@5: virtual ::FLAC__StreamEncoderInitStatus init(const char *filename); ///< See FLAC__stream_encoder_init_file() Chris@5: virtual ::FLAC__StreamEncoderInitStatus init(const std::string &filename); ///< See FLAC__stream_encoder_init_file() Chris@5: virtual ::FLAC__StreamEncoderInitStatus init_ogg(FILE *file); ///< See FLAC__stream_encoder_init_ogg_FILE() Chris@5: virtual ::FLAC__StreamEncoderInitStatus init_ogg(const char *filename); ///< See FLAC__stream_encoder_init_ogg_file() Chris@5: virtual ::FLAC__StreamEncoderInitStatus init_ogg(const std::string &filename); ///< See FLAC__stream_encoder_init_ogg_file() Chris@5: protected: Chris@5: /// See FLAC__StreamEncoderProgressCallback Chris@5: virtual void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate); Chris@5: 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__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame); Chris@5: private: Chris@5: static void progress_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data); Chris@5: Chris@5: // Private and undefined so you can't use them: Chris@5: File(const Stream &); Chris@5: void operator=(const Stream &); Chris@5: }; Chris@5: Chris@5: } Chris@5: } Chris@5: Chris@5: #endif