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/assert.h"
36 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
37 #pragma warning ( disable : 4800 )
43 SeekableStream::SeekableStream():
44 encoder_(::FLAC__seekable_stream_encoder_new())
47 SeekableStream::~SeekableStream()
50 ::FLAC__seekable_stream_encoder_finish(encoder_);
51 ::FLAC__seekable_stream_encoder_delete(encoder_);
55 bool SeekableStream::is_valid() const
60 bool SeekableStream::set_verify(bool value)
62 FLAC__ASSERT(is_valid());
63 return (bool)::FLAC__seekable_stream_encoder_set_verify(encoder_, value);
66 bool SeekableStream::set_streamable_subset(bool value)
68 FLAC__ASSERT(is_valid());
69 return (bool)::FLAC__seekable_stream_encoder_set_streamable_subset(encoder_, value);
72 bool SeekableStream::set_do_mid_side_stereo(bool value)
74 FLAC__ASSERT(is_valid());
75 return (bool)::FLAC__seekable_stream_encoder_set_do_mid_side_stereo(encoder_, value);
78 bool SeekableStream::set_loose_mid_side_stereo(bool value)
80 FLAC__ASSERT(is_valid());
81 return (bool)::FLAC__seekable_stream_encoder_set_loose_mid_side_stereo(encoder_, value);
84 bool SeekableStream::set_channels(unsigned value)
86 FLAC__ASSERT(is_valid());
87 return (bool)::FLAC__seekable_stream_encoder_set_channels(encoder_, value);
90 bool SeekableStream::set_bits_per_sample(unsigned value)
92 FLAC__ASSERT(is_valid());
93 return (bool)::FLAC__seekable_stream_encoder_set_bits_per_sample(encoder_, value);
96 bool SeekableStream::set_sample_rate(unsigned value)
98 FLAC__ASSERT(is_valid());
99 return (bool)::FLAC__seekable_stream_encoder_set_sample_rate(encoder_, value);
102 bool SeekableStream::set_blocksize(unsigned value)
104 FLAC__ASSERT(is_valid());
105 return (bool)::FLAC__seekable_stream_encoder_set_blocksize(encoder_, value);
108 bool SeekableStream::set_apodization(const char *specification)
110 FLAC__ASSERT(is_valid());
111 return (bool)::FLAC__seekable_stream_encoder_set_apodization(encoder_, specification);
114 bool SeekableStream::set_max_lpc_order(unsigned value)
116 FLAC__ASSERT(is_valid());
117 return (bool)::FLAC__seekable_stream_encoder_set_max_lpc_order(encoder_, value);
120 bool SeekableStream::set_qlp_coeff_precision(unsigned value)
122 FLAC__ASSERT(is_valid());
123 return (bool)::FLAC__seekable_stream_encoder_set_qlp_coeff_precision(encoder_, value);
126 bool SeekableStream::set_do_qlp_coeff_prec_search(bool value)
128 FLAC__ASSERT(is_valid());
129 return (bool)::FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search(encoder_, value);
132 bool SeekableStream::set_do_escape_coding(bool value)
134 FLAC__ASSERT(is_valid());
135 return (bool)::FLAC__seekable_stream_encoder_set_do_escape_coding(encoder_, value);
138 bool SeekableStream::set_do_exhaustive_model_search(bool value)
140 FLAC__ASSERT(is_valid());
141 return (bool)::FLAC__seekable_stream_encoder_set_do_exhaustive_model_search(encoder_, value);
144 bool SeekableStream::set_min_residual_partition_order(unsigned value)
146 FLAC__ASSERT(is_valid());
147 return (bool)::FLAC__seekable_stream_encoder_set_min_residual_partition_order(encoder_, value);
150 bool SeekableStream::set_max_residual_partition_order(unsigned value)
152 FLAC__ASSERT(is_valid());
153 return (bool)::FLAC__seekable_stream_encoder_set_max_residual_partition_order(encoder_, value);
156 bool SeekableStream::set_rice_parameter_search_dist(unsigned value)
158 FLAC__ASSERT(is_valid());
159 return (bool)::FLAC__seekable_stream_encoder_set_rice_parameter_search_dist(encoder_, value);
162 bool SeekableStream::set_total_samples_estimate(FLAC__uint64 value)
164 FLAC__ASSERT(is_valid());
165 return (bool)::FLAC__seekable_stream_encoder_set_total_samples_estimate(encoder_, value);
168 bool SeekableStream::set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks)
170 FLAC__ASSERT(is_valid());
171 return (bool)::FLAC__seekable_stream_encoder_set_metadata(encoder_, metadata, num_blocks);
174 bool SeekableStream::set_metadata(FLAC::Metadata::Prototype **metadata, unsigned num_blocks)
176 FLAC__ASSERT(is_valid());
177 #if (defined _MSC_VER) || (defined __SUNPRO_CC)
178 // MSVC++ can't handle:
179 // ::FLAC__StreamMetadata *m[num_blocks];
180 // so we do this ugly workaround
181 ::FLAC__StreamMetadata **m = new ::FLAC__StreamMetadata*[num_blocks];
183 ::FLAC__StreamMetadata *m[num_blocks];
185 for(unsigned i = 0; i < num_blocks; i++) {
186 // we can get away with this since we know the encoder will only correct the is_last flags
187 m[i] = const_cast< ::FLAC__StreamMetadata*>((::FLAC__StreamMetadata*)metadata[i]);
189 #if (defined _MSC_VER) || (defined __SUNPRO_CC)
191 const bool ok = (bool)::FLAC__seekable_stream_encoder_set_metadata(encoder_, m, num_blocks);
195 return (bool)::FLAC__seekable_stream_encoder_set_metadata(encoder_, m, num_blocks);
199 SeekableStream::State SeekableStream::get_state() const
201 FLAC__ASSERT(is_valid());
202 return State(::FLAC__seekable_stream_encoder_get_state(encoder_));
205 Stream::State SeekableStream::get_stream_encoder_state() const
207 FLAC__ASSERT(is_valid());
208 return Stream::State(::FLAC__seekable_stream_encoder_get_stream_encoder_state(encoder_));
211 Decoder::Stream::State SeekableStream::get_verify_decoder_state() const
213 FLAC__ASSERT(is_valid());
214 return Decoder::Stream::State(::FLAC__seekable_stream_encoder_get_verify_decoder_state(encoder_));
217 void SeekableStream::get_verify_decoder_error_stats(FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
219 FLAC__ASSERT(is_valid());
220 ::FLAC__seekable_stream_encoder_get_verify_decoder_error_stats(encoder_, absolute_sample, frame_number, channel, sample, expected, got);
223 bool SeekableStream::get_verify() const
225 FLAC__ASSERT(is_valid());
226 return (bool)::FLAC__seekable_stream_encoder_get_verify(encoder_);
229 bool SeekableStream::get_streamable_subset() const
231 FLAC__ASSERT(is_valid());
232 return (bool)::FLAC__seekable_stream_encoder_get_streamable_subset(encoder_);
235 bool SeekableStream::get_do_mid_side_stereo() const
237 FLAC__ASSERT(is_valid());
238 return (bool)::FLAC__seekable_stream_encoder_get_do_mid_side_stereo(encoder_);
241 bool SeekableStream::get_loose_mid_side_stereo() const
243 FLAC__ASSERT(is_valid());
244 return (bool)::FLAC__seekable_stream_encoder_get_loose_mid_side_stereo(encoder_);
247 unsigned SeekableStream::get_channels() const
249 FLAC__ASSERT(is_valid());
250 return ::FLAC__seekable_stream_encoder_get_channels(encoder_);
253 unsigned SeekableStream::get_bits_per_sample() const
255 FLAC__ASSERT(is_valid());
256 return ::FLAC__seekable_stream_encoder_get_bits_per_sample(encoder_);
259 unsigned SeekableStream::get_sample_rate() const
261 FLAC__ASSERT(is_valid());
262 return ::FLAC__seekable_stream_encoder_get_sample_rate(encoder_);
265 unsigned SeekableStream::get_blocksize() const
267 FLAC__ASSERT(is_valid());
268 return ::FLAC__seekable_stream_encoder_get_blocksize(encoder_);
271 unsigned SeekableStream::get_max_lpc_order() const
273 FLAC__ASSERT(is_valid());
274 return ::FLAC__seekable_stream_encoder_get_max_lpc_order(encoder_);
277 unsigned SeekableStream::get_qlp_coeff_precision() const
279 FLAC__ASSERT(is_valid());
280 return ::FLAC__seekable_stream_encoder_get_qlp_coeff_precision(encoder_);
283 bool SeekableStream::get_do_qlp_coeff_prec_search() const
285 FLAC__ASSERT(is_valid());
286 return (bool)::FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search(encoder_);
289 bool SeekableStream::get_do_escape_coding() const
291 FLAC__ASSERT(is_valid());
292 return (bool)::FLAC__seekable_stream_encoder_get_do_escape_coding(encoder_);
295 bool SeekableStream::get_do_exhaustive_model_search() const
297 FLAC__ASSERT(is_valid());
298 return (bool)::FLAC__seekable_stream_encoder_get_do_exhaustive_model_search(encoder_);
301 unsigned SeekableStream::get_min_residual_partition_order() const
303 FLAC__ASSERT(is_valid());
304 return ::FLAC__seekable_stream_encoder_get_min_residual_partition_order(encoder_);
307 unsigned SeekableStream::get_max_residual_partition_order() const
309 FLAC__ASSERT(is_valid());
310 return ::FLAC__seekable_stream_encoder_get_max_residual_partition_order(encoder_);
313 unsigned SeekableStream::get_rice_parameter_search_dist() const
315 FLAC__ASSERT(is_valid());
316 return ::FLAC__seekable_stream_encoder_get_rice_parameter_search_dist(encoder_);
319 FLAC__uint64 SeekableStream::get_total_samples_estimate() const
321 FLAC__ASSERT(is_valid());
322 return ::FLAC__seekable_stream_encoder_get_total_samples_estimate(encoder_);
325 SeekableStream::State SeekableStream::init()
327 FLAC__ASSERT(is_valid());
328 ::FLAC__seekable_stream_encoder_set_seek_callback(encoder_, seek_callback_);
329 ::FLAC__seekable_stream_encoder_set_tell_callback(encoder_, tell_callback_);
330 ::FLAC__seekable_stream_encoder_set_write_callback(encoder_, write_callback_);
331 ::FLAC__seekable_stream_encoder_set_client_data(encoder_, (void*)this);
332 return State(::FLAC__seekable_stream_encoder_init(encoder_));
335 void SeekableStream::finish()
337 FLAC__ASSERT(is_valid());
338 ::FLAC__seekable_stream_encoder_finish(encoder_);
341 bool SeekableStream::process(const FLAC__int32 * const buffer[], unsigned samples)
343 FLAC__ASSERT(is_valid());
344 return (bool)::FLAC__seekable_stream_encoder_process(encoder_, buffer, samples);
347 bool SeekableStream::process_interleaved(const FLAC__int32 buffer[], unsigned samples)
349 FLAC__ASSERT(is_valid());
350 return (bool)::FLAC__seekable_stream_encoder_process_interleaved(encoder_, buffer, samples);
353 ::FLAC__SeekableStreamEncoderSeekStatus SeekableStream::seek_callback_(const ::FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
356 FLAC__ASSERT(0 != client_data);
357 SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
358 FLAC__ASSERT(0 != instance);
359 return instance->seek_callback(absolute_byte_offset);
362 ::FLAC__SeekableStreamEncoderTellStatus SeekableStream::tell_callback_(const ::FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
365 FLAC__ASSERT(0 != client_data);
366 SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
367 FLAC__ASSERT(0 != instance);
368 return instance->tell_callback(absolute_byte_offset);
371 ::FLAC__StreamEncoderWriteStatus SeekableStream::write_callback_(const ::FLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
374 FLAC__ASSERT(0 != client_data);
375 SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
376 FLAC__ASSERT(0 != instance);
377 return instance->write_callback(buffer, bytes, samples, current_frame);