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())
51 ::FLAC__stream_encoder_finish(encoder_);
52 ::FLAC__stream_encoder_delete(encoder_);
56 bool Stream::is_valid() const
61 bool Stream::set_verify(bool value)
63 FLAC__ASSERT(is_valid());
64 return (bool)::FLAC__stream_encoder_set_verify(encoder_, value);
67 bool Stream::set_streamable_subset(bool value)
69 FLAC__ASSERT(is_valid());
70 return (bool)::FLAC__stream_encoder_set_streamable_subset(encoder_, value);
73 bool Stream::set_do_mid_side_stereo(bool value)
75 FLAC__ASSERT(is_valid());
76 return (bool)::FLAC__stream_encoder_set_do_mid_side_stereo(encoder_, value);
79 bool Stream::set_loose_mid_side_stereo(bool value)
81 FLAC__ASSERT(is_valid());
82 return (bool)::FLAC__stream_encoder_set_loose_mid_side_stereo(encoder_, value);
85 bool Stream::set_channels(unsigned value)
87 FLAC__ASSERT(is_valid());
88 return (bool)::FLAC__stream_encoder_set_channels(encoder_, value);
91 bool Stream::set_bits_per_sample(unsigned value)
93 FLAC__ASSERT(is_valid());
94 return (bool)::FLAC__stream_encoder_set_bits_per_sample(encoder_, value);
97 bool Stream::set_sample_rate(unsigned value)
99 FLAC__ASSERT(is_valid());
100 return (bool)::FLAC__stream_encoder_set_sample_rate(encoder_, value);
103 bool Stream::set_blocksize(unsigned value)
105 FLAC__ASSERT(is_valid());
106 return (bool)::FLAC__stream_encoder_set_blocksize(encoder_, value);
109 bool Stream::set_apodization(const char *specification)
111 FLAC__ASSERT(is_valid());
112 return (bool)::FLAC__stream_encoder_set_apodization(encoder_, specification);
115 bool Stream::set_max_lpc_order(unsigned value)
117 FLAC__ASSERT(is_valid());
118 return (bool)::FLAC__stream_encoder_set_max_lpc_order(encoder_, value);
121 bool Stream::set_qlp_coeff_precision(unsigned value)
123 FLAC__ASSERT(is_valid());
124 return (bool)::FLAC__stream_encoder_set_qlp_coeff_precision(encoder_, value);
127 bool Stream::set_do_qlp_coeff_prec_search(bool value)
129 FLAC__ASSERT(is_valid());
130 return (bool)::FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder_, value);
133 bool Stream::set_do_escape_coding(bool value)
135 FLAC__ASSERT(is_valid());
136 return (bool)::FLAC__stream_encoder_set_do_escape_coding(encoder_, value);
139 bool Stream::set_do_exhaustive_model_search(bool value)
141 FLAC__ASSERT(is_valid());
142 return (bool)::FLAC__stream_encoder_set_do_exhaustive_model_search(encoder_, value);
145 bool Stream::set_min_residual_partition_order(unsigned value)
147 FLAC__ASSERT(is_valid());
148 return (bool)::FLAC__stream_encoder_set_min_residual_partition_order(encoder_, value);
151 bool Stream::set_max_residual_partition_order(unsigned value)
153 FLAC__ASSERT(is_valid());
154 return (bool)::FLAC__stream_encoder_set_max_residual_partition_order(encoder_, value);
157 bool Stream::set_rice_parameter_search_dist(unsigned value)
159 FLAC__ASSERT(is_valid());
160 return (bool)::FLAC__stream_encoder_set_rice_parameter_search_dist(encoder_, value);
163 bool Stream::set_total_samples_estimate(FLAC__uint64 value)
165 FLAC__ASSERT(is_valid());
166 return (bool)::FLAC__stream_encoder_set_total_samples_estimate(encoder_, value);
169 bool Stream::set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks)
171 FLAC__ASSERT(is_valid());
172 return (bool)::FLAC__stream_encoder_set_metadata(encoder_, metadata, num_blocks);
175 bool Stream::set_metadata(FLAC::Metadata::Prototype **metadata, unsigned num_blocks)
177 FLAC__ASSERT(is_valid());
178 #if (defined _MSC_VER) || (defined __SUNPRO_CC)
179 // MSVC++ can't handle:
180 // ::FLAC__StreamMetadata *m[num_blocks];
181 // so we do this ugly workaround
182 ::FLAC__StreamMetadata **m = new ::FLAC__StreamMetadata*[num_blocks];
184 ::FLAC__StreamMetadata *m[num_blocks];
186 for(unsigned i = 0; i < num_blocks; i++) {
187 // we can get away with the const_cast since we know the encoder will only correct the is_last flags
188 m[i] = const_cast< ::FLAC__StreamMetadata*>((const ::FLAC__StreamMetadata*)metadata[i]);
190 #if (defined _MSC_VER) || (defined __SUNPRO_CC)
192 const bool ok = (bool)::FLAC__stream_encoder_set_metadata(encoder_, m, num_blocks);
196 return (bool)::FLAC__stream_encoder_set_metadata(encoder_, m, num_blocks);
200 Stream::State Stream::get_state() const
202 FLAC__ASSERT(is_valid());
203 return State(::FLAC__stream_encoder_get_state(encoder_));
206 Decoder::Stream::State Stream::get_verify_decoder_state() const
208 FLAC__ASSERT(is_valid());
209 return Decoder::Stream::State(::FLAC__stream_encoder_get_verify_decoder_state(encoder_));
212 void Stream::get_verify_decoder_error_stats(FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
214 FLAC__ASSERT(is_valid());
215 ::FLAC__stream_encoder_get_verify_decoder_error_stats(encoder_, absolute_sample, frame_number, channel, sample, expected, got);
218 bool Stream::get_verify() const
220 FLAC__ASSERT(is_valid());
221 return (bool)::FLAC__stream_encoder_get_verify(encoder_);
224 bool Stream::get_streamable_subset() const
226 FLAC__ASSERT(is_valid());
227 return (bool)::FLAC__stream_encoder_get_streamable_subset(encoder_);
230 bool Stream::get_do_mid_side_stereo() const
232 FLAC__ASSERT(is_valid());
233 return (bool)::FLAC__stream_encoder_get_do_mid_side_stereo(encoder_);
236 bool Stream::get_loose_mid_side_stereo() const
238 FLAC__ASSERT(is_valid());
239 return (bool)::FLAC__stream_encoder_get_loose_mid_side_stereo(encoder_);
242 unsigned Stream::get_channels() const
244 FLAC__ASSERT(is_valid());
245 return ::FLAC__stream_encoder_get_channels(encoder_);
248 unsigned Stream::get_bits_per_sample() const
250 FLAC__ASSERT(is_valid());
251 return ::FLAC__stream_encoder_get_bits_per_sample(encoder_);
254 unsigned Stream::get_sample_rate() const
256 FLAC__ASSERT(is_valid());
257 return ::FLAC__stream_encoder_get_sample_rate(encoder_);
260 unsigned Stream::get_blocksize() const
262 FLAC__ASSERT(is_valid());
263 return ::FLAC__stream_encoder_get_blocksize(encoder_);
266 unsigned Stream::get_max_lpc_order() const
268 FLAC__ASSERT(is_valid());
269 return ::FLAC__stream_encoder_get_max_lpc_order(encoder_);
272 unsigned Stream::get_qlp_coeff_precision() const
274 FLAC__ASSERT(is_valid());
275 return ::FLAC__stream_encoder_get_qlp_coeff_precision(encoder_);
278 bool Stream::get_do_qlp_coeff_prec_search() const
280 FLAC__ASSERT(is_valid());
281 return (bool)::FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder_);
284 bool Stream::get_do_escape_coding() const
286 FLAC__ASSERT(is_valid());
287 return (bool)::FLAC__stream_encoder_get_do_escape_coding(encoder_);
290 bool Stream::get_do_exhaustive_model_search() const
292 FLAC__ASSERT(is_valid());
293 return (bool)::FLAC__stream_encoder_get_do_exhaustive_model_search(encoder_);
296 unsigned Stream::get_min_residual_partition_order() const
298 FLAC__ASSERT(is_valid());
299 return ::FLAC__stream_encoder_get_min_residual_partition_order(encoder_);
302 unsigned Stream::get_max_residual_partition_order() const
304 FLAC__ASSERT(is_valid());
305 return ::FLAC__stream_encoder_get_max_residual_partition_order(encoder_);
308 unsigned Stream::get_rice_parameter_search_dist() const
310 FLAC__ASSERT(is_valid());
311 return ::FLAC__stream_encoder_get_rice_parameter_search_dist(encoder_);
314 FLAC__uint64 Stream::get_total_samples_estimate() const
316 FLAC__ASSERT(is_valid());
317 return ::FLAC__stream_encoder_get_total_samples_estimate(encoder_);
320 ::FLAC__StreamEncoderInitStatus Stream::init()
322 FLAC__ASSERT(is_valid());
323 return ::FLAC__stream_encoder_init_stream(encoder_, write_callback_, seek_callback_, tell_callback_, metadata_callback_, /*client_data=*/(void*)this);
326 void Stream::finish()
328 FLAC__ASSERT(is_valid());
329 ::FLAC__stream_encoder_finish(encoder_);
332 bool Stream::process(const FLAC__int32 * const buffer[], unsigned samples)
334 FLAC__ASSERT(is_valid());
335 return (bool)::FLAC__stream_encoder_process(encoder_, buffer, samples);
338 bool Stream::process_interleaved(const FLAC__int32 buffer[], unsigned samples)
340 FLAC__ASSERT(is_valid());
341 return (bool)::FLAC__stream_encoder_process_interleaved(encoder_, buffer, samples);
344 ::FLAC__StreamEncoderSeekStatus Stream::seek_callback(FLAC__uint64 absolute_byte_offset)
346 (void)absolute_byte_offset;
347 return ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED;
350 ::FLAC__StreamEncoderTellStatus Stream::tell_callback(FLAC__uint64 *absolute_byte_offset)
352 (void)absolute_byte_offset;
353 return ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED;
356 void Stream::metadata_callback(const ::FLAC__StreamMetadata *metadata)
361 ::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
364 FLAC__ASSERT(0 != client_data);
365 Stream *instance = reinterpret_cast<Stream *>(client_data);
366 FLAC__ASSERT(0 != instance);
367 return instance->write_callback(buffer, bytes, samples, current_frame);
370 ::FLAC__StreamEncoderSeekStatus Stream::seek_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
373 FLAC__ASSERT(0 != client_data);
374 Stream *instance = reinterpret_cast<Stream *>(client_data);
375 FLAC__ASSERT(0 != instance);
376 return instance->seek_callback(absolute_byte_offset);
379 ::FLAC__StreamEncoderTellStatus Stream::tell_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
382 FLAC__ASSERT(0 != client_data);
383 Stream *instance = reinterpret_cast<Stream *>(client_data);
384 FLAC__ASSERT(0 != instance);
385 return instance->tell_callback(absolute_byte_offset);
388 void Stream::metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
391 FLAC__ASSERT(0 != client_data);
392 Stream *instance = reinterpret_cast<Stream *>(client_data);
393 FLAC__ASSERT(0 != instance);
394 instance->metadata_callback(metadata);