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_STREAMINFO)
667 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
668 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
669 if(metadata_has_seektable) /* only one is allowed */
670 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
671 metadata_has_seektable = true;
672 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
673 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
675 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
676 if(metadata_has_vorbis_comment) /* only one is allowed */
677 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
678 metadata_has_vorbis_comment = true;
680 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
681 if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
682 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
686 encoder->private_->input_capacity = 0;
687 for(i = 0; i < encoder->protected_->channels; i++) {
688 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
689 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
691 for(i = 0; i < 2; i++) {
692 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
693 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
695 for(i = 0; i < encoder->protected_->channels; i++) {
696 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
697 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
698 encoder->private_->best_subframe[i] = 0;
700 for(i = 0; i < 2; i++) {
701 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
702 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
703 encoder->private_->best_subframe_mid_side[i] = 0;
705 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
706 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
707 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
708 encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
709 encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
710 if(encoder->private_->loose_mid_side_stereo_frames == 0)
711 encoder->private_->loose_mid_side_stereo_frames = 1;
712 encoder->private_->loose_mid_side_stereo_frame_count = 0;
713 encoder->private_->current_sample_number = 0;
714 encoder->private_->current_frame_number = 0;
716 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
717 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? */
718 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
721 * get the CPU info and set the function pointers
723 FLAC__cpu_info(&encoder->private_->cpuinfo);
724 /* first default to the non-asm routines */
725 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
726 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
727 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
728 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
729 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
730 /* now override with asm where appropriate */
732 if(encoder->private_->cpuinfo.use_asm) {
733 #ifdef FLAC__CPU_IA32
734 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
735 #ifdef FLAC__HAS_NASM
737 if(encoder->private_->cpuinfo.data.ia32.sse) {
738 if(encoder->protected_->max_lpc_order < 4)
739 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
740 else if(encoder->protected_->max_lpc_order < 8)
741 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
742 else if(encoder->protected_->max_lpc_order < 12)
743 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
745 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
749 if(encoder->private_->cpuinfo.data.ia32._3dnow)
750 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
752 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
753 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
754 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
755 if(encoder->private_->cpuinfo.data.ia32.mmx) {
756 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
757 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
760 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
761 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
767 /* finally override based on wide-ness if necessary */
768 if(encoder->private_->use_wide_by_block) {
769 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
772 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
773 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
775 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
776 /* the above function sets the state for us in case of an error */
777 return encoder->protected_->state;
780 if(!FLAC__bitbuffer_init(encoder->private_->frame))
781 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
784 * Set up the verify stuff if necessary
786 if(encoder->protected_->verify) {
788 * First, set up the fifo which will hold the
789 * original signal to compare against
791 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
792 for(i = 0; i < encoder->protected_->channels; i++) {
793 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
794 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
796 encoder->private_->verify.input_fifo.tail = 0;
799 * Now set up a stream decoder for verification
801 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
802 if(0 == encoder->private_->verify.decoder)
803 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
805 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
806 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
807 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
808 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
809 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
810 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
811 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
813 encoder->private_->verify.error_stats.absolute_sample = 0;
814 encoder->private_->verify.error_stats.frame_number = 0;
815 encoder->private_->verify.error_stats.channel = 0;
816 encoder->private_->verify.error_stats.sample = 0;
817 encoder->private_->verify.error_stats.expected = 0;
818 encoder->private_->verify.error_stats.got = 0;
821 * write the stream header
823 if(encoder->protected_->verify)
824 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
825 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
826 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
827 if(!write_bitbuffer_(encoder, 0)) {
828 /* the above function sets the state for us in case of an error */
829 return encoder->protected_->state;
833 * write the STREAMINFO metadata block
835 if(encoder->protected_->verify)
836 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
837 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
838 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
839 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
840 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
841 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
842 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
843 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
844 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
845 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
846 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
847 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
848 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
849 MD5Init(&encoder->private_->md5context);
850 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
851 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
852 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
853 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
854 if(!write_bitbuffer_(encoder, 0)) {
855 /* the above function sets the state for us in case of an error */
856 return encoder->protected_->state;
860 * Now that the STREAMINFO block is written, we can init this to an
861 * absurdly-high value...
863 encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
864 /* ... and clear this to 0 */
865 encoder->private_->metadata.data.stream_info.total_samples = 0;
868 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
869 * if not, we will write an empty one (FLAC__add_metadata_block()
870 * automatically supplies the vendor string).
872 if(!metadata_has_vorbis_comment) {
873 FLAC__StreamMetadata vorbis_comment;
874 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
875 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
876 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
877 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
878 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
879 vorbis_comment.data.vorbis_comment.num_comments = 0;
880 vorbis_comment.data.vorbis_comment.comments = 0;
881 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
882 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
883 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
884 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
885 if(!write_bitbuffer_(encoder, 0)) {
886 /* the above function sets the state for us in case of an error */
887 return encoder->protected_->state;
892 * write the user's metadata blocks
894 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
895 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
896 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
897 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
898 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
899 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
900 if(!write_bitbuffer_(encoder, 0)) {
901 /* the above function sets the state for us in case of an error */
902 return encoder->protected_->state;
906 if(encoder->protected_->verify)
907 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
909 return encoder->protected_->state;
912 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
914 FLAC__ASSERT(0 != encoder);
916 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
919 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
920 if(encoder->private_->current_sample_number != 0) {
921 encoder->protected_->blocksize = encoder->private_->current_sample_number;
922 process_frame_(encoder, true); /* true => is last frame */
926 MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
928 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
929 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
932 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
933 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
936 set_defaults_(encoder);
938 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
941 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
943 FLAC__ASSERT(0 != encoder);
944 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
946 encoder->protected_->verify = value;
950 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
952 FLAC__ASSERT(0 != encoder);
953 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
955 encoder->protected_->streamable_subset = value;
959 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
961 FLAC__ASSERT(0 != encoder);
962 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
964 encoder->protected_->do_mid_side_stereo = value;
968 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
970 FLAC__ASSERT(0 != encoder);
971 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
973 encoder->protected_->loose_mid_side_stereo = value;
977 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
979 FLAC__ASSERT(0 != encoder);
980 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
982 encoder->protected_->channels = value;
986 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
988 FLAC__ASSERT(0 != encoder);
989 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
991 encoder->protected_->bits_per_sample = value;
995 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
997 FLAC__ASSERT(0 != encoder);
998 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1000 encoder->protected_->sample_rate = value;
1004 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1006 FLAC__ASSERT(0 != encoder);
1007 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1009 encoder->protected_->blocksize = value;
1013 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1015 FLAC__ASSERT(0 != encoder);
1016 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1018 encoder->protected_->max_lpc_order = value;
1022 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1024 FLAC__ASSERT(0 != encoder);
1025 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1027 encoder->protected_->qlp_coeff_precision = value;
1031 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1033 FLAC__ASSERT(0 != encoder);
1034 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1036 encoder->protected_->do_qlp_coeff_prec_search = value;
1040 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1042 FLAC__ASSERT(0 != encoder);
1043 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1046 /*@@@ deprecated: */
1047 encoder->protected_->do_escape_coding = value;
1054 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1056 FLAC__ASSERT(0 != encoder);
1057 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1059 encoder->protected_->do_exhaustive_model_search = value;
1063 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1065 FLAC__ASSERT(0 != encoder);
1066 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1068 encoder->protected_->min_residual_partition_order = value;
1072 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1074 FLAC__ASSERT(0 != encoder);
1075 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1077 encoder->protected_->max_residual_partition_order = value;
1081 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1083 FLAC__ASSERT(0 != encoder);
1084 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1087 /*@@@ deprecated: */
1088 encoder->protected_->rice_parameter_search_dist = value;
1095 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1097 FLAC__ASSERT(0 != encoder);
1098 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1100 encoder->protected_->total_samples_estimate = value;
1104 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1106 FLAC__ASSERT(0 != encoder);
1107 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1109 encoder->protected_->metadata = metadata;
1110 encoder->protected_->num_metadata_blocks = num_blocks;
1114 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
1116 FLAC__ASSERT(0 != encoder);
1117 FLAC__ASSERT(0 != value);
1118 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1120 encoder->private_->write_callback = value;
1124 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
1126 FLAC__ASSERT(0 != encoder);
1127 FLAC__ASSERT(0 != value);
1128 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1130 encoder->private_->metadata_callback = value;
1134 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
1136 FLAC__ASSERT(0 != encoder);
1137 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1139 encoder->private_->client_data = value;
1144 * These three functions are not static, but not publically exposed in
1145 * include/FLAC/ either. They are used by the test suite.
1147 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1149 FLAC__ASSERT(0 != encoder);
1150 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1152 encoder->private_->disable_constant_subframes = value;
1156 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1158 FLAC__ASSERT(0 != encoder);
1159 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1161 encoder->private_->disable_fixed_subframes = value;
1165 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1167 FLAC__ASSERT(0 != encoder);
1168 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1170 encoder->private_->disable_verbatim_subframes = value;
1174 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1176 FLAC__ASSERT(0 != encoder);
1177 return encoder->protected_->state;
1180 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1182 FLAC__ASSERT(0 != encoder);
1183 if(encoder->protected_->verify)
1184 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1186 return FLAC__STREAM_DECODER_UNINITIALIZED;
1189 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1191 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1192 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1194 return FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(encoder->private_->verify.decoder)];
1197 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)
1199 FLAC__ASSERT(0 != encoder);
1200 if(0 != absolute_sample)
1201 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1202 if(0 != frame_number)
1203 *frame_number = encoder->private_->verify.error_stats.frame_number;
1205 *channel = encoder->private_->verify.error_stats.channel;
1207 *sample = encoder->private_->verify.error_stats.sample;
1209 *expected = encoder->private_->verify.error_stats.expected;
1211 *got = encoder->private_->verify.error_stats.got;
1214 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1216 FLAC__ASSERT(0 != encoder);
1217 return encoder->protected_->verify;
1220 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1222 FLAC__ASSERT(0 != encoder);
1223 return encoder->protected_->streamable_subset;
1226 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1228 FLAC__ASSERT(0 != encoder);
1229 return encoder->protected_->do_mid_side_stereo;
1232 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1234 FLAC__ASSERT(0 != encoder);
1235 return encoder->protected_->loose_mid_side_stereo;
1238 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1240 FLAC__ASSERT(0 != encoder);
1241 return encoder->protected_->channels;
1244 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1246 FLAC__ASSERT(0 != encoder);
1247 return encoder->protected_->bits_per_sample;
1250 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1252 FLAC__ASSERT(0 != encoder);
1253 return encoder->protected_->sample_rate;
1256 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1258 FLAC__ASSERT(0 != encoder);
1259 return encoder->protected_->blocksize;
1262 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1264 FLAC__ASSERT(0 != encoder);
1265 return encoder->protected_->max_lpc_order;
1268 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1270 FLAC__ASSERT(0 != encoder);
1271 return encoder->protected_->qlp_coeff_precision;
1274 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1276 FLAC__ASSERT(0 != encoder);
1277 return encoder->protected_->do_qlp_coeff_prec_search;
1280 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1282 FLAC__ASSERT(0 != encoder);
1283 return encoder->protected_->do_escape_coding;
1286 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1288 FLAC__ASSERT(0 != encoder);
1289 return encoder->protected_->do_exhaustive_model_search;
1292 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1294 FLAC__ASSERT(0 != encoder);
1295 return encoder->protected_->min_residual_partition_order;
1298 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1300 FLAC__ASSERT(0 != encoder);
1301 return encoder->protected_->max_residual_partition_order;
1304 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1306 FLAC__ASSERT(0 != encoder);
1307 return encoder->protected_->rice_parameter_search_dist;
1310 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1312 FLAC__ASSERT(0 != encoder);
1313 return encoder->protected_->total_samples_estimate;
1316 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1318 unsigned i, j, channel;
1319 FLAC__int32 x, mid, side;
1320 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1322 FLAC__ASSERT(0 != encoder);
1323 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1326 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1328 if(encoder->protected_->verify)
1329 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1331 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1332 x = mid = side = buffer[0][j];
1333 encoder->private_->integer_signal[0][i] = x;
1334 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1336 encoder->private_->integer_signal[1][i] = x;
1337 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1340 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1341 encoder->private_->integer_signal_mid_side[1][i] = side;
1342 encoder->private_->integer_signal_mid_side[0][i] = mid;
1343 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1344 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1345 encoder->private_->current_sample_number++;
1347 if(i == blocksize) {
1348 if(!process_frame_(encoder, false)) /* false => not last frame */
1351 } while(j < samples);
1355 if(encoder->protected_->verify)
1356 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1358 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1359 for(channel = 0; channel < channels; channel++) {
1360 x = buffer[channel][j];
1361 encoder->private_->integer_signal[channel][i] = x;
1362 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1364 encoder->private_->current_sample_number++;
1366 if(i == blocksize) {
1367 if(!process_frame_(encoder, false)) /* false => not last frame */
1370 } while(j < samples);
1376 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1378 unsigned i, j, k, channel;
1379 FLAC__int32 x, mid, side;
1380 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1382 FLAC__ASSERT(0 != encoder);
1383 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1386 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1388 if(encoder->protected_->verify)
1389 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1391 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1392 x = mid = side = buffer[k++];
1393 encoder->private_->integer_signal[0][i] = x;
1394 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1396 encoder->private_->integer_signal[1][i] = x;
1397 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1400 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1401 encoder->private_->integer_signal_mid_side[1][i] = side;
1402 encoder->private_->integer_signal_mid_side[0][i] = mid;
1403 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1404 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1405 encoder->private_->current_sample_number++;
1407 if(i == blocksize) {
1408 if(!process_frame_(encoder, false)) /* false => not last frame */
1411 } while(j < samples);
1415 if(encoder->protected_->verify)
1416 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1418 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1419 for(channel = 0; channel < channels; channel++) {
1421 encoder->private_->integer_signal[channel][i] = x;
1422 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1424 encoder->private_->current_sample_number++;
1426 if(i == blocksize) {
1427 if(!process_frame_(encoder, false)) /* false => not last frame */
1430 } while(j < samples);
1436 /***********************************************************************
1438 * Private class methods
1440 ***********************************************************************/
1442 void set_defaults_(FLAC__StreamEncoder *encoder)
1444 FLAC__ASSERT(0 != encoder);
1446 encoder->protected_->verify = false;
1447 encoder->protected_->streamable_subset = true;
1448 encoder->protected_->do_mid_side_stereo = false;
1449 encoder->protected_->loose_mid_side_stereo = false;
1450 encoder->protected_->channels = 2;
1451 encoder->protected_->bits_per_sample = 16;
1452 encoder->protected_->sample_rate = 44100;
1453 encoder->protected_->blocksize = 1152;
1454 encoder->protected_->max_lpc_order = 0;
1455 encoder->protected_->qlp_coeff_precision = 0;
1456 encoder->protected_->do_qlp_coeff_prec_search = false;
1457 encoder->protected_->do_exhaustive_model_search = false;
1458 encoder->protected_->do_escape_coding = false;
1459 encoder->protected_->min_residual_partition_order = 0;
1460 encoder->protected_->max_residual_partition_order = 0;
1461 encoder->protected_->rice_parameter_search_dist = 0;
1462 encoder->protected_->total_samples_estimate = 0;
1463 encoder->protected_->metadata = 0;
1464 encoder->protected_->num_metadata_blocks = 0;
1466 encoder->private_->disable_constant_subframes = false;
1467 encoder->private_->disable_fixed_subframes = false;
1468 encoder->private_->disable_verbatim_subframes = false;
1469 encoder->private_->write_callback = 0;
1470 encoder->private_->metadata_callback = 0;
1471 encoder->private_->client_data = 0;
1474 void free_(FLAC__StreamEncoder *encoder)
1476 unsigned i, channel;
1478 FLAC__ASSERT(0 != encoder);
1479 for(i = 0; i < encoder->protected_->channels; i++) {
1480 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1481 free(encoder->private_->integer_signal_unaligned[i]);
1482 encoder->private_->integer_signal_unaligned[i] = 0;
1484 if(0 != encoder->private_->real_signal_unaligned[i]) {
1485 free(encoder->private_->real_signal_unaligned[i]);
1486 encoder->private_->real_signal_unaligned[i] = 0;
1489 for(i = 0; i < 2; i++) {
1490 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1491 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1492 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1494 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1495 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1496 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1499 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1500 for(i = 0; i < 2; i++) {
1501 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1502 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1503 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1507 for(channel = 0; channel < 2; channel++) {
1508 for(i = 0; i < 2; i++) {
1509 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1510 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1511 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1515 if(0 != encoder->private_->abs_residual_unaligned) {
1516 free(encoder->private_->abs_residual_unaligned);
1517 encoder->private_->abs_residual_unaligned = 0;
1519 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1520 free(encoder->private_->abs_residual_partition_sums_unaligned);
1521 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1523 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1524 free(encoder->private_->raw_bits_per_partition_unaligned);
1525 encoder->private_->raw_bits_per_partition_unaligned = 0;
1527 if(encoder->protected_->verify) {
1528 for(i = 0; i < encoder->protected_->channels; i++) {
1529 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1530 free(encoder->private_->verify.input_fifo.data[i]);
1531 encoder->private_->verify.input_fifo.data[i] = 0;
1535 FLAC__bitbuffer_free(encoder->private_->frame);
1538 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
1541 unsigned i, channel;
1543 FLAC__ASSERT(new_size > 0);
1544 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1545 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1547 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1548 if(new_size <= encoder->private_->input_capacity)
1553 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1554 * requires that the input arrays (in our case the integer signals)
1555 * have a buffer of up to 3 zeroes in front (at negative indices) for
1556 * alignment purposes; we use 4 to keep the data well-aligned.
1559 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1560 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1561 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1562 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1563 encoder->private_->integer_signal[i] += 4;
1565 for(i = 0; ok && i < 2; i++) {
1566 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]);
1567 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]);
1568 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1569 encoder->private_->integer_signal_mid_side[i] += 4;
1571 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1572 for(i = 0; ok && i < 2; i++) {
1573 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1576 for(channel = 0; ok && channel < 2; channel++) {
1577 for(i = 0; ok && i < 2; i++) {
1578 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]);
1581 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1582 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 */
1583 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1584 if(encoder->protected_->do_escape_coding)
1585 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1588 encoder->private_->input_capacity = new_size;
1590 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1595 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
1597 const FLAC__byte *buffer;
1600 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1602 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1604 if(encoder->protected_->verify) {
1605 encoder->private_->verify.output.data = buffer;
1606 encoder->private_->verify.output.bytes = bytes;
1607 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1608 encoder->private_->verify.needs_magic_hack = true;
1611 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1612 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1613 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1614 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1620 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1621 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1622 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1626 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1629 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1630 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1636 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1638 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1641 * Accumulate raw signal to the MD5 signature
1643 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)) {
1644 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1649 * Process the frame header and subframes into the frame bitbuffer
1651 if(!process_subframes_(encoder, is_last_frame)) {
1652 /* the above function sets the state for us in case of an error */
1657 * Zero-pad the frame to a byte_boundary
1659 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1660 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1665 * CRC-16 the whole thing
1667 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1668 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1673 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1674 /* the above function sets the state for us in case of an error */
1679 * Get ready for the next frame
1681 encoder->private_->current_sample_number = 0;
1682 encoder->private_->current_frame_number++;
1683 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1688 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1690 FLAC__FrameHeader frame_header;
1691 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1692 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1695 * Calculate the min,max Rice partition orders
1698 max_partition_order = 0;
1701 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1702 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
1704 min_partition_order = min(min_partition_order, max_partition_order);
1706 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1711 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1712 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1715 frame_header.blocksize = encoder->protected_->blocksize;
1716 frame_header.sample_rate = encoder->protected_->sample_rate;
1717 frame_header.channels = encoder->protected_->channels;
1718 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1719 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1720 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1721 frame_header.number.frame_number = encoder->private_->current_frame_number;
1724 * Figure out what channel assignments to try
1726 if(encoder->protected_->do_mid_side_stereo) {
1727 if(encoder->protected_->loose_mid_side_stereo) {
1728 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1729 do_independent = true;
1733 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1734 do_mid_side = !do_independent;
1738 do_independent = true;
1743 do_independent = true;
1744 do_mid_side = false;
1747 FLAC__ASSERT(do_independent || do_mid_side);
1750 * Check for wasted bits; set effective bps for each subframe
1752 if(do_independent) {
1753 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1754 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1755 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1756 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1760 FLAC__ASSERT(encoder->protected_->channels == 2);
1761 for(channel = 0; channel < 2; channel++) {
1762 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1763 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1764 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1769 * First do a normal encoding pass of each independent channel
1771 if(do_independent) {
1772 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1776 min_partition_order,
1777 max_partition_order,
1778 precompute_partition_sums,
1780 encoder->private_->subframe_bps[channel],
1781 encoder->private_->integer_signal[channel],
1782 encoder->private_->real_signal[channel],
1783 encoder->private_->subframe_workspace_ptr[channel],
1784 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1785 encoder->private_->residual_workspace[channel],
1786 encoder->private_->best_subframe+channel,
1787 encoder->private_->best_subframe_bits+channel
1795 * Now do mid and side channels if requested
1798 FLAC__ASSERT(encoder->protected_->channels == 2);
1800 for(channel = 0; channel < 2; channel++) {
1804 min_partition_order,
1805 max_partition_order,
1806 precompute_partition_sums,
1808 encoder->private_->subframe_bps_mid_side[channel],
1809 encoder->private_->integer_signal_mid_side[channel],
1810 encoder->private_->real_signal_mid_side[channel],
1811 encoder->private_->subframe_workspace_ptr_mid_side[channel],
1812 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
1813 encoder->private_->residual_workspace_mid_side[channel],
1814 encoder->private_->best_subframe_mid_side+channel,
1815 encoder->private_->best_subframe_bits_mid_side+channel
1823 * Compose the frame bitbuffer
1826 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1827 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1828 FLAC__ChannelAssignment channel_assignment;
1830 FLAC__ASSERT(encoder->protected_->channels == 2);
1832 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1833 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1836 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1838 FLAC__ChannelAssignment ca;
1840 FLAC__ASSERT(do_independent && do_mid_side);
1842 /* We have to figure out which channel assignent results in the smallest frame */
1843 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
1844 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
1845 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
1846 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
1848 for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
1849 if(bits[ca] < min_bits) {
1850 min_bits = bits[ca];
1851 channel_assignment = ca;
1856 frame_header.channel_assignment = channel_assignment;
1858 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1859 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1863 switch(channel_assignment) {
1864 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1865 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1866 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1868 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1869 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1870 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1872 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1873 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1874 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1876 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1877 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1878 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1884 switch(channel_assignment) {
1885 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1886 left_bps = encoder->private_->subframe_bps [0];
1887 right_bps = encoder->private_->subframe_bps [1];
1889 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1890 left_bps = encoder->private_->subframe_bps [0];
1891 right_bps = encoder->private_->subframe_bps_mid_side[1];
1893 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1894 left_bps = encoder->private_->subframe_bps_mid_side[1];
1895 right_bps = encoder->private_->subframe_bps [1];
1897 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1898 left_bps = encoder->private_->subframe_bps_mid_side[0];
1899 right_bps = encoder->private_->subframe_bps_mid_side[1];
1905 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1906 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
1908 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1912 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1913 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1917 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1918 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
1919 /* the above function sets the state for us in case of an error */
1925 if(encoder->protected_->loose_mid_side_stereo) {
1926 encoder->private_->loose_mid_side_stereo_frame_count++;
1927 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1928 encoder->private_->loose_mid_side_stereo_frame_count = 0;
1931 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1936 FLAC__bool process_subframe_(
1937 FLAC__StreamEncoder *encoder,
1938 unsigned min_partition_order,
1939 unsigned max_partition_order,
1940 FLAC__bool precompute_partition_sums,
1941 const FLAC__FrameHeader *frame_header,
1942 unsigned subframe_bps,
1943 const FLAC__int32 integer_signal[],
1944 const FLAC__real real_signal[],
1945 FLAC__Subframe *subframe[2],
1946 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
1947 FLAC__int32 *residual[2],
1948 unsigned *best_subframe,
1952 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1953 FLAC__real lpc_residual_bits_per_sample;
1954 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 */
1955 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1956 unsigned min_lpc_order, max_lpc_order, lpc_order;
1957 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1958 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1959 unsigned rice_parameter;
1960 unsigned _candidate_bits, _best_bits;
1961 unsigned _best_subframe;
1963 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1965 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
1966 _best_bits = UINT_MAX;
1968 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1970 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1971 unsigned signal_is_constant = false;
1972 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);
1973 /* check for constant subframe */
1974 if(!encoder->private_->disable_constant_subframes && fixed_residual_bits_per_sample[1] == 0.0) {
1975 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1977 signal_is_constant = true;
1978 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1979 if(integer_signal[0] != integer_signal[i]) {
1980 signal_is_constant = false;
1985 if(signal_is_constant) {
1986 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1987 if(_candidate_bits < _best_bits) {
1988 _best_subframe = !_best_subframe;
1989 _best_bits = _candidate_bits;
1993 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
1995 if(encoder->protected_->do_exhaustive_model_search) {
1996 min_fixed_order = 0;
1997 max_fixed_order = FLAC__MAX_FIXED_ORDER;
2000 min_fixed_order = max_fixed_order = guess_fixed_order;
2002 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
2003 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
2004 continue; /* don't even try */
2005 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 */
2006 #ifndef FLAC__SYMMETRIC_RICE
2007 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2009 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2010 #ifdef DEBUG_VERBOSE
2011 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2013 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2016 evaluate_fixed_subframe_(
2019 residual[!_best_subframe],
2020 encoder->private_->abs_residual,
2021 encoder->private_->abs_residual_partition_sums,
2022 encoder->private_->raw_bits_per_partition,
2023 frame_header->blocksize,
2027 min_partition_order,
2028 max_partition_order,
2029 precompute_partition_sums,
2030 encoder->protected_->do_escape_coding,
2031 encoder->protected_->rice_parameter_search_dist,
2032 subframe[!_best_subframe],
2033 partitioned_rice_contents[!_best_subframe]
2035 if(_candidate_bits < _best_bits) {
2036 _best_subframe = !_best_subframe;
2037 _best_bits = _candidate_bits;
2043 if(encoder->protected_->max_lpc_order > 0) {
2044 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
2045 max_lpc_order = frame_header->blocksize-1;
2047 max_lpc_order = encoder->protected_->max_lpc_order;
2048 if(max_lpc_order > 0) {
2049 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2050 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2051 if(autoc[0] != 0.0) {
2052 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2053 if(encoder->protected_->do_exhaustive_model_search) {
2057 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
2058 min_lpc_order = max_lpc_order = guess_lpc_order;
2060 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2061 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2062 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
2063 continue; /* don't even try */
2064 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2065 #ifndef FLAC__SYMMETRIC_RICE
2066 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2068 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2069 #ifdef DEBUG_VERBOSE
2070 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2072 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2074 if(encoder->protected_->do_qlp_coeff_prec_search) {
2075 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2076 /* ensure a 32-bit datapath throughout for 16bps or less */
2077 if(subframe_bps <= 16)
2078 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2080 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2083 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2085 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2087 evaluate_lpc_subframe_(
2090 residual[!_best_subframe],
2091 encoder->private_->abs_residual,
2092 encoder->private_->abs_residual_partition_sums,
2093 encoder->private_->raw_bits_per_partition,
2094 encoder->private_->lp_coeff[lpc_order-1],
2095 frame_header->blocksize,
2098 qlp_coeff_precision,
2100 min_partition_order,
2101 max_partition_order,
2102 precompute_partition_sums,
2103 encoder->protected_->do_escape_coding,
2104 encoder->protected_->rice_parameter_search_dist,
2105 subframe[!_best_subframe],
2106 partitioned_rice_contents[!_best_subframe]
2108 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2109 if(_candidate_bits < _best_bits) {
2110 _best_subframe = !_best_subframe;
2111 _best_bits = _candidate_bits;
2122 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2123 if(_best_bits == UINT_MAX) {
2124 FLAC__ASSERT(_best_subframe == 0);
2125 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2128 *best_subframe = _best_subframe;
2129 *best_bits = _best_bits;
2134 FLAC__bool add_subframe_(
2135 FLAC__StreamEncoder *encoder,
2136 const FLAC__FrameHeader *frame_header,
2137 unsigned subframe_bps,
2138 const FLAC__Subframe *subframe,
2139 FLAC__BitBuffer *frame
2142 switch(subframe->type) {
2143 case FLAC__SUBFRAME_TYPE_CONSTANT:
2144 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
2145 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2149 case FLAC__SUBFRAME_TYPE_FIXED:
2150 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2151 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2155 case FLAC__SUBFRAME_TYPE_LPC:
2156 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2157 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2161 case FLAC__SUBFRAME_TYPE_VERBATIM:
2162 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2163 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2174 unsigned evaluate_constant_subframe_(
2175 const FLAC__int32 signal,
2176 unsigned subframe_bps,
2177 FLAC__Subframe *subframe
2180 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2181 subframe->data.constant.value = signal;
2183 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
2186 unsigned evaluate_fixed_subframe_(
2187 FLAC__StreamEncoder *encoder,
2188 const FLAC__int32 signal[],
2189 FLAC__int32 residual[],
2190 FLAC__uint32 abs_residual[],
2191 FLAC__uint64 abs_residual_partition_sums[],
2192 unsigned raw_bits_per_partition[],
2194 unsigned subframe_bps,
2196 unsigned rice_parameter,
2197 unsigned min_partition_order,
2198 unsigned max_partition_order,
2199 FLAC__bool precompute_partition_sums,
2200 FLAC__bool do_escape_coding,
2201 unsigned rice_parameter_search_dist,
2202 FLAC__Subframe *subframe,
2203 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2206 unsigned i, residual_bits;
2207 const unsigned residual_samples = blocksize - order;
2209 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2211 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2213 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2214 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2215 subframe->data.fixed.residual = residual;
2218 find_best_partition_order_(
2222 abs_residual_partition_sums,
2223 raw_bits_per_partition,
2227 min_partition_order,
2228 max_partition_order,
2229 precompute_partition_sums,
2231 rice_parameter_search_dist,
2232 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2235 subframe->data.fixed.order = order;
2236 for(i = 0; i < order; i++)
2237 subframe->data.fixed.warmup[i] = signal[i];
2239 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
2242 unsigned evaluate_lpc_subframe_(
2243 FLAC__StreamEncoder *encoder,
2244 const FLAC__int32 signal[],
2245 FLAC__int32 residual[],
2246 FLAC__uint32 abs_residual[],
2247 FLAC__uint64 abs_residual_partition_sums[],
2248 unsigned raw_bits_per_partition[],
2249 const FLAC__real lp_coeff[],
2251 unsigned subframe_bps,
2253 unsigned qlp_coeff_precision,
2254 unsigned rice_parameter,
2255 unsigned min_partition_order,
2256 unsigned max_partition_order,
2257 FLAC__bool precompute_partition_sums,
2258 FLAC__bool do_escape_coding,
2259 unsigned rice_parameter_search_dist,
2260 FLAC__Subframe *subframe,
2261 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2264 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
2265 unsigned i, residual_bits;
2266 int quantization, ret;
2267 const unsigned residual_samples = blocksize - order;
2269 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2270 if(subframe_bps <= 16) {
2271 FLAC__ASSERT(order > 0);
2272 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2273 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2276 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
2278 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2280 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2281 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2282 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2284 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2286 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2288 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2290 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2291 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2292 subframe->data.lpc.residual = residual;
2295 find_best_partition_order_(
2299 abs_residual_partition_sums,
2300 raw_bits_per_partition,
2304 min_partition_order,
2305 max_partition_order,
2306 precompute_partition_sums,
2308 rice_parameter_search_dist,
2309 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2312 subframe->data.lpc.order = order;
2313 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2314 subframe->data.lpc.quantization_level = quantization;
2315 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
2316 for(i = 0; i < order; i++)
2317 subframe->data.lpc.warmup[i] = signal[i];
2319 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;
2322 unsigned evaluate_verbatim_subframe_(
2323 const FLAC__int32 signal[],
2325 unsigned subframe_bps,
2326 FLAC__Subframe *subframe
2329 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2331 subframe->data.verbatim.data = signal;
2333 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
2336 unsigned find_best_partition_order_(
2337 FLAC__StreamEncoderPrivate *private_,
2338 const FLAC__int32 residual[],
2339 FLAC__uint32 abs_residual[],
2340 FLAC__uint64 abs_residual_partition_sums[],
2341 unsigned raw_bits_per_partition[],
2342 unsigned residual_samples,
2343 unsigned predictor_order,
2344 unsigned rice_parameter,
2345 unsigned min_partition_order,
2346 unsigned max_partition_order,
2347 FLAC__bool precompute_partition_sums,
2348 FLAC__bool do_escape_coding,
2349 unsigned rice_parameter_search_dist,
2350 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2354 unsigned residual_bits, best_residual_bits = 0;
2355 unsigned residual_sample;
2356 unsigned best_parameters_index = 0;
2357 const unsigned blocksize = residual_samples + predictor_order;
2359 /* compute abs(residual) for use later */
2360 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2361 r = residual[residual_sample];
2362 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2365 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2366 min_partition_order = min(min_partition_order, max_partition_order);
2368 if(precompute_partition_sums) {
2369 int partition_order;
2372 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2374 if(do_escape_coding)
2375 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2377 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2378 #ifdef DONT_ESTIMATE_RICE_BITS
2380 set_partitioned_rice_with_precompute_(
2382 abs_residual_partition_sums+sum,
2383 raw_bits_per_partition+sum,
2387 rice_parameter_search_dist,
2388 (unsigned)partition_order,
2390 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2396 set_partitioned_rice_with_precompute_(
2398 abs_residual_partition_sums+sum,
2399 raw_bits_per_partition+sum,
2403 rice_parameter_search_dist,
2404 (unsigned)partition_order,
2406 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2412 FLAC__ASSERT(best_residual_bits != 0);
2415 sum += 1u << partition_order;
2416 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2417 best_residual_bits = residual_bits;
2418 best_parameters_index = !best_parameters_index;
2419 best_partitioned_rice->order = partition_order;
2424 unsigned partition_order;
2425 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2426 #ifdef DONT_ESTIMATE_RICE_BITS
2428 set_partitioned_rice_(
2434 rice_parameter_search_dist,
2436 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2442 set_partitioned_rice_(
2447 rice_parameter_search_dist,
2449 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2455 FLAC__ASSERT(best_residual_bits != 0);
2458 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2459 best_residual_bits = residual_bits;
2460 best_parameters_index = !best_parameters_index;
2461 best_partitioned_rice->order = partition_order;
2467 * We are allowed to de-const the pointer based on our special knowledge;
2468 * it is const to the outside world.
2471 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2472 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2473 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2474 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2477 return best_residual_bits;
2480 void precompute_partition_info_sums_(
2481 const FLAC__uint32 abs_residual[],
2482 FLAC__uint64 abs_residual_partition_sums[],
2483 unsigned residual_samples,
2484 unsigned predictor_order,
2485 unsigned min_partition_order,
2486 unsigned max_partition_order
2489 int partition_order;
2490 unsigned from_partition, to_partition = 0;
2491 const unsigned blocksize = residual_samples + predictor_order;
2493 /* first do max_partition_order */
2494 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2495 FLAC__uint64 abs_residual_partition_sum;
2497 unsigned partition, partition_sample, partition_samples, residual_sample;
2498 const unsigned partitions = 1u << partition_order;
2499 const unsigned default_partition_samples = blocksize >> partition_order;
2501 FLAC__ASSERT(default_partition_samples > predictor_order);
2503 for(partition = residual_sample = 0; partition < partitions; partition++) {
2504 partition_samples = default_partition_samples;
2506 partition_samples -= predictor_order;
2507 abs_residual_partition_sum = 0;
2508 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2509 abs_r = abs_residual[residual_sample];
2510 abs_residual_partition_sum += abs_r;
2513 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2515 to_partition = partitions;
2519 /* now merge partitions for lower orders */
2520 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2523 const unsigned partitions = 1u << partition_order;
2524 for(i = 0; i < partitions; i++) {
2525 s = abs_residual_partition_sums[from_partition];
2527 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
2534 void precompute_partition_info_escapes_(
2535 const FLAC__int32 residual[],
2536 unsigned raw_bits_per_partition[],
2537 unsigned residual_samples,
2538 unsigned predictor_order,
2539 unsigned min_partition_order,
2540 unsigned max_partition_order
2543 int partition_order;
2544 unsigned from_partition, to_partition = 0;
2545 const unsigned blocksize = residual_samples + predictor_order;
2547 /* first do max_partition_order */
2548 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2549 FLAC__int32 r, residual_partition_min, residual_partition_max;
2550 unsigned silog2_min, silog2_max;
2551 unsigned partition, partition_sample, partition_samples, residual_sample;
2552 const unsigned partitions = 1u << partition_order;
2553 const unsigned default_partition_samples = blocksize >> partition_order;
2555 FLAC__ASSERT(default_partition_samples > predictor_order);
2557 for(partition = residual_sample = 0; partition < partitions; partition++) {
2558 partition_samples = default_partition_samples;
2560 partition_samples -= predictor_order;
2561 residual_partition_min = residual_partition_max = 0;
2562 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2563 r = residual[residual_sample];
2564 if(r < residual_partition_min)
2565 residual_partition_min = r;
2566 else if(r > residual_partition_max)
2567 residual_partition_max = r;
2570 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2571 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2572 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
2574 to_partition = partitions;
2578 /* now merge partitions for lower orders */
2579 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2582 const unsigned partitions = 1u << partition_order;
2583 for(i = 0; i < partitions; i++) {
2584 m = raw_bits_per_partition[from_partition];
2586 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2593 #ifdef VARIABLE_RICE_BITS
2594 #undef VARIABLE_RICE_BITS
2596 #ifndef DONT_ESTIMATE_RICE_BITS
2597 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
2600 #ifdef DONT_ESTIMATE_RICE_BITS
2601 FLAC__bool set_partitioned_rice_(
2602 const FLAC__uint32 abs_residual[],
2603 const FLAC__int32 residual[],
2604 const unsigned residual_samples,
2605 const unsigned predictor_order,
2606 const unsigned suggested_rice_parameter,
2607 const unsigned rice_parameter_search_dist,
2608 const unsigned partition_order,
2609 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2613 FLAC__bool set_partitioned_rice_(
2614 const FLAC__uint32 abs_residual[],
2615 const unsigned residual_samples,
2616 const unsigned predictor_order,
2617 const unsigned suggested_rice_parameter,
2618 const unsigned rice_parameter_search_dist,
2619 const unsigned partition_order,
2620 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2625 unsigned rice_parameter, partition_bits;
2626 #ifndef NO_RICE_SEARCH
2627 unsigned best_partition_bits;
2628 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2630 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2631 unsigned *parameters;
2633 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2635 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2636 parameters = partitioned_rice_contents->parameters;
2638 if(partition_order == 0) {
2641 #ifndef NO_RICE_SEARCH
2642 if(rice_parameter_search_dist) {
2643 if(suggested_rice_parameter < rice_parameter_search_dist)
2644 min_rice_parameter = 0;
2646 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2647 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2648 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2649 #ifdef DEBUG_VERBOSE
2650 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2652 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2656 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2658 best_partition_bits = 0xffffffff;
2659 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2661 #ifdef VARIABLE_RICE_BITS
2662 #ifdef FLAC__SYMMETRIC_RICE
2663 partition_bits = (2+rice_parameter) * residual_samples;
2665 const unsigned rice_parameter_estimate = rice_parameter-1;
2666 partition_bits = (1+rice_parameter) * residual_samples;
2671 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2672 for(i = 0; i < residual_samples; i++) {
2673 #ifdef VARIABLE_RICE_BITS
2674 #ifdef FLAC__SYMMETRIC_RICE
2675 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2677 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2680 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2683 #ifndef NO_RICE_SEARCH
2684 if(partition_bits < best_partition_bits) {
2685 best_rice_parameter = rice_parameter;
2686 best_partition_bits = partition_bits;
2690 parameters[0] = best_rice_parameter;
2691 bits_ += best_partition_bits;
2694 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2695 unsigned partition_samples;
2696 FLAC__uint64 mean, k;
2697 const unsigned partitions = 1u << partition_order;
2698 for(partition = residual_sample = 0; partition < partitions; partition++) {
2699 partition_samples = (residual_samples+predictor_order) >> partition_order;
2700 if(partition == 0) {
2701 if(partition_samples <= predictor_order)
2704 partition_samples -= predictor_order;
2707 save_residual_sample = residual_sample;
2708 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2709 mean += abs_residual[residual_sample];
2710 residual_sample = save_residual_sample;
2711 #ifdef FLAC__SYMMETRIC_RICE
2712 mean += partition_samples >> 1; /* for rounding effect */
2713 mean /= partition_samples;
2715 /* calc rice_parameter = floor(log2(mean)) */
2723 /* calc rice_parameter ala LOCO-I */
2724 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2727 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2728 #ifdef DEBUG_VERBOSE
2729 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2731 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2734 #ifndef NO_RICE_SEARCH
2735 if(rice_parameter_search_dist) {
2736 if(rice_parameter < rice_parameter_search_dist)
2737 min_rice_parameter = 0;
2739 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2740 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2741 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2742 #ifdef DEBUG_VERBOSE
2743 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2745 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2749 min_rice_parameter = max_rice_parameter = rice_parameter;
2751 best_partition_bits = 0xffffffff;
2752 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2754 #ifdef VARIABLE_RICE_BITS
2755 #ifdef FLAC__SYMMETRIC_RICE
2756 partition_bits = (2+rice_parameter) * partition_samples;
2758 const unsigned rice_parameter_estimate = rice_parameter-1;
2759 partition_bits = (1+rice_parameter) * partition_samples;
2764 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2765 save_residual_sample = residual_sample;
2766 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2767 #ifdef VARIABLE_RICE_BITS
2768 #ifdef FLAC__SYMMETRIC_RICE
2769 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2771 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2774 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2777 #ifndef NO_RICE_SEARCH
2778 if(rice_parameter != max_rice_parameter)
2779 residual_sample = save_residual_sample;
2780 if(partition_bits < best_partition_bits) {
2781 best_rice_parameter = rice_parameter;
2782 best_partition_bits = partition_bits;
2786 parameters[partition] = best_rice_parameter;
2787 bits_ += best_partition_bits;
2795 #ifdef DONT_ESTIMATE_RICE_BITS
2796 FLAC__bool set_partitioned_rice_with_precompute_(
2797 const FLAC__int32 residual[],
2798 const FLAC__uint64 abs_residual_partition_sums[],
2799 const unsigned raw_bits_per_partition[],
2800 const unsigned residual_samples,
2801 const unsigned predictor_order,
2802 const unsigned suggested_rice_parameter,
2803 const unsigned rice_parameter_search_dist,
2804 const unsigned partition_order,
2805 const FLAC__bool search_for_escapes,
2806 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2810 FLAC__bool set_partitioned_rice_with_precompute_(
2811 const FLAC__uint32 abs_residual[],
2812 const FLAC__uint64 abs_residual_partition_sums[],
2813 const unsigned raw_bits_per_partition[],
2814 const unsigned residual_samples,
2815 const unsigned predictor_order,
2816 const unsigned suggested_rice_parameter,
2817 const unsigned rice_parameter_search_dist,
2818 const unsigned partition_order,
2819 const FLAC__bool search_for_escapes,
2820 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2825 unsigned rice_parameter, partition_bits;
2826 #ifndef NO_RICE_SEARCH
2827 unsigned best_partition_bits;
2828 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2831 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2832 unsigned *parameters, *raw_bits;
2834 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2836 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2837 parameters = partitioned_rice_contents->parameters;
2838 raw_bits = partitioned_rice_contents->raw_bits;
2840 if(partition_order == 0) {
2843 #ifndef NO_RICE_SEARCH
2844 if(rice_parameter_search_dist) {
2845 if(suggested_rice_parameter < rice_parameter_search_dist)
2846 min_rice_parameter = 0;
2848 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2849 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2850 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2851 #ifdef DEBUG_VERBOSE
2852 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2854 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2858 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2860 best_partition_bits = 0xffffffff;
2861 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2863 #ifdef VARIABLE_RICE_BITS
2864 #ifdef FLAC__SYMMETRIC_RICE
2865 partition_bits = (2+rice_parameter) * residual_samples;
2867 const unsigned rice_parameter_estimate = rice_parameter-1;
2868 partition_bits = (1+rice_parameter) * residual_samples;
2873 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2874 for(i = 0; i < residual_samples; i++) {
2875 #ifdef VARIABLE_RICE_BITS
2876 #ifdef FLAC__SYMMETRIC_RICE
2877 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2879 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2882 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2885 #ifndef NO_RICE_SEARCH
2886 if(partition_bits < best_partition_bits) {
2887 best_rice_parameter = rice_parameter;
2888 best_partition_bits = partition_bits;
2892 if(search_for_escapes) {
2893 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;
2894 if(flat_bits <= best_partition_bits) {
2895 raw_bits[0] = raw_bits_per_partition[0];
2896 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2897 best_partition_bits = flat_bits;
2900 parameters[0] = best_rice_parameter;
2901 bits_ += best_partition_bits;
2904 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2905 unsigned partition_samples;
2906 FLAC__uint64 mean, k;
2907 const unsigned partitions = 1u << partition_order;
2908 for(partition = residual_sample = 0; partition < partitions; partition++) {
2909 partition_samples = (residual_samples+predictor_order) >> partition_order;
2910 if(partition == 0) {
2911 if(partition_samples <= predictor_order)
2914 partition_samples -= predictor_order;
2916 mean = abs_residual_partition_sums[partition];
2917 #ifdef FLAC__SYMMETRIC_RICE
2918 mean += partition_samples >> 1; /* for rounding effect */
2919 mean /= partition_samples;
2921 /* calc rice_parameter = floor(log2(mean)) */
2929 /* calc rice_parameter ala LOCO-I */
2930 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2933 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2934 #ifdef DEBUG_VERBOSE
2935 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2937 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2940 #ifndef NO_RICE_SEARCH
2941 if(rice_parameter_search_dist) {
2942 if(rice_parameter < rice_parameter_search_dist)
2943 min_rice_parameter = 0;
2945 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2946 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2947 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2948 #ifdef DEBUG_VERBOSE
2949 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2951 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2955 min_rice_parameter = max_rice_parameter = rice_parameter;
2957 best_partition_bits = 0xffffffff;
2958 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2960 #ifdef VARIABLE_RICE_BITS
2961 #ifdef FLAC__SYMMETRIC_RICE
2962 partition_bits = (2+rice_parameter) * partition_samples;
2964 const unsigned rice_parameter_estimate = rice_parameter-1;
2965 partition_bits = (1+rice_parameter) * partition_samples;
2970 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2971 save_residual_sample = residual_sample;
2972 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2973 #ifdef VARIABLE_RICE_BITS
2974 #ifdef FLAC__SYMMETRIC_RICE
2975 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2977 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2980 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2983 #ifndef NO_RICE_SEARCH
2984 if(rice_parameter != max_rice_parameter)
2985 residual_sample = save_residual_sample;
2986 if(partition_bits < best_partition_bits) {
2987 best_rice_parameter = rice_parameter;
2988 best_partition_bits = partition_bits;
2992 if(search_for_escapes) {
2993 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;
2994 if(flat_bits <= best_partition_bits) {
2995 raw_bits[partition] = raw_bits_per_partition[partition];
2996 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2997 best_partition_bits = flat_bits;
3000 parameters[partition] = best_rice_parameter;
3001 bits_ += best_partition_bits;
3009 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
3014 for(i = 0; i < samples && !(x&1); i++)
3021 for(shift = 0; !(x&1); shift++)
3026 for(i = 0; i < samples; i++)
3027 signal[i] >>= shift;
3033 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3037 for(channel = 0; channel < channels; channel++)
3038 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3040 fifo->tail += wide_samples;
3042 FLAC__ASSERT(fifo->tail <= fifo->size);
3045 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3048 unsigned sample, wide_sample;
3049 unsigned tail = fifo->tail;
3051 sample = input_offset * channels;
3052 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3053 for(channel = 0; channel < channels; channel++)
3054 fifo->data[channel][tail] = input[sample++];
3059 FLAC__ASSERT(fifo->tail <= fifo->size);
3062 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3064 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3065 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3068 if(encoder->private_->verify.needs_magic_hack) {
3069 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3070 *bytes = FLAC__STREAM_SYNC_LENGTH;
3071 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3072 encoder->private_->verify.needs_magic_hack = false;
3075 if(encoded_bytes == 0) {
3077 * If we get here, a FIFO underflow has occurred,
3078 * which means there is a bug somewhere.
3081 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3083 else if(encoded_bytes < *bytes)
3084 *bytes = encoded_bytes;
3085 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3086 encoder->private_->verify.output.data += *bytes;
3087 encoder->private_->verify.output.bytes -= *bytes;
3090 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3093 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3095 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3097 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3098 const unsigned blocksize = frame->header.blocksize;
3099 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3101 for(channel = 0; channel < channels; channel++) {
3102 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3103 unsigned i, sample = 0;
3104 FLAC__int32 expect = 0, got = 0;
3106 for(i = 0; i < blocksize; i++) {
3107 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3109 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3110 got = (FLAC__int32)buffer[channel][i];
3114 FLAC__ASSERT(i < blocksize);
3115 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3116 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
3117 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
3118 encoder->private_->verify.error_stats.channel = channel;
3119 encoder->private_->verify.error_stats.sample = sample;
3120 encoder->private_->verify.error_stats.expected = expect;
3121 encoder->private_->verify.error_stats.got = got;
3122 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3123 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3126 /* dequeue the frame from the fifo */
3127 for(channel = 0; channel < channels; channel++) {
3128 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3130 encoder->private_->verify.input_fifo.tail -= blocksize;
3131 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3134 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3136 (void)decoder, (void)metadata, (void)client_data;
3139 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3141 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3142 (void)decoder, (void)status;
3143 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;