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 )
45 encoder_(::FLAC__stream_encoder_new())
48 Stream::Stream(::FLAC__StreamEncoder *encoder):
54 // WATCHOUT: must check for NULL not only because
55 // ::FLAC__stream_encoder_new() might have failed in
56 // the constructor, but also because
57 // OggFLAC::Encoder::Stream deletes the encoder_ before
58 // we get to it here to make the C inheritance magic
61 ::FLAC__stream_encoder_finish(encoder_);
62 ::FLAC__stream_encoder_delete(encoder_);
66 bool Stream::is_valid() const
71 bool Stream::set_verify(bool value)
73 FLAC__ASSERT(is_valid());
74 return (bool)::FLAC__stream_encoder_set_verify(encoder_, value);
77 bool Stream::set_streamable_subset(bool value)
79 FLAC__ASSERT(is_valid());
80 return (bool)::FLAC__stream_encoder_set_streamable_subset(encoder_, value);
83 bool Stream::set_do_mid_side_stereo(bool value)
85 FLAC__ASSERT(is_valid());
86 return (bool)::FLAC__stream_encoder_set_do_mid_side_stereo(encoder_, value);
89 bool Stream::set_loose_mid_side_stereo(bool value)
91 FLAC__ASSERT(is_valid());
92 return (bool)::FLAC__stream_encoder_set_loose_mid_side_stereo(encoder_, value);
95 bool Stream::set_channels(unsigned value)
97 FLAC__ASSERT(is_valid());
98 return (bool)::FLAC__stream_encoder_set_channels(encoder_, value);
101 bool Stream::set_bits_per_sample(unsigned value)
103 FLAC__ASSERT(is_valid());
104 return (bool)::FLAC__stream_encoder_set_bits_per_sample(encoder_, value);
107 bool Stream::set_sample_rate(unsigned value)
109 FLAC__ASSERT(is_valid());
110 return (bool)::FLAC__stream_encoder_set_sample_rate(encoder_, value);
113 bool Stream::set_blocksize(unsigned value)
115 FLAC__ASSERT(is_valid());
116 return (bool)::FLAC__stream_encoder_set_blocksize(encoder_, value);
119 bool Stream::set_apodization(const char *specification)
121 FLAC__ASSERT(is_valid());
122 return (bool)::FLAC__stream_encoder_set_apodization(encoder_, specification);
125 bool Stream::set_max_lpc_order(unsigned value)
127 FLAC__ASSERT(is_valid());
128 return (bool)::FLAC__stream_encoder_set_max_lpc_order(encoder_, value);
131 bool Stream::set_qlp_coeff_precision(unsigned value)
133 FLAC__ASSERT(is_valid());
134 return (bool)::FLAC__stream_encoder_set_qlp_coeff_precision(encoder_, value);
137 bool Stream::set_do_qlp_coeff_prec_search(bool value)
139 FLAC__ASSERT(is_valid());
140 return (bool)::FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder_, value);
143 bool Stream::set_do_escape_coding(bool value)
145 FLAC__ASSERT(is_valid());
146 return (bool)::FLAC__stream_encoder_set_do_escape_coding(encoder_, value);
149 bool Stream::set_do_exhaustive_model_search(bool value)
151 FLAC__ASSERT(is_valid());
152 return (bool)::FLAC__stream_encoder_set_do_exhaustive_model_search(encoder_, value);
155 bool Stream::set_min_residual_partition_order(unsigned value)
157 FLAC__ASSERT(is_valid());
158 return (bool)::FLAC__stream_encoder_set_min_residual_partition_order(encoder_, value);
161 bool Stream::set_max_residual_partition_order(unsigned value)
163 FLAC__ASSERT(is_valid());
164 return (bool)::FLAC__stream_encoder_set_max_residual_partition_order(encoder_, value);
167 bool Stream::set_rice_parameter_search_dist(unsigned value)
169 FLAC__ASSERT(is_valid());
170 return (bool)::FLAC__stream_encoder_set_rice_parameter_search_dist(encoder_, value);
173 bool Stream::set_total_samples_estimate(FLAC__uint64 value)
175 FLAC__ASSERT(is_valid());
176 return (bool)::FLAC__stream_encoder_set_total_samples_estimate(encoder_, value);
179 bool Stream::set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks)
181 FLAC__ASSERT(is_valid());
182 return (bool)::FLAC__stream_encoder_set_metadata(encoder_, metadata, num_blocks);
185 bool Stream::set_metadata(FLAC::Metadata::Prototype **metadata, unsigned num_blocks)
187 FLAC__ASSERT(is_valid());
188 #if (defined _MSC_VER) || (defined __SUNPRO_CC)
189 // MSVC++ can't handle:
190 // ::FLAC__StreamMetadata *m[num_blocks];
191 // so we do this ugly workaround
192 ::FLAC__StreamMetadata **m = new ::FLAC__StreamMetadata*[num_blocks];
194 ::FLAC__StreamMetadata *m[num_blocks];
196 for(unsigned i = 0; i < num_blocks; i++) {
197 // we can get away with the const_cast since we know the encoder will only correct the is_last flags
198 m[i] = const_cast< ::FLAC__StreamMetadata*>((const ::FLAC__StreamMetadata*)metadata[i]);
200 #if (defined _MSC_VER) || (defined __SUNPRO_CC)
202 const bool ok = (bool)::FLAC__stream_encoder_set_metadata(encoder_, m, num_blocks);
206 return (bool)::FLAC__stream_encoder_set_metadata(encoder_, m, num_blocks);
210 Stream::State Stream::get_state() const
212 FLAC__ASSERT(is_valid());
213 return State(::FLAC__stream_encoder_get_state(encoder_));
216 Decoder::Stream::State Stream::get_verify_decoder_state() const
218 FLAC__ASSERT(is_valid());
219 return Decoder::Stream::State(::FLAC__stream_encoder_get_verify_decoder_state(encoder_));
222 void Stream::get_verify_decoder_error_stats(FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
224 FLAC__ASSERT(is_valid());
225 ::FLAC__stream_encoder_get_verify_decoder_error_stats(encoder_, absolute_sample, frame_number, channel, sample, expected, got);
228 bool Stream::get_verify() const
230 FLAC__ASSERT(is_valid());
231 return (bool)::FLAC__stream_encoder_get_verify(encoder_);
234 bool Stream::get_streamable_subset() const
236 FLAC__ASSERT(is_valid());
237 return (bool)::FLAC__stream_encoder_get_streamable_subset(encoder_);
240 bool Stream::get_do_mid_side_stereo() const
242 FLAC__ASSERT(is_valid());
243 return (bool)::FLAC__stream_encoder_get_do_mid_side_stereo(encoder_);
246 bool Stream::get_loose_mid_side_stereo() const
248 FLAC__ASSERT(is_valid());
249 return (bool)::FLAC__stream_encoder_get_loose_mid_side_stereo(encoder_);
252 unsigned Stream::get_channels() const
254 FLAC__ASSERT(is_valid());
255 return ::FLAC__stream_encoder_get_channels(encoder_);
258 unsigned Stream::get_bits_per_sample() const
260 FLAC__ASSERT(is_valid());
261 return ::FLAC__stream_encoder_get_bits_per_sample(encoder_);
264 unsigned Stream::get_sample_rate() const
266 FLAC__ASSERT(is_valid());
267 return ::FLAC__stream_encoder_get_sample_rate(encoder_);
270 unsigned Stream::get_blocksize() const
272 FLAC__ASSERT(is_valid());
273 return ::FLAC__stream_encoder_get_blocksize(encoder_);
276 unsigned Stream::get_max_lpc_order() const
278 FLAC__ASSERT(is_valid());
279 return ::FLAC__stream_encoder_get_max_lpc_order(encoder_);
282 unsigned Stream::get_qlp_coeff_precision() const
284 FLAC__ASSERT(is_valid());
285 return ::FLAC__stream_encoder_get_qlp_coeff_precision(encoder_);
288 bool Stream::get_do_qlp_coeff_prec_search() const
290 FLAC__ASSERT(is_valid());
291 return (bool)::FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder_);
294 bool Stream::get_do_escape_coding() const
296 FLAC__ASSERT(is_valid());
297 return (bool)::FLAC__stream_encoder_get_do_escape_coding(encoder_);
300 bool Stream::get_do_exhaustive_model_search() const
302 FLAC__ASSERT(is_valid());
303 return (bool)::FLAC__stream_encoder_get_do_exhaustive_model_search(encoder_);
306 unsigned Stream::get_min_residual_partition_order() const
308 FLAC__ASSERT(is_valid());
309 return ::FLAC__stream_encoder_get_min_residual_partition_order(encoder_);
312 unsigned Stream::get_max_residual_partition_order() const
314 FLAC__ASSERT(is_valid());
315 return ::FLAC__stream_encoder_get_max_residual_partition_order(encoder_);
318 unsigned Stream::get_rice_parameter_search_dist() const
320 FLAC__ASSERT(is_valid());
321 return ::FLAC__stream_encoder_get_rice_parameter_search_dist(encoder_);
324 FLAC__uint64 Stream::get_total_samples_estimate() const
326 FLAC__ASSERT(is_valid());
327 return ::FLAC__stream_encoder_get_total_samples_estimate(encoder_);
330 ::FLAC__StreamEncoderInitStatus Stream::init()
332 FLAC__ASSERT(is_valid());
333 return ::FLAC__stream_encoder_init_stream(encoder_, write_callback_, seek_callback_, tell_callback_, metadata_callback_, /*client_data=*/(void*)this);
336 void Stream::finish()
338 FLAC__ASSERT(is_valid());
339 ::FLAC__stream_encoder_finish(encoder_);
342 bool Stream::process(const FLAC__int32 * const buffer[], unsigned samples)
344 FLAC__ASSERT(is_valid());
345 return (bool)::FLAC__stream_encoder_process(encoder_, buffer, samples);
348 bool Stream::process_interleaved(const FLAC__int32 buffer[], unsigned samples)
350 FLAC__ASSERT(is_valid());
351 return (bool)::FLAC__stream_encoder_process_interleaved(encoder_, buffer, samples);
354 ::FLAC__StreamEncoderSeekStatus Stream::seek_callback(FLAC__uint64 absolute_byte_offset)
356 (void)absolute_byte_offset;
357 return ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED;
360 ::FLAC__StreamEncoderTellStatus Stream::tell_callback(FLAC__uint64 *absolute_byte_offset)
362 (void)absolute_byte_offset;
363 return ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED;
366 void Stream::metadata_callback(const ::FLAC__StreamMetadata *metadata)
371 ::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
374 FLAC__ASSERT(0 != client_data);
375 Stream *instance = reinterpret_cast<Stream *>(client_data);
376 FLAC__ASSERT(0 != instance);
377 return instance->write_callback(buffer, bytes, samples, current_frame);
380 ::FLAC__StreamEncoderSeekStatus Stream::seek_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
383 FLAC__ASSERT(0 != client_data);
384 Stream *instance = reinterpret_cast<Stream *>(client_data);
385 FLAC__ASSERT(0 != instance);
386 return instance->seek_callback(absolute_byte_offset);
389 ::FLAC__StreamEncoderTellStatus Stream::tell_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
392 FLAC__ASSERT(0 != client_data);
393 Stream *instance = reinterpret_cast<Stream *>(client_data);
394 FLAC__ASSERT(0 != instance);
395 return instance->tell_callback(absolute_byte_offset);
398 void Stream::metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
401 FLAC__ASSERT(0 != client_data);
402 Stream *instance = reinterpret_cast<Stream *>(client_data);
403 FLAC__ASSERT(0 != instance);
404 instance->metadata_callback(metadata);