add support for 64bit datapath through FIR filter, update logic for automatically...
[platform/upstream/flac.git] / src / libFLAC / stream_encoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memcpy() */
23 #include "FLAC/assert.h"
24 #include "FLAC/stream_decoder.h"
25 #include "protected/stream_encoder.h"
26 #include "private/bitbuffer.h"
27 #include "private/bitmath.h"
28 #include "private/crc.h"
29 #include "private/cpu.h"
30 #include "private/fixed.h"
31 #include "private/format.h"
32 #include "private/lpc.h"
33 #include "private/md5.h"
34 #include "private/memory.h"
35 #include "private/stream_encoder_framing.h"
36
37 #ifdef min
38 #undef min
39 #endif
40 #define min(x,y) ((x)<(y)?(x):(y))
41
42 #ifdef max
43 #undef max
44 #endif
45 #define max(x,y) ((x)>(y)?(x):(y))
46
47 typedef struct {
48         FLAC__int32 *data[FLAC__MAX_CHANNELS];
49         unsigned size; /* of each data[] in samples */
50         unsigned tail;
51 } verify_input_fifo;
52
53 typedef struct {
54         const FLAC__byte *data;
55         unsigned capacity;
56         unsigned bytes;
57 } verify_output;
58
59 typedef enum {
60         ENCODER_IN_MAGIC = 0,
61         ENCODER_IN_METADATA = 1,
62         ENCODER_IN_AUDIO = 2
63 } EncoderStateHint;
64
65 /***********************************************************************
66  *
67  * Private class method prototypes
68  *
69  ***********************************************************************/
70
71 static void set_defaults_(FLAC__StreamEncoder *encoder);
72 static void free_(FLAC__StreamEncoder *encoder);
73 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
74 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
75 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
76 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
77
78 static FLAC__bool process_subframe_(
79         FLAC__StreamEncoder *encoder,
80         unsigned min_partition_order,
81         unsigned max_partition_order,
82         FLAC__bool precompute_partition_sums,
83         FLAC__bool verbatim_only,
84         const FLAC__FrameHeader *frame_header,
85         unsigned subframe_bps,
86         const FLAC__int32 integer_signal[],
87         const FLAC__real real_signal[],
88         FLAC__Subframe *subframe[2],
89         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
90         FLAC__int32 *residual[2],
91         unsigned *best_subframe,
92         unsigned *best_bits
93 );
94
95 static FLAC__bool add_subframe_(
96         FLAC__StreamEncoder *encoder,
97         const FLAC__FrameHeader *frame_header,
98         unsigned subframe_bps,
99         const FLAC__Subframe *subframe,
100         FLAC__BitBuffer *frame
101 );
102
103 static unsigned evaluate_constant_subframe_(
104         const FLAC__int32 signal,
105         unsigned subframe_bps,
106         FLAC__Subframe *subframe
107 );
108
109 static unsigned evaluate_fixed_subframe_(
110         FLAC__StreamEncoder *encoder,
111         const FLAC__int32 signal[],
112         FLAC__int32 residual[],
113         FLAC__uint32 abs_residual[],
114         FLAC__uint64 abs_residual_partition_sums[],
115         unsigned raw_bits_per_partition[],
116         unsigned blocksize,
117         unsigned subframe_bps,
118         unsigned order,
119         unsigned rice_parameter,
120         unsigned min_partition_order,
121         unsigned max_partition_order,
122         FLAC__bool precompute_partition_sums,
123         FLAC__bool do_escape_coding,
124         unsigned rice_parameter_search_dist,
125         FLAC__Subframe *subframe,
126         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
127 );
128
129 static unsigned evaluate_lpc_subframe_(
130         FLAC__StreamEncoder *encoder,
131         const FLAC__int32 signal[],
132         FLAC__int32 residual[],
133         FLAC__uint32 abs_residual[],
134         FLAC__uint64 abs_residual_partition_sums[],
135         unsigned raw_bits_per_partition[],
136         const FLAC__real lp_coeff[],
137         unsigned blocksize,
138         unsigned subframe_bps,
139         unsigned order,
140         unsigned qlp_coeff_precision,
141         unsigned rice_parameter,
142         unsigned min_partition_order,
143         unsigned max_partition_order,
144         FLAC__bool precompute_partition_sums,
145         FLAC__bool do_escape_coding,
146         unsigned rice_parameter_search_dist,
147         FLAC__Subframe *subframe,
148         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
149 );
150
151 static unsigned evaluate_verbatim_subframe_(
152         const FLAC__int32 signal[],
153         unsigned blocksize,
154         unsigned subframe_bps,
155         FLAC__Subframe *subframe
156 );
157
158 static unsigned find_best_partition_order_(
159         struct FLAC__StreamEncoderPrivate *private_,
160         const FLAC__int32 residual[],
161         FLAC__uint32 abs_residual[],
162         FLAC__uint64 abs_residual_partition_sums[],
163         unsigned raw_bits_per_partition[],
164         unsigned residual_samples,
165         unsigned predictor_order,
166         unsigned rice_parameter,
167         unsigned min_partition_order,
168         unsigned max_partition_order,
169         FLAC__bool precompute_partition_sums,
170         FLAC__bool do_escape_coding,
171         unsigned rice_parameter_search_dist,
172         FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
173 );
174
175 static void precompute_partition_info_sums_(
176         const FLAC__uint32 abs_residual[],
177         FLAC__uint64 abs_residual_partition_sums[],
178         unsigned residual_samples,
179         unsigned predictor_order,
180         unsigned min_partition_order,
181         unsigned max_partition_order
182 );
183
184 static void precompute_partition_info_escapes_(
185         const FLAC__int32 residual[],
186         unsigned raw_bits_per_partition[],
187         unsigned residual_samples,
188         unsigned predictor_order,
189         unsigned min_partition_order,
190         unsigned max_partition_order
191 );
192
193 #ifdef DONT_ESTIMATE_RICE_BITS
194 static FLAC__bool set_partitioned_rice_(
195         const FLAC__uint32 abs_residual[],
196         const FLAC__int32 residual[],
197         const unsigned residual_samples,
198         const unsigned predictor_order,
199         const unsigned suggested_rice_parameter,
200         const unsigned rice_parameter_search_dist,
201         const unsigned partition_order,
202         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
203         unsigned *bits
204 );
205
206 static FLAC__bool set_partitioned_rice_with_precompute_(
207         const FLAC__int32 residual[],
208         const FLAC__uint64 abs_residual_partition_sums[],
209         const unsigned raw_bits_per_partition[],
210         const unsigned residual_samples,
211         const unsigned predictor_order,
212         const unsigned suggested_rice_parameter,
213         const unsigned rice_parameter_search_dist,
214         const unsigned partition_order,
215         const FLAC__bool search_for_escapes,
216         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
217         unsigned *bits
218 );
219 #else
220 static FLAC__bool set_partitioned_rice_(
221         const FLAC__uint32 abs_residual[],
222         const unsigned residual_samples,
223         const unsigned predictor_order,
224         const unsigned suggested_rice_parameter,
225         const unsigned rice_parameter_search_dist,
226         const unsigned partition_order,
227         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
228         unsigned *bits
229 );
230
231 static FLAC__bool set_partitioned_rice_with_precompute_(
232         const FLAC__uint32 abs_residual[],
233         const FLAC__uint64 abs_residual_partition_sums[],
234         const unsigned raw_bits_per_partition[],
235         const unsigned residual_samples,
236         const unsigned predictor_order,
237         const unsigned suggested_rice_parameter,
238         const unsigned rice_parameter_search_dist,
239         const unsigned partition_order,
240         const FLAC__bool search_for_escapes,
241         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
242         unsigned *bits
243 );
244 #endif
245
246 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
247
248 /* verify-related routines: */
249 static void append_to_verify_fifo_(
250         verify_input_fifo *fifo,
251         const FLAC__int32 * const input[],
252         unsigned input_offset,
253         unsigned channels,
254         unsigned wide_samples
255 );
256
257 static void append_to_verify_fifo_interleaved_(
258         verify_input_fifo *fifo,
259         const FLAC__int32 input[],
260         unsigned input_offset,
261         unsigned channels,
262         unsigned wide_samples
263 );
264
265 static FLAC__StreamDecoderReadStatus verify_read_callback_(
266         const FLAC__StreamDecoder *decoder,
267         FLAC__byte buffer[],
268         unsigned *bytes,
269         void *client_data
270 );
271
272 static FLAC__StreamDecoderWriteStatus verify_write_callback_(
273         const FLAC__StreamDecoder *decoder,
274         const FLAC__Frame *frame,
275         const FLAC__int32 * const buffer[],
276         void *client_data
277 );
278
279 static void verify_metadata_callback_(
280         const FLAC__StreamDecoder *decoder,
281         const FLAC__StreamMetadata *metadata,
282         void *client_data
283 );
284
285 static void verify_error_callback_(
286         const FLAC__StreamDecoder *decoder,
287         FLAC__StreamDecoderErrorStatus status,
288         void *client_data
289 );
290
291
292 /***********************************************************************
293  *
294  * Private class data
295  *
296  ***********************************************************************/
297
298 typedef struct FLAC__StreamEncoderPrivate {
299         unsigned input_capacity;                          /* current size (in samples) of the signal and residual buffers */
300         FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS];  /* the integer version of the input signal */
301         FLAC__int32 *integer_signal_mid_side[2];          /* the integer version of the mid-side input signal (stereo only) */
302         FLAC__real *real_signal[FLAC__MAX_CHANNELS];      /* the floating-point version of the input signal */
303         FLAC__real *real_signal_mid_side[2];              /* the floating-point version of the mid-side input signal (stereo only) */
304         unsigned subframe_bps[FLAC__MAX_CHANNELS];        /* the effective bits per sample of the input signal (stream bps - wasted bits) */
305         unsigned subframe_bps_mid_side[2];                /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
306         FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
307         FLAC__int32 *residual_workspace_mid_side[2][2];
308         FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
309         FLAC__Subframe subframe_workspace_mid_side[2][2];
310         FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
311         FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
312         FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
313         FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
314         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
315         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
316         unsigned best_subframe[FLAC__MAX_CHANNELS];       /* index into the above workspaces */
317         unsigned best_subframe_mid_side[2];
318         unsigned best_subframe_bits[FLAC__MAX_CHANNELS];  /* size in bits of the best subframe for each channel */
319         unsigned best_subframe_bits_mid_side[2];
320         FLAC__uint32 *abs_residual;                       /* workspace where abs(candidate residual) is stored */
321         FLAC__uint64 *abs_residual_partition_sums;        /* workspace where the sum of abs(candidate residual) for each partition is stored */
322         unsigned *raw_bits_per_partition;                 /* workspace where the sum of silog2(candidate residual) for each partition is stored */
323         FLAC__BitBuffer *frame;                           /* the current frame being worked on */
324         double loose_mid_side_stereo_frames_exact;        /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
325         unsigned loose_mid_side_stereo_frames;            /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
326         unsigned loose_mid_side_stereo_frame_count;       /* number of frames using the current channel assignment */
327         FLAC__ChannelAssignment last_channel_assignment;
328         FLAC__StreamMetadata metadata;
329         unsigned current_sample_number;
330         unsigned current_frame_number;
331         struct MD5Context md5context;
332         FLAC__CPUInfo cpuinfo;
333         unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
334         void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
335         void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
336         void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
337         void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
338         FLAC__bool use_wide_by_block;          /* use slow 64-bit versions of some functions because of the block size */
339         FLAC__bool use_wide_by_partition;      /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
340         FLAC__bool use_wide_by_order;          /* use slow 64-bit versions of some functions because of the lpc order */
341         FLAC__bool precompute_partition_sums;  /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
342         FLAC__StreamEncoderWriteCallback write_callback;
343         FLAC__StreamEncoderMetadataCallback metadata_callback;
344         void *client_data;
345         /* unaligned (original) pointers to allocated data */
346         FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
347         FLAC__int32 *integer_signal_mid_side_unaligned[2];
348         FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
349         FLAC__real *real_signal_mid_side_unaligned[2];
350         FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
351         FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
352         FLAC__uint32 *abs_residual_unaligned;
353         FLAC__uint64 *abs_residual_partition_sums_unaligned;
354         unsigned *raw_bits_per_partition_unaligned;
355         /*
356          * These fields have been moved here from private function local
357          * declarations merely to save stack space during encoding.
358          */
359         FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
360         FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
361         /*
362          * The data for the verify section
363          */
364         struct {
365                 FLAC__StreamDecoder *decoder;
366                 EncoderStateHint state_hint;
367                 FLAC__bool needs_magic_hack;
368                 verify_input_fifo input_fifo;
369                 verify_output output;
370                 struct {
371                         FLAC__uint64 absolute_sample;
372                         unsigned frame_number;
373                         unsigned channel;
374                         unsigned sample;
375                         FLAC__int32 expected;
376                         FLAC__int32 got;
377                 } error_stats;
378         } verify;
379         FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
380 } FLAC__StreamEncoderPrivate;
381
382 /***********************************************************************
383  *
384  * Public static class data
385  *
386  ***********************************************************************/
387
388 const char * const FLAC__StreamEncoderStateString[] = {
389         "FLAC__STREAM_ENCODER_OK",
390         "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
391         "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
392         "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
393         "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
394         "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
395         "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
396         "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
397         "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
398         "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
399         "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
400         "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
401         "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
402         "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
403         "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
404         "FLAC__STREAM_ENCODER_FRAMING_ERROR",
405         "FLAC__STREAM_ENCODER_INVALID_METADATA",
406         "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
407         "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
408         "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
409         "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
410         "FLAC__STREAM_ENCODER_UNINITIALIZED"
411 };
412
413 const char * const FLAC__StreamEncoderWriteStatusString[] = {
414         "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
415         "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
416 };
417
418 /***********************************************************************
419  *
420  * Class constructor/destructor
421  *
422  */
423 FLAC__StreamEncoder *FLAC__stream_encoder_new()
424 {
425         FLAC__StreamEncoder *encoder;
426         unsigned i;
427
428         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
429
430         encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
431         if(encoder == 0) {
432                 return 0;
433         }
434         memset(encoder, 0, sizeof(FLAC__StreamEncoder));
435
436         encoder->protected_ = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
437         if(encoder->protected_ == 0) {
438                 free(encoder);
439                 return 0;
440         }
441         memset(encoder->protected_, 0, sizeof(FLAC__StreamEncoderProtected));
442
443         encoder->private_ = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
444         if(encoder->private_ == 0) {
445                 free(encoder->protected_);
446                 free(encoder);
447                 return 0;
448         }
449         memset(encoder->private_, 0, sizeof(FLAC__StreamEncoderPrivate));
450
451         encoder->private_->frame = FLAC__bitbuffer_new();
452         if(encoder->private_->frame == 0) {
453                 free(encoder->private_);
454                 free(encoder->protected_);
455                 free(encoder);
456                 return 0;
457         }
458
459         set_defaults_(encoder);
460
461         encoder->private_->is_being_deleted = false;
462
463         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
464                 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
465                 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
466         }
467         for(i = 0; i < 2; i++) {
468                 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
469                 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
470         }
471         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
472                 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
473                 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
474         }
475         for(i = 0; i < 2; i++) {
476                 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
477                 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
478         }
479
480         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
481                 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
482                 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
483         }
484         for(i = 0; i < 2; i++) {
485                 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
486                 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
487         }
488         for(i = 0; i < 2; i++)
489                 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
490
491         encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
492
493         return encoder;
494 }
495
496 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
497 {
498         unsigned i;
499
500         FLAC__ASSERT(0 != encoder);
501         FLAC__ASSERT(0 != encoder->protected_);
502         FLAC__ASSERT(0 != encoder->private_);
503         FLAC__ASSERT(0 != encoder->private_->frame);
504
505         encoder->private_->is_being_deleted = true;
506
507         FLAC__stream_encoder_finish(encoder);
508
509         if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
510                 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
511
512         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
513                 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
514                 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
515         }
516         for(i = 0; i < 2; i++) {
517                 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
518                 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
519         }
520         for(i = 0; i < 2; i++)
521                 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
522
523         FLAC__bitbuffer_delete(encoder->private_->frame);
524         free(encoder->private_);
525         free(encoder->protected_);
526         free(encoder);
527 }
528
529 /***********************************************************************
530  *
531  * Public class methods
532  *
533  ***********************************************************************/
534
535 FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
536 {
537         unsigned i;
538         FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
539
540         FLAC__ASSERT(0 != encoder);
541
542         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
543                 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
544
545         encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
546
547         if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
548                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
549
550         if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
551                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
552
553         if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
554                 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
555
556         if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
557                 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
558
559         if(encoder->protected_->bits_per_sample >= 32)
560                 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
561
562         if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
563                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
564
565         if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
566                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
567
568         if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
569                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
570
571         if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
572                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
573
574         if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
575                 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
576
577         if(encoder->protected_->qlp_coeff_precision == 0) {
578                 if(encoder->protected_->bits_per_sample < 16) {
579                         /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
580                         /* @@@ until then we'll make a guess */
581                         encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
582                 }
583                 else if(encoder->protected_->bits_per_sample == 16) {
584                         if(encoder->protected_->blocksize <= 192)
585                                 encoder->protected_->qlp_coeff_precision = 7;
586                         else if(encoder->protected_->blocksize <= 384)
587                                 encoder->protected_->qlp_coeff_precision = 8;
588                         else if(encoder->protected_->blocksize <= 576)
589                                 encoder->protected_->qlp_coeff_precision = 9;
590                         else if(encoder->protected_->blocksize <= 1152)
591                                 encoder->protected_->qlp_coeff_precision = 10;
592                         else if(encoder->protected_->blocksize <= 2304)
593                                 encoder->protected_->qlp_coeff_precision = 11;
594                         else if(encoder->protected_->blocksize <= 4608)
595                                 encoder->protected_->qlp_coeff_precision = 12;
596                         else
597                                 encoder->protected_->qlp_coeff_precision = 13;
598                 }
599                 else {
600                         if(encoder->protected_->blocksize <= 384)
601                                 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
602                         else if(encoder->protected_->blocksize <= 1152)
603                                 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
604                         else
605                                 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
606                 }
607                 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
608         }
609         else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
610                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
611
612         if(encoder->protected_->streamable_subset) {
613                 if(
614                         encoder->protected_->blocksize != 192 &&
615                         encoder->protected_->blocksize != 576 &&
616                         encoder->protected_->blocksize != 1152 &&
617                         encoder->protected_->blocksize != 2304 &&
618                         encoder->protected_->blocksize != 4608 &&
619                         encoder->protected_->blocksize != 256 &&
620                         encoder->protected_->blocksize != 512 &&
621                         encoder->protected_->blocksize != 1024 &&
622                         encoder->protected_->blocksize != 2048 &&
623                         encoder->protected_->blocksize != 4096 &&
624                         encoder->protected_->blocksize != 8192 &&
625                         encoder->protected_->blocksize != 16384
626                 )
627                         return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
628                 if(
629                         encoder->protected_->sample_rate != 8000 &&
630                         encoder->protected_->sample_rate != 16000 &&
631                         encoder->protected_->sample_rate != 22050 &&
632                         encoder->protected_->sample_rate != 24000 &&
633                         encoder->protected_->sample_rate != 32000 &&
634                         encoder->protected_->sample_rate != 44100 &&
635                         encoder->protected_->sample_rate != 48000 &&
636                         encoder->protected_->sample_rate != 96000
637                 )
638                         return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
639                 if(
640                         encoder->protected_->bits_per_sample != 8 &&
641                         encoder->protected_->bits_per_sample != 12 &&
642                         encoder->protected_->bits_per_sample != 16 &&
643                         encoder->protected_->bits_per_sample != 20 &&
644                         encoder->protected_->bits_per_sample != 24
645                 )
646                         return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
647                 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
648                         return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
649         }
650
651         if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
652                 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
653         if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
654                 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
655
656         /* validate metadata */
657         if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
658                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
659         metadata_has_seektable = false;
660         metadata_has_vorbis_comment = false;
661         for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
662                 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
663                         return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
664                 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
665                         if(metadata_has_seektable) /* only one is allowed */
666                                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
667                         metadata_has_seektable = true;
668                         if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
669                                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
670                 }
671                 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
672                         if(metadata_has_vorbis_comment) /* only one is allowed */
673                                 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
674                         metadata_has_vorbis_comment = true;
675                 }
676         }
677
678         encoder->private_->input_capacity = 0;
679         for(i = 0; i < encoder->protected_->channels; i++) {
680                 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
681                 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
682         }
683         for(i = 0; i < 2; i++) {
684                 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
685                 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
686         }
687         for(i = 0; i < encoder->protected_->channels; i++) {
688                 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
689                 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
690                 encoder->private_->best_subframe[i] = 0;
691         }
692         for(i = 0; i < 2; i++) {
693                 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
694                 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
695                 encoder->private_->best_subframe_mid_side[i] = 0;
696         }
697         encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
698         encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
699         encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
700         encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
701         encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
702         if(encoder->private_->loose_mid_side_stereo_frames == 0)
703                 encoder->private_->loose_mid_side_stereo_frames = 1;
704         encoder->private_->loose_mid_side_stereo_frame_count = 0;
705         encoder->private_->current_sample_number = 0;
706         encoder->private_->current_frame_number = 0;
707
708         encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
709         encoder->private_->use_wide_by_order = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(max(encoder->protected_->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */
710         encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
711
712         /*
713          * get the CPU info and set the function pointers
714          */
715         FLAC__cpu_info(&encoder->private_->cpuinfo);
716         /* first default to the non-asm routines */
717         encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
718         encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
719         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
720         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
721         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
722         /* now override with asm where appropriate */
723 #ifndef FLAC__NO_ASM
724         if(encoder->private_->cpuinfo.use_asm) {
725 #ifdef FLAC__CPU_IA32
726                 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
727 #ifdef FLAC__HAS_NASM
728                 if(0 && encoder->private_->cpuinfo.data.ia32.sse) {
729                         if(encoder->protected_->max_lpc_order < 4)
730                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
731                         else if(encoder->protected_->max_lpc_order < 8)
732                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
733                         else if(encoder->protected_->max_lpc_order < 12)
734                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
735                         else
736                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
737                 }
738                 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
739                         encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
740                 else
741                         encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
742                 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
743                         encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
744                 if(encoder->private_->cpuinfo.data.ia32.mmx) {
745                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
746                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
747                 }
748                 else {
749                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
750                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
751                 }
752 #endif
753 #endif
754         }
755 #endif
756         /* finally override based on wide-ness if necessary */
757         if(encoder->private_->use_wide_by_block) {
758                 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
759         }
760
761         /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
762         encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
763
764         if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
765                 /* the above function sets the state for us in case of an error */
766                 return encoder->protected_->state;
767         }
768
769         if(!FLAC__bitbuffer_init(encoder->private_->frame))
770                 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
771
772         /*
773          * Set up the verify stuff if necessary
774          */
775         if(encoder->protected_->verify) {
776                 /*
777                  * First, set up the fifo which will hold the
778                  * original signal to compare against
779                  */
780                 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
781                 for(i = 0; i < encoder->protected_->channels; i++) {
782                         if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
783                                 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
784                 }
785                 encoder->private_->verify.input_fifo.tail = 0;
786
787                 /*
788                  * Now set up a stream decoder for verification
789                  */
790                 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
791                 if(0 == encoder->private_->verify.decoder)
792                         return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
793
794                 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
795                 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
796                 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
797                 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
798                 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
799                 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
800                         return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
801         }
802         encoder->private_->verify.error_stats.absolute_sample = 0;
803         encoder->private_->verify.error_stats.frame_number = 0;
804         encoder->private_->verify.error_stats.channel = 0;
805         encoder->private_->verify.error_stats.sample = 0;
806         encoder->private_->verify.error_stats.expected = 0;
807         encoder->private_->verify.error_stats.got = 0;
808
809         /*
810          * write the stream header
811          */
812         if(encoder->protected_->verify)
813                 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
814         if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
815                 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
816         if(!write_bitbuffer_(encoder, 0)) {
817                 /* the above function sets the state for us in case of an error */
818                 return encoder->protected_->state;
819         }
820
821         /*
822          * write the STREAMINFO metadata block
823          */
824         if(encoder->protected_->verify)
825                 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
826         encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
827         encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
828         encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
829         encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
830         encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
831         encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
832         encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
833         encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
834         encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
835         encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
836         encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
837         memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
838         MD5Init(&encoder->private_->md5context);
839         if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
840                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
841                 return false;
842         }
843         if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
844                 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
845         if(!write_bitbuffer_(encoder, 0)) {
846                 /* the above function sets the state for us in case of an error */
847                 return encoder->protected_->state;
848         }
849
850         /*
851          * Now that the STREAMINFO block is written, we can init this to an
852          * absurdly-high value...
853          */
854         encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
855         /* ... and clear this to 0 */
856         encoder->private_->metadata.data.stream_info.total_samples = 0;
857
858         /*
859          * Check to see if the supplied metadata contains a VORBIS_COMMENT;
860          * if not, we will write an empty one (FLAC__add_metadata_block()
861          * automatically supplies the vendor string).
862          */
863         if(!metadata_has_vorbis_comment) {
864                 FLAC__StreamMetadata vorbis_comment;
865                 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
866                 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
867                 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
868                 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
869                 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
870                 vorbis_comment.data.vorbis_comment.num_comments = 0;
871                 vorbis_comment.data.vorbis_comment.comments = 0;
872                 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
873                         encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
874                         return false;
875                 }
876                 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
877                         return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
878                 if(!write_bitbuffer_(encoder, 0)) {
879                         /* the above function sets the state for us in case of an error */
880                         return encoder->protected_->state;
881                 }
882         }
883
884         /*
885          * write the user's metadata blocks
886          */
887         for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
888                 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
889                 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
890                         encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
891                         return false;
892                 }
893                 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
894                         return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
895                 if(!write_bitbuffer_(encoder, 0)) {
896                         /* the above function sets the state for us in case of an error */
897                         return encoder->protected_->state;
898                 }
899         }
900
901         if(encoder->protected_->verify)
902                 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
903
904         return encoder->protected_->state;
905 }
906
907 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
908 {
909         FLAC__ASSERT(0 != encoder);
910
911         if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
912                 return;
913
914         if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
915                 if(encoder->private_->current_sample_number != 0) {
916                         encoder->protected_->blocksize = encoder->private_->current_sample_number;
917                         process_frame_(encoder, true); /* true => is last frame */
918                 }
919         }
920
921         MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
922
923         if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
924                 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
925         }
926
927         if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
928                 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
929
930         free_(encoder);
931         set_defaults_(encoder);
932
933         encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
934 }
935
936 FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
937 {
938         FLAC__ASSERT(0 != encoder);
939         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
940                 return false;
941         encoder->protected_->verify = value;
942         return true;
943 }
944
945 FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
946 {
947         FLAC__ASSERT(0 != encoder);
948         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
949                 return false;
950         encoder->protected_->streamable_subset = value;
951         return true;
952 }
953
954 FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
955 {
956         FLAC__ASSERT(0 != encoder);
957         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
958                 return false;
959         encoder->protected_->do_mid_side_stereo = value;
960         return true;
961 }
962
963 FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
964 {
965         FLAC__ASSERT(0 != encoder);
966         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
967                 return false;
968         encoder->protected_->loose_mid_side_stereo = value;
969         return true;
970 }
971
972 FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
973 {
974         FLAC__ASSERT(0 != encoder);
975         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
976                 return false;
977         encoder->protected_->channels = value;
978         return true;
979 }
980
981 FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
982 {
983         FLAC__ASSERT(0 != encoder);
984         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
985                 return false;
986         encoder->protected_->bits_per_sample = value;
987         return true;
988 }
989
990 FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
991 {
992         FLAC__ASSERT(0 != encoder);
993         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
994                 return false;
995         encoder->protected_->sample_rate = value;
996         return true;
997 }
998
999 FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1000 {
1001         FLAC__ASSERT(0 != encoder);
1002         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1003                 return false;
1004         encoder->protected_->blocksize = value;
1005         return true;
1006 }
1007
1008 FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1009 {
1010         FLAC__ASSERT(0 != encoder);
1011         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1012                 return false;
1013         encoder->protected_->max_lpc_order = value;
1014         return true;
1015 }
1016
1017 FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1018 {
1019         FLAC__ASSERT(0 != encoder);
1020         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1021                 return false;
1022         encoder->protected_->qlp_coeff_precision = value;
1023         return true;
1024 }
1025
1026 FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1027 {
1028         FLAC__ASSERT(0 != encoder);
1029         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1030                 return false;
1031         encoder->protected_->do_qlp_coeff_prec_search = value;
1032         return true;
1033 }
1034
1035 FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1036 {
1037         FLAC__ASSERT(0 != encoder);
1038         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1039                 return false;
1040 #if 0
1041         /*@@@ deprecated: */
1042         encoder->protected_->do_escape_coding = value;
1043 #else
1044         (void)value;
1045 #endif
1046         return true;
1047 }
1048
1049 FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1050 {
1051         FLAC__ASSERT(0 != encoder);
1052         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1053                 return false;
1054         encoder->protected_->do_exhaustive_model_search = value;
1055         return true;
1056 }
1057
1058 FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1059 {
1060         FLAC__ASSERT(0 != encoder);
1061         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1062                 return false;
1063         encoder->protected_->min_residual_partition_order = value;
1064         return true;
1065 }
1066
1067 FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1068 {
1069         FLAC__ASSERT(0 != encoder);
1070         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1071                 return false;
1072         encoder->protected_->max_residual_partition_order = value;
1073         return true;
1074 }
1075
1076 FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1077 {
1078         FLAC__ASSERT(0 != encoder);
1079         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1080                 return false;
1081 #if 0
1082         /*@@@ deprecated: */
1083         encoder->protected_->rice_parameter_search_dist = value;
1084 #else
1085         (void)value;
1086 #endif
1087         return true;
1088 }
1089
1090 FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1091 {
1092         FLAC__ASSERT(0 != encoder);
1093         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1094                 return false;
1095         encoder->protected_->total_samples_estimate = value;
1096         return true;
1097 }
1098
1099 FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1100 {
1101         FLAC__ASSERT(0 != encoder);
1102         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1103                 return false;
1104         encoder->protected_->metadata = metadata;
1105         encoder->protected_->num_metadata_blocks = num_blocks;
1106         return true;
1107 }
1108
1109 FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
1110 {
1111         FLAC__ASSERT(0 != encoder);
1112         FLAC__ASSERT(0 != value);
1113         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1114                 return false;
1115         encoder->private_->write_callback = value;
1116         return true;
1117 }
1118
1119 FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
1120 {
1121         FLAC__ASSERT(0 != encoder);
1122         FLAC__ASSERT(0 != value);
1123         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1124                 return false;
1125         encoder->private_->metadata_callback = value;
1126         return true;
1127 }
1128
1129 FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
1130 {
1131         FLAC__ASSERT(0 != encoder);
1132         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1133                 return false;
1134         encoder->private_->client_data = value;
1135         return true;
1136 }
1137
1138 FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1139 {
1140         FLAC__ASSERT(0 != encoder);
1141         return encoder->protected_->state;
1142 }
1143
1144 FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1145 {
1146         FLAC__ASSERT(0 != encoder);
1147         if(encoder->protected_->verify)
1148                 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1149         else
1150                 return FLAC__STREAM_DECODER_UNINITIALIZED;
1151 }
1152
1153 void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
1154 {
1155         FLAC__ASSERT(0 != encoder);
1156         if(0 != absolute_sample)
1157                 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1158         if(0 != frame_number)
1159                 *frame_number = encoder->private_->verify.error_stats.frame_number;
1160         if(0 != channel)
1161                 *channel = encoder->private_->verify.error_stats.channel;
1162         if(0 != sample)
1163                 *sample = encoder->private_->verify.error_stats.sample;
1164         if(0 != expected)
1165                 *expected = encoder->private_->verify.error_stats.expected;
1166         if(0 != got)
1167                 *got = encoder->private_->verify.error_stats.got;
1168 }
1169
1170 FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1171 {
1172         FLAC__ASSERT(0 != encoder);
1173         return encoder->protected_->verify;
1174 }
1175
1176 FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1177 {
1178         FLAC__ASSERT(0 != encoder);
1179         return encoder->protected_->streamable_subset;
1180 }
1181
1182 FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1183 {
1184         FLAC__ASSERT(0 != encoder);
1185         return encoder->protected_->do_mid_side_stereo;
1186 }
1187
1188 FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1189 {
1190         FLAC__ASSERT(0 != encoder);
1191         return encoder->protected_->loose_mid_side_stereo;
1192 }
1193
1194 unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1195 {
1196         FLAC__ASSERT(0 != encoder);
1197         return encoder->protected_->channels;
1198 }
1199
1200 unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1201 {
1202         FLAC__ASSERT(0 != encoder);
1203         return encoder->protected_->bits_per_sample;
1204 }
1205
1206 unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1207 {
1208         FLAC__ASSERT(0 != encoder);
1209         return encoder->protected_->sample_rate;
1210 }
1211
1212 unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1213 {
1214         FLAC__ASSERT(0 != encoder);
1215         return encoder->protected_->blocksize;
1216 }
1217
1218 unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1219 {
1220         FLAC__ASSERT(0 != encoder);
1221         return encoder->protected_->max_lpc_order;
1222 }
1223
1224 unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1225 {
1226         FLAC__ASSERT(0 != encoder);
1227         return encoder->protected_->qlp_coeff_precision;
1228 }
1229
1230 FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1231 {
1232         FLAC__ASSERT(0 != encoder);
1233         return encoder->protected_->do_qlp_coeff_prec_search;
1234 }
1235
1236 FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1237 {
1238         FLAC__ASSERT(0 != encoder);
1239         return encoder->protected_->do_escape_coding;
1240 }
1241
1242 FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1243 {
1244         FLAC__ASSERT(0 != encoder);
1245         return encoder->protected_->do_exhaustive_model_search;
1246 }
1247
1248 unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1249 {
1250         FLAC__ASSERT(0 != encoder);
1251         return encoder->protected_->min_residual_partition_order;
1252 }
1253
1254 unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1255 {
1256         FLAC__ASSERT(0 != encoder);
1257         return encoder->protected_->max_residual_partition_order;
1258 }
1259
1260 unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1261 {
1262         FLAC__ASSERT(0 != encoder);
1263         return encoder->protected_->rice_parameter_search_dist;
1264 }
1265
1266 FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1267 {
1268         FLAC__ASSERT(0 != encoder);
1269         return encoder->protected_->total_samples_estimate;
1270 }
1271
1272 FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1273 {
1274         unsigned i, j, channel;
1275         FLAC__int32 x, mid, side;
1276         const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1277
1278         FLAC__ASSERT(0 != encoder);
1279         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1280
1281         j = 0;
1282         if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1283                 do {
1284                         if(encoder->protected_->verify)
1285                                 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1286
1287                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1288                                 x = mid = side = buffer[0][j];
1289                                 encoder->private_->integer_signal[0][i] = x;
1290                                 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1291                                 x = buffer[1][j];
1292                                 encoder->private_->integer_signal[1][i] = x;
1293                                 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1294                                 mid += x;
1295                                 side -= x;
1296                                 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1297                                 encoder->private_->integer_signal_mid_side[1][i] = side;
1298                                 encoder->private_->integer_signal_mid_side[0][i] = mid;
1299                                 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1300                                 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1301                                 encoder->private_->current_sample_number++;
1302                         }
1303                         if(i == blocksize) {
1304                                 if(!process_frame_(encoder, false)) /* false => not last frame */
1305                                         return false;
1306                         }
1307                 } while(j < samples);
1308         }
1309         else {
1310                 do {
1311                         if(encoder->protected_->verify)
1312                                 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1313
1314                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1315                                 for(channel = 0; channel < channels; channel++) {
1316                                         x = buffer[channel][j];
1317                                         encoder->private_->integer_signal[channel][i] = x;
1318                                         encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1319                                 }
1320                                 encoder->private_->current_sample_number++;
1321                         }
1322                         if(i == blocksize) {
1323                                 if(!process_frame_(encoder, false)) /* false => not last frame */
1324                                         return false;
1325                         }
1326                 } while(j < samples);
1327         }
1328
1329         return true;
1330 }
1331
1332 FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1333 {
1334         unsigned i, j, k, channel;
1335         FLAC__int32 x, mid, side;
1336         const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1337
1338         FLAC__ASSERT(0 != encoder);
1339         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1340
1341         j = k = 0;
1342         if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1343                 do {
1344                         if(encoder->protected_->verify)
1345                                 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1346
1347                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1348                                 x = mid = side = buffer[k++];
1349                                 encoder->private_->integer_signal[0][i] = x;
1350                                 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1351                                 x = buffer[k++];
1352                                 encoder->private_->integer_signal[1][i] = x;
1353                                 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1354                                 mid += x;
1355                                 side -= x;
1356                                 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1357                                 encoder->private_->integer_signal_mid_side[1][i] = side;
1358                                 encoder->private_->integer_signal_mid_side[0][i] = mid;
1359                                 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1360                                 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1361                                 encoder->private_->current_sample_number++;
1362                         }
1363                         if(i == blocksize) {
1364                                 if(!process_frame_(encoder, false)) /* false => not last frame */
1365                                         return false;
1366                         }
1367                 } while(j < samples);
1368         }
1369         else {
1370                 do {
1371                         if(encoder->protected_->verify)
1372                                 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1373
1374                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1375                                 for(channel = 0; channel < channels; channel++) {
1376                                         x = buffer[k++];
1377                                         encoder->private_->integer_signal[channel][i] = x;
1378                                         encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1379                                 }
1380                                 encoder->private_->current_sample_number++;
1381                         }
1382                         if(i == blocksize) {
1383                                 if(!process_frame_(encoder, false)) /* false => not last frame */
1384                                         return false;
1385                         }
1386                 } while(j < samples);
1387         }
1388
1389         return true;
1390 }
1391
1392 /***********************************************************************
1393  *
1394  * Private class methods
1395  *
1396  ***********************************************************************/
1397
1398 void set_defaults_(FLAC__StreamEncoder *encoder)
1399 {
1400         FLAC__ASSERT(0 != encoder);
1401
1402         encoder->protected_->verify = false;
1403         encoder->protected_->streamable_subset = true;
1404         encoder->protected_->do_mid_side_stereo = false;
1405         encoder->protected_->loose_mid_side_stereo = false;
1406         encoder->protected_->channels = 2;
1407         encoder->protected_->bits_per_sample = 16;
1408         encoder->protected_->sample_rate = 44100;
1409         encoder->protected_->blocksize = 1152;
1410         encoder->protected_->max_lpc_order = 0;
1411         encoder->protected_->qlp_coeff_precision = 0;
1412         encoder->protected_->do_qlp_coeff_prec_search = false;
1413         encoder->protected_->do_exhaustive_model_search = false;
1414         encoder->protected_->do_escape_coding = false;
1415         encoder->protected_->min_residual_partition_order = 0;
1416         encoder->protected_->max_residual_partition_order = 0;
1417         encoder->protected_->rice_parameter_search_dist = 0;
1418         encoder->protected_->total_samples_estimate = 0;
1419         encoder->protected_->metadata = 0;
1420         encoder->protected_->num_metadata_blocks = 0;
1421
1422         encoder->private_->write_callback = 0;
1423         encoder->private_->metadata_callback = 0;
1424         encoder->private_->client_data = 0;
1425 }
1426
1427 void free_(FLAC__StreamEncoder *encoder)
1428 {
1429         unsigned i, channel;
1430
1431         FLAC__ASSERT(0 != encoder);
1432         for(i = 0; i < encoder->protected_->channels; i++) {
1433                 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1434                         free(encoder->private_->integer_signal_unaligned[i]);
1435                         encoder->private_->integer_signal_unaligned[i] = 0;
1436                 }
1437                 if(0 != encoder->private_->real_signal_unaligned[i]) {
1438                         free(encoder->private_->real_signal_unaligned[i]);
1439                         encoder->private_->real_signal_unaligned[i] = 0;
1440                 }
1441         }
1442         for(i = 0; i < 2; i++) {
1443                 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1444                         free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1445                         encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1446                 }
1447                 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1448                         free(encoder->private_->real_signal_mid_side_unaligned[i]);
1449                         encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1450                 }
1451         }
1452         for(channel = 0; channel < encoder->protected_->channels; channel++) {
1453                 for(i = 0; i < 2; i++) {
1454                         if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1455                                 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1456                                 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1457                         }
1458                 }
1459         }
1460         for(channel = 0; channel < 2; channel++) {
1461                 for(i = 0; i < 2; i++) {
1462                         if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1463                                 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1464                                 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1465                         }
1466                 }
1467         }
1468         if(0 != encoder->private_->abs_residual_unaligned) {
1469                 free(encoder->private_->abs_residual_unaligned);
1470                 encoder->private_->abs_residual_unaligned = 0;
1471         }
1472         if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1473                 free(encoder->private_->abs_residual_partition_sums_unaligned);
1474                 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1475         }
1476         if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1477                 free(encoder->private_->raw_bits_per_partition_unaligned);
1478                 encoder->private_->raw_bits_per_partition_unaligned = 0;
1479         }
1480         if(encoder->protected_->verify) {
1481                 for(i = 0; i < encoder->protected_->channels; i++) {
1482                         if(0 != encoder->private_->verify.input_fifo.data[i]) {
1483                                 free(encoder->private_->verify.input_fifo.data[i]);
1484                                 encoder->private_->verify.input_fifo.data[i] = 0;
1485                         }
1486                 }
1487         }
1488         FLAC__bitbuffer_free(encoder->private_->frame);
1489 }
1490
1491 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
1492 {
1493         FLAC__bool ok;
1494         unsigned i, channel;
1495
1496         FLAC__ASSERT(new_size > 0);
1497         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1498         FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1499
1500         /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1501         if(new_size <= encoder->private_->input_capacity)
1502                 return true;
1503
1504         ok = true;
1505
1506         /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1507          * requires that the input arrays (in our case the integer signals)
1508          * have a buffer of up to 3 zeroes in front (at negative indices) for
1509          * alignment purposes; we use 4 to keep the data well-aligned.
1510          */
1511
1512         for(i = 0; ok && i < encoder->protected_->channels; i++) {
1513                 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1514                 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1515                 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1516                 encoder->private_->integer_signal[i] += 4;
1517         }
1518         for(i = 0; ok && i < 2; i++) {
1519                 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
1520                 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
1521                 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1522                 encoder->private_->integer_signal_mid_side[i] += 4;
1523         }
1524         for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1525                 for(i = 0; ok && i < 2; i++) {
1526                         ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1527                 }
1528         }
1529         for(channel = 0; ok && channel < 2; channel++) {
1530                 for(i = 0; ok && i < 2; i++) {
1531                         ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
1532                 }
1533         }
1534         ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1535         if(encoder->private_->precompute_partition_sums || encoder->protected_->do_escape_coding) /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
1536                 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1537         if(encoder->protected_->do_escape_coding)
1538                 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1539
1540         if(ok)
1541                 encoder->private_->input_capacity = new_size;
1542         else
1543                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1544
1545         return ok;
1546 }
1547
1548 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
1549 {
1550         const FLAC__byte *buffer;
1551         unsigned bytes;
1552
1553         FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1554
1555         FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1556
1557         if(encoder->protected_->verify) {
1558                 encoder->private_->verify.output.data = buffer;
1559                 encoder->private_->verify.output.bytes = bytes;
1560                 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1561                         encoder->private_->verify.needs_magic_hack = true;
1562                 }
1563                 else {
1564                         if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1565                                 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1566                                 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1567                                         encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1568                                 return false;
1569                         }
1570                 }
1571         }
1572
1573         if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1574                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1575                 return false;
1576         }
1577
1578         FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1579
1580         if(samples > 0) {
1581                 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1582                 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1583         }
1584
1585         return true;
1586 }
1587
1588 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1589 {
1590         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1591
1592         /*
1593          * Accumulate raw signal to the MD5 signature
1594          */
1595         if(!FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) {
1596                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1597                 return false;
1598         }
1599
1600         /*
1601          * Process the frame header and subframes into the frame bitbuffer
1602          */
1603         if(!process_subframes_(encoder, is_last_frame)) {
1604                 /* the above function sets the state for us in case of an error */
1605                 return false;
1606         }
1607
1608         /*
1609          * Zero-pad the frame to a byte_boundary
1610          */
1611         if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1612                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1613                 return false;
1614         }
1615
1616         /*
1617          * CRC-16 the whole thing
1618          */
1619         FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1620         FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1621
1622         /*
1623          * Write it
1624          */
1625         if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1626                 /* the above function sets the state for us in case of an error */
1627                 return false;
1628         }
1629
1630         /*
1631          * Get ready for the next frame
1632          */
1633         encoder->private_->current_sample_number = 0;
1634         encoder->private_->current_frame_number++;
1635         encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1636
1637         return true;
1638 }
1639
1640 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1641 {
1642         FLAC__FrameHeader frame_header;
1643         unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1644         FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1645
1646         /*
1647          * Calculate the min,max Rice partition orders
1648          */
1649         if(is_last_frame) {
1650                 max_partition_order = 0;
1651         }
1652         else {
1653                 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1654                 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
1655         }
1656         min_partition_order = min(min_partition_order, max_partition_order);
1657
1658         precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1659
1660         /*
1661          * Setup the frame
1662          */
1663         if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1664                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1665                 return false;
1666         }
1667         frame_header.blocksize = encoder->protected_->blocksize;
1668         frame_header.sample_rate = encoder->protected_->sample_rate;
1669         frame_header.channels = encoder->protected_->channels;
1670         frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1671         frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1672         frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1673         frame_header.number.frame_number = encoder->private_->current_frame_number;
1674
1675         /*
1676          * Figure out what channel assignments to try
1677          */
1678         if(encoder->protected_->do_mid_side_stereo) {
1679                 if(encoder->protected_->loose_mid_side_stereo) {
1680                         if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1681                                 do_independent = true;
1682                                 do_mid_side = true;
1683                         }
1684                         else {
1685                                 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1686                                 do_mid_side = !do_independent;
1687                         }
1688                 }
1689                 else {
1690                         do_independent = true;
1691                         do_mid_side = true;
1692                 }
1693         }
1694         else {
1695                 do_independent = true;
1696                 do_mid_side = false;
1697         }
1698
1699         FLAC__ASSERT(do_independent || do_mid_side);
1700
1701         /*
1702          * Check for wasted bits; set effective bps for each subframe
1703          */
1704         if(do_independent) {
1705                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1706                         const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1707                         encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1708                         encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1709                 }
1710         }
1711         if(do_mid_side) {
1712                 FLAC__ASSERT(encoder->protected_->channels == 2);
1713                 for(channel = 0; channel < 2; channel++) {
1714                         const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1715                         encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1716                         encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1717                 }
1718         }
1719
1720         /*
1721          * First do a normal encoding pass of each independent channel
1722          */
1723         if(do_independent) {
1724                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1725                         if(!
1726                                 process_subframe_(
1727                                         encoder,
1728                                         min_partition_order,
1729                                         max_partition_order,
1730                                         precompute_partition_sums,
1731                                         false,
1732                                         &frame_header,
1733                                         encoder->private_->subframe_bps[channel],
1734                                         encoder->private_->integer_signal[channel],
1735                                         encoder->private_->real_signal[channel],
1736                                         encoder->private_->subframe_workspace_ptr[channel],
1737                                         encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1738                                         encoder->private_->residual_workspace[channel],
1739                                         encoder->private_->best_subframe+channel,
1740                                         encoder->private_->best_subframe_bits+channel
1741                                 )
1742                         )
1743                                 return false;
1744                 }
1745         }
1746
1747         /*
1748          * Now do mid and side channels if requested
1749          */
1750         if(do_mid_side) {
1751                 FLAC__ASSERT(encoder->protected_->channels == 2);
1752
1753                 for(channel = 0; channel < 2; channel++) {
1754                         if(!
1755                                 process_subframe_(
1756                                         encoder,
1757                                         min_partition_order,
1758                                         max_partition_order,
1759                                         precompute_partition_sums,
1760                                         false,
1761                                         &frame_header,
1762                                         encoder->private_->subframe_bps_mid_side[channel],
1763                                         encoder->private_->integer_signal_mid_side[channel],
1764                                         encoder->private_->real_signal_mid_side[channel],
1765                                         encoder->private_->subframe_workspace_ptr_mid_side[channel],
1766                                         encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
1767                                         encoder->private_->residual_workspace_mid_side[channel],
1768                                         encoder->private_->best_subframe_mid_side+channel,
1769                                         encoder->private_->best_subframe_bits_mid_side+channel
1770                                 )
1771                         )
1772                                 return false;
1773                 }
1774         }
1775
1776         /*
1777          * Compose the frame bitbuffer
1778          */
1779         if(do_mid_side) {
1780                 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1781                 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1782                 FLAC__ChannelAssignment channel_assignment;
1783
1784                 FLAC__ASSERT(encoder->protected_->channels == 2);
1785
1786                 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1787                         channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1788                 }
1789                 else {
1790                         unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1791                         unsigned min_bits;
1792                         FLAC__ChannelAssignment ca;
1793
1794                         FLAC__ASSERT(do_independent && do_mid_side);
1795
1796                         /* We have to figure out which channel assignent results in the smallest frame */
1797                         bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits         [0] + encoder->private_->best_subframe_bits         [1];
1798                         bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->private_->best_subframe_bits         [0] + encoder->private_->best_subframe_bits_mid_side[1];
1799                         bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits         [1] + encoder->private_->best_subframe_bits_mid_side[1];
1800                         bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
1801
1802                         for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
1803                                 if(bits[ca] < min_bits) {
1804                                         min_bits = bits[ca];
1805                                         channel_assignment = ca;
1806                                 }
1807                         }
1808                 }
1809
1810                 frame_header.channel_assignment = channel_assignment;
1811
1812                 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1813                         encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1814                         return false;
1815                 }
1816
1817                 switch(channel_assignment) {
1818                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1819                                 left_subframe  = &encoder->private_->subframe_workspace         [0][encoder->private_->best_subframe         [0]];
1820                                 right_subframe = &encoder->private_->subframe_workspace         [1][encoder->private_->best_subframe         [1]];
1821                                 break;
1822                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1823                                 left_subframe  = &encoder->private_->subframe_workspace         [0][encoder->private_->best_subframe         [0]];
1824                                 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1825                                 break;
1826                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1827                                 left_subframe  = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1828                                 right_subframe = &encoder->private_->subframe_workspace         [1][encoder->private_->best_subframe         [1]];
1829                                 break;
1830                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1831                                 left_subframe  = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1832                                 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1833                                 break;
1834                         default:
1835                                 FLAC__ASSERT(0);
1836                 }
1837
1838                 switch(channel_assignment) {
1839                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1840                                 left_bps  = encoder->private_->subframe_bps         [0];
1841                                 right_bps = encoder->private_->subframe_bps         [1];
1842                                 break;
1843                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1844                                 left_bps  = encoder->private_->subframe_bps         [0];
1845                                 right_bps = encoder->private_->subframe_bps_mid_side[1];
1846                                 break;
1847                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1848                                 left_bps  = encoder->private_->subframe_bps_mid_side[1];
1849                                 right_bps = encoder->private_->subframe_bps         [1];
1850                                 break;
1851                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1852                                 left_bps  = encoder->private_->subframe_bps_mid_side[0];
1853                                 right_bps = encoder->private_->subframe_bps_mid_side[1];
1854                                 break;
1855                         default:
1856                                 FLAC__ASSERT(0);
1857                 }
1858
1859                 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1860                 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
1861                         return false;
1862                 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1863                         return false;
1864         }
1865         else {
1866                 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1867                         encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1868                         return false;
1869                 }
1870
1871                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1872                         if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
1873                                 /* the above function sets the state for us in case of an error */
1874                                 return false;
1875                         }
1876                 }
1877         }
1878
1879         if(encoder->protected_->loose_mid_side_stereo) {
1880                 encoder->private_->loose_mid_side_stereo_frame_count++;
1881                 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1882                         encoder->private_->loose_mid_side_stereo_frame_count = 0;
1883         }
1884
1885         encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1886
1887         return true;
1888 }
1889
1890 FLAC__bool process_subframe_(
1891         FLAC__StreamEncoder *encoder,
1892         unsigned min_partition_order,
1893         unsigned max_partition_order,
1894         FLAC__bool precompute_partition_sums,
1895         FLAC__bool verbatim_only,
1896         const FLAC__FrameHeader *frame_header,
1897         unsigned subframe_bps,
1898         const FLAC__int32 integer_signal[],
1899         const FLAC__real real_signal[],
1900         FLAC__Subframe *subframe[2],
1901         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
1902         FLAC__int32 *residual[2],
1903         unsigned *best_subframe,
1904         unsigned *best_bits
1905 )
1906 {
1907         FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1908         FLAC__real lpc_residual_bits_per_sample;
1909         FLAC__real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->protected_->max_lpc_order might be less; some asm routines need all the space */
1910         FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1911         unsigned min_lpc_order, max_lpc_order, lpc_order;
1912         unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1913         unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1914         unsigned rice_parameter;
1915         unsigned _candidate_bits, _best_bits;
1916         unsigned _best_subframe;
1917
1918         /* verbatim subframe is the baseline against which we measure other compressed subframes */
1919         _best_subframe = 0;
1920         _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1921
1922         if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1923                 /* check for constant subframe */
1924                 guess_fixed_order = encoder->private_->local_fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
1925                 if(fixed_residual_bits_per_sample[1] == 0.0) {
1926                         /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1927                         unsigned i, signal_is_constant = true;
1928                         for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1929                                 if(integer_signal[0] != integer_signal[i]) {
1930                                         signal_is_constant = false;
1931                                         break;
1932                                 }
1933                         }
1934                         if(signal_is_constant) {
1935                                 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1936                                 if(_candidate_bits < _best_bits) {
1937                                         _best_subframe = !_best_subframe;
1938                                         _best_bits = _candidate_bits;
1939                                 }
1940                         }
1941                 }
1942                 else {
1943                         /* encode fixed */
1944                         if(encoder->protected_->do_exhaustive_model_search) {
1945                                 min_fixed_order = 0;
1946                                 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1947                         }
1948                         else {
1949                                 min_fixed_order = max_fixed_order = guess_fixed_order;
1950                         }
1951                         for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1952                                 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1953                                         continue; /* don't even try */
1954                                 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 */
1955 #ifndef FLAC__SYMMETRIC_RICE
1956                                 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1957 #endif
1958                                 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1959 #ifdef DEBUG_VERBOSE
1960                                         fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1961 #endif
1962                                         rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1963                                 }
1964                                 _candidate_bits =
1965                                         evaluate_fixed_subframe_(
1966                                                 encoder,
1967                                                 integer_signal,
1968                                                 residual[!_best_subframe],
1969                                                 encoder->private_->abs_residual,
1970                                                 encoder->private_->abs_residual_partition_sums,
1971                                                 encoder->private_->raw_bits_per_partition,
1972                                                 frame_header->blocksize,
1973                                                 subframe_bps,
1974                                                 fixed_order,
1975                                                 rice_parameter,
1976                                                 min_partition_order,
1977                                                 max_partition_order,
1978                                                 precompute_partition_sums,
1979                                                 encoder->protected_->do_escape_coding,
1980                                                 encoder->protected_->rice_parameter_search_dist,
1981                                                 subframe[!_best_subframe],
1982                                                 partitioned_rice_contents[!_best_subframe]
1983                                         );
1984                                 if(_candidate_bits < _best_bits) {
1985                                         _best_subframe = !_best_subframe;
1986                                         _best_bits = _candidate_bits;
1987                                 }
1988                         }
1989
1990                         /* encode lpc */
1991                         if(encoder->protected_->max_lpc_order > 0) {
1992                                 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
1993                                         max_lpc_order = frame_header->blocksize-1;
1994                                 else
1995                                         max_lpc_order = encoder->protected_->max_lpc_order;
1996                                 if(max_lpc_order > 0) {
1997                                         encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
1998                                         /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
1999                                         if(autoc[0] != 0.0) {
2000                                                 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2001                                                 if(encoder->protected_->do_exhaustive_model_search) {
2002                                                         min_lpc_order = 1;
2003                                                 }
2004                                                 else {
2005                                                         unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
2006                                                         min_lpc_order = max_lpc_order = guess_lpc_order;
2007                                                 }
2008                                                 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2009                                                         lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2010                                                         if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
2011                                                                 continue; /* don't even try */
2012                                                         rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2013 #ifndef FLAC__SYMMETRIC_RICE
2014                                                         rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2015 #endif
2016                                                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2017 #ifdef DEBUG_VERBOSE
2018                                                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2019 #endif
2020                                                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2021                                                         }
2022                                                         if(encoder->protected_->do_qlp_coeff_prec_search) {
2023                                                                 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2024                                                                 /* ensure a 32-bit datapath throughout for 16bps or less */
2025                                                                 if(subframe_bps <= 16)
2026                                                                         max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2027                                                                 else
2028                                                                         max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2029                                                         }
2030                                                         else {
2031                                                                 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2032                                                         }
2033                                                         for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2034                                                                 _candidate_bits =
2035                                                                         evaluate_lpc_subframe_(
2036                                                                                 encoder,
2037                                                                                 integer_signal,
2038                                                                                 residual[!_best_subframe],
2039                                                                                 encoder->private_->abs_residual,
2040                                                                                 encoder->private_->abs_residual_partition_sums,
2041                                                                                 encoder->private_->raw_bits_per_partition,
2042                                                                                 encoder->private_->lp_coeff[lpc_order-1],
2043                                                                                 frame_header->blocksize,
2044                                                                                 subframe_bps,
2045                                                                                 lpc_order,
2046                                                                                 qlp_coeff_precision,
2047                                                                                 rice_parameter,
2048                                                                                 min_partition_order,
2049                                                                                 max_partition_order,
2050                                                                                 precompute_partition_sums,
2051                                                                                 encoder->protected_->do_escape_coding,
2052                                                                                 encoder->protected_->rice_parameter_search_dist,
2053                                                                                 subframe[!_best_subframe],
2054                                                                                 partitioned_rice_contents[!_best_subframe]
2055                                                                         );
2056                                                                 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2057                                                                         if(_candidate_bits < _best_bits) {
2058                                                                                 _best_subframe = !_best_subframe;
2059                                                                                 _best_bits = _candidate_bits;
2060                                                                         }
2061                                                                 }
2062                                                         }
2063                                                 }
2064                                         }
2065                                 }
2066                         }
2067                 }
2068         }
2069
2070         *best_subframe = _best_subframe;
2071         *best_bits = _best_bits;
2072
2073         return true;
2074 }
2075
2076 FLAC__bool add_subframe_(
2077         FLAC__StreamEncoder *encoder,
2078         const FLAC__FrameHeader *frame_header,
2079         unsigned subframe_bps,
2080         const FLAC__Subframe *subframe,
2081         FLAC__BitBuffer *frame
2082 )
2083 {
2084         switch(subframe->type) {
2085                 case FLAC__SUBFRAME_TYPE_CONSTANT:
2086                         if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
2087                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2088                                 return false;
2089                         }
2090                         break;
2091                 case FLAC__SUBFRAME_TYPE_FIXED:
2092                         if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2093                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2094                                 return false;
2095                         }
2096                         break;
2097                 case FLAC__SUBFRAME_TYPE_LPC:
2098                         if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2099                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2100                                 return false;
2101                         }
2102                         break;
2103                 case FLAC__SUBFRAME_TYPE_VERBATIM:
2104                         if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2105                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2106                                 return false;
2107                         }
2108                         break;
2109                 default:
2110                         FLAC__ASSERT(0);
2111         }
2112
2113         return true;
2114 }
2115
2116 unsigned evaluate_constant_subframe_(
2117         const FLAC__int32 signal,
2118         unsigned subframe_bps,
2119         FLAC__Subframe *subframe
2120 )
2121 {
2122         subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2123         subframe->data.constant.value = signal;
2124
2125         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
2126 }
2127
2128 unsigned evaluate_fixed_subframe_(
2129         FLAC__StreamEncoder *encoder,
2130         const FLAC__int32 signal[],
2131         FLAC__int32 residual[],
2132         FLAC__uint32 abs_residual[],
2133         FLAC__uint64 abs_residual_partition_sums[],
2134         unsigned raw_bits_per_partition[],
2135         unsigned blocksize,
2136         unsigned subframe_bps,
2137         unsigned order,
2138         unsigned rice_parameter,
2139         unsigned min_partition_order,
2140         unsigned max_partition_order,
2141         FLAC__bool precompute_partition_sums,
2142         FLAC__bool do_escape_coding,
2143         unsigned rice_parameter_search_dist,
2144         FLAC__Subframe *subframe,
2145         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2146 )
2147 {
2148         unsigned i, residual_bits;
2149         const unsigned residual_samples = blocksize - order;
2150
2151         FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2152
2153         subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2154
2155         subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2156         subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2157         subframe->data.fixed.residual = residual;
2158
2159         residual_bits =
2160                 find_best_partition_order_(
2161                         encoder->private_,
2162                         residual,
2163                         abs_residual,
2164                         abs_residual_partition_sums,
2165                         raw_bits_per_partition,
2166                         residual_samples,
2167                         order,
2168                         rice_parameter,
2169                         min_partition_order,
2170                         max_partition_order,
2171                         precompute_partition_sums,
2172                         do_escape_coding,
2173                         rice_parameter_search_dist,
2174                         &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2175                 );
2176
2177         subframe->data.fixed.order = order;
2178         for(i = 0; i < order; i++)
2179                 subframe->data.fixed.warmup[i] = signal[i];
2180
2181         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
2182 }
2183
2184 unsigned evaluate_lpc_subframe_(
2185         FLAC__StreamEncoder *encoder,
2186         const FLAC__int32 signal[],
2187         FLAC__int32 residual[],
2188         FLAC__uint32 abs_residual[],
2189         FLAC__uint64 abs_residual_partition_sums[],
2190         unsigned raw_bits_per_partition[],
2191         const FLAC__real lp_coeff[],
2192         unsigned blocksize,
2193         unsigned subframe_bps,
2194         unsigned order,
2195         unsigned qlp_coeff_precision,
2196         unsigned rice_parameter,
2197         unsigned min_partition_order,
2198         unsigned max_partition_order,
2199         FLAC__bool precompute_partition_sums,
2200         FLAC__bool do_escape_coding,
2201         unsigned rice_parameter_search_dist,
2202         FLAC__Subframe *subframe,
2203         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2204 )
2205 {
2206         FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
2207         unsigned i, residual_bits;
2208         int quantization, ret;
2209         const unsigned residual_samples = blocksize - order;
2210
2211         /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2212         if(subframe_bps <= 16) {
2213                 FLAC__ASSERT(order > 0);
2214                 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2215                 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2216         }
2217
2218         ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
2219         if(ret != 0)
2220                 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2221
2222         if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2223                 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2224         else if(subframe_bps + qlp_coeff_precision + order <= 32)
2225                 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2226         else
2227                 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2228
2229         subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2230
2231         subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2232         subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2233         subframe->data.lpc.residual = residual;
2234
2235         residual_bits =
2236                 find_best_partition_order_(
2237                         encoder->private_,
2238                         residual,
2239                         abs_residual,
2240                         abs_residual_partition_sums,
2241                         raw_bits_per_partition,
2242                         residual_samples,
2243                         order,
2244                         rice_parameter,
2245                         min_partition_order,
2246                         max_partition_order,
2247                         precompute_partition_sums,
2248                         do_escape_coding,
2249                         rice_parameter_search_dist,
2250                         &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2251                 );
2252
2253         subframe->data.lpc.order = order;
2254         subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2255         subframe->data.lpc.quantization_level = quantization;
2256         memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
2257         for(i = 0; i < order; i++)
2258                 subframe->data.lpc.warmup[i] = signal[i];
2259
2260         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;
2261 }
2262
2263 unsigned evaluate_verbatim_subframe_(
2264         const FLAC__int32 signal[],
2265         unsigned blocksize,
2266         unsigned subframe_bps,
2267         FLAC__Subframe *subframe
2268 )
2269 {
2270         subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2271
2272         subframe->data.verbatim.data = signal;
2273
2274         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
2275 }
2276
2277 unsigned find_best_partition_order_(
2278         FLAC__StreamEncoderPrivate *private_,
2279         const FLAC__int32 residual[],
2280         FLAC__uint32 abs_residual[],
2281         FLAC__uint64 abs_residual_partition_sums[],
2282         unsigned raw_bits_per_partition[],
2283         unsigned residual_samples,
2284         unsigned predictor_order,
2285         unsigned rice_parameter,
2286         unsigned min_partition_order,
2287         unsigned max_partition_order,
2288         FLAC__bool precompute_partition_sums,
2289         FLAC__bool do_escape_coding,
2290         unsigned rice_parameter_search_dist,
2291         FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2292 )
2293 {
2294         FLAC__int32 r;
2295         unsigned residual_bits, best_residual_bits = 0;
2296         unsigned residual_sample;
2297         unsigned best_parameters_index = 0;
2298         const unsigned blocksize = residual_samples + predictor_order;
2299
2300         /* compute abs(residual) for use later */
2301         for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2302                 r = residual[residual_sample];
2303                 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2304         }
2305
2306         max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2307         min_partition_order = min(min_partition_order, max_partition_order);
2308
2309         if(precompute_partition_sums) {
2310                 int partition_order;
2311                 unsigned sum;
2312
2313                 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2314
2315                 if(do_escape_coding)
2316                         precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2317
2318                 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2319 #ifdef DONT_ESTIMATE_RICE_BITS
2320                         if(!
2321                                 set_partitioned_rice_with_precompute_(
2322                                         residual,
2323                                         abs_residual_partition_sums+sum,
2324                                         raw_bits_per_partition+sum,
2325                                         residual_samples,
2326                                         predictor_order,
2327                                         rice_parameter,
2328                                         rice_parameter_search_dist,
2329                                         (unsigned)partition_order,
2330                                         do_escape_coding,
2331                                         &private_->partitioned_rice_contents_extra[!best_parameters_index],
2332                                         &residual_bits
2333                                 )
2334                         )
2335 #else
2336                         if(!
2337                                 set_partitioned_rice_with_precompute_(
2338                                         abs_residual,
2339                                         abs_residual_partition_sums+sum,
2340                                         raw_bits_per_partition+sum,
2341                                         residual_samples,
2342                                         predictor_order,
2343                                         rice_parameter,
2344                                         rice_parameter_search_dist,
2345                                         (unsigned)partition_order,
2346                                         do_escape_coding,
2347                                         &private_->partitioned_rice_contents_extra[!best_parameters_index],
2348                                         &residual_bits
2349                                 )
2350                         )
2351 #endif
2352                         {
2353                                 FLAC__ASSERT(best_residual_bits != 0);
2354                                 break;
2355                         }
2356                         sum += 1u << partition_order;
2357                         if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2358                                 best_residual_bits = residual_bits;
2359                                 best_parameters_index = !best_parameters_index;
2360                                 best_partitioned_rice->order = partition_order;
2361                         }
2362                 }
2363         }
2364         else {
2365                 unsigned partition_order;
2366                 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2367 #ifdef DONT_ESTIMATE_RICE_BITS
2368                         if(!
2369                                 set_partitioned_rice_(
2370                                         abs_residual,
2371                                         residual,
2372                                         residual_samples,
2373                                         predictor_order,
2374                                         rice_parameter,
2375                                         rice_parameter_search_dist,
2376                                         partition_order,
2377                                         &private_->partitioned_rice_contents_extra[!best_parameters_index],
2378                                         &residual_bits
2379                                 )
2380                         )
2381 #else
2382                         if(!
2383                                 set_partitioned_rice_(
2384                                         abs_residual,
2385                                         residual_samples,
2386                                         predictor_order,
2387                                         rice_parameter,
2388                                         rice_parameter_search_dist,
2389                                         partition_order,
2390                                         &private_->partitioned_rice_contents_extra[!best_parameters_index],
2391                                         &residual_bits
2392                                 )
2393                         )
2394 #endif
2395                         {
2396                                 FLAC__ASSERT(best_residual_bits != 0);
2397                                 break;
2398                         }
2399                         if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2400                                 best_residual_bits = residual_bits;
2401                                 best_parameters_index = !best_parameters_index;
2402                                 best_partitioned_rice->order = partition_order;
2403                         }
2404                 }
2405         }
2406
2407         /*
2408          * We are allowed to de-const the pointer based on our special knowledge;
2409          * it is const to the outside world.
2410          */
2411         {
2412                 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2413                 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2414                 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2415                 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2416         }
2417
2418         return best_residual_bits;
2419 }
2420
2421 void precompute_partition_info_sums_(
2422         const FLAC__uint32 abs_residual[],
2423         FLAC__uint64 abs_residual_partition_sums[],
2424         unsigned residual_samples,
2425         unsigned predictor_order,
2426         unsigned min_partition_order,
2427         unsigned max_partition_order
2428 )
2429 {
2430         int partition_order;
2431         unsigned from_partition, to_partition = 0;
2432         const unsigned blocksize = residual_samples + predictor_order;
2433
2434         /* first do max_partition_order */
2435         for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2436                 FLAC__uint64 abs_residual_partition_sum;
2437                 FLAC__uint32 abs_r;
2438                 unsigned partition, partition_sample, partition_samples, residual_sample;
2439                 const unsigned partitions = 1u << partition_order;
2440                 const unsigned default_partition_samples = blocksize >> partition_order;
2441
2442                 FLAC__ASSERT(default_partition_samples > predictor_order);
2443
2444                 for(partition = residual_sample = 0; partition < partitions; partition++) {
2445                         partition_samples = default_partition_samples;
2446                         if(partition == 0)
2447                                 partition_samples -= predictor_order;
2448                         abs_residual_partition_sum = 0;
2449                         for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2450                                 abs_r = abs_residual[residual_sample];
2451                                 abs_residual_partition_sum += abs_r;
2452                                 residual_sample++;
2453                         }
2454                         abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2455                 }
2456                 to_partition = partitions;
2457                 break;
2458         }
2459
2460         /* now merge partitions for lower orders */
2461         for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2462                 FLAC__uint64 s;
2463                 unsigned i;
2464                 const unsigned partitions = 1u << partition_order;
2465                 for(i = 0; i < partitions; i++) {
2466                         s = abs_residual_partition_sums[from_partition];
2467                         from_partition++;
2468                         abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
2469                         from_partition++;
2470                         to_partition++;
2471                 }
2472         }
2473 }
2474
2475 void precompute_partition_info_escapes_(
2476         const FLAC__int32 residual[],
2477         unsigned raw_bits_per_partition[],
2478         unsigned residual_samples,
2479         unsigned predictor_order,
2480         unsigned min_partition_order,
2481         unsigned max_partition_order
2482 )
2483 {
2484         int partition_order;
2485         unsigned from_partition, to_partition = 0;
2486         const unsigned blocksize = residual_samples + predictor_order;
2487
2488         /* first do max_partition_order */
2489         for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2490                 FLAC__int32 r, residual_partition_min, residual_partition_max;
2491                 unsigned silog2_min, silog2_max;
2492                 unsigned partition, partition_sample, partition_samples, residual_sample;
2493                 const unsigned partitions = 1u << partition_order;
2494                 const unsigned default_partition_samples = blocksize >> partition_order;
2495
2496                 FLAC__ASSERT(default_partition_samples > predictor_order);
2497
2498                 for(partition = residual_sample = 0; partition < partitions; partition++) {
2499                         partition_samples = default_partition_samples;
2500                         if(partition == 0)
2501                                 partition_samples -= predictor_order;
2502                         residual_partition_min = residual_partition_max = 0;
2503                         for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2504                                 r = residual[residual_sample];
2505                                 if(r < residual_partition_min)
2506                                         residual_partition_min = r;
2507                                 else if(r > residual_partition_max)
2508                                         residual_partition_max = r;
2509                                 residual_sample++;
2510                         }
2511                         silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2512                         silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2513                         raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
2514                 }
2515                 to_partition = partitions;
2516                 break;
2517         }
2518
2519         /* now merge partitions for lower orders */
2520         for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2521                 unsigned m;
2522                 unsigned i;
2523                 const unsigned partitions = 1u << partition_order;
2524                 for(i = 0; i < partitions; i++) {
2525                         m = raw_bits_per_partition[from_partition];
2526                         from_partition++;
2527                         raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2528                         from_partition++;
2529                         to_partition++;
2530                 }
2531         }
2532 }
2533
2534 #ifdef VARIABLE_RICE_BITS
2535 #undef VARIABLE_RICE_BITS
2536 #endif
2537 #ifndef DONT_ESTIMATE_RICE_BITS
2538 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
2539 #endif
2540
2541 #ifdef DONT_ESTIMATE_RICE_BITS
2542 FLAC__bool set_partitioned_rice_(
2543         const FLAC__uint32 abs_residual[],
2544         const FLAC__int32 residual[],
2545         const unsigned residual_samples,
2546         const unsigned predictor_order,
2547         const unsigned suggested_rice_parameter,
2548         const unsigned rice_parameter_search_dist,
2549         const unsigned partition_order,
2550         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2551         unsigned *bits
2552 )
2553 #else
2554 FLAC__bool set_partitioned_rice_(
2555         const FLAC__uint32 abs_residual[],
2556         const unsigned residual_samples,
2557         const unsigned predictor_order,
2558         const unsigned suggested_rice_parameter,
2559         const unsigned rice_parameter_search_dist,
2560         const unsigned partition_order,
2561         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2562         unsigned *bits
2563 )
2564 #endif
2565 {
2566         unsigned rice_parameter, partition_bits;
2567 #ifndef NO_RICE_SEARCH
2568         unsigned best_partition_bits;
2569         unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2570 #endif
2571         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2572         unsigned *parameters;
2573
2574         FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2575
2576         FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2577         parameters = partitioned_rice_contents->parameters;
2578
2579         if(partition_order == 0) {
2580                 unsigned i;
2581
2582 #ifndef NO_RICE_SEARCH
2583                 if(rice_parameter_search_dist) {
2584                         if(suggested_rice_parameter < rice_parameter_search_dist)
2585                                 min_rice_parameter = 0;
2586                         else
2587                                 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2588                         max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2589                         if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2590 #ifdef DEBUG_VERBOSE
2591                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2592 #endif
2593                                 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2594                         }
2595                 }
2596                 else
2597                         min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2598
2599                 best_partition_bits = 0xffffffff;
2600                 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2601 #endif
2602 #ifdef VARIABLE_RICE_BITS
2603 #ifdef FLAC__SYMMETRIC_RICE
2604                         partition_bits = (2+rice_parameter) * residual_samples;
2605 #else
2606                         const unsigned rice_parameter_estimate = rice_parameter-1;
2607                         partition_bits = (1+rice_parameter) * residual_samples;
2608 #endif
2609 #else
2610                         partition_bits = 0;
2611 #endif
2612                         partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2613                         for(i = 0; i < residual_samples; i++) {
2614 #ifdef VARIABLE_RICE_BITS
2615 #ifdef FLAC__SYMMETRIC_RICE
2616                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2617 #else
2618                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2619 #endif
2620 #else
2621                                 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2622 #endif
2623                         }
2624 #ifndef NO_RICE_SEARCH
2625                         if(partition_bits < best_partition_bits) {
2626                                 best_rice_parameter = rice_parameter;
2627                                 best_partition_bits = partition_bits;
2628                         }
2629                 }
2630 #endif
2631                 parameters[0] = best_rice_parameter;
2632                 bits_ += best_partition_bits;
2633         }
2634         else {
2635                 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2636                 unsigned partition_samples;
2637                 FLAC__uint64 mean, k;
2638                 const unsigned partitions = 1u << partition_order;
2639                 for(partition = residual_sample = 0; partition < partitions; partition++) {
2640                         partition_samples = (residual_samples+predictor_order) >> partition_order;
2641                         if(partition == 0) {
2642                                 if(partition_samples <= predictor_order)
2643                                         return false;
2644                                 else
2645                                         partition_samples -= predictor_order;
2646                         }
2647                         mean = 0;
2648                         save_residual_sample = residual_sample;
2649                         for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2650                                 mean += abs_residual[residual_sample];
2651                         residual_sample = save_residual_sample;
2652 #ifdef FLAC__SYMMETRIC_RICE
2653                         mean += partition_samples >> 1; /* for rounding effect */
2654                         mean /= partition_samples;
2655
2656                         /* calc rice_parameter = floor(log2(mean)) */
2657                         rice_parameter = 0;
2658                         mean>>=1;
2659                         while(mean) {
2660                                 rice_parameter++;
2661                                 mean >>= 1;
2662                         }
2663 #else
2664                         /* calc rice_parameter ala LOCO-I */
2665                         for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2666                                 ;
2667 #endif
2668                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2669 #ifdef DEBUG_VERBOSE
2670                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2671 #endif
2672                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2673                         }
2674
2675 #ifndef NO_RICE_SEARCH
2676                         if(rice_parameter_search_dist) {
2677                                 if(rice_parameter < rice_parameter_search_dist)
2678                                         min_rice_parameter = 0;
2679                                 else
2680                                         min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2681                                 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2682                                 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2683 #ifdef DEBUG_VERBOSE
2684                                         fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2685 #endif
2686                                         max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2687                                 }
2688                         }
2689                         else
2690                                 min_rice_parameter = max_rice_parameter = rice_parameter;
2691
2692                         best_partition_bits = 0xffffffff;
2693                         for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2694 #endif
2695 #ifdef VARIABLE_RICE_BITS
2696 #ifdef FLAC__SYMMETRIC_RICE
2697                                 partition_bits = (2+rice_parameter) * partition_samples;
2698 #else
2699                                 const unsigned rice_parameter_estimate = rice_parameter-1;
2700                                 partition_bits = (1+rice_parameter) * partition_samples;
2701 #endif
2702 #else
2703                                 partition_bits = 0;
2704 #endif
2705                                 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2706                                 save_residual_sample = residual_sample;
2707                                 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2708 #ifdef VARIABLE_RICE_BITS
2709 #ifdef FLAC__SYMMETRIC_RICE
2710                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2711 #else
2712                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2713 #endif
2714 #else
2715                                         partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2716 #endif
2717                                 }
2718 #ifndef NO_RICE_SEARCH
2719                                 if(rice_parameter != max_rice_parameter)
2720                                         residual_sample = save_residual_sample;
2721                                 if(partition_bits < best_partition_bits) {
2722                                         best_rice_parameter = rice_parameter;
2723                                         best_partition_bits = partition_bits;
2724                                 }
2725                         }
2726 #endif
2727                         parameters[partition] = best_rice_parameter;
2728                         bits_ += best_partition_bits;
2729                 }
2730         }
2731
2732         *bits = bits_;
2733         return true;
2734 }
2735
2736 #ifdef DONT_ESTIMATE_RICE_BITS
2737 FLAC__bool set_partitioned_rice_with_precompute_(
2738         const FLAC__int32 residual[],
2739         const FLAC__uint64 abs_residual_partition_sums[],
2740         const unsigned raw_bits_per_partition[],
2741         const unsigned residual_samples,
2742         const unsigned predictor_order,
2743         const unsigned suggested_rice_parameter,
2744         const unsigned rice_parameter_search_dist,
2745         const unsigned partition_order,
2746         const FLAC__bool search_for_escapes,
2747         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2748         unsigned *bits
2749 )
2750 #else
2751 FLAC__bool set_partitioned_rice_with_precompute_(
2752         const FLAC__uint32 abs_residual[],
2753         const FLAC__uint64 abs_residual_partition_sums[],
2754         const unsigned raw_bits_per_partition[],
2755         const unsigned residual_samples,
2756         const unsigned predictor_order,
2757         const unsigned suggested_rice_parameter,
2758         const unsigned rice_parameter_search_dist,
2759         const unsigned partition_order,
2760         const FLAC__bool search_for_escapes,
2761         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2762         unsigned *bits
2763 )
2764 #endif
2765 {
2766         unsigned rice_parameter, partition_bits;
2767 #ifndef NO_RICE_SEARCH
2768         unsigned best_partition_bits;
2769         unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2770 #endif
2771         unsigned flat_bits;
2772         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2773         unsigned *parameters, *raw_bits;
2774
2775         FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2776
2777         FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2778         parameters = partitioned_rice_contents->parameters;
2779         raw_bits = partitioned_rice_contents->raw_bits;
2780
2781         if(partition_order == 0) {
2782                 unsigned i;
2783
2784 #ifndef NO_RICE_SEARCH
2785                 if(rice_parameter_search_dist) {
2786                         if(suggested_rice_parameter < rice_parameter_search_dist)
2787                                 min_rice_parameter = 0;
2788                         else
2789                                 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2790                         max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2791                         if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2792 #ifdef DEBUG_VERBOSE
2793                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2794 #endif
2795                                 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2796                         }
2797                 }
2798                 else
2799                         min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2800
2801                 best_partition_bits = 0xffffffff;
2802                 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2803 #endif
2804 #ifdef VARIABLE_RICE_BITS
2805 #ifdef FLAC__SYMMETRIC_RICE
2806                         partition_bits = (2+rice_parameter) * residual_samples;
2807 #else
2808                         const unsigned rice_parameter_estimate = rice_parameter-1;
2809                         partition_bits = (1+rice_parameter) * residual_samples;
2810 #endif
2811 #else
2812                         partition_bits = 0;
2813 #endif
2814                         partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2815                         for(i = 0; i < residual_samples; i++) {
2816 #ifdef VARIABLE_RICE_BITS
2817 #ifdef FLAC__SYMMETRIC_RICE
2818                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2819 #else
2820                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2821 #endif
2822 #else
2823                                 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2824 #endif
2825                         }
2826 #ifndef NO_RICE_SEARCH
2827                         if(partition_bits < best_partition_bits) {
2828                                 best_rice_parameter = rice_parameter;
2829                                 best_partition_bits = partition_bits;
2830                         }
2831                 }
2832 #endif
2833                 if(search_for_escapes) {
2834                         flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
2835                         if(flat_bits <= best_partition_bits) {
2836                                 raw_bits[0] = raw_bits_per_partition[0];
2837                                 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2838                                 best_partition_bits = flat_bits;
2839                         }
2840                 }
2841                 parameters[0] = best_rice_parameter;
2842                 bits_ += best_partition_bits;
2843         }
2844         else {
2845                 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2846                 unsigned partition_samples;
2847                 FLAC__uint64 mean, k;
2848                 const unsigned partitions = 1u << partition_order;
2849                 for(partition = residual_sample = 0; partition < partitions; partition++) {
2850                         partition_samples = (residual_samples+predictor_order) >> partition_order;
2851                         if(partition == 0) {
2852                                 if(partition_samples <= predictor_order)
2853                                         return false;
2854                                 else
2855                                         partition_samples -= predictor_order;
2856                         }
2857                         mean = abs_residual_partition_sums[partition];
2858 #ifdef FLAC__SYMMETRIC_RICE
2859                         mean += partition_samples >> 1; /* for rounding effect */
2860                         mean /= partition_samples;
2861
2862                         /* calc rice_parameter = floor(log2(mean)) */
2863                         rice_parameter = 0;
2864                         mean>>=1;
2865                         while(mean) {
2866                                 rice_parameter++;
2867                                 mean >>= 1;
2868                         }
2869 #else
2870                         /* calc rice_parameter ala LOCO-I */
2871                         for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2872                                 ;
2873 #endif
2874                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2875 #ifdef DEBUG_VERBOSE
2876                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2877 #endif
2878                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2879                         }
2880
2881 #ifndef NO_RICE_SEARCH
2882                         if(rice_parameter_search_dist) {
2883                                 if(rice_parameter < rice_parameter_search_dist)
2884                                         min_rice_parameter = 0;
2885                                 else
2886                                         min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2887                                 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2888                                 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2889 #ifdef DEBUG_VERBOSE
2890                                         fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2891 #endif
2892                                         max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2893                                 }
2894                         }
2895                         else
2896                                 min_rice_parameter = max_rice_parameter = rice_parameter;
2897
2898                         best_partition_bits = 0xffffffff;
2899                         for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2900 #endif
2901 #ifdef VARIABLE_RICE_BITS
2902 #ifdef FLAC__SYMMETRIC_RICE
2903                                 partition_bits = (2+rice_parameter) * partition_samples;
2904 #else
2905                                 const unsigned rice_parameter_estimate = rice_parameter-1;
2906                                 partition_bits = (1+rice_parameter) * partition_samples;
2907 #endif
2908 #else
2909                                 partition_bits = 0;
2910 #endif
2911                                 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2912                                 save_residual_sample = residual_sample;
2913                                 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2914 #ifdef VARIABLE_RICE_BITS
2915 #ifdef FLAC__SYMMETRIC_RICE
2916                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2917 #else
2918                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2919 #endif
2920 #else
2921                                         partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2922 #endif
2923                                 }
2924 #ifndef NO_RICE_SEARCH
2925                                 if(rice_parameter != max_rice_parameter)
2926                                         residual_sample = save_residual_sample;
2927                                 if(partition_bits < best_partition_bits) {
2928                                         best_rice_parameter = rice_parameter;
2929                                         best_partition_bits = partition_bits;
2930                                 }
2931                         }
2932 #endif
2933                         if(search_for_escapes) {
2934                                 flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
2935                                 if(flat_bits <= best_partition_bits) {
2936                                         raw_bits[partition] = raw_bits_per_partition[partition];
2937                                         best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2938                                         best_partition_bits = flat_bits;
2939                                 }
2940                         }
2941                         parameters[partition] = best_rice_parameter;
2942                         bits_ += best_partition_bits;
2943                 }
2944         }
2945
2946         *bits = bits_;
2947         return true;
2948 }
2949
2950 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
2951 {
2952         unsigned i, shift;
2953         FLAC__int32 x = 0;
2954
2955         for(i = 0; i < samples && !(x&1); i++)
2956                 x |= signal[i];
2957
2958         if(x == 0) {
2959                 shift = 0;
2960         }
2961         else {
2962                 for(shift = 0; !(x&1); shift++)
2963                         x >>= 1;
2964         }
2965
2966         if(shift > 0) {
2967                 for(i = 0; i < samples; i++)
2968                          signal[i] >>= shift;
2969         }
2970
2971         return shift;
2972 }
2973
2974 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
2975 {
2976         unsigned channel;
2977
2978         for(channel = 0; channel < channels; channel++)
2979                 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
2980
2981         fifo->tail += wide_samples;
2982
2983         FLAC__ASSERT(fifo->tail <= fifo->size);
2984 }
2985
2986 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
2987 {
2988         unsigned channel;
2989         unsigned sample, wide_sample;
2990         unsigned tail = fifo->tail;
2991
2992         sample = input_offset * channels;
2993         for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
2994                 for(channel = 0; channel < channels; channel++)
2995                         fifo->data[channel][tail] = input[sample++];
2996                 tail++;
2997         }
2998         fifo->tail = tail;
2999
3000         FLAC__ASSERT(fifo->tail <= fifo->size);
3001 }
3002
3003 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3004 {
3005         FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3006         const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3007         (void)decoder;
3008
3009         if(encoder->private_->verify.needs_magic_hack) {
3010                 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3011                 *bytes = FLAC__STREAM_SYNC_LENGTH;
3012                 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3013                 encoder->private_->verify.needs_magic_hack = false;
3014         }
3015         else {
3016                 if(encoded_bytes == 0) {
3017                         /*
3018                          * If we get here, a FIFO underflow has occurred,
3019                          * which means there is a bug somewhere.
3020                          */
3021                         FLAC__ASSERT(0);
3022                         return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3023                 }
3024                 else if(encoded_bytes < *bytes)
3025                         *bytes = encoded_bytes;
3026                 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3027                 encoder->private_->verify.output.data += *bytes;
3028                 encoder->private_->verify.output.bytes -= *bytes;
3029         }
3030
3031         return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3032 }
3033
3034 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3035 {
3036         FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3037         unsigned channel;
3038         const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3039         const unsigned blocksize = frame->header.blocksize;
3040         const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3041
3042         for(channel = 0; channel < channels; channel++) {
3043                 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3044                         unsigned i, sample = 0;
3045                         FLAC__int32 expect = 0, got = 0;
3046
3047                         for(i = 0; i < blocksize; i++) {
3048                                 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3049                                         sample = i;
3050                                         expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3051                                         got = (FLAC__int32)buffer[channel][i];
3052                                         break;
3053                                 }
3054                         }
3055                         FLAC__ASSERT(i < blocksize);
3056                         FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3057                         encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
3058                         encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
3059                         encoder->private_->verify.error_stats.channel = channel;
3060                         encoder->private_->verify.error_stats.sample = sample;
3061                         encoder->private_->verify.error_stats.expected = expect;
3062                         encoder->private_->verify.error_stats.got = got;
3063                         encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3064                         return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3065                 }
3066         }
3067         /* dequeue the frame from the fifo */
3068         for(channel = 0; channel < channels; channel++) {
3069                 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3070         }
3071         encoder->private_->verify.input_fifo.tail -= blocksize;
3072         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3073 }
3074
3075 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3076 {
3077         (void)decoder, (void)metadata, (void)client_data;
3078 }
3079
3080 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3081 {
3082         FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3083         (void)decoder, (void)status;
3084         encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
3085 }