1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include <stdlib.h> /* for malloc() */
23 #include <string.h> /* for memcpy() */
24 #include "FLAC/assert.h"
25 #include "FLAC/stream_decoder.h"
26 #include "protected/stream_encoder.h"
27 #include "private/bitbuffer.h"
28 #include "private/bitmath.h"
29 #include "private/crc.h"
30 #include "private/cpu.h"
31 #include "private/fixed.h"
32 #include "private/format.h"
33 #include "private/lpc.h"
34 #include "private/md5.h"
35 #include "private/memory.h"
36 #include "private/stream_encoder_framing.h"
45 #define min(x,y) ((x)<(y)?(x):(y))
50 #define max(x,y) ((x)>(y)?(x):(y))
53 FLAC__int32 *data[FLAC__MAX_CHANNELS];
54 unsigned size; /* of each data[] in samples */
59 const FLAC__byte *data;
66 ENCODER_IN_METADATA = 1,
70 /***********************************************************************
72 * Private class method prototypes
74 ***********************************************************************/
76 static void set_defaults_(FLAC__StreamEncoder *encoder);
77 static void free_(FLAC__StreamEncoder *encoder);
78 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
79 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
80 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
81 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
83 static FLAC__bool process_subframe_(
84 FLAC__StreamEncoder *encoder,
85 unsigned min_partition_order,
86 unsigned max_partition_order,
87 FLAC__bool precompute_partition_sums,
88 const FLAC__FrameHeader *frame_header,
89 unsigned subframe_bps,
90 const FLAC__int32 integer_signal[],
91 const FLAC__real real_signal[],
92 FLAC__Subframe *subframe[2],
93 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
94 FLAC__int32 *residual[2],
95 unsigned *best_subframe,
99 static FLAC__bool add_subframe_(
100 FLAC__StreamEncoder *encoder,
101 const FLAC__FrameHeader *frame_header,
102 unsigned subframe_bps,
103 const FLAC__Subframe *subframe,
104 FLAC__BitBuffer *frame
107 static unsigned evaluate_constant_subframe_(
108 const FLAC__int32 signal,
109 unsigned subframe_bps,
110 FLAC__Subframe *subframe
113 static unsigned evaluate_fixed_subframe_(
114 FLAC__StreamEncoder *encoder,
115 const FLAC__int32 signal[],
116 FLAC__int32 residual[],
117 FLAC__uint32 abs_residual[],
118 FLAC__uint64 abs_residual_partition_sums[],
119 unsigned raw_bits_per_partition[],
121 unsigned subframe_bps,
123 unsigned rice_parameter,
124 unsigned min_partition_order,
125 unsigned max_partition_order,
126 FLAC__bool precompute_partition_sums,
127 FLAC__bool do_escape_coding,
128 unsigned rice_parameter_search_dist,
129 FLAC__Subframe *subframe,
130 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
133 static unsigned evaluate_lpc_subframe_(
134 FLAC__StreamEncoder *encoder,
135 const FLAC__int32 signal[],
136 FLAC__int32 residual[],
137 FLAC__uint32 abs_residual[],
138 FLAC__uint64 abs_residual_partition_sums[],
139 unsigned raw_bits_per_partition[],
140 const FLAC__real lp_coeff[],
142 unsigned subframe_bps,
144 unsigned qlp_coeff_precision,
145 unsigned rice_parameter,
146 unsigned min_partition_order,
147 unsigned max_partition_order,
148 FLAC__bool precompute_partition_sums,
149 FLAC__bool do_escape_coding,
150 unsigned rice_parameter_search_dist,
151 FLAC__Subframe *subframe,
152 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
155 static unsigned evaluate_verbatim_subframe_(
156 const FLAC__int32 signal[],
158 unsigned subframe_bps,
159 FLAC__Subframe *subframe
162 static unsigned find_best_partition_order_(
163 struct FLAC__StreamEncoderPrivate *private_,
164 const FLAC__int32 residual[],
165 FLAC__uint32 abs_residual[],
166 FLAC__uint64 abs_residual_partition_sums[],
167 unsigned raw_bits_per_partition[],
168 unsigned residual_samples,
169 unsigned predictor_order,
170 unsigned rice_parameter,
171 unsigned min_partition_order,
172 unsigned max_partition_order,
173 FLAC__bool precompute_partition_sums,
174 FLAC__bool do_escape_coding,
175 unsigned rice_parameter_search_dist,
176 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
179 static void precompute_partition_info_sums_(
180 const FLAC__uint32 abs_residual[],
181 FLAC__uint64 abs_residual_partition_sums[],
182 unsigned residual_samples,
183 unsigned predictor_order,
184 unsigned min_partition_order,
185 unsigned max_partition_order
188 static void precompute_partition_info_escapes_(
189 const FLAC__int32 residual[],
190 unsigned raw_bits_per_partition[],
191 unsigned residual_samples,
192 unsigned predictor_order,
193 unsigned min_partition_order,
194 unsigned max_partition_order
197 #ifdef DONT_ESTIMATE_RICE_BITS
198 static FLAC__bool set_partitioned_rice_(
199 const FLAC__uint32 abs_residual[],
200 const FLAC__int32 residual[],
201 const unsigned residual_samples,
202 const unsigned predictor_order,
203 const unsigned suggested_rice_parameter,
204 const unsigned rice_parameter_search_dist,
205 const unsigned partition_order,
206 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
210 static FLAC__bool set_partitioned_rice_with_precompute_(
211 const FLAC__int32 residual[],
212 const FLAC__uint64 abs_residual_partition_sums[],
213 const unsigned raw_bits_per_partition[],
214 const unsigned residual_samples,
215 const unsigned predictor_order,
216 const unsigned suggested_rice_parameter,
217 const unsigned rice_parameter_search_dist,
218 const unsigned partition_order,
219 const FLAC__bool search_for_escapes,
220 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
224 static FLAC__bool set_partitioned_rice_(
225 const FLAC__uint32 abs_residual[],
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 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
235 static FLAC__bool set_partitioned_rice_with_precompute_(
236 const FLAC__uint32 abs_residual[],
237 const FLAC__uint64 abs_residual_partition_sums[],
238 const unsigned raw_bits_per_partition[],
239 const unsigned residual_samples,
240 const unsigned predictor_order,
241 const unsigned suggested_rice_parameter,
242 const unsigned rice_parameter_search_dist,
243 const unsigned partition_order,
244 const FLAC__bool search_for_escapes,
245 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
250 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
252 /* verify-related routines: */
253 static void append_to_verify_fifo_(
254 verify_input_fifo *fifo,
255 const FLAC__int32 * const input[],
256 unsigned input_offset,
258 unsigned wide_samples
261 static void append_to_verify_fifo_interleaved_(
262 verify_input_fifo *fifo,
263 const FLAC__int32 input[],
264 unsigned input_offset,
266 unsigned wide_samples
269 static FLAC__StreamDecoderReadStatus verify_read_callback_(
270 const FLAC__StreamDecoder *decoder,
276 static FLAC__StreamDecoderWriteStatus verify_write_callback_(
277 const FLAC__StreamDecoder *decoder,
278 const FLAC__Frame *frame,
279 const FLAC__int32 * const buffer[],
283 static void verify_metadata_callback_(
284 const FLAC__StreamDecoder *decoder,
285 const FLAC__StreamMetadata *metadata,
289 static void verify_error_callback_(
290 const FLAC__StreamDecoder *decoder,
291 FLAC__StreamDecoderErrorStatus status,
296 /***********************************************************************
300 ***********************************************************************/
302 typedef struct FLAC__StreamEncoderPrivate {
303 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
304 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
305 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
306 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
307 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
308 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
309 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
310 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
311 FLAC__int32 *residual_workspace_mid_side[2][2];
312 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
313 FLAC__Subframe subframe_workspace_mid_side[2][2];
314 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
315 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
316 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
317 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
318 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
319 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
320 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
321 unsigned best_subframe_mid_side[2];
322 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
323 unsigned best_subframe_bits_mid_side[2];
324 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
325 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
326 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
327 FLAC__BitBuffer *frame; /* the current frame being worked on */
328 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
329 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
330 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
331 FLAC__ChannelAssignment last_channel_assignment;
332 FLAC__StreamMetadata metadata;
333 unsigned current_sample_number;
334 unsigned current_frame_number;
335 struct MD5Context md5context;
336 FLAC__CPUInfo cpuinfo;
337 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
338 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
339 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[]);
340 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[]);
341 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[]);
342 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
343 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
344 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
345 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
346 FLAC__bool disable_constant_subframes;
347 FLAC__bool disable_fixed_subframes;
348 FLAC__bool disable_verbatim_subframes;
349 FLAC__StreamEncoderWriteCallback write_callback;
350 FLAC__StreamEncoderMetadataCallback metadata_callback;
352 /* unaligned (original) pointers to allocated data */
353 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
354 FLAC__int32 *integer_signal_mid_side_unaligned[2];
355 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
356 FLAC__real *real_signal_mid_side_unaligned[2];
357 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
358 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
359 FLAC__uint32 *abs_residual_unaligned;
360 FLAC__uint64 *abs_residual_partition_sums_unaligned;
361 unsigned *raw_bits_per_partition_unaligned;
363 * These fields have been moved here from private function local
364 * declarations merely to save stack space during encoding.
366 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
367 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
369 * The data for the verify section
372 FLAC__StreamDecoder *decoder;
373 EncoderStateHint state_hint;
374 FLAC__bool needs_magic_hack;
375 verify_input_fifo input_fifo;
376 verify_output output;
378 FLAC__uint64 absolute_sample;
379 unsigned frame_number;
382 FLAC__int32 expected;
386 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
387 } FLAC__StreamEncoderPrivate;
389 /***********************************************************************
391 * Public static class data
393 ***********************************************************************/
395 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
396 "FLAC__STREAM_ENCODER_OK",
397 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
398 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
399 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
400 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
401 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
402 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
403 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
404 "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
405 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
406 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
407 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
408 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
409 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
410 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
411 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
412 "FLAC__STREAM_ENCODER_INVALID_METADATA",
413 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
414 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
415 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
416 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
417 "FLAC__STREAM_ENCODER_UNINITIALIZED"
420 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
421 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
422 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
425 /***********************************************************************
427 * Class constructor/destructor
430 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
432 FLAC__StreamEncoder *encoder;
435 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
437 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
442 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
443 if(encoder->protected_ == 0) {
448 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
449 if(encoder->private_ == 0) {
450 free(encoder->protected_);
455 encoder->private_->frame = FLAC__bitbuffer_new();
456 if(encoder->private_->frame == 0) {
457 free(encoder->private_);
458 free(encoder->protected_);
463 set_defaults_(encoder);
465 encoder->private_->is_being_deleted = false;
467 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
468 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
469 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
471 for(i = 0; i < 2; i++) {
472 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
473 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
475 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
476 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
477 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
479 for(i = 0; i < 2; i++) {
480 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
481 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
484 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
485 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
486 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
488 for(i = 0; i < 2; i++) {
489 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
490 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
492 for(i = 0; i < 2; i++)
493 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
495 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
500 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
504 FLAC__ASSERT(0 != encoder);
505 FLAC__ASSERT(0 != encoder->protected_);
506 FLAC__ASSERT(0 != encoder->private_);
507 FLAC__ASSERT(0 != encoder->private_->frame);
509 encoder->private_->is_being_deleted = true;
511 FLAC__stream_encoder_finish(encoder);
513 if(0 != encoder->private_->verify.decoder)
514 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
516 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
517 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
518 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
520 for(i = 0; i < 2; i++) {
521 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
522 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
524 for(i = 0; i < 2; i++)
525 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
527 FLAC__bitbuffer_delete(encoder->private_->frame);
528 free(encoder->private_);
529 free(encoder->protected_);
533 /***********************************************************************
535 * Public class methods
537 ***********************************************************************/
539 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
542 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
544 FLAC__ASSERT(0 != encoder);
546 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
547 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
549 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
551 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
552 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
554 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
555 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
557 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
558 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
560 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
561 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
563 if(encoder->protected_->bits_per_sample >= 32)
564 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
566 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
567 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
569 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
570 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
572 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
573 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
575 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
576 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
578 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
579 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
581 if(encoder->protected_->qlp_coeff_precision == 0) {
582 if(encoder->protected_->bits_per_sample < 16) {
583 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
584 /* @@@ until then we'll make a guess */
585 encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
587 else if(encoder->protected_->bits_per_sample == 16) {
588 if(encoder->protected_->blocksize <= 192)
589 encoder->protected_->qlp_coeff_precision = 7;
590 else if(encoder->protected_->blocksize <= 384)
591 encoder->protected_->qlp_coeff_precision = 8;
592 else if(encoder->protected_->blocksize <= 576)
593 encoder->protected_->qlp_coeff_precision = 9;
594 else if(encoder->protected_->blocksize <= 1152)
595 encoder->protected_->qlp_coeff_precision = 10;
596 else if(encoder->protected_->blocksize <= 2304)
597 encoder->protected_->qlp_coeff_precision = 11;
598 else if(encoder->protected_->blocksize <= 4608)
599 encoder->protected_->qlp_coeff_precision = 12;
601 encoder->protected_->qlp_coeff_precision = 13;
604 if(encoder->protected_->blocksize <= 384)
605 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
606 else if(encoder->protected_->blocksize <= 1152)
607 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
609 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
611 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
613 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
614 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
616 if(encoder->protected_->streamable_subset) {
618 encoder->protected_->blocksize != 192 &&
619 encoder->protected_->blocksize != 576 &&
620 encoder->protected_->blocksize != 1152 &&
621 encoder->protected_->blocksize != 2304 &&
622 encoder->protected_->blocksize != 4608 &&
623 encoder->protected_->blocksize != 256 &&
624 encoder->protected_->blocksize != 512 &&
625 encoder->protected_->blocksize != 1024 &&
626 encoder->protected_->blocksize != 2048 &&
627 encoder->protected_->blocksize != 4096 &&
628 encoder->protected_->blocksize != 8192 &&
629 encoder->protected_->blocksize != 16384
631 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
633 encoder->protected_->sample_rate != 8000 &&
634 encoder->protected_->sample_rate != 16000 &&
635 encoder->protected_->sample_rate != 22050 &&
636 encoder->protected_->sample_rate != 24000 &&
637 encoder->protected_->sample_rate != 32000 &&
638 encoder->protected_->sample_rate != 44100 &&
639 encoder->protected_->sample_rate != 48000 &&
640 encoder->protected_->sample_rate != 96000
642 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
644 encoder->protected_->bits_per_sample != 8 &&
645 encoder->protected_->bits_per_sample != 12 &&
646 encoder->protected_->bits_per_sample != 16 &&
647 encoder->protected_->bits_per_sample != 20 &&
648 encoder->protected_->bits_per_sample != 24
650 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
651 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
652 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
655 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
656 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
657 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
658 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
660 /* validate metadata */
661 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
662 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
663 metadata_has_seektable = false;
664 metadata_has_vorbis_comment = false;
665 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
666 if(encoder->protected_->metadata[i]->type >= FLAC__METADATA_TYPE_UNDEFINED)
667 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
668 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
669 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
670 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
671 if(metadata_has_seektable) /* only one is allowed */
672 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
673 metadata_has_seektable = true;
674 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
675 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
677 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
678 if(metadata_has_vorbis_comment) /* only one is allowed */
679 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
680 metadata_has_vorbis_comment = true;
682 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
683 if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
684 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
688 encoder->private_->input_capacity = 0;
689 for(i = 0; i < encoder->protected_->channels; i++) {
690 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
691 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
693 for(i = 0; i < 2; i++) {
694 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
695 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
697 for(i = 0; i < encoder->protected_->channels; i++) {
698 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
699 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
700 encoder->private_->best_subframe[i] = 0;
702 for(i = 0; i < 2; i++) {
703 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
704 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
705 encoder->private_->best_subframe_mid_side[i] = 0;
707 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
708 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
709 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
710 encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
711 encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
712 if(encoder->private_->loose_mid_side_stereo_frames == 0)
713 encoder->private_->loose_mid_side_stereo_frames = 1;
714 encoder->private_->loose_mid_side_stereo_frame_count = 0;
715 encoder->private_->current_sample_number = 0;
716 encoder->private_->current_frame_number = 0;
718 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
719 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? */
720 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
723 * get the CPU info and set the function pointers
725 FLAC__cpu_info(&encoder->private_->cpuinfo);
726 /* first default to the non-asm routines */
727 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
728 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
729 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
730 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
731 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
732 /* now override with asm where appropriate */
734 if(encoder->private_->cpuinfo.use_asm) {
735 #ifdef FLAC__CPU_IA32
736 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
737 #ifdef FLAC__HAS_NASM
739 if(encoder->private_->cpuinfo.data.ia32.sse) {
740 if(encoder->protected_->max_lpc_order < 4)
741 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
742 else if(encoder->protected_->max_lpc_order < 8)
743 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
744 else if(encoder->protected_->max_lpc_order < 12)
745 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
747 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
751 if(encoder->private_->cpuinfo.data.ia32._3dnow)
752 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
754 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
755 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
756 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
757 if(encoder->private_->cpuinfo.data.ia32.mmx) {
758 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
759 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
762 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
763 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
769 /* finally override based on wide-ness if necessary */
770 if(encoder->private_->use_wide_by_block) {
771 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
774 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
775 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
777 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
778 /* the above function sets the state for us in case of an error */
779 return encoder->protected_->state;
782 if(!FLAC__bitbuffer_init(encoder->private_->frame))
783 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
786 * Set up the verify stuff if necessary
788 if(encoder->protected_->verify) {
790 * First, set up the fifo which will hold the
791 * original signal to compare against
793 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
794 for(i = 0; i < encoder->protected_->channels; i++) {
795 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
796 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
798 encoder->private_->verify.input_fifo.tail = 0;
801 * Now set up a stream decoder for verification
803 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
804 if(0 == encoder->private_->verify.decoder)
805 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
807 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
808 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
809 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
810 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
811 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
812 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
813 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
815 encoder->private_->verify.error_stats.absolute_sample = 0;
816 encoder->private_->verify.error_stats.frame_number = 0;
817 encoder->private_->verify.error_stats.channel = 0;
818 encoder->private_->verify.error_stats.sample = 0;
819 encoder->private_->verify.error_stats.expected = 0;
820 encoder->private_->verify.error_stats.got = 0;
823 * write the stream header
825 if(encoder->protected_->verify)
826 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
827 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
828 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
829 if(!write_bitbuffer_(encoder, 0)) {
830 /* the above function sets the state for us in case of an error */
831 return encoder->protected_->state;
835 * write the STREAMINFO metadata block
837 if(encoder->protected_->verify)
838 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
839 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
840 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
841 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
842 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
843 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
844 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
845 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
846 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
847 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
848 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
849 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
850 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
851 MD5Init(&encoder->private_->md5context);
852 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
853 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
854 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
855 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
856 if(!write_bitbuffer_(encoder, 0)) {
857 /* the above function sets the state for us in case of an error */
858 return encoder->protected_->state;
862 * Now that the STREAMINFO block is written, we can init this to an
863 * absurdly-high value...
865 encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
866 /* ... and clear this to 0 */
867 encoder->private_->metadata.data.stream_info.total_samples = 0;
870 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
871 * if not, we will write an empty one (FLAC__add_metadata_block()
872 * automatically supplies the vendor string).
874 if(!metadata_has_vorbis_comment) {
875 FLAC__StreamMetadata vorbis_comment;
876 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
877 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
878 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
879 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
880 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
881 vorbis_comment.data.vorbis_comment.num_comments = 0;
882 vorbis_comment.data.vorbis_comment.comments = 0;
883 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
884 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
885 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
886 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
887 if(!write_bitbuffer_(encoder, 0)) {
888 /* the above function sets the state for us in case of an error */
889 return encoder->protected_->state;
894 * write the user's metadata blocks
896 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
897 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
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(encoder->protected_->metadata[i], 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;
908 if(encoder->protected_->verify)
909 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
911 return encoder->protected_->state;
914 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
916 FLAC__ASSERT(0 != encoder);
918 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
921 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
922 if(encoder->private_->current_sample_number != 0) {
923 encoder->protected_->blocksize = encoder->private_->current_sample_number;
924 process_frame_(encoder, true); /* true => is last frame */
928 MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
930 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
931 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
934 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
935 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
938 set_defaults_(encoder);
940 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
943 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
945 FLAC__ASSERT(0 != encoder);
946 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
948 encoder->protected_->verify = value;
952 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
954 FLAC__ASSERT(0 != encoder);
955 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
957 encoder->protected_->streamable_subset = value;
961 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
963 FLAC__ASSERT(0 != encoder);
964 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
966 encoder->protected_->do_mid_side_stereo = value;
970 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
972 FLAC__ASSERT(0 != encoder);
973 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
975 encoder->protected_->loose_mid_side_stereo = value;
979 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
981 FLAC__ASSERT(0 != encoder);
982 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
984 encoder->protected_->channels = value;
988 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
990 FLAC__ASSERT(0 != encoder);
991 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
993 encoder->protected_->bits_per_sample = value;
997 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
999 FLAC__ASSERT(0 != encoder);
1000 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1002 encoder->protected_->sample_rate = value;
1006 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1008 FLAC__ASSERT(0 != encoder);
1009 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1011 encoder->protected_->blocksize = value;
1015 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1017 FLAC__ASSERT(0 != encoder);
1018 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1020 encoder->protected_->max_lpc_order = value;
1024 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1026 FLAC__ASSERT(0 != encoder);
1027 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1029 encoder->protected_->qlp_coeff_precision = value;
1033 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1035 FLAC__ASSERT(0 != encoder);
1036 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1038 encoder->protected_->do_qlp_coeff_prec_search = value;
1042 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1044 FLAC__ASSERT(0 != encoder);
1045 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1048 /*@@@ deprecated: */
1049 encoder->protected_->do_escape_coding = value;
1056 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1058 FLAC__ASSERT(0 != encoder);
1059 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1061 encoder->protected_->do_exhaustive_model_search = value;
1065 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1067 FLAC__ASSERT(0 != encoder);
1068 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1070 encoder->protected_->min_residual_partition_order = value;
1074 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1076 FLAC__ASSERT(0 != encoder);
1077 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1079 encoder->protected_->max_residual_partition_order = value;
1083 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1085 FLAC__ASSERT(0 != encoder);
1086 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1089 /*@@@ deprecated: */
1090 encoder->protected_->rice_parameter_search_dist = value;
1097 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1099 FLAC__ASSERT(0 != encoder);
1100 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1102 encoder->protected_->total_samples_estimate = value;
1106 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1108 FLAC__ASSERT(0 != encoder);
1109 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1111 encoder->protected_->metadata = metadata;
1112 encoder->protected_->num_metadata_blocks = num_blocks;
1116 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
1118 FLAC__ASSERT(0 != encoder);
1119 FLAC__ASSERT(0 != value);
1120 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1122 encoder->private_->write_callback = value;
1126 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
1128 FLAC__ASSERT(0 != encoder);
1129 FLAC__ASSERT(0 != value);
1130 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1132 encoder->private_->metadata_callback = value;
1136 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
1138 FLAC__ASSERT(0 != encoder);
1139 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1141 encoder->private_->client_data = value;
1146 * These three functions are not static, but not publically exposed in
1147 * include/FLAC/ either. They are used by the test suite.
1149 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1151 FLAC__ASSERT(0 != encoder);
1152 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1154 encoder->private_->disable_constant_subframes = value;
1158 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1160 FLAC__ASSERT(0 != encoder);
1161 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1163 encoder->private_->disable_fixed_subframes = value;
1167 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1169 FLAC__ASSERT(0 != encoder);
1170 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1172 encoder->private_->disable_verbatim_subframes = value;
1176 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1178 FLAC__ASSERT(0 != encoder);
1179 return encoder->protected_->state;
1182 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1184 FLAC__ASSERT(0 != encoder);
1185 if(encoder->protected_->verify)
1186 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1188 return FLAC__STREAM_DECODER_UNINITIALIZED;
1191 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1193 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1194 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1196 return FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(encoder->private_->verify.decoder)];
1199 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)
1201 FLAC__ASSERT(0 != encoder);
1202 if(0 != absolute_sample)
1203 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1204 if(0 != frame_number)
1205 *frame_number = encoder->private_->verify.error_stats.frame_number;
1207 *channel = encoder->private_->verify.error_stats.channel;
1209 *sample = encoder->private_->verify.error_stats.sample;
1211 *expected = encoder->private_->verify.error_stats.expected;
1213 *got = encoder->private_->verify.error_stats.got;
1216 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1218 FLAC__ASSERT(0 != encoder);
1219 return encoder->protected_->verify;
1222 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1224 FLAC__ASSERT(0 != encoder);
1225 return encoder->protected_->streamable_subset;
1228 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1230 FLAC__ASSERT(0 != encoder);
1231 return encoder->protected_->do_mid_side_stereo;
1234 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1236 FLAC__ASSERT(0 != encoder);
1237 return encoder->protected_->loose_mid_side_stereo;
1240 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1242 FLAC__ASSERT(0 != encoder);
1243 return encoder->protected_->channels;
1246 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1248 FLAC__ASSERT(0 != encoder);
1249 return encoder->protected_->bits_per_sample;
1252 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1254 FLAC__ASSERT(0 != encoder);
1255 return encoder->protected_->sample_rate;
1258 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1260 FLAC__ASSERT(0 != encoder);
1261 return encoder->protected_->blocksize;
1264 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1266 FLAC__ASSERT(0 != encoder);
1267 return encoder->protected_->max_lpc_order;
1270 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1272 FLAC__ASSERT(0 != encoder);
1273 return encoder->protected_->qlp_coeff_precision;
1276 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1278 FLAC__ASSERT(0 != encoder);
1279 return encoder->protected_->do_qlp_coeff_prec_search;
1282 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1284 FLAC__ASSERT(0 != encoder);
1285 return encoder->protected_->do_escape_coding;
1288 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1290 FLAC__ASSERT(0 != encoder);
1291 return encoder->protected_->do_exhaustive_model_search;
1294 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1296 FLAC__ASSERT(0 != encoder);
1297 return encoder->protected_->min_residual_partition_order;
1300 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1302 FLAC__ASSERT(0 != encoder);
1303 return encoder->protected_->max_residual_partition_order;
1306 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1308 FLAC__ASSERT(0 != encoder);
1309 return encoder->protected_->rice_parameter_search_dist;
1312 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1314 FLAC__ASSERT(0 != encoder);
1315 return encoder->protected_->total_samples_estimate;
1318 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1320 unsigned i, j, channel;
1321 FLAC__int32 x, mid, side;
1322 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1324 FLAC__ASSERT(0 != encoder);
1325 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1328 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1330 if(encoder->protected_->verify)
1331 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1333 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1334 x = mid = side = buffer[0][j];
1335 encoder->private_->integer_signal[0][i] = x;
1336 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1338 encoder->private_->integer_signal[1][i] = x;
1339 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1342 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1343 encoder->private_->integer_signal_mid_side[1][i] = side;
1344 encoder->private_->integer_signal_mid_side[0][i] = mid;
1345 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1346 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1347 encoder->private_->current_sample_number++;
1349 if(i == blocksize) {
1350 if(!process_frame_(encoder, false)) /* false => not last frame */
1353 } while(j < samples);
1357 if(encoder->protected_->verify)
1358 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1360 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1361 for(channel = 0; channel < channels; channel++) {
1362 x = buffer[channel][j];
1363 encoder->private_->integer_signal[channel][i] = x;
1364 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1366 encoder->private_->current_sample_number++;
1368 if(i == blocksize) {
1369 if(!process_frame_(encoder, false)) /* false => not last frame */
1372 } while(j < samples);
1378 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1380 unsigned i, j, k, channel;
1381 FLAC__int32 x, mid, side;
1382 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1384 FLAC__ASSERT(0 != encoder);
1385 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1388 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1390 if(encoder->protected_->verify)
1391 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1393 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1394 x = mid = side = buffer[k++];
1395 encoder->private_->integer_signal[0][i] = x;
1396 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1398 encoder->private_->integer_signal[1][i] = x;
1399 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1402 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1403 encoder->private_->integer_signal_mid_side[1][i] = side;
1404 encoder->private_->integer_signal_mid_side[0][i] = mid;
1405 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1406 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1407 encoder->private_->current_sample_number++;
1409 if(i == blocksize) {
1410 if(!process_frame_(encoder, false)) /* false => not last frame */
1413 } while(j < samples);
1417 if(encoder->protected_->verify)
1418 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1420 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1421 for(channel = 0; channel < channels; channel++) {
1423 encoder->private_->integer_signal[channel][i] = x;
1424 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1426 encoder->private_->current_sample_number++;
1428 if(i == blocksize) {
1429 if(!process_frame_(encoder, false)) /* false => not last frame */
1432 } while(j < samples);
1438 /***********************************************************************
1440 * Private class methods
1442 ***********************************************************************/
1444 void set_defaults_(FLAC__StreamEncoder *encoder)
1446 FLAC__ASSERT(0 != encoder);
1448 encoder->protected_->verify = false;
1449 encoder->protected_->streamable_subset = true;
1450 encoder->protected_->do_mid_side_stereo = false;
1451 encoder->protected_->loose_mid_side_stereo = false;
1452 encoder->protected_->channels = 2;
1453 encoder->protected_->bits_per_sample = 16;
1454 encoder->protected_->sample_rate = 44100;
1455 encoder->protected_->blocksize = 1152;
1456 encoder->protected_->max_lpc_order = 0;
1457 encoder->protected_->qlp_coeff_precision = 0;
1458 encoder->protected_->do_qlp_coeff_prec_search = false;
1459 encoder->protected_->do_exhaustive_model_search = false;
1460 encoder->protected_->do_escape_coding = false;
1461 encoder->protected_->min_residual_partition_order = 0;
1462 encoder->protected_->max_residual_partition_order = 0;
1463 encoder->protected_->rice_parameter_search_dist = 0;
1464 encoder->protected_->total_samples_estimate = 0;
1465 encoder->protected_->metadata = 0;
1466 encoder->protected_->num_metadata_blocks = 0;
1468 encoder->private_->disable_constant_subframes = false;
1469 encoder->private_->disable_fixed_subframes = false;
1470 encoder->private_->disable_verbatim_subframes = false;
1471 encoder->private_->write_callback = 0;
1472 encoder->private_->metadata_callback = 0;
1473 encoder->private_->client_data = 0;
1476 void free_(FLAC__StreamEncoder *encoder)
1478 unsigned i, channel;
1480 FLAC__ASSERT(0 != encoder);
1481 for(i = 0; i < encoder->protected_->channels; i++) {
1482 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1483 free(encoder->private_->integer_signal_unaligned[i]);
1484 encoder->private_->integer_signal_unaligned[i] = 0;
1486 if(0 != encoder->private_->real_signal_unaligned[i]) {
1487 free(encoder->private_->real_signal_unaligned[i]);
1488 encoder->private_->real_signal_unaligned[i] = 0;
1491 for(i = 0; i < 2; i++) {
1492 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1493 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1494 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1496 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1497 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1498 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1501 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1502 for(i = 0; i < 2; i++) {
1503 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1504 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1505 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1509 for(channel = 0; channel < 2; channel++) {
1510 for(i = 0; i < 2; i++) {
1511 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1512 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1513 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1517 if(0 != encoder->private_->abs_residual_unaligned) {
1518 free(encoder->private_->abs_residual_unaligned);
1519 encoder->private_->abs_residual_unaligned = 0;
1521 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1522 free(encoder->private_->abs_residual_partition_sums_unaligned);
1523 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1525 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1526 free(encoder->private_->raw_bits_per_partition_unaligned);
1527 encoder->private_->raw_bits_per_partition_unaligned = 0;
1529 if(encoder->protected_->verify) {
1530 for(i = 0; i < encoder->protected_->channels; i++) {
1531 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1532 free(encoder->private_->verify.input_fifo.data[i]);
1533 encoder->private_->verify.input_fifo.data[i] = 0;
1537 FLAC__bitbuffer_free(encoder->private_->frame);
1540 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
1543 unsigned i, channel;
1545 FLAC__ASSERT(new_size > 0);
1546 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1547 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1549 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1550 if(new_size <= encoder->private_->input_capacity)
1555 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1556 * requires that the input arrays (in our case the integer signals)
1557 * have a buffer of up to 3 zeroes in front (at negative indices) for
1558 * alignment purposes; we use 4 to keep the data well-aligned.
1561 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1562 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1563 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1564 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1565 encoder->private_->integer_signal[i] += 4;
1567 for(i = 0; ok && i < 2; i++) {
1568 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]);
1569 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]);
1570 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1571 encoder->private_->integer_signal_mid_side[i] += 4;
1573 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1574 for(i = 0; ok && i < 2; i++) {
1575 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1578 for(channel = 0; ok && channel < 2; channel++) {
1579 for(i = 0; ok && i < 2; i++) {
1580 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]);
1583 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1584 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 */
1585 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1586 if(encoder->protected_->do_escape_coding)
1587 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1590 encoder->private_->input_capacity = new_size;
1592 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1597 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
1599 const FLAC__byte *buffer;
1602 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1604 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1606 if(encoder->protected_->verify) {
1607 encoder->private_->verify.output.data = buffer;
1608 encoder->private_->verify.output.bytes = bytes;
1609 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1610 encoder->private_->verify.needs_magic_hack = true;
1613 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1614 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1615 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1616 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1622 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1623 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1624 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1628 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1631 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1632 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1638 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1640 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1643 * Accumulate raw signal to the MD5 signature
1645 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)) {
1646 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1651 * Process the frame header and subframes into the frame bitbuffer
1653 if(!process_subframes_(encoder, is_last_frame)) {
1654 /* the above function sets the state for us in case of an error */
1659 * Zero-pad the frame to a byte_boundary
1661 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1662 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1667 * CRC-16 the whole thing
1669 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1670 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1675 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1676 /* the above function sets the state for us in case of an error */
1681 * Get ready for the next frame
1683 encoder->private_->current_sample_number = 0;
1684 encoder->private_->current_frame_number++;
1685 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1690 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1692 FLAC__FrameHeader frame_header;
1693 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1694 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1697 * Calculate the min,max Rice partition orders
1700 max_partition_order = 0;
1703 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1704 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
1706 min_partition_order = min(min_partition_order, max_partition_order);
1708 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1713 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1714 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1717 frame_header.blocksize = encoder->protected_->blocksize;
1718 frame_header.sample_rate = encoder->protected_->sample_rate;
1719 frame_header.channels = encoder->protected_->channels;
1720 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1721 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1722 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1723 frame_header.number.frame_number = encoder->private_->current_frame_number;
1726 * Figure out what channel assignments to try
1728 if(encoder->protected_->do_mid_side_stereo) {
1729 if(encoder->protected_->loose_mid_side_stereo) {
1730 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1731 do_independent = true;
1735 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1736 do_mid_side = !do_independent;
1740 do_independent = true;
1745 do_independent = true;
1746 do_mid_side = false;
1749 FLAC__ASSERT(do_independent || do_mid_side);
1752 * Check for wasted bits; set effective bps for each subframe
1754 if(do_independent) {
1755 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1756 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1757 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1758 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1762 FLAC__ASSERT(encoder->protected_->channels == 2);
1763 for(channel = 0; channel < 2; channel++) {
1764 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1765 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1766 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1771 * First do a normal encoding pass of each independent channel
1773 if(do_independent) {
1774 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1778 min_partition_order,
1779 max_partition_order,
1780 precompute_partition_sums,
1782 encoder->private_->subframe_bps[channel],
1783 encoder->private_->integer_signal[channel],
1784 encoder->private_->real_signal[channel],
1785 encoder->private_->subframe_workspace_ptr[channel],
1786 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1787 encoder->private_->residual_workspace[channel],
1788 encoder->private_->best_subframe+channel,
1789 encoder->private_->best_subframe_bits+channel
1797 * Now do mid and side channels if requested
1800 FLAC__ASSERT(encoder->protected_->channels == 2);
1802 for(channel = 0; channel < 2; channel++) {
1806 min_partition_order,
1807 max_partition_order,
1808 precompute_partition_sums,
1810 encoder->private_->subframe_bps_mid_side[channel],
1811 encoder->private_->integer_signal_mid_side[channel],
1812 encoder->private_->real_signal_mid_side[channel],
1813 encoder->private_->subframe_workspace_ptr_mid_side[channel],
1814 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
1815 encoder->private_->residual_workspace_mid_side[channel],
1816 encoder->private_->best_subframe_mid_side+channel,
1817 encoder->private_->best_subframe_bits_mid_side+channel
1825 * Compose the frame bitbuffer
1828 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1829 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1830 FLAC__ChannelAssignment channel_assignment;
1832 FLAC__ASSERT(encoder->protected_->channels == 2);
1834 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1835 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1838 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1840 FLAC__ChannelAssignment ca;
1842 FLAC__ASSERT(do_independent && do_mid_side);
1844 /* We have to figure out which channel assignent results in the smallest frame */
1845 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
1846 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
1847 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
1848 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
1850 for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
1851 if(bits[ca] < min_bits) {
1852 min_bits = bits[ca];
1853 channel_assignment = ca;
1858 frame_header.channel_assignment = channel_assignment;
1860 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1861 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1865 switch(channel_assignment) {
1866 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1867 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1868 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1870 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1871 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1872 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1874 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1875 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1876 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1878 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1879 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1880 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1886 switch(channel_assignment) {
1887 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1888 left_bps = encoder->private_->subframe_bps [0];
1889 right_bps = encoder->private_->subframe_bps [1];
1891 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1892 left_bps = encoder->private_->subframe_bps [0];
1893 right_bps = encoder->private_->subframe_bps_mid_side[1];
1895 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1896 left_bps = encoder->private_->subframe_bps_mid_side[1];
1897 right_bps = encoder->private_->subframe_bps [1];
1899 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1900 left_bps = encoder->private_->subframe_bps_mid_side[0];
1901 right_bps = encoder->private_->subframe_bps_mid_side[1];
1907 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1908 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
1910 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1914 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1915 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1919 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1920 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
1921 /* the above function sets the state for us in case of an error */
1927 if(encoder->protected_->loose_mid_side_stereo) {
1928 encoder->private_->loose_mid_side_stereo_frame_count++;
1929 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1930 encoder->private_->loose_mid_side_stereo_frame_count = 0;
1933 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1938 FLAC__bool process_subframe_(
1939 FLAC__StreamEncoder *encoder,
1940 unsigned min_partition_order,
1941 unsigned max_partition_order,
1942 FLAC__bool precompute_partition_sums,
1943 const FLAC__FrameHeader *frame_header,
1944 unsigned subframe_bps,
1945 const FLAC__int32 integer_signal[],
1946 const FLAC__real real_signal[],
1947 FLAC__Subframe *subframe[2],
1948 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
1949 FLAC__int32 *residual[2],
1950 unsigned *best_subframe,
1954 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1955 FLAC__real lpc_residual_bits_per_sample;
1956 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 */
1957 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1958 unsigned min_lpc_order, max_lpc_order, lpc_order;
1959 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1960 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1961 unsigned rice_parameter;
1962 unsigned _candidate_bits, _best_bits;
1963 unsigned _best_subframe;
1965 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1967 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
1968 _best_bits = UINT_MAX;
1970 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1972 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1973 unsigned signal_is_constant = false;
1974 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);
1975 /* check for constant subframe */
1976 if(!encoder->private_->disable_constant_subframes && fixed_residual_bits_per_sample[1] == 0.0) {
1977 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1979 signal_is_constant = true;
1980 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1981 if(integer_signal[0] != integer_signal[i]) {
1982 signal_is_constant = false;
1987 if(signal_is_constant) {
1988 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1989 if(_candidate_bits < _best_bits) {
1990 _best_subframe = !_best_subframe;
1991 _best_bits = _candidate_bits;
1995 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
1997 if(encoder->protected_->do_exhaustive_model_search) {
1998 min_fixed_order = 0;
1999 max_fixed_order = FLAC__MAX_FIXED_ORDER;
2002 min_fixed_order = max_fixed_order = guess_fixed_order;
2004 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
2005 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
2006 continue; /* don't even try */
2007 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 */
2008 #ifndef FLAC__SYMMETRIC_RICE
2009 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2011 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2012 #ifdef DEBUG_VERBOSE
2013 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2015 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2018 evaluate_fixed_subframe_(
2021 residual[!_best_subframe],
2022 encoder->private_->abs_residual,
2023 encoder->private_->abs_residual_partition_sums,
2024 encoder->private_->raw_bits_per_partition,
2025 frame_header->blocksize,
2029 min_partition_order,
2030 max_partition_order,
2031 precompute_partition_sums,
2032 encoder->protected_->do_escape_coding,
2033 encoder->protected_->rice_parameter_search_dist,
2034 subframe[!_best_subframe],
2035 partitioned_rice_contents[!_best_subframe]
2037 if(_candidate_bits < _best_bits) {
2038 _best_subframe = !_best_subframe;
2039 _best_bits = _candidate_bits;
2045 if(encoder->protected_->max_lpc_order > 0) {
2046 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
2047 max_lpc_order = frame_header->blocksize-1;
2049 max_lpc_order = encoder->protected_->max_lpc_order;
2050 if(max_lpc_order > 0) {
2051 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2052 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2053 if(autoc[0] != 0.0) {
2054 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2055 if(encoder->protected_->do_exhaustive_model_search) {
2059 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
2060 min_lpc_order = max_lpc_order = guess_lpc_order;
2062 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2063 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2064 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
2065 continue; /* don't even try */
2066 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2067 #ifndef FLAC__SYMMETRIC_RICE
2068 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2070 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2071 #ifdef DEBUG_VERBOSE
2072 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2074 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2076 if(encoder->protected_->do_qlp_coeff_prec_search) {
2077 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2078 /* ensure a 32-bit datapath throughout for 16bps or less */
2079 if(subframe_bps <= 16)
2080 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2082 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2085 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2087 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2089 evaluate_lpc_subframe_(
2092 residual[!_best_subframe],
2093 encoder->private_->abs_residual,
2094 encoder->private_->abs_residual_partition_sums,
2095 encoder->private_->raw_bits_per_partition,
2096 encoder->private_->lp_coeff[lpc_order-1],
2097 frame_header->blocksize,
2100 qlp_coeff_precision,
2102 min_partition_order,
2103 max_partition_order,
2104 precompute_partition_sums,
2105 encoder->protected_->do_escape_coding,
2106 encoder->protected_->rice_parameter_search_dist,
2107 subframe[!_best_subframe],
2108 partitioned_rice_contents[!_best_subframe]
2110 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2111 if(_candidate_bits < _best_bits) {
2112 _best_subframe = !_best_subframe;
2113 _best_bits = _candidate_bits;
2124 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2125 if(_best_bits == UINT_MAX) {
2126 FLAC__ASSERT(_best_subframe == 0);
2127 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2130 *best_subframe = _best_subframe;
2131 *best_bits = _best_bits;
2136 FLAC__bool add_subframe_(
2137 FLAC__StreamEncoder *encoder,
2138 const FLAC__FrameHeader *frame_header,
2139 unsigned subframe_bps,
2140 const FLAC__Subframe *subframe,
2141 FLAC__BitBuffer *frame
2144 switch(subframe->type) {
2145 case FLAC__SUBFRAME_TYPE_CONSTANT:
2146 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
2147 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2151 case FLAC__SUBFRAME_TYPE_FIXED:
2152 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2153 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2157 case FLAC__SUBFRAME_TYPE_LPC:
2158 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2159 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2163 case FLAC__SUBFRAME_TYPE_VERBATIM:
2164 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2165 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2176 unsigned evaluate_constant_subframe_(
2177 const FLAC__int32 signal,
2178 unsigned subframe_bps,
2179 FLAC__Subframe *subframe
2182 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2183 subframe->data.constant.value = signal;
2185 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
2188 unsigned evaluate_fixed_subframe_(
2189 FLAC__StreamEncoder *encoder,
2190 const FLAC__int32 signal[],
2191 FLAC__int32 residual[],
2192 FLAC__uint32 abs_residual[],
2193 FLAC__uint64 abs_residual_partition_sums[],
2194 unsigned raw_bits_per_partition[],
2196 unsigned subframe_bps,
2198 unsigned rice_parameter,
2199 unsigned min_partition_order,
2200 unsigned max_partition_order,
2201 FLAC__bool precompute_partition_sums,
2202 FLAC__bool do_escape_coding,
2203 unsigned rice_parameter_search_dist,
2204 FLAC__Subframe *subframe,
2205 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2208 unsigned i, residual_bits;
2209 const unsigned residual_samples = blocksize - order;
2211 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2213 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2215 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2216 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2217 subframe->data.fixed.residual = residual;
2220 find_best_partition_order_(
2224 abs_residual_partition_sums,
2225 raw_bits_per_partition,
2229 min_partition_order,
2230 max_partition_order,
2231 precompute_partition_sums,
2233 rice_parameter_search_dist,
2234 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2237 subframe->data.fixed.order = order;
2238 for(i = 0; i < order; i++)
2239 subframe->data.fixed.warmup[i] = signal[i];
2241 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
2244 unsigned evaluate_lpc_subframe_(
2245 FLAC__StreamEncoder *encoder,
2246 const FLAC__int32 signal[],
2247 FLAC__int32 residual[],
2248 FLAC__uint32 abs_residual[],
2249 FLAC__uint64 abs_residual_partition_sums[],
2250 unsigned raw_bits_per_partition[],
2251 const FLAC__real lp_coeff[],
2253 unsigned subframe_bps,
2255 unsigned qlp_coeff_precision,
2256 unsigned rice_parameter,
2257 unsigned min_partition_order,
2258 unsigned max_partition_order,
2259 FLAC__bool precompute_partition_sums,
2260 FLAC__bool do_escape_coding,
2261 unsigned rice_parameter_search_dist,
2262 FLAC__Subframe *subframe,
2263 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2266 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
2267 unsigned i, residual_bits;
2268 int quantization, ret;
2269 const unsigned residual_samples = blocksize - order;
2271 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2272 if(subframe_bps <= 16) {
2273 FLAC__ASSERT(order > 0);
2274 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2275 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2278 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
2280 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2282 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2283 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2284 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2286 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2288 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2290 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2292 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2293 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2294 subframe->data.lpc.residual = residual;
2297 find_best_partition_order_(
2301 abs_residual_partition_sums,
2302 raw_bits_per_partition,
2306 min_partition_order,
2307 max_partition_order,
2308 precompute_partition_sums,
2310 rice_parameter_search_dist,
2311 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2314 subframe->data.lpc.order = order;
2315 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2316 subframe->data.lpc.quantization_level = quantization;
2317 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
2318 for(i = 0; i < order; i++)
2319 subframe->data.lpc.warmup[i] = signal[i];
2321 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;
2324 unsigned evaluate_verbatim_subframe_(
2325 const FLAC__int32 signal[],
2327 unsigned subframe_bps,
2328 FLAC__Subframe *subframe
2331 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2333 subframe->data.verbatim.data = signal;
2335 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
2338 unsigned find_best_partition_order_(
2339 FLAC__StreamEncoderPrivate *private_,
2340 const FLAC__int32 residual[],
2341 FLAC__uint32 abs_residual[],
2342 FLAC__uint64 abs_residual_partition_sums[],
2343 unsigned raw_bits_per_partition[],
2344 unsigned residual_samples,
2345 unsigned predictor_order,
2346 unsigned rice_parameter,
2347 unsigned min_partition_order,
2348 unsigned max_partition_order,
2349 FLAC__bool precompute_partition_sums,
2350 FLAC__bool do_escape_coding,
2351 unsigned rice_parameter_search_dist,
2352 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2356 unsigned residual_bits, best_residual_bits = 0;
2357 unsigned residual_sample;
2358 unsigned best_parameters_index = 0;
2359 const unsigned blocksize = residual_samples + predictor_order;
2361 /* compute abs(residual) for use later */
2362 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2363 r = residual[residual_sample];
2364 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2367 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2368 min_partition_order = min(min_partition_order, max_partition_order);
2370 if(precompute_partition_sums) {
2371 int partition_order;
2374 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2376 if(do_escape_coding)
2377 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2379 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2380 #ifdef DONT_ESTIMATE_RICE_BITS
2382 set_partitioned_rice_with_precompute_(
2384 abs_residual_partition_sums+sum,
2385 raw_bits_per_partition+sum,
2389 rice_parameter_search_dist,
2390 (unsigned)partition_order,
2392 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2398 set_partitioned_rice_with_precompute_(
2400 abs_residual_partition_sums+sum,
2401 raw_bits_per_partition+sum,
2405 rice_parameter_search_dist,
2406 (unsigned)partition_order,
2408 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2414 FLAC__ASSERT(best_residual_bits != 0);
2417 sum += 1u << partition_order;
2418 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2419 best_residual_bits = residual_bits;
2420 best_parameters_index = !best_parameters_index;
2421 best_partitioned_rice->order = partition_order;
2426 unsigned partition_order;
2427 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2428 #ifdef DONT_ESTIMATE_RICE_BITS
2430 set_partitioned_rice_(
2436 rice_parameter_search_dist,
2438 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2444 set_partitioned_rice_(
2449 rice_parameter_search_dist,
2451 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2457 FLAC__ASSERT(best_residual_bits != 0);
2460 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2461 best_residual_bits = residual_bits;
2462 best_parameters_index = !best_parameters_index;
2463 best_partitioned_rice->order = partition_order;
2469 * We are allowed to de-const the pointer based on our special knowledge;
2470 * it is const to the outside world.
2473 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2474 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2475 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2476 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2479 return best_residual_bits;
2482 void precompute_partition_info_sums_(
2483 const FLAC__uint32 abs_residual[],
2484 FLAC__uint64 abs_residual_partition_sums[],
2485 unsigned residual_samples,
2486 unsigned predictor_order,
2487 unsigned min_partition_order,
2488 unsigned max_partition_order
2491 int partition_order;
2492 unsigned from_partition, to_partition = 0;
2493 const unsigned blocksize = residual_samples + predictor_order;
2495 /* first do max_partition_order */
2496 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2497 FLAC__uint64 abs_residual_partition_sum;
2499 unsigned partition, partition_sample, partition_samples, residual_sample;
2500 const unsigned partitions = 1u << partition_order;
2501 const unsigned default_partition_samples = blocksize >> partition_order;
2503 FLAC__ASSERT(default_partition_samples > predictor_order);
2505 for(partition = residual_sample = 0; partition < partitions; partition++) {
2506 partition_samples = default_partition_samples;
2508 partition_samples -= predictor_order;
2509 abs_residual_partition_sum = 0;
2510 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2511 abs_r = abs_residual[residual_sample];
2512 abs_residual_partition_sum += abs_r;
2515 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2517 to_partition = partitions;
2521 /* now merge partitions for lower orders */
2522 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2525 const unsigned partitions = 1u << partition_order;
2526 for(i = 0; i < partitions; i++) {
2527 s = abs_residual_partition_sums[from_partition];
2529 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
2536 void precompute_partition_info_escapes_(
2537 const FLAC__int32 residual[],
2538 unsigned raw_bits_per_partition[],
2539 unsigned residual_samples,
2540 unsigned predictor_order,
2541 unsigned min_partition_order,
2542 unsigned max_partition_order
2545 int partition_order;
2546 unsigned from_partition, to_partition = 0;
2547 const unsigned blocksize = residual_samples + predictor_order;
2549 /* first do max_partition_order */
2550 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2551 FLAC__int32 r, residual_partition_min, residual_partition_max;
2552 unsigned silog2_min, silog2_max;
2553 unsigned partition, partition_sample, partition_samples, residual_sample;
2554 const unsigned partitions = 1u << partition_order;
2555 const unsigned default_partition_samples = blocksize >> partition_order;
2557 FLAC__ASSERT(default_partition_samples > predictor_order);
2559 for(partition = residual_sample = 0; partition < partitions; partition++) {
2560 partition_samples = default_partition_samples;
2562 partition_samples -= predictor_order;
2563 residual_partition_min = residual_partition_max = 0;
2564 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2565 r = residual[residual_sample];
2566 if(r < residual_partition_min)
2567 residual_partition_min = r;
2568 else if(r > residual_partition_max)
2569 residual_partition_max = r;
2572 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2573 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2574 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
2576 to_partition = partitions;
2580 /* now merge partitions for lower orders */
2581 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2584 const unsigned partitions = 1u << partition_order;
2585 for(i = 0; i < partitions; i++) {
2586 m = raw_bits_per_partition[from_partition];
2588 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2595 #ifdef VARIABLE_RICE_BITS
2596 #undef VARIABLE_RICE_BITS
2598 #ifndef DONT_ESTIMATE_RICE_BITS
2599 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
2602 #ifdef DONT_ESTIMATE_RICE_BITS
2603 FLAC__bool set_partitioned_rice_(
2604 const FLAC__uint32 abs_residual[],
2605 const FLAC__int32 residual[],
2606 const unsigned residual_samples,
2607 const unsigned predictor_order,
2608 const unsigned suggested_rice_parameter,
2609 const unsigned rice_parameter_search_dist,
2610 const unsigned partition_order,
2611 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2615 FLAC__bool set_partitioned_rice_(
2616 const FLAC__uint32 abs_residual[],
2617 const unsigned residual_samples,
2618 const unsigned predictor_order,
2619 const unsigned suggested_rice_parameter,
2620 const unsigned rice_parameter_search_dist,
2621 const unsigned partition_order,
2622 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2627 unsigned rice_parameter, partition_bits;
2628 #ifndef NO_RICE_SEARCH
2629 unsigned best_partition_bits;
2630 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2632 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2633 unsigned *parameters;
2635 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2637 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2638 parameters = partitioned_rice_contents->parameters;
2640 if(partition_order == 0) {
2643 #ifndef NO_RICE_SEARCH
2644 if(rice_parameter_search_dist) {
2645 if(suggested_rice_parameter < rice_parameter_search_dist)
2646 min_rice_parameter = 0;
2648 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2649 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2650 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2651 #ifdef DEBUG_VERBOSE
2652 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2654 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2658 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2660 best_partition_bits = 0xffffffff;
2661 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2663 #ifdef VARIABLE_RICE_BITS
2664 #ifdef FLAC__SYMMETRIC_RICE
2665 partition_bits = (2+rice_parameter) * residual_samples;
2667 const unsigned rice_parameter_estimate = rice_parameter-1;
2668 partition_bits = (1+rice_parameter) * residual_samples;
2673 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2674 for(i = 0; i < residual_samples; i++) {
2675 #ifdef VARIABLE_RICE_BITS
2676 #ifdef FLAC__SYMMETRIC_RICE
2677 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2679 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2682 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2685 #ifndef NO_RICE_SEARCH
2686 if(partition_bits < best_partition_bits) {
2687 best_rice_parameter = rice_parameter;
2688 best_partition_bits = partition_bits;
2692 parameters[0] = best_rice_parameter;
2693 bits_ += best_partition_bits;
2696 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2697 unsigned partition_samples;
2698 FLAC__uint64 mean, k;
2699 const unsigned partitions = 1u << partition_order;
2700 for(partition = residual_sample = 0; partition < partitions; partition++) {
2701 partition_samples = (residual_samples+predictor_order) >> partition_order;
2702 if(partition == 0) {
2703 if(partition_samples <= predictor_order)
2706 partition_samples -= predictor_order;
2709 save_residual_sample = residual_sample;
2710 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2711 mean += abs_residual[residual_sample];
2712 residual_sample = save_residual_sample;
2713 #ifdef FLAC__SYMMETRIC_RICE
2714 mean += partition_samples >> 1; /* for rounding effect */
2715 mean /= partition_samples;
2717 /* calc rice_parameter = floor(log2(mean)) */
2725 /* calc rice_parameter ala LOCO-I */
2726 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2729 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2730 #ifdef DEBUG_VERBOSE
2731 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2733 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2736 #ifndef NO_RICE_SEARCH
2737 if(rice_parameter_search_dist) {
2738 if(rice_parameter < rice_parameter_search_dist)
2739 min_rice_parameter = 0;
2741 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2742 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2743 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2744 #ifdef DEBUG_VERBOSE
2745 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2747 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2751 min_rice_parameter = max_rice_parameter = rice_parameter;
2753 best_partition_bits = 0xffffffff;
2754 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2756 #ifdef VARIABLE_RICE_BITS
2757 #ifdef FLAC__SYMMETRIC_RICE
2758 partition_bits = (2+rice_parameter) * partition_samples;
2760 const unsigned rice_parameter_estimate = rice_parameter-1;
2761 partition_bits = (1+rice_parameter) * partition_samples;
2766 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2767 save_residual_sample = residual_sample;
2768 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2769 #ifdef VARIABLE_RICE_BITS
2770 #ifdef FLAC__SYMMETRIC_RICE
2771 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2773 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2776 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2779 #ifndef NO_RICE_SEARCH
2780 if(rice_parameter != max_rice_parameter)
2781 residual_sample = save_residual_sample;
2782 if(partition_bits < best_partition_bits) {
2783 best_rice_parameter = rice_parameter;
2784 best_partition_bits = partition_bits;
2788 parameters[partition] = best_rice_parameter;
2789 bits_ += best_partition_bits;
2797 #ifdef DONT_ESTIMATE_RICE_BITS
2798 FLAC__bool set_partitioned_rice_with_precompute_(
2799 const FLAC__int32 residual[],
2800 const FLAC__uint64 abs_residual_partition_sums[],
2801 const unsigned raw_bits_per_partition[],
2802 const unsigned residual_samples,
2803 const unsigned predictor_order,
2804 const unsigned suggested_rice_parameter,
2805 const unsigned rice_parameter_search_dist,
2806 const unsigned partition_order,
2807 const FLAC__bool search_for_escapes,
2808 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2812 FLAC__bool set_partitioned_rice_with_precompute_(
2813 const FLAC__uint32 abs_residual[],
2814 const FLAC__uint64 abs_residual_partition_sums[],
2815 const unsigned raw_bits_per_partition[],
2816 const unsigned residual_samples,
2817 const unsigned predictor_order,
2818 const unsigned suggested_rice_parameter,
2819 const unsigned rice_parameter_search_dist,
2820 const unsigned partition_order,
2821 const FLAC__bool search_for_escapes,
2822 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2827 unsigned rice_parameter, partition_bits;
2828 #ifndef NO_RICE_SEARCH
2829 unsigned best_partition_bits;
2830 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2833 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2834 unsigned *parameters, *raw_bits;
2836 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2838 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2839 parameters = partitioned_rice_contents->parameters;
2840 raw_bits = partitioned_rice_contents->raw_bits;
2842 if(partition_order == 0) {
2845 #ifndef NO_RICE_SEARCH
2846 if(rice_parameter_search_dist) {
2847 if(suggested_rice_parameter < rice_parameter_search_dist)
2848 min_rice_parameter = 0;
2850 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2851 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2852 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2853 #ifdef DEBUG_VERBOSE
2854 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2856 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2860 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2862 best_partition_bits = 0xffffffff;
2863 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2865 #ifdef VARIABLE_RICE_BITS
2866 #ifdef FLAC__SYMMETRIC_RICE
2867 partition_bits = (2+rice_parameter) * residual_samples;
2869 const unsigned rice_parameter_estimate = rice_parameter-1;
2870 partition_bits = (1+rice_parameter) * residual_samples;
2875 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2876 for(i = 0; i < residual_samples; i++) {
2877 #ifdef VARIABLE_RICE_BITS
2878 #ifdef FLAC__SYMMETRIC_RICE
2879 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2881 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2884 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2887 #ifndef NO_RICE_SEARCH
2888 if(partition_bits < best_partition_bits) {
2889 best_rice_parameter = rice_parameter;
2890 best_partition_bits = partition_bits;
2894 if(search_for_escapes) {
2895 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;
2896 if(flat_bits <= best_partition_bits) {
2897 raw_bits[0] = raw_bits_per_partition[0];
2898 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2899 best_partition_bits = flat_bits;
2902 parameters[0] = best_rice_parameter;
2903 bits_ += best_partition_bits;
2906 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2907 unsigned partition_samples;
2908 FLAC__uint64 mean, k;
2909 const unsigned partitions = 1u << partition_order;
2910 for(partition = residual_sample = 0; partition < partitions; partition++) {
2911 partition_samples = (residual_samples+predictor_order) >> partition_order;
2912 if(partition == 0) {
2913 if(partition_samples <= predictor_order)
2916 partition_samples -= predictor_order;
2918 mean = abs_residual_partition_sums[partition];
2919 #ifdef FLAC__SYMMETRIC_RICE
2920 mean += partition_samples >> 1; /* for rounding effect */
2921 mean /= partition_samples;
2923 /* calc rice_parameter = floor(log2(mean)) */
2931 /* calc rice_parameter ala LOCO-I */
2932 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2935 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2936 #ifdef DEBUG_VERBOSE
2937 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2939 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2942 #ifndef NO_RICE_SEARCH
2943 if(rice_parameter_search_dist) {
2944 if(rice_parameter < rice_parameter_search_dist)
2945 min_rice_parameter = 0;
2947 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2948 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2949 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2950 #ifdef DEBUG_VERBOSE
2951 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2953 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2957 min_rice_parameter = max_rice_parameter = rice_parameter;
2959 best_partition_bits = 0xffffffff;
2960 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2962 #ifdef VARIABLE_RICE_BITS
2963 #ifdef FLAC__SYMMETRIC_RICE
2964 partition_bits = (2+rice_parameter) * partition_samples;
2966 const unsigned rice_parameter_estimate = rice_parameter-1;
2967 partition_bits = (1+rice_parameter) * partition_samples;
2972 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2973 save_residual_sample = residual_sample;
2974 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2975 #ifdef VARIABLE_RICE_BITS
2976 #ifdef FLAC__SYMMETRIC_RICE
2977 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2979 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2982 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2985 #ifndef NO_RICE_SEARCH
2986 if(rice_parameter != max_rice_parameter)
2987 residual_sample = save_residual_sample;
2988 if(partition_bits < best_partition_bits) {
2989 best_rice_parameter = rice_parameter;
2990 best_partition_bits = partition_bits;
2994 if(search_for_escapes) {
2995 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;
2996 if(flat_bits <= best_partition_bits) {
2997 raw_bits[partition] = raw_bits_per_partition[partition];
2998 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2999 best_partition_bits = flat_bits;
3002 parameters[partition] = best_rice_parameter;
3003 bits_ += best_partition_bits;
3011 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
3016 for(i = 0; i < samples && !(x&1); i++)
3023 for(shift = 0; !(x&1); shift++)
3028 for(i = 0; i < samples; i++)
3029 signal[i] >>= shift;
3035 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3039 for(channel = 0; channel < channels; channel++)
3040 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3042 fifo->tail += wide_samples;
3044 FLAC__ASSERT(fifo->tail <= fifo->size);
3047 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3050 unsigned sample, wide_sample;
3051 unsigned tail = fifo->tail;
3053 sample = input_offset * channels;
3054 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3055 for(channel = 0; channel < channels; channel++)
3056 fifo->data[channel][tail] = input[sample++];
3061 FLAC__ASSERT(fifo->tail <= fifo->size);
3064 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3066 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3067 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3070 if(encoder->private_->verify.needs_magic_hack) {
3071 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3072 *bytes = FLAC__STREAM_SYNC_LENGTH;
3073 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3074 encoder->private_->verify.needs_magic_hack = false;
3077 if(encoded_bytes == 0) {
3079 * If we get here, a FIFO underflow has occurred,
3080 * which means there is a bug somewhere.
3083 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3085 else if(encoded_bytes < *bytes)
3086 *bytes = encoded_bytes;
3087 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3088 encoder->private_->verify.output.data += *bytes;
3089 encoder->private_->verify.output.bytes -= *bytes;
3092 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3095 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3097 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3099 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3100 const unsigned blocksize = frame->header.blocksize;
3101 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3103 for(channel = 0; channel < channels; channel++) {
3104 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3105 unsigned i, sample = 0;
3106 FLAC__int32 expect = 0, got = 0;
3108 for(i = 0; i < blocksize; i++) {
3109 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3111 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3112 got = (FLAC__int32)buffer[channel][i];
3116 FLAC__ASSERT(i < blocksize);
3117 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3118 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
3119 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
3120 encoder->private_->verify.error_stats.channel = channel;
3121 encoder->private_->verify.error_stats.sample = sample;
3122 encoder->private_->verify.error_stats.expected = expect;
3123 encoder->private_->verify.error_stats.got = got;
3124 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3125 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3128 /* dequeue the frame from the fifo */
3129 for(channel = 0; channel < channels; channel++) {
3130 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3132 encoder->private_->verify.input_fifo.tail -= blocksize;
3133 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3136 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3138 (void)decoder, (void)metadata, (void)client_data;
3141 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3143 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3144 (void)decoder, (void)status;
3145 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;