1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002 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"
41 #define min(x,y) ((x)<(y)?(x):(y))
46 #define max(x,y) ((x)>(y)?(x):(y))
49 FLAC__int32 *data[FLAC__MAX_CHANNELS];
50 unsigned size; /* of each data[] in samples */
55 const FLAC__byte *data;
62 ENCODER_IN_METADATA = 1,
66 /***********************************************************************
68 * Private class method prototypes
70 ***********************************************************************/
72 static void set_defaults_(FLAC__StreamEncoder *encoder);
73 static void free_(FLAC__StreamEncoder *encoder);
74 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
75 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
76 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
77 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
79 static FLAC__bool process_subframe_(
80 FLAC__StreamEncoder *encoder,
81 unsigned min_partition_order,
82 unsigned max_partition_order,
83 FLAC__bool precompute_partition_sums,
84 const FLAC__FrameHeader *frame_header,
85 unsigned subframe_bps,
86 const FLAC__int32 integer_signal[],
87 const FLAC__real real_signal[],
88 FLAC__Subframe *subframe[2],
89 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
90 FLAC__int32 *residual[2],
91 unsigned *best_subframe,
95 static FLAC__bool add_subframe_(
96 FLAC__StreamEncoder *encoder,
97 const FLAC__FrameHeader *frame_header,
98 unsigned subframe_bps,
99 const FLAC__Subframe *subframe,
100 FLAC__BitBuffer *frame
103 static unsigned evaluate_constant_subframe_(
104 const FLAC__int32 signal,
105 unsigned subframe_bps,
106 FLAC__Subframe *subframe
109 static unsigned evaluate_fixed_subframe_(
110 FLAC__StreamEncoder *encoder,
111 const FLAC__int32 signal[],
112 FLAC__int32 residual[],
113 FLAC__uint32 abs_residual[],
114 FLAC__uint64 abs_residual_partition_sums[],
115 unsigned raw_bits_per_partition[],
117 unsigned subframe_bps,
119 unsigned rice_parameter,
120 unsigned min_partition_order,
121 unsigned max_partition_order,
122 FLAC__bool precompute_partition_sums,
123 FLAC__bool do_escape_coding,
124 unsigned rice_parameter_search_dist,
125 FLAC__Subframe *subframe,
126 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
129 static unsigned evaluate_lpc_subframe_(
130 FLAC__StreamEncoder *encoder,
131 const FLAC__int32 signal[],
132 FLAC__int32 residual[],
133 FLAC__uint32 abs_residual[],
134 FLAC__uint64 abs_residual_partition_sums[],
135 unsigned raw_bits_per_partition[],
136 const FLAC__real lp_coeff[],
138 unsigned subframe_bps,
140 unsigned qlp_coeff_precision,
141 unsigned rice_parameter,
142 unsigned min_partition_order,
143 unsigned max_partition_order,
144 FLAC__bool precompute_partition_sums,
145 FLAC__bool do_escape_coding,
146 unsigned rice_parameter_search_dist,
147 FLAC__Subframe *subframe,
148 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
151 static unsigned evaluate_verbatim_subframe_(
152 const FLAC__int32 signal[],
154 unsigned subframe_bps,
155 FLAC__Subframe *subframe
158 static unsigned find_best_partition_order_(
159 struct FLAC__StreamEncoderPrivate *private_,
160 const FLAC__int32 residual[],
161 FLAC__uint32 abs_residual[],
162 FLAC__uint64 abs_residual_partition_sums[],
163 unsigned raw_bits_per_partition[],
164 unsigned residual_samples,
165 unsigned predictor_order,
166 unsigned rice_parameter,
167 unsigned min_partition_order,
168 unsigned max_partition_order,
169 FLAC__bool precompute_partition_sums,
170 FLAC__bool do_escape_coding,
171 unsigned rice_parameter_search_dist,
172 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
175 static void precompute_partition_info_sums_(
176 const FLAC__uint32 abs_residual[],
177 FLAC__uint64 abs_residual_partition_sums[],
178 unsigned residual_samples,
179 unsigned predictor_order,
180 unsigned min_partition_order,
181 unsigned max_partition_order
184 static void precompute_partition_info_escapes_(
185 const FLAC__int32 residual[],
186 unsigned raw_bits_per_partition[],
187 unsigned residual_samples,
188 unsigned predictor_order,
189 unsigned min_partition_order,
190 unsigned max_partition_order
193 #ifdef DONT_ESTIMATE_RICE_BITS
194 static FLAC__bool set_partitioned_rice_(
195 const FLAC__uint32 abs_residual[],
196 const FLAC__int32 residual[],
197 const unsigned residual_samples,
198 const unsigned predictor_order,
199 const unsigned suggested_rice_parameter,
200 const unsigned rice_parameter_search_dist,
201 const unsigned partition_order,
202 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
206 static FLAC__bool set_partitioned_rice_with_precompute_(
207 const FLAC__int32 residual[],
208 const FLAC__uint64 abs_residual_partition_sums[],
209 const unsigned raw_bits_per_partition[],
210 const unsigned residual_samples,
211 const unsigned predictor_order,
212 const unsigned suggested_rice_parameter,
213 const unsigned rice_parameter_search_dist,
214 const unsigned partition_order,
215 const FLAC__bool search_for_escapes,
216 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
220 static FLAC__bool set_partitioned_rice_(
221 const FLAC__uint32 abs_residual[],
222 const unsigned residual_samples,
223 const unsigned predictor_order,
224 const unsigned suggested_rice_parameter,
225 const unsigned rice_parameter_search_dist,
226 const unsigned partition_order,
227 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
231 static FLAC__bool set_partitioned_rice_with_precompute_(
232 const FLAC__uint32 abs_residual[],
233 const FLAC__uint64 abs_residual_partition_sums[],
234 const unsigned raw_bits_per_partition[],
235 const unsigned residual_samples,
236 const unsigned predictor_order,
237 const unsigned suggested_rice_parameter,
238 const unsigned rice_parameter_search_dist,
239 const unsigned partition_order,
240 const FLAC__bool search_for_escapes,
241 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
246 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
248 /* verify-related routines: */
249 static void append_to_verify_fifo_(
250 verify_input_fifo *fifo,
251 const FLAC__int32 * const input[],
252 unsigned input_offset,
254 unsigned wide_samples
257 static void append_to_verify_fifo_interleaved_(
258 verify_input_fifo *fifo,
259 const FLAC__int32 input[],
260 unsigned input_offset,
262 unsigned wide_samples
265 static FLAC__StreamDecoderReadStatus verify_read_callback_(
266 const FLAC__StreamDecoder *decoder,
272 static FLAC__StreamDecoderWriteStatus verify_write_callback_(
273 const FLAC__StreamDecoder *decoder,
274 const FLAC__Frame *frame,
275 const FLAC__int32 * const buffer[],
279 static void verify_metadata_callback_(
280 const FLAC__StreamDecoder *decoder,
281 const FLAC__StreamMetadata *metadata,
285 static void verify_error_callback_(
286 const FLAC__StreamDecoder *decoder,
287 FLAC__StreamDecoderErrorStatus status,
292 /***********************************************************************
296 ***********************************************************************/
298 typedef struct FLAC__StreamEncoderPrivate {
299 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
300 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
301 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
302 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
303 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
304 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
305 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
306 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
307 FLAC__int32 *residual_workspace_mid_side[2][2];
308 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
309 FLAC__Subframe subframe_workspace_mid_side[2][2];
310 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
311 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
312 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
313 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
314 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
315 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
316 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
317 unsigned best_subframe_mid_side[2];
318 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
319 unsigned best_subframe_bits_mid_side[2];
320 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
321 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
322 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
323 FLAC__BitBuffer *frame; /* the current frame being worked on */
324 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
325 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
326 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
327 FLAC__ChannelAssignment last_channel_assignment;
328 FLAC__StreamMetadata metadata;
329 unsigned current_sample_number;
330 unsigned current_frame_number;
331 struct MD5Context md5context;
332 FLAC__CPUInfo cpuinfo;
333 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
334 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
335 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[]);
336 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[]);
337 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[]);
338 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
339 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
340 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
341 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
342 FLAC__bool disable_constant_subframes;
343 FLAC__bool disable_fixed_subframes;
344 FLAC__bool disable_verbatim_subframes;
345 FLAC__StreamEncoderWriteCallback write_callback;
346 FLAC__StreamEncoderMetadataCallback metadata_callback;
348 /* unaligned (original) pointers to allocated data */
349 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
350 FLAC__int32 *integer_signal_mid_side_unaligned[2];
351 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
352 FLAC__real *real_signal_mid_side_unaligned[2];
353 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
354 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
355 FLAC__uint32 *abs_residual_unaligned;
356 FLAC__uint64 *abs_residual_partition_sums_unaligned;
357 unsigned *raw_bits_per_partition_unaligned;
359 * These fields have been moved here from private function local
360 * declarations merely to save stack space during encoding.
362 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
363 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
365 * The data for the verify section
368 FLAC__StreamDecoder *decoder;
369 EncoderStateHint state_hint;
370 FLAC__bool needs_magic_hack;
371 verify_input_fifo input_fifo;
372 verify_output output;
374 FLAC__uint64 absolute_sample;
375 unsigned frame_number;
378 FLAC__int32 expected;
382 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
383 } FLAC__StreamEncoderPrivate;
385 /***********************************************************************
387 * Public static class data
389 ***********************************************************************/
391 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
392 "FLAC__STREAM_ENCODER_OK",
393 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
394 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
395 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
396 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
397 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
398 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
399 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
400 "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
401 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
402 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
403 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
404 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
405 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
406 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
407 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
408 "FLAC__STREAM_ENCODER_INVALID_METADATA",
409 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
410 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
411 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
412 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
413 "FLAC__STREAM_ENCODER_UNINITIALIZED"
416 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
417 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
418 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
421 /***********************************************************************
423 * Class constructor/destructor
426 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
428 FLAC__StreamEncoder *encoder;
431 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
433 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
438 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
439 if(encoder->protected_ == 0) {
444 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
445 if(encoder->private_ == 0) {
446 free(encoder->protected_);
451 encoder->private_->frame = FLAC__bitbuffer_new();
452 if(encoder->private_->frame == 0) {
453 free(encoder->private_);
454 free(encoder->protected_);
459 set_defaults_(encoder);
461 encoder->private_->is_being_deleted = false;
463 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
464 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
465 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
467 for(i = 0; i < 2; i++) {
468 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
469 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
471 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
472 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
473 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
475 for(i = 0; i < 2; i++) {
476 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
477 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
480 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
481 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
482 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
484 for(i = 0; i < 2; i++) {
485 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
486 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
488 for(i = 0; i < 2; i++)
489 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
491 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
496 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
500 FLAC__ASSERT(0 != encoder);
501 FLAC__ASSERT(0 != encoder->protected_);
502 FLAC__ASSERT(0 != encoder->private_);
503 FLAC__ASSERT(0 != encoder->private_->frame);
505 encoder->private_->is_being_deleted = true;
507 FLAC__stream_encoder_finish(encoder);
509 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
510 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
512 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
513 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
514 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
516 for(i = 0; i < 2; i++) {
517 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
518 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
520 for(i = 0; i < 2; i++)
521 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
523 FLAC__bitbuffer_delete(encoder->private_->frame);
524 free(encoder->private_);
525 free(encoder->protected_);
529 /***********************************************************************
531 * Public class methods
533 ***********************************************************************/
535 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
538 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
540 FLAC__ASSERT(0 != encoder);
542 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
543 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
545 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
547 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
548 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
550 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
551 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
553 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
554 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
556 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
557 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
559 if(encoder->protected_->bits_per_sample >= 32)
560 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
562 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
563 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
565 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
566 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
568 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
569 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
571 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
572 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
574 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
575 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
577 if(encoder->protected_->qlp_coeff_precision == 0) {
578 if(encoder->protected_->bits_per_sample < 16) {
579 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
580 /* @@@ until then we'll make a guess */
581 encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
583 else if(encoder->protected_->bits_per_sample == 16) {
584 if(encoder->protected_->blocksize <= 192)
585 encoder->protected_->qlp_coeff_precision = 7;
586 else if(encoder->protected_->blocksize <= 384)
587 encoder->protected_->qlp_coeff_precision = 8;
588 else if(encoder->protected_->blocksize <= 576)
589 encoder->protected_->qlp_coeff_precision = 9;
590 else if(encoder->protected_->blocksize <= 1152)
591 encoder->protected_->qlp_coeff_precision = 10;
592 else if(encoder->protected_->blocksize <= 2304)
593 encoder->protected_->qlp_coeff_precision = 11;
594 else if(encoder->protected_->blocksize <= 4608)
595 encoder->protected_->qlp_coeff_precision = 12;
597 encoder->protected_->qlp_coeff_precision = 13;
600 if(encoder->protected_->blocksize <= 384)
601 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
602 else if(encoder->protected_->blocksize <= 1152)
603 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
605 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
607 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
609 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
610 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
612 if(encoder->protected_->streamable_subset) {
614 encoder->protected_->blocksize != 192 &&
615 encoder->protected_->blocksize != 576 &&
616 encoder->protected_->blocksize != 1152 &&
617 encoder->protected_->blocksize != 2304 &&
618 encoder->protected_->blocksize != 4608 &&
619 encoder->protected_->blocksize != 256 &&
620 encoder->protected_->blocksize != 512 &&
621 encoder->protected_->blocksize != 1024 &&
622 encoder->protected_->blocksize != 2048 &&
623 encoder->protected_->blocksize != 4096 &&
624 encoder->protected_->blocksize != 8192 &&
625 encoder->protected_->blocksize != 16384
627 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
629 encoder->protected_->sample_rate != 8000 &&
630 encoder->protected_->sample_rate != 16000 &&
631 encoder->protected_->sample_rate != 22050 &&
632 encoder->protected_->sample_rate != 24000 &&
633 encoder->protected_->sample_rate != 32000 &&
634 encoder->protected_->sample_rate != 44100 &&
635 encoder->protected_->sample_rate != 48000 &&
636 encoder->protected_->sample_rate != 96000
638 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
640 encoder->protected_->bits_per_sample != 8 &&
641 encoder->protected_->bits_per_sample != 12 &&
642 encoder->protected_->bits_per_sample != 16 &&
643 encoder->protected_->bits_per_sample != 20 &&
644 encoder->protected_->bits_per_sample != 24
646 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
647 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
648 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
651 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
652 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
653 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
654 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
656 /* validate metadata */
657 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
658 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
659 metadata_has_seektable = false;
660 metadata_has_vorbis_comment = false;
661 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
662 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
663 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
664 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
665 if(metadata_has_seektable) /* only one is allowed */
666 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
667 metadata_has_seektable = true;
668 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
669 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
671 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
672 if(metadata_has_vorbis_comment) /* only one is allowed */
673 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
674 metadata_has_vorbis_comment = true;
678 encoder->private_->input_capacity = 0;
679 for(i = 0; i < encoder->protected_->channels; i++) {
680 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
681 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
683 for(i = 0; i < 2; i++) {
684 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
685 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
687 for(i = 0; i < encoder->protected_->channels; i++) {
688 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
689 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
690 encoder->private_->best_subframe[i] = 0;
692 for(i = 0; i < 2; i++) {
693 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
694 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
695 encoder->private_->best_subframe_mid_side[i] = 0;
697 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
698 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
699 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
700 encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
701 encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
702 if(encoder->private_->loose_mid_side_stereo_frames == 0)
703 encoder->private_->loose_mid_side_stereo_frames = 1;
704 encoder->private_->loose_mid_side_stereo_frame_count = 0;
705 encoder->private_->current_sample_number = 0;
706 encoder->private_->current_frame_number = 0;
708 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
709 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? */
710 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
713 * get the CPU info and set the function pointers
715 FLAC__cpu_info(&encoder->private_->cpuinfo);
716 /* first default to the non-asm routines */
717 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
718 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
719 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
720 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
721 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
722 /* now override with asm where appropriate */
724 if(encoder->private_->cpuinfo.use_asm) {
725 #ifdef FLAC__CPU_IA32
726 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
727 #ifdef FLAC__HAS_NASM
728 if(0 && encoder->private_->cpuinfo.data.ia32.sse) {
729 if(encoder->protected_->max_lpc_order < 4)
730 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
731 else if(encoder->protected_->max_lpc_order < 8)
732 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
733 else if(encoder->protected_->max_lpc_order < 12)
734 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
736 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
738 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
739 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
741 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
742 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
743 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
744 if(encoder->private_->cpuinfo.data.ia32.mmx) {
745 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
746 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
749 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
750 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
756 /* finally override based on wide-ness if necessary */
757 if(encoder->private_->use_wide_by_block) {
758 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
761 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
762 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
764 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
765 /* the above function sets the state for us in case of an error */
766 return encoder->protected_->state;
769 if(!FLAC__bitbuffer_init(encoder->private_->frame))
770 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
773 * Set up the verify stuff if necessary
775 if(encoder->protected_->verify) {
777 * First, set up the fifo which will hold the
778 * original signal to compare against
780 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
781 for(i = 0; i < encoder->protected_->channels; i++) {
782 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
783 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
785 encoder->private_->verify.input_fifo.tail = 0;
788 * Now set up a stream decoder for verification
790 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
791 if(0 == encoder->private_->verify.decoder)
792 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
794 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
795 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
796 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
797 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
798 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
799 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
800 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
802 encoder->private_->verify.error_stats.absolute_sample = 0;
803 encoder->private_->verify.error_stats.frame_number = 0;
804 encoder->private_->verify.error_stats.channel = 0;
805 encoder->private_->verify.error_stats.sample = 0;
806 encoder->private_->verify.error_stats.expected = 0;
807 encoder->private_->verify.error_stats.got = 0;
810 * write the stream header
812 if(encoder->protected_->verify)
813 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
814 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
815 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
816 if(!write_bitbuffer_(encoder, 0)) {
817 /* the above function sets the state for us in case of an error */
818 return encoder->protected_->state;
822 * write the STREAMINFO metadata block
824 if(encoder->protected_->verify)
825 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
826 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
827 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
828 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
829 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
830 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
831 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
832 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
833 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
834 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
835 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
836 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
837 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
838 MD5Init(&encoder->private_->md5context);
839 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
840 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
841 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
842 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
843 if(!write_bitbuffer_(encoder, 0)) {
844 /* the above function sets the state for us in case of an error */
845 return encoder->protected_->state;
849 * Now that the STREAMINFO block is written, we can init this to an
850 * absurdly-high value...
852 encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
853 /* ... and clear this to 0 */
854 encoder->private_->metadata.data.stream_info.total_samples = 0;
857 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
858 * if not, we will write an empty one (FLAC__add_metadata_block()
859 * automatically supplies the vendor string).
861 if(!metadata_has_vorbis_comment) {
862 FLAC__StreamMetadata vorbis_comment;
863 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
864 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
865 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
866 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
867 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
868 vorbis_comment.data.vorbis_comment.num_comments = 0;
869 vorbis_comment.data.vorbis_comment.comments = 0;
870 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
871 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
872 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
873 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
874 if(!write_bitbuffer_(encoder, 0)) {
875 /* the above function sets the state for us in case of an error */
876 return encoder->protected_->state;
881 * write the user's metadata blocks
883 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
884 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
885 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
886 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
887 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
888 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
889 if(!write_bitbuffer_(encoder, 0)) {
890 /* the above function sets the state for us in case of an error */
891 return encoder->protected_->state;
895 if(encoder->protected_->verify)
896 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
898 return encoder->protected_->state;
901 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
903 FLAC__ASSERT(0 != encoder);
905 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
908 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
909 if(encoder->private_->current_sample_number != 0) {
910 encoder->protected_->blocksize = encoder->private_->current_sample_number;
911 process_frame_(encoder, true); /* true => is last frame */
915 MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
917 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
918 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
921 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
922 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
925 set_defaults_(encoder);
927 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
930 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
932 FLAC__ASSERT(0 != encoder);
933 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
935 encoder->protected_->verify = value;
939 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
941 FLAC__ASSERT(0 != encoder);
942 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
944 encoder->protected_->streamable_subset = value;
948 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
950 FLAC__ASSERT(0 != encoder);
951 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
953 encoder->protected_->do_mid_side_stereo = value;
957 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
959 FLAC__ASSERT(0 != encoder);
960 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
962 encoder->protected_->loose_mid_side_stereo = value;
966 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
968 FLAC__ASSERT(0 != encoder);
969 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
971 encoder->protected_->channels = value;
975 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
977 FLAC__ASSERT(0 != encoder);
978 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
980 encoder->protected_->bits_per_sample = value;
984 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
986 FLAC__ASSERT(0 != encoder);
987 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
989 encoder->protected_->sample_rate = value;
993 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
995 FLAC__ASSERT(0 != encoder);
996 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
998 encoder->protected_->blocksize = value;
1002 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1004 FLAC__ASSERT(0 != encoder);
1005 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1007 encoder->protected_->max_lpc_order = value;
1011 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1013 FLAC__ASSERT(0 != encoder);
1014 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1016 encoder->protected_->qlp_coeff_precision = value;
1020 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1022 FLAC__ASSERT(0 != encoder);
1023 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1025 encoder->protected_->do_qlp_coeff_prec_search = value;
1029 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1031 FLAC__ASSERT(0 != encoder);
1032 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1035 /*@@@ deprecated: */
1036 encoder->protected_->do_escape_coding = value;
1043 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1045 FLAC__ASSERT(0 != encoder);
1046 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1048 encoder->protected_->do_exhaustive_model_search = value;
1052 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1054 FLAC__ASSERT(0 != encoder);
1055 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1057 encoder->protected_->min_residual_partition_order = value;
1061 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1063 FLAC__ASSERT(0 != encoder);
1064 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1066 encoder->protected_->max_residual_partition_order = value;
1070 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1072 FLAC__ASSERT(0 != encoder);
1073 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1076 /*@@@ deprecated: */
1077 encoder->protected_->rice_parameter_search_dist = value;
1084 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1086 FLAC__ASSERT(0 != encoder);
1087 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1089 encoder->protected_->total_samples_estimate = value;
1093 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1095 FLAC__ASSERT(0 != encoder);
1096 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1098 encoder->protected_->metadata = metadata;
1099 encoder->protected_->num_metadata_blocks = num_blocks;
1103 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
1105 FLAC__ASSERT(0 != encoder);
1106 FLAC__ASSERT(0 != value);
1107 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1109 encoder->private_->write_callback = value;
1113 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
1115 FLAC__ASSERT(0 != encoder);
1116 FLAC__ASSERT(0 != value);
1117 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1119 encoder->private_->metadata_callback = value;
1123 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
1125 FLAC__ASSERT(0 != encoder);
1126 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1128 encoder->private_->client_data = value;
1133 * These three functions are not static, but not publically exposed in
1134 * include/FLAC/ either. They are used by the test suite.
1136 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1138 FLAC__ASSERT(0 != encoder);
1139 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1141 encoder->private_->disable_constant_subframes = value;
1145 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1147 FLAC__ASSERT(0 != encoder);
1148 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1150 encoder->private_->disable_fixed_subframes = value;
1154 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1156 FLAC__ASSERT(0 != encoder);
1157 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1159 encoder->private_->disable_verbatim_subframes = value;
1163 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1165 FLAC__ASSERT(0 != encoder);
1166 return encoder->protected_->state;
1169 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1171 FLAC__ASSERT(0 != encoder);
1172 if(encoder->protected_->verify)
1173 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1175 return FLAC__STREAM_DECODER_UNINITIALIZED;
1178 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)
1180 FLAC__ASSERT(0 != encoder);
1181 if(0 != absolute_sample)
1182 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1183 if(0 != frame_number)
1184 *frame_number = encoder->private_->verify.error_stats.frame_number;
1186 *channel = encoder->private_->verify.error_stats.channel;
1188 *sample = encoder->private_->verify.error_stats.sample;
1190 *expected = encoder->private_->verify.error_stats.expected;
1192 *got = encoder->private_->verify.error_stats.got;
1195 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1197 FLAC__ASSERT(0 != encoder);
1198 return encoder->protected_->verify;
1201 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1203 FLAC__ASSERT(0 != encoder);
1204 return encoder->protected_->streamable_subset;
1207 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1209 FLAC__ASSERT(0 != encoder);
1210 return encoder->protected_->do_mid_side_stereo;
1213 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1215 FLAC__ASSERT(0 != encoder);
1216 return encoder->protected_->loose_mid_side_stereo;
1219 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1221 FLAC__ASSERT(0 != encoder);
1222 return encoder->protected_->channels;
1225 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1227 FLAC__ASSERT(0 != encoder);
1228 return encoder->protected_->bits_per_sample;
1231 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1233 FLAC__ASSERT(0 != encoder);
1234 return encoder->protected_->sample_rate;
1237 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1239 FLAC__ASSERT(0 != encoder);
1240 return encoder->protected_->blocksize;
1243 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1245 FLAC__ASSERT(0 != encoder);
1246 return encoder->protected_->max_lpc_order;
1249 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1251 FLAC__ASSERT(0 != encoder);
1252 return encoder->protected_->qlp_coeff_precision;
1255 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1257 FLAC__ASSERT(0 != encoder);
1258 return encoder->protected_->do_qlp_coeff_prec_search;
1261 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1263 FLAC__ASSERT(0 != encoder);
1264 return encoder->protected_->do_escape_coding;
1267 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1269 FLAC__ASSERT(0 != encoder);
1270 return encoder->protected_->do_exhaustive_model_search;
1273 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1275 FLAC__ASSERT(0 != encoder);
1276 return encoder->protected_->min_residual_partition_order;
1279 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1281 FLAC__ASSERT(0 != encoder);
1282 return encoder->protected_->max_residual_partition_order;
1285 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1287 FLAC__ASSERT(0 != encoder);
1288 return encoder->protected_->rice_parameter_search_dist;
1291 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1293 FLAC__ASSERT(0 != encoder);
1294 return encoder->protected_->total_samples_estimate;
1297 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1299 unsigned i, j, channel;
1300 FLAC__int32 x, mid, side;
1301 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1303 FLAC__ASSERT(0 != encoder);
1304 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1307 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1309 if(encoder->protected_->verify)
1310 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1312 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1313 x = mid = side = buffer[0][j];
1314 encoder->private_->integer_signal[0][i] = x;
1315 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1317 encoder->private_->integer_signal[1][i] = x;
1318 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1321 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1322 encoder->private_->integer_signal_mid_side[1][i] = side;
1323 encoder->private_->integer_signal_mid_side[0][i] = mid;
1324 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1325 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1326 encoder->private_->current_sample_number++;
1328 if(i == blocksize) {
1329 if(!process_frame_(encoder, false)) /* false => not last frame */
1332 } while(j < samples);
1336 if(encoder->protected_->verify)
1337 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1339 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1340 for(channel = 0; channel < channels; channel++) {
1341 x = buffer[channel][j];
1342 encoder->private_->integer_signal[channel][i] = x;
1343 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1345 encoder->private_->current_sample_number++;
1347 if(i == blocksize) {
1348 if(!process_frame_(encoder, false)) /* false => not last frame */
1351 } while(j < samples);
1357 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1359 unsigned i, j, k, channel;
1360 FLAC__int32 x, mid, side;
1361 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1363 FLAC__ASSERT(0 != encoder);
1364 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1367 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1369 if(encoder->protected_->verify)
1370 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1372 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1373 x = mid = side = buffer[k++];
1374 encoder->private_->integer_signal[0][i] = x;
1375 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1377 encoder->private_->integer_signal[1][i] = x;
1378 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1381 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1382 encoder->private_->integer_signal_mid_side[1][i] = side;
1383 encoder->private_->integer_signal_mid_side[0][i] = mid;
1384 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1385 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1386 encoder->private_->current_sample_number++;
1388 if(i == blocksize) {
1389 if(!process_frame_(encoder, false)) /* false => not last frame */
1392 } while(j < samples);
1396 if(encoder->protected_->verify)
1397 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1399 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1400 for(channel = 0; channel < channels; channel++) {
1402 encoder->private_->integer_signal[channel][i] = x;
1403 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1405 encoder->private_->current_sample_number++;
1407 if(i == blocksize) {
1408 if(!process_frame_(encoder, false)) /* false => not last frame */
1411 } while(j < samples);
1417 /***********************************************************************
1419 * Private class methods
1421 ***********************************************************************/
1423 void set_defaults_(FLAC__StreamEncoder *encoder)
1425 FLAC__ASSERT(0 != encoder);
1427 encoder->protected_->verify = false;
1428 encoder->protected_->streamable_subset = true;
1429 encoder->protected_->do_mid_side_stereo = false;
1430 encoder->protected_->loose_mid_side_stereo = false;
1431 encoder->protected_->channels = 2;
1432 encoder->protected_->bits_per_sample = 16;
1433 encoder->protected_->sample_rate = 44100;
1434 encoder->protected_->blocksize = 1152;
1435 encoder->protected_->max_lpc_order = 0;
1436 encoder->protected_->qlp_coeff_precision = 0;
1437 encoder->protected_->do_qlp_coeff_prec_search = false;
1438 encoder->protected_->do_exhaustive_model_search = false;
1439 encoder->protected_->do_escape_coding = false;
1440 encoder->protected_->min_residual_partition_order = 0;
1441 encoder->protected_->max_residual_partition_order = 0;
1442 encoder->protected_->rice_parameter_search_dist = 0;
1443 encoder->protected_->total_samples_estimate = 0;
1444 encoder->protected_->metadata = 0;
1445 encoder->protected_->num_metadata_blocks = 0;
1447 encoder->private_->disable_constant_subframes = false;
1448 encoder->private_->disable_fixed_subframes = false;
1449 encoder->private_->disable_verbatim_subframes = false;
1450 encoder->private_->write_callback = 0;
1451 encoder->private_->metadata_callback = 0;
1452 encoder->private_->client_data = 0;
1455 void free_(FLAC__StreamEncoder *encoder)
1457 unsigned i, channel;
1459 FLAC__ASSERT(0 != encoder);
1460 for(i = 0; i < encoder->protected_->channels; i++) {
1461 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1462 free(encoder->private_->integer_signal_unaligned[i]);
1463 encoder->private_->integer_signal_unaligned[i] = 0;
1465 if(0 != encoder->private_->real_signal_unaligned[i]) {
1466 free(encoder->private_->real_signal_unaligned[i]);
1467 encoder->private_->real_signal_unaligned[i] = 0;
1470 for(i = 0; i < 2; i++) {
1471 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1472 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1473 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1475 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1476 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1477 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1480 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1481 for(i = 0; i < 2; i++) {
1482 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1483 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1484 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1488 for(channel = 0; channel < 2; channel++) {
1489 for(i = 0; i < 2; i++) {
1490 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1491 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1492 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1496 if(0 != encoder->private_->abs_residual_unaligned) {
1497 free(encoder->private_->abs_residual_unaligned);
1498 encoder->private_->abs_residual_unaligned = 0;
1500 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1501 free(encoder->private_->abs_residual_partition_sums_unaligned);
1502 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1504 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1505 free(encoder->private_->raw_bits_per_partition_unaligned);
1506 encoder->private_->raw_bits_per_partition_unaligned = 0;
1508 if(encoder->protected_->verify) {
1509 for(i = 0; i < encoder->protected_->channels; i++) {
1510 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1511 free(encoder->private_->verify.input_fifo.data[i]);
1512 encoder->private_->verify.input_fifo.data[i] = 0;
1516 FLAC__bitbuffer_free(encoder->private_->frame);
1519 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
1522 unsigned i, channel;
1524 FLAC__ASSERT(new_size > 0);
1525 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1526 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1528 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1529 if(new_size <= encoder->private_->input_capacity)
1534 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1535 * requires that the input arrays (in our case the integer signals)
1536 * have a buffer of up to 3 zeroes in front (at negative indices) for
1537 * alignment purposes; we use 4 to keep the data well-aligned.
1540 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1541 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1542 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1543 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1544 encoder->private_->integer_signal[i] += 4;
1546 for(i = 0; ok && i < 2; i++) {
1547 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]);
1548 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]);
1549 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1550 encoder->private_->integer_signal_mid_side[i] += 4;
1552 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1553 for(i = 0; ok && i < 2; i++) {
1554 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1557 for(channel = 0; ok && channel < 2; channel++) {
1558 for(i = 0; ok && i < 2; i++) {
1559 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]);
1562 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1563 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 */
1564 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1565 if(encoder->protected_->do_escape_coding)
1566 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1569 encoder->private_->input_capacity = new_size;
1571 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1576 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
1578 const FLAC__byte *buffer;
1581 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1583 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1585 if(encoder->protected_->verify) {
1586 encoder->private_->verify.output.data = buffer;
1587 encoder->private_->verify.output.bytes = bytes;
1588 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1589 encoder->private_->verify.needs_magic_hack = true;
1592 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1593 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1594 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1595 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1601 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1602 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1606 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1609 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1610 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1616 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1618 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1621 * Accumulate raw signal to the MD5 signature
1623 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)) {
1624 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1629 * Process the frame header and subframes into the frame bitbuffer
1631 if(!process_subframes_(encoder, is_last_frame)) {
1632 /* the above function sets the state for us in case of an error */
1637 * Zero-pad the frame to a byte_boundary
1639 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1640 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1645 * CRC-16 the whole thing
1647 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1648 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1653 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1654 /* the above function sets the state for us in case of an error */
1659 * Get ready for the next frame
1661 encoder->private_->current_sample_number = 0;
1662 encoder->private_->current_frame_number++;
1663 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1668 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1670 FLAC__FrameHeader frame_header;
1671 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1672 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1675 * Calculate the min,max Rice partition orders
1678 max_partition_order = 0;
1681 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1682 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
1684 min_partition_order = min(min_partition_order, max_partition_order);
1686 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1691 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1692 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1695 frame_header.blocksize = encoder->protected_->blocksize;
1696 frame_header.sample_rate = encoder->protected_->sample_rate;
1697 frame_header.channels = encoder->protected_->channels;
1698 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1699 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1700 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1701 frame_header.number.frame_number = encoder->private_->current_frame_number;
1704 * Figure out what channel assignments to try
1706 if(encoder->protected_->do_mid_side_stereo) {
1707 if(encoder->protected_->loose_mid_side_stereo) {
1708 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1709 do_independent = true;
1713 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1714 do_mid_side = !do_independent;
1718 do_independent = true;
1723 do_independent = true;
1724 do_mid_side = false;
1727 FLAC__ASSERT(do_independent || do_mid_side);
1730 * Check for wasted bits; set effective bps for each subframe
1732 if(do_independent) {
1733 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1734 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1735 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1736 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1740 FLAC__ASSERT(encoder->protected_->channels == 2);
1741 for(channel = 0; channel < 2; channel++) {
1742 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1743 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1744 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1749 * First do a normal encoding pass of each independent channel
1751 if(do_independent) {
1752 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1756 min_partition_order,
1757 max_partition_order,
1758 precompute_partition_sums,
1760 encoder->private_->subframe_bps[channel],
1761 encoder->private_->integer_signal[channel],
1762 encoder->private_->real_signal[channel],
1763 encoder->private_->subframe_workspace_ptr[channel],
1764 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1765 encoder->private_->residual_workspace[channel],
1766 encoder->private_->best_subframe+channel,
1767 encoder->private_->best_subframe_bits+channel
1775 * Now do mid and side channels if requested
1778 FLAC__ASSERT(encoder->protected_->channels == 2);
1780 for(channel = 0; channel < 2; channel++) {
1784 min_partition_order,
1785 max_partition_order,
1786 precompute_partition_sums,
1788 encoder->private_->subframe_bps_mid_side[channel],
1789 encoder->private_->integer_signal_mid_side[channel],
1790 encoder->private_->real_signal_mid_side[channel],
1791 encoder->private_->subframe_workspace_ptr_mid_side[channel],
1792 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
1793 encoder->private_->residual_workspace_mid_side[channel],
1794 encoder->private_->best_subframe_mid_side+channel,
1795 encoder->private_->best_subframe_bits_mid_side+channel
1803 * Compose the frame bitbuffer
1806 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1807 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1808 FLAC__ChannelAssignment channel_assignment;
1810 FLAC__ASSERT(encoder->protected_->channels == 2);
1812 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1813 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1816 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1818 FLAC__ChannelAssignment ca;
1820 FLAC__ASSERT(do_independent && do_mid_side);
1822 /* We have to figure out which channel assignent results in the smallest frame */
1823 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
1824 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
1825 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
1826 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
1828 for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
1829 if(bits[ca] < min_bits) {
1830 min_bits = bits[ca];
1831 channel_assignment = ca;
1836 frame_header.channel_assignment = channel_assignment;
1838 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1839 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1843 switch(channel_assignment) {
1844 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1845 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1846 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1848 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1849 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1850 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1852 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1853 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1854 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1856 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1857 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1858 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1864 switch(channel_assignment) {
1865 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1866 left_bps = encoder->private_->subframe_bps [0];
1867 right_bps = encoder->private_->subframe_bps [1];
1869 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1870 left_bps = encoder->private_->subframe_bps [0];
1871 right_bps = encoder->private_->subframe_bps_mid_side[1];
1873 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1874 left_bps = encoder->private_->subframe_bps_mid_side[1];
1875 right_bps = encoder->private_->subframe_bps [1];
1877 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1878 left_bps = encoder->private_->subframe_bps_mid_side[0];
1879 right_bps = encoder->private_->subframe_bps_mid_side[1];
1885 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1886 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
1888 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1892 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1893 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1897 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1898 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
1899 /* the above function sets the state for us in case of an error */
1905 if(encoder->protected_->loose_mid_side_stereo) {
1906 encoder->private_->loose_mid_side_stereo_frame_count++;
1907 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1908 encoder->private_->loose_mid_side_stereo_frame_count = 0;
1911 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1916 FLAC__bool process_subframe_(
1917 FLAC__StreamEncoder *encoder,
1918 unsigned min_partition_order,
1919 unsigned max_partition_order,
1920 FLAC__bool precompute_partition_sums,
1921 const FLAC__FrameHeader *frame_header,
1922 unsigned subframe_bps,
1923 const FLAC__int32 integer_signal[],
1924 const FLAC__real real_signal[],
1925 FLAC__Subframe *subframe[2],
1926 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
1927 FLAC__int32 *residual[2],
1928 unsigned *best_subframe,
1932 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1933 FLAC__real lpc_residual_bits_per_sample;
1934 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 */
1935 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1936 unsigned min_lpc_order, max_lpc_order, lpc_order;
1937 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1938 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1939 unsigned rice_parameter;
1940 unsigned _candidate_bits, _best_bits;
1941 unsigned _best_subframe;
1943 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1945 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
1946 _best_bits = UINT_MAX;
1948 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1950 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1951 unsigned signal_is_constant = false;
1952 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);
1953 /* check for constant subframe */
1954 if(!encoder->private_->disable_constant_subframes && fixed_residual_bits_per_sample[1] == 0.0) {
1955 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1957 signal_is_constant = true;
1958 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1959 if(integer_signal[0] != integer_signal[i]) {
1960 signal_is_constant = false;
1965 if(signal_is_constant) {
1966 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1967 if(_candidate_bits < _best_bits) {
1968 _best_subframe = !_best_subframe;
1969 _best_bits = _candidate_bits;
1973 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
1975 if(encoder->protected_->do_exhaustive_model_search) {
1976 min_fixed_order = 0;
1977 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1980 min_fixed_order = max_fixed_order = guess_fixed_order;
1982 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1983 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1984 continue; /* don't even try */
1985 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 */
1986 #ifndef FLAC__SYMMETRIC_RICE
1987 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1989 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1990 #ifdef DEBUG_VERBOSE
1991 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1993 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1996 evaluate_fixed_subframe_(
1999 residual[!_best_subframe],
2000 encoder->private_->abs_residual,
2001 encoder->private_->abs_residual_partition_sums,
2002 encoder->private_->raw_bits_per_partition,
2003 frame_header->blocksize,
2007 min_partition_order,
2008 max_partition_order,
2009 precompute_partition_sums,
2010 encoder->protected_->do_escape_coding,
2011 encoder->protected_->rice_parameter_search_dist,
2012 subframe[!_best_subframe],
2013 partitioned_rice_contents[!_best_subframe]
2015 if(_candidate_bits < _best_bits) {
2016 _best_subframe = !_best_subframe;
2017 _best_bits = _candidate_bits;
2023 if(encoder->protected_->max_lpc_order > 0) {
2024 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
2025 max_lpc_order = frame_header->blocksize-1;
2027 max_lpc_order = encoder->protected_->max_lpc_order;
2028 if(max_lpc_order > 0) {
2029 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2030 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2031 if(autoc[0] != 0.0) {
2032 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2033 if(encoder->protected_->do_exhaustive_model_search) {
2037 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
2038 min_lpc_order = max_lpc_order = guess_lpc_order;
2040 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2041 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2042 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
2043 continue; /* don't even try */
2044 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2045 #ifndef FLAC__SYMMETRIC_RICE
2046 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2048 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2049 #ifdef DEBUG_VERBOSE
2050 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2052 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2054 if(encoder->protected_->do_qlp_coeff_prec_search) {
2055 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2056 /* ensure a 32-bit datapath throughout for 16bps or less */
2057 if(subframe_bps <= 16)
2058 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2060 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2063 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2065 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2067 evaluate_lpc_subframe_(
2070 residual[!_best_subframe],
2071 encoder->private_->abs_residual,
2072 encoder->private_->abs_residual_partition_sums,
2073 encoder->private_->raw_bits_per_partition,
2074 encoder->private_->lp_coeff[lpc_order-1],
2075 frame_header->blocksize,
2078 qlp_coeff_precision,
2080 min_partition_order,
2081 max_partition_order,
2082 precompute_partition_sums,
2083 encoder->protected_->do_escape_coding,
2084 encoder->protected_->rice_parameter_search_dist,
2085 subframe[!_best_subframe],
2086 partitioned_rice_contents[!_best_subframe]
2088 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2089 if(_candidate_bits < _best_bits) {
2090 _best_subframe = !_best_subframe;
2091 _best_bits = _candidate_bits;
2102 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2103 if(_best_bits == UINT_MAX) {
2104 FLAC__ASSERT(_best_subframe == 0);
2105 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2108 *best_subframe = _best_subframe;
2109 *best_bits = _best_bits;
2114 FLAC__bool add_subframe_(
2115 FLAC__StreamEncoder *encoder,
2116 const FLAC__FrameHeader *frame_header,
2117 unsigned subframe_bps,
2118 const FLAC__Subframe *subframe,
2119 FLAC__BitBuffer *frame
2122 switch(subframe->type) {
2123 case FLAC__SUBFRAME_TYPE_CONSTANT:
2124 //@@@@fprintf(stderr,"@@@@ add CONSTANT, bps=%u\n",subframe_bps);
2125 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
2126 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2130 case FLAC__SUBFRAME_TYPE_FIXED:
2131 //@@@@fprintf(stderr,"@@@@ add FIXED, bps=%u, order=%u\n",subframe_bps,subframe->data.fixed.order);
2132 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2133 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2137 case FLAC__SUBFRAME_TYPE_LPC:
2138 //@@@@fprintf(stderr,"@@@@ add LPC, bps=%u, order=%u, prec=%u, shift=%d\n",subframe_bps,subframe->data.lpc.order,subframe->data.lpc.qlp_coeff_precision,subframe->data.lpc.quantization_level);
2139 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2140 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2144 case FLAC__SUBFRAME_TYPE_VERBATIM:
2145 //@@@@fprintf(stderr,"@@@@ add VERBATIM, bps=%u\n",subframe_bps);
2146 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2147 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2158 unsigned evaluate_constant_subframe_(
2159 const FLAC__int32 signal,
2160 unsigned subframe_bps,
2161 FLAC__Subframe *subframe
2164 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2165 subframe->data.constant.value = signal;
2167 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
2170 unsigned evaluate_fixed_subframe_(
2171 FLAC__StreamEncoder *encoder,
2172 const FLAC__int32 signal[],
2173 FLAC__int32 residual[],
2174 FLAC__uint32 abs_residual[],
2175 FLAC__uint64 abs_residual_partition_sums[],
2176 unsigned raw_bits_per_partition[],
2178 unsigned subframe_bps,
2180 unsigned rice_parameter,
2181 unsigned min_partition_order,
2182 unsigned max_partition_order,
2183 FLAC__bool precompute_partition_sums,
2184 FLAC__bool do_escape_coding,
2185 unsigned rice_parameter_search_dist,
2186 FLAC__Subframe *subframe,
2187 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2190 unsigned i, residual_bits;
2191 const unsigned residual_samples = blocksize - order;
2193 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2195 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2197 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2198 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2199 subframe->data.fixed.residual = residual;
2202 find_best_partition_order_(
2206 abs_residual_partition_sums,
2207 raw_bits_per_partition,
2211 min_partition_order,
2212 max_partition_order,
2213 precompute_partition_sums,
2215 rice_parameter_search_dist,
2216 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2219 subframe->data.fixed.order = order;
2220 for(i = 0; i < order; i++)
2221 subframe->data.fixed.warmup[i] = signal[i];
2223 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
2226 unsigned evaluate_lpc_subframe_(
2227 FLAC__StreamEncoder *encoder,
2228 const FLAC__int32 signal[],
2229 FLAC__int32 residual[],
2230 FLAC__uint32 abs_residual[],
2231 FLAC__uint64 abs_residual_partition_sums[],
2232 unsigned raw_bits_per_partition[],
2233 const FLAC__real lp_coeff[],
2235 unsigned subframe_bps,
2237 unsigned qlp_coeff_precision,
2238 unsigned rice_parameter,
2239 unsigned min_partition_order,
2240 unsigned max_partition_order,
2241 FLAC__bool precompute_partition_sums,
2242 FLAC__bool do_escape_coding,
2243 unsigned rice_parameter_search_dist,
2244 FLAC__Subframe *subframe,
2245 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2248 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
2249 unsigned i, residual_bits;
2250 int quantization, ret;
2251 const unsigned residual_samples = blocksize - order;
2253 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2254 if(subframe_bps <= 16) {
2255 FLAC__ASSERT(order > 0);
2256 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2257 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2260 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
2262 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2264 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2265 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2266 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2268 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2270 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2272 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2274 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2275 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2276 subframe->data.lpc.residual = residual;
2279 find_best_partition_order_(
2283 abs_residual_partition_sums,
2284 raw_bits_per_partition,
2288 min_partition_order,
2289 max_partition_order,
2290 precompute_partition_sums,
2292 rice_parameter_search_dist,
2293 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2296 subframe->data.lpc.order = order;
2297 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2298 subframe->data.lpc.quantization_level = quantization;
2299 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
2300 for(i = 0; i < order; i++)
2301 subframe->data.lpc.warmup[i] = signal[i];
2303 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;
2306 unsigned evaluate_verbatim_subframe_(
2307 const FLAC__int32 signal[],
2309 unsigned subframe_bps,
2310 FLAC__Subframe *subframe
2313 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2315 subframe->data.verbatim.data = signal;
2317 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
2320 unsigned find_best_partition_order_(
2321 FLAC__StreamEncoderPrivate *private_,
2322 const FLAC__int32 residual[],
2323 FLAC__uint32 abs_residual[],
2324 FLAC__uint64 abs_residual_partition_sums[],
2325 unsigned raw_bits_per_partition[],
2326 unsigned residual_samples,
2327 unsigned predictor_order,
2328 unsigned rice_parameter,
2329 unsigned min_partition_order,
2330 unsigned max_partition_order,
2331 FLAC__bool precompute_partition_sums,
2332 FLAC__bool do_escape_coding,
2333 unsigned rice_parameter_search_dist,
2334 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2338 unsigned residual_bits, best_residual_bits = 0;
2339 unsigned residual_sample;
2340 unsigned best_parameters_index = 0;
2341 const unsigned blocksize = residual_samples + predictor_order;
2343 /* compute abs(residual) for use later */
2344 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2345 r = residual[residual_sample];
2346 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2349 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2350 min_partition_order = min(min_partition_order, max_partition_order);
2352 if(precompute_partition_sums) {
2353 int partition_order;
2356 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2358 if(do_escape_coding)
2359 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2361 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2362 #ifdef DONT_ESTIMATE_RICE_BITS
2364 set_partitioned_rice_with_precompute_(
2366 abs_residual_partition_sums+sum,
2367 raw_bits_per_partition+sum,
2371 rice_parameter_search_dist,
2372 (unsigned)partition_order,
2374 &private_->partitioned_rice_contents_extra[!best_parameters_index],
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 FLAC__ASSERT(best_residual_bits != 0);
2399 sum += 1u << partition_order;
2400 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2401 best_residual_bits = residual_bits;
2402 best_parameters_index = !best_parameters_index;
2403 best_partitioned_rice->order = partition_order;
2408 unsigned partition_order;
2409 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2410 #ifdef DONT_ESTIMATE_RICE_BITS
2412 set_partitioned_rice_(
2418 rice_parameter_search_dist,
2420 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2426 set_partitioned_rice_(
2431 rice_parameter_search_dist,
2433 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2439 FLAC__ASSERT(best_residual_bits != 0);
2442 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2443 best_residual_bits = residual_bits;
2444 best_parameters_index = !best_parameters_index;
2445 best_partitioned_rice->order = partition_order;
2451 * We are allowed to de-const the pointer based on our special knowledge;
2452 * it is const to the outside world.
2455 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2456 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2457 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2458 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2461 return best_residual_bits;
2464 void precompute_partition_info_sums_(
2465 const FLAC__uint32 abs_residual[],
2466 FLAC__uint64 abs_residual_partition_sums[],
2467 unsigned residual_samples,
2468 unsigned predictor_order,
2469 unsigned min_partition_order,
2470 unsigned max_partition_order
2473 int partition_order;
2474 unsigned from_partition, to_partition = 0;
2475 const unsigned blocksize = residual_samples + predictor_order;
2477 /* first do max_partition_order */
2478 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2479 FLAC__uint64 abs_residual_partition_sum;
2481 unsigned partition, partition_sample, partition_samples, residual_sample;
2482 const unsigned partitions = 1u << partition_order;
2483 const unsigned default_partition_samples = blocksize >> partition_order;
2485 FLAC__ASSERT(default_partition_samples > predictor_order);
2487 for(partition = residual_sample = 0; partition < partitions; partition++) {
2488 partition_samples = default_partition_samples;
2490 partition_samples -= predictor_order;
2491 abs_residual_partition_sum = 0;
2492 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2493 abs_r = abs_residual[residual_sample];
2494 abs_residual_partition_sum += abs_r;
2497 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2499 to_partition = partitions;
2503 /* now merge partitions for lower orders */
2504 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2507 const unsigned partitions = 1u << partition_order;
2508 for(i = 0; i < partitions; i++) {
2509 s = abs_residual_partition_sums[from_partition];
2511 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
2518 void precompute_partition_info_escapes_(
2519 const FLAC__int32 residual[],
2520 unsigned raw_bits_per_partition[],
2521 unsigned residual_samples,
2522 unsigned predictor_order,
2523 unsigned min_partition_order,
2524 unsigned max_partition_order
2527 int partition_order;
2528 unsigned from_partition, to_partition = 0;
2529 const unsigned blocksize = residual_samples + predictor_order;
2531 /* first do max_partition_order */
2532 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2533 FLAC__int32 r, residual_partition_min, residual_partition_max;
2534 unsigned silog2_min, silog2_max;
2535 unsigned partition, partition_sample, partition_samples, residual_sample;
2536 const unsigned partitions = 1u << partition_order;
2537 const unsigned default_partition_samples = blocksize >> partition_order;
2539 FLAC__ASSERT(default_partition_samples > predictor_order);
2541 for(partition = residual_sample = 0; partition < partitions; partition++) {
2542 partition_samples = default_partition_samples;
2544 partition_samples -= predictor_order;
2545 residual_partition_min = residual_partition_max = 0;
2546 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2547 r = residual[residual_sample];
2548 if(r < residual_partition_min)
2549 residual_partition_min = r;
2550 else if(r > residual_partition_max)
2551 residual_partition_max = r;
2554 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2555 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2556 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
2558 to_partition = partitions;
2562 /* now merge partitions for lower orders */
2563 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2566 const unsigned partitions = 1u << partition_order;
2567 for(i = 0; i < partitions; i++) {
2568 m = raw_bits_per_partition[from_partition];
2570 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2577 #ifdef VARIABLE_RICE_BITS
2578 #undef VARIABLE_RICE_BITS
2580 #ifndef DONT_ESTIMATE_RICE_BITS
2581 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
2584 #ifdef DONT_ESTIMATE_RICE_BITS
2585 FLAC__bool set_partitioned_rice_(
2586 const FLAC__uint32 abs_residual[],
2587 const FLAC__int32 residual[],
2588 const unsigned residual_samples,
2589 const unsigned predictor_order,
2590 const unsigned suggested_rice_parameter,
2591 const unsigned rice_parameter_search_dist,
2592 const unsigned partition_order,
2593 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2597 FLAC__bool set_partitioned_rice_(
2598 const FLAC__uint32 abs_residual[],
2599 const unsigned residual_samples,
2600 const unsigned predictor_order,
2601 const unsigned suggested_rice_parameter,
2602 const unsigned rice_parameter_search_dist,
2603 const unsigned partition_order,
2604 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2609 unsigned rice_parameter, partition_bits;
2610 #ifndef NO_RICE_SEARCH
2611 unsigned best_partition_bits;
2612 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2614 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2615 unsigned *parameters;
2617 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2619 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2620 parameters = partitioned_rice_contents->parameters;
2622 if(partition_order == 0) {
2625 #ifndef NO_RICE_SEARCH
2626 if(rice_parameter_search_dist) {
2627 if(suggested_rice_parameter < rice_parameter_search_dist)
2628 min_rice_parameter = 0;
2630 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2631 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2632 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2633 #ifdef DEBUG_VERBOSE
2634 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2636 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2640 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2642 best_partition_bits = 0xffffffff;
2643 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2645 #ifdef VARIABLE_RICE_BITS
2646 #ifdef FLAC__SYMMETRIC_RICE
2647 partition_bits = (2+rice_parameter) * residual_samples;
2649 const unsigned rice_parameter_estimate = rice_parameter-1;
2650 partition_bits = (1+rice_parameter) * residual_samples;
2655 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2656 for(i = 0; i < residual_samples; i++) {
2657 #ifdef VARIABLE_RICE_BITS
2658 #ifdef FLAC__SYMMETRIC_RICE
2659 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2661 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2664 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2667 #ifndef NO_RICE_SEARCH
2668 if(partition_bits < best_partition_bits) {
2669 best_rice_parameter = rice_parameter;
2670 best_partition_bits = partition_bits;
2674 parameters[0] = best_rice_parameter;
2675 bits_ += best_partition_bits;
2678 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2679 unsigned partition_samples;
2680 FLAC__uint64 mean, k;
2681 const unsigned partitions = 1u << partition_order;
2682 for(partition = residual_sample = 0; partition < partitions; partition++) {
2683 partition_samples = (residual_samples+predictor_order) >> partition_order;
2684 if(partition == 0) {
2685 if(partition_samples <= predictor_order)
2688 partition_samples -= predictor_order;
2691 save_residual_sample = residual_sample;
2692 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2693 mean += abs_residual[residual_sample];
2694 residual_sample = save_residual_sample;
2695 #ifdef FLAC__SYMMETRIC_RICE
2696 mean += partition_samples >> 1; /* for rounding effect */
2697 mean /= partition_samples;
2699 /* calc rice_parameter = floor(log2(mean)) */
2707 /* calc rice_parameter ala LOCO-I */
2708 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2711 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2712 #ifdef DEBUG_VERBOSE
2713 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2715 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2718 #ifndef NO_RICE_SEARCH
2719 if(rice_parameter_search_dist) {
2720 if(rice_parameter < rice_parameter_search_dist)
2721 min_rice_parameter = 0;
2723 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2724 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2725 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2726 #ifdef DEBUG_VERBOSE
2727 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2729 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2733 min_rice_parameter = max_rice_parameter = rice_parameter;
2735 best_partition_bits = 0xffffffff;
2736 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2738 #ifdef VARIABLE_RICE_BITS
2739 #ifdef FLAC__SYMMETRIC_RICE
2740 partition_bits = (2+rice_parameter) * partition_samples;
2742 const unsigned rice_parameter_estimate = rice_parameter-1;
2743 partition_bits = (1+rice_parameter) * partition_samples;
2748 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2749 save_residual_sample = residual_sample;
2750 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2751 #ifdef VARIABLE_RICE_BITS
2752 #ifdef FLAC__SYMMETRIC_RICE
2753 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2755 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2758 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2761 #ifndef NO_RICE_SEARCH
2762 if(rice_parameter != max_rice_parameter)
2763 residual_sample = save_residual_sample;
2764 if(partition_bits < best_partition_bits) {
2765 best_rice_parameter = rice_parameter;
2766 best_partition_bits = partition_bits;
2770 parameters[partition] = best_rice_parameter;
2771 bits_ += best_partition_bits;
2779 #ifdef DONT_ESTIMATE_RICE_BITS
2780 FLAC__bool set_partitioned_rice_with_precompute_(
2781 const FLAC__int32 residual[],
2782 const FLAC__uint64 abs_residual_partition_sums[],
2783 const unsigned raw_bits_per_partition[],
2784 const unsigned residual_samples,
2785 const unsigned predictor_order,
2786 const unsigned suggested_rice_parameter,
2787 const unsigned rice_parameter_search_dist,
2788 const unsigned partition_order,
2789 const FLAC__bool search_for_escapes,
2790 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2794 FLAC__bool set_partitioned_rice_with_precompute_(
2795 const FLAC__uint32 abs_residual[],
2796 const FLAC__uint64 abs_residual_partition_sums[],
2797 const unsigned raw_bits_per_partition[],
2798 const unsigned residual_samples,
2799 const unsigned predictor_order,
2800 const unsigned suggested_rice_parameter,
2801 const unsigned rice_parameter_search_dist,
2802 const unsigned partition_order,
2803 const FLAC__bool search_for_escapes,
2804 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2809 unsigned rice_parameter, partition_bits;
2810 #ifndef NO_RICE_SEARCH
2811 unsigned best_partition_bits;
2812 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2815 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2816 unsigned *parameters, *raw_bits;
2818 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2820 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2821 parameters = partitioned_rice_contents->parameters;
2822 raw_bits = partitioned_rice_contents->raw_bits;
2824 if(partition_order == 0) {
2827 #ifndef NO_RICE_SEARCH
2828 if(rice_parameter_search_dist) {
2829 if(suggested_rice_parameter < rice_parameter_search_dist)
2830 min_rice_parameter = 0;
2832 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2833 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2834 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2835 #ifdef DEBUG_VERBOSE
2836 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2838 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2842 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2844 best_partition_bits = 0xffffffff;
2845 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2847 #ifdef VARIABLE_RICE_BITS
2848 #ifdef FLAC__SYMMETRIC_RICE
2849 partition_bits = (2+rice_parameter) * residual_samples;
2851 const unsigned rice_parameter_estimate = rice_parameter-1;
2852 partition_bits = (1+rice_parameter) * residual_samples;
2857 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2858 for(i = 0; i < residual_samples; i++) {
2859 #ifdef VARIABLE_RICE_BITS
2860 #ifdef FLAC__SYMMETRIC_RICE
2861 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2863 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2866 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2869 #ifndef NO_RICE_SEARCH
2870 if(partition_bits < best_partition_bits) {
2871 best_rice_parameter = rice_parameter;
2872 best_partition_bits = partition_bits;
2876 if(search_for_escapes) {
2877 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;
2878 if(flat_bits <= best_partition_bits) {
2879 raw_bits[0] = raw_bits_per_partition[0];
2880 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2881 best_partition_bits = flat_bits;
2884 parameters[0] = best_rice_parameter;
2885 bits_ += best_partition_bits;
2888 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2889 unsigned partition_samples;
2890 FLAC__uint64 mean, k;
2891 const unsigned partitions = 1u << partition_order;
2892 for(partition = residual_sample = 0; partition < partitions; partition++) {
2893 partition_samples = (residual_samples+predictor_order) >> partition_order;
2894 if(partition == 0) {
2895 if(partition_samples <= predictor_order)
2898 partition_samples -= predictor_order;
2900 mean = abs_residual_partition_sums[partition];
2901 #ifdef FLAC__SYMMETRIC_RICE
2902 mean += partition_samples >> 1; /* for rounding effect */
2903 mean /= partition_samples;
2905 /* calc rice_parameter = floor(log2(mean)) */
2913 /* calc rice_parameter ala LOCO-I */
2914 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2917 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2918 #ifdef DEBUG_VERBOSE
2919 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2921 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2924 #ifndef NO_RICE_SEARCH
2925 if(rice_parameter_search_dist) {
2926 if(rice_parameter < rice_parameter_search_dist)
2927 min_rice_parameter = 0;
2929 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2930 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2931 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2932 #ifdef DEBUG_VERBOSE
2933 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2935 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2939 min_rice_parameter = max_rice_parameter = rice_parameter;
2941 best_partition_bits = 0xffffffff;
2942 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2944 #ifdef VARIABLE_RICE_BITS
2945 #ifdef FLAC__SYMMETRIC_RICE
2946 partition_bits = (2+rice_parameter) * partition_samples;
2948 const unsigned rice_parameter_estimate = rice_parameter-1;
2949 partition_bits = (1+rice_parameter) * partition_samples;
2954 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2955 save_residual_sample = residual_sample;
2956 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2957 #ifdef VARIABLE_RICE_BITS
2958 #ifdef FLAC__SYMMETRIC_RICE
2959 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2961 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2964 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2967 #ifndef NO_RICE_SEARCH
2968 if(rice_parameter != max_rice_parameter)
2969 residual_sample = save_residual_sample;
2970 if(partition_bits < best_partition_bits) {
2971 best_rice_parameter = rice_parameter;
2972 best_partition_bits = partition_bits;
2976 if(search_for_escapes) {
2977 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;
2978 if(flat_bits <= best_partition_bits) {
2979 raw_bits[partition] = raw_bits_per_partition[partition];
2980 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2981 best_partition_bits = flat_bits;
2984 parameters[partition] = best_rice_parameter;
2985 bits_ += best_partition_bits;
2993 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
2998 for(i = 0; i < samples && !(x&1); i++)
3005 for(shift = 0; !(x&1); shift++)
3010 for(i = 0; i < samples; i++)
3011 signal[i] >>= shift;
3017 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3021 for(channel = 0; channel < channels; channel++)
3022 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3024 fifo->tail += wide_samples;
3026 FLAC__ASSERT(fifo->tail <= fifo->size);
3029 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3032 unsigned sample, wide_sample;
3033 unsigned tail = fifo->tail;
3035 sample = input_offset * channels;
3036 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3037 for(channel = 0; channel < channels; channel++)
3038 fifo->data[channel][tail] = input[sample++];
3043 FLAC__ASSERT(fifo->tail <= fifo->size);
3046 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3048 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3049 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3052 if(encoder->private_->verify.needs_magic_hack) {
3053 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3054 *bytes = FLAC__STREAM_SYNC_LENGTH;
3055 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3056 encoder->private_->verify.needs_magic_hack = false;
3059 if(encoded_bytes == 0) {
3061 * If we get here, a FIFO underflow has occurred,
3062 * which means there is a bug somewhere.
3065 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3067 else if(encoded_bytes < *bytes)
3068 *bytes = encoded_bytes;
3069 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3070 encoder->private_->verify.output.data += *bytes;
3071 encoder->private_->verify.output.bytes -= *bytes;
3074 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3077 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3079 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3081 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3082 const unsigned blocksize = frame->header.blocksize;
3083 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3085 for(channel = 0; channel < channels; channel++) {
3086 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3087 unsigned i, sample = 0;
3088 FLAC__int32 expect = 0, got = 0;
3090 for(i = 0; i < blocksize; i++) {
3091 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3093 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3094 got = (FLAC__int32)buffer[channel][i];
3098 FLAC__ASSERT(i < blocksize);
3099 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3100 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
3101 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
3102 encoder->private_->verify.error_stats.channel = channel;
3103 encoder->private_->verify.error_stats.sample = sample;
3104 encoder->private_->verify.error_stats.expected = expect;
3105 encoder->private_->verify.error_stats.got = got;
3106 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3107 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3110 /* dequeue the frame from the fifo */
3111 for(channel = 0; channel < channels; channel++) {
3112 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3114 encoder->private_->verify.input_fifo.tail -= blocksize;
3115 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3118 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3120 (void)decoder, (void)metadata, (void)client_data;
3123 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3125 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3126 (void)decoder, (void)status;
3127 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;