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