encoder.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* libFLAC++ - Free Lossless Audio Codec library
  2. * Copyright (C) 2002-2009 Josh Coalson
  3. * Copyright (C) 2011-2014 Xiph.Org Foundation
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * - Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * - Neither the name of the Xiph.org Foundation nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef FLACPP__ENCODER_H
  33. #define FLACPP__ENCODER_H
  34. #include "export.h"
  35. #include "FLAC/stream_encoder.h"
  36. #include "decoder.h"
  37. #include "metadata.h"
  38. /** \file include/FLAC++/encoder.h
  39. *
  40. * \brief
  41. * This module contains the classes which implement the various
  42. * encoders.
  43. *
  44. * See the detailed documentation in the
  45. * \link flacpp_encoder encoder \endlink module.
  46. */
  47. /** \defgroup flacpp_encoder FLAC++/encoder.h: encoder classes
  48. * \ingroup flacpp
  49. *
  50. * \brief
  51. * This module describes the encoder layers provided by libFLAC++.
  52. *
  53. * The libFLAC++ encoder classes are object wrappers around their
  54. * counterparts in libFLAC. All encoding layers available in
  55. * libFLAC are also provided here. The interface is very similar;
  56. * make sure to read the \link flac_encoder libFLAC encoder module \endlink.
  57. *
  58. * There are only two significant differences here. First, instead of
  59. * passing in C function pointers for callbacks, you inherit from the
  60. * encoder class and provide implementations for the callbacks in your
  61. * derived class; because of this there is no need for a 'client_data'
  62. * property.
  63. *
  64. * Second, there are two stream encoder classes. FLAC::Encoder::Stream
  65. * is used for the same cases that FLAC__stream_encoder_init_stream() /
  66. * FLAC__stream_encoder_init_ogg_stream() are used, and FLAC::Encoder::File
  67. * is used for the same cases that
  68. * FLAC__stream_encoder_init_FILE() and FLAC__stream_encoder_init_file() /
  69. * FLAC__stream_encoder_init_ogg_FILE() and FLAC__stream_encoder_init_ogg_file()
  70. * are used.
  71. */
  72. namespace FLAC {
  73. namespace Encoder {
  74. /** \ingroup flacpp_encoder
  75. * \brief
  76. * This class wraps the ::FLAC__StreamEncoder. If you are
  77. * encoding to a file, FLAC::Encoder::File may be more
  78. * convenient.
  79. *
  80. * The usage of this class is similar to FLAC__StreamEncoder,
  81. * except instead of providing callbacks to
  82. * FLAC__stream_encoder_init*_stream(), you will inherit from this
  83. * class and override the virtual callback functions with your
  84. * own implementations, then call init() or init_ogg(). The rest of
  85. * the calls work the same as in the C layer.
  86. *
  87. * Only the write callback is mandatory. The others are
  88. * optional; this class provides default implementations that do
  89. * nothing. In order for some STREAMINFO and SEEKTABLE data to
  90. * be written properly, you must overide seek_callback() and
  91. * tell_callback(); see FLAC__stream_encoder_init_stream() as to
  92. * why.
  93. */
  94. class FLACPP_API Stream {
  95. public:
  96. /** This class is a wrapper around FLAC__StreamEncoderState.
  97. */
  98. class FLACPP_API State {
  99. public:
  100. inline State(::FLAC__StreamEncoderState state): state_(state) { }
  101. inline operator ::FLAC__StreamEncoderState() const { return state_; }
  102. inline const char *as_cstring() const { return ::FLAC__StreamEncoderStateString[state_]; }
  103. inline const char *resolved_as_cstring(const Stream &encoder) const { return ::FLAC__stream_encoder_get_resolved_state_string(encoder.encoder_); }
  104. protected:
  105. ::FLAC__StreamEncoderState state_;
  106. };
  107. Stream();
  108. virtual ~Stream();
  109. //@{
  110. /** Call after construction to check the that the object was created
  111. * successfully. If not, use get_state() to find out why not.
  112. *
  113. */
  114. virtual bool is_valid() const;
  115. inline operator bool() const { return is_valid(); } ///< See is_valid()
  116. //@}
  117. virtual bool set_ogg_serial_number(long value); ///< See FLAC__stream_encoder_set_ogg_serial_number()
  118. virtual bool set_verify(bool value); ///< See FLAC__stream_encoder_set_verify()
  119. virtual bool set_streamable_subset(bool value); ///< See FLAC__stream_encoder_set_streamable_subset()
  120. virtual bool set_channels(unsigned value); ///< See FLAC__stream_encoder_set_channels()
  121. virtual bool set_bits_per_sample(unsigned value); ///< See FLAC__stream_encoder_set_bits_per_sample()
  122. virtual bool set_sample_rate(unsigned value); ///< See FLAC__stream_encoder_set_sample_rate()
  123. virtual bool set_compression_level(unsigned value); ///< See FLAC__stream_encoder_set_compression_level()
  124. virtual bool set_blocksize(unsigned value); ///< See FLAC__stream_encoder_set_blocksize()
  125. virtual bool set_do_mid_side_stereo(bool value); ///< See FLAC__stream_encoder_set_do_mid_side_stereo()
  126. virtual bool set_loose_mid_side_stereo(bool value); ///< See FLAC__stream_encoder_set_loose_mid_side_stereo()
  127. virtual bool set_apodization(const char *specification); ///< See FLAC__stream_encoder_set_apodization()
  128. virtual bool set_max_lpc_order(unsigned value); ///< See FLAC__stream_encoder_set_max_lpc_order()
  129. virtual bool set_qlp_coeff_precision(unsigned value); ///< See FLAC__stream_encoder_set_qlp_coeff_precision()
  130. virtual bool set_do_qlp_coeff_prec_search(bool value); ///< See FLAC__stream_encoder_set_do_qlp_coeff_prec_search()
  131. virtual bool set_do_escape_coding(bool value); ///< See FLAC__stream_encoder_set_do_escape_coding()
  132. virtual bool set_do_exhaustive_model_search(bool value); ///< See FLAC__stream_encoder_set_do_exhaustive_model_search()
  133. virtual bool set_min_residual_partition_order(unsigned value); ///< See FLAC__stream_encoder_set_min_residual_partition_order()
  134. virtual bool set_max_residual_partition_order(unsigned value); ///< See FLAC__stream_encoder_set_max_residual_partition_order()
  135. virtual bool set_rice_parameter_search_dist(unsigned value); ///< See FLAC__stream_encoder_set_rice_parameter_search_dist()
  136. virtual bool set_total_samples_estimate(FLAC__uint64 value); ///< See FLAC__stream_encoder_set_total_samples_estimate()
  137. virtual bool set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks); ///< See FLAC__stream_encoder_set_metadata()
  138. virtual bool set_metadata(FLAC::Metadata::Prototype **metadata, unsigned num_blocks); ///< See FLAC__stream_encoder_set_metadata()
  139. /* get_state() is not virtual since we want subclasses to be able to return their own state */
  140. State get_state() const; ///< See FLAC__stream_encoder_get_state()
  141. virtual Decoder::Stream::State get_verify_decoder_state() const; ///< See FLAC__stream_encoder_get_verify_decoder_state()
  142. 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()
  143. virtual bool get_verify() const; ///< See FLAC__stream_encoder_get_verify()
  144. virtual bool get_streamable_subset() const; ///< See FLAC__stream_encoder_get_streamable_subset()
  145. virtual bool get_do_mid_side_stereo() const; ///< See FLAC__stream_encoder_get_do_mid_side_stereo()
  146. virtual bool get_loose_mid_side_stereo() const; ///< See FLAC__stream_encoder_get_loose_mid_side_stereo()
  147. virtual unsigned get_channels() const; ///< See FLAC__stream_encoder_get_channels()
  148. virtual unsigned get_bits_per_sample() const; ///< See FLAC__stream_encoder_get_bits_per_sample()
  149. virtual unsigned get_sample_rate() const; ///< See FLAC__stream_encoder_get_sample_rate()
  150. virtual unsigned get_blocksize() const; ///< See FLAC__stream_encoder_get_blocksize()
  151. virtual unsigned get_max_lpc_order() const; ///< See FLAC__stream_encoder_get_max_lpc_order()
  152. virtual unsigned get_qlp_coeff_precision() const; ///< See FLAC__stream_encoder_get_qlp_coeff_precision()
  153. virtual bool get_do_qlp_coeff_prec_search() const; ///< See FLAC__stream_encoder_get_do_qlp_coeff_prec_search()
  154. virtual bool get_do_escape_coding() const; ///< See FLAC__stream_encoder_get_do_escape_coding()
  155. virtual bool get_do_exhaustive_model_search() const; ///< See FLAC__stream_encoder_get_do_exhaustive_model_search()
  156. virtual unsigned get_min_residual_partition_order() const; ///< See FLAC__stream_encoder_get_min_residual_partition_order()
  157. virtual unsigned get_max_residual_partition_order() const; ///< See FLAC__stream_encoder_get_max_residual_partition_order()
  158. virtual unsigned get_rice_parameter_search_dist() const; ///< See FLAC__stream_encoder_get_rice_parameter_search_dist()
  159. virtual FLAC__uint64 get_total_samples_estimate() const; ///< See FLAC__stream_encoder_get_total_samples_estimate()
  160. virtual ::FLAC__StreamEncoderInitStatus init(); ///< See FLAC__stream_encoder_init_stream()
  161. virtual ::FLAC__StreamEncoderInitStatus init_ogg(); ///< See FLAC__stream_encoder_init_ogg_stream()
  162. virtual bool finish(); ///< See FLAC__stream_encoder_finish()
  163. virtual bool process(const FLAC__int32 * const buffer[], unsigned samples); ///< See FLAC__stream_encoder_process()
  164. virtual bool process_interleaved(const FLAC__int32 buffer[], unsigned samples); ///< See FLAC__stream_encoder_process_interleaved()
  165. protected:
  166. /// See FLAC__StreamEncoderReadCallback
  167. virtual ::FLAC__StreamEncoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes);
  168. /// See FLAC__StreamEncoderWriteCallback
  169. virtual ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame) = 0;
  170. /// See FLAC__StreamEncoderSeekCallback
  171. virtual ::FLAC__StreamEncoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
  172. /// See FLAC__StreamEncoderTellCallback
  173. virtual ::FLAC__StreamEncoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
  174. /// See FLAC__StreamEncoderMetadataCallback
  175. virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata);
  176. #if (defined _MSC_VER) || (defined __BORLANDC__) || (defined __GNUG__ && (__GNUG__ < 2 || (__GNUG__ == 2 && __GNUC_MINOR__ < 96))) || (defined __SUNPRO_CC)
  177. // lame hack: some MSVC/GCC versions can't see a protected encoder_ from nested State::resolved_as_cstring()
  178. friend State;
  179. #endif
  180. ::FLAC__StreamEncoder *encoder_;
  181. static ::FLAC__StreamEncoderReadStatus read_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
  182. static ::FLAC__StreamEncoderWriteStatus write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data);
  183. static ::FLAC__StreamEncoderSeekStatus seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
  184. static ::FLAC__StreamEncoderTellStatus tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
  185. static void metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetadata *metadata, void *client_data);
  186. private:
  187. // Private and undefined so you can't use them:
  188. Stream(const Stream &);
  189. void operator=(const Stream &);
  190. };
  191. /** \ingroup flacpp_encoder
  192. * \brief
  193. * This class wraps the ::FLAC__StreamEncoder. If you are
  194. * not encoding to a file, you may need to use
  195. * FLAC::Encoder::Stream.
  196. *
  197. * The usage of this class is similar to FLAC__StreamEncoder,
  198. * except instead of providing callbacks to
  199. * FLAC__stream_encoder_init*_FILE() or
  200. * FLAC__stream_encoder_init*_file(), you will inherit from this
  201. * class and override the virtual callback functions with your
  202. * own implementations, then call init() or init_ogg(). The rest
  203. * of the calls work the same as in the C layer.
  204. *
  205. * There are no mandatory callbacks; all the callbacks from
  206. * FLAC::Encoder::Stream are implemented here fully and support
  207. * full post-encode STREAMINFO and SEEKTABLE updating. There is
  208. * only an optional progress callback which you may override to
  209. * get periodic reports on the progress of the encode.
  210. */
  211. class FLACPP_API File: public Stream {
  212. public:
  213. File();
  214. virtual ~File();
  215. virtual ::FLAC__StreamEncoderInitStatus init(FILE *file); ///< See FLAC__stream_encoder_init_FILE()
  216. virtual ::FLAC__StreamEncoderInitStatus init(const char *filename); ///< See FLAC__stream_encoder_init_file()
  217. virtual ::FLAC__StreamEncoderInitStatus init(const std::string &filename); ///< See FLAC__stream_encoder_init_file()
  218. virtual ::FLAC__StreamEncoderInitStatus init_ogg(FILE *file); ///< See FLAC__stream_encoder_init_ogg_FILE()
  219. virtual ::FLAC__StreamEncoderInitStatus init_ogg(const char *filename); ///< See FLAC__stream_encoder_init_ogg_file()
  220. virtual ::FLAC__StreamEncoderInitStatus init_ogg(const std::string &filename); ///< See FLAC__stream_encoder_init_ogg_file()
  221. protected:
  222. /// See FLAC__StreamEncoderProgressCallback
  223. virtual void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate);
  224. /// This is a dummy implementation to satisfy the pure virtual in Stream that is actually supplied internally by the C layer
  225. virtual ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame);
  226. private:
  227. 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);
  228. // Private and undefined so you can't use them:
  229. File(const Stream &);
  230. void operator=(const Stream &);
  231. };
  232. }
  233. }
  234. #endif