1 /* libFLAC++ - Free Lossless Audio Codec library
2 * Copyright (C) 2002,2003,2004,2005,2006 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "FLAC++/encoder.h"
33 #include "FLAC++/metadata.h"
34 #include "FLAC/assert.h"
37 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
38 #pragma warning ( disable : 4800 )
44 // ------------------------------------------------------------
48 // ------------------------------------------------------------
51 encoder_(::FLAC__stream_encoder_new())
54 Stream::Stream(::FLAC__StreamEncoder *encoder):
60 // WATCHOUT: must check for NULL not only because
61 // ::FLAC__stream_encoder_new() might have failed in
62 // the constructor, but also because
63 // OggFLAC::Encoder::Stream deletes the encoder_ before
64 // we get to it here to make the C inheritance magic
67 ::FLAC__stream_encoder_finish(encoder_);
68 ::FLAC__stream_encoder_delete(encoder_);
72 bool Stream::is_valid() const
77 bool Stream::set_ogg_serial_number(long value)
79 FLAC__ASSERT(is_valid());
80 return (bool)::FLAC__stream_encoder_set_ogg_serial_number(encoder_, value);
83 bool Stream::set_verify(bool value)
85 FLAC__ASSERT(is_valid());
86 return (bool)::FLAC__stream_encoder_set_verify(encoder_, value);
89 bool Stream::set_streamable_subset(bool value)
91 FLAC__ASSERT(is_valid());
92 return (bool)::FLAC__stream_encoder_set_streamable_subset(encoder_, value);
95 bool Stream::set_do_mid_side_stereo(bool value)
97 FLAC__ASSERT(is_valid());
98 return (bool)::FLAC__stream_encoder_set_do_mid_side_stereo(encoder_, value);
101 bool Stream::set_loose_mid_side_stereo(bool value)
103 FLAC__ASSERT(is_valid());
104 return (bool)::FLAC__stream_encoder_set_loose_mid_side_stereo(encoder_, value);
107 bool Stream::set_channels(unsigned value)
109 FLAC__ASSERT(is_valid());
110 return (bool)::FLAC__stream_encoder_set_channels(encoder_, value);
113 bool Stream::set_bits_per_sample(unsigned value)
115 FLAC__ASSERT(is_valid());
116 return (bool)::FLAC__stream_encoder_set_bits_per_sample(encoder_, value);
119 bool Stream::set_sample_rate(unsigned value)
121 FLAC__ASSERT(is_valid());
122 return (bool)::FLAC__stream_encoder_set_sample_rate(encoder_, value);
125 bool Stream::set_blocksize(unsigned value)
127 FLAC__ASSERT(is_valid());
128 return (bool)::FLAC__stream_encoder_set_blocksize(encoder_, value);
131 bool Stream::set_apodization(const char *specification)
133 FLAC__ASSERT(is_valid());
134 return (bool)::FLAC__stream_encoder_set_apodization(encoder_, specification);
137 bool Stream::set_max_lpc_order(unsigned value)
139 FLAC__ASSERT(is_valid());
140 return (bool)::FLAC__stream_encoder_set_max_lpc_order(encoder_, value);
143 bool Stream::set_qlp_coeff_precision(unsigned value)
145 FLAC__ASSERT(is_valid());
146 return (bool)::FLAC__stream_encoder_set_qlp_coeff_precision(encoder_, value);
149 bool Stream::set_do_qlp_coeff_prec_search(bool value)
151 FLAC__ASSERT(is_valid());
152 return (bool)::FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder_, value);
155 bool Stream::set_do_escape_coding(bool value)
157 FLAC__ASSERT(is_valid());
158 return (bool)::FLAC__stream_encoder_set_do_escape_coding(encoder_, value);
161 bool Stream::set_do_exhaustive_model_search(bool value)
163 FLAC__ASSERT(is_valid());
164 return (bool)::FLAC__stream_encoder_set_do_exhaustive_model_search(encoder_, value);
167 bool Stream::set_min_residual_partition_order(unsigned value)
169 FLAC__ASSERT(is_valid());
170 return (bool)::FLAC__stream_encoder_set_min_residual_partition_order(encoder_, value);
173 bool Stream::set_max_residual_partition_order(unsigned value)
175 FLAC__ASSERT(is_valid());
176 return (bool)::FLAC__stream_encoder_set_max_residual_partition_order(encoder_, value);
179 bool Stream::set_rice_parameter_search_dist(unsigned value)
181 FLAC__ASSERT(is_valid());
182 return (bool)::FLAC__stream_encoder_set_rice_parameter_search_dist(encoder_, value);
185 bool Stream::set_total_samples_estimate(FLAC__uint64 value)
187 FLAC__ASSERT(is_valid());
188 return (bool)::FLAC__stream_encoder_set_total_samples_estimate(encoder_, value);
191 bool Stream::set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks)
193 FLAC__ASSERT(is_valid());
194 return (bool)::FLAC__stream_encoder_set_metadata(encoder_, metadata, num_blocks);
197 bool Stream::set_metadata(FLAC::Metadata::Prototype **metadata, unsigned num_blocks)
199 FLAC__ASSERT(is_valid());
200 #if (defined _MSC_VER) || (defined __SUNPRO_CC)
201 // MSVC++ can't handle:
202 // ::FLAC__StreamMetadata *m[num_blocks];
203 // so we do this ugly workaround
204 ::FLAC__StreamMetadata **m = new ::FLAC__StreamMetadata*[num_blocks];
206 ::FLAC__StreamMetadata *m[num_blocks];
208 for(unsigned i = 0; i < num_blocks; i++) {
209 // we can get away with the const_cast since we know the encoder will only correct the is_last flags
210 m[i] = const_cast< ::FLAC__StreamMetadata*>((const ::FLAC__StreamMetadata*)metadata[i]);
212 #if (defined _MSC_VER) || (defined __SUNPRO_CC)
214 const bool ok = (bool)::FLAC__stream_encoder_set_metadata(encoder_, m, num_blocks);
218 return (bool)::FLAC__stream_encoder_set_metadata(encoder_, m, num_blocks);
222 Stream::State Stream::get_state() const
224 FLAC__ASSERT(is_valid());
225 return State(::FLAC__stream_encoder_get_state(encoder_));
228 Decoder::Stream::State Stream::get_verify_decoder_state() const
230 FLAC__ASSERT(is_valid());
231 return Decoder::Stream::State(::FLAC__stream_encoder_get_verify_decoder_state(encoder_));
234 void Stream::get_verify_decoder_error_stats(FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
236 FLAC__ASSERT(is_valid());
237 ::FLAC__stream_encoder_get_verify_decoder_error_stats(encoder_, absolute_sample, frame_number, channel, sample, expected, got);
240 bool Stream::get_verify() const
242 FLAC__ASSERT(is_valid());
243 return (bool)::FLAC__stream_encoder_get_verify(encoder_);
246 bool Stream::get_streamable_subset() const
248 FLAC__ASSERT(is_valid());
249 return (bool)::FLAC__stream_encoder_get_streamable_subset(encoder_);
252 bool Stream::get_do_mid_side_stereo() const
254 FLAC__ASSERT(is_valid());
255 return (bool)::FLAC__stream_encoder_get_do_mid_side_stereo(encoder_);
258 bool Stream::get_loose_mid_side_stereo() const
260 FLAC__ASSERT(is_valid());
261 return (bool)::FLAC__stream_encoder_get_loose_mid_side_stereo(encoder_);
264 unsigned Stream::get_channels() const
266 FLAC__ASSERT(is_valid());
267 return ::FLAC__stream_encoder_get_channels(encoder_);
270 unsigned Stream::get_bits_per_sample() const
272 FLAC__ASSERT(is_valid());
273 return ::FLAC__stream_encoder_get_bits_per_sample(encoder_);
276 unsigned Stream::get_sample_rate() const
278 FLAC__ASSERT(is_valid());
279 return ::FLAC__stream_encoder_get_sample_rate(encoder_);
282 unsigned Stream::get_blocksize() const
284 FLAC__ASSERT(is_valid());
285 return ::FLAC__stream_encoder_get_blocksize(encoder_);
288 unsigned Stream::get_max_lpc_order() const
290 FLAC__ASSERT(is_valid());
291 return ::FLAC__stream_encoder_get_max_lpc_order(encoder_);
294 unsigned Stream::get_qlp_coeff_precision() const
296 FLAC__ASSERT(is_valid());
297 return ::FLAC__stream_encoder_get_qlp_coeff_precision(encoder_);
300 bool Stream::get_do_qlp_coeff_prec_search() const
302 FLAC__ASSERT(is_valid());
303 return (bool)::FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder_);
306 bool Stream::get_do_escape_coding() const
308 FLAC__ASSERT(is_valid());
309 return (bool)::FLAC__stream_encoder_get_do_escape_coding(encoder_);
312 bool Stream::get_do_exhaustive_model_search() const
314 FLAC__ASSERT(is_valid());
315 return (bool)::FLAC__stream_encoder_get_do_exhaustive_model_search(encoder_);
318 unsigned Stream::get_min_residual_partition_order() const
320 FLAC__ASSERT(is_valid());
321 return ::FLAC__stream_encoder_get_min_residual_partition_order(encoder_);
324 unsigned Stream::get_max_residual_partition_order() const
326 FLAC__ASSERT(is_valid());
327 return ::FLAC__stream_encoder_get_max_residual_partition_order(encoder_);
330 unsigned Stream::get_rice_parameter_search_dist() const
332 FLAC__ASSERT(is_valid());
333 return ::FLAC__stream_encoder_get_rice_parameter_search_dist(encoder_);
336 FLAC__uint64 Stream::get_total_samples_estimate() const
338 FLAC__ASSERT(is_valid());
339 return ::FLAC__stream_encoder_get_total_samples_estimate(encoder_);
342 ::FLAC__StreamEncoderInitStatus Stream::init()
344 FLAC__ASSERT(is_valid());
345 return ::FLAC__stream_encoder_init_stream(encoder_, write_callback_, seek_callback_, tell_callback_, metadata_callback_, /*client_data=*/(void*)this);
348 ::FLAC__StreamEncoderInitStatus Stream::init_ogg()
350 FLAC__ASSERT(is_valid());
351 return ::FLAC__stream_encoder_init_ogg_stream(encoder_, read_callback_, write_callback_, seek_callback_, tell_callback_, metadata_callback_, /*client_data=*/(void*)this);
354 void Stream::finish()
356 FLAC__ASSERT(is_valid());
357 ::FLAC__stream_encoder_finish(encoder_);
360 bool Stream::process(const FLAC__int32 * const buffer[], unsigned samples)
362 FLAC__ASSERT(is_valid());
363 return (bool)::FLAC__stream_encoder_process(encoder_, buffer, samples);
366 bool Stream::process_interleaved(const FLAC__int32 buffer[], unsigned samples)
368 FLAC__ASSERT(is_valid());
369 return (bool)::FLAC__stream_encoder_process_interleaved(encoder_, buffer, samples);
372 ::FLAC__StreamEncoderReadStatus Stream::read_callback(FLAC__byte buffer[], size_t *bytes)
374 (void)buffer, (void)bytes;
375 return ::FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED;
378 ::FLAC__StreamEncoderSeekStatus Stream::seek_callback(FLAC__uint64 absolute_byte_offset)
380 (void)absolute_byte_offset;
381 return ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED;
384 ::FLAC__StreamEncoderTellStatus Stream::tell_callback(FLAC__uint64 *absolute_byte_offset)
386 (void)absolute_byte_offset;
387 return ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED;
390 void Stream::metadata_callback(const ::FLAC__StreamMetadata *metadata)
395 ::FLAC__StreamEncoderReadStatus Stream::read_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
398 FLAC__ASSERT(0 != client_data);
399 Stream *instance = reinterpret_cast<Stream *>(client_data);
400 FLAC__ASSERT(0 != instance);
401 return instance->read_callback(buffer, bytes);
404 ::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
407 FLAC__ASSERT(0 != client_data);
408 Stream *instance = reinterpret_cast<Stream *>(client_data);
409 FLAC__ASSERT(0 != instance);
410 return instance->write_callback(buffer, bytes, samples, current_frame);
413 ::FLAC__StreamEncoderSeekStatus Stream::seek_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
416 FLAC__ASSERT(0 != client_data);
417 Stream *instance = reinterpret_cast<Stream *>(client_data);
418 FLAC__ASSERT(0 != instance);
419 return instance->seek_callback(absolute_byte_offset);
422 ::FLAC__StreamEncoderTellStatus Stream::tell_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
425 FLAC__ASSERT(0 != client_data);
426 Stream *instance = reinterpret_cast<Stream *>(client_data);
427 FLAC__ASSERT(0 != instance);
428 return instance->tell_callback(absolute_byte_offset);
431 void Stream::metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
434 FLAC__ASSERT(0 != client_data);
435 Stream *instance = reinterpret_cast<Stream *>(client_data);
436 FLAC__ASSERT(0 != instance);
437 instance->metadata_callback(metadata);
440 // ------------------------------------------------------------
444 // ------------------------------------------------------------
454 ::FLAC__StreamEncoderInitStatus File::init(FILE *file)
456 FLAC__ASSERT(is_valid());
457 return ::FLAC__stream_encoder_init_FILE(encoder_, file, progress_callback_, /*client_data=*/(void*)this);
460 ::FLAC__StreamEncoderInitStatus File::init(const char *filename)
462 FLAC__ASSERT(is_valid());
463 return ::FLAC__stream_encoder_init_file(encoder_, filename, progress_callback_, /*client_data=*/(void*)this);
466 ::FLAC__StreamEncoderInitStatus File::init(const std::string &filename)
468 return init(filename.c_str());
471 ::FLAC__StreamEncoderInitStatus File::init_ogg(FILE *file)
473 FLAC__ASSERT(is_valid());
474 return ::FLAC__stream_encoder_init_ogg_FILE(encoder_, file, progress_callback_, /*client_data=*/(void*)this);
477 ::FLAC__StreamEncoderInitStatus File::init_ogg(const char *filename)
479 FLAC__ASSERT(is_valid());
480 return ::FLAC__stream_encoder_init_ogg_file(encoder_, filename, progress_callback_, /*client_data=*/(void*)this);
483 ::FLAC__StreamEncoderInitStatus File::init_ogg(const std::string &filename)
485 return init_ogg(filename.c_str());
488 // This is a dummy to satisfy the pure virtual from Stream; the
489 // read callback will never be called since we are initializing
490 // with FLAC__stream_decoder_init_FILE() or
491 // FLAC__stream_decoder_init_file() and those supply the read
492 // callback internally.
493 ::FLAC__StreamEncoderWriteStatus File::write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame)
495 (void)buffer, (void)bytes, (void)samples, (void)current_frame;
497 return ::FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; // double protection
500 void File::progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate)
502 (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate;
505 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)
508 FLAC__ASSERT(0 != client_data);
509 File *instance = reinterpret_cast<File *>(client_data);
510 FLAC__ASSERT(0 != instance);
511 instance->progress_callback(bytes_written, samples_written, frames_written, total_frames_estimate);