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.
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memcpy() */
23 #include "FLAC/assert.h"
24 #include "FLAC/stream_decoder.h"
25 #include "protected/stream_encoder.h"
26 #include "private/bitbuffer.h"
27 #include "private/bitmath.h"
28 #include "private/crc.h"
29 #include "private/cpu.h"
30 #include "private/fixed.h"
31 #include "private/format.h"
32 #include "private/lpc.h"
33 #include "private/md5.h"
34 #include "private/memory.h"
35 #include "private/stream_encoder_framing.h"
40 #define min(x,y) ((x)<(y)?(x):(y))
45 #define max(x,y) ((x)>(y)?(x):(y))
48 FLAC__int32 *data[FLAC__MAX_CHANNELS];
49 unsigned size; /* of each data[] in samples */
54 const FLAC__byte *data;
61 ENCODER_IN_METADATA = 1,
65 /***********************************************************************
67 * Private class method prototypes
69 ***********************************************************************/
71 static void set_defaults_(FLAC__StreamEncoder *encoder);
72 static void free_(FLAC__StreamEncoder *encoder);
73 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
74 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
75 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
76 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
78 static FLAC__bool process_subframe_(
79 FLAC__StreamEncoder *encoder,
80 unsigned min_partition_order,
81 unsigned max_partition_order,
82 FLAC__bool precompute_partition_sums,
83 FLAC__bool verbatim_only,
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_16bit)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
337 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
338 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
339 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
340 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
341 FLAC__StreamEncoderWriteCallback write_callback;
342 FLAC__StreamEncoderMetadataCallback metadata_callback;
344 /* unaligned (original) pointers to allocated data */
345 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
346 FLAC__int32 *integer_signal_mid_side_unaligned[2];
347 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
348 FLAC__real *real_signal_mid_side_unaligned[2];
349 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
350 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
351 FLAC__uint32 *abs_residual_unaligned;
352 FLAC__uint64 *abs_residual_partition_sums_unaligned;
353 unsigned *raw_bits_per_partition_unaligned;
355 * These fields have been moved here from private function local
356 * declarations merely to save stack space during encoding.
358 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
359 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
361 * The data for the verify section
364 FLAC__StreamDecoder *decoder;
365 EncoderStateHint state_hint;
366 FLAC__bool needs_magic_hack;
367 verify_input_fifo input_fifo;
368 verify_output output;
370 FLAC__uint64 absolute_sample;
371 unsigned frame_number;
374 FLAC__int32 expected;
378 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
379 } FLAC__StreamEncoderPrivate;
381 /***********************************************************************
383 * Public static class data
385 ***********************************************************************/
387 const char * const FLAC__StreamEncoderStateString[] = {
388 "FLAC__STREAM_ENCODER_OK",
389 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
390 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
391 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
392 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
393 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
394 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
395 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
396 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
397 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
398 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
399 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
400 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
401 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
402 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
403 "FLAC__STREAM_ENCODER_INVALID_METADATA",
404 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
405 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
406 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
407 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
408 "FLAC__STREAM_ENCODER_UNINITIALIZED"
411 const char * const FLAC__StreamEncoderWriteStatusString[] = {
412 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
413 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
416 /***********************************************************************
418 * Class constructor/destructor
421 FLAC__StreamEncoder *FLAC__stream_encoder_new()
423 FLAC__StreamEncoder *encoder;
426 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
428 encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
432 memset(encoder, 0, sizeof(FLAC__StreamEncoder));
434 encoder->protected_ = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
435 if(encoder->protected_ == 0) {
439 memset(encoder->protected_, 0, sizeof(FLAC__StreamEncoderProtected));
441 encoder->private_ = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
442 if(encoder->private_ == 0) {
443 free(encoder->protected_);
447 memset(encoder->private_, 0, sizeof(FLAC__StreamEncoderPrivate));
449 encoder->private_->frame = FLAC__bitbuffer_new();
450 if(encoder->private_->frame == 0) {
451 free(encoder->private_);
452 free(encoder->protected_);
457 set_defaults_(encoder);
459 encoder->private_->is_being_deleted = false;
461 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
462 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
463 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
465 for(i = 0; i < 2; i++) {
466 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
467 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
469 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
470 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
471 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
473 for(i = 0; i < 2; i++) {
474 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
475 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
478 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
479 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
480 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
482 for(i = 0; i < 2; i++) {
483 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
484 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
486 for(i = 0; i < 2; i++)
487 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
489 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
494 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
498 FLAC__ASSERT(0 != encoder);
499 FLAC__ASSERT(0 != encoder->protected_);
500 FLAC__ASSERT(0 != encoder->private_);
501 FLAC__ASSERT(0 != encoder->private_->frame);
503 encoder->private_->is_being_deleted = true;
505 FLAC__stream_encoder_finish(encoder);
507 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
508 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
510 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
511 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
512 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
514 for(i = 0; i < 2; i++) {
515 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
516 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
518 for(i = 0; i < 2; i++)
519 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
521 FLAC__bitbuffer_delete(encoder->private_->frame);
522 free(encoder->private_);
523 free(encoder->protected_);
527 /***********************************************************************
529 * Public class methods
531 ***********************************************************************/
533 FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
537 FLAC__ASSERT(0 != encoder);
539 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
540 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
542 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
544 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
545 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
547 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
548 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
550 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
551 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
553 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
554 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
556 if(encoder->protected_->bits_per_sample >= 32)
557 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
559 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
560 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
562 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
563 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
565 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
566 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
568 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
569 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
571 if(encoder->protected_->qlp_coeff_precision == 0) {
572 if(encoder->protected_->bits_per_sample < 16) {
573 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
574 /* @@@ until then we'll make a guess */
575 encoder->protected_->qlp_coeff_precision = max(5, 2 + encoder->protected_->bits_per_sample / 2);
577 else if(encoder->protected_->bits_per_sample == 16) {
578 if(encoder->protected_->blocksize <= 192)
579 encoder->protected_->qlp_coeff_precision = 7;
580 else if(encoder->protected_->blocksize <= 384)
581 encoder->protected_->qlp_coeff_precision = 8;
582 else if(encoder->protected_->blocksize <= 576)
583 encoder->protected_->qlp_coeff_precision = 9;
584 else if(encoder->protected_->blocksize <= 1152)
585 encoder->protected_->qlp_coeff_precision = 10;
586 else if(encoder->protected_->blocksize <= 2304)
587 encoder->protected_->qlp_coeff_precision = 11;
588 else if(encoder->protected_->blocksize <= 4608)
589 encoder->protected_->qlp_coeff_precision = 12;
591 encoder->protected_->qlp_coeff_precision = 13;
594 encoder->protected_->qlp_coeff_precision = min(13, 8*sizeof(FLAC__int32) - encoder->protected_->bits_per_sample - 1 - 2); /* @@@ -2 to keep things 32-bit safe */
597 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision + encoder->protected_->bits_per_sample >= 8*sizeof(FLAC__uint32) || encoder->protected_->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
598 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
600 if(encoder->protected_->streamable_subset) {
601 /*@@@ add check for blocksize here */
602 if(encoder->protected_->bits_per_sample != 8 && encoder->protected_->bits_per_sample != 12 && encoder->protected_->bits_per_sample != 16 && encoder->protected_->bits_per_sample != 20 && encoder->protected_->bits_per_sample != 24)
603 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
604 if(encoder->protected_->sample_rate > 655350)
605 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
608 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
609 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
610 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
611 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
613 /* validate metadata */
614 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
615 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
616 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
617 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
618 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
619 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
620 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
621 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
625 encoder->private_->input_capacity = 0;
626 for(i = 0; i < encoder->protected_->channels; i++) {
627 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
628 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
630 for(i = 0; i < 2; i++) {
631 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
632 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
634 for(i = 0; i < encoder->protected_->channels; i++) {
635 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
636 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
637 encoder->private_->best_subframe[i] = 0;
639 for(i = 0; i < 2; i++) {
640 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
641 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
642 encoder->private_->best_subframe_mid_side[i] = 0;
644 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
645 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
646 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
647 encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
648 encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
649 if(encoder->private_->loose_mid_side_stereo_frames == 0)
650 encoder->private_->loose_mid_side_stereo_frames = 1;
651 encoder->private_->loose_mid_side_stereo_frame_count = 0;
652 encoder->private_->current_sample_number = 0;
653 encoder->private_->current_frame_number = 0;
655 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
656 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? */
657 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
660 * get the CPU info and set the function pointers
662 FLAC__cpu_info(&encoder->private_->cpuinfo);
663 /* first default to the non-asm routines */
664 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
665 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
666 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
667 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
668 /* now override with asm where appropriate */
670 if(encoder->private_->cpuinfo.use_asm) {
671 #ifdef FLAC__CPU_IA32
672 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
673 #ifdef FLAC__HAS_NASM
674 if(0 && encoder->private_->cpuinfo.data.ia32.sse) {
675 if(encoder->protected_->max_lpc_order < 4)
676 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
677 else if(encoder->protected_->max_lpc_order < 8)
678 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
679 else if(encoder->protected_->max_lpc_order < 12)
680 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
682 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
684 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
685 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
687 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
688 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
689 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
690 if(encoder->private_->cpuinfo.data.ia32.mmx) {
691 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
692 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
695 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
696 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
702 /* finally override based on wide-ness if necessary */
703 if(encoder->private_->use_wide_by_block) {
704 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
707 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
708 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
710 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
711 /* the above function sets the state for us in case of an error */
712 return encoder->protected_->state;
715 if(!FLAC__bitbuffer_init(encoder->private_->frame))
716 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
719 * Set up the verify stuff if necessary
721 if(encoder->protected_->verify) {
723 * First, set up the fifo which will hold the
724 * original signal to compare against
726 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
727 for(i = 0; i < encoder->protected_->channels; i++) {
728 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
729 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
731 encoder->private_->verify.input_fifo.tail = 0;
734 * Now set up a stream decoder for verification
736 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
737 if(0 == encoder->private_->verify.decoder)
738 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
740 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
741 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
742 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
743 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
744 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
745 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
746 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
748 encoder->private_->verify.error_stats.absolute_sample = 0;
749 encoder->private_->verify.error_stats.frame_number = 0;
750 encoder->private_->verify.error_stats.channel = 0;
751 encoder->private_->verify.error_stats.sample = 0;
752 encoder->private_->verify.error_stats.expected = 0;
753 encoder->private_->verify.error_stats.got = 0;
756 * write the stream header
758 if(encoder->protected_->verify)
759 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
760 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
761 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
762 if(!write_bitbuffer_(encoder, 0)) {
763 /* the above function sets the state for us in case of an error */
764 return encoder->protected_->state;
768 * write the STREAMINFO metadata block
770 if(encoder->protected_->verify)
771 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
772 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
773 encoder->private_->metadata.is_last = (encoder->protected_->num_metadata_blocks == 0);
774 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
775 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
776 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
777 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
778 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
779 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
780 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
781 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
782 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
783 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
784 MD5Init(&encoder->private_->md5context);
785 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
786 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
789 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
790 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
791 if(!write_bitbuffer_(encoder, 0)) {
792 /* the above function sets the state for us in case of an error */
793 return encoder->protected_->state;
797 * Now that the STREAMINFO block is written, we can init this to an
798 * absurdly-high value...
800 encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
801 /* ... and clear this to 0 */
802 encoder->private_->metadata.data.stream_info.total_samples = 0;
805 * write the user's metadata blocks
807 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
808 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
809 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
810 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
813 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
814 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
815 if(!write_bitbuffer_(encoder, 0)) {
816 /* the above function sets the state for us in case of an error */
817 return encoder->protected_->state;
821 if(encoder->protected_->verify)
822 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
824 return encoder->protected_->state;
827 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
829 FLAC__ASSERT(0 != encoder);
831 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
834 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
835 if(encoder->private_->current_sample_number != 0) {
836 encoder->protected_->blocksize = encoder->private_->current_sample_number;
837 process_frame_(encoder, true); /* true => is last frame */
841 MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
843 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
844 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
847 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
848 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
851 set_defaults_(encoder);
853 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
856 FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
858 FLAC__ASSERT(0 != encoder);
859 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
861 encoder->protected_->verify = value;
865 FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
867 FLAC__ASSERT(0 != encoder);
868 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
870 encoder->protected_->streamable_subset = value;
874 FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
876 FLAC__ASSERT(0 != encoder);
877 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
879 encoder->protected_->do_mid_side_stereo = value;
883 FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
885 FLAC__ASSERT(0 != encoder);
886 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
888 encoder->protected_->loose_mid_side_stereo = value;
892 FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
894 FLAC__ASSERT(0 != encoder);
895 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
897 encoder->protected_->channels = value;
901 FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
903 FLAC__ASSERT(0 != encoder);
904 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
906 encoder->protected_->bits_per_sample = value;
910 FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
912 FLAC__ASSERT(0 != encoder);
913 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
915 encoder->protected_->sample_rate = value;
919 FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
921 FLAC__ASSERT(0 != encoder);
922 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
924 encoder->protected_->blocksize = value;
928 FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
930 FLAC__ASSERT(0 != encoder);
931 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
933 encoder->protected_->max_lpc_order = value;
937 FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
939 FLAC__ASSERT(0 != encoder);
940 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
942 encoder->protected_->qlp_coeff_precision = value;
946 FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
948 FLAC__ASSERT(0 != encoder);
949 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
951 encoder->protected_->do_qlp_coeff_prec_search = value;
955 FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
957 FLAC__ASSERT(0 != encoder);
958 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
962 encoder->protected_->do_escape_coding = value;
969 FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
971 FLAC__ASSERT(0 != encoder);
972 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
974 encoder->protected_->do_exhaustive_model_search = value;
978 FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
980 FLAC__ASSERT(0 != encoder);
981 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
983 encoder->protected_->min_residual_partition_order = value;
987 FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
989 FLAC__ASSERT(0 != encoder);
990 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
992 encoder->protected_->max_residual_partition_order = value;
996 FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
998 FLAC__ASSERT(0 != encoder);
999 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1002 /*@@@ deprecated: */
1003 encoder->protected_->rice_parameter_search_dist = value;
1010 FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1012 FLAC__ASSERT(0 != encoder);
1013 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1015 encoder->protected_->total_samples_estimate = value;
1019 FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1021 FLAC__ASSERT(0 != encoder);
1022 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1024 encoder->protected_->metadata = metadata;
1025 encoder->protected_->num_metadata_blocks = num_blocks;
1029 FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
1031 FLAC__ASSERT(0 != encoder);
1032 FLAC__ASSERT(0 != value);
1033 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1035 encoder->private_->write_callback = value;
1039 FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
1041 FLAC__ASSERT(0 != encoder);
1042 FLAC__ASSERT(0 != value);
1043 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1045 encoder->private_->metadata_callback = value;
1049 FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
1051 FLAC__ASSERT(0 != encoder);
1052 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1054 encoder->private_->client_data = value;
1058 FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1060 FLAC__ASSERT(0 != encoder);
1061 return encoder->protected_->state;
1064 FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1066 FLAC__ASSERT(0 != encoder);
1067 if(encoder->protected_->verify)
1068 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1070 return FLAC__STREAM_DECODER_UNINITIALIZED;
1073 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)
1075 FLAC__ASSERT(0 != encoder);
1076 if(0 != absolute_sample)
1077 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1078 if(0 != frame_number)
1079 *frame_number = encoder->private_->verify.error_stats.frame_number;
1081 *channel = encoder->private_->verify.error_stats.channel;
1083 *sample = encoder->private_->verify.error_stats.sample;
1085 *expected = encoder->private_->verify.error_stats.expected;
1087 *got = encoder->private_->verify.error_stats.got;
1090 FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1092 FLAC__ASSERT(0 != encoder);
1093 return encoder->protected_->verify;
1096 FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1098 FLAC__ASSERT(0 != encoder);
1099 return encoder->protected_->streamable_subset;
1102 FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1104 FLAC__ASSERT(0 != encoder);
1105 return encoder->protected_->do_mid_side_stereo;
1108 FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1110 FLAC__ASSERT(0 != encoder);
1111 return encoder->protected_->loose_mid_side_stereo;
1114 unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1116 FLAC__ASSERT(0 != encoder);
1117 return encoder->protected_->channels;
1120 unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1122 FLAC__ASSERT(0 != encoder);
1123 return encoder->protected_->bits_per_sample;
1126 unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1128 FLAC__ASSERT(0 != encoder);
1129 return encoder->protected_->sample_rate;
1132 unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1134 FLAC__ASSERT(0 != encoder);
1135 return encoder->protected_->blocksize;
1138 unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1140 FLAC__ASSERT(0 != encoder);
1141 return encoder->protected_->max_lpc_order;
1144 unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1146 FLAC__ASSERT(0 != encoder);
1147 return encoder->protected_->qlp_coeff_precision;
1150 FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1152 FLAC__ASSERT(0 != encoder);
1153 return encoder->protected_->do_qlp_coeff_prec_search;
1156 FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1158 FLAC__ASSERT(0 != encoder);
1159 return encoder->protected_->do_escape_coding;
1162 FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1164 FLAC__ASSERT(0 != encoder);
1165 return encoder->protected_->do_exhaustive_model_search;
1168 unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1170 FLAC__ASSERT(0 != encoder);
1171 return encoder->protected_->min_residual_partition_order;
1174 unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1176 FLAC__ASSERT(0 != encoder);
1177 return encoder->protected_->max_residual_partition_order;
1180 unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1182 FLAC__ASSERT(0 != encoder);
1183 return encoder->protected_->rice_parameter_search_dist;
1186 FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1188 FLAC__ASSERT(0 != encoder);
1189 return encoder->protected_->total_samples_estimate;
1192 FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1194 unsigned i, j, channel;
1195 FLAC__int32 x, mid, side;
1196 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1198 FLAC__ASSERT(0 != encoder);
1199 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1202 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1204 if(encoder->protected_->verify)
1205 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1207 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1208 x = mid = side = buffer[0][j];
1209 encoder->private_->integer_signal[0][i] = x;
1210 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1212 encoder->private_->integer_signal[1][i] = x;
1213 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1216 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1217 encoder->private_->integer_signal_mid_side[1][i] = side;
1218 encoder->private_->integer_signal_mid_side[0][i] = mid;
1219 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1220 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1221 encoder->private_->current_sample_number++;
1223 if(i == blocksize) {
1224 if(!process_frame_(encoder, false)) /* false => not last frame */
1227 } while(j < samples);
1231 if(encoder->protected_->verify)
1232 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1234 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1235 for(channel = 0; channel < channels; channel++) {
1236 x = buffer[channel][j];
1237 encoder->private_->integer_signal[channel][i] = x;
1238 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1240 encoder->private_->current_sample_number++;
1242 if(i == blocksize) {
1243 if(!process_frame_(encoder, false)) /* false => not last frame */
1246 } while(j < samples);
1252 FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1254 unsigned i, j, k, channel;
1255 FLAC__int32 x, mid, side;
1256 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1258 FLAC__ASSERT(0 != encoder);
1259 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1262 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1264 if(encoder->protected_->verify)
1265 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1267 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1268 x = mid = side = buffer[k++];
1269 encoder->private_->integer_signal[0][i] = x;
1270 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1272 encoder->private_->integer_signal[1][i] = x;
1273 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1276 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1277 encoder->private_->integer_signal_mid_side[1][i] = side;
1278 encoder->private_->integer_signal_mid_side[0][i] = mid;
1279 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1280 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1281 encoder->private_->current_sample_number++;
1283 if(i == blocksize) {
1284 if(!process_frame_(encoder, false)) /* false => not last frame */
1287 } while(j < samples);
1291 if(encoder->protected_->verify)
1292 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1294 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1295 for(channel = 0; channel < channels; channel++) {
1297 encoder->private_->integer_signal[channel][i] = x;
1298 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1300 encoder->private_->current_sample_number++;
1302 if(i == blocksize) {
1303 if(!process_frame_(encoder, false)) /* false => not last frame */
1306 } while(j < samples);
1312 /***********************************************************************
1314 * Private class methods
1316 ***********************************************************************/
1318 void set_defaults_(FLAC__StreamEncoder *encoder)
1320 FLAC__ASSERT(0 != encoder);
1322 encoder->protected_->verify = false;
1323 encoder->protected_->streamable_subset = true;
1324 encoder->protected_->do_mid_side_stereo = false;
1325 encoder->protected_->loose_mid_side_stereo = false;
1326 encoder->protected_->channels = 2;
1327 encoder->protected_->bits_per_sample = 16;
1328 encoder->protected_->sample_rate = 44100;
1329 encoder->protected_->blocksize = 1152;
1330 encoder->protected_->max_lpc_order = 0;
1331 encoder->protected_->qlp_coeff_precision = 0;
1332 encoder->protected_->do_qlp_coeff_prec_search = false;
1333 encoder->protected_->do_exhaustive_model_search = false;
1334 encoder->protected_->do_escape_coding = false;
1335 encoder->protected_->min_residual_partition_order = 0;
1336 encoder->protected_->max_residual_partition_order = 0;
1337 encoder->protected_->rice_parameter_search_dist = 0;
1338 encoder->protected_->total_samples_estimate = 0;
1339 encoder->protected_->metadata = 0;
1340 encoder->protected_->num_metadata_blocks = 0;
1342 encoder->private_->write_callback = 0;
1343 encoder->private_->metadata_callback = 0;
1344 encoder->private_->client_data = 0;
1347 void free_(FLAC__StreamEncoder *encoder)
1349 unsigned i, channel;
1351 FLAC__ASSERT(0 != encoder);
1352 for(i = 0; i < encoder->protected_->channels; i++) {
1353 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1354 free(encoder->private_->integer_signal_unaligned[i]);
1355 encoder->private_->integer_signal_unaligned[i] = 0;
1357 if(0 != encoder->private_->real_signal_unaligned[i]) {
1358 free(encoder->private_->real_signal_unaligned[i]);
1359 encoder->private_->real_signal_unaligned[i] = 0;
1362 for(i = 0; i < 2; i++) {
1363 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1364 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1365 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1367 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1368 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1369 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1372 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1373 for(i = 0; i < 2; i++) {
1374 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1375 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1376 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1380 for(channel = 0; channel < 2; channel++) {
1381 for(i = 0; i < 2; i++) {
1382 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1383 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1384 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1388 if(0 != encoder->private_->abs_residual_unaligned) {
1389 free(encoder->private_->abs_residual_unaligned);
1390 encoder->private_->abs_residual_unaligned = 0;
1392 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1393 free(encoder->private_->abs_residual_partition_sums_unaligned);
1394 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1396 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1397 free(encoder->private_->raw_bits_per_partition_unaligned);
1398 encoder->private_->raw_bits_per_partition_unaligned = 0;
1400 if(encoder->protected_->verify) {
1401 for(i = 0; i < encoder->protected_->channels; i++) {
1402 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1403 free(encoder->private_->verify.input_fifo.data[i]);
1404 encoder->private_->verify.input_fifo.data[i] = 0;
1408 FLAC__bitbuffer_free(encoder->private_->frame);
1411 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
1414 unsigned i, channel;
1416 FLAC__ASSERT(new_size > 0);
1417 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1418 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1420 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1421 if(new_size <= encoder->private_->input_capacity)
1426 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx() requires that the input arrays (in our case the integer signals) have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
1428 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1429 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1430 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1431 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1432 encoder->private_->integer_signal[i] += 4;
1434 for(i = 0; ok && i < 2; i++) {
1435 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]);
1436 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]);
1437 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1438 encoder->private_->integer_signal_mid_side[i] += 4;
1440 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1441 for(i = 0; ok && i < 2; i++) {
1442 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1445 for(channel = 0; ok && channel < 2; channel++) {
1446 for(i = 0; ok && i < 2; i++) {
1447 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]);
1450 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1451 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 */
1452 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1453 if(encoder->protected_->do_escape_coding)
1454 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1457 encoder->private_->input_capacity = new_size;
1459 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1464 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
1466 const FLAC__byte *buffer;
1469 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1471 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1473 if(encoder->protected_->verify) {
1474 encoder->private_->verify.output.data = buffer;
1475 encoder->private_->verify.output.bytes = bytes;
1476 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1477 encoder->private_->verify.needs_magic_hack = true;
1480 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1481 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1482 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1483 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1489 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1490 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1494 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1497 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1498 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1504 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1506 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1509 * Accumulate raw signal to the MD5 signature
1511 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)) {
1512 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1517 * Process the frame header and subframes into the frame bitbuffer
1519 if(!process_subframes_(encoder, is_last_frame)) {
1520 /* the above function sets the state for us in case of an error */
1525 * Zero-pad the frame to a byte_boundary
1527 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1528 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1533 * CRC-16 the whole thing
1535 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1536 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1541 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1542 /* the above function sets the state for us in case of an error */
1547 * Get ready for the next frame
1549 encoder->private_->current_sample_number = 0;
1550 encoder->private_->current_frame_number++;
1551 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1556 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1558 FLAC__FrameHeader frame_header;
1559 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1560 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1563 * Calculate the min,max Rice partition orders
1566 max_partition_order = 0;
1569 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1570 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
1572 min_partition_order = min(min_partition_order, max_partition_order);
1574 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1579 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1580 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1583 frame_header.blocksize = encoder->protected_->blocksize;
1584 frame_header.sample_rate = encoder->protected_->sample_rate;
1585 frame_header.channels = encoder->protected_->channels;
1586 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1587 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1588 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1589 frame_header.number.frame_number = encoder->private_->current_frame_number;
1592 * Figure out what channel assignments to try
1594 if(encoder->protected_->do_mid_side_stereo) {
1595 if(encoder->protected_->loose_mid_side_stereo) {
1596 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1597 do_independent = true;
1601 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1602 do_mid_side = !do_independent;
1606 do_independent = true;
1611 do_independent = true;
1612 do_mid_side = false;
1615 FLAC__ASSERT(do_independent || do_mid_side);
1618 * Check for wasted bits; set effective bps for each subframe
1620 if(do_independent) {
1621 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1622 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1623 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1624 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1628 FLAC__ASSERT(encoder->protected_->channels == 2);
1629 for(channel = 0; channel < 2; channel++) {
1630 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1631 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1632 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1637 * First do a normal encoding pass of each independent channel
1639 if(do_independent) {
1640 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1644 min_partition_order,
1645 max_partition_order,
1646 precompute_partition_sums,
1649 encoder->private_->subframe_bps[channel],
1650 encoder->private_->integer_signal[channel],
1651 encoder->private_->real_signal[channel],
1652 encoder->private_->subframe_workspace_ptr[channel],
1653 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1654 encoder->private_->residual_workspace[channel],
1655 encoder->private_->best_subframe+channel,
1656 encoder->private_->best_subframe_bits+channel
1664 * Now do mid and side channels if requested
1667 FLAC__ASSERT(encoder->protected_->channels == 2);
1669 for(channel = 0; channel < 2; channel++) {
1673 min_partition_order,
1674 max_partition_order,
1675 precompute_partition_sums,
1678 encoder->private_->subframe_bps_mid_side[channel],
1679 encoder->private_->integer_signal_mid_side[channel],
1680 encoder->private_->real_signal_mid_side[channel],
1681 encoder->private_->subframe_workspace_ptr_mid_side[channel],
1682 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
1683 encoder->private_->residual_workspace_mid_side[channel],
1684 encoder->private_->best_subframe_mid_side+channel,
1685 encoder->private_->best_subframe_bits_mid_side+channel
1693 * Compose the frame bitbuffer
1696 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1697 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1698 FLAC__ChannelAssignment channel_assignment;
1700 FLAC__ASSERT(encoder->protected_->channels == 2);
1702 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1703 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1706 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1708 FLAC__ChannelAssignment ca;
1710 FLAC__ASSERT(do_independent && do_mid_side);
1712 /* We have to figure out which channel assignent results in the smallest frame */
1713 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
1714 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
1715 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
1716 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
1718 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
1719 if(bits[ca] < min_bits) {
1720 min_bits = bits[ca];
1721 channel_assignment = ca;
1726 frame_header.channel_assignment = channel_assignment;
1728 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1729 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1733 switch(channel_assignment) {
1734 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1735 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1736 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1738 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1739 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1740 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1742 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1743 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1744 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1746 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1747 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1748 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1754 switch(channel_assignment) {
1755 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1756 left_bps = encoder->private_->subframe_bps [0];
1757 right_bps = encoder->private_->subframe_bps [1];
1759 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1760 left_bps = encoder->private_->subframe_bps [0];
1761 right_bps = encoder->private_->subframe_bps_mid_side[1];
1763 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1764 left_bps = encoder->private_->subframe_bps_mid_side[1];
1765 right_bps = encoder->private_->subframe_bps [1];
1767 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1768 left_bps = encoder->private_->subframe_bps_mid_side[0];
1769 right_bps = encoder->private_->subframe_bps_mid_side[1];
1775 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1776 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
1778 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1782 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1783 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1787 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1788 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
1789 /* the above function sets the state for us in case of an error */
1795 if(encoder->protected_->loose_mid_side_stereo) {
1796 encoder->private_->loose_mid_side_stereo_frame_count++;
1797 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1798 encoder->private_->loose_mid_side_stereo_frame_count = 0;
1801 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1806 FLAC__bool process_subframe_(
1807 FLAC__StreamEncoder *encoder,
1808 unsigned min_partition_order,
1809 unsigned max_partition_order,
1810 FLAC__bool precompute_partition_sums,
1811 FLAC__bool verbatim_only,
1812 const FLAC__FrameHeader *frame_header,
1813 unsigned subframe_bps,
1814 const FLAC__int32 integer_signal[],
1815 const FLAC__real real_signal[],
1816 FLAC__Subframe *subframe[2],
1817 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
1818 FLAC__int32 *residual[2],
1819 unsigned *best_subframe,
1823 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1824 FLAC__real lpc_residual_bits_per_sample;
1825 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 */
1826 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1827 unsigned min_lpc_order, max_lpc_order, lpc_order;
1828 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1829 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1830 unsigned rice_parameter;
1831 unsigned _candidate_bits, _best_bits;
1832 unsigned _best_subframe;
1834 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1836 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1838 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1839 /* check for constant subframe */
1840 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);
1841 if(fixed_residual_bits_per_sample[1] == 0.0) {
1842 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1843 unsigned i, signal_is_constant = true;
1844 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1845 if(integer_signal[0] != integer_signal[i]) {
1846 signal_is_constant = false;
1850 if(signal_is_constant) {
1851 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1852 if(_candidate_bits < _best_bits) {
1853 _best_subframe = !_best_subframe;
1854 _best_bits = _candidate_bits;
1860 if(encoder->protected_->do_exhaustive_model_search) {
1861 min_fixed_order = 0;
1862 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1865 min_fixed_order = max_fixed_order = guess_fixed_order;
1867 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1868 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1869 continue; /* don't even try */
1870 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 */
1871 #ifndef FLAC__SYMMETRIC_RICE
1872 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1874 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1875 #ifdef DEBUG_VERBOSE
1876 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1878 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1881 evaluate_fixed_subframe_(
1884 residual[!_best_subframe],
1885 encoder->private_->abs_residual,
1886 encoder->private_->abs_residual_partition_sums,
1887 encoder->private_->raw_bits_per_partition,
1888 frame_header->blocksize,
1892 min_partition_order,
1893 max_partition_order,
1894 precompute_partition_sums,
1895 encoder->protected_->do_escape_coding,
1896 encoder->protected_->rice_parameter_search_dist,
1897 subframe[!_best_subframe],
1898 partitioned_rice_contents[!_best_subframe]
1900 if(_candidate_bits < _best_bits) {
1901 _best_subframe = !_best_subframe;
1902 _best_bits = _candidate_bits;
1907 if(encoder->protected_->max_lpc_order > 0) {
1908 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
1909 max_lpc_order = frame_header->blocksize-1;
1911 max_lpc_order = encoder->protected_->max_lpc_order;
1912 if(max_lpc_order > 0) {
1913 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
1914 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
1915 if(autoc[0] != 0.0) {
1916 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
1917 if(encoder->protected_->do_exhaustive_model_search) {
1921 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
1922 min_lpc_order = max_lpc_order = guess_lpc_order;
1924 if(encoder->protected_->do_qlp_coeff_prec_search) {
1925 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
1926 max_qlp_coeff_precision = min(8*sizeof(FLAC__int32) - subframe_bps - 1 - 2, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1); /* -2 to keep things 32-bit safe */
1929 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
1931 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
1932 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
1933 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
1934 continue; /* don't even try */
1935 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
1936 #ifndef FLAC__SYMMETRIC_RICE
1937 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1939 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1940 #ifdef DEBUG_VERBOSE
1941 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1943 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1945 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
1947 evaluate_lpc_subframe_(
1950 residual[!_best_subframe],
1951 encoder->private_->abs_residual,
1952 encoder->private_->abs_residual_partition_sums,
1953 encoder->private_->raw_bits_per_partition,
1954 encoder->private_->lp_coeff[lpc_order-1],
1955 frame_header->blocksize,
1958 qlp_coeff_precision,
1960 min_partition_order,
1961 max_partition_order,
1962 precompute_partition_sums,
1963 encoder->protected_->do_escape_coding,
1964 encoder->protected_->rice_parameter_search_dist,
1965 subframe[!_best_subframe],
1966 partitioned_rice_contents[!_best_subframe]
1968 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1969 if(_candidate_bits < _best_bits) {
1970 _best_subframe = !_best_subframe;
1971 _best_bits = _candidate_bits;
1982 *best_subframe = _best_subframe;
1983 *best_bits = _best_bits;
1988 FLAC__bool add_subframe_(
1989 FLAC__StreamEncoder *encoder,
1990 const FLAC__FrameHeader *frame_header,
1991 unsigned subframe_bps,
1992 const FLAC__Subframe *subframe,
1993 FLAC__BitBuffer *frame
1996 switch(subframe->type) {
1997 case FLAC__SUBFRAME_TYPE_CONSTANT:
1998 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
1999 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2003 case FLAC__SUBFRAME_TYPE_FIXED:
2004 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2005 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2009 case FLAC__SUBFRAME_TYPE_LPC:
2010 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2011 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2015 case FLAC__SUBFRAME_TYPE_VERBATIM:
2016 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2017 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2028 unsigned evaluate_constant_subframe_(
2029 const FLAC__int32 signal,
2030 unsigned subframe_bps,
2031 FLAC__Subframe *subframe
2034 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2035 subframe->data.constant.value = signal;
2037 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
2040 unsigned evaluate_fixed_subframe_(
2041 FLAC__StreamEncoder *encoder,
2042 const FLAC__int32 signal[],
2043 FLAC__int32 residual[],
2044 FLAC__uint32 abs_residual[],
2045 FLAC__uint64 abs_residual_partition_sums[],
2046 unsigned raw_bits_per_partition[],
2048 unsigned subframe_bps,
2050 unsigned rice_parameter,
2051 unsigned min_partition_order,
2052 unsigned max_partition_order,
2053 FLAC__bool precompute_partition_sums,
2054 FLAC__bool do_escape_coding,
2055 unsigned rice_parameter_search_dist,
2056 FLAC__Subframe *subframe,
2057 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2060 unsigned i, residual_bits;
2061 const unsigned residual_samples = blocksize - order;
2063 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2065 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2067 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2068 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2069 subframe->data.fixed.residual = residual;
2072 find_best_partition_order_(
2076 abs_residual_partition_sums,
2077 raw_bits_per_partition,
2081 min_partition_order,
2082 max_partition_order,
2083 precompute_partition_sums,
2085 rice_parameter_search_dist,
2086 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2089 subframe->data.fixed.order = order;
2090 for(i = 0; i < order; i++)
2091 subframe->data.fixed.warmup[i] = signal[i];
2093 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
2096 unsigned evaluate_lpc_subframe_(
2097 FLAC__StreamEncoder *encoder,
2098 const FLAC__int32 signal[],
2099 FLAC__int32 residual[],
2100 FLAC__uint32 abs_residual[],
2101 FLAC__uint64 abs_residual_partition_sums[],
2102 unsigned raw_bits_per_partition[],
2103 const FLAC__real lp_coeff[],
2105 unsigned subframe_bps,
2107 unsigned qlp_coeff_precision,
2108 unsigned rice_parameter,
2109 unsigned min_partition_order,
2110 unsigned max_partition_order,
2111 FLAC__bool precompute_partition_sums,
2112 FLAC__bool do_escape_coding,
2113 unsigned rice_parameter_search_dist,
2114 FLAC__Subframe *subframe,
2115 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2118 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
2119 unsigned i, residual_bits;
2120 int quantization, ret;
2121 const unsigned residual_samples = blocksize - order;
2123 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
2125 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2127 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2128 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2130 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2132 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2134 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2135 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2136 subframe->data.lpc.residual = residual;
2139 find_best_partition_order_(
2143 abs_residual_partition_sums,
2144 raw_bits_per_partition,
2148 min_partition_order,
2149 max_partition_order,
2150 precompute_partition_sums,
2152 rice_parameter_search_dist,
2153 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2156 subframe->data.lpc.order = order;
2157 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2158 subframe->data.lpc.quantization_level = quantization;
2159 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
2160 for(i = 0; i < order; i++)
2161 subframe->data.lpc.warmup[i] = signal[i];
2163 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;
2166 unsigned evaluate_verbatim_subframe_(
2167 const FLAC__int32 signal[],
2169 unsigned subframe_bps,
2170 FLAC__Subframe *subframe
2173 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2175 subframe->data.verbatim.data = signal;
2177 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
2180 unsigned find_best_partition_order_(
2181 FLAC__StreamEncoderPrivate *private_,
2182 const FLAC__int32 residual[],
2183 FLAC__uint32 abs_residual[],
2184 FLAC__uint64 abs_residual_partition_sums[],
2185 unsigned raw_bits_per_partition[],
2186 unsigned residual_samples,
2187 unsigned predictor_order,
2188 unsigned rice_parameter,
2189 unsigned min_partition_order,
2190 unsigned max_partition_order,
2191 FLAC__bool precompute_partition_sums,
2192 FLAC__bool do_escape_coding,
2193 unsigned rice_parameter_search_dist,
2194 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2198 unsigned residual_bits, best_residual_bits = 0;
2199 unsigned residual_sample;
2200 unsigned best_parameters_index = 0;
2201 const unsigned blocksize = residual_samples + predictor_order;
2203 /* compute abs(residual) for use later */
2204 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2205 r = residual[residual_sample];
2206 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2209 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2210 min_partition_order = min(min_partition_order, max_partition_order);
2212 if(precompute_partition_sums) {
2213 int partition_order;
2216 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2218 if(do_escape_coding)
2219 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2221 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2222 #ifdef DONT_ESTIMATE_RICE_BITS
2224 set_partitioned_rice_with_precompute_(
2226 abs_residual_partition_sums+sum,
2227 raw_bits_per_partition+sum,
2231 rice_parameter_search_dist,
2232 (unsigned)partition_order,
2234 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2240 set_partitioned_rice_with_precompute_(
2242 abs_residual_partition_sums+sum,
2243 raw_bits_per_partition+sum,
2247 rice_parameter_search_dist,
2248 (unsigned)partition_order,
2250 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2256 FLAC__ASSERT(best_residual_bits != 0);
2259 sum += 1u << partition_order;
2260 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2261 best_residual_bits = residual_bits;
2262 best_parameters_index = !best_parameters_index;
2263 best_partitioned_rice->order = partition_order;
2268 unsigned partition_order;
2269 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2270 #ifdef DONT_ESTIMATE_RICE_BITS
2272 set_partitioned_rice_(
2278 rice_parameter_search_dist,
2280 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2286 set_partitioned_rice_(
2291 rice_parameter_search_dist,
2293 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2299 FLAC__ASSERT(best_residual_bits != 0);
2302 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2303 best_residual_bits = residual_bits;
2304 best_parameters_index = !best_parameters_index;
2305 best_partitioned_rice->order = partition_order;
2311 * We are allowed to de-const the pointer based on our special knowledhe;
2312 * it is const to the outside world.
2315 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2316 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2317 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2318 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2321 return best_residual_bits;
2324 void precompute_partition_info_sums_(
2325 const FLAC__uint32 abs_residual[],
2326 FLAC__uint64 abs_residual_partition_sums[],
2327 unsigned residual_samples,
2328 unsigned predictor_order,
2329 unsigned min_partition_order,
2330 unsigned max_partition_order
2333 int partition_order;
2334 unsigned from_partition, to_partition = 0;
2335 const unsigned blocksize = residual_samples + predictor_order;
2337 /* first do max_partition_order */
2338 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2339 FLAC__uint64 abs_residual_partition_sum;
2341 unsigned partition, partition_sample, partition_samples, residual_sample;
2342 const unsigned partitions = 1u << partition_order;
2343 const unsigned default_partition_samples = blocksize >> partition_order;
2345 FLAC__ASSERT(default_partition_samples > predictor_order);
2347 for(partition = residual_sample = 0; partition < partitions; partition++) {
2348 partition_samples = default_partition_samples;
2350 partition_samples -= predictor_order;
2351 abs_residual_partition_sum = 0;
2352 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2353 abs_r = abs_residual[residual_sample];
2354 abs_residual_partition_sum += abs_r;
2357 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2359 to_partition = partitions;
2363 /* now merge partitions for lower orders */
2364 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2367 const unsigned partitions = 1u << partition_order;
2368 for(i = 0; i < partitions; i++) {
2369 s = abs_residual_partition_sums[from_partition];
2371 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
2378 void precompute_partition_info_escapes_(
2379 const FLAC__int32 residual[],
2380 unsigned raw_bits_per_partition[],
2381 unsigned residual_samples,
2382 unsigned predictor_order,
2383 unsigned min_partition_order,
2384 unsigned max_partition_order
2387 int partition_order;
2388 unsigned from_partition, to_partition = 0;
2389 const unsigned blocksize = residual_samples + predictor_order;
2391 /* first do max_partition_order */
2392 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2393 FLAC__int32 r, residual_partition_min, residual_partition_max;
2394 unsigned silog2_min, silog2_max;
2395 unsigned partition, partition_sample, partition_samples, residual_sample;
2396 const unsigned partitions = 1u << partition_order;
2397 const unsigned default_partition_samples = blocksize >> partition_order;
2399 FLAC__ASSERT(default_partition_samples > predictor_order);
2401 for(partition = residual_sample = 0; partition < partitions; partition++) {
2402 partition_samples = default_partition_samples;
2404 partition_samples -= predictor_order;
2405 residual_partition_min = residual_partition_max = 0;
2406 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2407 r = residual[residual_sample];
2408 if(r < residual_partition_min)
2409 residual_partition_min = r;
2410 else if(r > residual_partition_max)
2411 residual_partition_max = r;
2414 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2415 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2416 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
2418 to_partition = partitions;
2422 /* now merge partitions for lower orders */
2423 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2426 const unsigned partitions = 1u << partition_order;
2427 for(i = 0; i < partitions; i++) {
2428 m = raw_bits_per_partition[from_partition];
2430 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2437 #ifdef VARIABLE_RICE_BITS
2438 #undef VARIABLE_RICE_BITS
2440 #ifndef DONT_ESTIMATE_RICE_BITS
2441 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
2444 #ifdef DONT_ESTIMATE_RICE_BITS
2445 FLAC__bool set_partitioned_rice_(
2446 const FLAC__uint32 abs_residual[],
2447 const FLAC__int32 residual[],
2448 const unsigned residual_samples,
2449 const unsigned predictor_order,
2450 const unsigned suggested_rice_parameter,
2451 const unsigned rice_parameter_search_dist,
2452 const unsigned partition_order,
2453 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2457 FLAC__bool set_partitioned_rice_(
2458 const FLAC__uint32 abs_residual[],
2459 const unsigned residual_samples,
2460 const unsigned predictor_order,
2461 const unsigned suggested_rice_parameter,
2462 const unsigned rice_parameter_search_dist,
2463 const unsigned partition_order,
2464 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2469 unsigned rice_parameter, partition_bits;
2470 #ifndef NO_RICE_SEARCH
2471 unsigned best_partition_bits;
2472 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2474 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2475 unsigned *parameters;
2477 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2479 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2480 parameters = partitioned_rice_contents->parameters;
2482 if(partition_order == 0) {
2485 #ifndef NO_RICE_SEARCH
2486 if(rice_parameter_search_dist) {
2487 if(suggested_rice_parameter < rice_parameter_search_dist)
2488 min_rice_parameter = 0;
2490 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2491 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2492 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2493 #ifdef DEBUG_VERBOSE
2494 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2496 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2500 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2502 best_partition_bits = 0xffffffff;
2503 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2505 #ifdef VARIABLE_RICE_BITS
2506 #ifdef FLAC__SYMMETRIC_RICE
2507 partition_bits = (2+rice_parameter) * residual_samples;
2509 const unsigned rice_parameter_estimate = rice_parameter-1;
2510 partition_bits = (1+rice_parameter) * residual_samples;
2515 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2516 for(i = 0; i < residual_samples; i++) {
2517 #ifdef VARIABLE_RICE_BITS
2518 #ifdef FLAC__SYMMETRIC_RICE
2519 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2521 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2524 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2527 #ifndef NO_RICE_SEARCH
2528 if(partition_bits < best_partition_bits) {
2529 best_rice_parameter = rice_parameter;
2530 best_partition_bits = partition_bits;
2534 parameters[0] = best_rice_parameter;
2535 bits_ += best_partition_bits;
2538 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2539 unsigned partition_samples;
2540 FLAC__uint64 mean, k;
2541 const unsigned partitions = 1u << partition_order;
2542 for(partition = residual_sample = 0; partition < partitions; partition++) {
2543 partition_samples = (residual_samples+predictor_order) >> partition_order;
2544 if(partition == 0) {
2545 if(partition_samples <= predictor_order)
2548 partition_samples -= predictor_order;
2551 save_residual_sample = residual_sample;
2552 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2553 mean += abs_residual[residual_sample];
2554 residual_sample = save_residual_sample;
2555 #ifdef FLAC__SYMMETRIC_RICE
2556 mean += partition_samples >> 1; /* for rounding effect */
2557 mean /= partition_samples;
2559 /* calc rice_parameter = floor(log2(mean)) */
2567 /* calc rice_parameter ala LOCO-I */
2568 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2571 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2572 #ifdef DEBUG_VERBOSE
2573 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2575 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2578 #ifndef NO_RICE_SEARCH
2579 if(rice_parameter_search_dist) {
2580 if(rice_parameter < rice_parameter_search_dist)
2581 min_rice_parameter = 0;
2583 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2584 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2585 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2586 #ifdef DEBUG_VERBOSE
2587 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2589 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2593 min_rice_parameter = max_rice_parameter = rice_parameter;
2595 best_partition_bits = 0xffffffff;
2596 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2598 #ifdef VARIABLE_RICE_BITS
2599 #ifdef FLAC__SYMMETRIC_RICE
2600 partition_bits = (2+rice_parameter) * partition_samples;
2602 const unsigned rice_parameter_estimate = rice_parameter-1;
2603 partition_bits = (1+rice_parameter) * partition_samples;
2608 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2609 save_residual_sample = residual_sample;
2610 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2611 #ifdef VARIABLE_RICE_BITS
2612 #ifdef FLAC__SYMMETRIC_RICE
2613 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2615 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2618 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2621 #ifndef NO_RICE_SEARCH
2622 if(rice_parameter != max_rice_parameter)
2623 residual_sample = save_residual_sample;
2624 if(partition_bits < best_partition_bits) {
2625 best_rice_parameter = rice_parameter;
2626 best_partition_bits = partition_bits;
2630 parameters[partition] = best_rice_parameter;
2631 bits_ += best_partition_bits;
2639 #ifdef DONT_ESTIMATE_RICE_BITS
2640 FLAC__bool set_partitioned_rice_with_precompute_(
2641 const FLAC__int32 residual[],
2642 const FLAC__uint64 abs_residual_partition_sums[],
2643 const unsigned raw_bits_per_partition[],
2644 const unsigned residual_samples,
2645 const unsigned predictor_order,
2646 const unsigned suggested_rice_parameter,
2647 const unsigned rice_parameter_search_dist,
2648 const unsigned partition_order,
2649 const FLAC__bool search_for_escapes,
2650 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2654 FLAC__bool set_partitioned_rice_with_precompute_(
2655 const FLAC__uint32 abs_residual[],
2656 const FLAC__uint64 abs_residual_partition_sums[],
2657 const unsigned raw_bits_per_partition[],
2658 const unsigned residual_samples,
2659 const unsigned predictor_order,
2660 const unsigned suggested_rice_parameter,
2661 const unsigned rice_parameter_search_dist,
2662 const unsigned partition_order,
2663 const FLAC__bool search_for_escapes,
2664 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2669 unsigned rice_parameter, partition_bits;
2670 #ifndef NO_RICE_SEARCH
2671 unsigned best_partition_bits;
2672 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2675 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2676 unsigned *parameters, *raw_bits;
2678 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2680 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2681 parameters = partitioned_rice_contents->parameters;
2682 raw_bits = partitioned_rice_contents->raw_bits;
2684 if(partition_order == 0) {
2687 #ifndef NO_RICE_SEARCH
2688 if(rice_parameter_search_dist) {
2689 if(suggested_rice_parameter < rice_parameter_search_dist)
2690 min_rice_parameter = 0;
2692 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2693 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2694 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2695 #ifdef DEBUG_VERBOSE
2696 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2698 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2702 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2704 best_partition_bits = 0xffffffff;
2705 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2707 #ifdef VARIABLE_RICE_BITS
2708 #ifdef FLAC__SYMMETRIC_RICE
2709 partition_bits = (2+rice_parameter) * residual_samples;
2711 const unsigned rice_parameter_estimate = rice_parameter-1;
2712 partition_bits = (1+rice_parameter) * residual_samples;
2717 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2718 for(i = 0; i < residual_samples; i++) {
2719 #ifdef VARIABLE_RICE_BITS
2720 #ifdef FLAC__SYMMETRIC_RICE
2721 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2723 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2726 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2729 #ifndef NO_RICE_SEARCH
2730 if(partition_bits < best_partition_bits) {
2731 best_rice_parameter = rice_parameter;
2732 best_partition_bits = partition_bits;
2736 if(search_for_escapes) {
2737 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;
2738 if(flat_bits <= best_partition_bits) {
2739 raw_bits[0] = raw_bits_per_partition[0];
2740 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2741 best_partition_bits = flat_bits;
2744 parameters[0] = best_rice_parameter;
2745 bits_ += best_partition_bits;
2748 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2749 unsigned partition_samples;
2750 FLAC__uint64 mean, k;
2751 const unsigned partitions = 1u << partition_order;
2752 for(partition = residual_sample = 0; partition < partitions; partition++) {
2753 partition_samples = (residual_samples+predictor_order) >> partition_order;
2754 if(partition == 0) {
2755 if(partition_samples <= predictor_order)
2758 partition_samples -= predictor_order;
2760 mean = abs_residual_partition_sums[partition];
2761 #ifdef FLAC__SYMMETRIC_RICE
2762 mean += partition_samples >> 1; /* for rounding effect */
2763 mean /= partition_samples;
2765 /* calc rice_parameter = floor(log2(mean)) */
2773 /* calc rice_parameter ala LOCO-I */
2774 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2777 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2778 #ifdef DEBUG_VERBOSE
2779 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2781 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2784 #ifndef NO_RICE_SEARCH
2785 if(rice_parameter_search_dist) {
2786 if(rice_parameter < rice_parameter_search_dist)
2787 min_rice_parameter = 0;
2789 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2790 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2791 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2792 #ifdef DEBUG_VERBOSE
2793 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2795 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2799 min_rice_parameter = max_rice_parameter = rice_parameter;
2801 best_partition_bits = 0xffffffff;
2802 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2804 #ifdef VARIABLE_RICE_BITS
2805 #ifdef FLAC__SYMMETRIC_RICE
2806 partition_bits = (2+rice_parameter) * partition_samples;
2808 const unsigned rice_parameter_estimate = rice_parameter-1;
2809 partition_bits = (1+rice_parameter) * partition_samples;
2814 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2815 save_residual_sample = residual_sample;
2816 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2817 #ifdef VARIABLE_RICE_BITS
2818 #ifdef FLAC__SYMMETRIC_RICE
2819 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2821 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2824 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2827 #ifndef NO_RICE_SEARCH
2828 if(rice_parameter != max_rice_parameter)
2829 residual_sample = save_residual_sample;
2830 if(partition_bits < best_partition_bits) {
2831 best_rice_parameter = rice_parameter;
2832 best_partition_bits = partition_bits;
2836 if(search_for_escapes) {
2837 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;
2838 if(flat_bits <= best_partition_bits) {
2839 raw_bits[partition] = raw_bits_per_partition[partition];
2840 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2841 best_partition_bits = flat_bits;
2844 parameters[partition] = best_rice_parameter;
2845 bits_ += best_partition_bits;
2853 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
2858 for(i = 0; i < samples && !(x&1); i++)
2865 for(shift = 0; !(x&1); shift++)
2870 for(i = 0; i < samples; i++)
2871 signal[i] >>= shift;
2877 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
2881 for(channel = 0; channel < channels; channel++)
2882 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
2884 fifo->tail += wide_samples;
2886 FLAC__ASSERT(fifo->tail <= fifo->size);
2889 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
2892 unsigned sample, wide_sample;
2893 unsigned tail = fifo->tail;
2895 sample = input_offset * channels;
2896 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
2897 for(channel = 0; channel < channels; channel++)
2898 fifo->data[channel][tail] = input[sample++];
2903 FLAC__ASSERT(fifo->tail <= fifo->size);
2906 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
2908 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
2909 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
2912 if(encoder->private_->verify.needs_magic_hack) {
2913 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
2914 *bytes = FLAC__STREAM_SYNC_LENGTH;
2915 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
2916 encoder->private_->verify.needs_magic_hack = false;
2919 if(encoded_bytes == 0) {
2921 * If we get here, a FIFO underflow has occurred,
2922 * which means there is a bug somewhere.
2925 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2927 else if(encoded_bytes < *bytes)
2928 *bytes = encoded_bytes;
2929 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
2930 encoder->private_->verify.output.data += *bytes;
2931 encoder->private_->verify.output.bytes -= *bytes;
2934 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2937 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
2939 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
2941 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
2942 const unsigned blocksize = frame->header.blocksize;
2943 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
2945 for(channel = 0; channel < channels; channel++) {
2946 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
2947 unsigned i, sample = 0;
2948 FLAC__int32 expect = 0, got = 0;
2950 for(i = 0; i < blocksize; i++) {
2951 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
2953 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
2954 got = (FLAC__int32)buffer[channel][i];
2958 FLAC__ASSERT(i < blocksize);
2959 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2960 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
2961 encoder->private_->verify.error_stats.frame_number = frame->header.number.sample_number / blocksize;
2962 encoder->private_->verify.error_stats.channel = channel;
2963 encoder->private_->verify.error_stats.sample = sample;
2964 encoder->private_->verify.error_stats.expected = expect;
2965 encoder->private_->verify.error_stats.got = got;
2966 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
2967 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
2970 /* dequeue the frame from the fifo */
2971 for(channel = 0; channel < channels; channel++) {
2972 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
2974 encoder->private_->verify.input_fifo.tail -= blocksize;
2975 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
2978 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
2980 (void)decoder, (void)metadata, (void)client_data;
2983 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
2985 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
2986 (void)decoder, (void)status;
2987 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;