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