1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004 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.
34 #include <stdlib.h> /* for malloc() */
35 #include <string.h> /* for memcpy() */
36 #include "FLAC/assert.h"
37 #include "FLAC/stream_decoder.h"
38 #include "protected/stream_encoder.h"
39 #include "private/bitbuffer.h"
40 #include "private/bitmath.h"
41 #include "private/crc.h"
42 #include "private/cpu.h"
43 #include "private/fixed.h"
44 #include "private/format.h"
45 #include "private/lpc.h"
46 #include "private/md5.h"
47 #include "private/memory.h"
48 #include "private/stream_encoder_framing.h"
57 #define min(x,y) ((x)<(y)?(x):(y))
62 #define max(x,y) ((x)>(y)?(x):(y))
65 FLAC__int32 *data[FLAC__MAX_CHANNELS];
66 unsigned size; /* of each data[] in samples */
71 const FLAC__byte *data;
78 ENCODER_IN_METADATA = 1,
82 /***********************************************************************
84 * Private class method prototypes
86 ***********************************************************************/
88 static void set_defaults_(FLAC__StreamEncoder *encoder);
89 static void free_(FLAC__StreamEncoder *encoder);
90 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
91 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
92 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
93 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
95 static FLAC__bool process_subframe_(
96 FLAC__StreamEncoder *encoder,
97 unsigned min_partition_order,
98 unsigned max_partition_order,
99 FLAC__bool precompute_partition_sums,
100 const FLAC__FrameHeader *frame_header,
101 unsigned subframe_bps,
102 const FLAC__int32 integer_signal[],
103 const FLAC__real real_signal[],
104 FLAC__Subframe *subframe[2],
105 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
106 FLAC__int32 *residual[2],
107 unsigned *best_subframe,
111 static FLAC__bool add_subframe_(
112 FLAC__StreamEncoder *encoder,
113 const FLAC__FrameHeader *frame_header,
114 unsigned subframe_bps,
115 const FLAC__Subframe *subframe,
116 FLAC__BitBuffer *frame
119 static unsigned evaluate_constant_subframe_(
120 const FLAC__int32 signal,
121 unsigned subframe_bps,
122 FLAC__Subframe *subframe
125 static unsigned evaluate_fixed_subframe_(
126 FLAC__StreamEncoder *encoder,
127 const FLAC__int32 signal[],
128 FLAC__int32 residual[],
129 FLAC__uint32 abs_residual[],
130 FLAC__uint64 abs_residual_partition_sums[],
131 unsigned raw_bits_per_partition[],
133 unsigned subframe_bps,
135 unsigned rice_parameter,
136 unsigned min_partition_order,
137 unsigned max_partition_order,
138 FLAC__bool precompute_partition_sums,
139 FLAC__bool do_escape_coding,
140 unsigned rice_parameter_search_dist,
141 FLAC__Subframe *subframe,
142 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
145 static unsigned evaluate_lpc_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[],
152 const FLAC__real lp_coeff[],
154 unsigned subframe_bps,
156 unsigned qlp_coeff_precision,
157 unsigned rice_parameter,
158 unsigned min_partition_order,
159 unsigned max_partition_order,
160 FLAC__bool precompute_partition_sums,
161 FLAC__bool do_escape_coding,
162 unsigned rice_parameter_search_dist,
163 FLAC__Subframe *subframe,
164 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
167 static unsigned evaluate_verbatim_subframe_(
168 const FLAC__int32 signal[],
170 unsigned subframe_bps,
171 FLAC__Subframe *subframe
174 static unsigned find_best_partition_order_(
175 struct FLAC__StreamEncoderPrivate *private_,
176 const FLAC__int32 residual[],
177 FLAC__uint32 abs_residual[],
178 FLAC__uint64 abs_residual_partition_sums[],
179 unsigned raw_bits_per_partition[],
180 unsigned residual_samples,
181 unsigned predictor_order,
182 unsigned rice_parameter,
183 unsigned min_partition_order,
184 unsigned max_partition_order,
185 FLAC__bool precompute_partition_sums,
186 FLAC__bool do_escape_coding,
187 unsigned rice_parameter_search_dist,
188 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
191 static void precompute_partition_info_sums_(
192 const FLAC__uint32 abs_residual[],
193 FLAC__uint64 abs_residual_partition_sums[],
194 unsigned residual_samples,
195 unsigned predictor_order,
196 unsigned min_partition_order,
197 unsigned max_partition_order
200 static void precompute_partition_info_escapes_(
201 const FLAC__int32 residual[],
202 unsigned raw_bits_per_partition[],
203 unsigned residual_samples,
204 unsigned predictor_order,
205 unsigned min_partition_order,
206 unsigned max_partition_order
209 #ifdef DONT_ESTIMATE_RICE_BITS
210 static FLAC__bool set_partitioned_rice_(
211 const FLAC__uint32 abs_residual[],
212 const FLAC__int32 residual[],
213 const unsigned residual_samples,
214 const unsigned predictor_order,
215 const unsigned suggested_rice_parameter,
216 const unsigned rice_parameter_search_dist,
217 const unsigned partition_order,
218 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
222 static FLAC__bool set_partitioned_rice_with_precompute_(
223 const FLAC__int32 residual[],
224 const FLAC__uint64 abs_residual_partition_sums[],
225 const unsigned raw_bits_per_partition[],
226 const unsigned residual_samples,
227 const unsigned predictor_order,
228 const unsigned suggested_rice_parameter,
229 const unsigned rice_parameter_search_dist,
230 const unsigned partition_order,
231 const FLAC__bool search_for_escapes,
232 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
236 static FLAC__bool set_partitioned_rice_(
237 const FLAC__uint32 abs_residual[],
238 const unsigned residual_samples,
239 const unsigned predictor_order,
240 const unsigned suggested_rice_parameter,
241 const unsigned rice_parameter_search_dist,
242 const unsigned partition_order,
243 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
247 static FLAC__bool set_partitioned_rice_with_precompute_(
248 const FLAC__uint32 abs_residual[],
249 const FLAC__uint64 abs_residual_partition_sums[],
250 const unsigned raw_bits_per_partition[],
251 const unsigned residual_samples,
252 const unsigned predictor_order,
253 const unsigned suggested_rice_parameter,
254 const unsigned rice_parameter_search_dist,
255 const unsigned partition_order,
256 const FLAC__bool search_for_escapes,
257 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
262 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
264 /* verify-related routines: */
265 static void append_to_verify_fifo_(
266 verify_input_fifo *fifo,
267 const FLAC__int32 * const input[],
268 unsigned input_offset,
270 unsigned wide_samples
273 static void append_to_verify_fifo_interleaved_(
274 verify_input_fifo *fifo,
275 const FLAC__int32 input[],
276 unsigned input_offset,
278 unsigned wide_samples
281 static FLAC__StreamDecoderReadStatus verify_read_callback_(
282 const FLAC__StreamDecoder *decoder,
288 static FLAC__StreamDecoderWriteStatus verify_write_callback_(
289 const FLAC__StreamDecoder *decoder,
290 const FLAC__Frame *frame,
291 const FLAC__int32 * const buffer[],
295 static void verify_metadata_callback_(
296 const FLAC__StreamDecoder *decoder,
297 const FLAC__StreamMetadata *metadata,
301 static void verify_error_callback_(
302 const FLAC__StreamDecoder *decoder,
303 FLAC__StreamDecoderErrorStatus status,
308 /***********************************************************************
312 ***********************************************************************/
314 typedef struct FLAC__StreamEncoderPrivate {
315 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
316 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
317 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
318 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
319 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
320 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
321 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
322 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
323 FLAC__int32 *residual_workspace_mid_side[2][2];
324 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
325 FLAC__Subframe subframe_workspace_mid_side[2][2];
326 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
327 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
328 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
329 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
330 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
331 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
332 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
333 unsigned best_subframe_mid_side[2];
334 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
335 unsigned best_subframe_bits_mid_side[2];
336 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
337 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
338 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
339 FLAC__BitBuffer *frame; /* the current frame being worked on */
340 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
341 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
342 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
343 FLAC__ChannelAssignment last_channel_assignment;
344 FLAC__StreamMetadata metadata;
345 unsigned current_sample_number;
346 unsigned current_frame_number;
347 struct FLAC__MD5Context md5context;
348 FLAC__CPUInfo cpuinfo;
349 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
350 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
351 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[]);
352 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[]);
353 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[]);
354 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
355 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
356 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
357 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
358 FLAC__bool disable_constant_subframes;
359 FLAC__bool disable_fixed_subframes;
360 FLAC__bool disable_verbatim_subframes;
361 FLAC__StreamEncoderWriteCallback write_callback;
362 FLAC__StreamEncoderMetadataCallback metadata_callback;
364 /* unaligned (original) pointers to allocated data */
365 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
366 FLAC__int32 *integer_signal_mid_side_unaligned[2];
367 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
368 FLAC__real *real_signal_mid_side_unaligned[2];
369 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
370 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
371 FLAC__uint32 *abs_residual_unaligned;
372 FLAC__uint64 *abs_residual_partition_sums_unaligned;
373 unsigned *raw_bits_per_partition_unaligned;
375 * These fields have been moved here from private function local
376 * declarations merely to save stack space during encoding.
378 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
379 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
381 * The data for the verify section
384 FLAC__StreamDecoder *decoder;
385 EncoderStateHint state_hint;
386 FLAC__bool needs_magic_hack;
387 verify_input_fifo input_fifo;
388 verify_output output;
390 FLAC__uint64 absolute_sample;
391 unsigned frame_number;
394 FLAC__int32 expected;
398 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
399 } FLAC__StreamEncoderPrivate;
401 /***********************************************************************
403 * Public static class data
405 ***********************************************************************/
407 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
408 "FLAC__STREAM_ENCODER_OK",
409 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
410 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
411 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
412 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
413 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
414 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
415 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
416 "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
417 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
418 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
419 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
420 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
421 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
422 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
423 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
424 "FLAC__STREAM_ENCODER_INVALID_METADATA",
425 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
426 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
427 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
428 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
429 "FLAC__STREAM_ENCODER_UNINITIALIZED"
432 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
433 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
434 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
437 /***********************************************************************
439 * Class constructor/destructor
442 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
444 FLAC__StreamEncoder *encoder;
447 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
449 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
454 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
455 if(encoder->protected_ == 0) {
460 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
461 if(encoder->private_ == 0) {
462 free(encoder->protected_);
467 encoder->private_->frame = FLAC__bitbuffer_new();
468 if(encoder->private_->frame == 0) {
469 free(encoder->private_);
470 free(encoder->protected_);
475 set_defaults_(encoder);
477 encoder->private_->is_being_deleted = false;
479 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
480 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
481 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
483 for(i = 0; i < 2; i++) {
484 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
485 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
487 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
488 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
489 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
491 for(i = 0; i < 2; i++) {
492 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
493 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
496 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
497 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
498 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
500 for(i = 0; i < 2; i++) {
501 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
502 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
504 for(i = 0; i < 2; i++)
505 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
507 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
512 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
516 FLAC__ASSERT(0 != encoder);
517 FLAC__ASSERT(0 != encoder->protected_);
518 FLAC__ASSERT(0 != encoder->private_);
519 FLAC__ASSERT(0 != encoder->private_->frame);
521 encoder->private_->is_being_deleted = true;
523 FLAC__stream_encoder_finish(encoder);
525 if(0 != encoder->private_->verify.decoder)
526 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
528 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
529 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
530 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
532 for(i = 0; i < 2; i++) {
533 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
534 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
536 for(i = 0; i < 2; i++)
537 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
539 FLAC__bitbuffer_delete(encoder->private_->frame);
540 free(encoder->private_);
541 free(encoder->protected_);
545 /***********************************************************************
547 * Public class methods
549 ***********************************************************************/
551 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
554 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
556 FLAC__ASSERT(0 != encoder);
558 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
559 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
561 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
563 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
564 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
566 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
567 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
569 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
570 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
572 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
573 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
575 if(encoder->protected_->bits_per_sample >= 32)
576 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
578 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
579 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
581 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
582 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
584 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
585 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
587 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
588 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
590 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
591 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
593 if(encoder->protected_->qlp_coeff_precision == 0) {
594 if(encoder->protected_->bits_per_sample < 16) {
595 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
596 /* @@@ until then we'll make a guess */
597 encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
599 else if(encoder->protected_->bits_per_sample == 16) {
600 if(encoder->protected_->blocksize <= 192)
601 encoder->protected_->qlp_coeff_precision = 7;
602 else if(encoder->protected_->blocksize <= 384)
603 encoder->protected_->qlp_coeff_precision = 8;
604 else if(encoder->protected_->blocksize <= 576)
605 encoder->protected_->qlp_coeff_precision = 9;
606 else if(encoder->protected_->blocksize <= 1152)
607 encoder->protected_->qlp_coeff_precision = 10;
608 else if(encoder->protected_->blocksize <= 2304)
609 encoder->protected_->qlp_coeff_precision = 11;
610 else if(encoder->protected_->blocksize <= 4608)
611 encoder->protected_->qlp_coeff_precision = 12;
613 encoder->protected_->qlp_coeff_precision = 13;
616 if(encoder->protected_->blocksize <= 384)
617 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
618 else if(encoder->protected_->blocksize <= 1152)
619 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
621 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
623 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
625 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
626 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
628 if(encoder->protected_->streamable_subset) {
630 encoder->protected_->blocksize != 192 &&
631 encoder->protected_->blocksize != 576 &&
632 encoder->protected_->blocksize != 1152 &&
633 encoder->protected_->blocksize != 2304 &&
634 encoder->protected_->blocksize != 4608 &&
635 encoder->protected_->blocksize != 256 &&
636 encoder->protected_->blocksize != 512 &&
637 encoder->protected_->blocksize != 1024 &&
638 encoder->protected_->blocksize != 2048 &&
639 encoder->protected_->blocksize != 4096 &&
640 encoder->protected_->blocksize != 8192 &&
641 encoder->protected_->blocksize != 16384
643 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
645 encoder->protected_->sample_rate != 8000 &&
646 encoder->protected_->sample_rate != 16000 &&
647 encoder->protected_->sample_rate != 22050 &&
648 encoder->protected_->sample_rate != 24000 &&
649 encoder->protected_->sample_rate != 32000 &&
650 encoder->protected_->sample_rate != 44100 &&
651 encoder->protected_->sample_rate != 48000 &&
652 encoder->protected_->sample_rate != 96000
654 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
656 encoder->protected_->bits_per_sample != 8 &&
657 encoder->protected_->bits_per_sample != 12 &&
658 encoder->protected_->bits_per_sample != 16 &&
659 encoder->protected_->bits_per_sample != 20 &&
660 encoder->protected_->bits_per_sample != 24
662 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
663 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
664 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
667 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
668 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
669 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
670 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
672 /* validate metadata */
673 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
674 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
675 metadata_has_seektable = false;
676 metadata_has_vorbis_comment = false;
677 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
678 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
679 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
680 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
681 if(metadata_has_seektable) /* only one is allowed */
682 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
683 metadata_has_seektable = true;
684 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
685 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
687 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
688 if(metadata_has_vorbis_comment) /* only one is allowed */
689 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
690 metadata_has_vorbis_comment = true;
692 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
693 if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
694 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
698 encoder->private_->input_capacity = 0;
699 for(i = 0; i < encoder->protected_->channels; i++) {
700 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
701 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
703 for(i = 0; i < 2; i++) {
704 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
705 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
707 for(i = 0; i < encoder->protected_->channels; i++) {
708 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
709 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
710 encoder->private_->best_subframe[i] = 0;
712 for(i = 0; i < 2; i++) {
713 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
714 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
715 encoder->private_->best_subframe_mid_side[i] = 0;
717 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
718 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
719 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
720 encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
721 encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
722 if(encoder->private_->loose_mid_side_stereo_frames == 0)
723 encoder->private_->loose_mid_side_stereo_frames = 1;
724 encoder->private_->loose_mid_side_stereo_frame_count = 0;
725 encoder->private_->current_sample_number = 0;
726 encoder->private_->current_frame_number = 0;
728 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
729 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? */
730 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
733 * get the CPU info and set the function pointers
735 FLAC__cpu_info(&encoder->private_->cpuinfo);
736 /* first default to the non-asm routines */
737 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
738 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
739 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
740 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
741 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
742 /* now override with asm where appropriate */
744 if(encoder->private_->cpuinfo.use_asm) {
745 #ifdef FLAC__CPU_IA32
746 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
747 #ifdef FLAC__HAS_NASM
749 if(encoder->private_->cpuinfo.data.ia32.sse) {
750 if(encoder->protected_->max_lpc_order < 4)
751 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
752 else if(encoder->protected_->max_lpc_order < 8)
753 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
754 else if(encoder->protected_->max_lpc_order < 12)
755 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
757 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
761 if(encoder->private_->cpuinfo.data.ia32._3dnow)
762 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
764 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
765 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
766 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
767 if(encoder->private_->cpuinfo.data.ia32.mmx) {
768 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
769 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
772 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
773 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
779 /* finally override based on wide-ness if necessary */
780 if(encoder->private_->use_wide_by_block) {
781 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
784 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
785 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
787 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
788 /* the above function sets the state for us in case of an error */
789 return encoder->protected_->state;
792 if(!FLAC__bitbuffer_init(encoder->private_->frame))
793 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
796 * Set up the verify stuff if necessary
798 if(encoder->protected_->verify) {
800 * First, set up the fifo which will hold the
801 * original signal to compare against
803 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
804 for(i = 0; i < encoder->protected_->channels; i++) {
805 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
806 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
808 encoder->private_->verify.input_fifo.tail = 0;
811 * Now set up a stream decoder for verification
813 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
814 if(0 == encoder->private_->verify.decoder)
815 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
817 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
818 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
819 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
820 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
821 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
822 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
823 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
825 encoder->private_->verify.error_stats.absolute_sample = 0;
826 encoder->private_->verify.error_stats.frame_number = 0;
827 encoder->private_->verify.error_stats.channel = 0;
828 encoder->private_->verify.error_stats.sample = 0;
829 encoder->private_->verify.error_stats.expected = 0;
830 encoder->private_->verify.error_stats.got = 0;
833 * write the stream header
835 if(encoder->protected_->verify)
836 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
837 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
838 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
839 if(!write_bitbuffer_(encoder, 0)) {
840 /* the above function sets the state for us in case of an error */
841 return encoder->protected_->state;
845 * write the STREAMINFO metadata block
847 if(encoder->protected_->verify)
848 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
849 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
850 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
851 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
852 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
853 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
854 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
855 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
856 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
857 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
858 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
859 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
860 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
861 FLAC__MD5Init(&encoder->private_->md5context);
862 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
863 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
864 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
865 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
866 if(!write_bitbuffer_(encoder, 0)) {
867 /* the above function sets the state for us in case of an error */
868 return encoder->protected_->state;
872 * Now that the STREAMINFO block is written, we can init this to an
873 * absurdly-high value...
875 encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
876 /* ... and clear this to 0 */
877 encoder->private_->metadata.data.stream_info.total_samples = 0;
880 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
881 * if not, we will write an empty one (FLAC__add_metadata_block()
882 * automatically supplies the vendor string).
884 * WATCHOUT: libOggFLAC depends on us to write this block after the
885 * STREAMINFO since that's what the mapping requires. (In the case
886 * that metadata_has_vorbis_comment it true it will have already
887 * insured that the metadata list is properly ordered.)
889 if(!metadata_has_vorbis_comment) {
890 FLAC__StreamMetadata vorbis_comment;
891 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
892 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
893 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
894 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
895 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
896 vorbis_comment.data.vorbis_comment.num_comments = 0;
897 vorbis_comment.data.vorbis_comment.comments = 0;
898 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
899 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
900 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
901 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
902 if(!write_bitbuffer_(encoder, 0)) {
903 /* the above function sets the state for us in case of an error */
904 return encoder->protected_->state;
909 * write the user's metadata blocks
911 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
912 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
913 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
914 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
915 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
916 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
917 if(!write_bitbuffer_(encoder, 0)) {
918 /* the above function sets the state for us in case of an error */
919 return encoder->protected_->state;
923 if(encoder->protected_->verify)
924 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
926 return encoder->protected_->state;
929 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
931 FLAC__ASSERT(0 != encoder);
933 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
936 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
937 if(encoder->private_->current_sample_number != 0) {
938 encoder->protected_->blocksize = encoder->private_->current_sample_number;
939 process_frame_(encoder, true); /* true => is last frame */
943 FLAC__MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
945 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
946 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
949 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
950 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
953 set_defaults_(encoder);
955 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
958 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
960 FLAC__ASSERT(0 != encoder);
961 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
963 encoder->protected_->verify = value;
967 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
969 FLAC__ASSERT(0 != encoder);
970 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
972 encoder->protected_->streamable_subset = value;
976 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
978 FLAC__ASSERT(0 != encoder);
979 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
981 encoder->protected_->do_mid_side_stereo = value;
985 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
987 FLAC__ASSERT(0 != encoder);
988 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
990 encoder->protected_->loose_mid_side_stereo = value;
994 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
996 FLAC__ASSERT(0 != encoder);
997 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
999 encoder->protected_->channels = value;
1003 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
1005 FLAC__ASSERT(0 != encoder);
1006 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1008 encoder->protected_->bits_per_sample = value;
1012 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
1014 FLAC__ASSERT(0 != encoder);
1015 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1017 encoder->protected_->sample_rate = value;
1021 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1023 FLAC__ASSERT(0 != encoder);
1024 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1026 encoder->protected_->blocksize = value;
1030 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1032 FLAC__ASSERT(0 != encoder);
1033 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1035 encoder->protected_->max_lpc_order = value;
1039 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1041 FLAC__ASSERT(0 != encoder);
1042 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1044 encoder->protected_->qlp_coeff_precision = value;
1048 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1050 FLAC__ASSERT(0 != encoder);
1051 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1053 encoder->protected_->do_qlp_coeff_prec_search = value;
1057 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1059 FLAC__ASSERT(0 != encoder);
1060 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1063 /*@@@ deprecated: */
1064 encoder->protected_->do_escape_coding = value;
1071 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1073 FLAC__ASSERT(0 != encoder);
1074 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1076 encoder->protected_->do_exhaustive_model_search = value;
1080 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1082 FLAC__ASSERT(0 != encoder);
1083 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1085 encoder->protected_->min_residual_partition_order = value;
1089 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1091 FLAC__ASSERT(0 != encoder);
1092 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1094 encoder->protected_->max_residual_partition_order = value;
1098 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1100 FLAC__ASSERT(0 != encoder);
1101 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1104 /*@@@ deprecated: */
1105 encoder->protected_->rice_parameter_search_dist = value;
1112 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1114 FLAC__ASSERT(0 != encoder);
1115 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1117 encoder->protected_->total_samples_estimate = value;
1121 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1123 FLAC__ASSERT(0 != encoder);
1124 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1126 encoder->protected_->metadata = metadata;
1127 encoder->protected_->num_metadata_blocks = num_blocks;
1131 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
1133 FLAC__ASSERT(0 != encoder);
1134 FLAC__ASSERT(0 != value);
1135 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1137 encoder->private_->write_callback = value;
1141 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
1143 FLAC__ASSERT(0 != encoder);
1144 FLAC__ASSERT(0 != value);
1145 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1147 encoder->private_->metadata_callback = value;
1151 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
1153 FLAC__ASSERT(0 != encoder);
1154 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1156 encoder->private_->client_data = value;
1161 * These three functions are not static, but not publically exposed in
1162 * include/FLAC/ either. They are used by the test suite.
1164 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1166 FLAC__ASSERT(0 != encoder);
1167 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1169 encoder->private_->disable_constant_subframes = value;
1173 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1175 FLAC__ASSERT(0 != encoder);
1176 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1178 encoder->private_->disable_fixed_subframes = value;
1182 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1184 FLAC__ASSERT(0 != encoder);
1185 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1187 encoder->private_->disable_verbatim_subframes = value;
1191 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1193 FLAC__ASSERT(0 != encoder);
1194 return encoder->protected_->state;
1197 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1199 FLAC__ASSERT(0 != encoder);
1200 if(encoder->protected_->verify)
1201 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1203 return FLAC__STREAM_DECODER_UNINITIALIZED;
1206 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1208 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1209 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1211 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
1214 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)
1216 FLAC__ASSERT(0 != encoder);
1217 if(0 != absolute_sample)
1218 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1219 if(0 != frame_number)
1220 *frame_number = encoder->private_->verify.error_stats.frame_number;
1222 *channel = encoder->private_->verify.error_stats.channel;
1224 *sample = encoder->private_->verify.error_stats.sample;
1226 *expected = encoder->private_->verify.error_stats.expected;
1228 *got = encoder->private_->verify.error_stats.got;
1231 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1233 FLAC__ASSERT(0 != encoder);
1234 return encoder->protected_->verify;
1237 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1239 FLAC__ASSERT(0 != encoder);
1240 return encoder->protected_->streamable_subset;
1243 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1245 FLAC__ASSERT(0 != encoder);
1246 return encoder->protected_->do_mid_side_stereo;
1249 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1251 FLAC__ASSERT(0 != encoder);
1252 return encoder->protected_->loose_mid_side_stereo;
1255 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1257 FLAC__ASSERT(0 != encoder);
1258 return encoder->protected_->channels;
1261 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1263 FLAC__ASSERT(0 != encoder);
1264 return encoder->protected_->bits_per_sample;
1267 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1269 FLAC__ASSERT(0 != encoder);
1270 return encoder->protected_->sample_rate;
1273 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1275 FLAC__ASSERT(0 != encoder);
1276 return encoder->protected_->blocksize;
1279 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1281 FLAC__ASSERT(0 != encoder);
1282 return encoder->protected_->max_lpc_order;
1285 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1287 FLAC__ASSERT(0 != encoder);
1288 return encoder->protected_->qlp_coeff_precision;
1291 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1293 FLAC__ASSERT(0 != encoder);
1294 return encoder->protected_->do_qlp_coeff_prec_search;
1297 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1299 FLAC__ASSERT(0 != encoder);
1300 return encoder->protected_->do_escape_coding;
1303 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1305 FLAC__ASSERT(0 != encoder);
1306 return encoder->protected_->do_exhaustive_model_search;
1309 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1311 FLAC__ASSERT(0 != encoder);
1312 return encoder->protected_->min_residual_partition_order;
1315 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1317 FLAC__ASSERT(0 != encoder);
1318 return encoder->protected_->max_residual_partition_order;
1321 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1323 FLAC__ASSERT(0 != encoder);
1324 return encoder->protected_->rice_parameter_search_dist;
1327 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1329 FLAC__ASSERT(0 != encoder);
1330 return encoder->protected_->total_samples_estimate;
1333 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1335 unsigned i, j, channel;
1336 FLAC__int32 x, mid, side;
1337 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1339 FLAC__ASSERT(0 != encoder);
1340 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1343 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1345 if(encoder->protected_->verify)
1346 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1348 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1349 x = mid = side = buffer[0][j];
1350 encoder->private_->integer_signal[0][i] = x;
1351 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1353 encoder->private_->integer_signal[1][i] = x;
1354 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1357 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1358 encoder->private_->integer_signal_mid_side[1][i] = side;
1359 encoder->private_->integer_signal_mid_side[0][i] = mid;
1360 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1361 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1362 encoder->private_->current_sample_number++;
1364 if(i == blocksize) {
1365 if(!process_frame_(encoder, false)) /* false => not last frame */
1368 } while(j < samples);
1372 if(encoder->protected_->verify)
1373 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1375 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1376 for(channel = 0; channel < channels; channel++) {
1377 x = buffer[channel][j];
1378 encoder->private_->integer_signal[channel][i] = x;
1379 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1381 encoder->private_->current_sample_number++;
1383 if(i == blocksize) {
1384 if(!process_frame_(encoder, false)) /* false => not last frame */
1387 } while(j < samples);
1393 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1395 unsigned i, j, k, channel;
1396 FLAC__int32 x, mid, side;
1397 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1399 FLAC__ASSERT(0 != encoder);
1400 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1403 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1405 if(encoder->protected_->verify)
1406 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1408 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1409 x = mid = side = buffer[k++];
1410 encoder->private_->integer_signal[0][i] = x;
1411 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1413 encoder->private_->integer_signal[1][i] = x;
1414 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1417 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1418 encoder->private_->integer_signal_mid_side[1][i] = side;
1419 encoder->private_->integer_signal_mid_side[0][i] = mid;
1420 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1421 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1422 encoder->private_->current_sample_number++;
1424 if(i == blocksize) {
1425 if(!process_frame_(encoder, false)) /* false => not last frame */
1428 } while(j < samples);
1432 if(encoder->protected_->verify)
1433 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1435 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1436 for(channel = 0; channel < channels; channel++) {
1438 encoder->private_->integer_signal[channel][i] = x;
1439 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1441 encoder->private_->current_sample_number++;
1443 if(i == blocksize) {
1444 if(!process_frame_(encoder, false)) /* false => not last frame */
1447 } while(j < samples);
1453 /***********************************************************************
1455 * Private class methods
1457 ***********************************************************************/
1459 void set_defaults_(FLAC__StreamEncoder *encoder)
1461 FLAC__ASSERT(0 != encoder);
1463 encoder->protected_->verify = false;
1464 encoder->protected_->streamable_subset = true;
1465 encoder->protected_->do_mid_side_stereo = false;
1466 encoder->protected_->loose_mid_side_stereo = false;
1467 encoder->protected_->channels = 2;
1468 encoder->protected_->bits_per_sample = 16;
1469 encoder->protected_->sample_rate = 44100;
1470 encoder->protected_->blocksize = 1152;
1471 encoder->protected_->max_lpc_order = 0;
1472 encoder->protected_->qlp_coeff_precision = 0;
1473 encoder->protected_->do_qlp_coeff_prec_search = false;
1474 encoder->protected_->do_exhaustive_model_search = false;
1475 encoder->protected_->do_escape_coding = false;
1476 encoder->protected_->min_residual_partition_order = 0;
1477 encoder->protected_->max_residual_partition_order = 0;
1478 encoder->protected_->rice_parameter_search_dist = 0;
1479 encoder->protected_->total_samples_estimate = 0;
1480 encoder->protected_->metadata = 0;
1481 encoder->protected_->num_metadata_blocks = 0;
1483 encoder->private_->disable_constant_subframes = false;
1484 encoder->private_->disable_fixed_subframes = false;
1485 encoder->private_->disable_verbatim_subframes = false;
1486 encoder->private_->write_callback = 0;
1487 encoder->private_->metadata_callback = 0;
1488 encoder->private_->client_data = 0;
1491 void free_(FLAC__StreamEncoder *encoder)
1493 unsigned i, channel;
1495 FLAC__ASSERT(0 != encoder);
1496 for(i = 0; i < encoder->protected_->channels; i++) {
1497 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1498 free(encoder->private_->integer_signal_unaligned[i]);
1499 encoder->private_->integer_signal_unaligned[i] = 0;
1501 if(0 != encoder->private_->real_signal_unaligned[i]) {
1502 free(encoder->private_->real_signal_unaligned[i]);
1503 encoder->private_->real_signal_unaligned[i] = 0;
1506 for(i = 0; i < 2; i++) {
1507 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1508 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1509 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1511 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1512 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1513 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1516 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1517 for(i = 0; i < 2; i++) {
1518 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1519 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1520 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1524 for(channel = 0; channel < 2; channel++) {
1525 for(i = 0; i < 2; i++) {
1526 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1527 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1528 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1532 if(0 != encoder->private_->abs_residual_unaligned) {
1533 free(encoder->private_->abs_residual_unaligned);
1534 encoder->private_->abs_residual_unaligned = 0;
1536 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1537 free(encoder->private_->abs_residual_partition_sums_unaligned);
1538 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1540 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1541 free(encoder->private_->raw_bits_per_partition_unaligned);
1542 encoder->private_->raw_bits_per_partition_unaligned = 0;
1544 if(encoder->protected_->verify) {
1545 for(i = 0; i < encoder->protected_->channels; i++) {
1546 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1547 free(encoder->private_->verify.input_fifo.data[i]);
1548 encoder->private_->verify.input_fifo.data[i] = 0;
1552 FLAC__bitbuffer_free(encoder->private_->frame);
1555 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
1558 unsigned i, channel;
1560 FLAC__ASSERT(new_size > 0);
1561 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1562 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1564 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1565 if(new_size <= encoder->private_->input_capacity)
1570 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1571 * requires that the input arrays (in our case the integer signals)
1572 * have a buffer of up to 3 zeroes in front (at negative indices) for
1573 * alignment purposes; we use 4 to keep the data well-aligned.
1576 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1577 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1578 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1579 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1580 encoder->private_->integer_signal[i] += 4;
1582 for(i = 0; ok && i < 2; i++) {
1583 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]);
1584 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]);
1585 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1586 encoder->private_->integer_signal_mid_side[i] += 4;
1588 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1589 for(i = 0; ok && i < 2; i++) {
1590 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1593 for(channel = 0; ok && channel < 2; channel++) {
1594 for(i = 0; ok && i < 2; i++) {
1595 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]);
1598 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1599 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 */
1600 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1601 if(encoder->protected_->do_escape_coding)
1602 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1605 encoder->private_->input_capacity = new_size;
1607 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1612 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
1614 const FLAC__byte *buffer;
1617 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1619 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1621 if(encoder->protected_->verify) {
1622 encoder->private_->verify.output.data = buffer;
1623 encoder->private_->verify.output.bytes = bytes;
1624 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1625 encoder->private_->verify.needs_magic_hack = true;
1628 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1629 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1630 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1631 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1637 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1638 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1639 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1643 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1646 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1647 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1653 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1655 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1658 * Accumulate raw signal to the MD5 signature
1660 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)) {
1661 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1666 * Process the frame header and subframes into the frame bitbuffer
1668 if(!process_subframes_(encoder, is_last_frame)) {
1669 /* the above function sets the state for us in case of an error */
1674 * Zero-pad the frame to a byte_boundary
1676 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1677 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1682 * CRC-16 the whole thing
1684 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1685 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1690 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1691 /* the above function sets the state for us in case of an error */
1696 * Get ready for the next frame
1698 encoder->private_->current_sample_number = 0;
1699 encoder->private_->current_frame_number++;
1700 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1705 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1707 FLAC__FrameHeader frame_header;
1708 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1709 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1712 * Calculate the min,max Rice partition orders
1715 max_partition_order = 0;
1718 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1719 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
1721 min_partition_order = min(min_partition_order, max_partition_order);
1723 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1728 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1729 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1732 frame_header.blocksize = encoder->protected_->blocksize;
1733 frame_header.sample_rate = encoder->protected_->sample_rate;
1734 frame_header.channels = encoder->protected_->channels;
1735 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1736 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1737 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1738 frame_header.number.frame_number = encoder->private_->current_frame_number;
1741 * Figure out what channel assignments to try
1743 if(encoder->protected_->do_mid_side_stereo) {
1744 if(encoder->protected_->loose_mid_side_stereo) {
1745 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1746 do_independent = true;
1750 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1751 do_mid_side = !do_independent;
1755 do_independent = true;
1760 do_independent = true;
1761 do_mid_side = false;
1764 FLAC__ASSERT(do_independent || do_mid_side);
1767 * Check for wasted bits; set effective bps for each subframe
1769 if(do_independent) {
1770 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1771 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1772 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1773 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1777 FLAC__ASSERT(encoder->protected_->channels == 2);
1778 for(channel = 0; channel < 2; channel++) {
1779 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1780 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1781 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1786 * First do a normal encoding pass of each independent channel
1788 if(do_independent) {
1789 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1793 min_partition_order,
1794 max_partition_order,
1795 precompute_partition_sums,
1797 encoder->private_->subframe_bps[channel],
1798 encoder->private_->integer_signal[channel],
1799 encoder->private_->real_signal[channel],
1800 encoder->private_->subframe_workspace_ptr[channel],
1801 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1802 encoder->private_->residual_workspace[channel],
1803 encoder->private_->best_subframe+channel,
1804 encoder->private_->best_subframe_bits+channel
1812 * Now do mid and side channels if requested
1815 FLAC__ASSERT(encoder->protected_->channels == 2);
1817 for(channel = 0; channel < 2; channel++) {
1821 min_partition_order,
1822 max_partition_order,
1823 precompute_partition_sums,
1825 encoder->private_->subframe_bps_mid_side[channel],
1826 encoder->private_->integer_signal_mid_side[channel],
1827 encoder->private_->real_signal_mid_side[channel],
1828 encoder->private_->subframe_workspace_ptr_mid_side[channel],
1829 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
1830 encoder->private_->residual_workspace_mid_side[channel],
1831 encoder->private_->best_subframe_mid_side+channel,
1832 encoder->private_->best_subframe_bits_mid_side+channel
1840 * Compose the frame bitbuffer
1843 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1844 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1845 FLAC__ChannelAssignment channel_assignment;
1847 FLAC__ASSERT(encoder->protected_->channels == 2);
1849 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1850 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1853 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1855 FLAC__ChannelAssignment ca;
1857 FLAC__ASSERT(do_independent && do_mid_side);
1859 /* We have to figure out which channel assignent results in the smallest frame */
1860 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
1861 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
1862 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
1863 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
1865 for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
1866 if(bits[ca] < min_bits) {
1867 min_bits = bits[ca];
1868 channel_assignment = ca;
1873 frame_header.channel_assignment = channel_assignment;
1875 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
1876 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1880 switch(channel_assignment) {
1881 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1882 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1883 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1885 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1886 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1887 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1889 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1890 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1891 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1893 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1894 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1895 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1901 switch(channel_assignment) {
1902 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1903 left_bps = encoder->private_->subframe_bps [0];
1904 right_bps = encoder->private_->subframe_bps [1];
1906 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1907 left_bps = encoder->private_->subframe_bps [0];
1908 right_bps = encoder->private_->subframe_bps_mid_side[1];
1910 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1911 left_bps = encoder->private_->subframe_bps_mid_side[1];
1912 right_bps = encoder->private_->subframe_bps [1];
1914 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1915 left_bps = encoder->private_->subframe_bps_mid_side[0];
1916 right_bps = encoder->private_->subframe_bps_mid_side[1];
1922 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1923 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
1925 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1929 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
1930 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1934 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1935 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
1936 /* the above function sets the state for us in case of an error */
1942 if(encoder->protected_->loose_mid_side_stereo) {
1943 encoder->private_->loose_mid_side_stereo_frame_count++;
1944 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1945 encoder->private_->loose_mid_side_stereo_frame_count = 0;
1948 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1953 FLAC__bool process_subframe_(
1954 FLAC__StreamEncoder *encoder,
1955 unsigned min_partition_order,
1956 unsigned max_partition_order,
1957 FLAC__bool precompute_partition_sums,
1958 const FLAC__FrameHeader *frame_header,
1959 unsigned subframe_bps,
1960 const FLAC__int32 integer_signal[],
1961 const FLAC__real real_signal[],
1962 FLAC__Subframe *subframe[2],
1963 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
1964 FLAC__int32 *residual[2],
1965 unsigned *best_subframe,
1969 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1970 FLAC__real lpc_residual_bits_per_sample;
1971 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 */
1972 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1973 unsigned min_lpc_order, max_lpc_order, lpc_order;
1974 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1975 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1976 unsigned rice_parameter;
1977 unsigned _candidate_bits, _best_bits;
1978 unsigned _best_subframe;
1980 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1982 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
1983 _best_bits = UINT_MAX;
1985 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1987 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1988 unsigned signal_is_constant = false;
1989 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);
1990 /* check for constant subframe */
1991 if(!encoder->private_->disable_constant_subframes && fixed_residual_bits_per_sample[1] == 0.0) {
1992 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1994 signal_is_constant = true;
1995 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1996 if(integer_signal[0] != integer_signal[i]) {
1997 signal_is_constant = false;
2002 if(signal_is_constant) {
2003 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
2004 if(_candidate_bits < _best_bits) {
2005 _best_subframe = !_best_subframe;
2006 _best_bits = _candidate_bits;
2010 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
2012 if(encoder->protected_->do_exhaustive_model_search) {
2013 min_fixed_order = 0;
2014 max_fixed_order = FLAC__MAX_FIXED_ORDER;
2017 min_fixed_order = max_fixed_order = guess_fixed_order;
2019 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
2020 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
2021 continue; /* don't even try */
2022 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 */
2023 #ifndef FLAC__SYMMETRIC_RICE
2024 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2026 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2027 #ifdef DEBUG_VERBOSE
2028 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2030 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2033 evaluate_fixed_subframe_(
2036 residual[!_best_subframe],
2037 encoder->private_->abs_residual,
2038 encoder->private_->abs_residual_partition_sums,
2039 encoder->private_->raw_bits_per_partition,
2040 frame_header->blocksize,
2044 min_partition_order,
2045 max_partition_order,
2046 precompute_partition_sums,
2047 encoder->protected_->do_escape_coding,
2048 encoder->protected_->rice_parameter_search_dist,
2049 subframe[!_best_subframe],
2050 partitioned_rice_contents[!_best_subframe]
2052 if(_candidate_bits < _best_bits) {
2053 _best_subframe = !_best_subframe;
2054 _best_bits = _candidate_bits;
2060 if(encoder->protected_->max_lpc_order > 0) {
2061 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
2062 max_lpc_order = frame_header->blocksize-1;
2064 max_lpc_order = encoder->protected_->max_lpc_order;
2065 if(max_lpc_order > 0) {
2066 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2067 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2068 if(autoc[0] != 0.0) {
2069 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2070 if(encoder->protected_->do_exhaustive_model_search) {
2074 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
2075 min_lpc_order = max_lpc_order = guess_lpc_order;
2077 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2078 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2079 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
2080 continue; /* don't even try */
2081 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2082 #ifndef FLAC__SYMMETRIC_RICE
2083 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2085 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2086 #ifdef DEBUG_VERBOSE
2087 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2089 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2091 if(encoder->protected_->do_qlp_coeff_prec_search) {
2092 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2093 /* ensure a 32-bit datapath throughout for 16bps or less */
2094 if(subframe_bps <= 16)
2095 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2097 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2100 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2102 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2104 evaluate_lpc_subframe_(
2107 residual[!_best_subframe],
2108 encoder->private_->abs_residual,
2109 encoder->private_->abs_residual_partition_sums,
2110 encoder->private_->raw_bits_per_partition,
2111 encoder->private_->lp_coeff[lpc_order-1],
2112 frame_header->blocksize,
2115 qlp_coeff_precision,
2117 min_partition_order,
2118 max_partition_order,
2119 precompute_partition_sums,
2120 encoder->protected_->do_escape_coding,
2121 encoder->protected_->rice_parameter_search_dist,
2122 subframe[!_best_subframe],
2123 partitioned_rice_contents[!_best_subframe]
2125 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2126 if(_candidate_bits < _best_bits) {
2127 _best_subframe = !_best_subframe;
2128 _best_bits = _candidate_bits;
2139 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2140 if(_best_bits == UINT_MAX) {
2141 FLAC__ASSERT(_best_subframe == 0);
2142 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2145 *best_subframe = _best_subframe;
2146 *best_bits = _best_bits;
2151 FLAC__bool add_subframe_(
2152 FLAC__StreamEncoder *encoder,
2153 const FLAC__FrameHeader *frame_header,
2154 unsigned subframe_bps,
2155 const FLAC__Subframe *subframe,
2156 FLAC__BitBuffer *frame
2159 switch(subframe->type) {
2160 case FLAC__SUBFRAME_TYPE_CONSTANT:
2161 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
2162 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2166 case FLAC__SUBFRAME_TYPE_FIXED:
2167 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2168 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2172 case FLAC__SUBFRAME_TYPE_LPC:
2173 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2174 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2178 case FLAC__SUBFRAME_TYPE_VERBATIM:
2179 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2180 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2191 unsigned evaluate_constant_subframe_(
2192 const FLAC__int32 signal,
2193 unsigned subframe_bps,
2194 FLAC__Subframe *subframe
2197 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2198 subframe->data.constant.value = signal;
2200 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
2203 unsigned evaluate_fixed_subframe_(
2204 FLAC__StreamEncoder *encoder,
2205 const FLAC__int32 signal[],
2206 FLAC__int32 residual[],
2207 FLAC__uint32 abs_residual[],
2208 FLAC__uint64 abs_residual_partition_sums[],
2209 unsigned raw_bits_per_partition[],
2211 unsigned subframe_bps,
2213 unsigned rice_parameter,
2214 unsigned min_partition_order,
2215 unsigned max_partition_order,
2216 FLAC__bool precompute_partition_sums,
2217 FLAC__bool do_escape_coding,
2218 unsigned rice_parameter_search_dist,
2219 FLAC__Subframe *subframe,
2220 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2223 unsigned i, residual_bits;
2224 const unsigned residual_samples = blocksize - order;
2226 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2228 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2230 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2231 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2232 subframe->data.fixed.residual = residual;
2235 find_best_partition_order_(
2239 abs_residual_partition_sums,
2240 raw_bits_per_partition,
2244 min_partition_order,
2245 max_partition_order,
2246 precompute_partition_sums,
2248 rice_parameter_search_dist,
2249 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2252 subframe->data.fixed.order = order;
2253 for(i = 0; i < order; i++)
2254 subframe->data.fixed.warmup[i] = signal[i];
2256 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
2259 unsigned evaluate_lpc_subframe_(
2260 FLAC__StreamEncoder *encoder,
2261 const FLAC__int32 signal[],
2262 FLAC__int32 residual[],
2263 FLAC__uint32 abs_residual[],
2264 FLAC__uint64 abs_residual_partition_sums[],
2265 unsigned raw_bits_per_partition[],
2266 const FLAC__real lp_coeff[],
2268 unsigned subframe_bps,
2270 unsigned qlp_coeff_precision,
2271 unsigned rice_parameter,
2272 unsigned min_partition_order,
2273 unsigned max_partition_order,
2274 FLAC__bool precompute_partition_sums,
2275 FLAC__bool do_escape_coding,
2276 unsigned rice_parameter_search_dist,
2277 FLAC__Subframe *subframe,
2278 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2281 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
2282 unsigned i, residual_bits;
2283 int quantization, ret;
2284 const unsigned residual_samples = blocksize - order;
2286 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2287 if(subframe_bps <= 16) {
2288 FLAC__ASSERT(order > 0);
2289 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2290 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2293 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
2295 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2297 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2298 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2299 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2301 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2303 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2305 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2307 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2308 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2309 subframe->data.lpc.residual = residual;
2312 find_best_partition_order_(
2316 abs_residual_partition_sums,
2317 raw_bits_per_partition,
2321 min_partition_order,
2322 max_partition_order,
2323 precompute_partition_sums,
2325 rice_parameter_search_dist,
2326 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2329 subframe->data.lpc.order = order;
2330 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2331 subframe->data.lpc.quantization_level = quantization;
2332 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
2333 for(i = 0; i < order; i++)
2334 subframe->data.lpc.warmup[i] = signal[i];
2336 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;
2339 unsigned evaluate_verbatim_subframe_(
2340 const FLAC__int32 signal[],
2342 unsigned subframe_bps,
2343 FLAC__Subframe *subframe
2346 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2348 subframe->data.verbatim.data = signal;
2350 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
2353 unsigned find_best_partition_order_(
2354 FLAC__StreamEncoderPrivate *private_,
2355 const FLAC__int32 residual[],
2356 FLAC__uint32 abs_residual[],
2357 FLAC__uint64 abs_residual_partition_sums[],
2358 unsigned raw_bits_per_partition[],
2359 unsigned residual_samples,
2360 unsigned predictor_order,
2361 unsigned rice_parameter,
2362 unsigned min_partition_order,
2363 unsigned max_partition_order,
2364 FLAC__bool precompute_partition_sums,
2365 FLAC__bool do_escape_coding,
2366 unsigned rice_parameter_search_dist,
2367 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2371 unsigned residual_bits, best_residual_bits = 0;
2372 unsigned residual_sample;
2373 unsigned best_parameters_index = 0;
2374 const unsigned blocksize = residual_samples + predictor_order;
2376 /* compute abs(residual) for use later */
2377 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2378 r = residual[residual_sample];
2379 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2382 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2383 min_partition_order = min(min_partition_order, max_partition_order);
2385 if(precompute_partition_sums) {
2386 int partition_order;
2389 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2391 if(do_escape_coding)
2392 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2394 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2395 #ifdef DONT_ESTIMATE_RICE_BITS
2397 set_partitioned_rice_with_precompute_(
2399 abs_residual_partition_sums+sum,
2400 raw_bits_per_partition+sum,
2404 rice_parameter_search_dist,
2405 (unsigned)partition_order,
2407 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2413 set_partitioned_rice_with_precompute_(
2415 abs_residual_partition_sums+sum,
2416 raw_bits_per_partition+sum,
2420 rice_parameter_search_dist,
2421 (unsigned)partition_order,
2423 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2429 FLAC__ASSERT(best_residual_bits != 0);
2432 sum += 1u << partition_order;
2433 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2434 best_residual_bits = residual_bits;
2435 best_parameters_index = !best_parameters_index;
2436 best_partitioned_rice->order = partition_order;
2441 unsigned partition_order;
2442 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2443 #ifdef DONT_ESTIMATE_RICE_BITS
2445 set_partitioned_rice_(
2451 rice_parameter_search_dist,
2453 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2459 set_partitioned_rice_(
2464 rice_parameter_search_dist,
2466 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2472 FLAC__ASSERT(best_residual_bits != 0);
2475 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2476 best_residual_bits = residual_bits;
2477 best_parameters_index = !best_parameters_index;
2478 best_partitioned_rice->order = partition_order;
2484 * We are allowed to de-const the pointer based on our special knowledge;
2485 * it is const to the outside world.
2488 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2489 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2490 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2491 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2494 return best_residual_bits;
2497 void precompute_partition_info_sums_(
2498 const FLAC__uint32 abs_residual[],
2499 FLAC__uint64 abs_residual_partition_sums[],
2500 unsigned residual_samples,
2501 unsigned predictor_order,
2502 unsigned min_partition_order,
2503 unsigned max_partition_order
2506 int partition_order;
2507 unsigned from_partition, to_partition = 0;
2508 const unsigned blocksize = residual_samples + predictor_order;
2510 /* first do max_partition_order */
2511 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2512 FLAC__uint64 abs_residual_partition_sum;
2514 unsigned partition, partition_sample, partition_samples, residual_sample;
2515 const unsigned partitions = 1u << partition_order;
2516 const unsigned default_partition_samples = blocksize >> partition_order;
2518 FLAC__ASSERT(default_partition_samples > predictor_order);
2520 for(partition = residual_sample = 0; partition < partitions; partition++) {
2521 partition_samples = default_partition_samples;
2523 partition_samples -= predictor_order;
2524 abs_residual_partition_sum = 0;
2525 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2526 abs_r = abs_residual[residual_sample];
2527 abs_residual_partition_sum += abs_r;
2530 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2532 to_partition = partitions;
2536 /* now merge partitions for lower orders */
2537 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2540 const unsigned partitions = 1u << partition_order;
2541 for(i = 0; i < partitions; i++) {
2542 s = abs_residual_partition_sums[from_partition];
2544 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
2551 void precompute_partition_info_escapes_(
2552 const FLAC__int32 residual[],
2553 unsigned raw_bits_per_partition[],
2554 unsigned residual_samples,
2555 unsigned predictor_order,
2556 unsigned min_partition_order,
2557 unsigned max_partition_order
2560 int partition_order;
2561 unsigned from_partition, to_partition = 0;
2562 const unsigned blocksize = residual_samples + predictor_order;
2564 /* first do max_partition_order */
2565 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2566 FLAC__int32 r, residual_partition_min, residual_partition_max;
2567 unsigned silog2_min, silog2_max;
2568 unsigned partition, partition_sample, partition_samples, residual_sample;
2569 const unsigned partitions = 1u << partition_order;
2570 const unsigned default_partition_samples = blocksize >> partition_order;
2572 FLAC__ASSERT(default_partition_samples > predictor_order);
2574 for(partition = residual_sample = 0; partition < partitions; partition++) {
2575 partition_samples = default_partition_samples;
2577 partition_samples -= predictor_order;
2578 residual_partition_min = residual_partition_max = 0;
2579 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2580 r = residual[residual_sample];
2581 if(r < residual_partition_min)
2582 residual_partition_min = r;
2583 else if(r > residual_partition_max)
2584 residual_partition_max = r;
2587 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2588 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2589 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
2591 to_partition = partitions;
2595 /* now merge partitions for lower orders */
2596 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2599 const unsigned partitions = 1u << partition_order;
2600 for(i = 0; i < partitions; i++) {
2601 m = raw_bits_per_partition[from_partition];
2603 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2610 #ifdef VARIABLE_RICE_BITS
2611 #undef VARIABLE_RICE_BITS
2613 #ifndef DONT_ESTIMATE_RICE_BITS
2614 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
2617 #ifdef DONT_ESTIMATE_RICE_BITS
2618 FLAC__bool set_partitioned_rice_(
2619 const FLAC__uint32 abs_residual[],
2620 const FLAC__int32 residual[],
2621 const unsigned residual_samples,
2622 const unsigned predictor_order,
2623 const unsigned suggested_rice_parameter,
2624 const unsigned rice_parameter_search_dist,
2625 const unsigned partition_order,
2626 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2630 FLAC__bool set_partitioned_rice_(
2631 const FLAC__uint32 abs_residual[],
2632 const unsigned residual_samples,
2633 const unsigned predictor_order,
2634 const unsigned suggested_rice_parameter,
2635 const unsigned rice_parameter_search_dist,
2636 const unsigned partition_order,
2637 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2642 unsigned rice_parameter, partition_bits;
2643 #ifndef NO_RICE_SEARCH
2644 unsigned best_partition_bits;
2645 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2647 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2648 unsigned *parameters;
2650 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2652 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2653 parameters = partitioned_rice_contents->parameters;
2655 if(partition_order == 0) {
2658 #ifndef NO_RICE_SEARCH
2659 if(rice_parameter_search_dist) {
2660 if(suggested_rice_parameter < rice_parameter_search_dist)
2661 min_rice_parameter = 0;
2663 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2664 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2665 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2666 #ifdef DEBUG_VERBOSE
2667 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2669 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2673 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2675 best_partition_bits = 0xffffffff;
2676 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2678 #ifdef VARIABLE_RICE_BITS
2679 #ifdef FLAC__SYMMETRIC_RICE
2680 partition_bits = (2+rice_parameter) * residual_samples;
2682 const unsigned rice_parameter_estimate = rice_parameter-1;
2683 partition_bits = (1+rice_parameter) * residual_samples;
2688 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2689 for(i = 0; i < residual_samples; i++) {
2690 #ifdef VARIABLE_RICE_BITS
2691 #ifdef FLAC__SYMMETRIC_RICE
2692 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2694 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2697 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2700 #ifndef NO_RICE_SEARCH
2701 if(partition_bits < best_partition_bits) {
2702 best_rice_parameter = rice_parameter;
2703 best_partition_bits = partition_bits;
2707 parameters[0] = best_rice_parameter;
2708 bits_ += best_partition_bits;
2711 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2712 unsigned partition_samples;
2713 FLAC__uint64 mean, k;
2714 const unsigned partitions = 1u << partition_order;
2715 for(partition = residual_sample = 0; partition < partitions; partition++) {
2716 partition_samples = (residual_samples+predictor_order) >> partition_order;
2717 if(partition == 0) {
2718 if(partition_samples <= predictor_order)
2721 partition_samples -= predictor_order;
2724 save_residual_sample = residual_sample;
2725 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2726 mean += abs_residual[residual_sample];
2727 residual_sample = save_residual_sample;
2728 #ifdef FLAC__SYMMETRIC_RICE
2729 mean += partition_samples >> 1; /* for rounding effect */
2730 mean /= partition_samples;
2732 /* calc rice_parameter = floor(log2(mean)) */
2740 /* calc rice_parameter ala LOCO-I */
2741 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2744 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2745 #ifdef DEBUG_VERBOSE
2746 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2748 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2751 #ifndef NO_RICE_SEARCH
2752 if(rice_parameter_search_dist) {
2753 if(rice_parameter < rice_parameter_search_dist)
2754 min_rice_parameter = 0;
2756 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2757 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2758 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2759 #ifdef DEBUG_VERBOSE
2760 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2762 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2766 min_rice_parameter = max_rice_parameter = rice_parameter;
2768 best_partition_bits = 0xffffffff;
2769 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2771 #ifdef VARIABLE_RICE_BITS
2772 #ifdef FLAC__SYMMETRIC_RICE
2773 partition_bits = (2+rice_parameter) * partition_samples;
2775 const unsigned rice_parameter_estimate = rice_parameter-1;
2776 partition_bits = (1+rice_parameter) * partition_samples;
2781 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2782 save_residual_sample = residual_sample;
2783 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2784 #ifdef VARIABLE_RICE_BITS
2785 #ifdef FLAC__SYMMETRIC_RICE
2786 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2788 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2791 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2794 #ifndef NO_RICE_SEARCH
2795 if(rice_parameter != max_rice_parameter)
2796 residual_sample = save_residual_sample;
2797 if(partition_bits < best_partition_bits) {
2798 best_rice_parameter = rice_parameter;
2799 best_partition_bits = partition_bits;
2803 parameters[partition] = best_rice_parameter;
2804 bits_ += best_partition_bits;
2812 #ifdef DONT_ESTIMATE_RICE_BITS
2813 FLAC__bool set_partitioned_rice_with_precompute_(
2814 const FLAC__int32 residual[],
2815 const FLAC__uint64 abs_residual_partition_sums[],
2816 const unsigned raw_bits_per_partition[],
2817 const unsigned residual_samples,
2818 const unsigned predictor_order,
2819 const unsigned suggested_rice_parameter,
2820 const unsigned rice_parameter_search_dist,
2821 const unsigned partition_order,
2822 const FLAC__bool search_for_escapes,
2823 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2827 FLAC__bool set_partitioned_rice_with_precompute_(
2828 const FLAC__uint32 abs_residual[],
2829 const FLAC__uint64 abs_residual_partition_sums[],
2830 const unsigned raw_bits_per_partition[],
2831 const unsigned residual_samples,
2832 const unsigned predictor_order,
2833 const unsigned suggested_rice_parameter,
2834 const unsigned rice_parameter_search_dist,
2835 const unsigned partition_order,
2836 const FLAC__bool search_for_escapes,
2837 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2842 unsigned rice_parameter, partition_bits;
2843 #ifndef NO_RICE_SEARCH
2844 unsigned best_partition_bits;
2845 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2848 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2849 unsigned *parameters, *raw_bits;
2851 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2853 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2854 parameters = partitioned_rice_contents->parameters;
2855 raw_bits = partitioned_rice_contents->raw_bits;
2857 if(partition_order == 0) {
2860 #ifndef NO_RICE_SEARCH
2861 if(rice_parameter_search_dist) {
2862 if(suggested_rice_parameter < rice_parameter_search_dist)
2863 min_rice_parameter = 0;
2865 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2866 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2867 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2868 #ifdef DEBUG_VERBOSE
2869 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2871 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2875 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2877 best_partition_bits = 0xffffffff;
2878 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2880 #ifdef VARIABLE_RICE_BITS
2881 #ifdef FLAC__SYMMETRIC_RICE
2882 partition_bits = (2+rice_parameter) * residual_samples;
2884 const unsigned rice_parameter_estimate = rice_parameter-1;
2885 partition_bits = (1+rice_parameter) * residual_samples;
2890 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2891 for(i = 0; i < residual_samples; i++) {
2892 #ifdef VARIABLE_RICE_BITS
2893 #ifdef FLAC__SYMMETRIC_RICE
2894 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2896 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2899 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2902 #ifndef NO_RICE_SEARCH
2903 if(partition_bits < best_partition_bits) {
2904 best_rice_parameter = rice_parameter;
2905 best_partition_bits = partition_bits;
2909 if(search_for_escapes) {
2910 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;
2911 if(flat_bits <= best_partition_bits) {
2912 raw_bits[0] = raw_bits_per_partition[0];
2913 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2914 best_partition_bits = flat_bits;
2917 parameters[0] = best_rice_parameter;
2918 bits_ += best_partition_bits;
2921 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2922 unsigned partition_samples;
2923 FLAC__uint64 mean, k;
2924 const unsigned partitions = 1u << partition_order;
2925 for(partition = residual_sample = 0; partition < partitions; partition++) {
2926 partition_samples = (residual_samples+predictor_order) >> partition_order;
2927 if(partition == 0) {
2928 if(partition_samples <= predictor_order)
2931 partition_samples -= predictor_order;
2933 mean = abs_residual_partition_sums[partition];
2934 #ifdef FLAC__SYMMETRIC_RICE
2935 mean += partition_samples >> 1; /* for rounding effect */
2936 mean /= partition_samples;
2938 /* calc rice_parameter = floor(log2(mean)) */
2946 /* calc rice_parameter ala LOCO-I */
2947 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2950 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2951 #ifdef DEBUG_VERBOSE
2952 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2954 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2957 #ifndef NO_RICE_SEARCH
2958 if(rice_parameter_search_dist) {
2959 if(rice_parameter < rice_parameter_search_dist)
2960 min_rice_parameter = 0;
2962 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2963 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2964 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2965 #ifdef DEBUG_VERBOSE
2966 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2968 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2972 min_rice_parameter = max_rice_parameter = rice_parameter;
2974 best_partition_bits = 0xffffffff;
2975 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2977 #ifdef VARIABLE_RICE_BITS
2978 #ifdef FLAC__SYMMETRIC_RICE
2979 partition_bits = (2+rice_parameter) * partition_samples;
2981 const unsigned rice_parameter_estimate = rice_parameter-1;
2982 partition_bits = (1+rice_parameter) * partition_samples;
2987 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2988 save_residual_sample = residual_sample;
2989 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2990 #ifdef VARIABLE_RICE_BITS
2991 #ifdef FLAC__SYMMETRIC_RICE
2992 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2994 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2997 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
3000 #ifndef NO_RICE_SEARCH
3001 if(rice_parameter != max_rice_parameter)
3002 residual_sample = save_residual_sample;
3003 if(partition_bits < best_partition_bits) {
3004 best_rice_parameter = rice_parameter;
3005 best_partition_bits = partition_bits;
3009 if(search_for_escapes) {
3010 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;
3011 if(flat_bits <= best_partition_bits) {
3012 raw_bits[partition] = raw_bits_per_partition[partition];
3013 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3014 best_partition_bits = flat_bits;
3017 parameters[partition] = best_rice_parameter;
3018 bits_ += best_partition_bits;
3026 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
3031 for(i = 0; i < samples && !(x&1); i++)
3038 for(shift = 0; !(x&1); shift++)
3043 for(i = 0; i < samples; i++)
3044 signal[i] >>= shift;
3050 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3054 for(channel = 0; channel < channels; channel++)
3055 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3057 fifo->tail += wide_samples;
3059 FLAC__ASSERT(fifo->tail <= fifo->size);
3062 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3065 unsigned sample, wide_sample;
3066 unsigned tail = fifo->tail;
3068 sample = input_offset * channels;
3069 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3070 for(channel = 0; channel < channels; channel++)
3071 fifo->data[channel][tail] = input[sample++];
3076 FLAC__ASSERT(fifo->tail <= fifo->size);
3079 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3081 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3082 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3085 if(encoder->private_->verify.needs_magic_hack) {
3086 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3087 *bytes = FLAC__STREAM_SYNC_LENGTH;
3088 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3089 encoder->private_->verify.needs_magic_hack = false;
3092 if(encoded_bytes == 0) {
3094 * If we get here, a FIFO underflow has occurred,
3095 * which means there is a bug somewhere.
3098 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3100 else if(encoded_bytes < *bytes)
3101 *bytes = encoded_bytes;
3102 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3103 encoder->private_->verify.output.data += *bytes;
3104 encoder->private_->verify.output.bytes -= *bytes;
3107 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3110 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3112 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3114 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3115 const unsigned blocksize = frame->header.blocksize;
3116 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3118 for(channel = 0; channel < channels; channel++) {
3119 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3120 unsigned i, sample = 0;
3121 FLAC__int32 expect = 0, got = 0;
3123 for(i = 0; i < blocksize; i++) {
3124 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3126 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3127 got = (FLAC__int32)buffer[channel][i];
3131 FLAC__ASSERT(i < blocksize);
3132 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3133 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
3134 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
3135 encoder->private_->verify.error_stats.channel = channel;
3136 encoder->private_->verify.error_stats.sample = sample;
3137 encoder->private_->verify.error_stats.expected = expect;
3138 encoder->private_->verify.error_stats.got = got;
3139 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3140 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3143 /* dequeue the frame from the fifo */
3144 for(channel = 0; channel < channels; channel++) {
3145 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3147 encoder->private_->verify.input_fifo.tail -= blocksize;
3148 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3151 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3153 (void)decoder, (void)metadata, (void)client_data;
3156 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3158 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3159 (void)decoder, (void)status;
3160 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;