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