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);
77 static FLAC__bool process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
78 static FLAC__bool add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
79 static unsigned evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
80 static unsigned evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice);
81 static unsigned evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice);
82 static unsigned evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
83 static unsigned find_best_partition_order_(struct FLAC__StreamEncoderPrivate *private_, const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice);
84 static void precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
85 static void precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
86 #ifdef DONT_ESTIMATE_RICE_BITS
87 static FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits);
88 static FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits);
90 static FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits);
91 static FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits);
93 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
94 /* verify-related routines: */
95 static void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples);
96 static void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples);
97 static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
98 static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
99 static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
100 static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
102 /***********************************************************************
106 ***********************************************************************/
108 typedef struct FLAC__StreamEncoderPrivate {
109 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
110 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
111 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
112 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
113 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
114 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
115 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
116 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
117 FLAC__int32 *residual_workspace_mid_side[2][2];
118 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
119 FLAC__Subframe subframe_workspace_mid_side[2][2];
120 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
121 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
122 FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice_workspace[FLAC__MAX_CHANNELS][2];
123 FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice_workspace_mid_side[FLAC__MAX_CHANNELS][2];
124 FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice_workspace_ptr[FLAC__MAX_CHANNELS][2];
125 FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
126 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
127 unsigned best_subframe_mid_side[2];
128 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
129 unsigned best_subframe_bits_mid_side[2];
130 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
131 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
132 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
133 FLAC__BitBuffer *frame; /* the current frame being worked on */
134 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
135 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
136 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
137 FLAC__ChannelAssignment last_channel_assignment;
138 FLAC__StreamMetadata metadata;
139 unsigned current_sample_number;
140 unsigned current_frame_number;
141 struct MD5Context md5context;
142 FLAC__CPUInfo cpuinfo;
143 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
144 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
145 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[]);
146 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[]);
147 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
148 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
149 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
150 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
151 FLAC__StreamEncoderWriteCallback write_callback;
152 FLAC__StreamEncoderMetadataCallback metadata_callback;
154 /* unaligned (original) pointers to allocated data */
155 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
156 FLAC__int32 *integer_signal_mid_side_unaligned[2];
157 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
158 FLAC__real *real_signal_mid_side_unaligned[2];
159 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
160 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
161 FLAC__uint32 *abs_residual_unaligned;
162 FLAC__uint64 *abs_residual_partition_sums_unaligned;
163 unsigned *raw_bits_per_partition_unaligned;
165 * These fields have been moved here from private function local
166 * declarations merely to save stack space during encoding.
168 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
169 FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice_extra[2]; /* from find_best_partition_order_() */
171 * The data for the verify section
174 FLAC__StreamDecoder *decoder;
175 EncoderStateHint state_hint;
176 FLAC__bool needs_magic_hack;
177 verify_input_fifo input_fifo;
178 verify_output output;
180 FLAC__uint64 absolute_sample;
181 unsigned frame_number;
184 FLAC__int32 expected;
188 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
189 } FLAC__StreamEncoderPrivate;
191 /***********************************************************************
193 * Public static class data
195 ***********************************************************************/
197 const char * const FLAC__StreamEncoderStateString[] = {
198 "FLAC__STREAM_ENCODER_OK",
199 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
200 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
201 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
202 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
203 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
204 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
205 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
206 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
207 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
208 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
209 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
210 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
211 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
212 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
213 "FLAC__STREAM_ENCODER_INVALID_METADATA",
214 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
215 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
216 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
217 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
218 "FLAC__STREAM_ENCODER_UNINITIALIZED"
221 const char * const FLAC__StreamEncoderWriteStatusString[] = {
222 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
223 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
226 /***********************************************************************
228 * Class constructor/destructor
231 FLAC__StreamEncoder *FLAC__stream_encoder_new()
233 FLAC__StreamEncoder *encoder;
236 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
238 encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
242 memset(encoder, 0, sizeof(FLAC__StreamEncoder));
244 encoder->protected_ = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
245 if(encoder->protected_ == 0) {
249 memset(encoder->protected_, 0, sizeof(FLAC__StreamEncoderProtected));
251 encoder->private_ = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
252 if(encoder->private_ == 0) {
253 free(encoder->protected_);
257 memset(encoder->private_, 0, sizeof(FLAC__StreamEncoderPrivate));
259 encoder->private_->frame = FLAC__bitbuffer_new();
260 if(encoder->private_->frame == 0) {
261 free(encoder->private_);
262 free(encoder->protected_);
267 set_defaults_(encoder);
269 encoder->private_->is_being_deleted = false;
271 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
272 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
273 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
275 for(i = 0; i < 2; i++) {
276 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
277 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
279 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
280 encoder->private_->partitioned_rice_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_workspace[i][0];
281 encoder->private_->partitioned_rice_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_workspace[i][1];
283 for(i = 0; i < 2; i++) {
284 encoder->private_->partitioned_rice_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_workspace_mid_side[i][0];
285 encoder->private_->partitioned_rice_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_workspace_mid_side[i][1];
288 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
289 FLAC__format_entropy_coding_method_partitioned_rice_init(&encoder->private_->partitioned_rice_workspace[i][0]);
290 FLAC__format_entropy_coding_method_partitioned_rice_init(&encoder->private_->partitioned_rice_workspace[i][1]);
292 for(i = 0; i < 2; i++) {
293 FLAC__format_entropy_coding_method_partitioned_rice_init(&encoder->private_->partitioned_rice_workspace_mid_side[i][0]);
294 FLAC__format_entropy_coding_method_partitioned_rice_init(&encoder->private_->partitioned_rice_workspace_mid_side[i][1]);
296 for(i = 0; i < 2; i++)
297 FLAC__format_entropy_coding_method_partitioned_rice_init(&encoder->private_->partitioned_rice_extra[i]);
299 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
304 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
308 FLAC__ASSERT(0 != encoder);
309 FLAC__ASSERT(0 != encoder->protected_);
310 FLAC__ASSERT(0 != encoder->private_);
311 FLAC__ASSERT(0 != encoder->private_->frame);
313 encoder->private_->is_being_deleted = true;
315 FLAC__stream_encoder_finish(encoder);
317 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
318 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
320 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
321 FLAC__format_entropy_coding_method_partitioned_rice_clear(&encoder->private_->partitioned_rice_workspace[i][0]);
322 FLAC__format_entropy_coding_method_partitioned_rice_clear(&encoder->private_->partitioned_rice_workspace[i][1]);
324 for(i = 0; i < 2; i++) {
325 FLAC__format_entropy_coding_method_partitioned_rice_clear(&encoder->private_->partitioned_rice_workspace_mid_side[i][0]);
326 FLAC__format_entropy_coding_method_partitioned_rice_clear(&encoder->private_->partitioned_rice_workspace_mid_side[i][1]);
328 for(i = 0; i < 2; i++)
329 FLAC__format_entropy_coding_method_partitioned_rice_clear(&encoder->private_->partitioned_rice_extra[i]);
331 FLAC__bitbuffer_delete(encoder->private_->frame);
332 free(encoder->private_);
333 free(encoder->protected_);
337 /***********************************************************************
339 * Public class methods
341 ***********************************************************************/
343 FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
347 FLAC__ASSERT(0 != encoder);
349 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
350 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
352 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
354 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
355 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
357 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
358 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
360 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
361 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
363 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
364 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
366 if(encoder->protected_->bits_per_sample >= 32)
367 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
369 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
370 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
372 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
373 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
375 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
376 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
378 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
379 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
381 if(encoder->protected_->qlp_coeff_precision == 0) {
382 if(encoder->protected_->bits_per_sample < 16) {
383 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
384 /* @@@ until then we'll make a guess */
385 encoder->protected_->qlp_coeff_precision = max(5, 2 + encoder->protected_->bits_per_sample / 2);
387 else if(encoder->protected_->bits_per_sample == 16) {
388 if(encoder->protected_->blocksize <= 192)
389 encoder->protected_->qlp_coeff_precision = 7;
390 else if(encoder->protected_->blocksize <= 384)
391 encoder->protected_->qlp_coeff_precision = 8;
392 else if(encoder->protected_->blocksize <= 576)
393 encoder->protected_->qlp_coeff_precision = 9;
394 else if(encoder->protected_->blocksize <= 1152)
395 encoder->protected_->qlp_coeff_precision = 10;
396 else if(encoder->protected_->blocksize <= 2304)
397 encoder->protected_->qlp_coeff_precision = 11;
398 else if(encoder->protected_->blocksize <= 4608)
399 encoder->protected_->qlp_coeff_precision = 12;
401 encoder->protected_->qlp_coeff_precision = 13;
404 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 */
407 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))
408 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
410 if(encoder->protected_->streamable_subset) {
411 /*@@@ add check for blocksize here */
412 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)
413 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
414 if(encoder->protected_->sample_rate > 655350)
415 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
418 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
419 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
420 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
421 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
423 /* validate metadata */
424 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
425 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
426 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
427 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
428 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
429 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
430 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
431 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
435 encoder->private_->input_capacity = 0;
436 for(i = 0; i < encoder->protected_->channels; i++) {
437 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
438 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
440 for(i = 0; i < 2; i++) {
441 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
442 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
444 for(i = 0; i < encoder->protected_->channels; i++) {
445 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
446 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
447 encoder->private_->best_subframe[i] = 0;
449 for(i = 0; i < 2; i++) {
450 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
451 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
452 encoder->private_->best_subframe_mid_side[i] = 0;
454 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
455 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
456 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
457 encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
458 encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
459 if(encoder->private_->loose_mid_side_stereo_frames == 0)
460 encoder->private_->loose_mid_side_stereo_frames = 1;
461 encoder->private_->loose_mid_side_stereo_frame_count = 0;
462 encoder->private_->current_sample_number = 0;
463 encoder->private_->current_frame_number = 0;
465 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
466 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? */
467 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
470 * get the CPU info and set the function pointers
472 FLAC__cpu_info(&encoder->private_->cpuinfo);
473 /* first default to the non-asm routines */
474 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
475 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
476 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
477 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
478 /* now override with asm where appropriate */
480 if(encoder->private_->cpuinfo.use_asm) {
481 #ifdef FLAC__CPU_IA32
482 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
483 #ifdef FLAC__HAS_NASM
484 if(0 && encoder->private_->cpuinfo.data.ia32.sse) {
485 if(encoder->protected_->max_lpc_order < 4)
486 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
487 else if(encoder->protected_->max_lpc_order < 8)
488 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
489 else if(encoder->protected_->max_lpc_order < 12)
490 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
492 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
494 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
495 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
497 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
498 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
499 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
500 if(encoder->private_->cpuinfo.data.ia32.mmx) {
501 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
502 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
505 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
506 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
512 /* finally override based on wide-ness if necessary */
513 if(encoder->private_->use_wide_by_block) {
514 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
517 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
518 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
520 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
521 /* the above function sets the state for us in case of an error */
522 return encoder->protected_->state;
525 if(!FLAC__bitbuffer_init(encoder->private_->frame))
526 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
529 * Set up the verify stuff if necessary
531 if(encoder->protected_->verify) {
533 * First, set up the fifo which will hold the
534 * original signal to compare against
536 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
537 for(i = 0; i < encoder->protected_->channels; i++) {
538 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
539 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
541 encoder->private_->verify.input_fifo.tail = 0;
544 * Now set up a stream decoder for verification
546 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
547 if(0 == encoder->private_->verify.decoder)
548 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
550 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
551 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
552 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
553 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
554 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
555 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
556 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
558 encoder->private_->verify.error_stats.absolute_sample = 0;
559 encoder->private_->verify.error_stats.frame_number = 0;
560 encoder->private_->verify.error_stats.channel = 0;
561 encoder->private_->verify.error_stats.sample = 0;
562 encoder->private_->verify.error_stats.expected = 0;
563 encoder->private_->verify.error_stats.got = 0;
566 * write the stream header
568 if(encoder->protected_->verify)
569 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
570 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
571 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
572 if(!write_bitbuffer_(encoder, 0)) {
573 /* the above function sets the state for us in case of an error */
574 return encoder->protected_->state;
578 * write the STREAMINFO metadata block
580 if(encoder->protected_->verify)
581 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
582 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
583 encoder->private_->metadata.is_last = (encoder->protected_->num_metadata_blocks == 0);
584 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
585 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
586 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
587 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
588 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
589 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
590 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
591 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
592 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
593 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
594 MD5Init(&encoder->private_->md5context);
595 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
596 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
599 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
600 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
601 if(!write_bitbuffer_(encoder, 0)) {
602 /* the above function sets the state for us in case of an error */
603 return encoder->protected_->state;
607 * Now that the STREAMINFO block is written, we can init this to an
608 * absurdly-high value...
610 encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
611 /* ... and clear this to 0 */
612 encoder->private_->metadata.data.stream_info.total_samples = 0;
615 * write the user's metadata blocks
617 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
618 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
619 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
620 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
623 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
624 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
625 if(!write_bitbuffer_(encoder, 0)) {
626 /* the above function sets the state for us in case of an error */
627 return encoder->protected_->state;
631 if(encoder->protected_->verify)
632 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
634 return encoder->protected_->state;
637 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
639 FLAC__ASSERT(0 != encoder);
641 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
644 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
645 if(encoder->private_->current_sample_number != 0) {
646 encoder->protected_->blocksize = encoder->private_->current_sample_number;
647 process_frame_(encoder, true); /* true => is last frame */
651 MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
653 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
654 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
657 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
658 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
661 set_defaults_(encoder);
663 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
666 FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
668 FLAC__ASSERT(0 != encoder);
669 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
671 encoder->protected_->verify = value;
675 FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
677 FLAC__ASSERT(0 != encoder);
678 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
680 encoder->protected_->streamable_subset = value;
684 FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
686 FLAC__ASSERT(0 != encoder);
687 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
689 encoder->protected_->do_mid_side_stereo = value;
693 FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
695 FLAC__ASSERT(0 != encoder);
696 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
698 encoder->protected_->loose_mid_side_stereo = value;
702 FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
704 FLAC__ASSERT(0 != encoder);
705 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
707 encoder->protected_->channels = value;
711 FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
713 FLAC__ASSERT(0 != encoder);
714 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
716 encoder->protected_->bits_per_sample = value;
720 FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
722 FLAC__ASSERT(0 != encoder);
723 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
725 encoder->protected_->sample_rate = value;
729 FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
731 FLAC__ASSERT(0 != encoder);
732 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
734 encoder->protected_->blocksize = value;
738 FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
740 FLAC__ASSERT(0 != encoder);
741 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
743 encoder->protected_->max_lpc_order = value;
747 FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
749 FLAC__ASSERT(0 != encoder);
750 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
752 encoder->protected_->qlp_coeff_precision = value;
756 FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
758 FLAC__ASSERT(0 != encoder);
759 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
761 encoder->protected_->do_qlp_coeff_prec_search = value;
765 FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
767 FLAC__ASSERT(0 != encoder);
768 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
772 encoder->protected_->do_escape_coding = value;
779 FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
781 FLAC__ASSERT(0 != encoder);
782 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
784 encoder->protected_->do_exhaustive_model_search = value;
788 FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
790 FLAC__ASSERT(0 != encoder);
791 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
793 encoder->protected_->min_residual_partition_order = value;
797 FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
799 FLAC__ASSERT(0 != encoder);
800 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
802 encoder->protected_->max_residual_partition_order = value;
806 FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
808 FLAC__ASSERT(0 != encoder);
809 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
813 encoder->protected_->rice_parameter_search_dist = value;
820 FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
822 FLAC__ASSERT(0 != encoder);
823 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
825 encoder->protected_->total_samples_estimate = value;
829 FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
831 FLAC__ASSERT(0 != encoder);
832 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
834 encoder->protected_->metadata = metadata;
835 encoder->protected_->num_metadata_blocks = num_blocks;
839 FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
841 FLAC__ASSERT(0 != encoder);
842 FLAC__ASSERT(0 != value);
843 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
845 encoder->private_->write_callback = value;
849 FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
851 FLAC__ASSERT(0 != encoder);
852 FLAC__ASSERT(0 != value);
853 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
855 encoder->private_->metadata_callback = value;
859 FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
861 FLAC__ASSERT(0 != encoder);
862 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
864 encoder->private_->client_data = value;
868 FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
870 FLAC__ASSERT(0 != encoder);
871 return encoder->protected_->state;
874 FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
876 FLAC__ASSERT(0 != encoder);
877 if(encoder->protected_->verify)
878 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
880 return FLAC__STREAM_DECODER_UNINITIALIZED;
883 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)
885 FLAC__ASSERT(0 != encoder);
886 if(0 != absolute_sample)
887 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
888 if(0 != frame_number)
889 *frame_number = encoder->private_->verify.error_stats.frame_number;
891 *channel = encoder->private_->verify.error_stats.channel;
893 *sample = encoder->private_->verify.error_stats.sample;
895 *expected = encoder->private_->verify.error_stats.expected;
897 *got = encoder->private_->verify.error_stats.got;
900 FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
902 FLAC__ASSERT(0 != encoder);
903 return encoder->protected_->verify;
906 FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
908 FLAC__ASSERT(0 != encoder);
909 return encoder->protected_->streamable_subset;
912 FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
914 FLAC__ASSERT(0 != encoder);
915 return encoder->protected_->do_mid_side_stereo;
918 FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
920 FLAC__ASSERT(0 != encoder);
921 return encoder->protected_->loose_mid_side_stereo;
924 unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
926 FLAC__ASSERT(0 != encoder);
927 return encoder->protected_->channels;
930 unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
932 FLAC__ASSERT(0 != encoder);
933 return encoder->protected_->bits_per_sample;
936 unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
938 FLAC__ASSERT(0 != encoder);
939 return encoder->protected_->sample_rate;
942 unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
944 FLAC__ASSERT(0 != encoder);
945 return encoder->protected_->blocksize;
948 unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
950 FLAC__ASSERT(0 != encoder);
951 return encoder->protected_->max_lpc_order;
954 unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
956 FLAC__ASSERT(0 != encoder);
957 return encoder->protected_->qlp_coeff_precision;
960 FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
962 FLAC__ASSERT(0 != encoder);
963 return encoder->protected_->do_qlp_coeff_prec_search;
966 FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
968 FLAC__ASSERT(0 != encoder);
969 return encoder->protected_->do_escape_coding;
972 FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
974 FLAC__ASSERT(0 != encoder);
975 return encoder->protected_->do_exhaustive_model_search;
978 unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
980 FLAC__ASSERT(0 != encoder);
981 return encoder->protected_->min_residual_partition_order;
984 unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
986 FLAC__ASSERT(0 != encoder);
987 return encoder->protected_->max_residual_partition_order;
990 unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
992 FLAC__ASSERT(0 != encoder);
993 return encoder->protected_->rice_parameter_search_dist;
996 FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
998 FLAC__ASSERT(0 != encoder);
999 return encoder->protected_->total_samples_estimate;
1002 FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1004 unsigned i, j, channel;
1005 FLAC__int32 x, mid, side;
1006 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1008 FLAC__ASSERT(0 != encoder);
1009 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1012 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1014 if(encoder->protected_->verify)
1015 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1017 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1018 x = mid = side = buffer[0][j];
1019 encoder->private_->integer_signal[0][i] = x;
1020 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1022 encoder->private_->integer_signal[1][i] = x;
1023 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1026 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1027 encoder->private_->integer_signal_mid_side[1][i] = side;
1028 encoder->private_->integer_signal_mid_side[0][i] = mid;
1029 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1030 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1031 encoder->private_->current_sample_number++;
1033 if(i == blocksize) {
1034 if(!process_frame_(encoder, false)) /* false => not last frame */
1037 } while(j < samples);
1041 if(encoder->protected_->verify)
1042 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1044 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1045 for(channel = 0; channel < channels; channel++) {
1046 x = buffer[channel][j];
1047 encoder->private_->integer_signal[channel][i] = x;
1048 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1050 encoder->private_->current_sample_number++;
1052 if(i == blocksize) {
1053 if(!process_frame_(encoder, false)) /* false => not last frame */
1056 } while(j < samples);
1062 FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1064 unsigned i, j, k, channel;
1065 FLAC__int32 x, mid, side;
1066 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1068 FLAC__ASSERT(0 != encoder);
1069 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1072 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1074 if(encoder->protected_->verify)
1075 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1077 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1078 x = mid = side = buffer[k++];
1079 encoder->private_->integer_signal[0][i] = x;
1080 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1082 encoder->private_->integer_signal[1][i] = x;
1083 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1086 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1087 encoder->private_->integer_signal_mid_side[1][i] = side;
1088 encoder->private_->integer_signal_mid_side[0][i] = mid;
1089 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1090 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1091 encoder->private_->current_sample_number++;
1093 if(i == blocksize) {
1094 if(!process_frame_(encoder, false)) /* false => not last frame */
1097 } while(j < samples);
1101 if(encoder->protected_->verify)
1102 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1104 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1105 for(channel = 0; channel < channels; channel++) {
1107 encoder->private_->integer_signal[channel][i] = x;
1108 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1110 encoder->private_->current_sample_number++;
1112 if(i == blocksize) {
1113 if(!process_frame_(encoder, false)) /* false => not last frame */
1116 } while(j < samples);
1122 /***********************************************************************
1124 * Private class methods
1126 ***********************************************************************/
1128 void set_defaults_(FLAC__StreamEncoder *encoder)
1130 FLAC__ASSERT(0 != encoder);
1132 encoder->protected_->verify = false;
1133 encoder->protected_->streamable_subset = true;
1134 encoder->protected_->do_mid_side_stereo = false;
1135 encoder->protected_->loose_mid_side_stereo = false;
1136 encoder->protected_->channels = 2;
1137 encoder->protected_->bits_per_sample = 16;
1138 encoder->protected_->sample_rate = 44100;
1139 encoder->protected_->blocksize = 1152;
1140 encoder->protected_->max_lpc_order = 0;
1141 encoder->protected_->qlp_coeff_precision = 0;
1142 encoder->protected_->do_qlp_coeff_prec_search = false;
1143 encoder->protected_->do_exhaustive_model_search = false;
1144 encoder->protected_->do_escape_coding = false;
1145 encoder->protected_->min_residual_partition_order = 0;
1146 encoder->protected_->max_residual_partition_order = 0;
1147 encoder->protected_->rice_parameter_search_dist = 0;
1148 encoder->protected_->total_samples_estimate = 0;
1149 encoder->protected_->metadata = 0;
1150 encoder->protected_->num_metadata_blocks = 0;
1152 encoder->private_->write_callback = 0;
1153 encoder->private_->metadata_callback = 0;
1154 encoder->private_->client_data = 0;
1157 void free_(FLAC__StreamEncoder *encoder)
1159 unsigned i, channel;
1161 FLAC__ASSERT(0 != encoder);
1162 for(i = 0; i < encoder->protected_->channels; i++) {
1163 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1164 free(encoder->private_->integer_signal_unaligned[i]);
1165 encoder->private_->integer_signal_unaligned[i] = 0;
1167 if(0 != encoder->private_->real_signal_unaligned[i]) {
1168 free(encoder->private_->real_signal_unaligned[i]);
1169 encoder->private_->real_signal_unaligned[i] = 0;
1172 for(i = 0; i < 2; i++) {
1173 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1174 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1175 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1177 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1178 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1179 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1182 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1183 for(i = 0; i < 2; i++) {
1184 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1185 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1186 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1190 for(channel = 0; channel < 2; channel++) {
1191 for(i = 0; i < 2; i++) {
1192 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1193 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1194 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1198 if(0 != encoder->private_->abs_residual_unaligned) {
1199 free(encoder->private_->abs_residual_unaligned);
1200 encoder->private_->abs_residual_unaligned = 0;
1202 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1203 free(encoder->private_->abs_residual_partition_sums_unaligned);
1204 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1206 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1207 free(encoder->private_->raw_bits_per_partition_unaligned);
1208 encoder->private_->raw_bits_per_partition_unaligned = 0;
1210 if(encoder->protected_->verify) {
1211 for(i = 0; i < encoder->protected_->channels; i++) {
1212 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1213 free(encoder->private_->verify.input_fifo.data[i]);
1214 encoder->private_->verify.input_fifo.data[i] = 0;
1218 FLAC__bitbuffer_free(encoder->private_->frame);
1221 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
1224 unsigned i, channel;
1226 FLAC__ASSERT(new_size > 0);
1227 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1228 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1230 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1231 if(new_size <= encoder->private_->input_capacity)
1236 /* 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. */
1238 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1239 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1240 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1241 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1242 encoder->private_->integer_signal[i] += 4;
1244 for(i = 0; ok && i < 2; i++) {
1245 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]);
1246 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]);
1247 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1248 encoder->private_->integer_signal_mid_side[i] += 4;
1250 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1251 for(i = 0; ok && i < 2; i++) {
1252 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1255 for(channel = 0; ok && channel < 2; channel++) {
1256 for(i = 0; ok && i < 2; i++) {
1257 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]);
1260 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1261 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 */
1262 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1263 if(encoder->protected_->do_escape_coding)
1264 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1267 encoder->private_->input_capacity = new_size;
1269 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1274 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
1276 const FLAC__byte *buffer;
1279 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1281 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1283 if(encoder->protected_->verify) {
1284 encoder->private_->verify.output.data = buffer;
1285 encoder->private_->verify.output.bytes = bytes;
1286 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1287 encoder->private_->verify.needs_magic_hack = true;
1290 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1291 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1292 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1293 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1299 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1300 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1304 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1307 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1308 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1314 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1316 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1319 * Accumulate raw signal to the MD5 signature
1321 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)) {
1322 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1327 * Process the frame header and subframes into the frame bitbuffer
1329 if(!process_subframes_(encoder, is_last_frame)) {
1330 /* the above function sets the state for us in case of an error */
1335 * Zero-pad the frame to a byte_boundary
1337 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1338 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1343 * CRC-16 the whole thing
1345 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1346 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1351 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1352 /* the above function sets the state for us in case of an error */
1357 * Get ready for the next frame
1359 encoder->private_->current_sample_number = 0;
1360 encoder->private_->current_frame_number++;
1361 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1366 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1368 FLAC__FrameHeader frame_header;
1369 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1370 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1373 * Calculate the min,max Rice partition orders
1376 max_partition_order = 0;
1379 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1380 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
1382 min_partition_order = min(min_partition_order, max_partition_order);
1384 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1389 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1390 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1393 frame_header.blocksize = encoder->protected_->blocksize;
1394 frame_header.sample_rate = encoder->protected_->sample_rate;
1395 frame_header.channels = encoder->protected_->channels;
1396 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1397 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1398 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1399 frame_header.number.frame_number = encoder->private_->current_frame_number;
1402 * Figure out what channel assignments to try
1404 if(encoder->protected_->do_mid_side_stereo) {
1405 if(encoder->protected_->loose_mid_side_stereo) {
1406 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1407 do_independent = true;
1411 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1412 do_mid_side = !do_independent;
1416 do_independent = true;
1421 do_independent = true;
1422 do_mid_side = false;
1425 FLAC__ASSERT(do_independent || do_mid_side);
1428 * Check for wasted bits; set effective bps for each subframe
1430 if(do_independent) {
1431 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1432 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1433 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1434 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1438 FLAC__ASSERT(encoder->protected_->channels == 2);
1439 for(channel = 0; channel < 2; channel++) {
1440 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1441 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1442 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1447 * First do a normal encoding pass of each independent channel
1449 if(do_independent) {
1450 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1451 if(!process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private_->subframe_bps[channel], encoder->private_->integer_signal[channel], encoder->private_->real_signal[channel], encoder->private_->subframe_workspace_ptr[channel], encoder->private_->partitioned_rice_workspace_ptr[channel], encoder->private_->residual_workspace[channel], encoder->private_->best_subframe+channel, encoder->private_->best_subframe_bits+channel))
1457 * Now do mid and side channels if requested
1460 FLAC__ASSERT(encoder->protected_->channels == 2);
1462 for(channel = 0; channel < 2; channel++) {
1463 if(!process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private_->subframe_bps_mid_side[channel], encoder->private_->integer_signal_mid_side[channel], encoder->private_->real_signal_mid_side[channel], encoder->private_->subframe_workspace_ptr_mid_side[channel], encoder->private_->partitioned_rice_workspace_ptr_mid_side[channel], encoder->private_->residual_workspace_mid_side[channel], encoder->private_->best_subframe_mid_side+channel, encoder->private_->best_subframe_bits_mid_side+channel))
1469 * Compose the frame bitbuffer
1472 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1473 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1474 FLAC__ChannelAssignment channel_assignment;
1476 FLAC__ASSERT(encoder->protected_->channels == 2);
1478 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1479 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1482 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1484 FLAC__ChannelAssignment ca;
1486 FLAC__ASSERT(do_independent && do_mid_side);
1488 /* We have to figure out which channel assignent results in the smallest frame */
1489 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
1490 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
1491 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
1492 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
1494 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
1495 if(bits[ca] < min_bits) {
1496 min_bits = bits[ca];
1497 channel_assignment = ca;
1502 frame_header.channel_assignment = channel_assignment;
1504 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1505 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1509 switch(channel_assignment) {
1510 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1511 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1512 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1514 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1515 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1516 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1518 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1519 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1520 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1522 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1523 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1524 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1530 switch(channel_assignment) {
1531 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1532 left_bps = encoder->private_->subframe_bps [0];
1533 right_bps = encoder->private_->subframe_bps [1];
1535 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1536 left_bps = encoder->private_->subframe_bps [0];
1537 right_bps = encoder->private_->subframe_bps_mid_side[1];
1539 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1540 left_bps = encoder->private_->subframe_bps_mid_side[1];
1541 right_bps = encoder->private_->subframe_bps [1];
1543 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1544 left_bps = encoder->private_->subframe_bps_mid_side[0];
1545 right_bps = encoder->private_->subframe_bps_mid_side[1];
1551 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1552 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
1554 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1558 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1559 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1563 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1564 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
1565 /* the above function sets the state for us in case of an error */
1571 if(encoder->protected_->loose_mid_side_stereo) {
1572 encoder->private_->loose_mid_side_stereo_frame_count++;
1573 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1574 encoder->private_->loose_mid_side_stereo_frame_count = 0;
1577 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1582 FLAC__bool process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
1584 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1585 FLAC__real lpc_residual_bits_per_sample;
1586 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 */
1587 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1588 unsigned min_lpc_order, max_lpc_order, lpc_order;
1589 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1590 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1591 unsigned rice_parameter;
1592 unsigned _candidate_bits, _best_bits;
1593 unsigned _best_subframe;
1595 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1597 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1599 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1600 /* check for constant subframe */
1601 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);
1602 if(fixed_residual_bits_per_sample[1] == 0.0) {
1603 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1604 unsigned i, signal_is_constant = true;
1605 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1606 if(integer_signal[0] != integer_signal[i]) {
1607 signal_is_constant = false;
1611 if(signal_is_constant) {
1612 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1613 if(_candidate_bits < _best_bits) {
1614 _best_subframe = !_best_subframe;
1615 _best_bits = _candidate_bits;
1621 if(encoder->protected_->do_exhaustive_model_search) {
1622 min_fixed_order = 0;
1623 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1626 min_fixed_order = max_fixed_order = guess_fixed_order;
1628 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1629 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1630 continue; /* don't even try */
1631 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 */
1632 #ifndef FLAC__SYMMETRIC_RICE
1633 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1635 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1636 #ifdef DEBUG_VERBOSE
1637 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1639 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1641 _candidate_bits = evaluate_fixed_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private_->abs_residual, encoder->private_->abs_residual_partition_sums, encoder->private_->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected_->do_escape_coding, encoder->protected_->rice_parameter_search_dist, subframe[!_best_subframe], partitioned_rice[!_best_subframe]);
1642 if(_candidate_bits < _best_bits) {
1643 _best_subframe = !_best_subframe;
1644 _best_bits = _candidate_bits;
1649 if(encoder->protected_->max_lpc_order > 0) {
1650 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
1651 max_lpc_order = frame_header->blocksize-1;
1653 max_lpc_order = encoder->protected_->max_lpc_order;
1654 if(max_lpc_order > 0) {
1655 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
1656 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
1657 if(autoc[0] != 0.0) {
1658 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
1659 if(encoder->protected_->do_exhaustive_model_search) {
1663 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
1664 min_lpc_order = max_lpc_order = guess_lpc_order;
1666 if(encoder->protected_->do_qlp_coeff_prec_search) {
1667 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
1668 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 */
1671 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
1673 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
1674 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
1675 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
1676 continue; /* don't even try */
1677 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
1678 #ifndef FLAC__SYMMETRIC_RICE
1679 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1681 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1682 #ifdef DEBUG_VERBOSE
1683 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1685 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1687 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
1688 _candidate_bits = evaluate_lpc_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private_->abs_residual, encoder->private_->abs_residual_partition_sums, encoder->private_->raw_bits_per_partition, encoder->private_->lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected_->do_escape_coding, encoder->protected_->rice_parameter_search_dist, subframe[!_best_subframe], partitioned_rice[!_best_subframe]);
1689 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1690 if(_candidate_bits < _best_bits) {
1691 _best_subframe = !_best_subframe;
1692 _best_bits = _candidate_bits;
1703 *best_subframe = _best_subframe;
1704 *best_bits = _best_bits;
1709 FLAC__bool add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
1711 switch(subframe->type) {
1712 case FLAC__SUBFRAME_TYPE_CONSTANT:
1713 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
1714 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1718 case FLAC__SUBFRAME_TYPE_FIXED:
1719 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
1720 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1724 case FLAC__SUBFRAME_TYPE_LPC:
1725 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
1726 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1730 case FLAC__SUBFRAME_TYPE_VERBATIM:
1731 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
1732 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1743 unsigned evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
1745 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1746 subframe->data.constant.value = signal;
1748 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
1751 unsigned evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice)
1753 unsigned i, residual_bits;
1754 const unsigned residual_samples = blocksize - order;
1756 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1758 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1760 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1761 subframe->data.fixed.residual = residual;
1763 residual_bits = find_best_partition_order_(encoder->private_, residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, partitioned_rice);
1765 subframe->data.fixed.entropy_coding_method.data.partitioned_rice = *partitioned_rice;
1767 subframe->data.fixed.order = order;
1768 for(i = 0; i < order; i++)
1769 subframe->data.fixed.warmup[i] = signal[i];
1771 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
1774 unsigned evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice)
1776 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1777 unsigned i, residual_bits;
1778 int quantization, ret;
1779 const unsigned residual_samples = blocksize - order;
1781 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
1783 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1785 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
1786 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1788 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1790 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1792 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1793 subframe->data.lpc.residual = residual;
1795 residual_bits = find_best_partition_order_(encoder->private_, residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, partitioned_rice);
1797 subframe->data.fixed.entropy_coding_method.data.partitioned_rice = *partitioned_rice;
1799 subframe->data.lpc.order = order;
1800 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1801 subframe->data.lpc.quantization_level = quantization;
1802 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
1803 for(i = 0; i < order; i++)
1804 subframe->data.lpc.warmup[i] = signal[i];
1806 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;
1809 unsigned evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
1811 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1813 subframe->data.verbatim.data = signal;
1815 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
1818 unsigned find_best_partition_order_(FLAC__StreamEncoderPrivate *private_, const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice)
1821 unsigned residual_bits, best_residual_bits = 0;
1822 unsigned residual_sample;
1823 unsigned best_parameters_index = 0;
1824 const unsigned blocksize = residual_samples + predictor_order;
1826 /* compute abs(residual) for use later */
1827 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1828 r = residual[residual_sample];
1829 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
1832 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
1833 min_partition_order = min(min_partition_order, max_partition_order);
1835 if(precompute_partition_sums) {
1836 int partition_order;
1839 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
1841 if(do_escape_coding)
1842 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
1844 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1845 #ifdef DONT_ESTIMATE_RICE_BITS
1846 if(!set_partitioned_rice_with_precompute_(residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, &private_->partitioned_rice_extra[!best_parameters_index], &residual_bits))
1848 if(!set_partitioned_rice_with_precompute_(abs_residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, &private_->partitioned_rice_extra[!best_parameters_index], &residual_bits))
1851 FLAC__ASSERT(best_residual_bits != 0);
1854 sum += 1u << partition_order;
1855 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1856 best_residual_bits = residual_bits;
1857 best_parameters_index = !best_parameters_index;
1858 best_partitioned_rice->order = partition_order;
1863 unsigned partition_order;
1864 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1865 #ifdef DONT_ESTIMATE_RICE_BITS
1866 if(!set_partitioned_rice_(abs_residual, residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, &private_->partitioned_rice_extra[!best_parameters_index], &residual_bits))
1868 if(!set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, &private_->partitioned_rice_extra[!best_parameters_index], &residual_bits))
1871 FLAC__ASSERT(best_residual_bits != 0);
1874 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1875 best_residual_bits = residual_bits;
1876 best_parameters_index = !best_parameters_index;
1877 best_partitioned_rice->order = partition_order;
1882 FLAC__format_entropy_coding_method_partitioned_rice_ensure_size(best_partitioned_rice, max(6, best_partitioned_rice->order));
1883 memcpy(best_partitioned_rice->parameters, private_->partitioned_rice_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
1884 memcpy(best_partitioned_rice->raw_bits, private_->partitioned_rice_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
1886 return best_residual_bits;
1889 void precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
1891 int partition_order;
1892 unsigned from_partition, to_partition = 0;
1893 const unsigned blocksize = residual_samples + predictor_order;
1895 /* first do max_partition_order */
1896 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1897 FLAC__uint64 abs_residual_partition_sum;
1899 unsigned partition, partition_sample, partition_samples, residual_sample;
1900 const unsigned partitions = 1u << partition_order;
1901 const unsigned default_partition_samples = blocksize >> partition_order;
1903 FLAC__ASSERT(default_partition_samples > predictor_order);
1905 for(partition = residual_sample = 0; partition < partitions; partition++) {
1906 partition_samples = default_partition_samples;
1908 partition_samples -= predictor_order;
1909 abs_residual_partition_sum = 0;
1910 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1911 abs_r = abs_residual[residual_sample];
1912 abs_residual_partition_sum += abs_r;
1915 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
1917 to_partition = partitions;
1921 /* now merge partitions for lower orders */
1922 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1925 const unsigned partitions = 1u << partition_order;
1926 for(i = 0; i < partitions; i++) {
1927 s = abs_residual_partition_sums[from_partition];
1929 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1936 void precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
1938 int partition_order;
1939 unsigned from_partition, to_partition = 0;
1940 const unsigned blocksize = residual_samples + predictor_order;
1942 /* first do max_partition_order */
1943 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1944 FLAC__int32 r, residual_partition_min, residual_partition_max;
1945 unsigned silog2_min, silog2_max;
1946 unsigned partition, partition_sample, partition_samples, residual_sample;
1947 const unsigned partitions = 1u << partition_order;
1948 const unsigned default_partition_samples = blocksize >> partition_order;
1950 FLAC__ASSERT(default_partition_samples > predictor_order);
1952 for(partition = residual_sample = 0; partition < partitions; partition++) {
1953 partition_samples = default_partition_samples;
1955 partition_samples -= predictor_order;
1956 residual_partition_min = residual_partition_max = 0;
1957 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1958 r = residual[residual_sample];
1959 if(r < residual_partition_min)
1960 residual_partition_min = r;
1961 else if(r > residual_partition_max)
1962 residual_partition_max = r;
1965 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
1966 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
1967 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
1969 to_partition = partitions;
1973 /* now merge partitions for lower orders */
1974 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1977 const unsigned partitions = 1u << partition_order;
1978 for(i = 0; i < partitions; i++) {
1979 m = raw_bits_per_partition[from_partition];
1981 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1988 #ifdef VARIABLE_RICE_BITS
1989 #undef VARIABLE_RICE_BITS
1991 #ifndef DONT_ESTIMATE_RICE_BITS
1992 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1995 #ifdef DONT_ESTIMATE_RICE_BITS
1996 FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits)
1998 FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits)
2001 unsigned rice_parameter, partition_bits;
2002 #ifndef NO_RICE_SEARCH
2003 unsigned best_partition_bits;
2004 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2006 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2007 unsigned *parameters;
2009 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2011 partitioned_rice->order = partition_order;
2012 FLAC__format_entropy_coding_method_partitioned_rice_ensure_size(partitioned_rice, max(6, partition_order));
2013 parameters = partitioned_rice->parameters;
2015 if(partition_order == 0) {
2018 #ifndef NO_RICE_SEARCH
2019 if(rice_parameter_search_dist) {
2020 if(suggested_rice_parameter < rice_parameter_search_dist)
2021 min_rice_parameter = 0;
2023 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2024 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2025 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2026 #ifdef DEBUG_VERBOSE
2027 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2029 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2033 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2035 best_partition_bits = 0xffffffff;
2036 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2038 #ifdef VARIABLE_RICE_BITS
2039 #ifdef FLAC__SYMMETRIC_RICE
2040 partition_bits = (2+rice_parameter) * residual_samples;
2042 const unsigned rice_parameter_estimate = rice_parameter-1;
2043 partition_bits = (1+rice_parameter) * residual_samples;
2048 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2049 for(i = 0; i < residual_samples; i++) {
2050 #ifdef VARIABLE_RICE_BITS
2051 #ifdef FLAC__SYMMETRIC_RICE
2052 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2054 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2057 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2060 #ifndef NO_RICE_SEARCH
2061 if(partition_bits < best_partition_bits) {
2062 best_rice_parameter = rice_parameter;
2063 best_partition_bits = partition_bits;
2067 parameters[0] = best_rice_parameter;
2068 bits_ += best_partition_bits;
2071 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2072 unsigned partition_samples;
2073 FLAC__uint64 mean, k;
2074 const unsigned partitions = 1u << partition_order;
2075 for(partition = residual_sample = 0; partition < partitions; partition++) {
2076 partition_samples = (residual_samples+predictor_order) >> partition_order;
2077 if(partition == 0) {
2078 if(partition_samples <= predictor_order)
2081 partition_samples -= predictor_order;
2084 save_residual_sample = residual_sample;
2085 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2086 mean += abs_residual[residual_sample];
2087 residual_sample = save_residual_sample;
2088 #ifdef FLAC__SYMMETRIC_RICE
2089 mean += partition_samples >> 1; /* for rounding effect */
2090 mean /= partition_samples;
2092 /* calc rice_parameter = floor(log2(mean)) */
2100 /* calc rice_parameter ala LOCO-I */
2101 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2104 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2105 #ifdef DEBUG_VERBOSE
2106 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2108 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2111 #ifndef NO_RICE_SEARCH
2112 if(rice_parameter_search_dist) {
2113 if(rice_parameter < rice_parameter_search_dist)
2114 min_rice_parameter = 0;
2116 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2117 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2118 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2119 #ifdef DEBUG_VERBOSE
2120 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2122 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2126 min_rice_parameter = max_rice_parameter = rice_parameter;
2128 best_partition_bits = 0xffffffff;
2129 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2131 #ifdef VARIABLE_RICE_BITS
2132 #ifdef FLAC__SYMMETRIC_RICE
2133 partition_bits = (2+rice_parameter) * partition_samples;
2135 const unsigned rice_parameter_estimate = rice_parameter-1;
2136 partition_bits = (1+rice_parameter) * partition_samples;
2141 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2142 save_residual_sample = residual_sample;
2143 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2144 #ifdef VARIABLE_RICE_BITS
2145 #ifdef FLAC__SYMMETRIC_RICE
2146 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2148 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2151 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2154 #ifndef NO_RICE_SEARCH
2155 if(rice_parameter != max_rice_parameter)
2156 residual_sample = save_residual_sample;
2157 if(partition_bits < best_partition_bits) {
2158 best_rice_parameter = rice_parameter;
2159 best_partition_bits = partition_bits;
2163 parameters[partition] = best_rice_parameter;
2164 bits_ += best_partition_bits;
2172 #ifdef DONT_ESTIMATE_RICE_BITS
2173 FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits)
2175 FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits)
2178 unsigned rice_parameter, partition_bits;
2179 #ifndef NO_RICE_SEARCH
2180 unsigned best_partition_bits;
2181 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2184 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2185 unsigned *parameters, *raw_bits;
2187 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2189 partitioned_rice->order = partition_order;
2190 FLAC__format_entropy_coding_method_partitioned_rice_ensure_size(partitioned_rice, max(6, partition_order));
2191 parameters = partitioned_rice->parameters;
2192 raw_bits = partitioned_rice->raw_bits;
2194 if(partition_order == 0) {
2197 #ifndef NO_RICE_SEARCH
2198 if(rice_parameter_search_dist) {
2199 if(suggested_rice_parameter < rice_parameter_search_dist)
2200 min_rice_parameter = 0;
2202 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2203 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2204 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2205 #ifdef DEBUG_VERBOSE
2206 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2208 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2212 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2214 best_partition_bits = 0xffffffff;
2215 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2217 #ifdef VARIABLE_RICE_BITS
2218 #ifdef FLAC__SYMMETRIC_RICE
2219 partition_bits = (2+rice_parameter) * residual_samples;
2221 const unsigned rice_parameter_estimate = rice_parameter-1;
2222 partition_bits = (1+rice_parameter) * residual_samples;
2227 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2228 for(i = 0; i < residual_samples; i++) {
2229 #ifdef VARIABLE_RICE_BITS
2230 #ifdef FLAC__SYMMETRIC_RICE
2231 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2233 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2236 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2239 #ifndef NO_RICE_SEARCH
2240 if(partition_bits < best_partition_bits) {
2241 best_rice_parameter = rice_parameter;
2242 best_partition_bits = partition_bits;
2246 if(search_for_escapes) {
2247 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;
2248 if(flat_bits <= best_partition_bits) {
2249 raw_bits[0] = raw_bits_per_partition[0];
2250 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2251 best_partition_bits = flat_bits;
2254 parameters[0] = best_rice_parameter;
2255 bits_ += best_partition_bits;
2258 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2259 unsigned partition_samples;
2260 FLAC__uint64 mean, k;
2261 const unsigned partitions = 1u << partition_order;
2262 for(partition = residual_sample = 0; partition < partitions; partition++) {
2263 partition_samples = (residual_samples+predictor_order) >> partition_order;
2264 if(partition == 0) {
2265 if(partition_samples <= predictor_order)
2268 partition_samples -= predictor_order;
2270 mean = abs_residual_partition_sums[partition];
2271 #ifdef FLAC__SYMMETRIC_RICE
2272 mean += partition_samples >> 1; /* for rounding effect */
2273 mean /= partition_samples;
2275 /* calc rice_parameter = floor(log2(mean)) */
2283 /* calc rice_parameter ala LOCO-I */
2284 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2287 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2288 #ifdef DEBUG_VERBOSE
2289 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2291 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2294 #ifndef NO_RICE_SEARCH
2295 if(rice_parameter_search_dist) {
2296 if(rice_parameter < rice_parameter_search_dist)
2297 min_rice_parameter = 0;
2299 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2300 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2301 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2302 #ifdef DEBUG_VERBOSE
2303 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2305 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2309 min_rice_parameter = max_rice_parameter = rice_parameter;
2311 best_partition_bits = 0xffffffff;
2312 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2314 #ifdef VARIABLE_RICE_BITS
2315 #ifdef FLAC__SYMMETRIC_RICE
2316 partition_bits = (2+rice_parameter) * partition_samples;
2318 const unsigned rice_parameter_estimate = rice_parameter-1;
2319 partition_bits = (1+rice_parameter) * partition_samples;
2324 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2325 save_residual_sample = residual_sample;
2326 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2327 #ifdef VARIABLE_RICE_BITS
2328 #ifdef FLAC__SYMMETRIC_RICE
2329 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2331 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2334 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2337 #ifndef NO_RICE_SEARCH
2338 if(rice_parameter != max_rice_parameter)
2339 residual_sample = save_residual_sample;
2340 if(partition_bits < best_partition_bits) {
2341 best_rice_parameter = rice_parameter;
2342 best_partition_bits = partition_bits;
2346 if(search_for_escapes) {
2347 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;
2348 if(flat_bits <= best_partition_bits) {
2349 raw_bits[partition] = raw_bits_per_partition[partition];
2350 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2351 best_partition_bits = flat_bits;
2354 parameters[partition] = best_rice_parameter;
2355 bits_ += best_partition_bits;
2363 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
2368 for(i = 0; i < samples && !(x&1); i++)
2375 for(shift = 0; !(x&1); shift++)
2380 for(i = 0; i < samples; i++)
2381 signal[i] >>= shift;
2387 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
2391 for(channel = 0; channel < channels; channel++)
2392 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
2394 fifo->tail += wide_samples;
2396 FLAC__ASSERT(fifo->tail <= fifo->size);
2399 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
2402 unsigned sample, wide_sample;
2403 unsigned tail = fifo->tail;
2405 sample = input_offset * channels;
2406 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
2407 for(channel = 0; channel < channels; channel++)
2408 fifo->data[channel][tail] = input[sample++];
2413 FLAC__ASSERT(fifo->tail <= fifo->size);
2416 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
2418 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
2419 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
2422 if(encoder->private_->verify.needs_magic_hack) {
2423 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
2424 *bytes = FLAC__STREAM_SYNC_LENGTH;
2425 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
2426 encoder->private_->verify.needs_magic_hack = false;
2429 if(encoded_bytes == 0) {
2431 * If we get here, a FIFO underflow has occurred,
2432 * which means there is a bug somewhere.
2435 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2437 else if(encoded_bytes < *bytes)
2438 *bytes = encoded_bytes;
2439 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
2440 encoder->private_->verify.output.data += *bytes;
2441 encoder->private_->verify.output.bytes -= *bytes;
2444 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2447 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
2449 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
2451 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
2452 const unsigned blocksize = frame->header.blocksize;
2453 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
2455 for(channel = 0; channel < channels; channel++) {
2456 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
2457 unsigned i, sample = 0;
2458 FLAC__int32 expect = 0, got = 0;
2460 for(i = 0; i < blocksize; i++) {
2461 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
2463 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
2464 got = (FLAC__int32)buffer[channel][i];
2468 FLAC__ASSERT(i < blocksize);
2469 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2470 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
2471 encoder->private_->verify.error_stats.frame_number = frame->header.number.sample_number / blocksize;
2472 encoder->private_->verify.error_stats.channel = channel;
2473 encoder->private_->verify.error_stats.sample = sample;
2474 encoder->private_->verify.error_stats.expected = expect;
2475 encoder->private_->verify.error_stats.got = got;
2476 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
2477 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
2480 /* dequeue the frame from the fifo */
2481 for(channel = 0; channel < channels; channel++) {
2482 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
2484 encoder->private_->verify.input_fifo.tail -= blocksize;
2485 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
2488 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
2490 (void)decoder, (void)metadata, (void)client_data;
2493 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
2495 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
2496 (void)decoder, (void)status;
2497 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;