1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #if defined _MSC_VER || defined __MINGW32__
37 #include <io.h> /* for _setmode() */
38 #include <fcntl.h> /* for _O_BINARY */
40 #if defined __CYGWIN__ || defined __EMX__
41 #include <io.h> /* for setmode(), O_BINARY */
42 #include <fcntl.h> /* for _O_BINARY */
46 #include <stdlib.h> /* for malloc() */
47 #include <string.h> /* for memcpy() */
48 #include <sys/types.h> /* for off_t */
49 #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
50 #if _MSC_VER <= 1200 || defined __BORLANDC__ /* @@@ [2G limit] */
55 #include "FLAC/assert.h"
56 #include "FLAC/stream_decoder.h"
57 #include "protected/stream_encoder.h"
58 #include "private/bitbuffer.h"
59 #include "private/bitmath.h"
60 #include "private/crc.h"
61 #include "private/cpu.h"
62 #include "private/fixed.h"
63 #include "private/format.h"
64 #include "private/lpc.h"
65 #include "private/md5.h"
66 #include "private/memory.h"
68 #include "private/ogg_helper.h"
69 #include "private/ogg_mapping.h"
71 #include "private/stream_encoder_framing.h"
72 #include "private/window.h"
77 #define min(x,y) ((x)<(y)?(x):(y))
82 #define max(x,y) ((x)>(y)?(x):(y))
84 /* Exact Rice codeword length calculation is off by default. The simple
85 * (and fast) estimation (of how many bits a residual value will be
86 * encoded with) in this encoder is very good, almost always yielding
87 * compression within 0.1% of exact calculation.
89 #undef EXACT_RICE_BITS_CALCULATION
90 /* Rice parameter searching is off by default. The simple (and fast)
91 * parameter estimation in this encoder is very good, almost always
92 * yielding compression within 0.1% of the optimal parameters.
94 #undef ENABLE_RICE_PARAMETER_SEARCH
97 FLAC__int32 *data[FLAC__MAX_CHANNELS];
98 unsigned size; /* of each data[] in samples */
103 const FLAC__byte *data;
109 ENCODER_IN_MAGIC = 0,
110 ENCODER_IN_METADATA = 1,
114 static struct CompressionLevels {
115 FLAC__bool do_mid_side_stereo;
116 FLAC__bool loose_mid_side_stereo;
117 unsigned max_lpc_order;
118 unsigned qlp_coeff_precision;
119 FLAC__bool do_qlp_coeff_prec_search;
120 FLAC__bool do_escape_coding;
121 FLAC__bool do_exhaustive_model_search;
122 unsigned min_residual_partition_order;
123 unsigned max_residual_partition_order;
124 unsigned rice_parameter_search_dist;
125 } compression_levels_[] = {
126 { false, false, 0, 0, false, false, false, 2, 2, 0 },
127 { true , true , 0, 0, false, false, false, 2, 2, 0 },
128 { true , false, 0, 0, false, false, false, 0, 3, 0 },
129 { false, false, 6, 0, false, false, false, 3, 3, 0 },
130 { true , true , 8, 0, false, false, false, 3, 3, 0 },
131 { true , false, 8, 0, false, false, false, 3, 3, 0 },
132 { true , false, 8, 0, false, false, false, 0, 4, 0 },
133 { true , false, 8, 0, false, false, true , 0, 6, 0 },
134 { true , false, 12, 0, false, false, true , 0, 6, 0 }
138 /***********************************************************************
140 * Private class method prototypes
142 ***********************************************************************/
144 static void set_defaults_(FLAC__StreamEncoder *encoder);
145 static void free_(FLAC__StreamEncoder *encoder);
146 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize);
147 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block);
148 static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block);
149 static void update_metadata_(const FLAC__StreamEncoder *encoder);
151 static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
153 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block);
154 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);
156 static FLAC__bool process_subframe_(
157 FLAC__StreamEncoder *encoder,
158 unsigned min_partition_order,
159 unsigned max_partition_order,
160 FLAC__bool precompute_partition_sums,
161 const FLAC__FrameHeader *frame_header,
162 unsigned subframe_bps,
163 const FLAC__int32 integer_signal[],
164 #ifndef FLAC__INTEGER_ONLY_LIBRARY
165 const FLAC__real real_signal[],
167 FLAC__Subframe *subframe[2],
168 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
169 FLAC__int32 *residual[2],
170 unsigned *best_subframe,
174 static FLAC__bool add_subframe_(
175 FLAC__StreamEncoder *encoder,
177 unsigned subframe_bps,
178 const FLAC__Subframe *subframe,
179 FLAC__BitBuffer *frame
182 static unsigned evaluate_constant_subframe_(
183 FLAC__StreamEncoder *encoder,
184 const FLAC__int32 signal,
186 unsigned subframe_bps,
187 FLAC__Subframe *subframe
190 static unsigned evaluate_fixed_subframe_(
191 FLAC__StreamEncoder *encoder,
192 const FLAC__int32 signal[],
193 FLAC__int32 residual[],
194 FLAC__uint32 abs_residual[],
195 FLAC__uint64 abs_residual_partition_sums[],
196 unsigned raw_bits_per_partition[],
198 unsigned subframe_bps,
200 unsigned rice_parameter,
201 unsigned min_partition_order,
202 unsigned max_partition_order,
203 FLAC__bool precompute_partition_sums,
204 FLAC__bool do_escape_coding,
205 unsigned rice_parameter_search_dist,
206 FLAC__Subframe *subframe,
207 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
210 #ifndef FLAC__INTEGER_ONLY_LIBRARY
211 static unsigned evaluate_lpc_subframe_(
212 FLAC__StreamEncoder *encoder,
213 const FLAC__int32 signal[],
214 FLAC__int32 residual[],
215 FLAC__uint32 abs_residual[],
216 FLAC__uint64 abs_residual_partition_sums[],
217 unsigned raw_bits_per_partition[],
218 const FLAC__real lp_coeff[],
220 unsigned subframe_bps,
222 unsigned qlp_coeff_precision,
223 unsigned rice_parameter,
224 unsigned min_partition_order,
225 unsigned max_partition_order,
226 FLAC__bool precompute_partition_sums,
227 FLAC__bool do_escape_coding,
228 unsigned rice_parameter_search_dist,
229 FLAC__Subframe *subframe,
230 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
234 static unsigned evaluate_verbatim_subframe_(
235 FLAC__StreamEncoder *encoder,
236 const FLAC__int32 signal[],
238 unsigned subframe_bps,
239 FLAC__Subframe *subframe
242 static unsigned find_best_partition_order_(
243 struct FLAC__StreamEncoderPrivate *private_,
244 const FLAC__int32 residual[],
245 FLAC__uint32 abs_residual[],
246 FLAC__uint64 abs_residual_partition_sums[],
247 unsigned raw_bits_per_partition[],
248 unsigned residual_samples,
249 unsigned predictor_order,
250 unsigned rice_parameter,
251 unsigned min_partition_order,
252 unsigned max_partition_order,
253 FLAC__bool precompute_partition_sums,
254 FLAC__bool do_escape_coding,
255 unsigned rice_parameter_search_dist,
256 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
259 static void precompute_partition_info_sums_(
260 const FLAC__uint32 abs_residual[],
261 FLAC__uint64 abs_residual_partition_sums[],
262 unsigned residual_samples,
263 unsigned predictor_order,
264 unsigned min_partition_order,
265 unsigned max_partition_order
268 static void precompute_partition_info_escapes_(
269 const FLAC__int32 residual[],
270 unsigned raw_bits_per_partition[],
271 unsigned residual_samples,
272 unsigned predictor_order,
273 unsigned min_partition_order,
274 unsigned max_partition_order
277 static FLAC__bool set_partitioned_rice_(
278 const FLAC__uint32 abs_residual[],
279 #ifdef EXACT_RICE_BITS_CALCULATION
280 const FLAC__int32 residual[],
282 const unsigned residual_samples,
283 const unsigned predictor_order,
284 const unsigned suggested_rice_parameter,
285 const unsigned rice_parameter_search_dist,
286 const unsigned partition_order,
287 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
291 static FLAC__bool set_partitioned_rice_with_precompute_(
292 #ifdef EXACT_RICE_BITS_CALCULATION
293 const FLAC__int32 residual[],
295 const FLAC__uint32 abs_residual[],
297 const FLAC__uint64 abs_residual_partition_sums[],
298 const unsigned raw_bits_per_partition[],
299 const unsigned residual_samples,
300 const unsigned predictor_order,
301 const unsigned suggested_rice_parameter,
302 const unsigned rice_parameter_search_dist,
303 const unsigned partition_order,
304 const FLAC__bool search_for_escapes,
305 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
309 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
311 /* verify-related routines: */
312 static void append_to_verify_fifo_(
313 verify_input_fifo *fifo,
314 const FLAC__int32 * const input[],
315 unsigned input_offset,
317 unsigned wide_samples
320 static void append_to_verify_fifo_interleaved_(
321 verify_input_fifo *fifo,
322 const FLAC__int32 input[],
323 unsigned input_offset,
325 unsigned wide_samples
328 static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
329 static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
330 static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
331 static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
333 static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
334 static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
335 static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
336 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);
337 static FILE *get_binary_stdout_();
340 /***********************************************************************
344 ***********************************************************************/
346 typedef struct FLAC__StreamEncoderPrivate {
347 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
348 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
349 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
350 #ifndef FLAC__INTEGER_ONLY_LIBRARY
351 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
352 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
353 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
354 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
356 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
357 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
358 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
359 FLAC__int32 *residual_workspace_mid_side[2][2];
360 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
361 FLAC__Subframe subframe_workspace_mid_side[2][2];
362 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
363 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
364 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
365 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
366 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
367 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
368 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index (0 or 1) into 2nd dimension of the above workspaces */
369 unsigned best_subframe_mid_side[2];
370 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
371 unsigned best_subframe_bits_mid_side[2];
372 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
373 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
374 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
375 FLAC__BitBuffer *frame; /* the current frame being worked on */
376 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
377 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
378 FLAC__ChannelAssignment last_channel_assignment;
379 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
380 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
381 unsigned current_sample_number;
382 unsigned current_frame_number;
383 struct FLAC__MD5Context md5context;
384 FLAC__CPUInfo cpuinfo;
385 #ifndef FLAC__INTEGER_ONLY_LIBRARY
386 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
388 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
390 #ifndef FLAC__INTEGER_ONLY_LIBRARY
391 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
392 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[]);
393 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[]);
394 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[]);
396 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
397 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
398 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
399 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums could be a speed improvement */
400 FLAC__bool disable_constant_subframes;
401 FLAC__bool disable_fixed_subframes;
402 FLAC__bool disable_verbatim_subframes;
406 FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
407 FLAC__StreamEncoderSeekCallback seek_callback;
408 FLAC__StreamEncoderTellCallback tell_callback;
409 FLAC__StreamEncoderWriteCallback write_callback;
410 FLAC__StreamEncoderMetadataCallback metadata_callback;
411 FLAC__StreamEncoderProgressCallback progress_callback;
413 unsigned first_seekpoint_to_check;
414 FILE *file; /* only used when encoding to a file */
415 FLAC__uint64 bytes_written;
416 FLAC__uint64 samples_written;
417 unsigned frames_written;
418 unsigned total_frames_estimate;
419 /* unaligned (original) pointers to allocated data */
420 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
421 FLAC__int32 *integer_signal_mid_side_unaligned[2];
422 #ifndef FLAC__INTEGER_ONLY_LIBRARY
423 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
424 FLAC__real *real_signal_mid_side_unaligned[2];
425 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
426 FLAC__real *windowed_signal_unaligned;
428 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
429 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
430 FLAC__uint32 *abs_residual_unaligned;
431 FLAC__uint64 *abs_residual_partition_sums_unaligned;
432 unsigned *raw_bits_per_partition_unaligned;
434 * These fields have been moved here from private function local
435 * declarations merely to save stack space during encoding.
437 #ifndef FLAC__INTEGER_ONLY_LIBRARY
438 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
440 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
442 * The data for the verify section
445 FLAC__StreamDecoder *decoder;
446 EncoderStateHint state_hint;
447 FLAC__bool needs_magic_hack;
448 verify_input_fifo input_fifo;
449 verify_output output;
451 FLAC__uint64 absolute_sample;
452 unsigned frame_number;
455 FLAC__int32 expected;
459 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
460 } FLAC__StreamEncoderPrivate;
462 /***********************************************************************
464 * Public static class data
466 ***********************************************************************/
468 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
469 "FLAC__STREAM_ENCODER_OK",
470 "FLAC__STREAM_ENCODER_UNINITIALIZED",
471 "FLAC__STREAM_ENCODER_OGG_ERROR",
472 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
473 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
474 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
475 "FLAC__STREAM_ENCODER_IO_ERROR",
476 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
477 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
480 FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
481 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
482 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
483 "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
484 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
485 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
486 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
487 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
488 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
489 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
490 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
491 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
492 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
493 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
494 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
497 FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
498 "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
499 "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
500 "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
501 "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
504 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
505 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
506 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
509 FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
510 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
511 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
512 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
515 FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
516 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
517 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
518 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
521 /* Number of samples that will be overread to watch for end of stream. By
522 * 'overread', we mean that the FLAC__stream_encoder_process*() calls will
523 * always try to read blocksize+1 samples before encoding a block, so that
524 * even if the stream has a total sample count that is an integral multiple
525 * of the blocksize, we will still notice when we are encoding the last
526 * block. This is needed, for example, to correctly set the end-of-stream
527 * marker in Ogg FLAC.
529 * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
530 * not really any reason to change it.
532 static const unsigned OVERREAD_ = 1;
534 /***********************************************************************
536 * Class constructor/destructor
539 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
541 FLAC__StreamEncoder *encoder;
544 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
546 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
551 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
552 if(encoder->protected_ == 0) {
557 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
558 if(encoder->private_ == 0) {
559 free(encoder->protected_);
564 encoder->private_->frame = FLAC__bitbuffer_new();
565 if(encoder->private_->frame == 0) {
566 free(encoder->private_);
567 free(encoder->protected_);
572 encoder->private_->file = 0;
574 set_defaults_(encoder);
576 encoder->private_->is_being_deleted = false;
578 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
579 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
580 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
582 for(i = 0; i < 2; i++) {
583 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
584 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
586 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
587 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
588 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
590 for(i = 0; i < 2; i++) {
591 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
592 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
595 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
596 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
597 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
599 for(i = 0; i < 2; i++) {
600 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
601 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
603 for(i = 0; i < 2; i++)
604 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
606 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
611 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
615 FLAC__ASSERT(0 != encoder);
616 FLAC__ASSERT(0 != encoder->protected_);
617 FLAC__ASSERT(0 != encoder->private_);
618 FLAC__ASSERT(0 != encoder->private_->frame);
620 encoder->private_->is_being_deleted = true;
622 (void)FLAC__stream_encoder_finish(encoder);
624 if(0 != encoder->private_->verify.decoder)
625 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
627 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
628 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
629 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
631 for(i = 0; i < 2; i++) {
632 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
633 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
635 for(i = 0; i < 2; i++)
636 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
638 FLAC__bitbuffer_delete(encoder->private_->frame);
639 free(encoder->private_);
640 free(encoder->protected_);
644 /***********************************************************************
646 * Public class methods
648 ***********************************************************************/
650 static FLAC__StreamEncoderInitStatus init_stream_internal_(
651 FLAC__StreamEncoder *encoder,
652 FLAC__StreamEncoderReadCallback read_callback,
653 FLAC__StreamEncoderWriteCallback write_callback,
654 FLAC__StreamEncoderSeekCallback seek_callback,
655 FLAC__StreamEncoderTellCallback tell_callback,
656 FLAC__StreamEncoderMetadataCallback metadata_callback,
662 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
664 FLAC__ASSERT(0 != encoder);
666 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
667 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
671 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
674 if(0 == write_callback || (seek_callback && 0 == tell_callback))
675 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
677 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
678 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
680 if(encoder->protected_->channels != 2) {
681 encoder->protected_->do_mid_side_stereo = false;
682 encoder->protected_->loose_mid_side_stereo = false;
684 else if(!encoder->protected_->do_mid_side_stereo)
685 encoder->protected_->loose_mid_side_stereo = false;
687 if(encoder->protected_->bits_per_sample >= 32)
688 encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */
690 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
691 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
693 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
694 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
696 if(encoder->protected_->blocksize == 0) {
697 if(encoder->protected_->max_lpc_order == 0)
698 encoder->protected_->blocksize = 1152;
700 encoder->protected_->blocksize = 4608;
703 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
704 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
706 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
707 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
709 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
710 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
712 if(encoder->protected_->qlp_coeff_precision == 0) {
713 if(encoder->protected_->bits_per_sample < 16) {
714 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
715 /* @@@ until then we'll make a guess */
716 encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
718 else if(encoder->protected_->bits_per_sample == 16) {
719 if(encoder->protected_->blocksize <= 192)
720 encoder->protected_->qlp_coeff_precision = 7;
721 else if(encoder->protected_->blocksize <= 384)
722 encoder->protected_->qlp_coeff_precision = 8;
723 else if(encoder->protected_->blocksize <= 576)
724 encoder->protected_->qlp_coeff_precision = 9;
725 else if(encoder->protected_->blocksize <= 1152)
726 encoder->protected_->qlp_coeff_precision = 10;
727 else if(encoder->protected_->blocksize <= 2304)
728 encoder->protected_->qlp_coeff_precision = 11;
729 else if(encoder->protected_->blocksize <= 4608)
730 encoder->protected_->qlp_coeff_precision = 12;
732 encoder->protected_->qlp_coeff_precision = 13;
735 if(encoder->protected_->blocksize <= 384)
736 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
737 else if(encoder->protected_->blocksize <= 1152)
738 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
740 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
742 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
744 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
745 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
747 if(encoder->protected_->streamable_subset) {
749 encoder->protected_->blocksize != 192 &&
750 encoder->protected_->blocksize != 576 &&
751 encoder->protected_->blocksize != 1152 &&
752 encoder->protected_->blocksize != 2304 &&
753 encoder->protected_->blocksize != 4608 &&
754 encoder->protected_->blocksize != 256 &&
755 encoder->protected_->blocksize != 512 &&
756 encoder->protected_->blocksize != 1024 &&
757 encoder->protected_->blocksize != 2048 &&
758 encoder->protected_->blocksize != 4096 &&
759 encoder->protected_->blocksize != 8192 &&
760 encoder->protected_->blocksize != 16384
762 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
764 encoder->protected_->sample_rate != 8000 &&
765 encoder->protected_->sample_rate != 16000 &&
766 encoder->protected_->sample_rate != 22050 &&
767 encoder->protected_->sample_rate != 24000 &&
768 encoder->protected_->sample_rate != 32000 &&
769 encoder->protected_->sample_rate != 44100 &&
770 encoder->protected_->sample_rate != 48000 &&
771 encoder->protected_->sample_rate != 96000
773 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
775 encoder->protected_->bits_per_sample != 8 &&
776 encoder->protected_->bits_per_sample != 12 &&
777 encoder->protected_->bits_per_sample != 16 &&
778 encoder->protected_->bits_per_sample != 20 &&
779 encoder->protected_->bits_per_sample != 24
781 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
782 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
783 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
785 encoder->protected_->sample_rate <= 48000 &&
787 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
788 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
791 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
795 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
796 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
797 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
798 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
801 /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
802 if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
804 for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
805 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
806 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
808 encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
809 encoder->protected_->metadata[0] = vc;
815 /* keep track of any SEEKTABLE block */
816 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
818 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
819 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
820 encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
821 break; /* take only the first one */
826 /* validate metadata */
827 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
828 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
829 metadata_has_seektable = false;
830 metadata_has_vorbis_comment = false;
831 metadata_picture_has_type1 = false;
832 metadata_picture_has_type2 = false;
833 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
834 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
835 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
836 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
837 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
838 if(metadata_has_seektable) /* only one is allowed */
839 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
840 metadata_has_seektable = true;
841 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
842 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
844 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
845 if(metadata_has_vorbis_comment) /* only one is allowed */
846 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
847 metadata_has_vorbis_comment = true;
849 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
850 if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
851 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
853 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
854 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
855 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
856 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
857 if(metadata_picture_has_type1) /* there should only be 1 per stream */
858 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
859 metadata_picture_has_type1 = true;
860 /* standard icon must be 32x32 pixel PNG */
862 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
864 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
865 m->data.picture.width != 32 ||
866 m->data.picture.height != 32
869 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
871 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
872 if(metadata_picture_has_type2) /* there should only be 1 per stream */
873 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
874 metadata_picture_has_type2 = true;
879 encoder->private_->input_capacity = 0;
880 for(i = 0; i < encoder->protected_->channels; i++) {
881 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
882 #ifndef FLAC__INTEGER_ONLY_LIBRARY
883 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
886 for(i = 0; i < 2; i++) {
887 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
888 #ifndef FLAC__INTEGER_ONLY_LIBRARY
889 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
892 #ifndef FLAC__INTEGER_ONLY_LIBRARY
893 for(i = 0; i < encoder->protected_->num_apodizations; i++)
894 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
895 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
897 for(i = 0; i < encoder->protected_->channels; i++) {
898 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
899 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
900 encoder->private_->best_subframe[i] = 0;
902 for(i = 0; i < 2; i++) {
903 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
904 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
905 encoder->private_->best_subframe_mid_side[i] = 0;
907 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
908 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
909 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
910 #ifndef FLAC__INTEGER_ONLY_LIBRARY
911 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
913 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
914 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply÷ by hand */
915 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
916 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
917 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
918 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
919 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);
921 if(encoder->private_->loose_mid_side_stereo_frames == 0)
922 encoder->private_->loose_mid_side_stereo_frames = 1;
923 encoder->private_->loose_mid_side_stereo_frame_count = 0;
924 encoder->private_->current_sample_number = 0;
925 encoder->private_->current_frame_number = 0;
927 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
928 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? */
929 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
932 * get the CPU info and set the function pointers
934 FLAC__cpu_info(&encoder->private_->cpuinfo);
935 /* first default to the non-asm routines */
936 #ifndef FLAC__INTEGER_ONLY_LIBRARY
937 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
939 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
940 #ifndef FLAC__INTEGER_ONLY_LIBRARY
941 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
942 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
943 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
945 /* now override with asm where appropriate */
946 #ifndef FLAC__INTEGER_ONLY_LIBRARY
947 # ifndef FLAC__NO_ASM
948 if(encoder->private_->cpuinfo.use_asm) {
949 # ifdef FLAC__CPU_IA32
950 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
951 # ifdef FLAC__HAS_NASM
953 if(encoder->private_->cpuinfo.data.ia32.sse) {
954 if(encoder->protected_->max_lpc_order < 4)
955 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
956 else if(encoder->protected_->max_lpc_order < 8)
957 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
958 else if(encoder->protected_->max_lpc_order < 12)
959 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
961 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
964 # endif /* FLAC__SSE_OS */
965 if(encoder->private_->cpuinfo.data.ia32._3dnow)
966 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
968 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
969 if(encoder->private_->cpuinfo.data.ia32.mmx) {
970 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
971 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
974 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
975 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
977 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
978 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
979 # endif /* FLAC__HAS_NASM */
980 # endif /* FLAC__CPU_IA32 */
982 # endif /* !FLAC__NO_ASM */
983 #endif /* !FLAC__INTEGER_ONLY_LIBRARY */
984 /* finally override based on wide-ness if necessary */
985 if(encoder->private_->use_wide_by_block) {
986 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
989 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
990 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
992 /* set state to OK; from here on, errors are fatal and we'll override the state then */
993 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
996 encoder->private_->is_ogg = is_ogg;
997 if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
998 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
999 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1003 encoder->private_->read_callback = read_callback;
1004 encoder->private_->write_callback = write_callback;
1005 encoder->private_->seek_callback = seek_callback;
1006 encoder->private_->tell_callback = tell_callback;
1007 encoder->private_->metadata_callback = metadata_callback;
1008 encoder->private_->client_data = client_data;
1010 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
1011 /* the above function sets the state for us in case of an error */
1012 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1015 if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
1016 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1017 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1021 * Set up the verify stuff if necessary
1023 if(encoder->protected_->verify) {
1025 * First, set up the fifo which will hold the
1026 * original signal to compare against
1028 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
1029 for(i = 0; i < encoder->protected_->channels; i++) {
1030 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
1031 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1032 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1035 encoder->private_->verify.input_fifo.tail = 0;
1038 * Now set up a stream decoder for verification
1040 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
1041 if(0 == encoder->private_->verify.decoder) {
1042 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1043 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1046 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) {
1047 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1048 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1051 encoder->private_->verify.error_stats.absolute_sample = 0;
1052 encoder->private_->verify.error_stats.frame_number = 0;
1053 encoder->private_->verify.error_stats.channel = 0;
1054 encoder->private_->verify.error_stats.sample = 0;
1055 encoder->private_->verify.error_stats.expected = 0;
1056 encoder->private_->verify.error_stats.got = 0;
1059 * These must be done before we write any metadata, because that
1060 * calls the write_callback, which uses these values.
1062 encoder->private_->first_seekpoint_to_check = 0;
1063 encoder->private_->samples_written = 0;
1064 encoder->protected_->streaminfo_offset = 0;
1065 encoder->protected_->seektable_offset = 0;
1066 encoder->protected_->audio_offset = 0;
1069 * write the stream header
1071 if(encoder->protected_->verify)
1072 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
1073 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
1074 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1075 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1077 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1078 /* the above function sets the state for us in case of an error */
1079 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1083 * write the STREAMINFO metadata block
1085 if(encoder->protected_->verify)
1086 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
1087 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1088 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1089 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1090 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1091 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1092 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1093 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1094 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1095 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1096 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1097 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1098 memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
1099 FLAC__MD5Init(&encoder->private_->md5context);
1100 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1101 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1102 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1104 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1105 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1106 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1108 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1109 /* the above function sets the state for us in case of an error */
1110 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1114 * Now that the STREAMINFO block is written, we can init this to an
1115 * absurdly-high value...
1117 encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
1118 /* ... and clear this to 0 */
1119 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
1122 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1123 * if not, we will write an empty one (FLAC__add_metadata_block()
1124 * automatically supplies the vendor string).
1126 * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1127 * the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1128 * true it will have already insured that the metadata list is properly
1131 if(!metadata_has_vorbis_comment) {
1132 FLAC__StreamMetadata vorbis_comment;
1133 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1134 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1135 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1136 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1137 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1138 vorbis_comment.data.vorbis_comment.num_comments = 0;
1139 vorbis_comment.data.vorbis_comment.comments = 0;
1140 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1141 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1142 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1144 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1145 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1146 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1148 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1149 /* the above function sets the state for us in case of an error */
1150 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1155 * write the user's metadata blocks
1157 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1158 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
1159 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1160 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1161 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1163 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1164 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1165 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1167 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1168 /* the above function sets the state for us in case of an error */
1169 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1173 /* now that all the metadata is written, we save the stream offset */
1174 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 */
1175 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1176 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1179 if(encoder->protected_->verify)
1180 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1182 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1185 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1186 FLAC__StreamEncoder *encoder,
1187 FLAC__StreamEncoderWriteCallback write_callback,
1188 FLAC__StreamEncoderSeekCallback seek_callback,
1189 FLAC__StreamEncoderTellCallback tell_callback,
1190 FLAC__StreamEncoderMetadataCallback metadata_callback,
1194 return init_stream_internal_(
1196 /*read_callback=*/0,
1206 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1207 FLAC__StreamEncoder *encoder,
1208 FLAC__StreamEncoderReadCallback read_callback,
1209 FLAC__StreamEncoderWriteCallback write_callback,
1210 FLAC__StreamEncoderSeekCallback seek_callback,
1211 FLAC__StreamEncoderTellCallback tell_callback,
1212 FLAC__StreamEncoderMetadataCallback metadata_callback,
1216 return init_stream_internal_(
1228 static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1229 FLAC__StreamEncoder *encoder,
1231 FLAC__StreamEncoderProgressCallback progress_callback,
1236 FLAC__StreamEncoderInitStatus init_status;
1238 FLAC__ASSERT(0 != encoder);
1239 FLAC__ASSERT(0 != file);
1241 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1242 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1244 /* double protection */
1246 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1247 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1251 * To make sure that our file does not go unclosed after an error, we
1252 * must assign the FILE pointer before any further error can occur in
1256 file = get_binary_stdout_(); /* just to be safe */
1258 encoder->private_->file = file;
1260 encoder->private_->progress_callback = progress_callback;
1261 encoder->private_->bytes_written = 0;
1262 encoder->private_->samples_written = 0;
1263 encoder->private_->frames_written = 0;
1265 init_status = init_stream_internal_(
1267 encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0,
1268 file_write_callback_,
1269 encoder->private_->file == stdout? 0 : file_seek_callback_,
1270 encoder->private_->file == stdout? 0 : file_tell_callback_,
1271 /*metadata_callback=*/0,
1275 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1276 /* the above function sets the state for us in case of an error */
1281 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1283 FLAC__ASSERT(blocksize != 0);
1284 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1290 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1291 FLAC__StreamEncoder *encoder,
1293 FLAC__StreamEncoderProgressCallback progress_callback,
1297 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1300 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1301 FLAC__StreamEncoder *encoder,
1303 FLAC__StreamEncoderProgressCallback progress_callback,
1307 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1310 static FLAC__StreamEncoderInitStatus init_file_internal_(
1311 FLAC__StreamEncoder *encoder,
1312 const char *filename,
1313 FLAC__StreamEncoderProgressCallback progress_callback,
1320 FLAC__ASSERT(0 != encoder);
1323 * To make sure that our file does not go unclosed after an error, we
1324 * have to do the same entrance checks here that are later performed
1325 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1327 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1328 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1330 file = filename? fopen(filename, "w+b") : stdout;
1333 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1334 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1337 return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1340 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1341 FLAC__StreamEncoder *encoder,
1342 const char *filename,
1343 FLAC__StreamEncoderProgressCallback progress_callback,
1347 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1350 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1351 FLAC__StreamEncoder *encoder,
1352 const char *filename,
1353 FLAC__StreamEncoderProgressCallback progress_callback,
1357 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
1360 FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
1362 FLAC__bool error = false;
1364 FLAC__ASSERT(0 != encoder);
1365 FLAC__ASSERT(0 != encoder->private_);
1366 FLAC__ASSERT(0 != encoder->protected_);
1368 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
1371 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
1372 if(encoder->private_->current_sample_number != 0) {
1373 const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
1374 encoder->protected_->blocksize = encoder->private_->current_sample_number;
1375 if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
1380 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
1382 if(!encoder->private_->is_being_deleted) {
1383 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
1384 if(encoder->private_->seek_callback) {
1386 if(encoder->private_->is_ogg)
1387 update_ogg_metadata_(encoder);
1390 update_metadata_(encoder);
1392 /* check if an error occurred while updating metadata */
1393 if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
1396 if(encoder->private_->metadata_callback)
1397 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1400 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
1402 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1407 if(0 != encoder->private_->file) {
1408 if(encoder->private_->file != stdout)
1409 fclose(encoder->private_->file);
1410 encoder->private_->file = 0;
1414 if(encoder->private_->is_ogg)
1415 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1419 set_defaults_(encoder);
1422 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
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)
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);
1515 ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
1516 /* but it's too hard to specify the string in a locale-specific way */
1518 encoder->protected_->num_apodizations = 1;
1519 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1520 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1522 ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1523 ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1524 ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1525 ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1526 ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1527 ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1528 ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1529 ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1533 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1535 FLAC__ASSERT(0 != encoder);
1536 FLAC__ASSERT(0 != encoder->private_);
1537 FLAC__ASSERT(0 != encoder->protected_);
1538 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1540 encoder->protected_->blocksize = value;
1544 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1546 FLAC__ASSERT(0 != encoder);
1547 FLAC__ASSERT(0 != encoder->private_);
1548 FLAC__ASSERT(0 != encoder->protected_);
1549 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1551 encoder->protected_->do_mid_side_stereo = value;
1555 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1557 FLAC__ASSERT(0 != encoder);
1558 FLAC__ASSERT(0 != encoder->private_);
1559 FLAC__ASSERT(0 != encoder->protected_);
1560 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1562 encoder->protected_->loose_mid_side_stereo = value;
1566 FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1568 FLAC__ASSERT(0 != encoder);
1569 FLAC__ASSERT(0 != encoder->private_);
1570 FLAC__ASSERT(0 != encoder->protected_);
1571 FLAC__ASSERT(0 != specification);
1572 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1574 #ifdef FLAC__INTEGER_ONLY_LIBRARY
1575 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1577 encoder->protected_->num_apodizations = 0;
1579 const char *s = strchr(specification, ';');
1580 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1581 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1582 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1583 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1584 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1585 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1586 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1587 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1588 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1589 else if(n==6 && 0 == strncmp("connes" , specification, n))
1590 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1591 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1592 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1593 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1594 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1595 if (stddev > 0.0 && stddev <= 0.5) {
1596 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1597 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1600 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1601 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1602 else if(n==4 && 0 == strncmp("hann" , specification, n))
1603 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1604 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1605 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1606 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1607 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1608 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1609 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1610 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1611 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1612 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1613 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1614 if (p >= 0.0 && p <= 1.0) {
1615 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1616 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1619 else if(n==5 && 0 == strncmp("welch" , specification, n))
1620 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1621 if (encoder->protected_->num_apodizations == 32)
1624 specification = s+1;
1628 if(encoder->protected_->num_apodizations == 0) {
1629 encoder->protected_->num_apodizations = 1;
1630 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1631 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1637 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1639 FLAC__ASSERT(0 != encoder);
1640 FLAC__ASSERT(0 != encoder->private_);
1641 FLAC__ASSERT(0 != encoder->protected_);
1642 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1644 encoder->protected_->max_lpc_order = value;
1648 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1650 FLAC__ASSERT(0 != encoder);
1651 FLAC__ASSERT(0 != encoder->private_);
1652 FLAC__ASSERT(0 != encoder->protected_);
1653 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1655 encoder->protected_->qlp_coeff_precision = value;
1659 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1661 FLAC__ASSERT(0 != encoder);
1662 FLAC__ASSERT(0 != encoder->private_);
1663 FLAC__ASSERT(0 != encoder->protected_);
1664 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1666 encoder->protected_->do_qlp_coeff_prec_search = value;
1670 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1672 FLAC__ASSERT(0 != encoder);
1673 FLAC__ASSERT(0 != encoder->private_);
1674 FLAC__ASSERT(0 != encoder->protected_);
1675 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1678 /*@@@ deprecated: */
1679 encoder->protected_->do_escape_coding = value;
1686 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1688 FLAC__ASSERT(0 != encoder);
1689 FLAC__ASSERT(0 != encoder->private_);
1690 FLAC__ASSERT(0 != encoder->protected_);
1691 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1693 encoder->protected_->do_exhaustive_model_search = value;
1697 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1699 FLAC__ASSERT(0 != encoder);
1700 FLAC__ASSERT(0 != encoder->private_);
1701 FLAC__ASSERT(0 != encoder->protected_);
1702 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1704 encoder->protected_->min_residual_partition_order = value;
1708 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1710 FLAC__ASSERT(0 != encoder);
1711 FLAC__ASSERT(0 != encoder->private_);
1712 FLAC__ASSERT(0 != encoder->protected_);
1713 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1715 encoder->protected_->max_residual_partition_order = value;
1719 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1721 FLAC__ASSERT(0 != encoder);
1722 FLAC__ASSERT(0 != encoder->private_);
1723 FLAC__ASSERT(0 != encoder->protected_);
1724 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1727 /*@@@ deprecated: */
1728 encoder->protected_->rice_parameter_search_dist = value;
1735 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1737 FLAC__ASSERT(0 != encoder);
1738 FLAC__ASSERT(0 != encoder->private_);
1739 FLAC__ASSERT(0 != encoder->protected_);
1740 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1742 encoder->protected_->total_samples_estimate = value;
1746 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1748 FLAC__ASSERT(0 != encoder);
1749 FLAC__ASSERT(0 != encoder->private_);
1750 FLAC__ASSERT(0 != encoder->protected_);
1751 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1753 encoder->protected_->metadata = metadata;
1754 encoder->protected_->num_metadata_blocks = num_blocks;
1756 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1763 * These three functions are not static, but not publically exposed in
1764 * include/FLAC/ either. They are used by the test suite.
1766 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1768 FLAC__ASSERT(0 != encoder);
1769 FLAC__ASSERT(0 != encoder->private_);
1770 FLAC__ASSERT(0 != encoder->protected_);
1771 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1773 encoder->private_->disable_constant_subframes = value;
1777 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1779 FLAC__ASSERT(0 != encoder);
1780 FLAC__ASSERT(0 != encoder->private_);
1781 FLAC__ASSERT(0 != encoder->protected_);
1782 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1784 encoder->private_->disable_fixed_subframes = value;
1788 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1790 FLAC__ASSERT(0 != encoder);
1791 FLAC__ASSERT(0 != encoder->private_);
1792 FLAC__ASSERT(0 != encoder->protected_);
1793 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1795 encoder->private_->disable_verbatim_subframes = value;
1799 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1801 FLAC__ASSERT(0 != encoder);
1802 FLAC__ASSERT(0 != encoder->private_);
1803 FLAC__ASSERT(0 != encoder->protected_);
1804 return encoder->protected_->state;
1807 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1809 FLAC__ASSERT(0 != encoder);
1810 FLAC__ASSERT(0 != encoder->private_);
1811 FLAC__ASSERT(0 != encoder->protected_);
1812 if(encoder->protected_->verify)
1813 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1815 return FLAC__STREAM_DECODER_UNINITIALIZED;
1818 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1820 FLAC__ASSERT(0 != encoder);
1821 FLAC__ASSERT(0 != encoder->private_);
1822 FLAC__ASSERT(0 != encoder->protected_);
1823 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1824 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1826 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
1829 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)
1831 FLAC__ASSERT(0 != encoder);
1832 FLAC__ASSERT(0 != encoder->private_);
1833 FLAC__ASSERT(0 != encoder->protected_);
1834 if(0 != absolute_sample)
1835 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1836 if(0 != frame_number)
1837 *frame_number = encoder->private_->verify.error_stats.frame_number;
1839 *channel = encoder->private_->verify.error_stats.channel;
1841 *sample = encoder->private_->verify.error_stats.sample;
1843 *expected = encoder->private_->verify.error_stats.expected;
1845 *got = encoder->private_->verify.error_stats.got;
1848 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(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_->verify;
1856 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(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_->streamable_subset;
1864 FLAC_API unsigned FLAC__stream_encoder_get_channels(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_->channels;
1872 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(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_->bits_per_sample;
1880 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(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_->sample_rate;
1888 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(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_->blocksize;
1896 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_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_->do_mid_side_stereo;
1904 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(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_->loose_mid_side_stereo;
1912 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(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_->max_lpc_order;
1920 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(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_->qlp_coeff_precision;
1928 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(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_qlp_coeff_prec_search;
1936 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(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_escape_coding;
1944 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(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_->do_exhaustive_model_search;
1952 FLAC_API unsigned FLAC__stream_encoder_get_min_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_->min_residual_partition_order;
1960 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(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_->max_residual_partition_order;
1968 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(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_->rice_parameter_search_dist;
1976 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1978 FLAC__ASSERT(0 != encoder);
1979 FLAC__ASSERT(0 != encoder->private_);
1980 FLAC__ASSERT(0 != encoder->protected_);
1981 return encoder->protected_->total_samples_estimate;
1984 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1986 unsigned i, j, channel;
1987 FLAC__int32 x, mid, side;
1988 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1990 FLAC__ASSERT(0 != encoder);
1991 FLAC__ASSERT(0 != encoder->private_);
1992 FLAC__ASSERT(0 != encoder->protected_);
1993 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1997 * we have several flavors of the same basic loop, optimized for
1998 * different conditions:
2000 if(encoder->protected_->max_lpc_order > 0) {
2001 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2003 * stereo coding: unroll channel loop
2004 * with LPC: calculate floating point version of signal
2007 if(encoder->protected_->verify)
2008 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2010 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2011 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2012 x = mid = side = buffer[0][j];
2013 encoder->private_->integer_signal[0][i] = x;
2014 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2015 encoder->private_->real_signal[0][i] = (FLAC__real)x;
2018 encoder->private_->integer_signal[1][i] = x;
2019 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2020 encoder->private_->real_signal[1][i] = (FLAC__real)x;
2024 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2025 encoder->private_->integer_signal_mid_side[1][i] = side;
2026 encoder->private_->integer_signal_mid_side[0][i] = mid;
2027 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2028 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2029 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
2031 encoder->private_->current_sample_number++;
2033 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2035 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2037 /* move unprocessed overread samples to beginnings of arrays */
2038 FLAC__ASSERT(i == blocksize+OVERREAD_);
2039 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2041 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2042 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2043 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2044 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2045 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2046 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2047 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2048 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2049 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2051 encoder->private_->current_sample_number = 1;
2053 } while(j < samples);
2057 * independent channel coding: buffer each channel in inner loop
2058 * with LPC: calculate floating point version of signal
2061 if(encoder->protected_->verify)
2062 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2064 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2065 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2066 for(channel = 0; channel < channels; channel++) {
2067 x = buffer[channel][j];
2068 encoder->private_->integer_signal[channel][i] = x;
2069 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2070 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2073 encoder->private_->current_sample_number++;
2075 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2077 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2079 /* move unprocessed overread samples to beginnings of arrays */
2080 FLAC__ASSERT(i == blocksize+OVERREAD_);
2081 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2083 for(channel = 0; channel < channels; channel++) {
2084 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2085 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2086 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2089 encoder->private_->current_sample_number = 1;
2091 } while(j < samples);
2095 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2097 * stereo coding: unroll channel loop
2098 * without LPC: no need to calculate floating point version of signal
2101 if(encoder->protected_->verify)
2102 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2104 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2105 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2106 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
2108 encoder->private_->integer_signal[1][i] = x;
2111 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2112 encoder->private_->integer_signal_mid_side[1][i] = side;
2113 encoder->private_->integer_signal_mid_side[0][i] = mid;
2114 encoder->private_->current_sample_number++;
2116 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2118 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2120 /* move unprocessed overread samples to beginnings of arrays */
2121 FLAC__ASSERT(i == blocksize+OVERREAD_);
2122 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2124 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2125 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2126 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2127 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2128 encoder->private_->current_sample_number = 1;
2130 } while(j < samples);
2134 * independent channel coding: buffer each channel in inner loop
2135 * without LPC: no need to calculate floating point version of signal
2138 if(encoder->protected_->verify)
2139 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2141 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2142 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2143 for(channel = 0; channel < channels; channel++)
2144 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
2145 encoder->private_->current_sample_number++;
2147 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2149 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2151 /* move unprocessed overread samples to beginnings of arrays */
2152 FLAC__ASSERT(i == blocksize+OVERREAD_);
2153 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2155 for(channel = 0; channel < channels; channel++)
2156 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2157 encoder->private_->current_sample_number = 1;
2159 } while(j < samples);
2166 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
2168 unsigned i, j, k, channel;
2169 FLAC__int32 x, mid, side;
2170 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
2172 FLAC__ASSERT(0 != encoder);
2173 FLAC__ASSERT(0 != encoder->private_);
2174 FLAC__ASSERT(0 != encoder->protected_);
2175 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2179 * we have several flavors of the same basic loop, optimized for
2180 * different conditions:
2182 if(encoder->protected_->max_lpc_order > 0) {
2183 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2185 * stereo coding: unroll channel loop
2186 * with LPC: calculate floating point version of signal
2189 if(encoder->protected_->verify)
2190 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2192 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2193 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2194 x = mid = side = buffer[k++];
2195 encoder->private_->integer_signal[0][i] = x;
2196 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2197 encoder->private_->real_signal[0][i] = (FLAC__real)x;
2200 encoder->private_->integer_signal[1][i] = x;
2201 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2202 encoder->private_->real_signal[1][i] = (FLAC__real)x;
2206 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2207 encoder->private_->integer_signal_mid_side[1][i] = side;
2208 encoder->private_->integer_signal_mid_side[0][i] = mid;
2209 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2210 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2211 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
2213 encoder->private_->current_sample_number++;
2215 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2217 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2219 /* move unprocessed overread samples to beginnings of arrays */
2220 FLAC__ASSERT(i == blocksize+OVERREAD_);
2221 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2223 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2224 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2225 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2226 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2227 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2228 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2229 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2230 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2231 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2233 encoder->private_->current_sample_number = 1;
2235 } while(j < samples);
2239 * independent channel coding: buffer each channel in inner loop
2240 * with LPC: calculate floating point version of signal
2243 if(encoder->protected_->verify)
2244 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2246 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2247 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2248 for(channel = 0; channel < channels; channel++) {
2250 encoder->private_->integer_signal[channel][i] = x;
2251 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2252 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2255 encoder->private_->current_sample_number++;
2257 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2259 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2261 /* move unprocessed overread samples to beginnings of arrays */
2262 FLAC__ASSERT(i == blocksize+OVERREAD_);
2263 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2265 for(channel = 0; channel < channels; channel++) {
2266 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2267 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2268 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2271 encoder->private_->current_sample_number = 1;
2273 } while(j < samples);
2277 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2279 * stereo coding: unroll channel loop
2280 * without LPC: no need to calculate floating point version of signal
2283 if(encoder->protected_->verify)
2284 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2286 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2287 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2288 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
2290 encoder->private_->integer_signal[1][i] = x;
2293 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2294 encoder->private_->integer_signal_mid_side[1][i] = side;
2295 encoder->private_->integer_signal_mid_side[0][i] = mid;
2296 encoder->private_->current_sample_number++;
2298 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2300 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2302 /* move unprocessed overread samples to beginnings of arrays */
2303 FLAC__ASSERT(i == blocksize+OVERREAD_);
2304 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2306 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2307 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2308 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2309 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2310 encoder->private_->current_sample_number = 1;
2312 } while(j < samples);
2316 * independent channel coding: buffer each channel in inner loop
2317 * without LPC: no need to calculate floating point version of signal
2320 if(encoder->protected_->verify)
2321 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
2323 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2324 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2325 for(channel = 0; channel < channels; channel++)
2326 encoder->private_->integer_signal[channel][i] = buffer[k++];
2327 encoder->private_->current_sample_number++;
2329 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2331 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2333 /* move unprocessed overread samples to beginnings of arrays */
2334 FLAC__ASSERT(i == blocksize+OVERREAD_);
2335 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2337 for(channel = 0; channel < channels; channel++)
2338 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2339 encoder->private_->current_sample_number = 1;
2341 } while(j < samples);
2348 /***********************************************************************
2350 * Private class methods
2352 ***********************************************************************/
2354 void set_defaults_(FLAC__StreamEncoder *encoder)
2356 FLAC__ASSERT(0 != encoder);
2358 #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2359 encoder->protected_->verify = true;
2361 encoder->protected_->verify = false;
2363 encoder->protected_->streamable_subset = true;
2364 encoder->protected_->do_mid_side_stereo = false;
2365 encoder->protected_->loose_mid_side_stereo = false;
2366 encoder->protected_->channels = 2;
2367 encoder->protected_->bits_per_sample = 16;
2368 encoder->protected_->sample_rate = 44100;
2369 encoder->protected_->blocksize = 0;
2370 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2371 encoder->protected_->num_apodizations = 1;
2372 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2373 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
2375 encoder->protected_->max_lpc_order = 0;
2376 encoder->protected_->qlp_coeff_precision = 0;
2377 encoder->protected_->do_qlp_coeff_prec_search = false;
2378 encoder->protected_->do_exhaustive_model_search = false;
2379 encoder->protected_->do_escape_coding = false;
2380 encoder->protected_->min_residual_partition_order = 0;
2381 encoder->protected_->max_residual_partition_order = 0;
2382 encoder->protected_->rice_parameter_search_dist = 0;
2383 encoder->protected_->total_samples_estimate = 0;
2384 encoder->protected_->metadata = 0;
2385 encoder->protected_->num_metadata_blocks = 0;
2387 encoder->private_->seek_table = 0;
2388 encoder->private_->disable_constant_subframes = false;
2389 encoder->private_->disable_fixed_subframes = false;
2390 encoder->private_->disable_verbatim_subframes = false;
2392 encoder->private_->is_ogg = false;
2394 encoder->private_->read_callback = 0;
2395 encoder->private_->write_callback = 0;
2396 encoder->private_->seek_callback = 0;
2397 encoder->private_->tell_callback = 0;
2398 encoder->private_->metadata_callback = 0;
2399 encoder->private_->progress_callback = 0;
2400 encoder->private_->client_data = 0;
2403 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2407 void free_(FLAC__StreamEncoder *encoder)
2409 unsigned i, channel;
2411 FLAC__ASSERT(0 != encoder);
2412 for(i = 0; i < encoder->protected_->channels; i++) {
2413 if(0 != encoder->private_->integer_signal_unaligned[i]) {
2414 free(encoder->private_->integer_signal_unaligned[i]);
2415 encoder->private_->integer_signal_unaligned[i] = 0;
2417 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2418 if(0 != encoder->private_->real_signal_unaligned[i]) {
2419 free(encoder->private_->real_signal_unaligned[i]);
2420 encoder->private_->real_signal_unaligned[i] = 0;
2424 for(i = 0; i < 2; i++) {
2425 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
2426 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2427 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2429 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2430 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
2431 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2432 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2436 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2437 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2438 if(0 != encoder->private_->window_unaligned[i]) {
2439 free(encoder->private_->window_unaligned[i]);
2440 encoder->private_->window_unaligned[i] = 0;
2443 if(0 != encoder->private_->windowed_signal_unaligned) {
2444 free(encoder->private_->windowed_signal_unaligned);
2445 encoder->private_->windowed_signal_unaligned = 0;
2448 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2449 for(i = 0; i < 2; i++) {
2450 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
2451 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2452 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2456 for(channel = 0; channel < 2; channel++) {
2457 for(i = 0; i < 2; i++) {
2458 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
2459 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2460 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2464 if(0 != encoder->private_->abs_residual_unaligned) {
2465 free(encoder->private_->abs_residual_unaligned);
2466 encoder->private_->abs_residual_unaligned = 0;
2468 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
2469 free(encoder->private_->abs_residual_partition_sums_unaligned);
2470 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2472 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
2473 free(encoder->private_->raw_bits_per_partition_unaligned);
2474 encoder->private_->raw_bits_per_partition_unaligned = 0;
2476 if(encoder->protected_->verify) {
2477 for(i = 0; i < encoder->protected_->channels; i++) {
2478 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2479 free(encoder->private_->verify.input_fifo.data[i]);
2480 encoder->private_->verify.input_fifo.data[i] = 0;
2484 FLAC__bitbuffer_free(encoder->private_->frame);
2487 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
2490 unsigned i, channel;
2492 FLAC__ASSERT(new_blocksize > 0);
2493 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2494 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
2496 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
2497 if(new_blocksize <= encoder->private_->input_capacity)
2502 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2503 * requires that the input arrays (in our case the integer signals)
2504 * have a buffer of up to 3 zeroes in front (at negative indices) for
2505 * alignment purposes; we use 4 in front to keep the data well-aligned.
2508 for(i = 0; ok && i < encoder->protected_->channels; i++) {
2509 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
2510 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2511 encoder->private_->integer_signal[i] += 4;
2512 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2513 if(encoder->protected_->max_lpc_order > 0)
2514 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
2517 for(i = 0; ok && i < 2; i++) {
2518 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]);
2519 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2520 encoder->private_->integer_signal_mid_side[i] += 4;
2521 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2522 if(encoder->protected_->max_lpc_order > 0)
2523 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]);
2526 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2527 if(ok && encoder->protected_->max_lpc_order > 0) {
2528 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2529 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2530 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
2533 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
2534 for(i = 0; ok && i < 2; i++) {
2535 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
2538 for(channel = 0; ok && channel < 2; channel++) {
2539 for(i = 0; ok && i < 2; i++) {
2540 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]);
2543 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_blocksize, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
2544 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 */
2545 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
2546 if(encoder->protected_->do_escape_coding)
2547 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
2549 /* now adjust the windows if the blocksize has changed */
2550 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2551 if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
2552 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2553 switch(encoder->protected_->apodizations[i].type) {
2554 case FLAC__APODIZATION_BARTLETT:
2555 FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
2557 case FLAC__APODIZATION_BARTLETT_HANN:
2558 FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
2560 case FLAC__APODIZATION_BLACKMAN:
2561 FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
2563 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2564 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
2566 case FLAC__APODIZATION_CONNES:
2567 FLAC__window_connes(encoder->private_->window[i], new_blocksize);
2569 case FLAC__APODIZATION_FLATTOP:
2570 FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
2572 case FLAC__APODIZATION_GAUSS:
2573 FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2575 case FLAC__APODIZATION_HAMMING:
2576 FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
2578 case FLAC__APODIZATION_HANN:
2579 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2581 case FLAC__APODIZATION_KAISER_BESSEL:
2582 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
2584 case FLAC__APODIZATION_NUTTALL:
2585 FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
2587 case FLAC__APODIZATION_RECTANGLE:
2588 FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
2590 case FLAC__APODIZATION_TRIANGLE:
2591 FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
2593 case FLAC__APODIZATION_TUKEY:
2594 FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
2596 case FLAC__APODIZATION_WELCH:
2597 FLAC__window_welch(encoder->private_->window[i], new_blocksize);
2601 /* double protection */
2602 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2610 encoder->private_->input_capacity = new_blocksize;
2612 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2617 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
2619 const FLAC__byte *buffer;
2622 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2624 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2626 if(encoder->protected_->verify) {
2627 encoder->private_->verify.output.data = buffer;
2628 encoder->private_->verify.output.bytes = bytes;
2629 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2630 encoder->private_->verify.needs_magic_hack = true;
2633 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2634 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2635 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2636 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2642 if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2643 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2644 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2648 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2651 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2652 encoder->private_->streaminfo.data.stream_info.max_framesize = max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
2658 FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block)
2660 FLAC__StreamEncoderWriteStatus status;
2661 FLAC__uint64 output_position = 0;
2663 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2664 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2665 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2666 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2670 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2673 FLAC__MetadataType type = (buffer[0] & 0x7f);
2674 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2675 encoder->protected_->streaminfo_offset = output_position;
2676 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2677 encoder->protected_->seektable_offset = output_position;
2681 * Mark the current seek point if hit (if audio_offset == 0 that
2682 * means we're still writing metadata and haven't hit the first
2685 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2686 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2687 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2688 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2689 FLAC__uint64 test_sample;
2691 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2692 test_sample = encoder->private_->seek_table->points[i].sample_number;
2693 if(test_sample > frame_last_sample) {
2696 else if(test_sample >= frame_first_sample) {
2697 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2698 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2699 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2700 encoder->private_->first_seekpoint_to_check++;
2701 /* DO NOT: "break;" and here's why:
2702 * The seektable template may contain more than one target
2703 * sample for any given frame; we will keep looping, generating
2704 * duplicate seekpoints for them, and we'll clean it up later,
2705 * just before writing the seektable back to the metadata.
2709 encoder->private_->first_seekpoint_to_check++;
2715 if(encoder->private_->is_ogg) {
2716 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2717 &encoder->protected_->ogg_encoder_aspect,
2721 encoder->private_->current_frame_number,
2723 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2725 encoder->private_->client_data
2730 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2732 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2733 encoder->private_->bytes_written += bytes;
2734 encoder->private_->samples_written += samples;
2735 /* we keep a high watermark on the number of frames written because
2736 * when the encoder goes back to write metadata, 'current_frame'
2737 * will drop back to 0.
2739 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2742 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2747 /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2748 void update_metadata_(const FLAC__StreamEncoder *encoder)
2750 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2751 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2752 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2753 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2754 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2755 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2756 FLAC__StreamEncoderSeekStatus seek_status;
2758 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2760 /* All this is based on intimate knowledge of the stream header
2761 * layout, but a change to the header format that would break this
2762 * would also break all streams encoded in the previous format.
2766 * Write MD5 signature
2769 const unsigned md5_offset =
2770 FLAC__STREAM_METADATA_HEADER_LENGTH +
2772 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2773 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2774 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2775 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2776 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2777 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2778 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2779 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2782 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2783 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2784 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2787 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2788 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2794 * Write total samples
2797 const unsigned total_samples_byte_offset =
2798 FLAC__STREAM_METADATA_HEADER_LENGTH +
2800 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2801 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2802 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2803 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2804 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2805 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2806 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2810 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2811 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2812 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2813 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2814 b[4] = (FLAC__byte)(samples & 0xFF);
2815 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) {
2816 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2817 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2820 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2821 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2827 * Write min/max framesize
2830 const unsigned min_framesize_offset =
2831 FLAC__STREAM_METADATA_HEADER_LENGTH +
2833 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2834 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2837 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2838 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2839 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2840 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2841 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2842 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2843 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) {
2844 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2845 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2848 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2849 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2857 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2860 FLAC__format_seektable_sort(encoder->private_->seek_table);
2862 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2864 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) {
2865 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2866 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2870 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2873 xx = encoder->private_->seek_table->points[i].sample_number;
2874 b[7] = (FLAC__byte)xx; xx >>= 8;
2875 b[6] = (FLAC__byte)xx; xx >>= 8;
2876 b[5] = (FLAC__byte)xx; xx >>= 8;
2877 b[4] = (FLAC__byte)xx; xx >>= 8;
2878 b[3] = (FLAC__byte)xx; xx >>= 8;
2879 b[2] = (FLAC__byte)xx; xx >>= 8;
2880 b[1] = (FLAC__byte)xx; xx >>= 8;
2881 b[0] = (FLAC__byte)xx; xx >>= 8;
2882 xx = encoder->private_->seek_table->points[i].stream_offset;
2883 b[15] = (FLAC__byte)xx; xx >>= 8;
2884 b[14] = (FLAC__byte)xx; xx >>= 8;
2885 b[13] = (FLAC__byte)xx; xx >>= 8;
2886 b[12] = (FLAC__byte)xx; xx >>= 8;
2887 b[11] = (FLAC__byte)xx; xx >>= 8;
2888 b[10] = (FLAC__byte)xx; xx >>= 8;
2889 b[9] = (FLAC__byte)xx; xx >>= 8;
2890 b[8] = (FLAC__byte)xx; xx >>= 8;
2891 x = encoder->private_->seek_table->points[i].frame_samples;
2892 b[17] = (FLAC__byte)x; x >>= 8;
2893 b[16] = (FLAC__byte)x; x >>= 8;
2894 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2895 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2903 /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2904 void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2906 /* the # of bytes in the 1st packet that precede the STREAMINFO */
2907 static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
2908 FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
2909 FLAC__OGG_MAPPING_MAGIC_LENGTH +
2910 FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
2911 FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
2912 FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
2913 FLAC__STREAM_SYNC_LENGTH
2915 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2916 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2917 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2918 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2919 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2922 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2923 FLAC__ASSERT(0 != encoder->private_->seek_callback);
2925 /* Pre-check that client supports seeking, since we don't want the
2926 * ogg_helper code to ever have to deal with this condition.
2928 if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
2931 /* All this is based on intimate knowledge of the stream header
2932 * layout, but a change to the header format that would break this
2933 * would also break all streams encoded in the previous format.
2937 ** Write STREAMINFO stats
2939 simple_ogg_page__init(&page);
2940 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2941 simple_ogg_page__clear(&page);
2942 return; /* state already set */
2946 * Write MD5 signature
2949 const unsigned md5_offset =
2950 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
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 +
2960 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2963 if(md5_offset + 16 > (unsigned)page.body_len) {
2964 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2965 simple_ogg_page__clear(&page);
2968 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2972 * Write total samples
2975 const unsigned total_samples_byte_offset =
2976 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2977 FLAC__STREAM_METADATA_HEADER_LENGTH +
2979 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2980 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2981 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2982 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2983 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2984 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2985 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2989 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2990 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2991 simple_ogg_page__clear(&page);
2994 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2995 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2996 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2997 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2998 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2999 b[4] = (FLAC__byte)(samples & 0xFF);
3000 memcpy(page.body + total_samples_byte_offset, b, 5);
3004 * Write min/max framesize
3007 const unsigned min_framesize_offset =
3008 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
3009 FLAC__STREAM_METADATA_HEADER_LENGTH +
3011 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
3012 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
3015 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
3016 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3017 simple_ogg_page__clear(&page);
3020 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
3021 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
3022 b[2] = (FLAC__byte)(min_framesize & 0xFF);
3023 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
3024 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
3025 b[5] = (FLAC__byte)(max_framesize & 0xFF);
3026 memcpy(page.body + min_framesize_offset, b, 6);
3028 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3029 simple_ogg_page__clear(&page);
3030 return; /* state already set */
3032 simple_ogg_page__clear(&page);
3037 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
3041 FLAC__format_seektable_sort(encoder->private_->seek_table);
3043 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
3045 simple_ogg_page__init(&page);
3046 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
3047 simple_ogg_page__clear(&page);
3048 return; /* state already set */
3051 if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) {
3052 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3053 simple_ogg_page__clear(&page);
3057 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
3060 xx = encoder->private_->seek_table->points[i].sample_number;
3061 b[7] = (FLAC__byte)xx; xx >>= 8;
3062 b[6] = (FLAC__byte)xx; xx >>= 8;
3063 b[5] = (FLAC__byte)xx; xx >>= 8;
3064 b[4] = (FLAC__byte)xx; xx >>= 8;
3065 b[3] = (FLAC__byte)xx; xx >>= 8;
3066 b[2] = (FLAC__byte)xx; xx >>= 8;
3067 b[1] = (FLAC__byte)xx; xx >>= 8;
3068 b[0] = (FLAC__byte)xx; xx >>= 8;
3069 xx = encoder->private_->seek_table->points[i].stream_offset;
3070 b[15] = (FLAC__byte)xx; xx >>= 8;
3071 b[14] = (FLAC__byte)xx; xx >>= 8;
3072 b[13] = (FLAC__byte)xx; xx >>= 8;
3073 b[12] = (FLAC__byte)xx; xx >>= 8;
3074 b[11] = (FLAC__byte)xx; xx >>= 8;
3075 b[10] = (FLAC__byte)xx; xx >>= 8;
3076 b[9] = (FLAC__byte)xx; xx >>= 8;
3077 b[8] = (FLAC__byte)xx; xx >>= 8;
3078 x = encoder->private_->seek_table->points[i].frame_samples;
3079 b[17] = (FLAC__byte)x; x >>= 8;
3080 b[16] = (FLAC__byte)x; x >>= 8;
3084 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3085 simple_ogg_page__clear(&page);
3086 return; /* state already set */
3088 simple_ogg_page__clear(&page);
3093 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
3095 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
3098 * Accumulate raw signal to the MD5 signature
3100 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)) {
3101 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
3106 * Process the frame header and subframes into the frame bitbuffer
3108 if(!process_subframes_(encoder, is_fractional_block)) {
3109 /* the above function sets the state for us in case of an error */
3114 * Zero-pad the frame to a byte_boundary
3116 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
3117 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
3122 * CRC-16 the whole thing
3124 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
3125 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
3130 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
3131 /* the above function sets the state for us in case of an error */
3136 * Get ready for the next frame
3138 encoder->private_->current_sample_number = 0;
3139 encoder->private_->current_frame_number++;
3140 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
3145 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
3147 FLAC__FrameHeader frame_header;
3148 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
3149 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
3152 * Calculate the min,max Rice partition orders
3154 if(is_fractional_block) {
3155 max_partition_order = 0;
3158 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
3159 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
3161 min_partition_order = min(min_partition_order, max_partition_order);
3163 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
3168 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
3169 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
3172 frame_header.blocksize = encoder->protected_->blocksize;
3173 frame_header.sample_rate = encoder->protected_->sample_rate;
3174 frame_header.channels = encoder->protected_->channels;
3175 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
3176 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
3177 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
3178 frame_header.number.frame_number = encoder->private_->current_frame_number;
3181 * Figure out what channel assignments to try
3183 if(encoder->protected_->do_mid_side_stereo) {
3184 if(encoder->protected_->loose_mid_side_stereo) {
3185 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
3186 do_independent = true;
3190 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
3191 do_mid_side = !do_independent;
3195 do_independent = true;
3200 do_independent = true;
3201 do_mid_side = false;
3204 FLAC__ASSERT(do_independent || do_mid_side);
3207 * Check for wasted bits; set effective bps for each subframe
3209 if(do_independent) {
3210 for(channel = 0; channel < encoder->protected_->channels; channel++) {
3211 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
3212 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
3213 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
3217 FLAC__ASSERT(encoder->protected_->channels == 2);
3218 for(channel = 0; channel < 2; channel++) {
3219 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
3220 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
3221 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
3226 * First do a normal encoding pass of each independent channel
3228 if(do_independent) {
3229 for(channel = 0; channel < encoder->protected_->channels; channel++) {
3233 min_partition_order,
3234 max_partition_order,
3235 precompute_partition_sums,
3237 encoder->private_->subframe_bps[channel],
3238 encoder->private_->integer_signal[channel],
3239 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3240 encoder->private_->real_signal[channel],
3242 encoder->private_->subframe_workspace_ptr[channel],
3243 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
3244 encoder->private_->residual_workspace[channel],
3245 encoder->private_->best_subframe+channel,
3246 encoder->private_->best_subframe_bits+channel
3254 * Now do mid and side channels if requested
3257 FLAC__ASSERT(encoder->protected_->channels == 2);
3259 for(channel = 0; channel < 2; channel++) {
3263 min_partition_order,
3264 max_partition_order,
3265 precompute_partition_sums,
3267 encoder->private_->subframe_bps_mid_side[channel],
3268 encoder->private_->integer_signal_mid_side[channel],
3269 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3270 encoder->private_->real_signal_mid_side[channel],
3272 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3273 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3274 encoder->private_->residual_workspace_mid_side[channel],
3275 encoder->private_->best_subframe_mid_side+channel,
3276 encoder->private_->best_subframe_bits_mid_side+channel
3284 * Compose the frame bitbuffer
3287 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3288 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
3289 FLAC__ChannelAssignment channel_assignment;
3291 FLAC__ASSERT(encoder->protected_->channels == 2);
3293 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3294 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
3297 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3301 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT == 0);
3302 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE == 1);
3303 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE == 2);
3304 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_MID_SIDE == 3);
3305 FLAC__ASSERT(do_independent && do_mid_side);
3307 /* We have to figure out which channel assignent results in the smallest frame */
3308 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3309 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3310 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3311 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
3313 channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
3314 min_bits = bits[channel_assignment];
3315 for(ca = 1; ca <= 3; ca++) {
3316 if(bits[ca] < min_bits) {
3317 min_bits = bits[ca];
3318 channel_assignment = (FLAC__ChannelAssignment)ca;
3323 frame_header.channel_assignment = channel_assignment;
3325 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3326 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3330 switch(channel_assignment) {
3331 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3332 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3333 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
3335 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3336 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3337 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3339 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3340 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3341 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
3343 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3344 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3345 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3351 switch(channel_assignment) {
3352 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3353 left_bps = encoder->private_->subframe_bps [0];
3354 right_bps = encoder->private_->subframe_bps [1];
3356 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3357 left_bps = encoder->private_->subframe_bps [0];
3358 right_bps = encoder->private_->subframe_bps_mid_side[1];
3360 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3361 left_bps = encoder->private_->subframe_bps_mid_side[1];
3362 right_bps = encoder->private_->subframe_bps [1];
3364 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3365 left_bps = encoder->private_->subframe_bps_mid_side[0];
3366 right_bps = encoder->private_->subframe_bps_mid_side[1];
3372 /* note that encoder_add_subframe_ sets the state for us in case of an error */
3373 if(!add_subframe_(encoder, frame_header.blocksize, left_bps , left_subframe , encoder->private_->frame))
3375 if(!add_subframe_(encoder, frame_header.blocksize, right_bps, right_subframe, encoder->private_->frame))
3379 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3380 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3384 for(channel = 0; channel < encoder->protected_->channels; channel++) {
3385 if(!add_subframe_(encoder, frame_header.blocksize, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
3386 /* the above function sets the state for us in case of an error */
3392 if(encoder->protected_->loose_mid_side_stereo) {
3393 encoder->private_->loose_mid_side_stereo_frame_count++;
3394 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3395 encoder->private_->loose_mid_side_stereo_frame_count = 0;
3398 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
3403 FLAC__bool process_subframe_(
3404 FLAC__StreamEncoder *encoder,
3405 unsigned min_partition_order,
3406 unsigned max_partition_order,
3407 FLAC__bool precompute_partition_sums,
3408 const FLAC__FrameHeader *frame_header,
3409 unsigned subframe_bps,
3410 const FLAC__int32 integer_signal[],
3411 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3412 const FLAC__real real_signal[],
3414 FLAC__Subframe *subframe[2],
3415 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3416 FLAC__int32 *residual[2],
3417 unsigned *best_subframe,
3421 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3422 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3424 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3426 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3427 FLAC__double lpc_residual_bits_per_sample;
3428 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 */
3429 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
3430 unsigned min_lpc_order, max_lpc_order, lpc_order;
3431 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
3433 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
3434 unsigned rice_parameter;
3435 unsigned _candidate_bits, _best_bits;
3436 unsigned _best_subframe;
3438 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3440 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3441 _best_bits = UINT_MAX;
3443 _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3445 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3446 unsigned signal_is_constant = false;
3447 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);
3448 /* check for constant subframe */
3450 !encoder->private_->disable_constant_subframes &&
3451 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3452 fixed_residual_bits_per_sample[1] == 0.0
3454 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3457 /* the above means it's possible all samples are the same value; now double-check it: */
3459 signal_is_constant = true;
3460 for(i = 1; i < frame_header->blocksize; i++) {
3461 if(integer_signal[0] != integer_signal[i]) {
3462 signal_is_constant = false;
3467 if(signal_is_constant) {
3468 _candidate_bits = evaluate_constant_subframe_(encoder, integer_signal[0], frame_header->blocksize, subframe_bps, subframe[!_best_subframe]);
3469 if(_candidate_bits < _best_bits) {
3470 _best_subframe = !_best_subframe;
3471 _best_bits = _candidate_bits;
3475 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3477 if(encoder->protected_->do_exhaustive_model_search) {
3478 min_fixed_order = 0;
3479 max_fixed_order = FLAC__MAX_FIXED_ORDER;
3482 min_fixed_order = max_fixed_order = guess_fixed_order;
3484 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
3485 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3486 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
3487 continue; /* don't even try */
3488 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 */
3490 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3491 continue; /* don't even try */
3492 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 */
3494 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3495 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3496 #ifdef DEBUG_VERBOSE
3497 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3499 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3502 evaluate_fixed_subframe_(
3505 residual[!_best_subframe],
3506 encoder->private_->abs_residual,
3507 encoder->private_->abs_residual_partition_sums,
3508 encoder->private_->raw_bits_per_partition,
3509 frame_header->blocksize,
3513 min_partition_order,
3514 max_partition_order,
3515 precompute_partition_sums,
3516 encoder->protected_->do_escape_coding,
3517 encoder->protected_->rice_parameter_search_dist,
3518 subframe[!_best_subframe],
3519 partitioned_rice_contents[!_best_subframe]
3521 if(_candidate_bits < _best_bits) {
3522 _best_subframe = !_best_subframe;
3523 _best_bits = _candidate_bits;
3528 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3530 if(encoder->protected_->max_lpc_order > 0) {
3531 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
3532 max_lpc_order = frame_header->blocksize-1;
3534 max_lpc_order = encoder->protected_->max_lpc_order;
3535 if(max_lpc_order > 0) {
3537 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
3538 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
3539 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3540 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3541 if(autoc[0] != 0.0) {
3542 FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3543 if(encoder->protected_->do_exhaustive_model_search) {
3547 const unsigned guess_lpc_order =
3548 FLAC__lpc_compute_best_order(
3551 frame_header->blocksize,
3553 encoder->protected_->do_qlp_coeff_prec_search?
3554 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3555 encoder->protected_->qlp_coeff_precision
3558 min_lpc_order = max_lpc_order = guess_lpc_order;
3560 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3561 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3562 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3563 continue; /* don't even try */
3564 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3565 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3566 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3567 #ifdef DEBUG_VERBOSE
3568 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3570 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3572 if(encoder->protected_->do_qlp_coeff_prec_search) {
3573 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
3574 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3575 if(subframe_bps <= 17) {
3576 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
3577 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3580 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3583 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3585 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3587 evaluate_lpc_subframe_(
3590 residual[!_best_subframe],
3591 encoder->private_->abs_residual,
3592 encoder->private_->abs_residual_partition_sums,
3593 encoder->private_->raw_bits_per_partition,
3594 encoder->private_->lp_coeff[lpc_order-1],
3595 frame_header->blocksize,
3598 qlp_coeff_precision,
3600 min_partition_order,
3601 max_partition_order,
3602 precompute_partition_sums,
3603 encoder->protected_->do_escape_coding,
3604 encoder->protected_->rice_parameter_search_dist,
3605 subframe[!_best_subframe],
3606 partitioned_rice_contents[!_best_subframe]
3608 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3609 if(_candidate_bits < _best_bits) {
3610 _best_subframe = !_best_subframe;
3611 _best_bits = _candidate_bits;
3620 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
3624 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3625 if(_best_bits == UINT_MAX) {
3626 FLAC__ASSERT(_best_subframe == 0);
3627 _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3630 *best_subframe = _best_subframe;
3631 *best_bits = _best_bits;
3636 FLAC__bool add_subframe_(
3637 FLAC__StreamEncoder *encoder,
3639 unsigned subframe_bps,
3640 const FLAC__Subframe *subframe,
3641 FLAC__BitBuffer *frame
3644 switch(subframe->type) {
3645 case FLAC__SUBFRAME_TYPE_CONSTANT:
3646 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
3647 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3651 case FLAC__SUBFRAME_TYPE_FIXED:
3652 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
3653 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3657 case FLAC__SUBFRAME_TYPE_LPC:
3658 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
3659 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3663 case FLAC__SUBFRAME_TYPE_VERBATIM:
3664 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), blocksize, subframe_bps, subframe->wasted_bits, frame)) {
3665 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3676 #define SPOTCHECK_ESTIMATE 0 //@@@@@@@@@
3677 #if SPOTCHECK_ESTIMATE
3678 static void spotcheck_subframe_estimate_(
3679 FLAC__StreamEncoder *encoder,
3681 unsigned subframe_bps,
3682 const FLAC__Subframe *subframe,
3687 FLAC__BitBuffer *frame = FLAC__bitbuffer_new();
3689 fprintf(stderr, "EST: can't allocate frame\n");
3692 if(!FLAC__bitbuffer_init(frame)) {
3693 fprintf(stderr, "EST: can't init frame\n");
3696 ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame);
3699 const unsigned actual = FLAC__bitbuffer_get_input_bits_unconsumed(frame);
3700 if(estimate != actual)
3701 fprintf(stderr, "EST: bad, frame#%u sub#%%d type=%8s est=%u, actual=%u, delta=%d\n", encoder->private_->current_frame_number, FLAC__SubframeTypeString[subframe->type], estimate, actual, (int)actual-(int)estimate);
3703 FLAC__bitbuffer_delete(frame);
3707 unsigned evaluate_constant_subframe_(
3708 FLAC__StreamEncoder *encoder,
3709 const FLAC__int32 signal,
3711 unsigned subframe_bps,
3712 FLAC__Subframe *subframe
3716 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3717 subframe->data.constant.value = signal;
3719 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
3721 #if SPOTCHECK_ESTIMATE
3722 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3724 (void)encoder, (void)blocksize;
3730 unsigned evaluate_fixed_subframe_(
3731 FLAC__StreamEncoder *encoder,
3732 const FLAC__int32 signal[],
3733 FLAC__int32 residual[],
3734 FLAC__uint32 abs_residual[],
3735 FLAC__uint64 abs_residual_partition_sums[],
3736 unsigned raw_bits_per_partition[],
3738 unsigned subframe_bps,
3740 unsigned rice_parameter,
3741 unsigned min_partition_order,
3742 unsigned max_partition_order,
3743 FLAC__bool precompute_partition_sums,
3744 FLAC__bool do_escape_coding,
3745 unsigned rice_parameter_search_dist,
3746 FLAC__Subframe *subframe,
3747 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3750 unsigned i, residual_bits, estimate;
3751 const unsigned residual_samples = blocksize - order;
3753 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3755 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3757 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3758 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3759 subframe->data.fixed.residual = residual;
3762 find_best_partition_order_(
3766 abs_residual_partition_sums,
3767 raw_bits_per_partition,
3771 min_partition_order,
3772 max_partition_order,
3773 precompute_partition_sums,
3775 rice_parameter_search_dist,
3776 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3779 subframe->data.fixed.order = order;
3780 for(i = 0; i < order; i++)
3781 subframe->data.fixed.warmup[i] = signal[i];
3783 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
3785 #if SPOTCHECK_ESTIMATE
3786 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3792 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3793 unsigned evaluate_lpc_subframe_(
3794 FLAC__StreamEncoder *encoder,
3795 const FLAC__int32 signal[],
3796 FLAC__int32 residual[],
3797 FLAC__uint32 abs_residual[],
3798 FLAC__uint64 abs_residual_partition_sums[],
3799 unsigned raw_bits_per_partition[],
3800 const FLAC__real lp_coeff[],
3802 unsigned subframe_bps,
3804 unsigned qlp_coeff_precision,
3805 unsigned rice_parameter,
3806 unsigned min_partition_order,
3807 unsigned max_partition_order,
3808 FLAC__bool precompute_partition_sums,
3809 FLAC__bool do_escape_coding,
3810 unsigned rice_parameter_search_dist,
3811 FLAC__Subframe *subframe,
3812 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3815 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
3816 unsigned i, residual_bits, estimate;
3817 int quantization, ret;
3818 const unsigned residual_samples = blocksize - order;
3820 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3821 if(subframe_bps <= 16) {
3822 FLAC__ASSERT(order > 0);
3823 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3824 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3827 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
3829 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3831 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3832 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3833 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3835 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3837 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3839 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3841 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3842 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3843 subframe->data.lpc.residual = residual;
3846 find_best_partition_order_(
3850 abs_residual_partition_sums,
3851 raw_bits_per_partition,
3855 min_partition_order,
3856 max_partition_order,
3857 precompute_partition_sums,
3859 rice_parameter_search_dist,
3860 &subframe->data.lpc.entropy_coding_method.data.partitioned_rice
3863 subframe->data.lpc.order = order;
3864 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3865 subframe->data.lpc.quantization_level = quantization;
3866 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
3867 for(i = 0; i < order; i++)
3868 subframe->data.lpc.warmup[i] = signal[i];
3870 estimate = 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;
3872 #if SPOTCHECK_ESTIMATE
3873 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3880 unsigned evaluate_verbatim_subframe_(
3881 FLAC__StreamEncoder *encoder,
3882 const FLAC__int32 signal[],
3884 unsigned subframe_bps,
3885 FLAC__Subframe *subframe
3890 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3892 subframe->data.verbatim.data = signal;
3894 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
3896 #if SPOTCHECK_ESTIMATE
3897 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3905 unsigned find_best_partition_order_(
3906 FLAC__StreamEncoderPrivate *private_,
3907 const FLAC__int32 residual[],
3908 FLAC__uint32 abs_residual[],
3909 FLAC__uint64 abs_residual_partition_sums[],
3910 unsigned raw_bits_per_partition[],
3911 unsigned residual_samples,
3912 unsigned predictor_order,
3913 unsigned rice_parameter,
3914 unsigned min_partition_order,
3915 unsigned max_partition_order,
3916 FLAC__bool precompute_partition_sums,
3917 FLAC__bool do_escape_coding,
3918 unsigned rice_parameter_search_dist,
3919 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3923 unsigned residual_bits, best_residual_bits = 0;
3924 unsigned residual_sample;
3925 unsigned best_parameters_index = 0;
3926 const unsigned blocksize = residual_samples + predictor_order;
3928 /* compute abs(residual) for use later */
3929 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3930 r = residual[residual_sample];
3931 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
3934 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
3935 min_partition_order = min(min_partition_order, max_partition_order);
3937 if(precompute_partition_sums) {
3938 int partition_order;
3941 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
3943 if(do_escape_coding)
3944 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
3946 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3948 set_partitioned_rice_with_precompute_(
3949 #ifdef EXACT_RICE_BITS_CALCULATION
3954 abs_residual_partition_sums+sum,
3955 raw_bits_per_partition+sum,
3959 rice_parameter_search_dist,
3960 (unsigned)partition_order,
3962 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3967 FLAC__ASSERT(best_residual_bits != 0);
3970 sum += 1u << partition_order;
3971 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3972 best_residual_bits = residual_bits;
3973 best_parameters_index = !best_parameters_index;
3974 best_partitioned_rice->order = partition_order;
3979 unsigned partition_order;
3980 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3982 set_partitioned_rice_(
3984 #ifdef EXACT_RICE_BITS_CALCULATION
3990 rice_parameter_search_dist,
3992 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3997 FLAC__ASSERT(best_residual_bits != 0);
4000 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
4001 best_residual_bits = residual_bits;
4002 best_parameters_index = !best_parameters_index;
4003 best_partitioned_rice->order = partition_order;
4009 * We are allowed to de-const the pointer based on our special knowledge;
4010 * it is const to the outside world.
4013 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
4014 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
4015 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
4016 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
4019 return best_residual_bits;
4022 void precompute_partition_info_sums_(
4023 const FLAC__uint32 abs_residual[],
4024 FLAC__uint64 abs_residual_partition_sums[],
4025 unsigned residual_samples,
4026 unsigned predictor_order,
4027 unsigned min_partition_order,
4028 unsigned max_partition_order
4031 int partition_order;
4032 unsigned from_partition, to_partition = 0;
4033 const unsigned blocksize = residual_samples + predictor_order;
4035 /* first do max_partition_order */
4036 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
4037 FLAC__uint64 abs_residual_partition_sum;
4038 unsigned partition, partition_sample, partition_samples, residual_sample;
4039 const unsigned partitions = 1u << partition_order;
4040 const unsigned default_partition_samples = blocksize >> partition_order;
4042 FLAC__ASSERT(default_partition_samples > predictor_order);
4044 for(partition = residual_sample = 0; partition < partitions; partition++) {
4045 partition_samples = default_partition_samples;
4047 partition_samples -= predictor_order;
4048 abs_residual_partition_sum = 0;
4049 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++)
4050 abs_residual_partition_sum += abs_residual[residual_sample++];
4051 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
4053 to_partition = partitions;
4057 /* now merge partitions for lower orders */
4058 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
4061 const unsigned partitions = 1u << partition_order;
4062 for(i = 0; i < partitions; i++) {
4063 s = abs_residual_partition_sums[from_partition];
4065 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
4072 void precompute_partition_info_escapes_(
4073 const FLAC__int32 residual[],
4074 unsigned raw_bits_per_partition[],
4075 unsigned residual_samples,
4076 unsigned predictor_order,
4077 unsigned min_partition_order,
4078 unsigned max_partition_order
4081 int partition_order;
4082 unsigned from_partition, to_partition = 0;
4083 const unsigned blocksize = residual_samples + predictor_order;
4085 /* first do max_partition_order */
4086 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
4087 FLAC__int32 r, residual_partition_min, residual_partition_max;
4088 unsigned silog2_min, silog2_max;
4089 unsigned partition, partition_sample, partition_samples, residual_sample;
4090 const unsigned partitions = 1u << partition_order;
4091 const unsigned default_partition_samples = blocksize >> partition_order;
4093 FLAC__ASSERT(default_partition_samples > predictor_order);
4095 for(partition = residual_sample = 0; partition < partitions; partition++) {
4096 partition_samples = default_partition_samples;
4098 partition_samples -= predictor_order;
4099 residual_partition_min = residual_partition_max = 0;
4100 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
4101 r = residual[residual_sample];
4102 if(r < residual_partition_min)
4103 residual_partition_min = r;
4104 else if(r > residual_partition_max)
4105 residual_partition_max = r;
4108 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
4109 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
4110 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
4112 to_partition = partitions;
4113 break; /*@@@ yuck, should remove the 'for' loop instead */
4116 /* now merge partitions for lower orders */
4117 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
4120 const unsigned partitions = 1u << partition_order;
4121 for(i = 0; i < partitions; i++) {
4122 m = raw_bits_per_partition[from_partition];
4124 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
4131 #undef VARIABLE_RICE_BITS
4132 #ifndef EXACT_RICE_BITS_CALCULATION
4133 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
4136 #ifdef EXACT_RICE_BITS_CALCULATION
4137 static __inline unsigned count_rice_bits_(
4138 const unsigned rice_parameter,
4139 const unsigned partition_samples,
4140 const FLAC__int32 *residual
4143 unsigned i, partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4144 for(i = 0; i < partition_samples; i++)
4145 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter);
4146 return partition_bits;
4149 static __inline unsigned count_rice_bits_(
4150 const unsigned rice_parameter,
4151 const unsigned partition_samples,
4152 const FLAC__uint32 *abs_residual
4155 const unsigned rice_parameter_estimate = rice_parameter-1;
4156 unsigned i, partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + (1+rice_parameter) * partition_samples;
4157 for(i = 0; i < partition_samples; i++)
4158 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
4159 return partition_bits;
4163 FLAC__bool set_partitioned_rice_(
4164 const FLAC__uint32 abs_residual[],
4165 #ifdef EXACT_RICE_BITS_CALCULATION
4166 const FLAC__int32 residual[],
4168 const unsigned residual_samples,
4169 const unsigned predictor_order,
4170 const unsigned suggested_rice_parameter,
4171 const unsigned rice_parameter_search_dist,
4172 const unsigned partition_order,
4173 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4177 unsigned rice_parameter, partition_bits;
4178 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
4179 unsigned *parameters;
4180 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4181 unsigned best_partition_bits, best_rice_parameter = 0;
4182 unsigned min_rice_parameter, max_rice_parameter;
4184 (void)rice_parameter_search_dist;
4187 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4189 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4190 parameters = partitioned_rice_contents->parameters;
4192 if(partition_order == 0) {
4193 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4194 best_partition_bits = 0xffffffff;
4195 if(rice_parameter_search_dist) {
4196 if(suggested_rice_parameter < rice_parameter_search_dist)
4197 min_rice_parameter = 0;
4199 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4200 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4201 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4202 #ifdef DEBUG_VERBOSE
4203 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4205 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4209 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
4211 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4213 rice_parameter = suggested_rice_parameter;
4215 #ifdef EXACT_RICE_BITS_CALCULATION
4216 partition_bits = count_rice_bits_(rice_parameter, residual_samples, residual);
4218 partition_bits = count_rice_bits_(rice_parameter, residual_samples, abs_residual);
4220 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4221 if(partition_bits < best_partition_bits) {
4222 best_rice_parameter = rice_parameter;
4223 best_partition_bits = partition_bits;
4226 parameters[0] = best_rice_parameter;
4227 bits_ += best_partition_bits;
4229 parameters[0] = rice_parameter;
4230 bits_ += partition_bits;
4234 unsigned partition, residual_sample;
4235 unsigned partition_samples;
4236 FLAC__uint64 mean, k;
4237 const unsigned partitions = 1u << partition_order;
4238 for(partition = residual_sample = 0; partition < partitions; partition++) {
4239 partition_samples = (residual_samples+predictor_order) >> partition_order;
4240 if(partition == 0) {
4241 if(partition_samples <= predictor_order)
4244 partition_samples -= predictor_order;
4249 for(ps = 0, rs = residual_sample; ps < partition_samples; ps++)
4250 mean += abs_residual[rs++];
4252 /* we are basically calculating the size in bits of the
4253 * average residual magnitude in the partition:
4254 * rice_parameter = floor(log2(mean/partition_samples))
4255 * 'mean' is not a good name for the variable, it is
4256 * actually the sum of magnitudes of all residual values
4257 * in the partition, so the actual mean is
4258 * mean/partition_samples
4260 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
4262 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4263 #ifdef DEBUG_VERBOSE
4264 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4266 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4269 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4270 best_partition_bits = 0xffffffff;
4271 if(rice_parameter_search_dist) {
4272 if(rice_parameter < rice_parameter_search_dist)
4273 min_rice_parameter = 0;
4275 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4276 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4277 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4278 #ifdef DEBUG_VERBOSE
4279 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4281 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4285 min_rice_parameter = max_rice_parameter = rice_parameter;
4287 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4289 #ifdef EXACT_RICE_BITS_CALCULATION
4290 partition_bits = count_rice_bits_(rice_parameter, partition_samples, residual+residual_sample);
4292 partition_bits = count_rice_bits_(rice_parameter, partition_samples, abs_residual+residual_sample);
4294 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4295 if(partition_bits < best_partition_bits) {
4296 best_rice_parameter = rice_parameter;
4297 best_partition_bits = partition_bits;
4300 parameters[partition] = best_rice_parameter;
4301 bits_ += best_partition_bits;
4303 parameters[partition] = rice_parameter;
4304 bits_ += partition_bits;
4306 residual_sample += partition_samples;
4314 FLAC__bool set_partitioned_rice_with_precompute_(
4315 #ifdef EXACT_RICE_BITS_CALCULATION
4316 const FLAC__int32 residual[],
4318 const FLAC__uint32 abs_residual[],
4320 const FLAC__uint64 abs_residual_partition_sums[],
4321 const unsigned raw_bits_per_partition[],
4322 const unsigned residual_samples,
4323 const unsigned predictor_order,
4324 const unsigned suggested_rice_parameter,
4325 const unsigned rice_parameter_search_dist,
4326 const unsigned partition_order,
4327 const FLAC__bool search_for_escapes,
4328 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4332 unsigned rice_parameter, partition_bits;
4333 unsigned best_partition_bits, best_rice_parameter = 0;
4335 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
4336 unsigned *parameters, *raw_bits;
4337 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4338 unsigned min_rice_parameter, max_rice_parameter;
4340 (void)rice_parameter_search_dist;
4343 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4345 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4346 parameters = partitioned_rice_contents->parameters;
4347 raw_bits = partitioned_rice_contents->raw_bits;
4349 if(partition_order == 0) {
4350 best_partition_bits = 0xffffffff;
4351 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4352 if(rice_parameter_search_dist) {
4353 if(suggested_rice_parameter < rice_parameter_search_dist)
4354 min_rice_parameter = 0;
4356 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4357 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4358 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4359 #ifdef DEBUG_VERBOSE
4360 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4362 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4366 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
4368 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4370 rice_parameter = suggested_rice_parameter;
4372 #ifdef EXACT_RICE_BITS_CALCULATION
4373 partition_bits = count_rice_bits_(rice_parameter, residual_samples, residual);
4375 partition_bits = count_rice_bits_(rice_parameter, residual_samples, abs_residual);
4377 if(partition_bits < best_partition_bits) {
4378 best_rice_parameter = rice_parameter;
4379 best_partition_bits = partition_bits;
4381 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4384 if(search_for_escapes) {
4385 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;
4386 if(flat_bits <= best_partition_bits) {
4387 raw_bits[0] = raw_bits_per_partition[0];
4388 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4389 best_partition_bits = flat_bits;
4392 parameters[0] = best_rice_parameter;
4393 bits_ += best_partition_bits;
4396 unsigned partition, residual_sample;
4397 unsigned partition_samples;
4398 FLAC__uint64 mean, k;
4399 const unsigned partitions = 1u << partition_order;
4400 for(partition = residual_sample = 0; partition < partitions; partition++) {
4401 partition_samples = (residual_samples+predictor_order) >> partition_order;
4402 if(partition == 0) {
4403 if(partition_samples <= predictor_order)
4406 partition_samples -= predictor_order;
4408 mean = abs_residual_partition_sums[partition];
4409 /* we are basically calculating the size in bits of the
4410 * average residual magnitude in the partition:
4411 * rice_parameter = floor(log2(mean/partition_samples))
4412 * 'mean' is not a good name for the variable, it is
4413 * actually the sum of magnitudes of all residual values
4414 * in the partition, so the actual mean is
4415 * mean/partition_samples
4417 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
4419 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4420 #ifdef DEBUG_VERBOSE
4421 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4423 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4426 best_partition_bits = 0xffffffff;
4427 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4428 if(rice_parameter_search_dist) {
4429 if(rice_parameter < rice_parameter_search_dist)
4430 min_rice_parameter = 0;
4432 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4433 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4434 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4435 #ifdef DEBUG_VERBOSE
4436 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4438 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4442 min_rice_parameter = max_rice_parameter = rice_parameter;
4444 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4446 #ifdef EXACT_RICE_BITS_CALCULATION
4447 partition_bits = count_rice_bits_(rice_parameter, partition_samples, residual+residual_sample);
4449 partition_bits = count_rice_bits_(rice_parameter, partition_samples, abs_residual+residual_sample);
4451 if(partition_bits < best_partition_bits) {
4452 best_rice_parameter = rice_parameter;
4453 best_partition_bits = partition_bits;
4455 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4458 if(search_for_escapes) {
4459 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;
4460 if(flat_bits <= best_partition_bits) {
4461 raw_bits[partition] = raw_bits_per_partition[partition];
4462 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4463 best_partition_bits = flat_bits;
4466 parameters[partition] = best_rice_parameter;
4467 bits_ += best_partition_bits;
4468 residual_sample += partition_samples;
4476 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
4481 for(i = 0; i < samples && !(x&1); i++)
4488 for(shift = 0; !(x&1); shift++)
4493 for(i = 0; i < samples; i++)
4494 signal[i] >>= shift;
4500 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4504 for(channel = 0; channel < channels; channel++)
4505 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4507 fifo->tail += wide_samples;
4509 FLAC__ASSERT(fifo->tail <= fifo->size);
4512 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4515 unsigned sample, wide_sample;
4516 unsigned tail = fifo->tail;
4518 sample = input_offset * channels;
4519 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4520 for(channel = 0; channel < channels; channel++)
4521 fifo->data[channel][tail] = input[sample++];
4526 FLAC__ASSERT(fifo->tail <= fifo->size);
4529 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4531 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4532 const size_t encoded_bytes = encoder->private_->verify.output.bytes;
4535 if(encoder->private_->verify.needs_magic_hack) {
4536 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4537 *bytes = FLAC__STREAM_SYNC_LENGTH;
4538 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4539 encoder->private_->verify.needs_magic_hack = false;
4542 if(encoded_bytes == 0) {
4544 * If we get here, a FIFO underflow has occurred,
4545 * which means there is a bug somewhere.
4548 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4550 else if(encoded_bytes < *bytes)
4551 *bytes = encoded_bytes;
4552 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4553 encoder->private_->verify.output.data += *bytes;
4554 encoder->private_->verify.output.bytes -= *bytes;
4557 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4560 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4562 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4564 const unsigned channels = frame->header.channels;
4565 const unsigned blocksize = frame->header.blocksize;
4566 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4570 for(channel = 0; channel < channels; channel++) {
4571 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4572 unsigned i, sample = 0;
4573 FLAC__int32 expect = 0, got = 0;
4575 for(i = 0; i < blocksize; i++) {
4576 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4578 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4579 got = (FLAC__int32)buffer[channel][i];
4583 FLAC__ASSERT(i < blocksize);
4584 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4585 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
4586 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
4587 encoder->private_->verify.error_stats.channel = channel;
4588 encoder->private_->verify.error_stats.sample = sample;
4589 encoder->private_->verify.error_stats.expected = expect;
4590 encoder->private_->verify.error_stats.got = got;
4591 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4592 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4595 /* dequeue the frame from the fifo */
4596 encoder->private_->verify.input_fifo.tail -= blocksize;
4597 FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
4598 for(channel = 0; channel < channels; channel++)
4599 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]));
4600 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4603 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4605 (void)decoder, (void)metadata, (void)client_data;
4608 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4610 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4611 (void)decoder, (void)status;
4612 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4615 FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4619 *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
4621 if (feof(encoder->private_->file))
4622 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4623 else if (ferror(encoder->private_->file))
4624 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4626 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4629 FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4633 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4634 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4636 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4639 FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4645 offset = ftello(encoder->private_->file);
4648 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4651 *absolute_byte_offset = (FLAC__uint64)offset;
4652 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4656 #ifdef FLAC__VALGRIND_TESTING
4657 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4659 size_t ret = fwrite(ptr, size, nmemb, stream);
4665 #define local__fwrite fwrite
4668 FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
4670 (void)client_data, (void)current_frame;
4672 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
4673 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4675 /* We would like to be able to use 'samples > 0' in the
4676 * clause here but currently because of the nature of our
4677 * Ogg writing implementation, 'samples' is always 0 (see
4678 * ogg_encoder_aspect.c). The downside is extra progress
4681 encoder->private_->is_ogg? true :
4686 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4687 * because at this point in the callback chain, the stats
4688 * have not been updated. Only after we return and control
4689 * gets back to write_frame_() are the stats updated
4691 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);
4693 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4696 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4700 * This will forcibly set stdout to binary mode (for OSes that require it)
4702 FILE *get_binary_stdout_()
4704 /* if something breaks here it is probably due to the presence or
4705 * absence of an underscore before the identifiers 'setmode',
4706 * 'fileno', and/or 'O_BINARY'; check your system header files.
4708 #if defined _MSC_VER || defined __MINGW32__
4709 _setmode(_fileno(stdout), _O_BINARY);
4710 #elif defined __CYGWIN__
4711 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4712 setmode(_fileno(stdout), _O_BINARY);
4713 #elif defined __EMX__
4714 setmode(fileno(stdout), O_BINARY);