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