make typedefs for callbacks
[platform/upstream/flac.git] / src / libFLAC / stream_encoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <stdio.h>
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"
34
35 #ifdef min
36 #undef min
37 #endif
38 #define min(x,y) ((x)<(y)?(x):(y))
39
40 #ifdef max
41 #undef max
42 #endif
43 #define max(x,y) ((x)>(y)?(x):(y))
44
45 /***********************************************************************
46  *
47  * Private class method prototypes
48  *
49  ***********************************************************************/
50
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);
69 #else
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);
72 #endif
73 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
74
75 /***********************************************************************
76  *
77  * Private class data
78  *
79  ***********************************************************************/
80
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;
122         void *client_data;
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;
133         /*
134          * These fields have been moved here from private function local
135          * declarations merely to save stack space during encoding.
136          */
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;
140
141 /***********************************************************************
142  *
143  * Public static class data
144  *
145  ***********************************************************************/
146
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"
167 };
168
169 const char * const FLAC__StreamEncoderWriteStatusString[] = {
170         "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
171         "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
172 };
173
174 /***********************************************************************
175  *
176  * Class constructor/destructor
177  *
178  ***********************************************************************/
179 FLAC__StreamEncoder *FLAC__stream_encoder_new()
180 {
181         FLAC__StreamEncoder *encoder;
182
183         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
184
185         encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
186         if(encoder == 0) {
187                 return 0;
188         }
189         encoder->protected_ = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
190         if(encoder->protected_ == 0) {
191                 free(encoder);
192                 return 0;
193         }
194         encoder->private_ = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
195         if(encoder->private_ == 0) {
196                 free(encoder->protected_);
197                 free(encoder);
198                 return 0;
199         }
200         encoder->private_->frame = FLAC__bitbuffer_new();
201         if(encoder->private_->frame == 0) {
202                 free(encoder->private_);
203                 free(encoder->protected_);
204                 free(encoder);
205                 return 0;
206         }
207
208         set_defaults_(encoder);
209
210         encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
211
212         return encoder;
213 }
214
215 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
216 {
217         FLAC__ASSERT(0 != encoder);
218         FLAC__ASSERT(0 != encoder->protected_);
219         FLAC__ASSERT(0 != encoder->private_);
220         FLAC__ASSERT(0 != encoder->private_->frame);
221
222         free_(encoder);
223         FLAC__bitbuffer_delete(encoder->private_->frame);
224         free(encoder->private_);
225         free(encoder->protected_);
226         free(encoder);
227 }
228
229 /***********************************************************************
230  *
231  * Public class methods
232  *
233  ***********************************************************************/
234
235 FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
236 {
237         unsigned i;
238
239         FLAC__ASSERT(0 != encoder);
240
241         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
242                 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
243
244         encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
245
246         if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
247                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
248
249         if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
250                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
251
252         if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
253                 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
254
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;
257
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 */
260
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;
263
264         if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
265                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
266
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;
269
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;
272
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);
278                 }
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;
292                         else
293                                 encoder->protected_->qlp_coeff_precision = 13;
294                 }
295                 else {
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 */
297                 }
298         }
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;
301
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;
308         }
309
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;
314
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;
324                 }
325         }
326
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;
331         }
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;
335         }
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;
340         }
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;
345         }
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];
349         }
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];
353         }
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;
364
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 */
368
369         /*
370          * get the CPU info and set the function pointers
371          */
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 */
379 #ifndef FLAC__NO_ASM
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;
391                         else
392                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
393                 }
394                 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
395                         encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
396                 else
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;
403                 }
404                 else {
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;
407                 }
408 #endif
409 #endif
410         }
411 #endif
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;
415         }
416
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;
419
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;
423         }
424
425         if(!FLAC__bitbuffer_init(encoder->private_->frame))
426                 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
427
428         /*
429          * write the stream header
430          */
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;
435
436         /*
437          * write the STREAMINFO metadata block
438          */
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;
454                 return false;
455         }
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;
460
461         /*
462          * Now that the STREAMINFO block is written, we can init this to an
463          * absurdly-high value...
464          */
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;
468
469         /*
470          * write the user's metadata blocks
471          */
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;
476                         return false;
477                 }
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;
482         }
483
484         return encoder->protected_->state;
485 }
486
487 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
488 {
489         FLAC__ASSERT(0 != encoder);
490         if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
491                 return;
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 */
495         }
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);
498
499         free_(encoder);
500         set_defaults_(encoder);
501
502         encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
503 }
504
505 FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
506 {
507         FLAC__ASSERT(0 != encoder);
508         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
509                 return false;
510         encoder->protected_->streamable_subset = value;
511         return true;
512 }
513
514 FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
515 {
516         FLAC__ASSERT(0 != encoder);
517         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
518                 return false;
519         encoder->protected_->do_mid_side_stereo = value;
520         return true;
521 }
522
523 FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
524 {
525         FLAC__ASSERT(0 != encoder);
526         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
527                 return false;
528         encoder->protected_->loose_mid_side_stereo = value;
529         return true;
530 }
531
532 FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
533 {
534         FLAC__ASSERT(0 != encoder);
535         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
536                 return false;
537         encoder->protected_->channels = value;
538         return true;
539 }
540
541 FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
542 {
543         FLAC__ASSERT(0 != encoder);
544         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
545                 return false;
546         encoder->protected_->bits_per_sample = value;
547         return true;
548 }
549
550 FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
551 {
552         FLAC__ASSERT(0 != encoder);
553         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
554                 return false;
555         encoder->protected_->sample_rate = value;
556         return true;
557 }
558
559 FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
560 {
561         FLAC__ASSERT(0 != encoder);
562         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
563                 return false;
564         encoder->protected_->blocksize = value;
565         return true;
566 }
567
568 FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
569 {
570         FLAC__ASSERT(0 != encoder);
571         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
572                 return false;
573         encoder->protected_->max_lpc_order = value;
574         return true;
575 }
576
577 FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
578 {
579         FLAC__ASSERT(0 != encoder);
580         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
581                 return false;
582         encoder->protected_->qlp_coeff_precision = value;
583         return true;
584 }
585
586 FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
587 {
588         FLAC__ASSERT(0 != encoder);
589         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
590                 return false;
591         encoder->protected_->do_qlp_coeff_prec_search = value;
592         return true;
593 }
594
595 FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
596 {
597         FLAC__ASSERT(0 != encoder);
598         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
599                 return false;
600 #if 0
601         /*@@@ deprecated: */
602         encoder->protected_->do_escape_coding = value;
603 #else
604         (void)value;
605 #endif
606         return true;
607 }
608
609 FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
610 {
611         FLAC__ASSERT(0 != encoder);
612         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
613                 return false;
614         encoder->protected_->do_exhaustive_model_search = value;
615         return true;
616 }
617
618 FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
619 {
620         FLAC__ASSERT(0 != encoder);
621         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
622                 return false;
623         encoder->protected_->min_residual_partition_order = value;
624         return true;
625 }
626
627 FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
628 {
629         FLAC__ASSERT(0 != encoder);
630         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
631                 return false;
632         encoder->protected_->max_residual_partition_order = value;
633         return true;
634 }
635
636 FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
637 {
638         FLAC__ASSERT(0 != encoder);
639         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
640                 return false;
641 #if 0
642         /*@@@ deprecated: */
643         encoder->protected_->rice_parameter_search_dist = value;
644 #else
645         (void)value;
646 #endif
647         return true;
648 }
649
650 FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
651 {
652         FLAC__ASSERT(0 != encoder);
653         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
654                 return false;
655         encoder->protected_->total_samples_estimate = value;
656         return true;
657 }
658
659 FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
660 {
661         FLAC__ASSERT(0 != encoder);
662         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
663                 return false;
664         encoder->protected_->metadata = metadata;
665         encoder->protected_->num_metadata_blocks = num_blocks;
666         return true;
667 }
668
669 FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
670 {
671         FLAC__ASSERT(0 != encoder);
672         FLAC__ASSERT(0 != value);
673         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
674                 return false;
675         encoder->private_->write_callback = value;
676         return true;
677 }
678
679 FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
680 {
681         FLAC__ASSERT(0 != encoder);
682         FLAC__ASSERT(0 != value);
683         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
684                 return false;
685         encoder->private_->metadata_callback = value;
686         return true;
687 }
688
689 FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
690 {
691         FLAC__ASSERT(0 != encoder);
692         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
693                 return false;
694         encoder->private_->client_data = value;
695         return true;
696 }
697
698 FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
699 {
700         FLAC__ASSERT(0 != encoder);
701         return encoder->protected_->state;
702 }
703
704 FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
705 {
706         FLAC__ASSERT(0 != encoder);
707         return encoder->protected_->streamable_subset;
708 }
709
710 FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
711 {
712         FLAC__ASSERT(0 != encoder);
713         return encoder->protected_->do_mid_side_stereo;
714 }
715
716 FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
717 {
718         FLAC__ASSERT(0 != encoder);
719         return encoder->protected_->loose_mid_side_stereo;
720 }
721
722 unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
723 {
724         FLAC__ASSERT(0 != encoder);
725         return encoder->protected_->channels;
726 }
727
728 unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
729 {
730         FLAC__ASSERT(0 != encoder);
731         return encoder->protected_->bits_per_sample;
732 }
733
734 unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
735 {
736         FLAC__ASSERT(0 != encoder);
737         return encoder->protected_->sample_rate;
738 }
739
740 unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
741 {
742         FLAC__ASSERT(0 != encoder);
743         return encoder->protected_->blocksize;
744 }
745
746 unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
747 {
748         FLAC__ASSERT(0 != encoder);
749         return encoder->protected_->max_lpc_order;
750 }
751
752 unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
753 {
754         FLAC__ASSERT(0 != encoder);
755         return encoder->protected_->qlp_coeff_precision;
756 }
757
758 FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
759 {
760         FLAC__ASSERT(0 != encoder);
761         return encoder->protected_->do_qlp_coeff_prec_search;
762 }
763
764 FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
765 {
766         FLAC__ASSERT(0 != encoder);
767         return encoder->protected_->do_escape_coding;
768 }
769
770 FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
771 {
772         FLAC__ASSERT(0 != encoder);
773         return encoder->protected_->do_exhaustive_model_search;
774 }
775
776 unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
777 {
778         FLAC__ASSERT(0 != encoder);
779         return encoder->protected_->min_residual_partition_order;
780 }
781
782 unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
783 {
784         FLAC__ASSERT(0 != encoder);
785         return encoder->protected_->max_residual_partition_order;
786 }
787
788 unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
789 {
790         FLAC__ASSERT(0 != encoder);
791         return encoder->protected_->rice_parameter_search_dist;
792 }
793
794 FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
795 {
796         unsigned i, j, channel;
797         FLAC__int32 x, mid, side;
798         const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
799
800         FLAC__ASSERT(0 != encoder);
801         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
802
803         j = 0;
804         if(encoder->protected_->do_mid_side_stereo && channels == 2) {
805                 do {
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;
810                                 x = buffer[1][j];
811                                 encoder->private_->integer_signal[1][i] = x;
812                                 encoder->private_->real_signal[1][i] = (FLAC__real)x;
813                                 mid += x;
814                                 side -= 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++;
821                         }
822                         if(i == blocksize) {
823                                 if(!process_frame_(encoder, false)) /* false => not last frame */
824                                         return false;
825                         }
826                 } while(j < samples);
827         }
828         else {
829                 do {
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;
835                                 }
836                                 encoder->private_->current_sample_number++;
837                         }
838                         if(i == blocksize) {
839                                 if(!process_frame_(encoder, false)) /* false => not last frame */
840                                         return false;
841                         }
842                 } while(j < samples);
843         }
844
845         return true;
846 }
847
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)
850 {
851         unsigned i, j, k, channel;
852         FLAC__int32 x, mid, side;
853         const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
854
855         FLAC__ASSERT(0 != encoder);
856         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
857
858         j = k = 0;
859         if(encoder->protected_->do_mid_side_stereo && channels == 2) {
860                 do {
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;
865                                 x = buffer[k++];
866                                 encoder->private_->integer_signal[1][i] = x;
867                                 encoder->private_->real_signal[1][i] = (FLAC__real)x;
868                                 mid += x;
869                                 side -= 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++;
876                         }
877                         if(i == blocksize) {
878                                 if(!process_frame_(encoder, false)) /* false => not last frame */
879                                         return false;
880                         }
881                 } while(j < samples);
882         }
883         else {
884                 do {
885                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
886                                 for(channel = 0; channel < channels; channel++) {
887                                         x = buffer[k++];
888                                         encoder->private_->integer_signal[channel][i] = x;
889                                         encoder->private_->real_signal[channel][i] = (FLAC__real)x;
890                                 }
891                                 encoder->private_->current_sample_number++;
892                         }
893                         if(i == blocksize) {
894                                 if(!process_frame_(encoder, false)) /* false => not last frame */
895                                         return false;
896                         }
897                 } while(j < samples);
898         }
899
900         return true;
901 }
902
903 /***********************************************************************
904  *
905  * Private class methods
906  *
907  ***********************************************************************/
908
909 void set_defaults_(FLAC__StreamEncoder *encoder)
910 {
911         FLAC__ASSERT(0 != encoder);
912
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;
931
932         encoder->private_->write_callback = 0;
933         encoder->private_->metadata_callback = 0;
934         encoder->private_->client_data = 0;
935 }
936
937 void free_(FLAC__StreamEncoder *encoder)
938 {
939         unsigned i, channel;
940
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;
946                 }
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;
950                 }
951         }
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;
956                 }
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;
960                 }
961         }
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;
967                         }
968                 }
969         }
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;
975                         }
976                 }
977         }
978         if(0 != encoder->private_->abs_residual_unaligned) {
979                 free(encoder->private_->abs_residual_unaligned);
980                 encoder->private_->abs_residual_unaligned = 0;
981         }
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;
985         }
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;
989         }
990         FLAC__bitbuffer_free(encoder->private_->frame);
991 }
992
993 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
994 {
995         FLAC__bool ok;
996         unsigned i, channel;
997
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);
1001
1002         /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1003         if(new_size <= encoder->private_->input_capacity)
1004                 return true;
1005
1006         ok = true;
1007
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. */
1009
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;
1015         }
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;
1021         }
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]);
1025                 }
1026         }
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]);
1030                 }
1031         }
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);
1037
1038         if(ok)
1039                 encoder->private_->input_capacity = new_size;
1040         else
1041                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1042
1043         return ok;
1044 }
1045
1046 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder)
1047 {
1048         const FLAC__byte *buffer;
1049         unsigned bytes;
1050
1051         FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1052
1053         FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1054
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)
1056                 return false;
1057
1058         FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1059
1060         return true;
1061 }
1062
1063 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1064 {
1065         const FLAC__byte *buffer;
1066         unsigned bytes;
1067
1068         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1069
1070         /*
1071          * Accumulate raw signal to the MD5 signature
1072          */
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;
1075                 return false;
1076         }
1077
1078         /*
1079          * Process the frame header and subframes into the frame bitbuffer
1080          */
1081         if(!process_subframes_(encoder, is_last_frame)) {
1082                 /* the above function sets the state for us in case of an error */
1083                 return false;
1084         }
1085
1086         /*
1087          * Zero-pad the frame to a byte_boundary
1088          */
1089         if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1090                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1091                 return false;
1092         }
1093
1094         /*
1095          * CRC-16 the whole thing
1096          */
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);
1099
1100         /*
1101          * Write it
1102          */
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;
1106                 return false;
1107         }
1108         FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1109
1110         /*
1111          * Get ready for the next frame
1112          */
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);
1118
1119         return true;
1120 }
1121
1122 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1123 {
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;
1127
1128         /*
1129          * Calculate the min,max Rice partition orders
1130          */
1131         if(is_last_frame) {
1132                 max_partition_order = 0;
1133         }
1134         else {
1135                 unsigned limit = 0, b = encoder->protected_->blocksize;
1136                 while(!(b & 1)) {
1137                         limit++;
1138                         b >>= 1;
1139                 }
1140                 max_partition_order = min(encoder->protected_->max_residual_partition_order, limit);
1141         }
1142         min_partition_order = min(min_partition_order, max_partition_order);
1143
1144         precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1145
1146         /*
1147          * Setup the frame
1148          */
1149         if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1150                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1151                 return false;
1152         }
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;
1160
1161         /*
1162          * Figure out what channel assignments to try
1163          */
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;
1168                                 do_mid_side = true;
1169                         }
1170                         else {
1171                                 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1172                                 do_mid_side = !do_independent;
1173                         }
1174                 }
1175                 else {
1176                         do_independent = true;
1177                         do_mid_side = true;
1178                 }
1179         }
1180         else {
1181                 do_independent = true;
1182                 do_mid_side = false;
1183         }
1184
1185         FLAC__ASSERT(do_independent || do_mid_side);
1186
1187         /*
1188          * Check for wasted bits; set effective bps for each subframe
1189          */
1190         if(do_independent) {
1191                 unsigned w;
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;
1196                 }
1197         }
1198         if(do_mid_side) {
1199                 unsigned 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);
1205                 }
1206         }
1207
1208         /*
1209          * First do a normal encoding pass of each independent channel
1210          */
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))
1214                                 return false;
1215                 }
1216         }
1217
1218         /*
1219          * Now do mid and side channels if requested
1220          */
1221         if(do_mid_side) {
1222                 FLAC__ASSERT(encoder->protected_->channels == 2);
1223
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))
1226                                 return false;
1227                 }
1228         }
1229
1230         /*
1231          * Compose the frame bitbuffer
1232          */
1233         if(do_mid_side) {
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;
1237
1238                 FLAC__ASSERT(encoder->protected_->channels == 2);
1239
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);
1242                 }
1243                 else {
1244                         unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1245                         unsigned min_bits;
1246                         FLAC__ChannelAssignment ca;
1247
1248                         FLAC__ASSERT(do_independent && do_mid_side);
1249
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];
1255
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;
1260                                 }
1261                         }
1262                 }
1263
1264                 frame_header.channel_assignment = channel_assignment;
1265
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;
1268                         return false;
1269                 }
1270
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]];
1275                                 break;
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]];
1279                                 break;
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]];
1283                                 break;
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]];
1287                                 break;
1288                         default:
1289                                 FLAC__ASSERT(0);
1290                 }
1291
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];
1296                                 break;
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];
1300                                 break;
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];
1304                                 break;
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];
1308                                 break;
1309                         default:
1310                                 FLAC__ASSERT(0);
1311                 }
1312
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))
1315                         return false;
1316                 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1317                         return false;
1318         }
1319         else {
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;
1322                         return false;
1323                 }
1324
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 */
1328                                 return false;
1329                         }
1330                 }
1331         }
1332
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;
1337         }
1338
1339         encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1340
1341         return true;
1342 }
1343
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)
1345 {
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;
1356
1357         /* verbatim subframe is the baseline against which we measure other compressed subframes */
1358         _best_subframe = 0;
1359         _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1360
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;
1370                                         break;
1371                                 }
1372                         }
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;
1378                                 }
1379                         }
1380                 }
1381                 else {
1382                         /* encode fixed */
1383                         if(encoder->protected_->do_exhaustive_model_search) {
1384                                 min_fixed_order = 0;
1385                                 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1386                         }
1387                         else {
1388                                 min_fixed_order = max_fixed_order = guess_fixed_order;
1389                         }
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 */
1396 #endif
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);
1400 #endif
1401                                         rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1402                                 }
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;
1407                                 }
1408                         }
1409
1410                         /* encode lpc */
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;
1414                                 else
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) {
1422                                                         min_lpc_order = 1;
1423                                                 }
1424                                                 else {
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;
1427                                                 }
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 */
1431                                                 }
1432                                                 else {
1433                                                         min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
1434                                                 }
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 */
1442 #endif
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);
1446 #endif
1447                                                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1448                                                         }
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;
1455                                                                         }
1456                                                                 }
1457                                                         }
1458                                                 }
1459                                         }
1460                                 }
1461                         }
1462                 }
1463         }
1464
1465         *best_subframe = _best_subframe;
1466         *best_bits = _best_bits;
1467
1468         return true;
1469 }
1470
1471 FLAC__bool add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
1472 {
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;
1477                                 return false;
1478                         }
1479                         break;
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;
1483                                 return false;
1484                         }
1485                         break;
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;
1489                                 return false;
1490                         }
1491                         break;
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;
1495                                 return false;
1496                         }
1497                         break;
1498                 default:
1499                         FLAC__ASSERT(0);
1500         }
1501
1502         return true;
1503 }
1504
1505 unsigned evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
1506 {
1507         subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1508         subframe->data.constant.value = signal;
1509
1510         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
1511 }
1512
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)
1514 {
1515         unsigned i, residual_bits;
1516         const unsigned residual_samples = blocksize - order;
1517
1518         FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1519
1520         subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1521
1522         subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1523         subframe->data.fixed.residual = residual;
1524
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);
1526
1527         subframe->data.fixed.order = order;
1528         for(i = 0; i < order; i++)
1529                 subframe->data.fixed.warmup[i] = signal[i];
1530
1531         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
1532 }
1533
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)
1535 {
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;
1540
1541         ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
1542         if(ret != 0)
1543                 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1544
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);
1547         else
1548                 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1549
1550         subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1551
1552         subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1553         subframe->data.lpc.residual = residual;
1554
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);
1556
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];
1563
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;
1565 }
1566
1567 unsigned evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
1568 {
1569         subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1570
1571         subframe->data.verbatim.data = signal;
1572
1573         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
1574 }
1575
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[])
1577 {
1578         FLAC__int32 r;
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;
1583
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);
1588         }
1589
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);
1594
1595         if(precompute_partition_sums) {
1596                 int partition_order;
1597                 unsigned sum;
1598
1599                 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
1600
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);
1603
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))
1607 #else
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))
1609 #endif
1610                         {
1611                                 FLAC__ASSERT(best_residual_bits != 0);
1612                                 break;
1613                         }
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;
1619                         }
1620                 }
1621         }
1622         else {
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))
1627 #else
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))
1629 #endif
1630                         {
1631                                 FLAC__ASSERT(best_residual_bits != 0);
1632                                 break;
1633                         }
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;
1638                         }
1639                 }
1640         }
1641
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)));
1644
1645         return best_residual_bits;
1646 }
1647
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)
1649 {
1650         int partition_order;
1651         unsigned from_partition, to_partition = 0;
1652         const unsigned blocksize = residual_samples + predictor_order;
1653
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;
1657                 FLAC__uint32 abs_r;
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;
1661
1662                 FLAC__ASSERT(default_partition_samples > predictor_order);
1663
1664                 for(partition = residual_sample = 0; partition < partitions; partition++) {
1665                         partition_samples = default_partition_samples;
1666                         if(partition == 0)
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;
1672                                 residual_sample++;
1673                         }
1674                         abs_residual_partition_sums[partition] = abs_residual_partition_sum;
1675                 }
1676                 to_partition = partitions;
1677                 break;
1678         }
1679
1680         /* now merge partitions for lower orders */
1681         for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1682                 FLAC__uint64 s;
1683                 unsigned i;
1684                 const unsigned partitions = 1u << partition_order;
1685                 for(i = 0; i < partitions; i++) {
1686                         s = abs_residual_partition_sums[from_partition];
1687                         from_partition++;
1688                         abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1689                         from_partition++;
1690                         to_partition++;
1691                 }
1692         }
1693 }
1694
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)
1696 {
1697         int partition_order;
1698         unsigned from_partition, to_partition = 0;
1699         const unsigned blocksize = residual_samples + predictor_order;
1700
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;
1708
1709                 FLAC__ASSERT(default_partition_samples > predictor_order);
1710
1711                 for(partition = residual_sample = 0; partition < partitions; partition++) {
1712                         partition_samples = default_partition_samples;
1713                         if(partition == 0)
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;
1722                                 residual_sample++;
1723                         }
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);
1727                 }
1728                 to_partition = partitions;
1729                 break;
1730         }
1731
1732         /* now merge partitions for lower orders */
1733         for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1734                 unsigned m;
1735                 unsigned i;
1736                 const unsigned partitions = 1u << partition_order;
1737                 for(i = 0; i < partitions; i++) {
1738                         m = raw_bits_per_partition[from_partition];
1739                         from_partition++;
1740                         raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1741                         from_partition++;
1742                         to_partition++;
1743                 }
1744         }
1745 }
1746
1747 #ifdef VARIABLE_RICE_BITS
1748 #undef VARIABLE_RICE_BITS
1749 #endif
1750 #ifndef DONT_ESTIMATE_RICE_BITS
1751 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1752 #endif
1753
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)
1756 #else
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)
1758 #endif
1759 {
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;
1764 #endif
1765         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1766
1767         FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1768
1769         if(partition_order == 0) {
1770                 unsigned i;
1771
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;
1776                         else
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);
1782 #endif
1783                                 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1784                         }
1785                 }
1786                 else
1787                         min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1788
1789                 best_partition_bits = 0xffffffff;
1790                 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1791 #endif
1792 #ifdef VARIABLE_RICE_BITS
1793 #ifdef FLAC__SYMMETRIC_RICE
1794                         partition_bits = (2+rice_parameter) * residual_samples;
1795 #else
1796                         const unsigned rice_parameter_estimate = rice_parameter-1;
1797                         partition_bits = (1+rice_parameter) * residual_samples;
1798 #endif
1799 #else
1800                         partition_bits = 0;
1801 #endif
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);
1807 #else
1808                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1809 #endif
1810 #else
1811                                 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
1812 #endif
1813                         }
1814 #ifndef NO_RICE_SEARCH
1815                         if(partition_bits < best_partition_bits) {
1816                                 best_rice_parameter = rice_parameter;
1817                                 best_partition_bits = partition_bits;
1818                         }
1819                 }
1820 #endif
1821                 parameters[0] = best_rice_parameter;
1822                 bits_ += best_partition_bits;
1823         }
1824         else {
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)
1833                                         return false;
1834                                 else
1835                                         partition_samples -= predictor_order;
1836                         }
1837                         mean = 0;
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;
1845
1846                         /* calc rice_parameter = floor(log2(mean)) */
1847                         rice_parameter = 0;
1848                         mean>>=1;
1849                         while(mean) {
1850                                 rice_parameter++;
1851                                 mean >>= 1;
1852                         }
1853 #else
1854                         /* calc rice_parameter ala LOCO-I */
1855                         for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
1856                                 ;
1857 #endif
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);
1861 #endif
1862                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1863                         }
1864
1865 #ifndef NO_RICE_SEARCH
1866                         if(rice_parameter_search_dist) {
1867                                 if(rice_parameter < rice_parameter_search_dist)
1868                                         min_rice_parameter = 0;
1869                                 else
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);
1875 #endif
1876                                         max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1877                                 }
1878                         }
1879                         else
1880                                 min_rice_parameter = max_rice_parameter = rice_parameter;
1881
1882                         best_partition_bits = 0xffffffff;
1883                         for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1884 #endif
1885 #ifdef VARIABLE_RICE_BITS
1886 #ifdef FLAC__SYMMETRIC_RICE
1887                                 partition_bits = (2+rice_parameter) * partition_samples;
1888 #else
1889                                 const unsigned rice_parameter_estimate = rice_parameter-1;
1890                                 partition_bits = (1+rice_parameter) * partition_samples;
1891 #endif
1892 #else
1893                                 partition_bits = 0;
1894 #endif
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);
1901 #else
1902                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
1903 #endif
1904 #else
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[] */
1906 #endif
1907                                 }
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;
1914                                 }
1915                         }
1916 #endif
1917                         parameters[partition] = best_rice_parameter;
1918                         bits_ += best_partition_bits;
1919                 }
1920         }
1921
1922         *bits = bits_;
1923         return true;
1924 }
1925
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)
1928 #else
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)
1930 #endif
1931 {
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;
1936 #endif
1937         unsigned flat_bits;
1938         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1939
1940         FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1941
1942         if(partition_order == 0) {
1943                 unsigned i;
1944
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;
1949                         else
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);
1955 #endif
1956                                 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1957                         }
1958                 }
1959                 else
1960                         min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1961
1962                 best_partition_bits = 0xffffffff;
1963                 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1964 #endif
1965 #ifdef VARIABLE_RICE_BITS
1966 #ifdef FLAC__SYMMETRIC_RICE
1967                         partition_bits = (2+rice_parameter) * residual_samples;
1968 #else
1969                         const unsigned rice_parameter_estimate = rice_parameter-1;
1970                         partition_bits = (1+rice_parameter) * residual_samples;
1971 #endif
1972 #else
1973                         partition_bits = 0;
1974 #endif
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);
1980 #else
1981                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1982 #endif
1983 #else
1984                                 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1985 #endif
1986                         }
1987 #ifndef NO_RICE_SEARCH
1988                         if(partition_bits < best_partition_bits) {
1989                                 best_rice_parameter = rice_parameter;
1990                                 best_partition_bits = partition_bits;
1991                         }
1992                 }
1993 #endif
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;
2000                         }
2001                 }
2002                 parameters[0] = best_rice_parameter;
2003                 bits_ += best_partition_bits;
2004         }
2005         else {
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)
2014                                         return false;
2015                                 else
2016                                         partition_samples -= predictor_order;
2017                         }
2018                         mean = abs_residual_partition_sums[partition];
2019 #ifdef FLAC__SYMMETRIC_RICE
2020                         mean += partition_samples >> 1; /* for rounding effect */
2021                         mean /= partition_samples;
2022
2023                         /* calc rice_parameter = floor(log2(mean)) */
2024                         rice_parameter = 0;
2025                         mean>>=1;
2026                         while(mean) {
2027                                 rice_parameter++;
2028                                 mean >>= 1;
2029                         }
2030 #else
2031                         /* calc rice_parameter ala LOCO-I */
2032                         for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2033                                 ;
2034 #endif
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);
2038 #endif
2039                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2040                         }
2041
2042 #ifndef NO_RICE_SEARCH
2043                         if(rice_parameter_search_dist) {
2044                                 if(rice_parameter < rice_parameter_search_dist)
2045                                         min_rice_parameter = 0;
2046                                 else
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);
2052 #endif
2053                                         max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2054                                 }
2055                         }
2056                         else
2057                                 min_rice_parameter = max_rice_parameter = rice_parameter;
2058
2059                         best_partition_bits = 0xffffffff;
2060                         for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2061 #endif
2062 #ifdef VARIABLE_RICE_BITS
2063 #ifdef FLAC__SYMMETRIC_RICE
2064                                 partition_bits = (2+rice_parameter) * partition_samples;
2065 #else
2066                                 const unsigned rice_parameter_estimate = rice_parameter-1;
2067                                 partition_bits = (1+rice_parameter) * partition_samples;
2068 #endif
2069 #else
2070                                 partition_bits = 0;
2071 #endif
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);
2078 #else
2079                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2080 #endif
2081 #else
2082                                         partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2083 #endif
2084                                 }
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;
2091                                 }
2092                         }
2093 #endif
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;
2100                                 }
2101                         }
2102                         parameters[partition] = best_rice_parameter;
2103                         bits_ += best_partition_bits;
2104                 }
2105         }
2106
2107         *bits = bits_;
2108         return true;
2109 }
2110
2111 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
2112 {
2113         unsigned i, shift;
2114         FLAC__int32 x = 0;
2115
2116         for(i = 0; i < samples && !(x&1); i++)
2117                 x |= signal[i];
2118
2119         if(x == 0) {
2120                 shift = 0;
2121         }
2122         else {
2123                 for(shift = 0; !(x&1); shift++)
2124                         x >>= 1;
2125         }
2126
2127         if(shift > 0) {
2128                 for(i = 0; i < samples; i++)
2129                          signal[i] >>= shift;
2130         }
2131
2132         return shift;
2133 }