another big glob of changes/fixes
[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 "FLAC/metadata.h" /* for FLAC__metadata_object_seektable_is_legal() */
25 #include "protected/stream_encoder.h"
26 #include "private/bitbuffer.h"
27 #include "private/bitmath.h"
28 #include "private/crc.h"
29 #include "private/cpu.h"
30 #include "private/stream_encoder_framing.h"
31 #include "private/fixed.h"
32 #include "private/lpc.h"
33 #include "private/md5.h"
34 #include "private/memory.h"
35
36 #ifdef min
37 #undef min
38 #endif
39 #define min(x,y) ((x)<(y)?(x):(y))
40
41 #ifdef max
42 #undef max
43 #endif
44 #define max(x,y) ((x)>(y)?(x):(y))
45
46 /***********************************************************************
47  *
48  * Private class method prototypes
49  *
50  ***********************************************************************/
51
52 static FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
53 static FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
54 static FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
55 static FLAC__bool stream_encoder_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);
56 static FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
57 static unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
58 static unsigned stream_encoder_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);
59 static unsigned stream_encoder_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);
60 static unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
61 static unsigned stream_encoder_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[]);
62 static void stream_encoder_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);
63 static void stream_encoder_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);
64 #ifdef DONT_ESTIMATE_RICE_BITS
65 static FLAC__bool stream_encoder_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);
66 static FLAC__bool stream_encoder_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);
67 #else
68 static FLAC__bool stream_encoder_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);
69 static FLAC__bool stream_encoder_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);
70 #endif
71 static unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
72
73 /***********************************************************************
74  *
75  * Private class data
76  *
77  ***********************************************************************/
78
79 typedef struct FLAC__StreamEncoderPrivate {
80         unsigned input_capacity;                          /* current size (in samples) of the signal and residual buffers */
81         FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS];  /* the integer version of the input signal */
82         FLAC__int32 *integer_signal_mid_side[2];          /* the integer version of the mid-side input signal (stereo only) */
83         FLAC__real *real_signal[FLAC__MAX_CHANNELS];      /* the floating-point version of the input signal */
84         FLAC__real *real_signal_mid_side[2];              /* the floating-point version of the mid-side input signal (stereo only) */
85         unsigned subframe_bps[FLAC__MAX_CHANNELS];        /* the effective bits per sample of the input signal (stream bps - wasted bits) */
86         unsigned subframe_bps_mid_side[2];                /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
87         FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
88         FLAC__int32 *residual_workspace_mid_side[2][2];
89         FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
90         FLAC__Subframe subframe_workspace_mid_side[2][2];
91         FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
92         FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
93         unsigned best_subframe[FLAC__MAX_CHANNELS];       /* index into the above workspaces */
94         unsigned best_subframe_mid_side[2];
95         unsigned best_subframe_bits[FLAC__MAX_CHANNELS];  /* size in bits of the best subframe for each channel */
96         unsigned best_subframe_bits_mid_side[2];
97         FLAC__uint32 *abs_residual;                       /* workspace where abs(candidate residual) is stored */
98         FLAC__uint64 *abs_residual_partition_sums;        /* workspace where the sum of abs(candidate residual) for each partition is stored */
99         unsigned *raw_bits_per_partition;                 /* workspace where the sum of silog2(candidate residual) for each partition is stored */
100         FLAC__BitBuffer *frame;                           /* the current frame being worked on */
101         double loose_mid_side_stereo_frames_exact;        /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
102         unsigned loose_mid_side_stereo_frames;            /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
103         unsigned loose_mid_side_stereo_frame_count;       /* number of frames using the current channel assignment */
104         FLAC__ChannelAssignment last_channel_assignment;
105         FLAC__StreamMetadata metadata;
106         unsigned current_sample_number;
107         unsigned current_frame_number;
108         struct MD5Context md5context;
109         FLAC__CPUInfo cpuinfo;
110         unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
111         void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
112         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[]);
113         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[]);
114         FLAC__bool use_wide_by_block;                     /* use slow 64-bit versions of some functions because of the block size */
115         FLAC__bool use_wide_by_partition;                 /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
116         FLAC__bool use_wide_by_order;                     /* use slow 64-bit versions of some functions because of the lpc order */
117         FLAC__bool precompute_partition_sums;             /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
118         FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
119         void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
120         void *client_data;
121         /* unaligned (original) pointers to allocated data */
122         FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
123         FLAC__int32 *integer_signal_mid_side_unaligned[2];
124         FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
125         FLAC__real *real_signal_mid_side_unaligned[2];
126         FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
127         FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
128         FLAC__uint32 *abs_residual_unaligned;
129         FLAC__uint64 *abs_residual_partition_sums_unaligned;
130         unsigned *raw_bits_per_partition_unaligned;
131         /*
132          * These fields have been moved here from private function local
133          * declarations merely to save stack space during encoding.
134          */
135         FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from stream_encoder_process_subframe_() */
136         unsigned parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER]; /* from stream_encoder_find_best_partition_order_() */
137 } FLAC__StreamEncoderPrivate;
138
139 /***********************************************************************
140  *
141  * Public static class data
142  *
143  ***********************************************************************/
144
145 const char * const FLAC__StreamEncoderStateString[] = {
146         "FLAC__STREAM_ENCODER_OK",
147         "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
148         "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
149         "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
150         "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
151         "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
152         "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
153         "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
154         "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
155         "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
156         "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
157         "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
158         "FLAC__STREAM_ENCODER_FRAMING_ERROR",
159         "FLAC__STREAM_ENCODER_INVALID_METADATA",
160         "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
161         "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
162         "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
163         "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
164         "FLAC__STREAM_ENCODER_UNINITIALIZED"
165 };
166
167 const char * const FLAC__StreamEncoderWriteStatusString[] = {
168         "FLAC__STREAM_ENCODER_WRITE_OK",
169         "FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR"
170 };
171
172 /***********************************************************************
173  *
174  * Class constructor/destructor
175  *
176  ***********************************************************************/
177 FLAC__StreamEncoder *FLAC__stream_encoder_new()
178 {
179         FLAC__StreamEncoder *encoder;
180
181         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
182
183         encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
184         if(encoder == 0) {
185                 return 0;
186         }
187         encoder->protected_ = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
188         if(encoder->protected_ == 0) {
189                 free(encoder);
190                 return 0;
191         }
192         encoder->private_ = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
193         if(encoder->private_ == 0) {
194                 free(encoder->protected_);
195                 free(encoder);
196                 return 0;
197         }
198         encoder->private_->frame = FLAC__bitbuffer_new();
199         if(encoder->private_->frame == 0) {
200                 free(encoder->private_);
201                 free(encoder->protected_);
202                 free(encoder);
203                 return 0;
204         }
205
206         encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
207
208         encoder->protected_->streamable_subset = true;
209         encoder->protected_->do_mid_side_stereo = false;
210         encoder->protected_->loose_mid_side_stereo = false;
211         encoder->protected_->channels = 2;
212         encoder->protected_->bits_per_sample = 16;
213         encoder->protected_->sample_rate = 44100;
214         encoder->protected_->blocksize = 1152;
215         encoder->protected_->max_lpc_order = 0;
216         encoder->protected_->qlp_coeff_precision = 0;
217         encoder->protected_->do_qlp_coeff_prec_search = false;
218         encoder->protected_->do_exhaustive_model_search = false;
219         encoder->protected_->do_escape_coding = false;
220         encoder->protected_->min_residual_partition_order = 0;
221         encoder->protected_->max_residual_partition_order = 0;
222         encoder->protected_->rice_parameter_search_dist = 0;
223         encoder->protected_->total_samples_estimate = 0;
224         encoder->protected_->metadata = 0;
225         encoder->protected_->num_metadata_blocks = 0;
226
227         encoder->private_->write_callback = 0;
228         encoder->private_->metadata_callback = 0;
229         encoder->private_->client_data = 0;
230
231         return encoder;
232 }
233
234 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
235 {
236         FLAC__ASSERT(encoder != 0);
237         FLAC__ASSERT(encoder->protected_ != 0);
238         FLAC__ASSERT(encoder->private_ != 0);
239         FLAC__ASSERT(encoder->private_->frame != 0);
240
241         FLAC__bitbuffer_delete(encoder->private_->frame);
242         free(encoder->private_);
243         free(encoder->protected_);
244         free(encoder);
245 }
246
247 /***********************************************************************
248  *
249  * Public class methods
250  *
251  ***********************************************************************/
252
253 FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
254 {
255         unsigned i;
256
257         FLAC__ASSERT(encoder != 0);
258
259         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
260                 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
261
262         encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
263
264         if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
265                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
266
267         if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
268                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
269
270         if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
271                 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
272
273         if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
274                 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
275
276         if(encoder->protected_->bits_per_sample >= 32)
277                 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
278
279         if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
280                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
281
282         if(!FLAC__format_is_valid_sample_rate(encoder->protected_->sample_rate))
283                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
284
285         if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
286                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
287
288         if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
289                 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
290
291         if(encoder->protected_->qlp_coeff_precision == 0) {
292                 if(encoder->protected_->bits_per_sample < 16) {
293                         /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
294                         /* @@@ until then we'll make a guess */
295                         encoder->protected_->qlp_coeff_precision = max(5, 2 + encoder->protected_->bits_per_sample / 2);
296                 }
297                 else if(encoder->protected_->bits_per_sample == 16) {
298                         if(encoder->protected_->blocksize <= 192)
299                                 encoder->protected_->qlp_coeff_precision = 7;
300                         else if(encoder->protected_->blocksize <= 384)
301                                 encoder->protected_->qlp_coeff_precision = 8;
302                         else if(encoder->protected_->blocksize <= 576)
303                                 encoder->protected_->qlp_coeff_precision = 9;
304                         else if(encoder->protected_->blocksize <= 1152)
305                                 encoder->protected_->qlp_coeff_precision = 10;
306                         else if(encoder->protected_->blocksize <= 2304)
307                                 encoder->protected_->qlp_coeff_precision = 11;
308                         else if(encoder->protected_->blocksize <= 4608)
309                                 encoder->protected_->qlp_coeff_precision = 12;
310                         else
311                                 encoder->protected_->qlp_coeff_precision = 13;
312                 }
313                 else {
314                         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 */
315                 }
316         }
317         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))
318                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
319
320         if(encoder->protected_->streamable_subset) {
321                 /*@@@ add check for blocksize here */
322                 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)
323                         return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
324                 if(encoder->protected_->sample_rate > 655350)
325                         return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
326         }
327
328         if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
329                 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
330         if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
331                 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
332
333         /* validate metadata */
334         if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
335                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
336         for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
337                 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
338                         return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
339                 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
340                         if(!FLAC__metadata_object_seektable_is_legal(encoder->protected_->metadata[i]))
341                                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
342                 }
343         }
344
345         encoder->private_->input_capacity = 0;
346         for(i = 0; i < encoder->protected_->channels; i++) {
347                 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
348                 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
349         }
350         for(i = 0; i < 2; i++) {
351                 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
352                 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
353         }
354         for(i = 0; i < encoder->protected_->channels; i++) {
355                 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
356                 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
357                 encoder->private_->best_subframe[i] = 0;
358         }
359         for(i = 0; i < 2; i++) {
360                 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
361                 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
362                 encoder->private_->best_subframe_mid_side[i] = 0;
363         }
364         for(i = 0; i < encoder->protected_->channels; i++) {
365                 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
366                 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
367         }
368         for(i = 0; i < 2; i++) {
369                 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
370                 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
371         }
372         encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
373         encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
374         encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
375         encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
376         encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
377         if(encoder->private_->loose_mid_side_stereo_frames == 0)
378                 encoder->private_->loose_mid_side_stereo_frames = 1;
379         encoder->private_->loose_mid_side_stereo_frame_count = 0;
380         encoder->private_->current_sample_number = 0;
381         encoder->private_->current_frame_number = 0;
382
383         encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
384         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? */
385         encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
386
387         /*
388          * get the CPU info and set the function pointers
389          */
390         FLAC__cpu_info(&encoder->private_->cpuinfo);
391         /* first default to the non-asm routines */
392         encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
393         encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
394         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
395         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
396         /* now override with asm where appropriate */
397 #ifndef FLAC__NO_ASM
398         if(encoder->private_->cpuinfo.use_asm) {
399 #ifdef FLAC__CPU_IA32
400                 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
401 #ifdef FLAC__HAS_NASM
402                 if(0 && encoder->private_->cpuinfo.data.ia32.sse) {
403                         if(encoder->protected_->max_lpc_order < 4)
404                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
405                         else if(encoder->protected_->max_lpc_order < 8)
406                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
407                         else if(encoder->protected_->max_lpc_order < 12)
408                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
409                         else
410                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
411                 }
412                 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
413                         encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
414                 else
415                         encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
416                 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
417                         encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
418                 if(encoder->private_->cpuinfo.data.ia32.mmx) {
419                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
420                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
421                 }
422                 else {
423                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
424                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
425                 }
426 #endif
427 #endif
428         }
429 #endif
430         /* finally override based on wide-ness if necessary */
431         if(encoder->private_->use_wide_by_block) {
432                 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
433         }
434
435         /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
436         encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
437
438         if(!stream_encoder_resize_buffers_(encoder, encoder->protected_->blocksize)) {
439                 /* the above function sets the state for us in case of an error */
440                 return encoder->protected_->state;
441         }
442
443         if(!FLAC__bitbuffer_init(encoder->private_->frame))
444                 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
445
446         /*
447          * write the stream header
448          */
449         if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
450                 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
451
452         encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
453         encoder->private_->metadata.is_last = (encoder->protected_->num_metadata_blocks == 0);
454         encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
455         encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
456         encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
457         encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
458         encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
459         encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
460         encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
461         encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
462         encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
463         memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
464         MD5Init(&encoder->private_->md5context);
465         if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
466                 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
467
468         for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
469                 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
470                 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
471                         return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
472         }
473
474         FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
475         {
476                 const FLAC__byte *buffer;
477                 unsigned bytes;
478
479                 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
480
481                 if(encoder->private_->write_callback(encoder, buffer, bytes, 0, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_OK)
482                         return encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
483
484                 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
485         }
486
487         /* now that the metadata block is written, we can init this to an absurdly-high value... */
488         encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
489         /* ... and clear this to 0 */
490         encoder->private_->metadata.data.stream_info.total_samples = 0;
491
492         return encoder->protected_->state;
493 }
494
495 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
496 {
497         unsigned i, channel;
498
499         FLAC__ASSERT(encoder != 0);
500         if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
501                 return;
502         if(encoder->private_->current_sample_number != 0) {
503                 encoder->protected_->blocksize = encoder->private_->current_sample_number;
504                 stream_encoder_process_frame_(encoder, true); /* true => is last frame */
505         }
506         MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
507         encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
508         for(i = 0; i < encoder->protected_->channels; i++) {
509                 if(encoder->private_->integer_signal_unaligned[i] != 0) {
510                         free(encoder->private_->integer_signal_unaligned[i]);
511                         encoder->private_->integer_signal_unaligned[i] = 0;
512                 }
513                 if(encoder->private_->real_signal_unaligned[i] != 0) {
514                         free(encoder->private_->real_signal_unaligned[i]);
515                         encoder->private_->real_signal_unaligned[i] = 0;
516                 }
517         }
518         for(i = 0; i < 2; i++) {
519                 if(encoder->private_->integer_signal_mid_side_unaligned[i] != 0) {
520                         free(encoder->private_->integer_signal_mid_side_unaligned[i]);
521                         encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
522                 }
523                 if(encoder->private_->real_signal_mid_side_unaligned[i] != 0) {
524                         free(encoder->private_->real_signal_mid_side_unaligned[i]);
525                         encoder->private_->real_signal_mid_side_unaligned[i] = 0;
526                 }
527         }
528         for(channel = 0; channel < encoder->protected_->channels; channel++) {
529                 for(i = 0; i < 2; i++) {
530                         if(encoder->private_->residual_workspace_unaligned[channel][i] != 0) {
531                                 free(encoder->private_->residual_workspace_unaligned[channel][i]);
532                                 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
533                         }
534                 }
535         }
536         for(channel = 0; channel < 2; channel++) {
537                 for(i = 0; i < 2; i++) {
538                         if(encoder->private_->residual_workspace_mid_side_unaligned[channel][i] != 0) {
539                                 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
540                                 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
541                         }
542                 }
543         }
544         if(encoder->private_->abs_residual_unaligned != 0) {
545                 free(encoder->private_->abs_residual_unaligned);
546                 encoder->private_->abs_residual_unaligned = 0;
547         }
548         if(encoder->private_->abs_residual_partition_sums_unaligned != 0) {
549                 free(encoder->private_->abs_residual_partition_sums_unaligned);
550                 encoder->private_->abs_residual_partition_sums_unaligned = 0;
551         }
552         if(encoder->private_->raw_bits_per_partition_unaligned != 0) {
553                 free(encoder->private_->raw_bits_per_partition_unaligned);
554                 encoder->private_->raw_bits_per_partition_unaligned = 0;
555         }
556         FLAC__bitbuffer_free(encoder->private_->frame);
557
558         encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
559 }
560
561 FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
562 {
563         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
564                 return false;
565         encoder->protected_->streamable_subset = value;
566         return true;
567 }
568
569 FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
570 {
571         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
572                 return false;
573         encoder->protected_->do_mid_side_stereo = value;
574         return true;
575 }
576
577 FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
578 {
579         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
580                 return false;
581         encoder->protected_->loose_mid_side_stereo = value;
582         return true;
583 }
584
585 FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
586 {
587         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
588                 return false;
589         encoder->protected_->channels = value;
590         return true;
591 }
592
593 FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
594 {
595         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
596                 return false;
597         encoder->protected_->bits_per_sample = value;
598         return true;
599 }
600
601 FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
602 {
603         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
604                 return false;
605         encoder->protected_->sample_rate = value;
606         return true;
607 }
608
609 FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
610 {
611         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
612                 return false;
613         encoder->protected_->blocksize = value;
614         return true;
615 }
616
617 FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
618 {
619         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
620                 return false;
621         encoder->protected_->max_lpc_order = value;
622         return true;
623 }
624
625 FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
626 {
627         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
628                 return false;
629         encoder->protected_->qlp_coeff_precision = value;
630         return true;
631 }
632
633 FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
634 {
635         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
636                 return false;
637         encoder->protected_->do_qlp_coeff_prec_search = value;
638         return true;
639 }
640
641 FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
642 {
643         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
644                 return false;
645         encoder->protected_->do_escape_coding = value;
646         return true;
647 }
648
649 FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
650 {
651         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
652                 return false;
653         encoder->protected_->do_exhaustive_model_search = value;
654         return true;
655 }
656
657 FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
658 {
659         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
660                 return false;
661         encoder->protected_->min_residual_partition_order = value;
662         return true;
663 }
664
665 FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
666 {
667         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
668                 return false;
669         encoder->protected_->max_residual_partition_order = value;
670         return true;
671 }
672
673 FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
674 {
675         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
676                 return false;
677         encoder->protected_->rice_parameter_search_dist = value;
678         return true;
679 }
680
681 FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
682 {
683         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
684                 return false;
685         encoder->protected_->total_samples_estimate = value;
686         return true;
687 }
688
689 FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
690 {
691         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
692                 return false;
693         encoder->protected_->metadata = metadata;
694         encoder->protected_->num_metadata_blocks = num_blocks;
695         return true;
696 }
697
698 FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteStatus (*value)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data))
699 {
700         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
701                 return false;
702         encoder->private_->write_callback = value;
703         return true;
704 }
705
706 FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, void (*value)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data))
707 {
708         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
709                 return false;
710         encoder->private_->metadata_callback = value;
711         return true;
712 }
713
714 FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
715 {
716         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
717                 return false;
718         encoder->private_->client_data = value;
719         return true;
720 }
721
722 FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
723 {
724         return encoder->protected_->state;
725 }
726
727 FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
728 {
729         return encoder->protected_->streamable_subset;
730 }
731
732 FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
733 {
734         return encoder->protected_->do_mid_side_stereo;
735 }
736
737 FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
738 {
739         return encoder->protected_->loose_mid_side_stereo;
740 }
741
742 unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
743 {
744         return encoder->protected_->channels;
745 }
746
747 unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
748 {
749         return encoder->protected_->bits_per_sample;
750 }
751
752 unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
753 {
754         return encoder->protected_->sample_rate;
755 }
756
757 unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
758 {
759         return encoder->protected_->blocksize;
760 }
761
762 unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
763 {
764         return encoder->protected_->max_lpc_order;
765 }
766
767 unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
768 {
769         return encoder->protected_->qlp_coeff_precision;
770 }
771
772 FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
773 {
774         return encoder->protected_->do_qlp_coeff_prec_search;
775 }
776
777 FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
778 {
779         return encoder->protected_->do_escape_coding;
780 }
781
782 FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
783 {
784         return encoder->protected_->do_exhaustive_model_search;
785 }
786
787 unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
788 {
789         return encoder->protected_->min_residual_partition_order;
790 }
791
792 unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
793 {
794         return encoder->protected_->max_residual_partition_order;
795 }
796
797 unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
798 {
799         return encoder->protected_->rice_parameter_search_dist;
800 }
801
802 FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
803 {
804         unsigned i, j, channel;
805         FLAC__int32 x, mid, side;
806         const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
807
808         FLAC__ASSERT(encoder != 0);
809         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
810
811         j = 0;
812         if(encoder->protected_->do_mid_side_stereo && channels == 2) {
813                 do {
814                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
815                                 x = mid = side = buffer[0][j];
816                                 encoder->private_->integer_signal[0][i] = x;
817                                 encoder->private_->real_signal[0][i] = (FLAC__real)x;
818                                 x = buffer[1][j];
819                                 encoder->private_->integer_signal[1][i] = x;
820                                 encoder->private_->real_signal[1][i] = (FLAC__real)x;
821                                 mid += x;
822                                 side -= x;
823                                 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
824                                 encoder->private_->integer_signal_mid_side[1][i] = side;
825                                 encoder->private_->integer_signal_mid_side[0][i] = mid;
826                                 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
827                                 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
828                                 encoder->private_->current_sample_number++;
829                         }
830                         if(i == blocksize) {
831                                 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
832                                         return false;
833                         }
834                 } while(j < samples);
835         }
836         else {
837                 do {
838                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
839                                 for(channel = 0; channel < channels; channel++) {
840                                         x = buffer[channel][j];
841                                         encoder->private_->integer_signal[channel][i] = x;
842                                         encoder->private_->real_signal[channel][i] = (FLAC__real)x;
843                                 }
844                                 encoder->private_->current_sample_number++;
845                         }
846                         if(i == blocksize) {
847                                 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
848                                         return false;
849                         }
850                 } while(j < samples);
851         }
852
853         return true;
854 }
855
856 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
857 FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
858 {
859         unsigned i, j, k, channel;
860         FLAC__int32 x, mid, side;
861         const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
862
863         FLAC__ASSERT(encoder != 0);
864         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
865
866         j = k = 0;
867         if(encoder->protected_->do_mid_side_stereo && channels == 2) {
868                 do {
869                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
870                                 x = mid = side = buffer[k++];
871                                 encoder->private_->integer_signal[0][i] = x;
872                                 encoder->private_->real_signal[0][i] = (FLAC__real)x;
873                                 x = buffer[k++];
874                                 encoder->private_->integer_signal[1][i] = x;
875                                 encoder->private_->real_signal[1][i] = (FLAC__real)x;
876                                 mid += x;
877                                 side -= x;
878                                 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
879                                 encoder->private_->integer_signal_mid_side[1][i] = side;
880                                 encoder->private_->integer_signal_mid_side[0][i] = mid;
881                                 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
882                                 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
883                                 encoder->private_->current_sample_number++;
884                         }
885                         if(i == blocksize) {
886                                 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
887                                         return false;
888                         }
889                 } while(j < samples);
890         }
891         else {
892                 do {
893                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
894                                 for(channel = 0; channel < channels; channel++) {
895                                         x = buffer[k++];
896                                         encoder->private_->integer_signal[channel][i] = x;
897                                         encoder->private_->real_signal[channel][i] = (FLAC__real)x;
898                                 }
899                                 encoder->private_->current_sample_number++;
900                         }
901                         if(i == blocksize) {
902                                 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
903                                         return false;
904                         }
905                 } while(j < samples);
906         }
907
908         return true;
909 }
910
911 FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
912 {
913         FLAC__bool ok;
914         unsigned i, channel;
915
916         FLAC__ASSERT(new_size > 0);
917         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
918         FLAC__ASSERT(encoder->private_->current_sample_number == 0);
919
920         /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
921         if(new_size <= encoder->private_->input_capacity)
922                 return true;
923
924         ok = true;
925
926         /* 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. */
927
928         for(i = 0; ok && i < encoder->protected_->channels; i++) {
929                 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
930                 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
931                 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
932                 encoder->private_->integer_signal[i] += 4;
933         }
934         for(i = 0; ok && i < 2; i++) {
935                 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]);
936                 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]);
937                 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
938                 encoder->private_->integer_signal_mid_side[i] += 4;
939         }
940         for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
941                 for(i = 0; ok && i < 2; i++) {
942                         ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
943                 }
944         }
945         for(channel = 0; ok && channel < 2; channel++) {
946                 for(i = 0; ok && i < 2; i++) {
947                         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]);
948                 }
949         }
950         ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
951         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 */
952                 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
953         if(encoder->protected_->do_escape_coding)
954                 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
955
956         if(ok)
957                 encoder->private_->input_capacity = new_size;
958         else
959                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
960
961         return ok;
962 }
963
964 /***********************************************************************
965  *
966  * Private class methods
967  *
968  ***********************************************************************/
969
970 FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
971 {
972         const FLAC__byte *buffer;
973         unsigned bytes;
974
975         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
976
977         /*
978          * Accumulate raw signal to the MD5 signature
979          */
980         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)) {
981                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
982                 return false;
983         }
984
985         /*
986          * Process the frame header and subframes into the frame bitbuffer
987          */
988         if(!stream_encoder_process_subframes_(encoder, is_last_frame)) {
989                 /* the above function sets the state for us in case of an error */
990                 return false;
991         }
992
993         /*
994          * Zero-pad the frame to a byte_boundary
995          */
996         if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
997                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
998                 return false;
999         }
1000
1001         /*
1002          * CRC-16 the whole thing
1003          */
1004         FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1005         FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1006
1007         /*
1008          * Write it
1009          */
1010         FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1011         if(encoder->private_->write_callback(encoder, buffer, bytes, encoder->protected_->blocksize, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_OK) {
1012                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1013                 return false;
1014         }
1015         FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1016
1017         /*
1018          * Get ready for the next frame
1019          */
1020         encoder->private_->current_sample_number = 0;
1021         encoder->private_->current_frame_number++;
1022         encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1023         encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1024         encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1025
1026         return true;
1027 }
1028
1029 FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1030 {
1031         FLAC__FrameHeader frame_header;
1032         unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1033         FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1034
1035         /*
1036          * Calculate the min,max Rice partition orders
1037          */
1038         if(is_last_frame) {
1039                 max_partition_order = 0;
1040         }
1041         else {
1042                 unsigned limit = 0, b = encoder->protected_->blocksize;
1043                 while(!(b & 1)) {
1044                         limit++;
1045                         b >>= 1;
1046                 }
1047                 max_partition_order = min(encoder->protected_->max_residual_partition_order, limit);
1048         }
1049         min_partition_order = min(min_partition_order, max_partition_order);
1050
1051         precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1052
1053         /*
1054          * Setup the frame
1055          */
1056         if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1057                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1058                 return false;
1059         }
1060         frame_header.blocksize = encoder->protected_->blocksize;
1061         frame_header.sample_rate = encoder->protected_->sample_rate;
1062         frame_header.channels = encoder->protected_->channels;
1063         frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1064         frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1065         frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1066         frame_header.number.frame_number = encoder->private_->current_frame_number;
1067
1068         /*
1069          * Figure out what channel assignments to try
1070          */
1071         if(encoder->protected_->do_mid_side_stereo) {
1072                 if(encoder->protected_->loose_mid_side_stereo) {
1073                         if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1074                                 do_independent = true;
1075                                 do_mid_side = true;
1076                         }
1077                         else {
1078                                 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1079                                 do_mid_side = !do_independent;
1080                         }
1081                 }
1082                 else {
1083                         do_independent = true;
1084                         do_mid_side = true;
1085                 }
1086         }
1087         else {
1088                 do_independent = true;
1089                 do_mid_side = false;
1090         }
1091
1092         FLAC__ASSERT(do_independent || do_mid_side);
1093
1094         /*
1095          * Check for wasted bits; set effective bps for each subframe
1096          */
1097         if(do_independent) {
1098                 unsigned w;
1099                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1100                         w = stream_encoder_get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1101                         encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1102                         encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1103                 }
1104         }
1105         if(do_mid_side) {
1106                 unsigned w;
1107                 FLAC__ASSERT(encoder->protected_->channels == 2);
1108                 for(channel = 0; channel < 2; channel++) {
1109                         w = stream_encoder_get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1110                         encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1111                         encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1112                 }
1113         }
1114
1115         /*
1116          * First do a normal encoding pass of each independent channel
1117          */
1118         if(do_independent) {
1119                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1120                         if(!stream_encoder_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))
1121                                 return false;
1122                 }
1123         }
1124
1125         /*
1126          * Now do mid and side channels if requested
1127          */
1128         if(do_mid_side) {
1129                 FLAC__ASSERT(encoder->protected_->channels == 2);
1130
1131                 for(channel = 0; channel < 2; channel++) {
1132                         if(!stream_encoder_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))
1133                                 return false;
1134                 }
1135         }
1136
1137         /*
1138          * Compose the frame bitbuffer
1139          */
1140         if(do_mid_side) {
1141                 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1142                 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1143                 FLAC__ChannelAssignment channel_assignment;
1144
1145                 FLAC__ASSERT(encoder->protected_->channels == 2);
1146
1147                 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1148                         channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1149                 }
1150                 else {
1151                         unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1152                         unsigned min_bits;
1153                         FLAC__ChannelAssignment ca;
1154
1155                         FLAC__ASSERT(do_independent && do_mid_side);
1156
1157                         /* We have to figure out which channel assignent results in the smallest frame */
1158                         bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits         [0] + encoder->private_->best_subframe_bits         [1];
1159                         bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->private_->best_subframe_bits         [0] + encoder->private_->best_subframe_bits_mid_side[1];
1160                         bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits         [1] + encoder->private_->best_subframe_bits_mid_side[1];
1161                         bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
1162
1163                         for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
1164                                 if(bits[ca] < min_bits) {
1165                                         min_bits = bits[ca];
1166                                         channel_assignment = ca;
1167                                 }
1168                         }
1169                 }
1170
1171                 frame_header.channel_assignment = channel_assignment;
1172
1173                 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1174                         encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1175                         return false;
1176                 }
1177
1178                 switch(channel_assignment) {
1179                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1180                                 left_subframe  = &encoder->private_->subframe_workspace         [0][encoder->private_->best_subframe         [0]];
1181                                 right_subframe = &encoder->private_->subframe_workspace         [1][encoder->private_->best_subframe         [1]];
1182                                 break;
1183                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1184                                 left_subframe  = &encoder->private_->subframe_workspace         [0][encoder->private_->best_subframe         [0]];
1185                                 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1186                                 break;
1187                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1188                                 left_subframe  = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1189                                 right_subframe = &encoder->private_->subframe_workspace         [1][encoder->private_->best_subframe         [1]];
1190                                 break;
1191                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1192                                 left_subframe  = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1193                                 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1194                                 break;
1195                         default:
1196                                 FLAC__ASSERT(0);
1197                 }
1198
1199                 switch(channel_assignment) {
1200                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1201                                 left_bps  = encoder->private_->subframe_bps         [0];
1202                                 right_bps = encoder->private_->subframe_bps         [1];
1203                                 break;
1204                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1205                                 left_bps  = encoder->private_->subframe_bps         [0];
1206                                 right_bps = encoder->private_->subframe_bps_mid_side[1];
1207                                 break;
1208                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1209                                 left_bps  = encoder->private_->subframe_bps_mid_side[1];
1210                                 right_bps = encoder->private_->subframe_bps         [1];
1211                                 break;
1212                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1213                                 left_bps  = encoder->private_->subframe_bps_mid_side[0];
1214                                 right_bps = encoder->private_->subframe_bps_mid_side[1];
1215                                 break;
1216                         default:
1217                                 FLAC__ASSERT(0);
1218                 }
1219
1220                 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1221                 if(!stream_encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
1222                         return false;
1223                 if(!stream_encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1224                         return false;
1225         }
1226         else {
1227                 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1228                         encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1229                         return false;
1230                 }
1231
1232                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1233                         if(!stream_encoder_add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
1234                                 /* the above function sets the state for us in case of an error */
1235                                 return false;
1236                         }
1237                 }
1238         }
1239
1240         if(encoder->protected_->loose_mid_side_stereo) {
1241                 encoder->private_->loose_mid_side_stereo_frame_count++;
1242                 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1243                         encoder->private_->loose_mid_side_stereo_frame_count = 0;
1244         }
1245
1246         encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1247
1248         return true;
1249 }
1250
1251 FLAC__bool stream_encoder_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)
1252 {
1253         FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1254         FLAC__real lpc_residual_bits_per_sample;
1255         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 */
1256         FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1257         unsigned min_lpc_order, max_lpc_order, lpc_order;
1258         unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1259         unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1260         unsigned rice_parameter;
1261         unsigned _candidate_bits, _best_bits;
1262         unsigned _best_subframe;
1263
1264         /* verbatim subframe is the baseline against which we measure other compressed subframes */
1265         _best_subframe = 0;
1266         _best_bits = stream_encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1267
1268         if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1269                 /* check for constant subframe */
1270                 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);
1271                 if(fixed_residual_bits_per_sample[1] == 0.0) {
1272                         /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1273                         unsigned i, signal_is_constant = true;
1274                         for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1275                                 if(integer_signal[0] != integer_signal[i]) {
1276                                         signal_is_constant = false;
1277                                         break;
1278                                 }
1279                         }
1280                         if(signal_is_constant) {
1281                                 _candidate_bits = stream_encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1282                                 if(_candidate_bits < _best_bits) {
1283                                         _best_subframe = !_best_subframe;
1284                                         _best_bits = _candidate_bits;
1285                                 }
1286                         }
1287                 }
1288                 else {
1289                         /* encode fixed */
1290                         if(encoder->protected_->do_exhaustive_model_search) {
1291                                 min_fixed_order = 0;
1292                                 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1293                         }
1294                         else {
1295                                 min_fixed_order = max_fixed_order = guess_fixed_order;
1296                         }
1297                         for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1298                                 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1299                                         continue; /* don't even try */
1300                                 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 */
1301 #ifndef FLAC__SYMMETRIC_RICE
1302                                 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1303 #endif
1304                                 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1305 #ifdef DEBUG_VERBOSE
1306                                         fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1307 #endif
1308                                         rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1309                                 }
1310                                 _candidate_bits = stream_encoder_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]);
1311                                 if(_candidate_bits < _best_bits) {
1312                                         _best_subframe = !_best_subframe;
1313                                         _best_bits = _candidate_bits;
1314                                 }
1315                         }
1316
1317                         /* encode lpc */
1318                         if(encoder->protected_->max_lpc_order > 0) {
1319                                 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
1320                                         max_lpc_order = frame_header->blocksize-1;
1321                                 else
1322                                         max_lpc_order = encoder->protected_->max_lpc_order;
1323                                 if(max_lpc_order > 0) {
1324                                         encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
1325                                         /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
1326                                         if(autoc[0] != 0.0) {
1327                                                 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
1328                                                 if(encoder->protected_->do_exhaustive_model_search) {
1329                                                         min_lpc_order = 1;
1330                                                 }
1331                                                 else {
1332                                                         unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
1333                                                         min_lpc_order = max_lpc_order = guess_lpc_order;
1334                                                 }
1335                                                 if(encoder->protected_->do_qlp_coeff_prec_search) {
1336                                                         min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
1337                                                         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 */
1338                                                 }
1339                                                 else {
1340                                                         min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
1341                                                 }
1342                                                 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
1343                                                         lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
1344                                                         if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
1345                                                                 continue; /* don't even try */
1346                                                         rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
1347 #ifndef FLAC__SYMMETRIC_RICE
1348                                                         rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1349 #endif
1350                                                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1351 #ifdef DEBUG_VERBOSE
1352                                                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1353 #endif
1354                                                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1355                                                         }
1356                                                         for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
1357                                                                 _candidate_bits = stream_encoder_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]);
1358                                                                 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1359                                                                         if(_candidate_bits < _best_bits) {
1360                                                                                 _best_subframe = !_best_subframe;
1361                                                                                 _best_bits = _candidate_bits;
1362                                                                         }
1363                                                                 }
1364                                                         }
1365                                                 }
1366                                         }
1367                                 }
1368                         }
1369                 }
1370         }
1371
1372         *best_subframe = _best_subframe;
1373         *best_bits = _best_bits;
1374
1375         return true;
1376 }
1377
1378 FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
1379 {
1380         switch(subframe->type) {
1381                 case FLAC__SUBFRAME_TYPE_CONSTANT:
1382                         if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
1383                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1384                                 return false;
1385                         }
1386                         break;
1387                 case FLAC__SUBFRAME_TYPE_FIXED:
1388                         if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
1389                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1390                                 return false;
1391                         }
1392                         break;
1393                 case FLAC__SUBFRAME_TYPE_LPC:
1394                         if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
1395                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1396                                 return false;
1397                         }
1398                         break;
1399                 case FLAC__SUBFRAME_TYPE_VERBATIM:
1400                         if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
1401                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1402                                 return false;
1403                         }
1404                         break;
1405                 default:
1406                         FLAC__ASSERT(0);
1407         }
1408
1409         return true;
1410 }
1411
1412 unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
1413 {
1414         subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1415         subframe->data.constant.value = signal;
1416
1417         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
1418 }
1419
1420 unsigned stream_encoder_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)
1421 {
1422         unsigned i, residual_bits;
1423         const unsigned residual_samples = blocksize - order;
1424
1425         FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1426
1427         subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1428
1429         subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1430         subframe->data.fixed.residual = residual;
1431
1432         residual_bits = stream_encoder_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);
1433
1434         subframe->data.fixed.order = order;
1435         for(i = 0; i < order; i++)
1436                 subframe->data.fixed.warmup[i] = signal[i];
1437
1438         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
1439 }
1440
1441 unsigned stream_encoder_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)
1442 {
1443         FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1444         unsigned i, residual_bits;
1445         int quantization, ret;
1446         const unsigned residual_samples = blocksize - order;
1447
1448         ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
1449         if(ret != 0)
1450                 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1451
1452         if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
1453                 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1454         else
1455                 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1456
1457         subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1458
1459         subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1460         subframe->data.lpc.residual = residual;
1461
1462         residual_bits = stream_encoder_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);
1463
1464         subframe->data.lpc.order = order;
1465         subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1466         subframe->data.lpc.quantization_level = quantization;
1467         memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
1468         for(i = 0; i < order; i++)
1469                 subframe->data.lpc.warmup[i] = signal[i];
1470
1471         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;
1472 }
1473
1474 unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
1475 {
1476         subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1477
1478         subframe->data.verbatim.data = signal;
1479
1480         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
1481 }
1482
1483 unsigned stream_encoder_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[])
1484 {
1485         FLAC__int32 r;
1486         unsigned residual_bits, best_residual_bits = 0;
1487         unsigned residual_sample;
1488         unsigned best_parameters_index = 0;
1489         const unsigned blocksize = residual_samples + predictor_order;
1490
1491         /* compute abs(residual) for use later */
1492         for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1493                 r = residual[residual_sample];
1494                 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
1495         }
1496
1497         while(max_partition_order > 0 && blocksize >> max_partition_order <= predictor_order)
1498                 max_partition_order--;
1499         FLAC__ASSERT(blocksize >> max_partition_order > predictor_order);
1500         min_partition_order = min(min_partition_order, max_partition_order);
1501
1502         if(precompute_partition_sums) {
1503                 int partition_order;
1504                 unsigned sum;
1505
1506                 stream_encoder_precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
1507
1508                 if(do_escape_coding)
1509                         stream_encoder_precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
1510
1511                 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1512 #ifdef DONT_ESTIMATE_RICE_BITS
1513                         if(!stream_encoder_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))
1514 #else
1515                         if(!stream_encoder_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))
1516 #endif
1517                         {
1518                                 FLAC__ASSERT(best_residual_bits != 0);
1519                                 break;
1520                         }
1521                         sum += 1u << partition_order;
1522                         if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1523                                 best_residual_bits = residual_bits;
1524                                 *best_partition_order = partition_order;
1525                                 best_parameters_index = !best_parameters_index;
1526                         }
1527                 }
1528         }
1529         else {
1530                 unsigned partition_order;
1531                 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1532 #ifdef DONT_ESTIMATE_RICE_BITS
1533                         if(!stream_encoder_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))
1534 #else
1535                         if(!stream_encoder_set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, private_->parameters[!best_parameters_index], &residual_bits))
1536 #endif
1537                         {
1538                                 FLAC__ASSERT(best_residual_bits != 0);
1539                                 break;
1540                         }
1541                         if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1542                                 best_residual_bits = residual_bits;
1543                                 *best_partition_order = partition_order;
1544                                 best_parameters_index = !best_parameters_index;
1545                         }
1546                 }
1547         }
1548
1549         memcpy(best_parameters, private_->parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1550         memcpy(best_raw_bits, private_->raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1551
1552         return best_residual_bits;
1553 }
1554
1555 void stream_encoder_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)
1556 {
1557         int partition_order;
1558         unsigned from_partition, to_partition = 0;
1559         const unsigned blocksize = residual_samples + predictor_order;
1560
1561         /* first do max_partition_order */
1562         for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1563                 FLAC__uint64 abs_residual_partition_sum;
1564                 FLAC__uint32 abs_r;
1565                 unsigned partition, partition_sample, partition_samples, residual_sample;
1566                 const unsigned partitions = 1u << partition_order;
1567                 const unsigned default_partition_samples = blocksize >> partition_order;
1568
1569                 FLAC__ASSERT(default_partition_samples > predictor_order);
1570
1571                 for(partition = residual_sample = 0; partition < partitions; partition++) {
1572                         partition_samples = default_partition_samples;
1573                         if(partition == 0)
1574                                 partition_samples -= predictor_order;
1575                         abs_residual_partition_sum = 0;
1576                         for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1577                                 abs_r = abs_residual[residual_sample];
1578                                 abs_residual_partition_sum += abs_r;
1579                                 residual_sample++;
1580                         }
1581                         abs_residual_partition_sums[partition] = abs_residual_partition_sum;
1582                 }
1583                 to_partition = partitions;
1584                 break;
1585         }
1586
1587         /* now merge partitions for lower orders */
1588         for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1589                 FLAC__uint64 s;
1590                 unsigned i;
1591                 const unsigned partitions = 1u << partition_order;
1592                 for(i = 0; i < partitions; i++) {
1593                         s = abs_residual_partition_sums[from_partition];
1594                         from_partition++;
1595                         abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1596                         from_partition++;
1597                         to_partition++;
1598                 }
1599         }
1600 }
1601
1602 void stream_encoder_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)
1603 {
1604         int partition_order;
1605         unsigned from_partition, to_partition = 0;
1606         const unsigned blocksize = residual_samples + predictor_order;
1607
1608         /* first do max_partition_order */
1609         for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1610                 FLAC__int32 r, residual_partition_min, residual_partition_max;
1611                 unsigned silog2_min, silog2_max;
1612                 unsigned partition, partition_sample, partition_samples, residual_sample;
1613                 const unsigned partitions = 1u << partition_order;
1614                 const unsigned default_partition_samples = blocksize >> partition_order;
1615
1616                 FLAC__ASSERT(default_partition_samples > predictor_order);
1617
1618                 for(partition = residual_sample = 0; partition < partitions; partition++) {
1619                         partition_samples = default_partition_samples;
1620                         if(partition == 0)
1621                                 partition_samples -= predictor_order;
1622                         residual_partition_min = residual_partition_max = 0;
1623                         for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1624                                 r = residual[residual_sample];
1625                                 if(r < residual_partition_min)
1626                                         residual_partition_min = r;
1627                                 else if(r > residual_partition_max)
1628                                         residual_partition_max = r;
1629                                 residual_sample++;
1630                         }
1631                         silog2_min = FLAC__bitmath_silog2(residual_partition_min);
1632                         silog2_max = FLAC__bitmath_silog2(residual_partition_max);
1633                         raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
1634                 }
1635                 to_partition = partitions;
1636                 break;
1637         }
1638
1639         /* now merge partitions for lower orders */
1640         for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1641                 unsigned m;
1642                 unsigned i;
1643                 const unsigned partitions = 1u << partition_order;
1644                 for(i = 0; i < partitions; i++) {
1645                         m = raw_bits_per_partition[from_partition];
1646                         from_partition++;
1647                         raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1648                         from_partition++;
1649                         to_partition++;
1650                 }
1651         }
1652 }
1653
1654 #ifdef VARIABLE_RICE_BITS
1655 #undef VARIABLE_RICE_BITS
1656 #endif
1657 #ifndef DONT_ESTIMATE_RICE_BITS
1658 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1659 #endif
1660
1661 #ifdef DONT_ESTIMATE_RICE_BITS
1662 FLAC__bool stream_encoder_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)
1663 #else
1664 FLAC__bool stream_encoder_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)
1665 #endif
1666 {
1667         unsigned rice_parameter, partition_bits;
1668 #ifndef NO_RICE_SEARCH
1669         unsigned best_partition_bits;
1670         unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1671 #endif
1672         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1673
1674         FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1675
1676         if(partition_order == 0) {
1677                 unsigned i;
1678
1679 #ifndef NO_RICE_SEARCH
1680                 if(rice_parameter_search_dist) {
1681                         if(suggested_rice_parameter < rice_parameter_search_dist)
1682                                 min_rice_parameter = 0;
1683                         else
1684                                 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1685                         max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1686                         if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1687 #ifdef DEBUG_VERBOSE
1688                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1689 #endif
1690                                 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1691                         }
1692                 }
1693                 else
1694                         min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1695
1696                 best_partition_bits = 0xffffffff;
1697                 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1698 #endif
1699 #ifdef VARIABLE_RICE_BITS
1700 #ifdef FLAC__SYMMETRIC_RICE
1701                         partition_bits = (2+rice_parameter) * residual_samples;
1702 #else
1703                         const unsigned rice_parameter_estimate = rice_parameter-1;
1704                         partition_bits = (1+rice_parameter) * residual_samples;
1705 #endif
1706 #else
1707                         partition_bits = 0;
1708 #endif
1709                         partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1710                         for(i = 0; i < residual_samples; i++) {
1711 #ifdef VARIABLE_RICE_BITS
1712 #ifdef FLAC__SYMMETRIC_RICE
1713                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1714 #else
1715                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1716 #endif
1717 #else
1718                                 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
1719 #endif
1720                         }
1721 #ifndef NO_RICE_SEARCH
1722                         if(partition_bits < best_partition_bits) {
1723                                 best_rice_parameter = rice_parameter;
1724                                 best_partition_bits = partition_bits;
1725                         }
1726                 }
1727 #endif
1728                 parameters[0] = best_rice_parameter;
1729                 bits_ += best_partition_bits;
1730         }
1731         else {
1732                 unsigned partition, residual_sample, save_residual_sample, partition_sample;
1733                 unsigned partition_samples;
1734                 FLAC__uint64 mean, k;
1735                 const unsigned partitions = 1u << partition_order;
1736                 for(partition = residual_sample = 0; partition < partitions; partition++) {
1737                         partition_samples = (residual_samples+predictor_order) >> partition_order;
1738                         if(partition == 0) {
1739                                 if(partition_samples <= predictor_order)
1740                                         return false;
1741                                 else
1742                                         partition_samples -= predictor_order;
1743                         }
1744                         mean = 0;
1745                         save_residual_sample = residual_sample;
1746                         for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
1747                                 mean += abs_residual[residual_sample];
1748                         residual_sample = save_residual_sample;
1749 #ifdef FLAC__SYMMETRIC_RICE
1750                         mean += partition_samples >> 1; /* for rounding effect */
1751                         mean /= partition_samples;
1752
1753                         /* calc rice_parameter = floor(log2(mean)) */
1754                         rice_parameter = 0;
1755                         mean>>=1;
1756                         while(mean) {
1757                                 rice_parameter++;
1758                                 mean >>= 1;
1759                         }
1760 #else
1761                         /* calc rice_parameter ala LOCO-I */
1762                         for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
1763                                 ;
1764 #endif
1765                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1766 #ifdef DEBUG_VERBOSE
1767                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1768 #endif
1769                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1770                         }
1771
1772 #ifndef NO_RICE_SEARCH
1773                         if(rice_parameter_search_dist) {
1774                                 if(rice_parameter < rice_parameter_search_dist)
1775                                         min_rice_parameter = 0;
1776                                 else
1777                                         min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1778                                 max_rice_parameter = 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) @4\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 = 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) * partition_samples;
1795 #else
1796                                 const unsigned rice_parameter_estimate = rice_parameter-1;
1797                                 partition_bits = (1+rice_parameter) * partition_samples;
1798 #endif
1799 #else
1800                                 partition_bits = 0;
1801 #endif
1802                                 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1803                                 save_residual_sample = residual_sample;
1804                                 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
1805 #ifdef VARIABLE_RICE_BITS
1806 #ifdef FLAC__SYMMETRIC_RICE
1807                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
1808 #else
1809                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
1810 #endif
1811 #else
1812                                         partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
1813 #endif
1814                                 }
1815 #ifndef NO_RICE_SEARCH
1816                                 if(rice_parameter != max_rice_parameter)
1817                                         residual_sample = save_residual_sample;
1818                                 if(partition_bits < best_partition_bits) {
1819                                         best_rice_parameter = rice_parameter;
1820                                         best_partition_bits = partition_bits;
1821                                 }
1822                         }
1823 #endif
1824                         parameters[partition] = best_rice_parameter;
1825                         bits_ += best_partition_bits;
1826                 }
1827         }
1828
1829         *bits = bits_;
1830         return true;
1831 }
1832
1833 #ifdef DONT_ESTIMATE_RICE_BITS
1834 FLAC__bool stream_encoder_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)
1835 #else
1836 FLAC__bool stream_encoder_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)
1837 #endif
1838 {
1839         unsigned rice_parameter, partition_bits;
1840 #ifndef NO_RICE_SEARCH
1841         unsigned best_partition_bits;
1842         unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1843 #endif
1844         unsigned flat_bits;
1845         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1846
1847         FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1848
1849         if(partition_order == 0) {
1850                 unsigned i;
1851
1852 #ifndef NO_RICE_SEARCH
1853                 if(rice_parameter_search_dist) {
1854                         if(suggested_rice_parameter < rice_parameter_search_dist)
1855                                 min_rice_parameter = 0;
1856                         else
1857                                 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1858                         max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1859                         if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1860 #ifdef DEBUG_VERBOSE
1861                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1862 #endif
1863                                 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1864                         }
1865                 }
1866                 else
1867                         min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1868
1869                 best_partition_bits = 0xffffffff;
1870                 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1871 #endif
1872 #ifdef VARIABLE_RICE_BITS
1873 #ifdef FLAC__SYMMETRIC_RICE
1874                         partition_bits = (2+rice_parameter) * residual_samples;
1875 #else
1876                         const unsigned rice_parameter_estimate = rice_parameter-1;
1877                         partition_bits = (1+rice_parameter) * residual_samples;
1878 #endif
1879 #else
1880                         partition_bits = 0;
1881 #endif
1882                         partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1883                         for(i = 0; i < residual_samples; i++) {
1884 #ifdef VARIABLE_RICE_BITS
1885 #ifdef FLAC__SYMMETRIC_RICE
1886                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1887 #else
1888                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1889 #endif
1890 #else
1891                                 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1892 #endif
1893                         }
1894 #ifndef NO_RICE_SEARCH
1895                         if(partition_bits < best_partition_bits) {
1896                                 best_rice_parameter = rice_parameter;
1897                                 best_partition_bits = partition_bits;
1898                         }
1899                 }
1900 #endif
1901                 if(search_for_escapes) {
1902                         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;
1903                         if(flat_bits <= best_partition_bits) {
1904                                 raw_bits[0] = raw_bits_per_partition[0];
1905                                 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1906                                 best_partition_bits = flat_bits;
1907                         }
1908                 }
1909                 parameters[0] = best_rice_parameter;
1910                 bits_ += best_partition_bits;
1911         }
1912         else {
1913                 unsigned partition, residual_sample, save_residual_sample, partition_sample;
1914                 unsigned partition_samples;
1915                 FLAC__uint64 mean, k;
1916                 const unsigned partitions = 1u << partition_order;
1917                 for(partition = residual_sample = 0; partition < partitions; partition++) {
1918                         partition_samples = (residual_samples+predictor_order) >> partition_order;
1919                         if(partition == 0) {
1920                                 if(partition_samples <= predictor_order)
1921                                         return false;
1922                                 else
1923                                         partition_samples -= predictor_order;
1924                         }
1925                         mean = abs_residual_partition_sums[partition];
1926 #ifdef FLAC__SYMMETRIC_RICE
1927                         mean += partition_samples >> 1; /* for rounding effect */
1928                         mean /= partition_samples;
1929
1930                         /* calc rice_parameter = floor(log2(mean)) */
1931                         rice_parameter = 0;
1932                         mean>>=1;
1933                         while(mean) {
1934                                 rice_parameter++;
1935                                 mean >>= 1;
1936                         }
1937 #else
1938                         /* calc rice_parameter ala LOCO-I */
1939                         for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
1940                                 ;
1941 #endif
1942                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1943 #ifdef DEBUG_VERBOSE
1944                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1945 #endif
1946                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1947                         }
1948
1949 #ifndef NO_RICE_SEARCH
1950                         if(rice_parameter_search_dist) {
1951                                 if(rice_parameter < rice_parameter_search_dist)
1952                                         min_rice_parameter = 0;
1953                                 else
1954                                         min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1955                                 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
1956                                 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1957 #ifdef DEBUG_VERBOSE
1958                                         fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1959 #endif
1960                                         max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1961                                 }
1962                         }
1963                         else
1964                                 min_rice_parameter = max_rice_parameter = rice_parameter;
1965
1966                         best_partition_bits = 0xffffffff;
1967                         for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1968 #endif
1969 #ifdef VARIABLE_RICE_BITS
1970 #ifdef FLAC__SYMMETRIC_RICE
1971                                 partition_bits = (2+rice_parameter) * partition_samples;
1972 #else
1973                                 const unsigned rice_parameter_estimate = rice_parameter-1;
1974                                 partition_bits = (1+rice_parameter) * partition_samples;
1975 #endif
1976 #else
1977                                 partition_bits = 0;
1978 #endif
1979                                 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1980                                 save_residual_sample = residual_sample;
1981                                 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
1982 #ifdef VARIABLE_RICE_BITS
1983 #ifdef FLAC__SYMMETRIC_RICE
1984                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
1985 #else
1986                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
1987 #endif
1988 #else
1989                                         partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1990 #endif
1991                                 }
1992 #ifndef NO_RICE_SEARCH
1993                                 if(rice_parameter != max_rice_parameter)
1994                                         residual_sample = save_residual_sample;
1995                                 if(partition_bits < best_partition_bits) {
1996                                         best_rice_parameter = rice_parameter;
1997                                         best_partition_bits = partition_bits;
1998                                 }
1999                         }
2000 #endif
2001                         if(search_for_escapes) {
2002                                 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;
2003                                 if(flat_bits <= best_partition_bits) {
2004                                         raw_bits[partition] = raw_bits_per_partition[partition];
2005                                         best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2006                                         best_partition_bits = flat_bits;
2007                                 }
2008                         }
2009                         parameters[partition] = best_rice_parameter;
2010                         bits_ += best_partition_bits;
2011                 }
2012         }
2013
2014         *bits = bits_;
2015         return true;
2016 }
2017
2018 unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
2019 {
2020         unsigned i, shift;
2021         FLAC__int32 x = 0;
2022
2023         for(i = 0; i < samples && !(x&1); i++)
2024                 x |= signal[i];
2025
2026         if(x == 0) {
2027                 shift = 0;
2028         }
2029         else {
2030                 for(shift = 0; !(x&1); shift++)
2031                         x >>= 1;
2032         }
2033
2034         if(shift > 0) {
2035                 for(i = 0; i < samples; i++)
2036                          signal[i] >>= shift;
2037         }
2038
2039         return shift;
2040 }