change "bytes" parameter of all encoder write callbacks from "unsigned" to "size_t"
[platform/upstream/flac.git] / src / libFLAC++ / stream_encoder.cpp
index 27e5d6b..cd17a76 100644 (file)
@@ -1,5 +1,5 @@
 /* libFLAC++ - Free Lossless Audio Codec library
- * Copyright (C) 2002,2003  Josh Coalson
+ * Copyright (C) 2002,2003,2004,2005,2006  Josh Coalson
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  */
 
 #include "FLAC++/encoder.h"
+#include "FLAC++/metadata.h"
 #include "FLAC/assert.h"
 
+#ifdef _MSC_VER
+// warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
+#pragma warning ( disable : 4800 )
+#endif
+
 namespace FLAC {
        namespace Encoder {
 
-               const char *Stream::State::resolved_as_cstring(const Stream &encoder) const
-               {
-                       if(state_ == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
-                               return encoder.get_verify_decoder_state().as_cstring();
-                       else
-                               return as_cstring();
-               }
+               // ------------------------------------------------------------
+               //
+               // Stream
+               //
+               // ------------------------------------------------------------
 
                Stream::Stream():
                encoder_(::FLAC__stream_encoder_new())
                { }
 
+               Stream::Stream(::FLAC__StreamEncoder *encoder):
+               encoder_(encoder)
+               { }
+
                Stream::~Stream()
                {
+                       // WATCHOUT: must check for NULL not only because
+                       // ::FLAC__stream_encoder_new() might have failed in
+                       // the constructor, but also because
+                       // OggFLAC::Encoder::Stream deletes the encoder_ before
+                       // we get to it here to make the C inheritance magic
+                       // work.
                        if(0 != encoder_) {
                                ::FLAC__stream_encoder_finish(encoder_);
                                ::FLAC__stream_encoder_delete(encoder_);
@@ -60,6 +74,12 @@ namespace FLAC {
                        return 0 != encoder_;
                }
 
+               bool Stream::set_ogg_serial_number(long value)
+               {
+                       FLAC__ASSERT(is_valid());
+                       return (bool)::FLAC__stream_encoder_set_ogg_serial_number(encoder_, value);
+               }
+
                bool Stream::set_verify(bool value)
                {
                        FLAC__ASSERT(is_valid());
@@ -108,6 +128,12 @@ namespace FLAC {
                        return (bool)::FLAC__stream_encoder_set_blocksize(encoder_, value);
                }
 
+               bool Stream::set_apodization(const char *specification)
+               {
+                       FLAC__ASSERT(is_valid());
+                       return (bool)::FLAC__stream_encoder_set_apodization(encoder_, specification);
+               }
+
                bool Stream::set_max_lpc_order(unsigned value)
                {
                        FLAC__ASSERT(is_valid());
@@ -168,6 +194,31 @@ namespace FLAC {
                        return (bool)::FLAC__stream_encoder_set_metadata(encoder_, metadata, num_blocks);
                }
 
+               bool Stream::set_metadata(FLAC::Metadata::Prototype **metadata, unsigned num_blocks)
+               {
+                       FLAC__ASSERT(is_valid());
+#if (defined _MSC_VER) || (defined __SUNPRO_CC)
+                       // MSVC++ can't handle:
+                       // ::FLAC__StreamMetadata *m[num_blocks];
+                       // so we do this ugly workaround
+                       ::FLAC__StreamMetadata **m = new ::FLAC__StreamMetadata*[num_blocks];
+#else
+                       ::FLAC__StreamMetadata *m[num_blocks];
+#endif
+                       for(unsigned i = 0; i < num_blocks; i++) {
+                               // we can get away with the const_cast since we know the encoder will only correct the is_last flags
+                               m[i] = const_cast< ::FLAC__StreamMetadata*>((const ::FLAC__StreamMetadata*)metadata[i]);
+                       }
+#if (defined _MSC_VER) || (defined __SUNPRO_CC)
+                       // complete the hack
+                       const bool ok = (bool)::FLAC__stream_encoder_set_metadata(encoder_, m, num_blocks);
+                       delete [] m;
+                       return ok;
+#else
+                       return (bool)::FLAC__stream_encoder_set_metadata(encoder_, m, num_blocks);
+#endif
+               }
+
                Stream::State Stream::get_state() const
                {
                        FLAC__ASSERT(is_valid());
@@ -288,13 +339,16 @@ namespace FLAC {
                        return ::FLAC__stream_encoder_get_total_samples_estimate(encoder_);
                }
 
-               Stream::State Stream::init()
+               ::FLAC__StreamEncoderInitStatus Stream::init()
                {
                        FLAC__ASSERT(is_valid());
-                       ::FLAC__stream_encoder_set_write_callback(encoder_, write_callback_);
-                       ::FLAC__stream_encoder_set_metadata_callback(encoder_, metadata_callback_);
-                       ::FLAC__stream_encoder_set_client_data(encoder_, (void*)this);
-                       return State(::FLAC__stream_encoder_init(encoder_));
+                       return ::FLAC__stream_encoder_init_stream(encoder_, write_callback_, seek_callback_, tell_callback_, metadata_callback_, /*client_data=*/(void*)this);
+               }
+
+               ::FLAC__StreamEncoderInitStatus Stream::init_ogg()
+               {
+                       FLAC__ASSERT(is_valid());
+                       return ::FLAC__stream_encoder_init_ogg_stream(encoder_, read_callback_, write_callback_, seek_callback_, tell_callback_, metadata_callback_, /*client_data=*/(void*)this);
                }
 
                void Stream::finish()
@@ -315,7 +369,39 @@ namespace FLAC {
                        return (bool)::FLAC__stream_encoder_process_interleaved(encoder_, buffer, samples);
                }
 
-               ::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
+               ::FLAC__StreamEncoderReadStatus Stream::read_callback(FLAC__byte buffer[], size_t *bytes)
+               {
+                       (void)buffer, (void)bytes;
+                       return ::FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED;
+               }
+
+               ::FLAC__StreamEncoderSeekStatus Stream::seek_callback(FLAC__uint64 absolute_byte_offset)
+               {
+                       (void)absolute_byte_offset;
+                       return ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED;
+               }
+
+               ::FLAC__StreamEncoderTellStatus Stream::tell_callback(FLAC__uint64 *absolute_byte_offset)
+               {
+                       (void)absolute_byte_offset;
+                       return ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED;
+               }
+
+               void Stream::metadata_callback(const ::FLAC__StreamMetadata *metadata)
+               {
+                       (void)metadata;
+               }
+
+               ::FLAC__StreamEncoderReadStatus Stream::read_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
+               {
+                       (void)encoder;
+                       FLAC__ASSERT(0 != client_data);
+                       Stream *instance = reinterpret_cast<Stream *>(client_data);
+                       FLAC__ASSERT(0 != instance);
+                       return instance->read_callback(buffer, bytes);
+               }
+
+               ::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
                {
                        (void)encoder;
                        FLAC__ASSERT(0 != client_data);
@@ -324,6 +410,24 @@ namespace FLAC {
                        return instance->write_callback(buffer, bytes, samples, current_frame);
                }
 
+               ::FLAC__StreamEncoderSeekStatus Stream::seek_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
+               {
+                       (void)encoder;
+                       FLAC__ASSERT(0 != client_data);
+                       Stream *instance = reinterpret_cast<Stream *>(client_data);
+                       FLAC__ASSERT(0 != instance);
+                       return instance->seek_callback(absolute_byte_offset);
+               }
+
+               ::FLAC__StreamEncoderTellStatus Stream::tell_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
+               {
+                       (void)encoder;
+                       FLAC__ASSERT(0 != client_data);
+                       Stream *instance = reinterpret_cast<Stream *>(client_data);
+                       FLAC__ASSERT(0 != instance);
+                       return instance->tell_callback(absolute_byte_offset);
+               }
+
                void Stream::metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
                {
                        (void)encoder;
@@ -333,5 +437,79 @@ namespace FLAC {
                        instance->metadata_callback(metadata);
                }
 
-       };
-};
+               // ------------------------------------------------------------
+               //
+               // File
+               //
+               // ------------------------------------------------------------
+
+               File::File():
+                       Stream()
+               { }
+
+               File::~File()
+               {
+               }
+
+               ::FLAC__StreamEncoderInitStatus File::init(FILE *file)
+               {
+                       FLAC__ASSERT(is_valid());
+                       return ::FLAC__stream_encoder_init_FILE(encoder_, file, progress_callback_, /*client_data=*/(void*)this);
+               }
+
+               ::FLAC__StreamEncoderInitStatus File::init(const char *filename)
+               {
+                       FLAC__ASSERT(is_valid());
+                       return ::FLAC__stream_encoder_init_file(encoder_, filename, progress_callback_, /*client_data=*/(void*)this);
+               }
+
+               ::FLAC__StreamEncoderInitStatus File::init(const std::string &filename)
+               {
+                       return init(filename.c_str());
+               }
+
+               ::FLAC__StreamEncoderInitStatus File::init_ogg(FILE *file)
+               {
+                       FLAC__ASSERT(is_valid());
+                       return ::FLAC__stream_encoder_init_ogg_FILE(encoder_, file, progress_callback_, /*client_data=*/(void*)this);
+               }
+
+               ::FLAC__StreamEncoderInitStatus File::init_ogg(const char *filename)
+               {
+                       FLAC__ASSERT(is_valid());
+                       return ::FLAC__stream_encoder_init_ogg_file(encoder_, filename, progress_callback_, /*client_data=*/(void*)this);
+               }
+
+               ::FLAC__StreamEncoderInitStatus File::init_ogg(const std::string &filename)
+               {
+                       return init_ogg(filename.c_str());
+               }
+
+               // This is a dummy to satisfy the pure virtual from Stream; the
+               // read callback will never be called since we are initializing
+               // with FLAC__stream_decoder_init_FILE() or
+               // FLAC__stream_decoder_init_file() and those supply the read
+               // callback internally.
+               ::FLAC__StreamEncoderWriteStatus File::write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame)
+               {
+                       (void)buffer, (void)bytes, (void)samples, (void)current_frame;
+                       FLAC__ASSERT(false);
+                       return ::FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; // double protection
+               }
+
+               void File::progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate)
+               {
+                       (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate;
+               }
+
+               void File::progress_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
+               {
+                       (void)encoder;
+                       FLAC__ASSERT(0 != client_data);
+                       File *instance = reinterpret_cast<File *>(client_data);
+                       FLAC__ASSERT(0 != instance);
+                       instance->progress_callback(bytes_written, samples_written, frames_written, total_frames_estimate);
+               }
+
+       }
+}