1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,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.
36 #if defined _MSC_VER || defined __MINGW32__
37 #include <io.h> /* for _setmode() */
38 #include <fcntl.h> /* for _O_BINARY */
40 #if defined __CYGWIN__ || defined __EMX__
41 #include <io.h> /* for setmode(), O_BINARY */
42 #include <fcntl.h> /* for _O_BINARY */
46 #include <stdlib.h> /* for malloc() */
47 #include <string.h> /* for memcpy() */
48 #include <sys/types.h> /* for off_t */
49 #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
50 #if _MSC_VER <= 1200 || defined __BORLANDC__ /* @@@ [2G limit] */
55 #include "FLAC/assert.h"
56 #include "FLAC/stream_decoder.h"
57 #include "protected/stream_encoder.h"
58 #include "private/bitbuffer.h"
59 #include "private/bitmath.h"
60 #include "private/crc.h"
61 #include "private/cpu.h"
62 #include "private/fixed.h"
63 #include "private/format.h"
64 #include "private/lpc.h"
65 #include "private/md5.h"
66 #include "private/memory.h"
68 #include "private/ogg_helper.h"
69 #include "private/ogg_mapping.h"
71 #include "private/stream_encoder_framing.h"
72 #include "private/window.h"
77 #define min(x,y) ((x)<(y)?(x):(y))
82 #define max(x,y) ((x)>(y)?(x):(y))
85 FLAC__int32 *data[FLAC__MAX_CHANNELS];
86 unsigned size; /* of each data[] in samples */
91 const FLAC__byte *data;
98 ENCODER_IN_METADATA = 1,
102 static struct CompressionLevels {
103 FLAC__bool do_mid_side_stereo;
104 FLAC__bool loose_mid_side_stereo;
105 const char *apodization;
106 unsigned max_lpc_order;
107 unsigned qlp_coeff_precision;
108 FLAC__bool do_qlp_coeff_prec_search;
109 FLAC__bool do_escape_coding;
110 FLAC__bool do_exhaustive_model_search;
111 unsigned min_residual_partition_order;
112 unsigned max_residual_partition_order;
113 unsigned rice_parameter_search_dist;
114 } compression_levels_[] = {
115 { false, false, "tukey(0.5)", 0, 0, false, false, false, 2, 2, 0 },
116 { true , true , "tukey(0.5)", 0, 0, false, false, false, 2, 2, 0 },
117 { true , false, "tukey(0.5)", 0, 0, false, false, false, 0, 3, 0 },
118 { false, false, "tukey(0.5)", 6, 0, false, false, false, 3, 3, 0 },
119 { true , true , "tukey(0.5)", 8, 0, false, false, false, 3, 3, 0 },
120 { true , false, "tukey(0.5)", 8, 0, false, false, false, 3, 3, 0 },
121 { true , false, "tukey(0.5)", 8, 0, false, false, false, 0, 4, 0 },
122 { true , false, "tukey(0.5)", 8, 0, false, false, true , 0, 6, 0 },
123 { true , false, "tukey(0.5)", 12, 0, false, false, true , 0, 6, 0 }
127 /***********************************************************************
129 * Private class method prototypes
131 ***********************************************************************/
133 static void set_defaults_(FLAC__StreamEncoder *encoder);
134 static void free_(FLAC__StreamEncoder *encoder);
135 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize);
136 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block);
137 static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block);
138 static void update_metadata_(const FLAC__StreamEncoder *encoder);
140 static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
142 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block);
143 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);
145 static FLAC__bool process_subframe_(
146 FLAC__StreamEncoder *encoder,
147 unsigned min_partition_order,
148 unsigned max_partition_order,
149 FLAC__bool precompute_partition_sums,
150 const FLAC__FrameHeader *frame_header,
151 unsigned subframe_bps,
152 const FLAC__int32 integer_signal[],
153 #ifndef FLAC__INTEGER_ONLY_LIBRARY
154 const FLAC__real real_signal[],
156 FLAC__Subframe *subframe[2],
157 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
158 FLAC__int32 *residual[2],
159 unsigned *best_subframe,
163 static FLAC__bool add_subframe_(
164 FLAC__StreamEncoder *encoder,
165 const FLAC__FrameHeader *frame_header,
166 unsigned subframe_bps,
167 const FLAC__Subframe *subframe,
168 FLAC__BitBuffer *frame
171 static unsigned evaluate_constant_subframe_(
172 const FLAC__int32 signal,
173 unsigned subframe_bps,
174 FLAC__Subframe *subframe
177 static unsigned evaluate_fixed_subframe_(
178 FLAC__StreamEncoder *encoder,
179 const FLAC__int32 signal[],
180 FLAC__int32 residual[],
181 FLAC__uint32 abs_residual[],
182 FLAC__uint64 abs_residual_partition_sums[],
183 unsigned raw_bits_per_partition[],
185 unsigned subframe_bps,
187 unsigned rice_parameter,
188 unsigned min_partition_order,
189 unsigned max_partition_order,
190 FLAC__bool precompute_partition_sums,
191 FLAC__bool do_escape_coding,
192 unsigned rice_parameter_search_dist,
193 FLAC__Subframe *subframe,
194 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
197 #ifndef FLAC__INTEGER_ONLY_LIBRARY
198 static unsigned evaluate_lpc_subframe_(
199 FLAC__StreamEncoder *encoder,
200 const FLAC__int32 signal[],
201 FLAC__int32 residual[],
202 FLAC__uint32 abs_residual[],
203 FLAC__uint64 abs_residual_partition_sums[],
204 unsigned raw_bits_per_partition[],
205 const FLAC__real lp_coeff[],
207 unsigned subframe_bps,
209 unsigned qlp_coeff_precision,
210 unsigned rice_parameter,
211 unsigned min_partition_order,
212 unsigned max_partition_order,
213 FLAC__bool precompute_partition_sums,
214 FLAC__bool do_escape_coding,
215 unsigned rice_parameter_search_dist,
216 FLAC__Subframe *subframe,
217 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
221 static unsigned evaluate_verbatim_subframe_(
222 const FLAC__int32 signal[],
224 unsigned subframe_bps,
225 FLAC__Subframe *subframe
228 static unsigned find_best_partition_order_(
229 struct FLAC__StreamEncoderPrivate *private_,
230 const FLAC__int32 residual[],
231 FLAC__uint32 abs_residual[],
232 FLAC__uint64 abs_residual_partition_sums[],
233 unsigned raw_bits_per_partition[],
234 unsigned residual_samples,
235 unsigned predictor_order,
236 unsigned rice_parameter,
237 unsigned min_partition_order,
238 unsigned max_partition_order,
239 FLAC__bool precompute_partition_sums,
240 FLAC__bool do_escape_coding,
241 unsigned rice_parameter_search_dist,
242 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
245 static void precompute_partition_info_sums_(
246 const FLAC__uint32 abs_residual[],
247 FLAC__uint64 abs_residual_partition_sums[],
248 unsigned residual_samples,
249 unsigned predictor_order,
250 unsigned min_partition_order,
251 unsigned max_partition_order
254 static void precompute_partition_info_escapes_(
255 const FLAC__int32 residual[],
256 unsigned raw_bits_per_partition[],
257 unsigned residual_samples,
258 unsigned predictor_order,
259 unsigned min_partition_order,
260 unsigned max_partition_order
263 #ifdef DONT_ESTIMATE_RICE_BITS
264 static FLAC__bool set_partitioned_rice_(
265 const FLAC__uint32 abs_residual[],
266 const FLAC__int32 residual[],
267 const unsigned residual_samples,
268 const unsigned predictor_order,
269 const unsigned suggested_rice_parameter,
270 const unsigned rice_parameter_search_dist,
271 const unsigned partition_order,
272 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
276 static FLAC__bool set_partitioned_rice_with_precompute_(
277 const FLAC__int32 residual[],
278 const FLAC__uint64 abs_residual_partition_sums[],
279 const unsigned raw_bits_per_partition[],
280 const unsigned residual_samples,
281 const unsigned predictor_order,
282 const unsigned suggested_rice_parameter,
283 const unsigned rice_parameter_search_dist,
284 const unsigned partition_order,
285 const FLAC__bool search_for_escapes,
286 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
290 static FLAC__bool set_partitioned_rice_(
291 const FLAC__uint32 abs_residual[],
292 const unsigned residual_samples,
293 const unsigned predictor_order,
294 const unsigned suggested_rice_parameter,
295 const unsigned rice_parameter_search_dist,
296 const unsigned partition_order,
297 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
301 static FLAC__bool set_partitioned_rice_with_precompute_(
302 const FLAC__uint32 abs_residual[],
303 const FLAC__uint64 abs_residual_partition_sums[],
304 const unsigned raw_bits_per_partition[],
305 const unsigned residual_samples,
306 const unsigned predictor_order,
307 const unsigned suggested_rice_parameter,
308 const unsigned rice_parameter_search_dist,
309 const unsigned partition_order,
310 const FLAC__bool search_for_escapes,
311 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
316 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
318 /* verify-related routines: */
319 static void append_to_verify_fifo_(
320 verify_input_fifo *fifo,
321 const FLAC__int32 * const input[],
322 unsigned input_offset,
324 unsigned wide_samples
327 static void append_to_verify_fifo_interleaved_(
328 verify_input_fifo *fifo,
329 const FLAC__int32 input[],
330 unsigned input_offset,
332 unsigned wide_samples
335 static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
336 static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
337 static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
338 static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
340 static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
341 static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
342 static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
343 static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data);
344 static FILE *get_binary_stdout_();
347 /***********************************************************************
351 ***********************************************************************/
353 typedef struct FLAC__StreamEncoderPrivate {
354 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
355 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
356 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
357 #ifndef FLAC__INTEGER_ONLY_LIBRARY
358 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
359 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
360 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
361 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
363 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
364 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
365 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
366 FLAC__int32 *residual_workspace_mid_side[2][2];
367 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
368 FLAC__Subframe subframe_workspace_mid_side[2][2];
369 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
370 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
371 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
372 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
373 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
374 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
375 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
376 unsigned best_subframe_mid_side[2];
377 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
378 unsigned best_subframe_bits_mid_side[2];
379 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
380 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
381 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
382 FLAC__BitBuffer *frame; /* the current frame being worked on */
383 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
384 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
385 FLAC__ChannelAssignment last_channel_assignment;
386 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
387 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
388 unsigned current_sample_number;
389 unsigned current_frame_number;
390 struct FLAC__MD5Context md5context;
391 FLAC__CPUInfo cpuinfo;
392 #ifndef FLAC__INTEGER_ONLY_LIBRARY
393 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
395 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
397 #ifndef FLAC__INTEGER_ONLY_LIBRARY
398 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
399 void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
400 void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
401 void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
403 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
404 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
405 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
406 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
407 FLAC__bool disable_constant_subframes;
408 FLAC__bool disable_fixed_subframes;
409 FLAC__bool disable_verbatim_subframes;
413 FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
414 FLAC__StreamEncoderSeekCallback seek_callback;
415 FLAC__StreamEncoderTellCallback tell_callback;
416 FLAC__StreamEncoderWriteCallback write_callback;
417 FLAC__StreamEncoderMetadataCallback metadata_callback;
418 FLAC__StreamEncoderProgressCallback progress_callback;
420 unsigned first_seekpoint_to_check;
421 FILE *file; /* only used when encoding to a file */
422 FLAC__uint64 bytes_written;
423 FLAC__uint64 samples_written;
424 unsigned frames_written;
425 unsigned total_frames_estimate;
426 /* unaligned (original) pointers to allocated data */
427 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
428 FLAC__int32 *integer_signal_mid_side_unaligned[2];
429 #ifndef FLAC__INTEGER_ONLY_LIBRARY
430 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
431 FLAC__real *real_signal_mid_side_unaligned[2];
432 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
433 FLAC__real *windowed_signal_unaligned;
435 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
436 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
437 FLAC__uint32 *abs_residual_unaligned;
438 FLAC__uint64 *abs_residual_partition_sums_unaligned;
439 unsigned *raw_bits_per_partition_unaligned;
441 * These fields have been moved here from private function local
442 * declarations merely to save stack space during encoding.
444 #ifndef FLAC__INTEGER_ONLY_LIBRARY
445 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
447 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
449 * The data for the verify section
452 FLAC__StreamDecoder *decoder;
453 EncoderStateHint state_hint;
454 FLAC__bool needs_magic_hack;
455 verify_input_fifo input_fifo;
456 verify_output output;
458 FLAC__uint64 absolute_sample;
459 unsigned frame_number;
462 FLAC__int32 expected;
466 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
467 } FLAC__StreamEncoderPrivate;
469 /***********************************************************************
471 * Public static class data
473 ***********************************************************************/
475 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
476 "FLAC__STREAM_ENCODER_OK",
477 "FLAC__STREAM_ENCODER_UNINITIALIZED",
478 "FLAC__STREAM_ENCODER_OGG_ERROR",
479 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
480 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
481 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
482 "FLAC__STREAM_ENCODER_IO_ERROR",
483 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
484 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
487 FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
488 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
489 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
490 "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
491 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
492 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
493 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
494 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
495 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
496 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
497 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
498 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
499 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
500 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
501 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
504 FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
505 "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
506 "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
507 "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
508 "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
511 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
512 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
513 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
516 FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
517 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
518 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
519 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
522 FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
523 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
524 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
525 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
528 /* Number of samples that will be overread to watch for end of stream. By
529 * 'overread', we mean that the FLAC__stream_encoder_process*() calls will
530 * always try to read blocksize+1 samples before encoding a block, so that
531 * even if the stream has a total sample count that is an integral multiple
532 * of the blocksize, we will still notice when we are encoding the last
533 * block. This is needed, for example, to correctly set the end-of-stream
534 * marker in Ogg FLAC.
536 * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
537 * not really any reason to change it.
539 static const unsigned OVERREAD_ = 1;
541 /***********************************************************************
543 * Class constructor/destructor
546 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
548 FLAC__StreamEncoder *encoder;
551 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
553 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
558 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
559 if(encoder->protected_ == 0) {
564 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
565 if(encoder->private_ == 0) {
566 free(encoder->protected_);
571 encoder->private_->frame = FLAC__bitbuffer_new();
572 if(encoder->private_->frame == 0) {
573 free(encoder->private_);
574 free(encoder->protected_);
579 encoder->private_->file = 0;
581 set_defaults_(encoder);
583 encoder->private_->is_being_deleted = false;
585 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
586 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
587 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
589 for(i = 0; i < 2; i++) {
590 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
591 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
593 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
594 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
595 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
597 for(i = 0; i < 2; i++) {
598 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
599 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
602 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
603 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
604 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
606 for(i = 0; i < 2; i++) {
607 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
608 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
610 for(i = 0; i < 2; i++)
611 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
613 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
618 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
622 FLAC__ASSERT(0 != encoder);
623 FLAC__ASSERT(0 != encoder->protected_);
624 FLAC__ASSERT(0 != encoder->private_);
625 FLAC__ASSERT(0 != encoder->private_->frame);
627 encoder->private_->is_being_deleted = true;
629 (void)FLAC__stream_encoder_finish(encoder);
631 if(0 != encoder->private_->verify.decoder)
632 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
634 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
635 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
636 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
638 for(i = 0; i < 2; i++) {
639 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
640 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
642 for(i = 0; i < 2; i++)
643 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
645 FLAC__bitbuffer_delete(encoder->private_->frame);
646 free(encoder->private_);
647 free(encoder->protected_);
651 /***********************************************************************
653 * Public class methods
655 ***********************************************************************/
657 static FLAC__StreamEncoderInitStatus init_stream_internal_(
658 FLAC__StreamEncoder *encoder,
659 FLAC__StreamEncoderReadCallback read_callback,
660 FLAC__StreamEncoderWriteCallback write_callback,
661 FLAC__StreamEncoderSeekCallback seek_callback,
662 FLAC__StreamEncoderTellCallback tell_callback,
663 FLAC__StreamEncoderMetadataCallback metadata_callback,
669 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
671 FLAC__ASSERT(0 != encoder);
673 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
674 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
678 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
681 if(0 == write_callback || (seek_callback && 0 == tell_callback))
682 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
684 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
685 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
687 if(encoder->protected_->channels != 2) {
688 encoder->protected_->do_mid_side_stereo = false;
689 encoder->protected_->loose_mid_side_stereo = false;
691 else if(!encoder->protected_->do_mid_side_stereo)
692 encoder->protected_->loose_mid_side_stereo = false;
694 if(encoder->protected_->bits_per_sample >= 32)
695 encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */
697 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
698 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
700 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
701 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
703 if(encoder->protected_->blocksize == 0) {
704 if(encoder->protected_->max_lpc_order == 0)
705 encoder->protected_->blocksize = 1152;
707 encoder->protected_->blocksize = 4608;
710 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
711 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
713 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
714 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
716 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
717 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
719 if(encoder->protected_->qlp_coeff_precision == 0) {
720 if(encoder->protected_->bits_per_sample < 16) {
721 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
722 /* @@@ until then we'll make a guess */
723 encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
725 else if(encoder->protected_->bits_per_sample == 16) {
726 if(encoder->protected_->blocksize <= 192)
727 encoder->protected_->qlp_coeff_precision = 7;
728 else if(encoder->protected_->blocksize <= 384)
729 encoder->protected_->qlp_coeff_precision = 8;
730 else if(encoder->protected_->blocksize <= 576)
731 encoder->protected_->qlp_coeff_precision = 9;
732 else if(encoder->protected_->blocksize <= 1152)
733 encoder->protected_->qlp_coeff_precision = 10;
734 else if(encoder->protected_->blocksize <= 2304)
735 encoder->protected_->qlp_coeff_precision = 11;
736 else if(encoder->protected_->blocksize <= 4608)
737 encoder->protected_->qlp_coeff_precision = 12;
739 encoder->protected_->qlp_coeff_precision = 13;
742 if(encoder->protected_->blocksize <= 384)
743 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
744 else if(encoder->protected_->blocksize <= 1152)
745 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
747 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
749 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
751 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
752 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
754 if(encoder->protected_->streamable_subset) {
756 encoder->protected_->blocksize != 192 &&
757 encoder->protected_->blocksize != 576 &&
758 encoder->protected_->blocksize != 1152 &&
759 encoder->protected_->blocksize != 2304 &&
760 encoder->protected_->blocksize != 4608 &&
761 encoder->protected_->blocksize != 256 &&
762 encoder->protected_->blocksize != 512 &&
763 encoder->protected_->blocksize != 1024 &&
764 encoder->protected_->blocksize != 2048 &&
765 encoder->protected_->blocksize != 4096 &&
766 encoder->protected_->blocksize != 8192 &&
767 encoder->protected_->blocksize != 16384
769 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
771 encoder->protected_->sample_rate != 8000 &&
772 encoder->protected_->sample_rate != 16000 &&
773 encoder->protected_->sample_rate != 22050 &&
774 encoder->protected_->sample_rate != 24000 &&
775 encoder->protected_->sample_rate != 32000 &&
776 encoder->protected_->sample_rate != 44100 &&
777 encoder->protected_->sample_rate != 48000 &&
778 encoder->protected_->sample_rate != 96000
780 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
782 encoder->protected_->bits_per_sample != 8 &&
783 encoder->protected_->bits_per_sample != 12 &&
784 encoder->protected_->bits_per_sample != 16 &&
785 encoder->protected_->bits_per_sample != 20 &&
786 encoder->protected_->bits_per_sample != 24
788 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
789 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
790 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
792 encoder->protected_->sample_rate <= 48000 &&
794 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
795 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
798 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
802 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
803 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
804 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
805 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
808 /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
809 if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
811 for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
812 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
813 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
815 encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
816 encoder->protected_->metadata[0] = vc;
822 /* keep track of any SEEKTABLE block */
823 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
825 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
826 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
827 encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
828 break; /* take only the first one */
833 /* validate metadata */
834 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
835 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
836 metadata_has_seektable = false;
837 metadata_has_vorbis_comment = false;
838 metadata_picture_has_type1 = false;
839 metadata_picture_has_type2 = false;
840 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
841 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
842 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
843 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
844 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
845 if(metadata_has_seektable) /* only one is allowed */
846 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
847 metadata_has_seektable = true;
848 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
849 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
851 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
852 if(metadata_has_vorbis_comment) /* only one is allowed */
853 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
854 metadata_has_vorbis_comment = true;
856 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
857 if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
858 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
860 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
861 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
862 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
863 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
864 if(metadata_picture_has_type1) /* there should only be 1 per stream */
865 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
866 metadata_picture_has_type1 = true;
867 /* standard icon must be 32x32 pixel PNG */
869 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
871 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
872 m->data.picture.width != 32 ||
873 m->data.picture.height != 32
876 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
878 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
879 if(metadata_picture_has_type2) /* there should only be 1 per stream */
880 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
881 metadata_picture_has_type2 = true;
886 encoder->private_->input_capacity = 0;
887 for(i = 0; i < encoder->protected_->channels; i++) {
888 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
889 #ifndef FLAC__INTEGER_ONLY_LIBRARY
890 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
893 for(i = 0; i < 2; i++) {
894 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
895 #ifndef FLAC__INTEGER_ONLY_LIBRARY
896 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
899 #ifndef FLAC__INTEGER_ONLY_LIBRARY
900 for(i = 0; i < encoder->protected_->num_apodizations; i++)
901 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
902 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
904 for(i = 0; i < encoder->protected_->channels; i++) {
905 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
906 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
907 encoder->private_->best_subframe[i] = 0;
909 for(i = 0; i < 2; i++) {
910 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
911 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
912 encoder->private_->best_subframe_mid_side[i] = 0;
914 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
915 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
916 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
917 #ifndef FLAC__INTEGER_ONLY_LIBRARY
918 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
920 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
921 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply÷ by hand */
922 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
923 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
924 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
925 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
926 encoder->private_->loose_mid_side_stereo_frames = (unsigned)FLAC__fixedpoint_trunc((((FLAC__uint64)(encoder->protected_->sample_rate) * (FLAC__uint64)(26214)) << 16) / (encoder->protected_->blocksize<<16) + FLAC__FP_ONE_HALF);
928 if(encoder->private_->loose_mid_side_stereo_frames == 0)
929 encoder->private_->loose_mid_side_stereo_frames = 1;
930 encoder->private_->loose_mid_side_stereo_frame_count = 0;
931 encoder->private_->current_sample_number = 0;
932 encoder->private_->current_frame_number = 0;
934 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
935 encoder->private_->use_wide_by_order = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(max(encoder->protected_->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */
936 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
939 * get the CPU info and set the function pointers
941 FLAC__cpu_info(&encoder->private_->cpuinfo);
942 /* first default to the non-asm routines */
943 #ifndef FLAC__INTEGER_ONLY_LIBRARY
944 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
946 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
947 #ifndef FLAC__INTEGER_ONLY_LIBRARY
948 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
949 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
950 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
952 /* now override with asm where appropriate */
953 #ifndef FLAC__INTEGER_ONLY_LIBRARY
954 # ifndef FLAC__NO_ASM
955 if(encoder->private_->cpuinfo.use_asm) {
956 # ifdef FLAC__CPU_IA32
957 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
958 # ifdef FLAC__HAS_NASM
960 if(encoder->private_->cpuinfo.data.ia32.sse) {
961 if(encoder->protected_->max_lpc_order < 4)
962 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
963 else if(encoder->protected_->max_lpc_order < 8)
964 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
965 else if(encoder->protected_->max_lpc_order < 12)
966 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
968 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
971 # endif /* FLAC__SSE_OS */
972 if(encoder->private_->cpuinfo.data.ia32._3dnow)
973 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
975 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
976 if(encoder->private_->cpuinfo.data.ia32.mmx) {
977 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
978 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
981 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
982 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
984 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
985 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
986 # endif /* FLAC__HAS_NASM */
987 # endif /* FLAC__CPU_IA32 */
989 # endif /* !FLAC__NO_ASM */
990 #endif /* !FLAC__INTEGER_ONLY_LIBRARY */
991 /* finally override based on wide-ness if necessary */
992 if(encoder->private_->use_wide_by_block) {
993 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
996 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
997 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
999 /* set state to OK; from here on, errors are fatal and we'll override the state then */
1000 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
1003 encoder->private_->is_ogg = is_ogg;
1004 if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
1005 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
1006 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1010 encoder->private_->read_callback = read_callback;
1011 encoder->private_->write_callback = write_callback;
1012 encoder->private_->seek_callback = seek_callback;
1013 encoder->private_->tell_callback = tell_callback;
1014 encoder->private_->metadata_callback = metadata_callback;
1015 encoder->private_->client_data = client_data;
1017 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
1018 /* the above function sets the state for us in case of an error */
1019 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1022 if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
1023 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1024 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1028 * Set up the verify stuff if necessary
1030 if(encoder->protected_->verify) {
1032 * First, set up the fifo which will hold the
1033 * original signal to compare against
1035 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
1036 for(i = 0; i < encoder->protected_->channels; i++) {
1037 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
1038 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1039 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1042 encoder->private_->verify.input_fifo.tail = 0;
1045 * Now set up a stream decoder for verification
1047 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
1048 if(0 == encoder->private_->verify.decoder) {
1049 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1050 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1053 if(FLAC__stream_decoder_init_stream(encoder->private_->verify.decoder, verify_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, verify_write_callback_, verify_metadata_callback_, verify_error_callback_, /*client_data=*/encoder) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
1054 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1055 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1058 encoder->private_->verify.error_stats.absolute_sample = 0;
1059 encoder->private_->verify.error_stats.frame_number = 0;
1060 encoder->private_->verify.error_stats.channel = 0;
1061 encoder->private_->verify.error_stats.sample = 0;
1062 encoder->private_->verify.error_stats.expected = 0;
1063 encoder->private_->verify.error_stats.got = 0;
1066 * These must be done before we write any metadata, because that
1067 * calls the write_callback, which uses these values.
1069 encoder->private_->first_seekpoint_to_check = 0;
1070 encoder->private_->samples_written = 0;
1071 encoder->protected_->streaminfo_offset = 0;
1072 encoder->protected_->seektable_offset = 0;
1073 encoder->protected_->audio_offset = 0;
1076 * write the stream header
1078 if(encoder->protected_->verify)
1079 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
1080 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
1081 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1082 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1084 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1085 /* the above function sets the state for us in case of an error */
1086 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1090 * write the STREAMINFO metadata block
1092 if(encoder->protected_->verify)
1093 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
1094 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1095 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1096 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1097 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1098 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1099 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1100 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1101 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1102 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1103 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1104 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1105 memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
1106 FLAC__MD5Init(&encoder->private_->md5context);
1107 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1108 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1109 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1111 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1112 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1113 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1115 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1116 /* the above function sets the state for us in case of an error */
1117 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1121 * Now that the STREAMINFO block is written, we can init this to an
1122 * absurdly-high value...
1124 encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
1125 /* ... and clear this to 0 */
1126 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
1129 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1130 * if not, we will write an empty one (FLAC__add_metadata_block()
1131 * automatically supplies the vendor string).
1133 * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1134 * the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1135 * true it will have already insured that the metadata list is properly
1138 if(!metadata_has_vorbis_comment) {
1139 FLAC__StreamMetadata vorbis_comment;
1140 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1141 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1142 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1143 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1144 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1145 vorbis_comment.data.vorbis_comment.num_comments = 0;
1146 vorbis_comment.data.vorbis_comment.comments = 0;
1147 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1148 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1149 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1151 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1152 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1153 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1155 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1156 /* the above function sets the state for us in case of an error */
1157 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1162 * write the user's metadata blocks
1164 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1165 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
1166 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1167 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1168 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1170 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1171 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1172 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1174 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1175 /* the above function sets the state for us in case of an error */
1176 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1180 /* now that all the metadata is written, we save the stream offset */
1181 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &encoder->protected_->audio_offset, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) { /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
1182 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1183 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1186 if(encoder->protected_->verify)
1187 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1189 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1192 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1193 FLAC__StreamEncoder *encoder,
1194 FLAC__StreamEncoderWriteCallback write_callback,
1195 FLAC__StreamEncoderSeekCallback seek_callback,
1196 FLAC__StreamEncoderTellCallback tell_callback,
1197 FLAC__StreamEncoderMetadataCallback metadata_callback,
1201 return init_stream_internal_(
1203 /*read_callback=*/0,
1213 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1214 FLAC__StreamEncoder *encoder,
1215 FLAC__StreamEncoderReadCallback read_callback,
1216 FLAC__StreamEncoderWriteCallback write_callback,
1217 FLAC__StreamEncoderSeekCallback seek_callback,
1218 FLAC__StreamEncoderTellCallback tell_callback,
1219 FLAC__StreamEncoderMetadataCallback metadata_callback,
1223 return init_stream_internal_(
1235 static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1236 FLAC__StreamEncoder *encoder,
1238 FLAC__StreamEncoderProgressCallback progress_callback,
1243 FLAC__StreamEncoderInitStatus init_status;
1245 FLAC__ASSERT(0 != encoder);
1246 FLAC__ASSERT(0 != file);
1248 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1249 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1251 /* double protection */
1253 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1254 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1258 * To make sure that our file does not go unclosed after an error, we
1259 * must assign the FILE pointer before any further error can occur in
1263 file = get_binary_stdout_(); /* just to be safe */
1265 encoder->private_->file = file;
1267 encoder->private_->progress_callback = progress_callback;
1268 encoder->private_->bytes_written = 0;
1269 encoder->private_->samples_written = 0;
1270 encoder->private_->frames_written = 0;
1272 init_status = init_stream_internal_(
1274 encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0,
1275 file_write_callback_,
1276 encoder->private_->file == stdout? 0 : file_seek_callback_,
1277 encoder->private_->file == stdout? 0 : file_tell_callback_,
1278 /*metadata_callback=*/0,
1282 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1283 /* the above function sets the state for us in case of an error */
1288 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1290 FLAC__ASSERT(blocksize != 0);
1291 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1297 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1298 FLAC__StreamEncoder *encoder,
1300 FLAC__StreamEncoderProgressCallback progress_callback,
1304 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1307 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1308 FLAC__StreamEncoder *encoder,
1310 FLAC__StreamEncoderProgressCallback progress_callback,
1314 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1317 static FLAC__StreamEncoderInitStatus init_file_internal_(
1318 FLAC__StreamEncoder *encoder,
1319 const char *filename,
1320 FLAC__StreamEncoderProgressCallback progress_callback,
1327 FLAC__ASSERT(0 != encoder);
1330 * To make sure that our file does not go unclosed after an error, we
1331 * have to do the same entrance checks here that are later performed
1332 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1334 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1335 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1337 file = filename? fopen(filename, "w+b") : stdout;
1340 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1341 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1344 return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1347 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1348 FLAC__StreamEncoder *encoder,
1349 const char *filename,
1350 FLAC__StreamEncoderProgressCallback progress_callback,
1354 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1357 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1358 FLAC__StreamEncoder *encoder,
1359 const char *filename,
1360 FLAC__StreamEncoderProgressCallback progress_callback,
1364 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
1367 FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
1369 FLAC__bool error = false;
1371 FLAC__ASSERT(0 != encoder);
1372 FLAC__ASSERT(0 != encoder->private_);
1373 FLAC__ASSERT(0 != encoder->protected_);
1375 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
1378 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
1379 if(encoder->private_->current_sample_number != 0) {
1380 const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
1381 encoder->protected_->blocksize = encoder->private_->current_sample_number;
1382 if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
1387 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
1389 if(!encoder->private_->is_being_deleted) {
1390 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
1391 if(encoder->private_->seek_callback) {
1393 if(encoder->private_->is_ogg)
1394 update_ogg_metadata_(encoder);
1397 update_metadata_(encoder);
1399 /* check if an error occurred while updating metadata */
1400 if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
1403 if(encoder->private_->metadata_callback)
1404 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1407 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
1409 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1414 if(0 != encoder->private_->file) {
1415 if(encoder->private_->file != stdout)
1416 fclose(encoder->private_->file);
1417 encoder->private_->file = 0;
1421 if(encoder->private_->is_ogg)
1422 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1426 set_defaults_(encoder);
1429 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1434 FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
1436 FLAC__ASSERT(0 != encoder);
1437 FLAC__ASSERT(0 != encoder->private_);
1438 FLAC__ASSERT(0 != encoder->protected_);
1439 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1442 /* can't check encoder->private_->is_ogg since that's not set until init time */
1443 FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1451 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
1453 FLAC__ASSERT(0 != encoder);
1454 FLAC__ASSERT(0 != encoder->private_);
1455 FLAC__ASSERT(0 != encoder->protected_);
1456 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1458 #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1459 encoder->protected_->verify = value;
1464 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
1466 FLAC__ASSERT(0 != encoder);
1467 FLAC__ASSERT(0 != encoder->private_);
1468 FLAC__ASSERT(0 != encoder->protected_);
1469 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1471 encoder->protected_->streamable_subset = value;
1475 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
1477 FLAC__ASSERT(0 != encoder);
1478 FLAC__ASSERT(0 != encoder->private_);
1479 FLAC__ASSERT(0 != encoder->protected_);
1480 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1482 encoder->protected_->channels = value;
1486 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
1488 FLAC__ASSERT(0 != encoder);
1489 FLAC__ASSERT(0 != encoder->private_);
1490 FLAC__ASSERT(0 != encoder->protected_);
1491 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1493 encoder->protected_->bits_per_sample = value;
1497 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
1499 FLAC__ASSERT(0 != encoder);
1500 FLAC__ASSERT(0 != encoder->private_);
1501 FLAC__ASSERT(0 != encoder->protected_);
1502 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1504 encoder->protected_->sample_rate = value;
1508 FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
1510 FLAC__bool ok = true;
1511 FLAC__ASSERT(0 != encoder);
1512 FLAC__ASSERT(0 != encoder->private_);
1513 FLAC__ASSERT(0 != encoder->protected_);
1514 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1516 if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1517 value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1518 ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
1519 ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
1520 ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
1521 ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1522 ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1523 ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1524 ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1525 ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1526 ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1527 ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1528 ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1532 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1534 FLAC__ASSERT(0 != encoder);
1535 FLAC__ASSERT(0 != encoder->private_);
1536 FLAC__ASSERT(0 != encoder->protected_);
1537 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1539 encoder->protected_->blocksize = value;
1543 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1545 FLAC__ASSERT(0 != encoder);
1546 FLAC__ASSERT(0 != encoder->private_);
1547 FLAC__ASSERT(0 != encoder->protected_);
1548 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1550 encoder->protected_->do_mid_side_stereo = value;
1554 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1556 FLAC__ASSERT(0 != encoder);
1557 FLAC__ASSERT(0 != encoder->private_);
1558 FLAC__ASSERT(0 != encoder->protected_);
1559 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1561 encoder->protected_->loose_mid_side_stereo = value;
1565 FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1567 FLAC__ASSERT(0 != encoder);
1568 FLAC__ASSERT(0 != encoder->private_);
1569 FLAC__ASSERT(0 != encoder->protected_);
1570 FLAC__ASSERT(0 != specification);
1571 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1573 #ifdef FLAC__INTEGER_ONLY_LIBRARY
1574 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1576 encoder->protected_->num_apodizations = 0;
1578 const char *s = strchr(specification, ';');
1579 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1580 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1581 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1582 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1583 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1584 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1585 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1586 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1587 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1588 else if(n==6 && 0 == strncmp("connes" , specification, n))
1589 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1590 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1591 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1592 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1593 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1594 if (stddev > 0.0 && stddev <= 0.5) {
1595 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1596 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1599 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1600 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1601 else if(n==4 && 0 == strncmp("hann" , specification, n))
1602 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1603 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1604 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1605 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1606 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1607 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1608 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1609 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1610 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1611 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1612 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1613 if (p >= 0.0 && p <= 1.0) {
1614 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1615 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1618 else if(n==5 && 0 == strncmp("welch" , specification, n))
1619 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1620 if (encoder->protected_->num_apodizations == 32)
1623 specification = s+1;
1627 if(encoder->protected_->num_apodizations == 0) {
1628 encoder->protected_->num_apodizations = 1;
1629 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1630 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1636 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1638 FLAC__ASSERT(0 != encoder);
1639 FLAC__ASSERT(0 != encoder->private_);
1640 FLAC__ASSERT(0 != encoder->protected_);
1641 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1643 encoder->protected_->max_lpc_order = value;
1647 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1649 FLAC__ASSERT(0 != encoder);
1650 FLAC__ASSERT(0 != encoder->private_);
1651 FLAC__ASSERT(0 != encoder->protected_);
1652 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1654 encoder->protected_->qlp_coeff_precision = value;
1658 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1660 FLAC__ASSERT(0 != encoder);
1661 FLAC__ASSERT(0 != encoder->private_);
1662 FLAC__ASSERT(0 != encoder->protected_);
1663 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1665 encoder->protected_->do_qlp_coeff_prec_search = value;
1669 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1671 FLAC__ASSERT(0 != encoder);
1672 FLAC__ASSERT(0 != encoder->private_);
1673 FLAC__ASSERT(0 != encoder->protected_);
1674 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1677 /*@@@ deprecated: */
1678 encoder->protected_->do_escape_coding = value;
1685 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1687 FLAC__ASSERT(0 != encoder);
1688 FLAC__ASSERT(0 != encoder->private_);
1689 FLAC__ASSERT(0 != encoder->protected_);
1690 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1692 encoder->protected_->do_exhaustive_model_search = value;
1696 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1698 FLAC__ASSERT(0 != encoder);
1699 FLAC__ASSERT(0 != encoder->private_);
1700 FLAC__ASSERT(0 != encoder->protected_);
1701 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1703 encoder->protected_->min_residual_partition_order = value;
1707 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1709 FLAC__ASSERT(0 != encoder);
1710 FLAC__ASSERT(0 != encoder->private_);
1711 FLAC__ASSERT(0 != encoder->protected_);
1712 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1714 encoder->protected_->max_residual_partition_order = value;
1718 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1720 FLAC__ASSERT(0 != encoder);
1721 FLAC__ASSERT(0 != encoder->private_);
1722 FLAC__ASSERT(0 != encoder->protected_);
1723 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1726 /*@@@ deprecated: */
1727 encoder->protected_->rice_parameter_search_dist = value;
1734 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1736 FLAC__ASSERT(0 != encoder);
1737 FLAC__ASSERT(0 != encoder->private_);
1738 FLAC__ASSERT(0 != encoder->protected_);
1739 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1741 encoder->protected_->total_samples_estimate = value;
1745 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1747 FLAC__ASSERT(0 != encoder);
1748 FLAC__ASSERT(0 != encoder->private_);
1749 FLAC__ASSERT(0 != encoder->protected_);
1750 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1752 encoder->protected_->metadata = metadata;
1753 encoder->protected_->num_metadata_blocks = num_blocks;
1755 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1762 * These three functions are not static, but not publically exposed in
1763 * include/FLAC/ either. They are used by the test suite.
1765 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1767 FLAC__ASSERT(0 != encoder);
1768 FLAC__ASSERT(0 != encoder->private_);
1769 FLAC__ASSERT(0 != encoder->protected_);
1770 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1772 encoder->private_->disable_constant_subframes = value;
1776 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1778 FLAC__ASSERT(0 != encoder);
1779 FLAC__ASSERT(0 != encoder->private_);
1780 FLAC__ASSERT(0 != encoder->protected_);
1781 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1783 encoder->private_->disable_fixed_subframes = value;
1787 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1789 FLAC__ASSERT(0 != encoder);
1790 FLAC__ASSERT(0 != encoder->private_);
1791 FLAC__ASSERT(0 != encoder->protected_);
1792 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1794 encoder->private_->disable_verbatim_subframes = value;
1798 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1800 FLAC__ASSERT(0 != encoder);
1801 FLAC__ASSERT(0 != encoder->private_);
1802 FLAC__ASSERT(0 != encoder->protected_);
1803 return encoder->protected_->state;
1806 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1808 FLAC__ASSERT(0 != encoder);
1809 FLAC__ASSERT(0 != encoder->private_);
1810 FLAC__ASSERT(0 != encoder->protected_);
1811 if(encoder->protected_->verify)
1812 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1814 return FLAC__STREAM_DECODER_UNINITIALIZED;
1817 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1819 FLAC__ASSERT(0 != encoder);
1820 FLAC__ASSERT(0 != encoder->private_);
1821 FLAC__ASSERT(0 != encoder->protected_);
1822 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1823 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1825 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
1828 FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
1830 FLAC__ASSERT(0 != encoder);
1831 FLAC__ASSERT(0 != encoder->private_);
1832 FLAC__ASSERT(0 != encoder->protected_);
1833 if(0 != absolute_sample)
1834 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1835 if(0 != frame_number)
1836 *frame_number = encoder->private_->verify.error_stats.frame_number;
1838 *channel = encoder->private_->verify.error_stats.channel;
1840 *sample = encoder->private_->verify.error_stats.sample;
1842 *expected = encoder->private_->verify.error_stats.expected;
1844 *got = encoder->private_->verify.error_stats.got;
1847 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1849 FLAC__ASSERT(0 != encoder);
1850 FLAC__ASSERT(0 != encoder->private_);
1851 FLAC__ASSERT(0 != encoder->protected_);
1852 return encoder->protected_->verify;
1855 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1857 FLAC__ASSERT(0 != encoder);
1858 FLAC__ASSERT(0 != encoder->private_);
1859 FLAC__ASSERT(0 != encoder->protected_);
1860 return encoder->protected_->streamable_subset;
1863 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1865 FLAC__ASSERT(0 != encoder);
1866 FLAC__ASSERT(0 != encoder->private_);
1867 FLAC__ASSERT(0 != encoder->protected_);
1868 return encoder->protected_->channels;
1871 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1873 FLAC__ASSERT(0 != encoder);
1874 FLAC__ASSERT(0 != encoder->private_);
1875 FLAC__ASSERT(0 != encoder->protected_);
1876 return encoder->protected_->bits_per_sample;
1879 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1881 FLAC__ASSERT(0 != encoder);
1882 FLAC__ASSERT(0 != encoder->private_);
1883 FLAC__ASSERT(0 != encoder->protected_);
1884 return encoder->protected_->sample_rate;
1887 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1889 FLAC__ASSERT(0 != encoder);
1890 FLAC__ASSERT(0 != encoder->private_);
1891 FLAC__ASSERT(0 != encoder->protected_);
1892 return encoder->protected_->blocksize;
1895 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1897 FLAC__ASSERT(0 != encoder);
1898 FLAC__ASSERT(0 != encoder->private_);
1899 FLAC__ASSERT(0 != encoder->protected_);
1900 return encoder->protected_->do_mid_side_stereo;
1903 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1905 FLAC__ASSERT(0 != encoder);
1906 FLAC__ASSERT(0 != encoder->private_);
1907 FLAC__ASSERT(0 != encoder->protected_);
1908 return encoder->protected_->loose_mid_side_stereo;
1911 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1913 FLAC__ASSERT(0 != encoder);
1914 FLAC__ASSERT(0 != encoder->private_);
1915 FLAC__ASSERT(0 != encoder->protected_);
1916 return encoder->protected_->max_lpc_order;
1919 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1921 FLAC__ASSERT(0 != encoder);
1922 FLAC__ASSERT(0 != encoder->private_);
1923 FLAC__ASSERT(0 != encoder->protected_);
1924 return encoder->protected_->qlp_coeff_precision;
1927 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1929 FLAC__ASSERT(0 != encoder);
1930 FLAC__ASSERT(0 != encoder->private_);
1931 FLAC__ASSERT(0 != encoder->protected_);
1932 return encoder->protected_->do_qlp_coeff_prec_search;
1935 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1937 FLAC__ASSERT(0 != encoder);
1938 FLAC__ASSERT(0 != encoder->private_);
1939 FLAC__ASSERT(0 != encoder->protected_);
1940 return encoder->protected_->do_escape_coding;
1943 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1945 FLAC__ASSERT(0 != encoder);
1946 FLAC__ASSERT(0 != encoder->private_);
1947 FLAC__ASSERT(0 != encoder->protected_);
1948 return encoder->protected_->do_exhaustive_model_search;
1951 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1953 FLAC__ASSERT(0 != encoder);
1954 FLAC__ASSERT(0 != encoder->private_);
1955 FLAC__ASSERT(0 != encoder->protected_);
1956 return encoder->protected_->min_residual_partition_order;
1959 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1961 FLAC__ASSERT(0 != encoder);
1962 FLAC__ASSERT(0 != encoder->private_);
1963 FLAC__ASSERT(0 != encoder->protected_);
1964 return encoder->protected_->max_residual_partition_order;
1967 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1969 FLAC__ASSERT(0 != encoder);
1970 FLAC__ASSERT(0 != encoder->private_);
1971 FLAC__ASSERT(0 != encoder->protected_);
1972 return encoder->protected_->rice_parameter_search_dist;
1975 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1977 FLAC__ASSERT(0 != encoder);
1978 FLAC__ASSERT(0 != encoder->private_);
1979 FLAC__ASSERT(0 != encoder->protected_);
1980 return encoder->protected_->total_samples_estimate;
1983 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1985 unsigned i, j, channel;
1986 FLAC__int32 x, mid, side;
1987 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1989 FLAC__ASSERT(0 != encoder);
1990 FLAC__ASSERT(0 != encoder->private_);
1991 FLAC__ASSERT(0 != encoder->protected_);
1992 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1996 * we have several flavors of the same basic loop, optimized for
1997 * different conditions:
1999 if(encoder->protected_->max_lpc_order > 0) {
2000 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2002 * stereo coding: unroll channel loop
2003 * with LPC: calculate floating point version of signal
2006 if(encoder->protected_->verify)
2007 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2009 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2010 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2011 x = mid = side = buffer[0][j];
2012 encoder->private_->integer_signal[0][i] = x;
2013 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2014 encoder->private_->real_signal[0][i] = (FLAC__real)x;
2017 encoder->private_->integer_signal[1][i] = x;
2018 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2019 encoder->private_->real_signal[1][i] = (FLAC__real)x;
2023 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2024 encoder->private_->integer_signal_mid_side[1][i] = side;
2025 encoder->private_->integer_signal_mid_side[0][i] = mid;
2026 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2027 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2028 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
2030 encoder->private_->current_sample_number++;
2032 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2034 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2036 /* move unprocessed overread samples to beginnings of arrays */
2037 FLAC__ASSERT(i == blocksize+OVERREAD_);
2038 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2040 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2041 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2042 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2043 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2044 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2045 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2046 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2047 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2048 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2050 encoder->private_->current_sample_number = 1;
2052 } while(j < samples);
2056 * independent channel coding: buffer each channel in inner loop
2057 * with LPC: calculate floating point version of signal
2060 if(encoder->protected_->verify)
2061 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2063 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2064 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2065 for(channel = 0; channel < channels; channel++) {
2066 x = buffer[channel][j];
2067 encoder->private_->integer_signal[channel][i] = x;
2068 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2069 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2072 encoder->private_->current_sample_number++;
2074 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2076 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2078 /* move unprocessed overread samples to beginnings of arrays */
2079 FLAC__ASSERT(i == blocksize+OVERREAD_);
2080 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2082 for(channel = 0; channel < channels; channel++) {
2083 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2084 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2085 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2088 encoder->private_->current_sample_number = 1;
2090 } while(j < samples);
2094 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2096 * stereo coding: unroll channel loop
2097 * without LPC: no need to calculate floating point version of signal
2100 if(encoder->protected_->verify)
2101 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2103 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2104 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2105 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
2107 encoder->private_->integer_signal[1][i] = x;
2110 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2111 encoder->private_->integer_signal_mid_side[1][i] = side;
2112 encoder->private_->integer_signal_mid_side[0][i] = mid;
2113 encoder->private_->current_sample_number++;
2115 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2117 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2119 /* move unprocessed overread samples to beginnings of arrays */
2120 FLAC__ASSERT(i == blocksize+OVERREAD_);
2121 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2123 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2124 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2125 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2126 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2127 encoder->private_->current_sample_number = 1;
2129 } while(j < samples);
2133 * independent channel coding: buffer each channel in inner loop
2134 * without LPC: no need to calculate floating point version of signal
2137 if(encoder->protected_->verify)
2138 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2140 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2141 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2142 for(channel = 0; channel < channels; channel++)
2143 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
2144 encoder->private_->current_sample_number++;
2146 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2148 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2150 /* move unprocessed overread samples to beginnings of arrays */
2151 FLAC__ASSERT(i == blocksize+OVERREAD_);
2152 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2154 for(channel = 0; channel < channels; channel++)
2155 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2156 encoder->private_->current_sample_number = 1;
2158 } while(j < samples);
2165 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
2167 unsigned i, j, k, channel;
2168 FLAC__int32 x, mid, side;
2169 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
2171 FLAC__ASSERT(0 != encoder);
2172 FLAC__ASSERT(0 != encoder->private_);
2173 FLAC__ASSERT(0 != encoder->protected_);
2174 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2178 * we have several flavors of the same basic loop, optimized for
2179 * different conditions:
2181 if(encoder->protected_->max_lpc_order > 0) {
2182 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2184 * stereo coding: unroll channel loop
2185 * with LPC: calculate floating point version of signal
2188 if(encoder->protected_->verify)
2189 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2191 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2192 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2193 x = mid = side = buffer[k++];
2194 encoder->private_->integer_signal[0][i] = x;
2195 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2196 encoder->private_->real_signal[0][i] = (FLAC__real)x;
2199 encoder->private_->integer_signal[1][i] = x;
2200 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2201 encoder->private_->real_signal[1][i] = (FLAC__real)x;
2205 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2206 encoder->private_->integer_signal_mid_side[1][i] = side;
2207 encoder->private_->integer_signal_mid_side[0][i] = mid;
2208 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2209 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2210 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
2212 encoder->private_->current_sample_number++;
2214 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2216 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2218 /* move unprocessed overread samples to beginnings of arrays */
2219 FLAC__ASSERT(i == blocksize+OVERREAD_);
2220 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2222 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2223 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2224 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2225 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2226 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2227 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2228 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2229 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2230 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2232 encoder->private_->current_sample_number = 1;
2234 } while(j < samples);
2238 * independent channel coding: buffer each channel in inner loop
2239 * with LPC: calculate floating point version of signal
2242 if(encoder->protected_->verify)
2243 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2245 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2246 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2247 for(channel = 0; channel < channels; channel++) {
2249 encoder->private_->integer_signal[channel][i] = x;
2250 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2251 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2254 encoder->private_->current_sample_number++;
2256 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2258 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2260 /* move unprocessed overread samples to beginnings of arrays */
2261 FLAC__ASSERT(i == blocksize+OVERREAD_);
2262 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2264 for(channel = 0; channel < channels; channel++) {
2265 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2266 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2267 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2270 encoder->private_->current_sample_number = 1;
2272 } while(j < samples);
2276 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2278 * stereo coding: unroll channel loop
2279 * without LPC: no need to calculate floating point version of signal
2282 if(encoder->protected_->verify)
2283 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2285 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2286 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2287 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
2289 encoder->private_->integer_signal[1][i] = x;
2292 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2293 encoder->private_->integer_signal_mid_side[1][i] = side;
2294 encoder->private_->integer_signal_mid_side[0][i] = mid;
2295 encoder->private_->current_sample_number++;
2297 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2299 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2301 /* move unprocessed overread samples to beginnings of arrays */
2302 FLAC__ASSERT(i == blocksize+OVERREAD_);
2303 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2305 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2306 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2307 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2308 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2309 encoder->private_->current_sample_number = 1;
2311 } while(j < samples);
2315 * independent channel coding: buffer each channel in inner loop
2316 * without LPC: no need to calculate floating point version of signal
2319 if(encoder->protected_->verify)
2320 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2322 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2323 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2324 for(channel = 0; channel < channels; channel++)
2325 encoder->private_->integer_signal[channel][i] = buffer[k++];
2326 encoder->private_->current_sample_number++;
2328 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2330 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2332 /* move unprocessed overread samples to beginnings of arrays */
2333 FLAC__ASSERT(i == blocksize+OVERREAD_);
2334 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2336 for(channel = 0; channel < channels; channel++)
2337 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2338 encoder->private_->current_sample_number = 1;
2340 } while(j < samples);
2347 /***********************************************************************
2349 * Private class methods
2351 ***********************************************************************/
2353 void set_defaults_(FLAC__StreamEncoder *encoder)
2355 FLAC__ASSERT(0 != encoder);
2357 #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2358 encoder->protected_->verify = true;
2360 encoder->protected_->verify = false;
2362 encoder->protected_->streamable_subset = true;
2363 encoder->protected_->do_mid_side_stereo = false;
2364 encoder->protected_->loose_mid_side_stereo = false;
2365 encoder->protected_->channels = 2;
2366 encoder->protected_->bits_per_sample = 16;
2367 encoder->protected_->sample_rate = 44100;
2368 encoder->protected_->blocksize = 0;
2369 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2370 encoder->protected_->num_apodizations = 1;
2371 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2372 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
2374 encoder->protected_->max_lpc_order = 0;
2375 encoder->protected_->qlp_coeff_precision = 0;
2376 encoder->protected_->do_qlp_coeff_prec_search = false;
2377 encoder->protected_->do_exhaustive_model_search = false;
2378 encoder->protected_->do_escape_coding = false;
2379 encoder->protected_->min_residual_partition_order = 0;
2380 encoder->protected_->max_residual_partition_order = 0;
2381 encoder->protected_->rice_parameter_search_dist = 0;
2382 encoder->protected_->total_samples_estimate = 0;
2383 encoder->protected_->metadata = 0;
2384 encoder->protected_->num_metadata_blocks = 0;
2386 encoder->private_->seek_table = 0;
2387 encoder->private_->disable_constant_subframes = false;
2388 encoder->private_->disable_fixed_subframes = false;
2389 encoder->private_->disable_verbatim_subframes = false;
2391 encoder->private_->is_ogg = false;
2393 encoder->private_->read_callback = 0;
2394 encoder->private_->write_callback = 0;
2395 encoder->private_->seek_callback = 0;
2396 encoder->private_->tell_callback = 0;
2397 encoder->private_->metadata_callback = 0;
2398 encoder->private_->progress_callback = 0;
2399 encoder->private_->client_data = 0;
2402 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2406 void free_(FLAC__StreamEncoder *encoder)
2408 unsigned i, channel;
2410 FLAC__ASSERT(0 != encoder);
2411 for(i = 0; i < encoder->protected_->channels; i++) {
2412 if(0 != encoder->private_->integer_signal_unaligned[i]) {
2413 free(encoder->private_->integer_signal_unaligned[i]);
2414 encoder->private_->integer_signal_unaligned[i] = 0;
2416 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2417 if(0 != encoder->private_->real_signal_unaligned[i]) {
2418 free(encoder->private_->real_signal_unaligned[i]);
2419 encoder->private_->real_signal_unaligned[i] = 0;
2423 for(i = 0; i < 2; i++) {
2424 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
2425 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2426 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2428 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2429 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
2430 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2431 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2435 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2436 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2437 if(0 != encoder->private_->window_unaligned[i]) {
2438 free(encoder->private_->window_unaligned[i]);
2439 encoder->private_->window_unaligned[i] = 0;
2442 if(0 != encoder->private_->windowed_signal_unaligned) {
2443 free(encoder->private_->windowed_signal_unaligned);
2444 encoder->private_->windowed_signal_unaligned = 0;
2447 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2448 for(i = 0; i < 2; i++) {
2449 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
2450 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2451 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2455 for(channel = 0; channel < 2; channel++) {
2456 for(i = 0; i < 2; i++) {
2457 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
2458 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2459 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2463 if(0 != encoder->private_->abs_residual_unaligned) {
2464 free(encoder->private_->abs_residual_unaligned);
2465 encoder->private_->abs_residual_unaligned = 0;
2467 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
2468 free(encoder->private_->abs_residual_partition_sums_unaligned);
2469 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2471 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
2472 free(encoder->private_->raw_bits_per_partition_unaligned);
2473 encoder->private_->raw_bits_per_partition_unaligned = 0;
2475 if(encoder->protected_->verify) {
2476 for(i = 0; i < encoder->protected_->channels; i++) {
2477 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2478 free(encoder->private_->verify.input_fifo.data[i]);
2479 encoder->private_->verify.input_fifo.data[i] = 0;
2483 FLAC__bitbuffer_free(encoder->private_->frame);
2486 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
2489 unsigned i, channel;
2491 FLAC__ASSERT(new_blocksize > 0);
2492 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2493 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
2495 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
2496 if(new_blocksize <= encoder->private_->input_capacity)
2501 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2502 * requires that the input arrays (in our case the integer signals)
2503 * have a buffer of up to 3 zeroes in front (at negative indices) for
2504 * alignment purposes; we use 4 in front to keep the data well-aligned.
2507 for(i = 0; ok && i < encoder->protected_->channels; i++) {
2508 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
2509 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2510 encoder->private_->integer_signal[i] += 4;
2511 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2512 if(encoder->protected_->max_lpc_order > 0)
2513 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
2516 for(i = 0; ok && i < 2; i++) {
2517 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
2518 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2519 encoder->private_->integer_signal_mid_side[i] += 4;
2520 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2521 if(encoder->protected_->max_lpc_order > 0)
2522 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
2525 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2526 if(ok && encoder->protected_->max_lpc_order > 0) {
2527 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2528 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2529 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
2532 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
2533 for(i = 0; ok && i < 2; i++) {
2534 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
2537 for(channel = 0; ok && channel < 2; channel++) {
2538 for(i = 0; ok && i < 2; i++) {
2539 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
2542 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_blocksize, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
2543 if(encoder->private_->precompute_partition_sums || encoder->protected_->do_escape_coding) /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
2544 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
2545 if(encoder->protected_->do_escape_coding)
2546 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
2548 /* now adjust the windows if the blocksize has changed */
2549 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2550 if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
2551 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2552 switch(encoder->protected_->apodizations[i].type) {
2553 case FLAC__APODIZATION_BARTLETT:
2554 FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
2556 case FLAC__APODIZATION_BARTLETT_HANN:
2557 FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
2559 case FLAC__APODIZATION_BLACKMAN:
2560 FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
2562 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2563 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
2565 case FLAC__APODIZATION_CONNES:
2566 FLAC__window_connes(encoder->private_->window[i], new_blocksize);
2568 case FLAC__APODIZATION_FLATTOP:
2569 FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
2571 case FLAC__APODIZATION_GAUSS:
2572 FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2574 case FLAC__APODIZATION_HAMMING:
2575 FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
2577 case FLAC__APODIZATION_HANN:
2578 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2580 case FLAC__APODIZATION_KAISER_BESSEL:
2581 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
2583 case FLAC__APODIZATION_NUTTALL:
2584 FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
2586 case FLAC__APODIZATION_RECTANGLE:
2587 FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
2589 case FLAC__APODIZATION_TRIANGLE:
2590 FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
2592 case FLAC__APODIZATION_TUKEY:
2593 FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
2595 case FLAC__APODIZATION_WELCH:
2596 FLAC__window_welch(encoder->private_->window[i], new_blocksize);
2600 /* double protection */
2601 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2609 encoder->private_->input_capacity = new_blocksize;
2611 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2616 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
2618 const FLAC__byte *buffer;
2621 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2623 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2625 if(encoder->protected_->verify) {
2626 encoder->private_->verify.output.data = buffer;
2627 encoder->private_->verify.output.bytes = bytes;
2628 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2629 encoder->private_->verify.needs_magic_hack = true;
2632 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2633 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2634 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2635 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2641 if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2642 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2643 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2647 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2650 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2651 encoder->private_->streaminfo.data.stream_info.max_framesize = max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
2657 FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block)
2659 FLAC__StreamEncoderWriteStatus status;
2660 FLAC__uint64 output_position = 0;
2662 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2663 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2664 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2665 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2669 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2672 FLAC__MetadataType type = (buffer[0] & 0x7f);
2673 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2674 encoder->protected_->streaminfo_offset = output_position;
2675 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2676 encoder->protected_->seektable_offset = output_position;
2680 * Mark the current seek point if hit (if audio_offset == 0 that
2681 * means we're still writing metadata and haven't hit the first
2684 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2685 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2686 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2687 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2688 FLAC__uint64 test_sample;
2690 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2691 test_sample = encoder->private_->seek_table->points[i].sample_number;
2692 if(test_sample > frame_last_sample) {
2695 else if(test_sample >= frame_first_sample) {
2696 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2697 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2698 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2699 encoder->private_->first_seekpoint_to_check++;
2700 /* DO NOT: "break;" and here's why:
2701 * The seektable template may contain more than one target
2702 * sample for any given frame; we will keep looping, generating
2703 * duplicate seekpoints for them, and we'll clean it up later,
2704 * just before writing the seektable back to the metadata.
2708 encoder->private_->first_seekpoint_to_check++;
2714 if(encoder->private_->is_ogg) {
2715 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2716 &encoder->protected_->ogg_encoder_aspect,
2720 encoder->private_->current_frame_number,
2722 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2724 encoder->private_->client_data
2729 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2731 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2732 encoder->private_->bytes_written += bytes;
2733 encoder->private_->samples_written += samples;
2734 /* we keep a high watermark on the number of frames written because
2735 * when the encoder goes back to write metadata, 'current_frame'
2736 * will drop back to 0.
2738 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2741 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2746 /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2747 void update_metadata_(const FLAC__StreamEncoder *encoder)
2749 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2750 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2751 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2752 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2753 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2754 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2755 FLAC__StreamEncoderSeekStatus seek_status;
2757 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2759 /* All this is based on intimate knowledge of the stream header
2760 * layout, but a change to the header format that would break this
2761 * would also break all streams encoded in the previous format.
2765 * Write MD5 signature
2768 const unsigned md5_offset =
2769 FLAC__STREAM_METADATA_HEADER_LENGTH +
2771 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2772 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2773 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2774 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2775 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2776 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2777 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2778 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2781 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2782 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2783 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2786 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2787 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2793 * Write total samples
2796 const unsigned total_samples_byte_offset =
2797 FLAC__STREAM_METADATA_HEADER_LENGTH +
2799 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2800 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2801 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2802 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2803 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2804 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2805 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2809 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2810 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2811 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2812 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2813 b[4] = (FLAC__byte)(samples & 0xFF);
2814 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + total_samples_byte_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2815 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2816 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2819 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2820 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2826 * Write min/max framesize
2829 const unsigned min_framesize_offset =
2830 FLAC__STREAM_METADATA_HEADER_LENGTH +
2832 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2833 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2836 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2837 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2838 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2839 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2840 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2841 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2842 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + min_framesize_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2843 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2844 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2847 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2848 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2856 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2859 FLAC__format_seektable_sort(encoder->private_->seek_table);
2861 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2863 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->seektable_offset + FLAC__STREAM_METADATA_HEADER_LENGTH, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2864 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2865 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2869 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2872 xx = encoder->private_->seek_table->points[i].sample_number;
2873 b[7] = (FLAC__byte)xx; xx >>= 8;
2874 b[6] = (FLAC__byte)xx; xx >>= 8;
2875 b[5] = (FLAC__byte)xx; xx >>= 8;
2876 b[4] = (FLAC__byte)xx; xx >>= 8;
2877 b[3] = (FLAC__byte)xx; xx >>= 8;
2878 b[2] = (FLAC__byte)xx; xx >>= 8;
2879 b[1] = (FLAC__byte)xx; xx >>= 8;
2880 b[0] = (FLAC__byte)xx; xx >>= 8;
2881 xx = encoder->private_->seek_table->points[i].stream_offset;
2882 b[15] = (FLAC__byte)xx; xx >>= 8;
2883 b[14] = (FLAC__byte)xx; xx >>= 8;
2884 b[13] = (FLAC__byte)xx; xx >>= 8;
2885 b[12] = (FLAC__byte)xx; xx >>= 8;
2886 b[11] = (FLAC__byte)xx; xx >>= 8;
2887 b[10] = (FLAC__byte)xx; xx >>= 8;
2888 b[9] = (FLAC__byte)xx; xx >>= 8;
2889 b[8] = (FLAC__byte)xx; xx >>= 8;
2890 x = encoder->private_->seek_table->points[i].frame_samples;
2891 b[17] = (FLAC__byte)x; x >>= 8;
2892 b[16] = (FLAC__byte)x; x >>= 8;
2893 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2894 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2902 /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2903 void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2905 /* the # of bytes in the 1st packet that precede the STREAMINFO */
2906 static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
2907 FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
2908 FLAC__OGG_MAPPING_MAGIC_LENGTH +
2909 FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
2910 FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
2911 FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
2912 FLAC__STREAM_SYNC_LENGTH
2914 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2915 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2916 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2917 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2918 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2921 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2922 FLAC__ASSERT(0 != encoder->private_->seek_callback);
2924 /* Pre-check that client supports seeking, since we don't want the
2925 * ogg_helper code to ever have to deal with this condition.
2927 if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
2930 /* All this is based on intimate knowledge of the stream header
2931 * layout, but a change to the header format that would break this
2932 * would also break all streams encoded in the previous format.
2936 ** Write STREAMINFO stats
2938 simple_ogg_page__init(&page);
2939 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2940 simple_ogg_page__clear(&page);
2941 return; /* state already set */
2945 * Write MD5 signature
2948 const unsigned md5_offset =
2949 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2950 FLAC__STREAM_METADATA_HEADER_LENGTH +
2952 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2953 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2954 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2955 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2956 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2957 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2958 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2959 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2962 if(md5_offset + 16 > (unsigned)page.body_len) {
2963 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2964 simple_ogg_page__clear(&page);
2967 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2971 * Write total samples
2974 const unsigned total_samples_byte_offset =
2975 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2976 FLAC__STREAM_METADATA_HEADER_LENGTH +
2978 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2979 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2980 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2981 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2982 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2983 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2984 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2988 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2989 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2990 simple_ogg_page__clear(&page);
2993 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2994 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2995 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2996 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2997 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2998 b[4] = (FLAC__byte)(samples & 0xFF);
2999 memcpy(page.body + total_samples_byte_offset, b, 5);
3003 * Write min/max framesize
3006 const unsigned min_framesize_offset =
3007 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
3008 FLAC__STREAM_METADATA_HEADER_LENGTH +
3010 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
3011 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
3014 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
3015 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3016 simple_ogg_page__clear(&page);
3019 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
3020 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
3021 b[2] = (FLAC__byte)(min_framesize & 0xFF);
3022 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
3023 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
3024 b[5] = (FLAC__byte)(max_framesize & 0xFF);
3025 memcpy(page.body + min_framesize_offset, b, 6);
3027 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3028 simple_ogg_page__clear(&page);
3029 return; /* state already set */
3031 simple_ogg_page__clear(&page);
3036 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
3040 FLAC__format_seektable_sort(encoder->private_->seek_table);
3042 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
3044 simple_ogg_page__init(&page);
3045 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
3046 simple_ogg_page__clear(&page);
3047 return; /* state already set */
3050 if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) {
3051 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3052 simple_ogg_page__clear(&page);
3056 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
3059 xx = encoder->private_->seek_table->points[i].sample_number;
3060 b[7] = (FLAC__byte)xx; xx >>= 8;
3061 b[6] = (FLAC__byte)xx; xx >>= 8;
3062 b[5] = (FLAC__byte)xx; xx >>= 8;
3063 b[4] = (FLAC__byte)xx; xx >>= 8;
3064 b[3] = (FLAC__byte)xx; xx >>= 8;
3065 b[2] = (FLAC__byte)xx; xx >>= 8;
3066 b[1] = (FLAC__byte)xx; xx >>= 8;
3067 b[0] = (FLAC__byte)xx; xx >>= 8;
3068 xx = encoder->private_->seek_table->points[i].stream_offset;
3069 b[15] = (FLAC__byte)xx; xx >>= 8;
3070 b[14] = (FLAC__byte)xx; xx >>= 8;
3071 b[13] = (FLAC__byte)xx; xx >>= 8;
3072 b[12] = (FLAC__byte)xx; xx >>= 8;
3073 b[11] = (FLAC__byte)xx; xx >>= 8;
3074 b[10] = (FLAC__byte)xx; xx >>= 8;
3075 b[9] = (FLAC__byte)xx; xx >>= 8;
3076 b[8] = (FLAC__byte)xx; xx >>= 8;
3077 x = encoder->private_->seek_table->points[i].frame_samples;
3078 b[17] = (FLAC__byte)x; x >>= 8;
3079 b[16] = (FLAC__byte)x; x >>= 8;
3083 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3084 simple_ogg_page__clear(&page);
3085 return; /* state already set */
3087 simple_ogg_page__clear(&page);
3092 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
3094 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
3097 * Accumulate raw signal to the MD5 signature
3099 if(!FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) {
3100 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
3105 * Process the frame header and subframes into the frame bitbuffer
3107 if(!process_subframes_(encoder, is_fractional_block)) {
3108 /* the above function sets the state for us in case of an error */
3113 * Zero-pad the frame to a byte_boundary
3115 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
3116 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
3121 * CRC-16 the whole thing
3123 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
3124 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
3129 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
3130 /* the above function sets the state for us in case of an error */
3135 * Get ready for the next frame
3137 encoder->private_->current_sample_number = 0;
3138 encoder->private_->current_frame_number++;
3139 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
3144 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
3146 FLAC__FrameHeader frame_header;
3147 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
3148 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
3151 * Calculate the min,max Rice partition orders
3153 if(is_fractional_block) {
3154 max_partition_order = 0;
3157 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
3158 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
3160 min_partition_order = min(min_partition_order, max_partition_order);
3162 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
3167 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
3168 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
3171 frame_header.blocksize = encoder->protected_->blocksize;
3172 frame_header.sample_rate = encoder->protected_->sample_rate;
3173 frame_header.channels = encoder->protected_->channels;
3174 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
3175 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
3176 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
3177 frame_header.number.frame_number = encoder->private_->current_frame_number;
3180 * Figure out what channel assignments to try
3182 if(encoder->protected_->do_mid_side_stereo) {
3183 if(encoder->protected_->loose_mid_side_stereo) {
3184 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
3185 do_independent = true;
3189 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
3190 do_mid_side = !do_independent;
3194 do_independent = true;
3199 do_independent = true;
3200 do_mid_side = false;
3203 FLAC__ASSERT(do_independent || do_mid_side);
3206 * Check for wasted bits; set effective bps for each subframe
3208 if(do_independent) {
3209 for(channel = 0; channel < encoder->protected_->channels; channel++) {
3210 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
3211 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
3212 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
3216 FLAC__ASSERT(encoder->protected_->channels == 2);
3217 for(channel = 0; channel < 2; channel++) {
3218 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
3219 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
3220 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
3225 * First do a normal encoding pass of each independent channel
3227 if(do_independent) {
3228 for(channel = 0; channel < encoder->protected_->channels; channel++) {
3232 min_partition_order,
3233 max_partition_order,
3234 precompute_partition_sums,
3236 encoder->private_->subframe_bps[channel],
3237 encoder->private_->integer_signal[channel],
3238 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3239 encoder->private_->real_signal[channel],
3241 encoder->private_->subframe_workspace_ptr[channel],
3242 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
3243 encoder->private_->residual_workspace[channel],
3244 encoder->private_->best_subframe+channel,
3245 encoder->private_->best_subframe_bits+channel
3253 * Now do mid and side channels if requested
3256 FLAC__ASSERT(encoder->protected_->channels == 2);
3258 for(channel = 0; channel < 2; channel++) {
3262 min_partition_order,
3263 max_partition_order,
3264 precompute_partition_sums,
3266 encoder->private_->subframe_bps_mid_side[channel],
3267 encoder->private_->integer_signal_mid_side[channel],
3268 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3269 encoder->private_->real_signal_mid_side[channel],
3271 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3272 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3273 encoder->private_->residual_workspace_mid_side[channel],
3274 encoder->private_->best_subframe_mid_side+channel,
3275 encoder->private_->best_subframe_bits_mid_side+channel
3283 * Compose the frame bitbuffer
3286 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3287 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
3288 FLAC__ChannelAssignment channel_assignment;
3290 FLAC__ASSERT(encoder->protected_->channels == 2);
3292 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3293 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
3296 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3298 FLAC__ChannelAssignment ca;
3300 FLAC__ASSERT(do_independent && do_mid_side);
3302 /* We have to figure out which channel assignent results in the smallest frame */
3303 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3304 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3305 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3306 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
3308 for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
3309 if(bits[ca] < min_bits) {
3310 min_bits = bits[ca];
3311 channel_assignment = ca;
3316 frame_header.channel_assignment = channel_assignment;
3318 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3319 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3323 switch(channel_assignment) {
3324 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3325 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3326 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
3328 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3329 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3330 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3332 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3333 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3334 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
3336 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3337 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3338 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3344 switch(channel_assignment) {
3345 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3346 left_bps = encoder->private_->subframe_bps [0];
3347 right_bps = encoder->private_->subframe_bps [1];
3349 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3350 left_bps = encoder->private_->subframe_bps [0];
3351 right_bps = encoder->private_->subframe_bps_mid_side[1];
3353 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3354 left_bps = encoder->private_->subframe_bps_mid_side[1];
3355 right_bps = encoder->private_->subframe_bps [1];
3357 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3358 left_bps = encoder->private_->subframe_bps_mid_side[0];
3359 right_bps = encoder->private_->subframe_bps_mid_side[1];
3365 /* note that encoder_add_subframe_ sets the state for us in case of an error */
3366 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
3368 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
3372 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3373 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3377 for(channel = 0; channel < encoder->protected_->channels; channel++) {
3378 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
3379 /* the above function sets the state for us in case of an error */
3385 if(encoder->protected_->loose_mid_side_stereo) {
3386 encoder->private_->loose_mid_side_stereo_frame_count++;
3387 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3388 encoder->private_->loose_mid_side_stereo_frame_count = 0;
3391 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
3396 FLAC__bool process_subframe_(
3397 FLAC__StreamEncoder *encoder,
3398 unsigned min_partition_order,
3399 unsigned max_partition_order,
3400 FLAC__bool precompute_partition_sums,
3401 const FLAC__FrameHeader *frame_header,
3402 unsigned subframe_bps,
3403 const FLAC__int32 integer_signal[],
3404 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3405 const FLAC__real real_signal[],
3407 FLAC__Subframe *subframe[2],
3408 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3409 FLAC__int32 *residual[2],
3410 unsigned *best_subframe,
3414 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3415 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3417 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3419 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3420 FLAC__double lpc_residual_bits_per_sample;
3421 FLAC__real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->protected_->max_lpc_order might be less; some asm routines need all the space */
3422 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
3423 unsigned min_lpc_order, max_lpc_order, lpc_order;
3424 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
3426 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
3427 unsigned rice_parameter;
3428 unsigned _candidate_bits, _best_bits;
3429 unsigned _best_subframe;
3431 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3433 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3434 _best_bits = UINT_MAX;
3436 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3438 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3439 unsigned signal_is_constant = false;
3440 guess_fixed_order = encoder->private_->local_fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
3441 /* check for constant subframe */
3443 !encoder->private_->disable_constant_subframes &&
3444 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3445 fixed_residual_bits_per_sample[1] == 0.0
3447 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3450 /* the above means it's possible all samples are the same value; now double-check it: */
3452 signal_is_constant = true;
3453 for(i = 1; i < frame_header->blocksize; i++) {
3454 if(integer_signal[0] != integer_signal[i]) {
3455 signal_is_constant = false;
3460 if(signal_is_constant) {
3461 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
3462 if(_candidate_bits < _best_bits) {
3463 _best_subframe = !_best_subframe;
3464 _best_bits = _candidate_bits;
3468 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3470 if(encoder->protected_->do_exhaustive_model_search) {
3471 min_fixed_order = 0;
3472 max_fixed_order = FLAC__MAX_FIXED_ORDER;
3475 min_fixed_order = max_fixed_order = guess_fixed_order;
3477 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
3478 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3479 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
3480 continue; /* don't even try */
3481 rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+0.5) : 0; /* 0.5 is for rounding */
3483 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3484 continue; /* don't even try */
3485 rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > FLAC__FP_ZERO)? (unsigned)FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]+FLAC__FP_ONE_HALF) : 0; /* 0.5 is for rounding */
3487 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3488 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3489 #ifdef DEBUG_VERBOSE
3490 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3492 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3495 evaluate_fixed_subframe_(
3498 residual[!_best_subframe],
3499 encoder->private_->abs_residual,
3500 encoder->private_->abs_residual_partition_sums,
3501 encoder->private_->raw_bits_per_partition,
3502 frame_header->blocksize,
3506 min_partition_order,
3507 max_partition_order,
3508 precompute_partition_sums,
3509 encoder->protected_->do_escape_coding,
3510 encoder->protected_->rice_parameter_search_dist,
3511 subframe[!_best_subframe],
3512 partitioned_rice_contents[!_best_subframe]
3514 if(_candidate_bits < _best_bits) {
3515 _best_subframe = !_best_subframe;
3516 _best_bits = _candidate_bits;
3521 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3523 if(encoder->protected_->max_lpc_order > 0) {
3524 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
3525 max_lpc_order = frame_header->blocksize-1;
3527 max_lpc_order = encoder->protected_->max_lpc_order;
3528 if(max_lpc_order > 0) {
3530 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
3531 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
3532 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3533 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3534 if(autoc[0] != 0.0) {
3535 FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3536 if(encoder->protected_->do_exhaustive_model_search) {
3540 const unsigned guess_lpc_order =
3541 FLAC__lpc_compute_best_order(
3544 frame_header->blocksize,
3546 encoder->protected_->do_qlp_coeff_prec_search?
3547 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3548 encoder->protected_->qlp_coeff_precision
3551 min_lpc_order = max_lpc_order = guess_lpc_order;
3553 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3554 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3555 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3556 continue; /* don't even try */
3557 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3558 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3559 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3560 #ifdef DEBUG_VERBOSE
3561 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3563 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3565 if(encoder->protected_->do_qlp_coeff_prec_search) {
3566 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
3567 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3568 if(subframe_bps <= 17) {
3569 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
3570 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3573 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3576 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3578 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3580 evaluate_lpc_subframe_(
3583 residual[!_best_subframe],
3584 encoder->private_->abs_residual,
3585 encoder->private_->abs_residual_partition_sums,
3586 encoder->private_->raw_bits_per_partition,
3587 encoder->private_->lp_coeff[lpc_order-1],
3588 frame_header->blocksize,
3591 qlp_coeff_precision,
3593 min_partition_order,
3594 max_partition_order,
3595 precompute_partition_sums,
3596 encoder->protected_->do_escape_coding,
3597 encoder->protected_->rice_parameter_search_dist,
3598 subframe[!_best_subframe],
3599 partitioned_rice_contents[!_best_subframe]
3601 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3602 if(_candidate_bits < _best_bits) {
3603 _best_subframe = !_best_subframe;
3604 _best_bits = _candidate_bits;
3613 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
3617 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3618 if(_best_bits == UINT_MAX) {
3619 FLAC__ASSERT(_best_subframe == 0);
3620 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3623 *best_subframe = _best_subframe;
3624 *best_bits = _best_bits;
3629 FLAC__bool add_subframe_(
3630 FLAC__StreamEncoder *encoder,
3631 const FLAC__FrameHeader *frame_header,
3632 unsigned subframe_bps,
3633 const FLAC__Subframe *subframe,
3634 FLAC__BitBuffer *frame
3637 switch(subframe->type) {
3638 case FLAC__SUBFRAME_TYPE_CONSTANT:
3639 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
3640 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3644 case FLAC__SUBFRAME_TYPE_FIXED:
3645 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
3646 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3650 case FLAC__SUBFRAME_TYPE_LPC:
3651 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
3652 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3656 case FLAC__SUBFRAME_TYPE_VERBATIM:
3657 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
3658 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3669 unsigned evaluate_constant_subframe_(
3670 const FLAC__int32 signal,
3671 unsigned subframe_bps,
3672 FLAC__Subframe *subframe
3675 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3676 subframe->data.constant.value = signal;
3678 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
3681 unsigned evaluate_fixed_subframe_(
3682 FLAC__StreamEncoder *encoder,
3683 const FLAC__int32 signal[],
3684 FLAC__int32 residual[],
3685 FLAC__uint32 abs_residual[],
3686 FLAC__uint64 abs_residual_partition_sums[],
3687 unsigned raw_bits_per_partition[],
3689 unsigned subframe_bps,
3691 unsigned rice_parameter,
3692 unsigned min_partition_order,
3693 unsigned max_partition_order,
3694 FLAC__bool precompute_partition_sums,
3695 FLAC__bool do_escape_coding,
3696 unsigned rice_parameter_search_dist,
3697 FLAC__Subframe *subframe,
3698 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3701 unsigned i, residual_bits;
3702 const unsigned residual_samples = blocksize - order;
3704 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3706 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3708 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3709 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3710 subframe->data.fixed.residual = residual;
3713 find_best_partition_order_(
3717 abs_residual_partition_sums,
3718 raw_bits_per_partition,
3722 min_partition_order,
3723 max_partition_order,
3724 precompute_partition_sums,
3726 rice_parameter_search_dist,
3727 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3730 subframe->data.fixed.order = order;
3731 for(i = 0; i < order; i++)
3732 subframe->data.fixed.warmup[i] = signal[i];
3734 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
3737 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3738 unsigned evaluate_lpc_subframe_(
3739 FLAC__StreamEncoder *encoder,
3740 const FLAC__int32 signal[],
3741 FLAC__int32 residual[],
3742 FLAC__uint32 abs_residual[],
3743 FLAC__uint64 abs_residual_partition_sums[],
3744 unsigned raw_bits_per_partition[],
3745 const FLAC__real lp_coeff[],
3747 unsigned subframe_bps,
3749 unsigned qlp_coeff_precision,
3750 unsigned rice_parameter,
3751 unsigned min_partition_order,
3752 unsigned max_partition_order,
3753 FLAC__bool precompute_partition_sums,
3754 FLAC__bool do_escape_coding,
3755 unsigned rice_parameter_search_dist,
3756 FLAC__Subframe *subframe,
3757 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3760 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
3761 unsigned i, residual_bits;
3762 int quantization, ret;
3763 const unsigned residual_samples = blocksize - order;
3765 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3766 if(subframe_bps <= 16) {
3767 FLAC__ASSERT(order > 0);
3768 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3769 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3772 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
3774 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3776 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3777 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3778 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3780 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3782 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3784 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3786 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3787 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3788 subframe->data.lpc.residual = residual;
3791 find_best_partition_order_(
3795 abs_residual_partition_sums,
3796 raw_bits_per_partition,
3800 min_partition_order,
3801 max_partition_order,
3802 precompute_partition_sums,
3804 rice_parameter_search_dist,
3805 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3808 subframe->data.lpc.order = order;
3809 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3810 subframe->data.lpc.quantization_level = quantization;
3811 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
3812 for(i = 0; i < order; i++)
3813 subframe->data.lpc.warmup[i] = signal[i];
3815 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
3819 unsigned evaluate_verbatim_subframe_(
3820 const FLAC__int32 signal[],
3822 unsigned subframe_bps,
3823 FLAC__Subframe *subframe
3826 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3828 subframe->data.verbatim.data = signal;
3830 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
3833 unsigned find_best_partition_order_(
3834 FLAC__StreamEncoderPrivate *private_,
3835 const FLAC__int32 residual[],
3836 FLAC__uint32 abs_residual[],
3837 FLAC__uint64 abs_residual_partition_sums[],
3838 unsigned raw_bits_per_partition[],
3839 unsigned residual_samples,
3840 unsigned predictor_order,
3841 unsigned rice_parameter,
3842 unsigned min_partition_order,
3843 unsigned max_partition_order,
3844 FLAC__bool precompute_partition_sums,
3845 FLAC__bool do_escape_coding,
3846 unsigned rice_parameter_search_dist,
3847 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3851 unsigned residual_bits, best_residual_bits = 0;
3852 unsigned residual_sample;
3853 unsigned best_parameters_index = 0;
3854 const unsigned blocksize = residual_samples + predictor_order;
3856 /* compute abs(residual) for use later */
3857 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3858 r = residual[residual_sample];
3859 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
3862 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
3863 min_partition_order = min(min_partition_order, max_partition_order);
3865 if(precompute_partition_sums) {
3866 int partition_order;
3869 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
3871 if(do_escape_coding)
3872 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
3874 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3875 #ifdef DONT_ESTIMATE_RICE_BITS
3877 set_partitioned_rice_with_precompute_(
3879 abs_residual_partition_sums+sum,
3880 raw_bits_per_partition+sum,
3884 rice_parameter_search_dist,
3885 (unsigned)partition_order,
3887 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3893 set_partitioned_rice_with_precompute_(
3895 abs_residual_partition_sums+sum,
3896 raw_bits_per_partition+sum,
3900 rice_parameter_search_dist,
3901 (unsigned)partition_order,
3903 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3909 FLAC__ASSERT(best_residual_bits != 0);
3912 sum += 1u << partition_order;
3913 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3914 best_residual_bits = residual_bits;
3915 best_parameters_index = !best_parameters_index;
3916 best_partitioned_rice->order = partition_order;
3921 unsigned partition_order;
3922 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3923 #ifdef DONT_ESTIMATE_RICE_BITS
3925 set_partitioned_rice_(
3931 rice_parameter_search_dist,
3933 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3939 set_partitioned_rice_(
3944 rice_parameter_search_dist,
3946 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3952 FLAC__ASSERT(best_residual_bits != 0);
3955 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3956 best_residual_bits = residual_bits;
3957 best_parameters_index = !best_parameters_index;
3958 best_partitioned_rice->order = partition_order;
3964 * We are allowed to de-const the pointer based on our special knowledge;
3965 * it is const to the outside world.
3968 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3969 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3970 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3971 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3974 return best_residual_bits;
3977 void precompute_partition_info_sums_(
3978 const FLAC__uint32 abs_residual[],
3979 FLAC__uint64 abs_residual_partition_sums[],
3980 unsigned residual_samples,
3981 unsigned predictor_order,
3982 unsigned min_partition_order,
3983 unsigned max_partition_order
3986 int partition_order;
3987 unsigned from_partition, to_partition = 0;
3988 const unsigned blocksize = residual_samples + predictor_order;
3990 /* first do max_partition_order */
3991 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3992 FLAC__uint64 abs_residual_partition_sum;
3994 unsigned partition, partition_sample, partition_samples, residual_sample;
3995 const unsigned partitions = 1u << partition_order;
3996 const unsigned default_partition_samples = blocksize >> partition_order;
3998 FLAC__ASSERT(default_partition_samples > predictor_order);
4000 for(partition = residual_sample = 0; partition < partitions; partition++) {
4001 partition_samples = default_partition_samples;
4003 partition_samples -= predictor_order;
4004 abs_residual_partition_sum = 0;
4005 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
4006 abs_r = abs_residual[residual_sample];
4007 abs_residual_partition_sum += abs_r;
4010 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
4012 to_partition = partitions;
4016 /* now merge partitions for lower orders */
4017 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
4020 const unsigned partitions = 1u << partition_order;
4021 for(i = 0; i < partitions; i++) {
4022 s = abs_residual_partition_sums[from_partition];
4024 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
4031 void precompute_partition_info_escapes_(
4032 const FLAC__int32 residual[],
4033 unsigned raw_bits_per_partition[],
4034 unsigned residual_samples,
4035 unsigned predictor_order,
4036 unsigned min_partition_order,
4037 unsigned max_partition_order
4040 int partition_order;
4041 unsigned from_partition, to_partition = 0;
4042 const unsigned blocksize = residual_samples + predictor_order;
4044 /* first do max_partition_order */
4045 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
4046 FLAC__int32 r, residual_partition_min, residual_partition_max;
4047 unsigned silog2_min, silog2_max;
4048 unsigned partition, partition_sample, partition_samples, residual_sample;
4049 const unsigned partitions = 1u << partition_order;
4050 const unsigned default_partition_samples = blocksize >> partition_order;
4052 FLAC__ASSERT(default_partition_samples > predictor_order);
4054 for(partition = residual_sample = 0; partition < partitions; partition++) {
4055 partition_samples = default_partition_samples;
4057 partition_samples -= predictor_order;
4058 residual_partition_min = residual_partition_max = 0;
4059 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
4060 r = residual[residual_sample];
4061 if(r < residual_partition_min)
4062 residual_partition_min = r;
4063 else if(r > residual_partition_max)
4064 residual_partition_max = r;
4067 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
4068 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
4069 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
4071 to_partition = partitions;
4072 break; /*@@@ yuck, should remove the 'for' loop instead */
4075 /* now merge partitions for lower orders */
4076 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
4079 const unsigned partitions = 1u << partition_order;
4080 for(i = 0; i < partitions; i++) {
4081 m = raw_bits_per_partition[from_partition];
4083 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
4090 #ifdef VARIABLE_RICE_BITS
4091 #undef VARIABLE_RICE_BITS
4093 #ifndef DONT_ESTIMATE_RICE_BITS
4094 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
4097 #ifdef DONT_ESTIMATE_RICE_BITS
4098 FLAC__bool set_partitioned_rice_(
4099 const FLAC__uint32 abs_residual[],
4100 const FLAC__int32 residual[],
4101 const unsigned residual_samples,
4102 const unsigned predictor_order,
4103 const unsigned suggested_rice_parameter,
4104 const unsigned rice_parameter_search_dist,
4105 const unsigned partition_order,
4106 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4110 FLAC__bool set_partitioned_rice_(
4111 const FLAC__uint32 abs_residual[],
4112 const unsigned residual_samples,
4113 const unsigned predictor_order,
4114 const unsigned suggested_rice_parameter,
4115 const unsigned rice_parameter_search_dist,
4116 const unsigned partition_order,
4117 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4122 unsigned rice_parameter, partition_bits;
4123 #ifndef NO_RICE_SEARCH
4124 unsigned best_partition_bits;
4125 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4127 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
4128 unsigned *parameters;
4130 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4132 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4133 parameters = partitioned_rice_contents->parameters;
4135 if(partition_order == 0) {
4138 #ifndef NO_RICE_SEARCH
4139 if(rice_parameter_search_dist) {
4140 if(suggested_rice_parameter < rice_parameter_search_dist)
4141 min_rice_parameter = 0;
4143 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4144 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4145 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4146 #ifdef DEBUG_VERBOSE
4147 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4149 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4153 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
4155 best_partition_bits = 0xffffffff;
4156 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4158 #ifdef VARIABLE_RICE_BITS
4159 const unsigned rice_parameter_estimate = rice_parameter-1;
4160 partition_bits = (1+rice_parameter) * residual_samples;
4164 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4165 for(i = 0; i < residual_samples; i++) {
4166 #ifdef VARIABLE_RICE_BITS
4167 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
4169 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4172 #ifndef NO_RICE_SEARCH
4173 if(partition_bits < best_partition_bits) {
4174 best_rice_parameter = rice_parameter;
4175 best_partition_bits = partition_bits;
4179 parameters[0] = best_rice_parameter;
4180 bits_ += best_partition_bits;
4183 unsigned partition, residual_sample, save_residual_sample, partition_sample;
4184 unsigned partition_samples;
4185 FLAC__uint64 mean, k;
4186 const unsigned partitions = 1u << partition_order;
4187 for(partition = residual_sample = 0; partition < partitions; partition++) {
4188 partition_samples = (residual_samples+predictor_order) >> partition_order;
4189 if(partition == 0) {
4190 if(partition_samples <= predictor_order)
4193 partition_samples -= predictor_order;
4196 save_residual_sample = residual_sample;
4197 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
4198 mean += abs_residual[residual_sample];
4199 residual_sample = save_residual_sample;
4200 /* we are basically calculating the size in bits of the
4201 * average residual magnitude in the partition:
4202 * rice_parameter = floor(log2(mean/partition_samples))
4203 * 'mean' is not a good name for the variable, it is
4204 * actually the sum of magnitudes of all residual values
4205 * in the partition, so the actual mean is
4206 * mean/partition_samples
4208 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
4210 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4211 #ifdef DEBUG_VERBOSE
4212 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4214 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4217 #ifndef NO_RICE_SEARCH
4218 if(rice_parameter_search_dist) {
4219 if(rice_parameter < rice_parameter_search_dist)
4220 min_rice_parameter = 0;
4222 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4223 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4224 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4225 #ifdef DEBUG_VERBOSE
4226 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4228 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4232 min_rice_parameter = max_rice_parameter = rice_parameter;
4234 best_partition_bits = 0xffffffff;
4235 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4237 #ifdef VARIABLE_RICE_BITS
4238 const unsigned rice_parameter_estimate = rice_parameter-1;
4239 partition_bits = (1+rice_parameter) * partition_samples;
4243 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4244 save_residual_sample = residual_sample;
4245 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
4246 #ifdef VARIABLE_RICE_BITS
4247 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
4249 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4252 #ifndef NO_RICE_SEARCH
4253 if(rice_parameter != max_rice_parameter)
4254 residual_sample = save_residual_sample;
4255 if(partition_bits < best_partition_bits) {
4256 best_rice_parameter = rice_parameter;
4257 best_partition_bits = partition_bits;
4261 parameters[partition] = best_rice_parameter;
4262 bits_ += best_partition_bits;
4270 #ifdef DONT_ESTIMATE_RICE_BITS
4271 FLAC__bool set_partitioned_rice_with_precompute_(
4272 const FLAC__int32 residual[],
4273 const FLAC__uint64 abs_residual_partition_sums[],
4274 const unsigned raw_bits_per_partition[],
4275 const unsigned residual_samples,
4276 const unsigned predictor_order,
4277 const unsigned suggested_rice_parameter,
4278 const unsigned rice_parameter_search_dist,
4279 const unsigned partition_order,
4280 const FLAC__bool search_for_escapes,
4281 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4285 FLAC__bool set_partitioned_rice_with_precompute_(
4286 const FLAC__uint32 abs_residual[],
4287 const FLAC__uint64 abs_residual_partition_sums[],
4288 const unsigned raw_bits_per_partition[],
4289 const unsigned residual_samples,
4290 const unsigned predictor_order,
4291 const unsigned suggested_rice_parameter,
4292 const unsigned rice_parameter_search_dist,
4293 const unsigned partition_order,
4294 const FLAC__bool search_for_escapes,
4295 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4300 unsigned rice_parameter, partition_bits;
4301 #ifndef NO_RICE_SEARCH
4302 unsigned best_partition_bits;
4303 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4306 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
4307 unsigned *parameters, *raw_bits;
4309 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4311 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4312 parameters = partitioned_rice_contents->parameters;
4313 raw_bits = partitioned_rice_contents->raw_bits;
4315 if(partition_order == 0) {
4318 #ifndef NO_RICE_SEARCH
4319 if(rice_parameter_search_dist) {
4320 if(suggested_rice_parameter < rice_parameter_search_dist)
4321 min_rice_parameter = 0;
4323 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4324 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4325 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4326 #ifdef DEBUG_VERBOSE
4327 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4329 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4333 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
4335 best_partition_bits = 0xffffffff;
4336 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4338 #ifdef VARIABLE_RICE_BITS
4339 const unsigned rice_parameter_estimate = rice_parameter-1;
4340 partition_bits = (1+rice_parameter) * residual_samples;
4344 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4345 for(i = 0; i < residual_samples; i++) {
4346 #ifdef VARIABLE_RICE_BITS
4347 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
4349 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
4352 #ifndef NO_RICE_SEARCH
4353 if(partition_bits < best_partition_bits) {
4354 best_rice_parameter = rice_parameter;
4355 best_partition_bits = partition_bits;
4359 if(search_for_escapes) {
4360 flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
4361 if(flat_bits <= best_partition_bits) {
4362 raw_bits[0] = raw_bits_per_partition[0];
4363 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4364 best_partition_bits = flat_bits;
4367 parameters[0] = best_rice_parameter;
4368 bits_ += best_partition_bits;
4371 unsigned partition, residual_sample, save_residual_sample, partition_sample;
4372 unsigned partition_samples;
4373 FLAC__uint64 mean, k;
4374 const unsigned partitions = 1u << partition_order;
4375 for(partition = residual_sample = 0; partition < partitions; partition++) {
4376 partition_samples = (residual_samples+predictor_order) >> partition_order;
4377 if(partition == 0) {
4378 if(partition_samples <= predictor_order)
4381 partition_samples -= predictor_order;
4383 mean = abs_residual_partition_sums[partition];
4384 /* we are basically calculating the size in bits of the
4385 * average residual magnitude in the partition:
4386 * rice_parameter = floor(log2(mean/partition_samples))
4387 * 'mean' is not a good name for the variable, it is
4388 * actually the sum of magnitudes of all residual values
4389 * in the partition, so the actual mean is
4390 * mean/partition_samples
4392 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
4394 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4395 #ifdef DEBUG_VERBOSE
4396 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4398 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4401 #ifndef NO_RICE_SEARCH
4402 if(rice_parameter_search_dist) {
4403 if(rice_parameter < rice_parameter_search_dist)
4404 min_rice_parameter = 0;
4406 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4407 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4408 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4409 #ifdef DEBUG_VERBOSE
4410 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4412 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4416 min_rice_parameter = max_rice_parameter = rice_parameter;
4418 best_partition_bits = 0xffffffff;
4419 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4421 #ifdef VARIABLE_RICE_BITS
4422 const unsigned rice_parameter_estimate = rice_parameter-1;
4423 partition_bits = (1+rice_parameter) * partition_samples;
4427 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4428 save_residual_sample = residual_sample;
4429 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
4430 #ifdef VARIABLE_RICE_BITS
4431 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
4433 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
4436 #ifndef NO_RICE_SEARCH
4437 if(rice_parameter != max_rice_parameter)
4438 residual_sample = save_residual_sample;
4439 if(partition_bits < best_partition_bits) {
4440 best_rice_parameter = rice_parameter;
4441 best_partition_bits = partition_bits;
4445 if(search_for_escapes) {
4446 flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
4447 if(flat_bits <= best_partition_bits) {
4448 raw_bits[partition] = raw_bits_per_partition[partition];
4449 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4450 best_partition_bits = flat_bits;
4453 parameters[partition] = best_rice_parameter;
4454 bits_ += best_partition_bits;
4462 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
4467 for(i = 0; i < samples && !(x&1); i++)
4474 for(shift = 0; !(x&1); shift++)
4479 for(i = 0; i < samples; i++)
4480 signal[i] >>= shift;
4486 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4490 for(channel = 0; channel < channels; channel++)
4491 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4493 fifo->tail += wide_samples;
4495 FLAC__ASSERT(fifo->tail <= fifo->size);
4498 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4501 unsigned sample, wide_sample;
4502 unsigned tail = fifo->tail;
4504 sample = input_offset * channels;
4505 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4506 for(channel = 0; channel < channels; channel++)
4507 fifo->data[channel][tail] = input[sample++];
4512 FLAC__ASSERT(fifo->tail <= fifo->size);
4515 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4517 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4518 const size_t encoded_bytes = encoder->private_->verify.output.bytes;
4521 if(encoder->private_->verify.needs_magic_hack) {
4522 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4523 *bytes = FLAC__STREAM_SYNC_LENGTH;
4524 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4525 encoder->private_->verify.needs_magic_hack = false;
4528 if(encoded_bytes == 0) {
4530 * If we get here, a FIFO underflow has occurred,
4531 * which means there is a bug somewhere.
4534 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4536 else if(encoded_bytes < *bytes)
4537 *bytes = encoded_bytes;
4538 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4539 encoder->private_->verify.output.data += *bytes;
4540 encoder->private_->verify.output.bytes -= *bytes;
4543 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4546 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4548 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4550 const unsigned channels = frame->header.channels;
4551 const unsigned blocksize = frame->header.blocksize;
4552 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4556 for(channel = 0; channel < channels; channel++) {
4557 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4558 unsigned i, sample = 0;
4559 FLAC__int32 expect = 0, got = 0;
4561 for(i = 0; i < blocksize; i++) {
4562 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4564 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4565 got = (FLAC__int32)buffer[channel][i];
4569 FLAC__ASSERT(i < blocksize);
4570 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4571 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
4572 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
4573 encoder->private_->verify.error_stats.channel = channel;
4574 encoder->private_->verify.error_stats.sample = sample;
4575 encoder->private_->verify.error_stats.expected = expect;
4576 encoder->private_->verify.error_stats.got = got;
4577 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4578 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4581 /* dequeue the frame from the fifo */
4582 encoder->private_->verify.input_fifo.tail -= blocksize;
4583 FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
4584 for(channel = 0; channel < channels; channel++)
4585 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail * sizeof(encoder->private_->verify.input_fifo.data[0][0]));
4586 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4589 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4591 (void)decoder, (void)metadata, (void)client_data;
4594 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4596 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4597 (void)decoder, (void)status;
4598 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4601 FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4605 *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
4607 if (feof(encoder->private_->file))
4608 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4609 else if (ferror(encoder->private_->file))
4610 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4612 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4615 FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4619 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4620 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4622 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4625 FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4631 offset = ftello(encoder->private_->file);
4634 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4637 *absolute_byte_offset = (FLAC__uint64)offset;
4638 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4642 #ifdef FLAC__VALGRIND_TESTING
4643 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4645 size_t ret = fwrite(ptr, size, nmemb, stream);
4651 #define local__fwrite fwrite
4654 FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
4656 (void)client_data, (void)current_frame;
4658 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
4659 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4661 /* We would like to be able to use 'samples > 0' in the
4662 * clause here but currently because of the nature of our
4663 * Ogg writing implementation, 'samples' is always 0 (see
4664 * ogg_encoder_aspect.c). The downside is extra progress
4667 encoder->private_->is_ogg? true :
4672 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4673 * because at this point in the callback chain, the stats
4674 * have not been updated. Only after we return and control
4675 * gets back to write_frame_() are the stats updated
4677 encoder->private_->progress_callback(encoder, encoder->private_->bytes_written+bytes, encoder->private_->samples_written+samples, encoder->private_->frames_written+(samples?1:0), encoder->private_->total_frames_estimate, encoder->private_->client_data);
4679 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4682 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4686 * This will forcibly set stdout to binary mode (for OSes that require it)
4688 FILE *get_binary_stdout_()
4690 /* if something breaks here it is probably due to the presence or
4691 * absence of an underscore before the identifiers 'setmode',
4692 * 'fileno', and/or 'O_BINARY'; check your system header files.
4694 #if defined _MSC_VER || defined __MINGW32__
4695 _setmode(_fileno(stdout), _O_BINARY);
4696 #elif defined __CYGWIN__
4697 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4698 setmode(_fileno(stdout), _O_BINARY);
4699 #elif defined __EMX__
4700 setmode(fileno(stdout), O_BINARY);