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 "protected/stream_encoder.h"
25 #include "private/bitbuffer.h"
26 #include "private/bitmath.h"
27 #include "private/crc.h"
28 #include "private/cpu.h"
29 #include "private/stream_encoder_framing.h"
30 #include "private/fixed.h"
31 #include "private/lpc.h"
32 #include "private/md5.h"
33 #include "private/memory.h"
38 #define min(x,y) ((x)<(y)?(x):(y))
43 #define max(x,y) ((x)>(y)?(x):(y))
45 /***********************************************************************
47 * Private class method prototypes
49 ***********************************************************************/
51 static void set_defaults_(FLAC__StreamEncoder *encoder);
52 static void free_(FLAC__StreamEncoder *encoder);
53 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
54 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder);
55 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
56 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
57 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__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
58 static FLAC__bool add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
59 static unsigned evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
60 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);
61 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);
62 static unsigned evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
63 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, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]);
64 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);
65 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);
66 #ifdef DONT_ESTIMATE_RICE_BITS
67 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, unsigned parameters[], unsigned *bits);
68 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, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
70 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, unsigned parameters[], unsigned *bits);
71 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, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
73 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
75 /***********************************************************************
79 ***********************************************************************/
81 typedef struct FLAC__StreamEncoderPrivate {
82 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
83 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
84 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
85 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
86 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
87 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
88 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
89 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
90 FLAC__int32 *residual_workspace_mid_side[2][2];
91 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
92 FLAC__Subframe subframe_workspace_mid_side[2][2];
93 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
94 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
95 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
96 unsigned best_subframe_mid_side[2];
97 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
98 unsigned best_subframe_bits_mid_side[2];
99 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
100 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
101 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
102 FLAC__BitBuffer *frame; /* the current frame being worked on */
103 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
104 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
105 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
106 FLAC__ChannelAssignment last_channel_assignment;
107 FLAC__StreamMetadata metadata;
108 unsigned current_sample_number;
109 unsigned current_frame_number;
110 struct MD5Context md5context;
111 FLAC__CPUInfo cpuinfo;
112 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
113 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
114 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[]);
115 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[]);
116 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
117 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
118 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
119 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
120 FLAC__StreamEncoderWriteCallback write_callback;
121 FLAC__StreamEncoderMetadataCallback metadata_callback;
123 /* unaligned (original) pointers to allocated data */
124 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
125 FLAC__int32 *integer_signal_mid_side_unaligned[2];
126 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
127 FLAC__real *real_signal_mid_side_unaligned[2];
128 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
129 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
130 FLAC__uint32 *abs_residual_unaligned;
131 FLAC__uint64 *abs_residual_partition_sums_unaligned;
132 unsigned *raw_bits_per_partition_unaligned;
134 * These fields have been moved here from private function local
135 * declarations merely to save stack space during encoding.
137 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
138 unsigned parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER]; /* from find_best_partition_order_() */
139 } FLAC__StreamEncoderPrivate;
141 /***********************************************************************
143 * Public static class data
145 ***********************************************************************/
147 const char * const FLAC__StreamEncoderStateString[] = {
148 "FLAC__STREAM_ENCODER_OK",
149 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
150 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
151 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
152 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
153 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
154 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
155 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
156 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
157 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
158 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
159 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
160 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
161 "FLAC__STREAM_ENCODER_INVALID_METADATA",
162 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
163 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
164 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
165 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
166 "FLAC__STREAM_ENCODER_UNINITIALIZED"
169 const char * const FLAC__StreamEncoderWriteStatusString[] = {
170 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
171 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
174 /***********************************************************************
176 * Class constructor/destructor
178 ***********************************************************************/
179 FLAC__StreamEncoder *FLAC__stream_encoder_new()
181 FLAC__StreamEncoder *encoder;
183 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
185 encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
189 encoder->protected_ = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
190 if(encoder->protected_ == 0) {
194 encoder->private_ = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
195 if(encoder->private_ == 0) {
196 free(encoder->protected_);
200 encoder->private_->frame = FLAC__bitbuffer_new();
201 if(encoder->private_->frame == 0) {
202 free(encoder->private_);
203 free(encoder->protected_);
208 set_defaults_(encoder);
210 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
215 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
217 FLAC__ASSERT(0 != encoder);
218 FLAC__ASSERT(0 != encoder->protected_);
219 FLAC__ASSERT(0 != encoder->private_);
220 FLAC__ASSERT(0 != encoder->private_->frame);
223 FLAC__bitbuffer_delete(encoder->private_->frame);
224 free(encoder->private_);
225 free(encoder->protected_);
229 /***********************************************************************
231 * Public class methods
233 ***********************************************************************/
235 FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
239 FLAC__ASSERT(0 != encoder);
241 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
242 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
244 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
246 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
247 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
249 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
250 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
252 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
253 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
255 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
256 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
258 if(encoder->protected_->bits_per_sample >= 32)
259 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
261 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
262 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
264 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
265 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
267 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
268 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
270 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
271 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
273 if(encoder->protected_->qlp_coeff_precision == 0) {
274 if(encoder->protected_->bits_per_sample < 16) {
275 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
276 /* @@@ until then we'll make a guess */
277 encoder->protected_->qlp_coeff_precision = max(5, 2 + encoder->protected_->bits_per_sample / 2);
279 else if(encoder->protected_->bits_per_sample == 16) {
280 if(encoder->protected_->blocksize <= 192)
281 encoder->protected_->qlp_coeff_precision = 7;
282 else if(encoder->protected_->blocksize <= 384)
283 encoder->protected_->qlp_coeff_precision = 8;
284 else if(encoder->protected_->blocksize <= 576)
285 encoder->protected_->qlp_coeff_precision = 9;
286 else if(encoder->protected_->blocksize <= 1152)
287 encoder->protected_->qlp_coeff_precision = 10;
288 else if(encoder->protected_->blocksize <= 2304)
289 encoder->protected_->qlp_coeff_precision = 11;
290 else if(encoder->protected_->blocksize <= 4608)
291 encoder->protected_->qlp_coeff_precision = 12;
293 encoder->protected_->qlp_coeff_precision = 13;
296 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 */
299 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))
300 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
302 if(encoder->protected_->streamable_subset) {
303 /*@@@ add check for blocksize here */
304 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)
305 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
306 if(encoder->protected_->sample_rate > 655350)
307 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
310 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
311 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
312 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
313 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
315 /* validate metadata */
316 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
317 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
318 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
319 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
320 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
321 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
322 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
323 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
327 encoder->private_->input_capacity = 0;
328 for(i = 0; i < encoder->protected_->channels; i++) {
329 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
330 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
332 for(i = 0; i < 2; i++) {
333 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
334 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
336 for(i = 0; i < encoder->protected_->channels; i++) {
337 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
338 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
339 encoder->private_->best_subframe[i] = 0;
341 for(i = 0; i < 2; i++) {
342 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
343 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
344 encoder->private_->best_subframe_mid_side[i] = 0;
346 for(i = 0; i < encoder->protected_->channels; i++) {
347 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
348 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
350 for(i = 0; i < 2; i++) {
351 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
352 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
354 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
355 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
356 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
357 encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
358 encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
359 if(encoder->private_->loose_mid_side_stereo_frames == 0)
360 encoder->private_->loose_mid_side_stereo_frames = 1;
361 encoder->private_->loose_mid_side_stereo_frame_count = 0;
362 encoder->private_->current_sample_number = 0;
363 encoder->private_->current_frame_number = 0;
365 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
366 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? */
367 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
370 * get the CPU info and set the function pointers
372 FLAC__cpu_info(&encoder->private_->cpuinfo);
373 /* first default to the non-asm routines */
374 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
375 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
376 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
377 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
378 /* now override with asm where appropriate */
380 if(encoder->private_->cpuinfo.use_asm) {
381 #ifdef FLAC__CPU_IA32
382 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
383 #ifdef FLAC__HAS_NASM
384 if(0 && encoder->private_->cpuinfo.data.ia32.sse) {
385 if(encoder->protected_->max_lpc_order < 4)
386 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
387 else if(encoder->protected_->max_lpc_order < 8)
388 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
389 else if(encoder->protected_->max_lpc_order < 12)
390 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
392 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
394 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
395 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
397 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
398 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
399 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
400 if(encoder->private_->cpuinfo.data.ia32.mmx) {
401 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
402 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
405 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
406 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
412 /* finally override based on wide-ness if necessary */
413 if(encoder->private_->use_wide_by_block) {
414 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
417 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
418 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
420 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
421 /* the above function sets the state for us in case of an error */
422 return encoder->protected_->state;
425 if(!FLAC__bitbuffer_init(encoder->private_->frame))
426 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
429 * write the stream header
431 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
432 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
433 if(!write_bitbuffer_(encoder))
434 return encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
437 * write the STREAMINFO metadata block
439 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
440 encoder->private_->metadata.is_last = (encoder->protected_->num_metadata_blocks == 0);
441 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
442 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
443 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
444 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
445 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
446 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
447 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
448 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
449 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
450 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
451 MD5Init(&encoder->private_->md5context);
452 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
453 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
456 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
457 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
458 if(!write_bitbuffer_(encoder))
459 return encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
462 * Now that the STREAMINFO block is written, we can init this to an
463 * absurdly-high value...
465 encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
466 /* ... and clear this to 0 */
467 encoder->private_->metadata.data.stream_info.total_samples = 0;
470 * write the user's metadata blocks
472 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
473 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
474 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
475 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
478 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
479 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
480 if(!write_bitbuffer_(encoder))
481 return encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
484 return encoder->protected_->state;
487 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
489 FLAC__ASSERT(0 != encoder);
490 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
492 if(encoder->private_->current_sample_number != 0) {
493 encoder->protected_->blocksize = encoder->private_->current_sample_number;
494 process_frame_(encoder, true); /* true => is last frame */
496 MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
497 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
500 set_defaults_(encoder);
502 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
505 FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
507 FLAC__ASSERT(0 != encoder);
508 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
510 encoder->protected_->streamable_subset = value;
514 FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
516 FLAC__ASSERT(0 != encoder);
517 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
519 encoder->protected_->do_mid_side_stereo = value;
523 FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
525 FLAC__ASSERT(0 != encoder);
526 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
528 encoder->protected_->loose_mid_side_stereo = value;
532 FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
534 FLAC__ASSERT(0 != encoder);
535 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
537 encoder->protected_->channels = value;
541 FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
543 FLAC__ASSERT(0 != encoder);
544 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
546 encoder->protected_->bits_per_sample = value;
550 FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
552 FLAC__ASSERT(0 != encoder);
553 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
555 encoder->protected_->sample_rate = value;
559 FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
561 FLAC__ASSERT(0 != encoder);
562 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
564 encoder->protected_->blocksize = value;
568 FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
570 FLAC__ASSERT(0 != encoder);
571 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
573 encoder->protected_->max_lpc_order = value;
577 FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
579 FLAC__ASSERT(0 != encoder);
580 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
582 encoder->protected_->qlp_coeff_precision = value;
586 FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
588 FLAC__ASSERT(0 != encoder);
589 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
591 encoder->protected_->do_qlp_coeff_prec_search = value;
595 FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
597 FLAC__ASSERT(0 != encoder);
598 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
602 encoder->protected_->do_escape_coding = value;
609 FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
611 FLAC__ASSERT(0 != encoder);
612 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
614 encoder->protected_->do_exhaustive_model_search = value;
618 FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
620 FLAC__ASSERT(0 != encoder);
621 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
623 encoder->protected_->min_residual_partition_order = value;
627 FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
629 FLAC__ASSERT(0 != encoder);
630 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
632 encoder->protected_->max_residual_partition_order = value;
636 FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
638 FLAC__ASSERT(0 != encoder);
639 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
643 encoder->protected_->rice_parameter_search_dist = value;
650 FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
652 FLAC__ASSERT(0 != encoder);
653 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
655 encoder->protected_->total_samples_estimate = value;
659 FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
661 FLAC__ASSERT(0 != encoder);
662 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
664 encoder->protected_->metadata = metadata;
665 encoder->protected_->num_metadata_blocks = num_blocks;
669 FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
671 FLAC__ASSERT(0 != encoder);
672 FLAC__ASSERT(0 != value);
673 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
675 encoder->private_->write_callback = value;
679 FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
681 FLAC__ASSERT(0 != encoder);
682 FLAC__ASSERT(0 != value);
683 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
685 encoder->private_->metadata_callback = value;
689 FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
691 FLAC__ASSERT(0 != encoder);
692 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
694 encoder->private_->client_data = value;
698 FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
700 FLAC__ASSERT(0 != encoder);
701 return encoder->protected_->state;
704 FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
706 FLAC__ASSERT(0 != encoder);
707 return encoder->protected_->streamable_subset;
710 FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
712 FLAC__ASSERT(0 != encoder);
713 return encoder->protected_->do_mid_side_stereo;
716 FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
718 FLAC__ASSERT(0 != encoder);
719 return encoder->protected_->loose_mid_side_stereo;
722 unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
724 FLAC__ASSERT(0 != encoder);
725 return encoder->protected_->channels;
728 unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
730 FLAC__ASSERT(0 != encoder);
731 return encoder->protected_->bits_per_sample;
734 unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
736 FLAC__ASSERT(0 != encoder);
737 return encoder->protected_->sample_rate;
740 unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
742 FLAC__ASSERT(0 != encoder);
743 return encoder->protected_->blocksize;
746 unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
748 FLAC__ASSERT(0 != encoder);
749 return encoder->protected_->max_lpc_order;
752 unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
754 FLAC__ASSERT(0 != encoder);
755 return encoder->protected_->qlp_coeff_precision;
758 FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
760 FLAC__ASSERT(0 != encoder);
761 return encoder->protected_->do_qlp_coeff_prec_search;
764 FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
766 FLAC__ASSERT(0 != encoder);
767 return encoder->protected_->do_escape_coding;
770 FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
772 FLAC__ASSERT(0 != encoder);
773 return encoder->protected_->do_exhaustive_model_search;
776 unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
778 FLAC__ASSERT(0 != encoder);
779 return encoder->protected_->min_residual_partition_order;
782 unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
784 FLAC__ASSERT(0 != encoder);
785 return encoder->protected_->max_residual_partition_order;
788 unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
790 FLAC__ASSERT(0 != encoder);
791 return encoder->protected_->rice_parameter_search_dist;
794 FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
796 unsigned i, j, channel;
797 FLAC__int32 x, mid, side;
798 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
800 FLAC__ASSERT(0 != encoder);
801 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
804 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
806 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
807 x = mid = side = buffer[0][j];
808 encoder->private_->integer_signal[0][i] = x;
809 encoder->private_->real_signal[0][i] = (FLAC__real)x;
811 encoder->private_->integer_signal[1][i] = x;
812 encoder->private_->real_signal[1][i] = (FLAC__real)x;
815 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
816 encoder->private_->integer_signal_mid_side[1][i] = side;
817 encoder->private_->integer_signal_mid_side[0][i] = mid;
818 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
819 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
820 encoder->private_->current_sample_number++;
823 if(!process_frame_(encoder, false)) /* false => not last frame */
826 } while(j < samples);
830 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
831 for(channel = 0; channel < channels; channel++) {
832 x = buffer[channel][j];
833 encoder->private_->integer_signal[channel][i] = x;
834 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
836 encoder->private_->current_sample_number++;
839 if(!process_frame_(encoder, false)) /* false => not last frame */
842 } while(j < samples);
848 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
849 FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
851 unsigned i, j, k, channel;
852 FLAC__int32 x, mid, side;
853 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
855 FLAC__ASSERT(0 != encoder);
856 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
859 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
861 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
862 x = mid = side = buffer[k++];
863 encoder->private_->integer_signal[0][i] = x;
864 encoder->private_->real_signal[0][i] = (FLAC__real)x;
866 encoder->private_->integer_signal[1][i] = x;
867 encoder->private_->real_signal[1][i] = (FLAC__real)x;
870 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
871 encoder->private_->integer_signal_mid_side[1][i] = side;
872 encoder->private_->integer_signal_mid_side[0][i] = mid;
873 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
874 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
875 encoder->private_->current_sample_number++;
878 if(!process_frame_(encoder, false)) /* false => not last frame */
881 } while(j < samples);
885 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
886 for(channel = 0; channel < channels; channel++) {
888 encoder->private_->integer_signal[channel][i] = x;
889 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
891 encoder->private_->current_sample_number++;
894 if(!process_frame_(encoder, false)) /* false => not last frame */
897 } while(j < samples);
903 /***********************************************************************
905 * Private class methods
907 ***********************************************************************/
909 void set_defaults_(FLAC__StreamEncoder *encoder)
911 FLAC__ASSERT(0 != encoder);
913 encoder->protected_->streamable_subset = true;
914 encoder->protected_->do_mid_side_stereo = false;
915 encoder->protected_->loose_mid_side_stereo = false;
916 encoder->protected_->channels = 2;
917 encoder->protected_->bits_per_sample = 16;
918 encoder->protected_->sample_rate = 44100;
919 encoder->protected_->blocksize = 1152;
920 encoder->protected_->max_lpc_order = 0;
921 encoder->protected_->qlp_coeff_precision = 0;
922 encoder->protected_->do_qlp_coeff_prec_search = false;
923 encoder->protected_->do_exhaustive_model_search = false;
924 encoder->protected_->do_escape_coding = false;
925 encoder->protected_->min_residual_partition_order = 0;
926 encoder->protected_->max_residual_partition_order = 0;
927 encoder->protected_->rice_parameter_search_dist = 0;
928 encoder->protected_->total_samples_estimate = 0;
929 encoder->protected_->metadata = 0;
930 encoder->protected_->num_metadata_blocks = 0;
932 encoder->private_->write_callback = 0;
933 encoder->private_->metadata_callback = 0;
934 encoder->private_->client_data = 0;
937 void free_(FLAC__StreamEncoder *encoder)
941 FLAC__ASSERT(0 != encoder);
942 for(i = 0; i < encoder->protected_->channels; i++) {
943 if(0 != encoder->private_->integer_signal_unaligned[i]) {
944 free(encoder->private_->integer_signal_unaligned[i]);
945 encoder->private_->integer_signal_unaligned[i] = 0;
947 if(0 != encoder->private_->real_signal_unaligned[i]) {
948 free(encoder->private_->real_signal_unaligned[i]);
949 encoder->private_->real_signal_unaligned[i] = 0;
952 for(i = 0; i < 2; i++) {
953 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
954 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
955 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
957 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
958 free(encoder->private_->real_signal_mid_side_unaligned[i]);
959 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
962 for(channel = 0; channel < encoder->protected_->channels; channel++) {
963 for(i = 0; i < 2; i++) {
964 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
965 free(encoder->private_->residual_workspace_unaligned[channel][i]);
966 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
970 for(channel = 0; channel < 2; channel++) {
971 for(i = 0; i < 2; i++) {
972 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
973 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
974 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
978 if(0 != encoder->private_->abs_residual_unaligned) {
979 free(encoder->private_->abs_residual_unaligned);
980 encoder->private_->abs_residual_unaligned = 0;
982 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
983 free(encoder->private_->abs_residual_partition_sums_unaligned);
984 encoder->private_->abs_residual_partition_sums_unaligned = 0;
986 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
987 free(encoder->private_->raw_bits_per_partition_unaligned);
988 encoder->private_->raw_bits_per_partition_unaligned = 0;
990 FLAC__bitbuffer_free(encoder->private_->frame);
993 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
998 FLAC__ASSERT(new_size > 0);
999 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1000 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1002 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1003 if(new_size <= encoder->private_->input_capacity)
1008 /* 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. */
1010 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1011 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1012 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1013 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1014 encoder->private_->integer_signal[i] += 4;
1016 for(i = 0; ok && i < 2; i++) {
1017 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]);
1018 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]);
1019 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1020 encoder->private_->integer_signal_mid_side[i] += 4;
1022 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1023 for(i = 0; ok && i < 2; i++) {
1024 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1027 for(channel = 0; ok && channel < 2; channel++) {
1028 for(i = 0; ok && i < 2; i++) {
1029 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]);
1032 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1033 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 */
1034 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1035 if(encoder->protected_->do_escape_coding)
1036 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1039 encoder->private_->input_capacity = new_size;
1041 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1046 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder)
1048 const FLAC__byte *buffer;
1051 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1053 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1055 if(encoder->private_->write_callback(encoder, buffer, bytes, 0, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
1058 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1063 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1065 const FLAC__byte *buffer;
1068 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1071 * Accumulate raw signal to the MD5 signature
1073 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)) {
1074 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1079 * Process the frame header and subframes into the frame bitbuffer
1081 if(!process_subframes_(encoder, is_last_frame)) {
1082 /* the above function sets the state for us in case of an error */
1087 * Zero-pad the frame to a byte_boundary
1089 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1090 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1095 * CRC-16 the whole thing
1097 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1098 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1103 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1104 if(encoder->private_->write_callback(encoder, buffer, bytes, encoder->protected_->blocksize, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1105 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1108 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1111 * Get ready for the next frame
1113 encoder->private_->current_sample_number = 0;
1114 encoder->private_->current_frame_number++;
1115 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1116 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1117 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1122 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1124 FLAC__FrameHeader frame_header;
1125 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1126 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1129 * Calculate the min,max Rice partition orders
1132 max_partition_order = 0;
1135 unsigned limit = 0, b = encoder->protected_->blocksize;
1140 max_partition_order = min(encoder->protected_->max_residual_partition_order, limit);
1142 min_partition_order = min(min_partition_order, max_partition_order);
1144 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1149 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1150 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1153 frame_header.blocksize = encoder->protected_->blocksize;
1154 frame_header.sample_rate = encoder->protected_->sample_rate;
1155 frame_header.channels = encoder->protected_->channels;
1156 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1157 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1158 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1159 frame_header.number.frame_number = encoder->private_->current_frame_number;
1162 * Figure out what channel assignments to try
1164 if(encoder->protected_->do_mid_side_stereo) {
1165 if(encoder->protected_->loose_mid_side_stereo) {
1166 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1167 do_independent = true;
1171 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1172 do_mid_side = !do_independent;
1176 do_independent = true;
1181 do_independent = true;
1182 do_mid_side = false;
1185 FLAC__ASSERT(do_independent || do_mid_side);
1188 * Check for wasted bits; set effective bps for each subframe
1190 if(do_independent) {
1192 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1193 w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1194 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1195 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1200 FLAC__ASSERT(encoder->protected_->channels == 2);
1201 for(channel = 0; channel < 2; channel++) {
1202 w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1203 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1204 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1209 * First do a normal encoding pass of each independent channel
1211 if(do_independent) {
1212 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1213 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_->residual_workspace[channel], encoder->private_->best_subframe+channel, encoder->private_->best_subframe_bits+channel))
1219 * Now do mid and side channels if requested
1222 FLAC__ASSERT(encoder->protected_->channels == 2);
1224 for(channel = 0; channel < 2; channel++) {
1225 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_->residual_workspace_mid_side[channel], encoder->private_->best_subframe_mid_side+channel, encoder->private_->best_subframe_bits_mid_side+channel))
1231 * Compose the frame bitbuffer
1234 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1235 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1236 FLAC__ChannelAssignment channel_assignment;
1238 FLAC__ASSERT(encoder->protected_->channels == 2);
1240 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1241 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1244 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1246 FLAC__ChannelAssignment ca;
1248 FLAC__ASSERT(do_independent && do_mid_side);
1250 /* We have to figure out which channel assignent results in the smallest frame */
1251 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
1252 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
1253 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
1254 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
1256 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
1257 if(bits[ca] < min_bits) {
1258 min_bits = bits[ca];
1259 channel_assignment = ca;
1264 frame_header.channel_assignment = channel_assignment;
1266 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1267 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1271 switch(channel_assignment) {
1272 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1273 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1274 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1276 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1277 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1278 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1280 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1281 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1282 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1284 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1285 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1286 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1292 switch(channel_assignment) {
1293 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1294 left_bps = encoder->private_->subframe_bps [0];
1295 right_bps = encoder->private_->subframe_bps [1];
1297 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1298 left_bps = encoder->private_->subframe_bps [0];
1299 right_bps = encoder->private_->subframe_bps_mid_side[1];
1301 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1302 left_bps = encoder->private_->subframe_bps_mid_side[1];
1303 right_bps = encoder->private_->subframe_bps [1];
1305 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1306 left_bps = encoder->private_->subframe_bps_mid_side[0];
1307 right_bps = encoder->private_->subframe_bps_mid_side[1];
1313 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1314 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
1316 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1320 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1321 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1325 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1326 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
1327 /* the above function sets the state for us in case of an error */
1333 if(encoder->protected_->loose_mid_side_stereo) {
1334 encoder->private_->loose_mid_side_stereo_frame_count++;
1335 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1336 encoder->private_->loose_mid_side_stereo_frame_count = 0;
1339 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1344 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__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
1346 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1347 FLAC__real lpc_residual_bits_per_sample;
1348 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 */
1349 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1350 unsigned min_lpc_order, max_lpc_order, lpc_order;
1351 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1352 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1353 unsigned rice_parameter;
1354 unsigned _candidate_bits, _best_bits;
1355 unsigned _best_subframe;
1357 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1359 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1361 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1362 /* check for constant subframe */
1363 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);
1364 if(fixed_residual_bits_per_sample[1] == 0.0) {
1365 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1366 unsigned i, signal_is_constant = true;
1367 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1368 if(integer_signal[0] != integer_signal[i]) {
1369 signal_is_constant = false;
1373 if(signal_is_constant) {
1374 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1375 if(_candidate_bits < _best_bits) {
1376 _best_subframe = !_best_subframe;
1377 _best_bits = _candidate_bits;
1383 if(encoder->protected_->do_exhaustive_model_search) {
1384 min_fixed_order = 0;
1385 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1388 min_fixed_order = max_fixed_order = guess_fixed_order;
1390 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1391 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1392 continue; /* don't even try */
1393 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 */
1394 #ifndef FLAC__SYMMETRIC_RICE
1395 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1397 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1398 #ifdef DEBUG_VERBOSE
1399 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1401 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1403 _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]);
1404 if(_candidate_bits < _best_bits) {
1405 _best_subframe = !_best_subframe;
1406 _best_bits = _candidate_bits;
1411 if(encoder->protected_->max_lpc_order > 0) {
1412 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
1413 max_lpc_order = frame_header->blocksize-1;
1415 max_lpc_order = encoder->protected_->max_lpc_order;
1416 if(max_lpc_order > 0) {
1417 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
1418 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
1419 if(autoc[0] != 0.0) {
1420 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
1421 if(encoder->protected_->do_exhaustive_model_search) {
1425 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
1426 min_lpc_order = max_lpc_order = guess_lpc_order;
1428 if(encoder->protected_->do_qlp_coeff_prec_search) {
1429 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
1430 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 */
1433 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
1435 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
1436 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
1437 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
1438 continue; /* don't even try */
1439 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
1440 #ifndef FLAC__SYMMETRIC_RICE
1441 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1443 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1444 #ifdef DEBUG_VERBOSE
1445 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1447 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1449 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
1450 _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]);
1451 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1452 if(_candidate_bits < _best_bits) {
1453 _best_subframe = !_best_subframe;
1454 _best_bits = _candidate_bits;
1465 *best_subframe = _best_subframe;
1466 *best_bits = _best_bits;
1471 FLAC__bool add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
1473 switch(subframe->type) {
1474 case FLAC__SUBFRAME_TYPE_CONSTANT:
1475 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
1476 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1480 case FLAC__SUBFRAME_TYPE_FIXED:
1481 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
1482 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1486 case FLAC__SUBFRAME_TYPE_LPC:
1487 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
1488 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1492 case FLAC__SUBFRAME_TYPE_VERBATIM:
1493 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
1494 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1505 unsigned evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
1507 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1508 subframe->data.constant.value = signal;
1510 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
1513 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)
1515 unsigned i, residual_bits;
1516 const unsigned residual_samples = blocksize - order;
1518 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1520 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1522 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1523 subframe->data.fixed.residual = residual;
1525 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, &subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.raw_bits);
1527 subframe->data.fixed.order = order;
1528 for(i = 0; i < order; i++)
1529 subframe->data.fixed.warmup[i] = signal[i];
1531 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
1534 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)
1536 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1537 unsigned i, residual_bits;
1538 int quantization, ret;
1539 const unsigned residual_samples = blocksize - order;
1541 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
1543 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1545 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
1546 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1548 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1550 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1552 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1553 subframe->data.lpc.residual = residual;
1555 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, &subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.raw_bits);
1557 subframe->data.lpc.order = order;
1558 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1559 subframe->data.lpc.quantization_level = quantization;
1560 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
1561 for(i = 0; i < order; i++)
1562 subframe->data.lpc.warmup[i] = signal[i];
1564 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;
1567 unsigned evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
1569 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1571 subframe->data.verbatim.data = signal;
1573 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
1576 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, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[])
1579 unsigned residual_bits, best_residual_bits = 0;
1580 unsigned residual_sample;
1581 unsigned best_parameters_index = 0;
1582 const unsigned blocksize = residual_samples + predictor_order;
1584 /* compute abs(residual) for use later */
1585 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1586 r = residual[residual_sample];
1587 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
1590 while(max_partition_order > 0 && blocksize >> max_partition_order <= predictor_order)
1591 max_partition_order--;
1592 FLAC__ASSERT(blocksize >> max_partition_order > predictor_order);
1593 min_partition_order = min(min_partition_order, max_partition_order);
1595 if(precompute_partition_sums) {
1596 int partition_order;
1599 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
1601 if(do_escape_coding)
1602 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
1604 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1605 #ifdef DONT_ESTIMATE_RICE_BITS
1606 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_->parameters[!best_parameters_index], private_->raw_bits[!best_parameters_index], &residual_bits))
1608 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_->parameters[!best_parameters_index], private_->raw_bits[!best_parameters_index], &residual_bits))
1611 FLAC__ASSERT(best_residual_bits != 0);
1614 sum += 1u << partition_order;
1615 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1616 best_residual_bits = residual_bits;
1617 *best_partition_order = partition_order;
1618 best_parameters_index = !best_parameters_index;
1623 unsigned partition_order;
1624 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1625 #ifdef DONT_ESTIMATE_RICE_BITS
1626 if(!set_partitioned_rice_(abs_residual, residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, private_->parameters[!best_parameters_index], &residual_bits))
1628 if(!set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, private_->parameters[!best_parameters_index], &residual_bits))
1631 FLAC__ASSERT(best_residual_bits != 0);
1634 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1635 best_residual_bits = residual_bits;
1636 *best_partition_order = partition_order;
1637 best_parameters_index = !best_parameters_index;
1642 memcpy(best_parameters, private_->parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1643 memcpy(best_raw_bits, private_->raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1645 return best_residual_bits;
1648 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)
1650 int partition_order;
1651 unsigned from_partition, to_partition = 0;
1652 const unsigned blocksize = residual_samples + predictor_order;
1654 /* first do max_partition_order */
1655 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1656 FLAC__uint64 abs_residual_partition_sum;
1658 unsigned partition, partition_sample, partition_samples, residual_sample;
1659 const unsigned partitions = 1u << partition_order;
1660 const unsigned default_partition_samples = blocksize >> partition_order;
1662 FLAC__ASSERT(default_partition_samples > predictor_order);
1664 for(partition = residual_sample = 0; partition < partitions; partition++) {
1665 partition_samples = default_partition_samples;
1667 partition_samples -= predictor_order;
1668 abs_residual_partition_sum = 0;
1669 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1670 abs_r = abs_residual[residual_sample];
1671 abs_residual_partition_sum += abs_r;
1674 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
1676 to_partition = partitions;
1680 /* now merge partitions for lower orders */
1681 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1684 const unsigned partitions = 1u << partition_order;
1685 for(i = 0; i < partitions; i++) {
1686 s = abs_residual_partition_sums[from_partition];
1688 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1695 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)
1697 int partition_order;
1698 unsigned from_partition, to_partition = 0;
1699 const unsigned blocksize = residual_samples + predictor_order;
1701 /* first do max_partition_order */
1702 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1703 FLAC__int32 r, residual_partition_min, residual_partition_max;
1704 unsigned silog2_min, silog2_max;
1705 unsigned partition, partition_sample, partition_samples, residual_sample;
1706 const unsigned partitions = 1u << partition_order;
1707 const unsigned default_partition_samples = blocksize >> partition_order;
1709 FLAC__ASSERT(default_partition_samples > predictor_order);
1711 for(partition = residual_sample = 0; partition < partitions; partition++) {
1712 partition_samples = default_partition_samples;
1714 partition_samples -= predictor_order;
1715 residual_partition_min = residual_partition_max = 0;
1716 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1717 r = residual[residual_sample];
1718 if(r < residual_partition_min)
1719 residual_partition_min = r;
1720 else if(r > residual_partition_max)
1721 residual_partition_max = r;
1724 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
1725 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
1726 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
1728 to_partition = partitions;
1732 /* now merge partitions for lower orders */
1733 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1736 const unsigned partitions = 1u << partition_order;
1737 for(i = 0; i < partitions; i++) {
1738 m = raw_bits_per_partition[from_partition];
1740 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1747 #ifdef VARIABLE_RICE_BITS
1748 #undef VARIABLE_RICE_BITS
1750 #ifndef DONT_ESTIMATE_RICE_BITS
1751 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1754 #ifdef DONT_ESTIMATE_RICE_BITS
1755 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, unsigned parameters[], unsigned *bits)
1757 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, unsigned parameters[], unsigned *bits)
1760 unsigned rice_parameter, partition_bits;
1761 #ifndef NO_RICE_SEARCH
1762 unsigned best_partition_bits;
1763 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1765 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1767 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1769 if(partition_order == 0) {
1772 #ifndef NO_RICE_SEARCH
1773 if(rice_parameter_search_dist) {
1774 if(suggested_rice_parameter < rice_parameter_search_dist)
1775 min_rice_parameter = 0;
1777 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1778 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1779 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1780 #ifdef DEBUG_VERBOSE
1781 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1783 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1787 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1789 best_partition_bits = 0xffffffff;
1790 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1792 #ifdef VARIABLE_RICE_BITS
1793 #ifdef FLAC__SYMMETRIC_RICE
1794 partition_bits = (2+rice_parameter) * residual_samples;
1796 const unsigned rice_parameter_estimate = rice_parameter-1;
1797 partition_bits = (1+rice_parameter) * residual_samples;
1802 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1803 for(i = 0; i < residual_samples; i++) {
1804 #ifdef VARIABLE_RICE_BITS
1805 #ifdef FLAC__SYMMETRIC_RICE
1806 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1808 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1811 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
1814 #ifndef NO_RICE_SEARCH
1815 if(partition_bits < best_partition_bits) {
1816 best_rice_parameter = rice_parameter;
1817 best_partition_bits = partition_bits;
1821 parameters[0] = best_rice_parameter;
1822 bits_ += best_partition_bits;
1825 unsigned partition, residual_sample, save_residual_sample, partition_sample;
1826 unsigned partition_samples;
1827 FLAC__uint64 mean, k;
1828 const unsigned partitions = 1u << partition_order;
1829 for(partition = residual_sample = 0; partition < partitions; partition++) {
1830 partition_samples = (residual_samples+predictor_order) >> partition_order;
1831 if(partition == 0) {
1832 if(partition_samples <= predictor_order)
1835 partition_samples -= predictor_order;
1838 save_residual_sample = residual_sample;
1839 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
1840 mean += abs_residual[residual_sample];
1841 residual_sample = save_residual_sample;
1842 #ifdef FLAC__SYMMETRIC_RICE
1843 mean += partition_samples >> 1; /* for rounding effect */
1844 mean /= partition_samples;
1846 /* calc rice_parameter = floor(log2(mean)) */
1854 /* calc rice_parameter ala LOCO-I */
1855 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
1858 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1859 #ifdef DEBUG_VERBOSE
1860 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1862 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1865 #ifndef NO_RICE_SEARCH
1866 if(rice_parameter_search_dist) {
1867 if(rice_parameter < rice_parameter_search_dist)
1868 min_rice_parameter = 0;
1870 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1871 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
1872 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1873 #ifdef DEBUG_VERBOSE
1874 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1876 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1880 min_rice_parameter = max_rice_parameter = rice_parameter;
1882 best_partition_bits = 0xffffffff;
1883 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1885 #ifdef VARIABLE_RICE_BITS
1886 #ifdef FLAC__SYMMETRIC_RICE
1887 partition_bits = (2+rice_parameter) * partition_samples;
1889 const unsigned rice_parameter_estimate = rice_parameter-1;
1890 partition_bits = (1+rice_parameter) * partition_samples;
1895 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1896 save_residual_sample = residual_sample;
1897 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
1898 #ifdef VARIABLE_RICE_BITS
1899 #ifdef FLAC__SYMMETRIC_RICE
1900 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
1902 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
1905 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
1908 #ifndef NO_RICE_SEARCH
1909 if(rice_parameter != max_rice_parameter)
1910 residual_sample = save_residual_sample;
1911 if(partition_bits < best_partition_bits) {
1912 best_rice_parameter = rice_parameter;
1913 best_partition_bits = partition_bits;
1917 parameters[partition] = best_rice_parameter;
1918 bits_ += best_partition_bits;
1926 #ifdef DONT_ESTIMATE_RICE_BITS
1927 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, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
1929 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, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
1932 unsigned rice_parameter, partition_bits;
1933 #ifndef NO_RICE_SEARCH
1934 unsigned best_partition_bits;
1935 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1938 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1940 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1942 if(partition_order == 0) {
1945 #ifndef NO_RICE_SEARCH
1946 if(rice_parameter_search_dist) {
1947 if(suggested_rice_parameter < rice_parameter_search_dist)
1948 min_rice_parameter = 0;
1950 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1951 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1952 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1953 #ifdef DEBUG_VERBOSE
1954 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1956 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1960 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1962 best_partition_bits = 0xffffffff;
1963 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1965 #ifdef VARIABLE_RICE_BITS
1966 #ifdef FLAC__SYMMETRIC_RICE
1967 partition_bits = (2+rice_parameter) * residual_samples;
1969 const unsigned rice_parameter_estimate = rice_parameter-1;
1970 partition_bits = (1+rice_parameter) * residual_samples;
1975 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1976 for(i = 0; i < residual_samples; i++) {
1977 #ifdef VARIABLE_RICE_BITS
1978 #ifdef FLAC__SYMMETRIC_RICE
1979 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1981 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1984 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1987 #ifndef NO_RICE_SEARCH
1988 if(partition_bits < best_partition_bits) {
1989 best_rice_parameter = rice_parameter;
1990 best_partition_bits = partition_bits;
1994 if(search_for_escapes) {
1995 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;
1996 if(flat_bits <= best_partition_bits) {
1997 raw_bits[0] = raw_bits_per_partition[0];
1998 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1999 best_partition_bits = flat_bits;
2002 parameters[0] = best_rice_parameter;
2003 bits_ += best_partition_bits;
2006 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2007 unsigned partition_samples;
2008 FLAC__uint64 mean, k;
2009 const unsigned partitions = 1u << partition_order;
2010 for(partition = residual_sample = 0; partition < partitions; partition++) {
2011 partition_samples = (residual_samples+predictor_order) >> partition_order;
2012 if(partition == 0) {
2013 if(partition_samples <= predictor_order)
2016 partition_samples -= predictor_order;
2018 mean = abs_residual_partition_sums[partition];
2019 #ifdef FLAC__SYMMETRIC_RICE
2020 mean += partition_samples >> 1; /* for rounding effect */
2021 mean /= partition_samples;
2023 /* calc rice_parameter = floor(log2(mean)) */
2031 /* calc rice_parameter ala LOCO-I */
2032 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2035 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2036 #ifdef DEBUG_VERBOSE
2037 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2039 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2042 #ifndef NO_RICE_SEARCH
2043 if(rice_parameter_search_dist) {
2044 if(rice_parameter < rice_parameter_search_dist)
2045 min_rice_parameter = 0;
2047 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2048 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2049 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2050 #ifdef DEBUG_VERBOSE
2051 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2053 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2057 min_rice_parameter = max_rice_parameter = rice_parameter;
2059 best_partition_bits = 0xffffffff;
2060 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2062 #ifdef VARIABLE_RICE_BITS
2063 #ifdef FLAC__SYMMETRIC_RICE
2064 partition_bits = (2+rice_parameter) * partition_samples;
2066 const unsigned rice_parameter_estimate = rice_parameter-1;
2067 partition_bits = (1+rice_parameter) * partition_samples;
2072 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2073 save_residual_sample = residual_sample;
2074 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2075 #ifdef VARIABLE_RICE_BITS
2076 #ifdef FLAC__SYMMETRIC_RICE
2077 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2079 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2082 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2085 #ifndef NO_RICE_SEARCH
2086 if(rice_parameter != max_rice_parameter)
2087 residual_sample = save_residual_sample;
2088 if(partition_bits < best_partition_bits) {
2089 best_rice_parameter = rice_parameter;
2090 best_partition_bits = partition_bits;
2094 if(search_for_escapes) {
2095 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;
2096 if(flat_bits <= best_partition_bits) {
2097 raw_bits[partition] = raw_bits_per_partition[partition];
2098 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2099 best_partition_bits = flat_bits;
2102 parameters[partition] = best_rice_parameter;
2103 bits_ += best_partition_bits;
2111 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
2116 for(i = 0; i < samples && !(x&1); i++)
2123 for(shift = 0; !(x&1); shift++)
2128 for(i = 0; i < samples; i++)
2129 signal[i] >>= shift;