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 __MINGW32__
50 #if _MSC_VER <= 1200 /* @@@ [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"
70 #include "private/stream_encoder_framing.h"
71 #include "private/window.h"
76 #define min(x,y) ((x)<(y)?(x):(y))
81 #define max(x,y) ((x)>(y)?(x):(y))
84 FLAC__int32 *data[FLAC__MAX_CHANNELS];
85 unsigned size; /* of each data[] in samples */
90 const FLAC__byte *data;
97 ENCODER_IN_METADATA = 1,
101 static struct CompressionLevels {
102 FLAC__bool do_mid_side_stereo;
103 FLAC__bool loose_mid_side_stereo;
104 const char *apodization;
105 unsigned max_lpc_order;
106 unsigned qlp_coeff_precision;
107 FLAC__bool do_qlp_coeff_prec_search;
108 FLAC__bool do_escape_coding;
109 FLAC__bool do_exhaustive_model_search;
110 unsigned min_residual_partition_order;
111 unsigned max_residual_partition_order;
112 unsigned rice_parameter_search_dist;
113 } compression_levels_[] = {
114 { false, false, "tukey(0.5)", 0, 0, false, false, false, 2, 2, 0 },
115 { true , true , "tukey(0.5)", 0, 0, false, false, false, 2, 2, 0 },
116 { true , false, "tukey(0.5)", 0, 0, false, false, false, 0, 3, 0 },
117 { false, false, "tukey(0.5)", 6, 0, false, false, false, 3, 3, 0 },
118 { true , true , "tukey(0.5)", 8, 0, false, false, false, 3, 3, 0 },
119 { true , false, "tukey(0.5)", 8, 0, false, false, false, 3, 3, 0 },
120 { true , false, "tukey(0.5)", 8, 0, false, false, false, 0, 4, 0 },
121 { true , false, "tukey(0.5)", 8, 0, false, false, true , 0, 6, 0 },
122 { true , false, "tukey(0.5)", 12, 0, false, false, true , 0, 6, 0 }
126 /***********************************************************************
128 * Private class method prototypes
130 ***********************************************************************/
132 static void set_defaults_(FLAC__StreamEncoder *encoder);
133 static void free_(FLAC__StreamEncoder *encoder);
134 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize);
135 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block);
136 static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block);
137 static void update_metadata_(const FLAC__StreamEncoder *encoder);
139 static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
141 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block);
142 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);
144 static FLAC__bool process_subframe_(
145 FLAC__StreamEncoder *encoder,
146 unsigned min_partition_order,
147 unsigned max_partition_order,
148 FLAC__bool precompute_partition_sums,
149 const FLAC__FrameHeader *frame_header,
150 unsigned subframe_bps,
151 const FLAC__int32 integer_signal[],
152 #ifndef FLAC__INTEGER_ONLY_LIBRARY
153 const FLAC__real real_signal[],
155 FLAC__Subframe *subframe[2],
156 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
157 FLAC__int32 *residual[2],
158 unsigned *best_subframe,
162 static FLAC__bool add_subframe_(
163 FLAC__StreamEncoder *encoder,
164 const FLAC__FrameHeader *frame_header,
165 unsigned subframe_bps,
166 const FLAC__Subframe *subframe,
167 FLAC__BitBuffer *frame
170 static unsigned evaluate_constant_subframe_(
171 const FLAC__int32 signal,
172 unsigned subframe_bps,
173 FLAC__Subframe *subframe
176 static unsigned evaluate_fixed_subframe_(
177 FLAC__StreamEncoder *encoder,
178 const FLAC__int32 signal[],
179 FLAC__int32 residual[],
180 FLAC__uint32 abs_residual[],
181 FLAC__uint64 abs_residual_partition_sums[],
182 unsigned raw_bits_per_partition[],
184 unsigned subframe_bps,
186 unsigned rice_parameter,
187 unsigned min_partition_order,
188 unsigned max_partition_order,
189 FLAC__bool precompute_partition_sums,
190 FLAC__bool do_escape_coding,
191 unsigned rice_parameter_search_dist,
192 FLAC__Subframe *subframe,
193 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
196 #ifndef FLAC__INTEGER_ONLY_LIBRARY
197 static unsigned evaluate_lpc_subframe_(
198 FLAC__StreamEncoder *encoder,
199 const FLAC__int32 signal[],
200 FLAC__int32 residual[],
201 FLAC__uint32 abs_residual[],
202 FLAC__uint64 abs_residual_partition_sums[],
203 unsigned raw_bits_per_partition[],
204 const FLAC__real lp_coeff[],
206 unsigned subframe_bps,
208 unsigned qlp_coeff_precision,
209 unsigned rice_parameter,
210 unsigned min_partition_order,
211 unsigned max_partition_order,
212 FLAC__bool precompute_partition_sums,
213 FLAC__bool do_escape_coding,
214 unsigned rice_parameter_search_dist,
215 FLAC__Subframe *subframe,
216 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
220 static unsigned evaluate_verbatim_subframe_(
221 const FLAC__int32 signal[],
223 unsigned subframe_bps,
224 FLAC__Subframe *subframe
227 static unsigned find_best_partition_order_(
228 struct FLAC__StreamEncoderPrivate *private_,
229 const FLAC__int32 residual[],
230 FLAC__uint32 abs_residual[],
231 FLAC__uint64 abs_residual_partition_sums[],
232 unsigned raw_bits_per_partition[],
233 unsigned residual_samples,
234 unsigned predictor_order,
235 unsigned rice_parameter,
236 unsigned min_partition_order,
237 unsigned max_partition_order,
238 FLAC__bool precompute_partition_sums,
239 FLAC__bool do_escape_coding,
240 unsigned rice_parameter_search_dist,
241 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
244 static void precompute_partition_info_sums_(
245 const FLAC__uint32 abs_residual[],
246 FLAC__uint64 abs_residual_partition_sums[],
247 unsigned residual_samples,
248 unsigned predictor_order,
249 unsigned min_partition_order,
250 unsigned max_partition_order
253 static void precompute_partition_info_escapes_(
254 const FLAC__int32 residual[],
255 unsigned raw_bits_per_partition[],
256 unsigned residual_samples,
257 unsigned predictor_order,
258 unsigned min_partition_order,
259 unsigned max_partition_order
262 #ifdef DONT_ESTIMATE_RICE_BITS
263 static FLAC__bool set_partitioned_rice_(
264 const FLAC__uint32 abs_residual[],
265 const FLAC__int32 residual[],
266 const unsigned residual_samples,
267 const unsigned predictor_order,
268 const unsigned suggested_rice_parameter,
269 const unsigned rice_parameter_search_dist,
270 const unsigned partition_order,
271 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
275 static FLAC__bool set_partitioned_rice_with_precompute_(
276 const FLAC__int32 residual[],
277 const FLAC__uint64 abs_residual_partition_sums[],
278 const unsigned raw_bits_per_partition[],
279 const unsigned residual_samples,
280 const unsigned predictor_order,
281 const unsigned suggested_rice_parameter,
282 const unsigned rice_parameter_search_dist,
283 const unsigned partition_order,
284 const FLAC__bool search_for_escapes,
285 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
289 static FLAC__bool set_partitioned_rice_(
290 const FLAC__uint32 abs_residual[],
291 const unsigned residual_samples,
292 const unsigned predictor_order,
293 const unsigned suggested_rice_parameter,
294 const unsigned rice_parameter_search_dist,
295 const unsigned partition_order,
296 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
300 static FLAC__bool set_partitioned_rice_with_precompute_(
301 const FLAC__uint32 abs_residual[],
302 const FLAC__uint64 abs_residual_partition_sums[],
303 const unsigned raw_bits_per_partition[],
304 const unsigned residual_samples,
305 const unsigned predictor_order,
306 const unsigned suggested_rice_parameter,
307 const unsigned rice_parameter_search_dist,
308 const unsigned partition_order,
309 const FLAC__bool search_for_escapes,
310 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
315 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
317 /* verify-related routines: */
318 static void append_to_verify_fifo_(
319 verify_input_fifo *fifo,
320 const FLAC__int32 * const input[],
321 unsigned input_offset,
323 unsigned wide_samples
326 static void append_to_verify_fifo_interleaved_(
327 verify_input_fifo *fifo,
328 const FLAC__int32 input[],
329 unsigned input_offset,
331 unsigned wide_samples
334 static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
335 static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
336 static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
337 static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
339 static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
340 static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
341 static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
342 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);
343 static FILE *get_binary_stdout_();
346 /***********************************************************************
350 ***********************************************************************/
352 typedef struct FLAC__StreamEncoderPrivate {
353 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
354 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
355 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
356 #ifndef FLAC__INTEGER_ONLY_LIBRARY
357 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
358 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
359 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
360 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
362 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
363 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
364 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
365 FLAC__int32 *residual_workspace_mid_side[2][2];
366 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
367 FLAC__Subframe subframe_workspace_mid_side[2][2];
368 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
369 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
370 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
371 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
372 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
373 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
374 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
375 unsigned best_subframe_mid_side[2];
376 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
377 unsigned best_subframe_bits_mid_side[2];
378 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
379 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
380 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
381 FLAC__BitBuffer *frame; /* the current frame being worked on */
382 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
383 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
384 FLAC__ChannelAssignment last_channel_assignment;
385 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
386 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
387 unsigned current_sample_number;
388 unsigned current_frame_number;
389 struct FLAC__MD5Context md5context;
390 FLAC__CPUInfo cpuinfo;
391 #ifndef FLAC__INTEGER_ONLY_LIBRARY
392 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
394 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
396 #ifndef FLAC__INTEGER_ONLY_LIBRARY
397 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
398 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[]);
399 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[]);
400 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[]);
402 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
403 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
404 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
405 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
406 FLAC__bool disable_constant_subframes;
407 FLAC__bool disable_fixed_subframes;
408 FLAC__bool disable_verbatim_subframes;
412 FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
413 FLAC__StreamEncoderSeekCallback seek_callback;
414 FLAC__StreamEncoderTellCallback tell_callback;
415 FLAC__StreamEncoderWriteCallback write_callback;
416 FLAC__StreamEncoderMetadataCallback metadata_callback;
417 FLAC__StreamEncoderProgressCallback progress_callback;
419 unsigned first_seekpoint_to_check;
420 FILE *file; /* only used when encoding to a file */
421 FLAC__uint64 bytes_written;
422 FLAC__uint64 samples_written;
423 unsigned frames_written;
424 unsigned total_frames_estimate;
425 /* unaligned (original) pointers to allocated data */
426 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
427 FLAC__int32 *integer_signal_mid_side_unaligned[2];
428 #ifndef FLAC__INTEGER_ONLY_LIBRARY
429 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
430 FLAC__real *real_signal_mid_side_unaligned[2];
431 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
432 FLAC__real *windowed_signal_unaligned;
434 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
435 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
436 FLAC__uint32 *abs_residual_unaligned;
437 FLAC__uint64 *abs_residual_partition_sums_unaligned;
438 unsigned *raw_bits_per_partition_unaligned;
440 * These fields have been moved here from private function local
441 * declarations merely to save stack space during encoding.
443 #ifndef FLAC__INTEGER_ONLY_LIBRARY
444 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
446 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
448 * The data for the verify section
451 FLAC__StreamDecoder *decoder;
452 EncoderStateHint state_hint;
453 FLAC__bool needs_magic_hack;
454 verify_input_fifo input_fifo;
455 verify_output output;
457 FLAC__uint64 absolute_sample;
458 unsigned frame_number;
461 FLAC__int32 expected;
465 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
466 } FLAC__StreamEncoderPrivate;
468 /***********************************************************************
470 * Public static class data
472 ***********************************************************************/
474 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
475 "FLAC__STREAM_ENCODER_OK",
476 "FLAC__STREAM_ENCODER_UNINITIALIZED",
477 "FLAC__STREAM_ENCODER_OGG_ERROR",
478 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
479 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
480 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
481 "FLAC__STREAM_ENCODER_IO_ERROR",
482 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
483 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
486 FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
487 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
488 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
489 "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
490 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
491 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
492 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
493 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
494 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
495 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
496 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
497 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
498 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
499 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
500 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
503 FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
504 "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
505 "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
506 "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
507 "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
510 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
511 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
512 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
515 FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
516 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
517 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
518 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
521 FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
522 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
523 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
524 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
527 /* Number of samples that will be overread to watch for end of stream. By
528 * 'overread', we mean that the FLAC__stream_encoder_process*() calls will
529 * always try to read blocksize+1 samples before encoding a block, so that
530 * even if the stream has a total sample count that is an integral multiple
531 * of the blocksize, we will still notice when we are encoding the last
532 * block. This is needed, for example, to correctly set the end-of-stream
533 * marker in Ogg FLAC.
535 * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
536 * not really any reason to change it.
538 static const unsigned OVERREAD_ = 1;
540 /***********************************************************************
542 * Class constructor/destructor
545 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
547 FLAC__StreamEncoder *encoder;
550 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
552 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
557 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
558 if(encoder->protected_ == 0) {
563 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
564 if(encoder->private_ == 0) {
565 free(encoder->protected_);
570 encoder->private_->frame = FLAC__bitbuffer_new();
571 if(encoder->private_->frame == 0) {
572 free(encoder->private_);
573 free(encoder->protected_);
578 encoder->private_->file = 0;
580 set_defaults_(encoder);
582 encoder->private_->is_being_deleted = false;
584 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
585 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
586 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
588 for(i = 0; i < 2; i++) {
589 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
590 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
592 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
593 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
594 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
596 for(i = 0; i < 2; i++) {
597 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
598 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
601 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
602 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
603 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
605 for(i = 0; i < 2; i++) {
606 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
607 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
609 for(i = 0; i < 2; i++)
610 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
612 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
617 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
621 FLAC__ASSERT(0 != encoder);
622 FLAC__ASSERT(0 != encoder->protected_);
623 FLAC__ASSERT(0 != encoder->private_);
624 FLAC__ASSERT(0 != encoder->private_->frame);
626 encoder->private_->is_being_deleted = true;
628 (void)FLAC__stream_encoder_finish(encoder);
630 if(0 != encoder->private_->verify.decoder)
631 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
633 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
634 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
635 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
637 for(i = 0; i < 2; i++) {
638 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
639 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
641 for(i = 0; i < 2; i++)
642 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
644 FLAC__bitbuffer_delete(encoder->private_->frame);
645 free(encoder->private_);
646 free(encoder->protected_);
650 /***********************************************************************
652 * Public class methods
654 ***********************************************************************/
656 static FLAC__StreamEncoderInitStatus init_stream_internal_(
657 FLAC__StreamEncoder *encoder,
658 FLAC__StreamEncoderReadCallback read_callback,
659 FLAC__StreamEncoderWriteCallback write_callback,
660 FLAC__StreamEncoderSeekCallback seek_callback,
661 FLAC__StreamEncoderTellCallback tell_callback,
662 FLAC__StreamEncoderMetadataCallback metadata_callback,
668 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
670 FLAC__ASSERT(0 != encoder);
672 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
673 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
675 #ifndef FLAC__HAS_OGG
677 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
680 if(0 == write_callback || (seek_callback && 0 == tell_callback))
681 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
683 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
684 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
686 if(encoder->protected_->channels != 2) {
687 encoder->protected_->do_mid_side_stereo = false;
688 encoder->protected_->loose_mid_side_stereo = false;
690 else if(!encoder->protected_->do_mid_side_stereo)
691 encoder->protected_->loose_mid_side_stereo = false;
693 if(encoder->protected_->bits_per_sample >= 32)
694 encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */
696 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
697 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
699 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
700 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
702 if(encoder->protected_->blocksize == 0) {
703 if(encoder->protected_->max_lpc_order == 0)
704 encoder->protected_->blocksize = 1152;
706 encoder->protected_->blocksize = 4608;
709 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
710 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
712 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
713 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
715 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
716 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
718 if(encoder->protected_->qlp_coeff_precision == 0) {
719 if(encoder->protected_->bits_per_sample < 16) {
720 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
721 /* @@@ until then we'll make a guess */
722 encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
724 else if(encoder->protected_->bits_per_sample == 16) {
725 if(encoder->protected_->blocksize <= 192)
726 encoder->protected_->qlp_coeff_precision = 7;
727 else if(encoder->protected_->blocksize <= 384)
728 encoder->protected_->qlp_coeff_precision = 8;
729 else if(encoder->protected_->blocksize <= 576)
730 encoder->protected_->qlp_coeff_precision = 9;
731 else if(encoder->protected_->blocksize <= 1152)
732 encoder->protected_->qlp_coeff_precision = 10;
733 else if(encoder->protected_->blocksize <= 2304)
734 encoder->protected_->qlp_coeff_precision = 11;
735 else if(encoder->protected_->blocksize <= 4608)
736 encoder->protected_->qlp_coeff_precision = 12;
738 encoder->protected_->qlp_coeff_precision = 13;
741 if(encoder->protected_->blocksize <= 384)
742 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
743 else if(encoder->protected_->blocksize <= 1152)
744 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
746 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
748 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
750 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
751 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
753 if(encoder->protected_->streamable_subset) {
755 encoder->protected_->blocksize != 192 &&
756 encoder->protected_->blocksize != 576 &&
757 encoder->protected_->blocksize != 1152 &&
758 encoder->protected_->blocksize != 2304 &&
759 encoder->protected_->blocksize != 4608 &&
760 encoder->protected_->blocksize != 256 &&
761 encoder->protected_->blocksize != 512 &&
762 encoder->protected_->blocksize != 1024 &&
763 encoder->protected_->blocksize != 2048 &&
764 encoder->protected_->blocksize != 4096 &&
765 encoder->protected_->blocksize != 8192 &&
766 encoder->protected_->blocksize != 16384
768 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
770 encoder->protected_->sample_rate != 8000 &&
771 encoder->protected_->sample_rate != 16000 &&
772 encoder->protected_->sample_rate != 22050 &&
773 encoder->protected_->sample_rate != 24000 &&
774 encoder->protected_->sample_rate != 32000 &&
775 encoder->protected_->sample_rate != 44100 &&
776 encoder->protected_->sample_rate != 48000 &&
777 encoder->protected_->sample_rate != 96000
779 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
781 encoder->protected_->bits_per_sample != 8 &&
782 encoder->protected_->bits_per_sample != 12 &&
783 encoder->protected_->bits_per_sample != 16 &&
784 encoder->protected_->bits_per_sample != 20 &&
785 encoder->protected_->bits_per_sample != 24
787 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
788 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
789 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
791 encoder->protected_->sample_rate <= 48000 &&
793 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
794 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
797 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
801 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
802 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
803 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
804 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
807 /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
808 if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
810 for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
811 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
812 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
814 encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
815 encoder->protected_->metadata[0] = vc;
821 /* keep track of any SEEKTABLE block */
822 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
824 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
825 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
826 encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
827 break; /* take only the first one */
832 /* validate metadata */
833 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
834 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
835 metadata_has_seektable = false;
836 metadata_has_vorbis_comment = false;
837 metadata_picture_has_type1 = false;
838 metadata_picture_has_type2 = false;
839 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
840 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
841 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
842 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
843 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
844 if(metadata_has_seektable) /* only one is allowed */
845 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
846 metadata_has_seektable = true;
847 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
848 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
850 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
851 if(metadata_has_vorbis_comment) /* only one is allowed */
852 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
853 metadata_has_vorbis_comment = true;
855 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
856 if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
857 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
859 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
860 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
861 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
862 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
863 if(metadata_picture_has_type1) /* there should only be 1 per stream */
864 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
865 metadata_picture_has_type1 = true;
866 /* standard icon must be 32x32 pixel PNG */
868 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
870 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
871 m->data.picture.width != 32 ||
872 m->data.picture.height != 32
875 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
877 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
878 if(metadata_picture_has_type2) /* there should only be 1 per stream */
879 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
880 metadata_picture_has_type2 = true;
885 encoder->private_->input_capacity = 0;
886 for(i = 0; i < encoder->protected_->channels; i++) {
887 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
888 #ifndef FLAC__INTEGER_ONLY_LIBRARY
889 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
892 for(i = 0; i < 2; i++) {
893 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
894 #ifndef FLAC__INTEGER_ONLY_LIBRARY
895 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
898 #ifndef FLAC__INTEGER_ONLY_LIBRARY
899 for(i = 0; i < encoder->protected_->num_apodizations; i++)
900 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
901 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
903 for(i = 0; i < encoder->protected_->channels; i++) {
904 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
905 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
906 encoder->private_->best_subframe[i] = 0;
908 for(i = 0; i < 2; i++) {
909 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
910 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
911 encoder->private_->best_subframe_mid_side[i] = 0;
913 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
914 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
915 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
916 #ifndef FLAC__INTEGER_ONLY_LIBRARY
917 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
919 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
920 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply÷ by hand */
921 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
922 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
923 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
924 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
925 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);
927 if(encoder->private_->loose_mid_side_stereo_frames == 0)
928 encoder->private_->loose_mid_side_stereo_frames = 1;
929 encoder->private_->loose_mid_side_stereo_frame_count = 0;
930 encoder->private_->current_sample_number = 0;
931 encoder->private_->current_frame_number = 0;
933 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
934 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? */
935 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
938 * get the CPU info and set the function pointers
940 FLAC__cpu_info(&encoder->private_->cpuinfo);
941 /* first default to the non-asm routines */
942 #ifndef FLAC__INTEGER_ONLY_LIBRARY
943 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
945 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
946 #ifndef FLAC__INTEGER_ONLY_LIBRARY
947 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
948 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
949 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
951 /* now override with asm where appropriate */
952 #ifndef FLAC__INTEGER_ONLY_LIBRARY
953 # ifndef FLAC__NO_ASM
954 if(encoder->private_->cpuinfo.use_asm) {
955 # ifdef FLAC__CPU_IA32
956 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
957 # ifdef FLAC__HAS_NASM
959 if(encoder->private_->cpuinfo.data.ia32.sse) {
960 if(encoder->protected_->max_lpc_order < 4)
961 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
962 else if(encoder->protected_->max_lpc_order < 8)
963 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
964 else if(encoder->protected_->max_lpc_order < 12)
965 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
967 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
970 # endif /* FLAC__SSE_OS */
971 if(encoder->private_->cpuinfo.data.ia32._3dnow)
972 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
974 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
975 if(encoder->private_->cpuinfo.data.ia32.mmx) {
976 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
977 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
980 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
981 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
983 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
984 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
985 # endif /* FLAC__HAS_NASM */
986 # endif /* FLAC__CPU_IA32 */
988 # endif /* !FLAC__NO_ASM */
989 #endif /* !FLAC__INTEGER_ONLY_LIBRARY */
990 /* finally override based on wide-ness if necessary */
991 if(encoder->private_->use_wide_by_block) {
992 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
995 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
996 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
998 /* set state to OK; from here on, errors are fatal and we'll override the state then */
999 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
1002 encoder->private_->is_ogg = is_ogg;
1003 if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
1004 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
1005 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1009 encoder->private_->read_callback = read_callback;
1010 encoder->private_->write_callback = write_callback;
1011 encoder->private_->seek_callback = seek_callback;
1012 encoder->private_->tell_callback = tell_callback;
1013 encoder->private_->metadata_callback = metadata_callback;
1014 encoder->private_->client_data = client_data;
1016 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
1017 /* the above function sets the state for us in case of an error */
1018 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1021 if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
1022 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1023 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1027 * Set up the verify stuff if necessary
1029 if(encoder->protected_->verify) {
1031 * First, set up the fifo which will hold the
1032 * original signal to compare against
1034 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
1035 for(i = 0; i < encoder->protected_->channels; i++) {
1036 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
1037 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1038 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1041 encoder->private_->verify.input_fifo.tail = 0;
1044 * Now set up a stream decoder for verification
1046 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
1047 if(0 == encoder->private_->verify.decoder) {
1048 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1049 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1052 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) {
1053 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1054 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1057 encoder->private_->verify.error_stats.absolute_sample = 0;
1058 encoder->private_->verify.error_stats.frame_number = 0;
1059 encoder->private_->verify.error_stats.channel = 0;
1060 encoder->private_->verify.error_stats.sample = 0;
1061 encoder->private_->verify.error_stats.expected = 0;
1062 encoder->private_->verify.error_stats.got = 0;
1065 * These must be done before we write any metadata, because that
1066 * calls the write_callback, which uses these values.
1068 encoder->private_->first_seekpoint_to_check = 0;
1069 encoder->private_->samples_written = 0;
1070 encoder->protected_->streaminfo_offset = 0;
1071 encoder->protected_->seektable_offset = 0;
1072 encoder->protected_->audio_offset = 0;
1075 * write the stream header
1077 if(encoder->protected_->verify)
1078 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
1079 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
1080 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1081 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1083 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1084 /* the above function sets the state for us in case of an error */
1085 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1089 * write the STREAMINFO metadata block
1091 if(encoder->protected_->verify)
1092 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
1093 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1094 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1095 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1096 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1097 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1098 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1099 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1100 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1101 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1102 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1103 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1104 memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
1105 FLAC__MD5Init(&encoder->private_->md5context);
1106 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1107 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1108 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1110 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1111 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1112 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1114 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1115 /* the above function sets the state for us in case of an error */
1116 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1120 * Now that the STREAMINFO block is written, we can init this to an
1121 * absurdly-high value...
1123 encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
1124 /* ... and clear this to 0 */
1125 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
1128 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1129 * if not, we will write an empty one (FLAC__add_metadata_block()
1130 * automatically supplies the vendor string).
1132 * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1133 * the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1134 * true it will have already insured that the metadata list is properly
1137 if(!metadata_has_vorbis_comment) {
1138 FLAC__StreamMetadata vorbis_comment;
1139 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1140 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1141 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1142 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1143 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1144 vorbis_comment.data.vorbis_comment.num_comments = 0;
1145 vorbis_comment.data.vorbis_comment.comments = 0;
1146 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1147 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1148 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1150 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1151 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1152 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1154 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1155 /* the above function sets the state for us in case of an error */
1156 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1161 * write the user's metadata blocks
1163 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1164 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
1165 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1166 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1167 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1169 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1170 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1171 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1173 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1174 /* the above function sets the state for us in case of an error */
1175 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1179 /* now that all the metadata is written, we save the stream offset */
1180 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 */
1181 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1182 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1185 if(encoder->protected_->verify)
1186 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1188 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1191 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1192 FLAC__StreamEncoder *encoder,
1193 FLAC__StreamEncoderWriteCallback write_callback,
1194 FLAC__StreamEncoderSeekCallback seek_callback,
1195 FLAC__StreamEncoderTellCallback tell_callback,
1196 FLAC__StreamEncoderMetadataCallback metadata_callback,
1200 return init_stream_internal_(
1202 /*read_callback=*/0,
1212 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1213 FLAC__StreamEncoder *encoder,
1214 FLAC__StreamEncoderReadCallback read_callback,
1215 FLAC__StreamEncoderWriteCallback write_callback,
1216 FLAC__StreamEncoderSeekCallback seek_callback,
1217 FLAC__StreamEncoderTellCallback tell_callback,
1218 FLAC__StreamEncoderMetadataCallback metadata_callback,
1222 return init_stream_internal_(
1234 static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1235 FLAC__StreamEncoder *encoder,
1237 FLAC__StreamEncoderProgressCallback progress_callback,
1242 FLAC__StreamEncoderInitStatus init_status;
1244 FLAC__ASSERT(0 != encoder);
1245 FLAC__ASSERT(0 != file);
1247 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1248 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1250 /* double protection */
1252 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1253 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1257 * To make sure that our file does not go unclosed after an error, we
1258 * must assign the FILE pointer before any further error can occur in
1262 file = get_binary_stdout_(); /* just to be safe */
1264 encoder->private_->file = file;
1266 encoder->private_->progress_callback = progress_callback;
1267 encoder->private_->bytes_written = 0;
1268 encoder->private_->samples_written = 0;
1269 encoder->private_->frames_written = 0;
1271 init_status = init_stream_internal_(
1273 is_ogg? file_read_callback_ : 0,
1274 file_write_callback_,
1275 file_seek_callback_,
1276 file_tell_callback_,
1277 /*metadata_callback=*/0,
1281 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1282 /* the above function sets the state for us in case of an error */
1287 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1289 FLAC__ASSERT(blocksize != 0);
1290 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1296 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1297 FLAC__StreamEncoder *encoder,
1299 FLAC__StreamEncoderProgressCallback progress_callback,
1303 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1306 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1307 FLAC__StreamEncoder *encoder,
1309 FLAC__StreamEncoderProgressCallback progress_callback,
1313 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1316 static FLAC__StreamEncoderInitStatus init_file_internal_(
1317 FLAC__StreamEncoder *encoder,
1318 const char *filename,
1319 FLAC__StreamEncoderProgressCallback progress_callback,
1326 FLAC__ASSERT(0 != encoder);
1329 * To make sure that our file does not go unclosed after an error, we
1330 * have to do the same entrance checks here that are later performed
1331 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1333 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1334 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1336 file = filename? fopen(filename, "w+b") : stdout;
1339 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1340 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1343 return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1346 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1347 FLAC__StreamEncoder *encoder,
1348 const char *filename,
1349 FLAC__StreamEncoderProgressCallback progress_callback,
1353 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1356 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1357 FLAC__StreamEncoder *encoder,
1358 const char *filename,
1359 FLAC__StreamEncoderProgressCallback progress_callback,
1363 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
1366 FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
1368 FLAC__bool verify_mismatch = false;
1370 FLAC__ASSERT(0 != encoder);
1371 FLAC__ASSERT(0 != encoder->private_);
1372 FLAC__ASSERT(0 != encoder->protected_);
1374 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
1377 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
1378 if(encoder->private_->current_sample_number != 0) {
1379 const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
1380 encoder->protected_->blocksize = encoder->private_->current_sample_number;
1381 if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true) && encoder->protected_->state == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1382 verify_mismatch = true;
1386 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
1388 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
1389 if(encoder->private_->seek_callback) {
1391 if(encoder->private_->is_ogg)
1392 update_ogg_metadata_(encoder);
1395 update_metadata_(encoder);
1397 if(encoder->private_->metadata_callback)
1398 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1401 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder))
1402 verify_mismatch = true;
1404 if(0 != encoder->private_->file) {
1405 if(encoder->private_->file != stdout)
1406 fclose(encoder->private_->file);
1407 encoder->private_->file = 0;
1411 if(encoder->private_->is_ogg)
1412 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1416 set_defaults_(encoder);
1418 /* change the state to uninitialized unless there was a verify mismatch */
1420 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1422 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1424 return !verify_mismatch;
1427 FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
1429 FLAC__ASSERT(0 != encoder);
1430 FLAC__ASSERT(0 != encoder->private_);
1431 FLAC__ASSERT(0 != encoder->protected_);
1432 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1434 #ifdef FLAC__HAS_OGG
1435 /* can't check encoder->private_->is_ogg since that's not set until init time */
1436 FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1444 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
1446 FLAC__ASSERT(0 != encoder);
1447 FLAC__ASSERT(0 != encoder->private_);
1448 FLAC__ASSERT(0 != encoder->protected_);
1449 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1451 #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1452 encoder->protected_->verify = value;
1457 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
1459 FLAC__ASSERT(0 != encoder);
1460 FLAC__ASSERT(0 != encoder->private_);
1461 FLAC__ASSERT(0 != encoder->protected_);
1462 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1464 encoder->protected_->streamable_subset = value;
1468 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
1470 FLAC__ASSERT(0 != encoder);
1471 FLAC__ASSERT(0 != encoder->private_);
1472 FLAC__ASSERT(0 != encoder->protected_);
1473 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1475 encoder->protected_->channels = value;
1479 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
1481 FLAC__ASSERT(0 != encoder);
1482 FLAC__ASSERT(0 != encoder->private_);
1483 FLAC__ASSERT(0 != encoder->protected_);
1484 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1486 encoder->protected_->bits_per_sample = value;
1490 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
1492 FLAC__ASSERT(0 != encoder);
1493 FLAC__ASSERT(0 != encoder->private_);
1494 FLAC__ASSERT(0 != encoder->protected_);
1495 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1497 encoder->protected_->sample_rate = value;
1501 FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
1503 FLAC__bool ok = true;
1504 FLAC__ASSERT(0 != encoder);
1505 FLAC__ASSERT(0 != encoder->private_);
1506 FLAC__ASSERT(0 != encoder->protected_);
1507 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1509 if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1510 value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1511 ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
1512 ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
1513 ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
1514 ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1515 ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1516 ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1517 ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1518 ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1519 ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1520 ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1521 ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1525 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1527 FLAC__ASSERT(0 != encoder);
1528 FLAC__ASSERT(0 != encoder->private_);
1529 FLAC__ASSERT(0 != encoder->protected_);
1530 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1532 encoder->protected_->blocksize = value;
1536 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1538 FLAC__ASSERT(0 != encoder);
1539 FLAC__ASSERT(0 != encoder->private_);
1540 FLAC__ASSERT(0 != encoder->protected_);
1541 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1543 encoder->protected_->do_mid_side_stereo = value;
1547 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1549 FLAC__ASSERT(0 != encoder);
1550 FLAC__ASSERT(0 != encoder->private_);
1551 FLAC__ASSERT(0 != encoder->protected_);
1552 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1554 encoder->protected_->loose_mid_side_stereo = value;
1558 FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1560 FLAC__ASSERT(0 != encoder);
1561 FLAC__ASSERT(0 != encoder->private_);
1562 FLAC__ASSERT(0 != encoder->protected_);
1563 FLAC__ASSERT(0 != specification);
1564 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1566 #ifdef FLAC__INTEGER_ONLY_LIBRARY
1567 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1569 encoder->protected_->num_apodizations = 0;
1571 const char *s = strchr(specification, ';');
1572 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1573 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1574 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1575 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1576 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1577 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1578 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1579 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1580 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1581 else if(n==6 && 0 == strncmp("connes" , specification, n))
1582 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1583 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1584 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1585 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1586 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1587 if (stddev > 0.0 && stddev <= 0.5) {
1588 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1589 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1592 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1593 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1594 else if(n==4 && 0 == strncmp("hann" , specification, n))
1595 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1596 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1597 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1598 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1599 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1600 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1601 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1602 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1603 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1604 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1605 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1606 if (p >= 0.0 && p <= 1.0) {
1607 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1608 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1611 else if(n==5 && 0 == strncmp("welch" , specification, n))
1612 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1613 if (encoder->protected_->num_apodizations == 32)
1616 specification = s+1;
1620 if(encoder->protected_->num_apodizations == 0) {
1621 encoder->protected_->num_apodizations = 1;
1622 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1623 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1629 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1631 FLAC__ASSERT(0 != encoder);
1632 FLAC__ASSERT(0 != encoder->private_);
1633 FLAC__ASSERT(0 != encoder->protected_);
1634 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1636 encoder->protected_->max_lpc_order = value;
1640 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1642 FLAC__ASSERT(0 != encoder);
1643 FLAC__ASSERT(0 != encoder->private_);
1644 FLAC__ASSERT(0 != encoder->protected_);
1645 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1647 encoder->protected_->qlp_coeff_precision = value;
1651 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1653 FLAC__ASSERT(0 != encoder);
1654 FLAC__ASSERT(0 != encoder->private_);
1655 FLAC__ASSERT(0 != encoder->protected_);
1656 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1658 encoder->protected_->do_qlp_coeff_prec_search = value;
1662 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1664 FLAC__ASSERT(0 != encoder);
1665 FLAC__ASSERT(0 != encoder->private_);
1666 FLAC__ASSERT(0 != encoder->protected_);
1667 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1670 /*@@@ deprecated: */
1671 encoder->protected_->do_escape_coding = value;
1678 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1680 FLAC__ASSERT(0 != encoder);
1681 FLAC__ASSERT(0 != encoder->private_);
1682 FLAC__ASSERT(0 != encoder->protected_);
1683 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1685 encoder->protected_->do_exhaustive_model_search = value;
1689 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1691 FLAC__ASSERT(0 != encoder);
1692 FLAC__ASSERT(0 != encoder->private_);
1693 FLAC__ASSERT(0 != encoder->protected_);
1694 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1696 encoder->protected_->min_residual_partition_order = value;
1700 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1702 FLAC__ASSERT(0 != encoder);
1703 FLAC__ASSERT(0 != encoder->private_);
1704 FLAC__ASSERT(0 != encoder->protected_);
1705 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1707 encoder->protected_->max_residual_partition_order = value;
1711 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1713 FLAC__ASSERT(0 != encoder);
1714 FLAC__ASSERT(0 != encoder->private_);
1715 FLAC__ASSERT(0 != encoder->protected_);
1716 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1719 /*@@@ deprecated: */
1720 encoder->protected_->rice_parameter_search_dist = value;
1727 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1729 FLAC__ASSERT(0 != encoder);
1730 FLAC__ASSERT(0 != encoder->private_);
1731 FLAC__ASSERT(0 != encoder->protected_);
1732 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1734 encoder->protected_->total_samples_estimate = value;
1738 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1740 FLAC__ASSERT(0 != encoder);
1741 FLAC__ASSERT(0 != encoder->private_);
1742 FLAC__ASSERT(0 != encoder->protected_);
1743 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1745 encoder->protected_->metadata = metadata;
1746 encoder->protected_->num_metadata_blocks = num_blocks;
1748 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1755 * These three functions are not static, but not publically exposed in
1756 * include/FLAC/ either. They are used by the test suite.
1758 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1760 FLAC__ASSERT(0 != encoder);
1761 FLAC__ASSERT(0 != encoder->private_);
1762 FLAC__ASSERT(0 != encoder->protected_);
1763 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1765 encoder->private_->disable_constant_subframes = value;
1769 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1771 FLAC__ASSERT(0 != encoder);
1772 FLAC__ASSERT(0 != encoder->private_);
1773 FLAC__ASSERT(0 != encoder->protected_);
1774 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1776 encoder->private_->disable_fixed_subframes = value;
1780 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1782 FLAC__ASSERT(0 != encoder);
1783 FLAC__ASSERT(0 != encoder->private_);
1784 FLAC__ASSERT(0 != encoder->protected_);
1785 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1787 encoder->private_->disable_verbatim_subframes = value;
1791 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1793 FLAC__ASSERT(0 != encoder);
1794 FLAC__ASSERT(0 != encoder->private_);
1795 FLAC__ASSERT(0 != encoder->protected_);
1796 return encoder->protected_->state;
1799 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1801 FLAC__ASSERT(0 != encoder);
1802 FLAC__ASSERT(0 != encoder->private_);
1803 FLAC__ASSERT(0 != encoder->protected_);
1804 if(encoder->protected_->verify)
1805 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1807 return FLAC__STREAM_DECODER_UNINITIALIZED;
1810 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1812 FLAC__ASSERT(0 != encoder);
1813 FLAC__ASSERT(0 != encoder->private_);
1814 FLAC__ASSERT(0 != encoder->protected_);
1815 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1816 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1818 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
1821 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)
1823 FLAC__ASSERT(0 != encoder);
1824 FLAC__ASSERT(0 != encoder->private_);
1825 FLAC__ASSERT(0 != encoder->protected_);
1826 if(0 != absolute_sample)
1827 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1828 if(0 != frame_number)
1829 *frame_number = encoder->private_->verify.error_stats.frame_number;
1831 *channel = encoder->private_->verify.error_stats.channel;
1833 *sample = encoder->private_->verify.error_stats.sample;
1835 *expected = encoder->private_->verify.error_stats.expected;
1837 *got = encoder->private_->verify.error_stats.got;
1840 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1842 FLAC__ASSERT(0 != encoder);
1843 FLAC__ASSERT(0 != encoder->private_);
1844 FLAC__ASSERT(0 != encoder->protected_);
1845 return encoder->protected_->verify;
1848 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1850 FLAC__ASSERT(0 != encoder);
1851 FLAC__ASSERT(0 != encoder->private_);
1852 FLAC__ASSERT(0 != encoder->protected_);
1853 return encoder->protected_->streamable_subset;
1856 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1858 FLAC__ASSERT(0 != encoder);
1859 FLAC__ASSERT(0 != encoder->private_);
1860 FLAC__ASSERT(0 != encoder->protected_);
1861 return encoder->protected_->channels;
1864 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1866 FLAC__ASSERT(0 != encoder);
1867 FLAC__ASSERT(0 != encoder->private_);
1868 FLAC__ASSERT(0 != encoder->protected_);
1869 return encoder->protected_->bits_per_sample;
1872 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1874 FLAC__ASSERT(0 != encoder);
1875 FLAC__ASSERT(0 != encoder->private_);
1876 FLAC__ASSERT(0 != encoder->protected_);
1877 return encoder->protected_->sample_rate;
1880 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1882 FLAC__ASSERT(0 != encoder);
1883 FLAC__ASSERT(0 != encoder->private_);
1884 FLAC__ASSERT(0 != encoder->protected_);
1885 return encoder->protected_->blocksize;
1888 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1890 FLAC__ASSERT(0 != encoder);
1891 FLAC__ASSERT(0 != encoder->private_);
1892 FLAC__ASSERT(0 != encoder->protected_);
1893 return encoder->protected_->do_mid_side_stereo;
1896 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1898 FLAC__ASSERT(0 != encoder);
1899 FLAC__ASSERT(0 != encoder->private_);
1900 FLAC__ASSERT(0 != encoder->protected_);
1901 return encoder->protected_->loose_mid_side_stereo;
1904 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1906 FLAC__ASSERT(0 != encoder);
1907 FLAC__ASSERT(0 != encoder->private_);
1908 FLAC__ASSERT(0 != encoder->protected_);
1909 return encoder->protected_->max_lpc_order;
1912 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1914 FLAC__ASSERT(0 != encoder);
1915 FLAC__ASSERT(0 != encoder->private_);
1916 FLAC__ASSERT(0 != encoder->protected_);
1917 return encoder->protected_->qlp_coeff_precision;
1920 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1922 FLAC__ASSERT(0 != encoder);
1923 FLAC__ASSERT(0 != encoder->private_);
1924 FLAC__ASSERT(0 != encoder->protected_);
1925 return encoder->protected_->do_qlp_coeff_prec_search;
1928 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1930 FLAC__ASSERT(0 != encoder);
1931 FLAC__ASSERT(0 != encoder->private_);
1932 FLAC__ASSERT(0 != encoder->protected_);
1933 return encoder->protected_->do_escape_coding;
1936 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1938 FLAC__ASSERT(0 != encoder);
1939 FLAC__ASSERT(0 != encoder->private_);
1940 FLAC__ASSERT(0 != encoder->protected_);
1941 return encoder->protected_->do_exhaustive_model_search;
1944 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1946 FLAC__ASSERT(0 != encoder);
1947 FLAC__ASSERT(0 != encoder->private_);
1948 FLAC__ASSERT(0 != encoder->protected_);
1949 return encoder->protected_->min_residual_partition_order;
1952 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1954 FLAC__ASSERT(0 != encoder);
1955 FLAC__ASSERT(0 != encoder->private_);
1956 FLAC__ASSERT(0 != encoder->protected_);
1957 return encoder->protected_->max_residual_partition_order;
1960 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1962 FLAC__ASSERT(0 != encoder);
1963 FLAC__ASSERT(0 != encoder->private_);
1964 FLAC__ASSERT(0 != encoder->protected_);
1965 return encoder->protected_->rice_parameter_search_dist;
1968 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1970 FLAC__ASSERT(0 != encoder);
1971 FLAC__ASSERT(0 != encoder->private_);
1972 FLAC__ASSERT(0 != encoder->protected_);
1973 return encoder->protected_->total_samples_estimate;
1976 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1978 unsigned i, j, channel;
1979 FLAC__int32 x, mid, side;
1980 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1982 FLAC__ASSERT(0 != encoder);
1983 FLAC__ASSERT(0 != encoder->private_);
1984 FLAC__ASSERT(0 != encoder->protected_);
1985 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1989 * we have several flavors of the same basic loop, optimized for
1990 * different conditions:
1992 if(encoder->protected_->max_lpc_order > 0) {
1993 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1995 * stereo coding: unroll channel loop
1996 * with LPC: calculate floating point version of signal
1999 if(encoder->protected_->verify)
2000 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2002 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2003 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2004 x = mid = side = buffer[0][j];
2005 encoder->private_->integer_signal[0][i] = x;
2006 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2007 encoder->private_->real_signal[0][i] = (FLAC__real)x;
2010 encoder->private_->integer_signal[1][i] = x;
2011 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2012 encoder->private_->real_signal[1][i] = (FLAC__real)x;
2016 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2017 encoder->private_->integer_signal_mid_side[1][i] = side;
2018 encoder->private_->integer_signal_mid_side[0][i] = mid;
2019 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2020 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2021 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
2023 encoder->private_->current_sample_number++;
2025 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2027 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2029 /* move unprocessed overread samples to beginnings of arrays */
2030 FLAC__ASSERT(i == blocksize+OVERREAD_);
2031 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2033 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2034 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2035 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2036 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2037 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2038 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2039 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2040 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2041 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2043 encoder->private_->current_sample_number = 1;
2045 } while(j < samples);
2049 * independent channel coding: buffer each channel in inner loop
2050 * with LPC: calculate floating point version of signal
2053 if(encoder->protected_->verify)
2054 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2056 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2057 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2058 for(channel = 0; channel < channels; channel++) {
2059 x = buffer[channel][j];
2060 encoder->private_->integer_signal[channel][i] = x;
2061 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2062 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2065 encoder->private_->current_sample_number++;
2067 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2069 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2071 /* move unprocessed overread samples to beginnings of arrays */
2072 FLAC__ASSERT(i == blocksize+OVERREAD_);
2073 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2075 for(channel = 0; channel < channels; channel++) {
2076 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2077 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2078 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2081 encoder->private_->current_sample_number = 1;
2083 } while(j < samples);
2087 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2089 * stereo coding: unroll channel loop
2090 * without LPC: no need to calculate floating point version of signal
2093 if(encoder->protected_->verify)
2094 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2096 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2097 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2098 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
2100 encoder->private_->integer_signal[1][i] = x;
2103 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2104 encoder->private_->integer_signal_mid_side[1][i] = side;
2105 encoder->private_->integer_signal_mid_side[0][i] = mid;
2106 encoder->private_->current_sample_number++;
2108 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2110 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2112 /* move unprocessed overread samples to beginnings of arrays */
2113 FLAC__ASSERT(i == blocksize+OVERREAD_);
2114 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2116 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2117 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2118 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2119 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2120 encoder->private_->current_sample_number = 1;
2122 } while(j < samples);
2126 * independent channel coding: buffer each channel in inner loop
2127 * without LPC: no need to calculate floating point version of signal
2130 if(encoder->protected_->verify)
2131 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2133 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2134 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2135 for(channel = 0; channel < channels; channel++)
2136 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
2137 encoder->private_->current_sample_number++;
2139 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2141 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2143 /* move unprocessed overread samples to beginnings of arrays */
2144 FLAC__ASSERT(i == blocksize+OVERREAD_);
2145 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2147 for(channel = 0; channel < channels; channel++)
2148 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2149 encoder->private_->current_sample_number = 1;
2151 } while(j < samples);
2158 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
2160 unsigned i, j, k, channel;
2161 FLAC__int32 x, mid, side;
2162 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
2164 FLAC__ASSERT(0 != encoder);
2165 FLAC__ASSERT(0 != encoder->private_);
2166 FLAC__ASSERT(0 != encoder->protected_);
2167 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2171 * we have several flavors of the same basic loop, optimized for
2172 * different conditions:
2174 if(encoder->protected_->max_lpc_order > 0) {
2175 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2177 * stereo coding: unroll channel loop
2178 * with LPC: calculate floating point version of signal
2181 if(encoder->protected_->verify)
2182 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2184 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2185 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2186 x = mid = side = buffer[k++];
2187 encoder->private_->integer_signal[0][i] = x;
2188 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2189 encoder->private_->real_signal[0][i] = (FLAC__real)x;
2192 encoder->private_->integer_signal[1][i] = x;
2193 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2194 encoder->private_->real_signal[1][i] = (FLAC__real)x;
2198 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2199 encoder->private_->integer_signal_mid_side[1][i] = side;
2200 encoder->private_->integer_signal_mid_side[0][i] = mid;
2201 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2202 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2203 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
2205 encoder->private_->current_sample_number++;
2207 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2209 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2211 /* move unprocessed overread samples to beginnings of arrays */
2212 FLAC__ASSERT(i == blocksize+OVERREAD_);
2213 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2215 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2216 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2217 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2218 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2219 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2220 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2221 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2222 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2223 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2225 encoder->private_->current_sample_number = 1;
2227 } while(j < samples);
2231 * independent channel coding: buffer each channel in inner loop
2232 * with LPC: calculate floating point version of signal
2235 if(encoder->protected_->verify)
2236 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2238 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2239 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2240 for(channel = 0; channel < channels; channel++) {
2242 encoder->private_->integer_signal[channel][i] = x;
2243 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2244 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2247 encoder->private_->current_sample_number++;
2249 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2251 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2253 /* move unprocessed overread samples to beginnings of arrays */
2254 FLAC__ASSERT(i == blocksize+OVERREAD_);
2255 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2257 for(channel = 0; channel < channels; channel++) {
2258 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2259 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2260 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2263 encoder->private_->current_sample_number = 1;
2265 } while(j < samples);
2269 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2271 * stereo coding: unroll channel loop
2272 * without LPC: no need to calculate floating point version of signal
2275 if(encoder->protected_->verify)
2276 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2278 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2279 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2280 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
2282 encoder->private_->integer_signal[1][i] = x;
2285 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2286 encoder->private_->integer_signal_mid_side[1][i] = side;
2287 encoder->private_->integer_signal_mid_side[0][i] = mid;
2288 encoder->private_->current_sample_number++;
2290 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2292 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2294 /* move unprocessed overread samples to beginnings of arrays */
2295 FLAC__ASSERT(i == blocksize+OVERREAD_);
2296 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2298 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2299 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2300 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2301 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2302 encoder->private_->current_sample_number = 1;
2304 } while(j < samples);
2308 * independent channel coding: buffer each channel in inner loop
2309 * without LPC: no need to calculate floating point version of signal
2312 if(encoder->protected_->verify)
2313 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2315 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2316 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2317 for(channel = 0; channel < channels; channel++)
2318 encoder->private_->integer_signal[channel][i] = buffer[k++];
2319 encoder->private_->current_sample_number++;
2321 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2323 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2325 /* move unprocessed overread samples to beginnings of arrays */
2326 FLAC__ASSERT(i == blocksize+OVERREAD_);
2327 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2329 for(channel = 0; channel < channels; channel++)
2330 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2331 encoder->private_->current_sample_number = 1;
2333 } while(j < samples);
2340 /***********************************************************************
2342 * Private class methods
2344 ***********************************************************************/
2346 void set_defaults_(FLAC__StreamEncoder *encoder)
2348 FLAC__ASSERT(0 != encoder);
2350 #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2351 encoder->protected_->verify = true;
2353 encoder->protected_->verify = false;
2355 encoder->protected_->streamable_subset = true;
2356 encoder->protected_->do_mid_side_stereo = false;
2357 encoder->protected_->loose_mid_side_stereo = false;
2358 encoder->protected_->channels = 2;
2359 encoder->protected_->bits_per_sample = 16;
2360 encoder->protected_->sample_rate = 44100;
2361 encoder->protected_->blocksize = 0;
2362 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2363 encoder->protected_->num_apodizations = 1;
2364 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2365 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
2367 encoder->protected_->max_lpc_order = 0;
2368 encoder->protected_->qlp_coeff_precision = 0;
2369 encoder->protected_->do_qlp_coeff_prec_search = false;
2370 encoder->protected_->do_exhaustive_model_search = false;
2371 encoder->protected_->do_escape_coding = false;
2372 encoder->protected_->min_residual_partition_order = 0;
2373 encoder->protected_->max_residual_partition_order = 0;
2374 encoder->protected_->rice_parameter_search_dist = 0;
2375 encoder->protected_->total_samples_estimate = 0;
2376 encoder->protected_->metadata = 0;
2377 encoder->protected_->num_metadata_blocks = 0;
2379 encoder->private_->seek_table = 0;
2380 encoder->private_->disable_constant_subframes = false;
2381 encoder->private_->disable_fixed_subframes = false;
2382 encoder->private_->disable_verbatim_subframes = false;
2384 encoder->private_->is_ogg = false;
2386 encoder->private_->read_callback = 0;
2387 encoder->private_->write_callback = 0;
2388 encoder->private_->seek_callback = 0;
2389 encoder->private_->tell_callback = 0;
2390 encoder->private_->metadata_callback = 0;
2391 encoder->private_->progress_callback = 0;
2392 encoder->private_->client_data = 0;
2395 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2399 void free_(FLAC__StreamEncoder *encoder)
2401 unsigned i, channel;
2403 FLAC__ASSERT(0 != encoder);
2404 for(i = 0; i < encoder->protected_->channels; i++) {
2405 if(0 != encoder->private_->integer_signal_unaligned[i]) {
2406 free(encoder->private_->integer_signal_unaligned[i]);
2407 encoder->private_->integer_signal_unaligned[i] = 0;
2409 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2410 if(0 != encoder->private_->real_signal_unaligned[i]) {
2411 free(encoder->private_->real_signal_unaligned[i]);
2412 encoder->private_->real_signal_unaligned[i] = 0;
2416 for(i = 0; i < 2; i++) {
2417 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
2418 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2419 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2421 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2422 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
2423 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2424 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2428 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2429 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2430 if(0 != encoder->private_->window_unaligned[i]) {
2431 free(encoder->private_->window_unaligned[i]);
2432 encoder->private_->window_unaligned[i] = 0;
2435 if(0 != encoder->private_->windowed_signal_unaligned) {
2436 free(encoder->private_->windowed_signal_unaligned);
2437 encoder->private_->windowed_signal_unaligned = 0;
2440 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2441 for(i = 0; i < 2; i++) {
2442 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
2443 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2444 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2448 for(channel = 0; channel < 2; channel++) {
2449 for(i = 0; i < 2; i++) {
2450 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
2451 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2452 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2456 if(0 != encoder->private_->abs_residual_unaligned) {
2457 free(encoder->private_->abs_residual_unaligned);
2458 encoder->private_->abs_residual_unaligned = 0;
2460 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
2461 free(encoder->private_->abs_residual_partition_sums_unaligned);
2462 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2464 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
2465 free(encoder->private_->raw_bits_per_partition_unaligned);
2466 encoder->private_->raw_bits_per_partition_unaligned = 0;
2468 if(encoder->protected_->verify) {
2469 for(i = 0; i < encoder->protected_->channels; i++) {
2470 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2471 free(encoder->private_->verify.input_fifo.data[i]);
2472 encoder->private_->verify.input_fifo.data[i] = 0;
2476 FLAC__bitbuffer_free(encoder->private_->frame);
2479 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
2482 unsigned i, channel;
2484 FLAC__ASSERT(new_blocksize > 0);
2485 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2486 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
2488 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
2489 if(new_blocksize <= encoder->private_->input_capacity)
2494 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2495 * requires that the input arrays (in our case the integer signals)
2496 * have a buffer of up to 3 zeroes in front (at negative indices) for
2497 * alignment purposes; we use 4 in front to keep the data well-aligned.
2500 for(i = 0; ok && i < encoder->protected_->channels; i++) {
2501 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
2502 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2503 encoder->private_->integer_signal[i] += 4;
2504 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2505 if(encoder->protected_->max_lpc_order > 0)
2506 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
2509 for(i = 0; ok && i < 2; i++) {
2510 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]);
2511 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2512 encoder->private_->integer_signal_mid_side[i] += 4;
2513 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2514 if(encoder->protected_->max_lpc_order > 0)
2515 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]);
2518 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2519 if(ok && encoder->protected_->max_lpc_order > 0) {
2520 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2521 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2522 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
2525 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
2526 for(i = 0; ok && i < 2; i++) {
2527 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
2530 for(channel = 0; ok && channel < 2; channel++) {
2531 for(i = 0; ok && i < 2; i++) {
2532 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]);
2535 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_blocksize, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
2536 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 */
2537 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
2538 if(encoder->protected_->do_escape_coding)
2539 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
2541 /* now adjust the windows if the blocksize has changed */
2542 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2543 if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
2544 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2545 switch(encoder->protected_->apodizations[i].type) {
2546 case FLAC__APODIZATION_BARTLETT:
2547 FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
2549 case FLAC__APODIZATION_BARTLETT_HANN:
2550 FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
2552 case FLAC__APODIZATION_BLACKMAN:
2553 FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
2555 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2556 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
2558 case FLAC__APODIZATION_CONNES:
2559 FLAC__window_connes(encoder->private_->window[i], new_blocksize);
2561 case FLAC__APODIZATION_FLATTOP:
2562 FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
2564 case FLAC__APODIZATION_GAUSS:
2565 FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2567 case FLAC__APODIZATION_HAMMING:
2568 FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
2570 case FLAC__APODIZATION_HANN:
2571 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2573 case FLAC__APODIZATION_KAISER_BESSEL:
2574 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
2576 case FLAC__APODIZATION_NUTTALL:
2577 FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
2579 case FLAC__APODIZATION_RECTANGLE:
2580 FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
2582 case FLAC__APODIZATION_TRIANGLE:
2583 FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
2585 case FLAC__APODIZATION_TUKEY:
2586 FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
2588 case FLAC__APODIZATION_WELCH:
2589 FLAC__window_welch(encoder->private_->window[i], new_blocksize);
2593 /* double protection */
2594 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2602 encoder->private_->input_capacity = new_blocksize;
2604 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2609 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
2611 const FLAC__byte *buffer;
2614 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2616 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2618 if(encoder->protected_->verify) {
2619 encoder->private_->verify.output.data = buffer;
2620 encoder->private_->verify.output.bytes = bytes;
2621 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2622 encoder->private_->verify.needs_magic_hack = true;
2625 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2626 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2627 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2628 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2634 if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2635 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2636 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2640 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2643 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2644 encoder->private_->streaminfo.data.stream_info.max_framesize = max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
2650 FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block)
2652 FLAC__StreamEncoderWriteStatus status;
2653 FLAC__uint64 output_position = 0;
2655 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2656 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2657 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2658 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2662 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2665 FLAC__MetadataType type = (buffer[0] & 0x7f);
2666 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2667 encoder->protected_->streaminfo_offset = output_position;
2668 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2669 encoder->protected_->seektable_offset = output_position;
2673 * Mark the current seek point if hit (if audio_offset == 0 that
2674 * means we're still writing metadata and haven't hit the first
2677 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2678 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2679 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2680 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2681 FLAC__uint64 test_sample;
2683 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2684 test_sample = encoder->private_->seek_table->points[i].sample_number;
2685 if(test_sample > frame_last_sample) {
2688 else if(test_sample >= frame_first_sample) {
2689 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2690 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2691 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2692 encoder->private_->first_seekpoint_to_check++;
2693 /* DO NOT: "break;" and here's why:
2694 * The seektable template may contain more than one target
2695 * sample for any given frame; we will keep looping, generating
2696 * duplicate seekpoints for them, and we'll clean it up later,
2697 * just before writing the seektable back to the metadata.
2701 encoder->private_->first_seekpoint_to_check++;
2707 if(encoder->private_->is_ogg) {
2708 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2709 &encoder->protected_->ogg_encoder_aspect,
2713 encoder->private_->current_frame_number,
2715 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2717 encoder->private_->client_data
2722 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2724 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2725 encoder->private_->bytes_written += bytes;
2726 encoder->private_->samples_written += samples;
2727 /* we keep a high watermark on the number of frames written because
2728 * when the encoder goes back to write metadata, 'current_frame'
2729 * will drop back to 0.
2731 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2734 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2739 /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2740 void update_metadata_(const FLAC__StreamEncoder *encoder)
2742 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2743 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2744 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2745 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2746 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2747 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2748 FLAC__StreamEncoderSeekStatus seek_status;
2750 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2752 /* All this is based on intimate knowledge of the stream header
2753 * layout, but a change to the header format that would break this
2754 * would also break all streams encoded in the previous format.
2758 * Write MD5 signature
2761 const unsigned md5_offset =
2762 FLAC__STREAM_METADATA_HEADER_LENGTH +
2764 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2765 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2766 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2767 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2768 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2769 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2770 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2771 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2774 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2775 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2776 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2779 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2780 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2786 * Write total samples
2789 const unsigned total_samples_byte_offset =
2790 FLAC__STREAM_METADATA_HEADER_LENGTH +
2792 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2793 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2794 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2795 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2796 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2797 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2798 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2802 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2803 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2804 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2805 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2806 b[4] = (FLAC__byte)(samples & 0xFF);
2807 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) {
2808 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2809 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2812 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2813 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2819 * Write min/max framesize
2822 const unsigned min_framesize_offset =
2823 FLAC__STREAM_METADATA_HEADER_LENGTH +
2825 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2826 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2829 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2830 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2831 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2832 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2833 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2834 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2835 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) {
2836 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2837 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2840 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2841 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2849 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2852 FLAC__format_seektable_sort(encoder->private_->seek_table);
2854 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2856 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) {
2857 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2858 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2862 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2865 xx = encoder->private_->seek_table->points[i].sample_number;
2866 b[7] = (FLAC__byte)xx; xx >>= 8;
2867 b[6] = (FLAC__byte)xx; xx >>= 8;
2868 b[5] = (FLAC__byte)xx; xx >>= 8;
2869 b[4] = (FLAC__byte)xx; xx >>= 8;
2870 b[3] = (FLAC__byte)xx; xx >>= 8;
2871 b[2] = (FLAC__byte)xx; xx >>= 8;
2872 b[1] = (FLAC__byte)xx; xx >>= 8;
2873 b[0] = (FLAC__byte)xx; xx >>= 8;
2874 xx = encoder->private_->seek_table->points[i].stream_offset;
2875 b[15] = (FLAC__byte)xx; xx >>= 8;
2876 b[14] = (FLAC__byte)xx; xx >>= 8;
2877 b[13] = (FLAC__byte)xx; xx >>= 8;
2878 b[12] = (FLAC__byte)xx; xx >>= 8;
2879 b[11] = (FLAC__byte)xx; xx >>= 8;
2880 b[10] = (FLAC__byte)xx; xx >>= 8;
2881 b[9] = (FLAC__byte)xx; xx >>= 8;
2882 b[8] = (FLAC__byte)xx; xx >>= 8;
2883 x = encoder->private_->seek_table->points[i].frame_samples;
2884 b[17] = (FLAC__byte)x; x >>= 8;
2885 b[16] = (FLAC__byte)x; x >>= 8;
2886 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2887 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2895 /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2896 void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2898 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2899 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2900 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2901 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2902 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2905 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2907 /* All this is based on intimate knowledge of the stream header
2908 * layout, but a change to the header format that would break this
2909 * would also break all streams encoded in the previous format.
2913 ** Write STREAMINFO stats
2915 simple_ogg_page__init(&page);
2916 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2917 simple_ogg_page__clear(&page);
2918 return; /* state already set */
2922 * Write MD5 signature
2925 const unsigned md5_offset =
2926 FLAC__STREAM_METADATA_HEADER_LENGTH +
2928 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2929 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2930 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2931 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2932 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2933 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2934 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2935 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2938 if(md5_offset + 16 > (unsigned)page.body_len) {
2939 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2940 simple_ogg_page__clear(&page);
2943 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2947 * Write total samples
2950 const unsigned total_samples_byte_offset =
2951 FLAC__STREAM_METADATA_HEADER_LENGTH +
2953 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2954 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2955 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2956 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2957 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2958 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2959 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2963 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2964 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2965 simple_ogg_page__clear(&page);
2968 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2969 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2970 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2971 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2972 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2973 b[4] = (FLAC__byte)(samples & 0xFF);
2974 memcpy(page.body + total_samples_byte_offset, b, 5);
2978 * Write min/max framesize
2981 const unsigned min_framesize_offset =
2982 FLAC__STREAM_METADATA_HEADER_LENGTH +
2984 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2985 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2988 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
2989 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2990 simple_ogg_page__clear(&page);
2993 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2994 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2995 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2996 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2997 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2998 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2999 memcpy(page.body + min_framesize_offset, b, 6);
3001 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3002 simple_ogg_page__clear(&page);
3003 return; /* state already set */
3005 simple_ogg_page__clear(&page);
3010 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
3014 FLAC__format_seektable_sort(encoder->private_->seek_table);
3016 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
3018 simple_ogg_page__init(&page);
3019 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
3020 simple_ogg_page__clear(&page);
3021 return; /* state already set */
3024 if(FLAC__STREAM_METADATA_HEADER_LENGTH + (18*encoder->private_->seek_table->num_points) > (unsigned)page.body_len) {
3025 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3026 simple_ogg_page__clear(&page);
3030 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
3033 xx = encoder->private_->seek_table->points[i].sample_number;
3034 b[7] = (FLAC__byte)xx; xx >>= 8;
3035 b[6] = (FLAC__byte)xx; xx >>= 8;
3036 b[5] = (FLAC__byte)xx; xx >>= 8;
3037 b[4] = (FLAC__byte)xx; xx >>= 8;
3038 b[3] = (FLAC__byte)xx; xx >>= 8;
3039 b[2] = (FLAC__byte)xx; xx >>= 8;
3040 b[1] = (FLAC__byte)xx; xx >>= 8;
3041 b[0] = (FLAC__byte)xx; xx >>= 8;
3042 xx = encoder->private_->seek_table->points[i].stream_offset;
3043 b[15] = (FLAC__byte)xx; xx >>= 8;
3044 b[14] = (FLAC__byte)xx; xx >>= 8;
3045 b[13] = (FLAC__byte)xx; xx >>= 8;
3046 b[12] = (FLAC__byte)xx; xx >>= 8;
3047 b[11] = (FLAC__byte)xx; xx >>= 8;
3048 b[10] = (FLAC__byte)xx; xx >>= 8;
3049 b[9] = (FLAC__byte)xx; xx >>= 8;
3050 b[8] = (FLAC__byte)xx; xx >>= 8;
3051 x = encoder->private_->seek_table->points[i].frame_samples;
3052 b[17] = (FLAC__byte)x; x >>= 8;
3053 b[16] = (FLAC__byte)x; x >>= 8;
3054 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
3055 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
3056 simple_ogg_page__clear(&page);
3062 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3063 simple_ogg_page__clear(&page);
3064 return; /* state already set */
3066 simple_ogg_page__clear(&page);
3071 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
3073 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
3076 * Accumulate raw signal to the MD5 signature
3078 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)) {
3079 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
3084 * Process the frame header and subframes into the frame bitbuffer
3086 if(!process_subframes_(encoder, is_fractional_block)) {
3087 /* the above function sets the state for us in case of an error */
3092 * Zero-pad the frame to a byte_boundary
3094 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
3095 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
3100 * CRC-16 the whole thing
3102 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
3103 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
3108 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
3109 /* the above function sets the state for us in case of an error */
3114 * Get ready for the next frame
3116 encoder->private_->current_sample_number = 0;
3117 encoder->private_->current_frame_number++;
3118 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
3123 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
3125 FLAC__FrameHeader frame_header;
3126 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
3127 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
3130 * Calculate the min,max Rice partition orders
3132 if(is_fractional_block) {
3133 max_partition_order = 0;
3136 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
3137 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
3139 min_partition_order = min(min_partition_order, max_partition_order);
3141 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
3146 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
3147 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
3150 frame_header.blocksize = encoder->protected_->blocksize;
3151 frame_header.sample_rate = encoder->protected_->sample_rate;
3152 frame_header.channels = encoder->protected_->channels;
3153 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
3154 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
3155 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
3156 frame_header.number.frame_number = encoder->private_->current_frame_number;
3159 * Figure out what channel assignments to try
3161 if(encoder->protected_->do_mid_side_stereo) {
3162 if(encoder->protected_->loose_mid_side_stereo) {
3163 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
3164 do_independent = true;
3168 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
3169 do_mid_side = !do_independent;
3173 do_independent = true;
3178 do_independent = true;
3179 do_mid_side = false;
3182 FLAC__ASSERT(do_independent || do_mid_side);
3185 * Check for wasted bits; set effective bps for each subframe
3187 if(do_independent) {
3188 for(channel = 0; channel < encoder->protected_->channels; channel++) {
3189 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
3190 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
3191 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
3195 FLAC__ASSERT(encoder->protected_->channels == 2);
3196 for(channel = 0; channel < 2; channel++) {
3197 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
3198 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
3199 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
3204 * First do a normal encoding pass of each independent channel
3206 if(do_independent) {
3207 for(channel = 0; channel < encoder->protected_->channels; channel++) {
3211 min_partition_order,
3212 max_partition_order,
3213 precompute_partition_sums,
3215 encoder->private_->subframe_bps[channel],
3216 encoder->private_->integer_signal[channel],
3217 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3218 encoder->private_->real_signal[channel],
3220 encoder->private_->subframe_workspace_ptr[channel],
3221 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
3222 encoder->private_->residual_workspace[channel],
3223 encoder->private_->best_subframe+channel,
3224 encoder->private_->best_subframe_bits+channel
3232 * Now do mid and side channels if requested
3235 FLAC__ASSERT(encoder->protected_->channels == 2);
3237 for(channel = 0; channel < 2; channel++) {
3241 min_partition_order,
3242 max_partition_order,
3243 precompute_partition_sums,
3245 encoder->private_->subframe_bps_mid_side[channel],
3246 encoder->private_->integer_signal_mid_side[channel],
3247 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3248 encoder->private_->real_signal_mid_side[channel],
3250 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3251 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3252 encoder->private_->residual_workspace_mid_side[channel],
3253 encoder->private_->best_subframe_mid_side+channel,
3254 encoder->private_->best_subframe_bits_mid_side+channel
3262 * Compose the frame bitbuffer
3265 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3266 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
3267 FLAC__ChannelAssignment channel_assignment;
3269 FLAC__ASSERT(encoder->protected_->channels == 2);
3271 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3272 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
3275 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3277 FLAC__ChannelAssignment ca;
3279 FLAC__ASSERT(do_independent && do_mid_side);
3281 /* We have to figure out which channel assignent results in the smallest frame */
3282 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3283 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3284 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3285 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
3287 for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
3288 if(bits[ca] < min_bits) {
3289 min_bits = bits[ca];
3290 channel_assignment = ca;
3295 frame_header.channel_assignment = channel_assignment;
3297 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3298 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3302 switch(channel_assignment) {
3303 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3304 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3305 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
3307 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3308 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3309 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3311 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3312 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3313 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
3315 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3316 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3317 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3323 switch(channel_assignment) {
3324 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3325 left_bps = encoder->private_->subframe_bps [0];
3326 right_bps = encoder->private_->subframe_bps [1];
3328 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3329 left_bps = encoder->private_->subframe_bps [0];
3330 right_bps = encoder->private_->subframe_bps_mid_side[1];
3332 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3333 left_bps = encoder->private_->subframe_bps_mid_side[1];
3334 right_bps = encoder->private_->subframe_bps [1];
3336 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3337 left_bps = encoder->private_->subframe_bps_mid_side[0];
3338 right_bps = encoder->private_->subframe_bps_mid_side[1];
3344 /* note that encoder_add_subframe_ sets the state for us in case of an error */
3345 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
3347 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
3351 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3352 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3356 for(channel = 0; channel < encoder->protected_->channels; channel++) {
3357 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
3358 /* the above function sets the state for us in case of an error */
3364 if(encoder->protected_->loose_mid_side_stereo) {
3365 encoder->private_->loose_mid_side_stereo_frame_count++;
3366 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3367 encoder->private_->loose_mid_side_stereo_frame_count = 0;
3370 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
3375 FLAC__bool process_subframe_(
3376 FLAC__StreamEncoder *encoder,
3377 unsigned min_partition_order,
3378 unsigned max_partition_order,
3379 FLAC__bool precompute_partition_sums,
3380 const FLAC__FrameHeader *frame_header,
3381 unsigned subframe_bps,
3382 const FLAC__int32 integer_signal[],
3383 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3384 const FLAC__real real_signal[],
3386 FLAC__Subframe *subframe[2],
3387 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3388 FLAC__int32 *residual[2],
3389 unsigned *best_subframe,
3393 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3394 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3396 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3398 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3399 FLAC__double lpc_residual_bits_per_sample;
3400 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 */
3401 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
3402 unsigned min_lpc_order, max_lpc_order, lpc_order;
3403 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
3405 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
3406 unsigned rice_parameter;
3407 unsigned _candidate_bits, _best_bits;
3408 unsigned _best_subframe;
3410 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3412 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3413 _best_bits = UINT_MAX;
3415 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3417 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3418 unsigned signal_is_constant = false;
3419 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);
3420 /* check for constant subframe */
3422 !encoder->private_->disable_constant_subframes &&
3423 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3424 fixed_residual_bits_per_sample[1] == 0.0
3426 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3429 /* the above means it's possible all samples are the same value; now double-check it: */
3431 signal_is_constant = true;
3432 for(i = 1; i < frame_header->blocksize; i++) {
3433 if(integer_signal[0] != integer_signal[i]) {
3434 signal_is_constant = false;
3439 if(signal_is_constant) {
3440 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
3441 if(_candidate_bits < _best_bits) {
3442 _best_subframe = !_best_subframe;
3443 _best_bits = _candidate_bits;
3447 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3449 if(encoder->protected_->do_exhaustive_model_search) {
3450 min_fixed_order = 0;
3451 max_fixed_order = FLAC__MAX_FIXED_ORDER;
3454 min_fixed_order = max_fixed_order = guess_fixed_order;
3456 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
3457 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3458 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
3459 continue; /* don't even try */
3460 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 */
3462 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3463 continue; /* don't even try */
3464 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 */
3466 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3467 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3468 #ifdef DEBUG_VERBOSE
3469 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3471 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3474 evaluate_fixed_subframe_(
3477 residual[!_best_subframe],
3478 encoder->private_->abs_residual,
3479 encoder->private_->abs_residual_partition_sums,
3480 encoder->private_->raw_bits_per_partition,
3481 frame_header->blocksize,
3485 min_partition_order,
3486 max_partition_order,
3487 precompute_partition_sums,
3488 encoder->protected_->do_escape_coding,
3489 encoder->protected_->rice_parameter_search_dist,
3490 subframe[!_best_subframe],
3491 partitioned_rice_contents[!_best_subframe]
3493 if(_candidate_bits < _best_bits) {
3494 _best_subframe = !_best_subframe;
3495 _best_bits = _candidate_bits;
3500 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3502 if(encoder->protected_->max_lpc_order > 0) {
3503 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
3504 max_lpc_order = frame_header->blocksize-1;
3506 max_lpc_order = encoder->protected_->max_lpc_order;
3507 if(max_lpc_order > 0) {
3509 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
3510 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
3511 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3512 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3513 if(autoc[0] != 0.0) {
3514 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3515 if(encoder->protected_->do_exhaustive_model_search) {
3519 const unsigned guess_lpc_order =
3520 FLAC__lpc_compute_best_order(
3523 frame_header->blocksize,
3525 encoder->protected_->do_qlp_coeff_prec_search?
3526 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3527 encoder->protected_->qlp_coeff_precision
3530 min_lpc_order = max_lpc_order = guess_lpc_order;
3532 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3533 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3534 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3535 continue; /* don't even try */
3536 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3537 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3538 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3539 #ifdef DEBUG_VERBOSE
3540 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3542 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3544 if(encoder->protected_->do_qlp_coeff_prec_search) {
3545 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
3546 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3547 if(subframe_bps <= 17) {
3548 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
3549 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3552 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3555 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3557 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3559 evaluate_lpc_subframe_(
3562 residual[!_best_subframe],
3563 encoder->private_->abs_residual,
3564 encoder->private_->abs_residual_partition_sums,
3565 encoder->private_->raw_bits_per_partition,
3566 encoder->private_->lp_coeff[lpc_order-1],
3567 frame_header->blocksize,
3570 qlp_coeff_precision,
3572 min_partition_order,
3573 max_partition_order,
3574 precompute_partition_sums,
3575 encoder->protected_->do_escape_coding,
3576 encoder->protected_->rice_parameter_search_dist,
3577 subframe[!_best_subframe],
3578 partitioned_rice_contents[!_best_subframe]
3580 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3581 if(_candidate_bits < _best_bits) {
3582 _best_subframe = !_best_subframe;
3583 _best_bits = _candidate_bits;
3592 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
3596 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3597 if(_best_bits == UINT_MAX) {
3598 FLAC__ASSERT(_best_subframe == 0);
3599 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3602 *best_subframe = _best_subframe;
3603 *best_bits = _best_bits;
3608 FLAC__bool add_subframe_(
3609 FLAC__StreamEncoder *encoder,
3610 const FLAC__FrameHeader *frame_header,
3611 unsigned subframe_bps,
3612 const FLAC__Subframe *subframe,
3613 FLAC__BitBuffer *frame
3616 switch(subframe->type) {
3617 case FLAC__SUBFRAME_TYPE_CONSTANT:
3618 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
3619 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3623 case FLAC__SUBFRAME_TYPE_FIXED:
3624 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
3625 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3629 case FLAC__SUBFRAME_TYPE_LPC:
3630 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
3631 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3635 case FLAC__SUBFRAME_TYPE_VERBATIM:
3636 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
3637 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3648 unsigned evaluate_constant_subframe_(
3649 const FLAC__int32 signal,
3650 unsigned subframe_bps,
3651 FLAC__Subframe *subframe
3654 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3655 subframe->data.constant.value = signal;
3657 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
3660 unsigned evaluate_fixed_subframe_(
3661 FLAC__StreamEncoder *encoder,
3662 const FLAC__int32 signal[],
3663 FLAC__int32 residual[],
3664 FLAC__uint32 abs_residual[],
3665 FLAC__uint64 abs_residual_partition_sums[],
3666 unsigned raw_bits_per_partition[],
3668 unsigned subframe_bps,
3670 unsigned rice_parameter,
3671 unsigned min_partition_order,
3672 unsigned max_partition_order,
3673 FLAC__bool precompute_partition_sums,
3674 FLAC__bool do_escape_coding,
3675 unsigned rice_parameter_search_dist,
3676 FLAC__Subframe *subframe,
3677 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3680 unsigned i, residual_bits;
3681 const unsigned residual_samples = blocksize - order;
3683 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3685 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3687 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3688 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3689 subframe->data.fixed.residual = residual;
3692 find_best_partition_order_(
3696 abs_residual_partition_sums,
3697 raw_bits_per_partition,
3701 min_partition_order,
3702 max_partition_order,
3703 precompute_partition_sums,
3705 rice_parameter_search_dist,
3706 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3709 subframe->data.fixed.order = order;
3710 for(i = 0; i < order; i++)
3711 subframe->data.fixed.warmup[i] = signal[i];
3713 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
3716 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3717 unsigned evaluate_lpc_subframe_(
3718 FLAC__StreamEncoder *encoder,
3719 const FLAC__int32 signal[],
3720 FLAC__int32 residual[],
3721 FLAC__uint32 abs_residual[],
3722 FLAC__uint64 abs_residual_partition_sums[],
3723 unsigned raw_bits_per_partition[],
3724 const FLAC__real lp_coeff[],
3726 unsigned subframe_bps,
3728 unsigned qlp_coeff_precision,
3729 unsigned rice_parameter,
3730 unsigned min_partition_order,
3731 unsigned max_partition_order,
3732 FLAC__bool precompute_partition_sums,
3733 FLAC__bool do_escape_coding,
3734 unsigned rice_parameter_search_dist,
3735 FLAC__Subframe *subframe,
3736 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3739 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
3740 unsigned i, residual_bits;
3741 int quantization, ret;
3742 const unsigned residual_samples = blocksize - order;
3744 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3745 if(subframe_bps <= 16) {
3746 FLAC__ASSERT(order > 0);
3747 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3748 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3751 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
3753 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3755 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3756 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3757 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3759 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3761 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3763 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3765 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3766 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3767 subframe->data.lpc.residual = residual;
3770 find_best_partition_order_(
3774 abs_residual_partition_sums,
3775 raw_bits_per_partition,
3779 min_partition_order,
3780 max_partition_order,
3781 precompute_partition_sums,
3783 rice_parameter_search_dist,
3784 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3787 subframe->data.lpc.order = order;
3788 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3789 subframe->data.lpc.quantization_level = quantization;
3790 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
3791 for(i = 0; i < order; i++)
3792 subframe->data.lpc.warmup[i] = signal[i];
3794 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;
3798 unsigned evaluate_verbatim_subframe_(
3799 const FLAC__int32 signal[],
3801 unsigned subframe_bps,
3802 FLAC__Subframe *subframe
3805 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3807 subframe->data.verbatim.data = signal;
3809 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
3812 unsigned find_best_partition_order_(
3813 FLAC__StreamEncoderPrivate *private_,
3814 const FLAC__int32 residual[],
3815 FLAC__uint32 abs_residual[],
3816 FLAC__uint64 abs_residual_partition_sums[],
3817 unsigned raw_bits_per_partition[],
3818 unsigned residual_samples,
3819 unsigned predictor_order,
3820 unsigned rice_parameter,
3821 unsigned min_partition_order,
3822 unsigned max_partition_order,
3823 FLAC__bool precompute_partition_sums,
3824 FLAC__bool do_escape_coding,
3825 unsigned rice_parameter_search_dist,
3826 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3830 unsigned residual_bits, best_residual_bits = 0;
3831 unsigned residual_sample;
3832 unsigned best_parameters_index = 0;
3833 const unsigned blocksize = residual_samples + predictor_order;
3835 /* compute abs(residual) for use later */
3836 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3837 r = residual[residual_sample];
3838 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
3841 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
3842 min_partition_order = min(min_partition_order, max_partition_order);
3844 if(precompute_partition_sums) {
3845 int partition_order;
3848 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
3850 if(do_escape_coding)
3851 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
3853 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3854 #ifdef DONT_ESTIMATE_RICE_BITS
3856 set_partitioned_rice_with_precompute_(
3858 abs_residual_partition_sums+sum,
3859 raw_bits_per_partition+sum,
3863 rice_parameter_search_dist,
3864 (unsigned)partition_order,
3866 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3872 set_partitioned_rice_with_precompute_(
3874 abs_residual_partition_sums+sum,
3875 raw_bits_per_partition+sum,
3879 rice_parameter_search_dist,
3880 (unsigned)partition_order,
3882 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3888 FLAC__ASSERT(best_residual_bits != 0);
3891 sum += 1u << partition_order;
3892 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3893 best_residual_bits = residual_bits;
3894 best_parameters_index = !best_parameters_index;
3895 best_partitioned_rice->order = partition_order;
3900 unsigned partition_order;
3901 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3902 #ifdef DONT_ESTIMATE_RICE_BITS
3904 set_partitioned_rice_(
3910 rice_parameter_search_dist,
3912 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3918 set_partitioned_rice_(
3923 rice_parameter_search_dist,
3925 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3931 FLAC__ASSERT(best_residual_bits != 0);
3934 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3935 best_residual_bits = residual_bits;
3936 best_parameters_index = !best_parameters_index;
3937 best_partitioned_rice->order = partition_order;
3943 * We are allowed to de-const the pointer based on our special knowledge;
3944 * it is const to the outside world.
3947 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3948 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3949 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3950 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3953 return best_residual_bits;
3956 void precompute_partition_info_sums_(
3957 const FLAC__uint32 abs_residual[],
3958 FLAC__uint64 abs_residual_partition_sums[],
3959 unsigned residual_samples,
3960 unsigned predictor_order,
3961 unsigned min_partition_order,
3962 unsigned max_partition_order
3965 int partition_order;
3966 unsigned from_partition, to_partition = 0;
3967 const unsigned blocksize = residual_samples + predictor_order;
3969 /* first do max_partition_order */
3970 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3971 FLAC__uint64 abs_residual_partition_sum;
3973 unsigned partition, partition_sample, partition_samples, residual_sample;
3974 const unsigned partitions = 1u << partition_order;
3975 const unsigned default_partition_samples = blocksize >> partition_order;
3977 FLAC__ASSERT(default_partition_samples > predictor_order);
3979 for(partition = residual_sample = 0; partition < partitions; partition++) {
3980 partition_samples = default_partition_samples;
3982 partition_samples -= predictor_order;
3983 abs_residual_partition_sum = 0;
3984 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3985 abs_r = abs_residual[residual_sample];
3986 abs_residual_partition_sum += abs_r;
3989 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
3991 to_partition = partitions;
3995 /* now merge partitions for lower orders */
3996 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3999 const unsigned partitions = 1u << partition_order;
4000 for(i = 0; i < partitions; i++) {
4001 s = abs_residual_partition_sums[from_partition];
4003 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
4010 void precompute_partition_info_escapes_(
4011 const FLAC__int32 residual[],
4012 unsigned raw_bits_per_partition[],
4013 unsigned residual_samples,
4014 unsigned predictor_order,
4015 unsigned min_partition_order,
4016 unsigned max_partition_order
4019 int partition_order;
4020 unsigned from_partition, to_partition = 0;
4021 const unsigned blocksize = residual_samples + predictor_order;
4023 /* first do max_partition_order */
4024 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
4025 FLAC__int32 r, residual_partition_min, residual_partition_max;
4026 unsigned silog2_min, silog2_max;
4027 unsigned partition, partition_sample, partition_samples, residual_sample;
4028 const unsigned partitions = 1u << partition_order;
4029 const unsigned default_partition_samples = blocksize >> partition_order;
4031 FLAC__ASSERT(default_partition_samples > predictor_order);
4033 for(partition = residual_sample = 0; partition < partitions; partition++) {
4034 partition_samples = default_partition_samples;
4036 partition_samples -= predictor_order;
4037 residual_partition_min = residual_partition_max = 0;
4038 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
4039 r = residual[residual_sample];
4040 if(r < residual_partition_min)
4041 residual_partition_min = r;
4042 else if(r > residual_partition_max)
4043 residual_partition_max = r;
4046 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
4047 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
4048 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
4050 to_partition = partitions;
4051 break; /*@@@ yuck, should remove the 'for' loop instead */
4054 /* now merge partitions for lower orders */
4055 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
4058 const unsigned partitions = 1u << partition_order;
4059 for(i = 0; i < partitions; i++) {
4060 m = raw_bits_per_partition[from_partition];
4062 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
4069 #ifdef VARIABLE_RICE_BITS
4070 #undef VARIABLE_RICE_BITS
4072 #ifndef DONT_ESTIMATE_RICE_BITS
4073 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
4076 #ifdef DONT_ESTIMATE_RICE_BITS
4077 FLAC__bool set_partitioned_rice_(
4078 const FLAC__uint32 abs_residual[],
4079 const FLAC__int32 residual[],
4080 const unsigned residual_samples,
4081 const unsigned predictor_order,
4082 const unsigned suggested_rice_parameter,
4083 const unsigned rice_parameter_search_dist,
4084 const unsigned partition_order,
4085 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4089 FLAC__bool set_partitioned_rice_(
4090 const FLAC__uint32 abs_residual[],
4091 const unsigned residual_samples,
4092 const unsigned predictor_order,
4093 const unsigned suggested_rice_parameter,
4094 const unsigned rice_parameter_search_dist,
4095 const unsigned partition_order,
4096 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4101 unsigned rice_parameter, partition_bits;
4102 #ifndef NO_RICE_SEARCH
4103 unsigned best_partition_bits;
4104 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4106 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
4107 unsigned *parameters;
4109 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4111 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4112 parameters = partitioned_rice_contents->parameters;
4114 if(partition_order == 0) {
4117 #ifndef NO_RICE_SEARCH
4118 if(rice_parameter_search_dist) {
4119 if(suggested_rice_parameter < rice_parameter_search_dist)
4120 min_rice_parameter = 0;
4122 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4123 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4124 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4125 #ifdef DEBUG_VERBOSE
4126 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4128 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4132 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
4134 best_partition_bits = 0xffffffff;
4135 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4137 #ifdef VARIABLE_RICE_BITS
4138 const unsigned rice_parameter_estimate = rice_parameter-1;
4139 partition_bits = (1+rice_parameter) * residual_samples;
4143 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4144 for(i = 0; i < residual_samples; i++) {
4145 #ifdef VARIABLE_RICE_BITS
4146 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
4148 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4151 #ifndef NO_RICE_SEARCH
4152 if(partition_bits < best_partition_bits) {
4153 best_rice_parameter = rice_parameter;
4154 best_partition_bits = partition_bits;
4158 parameters[0] = best_rice_parameter;
4159 bits_ += best_partition_bits;
4162 unsigned partition, residual_sample, save_residual_sample, partition_sample;
4163 unsigned partition_samples;
4164 FLAC__uint64 mean, k;
4165 const unsigned partitions = 1u << partition_order;
4166 for(partition = residual_sample = 0; partition < partitions; partition++) {
4167 partition_samples = (residual_samples+predictor_order) >> partition_order;
4168 if(partition == 0) {
4169 if(partition_samples <= predictor_order)
4172 partition_samples -= predictor_order;
4175 save_residual_sample = residual_sample;
4176 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
4177 mean += abs_residual[residual_sample];
4178 residual_sample = save_residual_sample;
4179 /* we are basically calculating the size in bits of the
4180 * average residual magnitude in the partition:
4181 * rice_parameter = floor(log2(mean/partition_samples))
4182 * 'mean' is not a good name for the variable, it is
4183 * actually the sum of magnitudes of all residual values
4184 * in the partition, so the actual mean is
4185 * mean/partition_samples
4187 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
4189 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4190 #ifdef DEBUG_VERBOSE
4191 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4193 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4196 #ifndef NO_RICE_SEARCH
4197 if(rice_parameter_search_dist) {
4198 if(rice_parameter < rice_parameter_search_dist)
4199 min_rice_parameter = 0;
4201 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4202 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4203 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4204 #ifdef DEBUG_VERBOSE
4205 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4207 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4211 min_rice_parameter = max_rice_parameter = rice_parameter;
4213 best_partition_bits = 0xffffffff;
4214 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4216 #ifdef VARIABLE_RICE_BITS
4217 const unsigned rice_parameter_estimate = rice_parameter-1;
4218 partition_bits = (1+rice_parameter) * partition_samples;
4222 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4223 save_residual_sample = residual_sample;
4224 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
4225 #ifdef VARIABLE_RICE_BITS
4226 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
4228 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4231 #ifndef NO_RICE_SEARCH
4232 if(rice_parameter != max_rice_parameter)
4233 residual_sample = save_residual_sample;
4234 if(partition_bits < best_partition_bits) {
4235 best_rice_parameter = rice_parameter;
4236 best_partition_bits = partition_bits;
4240 parameters[partition] = best_rice_parameter;
4241 bits_ += best_partition_bits;
4249 #ifdef DONT_ESTIMATE_RICE_BITS
4250 FLAC__bool set_partitioned_rice_with_precompute_(
4251 const FLAC__int32 residual[],
4252 const FLAC__uint64 abs_residual_partition_sums[],
4253 const unsigned raw_bits_per_partition[],
4254 const unsigned residual_samples,
4255 const unsigned predictor_order,
4256 const unsigned suggested_rice_parameter,
4257 const unsigned rice_parameter_search_dist,
4258 const unsigned partition_order,
4259 const FLAC__bool search_for_escapes,
4260 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4264 FLAC__bool set_partitioned_rice_with_precompute_(
4265 const FLAC__uint32 abs_residual[],
4266 const FLAC__uint64 abs_residual_partition_sums[],
4267 const unsigned raw_bits_per_partition[],
4268 const unsigned residual_samples,
4269 const unsigned predictor_order,
4270 const unsigned suggested_rice_parameter,
4271 const unsigned rice_parameter_search_dist,
4272 const unsigned partition_order,
4273 const FLAC__bool search_for_escapes,
4274 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4279 unsigned rice_parameter, partition_bits;
4280 #ifndef NO_RICE_SEARCH
4281 unsigned best_partition_bits;
4282 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4285 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
4286 unsigned *parameters, *raw_bits;
4288 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4290 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4291 parameters = partitioned_rice_contents->parameters;
4292 raw_bits = partitioned_rice_contents->raw_bits;
4294 if(partition_order == 0) {
4297 #ifndef NO_RICE_SEARCH
4298 if(rice_parameter_search_dist) {
4299 if(suggested_rice_parameter < rice_parameter_search_dist)
4300 min_rice_parameter = 0;
4302 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4303 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4304 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4305 #ifdef DEBUG_VERBOSE
4306 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4308 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4312 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
4314 best_partition_bits = 0xffffffff;
4315 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4317 #ifdef VARIABLE_RICE_BITS
4318 const unsigned rice_parameter_estimate = rice_parameter-1;
4319 partition_bits = (1+rice_parameter) * residual_samples;
4323 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4324 for(i = 0; i < residual_samples; i++) {
4325 #ifdef VARIABLE_RICE_BITS
4326 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
4328 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
4331 #ifndef NO_RICE_SEARCH
4332 if(partition_bits < best_partition_bits) {
4333 best_rice_parameter = rice_parameter;
4334 best_partition_bits = partition_bits;
4338 if(search_for_escapes) {
4339 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;
4340 if(flat_bits <= best_partition_bits) {
4341 raw_bits[0] = raw_bits_per_partition[0];
4342 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4343 best_partition_bits = flat_bits;
4346 parameters[0] = best_rice_parameter;
4347 bits_ += best_partition_bits;
4350 unsigned partition, residual_sample, save_residual_sample, partition_sample;
4351 unsigned partition_samples;
4352 FLAC__uint64 mean, k;
4353 const unsigned partitions = 1u << partition_order;
4354 for(partition = residual_sample = 0; partition < partitions; partition++) {
4355 partition_samples = (residual_samples+predictor_order) >> partition_order;
4356 if(partition == 0) {
4357 if(partition_samples <= predictor_order)
4360 partition_samples -= predictor_order;
4362 mean = abs_residual_partition_sums[partition];
4363 /* we are basically calculating the size in bits of the
4364 * average residual magnitude in the partition:
4365 * rice_parameter = floor(log2(mean/partition_samples))
4366 * 'mean' is not a good name for the variable, it is
4367 * actually the sum of magnitudes of all residual values
4368 * in the partition, so the actual mean is
4369 * mean/partition_samples
4371 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
4373 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4374 #ifdef DEBUG_VERBOSE
4375 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4377 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4380 #ifndef NO_RICE_SEARCH
4381 if(rice_parameter_search_dist) {
4382 if(rice_parameter < rice_parameter_search_dist)
4383 min_rice_parameter = 0;
4385 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4386 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4387 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4388 #ifdef DEBUG_VERBOSE
4389 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4391 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4395 min_rice_parameter = max_rice_parameter = rice_parameter;
4397 best_partition_bits = 0xffffffff;
4398 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4400 #ifdef VARIABLE_RICE_BITS
4401 const unsigned rice_parameter_estimate = rice_parameter-1;
4402 partition_bits = (1+rice_parameter) * partition_samples;
4406 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4407 save_residual_sample = residual_sample;
4408 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
4409 #ifdef VARIABLE_RICE_BITS
4410 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
4412 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
4415 #ifndef NO_RICE_SEARCH
4416 if(rice_parameter != max_rice_parameter)
4417 residual_sample = save_residual_sample;
4418 if(partition_bits < best_partition_bits) {
4419 best_rice_parameter = rice_parameter;
4420 best_partition_bits = partition_bits;
4424 if(search_for_escapes) {
4425 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;
4426 if(flat_bits <= best_partition_bits) {
4427 raw_bits[partition] = raw_bits_per_partition[partition];
4428 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4429 best_partition_bits = flat_bits;
4432 parameters[partition] = best_rice_parameter;
4433 bits_ += best_partition_bits;
4441 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
4446 for(i = 0; i < samples && !(x&1); i++)
4453 for(shift = 0; !(x&1); shift++)
4458 for(i = 0; i < samples; i++)
4459 signal[i] >>= shift;
4465 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4469 for(channel = 0; channel < channels; channel++)
4470 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4472 fifo->tail += wide_samples;
4474 FLAC__ASSERT(fifo->tail <= fifo->size);
4477 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4480 unsigned sample, wide_sample;
4481 unsigned tail = fifo->tail;
4483 sample = input_offset * channels;
4484 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4485 for(channel = 0; channel < channels; channel++)
4486 fifo->data[channel][tail] = input[sample++];
4491 FLAC__ASSERT(fifo->tail <= fifo->size);
4494 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4496 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4497 const size_t encoded_bytes = encoder->private_->verify.output.bytes;
4500 if(encoder->private_->verify.needs_magic_hack) {
4501 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4502 *bytes = FLAC__STREAM_SYNC_LENGTH;
4503 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4504 encoder->private_->verify.needs_magic_hack = false;
4507 if(encoded_bytes == 0) {
4509 * If we get here, a FIFO underflow has occurred,
4510 * which means there is a bug somewhere.
4513 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4515 else if(encoded_bytes < *bytes)
4516 *bytes = encoded_bytes;
4517 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4518 encoder->private_->verify.output.data += *bytes;
4519 encoder->private_->verify.output.bytes -= *bytes;
4522 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4525 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4527 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4529 const unsigned channels = frame->header.channels;
4530 const unsigned blocksize = frame->header.blocksize;
4531 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4535 for(channel = 0; channel < channels; channel++) {
4536 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4537 unsigned i, sample = 0;
4538 FLAC__int32 expect = 0, got = 0;
4540 for(i = 0; i < blocksize; i++) {
4541 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4543 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4544 got = (FLAC__int32)buffer[channel][i];
4548 FLAC__ASSERT(i < blocksize);
4549 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4550 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
4551 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
4552 encoder->private_->verify.error_stats.channel = channel;
4553 encoder->private_->verify.error_stats.sample = sample;
4554 encoder->private_->verify.error_stats.expected = expect;
4555 encoder->private_->verify.error_stats.got = got;
4556 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4557 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4560 /* dequeue the frame from the fifo */
4561 encoder->private_->verify.input_fifo.tail -= blocksize;
4562 FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
4563 for(channel = 0; channel < channels; channel++)
4564 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]));
4565 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4568 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4570 (void)decoder, (void)metadata, (void)client_data;
4573 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4575 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4576 (void)decoder, (void)status;
4577 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4580 FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4584 *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
4586 if (feof(encoder->private_->file))
4587 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4588 else if (ferror(encoder->private_->file))
4589 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4591 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4594 FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4598 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4599 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4601 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4604 FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4610 offset = ftello(encoder->private_->file);
4613 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4616 *absolute_byte_offset = (FLAC__uint64)offset;
4617 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4621 #ifdef FLAC__VALGRIND_TESTING
4622 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4624 size_t ret = fwrite(ptr, size, nmemb, stream);
4630 #define local__fwrite fwrite
4633 FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
4635 (void)client_data, (void)current_frame;
4637 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
4638 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4640 /* We would like to be able to use 'samples > 0' in the
4641 * clause here but currently because of the nature of our
4642 * Ogg writing implementation, 'samples' is always 0 (see
4643 * ogg_encoder_aspect.c). The downside is extra progress
4646 encoder->private_->is_ogg? true :
4651 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4652 * because at this point in the callback chain, the stats
4653 * have not been updated. Only after we return and control
4654 * gets back to write_frame_() are the stats updated
4656 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);
4658 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4661 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4665 * This will forcibly set stdout to binary mode (for OSes that require it)
4667 FILE *get_binary_stdout_()
4669 /* if something breaks here it is probably due to the presence or
4670 * absence of an underscore before the identifiers 'setmode',
4671 * 'fileno', and/or 'O_BINARY'; check your system header files.
4673 #if defined _MSC_VER || defined __MINGW32__
4674 _setmode(_fileno(stdout), _O_BINARY);
4675 #elif defined __CYGWIN__
4676 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4677 setmode(_fileno(stdout), _O_BINARY);
4678 #elif defined __EMX__
4679 setmode(fileno(stdout), O_BINARY);