1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #if defined _MSC_VER || defined __MINGW32__
37 #include <io.h> /* for _setmode() */
38 #include <fcntl.h> /* for _O_BINARY */
40 #if defined __CYGWIN__ || defined __EMX__
41 #include <io.h> /* for setmode(), O_BINARY */
42 #include <fcntl.h> /* for _O_BINARY */
46 #include <stdlib.h> /* for malloc() */
47 #include <string.h> /* for memcpy() */
48 #include <sys/types.h> /* for off_t */
49 #if defined _MSC_VER || defined __MINGW32__
50 #if _MSC_VER <= 1200 /* @@@ [2G limit] */
55 #include "FLAC/assert.h"
56 #include "FLAC/stream_decoder.h"
57 #include "protected/stream_encoder.h"
58 #include "private/bitbuffer.h"
59 #include "private/bitmath.h"
60 #include "private/crc.h"
61 #include "private/cpu.h"
62 #include "private/fixed.h"
63 #include "private/format.h"
64 #include "private/lpc.h"
65 #include "private/md5.h"
66 #include "private/memory.h"
67 #include "private/stream_encoder_framing.h"
68 #include "private/window.h"
73 #define min(x,y) ((x)<(y)?(x):(y))
78 #define max(x,y) ((x)>(y)?(x):(y))
81 FLAC__int32 *data[FLAC__MAX_CHANNELS];
82 unsigned size; /* of each data[] in samples */
87 const FLAC__byte *data;
94 ENCODER_IN_METADATA = 1,
98 /***********************************************************************
100 * Private class method prototypes
102 ***********************************************************************/
104 static void set_defaults_(FLAC__StreamEncoder *encoder);
105 static void free_(FLAC__StreamEncoder *encoder);
106 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
107 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
108 static FLAC__StreamEncoderWriteStatus write_frame_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples);
109 static void update_metadata_(const FLAC__StreamEncoder *encoder);
110 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
111 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
113 static FLAC__bool process_subframe_(
114 FLAC__StreamEncoder *encoder,
115 unsigned min_partition_order,
116 unsigned max_partition_order,
117 FLAC__bool precompute_partition_sums,
118 const FLAC__FrameHeader *frame_header,
119 unsigned subframe_bps,
120 const FLAC__int32 integer_signal[],
121 #ifndef FLAC__INTEGER_ONLY_LIBRARY
122 const FLAC__real real_signal[],
124 FLAC__Subframe *subframe[2],
125 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
126 FLAC__int32 *residual[2],
127 unsigned *best_subframe,
131 static FLAC__bool add_subframe_(
132 FLAC__StreamEncoder *encoder,
133 const FLAC__FrameHeader *frame_header,
134 unsigned subframe_bps,
135 const FLAC__Subframe *subframe,
136 FLAC__BitBuffer *frame
139 static unsigned evaluate_constant_subframe_(
140 const FLAC__int32 signal,
141 unsigned subframe_bps,
142 FLAC__Subframe *subframe
145 static unsigned evaluate_fixed_subframe_(
146 FLAC__StreamEncoder *encoder,
147 const FLAC__int32 signal[],
148 FLAC__int32 residual[],
149 FLAC__uint32 abs_residual[],
150 FLAC__uint64 abs_residual_partition_sums[],
151 unsigned raw_bits_per_partition[],
153 unsigned subframe_bps,
155 unsigned rice_parameter,
156 unsigned min_partition_order,
157 unsigned max_partition_order,
158 FLAC__bool precompute_partition_sums,
159 FLAC__bool do_escape_coding,
160 unsigned rice_parameter_search_dist,
161 FLAC__Subframe *subframe,
162 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
165 #ifndef FLAC__INTEGER_ONLY_LIBRARY
166 static unsigned evaluate_lpc_subframe_(
167 FLAC__StreamEncoder *encoder,
168 const FLAC__int32 signal[],
169 FLAC__int32 residual[],
170 FLAC__uint32 abs_residual[],
171 FLAC__uint64 abs_residual_partition_sums[],
172 unsigned raw_bits_per_partition[],
173 const FLAC__real lp_coeff[],
175 unsigned subframe_bps,
177 unsigned qlp_coeff_precision,
178 unsigned rice_parameter,
179 unsigned min_partition_order,
180 unsigned max_partition_order,
181 FLAC__bool precompute_partition_sums,
182 FLAC__bool do_escape_coding,
183 unsigned rice_parameter_search_dist,
184 FLAC__Subframe *subframe,
185 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
189 static unsigned evaluate_verbatim_subframe_(
190 const FLAC__int32 signal[],
192 unsigned subframe_bps,
193 FLAC__Subframe *subframe
196 static unsigned find_best_partition_order_(
197 struct FLAC__StreamEncoderPrivate *private_,
198 const FLAC__int32 residual[],
199 FLAC__uint32 abs_residual[],
200 FLAC__uint64 abs_residual_partition_sums[],
201 unsigned raw_bits_per_partition[],
202 unsigned residual_samples,
203 unsigned predictor_order,
204 unsigned rice_parameter,
205 unsigned min_partition_order,
206 unsigned max_partition_order,
207 FLAC__bool precompute_partition_sums,
208 FLAC__bool do_escape_coding,
209 unsigned rice_parameter_search_dist,
210 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
213 static void precompute_partition_info_sums_(
214 const FLAC__uint32 abs_residual[],
215 FLAC__uint64 abs_residual_partition_sums[],
216 unsigned residual_samples,
217 unsigned predictor_order,
218 unsigned min_partition_order,
219 unsigned max_partition_order
222 static void precompute_partition_info_escapes_(
223 const FLAC__int32 residual[],
224 unsigned raw_bits_per_partition[],
225 unsigned residual_samples,
226 unsigned predictor_order,
227 unsigned min_partition_order,
228 unsigned max_partition_order
231 #ifdef DONT_ESTIMATE_RICE_BITS
232 static FLAC__bool set_partitioned_rice_(
233 const FLAC__uint32 abs_residual[],
234 const FLAC__int32 residual[],
235 const unsigned residual_samples,
236 const unsigned predictor_order,
237 const unsigned suggested_rice_parameter,
238 const unsigned rice_parameter_search_dist,
239 const unsigned partition_order,
240 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
244 static FLAC__bool set_partitioned_rice_with_precompute_(
245 const FLAC__int32 residual[],
246 const FLAC__uint64 abs_residual_partition_sums[],
247 const unsigned raw_bits_per_partition[],
248 const unsigned residual_samples,
249 const unsigned predictor_order,
250 const unsigned suggested_rice_parameter,
251 const unsigned rice_parameter_search_dist,
252 const unsigned partition_order,
253 const FLAC__bool search_for_escapes,
254 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
258 static FLAC__bool set_partitioned_rice_(
259 const FLAC__uint32 abs_residual[],
260 const unsigned residual_samples,
261 const unsigned predictor_order,
262 const unsigned suggested_rice_parameter,
263 const unsigned rice_parameter_search_dist,
264 const unsigned partition_order,
265 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
269 static FLAC__bool set_partitioned_rice_with_precompute_(
270 const FLAC__uint32 abs_residual[],
271 const FLAC__uint64 abs_residual_partition_sums[],
272 const unsigned raw_bits_per_partition[],
273 const unsigned residual_samples,
274 const unsigned predictor_order,
275 const unsigned suggested_rice_parameter,
276 const unsigned rice_parameter_search_dist,
277 const unsigned partition_order,
278 const FLAC__bool search_for_escapes,
279 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
284 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
286 /* verify-related routines: */
287 static void append_to_verify_fifo_(
288 verify_input_fifo *fifo,
289 const FLAC__int32 * const input[],
290 unsigned input_offset,
292 unsigned wide_samples
295 static void append_to_verify_fifo_interleaved_(
296 verify_input_fifo *fifo,
297 const FLAC__int32 input[],
298 unsigned input_offset,
300 unsigned wide_samples
303 static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
304 static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
305 static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
306 static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
308 static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
309 static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
310 static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
311 static FILE *get_binary_stdout_();
314 /***********************************************************************
318 ***********************************************************************/
320 typedef struct FLAC__StreamEncoderPrivate {
321 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
322 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
323 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
324 #ifndef FLAC__INTEGER_ONLY_LIBRARY
325 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
326 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
327 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
328 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
330 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
331 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
332 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
333 FLAC__int32 *residual_workspace_mid_side[2][2];
334 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
335 FLAC__Subframe subframe_workspace_mid_side[2][2];
336 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
337 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
338 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
339 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
340 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
341 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
342 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
343 unsigned best_subframe_mid_side[2];
344 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
345 unsigned best_subframe_bits_mid_side[2];
346 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
347 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
348 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
349 FLAC__BitBuffer *frame; /* the current frame being worked on */
350 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
351 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
352 FLAC__ChannelAssignment last_channel_assignment;
353 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
354 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
355 unsigned current_sample_number;
356 unsigned current_frame_number;
357 struct FLAC__MD5Context md5context;
358 FLAC__CPUInfo cpuinfo;
359 #ifndef FLAC__INTEGER_ONLY_LIBRARY
360 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
362 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
364 #ifndef FLAC__INTEGER_ONLY_LIBRARY
365 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
366 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[]);
367 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[]);
368 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[]);
370 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
371 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
372 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
373 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
374 FLAC__bool disable_constant_subframes;
375 FLAC__bool disable_fixed_subframes;
376 FLAC__bool disable_verbatim_subframes;
377 FLAC__StreamEncoderSeekCallback seek_callback;
378 FLAC__StreamEncoderTellCallback tell_callback;
379 FLAC__StreamEncoderWriteCallback write_callback;
380 FLAC__StreamEncoderMetadataCallback metadata_callback;
381 FLAC__StreamEncoderProgressCallback progress_callback;
383 unsigned first_seekpoint_to_check;
384 FILE *file; /* only used when encoding to a file */
385 FLAC__uint64 bytes_written;
386 FLAC__uint64 samples_written;
387 unsigned frames_written;
388 unsigned total_frames_estimate;
389 /* unaligned (original) pointers to allocated data */
390 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
391 FLAC__int32 *integer_signal_mid_side_unaligned[2];
392 #ifndef FLAC__INTEGER_ONLY_LIBRARY
393 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
394 FLAC__real *real_signal_mid_side_unaligned[2];
395 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
396 FLAC__real *windowed_signal_unaligned;
398 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
399 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
400 FLAC__uint32 *abs_residual_unaligned;
401 FLAC__uint64 *abs_residual_partition_sums_unaligned;
402 unsigned *raw_bits_per_partition_unaligned;
404 * These fields have been moved here from private function local
405 * declarations merely to save stack space during encoding.
407 #ifndef FLAC__INTEGER_ONLY_LIBRARY
408 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
410 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
412 * The data for the verify section
415 FLAC__StreamDecoder *decoder;
416 EncoderStateHint state_hint;
417 FLAC__bool needs_magic_hack;
418 verify_input_fifo input_fifo;
419 verify_output output;
421 FLAC__uint64 absolute_sample;
422 unsigned frame_number;
425 FLAC__int32 expected;
429 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
430 } FLAC__StreamEncoderPrivate;
432 /***********************************************************************
434 * Public static class data
436 ***********************************************************************/
438 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
439 "FLAC__STREAM_ENCODER_OK",
440 "FLAC__STREAM_ENCODER_UNINITIALIZED",
441 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
442 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
443 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
444 "FLAC__STREAM_ENCODER_IO_ERROR",
445 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
446 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
449 FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
450 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
451 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
452 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
453 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
454 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
455 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
456 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
457 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
458 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
459 "FLAC__STREAM_ENCODER_INIT_STATUS_MID_SIDE_CHANNELS_MISMATCH",
460 "FLAC__STREAM_ENCODER_INIT_STATUS_ILLEGAL_MID_SIDE_FORCE",
461 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
462 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
463 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
464 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
467 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
468 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
469 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
472 FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
473 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
474 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
475 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
478 FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
479 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
480 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
481 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
484 /***********************************************************************
486 * Class constructor/destructor
489 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
491 FLAC__StreamEncoder *encoder;
494 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
496 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
501 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
502 if(encoder->protected_ == 0) {
507 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
508 if(encoder->private_ == 0) {
509 free(encoder->protected_);
514 encoder->private_->frame = FLAC__bitbuffer_new();
515 if(encoder->private_->frame == 0) {
516 free(encoder->private_);
517 free(encoder->protected_);
522 encoder->private_->file = 0;
524 set_defaults_(encoder);
526 encoder->private_->is_being_deleted = false;
528 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
529 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
530 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
532 for(i = 0; i < 2; i++) {
533 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
534 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
536 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
537 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
538 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
540 for(i = 0; i < 2; i++) {
541 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
542 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
545 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
546 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
547 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
549 for(i = 0; i < 2; i++) {
550 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
551 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
553 for(i = 0; i < 2; i++)
554 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
556 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
561 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
565 FLAC__ASSERT(0 != encoder);
566 FLAC__ASSERT(0 != encoder->protected_);
567 FLAC__ASSERT(0 != encoder->private_);
568 FLAC__ASSERT(0 != encoder->private_->frame);
570 encoder->private_->is_being_deleted = true;
572 FLAC__stream_encoder_finish(encoder);
574 if(0 != encoder->private_->verify.decoder)
575 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
577 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
578 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
579 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
581 for(i = 0; i < 2; i++) {
582 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
583 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
585 for(i = 0; i < 2; i++)
586 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
588 FLAC__bitbuffer_delete(encoder->private_->frame);
589 free(encoder->private_);
590 free(encoder->protected_);
594 /***********************************************************************
596 * Public class methods
598 ***********************************************************************/
600 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback write_callback, FLAC__StreamEncoderSeekCallback seek_callback, FLAC__StreamEncoderTellCallback tell_callback, FLAC__StreamEncoderMetadataCallback metadata_callback, void *client_data)
603 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
605 FLAC__ASSERT(0 != encoder);
607 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
608 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
610 if(0 == write_callback || (seek_callback && 0 == tell_callback))
611 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
613 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
614 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
616 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
617 return FLAC__STREAM_ENCODER_INIT_STATUS_MID_SIDE_CHANNELS_MISMATCH;
619 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
620 return FLAC__STREAM_ENCODER_INIT_STATUS_ILLEGAL_MID_SIDE_FORCE;
622 if(encoder->protected_->bits_per_sample >= 32)
623 encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */
625 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
626 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
628 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
629 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
631 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
632 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
634 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
635 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
637 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
638 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
640 if(encoder->protected_->qlp_coeff_precision == 0) {
641 if(encoder->protected_->bits_per_sample < 16) {
642 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
643 /* @@@ until then we'll make a guess */
644 encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
646 else if(encoder->protected_->bits_per_sample == 16) {
647 if(encoder->protected_->blocksize <= 192)
648 encoder->protected_->qlp_coeff_precision = 7;
649 else if(encoder->protected_->blocksize <= 384)
650 encoder->protected_->qlp_coeff_precision = 8;
651 else if(encoder->protected_->blocksize <= 576)
652 encoder->protected_->qlp_coeff_precision = 9;
653 else if(encoder->protected_->blocksize <= 1152)
654 encoder->protected_->qlp_coeff_precision = 10;
655 else if(encoder->protected_->blocksize <= 2304)
656 encoder->protected_->qlp_coeff_precision = 11;
657 else if(encoder->protected_->blocksize <= 4608)
658 encoder->protected_->qlp_coeff_precision = 12;
660 encoder->protected_->qlp_coeff_precision = 13;
663 if(encoder->protected_->blocksize <= 384)
664 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
665 else if(encoder->protected_->blocksize <= 1152)
666 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
668 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
670 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
672 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
673 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
675 if(encoder->protected_->streamable_subset) {
677 encoder->protected_->blocksize != 192 &&
678 encoder->protected_->blocksize != 576 &&
679 encoder->protected_->blocksize != 1152 &&
680 encoder->protected_->blocksize != 2304 &&
681 encoder->protected_->blocksize != 4608 &&
682 encoder->protected_->blocksize != 256 &&
683 encoder->protected_->blocksize != 512 &&
684 encoder->protected_->blocksize != 1024 &&
685 encoder->protected_->blocksize != 2048 &&
686 encoder->protected_->blocksize != 4096 &&
687 encoder->protected_->blocksize != 8192 &&
688 encoder->protected_->blocksize != 16384
690 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
692 encoder->protected_->sample_rate != 8000 &&
693 encoder->protected_->sample_rate != 16000 &&
694 encoder->protected_->sample_rate != 22050 &&
695 encoder->protected_->sample_rate != 24000 &&
696 encoder->protected_->sample_rate != 32000 &&
697 encoder->protected_->sample_rate != 44100 &&
698 encoder->protected_->sample_rate != 48000 &&
699 encoder->protected_->sample_rate != 96000
701 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
703 encoder->protected_->bits_per_sample != 8 &&
704 encoder->protected_->bits_per_sample != 12 &&
705 encoder->protected_->bits_per_sample != 16 &&
706 encoder->protected_->bits_per_sample != 20 &&
707 encoder->protected_->bits_per_sample != 24
709 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
710 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
711 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
713 encoder->protected_->sample_rate <= 48000 &&
715 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
716 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
719 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
723 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
724 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
725 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
726 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
728 /* validate metadata */
729 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
730 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
731 metadata_has_seektable = false;
732 metadata_has_vorbis_comment = false;
733 metadata_picture_has_type1 = false;
734 metadata_picture_has_type2 = false;
735 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
736 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
737 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
738 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
739 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
740 if(metadata_has_seektable) /* only one is allowed */
741 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
742 metadata_has_seektable = true;
743 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
744 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
746 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
747 if(metadata_has_vorbis_comment) /* only one is allowed */
748 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
749 metadata_has_vorbis_comment = true;
751 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
752 if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
753 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
755 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
756 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
757 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
758 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
759 if(metadata_picture_has_type1) /* there should only be 1 per stream */
760 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
761 metadata_picture_has_type1 = true;
762 /* standard icon must be 32x32 pixel PNG */
764 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
766 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
767 m->data.picture.width != 32 ||
768 m->data.picture.height != 32
771 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
773 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
774 if(metadata_picture_has_type2) /* there should only be 1 per stream */
775 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
776 metadata_picture_has_type2 = true;
781 encoder->private_->input_capacity = 0;
782 for(i = 0; i < encoder->protected_->channels; i++) {
783 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
784 #ifndef FLAC__INTEGER_ONLY_LIBRARY
785 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
788 for(i = 0; i < 2; i++) {
789 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
790 #ifndef FLAC__INTEGER_ONLY_LIBRARY
791 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
794 #ifndef FLAC__INTEGER_ONLY_LIBRARY
795 for(i = 0; i < encoder->protected_->num_apodizations; i++)
796 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
797 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
799 for(i = 0; i < encoder->protected_->channels; i++) {
800 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
801 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
802 encoder->private_->best_subframe[i] = 0;
804 for(i = 0; i < 2; i++) {
805 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
806 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
807 encoder->private_->best_subframe_mid_side[i] = 0;
809 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
810 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
811 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
812 #ifndef FLAC__INTEGER_ONLY_LIBRARY
813 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
815 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
816 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply÷ by hand */
817 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
818 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
819 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
820 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
821 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);
823 if(encoder->private_->loose_mid_side_stereo_frames == 0)
824 encoder->private_->loose_mid_side_stereo_frames = 1;
825 encoder->private_->loose_mid_side_stereo_frame_count = 0;
826 encoder->private_->current_sample_number = 0;
827 encoder->private_->current_frame_number = 0;
829 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
830 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? */
831 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
834 * get the CPU info and set the function pointers
836 FLAC__cpu_info(&encoder->private_->cpuinfo);
837 /* first default to the non-asm routines */
838 #ifndef FLAC__INTEGER_ONLY_LIBRARY
839 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
841 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
842 #ifndef FLAC__INTEGER_ONLY_LIBRARY
843 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
844 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
845 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
847 /* now override with asm where appropriate */
848 #ifndef FLAC__INTEGER_ONLY_LIBRARY
849 # ifndef FLAC__NO_ASM
850 if(encoder->private_->cpuinfo.use_asm) {
851 # ifdef FLAC__CPU_IA32
852 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
853 # ifdef FLAC__HAS_NASM
855 if(encoder->private_->cpuinfo.data.ia32.sse) {
856 if(encoder->protected_->max_lpc_order < 4)
857 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
858 else if(encoder->protected_->max_lpc_order < 8)
859 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
860 else if(encoder->protected_->max_lpc_order < 12)
861 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
863 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
866 # endif /* FLAC__SSE_OS */
867 if(encoder->private_->cpuinfo.data.ia32._3dnow)
868 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
870 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
871 if(encoder->private_->cpuinfo.data.ia32.mmx) {
872 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
873 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
876 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
877 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
879 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
880 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
881 # endif /* FLAC__HAS_NASM */
882 # endif /* FLAC__CPU_IA32 */
884 # endif /* !FLAC__NO_ASM */
885 #endif /* !FLAC__INTEGER_ONLY_LIBRARY */
886 /* finally override based on wide-ness if necessary */
887 if(encoder->private_->use_wide_by_block) {
888 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
891 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
892 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
894 /* set state to OK; from here on, errors are fatal and we'll override the state then */
895 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
897 encoder->private_->write_callback = write_callback;
898 encoder->private_->seek_callback = seek_callback;
899 encoder->private_->tell_callback = tell_callback;
900 encoder->private_->metadata_callback = metadata_callback;
901 encoder->private_->client_data = client_data;
903 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
904 /* the above function sets the state for us in case of an error */
905 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
908 if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
909 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
910 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
914 * Set up the verify stuff if necessary
916 if(encoder->protected_->verify) {
918 * First, set up the fifo which will hold the
919 * original signal to compare against
921 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
922 for(i = 0; i < encoder->protected_->channels; i++) {
923 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
924 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
925 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
928 encoder->private_->verify.input_fifo.tail = 0;
931 * Now set up a stream decoder for verification
933 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
934 if(0 == encoder->private_->verify.decoder) {
935 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
936 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
939 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) {
940 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
941 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
944 encoder->private_->verify.error_stats.absolute_sample = 0;
945 encoder->private_->verify.error_stats.frame_number = 0;
946 encoder->private_->verify.error_stats.channel = 0;
947 encoder->private_->verify.error_stats.sample = 0;
948 encoder->private_->verify.error_stats.expected = 0;
949 encoder->private_->verify.error_stats.got = 0;
952 * These must be done before we write any metadata, because that
953 * calls the write_callback, which uses these values.
955 encoder->private_->first_seekpoint_to_check = 0;
956 encoder->private_->samples_written = 0;
957 encoder->protected_->streaminfo_offset = 0;
958 encoder->protected_->seektable_offset = 0;
959 encoder->protected_->audio_offset = 0;
962 * write the stream header
964 if(encoder->protected_->verify)
965 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
966 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
967 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
968 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
970 if(!write_bitbuffer_(encoder, 0)) {
971 /* the above function sets the state for us in case of an error */
972 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
976 * write the STREAMINFO metadata block
978 if(encoder->protected_->verify)
979 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
980 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
981 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
982 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
983 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
984 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
985 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
986 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
987 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
988 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
989 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
990 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
991 memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
992 FLAC__MD5Init(&encoder->private_->md5context);
993 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
994 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
995 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
997 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
998 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
999 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1001 if(!write_bitbuffer_(encoder, 0)) {
1002 /* the above function sets the state for us in case of an error */
1003 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1007 * Now that the STREAMINFO block is written, we can init this to an
1008 * absurdly-high value...
1010 encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
1011 /* ... and clear this to 0 */
1012 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
1015 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1016 * if not, we will write an empty one (FLAC__add_metadata_block()
1017 * automatically supplies the vendor string).
1019 * WATCHOUT: libOggFLAC depends on us to write this block after the
1020 * STREAMINFO since that's what the mapping requires. (In the case
1021 * that metadata_has_vorbis_comment is true it will have already
1022 * insured that the metadata list is properly ordered.)
1024 if(!metadata_has_vorbis_comment) {
1025 FLAC__StreamMetadata vorbis_comment;
1026 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1027 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1028 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1029 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1030 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1031 vorbis_comment.data.vorbis_comment.num_comments = 0;
1032 vorbis_comment.data.vorbis_comment.comments = 0;
1033 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1034 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1035 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1037 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1038 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1039 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1041 if(!write_bitbuffer_(encoder, 0)) {
1042 /* the above function sets the state for us in case of an error */
1043 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1048 * write the user's metadata blocks
1050 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1051 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
1052 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1053 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1054 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1056 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1057 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1058 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1060 if(!write_bitbuffer_(encoder, 0)) {
1061 /* the above function sets the state for us in case of an error */
1062 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1066 /* now that all the metadata is written, we save the stream offset */
1067 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 */
1068 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1069 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1072 if(encoder->protected_->verify)
1073 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1075 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1078 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(FLAC__StreamEncoder *encoder, FILE *file, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data)
1080 FLAC__StreamEncoderInitStatus init_status;
1082 FLAC__ASSERT(0 != encoder);
1083 FLAC__ASSERT(0 != file);
1085 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1086 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1088 /* double protection */
1090 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1091 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1095 * To make sure that our file does not go unclosed after an error, we
1096 * must assign the FILE pointer before any further error can occur in
1100 file = get_binary_stdout_(); /* just to be safe */
1102 encoder->private_->file = file;
1104 encoder->private_->progress_callback = progress_callback;
1105 encoder->private_->bytes_written = 0;
1106 encoder->private_->samples_written = 0;
1107 encoder->private_->frames_written = 0;
1109 init_status = FLAC__stream_encoder_init_stream(encoder, file_write_callback_, file_seek_callback_, file_tell_callback_, /*metadata_callback=*/0, client_data);
1110 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1111 /* the above function sets the state for us in case of an error */
1116 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1118 FLAC__ASSERT(blocksize != 0);
1119 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1125 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(FLAC__StreamEncoder *encoder, const char *filename, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data)
1129 FLAC__ASSERT(0 != encoder);
1132 * To make sure that our file does not go unclosed after an error, we
1133 * have to do the same entrance checks here that are later performed
1134 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1136 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1137 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1139 file = filename? fopen(filename, "w+b") : stdout;
1142 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1143 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1146 return FLAC__stream_encoder_init_FILE(encoder, file, progress_callback, client_data);
1149 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
1151 FLAC__ASSERT(0 != encoder);
1152 FLAC__ASSERT(0 != encoder->private_);
1153 FLAC__ASSERT(0 != encoder->protected_);
1155 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
1158 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
1159 if(encoder->private_->current_sample_number != 0) {
1160 encoder->protected_->blocksize = encoder->private_->current_sample_number;
1161 process_frame_(encoder, true); /* true => is last frame */
1165 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
1167 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
1168 if(encoder->private_->seek_callback)
1169 update_metadata_(encoder);
1170 if(encoder->private_->metadata_callback)
1171 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1174 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
1175 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
1177 if(0 != encoder->private_->file) {
1178 if(encoder->private_->file != stdout)
1179 fclose(encoder->private_->file);
1180 encoder->private_->file = 0;
1184 set_defaults_(encoder);
1186 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1189 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
1191 FLAC__ASSERT(0 != encoder);
1192 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1194 #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1195 encoder->protected_->verify = value;
1200 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
1202 FLAC__ASSERT(0 != encoder);
1203 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1205 encoder->protected_->streamable_subset = value;
1209 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1211 FLAC__ASSERT(0 != encoder);
1212 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1214 encoder->protected_->do_mid_side_stereo = value;
1218 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1220 FLAC__ASSERT(0 != encoder);
1221 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1223 encoder->protected_->loose_mid_side_stereo = value;
1227 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
1229 FLAC__ASSERT(0 != encoder);
1230 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1232 encoder->protected_->channels = value;
1236 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
1238 FLAC__ASSERT(0 != encoder);
1239 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1241 encoder->protected_->bits_per_sample = value;
1245 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
1247 FLAC__ASSERT(0 != encoder);
1248 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1250 encoder->protected_->sample_rate = value;
1254 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1256 FLAC__ASSERT(0 != encoder);
1257 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1259 encoder->protected_->blocksize = value;
1263 FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1265 FLAC__ASSERT(0 != encoder);
1266 FLAC__ASSERT(0 != specification);
1267 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1269 #ifdef FLAC__INTEGER_ONLY_LIBRARY
1270 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1272 encoder->protected_->num_apodizations = 0;
1274 const char *s = strchr(specification, ';');
1275 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1276 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1277 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1278 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1279 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1280 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1281 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1282 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1283 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1284 else if(n==6 && 0 == strncmp("connes" , specification, n))
1285 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1286 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1287 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1288 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1289 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1290 if (stddev > 0.0 && stddev <= 0.5) {
1291 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1292 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1295 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1296 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1297 else if(n==4 && 0 == strncmp("hann" , specification, n))
1298 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1299 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1300 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1301 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1302 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1303 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1304 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1305 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1306 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1307 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1308 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1309 if (p >= 0.0 && p <= 1.0) {
1310 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1311 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1314 else if(n==5 && 0 == strncmp("welch" , specification, n))
1315 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1316 if (encoder->protected_->num_apodizations == 32)
1319 specification = s+1;
1323 if(encoder->protected_->num_apodizations == 0) {
1324 encoder->protected_->num_apodizations = 1;
1325 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1326 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1332 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1334 FLAC__ASSERT(0 != encoder);
1335 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1337 encoder->protected_->max_lpc_order = value;
1341 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1343 FLAC__ASSERT(0 != encoder);
1344 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1346 encoder->protected_->qlp_coeff_precision = value;
1350 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1352 FLAC__ASSERT(0 != encoder);
1353 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1355 encoder->protected_->do_qlp_coeff_prec_search = value;
1359 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1361 FLAC__ASSERT(0 != encoder);
1362 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1365 /*@@@ deprecated: */
1366 encoder->protected_->do_escape_coding = value;
1373 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1375 FLAC__ASSERT(0 != encoder);
1376 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1378 encoder->protected_->do_exhaustive_model_search = value;
1382 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1384 FLAC__ASSERT(0 != encoder);
1385 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1387 encoder->protected_->min_residual_partition_order = value;
1391 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1393 FLAC__ASSERT(0 != encoder);
1394 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1396 encoder->protected_->max_residual_partition_order = value;
1400 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1402 FLAC__ASSERT(0 != encoder);
1403 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1406 /*@@@ deprecated: */
1407 encoder->protected_->rice_parameter_search_dist = value;
1414 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1416 FLAC__ASSERT(0 != encoder);
1417 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1419 encoder->protected_->total_samples_estimate = value;
1423 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1425 FLAC__ASSERT(0 != encoder);
1426 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1428 encoder->protected_->metadata = metadata;
1429 encoder->protected_->num_metadata_blocks = num_blocks;
1430 if(0 != metadata && num_blocks > 0) {
1432 for(i = 0; i < num_blocks; i++) {
1433 if(0 != metadata[i] && metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
1434 encoder->private_->seek_table = &metadata[i]->data.seek_table;
1435 break; /* take only the first one */
1443 * These three functions are not static, but not publically exposed in
1444 * include/FLAC/ either. They are used by the test suite.
1446 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1448 FLAC__ASSERT(0 != encoder);
1449 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1451 encoder->private_->disable_constant_subframes = value;
1455 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1457 FLAC__ASSERT(0 != encoder);
1458 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1460 encoder->private_->disable_fixed_subframes = value;
1464 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1466 FLAC__ASSERT(0 != encoder);
1467 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1469 encoder->private_->disable_verbatim_subframes = value;
1473 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1475 FLAC__ASSERT(0 != encoder);
1476 return encoder->protected_->state;
1479 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1481 FLAC__ASSERT(0 != encoder);
1482 if(encoder->protected_->verify)
1483 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1485 return FLAC__STREAM_DECODER_UNINITIALIZED;
1488 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1490 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1491 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1493 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
1496 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)
1498 FLAC__ASSERT(0 != encoder);
1499 if(0 != absolute_sample)
1500 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1501 if(0 != frame_number)
1502 *frame_number = encoder->private_->verify.error_stats.frame_number;
1504 *channel = encoder->private_->verify.error_stats.channel;
1506 *sample = encoder->private_->verify.error_stats.sample;
1508 *expected = encoder->private_->verify.error_stats.expected;
1510 *got = encoder->private_->verify.error_stats.got;
1513 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1515 FLAC__ASSERT(0 != encoder);
1516 return encoder->protected_->verify;
1519 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1521 FLAC__ASSERT(0 != encoder);
1522 return encoder->protected_->streamable_subset;
1525 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1527 FLAC__ASSERT(0 != encoder);
1528 return encoder->protected_->do_mid_side_stereo;
1531 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1533 FLAC__ASSERT(0 != encoder);
1534 return encoder->protected_->loose_mid_side_stereo;
1537 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1539 FLAC__ASSERT(0 != encoder);
1540 return encoder->protected_->channels;
1543 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1545 FLAC__ASSERT(0 != encoder);
1546 return encoder->protected_->bits_per_sample;
1549 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1551 FLAC__ASSERT(0 != encoder);
1552 return encoder->protected_->sample_rate;
1555 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1557 FLAC__ASSERT(0 != encoder);
1558 return encoder->protected_->blocksize;
1561 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1563 FLAC__ASSERT(0 != encoder);
1564 return encoder->protected_->max_lpc_order;
1567 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1569 FLAC__ASSERT(0 != encoder);
1570 return encoder->protected_->qlp_coeff_precision;
1573 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1575 FLAC__ASSERT(0 != encoder);
1576 return encoder->protected_->do_qlp_coeff_prec_search;
1579 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1581 FLAC__ASSERT(0 != encoder);
1582 return encoder->protected_->do_escape_coding;
1585 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1587 FLAC__ASSERT(0 != encoder);
1588 return encoder->protected_->do_exhaustive_model_search;
1591 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1593 FLAC__ASSERT(0 != encoder);
1594 return encoder->protected_->min_residual_partition_order;
1597 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1599 FLAC__ASSERT(0 != encoder);
1600 return encoder->protected_->max_residual_partition_order;
1603 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1605 FLAC__ASSERT(0 != encoder);
1606 return encoder->protected_->rice_parameter_search_dist;
1609 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1611 FLAC__ASSERT(0 != encoder);
1612 return encoder->protected_->total_samples_estimate;
1615 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1617 unsigned i, j, channel;
1618 FLAC__int32 x, mid, side;
1619 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1621 FLAC__ASSERT(0 != encoder);
1622 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1626 * we have several flavors of the same basic loop, optimized for
1627 * different conditions:
1629 if(encoder->protected_->max_lpc_order > 0) {
1630 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1632 * stereo coding: unroll channel loop
1633 * with LPC: calculate floating point version of signal
1636 if(encoder->protected_->verify)
1637 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1639 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1640 x = mid = side = buffer[0][j];
1641 encoder->private_->integer_signal[0][i] = x;
1642 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1643 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1646 encoder->private_->integer_signal[1][i] = x;
1647 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1648 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1652 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1653 encoder->private_->integer_signal_mid_side[1][i] = side;
1654 encoder->private_->integer_signal_mid_side[0][i] = mid;
1655 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1656 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1657 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1659 encoder->private_->current_sample_number++;
1661 if(i == blocksize) {
1662 if(!process_frame_(encoder, false)) /* false => not last frame */
1665 } while(j < samples);
1669 * independent channel coding: buffer each channel in inner loop
1670 * with LPC: calculate floating point version of signal
1673 if(encoder->protected_->verify)
1674 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1676 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1677 for(channel = 0; channel < channels; channel++) {
1678 x = buffer[channel][j];
1679 encoder->private_->integer_signal[channel][i] = x;
1680 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1681 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1684 encoder->private_->current_sample_number++;
1686 if(i == blocksize) {
1687 if(!process_frame_(encoder, false)) /* false => not last frame */
1690 } while(j < samples);
1694 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1696 * stereo coding: unroll channel loop
1697 * without LPC: no need to calculate floating point version of signal
1700 if(encoder->protected_->verify)
1701 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1703 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1704 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
1706 encoder->private_->integer_signal[1][i] = x;
1709 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1710 encoder->private_->integer_signal_mid_side[1][i] = side;
1711 encoder->private_->integer_signal_mid_side[0][i] = mid;
1712 encoder->private_->current_sample_number++;
1714 if(i == blocksize) {
1715 if(!process_frame_(encoder, false)) /* false => not last frame */
1718 } while(j < samples);
1722 * independent channel coding: buffer each channel in inner loop
1723 * without LPC: no need to calculate floating point version of signal
1726 if(encoder->protected_->verify)
1727 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1729 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1730 for(channel = 0; channel < channels; channel++)
1731 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
1732 encoder->private_->current_sample_number++;
1734 if(i == blocksize) {
1735 if(!process_frame_(encoder, false)) /* false => not last frame */
1738 } while(j < samples);
1745 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1747 unsigned i, j, k, channel;
1748 FLAC__int32 x, mid, side;
1749 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1751 FLAC__ASSERT(0 != encoder);
1752 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1756 * we have several flavors of the same basic loop, optimized for
1757 * different conditions:
1759 if(encoder->protected_->max_lpc_order > 0) {
1760 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1762 * stereo coding: unroll channel loop
1763 * with LPC: calculate floating point version of signal
1766 if(encoder->protected_->verify)
1767 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1769 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1770 x = mid = side = buffer[k++];
1771 encoder->private_->integer_signal[0][i] = x;
1772 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1773 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1776 encoder->private_->integer_signal[1][i] = x;
1777 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1778 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1782 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1783 encoder->private_->integer_signal_mid_side[1][i] = side;
1784 encoder->private_->integer_signal_mid_side[0][i] = mid;
1785 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1786 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1787 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1789 encoder->private_->current_sample_number++;
1791 if(i == blocksize) {
1792 if(!process_frame_(encoder, false)) /* false => not last frame */
1795 } while(j < samples);
1799 * independent channel coding: buffer each channel in inner loop
1800 * with LPC: calculate floating point version of signal
1803 if(encoder->protected_->verify)
1804 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1806 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1807 for(channel = 0; channel < channels; channel++) {
1809 encoder->private_->integer_signal[channel][i] = x;
1810 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1811 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1814 encoder->private_->current_sample_number++;
1816 if(i == blocksize) {
1817 if(!process_frame_(encoder, false)) /* false => not last frame */
1820 } while(j < samples);
1824 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1826 * stereo coding: unroll channel loop
1827 * without LPC: no need to calculate floating point version of signal
1830 if(encoder->protected_->verify)
1831 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1833 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1834 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
1836 encoder->private_->integer_signal[1][i] = x;
1839 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1840 encoder->private_->integer_signal_mid_side[1][i] = side;
1841 encoder->private_->integer_signal_mid_side[0][i] = mid;
1842 encoder->private_->current_sample_number++;
1844 if(i == blocksize) {
1845 if(!process_frame_(encoder, false)) /* false => not last frame */
1848 } while(j < samples);
1852 * independent channel coding: buffer each channel in inner loop
1853 * without LPC: no need to calculate floating point version of signal
1856 if(encoder->protected_->verify)
1857 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1859 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1860 for(channel = 0; channel < channels; channel++)
1861 encoder->private_->integer_signal[channel][i] = buffer[k++];
1862 encoder->private_->current_sample_number++;
1864 if(i == blocksize) {
1865 if(!process_frame_(encoder, false)) /* false => not last frame */
1868 } while(j < samples);
1875 /***********************************************************************
1877 * Private class methods
1879 ***********************************************************************/
1881 void set_defaults_(FLAC__StreamEncoder *encoder)
1883 FLAC__ASSERT(0 != encoder);
1885 #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1886 encoder->protected_->verify = true;
1888 encoder->protected_->verify = false;
1890 encoder->protected_->streamable_subset = true;
1891 encoder->protected_->do_mid_side_stereo = false;
1892 encoder->protected_->loose_mid_side_stereo = false;
1893 encoder->protected_->channels = 2;
1894 encoder->protected_->bits_per_sample = 16;
1895 encoder->protected_->sample_rate = 44100;
1896 encoder->protected_->blocksize = 1152;
1897 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1898 encoder->protected_->num_apodizations = 1;
1899 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1900 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1902 encoder->protected_->max_lpc_order = 0;
1903 encoder->protected_->qlp_coeff_precision = 0;
1904 encoder->protected_->do_qlp_coeff_prec_search = false;
1905 encoder->protected_->do_exhaustive_model_search = false;
1906 encoder->protected_->do_escape_coding = false;
1907 encoder->protected_->min_residual_partition_order = 0;
1908 encoder->protected_->max_residual_partition_order = 0;
1909 encoder->protected_->rice_parameter_search_dist = 0;
1910 encoder->protected_->total_samples_estimate = 0;
1911 encoder->protected_->metadata = 0;
1912 encoder->protected_->num_metadata_blocks = 0;
1914 encoder->private_->seek_table = 0;
1915 encoder->private_->disable_constant_subframes = false;
1916 encoder->private_->disable_fixed_subframes = false;
1917 encoder->private_->disable_verbatim_subframes = false;
1918 encoder->private_->write_callback = 0;
1919 encoder->private_->seek_callback = 0;
1920 encoder->private_->tell_callback = 0;
1921 encoder->private_->metadata_callback = 0;
1922 encoder->private_->progress_callback = 0;
1923 encoder->private_->client_data = 0;
1926 void free_(FLAC__StreamEncoder *encoder)
1928 unsigned i, channel;
1930 FLAC__ASSERT(0 != encoder);
1931 for(i = 0; i < encoder->protected_->channels; i++) {
1932 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1933 free(encoder->private_->integer_signal_unaligned[i]);
1934 encoder->private_->integer_signal_unaligned[i] = 0;
1936 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1937 if(0 != encoder->private_->real_signal_unaligned[i]) {
1938 free(encoder->private_->real_signal_unaligned[i]);
1939 encoder->private_->real_signal_unaligned[i] = 0;
1943 for(i = 0; i < 2; i++) {
1944 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1945 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1946 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1948 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1949 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1950 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1951 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1955 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1956 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
1957 if(0 != encoder->private_->window_unaligned[i]) {
1958 free(encoder->private_->window_unaligned[i]);
1959 encoder->private_->window_unaligned[i] = 0;
1962 if(0 != encoder->private_->windowed_signal_unaligned) {
1963 free(encoder->private_->windowed_signal_unaligned);
1964 encoder->private_->windowed_signal_unaligned = 0;
1967 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1968 for(i = 0; i < 2; i++) {
1969 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1970 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1971 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1975 for(channel = 0; channel < 2; channel++) {
1976 for(i = 0; i < 2; i++) {
1977 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1978 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1979 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1983 if(0 != encoder->private_->abs_residual_unaligned) {
1984 free(encoder->private_->abs_residual_unaligned);
1985 encoder->private_->abs_residual_unaligned = 0;
1987 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1988 free(encoder->private_->abs_residual_partition_sums_unaligned);
1989 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1991 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1992 free(encoder->private_->raw_bits_per_partition_unaligned);
1993 encoder->private_->raw_bits_per_partition_unaligned = 0;
1995 if(encoder->protected_->verify) {
1996 for(i = 0; i < encoder->protected_->channels; i++) {
1997 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1998 free(encoder->private_->verify.input_fifo.data[i]);
1999 encoder->private_->verify.input_fifo.data[i] = 0;
2003 FLAC__bitbuffer_free(encoder->private_->frame);
2006 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
2009 unsigned i, channel;
2011 FLAC__ASSERT(new_size > 0);
2012 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2013 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
2015 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
2016 if(new_size <= encoder->private_->input_capacity)
2021 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2022 * requires that the input arrays (in our case the integer signals)
2023 * have a buffer of up to 3 zeroes in front (at negative indices) for
2024 * alignment purposes; we use 4 to keep the data well-aligned.
2027 for(i = 0; ok && i < encoder->protected_->channels; i++) {
2028 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
2029 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2030 if(encoder->protected_->max_lpc_order > 0)
2031 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
2033 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2034 encoder->private_->integer_signal[i] += 4;
2036 for(i = 0; ok && i < 2; i++) {
2037 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
2038 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2039 if(encoder->protected_->max_lpc_order > 0)
2040 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
2042 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2043 encoder->private_->integer_signal_mid_side[i] += 4;
2045 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2046 if(ok && encoder->protected_->max_lpc_order > 0) {
2047 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2048 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2049 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
2052 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
2053 for(i = 0; ok && i < 2; i++) {
2054 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
2057 for(channel = 0; ok && channel < 2; channel++) {
2058 for(i = 0; ok && i < 2; i++) {
2059 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
2062 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
2063 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 */
2064 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
2065 if(encoder->protected_->do_escape_coding)
2066 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
2069 encoder->private_->input_capacity = new_size;
2071 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2073 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2074 if(ok && encoder->protected_->max_lpc_order > 0) {
2075 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2076 switch(encoder->protected_->apodizations[i].type) {
2077 case FLAC__APODIZATION_BARTLETT:
2078 FLAC__window_bartlett(encoder->private_->window[i], new_size);
2080 case FLAC__APODIZATION_BARTLETT_HANN:
2081 FLAC__window_bartlett_hann(encoder->private_->window[i], new_size);
2083 case FLAC__APODIZATION_BLACKMAN:
2084 FLAC__window_blackman(encoder->private_->window[i], new_size);
2086 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2087 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_size);
2089 case FLAC__APODIZATION_CONNES:
2090 FLAC__window_connes(encoder->private_->window[i], new_size);
2092 case FLAC__APODIZATION_FLATTOP:
2093 FLAC__window_flattop(encoder->private_->window[i], new_size);
2095 case FLAC__APODIZATION_GAUSS:
2096 FLAC__window_gauss(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2098 case FLAC__APODIZATION_HAMMING:
2099 FLAC__window_hamming(encoder->private_->window[i], new_size);
2101 case FLAC__APODIZATION_HANN:
2102 FLAC__window_hann(encoder->private_->window[i], new_size);
2104 case FLAC__APODIZATION_KAISER_BESSEL:
2105 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_size);
2107 case FLAC__APODIZATION_NUTTALL:
2108 FLAC__window_nuttall(encoder->private_->window[i], new_size);
2110 case FLAC__APODIZATION_RECTANGLE:
2111 FLAC__window_rectangle(encoder->private_->window[i], new_size);
2113 case FLAC__APODIZATION_TRIANGLE:
2114 FLAC__window_triangle(encoder->private_->window[i], new_size);
2116 case FLAC__APODIZATION_TUKEY:
2117 FLAC__window_tukey(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.tukey.p);
2119 case FLAC__APODIZATION_WELCH:
2120 FLAC__window_welch(encoder->private_->window[i], new_size);
2124 /* double protection */
2125 FLAC__window_hann(encoder->private_->window[i], new_size);
2135 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
2137 const FLAC__byte *buffer;
2140 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2142 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2144 if(encoder->protected_->verify) {
2145 encoder->private_->verify.output.data = buffer;
2146 encoder->private_->verify.output.bytes = bytes;
2147 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2148 encoder->private_->verify.needs_magic_hack = true;
2151 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2152 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2153 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2154 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2160 if(write_frame_(encoder, buffer, bytes, samples) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2161 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2162 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2166 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2169 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2170 encoder->private_->streaminfo.data.stream_info.max_framesize = max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
2176 FLAC__StreamEncoderWriteStatus write_frame_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples)
2178 FLAC__StreamEncoderWriteStatus status;
2179 FLAC__uint64 output_position = 0;
2181 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2182 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2183 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2184 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2188 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2191 FLAC__MetadataType type = (buffer[0] & 0x7f);
2192 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2193 encoder->protected_->streaminfo_offset = output_position;
2194 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2195 encoder->protected_->seektable_offset = output_position;
2199 * Mark the current seek point if hit (if audio_offset == 0 that
2200 * means we're still writing metadata and haven't hit the first
2203 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2204 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2205 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2206 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2207 FLAC__uint64 test_sample;
2209 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2210 test_sample = encoder->private_->seek_table->points[i].sample_number;
2211 if(test_sample > frame_last_sample) {
2214 else if(test_sample >= frame_first_sample) {
2215 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2216 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2217 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2218 encoder->private_->first_seekpoint_to_check++;
2219 /* DO NOT: "break;" and here's why:
2220 * The seektable template may contain more than one target
2221 * sample for any given frame; we will keep looping, generating
2222 * duplicate seekpoints for them, and we'll clean it up later,
2223 * just before writing the seektable back to the metadata.
2227 encoder->private_->first_seekpoint_to_check++;
2232 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2234 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2235 encoder->private_->bytes_written += bytes;
2236 encoder->private_->samples_written += samples;
2237 /* we keep a high watermark on the number of frames written because
2238 * when the encoder goes back to write metadata, 'current_frame'
2239 * will drop back to 0.
2241 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2244 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2249 /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2250 void update_metadata_(const FLAC__StreamEncoder *encoder)
2252 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2253 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2254 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2255 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2256 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2257 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2258 FLAC__StreamEncoderSeekStatus seek_status;
2260 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2262 /* All this is based on intimate knowledge of the stream header
2263 * layout, but a change to the header format that would break this
2264 * would also break all streams encoded in the previous format.
2268 * Write MD5 signature
2271 const unsigned md5_offset =
2272 FLAC__STREAM_METADATA_HEADER_LENGTH +
2274 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2275 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2276 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2277 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2278 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2279 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2280 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2281 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2284 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2285 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2286 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2289 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2290 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2296 * Write total samples
2299 const unsigned total_samples_byte_offset =
2300 FLAC__STREAM_METADATA_HEADER_LENGTH +
2302 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2303 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2304 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2305 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2306 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2307 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2308 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2312 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2313 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2314 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2315 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2316 b[4] = (FLAC__byte)(samples & 0xFF);
2317 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) {
2318 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2319 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2322 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2323 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2329 * Write min/max framesize
2332 const unsigned min_framesize_offset =
2333 FLAC__STREAM_METADATA_HEADER_LENGTH +
2335 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2336 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2339 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2340 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2341 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2342 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2343 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2344 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2345 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) {
2346 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2347 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2350 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2351 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2359 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2362 FLAC__format_seektable_sort(encoder->private_->seek_table);
2364 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2366 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) {
2367 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2368 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2372 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2375 xx = encoder->private_->seek_table->points[i].sample_number;
2376 b[7] = (FLAC__byte)xx; xx >>= 8;
2377 b[6] = (FLAC__byte)xx; xx >>= 8;
2378 b[5] = (FLAC__byte)xx; xx >>= 8;
2379 b[4] = (FLAC__byte)xx; xx >>= 8;
2380 b[3] = (FLAC__byte)xx; xx >>= 8;
2381 b[2] = (FLAC__byte)xx; xx >>= 8;
2382 b[1] = (FLAC__byte)xx; xx >>= 8;
2383 b[0] = (FLAC__byte)xx; xx >>= 8;
2384 xx = encoder->private_->seek_table->points[i].stream_offset;
2385 b[15] = (FLAC__byte)xx; xx >>= 8;
2386 b[14] = (FLAC__byte)xx; xx >>= 8;
2387 b[13] = (FLAC__byte)xx; xx >>= 8;
2388 b[12] = (FLAC__byte)xx; xx >>= 8;
2389 b[11] = (FLAC__byte)xx; xx >>= 8;
2390 b[10] = (FLAC__byte)xx; xx >>= 8;
2391 b[9] = (FLAC__byte)xx; xx >>= 8;
2392 b[8] = (FLAC__byte)xx; xx >>= 8;
2393 x = encoder->private_->seek_table->points[i].frame_samples;
2394 b[17] = (FLAC__byte)x; x >>= 8;
2395 b[16] = (FLAC__byte)x; x >>= 8;
2396 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2397 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2404 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
2406 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2409 * Accumulate raw signal to the MD5 signature
2411 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)) {
2412 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2417 * Process the frame header and subframes into the frame bitbuffer
2419 if(!process_subframes_(encoder, is_last_frame)) {
2420 /* the above function sets the state for us in case of an error */
2425 * Zero-pad the frame to a byte_boundary
2427 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
2428 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2433 * CRC-16 the whole thing
2435 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2436 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
2441 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
2442 /* the above function sets the state for us in case of an error */
2447 * Get ready for the next frame
2449 encoder->private_->current_sample_number = 0;
2450 encoder->private_->current_frame_number++;
2451 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
2456 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
2458 FLAC__FrameHeader frame_header;
2459 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
2460 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
2463 * Calculate the min,max Rice partition orders
2466 max_partition_order = 0;
2469 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
2470 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
2472 min_partition_order = min(min_partition_order, max_partition_order);
2474 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
2479 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
2480 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2483 frame_header.blocksize = encoder->protected_->blocksize;
2484 frame_header.sample_rate = encoder->protected_->sample_rate;
2485 frame_header.channels = encoder->protected_->channels;
2486 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
2487 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
2488 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
2489 frame_header.number.frame_number = encoder->private_->current_frame_number;
2492 * Figure out what channel assignments to try
2494 if(encoder->protected_->do_mid_side_stereo) {
2495 if(encoder->protected_->loose_mid_side_stereo) {
2496 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
2497 do_independent = true;
2501 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
2502 do_mid_side = !do_independent;
2506 do_independent = true;
2511 do_independent = true;
2512 do_mid_side = false;
2515 FLAC__ASSERT(do_independent || do_mid_side);
2518 * Check for wasted bits; set effective bps for each subframe
2520 if(do_independent) {
2521 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2522 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
2523 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
2524 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
2528 FLAC__ASSERT(encoder->protected_->channels == 2);
2529 for(channel = 0; channel < 2; channel++) {
2530 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
2531 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
2532 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
2537 * First do a normal encoding pass of each independent channel
2539 if(do_independent) {
2540 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2544 min_partition_order,
2545 max_partition_order,
2546 precompute_partition_sums,
2548 encoder->private_->subframe_bps[channel],
2549 encoder->private_->integer_signal[channel],
2550 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2551 encoder->private_->real_signal[channel],
2553 encoder->private_->subframe_workspace_ptr[channel],
2554 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
2555 encoder->private_->residual_workspace[channel],
2556 encoder->private_->best_subframe+channel,
2557 encoder->private_->best_subframe_bits+channel
2565 * Now do mid and side channels if requested
2568 FLAC__ASSERT(encoder->protected_->channels == 2);
2570 for(channel = 0; channel < 2; channel++) {
2574 min_partition_order,
2575 max_partition_order,
2576 precompute_partition_sums,
2578 encoder->private_->subframe_bps_mid_side[channel],
2579 encoder->private_->integer_signal_mid_side[channel],
2580 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2581 encoder->private_->real_signal_mid_side[channel],
2583 encoder->private_->subframe_workspace_ptr_mid_side[channel],
2584 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
2585 encoder->private_->residual_workspace_mid_side[channel],
2586 encoder->private_->best_subframe_mid_side+channel,
2587 encoder->private_->best_subframe_bits_mid_side+channel
2595 * Compose the frame bitbuffer
2598 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
2599 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
2600 FLAC__ChannelAssignment channel_assignment;
2602 FLAC__ASSERT(encoder->protected_->channels == 2);
2604 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
2605 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
2608 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
2610 FLAC__ChannelAssignment ca;
2612 FLAC__ASSERT(do_independent && do_mid_side);
2614 /* We have to figure out which channel assignent results in the smallest frame */
2615 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
2616 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
2617 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
2618 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
2620 for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
2621 if(bits[ca] < min_bits) {
2622 min_bits = bits[ca];
2623 channel_assignment = ca;
2628 frame_header.channel_assignment = channel_assignment;
2630 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
2631 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2635 switch(channel_assignment) {
2636 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2637 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2638 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
2640 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2641 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2642 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2644 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2645 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2646 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
2648 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2649 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
2650 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2656 switch(channel_assignment) {
2657 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2658 left_bps = encoder->private_->subframe_bps [0];
2659 right_bps = encoder->private_->subframe_bps [1];
2661 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2662 left_bps = encoder->private_->subframe_bps [0];
2663 right_bps = encoder->private_->subframe_bps_mid_side[1];
2665 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2666 left_bps = encoder->private_->subframe_bps_mid_side[1];
2667 right_bps = encoder->private_->subframe_bps [1];
2669 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2670 left_bps = encoder->private_->subframe_bps_mid_side[0];
2671 right_bps = encoder->private_->subframe_bps_mid_side[1];
2677 /* note that encoder_add_subframe_ sets the state for us in case of an error */
2678 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
2680 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
2684 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
2685 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2689 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2690 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
2691 /* the above function sets the state for us in case of an error */
2697 if(encoder->protected_->loose_mid_side_stereo) {
2698 encoder->private_->loose_mid_side_stereo_frame_count++;
2699 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
2700 encoder->private_->loose_mid_side_stereo_frame_count = 0;
2703 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
2708 FLAC__bool process_subframe_(
2709 FLAC__StreamEncoder *encoder,
2710 unsigned min_partition_order,
2711 unsigned max_partition_order,
2712 FLAC__bool precompute_partition_sums,
2713 const FLAC__FrameHeader *frame_header,
2714 unsigned subframe_bps,
2715 const FLAC__int32 integer_signal[],
2716 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2717 const FLAC__real real_signal[],
2719 FLAC__Subframe *subframe[2],
2720 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
2721 FLAC__int32 *residual[2],
2722 unsigned *best_subframe,
2726 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2727 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
2729 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
2731 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2732 FLAC__double lpc_residual_bits_per_sample;
2733 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 */
2734 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
2735 unsigned min_lpc_order, max_lpc_order, lpc_order;
2736 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
2738 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
2739 unsigned rice_parameter;
2740 unsigned _candidate_bits, _best_bits;
2741 unsigned _best_subframe;
2743 /* verbatim subframe is the baseline against which we measure other compressed subframes */
2745 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
2746 _best_bits = UINT_MAX;
2748 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2750 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
2751 unsigned signal_is_constant = false;
2752 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);
2753 /* check for constant subframe */
2755 !encoder->private_->disable_constant_subframes &&
2756 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2757 fixed_residual_bits_per_sample[1] == 0.0
2759 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
2762 /* the above means it's possible all samples are the same value; now double-check it: */
2764 signal_is_constant = true;
2765 for(i = 1; i < frame_header->blocksize; i++) {
2766 if(integer_signal[0] != integer_signal[i]) {
2767 signal_is_constant = false;
2772 if(signal_is_constant) {
2773 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
2774 if(_candidate_bits < _best_bits) {
2775 _best_subframe = !_best_subframe;
2776 _best_bits = _candidate_bits;
2780 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
2782 if(encoder->protected_->do_exhaustive_model_search) {
2783 min_fixed_order = 0;
2784 max_fixed_order = FLAC__MAX_FIXED_ORDER;
2787 min_fixed_order = max_fixed_order = guess_fixed_order;
2789 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
2790 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2791 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
2792 continue; /* don't even try */
2793 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 */
2795 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
2796 continue; /* don't even try */
2797 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 */
2799 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2800 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2801 #ifdef DEBUG_VERBOSE
2802 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2804 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2807 evaluate_fixed_subframe_(
2810 residual[!_best_subframe],
2811 encoder->private_->abs_residual,
2812 encoder->private_->abs_residual_partition_sums,
2813 encoder->private_->raw_bits_per_partition,
2814 frame_header->blocksize,
2818 min_partition_order,
2819 max_partition_order,
2820 precompute_partition_sums,
2821 encoder->protected_->do_escape_coding,
2822 encoder->protected_->rice_parameter_search_dist,
2823 subframe[!_best_subframe],
2824 partitioned_rice_contents[!_best_subframe]
2826 if(_candidate_bits < _best_bits) {
2827 _best_subframe = !_best_subframe;
2828 _best_bits = _candidate_bits;
2833 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2835 if(encoder->protected_->max_lpc_order > 0) {
2836 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
2837 max_lpc_order = frame_header->blocksize-1;
2839 max_lpc_order = encoder->protected_->max_lpc_order;
2840 if(max_lpc_order > 0) {
2842 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
2843 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
2844 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2845 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2846 if(autoc[0] != 0.0) {
2847 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2848 if(encoder->protected_->do_exhaustive_model_search) {
2852 const unsigned guess_lpc_order =
2853 FLAC__lpc_compute_best_order(
2856 frame_header->blocksize,
2858 encoder->protected_->do_qlp_coeff_prec_search?
2859 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
2860 encoder->protected_->qlp_coeff_precision
2863 min_lpc_order = max_lpc_order = guess_lpc_order;
2865 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2866 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2867 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
2868 continue; /* don't even try */
2869 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2870 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2871 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2872 #ifdef DEBUG_VERBOSE
2873 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2875 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2877 if(encoder->protected_->do_qlp_coeff_prec_search) {
2878 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2879 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
2880 if(subframe_bps <= 17) {
2881 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2882 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
2885 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2888 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2890 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2892 evaluate_lpc_subframe_(
2895 residual[!_best_subframe],
2896 encoder->private_->abs_residual,
2897 encoder->private_->abs_residual_partition_sums,
2898 encoder->private_->raw_bits_per_partition,
2899 encoder->private_->lp_coeff[lpc_order-1],
2900 frame_header->blocksize,
2903 qlp_coeff_precision,
2905 min_partition_order,
2906 max_partition_order,
2907 precompute_partition_sums,
2908 encoder->protected_->do_escape_coding,
2909 encoder->protected_->rice_parameter_search_dist,
2910 subframe[!_best_subframe],
2911 partitioned_rice_contents[!_best_subframe]
2913 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2914 if(_candidate_bits < _best_bits) {
2915 _best_subframe = !_best_subframe;
2916 _best_bits = _candidate_bits;
2925 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
2929 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2930 if(_best_bits == UINT_MAX) {
2931 FLAC__ASSERT(_best_subframe == 0);
2932 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2935 *best_subframe = _best_subframe;
2936 *best_bits = _best_bits;
2941 FLAC__bool add_subframe_(
2942 FLAC__StreamEncoder *encoder,
2943 const FLAC__FrameHeader *frame_header,
2944 unsigned subframe_bps,
2945 const FLAC__Subframe *subframe,
2946 FLAC__BitBuffer *frame
2949 switch(subframe->type) {
2950 case FLAC__SUBFRAME_TYPE_CONSTANT:
2951 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
2952 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2956 case FLAC__SUBFRAME_TYPE_FIXED:
2957 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2958 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2962 case FLAC__SUBFRAME_TYPE_LPC:
2963 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2964 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2968 case FLAC__SUBFRAME_TYPE_VERBATIM:
2969 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2970 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2981 unsigned evaluate_constant_subframe_(
2982 const FLAC__int32 signal,
2983 unsigned subframe_bps,
2984 FLAC__Subframe *subframe
2987 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2988 subframe->data.constant.value = signal;
2990 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
2993 unsigned evaluate_fixed_subframe_(
2994 FLAC__StreamEncoder *encoder,
2995 const FLAC__int32 signal[],
2996 FLAC__int32 residual[],
2997 FLAC__uint32 abs_residual[],
2998 FLAC__uint64 abs_residual_partition_sums[],
2999 unsigned raw_bits_per_partition[],
3001 unsigned subframe_bps,
3003 unsigned rice_parameter,
3004 unsigned min_partition_order,
3005 unsigned max_partition_order,
3006 FLAC__bool precompute_partition_sums,
3007 FLAC__bool do_escape_coding,
3008 unsigned rice_parameter_search_dist,
3009 FLAC__Subframe *subframe,
3010 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3013 unsigned i, residual_bits;
3014 const unsigned residual_samples = blocksize - order;
3016 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3018 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3020 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3021 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3022 subframe->data.fixed.residual = residual;
3025 find_best_partition_order_(
3029 abs_residual_partition_sums,
3030 raw_bits_per_partition,
3034 min_partition_order,
3035 max_partition_order,
3036 precompute_partition_sums,
3038 rice_parameter_search_dist,
3039 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3042 subframe->data.fixed.order = order;
3043 for(i = 0; i < order; i++)
3044 subframe->data.fixed.warmup[i] = signal[i];
3046 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
3049 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3050 unsigned evaluate_lpc_subframe_(
3051 FLAC__StreamEncoder *encoder,
3052 const FLAC__int32 signal[],
3053 FLAC__int32 residual[],
3054 FLAC__uint32 abs_residual[],
3055 FLAC__uint64 abs_residual_partition_sums[],
3056 unsigned raw_bits_per_partition[],
3057 const FLAC__real lp_coeff[],
3059 unsigned subframe_bps,
3061 unsigned qlp_coeff_precision,
3062 unsigned rice_parameter,
3063 unsigned min_partition_order,
3064 unsigned max_partition_order,
3065 FLAC__bool precompute_partition_sums,
3066 FLAC__bool do_escape_coding,
3067 unsigned rice_parameter_search_dist,
3068 FLAC__Subframe *subframe,
3069 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3072 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
3073 unsigned i, residual_bits;
3074 int quantization, ret;
3075 const unsigned residual_samples = blocksize - order;
3077 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3078 if(subframe_bps <= 16) {
3079 FLAC__ASSERT(order > 0);
3080 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3081 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3084 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
3086 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3088 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3089 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3090 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3092 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3094 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3096 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3098 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3099 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3100 subframe->data.lpc.residual = residual;
3103 find_best_partition_order_(
3107 abs_residual_partition_sums,
3108 raw_bits_per_partition,
3112 min_partition_order,
3113 max_partition_order,
3114 precompute_partition_sums,
3116 rice_parameter_search_dist,
3117 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3120 subframe->data.lpc.order = order;
3121 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3122 subframe->data.lpc.quantization_level = quantization;
3123 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
3124 for(i = 0; i < order; i++)
3125 subframe->data.lpc.warmup[i] = signal[i];
3127 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
3131 unsigned evaluate_verbatim_subframe_(
3132 const FLAC__int32 signal[],
3134 unsigned subframe_bps,
3135 FLAC__Subframe *subframe
3138 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3140 subframe->data.verbatim.data = signal;
3142 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
3145 unsigned find_best_partition_order_(
3146 FLAC__StreamEncoderPrivate *private_,
3147 const FLAC__int32 residual[],
3148 FLAC__uint32 abs_residual[],
3149 FLAC__uint64 abs_residual_partition_sums[],
3150 unsigned raw_bits_per_partition[],
3151 unsigned residual_samples,
3152 unsigned predictor_order,
3153 unsigned rice_parameter,
3154 unsigned min_partition_order,
3155 unsigned max_partition_order,
3156 FLAC__bool precompute_partition_sums,
3157 FLAC__bool do_escape_coding,
3158 unsigned rice_parameter_search_dist,
3159 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3163 unsigned residual_bits, best_residual_bits = 0;
3164 unsigned residual_sample;
3165 unsigned best_parameters_index = 0;
3166 const unsigned blocksize = residual_samples + predictor_order;
3168 /* compute abs(residual) for use later */
3169 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3170 r = residual[residual_sample];
3171 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
3174 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
3175 min_partition_order = min(min_partition_order, max_partition_order);
3177 if(precompute_partition_sums) {
3178 int partition_order;
3181 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
3183 if(do_escape_coding)
3184 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
3186 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3187 #ifdef DONT_ESTIMATE_RICE_BITS
3189 set_partitioned_rice_with_precompute_(
3191 abs_residual_partition_sums+sum,
3192 raw_bits_per_partition+sum,
3196 rice_parameter_search_dist,
3197 (unsigned)partition_order,
3199 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3205 set_partitioned_rice_with_precompute_(
3207 abs_residual_partition_sums+sum,
3208 raw_bits_per_partition+sum,
3212 rice_parameter_search_dist,
3213 (unsigned)partition_order,
3215 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3221 FLAC__ASSERT(best_residual_bits != 0);
3224 sum += 1u << partition_order;
3225 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3226 best_residual_bits = residual_bits;
3227 best_parameters_index = !best_parameters_index;
3228 best_partitioned_rice->order = partition_order;
3233 unsigned partition_order;
3234 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3235 #ifdef DONT_ESTIMATE_RICE_BITS
3237 set_partitioned_rice_(
3243 rice_parameter_search_dist,
3245 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3251 set_partitioned_rice_(
3256 rice_parameter_search_dist,
3258 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3264 FLAC__ASSERT(best_residual_bits != 0);
3267 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3268 best_residual_bits = residual_bits;
3269 best_parameters_index = !best_parameters_index;
3270 best_partitioned_rice->order = partition_order;
3276 * We are allowed to de-const the pointer based on our special knowledge;
3277 * it is const to the outside world.
3280 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3281 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3282 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3283 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3286 return best_residual_bits;
3289 void precompute_partition_info_sums_(
3290 const FLAC__uint32 abs_residual[],
3291 FLAC__uint64 abs_residual_partition_sums[],
3292 unsigned residual_samples,
3293 unsigned predictor_order,
3294 unsigned min_partition_order,
3295 unsigned max_partition_order
3298 int partition_order;
3299 unsigned from_partition, to_partition = 0;
3300 const unsigned blocksize = residual_samples + predictor_order;
3302 /* first do max_partition_order */
3303 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3304 FLAC__uint64 abs_residual_partition_sum;
3306 unsigned partition, partition_sample, partition_samples, residual_sample;
3307 const unsigned partitions = 1u << partition_order;
3308 const unsigned default_partition_samples = blocksize >> partition_order;
3310 FLAC__ASSERT(default_partition_samples > predictor_order);
3312 for(partition = residual_sample = 0; partition < partitions; partition++) {
3313 partition_samples = default_partition_samples;
3315 partition_samples -= predictor_order;
3316 abs_residual_partition_sum = 0;
3317 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3318 abs_r = abs_residual[residual_sample];
3319 abs_residual_partition_sum += abs_r;
3322 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
3324 to_partition = partitions;
3328 /* now merge partitions for lower orders */
3329 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3332 const unsigned partitions = 1u << partition_order;
3333 for(i = 0; i < partitions; i++) {
3334 s = abs_residual_partition_sums[from_partition];
3336 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
3343 void precompute_partition_info_escapes_(
3344 const FLAC__int32 residual[],
3345 unsigned raw_bits_per_partition[],
3346 unsigned residual_samples,
3347 unsigned predictor_order,
3348 unsigned min_partition_order,
3349 unsigned max_partition_order
3352 int partition_order;
3353 unsigned from_partition, to_partition = 0;
3354 const unsigned blocksize = residual_samples + predictor_order;
3356 /* first do max_partition_order */
3357 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3358 FLAC__int32 r, residual_partition_min, residual_partition_max;
3359 unsigned silog2_min, silog2_max;
3360 unsigned partition, partition_sample, partition_samples, residual_sample;
3361 const unsigned partitions = 1u << partition_order;
3362 const unsigned default_partition_samples = blocksize >> partition_order;
3364 FLAC__ASSERT(default_partition_samples > predictor_order);
3366 for(partition = residual_sample = 0; partition < partitions; partition++) {
3367 partition_samples = default_partition_samples;
3369 partition_samples -= predictor_order;
3370 residual_partition_min = residual_partition_max = 0;
3371 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3372 r = residual[residual_sample];
3373 if(r < residual_partition_min)
3374 residual_partition_min = r;
3375 else if(r > residual_partition_max)
3376 residual_partition_max = r;
3379 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
3380 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
3381 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
3383 to_partition = partitions;
3384 break; /*@@@ yuck, should remove the 'for' loop instead */
3387 /* now merge partitions for lower orders */
3388 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3391 const unsigned partitions = 1u << partition_order;
3392 for(i = 0; i < partitions; i++) {
3393 m = raw_bits_per_partition[from_partition];
3395 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
3402 #ifdef VARIABLE_RICE_BITS
3403 #undef VARIABLE_RICE_BITS
3405 #ifndef DONT_ESTIMATE_RICE_BITS
3406 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
3409 #ifdef DONT_ESTIMATE_RICE_BITS
3410 FLAC__bool set_partitioned_rice_(
3411 const FLAC__uint32 abs_residual[],
3412 const FLAC__int32 residual[],
3413 const unsigned residual_samples,
3414 const unsigned predictor_order,
3415 const unsigned suggested_rice_parameter,
3416 const unsigned rice_parameter_search_dist,
3417 const unsigned partition_order,
3418 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3422 FLAC__bool set_partitioned_rice_(
3423 const FLAC__uint32 abs_residual[],
3424 const unsigned residual_samples,
3425 const unsigned predictor_order,
3426 const unsigned suggested_rice_parameter,
3427 const unsigned rice_parameter_search_dist,
3428 const unsigned partition_order,
3429 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3434 unsigned rice_parameter, partition_bits;
3435 #ifndef NO_RICE_SEARCH
3436 unsigned best_partition_bits;
3437 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3439 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
3440 unsigned *parameters;
3442 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
3444 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3445 parameters = partitioned_rice_contents->parameters;
3447 if(partition_order == 0) {
3450 #ifndef NO_RICE_SEARCH
3451 if(rice_parameter_search_dist) {
3452 if(suggested_rice_parameter < rice_parameter_search_dist)
3453 min_rice_parameter = 0;
3455 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3456 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3457 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3458 #ifdef DEBUG_VERBOSE
3459 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3461 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3465 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3467 best_partition_bits = 0xffffffff;
3468 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3470 #ifdef VARIABLE_RICE_BITS
3471 const unsigned rice_parameter_estimate = rice_parameter-1;
3472 partition_bits = (1+rice_parameter) * residual_samples;
3476 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3477 for(i = 0; i < residual_samples; i++) {
3478 #ifdef VARIABLE_RICE_BITS
3479 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
3481 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3484 #ifndef NO_RICE_SEARCH
3485 if(partition_bits < best_partition_bits) {
3486 best_rice_parameter = rice_parameter;
3487 best_partition_bits = partition_bits;
3491 parameters[0] = best_rice_parameter;
3492 bits_ += best_partition_bits;
3495 unsigned partition, residual_sample, save_residual_sample, partition_sample;
3496 unsigned partition_samples;
3497 FLAC__uint64 mean, k;
3498 const unsigned partitions = 1u << partition_order;
3499 for(partition = residual_sample = 0; partition < partitions; partition++) {
3500 partition_samples = (residual_samples+predictor_order) >> partition_order;
3501 if(partition == 0) {
3502 if(partition_samples <= predictor_order)
3505 partition_samples -= predictor_order;
3508 save_residual_sample = residual_sample;
3509 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
3510 mean += abs_residual[residual_sample];
3511 residual_sample = save_residual_sample;
3512 /* we are basically calculating the size in bits of the
3513 * average residual magnitude in the partition:
3514 * rice_parameter = floor(log2(mean/partition_samples))
3515 * 'mean' is not a good name for the variable, it is
3516 * actually the sum of magnitudes of all residual values
3517 * in the partition, so the actual mean is
3518 * mean/partition_samples
3520 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
3522 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3523 #ifdef DEBUG_VERBOSE
3524 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3526 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3529 #ifndef NO_RICE_SEARCH
3530 if(rice_parameter_search_dist) {
3531 if(rice_parameter < rice_parameter_search_dist)
3532 min_rice_parameter = 0;
3534 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3535 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
3536 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3537 #ifdef DEBUG_VERBOSE
3538 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3540 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3544 min_rice_parameter = max_rice_parameter = rice_parameter;
3546 best_partition_bits = 0xffffffff;
3547 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3549 #ifdef VARIABLE_RICE_BITS
3550 const unsigned rice_parameter_estimate = rice_parameter-1;
3551 partition_bits = (1+rice_parameter) * partition_samples;
3555 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3556 save_residual_sample = residual_sample;
3557 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3558 #ifdef VARIABLE_RICE_BITS
3559 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
3561 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3564 #ifndef NO_RICE_SEARCH
3565 if(rice_parameter != max_rice_parameter)
3566 residual_sample = save_residual_sample;
3567 if(partition_bits < best_partition_bits) {
3568 best_rice_parameter = rice_parameter;
3569 best_partition_bits = partition_bits;
3573 parameters[partition] = best_rice_parameter;
3574 bits_ += best_partition_bits;
3582 #ifdef DONT_ESTIMATE_RICE_BITS
3583 FLAC__bool set_partitioned_rice_with_precompute_(
3584 const FLAC__int32 residual[],
3585 const FLAC__uint64 abs_residual_partition_sums[],
3586 const unsigned raw_bits_per_partition[],
3587 const unsigned residual_samples,
3588 const unsigned predictor_order,
3589 const unsigned suggested_rice_parameter,
3590 const unsigned rice_parameter_search_dist,
3591 const unsigned partition_order,
3592 const FLAC__bool search_for_escapes,
3593 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3597 FLAC__bool set_partitioned_rice_with_precompute_(
3598 const FLAC__uint32 abs_residual[],
3599 const FLAC__uint64 abs_residual_partition_sums[],
3600 const unsigned raw_bits_per_partition[],
3601 const unsigned residual_samples,
3602 const unsigned predictor_order,
3603 const unsigned suggested_rice_parameter,
3604 const unsigned rice_parameter_search_dist,
3605 const unsigned partition_order,
3606 const FLAC__bool search_for_escapes,
3607 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3612 unsigned rice_parameter, partition_bits;
3613 #ifndef NO_RICE_SEARCH
3614 unsigned best_partition_bits;
3615 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3618 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
3619 unsigned *parameters, *raw_bits;
3621 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
3623 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3624 parameters = partitioned_rice_contents->parameters;
3625 raw_bits = partitioned_rice_contents->raw_bits;
3627 if(partition_order == 0) {
3630 #ifndef NO_RICE_SEARCH
3631 if(rice_parameter_search_dist) {
3632 if(suggested_rice_parameter < rice_parameter_search_dist)
3633 min_rice_parameter = 0;
3635 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3636 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3637 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3638 #ifdef DEBUG_VERBOSE
3639 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3641 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3645 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3647 best_partition_bits = 0xffffffff;
3648 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3650 #ifdef VARIABLE_RICE_BITS
3651 const unsigned rice_parameter_estimate = rice_parameter-1;
3652 partition_bits = (1+rice_parameter) * residual_samples;
3656 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3657 for(i = 0; i < residual_samples; i++) {
3658 #ifdef VARIABLE_RICE_BITS
3659 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
3661 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
3664 #ifndef NO_RICE_SEARCH
3665 if(partition_bits < best_partition_bits) {
3666 best_rice_parameter = rice_parameter;
3667 best_partition_bits = partition_bits;
3671 if(search_for_escapes) {
3672 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;
3673 if(flat_bits <= best_partition_bits) {
3674 raw_bits[0] = raw_bits_per_partition[0];
3675 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3676 best_partition_bits = flat_bits;
3679 parameters[0] = best_rice_parameter;
3680 bits_ += best_partition_bits;
3683 unsigned partition, residual_sample, save_residual_sample, partition_sample;
3684 unsigned partition_samples;
3685 FLAC__uint64 mean, k;
3686 const unsigned partitions = 1u << partition_order;
3687 for(partition = residual_sample = 0; partition < partitions; partition++) {
3688 partition_samples = (residual_samples+predictor_order) >> partition_order;
3689 if(partition == 0) {
3690 if(partition_samples <= predictor_order)
3693 partition_samples -= predictor_order;
3695 mean = abs_residual_partition_sums[partition];
3696 /* we are basically calculating the size in bits of the
3697 * average residual magnitude in the partition:
3698 * rice_parameter = floor(log2(mean/partition_samples))
3699 * 'mean' is not a good name for the variable, it is
3700 * actually the sum of magnitudes of all residual values
3701 * in the partition, so the actual mean is
3702 * mean/partition_samples
3704 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
3706 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3707 #ifdef DEBUG_VERBOSE
3708 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3710 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3713 #ifndef NO_RICE_SEARCH
3714 if(rice_parameter_search_dist) {
3715 if(rice_parameter < rice_parameter_search_dist)
3716 min_rice_parameter = 0;
3718 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3719 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
3720 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3721 #ifdef DEBUG_VERBOSE
3722 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3724 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3728 min_rice_parameter = max_rice_parameter = rice_parameter;
3730 best_partition_bits = 0xffffffff;
3731 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3733 #ifdef VARIABLE_RICE_BITS
3734 const unsigned rice_parameter_estimate = rice_parameter-1;
3735 partition_bits = (1+rice_parameter) * partition_samples;
3739 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3740 save_residual_sample = residual_sample;
3741 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3742 #ifdef VARIABLE_RICE_BITS
3743 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
3745 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
3748 #ifndef NO_RICE_SEARCH
3749 if(rice_parameter != max_rice_parameter)
3750 residual_sample = save_residual_sample;
3751 if(partition_bits < best_partition_bits) {
3752 best_rice_parameter = rice_parameter;
3753 best_partition_bits = partition_bits;
3757 if(search_for_escapes) {
3758 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;
3759 if(flat_bits <= best_partition_bits) {
3760 raw_bits[partition] = raw_bits_per_partition[partition];
3761 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3762 best_partition_bits = flat_bits;
3765 parameters[partition] = best_rice_parameter;
3766 bits_ += best_partition_bits;
3774 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
3779 for(i = 0; i < samples && !(x&1); i++)
3786 for(shift = 0; !(x&1); shift++)
3791 for(i = 0; i < samples; i++)
3792 signal[i] >>= shift;
3798 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3802 for(channel = 0; channel < channels; channel++)
3803 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3805 fifo->tail += wide_samples;
3807 FLAC__ASSERT(fifo->tail <= fifo->size);
3810 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3813 unsigned sample, wide_sample;
3814 unsigned tail = fifo->tail;
3816 sample = input_offset * channels;
3817 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3818 for(channel = 0; channel < channels; channel++)
3819 fifo->data[channel][tail] = input[sample++];
3824 FLAC__ASSERT(fifo->tail <= fifo->size);
3827 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3829 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3830 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3833 if(encoder->private_->verify.needs_magic_hack) {
3834 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3835 *bytes = FLAC__STREAM_SYNC_LENGTH;
3836 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3837 encoder->private_->verify.needs_magic_hack = false;
3840 if(encoded_bytes == 0) {
3842 * If we get here, a FIFO underflow has occurred,
3843 * which means there is a bug somewhere.
3846 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3848 else if(encoded_bytes < *bytes)
3849 *bytes = encoded_bytes;
3850 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3851 encoder->private_->verify.output.data += *bytes;
3852 encoder->private_->verify.output.bytes -= *bytes;
3855 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3858 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3860 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3862 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3863 const unsigned blocksize = frame->header.blocksize;
3864 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3866 for(channel = 0; channel < channels; channel++) {
3867 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3868 unsigned i, sample = 0;
3869 FLAC__int32 expect = 0, got = 0;
3871 for(i = 0; i < blocksize; i++) {
3872 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3874 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3875 got = (FLAC__int32)buffer[channel][i];
3879 FLAC__ASSERT(i < blocksize);
3880 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3881 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
3882 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
3883 encoder->private_->verify.error_stats.channel = channel;
3884 encoder->private_->verify.error_stats.sample = sample;
3885 encoder->private_->verify.error_stats.expected = expect;
3886 encoder->private_->verify.error_stats.got = got;
3887 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3888 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3891 /* dequeue the frame from the fifo */
3892 for(channel = 0; channel < channels; channel++) {
3893 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3895 encoder->private_->verify.input_fifo.tail -= blocksize;
3896 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3899 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3901 (void)decoder, (void)metadata, (void)client_data;
3904 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3906 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3907 (void)decoder, (void)status;
3908 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
3911 FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
3915 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
3916 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
3918 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
3921 FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
3927 offset = ftello(encoder->private_->file);
3930 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
3933 *absolute_byte_offset = (FLAC__uint64)offset;
3934 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
3938 #ifdef FLAC__VALGRIND_TESTING
3939 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
3941 size_t ret = fwrite(ptr, size, nmemb, stream);
3947 #define local__fwrite fwrite
3950 FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
3952 (void)client_data, (void)samples, (void)current_frame;
3954 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
3955 if(0 != encoder->private_->progress_callback && samples > 0)
3956 encoder->private_->progress_callback(encoder, encoder->private_->bytes_written, encoder->private_->samples_written, encoder->private_->frames_written, encoder->private_->total_frames_estimate, encoder->private_->client_data);
3957 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
3960 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
3964 * This will forcibly set stdout to binary mode (for OSes that require it)
3966 FILE *get_binary_stdout_()
3968 /* if something breaks here it is probably due to the presence or
3969 * absence of an underscore before the identifiers 'setmode',
3970 * 'fileno', and/or 'O_BINARY'; check your system header files.
3972 #if defined _MSC_VER || defined __MINGW32__
3973 _setmode(_fileno(stdout), _O_BINARY);
3974 #elif defined __CYGWIN__
3975 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
3976 setmode(_fileno(stdout), _O_BINARY);
3977 #elif defined __EMX__
3978 setmode(fileno(stdout), O_BINARY);