fix bugs returning the wrong meaning from FLAC__stream_encoder_init()
[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 <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h> /* for malloc() */
23 #include <string.h> /* for memcpy() */
24 #include "FLAC/assert.h"
25 #include "FLAC/stream_decoder.h"
26 #include "protected/stream_encoder.h"
27 #include "private/bitbuffer.h"
28 #include "private/bitmath.h"
29 #include "private/crc.h"
30 #include "private/cpu.h"
31 #include "private/fixed.h"
32 #include "private/format.h"
33 #include "private/lpc.h"
34 #include "private/md5.h"
35 #include "private/memory.h"
36 #include "private/stream_encoder_framing.h"
37
38 #ifdef min
39 #undef min
40 #endif
41 #define min(x,y) ((x)<(y)?(x):(y))
42
43 #ifdef max
44 #undef max
45 #endif
46 #define max(x,y) ((x)>(y)?(x):(y))
47
48 typedef struct {
49         FLAC__int32 *data[FLAC__MAX_CHANNELS];
50         unsigned size; /* of each data[] in samples */
51         unsigned tail;
52 } verify_input_fifo;
53
54 typedef struct {
55         const FLAC__byte *data;
56         unsigned capacity;
57         unsigned bytes;
58 } verify_output;
59
60 typedef enum {
61         ENCODER_IN_MAGIC = 0,
62         ENCODER_IN_METADATA = 1,
63         ENCODER_IN_AUDIO = 2
64 } EncoderStateHint;
65
66 /***********************************************************************
67  *
68  * Private class method prototypes
69  *
70  ***********************************************************************/
71
72 static void set_defaults_(FLAC__StreamEncoder *encoder);
73 static void free_(FLAC__StreamEncoder *encoder);
74 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
75 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
76 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
77 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
78
79 static FLAC__bool process_subframe_(
80         FLAC__StreamEncoder *encoder,
81         unsigned min_partition_order,
82         unsigned max_partition_order,
83         FLAC__bool precompute_partition_sums,
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__bool disable_constant_subframes;
343         FLAC__bool disable_fixed_subframes;
344         FLAC__bool disable_verbatim_subframes;
345         FLAC__StreamEncoderWriteCallback write_callback;
346         FLAC__StreamEncoderMetadataCallback metadata_callback;
347         void *client_data;
348         /* unaligned (original) pointers to allocated data */
349         FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
350         FLAC__int32 *integer_signal_mid_side_unaligned[2];
351         FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
352         FLAC__real *real_signal_mid_side_unaligned[2];
353         FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
354         FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
355         FLAC__uint32 *abs_residual_unaligned;
356         FLAC__uint64 *abs_residual_partition_sums_unaligned;
357         unsigned *raw_bits_per_partition_unaligned;
358         /*
359          * These fields have been moved here from private function local
360          * declarations merely to save stack space during encoding.
361          */
362         FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
363         FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
364         /*
365          * The data for the verify section
366          */
367         struct {
368                 FLAC__StreamDecoder *decoder;
369                 EncoderStateHint state_hint;
370                 FLAC__bool needs_magic_hack;
371                 verify_input_fifo input_fifo;
372                 verify_output output;
373                 struct {
374                         FLAC__uint64 absolute_sample;
375                         unsigned frame_number;
376                         unsigned channel;
377                         unsigned sample;
378                         FLAC__int32 expected;
379                         FLAC__int32 got;
380                 } error_stats;
381         } verify;
382         FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
383 } FLAC__StreamEncoderPrivate;
384
385 /***********************************************************************
386  *
387  * Public static class data
388  *
389  ***********************************************************************/
390
391 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
392         "FLAC__STREAM_ENCODER_OK",
393         "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
394         "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
395         "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
396         "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
397         "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
398         "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
399         "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
400         "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
401         "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
402         "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
403         "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
404         "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
405         "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
406         "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
407         "FLAC__STREAM_ENCODER_FRAMING_ERROR",
408         "FLAC__STREAM_ENCODER_INVALID_METADATA",
409         "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
410         "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
411         "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
412         "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
413         "FLAC__STREAM_ENCODER_UNINITIALIZED"
414 };
415
416 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
417         "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
418         "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
419 };
420
421 /***********************************************************************
422  *
423  * Class constructor/destructor
424  *
425  */
426 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
427 {
428         FLAC__StreamEncoder *encoder;
429         unsigned i;
430
431         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
432
433         encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
434         if(encoder == 0) {
435                 return 0;
436         }
437
438         encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
439         if(encoder->protected_ == 0) {
440                 free(encoder);
441                 return 0;
442         }
443
444         encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
445         if(encoder->private_ == 0) {
446                 free(encoder->protected_);
447                 free(encoder);
448                 return 0;
449         }
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 FLAC_API 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_API 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                 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
841         if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
842                 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
843         if(!write_bitbuffer_(encoder, 0)) {
844                 /* the above function sets the state for us in case of an error */
845                 return encoder->protected_->state;
846         }
847
848         /*
849          * Now that the STREAMINFO block is written, we can init this to an
850          * absurdly-high value...
851          */
852         encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
853         /* ... and clear this to 0 */
854         encoder->private_->metadata.data.stream_info.total_samples = 0;
855
856         /*
857          * Check to see if the supplied metadata contains a VORBIS_COMMENT;
858          * if not, we will write an empty one (FLAC__add_metadata_block()
859          * automatically supplies the vendor string).
860          */
861         if(!metadata_has_vorbis_comment) {
862                 FLAC__StreamMetadata vorbis_comment;
863                 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
864                 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
865                 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
866                 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
867                 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
868                 vorbis_comment.data.vorbis_comment.num_comments = 0;
869                 vorbis_comment.data.vorbis_comment.comments = 0;
870                 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
871                         return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
872                 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
873                         return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
874                 if(!write_bitbuffer_(encoder, 0)) {
875                         /* the above function sets the state for us in case of an error */
876                         return encoder->protected_->state;
877                 }
878         }
879
880         /*
881          * write the user's metadata blocks
882          */
883         for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
884                 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
885                 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
886                         return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
887                 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
888                         return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
889                 if(!write_bitbuffer_(encoder, 0)) {
890                         /* the above function sets the state for us in case of an error */
891                         return encoder->protected_->state;
892                 }
893         }
894
895         if(encoder->protected_->verify)
896                 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
897
898         return encoder->protected_->state;
899 }
900
901 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
902 {
903         FLAC__ASSERT(0 != encoder);
904
905         if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
906                 return;
907
908         if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
909                 if(encoder->private_->current_sample_number != 0) {
910                         encoder->protected_->blocksize = encoder->private_->current_sample_number;
911                         process_frame_(encoder, true); /* true => is last frame */
912                 }
913         }
914
915         MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
916
917         if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
918                 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
919         }
920
921         if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
922                 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
923
924         free_(encoder);
925         set_defaults_(encoder);
926
927         encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
928 }
929
930 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
931 {
932         FLAC__ASSERT(0 != encoder);
933         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
934                 return false;
935         encoder->protected_->verify = value;
936         return true;
937 }
938
939 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
940 {
941         FLAC__ASSERT(0 != encoder);
942         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
943                 return false;
944         encoder->protected_->streamable_subset = value;
945         return true;
946 }
947
948 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
949 {
950         FLAC__ASSERT(0 != encoder);
951         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
952                 return false;
953         encoder->protected_->do_mid_side_stereo = value;
954         return true;
955 }
956
957 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
958 {
959         FLAC__ASSERT(0 != encoder);
960         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
961                 return false;
962         encoder->protected_->loose_mid_side_stereo = value;
963         return true;
964 }
965
966 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
967 {
968         FLAC__ASSERT(0 != encoder);
969         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
970                 return false;
971         encoder->protected_->channels = value;
972         return true;
973 }
974
975 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
976 {
977         FLAC__ASSERT(0 != encoder);
978         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
979                 return false;
980         encoder->protected_->bits_per_sample = value;
981         return true;
982 }
983
984 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
985 {
986         FLAC__ASSERT(0 != encoder);
987         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
988                 return false;
989         encoder->protected_->sample_rate = value;
990         return true;
991 }
992
993 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
994 {
995         FLAC__ASSERT(0 != encoder);
996         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
997                 return false;
998         encoder->protected_->blocksize = value;
999         return true;
1000 }
1001
1002 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1003 {
1004         FLAC__ASSERT(0 != encoder);
1005         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1006                 return false;
1007         encoder->protected_->max_lpc_order = value;
1008         return true;
1009 }
1010
1011 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1012 {
1013         FLAC__ASSERT(0 != encoder);
1014         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1015                 return false;
1016         encoder->protected_->qlp_coeff_precision = value;
1017         return true;
1018 }
1019
1020 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1021 {
1022         FLAC__ASSERT(0 != encoder);
1023         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1024                 return false;
1025         encoder->protected_->do_qlp_coeff_prec_search = value;
1026         return true;
1027 }
1028
1029 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1030 {
1031         FLAC__ASSERT(0 != encoder);
1032         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1033                 return false;
1034 #if 0
1035         /*@@@ deprecated: */
1036         encoder->protected_->do_escape_coding = value;
1037 #else
1038         (void)value;
1039 #endif
1040         return true;
1041 }
1042
1043 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1044 {
1045         FLAC__ASSERT(0 != encoder);
1046         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1047                 return false;
1048         encoder->protected_->do_exhaustive_model_search = value;
1049         return true;
1050 }
1051
1052 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1053 {
1054         FLAC__ASSERT(0 != encoder);
1055         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1056                 return false;
1057         encoder->protected_->min_residual_partition_order = value;
1058         return true;
1059 }
1060
1061 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1062 {
1063         FLAC__ASSERT(0 != encoder);
1064         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1065                 return false;
1066         encoder->protected_->max_residual_partition_order = value;
1067         return true;
1068 }
1069
1070 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1071 {
1072         FLAC__ASSERT(0 != encoder);
1073         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1074                 return false;
1075 #if 0
1076         /*@@@ deprecated: */
1077         encoder->protected_->rice_parameter_search_dist = value;
1078 #else
1079         (void)value;
1080 #endif
1081         return true;
1082 }
1083
1084 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1085 {
1086         FLAC__ASSERT(0 != encoder);
1087         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1088                 return false;
1089         encoder->protected_->total_samples_estimate = value;
1090         return true;
1091 }
1092
1093 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1094 {
1095         FLAC__ASSERT(0 != encoder);
1096         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1097                 return false;
1098         encoder->protected_->metadata = metadata;
1099         encoder->protected_->num_metadata_blocks = num_blocks;
1100         return true;
1101 }
1102
1103 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
1104 {
1105         FLAC__ASSERT(0 != encoder);
1106         FLAC__ASSERT(0 != value);
1107         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1108                 return false;
1109         encoder->private_->write_callback = value;
1110         return true;
1111 }
1112
1113 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
1114 {
1115         FLAC__ASSERT(0 != encoder);
1116         FLAC__ASSERT(0 != value);
1117         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1118                 return false;
1119         encoder->private_->metadata_callback = value;
1120         return true;
1121 }
1122
1123 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
1124 {
1125         FLAC__ASSERT(0 != encoder);
1126         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1127                 return false;
1128         encoder->private_->client_data = value;
1129         return true;
1130 }
1131
1132 /*
1133  * These three functions are not static, but not publically exposed in
1134  * include/FLAC/ either.  They are used by the test suite.
1135  */
1136 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1137 {
1138         FLAC__ASSERT(0 != encoder);
1139         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1140                 return false;
1141         encoder->private_->disable_constant_subframes = value;
1142         return true;
1143 }
1144
1145 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1146 {
1147         FLAC__ASSERT(0 != encoder);
1148         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1149                 return false;
1150         encoder->private_->disable_fixed_subframes = value;
1151         return true;
1152 }
1153
1154 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1155 {
1156         FLAC__ASSERT(0 != encoder);
1157         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1158                 return false;
1159         encoder->private_->disable_verbatim_subframes = value;
1160         return true;
1161 }
1162
1163 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1164 {
1165         FLAC__ASSERT(0 != encoder);
1166         return encoder->protected_->state;
1167 }
1168
1169 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1170 {
1171         FLAC__ASSERT(0 != encoder);
1172         if(encoder->protected_->verify)
1173                 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1174         else
1175                 return FLAC__STREAM_DECODER_UNINITIALIZED;
1176 }
1177
1178 FLAC_API 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)
1179 {
1180         FLAC__ASSERT(0 != encoder);
1181         if(0 != absolute_sample)
1182                 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1183         if(0 != frame_number)
1184                 *frame_number = encoder->private_->verify.error_stats.frame_number;
1185         if(0 != channel)
1186                 *channel = encoder->private_->verify.error_stats.channel;
1187         if(0 != sample)
1188                 *sample = encoder->private_->verify.error_stats.sample;
1189         if(0 != expected)
1190                 *expected = encoder->private_->verify.error_stats.expected;
1191         if(0 != got)
1192                 *got = encoder->private_->verify.error_stats.got;
1193 }
1194
1195 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1196 {
1197         FLAC__ASSERT(0 != encoder);
1198         return encoder->protected_->verify;
1199 }
1200
1201 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1202 {
1203         FLAC__ASSERT(0 != encoder);
1204         return encoder->protected_->streamable_subset;
1205 }
1206
1207 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1208 {
1209         FLAC__ASSERT(0 != encoder);
1210         return encoder->protected_->do_mid_side_stereo;
1211 }
1212
1213 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1214 {
1215         FLAC__ASSERT(0 != encoder);
1216         return encoder->protected_->loose_mid_side_stereo;
1217 }
1218
1219 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1220 {
1221         FLAC__ASSERT(0 != encoder);
1222         return encoder->protected_->channels;
1223 }
1224
1225 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1226 {
1227         FLAC__ASSERT(0 != encoder);
1228         return encoder->protected_->bits_per_sample;
1229 }
1230
1231 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1232 {
1233         FLAC__ASSERT(0 != encoder);
1234         return encoder->protected_->sample_rate;
1235 }
1236
1237 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1238 {
1239         FLAC__ASSERT(0 != encoder);
1240         return encoder->protected_->blocksize;
1241 }
1242
1243 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1244 {
1245         FLAC__ASSERT(0 != encoder);
1246         return encoder->protected_->max_lpc_order;
1247 }
1248
1249 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1250 {
1251         FLAC__ASSERT(0 != encoder);
1252         return encoder->protected_->qlp_coeff_precision;
1253 }
1254
1255 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1256 {
1257         FLAC__ASSERT(0 != encoder);
1258         return encoder->protected_->do_qlp_coeff_prec_search;
1259 }
1260
1261 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1262 {
1263         FLAC__ASSERT(0 != encoder);
1264         return encoder->protected_->do_escape_coding;
1265 }
1266
1267 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1268 {
1269         FLAC__ASSERT(0 != encoder);
1270         return encoder->protected_->do_exhaustive_model_search;
1271 }
1272
1273 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1274 {
1275         FLAC__ASSERT(0 != encoder);
1276         return encoder->protected_->min_residual_partition_order;
1277 }
1278
1279 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1280 {
1281         FLAC__ASSERT(0 != encoder);
1282         return encoder->protected_->max_residual_partition_order;
1283 }
1284
1285 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1286 {
1287         FLAC__ASSERT(0 != encoder);
1288         return encoder->protected_->rice_parameter_search_dist;
1289 }
1290
1291 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1292 {
1293         FLAC__ASSERT(0 != encoder);
1294         return encoder->protected_->total_samples_estimate;
1295 }
1296
1297 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1298 {
1299         unsigned i, j, channel;
1300         FLAC__int32 x, mid, side;
1301         const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1302
1303         FLAC__ASSERT(0 != encoder);
1304         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1305
1306         j = 0;
1307         if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1308                 do {
1309                         if(encoder->protected_->verify)
1310                                 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1311
1312                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1313                                 x = mid = side = buffer[0][j];
1314                                 encoder->private_->integer_signal[0][i] = x;
1315                                 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1316                                 x = buffer[1][j];
1317                                 encoder->private_->integer_signal[1][i] = x;
1318                                 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1319                                 mid += x;
1320                                 side -= x;
1321                                 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1322                                 encoder->private_->integer_signal_mid_side[1][i] = side;
1323                                 encoder->private_->integer_signal_mid_side[0][i] = mid;
1324                                 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1325                                 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1326                                 encoder->private_->current_sample_number++;
1327                         }
1328                         if(i == blocksize) {
1329                                 if(!process_frame_(encoder, false)) /* false => not last frame */
1330                                         return false;
1331                         }
1332                 } while(j < samples);
1333         }
1334         else {
1335                 do {
1336                         if(encoder->protected_->verify)
1337                                 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1338
1339                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1340                                 for(channel = 0; channel < channels; channel++) {
1341                                         x = buffer[channel][j];
1342                                         encoder->private_->integer_signal[channel][i] = x;
1343                                         encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1344                                 }
1345                                 encoder->private_->current_sample_number++;
1346                         }
1347                         if(i == blocksize) {
1348                                 if(!process_frame_(encoder, false)) /* false => not last frame */
1349                                         return false;
1350                         }
1351                 } while(j < samples);
1352         }
1353
1354         return true;
1355 }
1356
1357 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1358 {
1359         unsigned i, j, k, channel;
1360         FLAC__int32 x, mid, side;
1361         const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1362
1363         FLAC__ASSERT(0 != encoder);
1364         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1365
1366         j = k = 0;
1367         if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1368                 do {
1369                         if(encoder->protected_->verify)
1370                                 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1371
1372                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1373                                 x = mid = side = buffer[k++];
1374                                 encoder->private_->integer_signal[0][i] = x;
1375                                 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1376                                 x = buffer[k++];
1377                                 encoder->private_->integer_signal[1][i] = x;
1378                                 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1379                                 mid += x;
1380                                 side -= x;
1381                                 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1382                                 encoder->private_->integer_signal_mid_side[1][i] = side;
1383                                 encoder->private_->integer_signal_mid_side[0][i] = mid;
1384                                 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1385                                 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1386                                 encoder->private_->current_sample_number++;
1387                         }
1388                         if(i == blocksize) {
1389                                 if(!process_frame_(encoder, false)) /* false => not last frame */
1390                                         return false;
1391                         }
1392                 } while(j < samples);
1393         }
1394         else {
1395                 do {
1396                         if(encoder->protected_->verify)
1397                                 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1398
1399                         for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1400                                 for(channel = 0; channel < channels; channel++) {
1401                                         x = buffer[k++];
1402                                         encoder->private_->integer_signal[channel][i] = x;
1403                                         encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1404                                 }
1405                                 encoder->private_->current_sample_number++;
1406                         }
1407                         if(i == blocksize) {
1408                                 if(!process_frame_(encoder, false)) /* false => not last frame */
1409                                         return false;
1410                         }
1411                 } while(j < samples);
1412         }
1413
1414         return true;
1415 }
1416
1417 /***********************************************************************
1418  *
1419  * Private class methods
1420  *
1421  ***********************************************************************/
1422
1423 void set_defaults_(FLAC__StreamEncoder *encoder)
1424 {
1425         FLAC__ASSERT(0 != encoder);
1426
1427         encoder->protected_->verify = false;
1428         encoder->protected_->streamable_subset = true;
1429         encoder->protected_->do_mid_side_stereo = false;
1430         encoder->protected_->loose_mid_side_stereo = false;
1431         encoder->protected_->channels = 2;
1432         encoder->protected_->bits_per_sample = 16;
1433         encoder->protected_->sample_rate = 44100;
1434         encoder->protected_->blocksize = 1152;
1435         encoder->protected_->max_lpc_order = 0;
1436         encoder->protected_->qlp_coeff_precision = 0;
1437         encoder->protected_->do_qlp_coeff_prec_search = false;
1438         encoder->protected_->do_exhaustive_model_search = false;
1439         encoder->protected_->do_escape_coding = false;
1440         encoder->protected_->min_residual_partition_order = 0;
1441         encoder->protected_->max_residual_partition_order = 0;
1442         encoder->protected_->rice_parameter_search_dist = 0;
1443         encoder->protected_->total_samples_estimate = 0;
1444         encoder->protected_->metadata = 0;
1445         encoder->protected_->num_metadata_blocks = 0;
1446
1447         encoder->private_->disable_constant_subframes = false;
1448         encoder->private_->disable_fixed_subframes = false;
1449         encoder->private_->disable_verbatim_subframes = false;
1450         encoder->private_->write_callback = 0;
1451         encoder->private_->metadata_callback = 0;
1452         encoder->private_->client_data = 0;
1453 }
1454
1455 void free_(FLAC__StreamEncoder *encoder)
1456 {
1457         unsigned i, channel;
1458
1459         FLAC__ASSERT(0 != encoder);
1460         for(i = 0; i < encoder->protected_->channels; i++) {
1461                 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1462                         free(encoder->private_->integer_signal_unaligned[i]);
1463                         encoder->private_->integer_signal_unaligned[i] = 0;
1464                 }
1465                 if(0 != encoder->private_->real_signal_unaligned[i]) {
1466                         free(encoder->private_->real_signal_unaligned[i]);
1467                         encoder->private_->real_signal_unaligned[i] = 0;
1468                 }
1469         }
1470         for(i = 0; i < 2; i++) {
1471                 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1472                         free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1473                         encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1474                 }
1475                 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1476                         free(encoder->private_->real_signal_mid_side_unaligned[i]);
1477                         encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1478                 }
1479         }
1480         for(channel = 0; channel < encoder->protected_->channels; channel++) {
1481                 for(i = 0; i < 2; i++) {
1482                         if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1483                                 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1484                                 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1485                         }
1486                 }
1487         }
1488         for(channel = 0; channel < 2; channel++) {
1489                 for(i = 0; i < 2; i++) {
1490                         if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1491                                 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1492                                 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1493                         }
1494                 }
1495         }
1496         if(0 != encoder->private_->abs_residual_unaligned) {
1497                 free(encoder->private_->abs_residual_unaligned);
1498                 encoder->private_->abs_residual_unaligned = 0;
1499         }
1500         if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1501                 free(encoder->private_->abs_residual_partition_sums_unaligned);
1502                 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1503         }
1504         if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1505                 free(encoder->private_->raw_bits_per_partition_unaligned);
1506                 encoder->private_->raw_bits_per_partition_unaligned = 0;
1507         }
1508         if(encoder->protected_->verify) {
1509                 for(i = 0; i < encoder->protected_->channels; i++) {
1510                         if(0 != encoder->private_->verify.input_fifo.data[i]) {
1511                                 free(encoder->private_->verify.input_fifo.data[i]);
1512                                 encoder->private_->verify.input_fifo.data[i] = 0;
1513                         }
1514                 }
1515         }
1516         FLAC__bitbuffer_free(encoder->private_->frame);
1517 }
1518
1519 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
1520 {
1521         FLAC__bool ok;
1522         unsigned i, channel;
1523
1524         FLAC__ASSERT(new_size > 0);
1525         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1526         FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1527
1528         /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1529         if(new_size <= encoder->private_->input_capacity)
1530                 return true;
1531
1532         ok = true;
1533
1534         /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1535          * requires that the input arrays (in our case the integer signals)
1536          * have a buffer of up to 3 zeroes in front (at negative indices) for
1537          * alignment purposes; we use 4 to keep the data well-aligned.
1538          */
1539
1540         for(i = 0; ok && i < encoder->protected_->channels; i++) {
1541                 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1542                 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1543                 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1544                 encoder->private_->integer_signal[i] += 4;
1545         }
1546         for(i = 0; ok && i < 2; i++) {
1547                 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]);
1548                 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]);
1549                 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1550                 encoder->private_->integer_signal_mid_side[i] += 4;
1551         }
1552         for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1553                 for(i = 0; ok && i < 2; i++) {
1554                         ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1555                 }
1556         }
1557         for(channel = 0; ok && channel < 2; channel++) {
1558                 for(i = 0; ok && i < 2; i++) {
1559                         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]);
1560                 }
1561         }
1562         ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1563         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 */
1564                 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1565         if(encoder->protected_->do_escape_coding)
1566                 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1567
1568         if(ok)
1569                 encoder->private_->input_capacity = new_size;
1570         else
1571                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1572
1573         return ok;
1574 }
1575
1576 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
1577 {
1578         const FLAC__byte *buffer;
1579         unsigned bytes;
1580
1581         FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1582
1583         FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1584
1585         if(encoder->protected_->verify) {
1586                 encoder->private_->verify.output.data = buffer;
1587                 encoder->private_->verify.output.bytes = bytes;
1588                 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1589                         encoder->private_->verify.needs_magic_hack = true;
1590                 }
1591                 else {
1592                         if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1593                                 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1594                                 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1595                                         encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1596                                 return false;
1597                         }
1598                 }
1599         }
1600
1601         if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1602                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1603                 return false;
1604         }
1605
1606         FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1607
1608         if(samples > 0) {
1609                 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1610                 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1611         }
1612
1613         return true;
1614 }
1615
1616 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1617 {
1618         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1619
1620         /*
1621          * Accumulate raw signal to the MD5 signature
1622          */
1623         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)) {
1624                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1625                 return false;
1626         }
1627
1628         /*
1629          * Process the frame header and subframes into the frame bitbuffer
1630          */
1631         if(!process_subframes_(encoder, is_last_frame)) {
1632                 /* the above function sets the state for us in case of an error */
1633                 return false;
1634         }
1635
1636         /*
1637          * Zero-pad the frame to a byte_boundary
1638          */
1639         if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1640                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1641                 return false;
1642         }
1643
1644         /*
1645          * CRC-16 the whole thing
1646          */
1647         FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1648         FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1649
1650         /*
1651          * Write it
1652          */
1653         if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1654                 /* the above function sets the state for us in case of an error */
1655                 return false;
1656         }
1657
1658         /*
1659          * Get ready for the next frame
1660          */
1661         encoder->private_->current_sample_number = 0;
1662         encoder->private_->current_frame_number++;
1663         encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1664
1665         return true;
1666 }
1667
1668 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1669 {
1670         FLAC__FrameHeader frame_header;
1671         unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1672         FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1673
1674         /*
1675          * Calculate the min,max Rice partition orders
1676          */
1677         if(is_last_frame) {
1678                 max_partition_order = 0;
1679         }
1680         else {
1681                 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1682                 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
1683         }
1684         min_partition_order = min(min_partition_order, max_partition_order);
1685
1686         precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1687
1688         /*
1689          * Setup the frame
1690          */
1691         if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1692                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1693                 return false;
1694         }
1695         frame_header.blocksize = encoder->protected_->blocksize;
1696         frame_header.sample_rate = encoder->protected_->sample_rate;
1697         frame_header.channels = encoder->protected_->channels;
1698         frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1699         frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1700         frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1701         frame_header.number.frame_number = encoder->private_->current_frame_number;
1702
1703         /*
1704          * Figure out what channel assignments to try
1705          */
1706         if(encoder->protected_->do_mid_side_stereo) {
1707                 if(encoder->protected_->loose_mid_side_stereo) {
1708                         if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1709                                 do_independent = true;
1710                                 do_mid_side = true;
1711                         }
1712                         else {
1713                                 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1714                                 do_mid_side = !do_independent;
1715                         }
1716                 }
1717                 else {
1718                         do_independent = true;
1719                         do_mid_side = true;
1720                 }
1721         }
1722         else {
1723                 do_independent = true;
1724                 do_mid_side = false;
1725         }
1726
1727         FLAC__ASSERT(do_independent || do_mid_side);
1728
1729         /*
1730          * Check for wasted bits; set effective bps for each subframe
1731          */
1732         if(do_independent) {
1733                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1734                         const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1735                         encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1736                         encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1737                 }
1738         }
1739         if(do_mid_side) {
1740                 FLAC__ASSERT(encoder->protected_->channels == 2);
1741                 for(channel = 0; channel < 2; channel++) {
1742                         const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1743                         encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1744                         encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1745                 }
1746         }
1747
1748         /*
1749          * First do a normal encoding pass of each independent channel
1750          */
1751         if(do_independent) {
1752                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1753                         if(!
1754                                 process_subframe_(
1755                                         encoder,
1756                                         min_partition_order,
1757                                         max_partition_order,
1758                                         precompute_partition_sums,
1759                                         &frame_header,
1760                                         encoder->private_->subframe_bps[channel],
1761                                         encoder->private_->integer_signal[channel],
1762                                         encoder->private_->real_signal[channel],
1763                                         encoder->private_->subframe_workspace_ptr[channel],
1764                                         encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1765                                         encoder->private_->residual_workspace[channel],
1766                                         encoder->private_->best_subframe+channel,
1767                                         encoder->private_->best_subframe_bits+channel
1768                                 )
1769                         )
1770                                 return false;
1771                 }
1772         }
1773
1774         /*
1775          * Now do mid and side channels if requested
1776          */
1777         if(do_mid_side) {
1778                 FLAC__ASSERT(encoder->protected_->channels == 2);
1779
1780                 for(channel = 0; channel < 2; channel++) {
1781                         if(!
1782                                 process_subframe_(
1783                                         encoder,
1784                                         min_partition_order,
1785                                         max_partition_order,
1786                                         precompute_partition_sums,
1787                                         &frame_header,
1788                                         encoder->private_->subframe_bps_mid_side[channel],
1789                                         encoder->private_->integer_signal_mid_side[channel],
1790                                         encoder->private_->real_signal_mid_side[channel],
1791                                         encoder->private_->subframe_workspace_ptr_mid_side[channel],
1792                                         encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
1793                                         encoder->private_->residual_workspace_mid_side[channel],
1794                                         encoder->private_->best_subframe_mid_side+channel,
1795                                         encoder->private_->best_subframe_bits_mid_side+channel
1796                                 )
1797                         )
1798                                 return false;
1799                 }
1800         }
1801
1802         /*
1803          * Compose the frame bitbuffer
1804          */
1805         if(do_mid_side) {
1806                 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1807                 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1808                 FLAC__ChannelAssignment channel_assignment;
1809
1810                 FLAC__ASSERT(encoder->protected_->channels == 2);
1811
1812                 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1813                         channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1814                 }
1815                 else {
1816                         unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1817                         unsigned min_bits;
1818                         FLAC__ChannelAssignment ca;
1819
1820                         FLAC__ASSERT(do_independent && do_mid_side);
1821
1822                         /* We have to figure out which channel assignent results in the smallest frame */
1823                         bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits         [0] + encoder->private_->best_subframe_bits         [1];
1824                         bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->private_->best_subframe_bits         [0] + encoder->private_->best_subframe_bits_mid_side[1];
1825                         bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits         [1] + encoder->private_->best_subframe_bits_mid_side[1];
1826                         bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
1827
1828                         for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
1829                                 if(bits[ca] < min_bits) {
1830                                         min_bits = bits[ca];
1831                                         channel_assignment = ca;
1832                                 }
1833                         }
1834                 }
1835
1836                 frame_header.channel_assignment = channel_assignment;
1837
1838                 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1839                         encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1840                         return false;
1841                 }
1842
1843                 switch(channel_assignment) {
1844                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1845                                 left_subframe  = &encoder->private_->subframe_workspace         [0][encoder->private_->best_subframe         [0]];
1846                                 right_subframe = &encoder->private_->subframe_workspace         [1][encoder->private_->best_subframe         [1]];
1847                                 break;
1848                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1849                                 left_subframe  = &encoder->private_->subframe_workspace         [0][encoder->private_->best_subframe         [0]];
1850                                 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1851                                 break;
1852                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1853                                 left_subframe  = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1854                                 right_subframe = &encoder->private_->subframe_workspace         [1][encoder->private_->best_subframe         [1]];
1855                                 break;
1856                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1857                                 left_subframe  = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1858                                 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1859                                 break;
1860                         default:
1861                                 FLAC__ASSERT(0);
1862                 }
1863
1864                 switch(channel_assignment) {
1865                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1866                                 left_bps  = encoder->private_->subframe_bps         [0];
1867                                 right_bps = encoder->private_->subframe_bps         [1];
1868                                 break;
1869                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1870                                 left_bps  = encoder->private_->subframe_bps         [0];
1871                                 right_bps = encoder->private_->subframe_bps_mid_side[1];
1872                                 break;
1873                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1874                                 left_bps  = encoder->private_->subframe_bps_mid_side[1];
1875                                 right_bps = encoder->private_->subframe_bps         [1];
1876                                 break;
1877                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1878                                 left_bps  = encoder->private_->subframe_bps_mid_side[0];
1879                                 right_bps = encoder->private_->subframe_bps_mid_side[1];
1880                                 break;
1881                         default:
1882                                 FLAC__ASSERT(0);
1883                 }
1884
1885                 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1886                 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
1887                         return false;
1888                 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1889                         return false;
1890         }
1891         else {
1892                 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1893                         encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1894                         return false;
1895                 }
1896
1897                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1898                         if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
1899                                 /* the above function sets the state for us in case of an error */
1900                                 return false;
1901                         }
1902                 }
1903         }
1904
1905         if(encoder->protected_->loose_mid_side_stereo) {
1906                 encoder->private_->loose_mid_side_stereo_frame_count++;
1907                 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1908                         encoder->private_->loose_mid_side_stereo_frame_count = 0;
1909         }
1910
1911         encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1912
1913         return true;
1914 }
1915
1916 FLAC__bool process_subframe_(
1917         FLAC__StreamEncoder *encoder,
1918         unsigned min_partition_order,
1919         unsigned max_partition_order,
1920         FLAC__bool precompute_partition_sums,
1921         const FLAC__FrameHeader *frame_header,
1922         unsigned subframe_bps,
1923         const FLAC__int32 integer_signal[],
1924         const FLAC__real real_signal[],
1925         FLAC__Subframe *subframe[2],
1926         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
1927         FLAC__int32 *residual[2],
1928         unsigned *best_subframe,
1929         unsigned *best_bits
1930 )
1931 {
1932         FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1933         FLAC__real lpc_residual_bits_per_sample;
1934         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 */
1935         FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1936         unsigned min_lpc_order, max_lpc_order, lpc_order;
1937         unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1938         unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1939         unsigned rice_parameter;
1940         unsigned _candidate_bits, _best_bits;
1941         unsigned _best_subframe;
1942
1943         /* verbatim subframe is the baseline against which we measure other compressed subframes */
1944         _best_subframe = 0;
1945         if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
1946                 _best_bits = UINT_MAX;
1947         else
1948                 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1949
1950         if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1951                 unsigned signal_is_constant = false;
1952                 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);
1953                 /* check for constant subframe */
1954                 if(!encoder->private_->disable_constant_subframes && fixed_residual_bits_per_sample[1] == 0.0) {
1955                         /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1956                         unsigned i;
1957                         signal_is_constant = true;
1958                         for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1959                                 if(integer_signal[0] != integer_signal[i]) {
1960                                         signal_is_constant = false;
1961                                         break;
1962                                 }
1963                         }
1964                 }
1965                 if(signal_is_constant) {
1966                         _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1967                         if(_candidate_bits < _best_bits) {
1968                                 _best_subframe = !_best_subframe;
1969                                 _best_bits = _candidate_bits;
1970                         }
1971                 }
1972                 else {
1973                         if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
1974                                 /* encode fixed */
1975                                 if(encoder->protected_->do_exhaustive_model_search) {
1976                                         min_fixed_order = 0;
1977                                         max_fixed_order = FLAC__MAX_FIXED_ORDER;
1978                                 }
1979                                 else {
1980                                         min_fixed_order = max_fixed_order = guess_fixed_order;
1981                                 }
1982                                 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1983                                         if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1984                                                 continue; /* don't even try */
1985                                         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 */
1986 #ifndef FLAC__SYMMETRIC_RICE
1987                                         rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1988 #endif
1989                                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1990 #ifdef DEBUG_VERBOSE
1991                                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1992 #endif
1993                                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1994                                         }
1995                                         _candidate_bits =
1996                                                 evaluate_fixed_subframe_(
1997                                                         encoder,
1998                                                         integer_signal,
1999                                                         residual[!_best_subframe],
2000                                                         encoder->private_->abs_residual,
2001                                                         encoder->private_->abs_residual_partition_sums,
2002                                                         encoder->private_->raw_bits_per_partition,
2003                                                         frame_header->blocksize,
2004                                                         subframe_bps,
2005                                                         fixed_order,
2006                                                         rice_parameter,
2007                                                         min_partition_order,
2008                                                         max_partition_order,
2009                                                         precompute_partition_sums,
2010                                                         encoder->protected_->do_escape_coding,
2011                                                         encoder->protected_->rice_parameter_search_dist,
2012                                                         subframe[!_best_subframe],
2013                                                         partitioned_rice_contents[!_best_subframe]
2014                                                 );
2015                                         if(_candidate_bits < _best_bits) {
2016                                                 _best_subframe = !_best_subframe;
2017                                                 _best_bits = _candidate_bits;
2018                                         }
2019                                 }
2020                         }
2021
2022                         /* encode lpc */
2023                         if(encoder->protected_->max_lpc_order > 0) {
2024                                 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
2025                                         max_lpc_order = frame_header->blocksize-1;
2026                                 else
2027                                         max_lpc_order = encoder->protected_->max_lpc_order;
2028                                 if(max_lpc_order > 0) {
2029                                         encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2030                                         /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2031                                         if(autoc[0] != 0.0) {
2032                                                 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2033                                                 if(encoder->protected_->do_exhaustive_model_search) {
2034                                                         min_lpc_order = 1;
2035                                                 }
2036                                                 else {
2037                                                         unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
2038                                                         min_lpc_order = max_lpc_order = guess_lpc_order;
2039                                                 }
2040                                                 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2041                                                         lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2042                                                         if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
2043                                                                 continue; /* don't even try */
2044                                                         rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2045 #ifndef FLAC__SYMMETRIC_RICE
2046                                                         rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2047 #endif
2048                                                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2049 #ifdef DEBUG_VERBOSE
2050                                                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2051 #endif
2052                                                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2053                                                         }
2054                                                         if(encoder->protected_->do_qlp_coeff_prec_search) {
2055                                                                 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2056                                                                 /* ensure a 32-bit datapath throughout for 16bps or less */
2057                                                                 if(subframe_bps <= 16)
2058                                                                         max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2059                                                                 else
2060                                                                         max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2061                                                         }
2062                                                         else {
2063                                                                 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2064                                                         }
2065                                                         for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2066                                                                 _candidate_bits =
2067                                                                         evaluate_lpc_subframe_(
2068                                                                                 encoder,
2069                                                                                 integer_signal,
2070                                                                                 residual[!_best_subframe],
2071                                                                                 encoder->private_->abs_residual,
2072                                                                                 encoder->private_->abs_residual_partition_sums,
2073                                                                                 encoder->private_->raw_bits_per_partition,
2074                                                                                 encoder->private_->lp_coeff[lpc_order-1],
2075                                                                                 frame_header->blocksize,
2076                                                                                 subframe_bps,
2077                                                                                 lpc_order,
2078                                                                                 qlp_coeff_precision,
2079                                                                                 rice_parameter,
2080                                                                                 min_partition_order,
2081                                                                                 max_partition_order,
2082                                                                                 precompute_partition_sums,
2083                                                                                 encoder->protected_->do_escape_coding,
2084                                                                                 encoder->protected_->rice_parameter_search_dist,
2085                                                                                 subframe[!_best_subframe],
2086                                                                                 partitioned_rice_contents[!_best_subframe]
2087                                                                         );
2088                                                                 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2089                                                                         if(_candidate_bits < _best_bits) {
2090                                                                                 _best_subframe = !_best_subframe;
2091                                                                                 _best_bits = _candidate_bits;
2092                                                                         }
2093                                                                 }
2094                                                         }
2095                                                 }
2096                                         }
2097                                 }
2098                         }
2099                 }
2100         }
2101
2102         /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2103         if(_best_bits == UINT_MAX) {
2104                 FLAC__ASSERT(_best_subframe == 0);
2105                 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2106         }
2107
2108         *best_subframe = _best_subframe;
2109         *best_bits = _best_bits;
2110
2111         return true;
2112 }
2113
2114 FLAC__bool add_subframe_(
2115         FLAC__StreamEncoder *encoder,
2116         const FLAC__FrameHeader *frame_header,
2117         unsigned subframe_bps,
2118         const FLAC__Subframe *subframe,
2119         FLAC__BitBuffer *frame
2120 )
2121 {
2122         switch(subframe->type) {
2123                 case FLAC__SUBFRAME_TYPE_CONSTANT:
2124 //@@@@fprintf(stderr,"@@@@ add CONSTANT, bps=%u\n",subframe_bps);
2125                         if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
2126                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2127                                 return false;
2128                         }
2129                         break;
2130                 case FLAC__SUBFRAME_TYPE_FIXED:
2131 //@@@@fprintf(stderr,"@@@@ add FIXED, bps=%u, order=%u\n",subframe_bps,subframe->data.fixed.order);
2132                         if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2133                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2134                                 return false;
2135                         }
2136                         break;
2137                 case FLAC__SUBFRAME_TYPE_LPC:
2138 //@@@@fprintf(stderr,"@@@@ add LPC, bps=%u, order=%u, prec=%u, shift=%d\n",subframe_bps,subframe->data.lpc.order,subframe->data.lpc.qlp_coeff_precision,subframe->data.lpc.quantization_level);
2139                         if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2140                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2141                                 return false;
2142                         }
2143                         break;
2144                 case FLAC__SUBFRAME_TYPE_VERBATIM:
2145 //@@@@fprintf(stderr,"@@@@ add VERBATIM, bps=%u\n",subframe_bps);
2146                         if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2147                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2148                                 return false;
2149                         }
2150                         break;
2151                 default:
2152                         FLAC__ASSERT(0);
2153         }
2154
2155         return true;
2156 }
2157
2158 unsigned evaluate_constant_subframe_(
2159         const FLAC__int32 signal,
2160         unsigned subframe_bps,
2161         FLAC__Subframe *subframe
2162 )
2163 {
2164         subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2165         subframe->data.constant.value = signal;
2166
2167         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
2168 }
2169
2170 unsigned evaluate_fixed_subframe_(
2171         FLAC__StreamEncoder *encoder,
2172         const FLAC__int32 signal[],
2173         FLAC__int32 residual[],
2174         FLAC__uint32 abs_residual[],
2175         FLAC__uint64 abs_residual_partition_sums[],
2176         unsigned raw_bits_per_partition[],
2177         unsigned blocksize,
2178         unsigned subframe_bps,
2179         unsigned order,
2180         unsigned rice_parameter,
2181         unsigned min_partition_order,
2182         unsigned max_partition_order,
2183         FLAC__bool precompute_partition_sums,
2184         FLAC__bool do_escape_coding,
2185         unsigned rice_parameter_search_dist,
2186         FLAC__Subframe *subframe,
2187         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2188 )
2189 {
2190         unsigned i, residual_bits;
2191         const unsigned residual_samples = blocksize - order;
2192
2193         FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2194
2195         subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2196
2197         subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2198         subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2199         subframe->data.fixed.residual = residual;
2200
2201         residual_bits =
2202                 find_best_partition_order_(
2203                         encoder->private_,
2204                         residual,
2205                         abs_residual,
2206                         abs_residual_partition_sums,
2207                         raw_bits_per_partition,
2208                         residual_samples,
2209                         order,
2210                         rice_parameter,
2211                         min_partition_order,
2212                         max_partition_order,
2213                         precompute_partition_sums,
2214                         do_escape_coding,
2215                         rice_parameter_search_dist,
2216                         &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2217                 );
2218
2219         subframe->data.fixed.order = order;
2220         for(i = 0; i < order; i++)
2221                 subframe->data.fixed.warmup[i] = signal[i];
2222
2223         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
2224 }
2225
2226 unsigned evaluate_lpc_subframe_(
2227         FLAC__StreamEncoder *encoder,
2228         const FLAC__int32 signal[],
2229         FLAC__int32 residual[],
2230         FLAC__uint32 abs_residual[],
2231         FLAC__uint64 abs_residual_partition_sums[],
2232         unsigned raw_bits_per_partition[],
2233         const FLAC__real lp_coeff[],
2234         unsigned blocksize,
2235         unsigned subframe_bps,
2236         unsigned order,
2237         unsigned qlp_coeff_precision,
2238         unsigned rice_parameter,
2239         unsigned min_partition_order,
2240         unsigned max_partition_order,
2241         FLAC__bool precompute_partition_sums,
2242         FLAC__bool do_escape_coding,
2243         unsigned rice_parameter_search_dist,
2244         FLAC__Subframe *subframe,
2245         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2246 )
2247 {
2248         FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
2249         unsigned i, residual_bits;
2250         int quantization, ret;
2251         const unsigned residual_samples = blocksize - order;
2252
2253         /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2254         if(subframe_bps <= 16) {
2255                 FLAC__ASSERT(order > 0);
2256                 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2257                 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2258         }
2259
2260         ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
2261         if(ret != 0)
2262                 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2263
2264         if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2265                 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2266                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2267                 else
2268                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2269         else
2270                 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2271
2272         subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2273
2274         subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2275         subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2276         subframe->data.lpc.residual = residual;
2277
2278         residual_bits =
2279                 find_best_partition_order_(
2280                         encoder->private_,
2281                         residual,
2282                         abs_residual,
2283                         abs_residual_partition_sums,
2284                         raw_bits_per_partition,
2285                         residual_samples,
2286                         order,
2287                         rice_parameter,
2288                         min_partition_order,
2289                         max_partition_order,
2290                         precompute_partition_sums,
2291                         do_escape_coding,
2292                         rice_parameter_search_dist,
2293                         &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2294                 );
2295
2296         subframe->data.lpc.order = order;
2297         subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2298         subframe->data.lpc.quantization_level = quantization;
2299         memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
2300         for(i = 0; i < order; i++)
2301                 subframe->data.lpc.warmup[i] = signal[i];
2302
2303         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;
2304 }
2305
2306 unsigned evaluate_verbatim_subframe_(
2307         const FLAC__int32 signal[],
2308         unsigned blocksize,
2309         unsigned subframe_bps,
2310         FLAC__Subframe *subframe
2311 )
2312 {
2313         subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2314
2315         subframe->data.verbatim.data = signal;
2316
2317         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
2318 }
2319
2320 unsigned find_best_partition_order_(
2321         FLAC__StreamEncoderPrivate *private_,
2322         const FLAC__int32 residual[],
2323         FLAC__uint32 abs_residual[],
2324         FLAC__uint64 abs_residual_partition_sums[],
2325         unsigned raw_bits_per_partition[],
2326         unsigned residual_samples,
2327         unsigned predictor_order,
2328         unsigned rice_parameter,
2329         unsigned min_partition_order,
2330         unsigned max_partition_order,
2331         FLAC__bool precompute_partition_sums,
2332         FLAC__bool do_escape_coding,
2333         unsigned rice_parameter_search_dist,
2334         FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2335 )
2336 {
2337         FLAC__int32 r;
2338         unsigned residual_bits, best_residual_bits = 0;
2339         unsigned residual_sample;
2340         unsigned best_parameters_index = 0;
2341         const unsigned blocksize = residual_samples + predictor_order;
2342
2343         /* compute abs(residual) for use later */
2344         for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2345                 r = residual[residual_sample];
2346                 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2347         }
2348
2349         max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2350         min_partition_order = min(min_partition_order, max_partition_order);
2351
2352         if(precompute_partition_sums) {
2353                 int partition_order;
2354                 unsigned sum;
2355
2356                 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2357
2358                 if(do_escape_coding)
2359                         precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2360
2361                 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2362 #ifdef DONT_ESTIMATE_RICE_BITS
2363                         if(!
2364                                 set_partitioned_rice_with_precompute_(
2365                                         residual,
2366                                         abs_residual_partition_sums+sum,
2367                                         raw_bits_per_partition+sum,
2368                                         residual_samples,
2369                                         predictor_order,
2370                                         rice_parameter,
2371                                         rice_parameter_search_dist,
2372                                         (unsigned)partition_order,
2373                                         do_escape_coding,
2374                                         &private_->partitioned_rice_contents_extra[!best_parameters_index],
2375                                         &residual_bits
2376                                 )
2377                         )
2378 #else
2379                         if(!
2380                                 set_partitioned_rice_with_precompute_(
2381                                         abs_residual,
2382                                         abs_residual_partition_sums+sum,
2383                                         raw_bits_per_partition+sum,
2384                                         residual_samples,
2385                                         predictor_order,
2386                                         rice_parameter,
2387                                         rice_parameter_search_dist,
2388                                         (unsigned)partition_order,
2389                                         do_escape_coding,
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                         sum += 1u << partition_order;
2400                         if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2401                                 best_residual_bits = residual_bits;
2402                                 best_parameters_index = !best_parameters_index;
2403                                 best_partitioned_rice->order = partition_order;
2404                         }
2405                 }
2406         }
2407         else {
2408                 unsigned partition_order;
2409                 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2410 #ifdef DONT_ESTIMATE_RICE_BITS
2411                         if(!
2412                                 set_partitioned_rice_(
2413                                         abs_residual,
2414                                         residual,
2415                                         residual_samples,
2416                                         predictor_order,
2417                                         rice_parameter,
2418                                         rice_parameter_search_dist,
2419                                         partition_order,
2420                                         &private_->partitioned_rice_contents_extra[!best_parameters_index],
2421                                         &residual_bits
2422                                 )
2423                         )
2424 #else
2425                         if(!
2426                                 set_partitioned_rice_(
2427                                         abs_residual,
2428                                         residual_samples,
2429                                         predictor_order,
2430                                         rice_parameter,
2431                                         rice_parameter_search_dist,
2432                                         partition_order,
2433                                         &private_->partitioned_rice_contents_extra[!best_parameters_index],
2434                                         &residual_bits
2435                                 )
2436                         )
2437 #endif
2438                         {
2439                                 FLAC__ASSERT(best_residual_bits != 0);
2440                                 break;
2441                         }
2442                         if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2443                                 best_residual_bits = residual_bits;
2444                                 best_parameters_index = !best_parameters_index;
2445                                 best_partitioned_rice->order = partition_order;
2446                         }
2447                 }
2448         }
2449
2450         /*
2451          * We are allowed to de-const the pointer based on our special knowledge;
2452          * it is const to the outside world.
2453          */
2454         {
2455                 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2456                 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2457                 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2458                 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2459         }
2460
2461         return best_residual_bits;
2462 }
2463
2464 void precompute_partition_info_sums_(
2465         const FLAC__uint32 abs_residual[],
2466         FLAC__uint64 abs_residual_partition_sums[],
2467         unsigned residual_samples,
2468         unsigned predictor_order,
2469         unsigned min_partition_order,
2470         unsigned max_partition_order
2471 )
2472 {
2473         int partition_order;
2474         unsigned from_partition, to_partition = 0;
2475         const unsigned blocksize = residual_samples + predictor_order;
2476
2477         /* first do max_partition_order */
2478         for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2479                 FLAC__uint64 abs_residual_partition_sum;
2480                 FLAC__uint32 abs_r;
2481                 unsigned partition, partition_sample, partition_samples, residual_sample;
2482                 const unsigned partitions = 1u << partition_order;
2483                 const unsigned default_partition_samples = blocksize >> partition_order;
2484
2485                 FLAC__ASSERT(default_partition_samples > predictor_order);
2486
2487                 for(partition = residual_sample = 0; partition < partitions; partition++) {
2488                         partition_samples = default_partition_samples;
2489                         if(partition == 0)
2490                                 partition_samples -= predictor_order;
2491                         abs_residual_partition_sum = 0;
2492                         for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2493                                 abs_r = abs_residual[residual_sample];
2494                                 abs_residual_partition_sum += abs_r;
2495                                 residual_sample++;
2496                         }
2497                         abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2498                 }
2499                 to_partition = partitions;
2500                 break;
2501         }
2502
2503         /* now merge partitions for lower orders */
2504         for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2505                 FLAC__uint64 s;
2506                 unsigned i;
2507                 const unsigned partitions = 1u << partition_order;
2508                 for(i = 0; i < partitions; i++) {
2509                         s = abs_residual_partition_sums[from_partition];
2510                         from_partition++;
2511                         abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
2512                         from_partition++;
2513                         to_partition++;
2514                 }
2515         }
2516 }
2517
2518 void precompute_partition_info_escapes_(
2519         const FLAC__int32 residual[],
2520         unsigned raw_bits_per_partition[],
2521         unsigned residual_samples,
2522         unsigned predictor_order,
2523         unsigned min_partition_order,
2524         unsigned max_partition_order
2525 )
2526 {
2527         int partition_order;
2528         unsigned from_partition, to_partition = 0;
2529         const unsigned blocksize = residual_samples + predictor_order;
2530
2531         /* first do max_partition_order */
2532         for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2533                 FLAC__int32 r, residual_partition_min, residual_partition_max;
2534                 unsigned silog2_min, silog2_max;
2535                 unsigned partition, partition_sample, partition_samples, residual_sample;
2536                 const unsigned partitions = 1u << partition_order;
2537                 const unsigned default_partition_samples = blocksize >> partition_order;
2538
2539                 FLAC__ASSERT(default_partition_samples > predictor_order);
2540
2541                 for(partition = residual_sample = 0; partition < partitions; partition++) {
2542                         partition_samples = default_partition_samples;
2543                         if(partition == 0)
2544                                 partition_samples -= predictor_order;
2545                         residual_partition_min = residual_partition_max = 0;
2546                         for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2547                                 r = residual[residual_sample];
2548                                 if(r < residual_partition_min)
2549                                         residual_partition_min = r;
2550                                 else if(r > residual_partition_max)
2551                                         residual_partition_max = r;
2552                                 residual_sample++;
2553                         }
2554                         silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2555                         silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2556                         raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
2557                 }
2558                 to_partition = partitions;
2559                 break;
2560         }
2561
2562         /* now merge partitions for lower orders */
2563         for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2564                 unsigned m;
2565                 unsigned i;
2566                 const unsigned partitions = 1u << partition_order;
2567                 for(i = 0; i < partitions; i++) {
2568                         m = raw_bits_per_partition[from_partition];
2569                         from_partition++;
2570                         raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2571                         from_partition++;
2572                         to_partition++;
2573                 }
2574         }
2575 }
2576
2577 #ifdef VARIABLE_RICE_BITS
2578 #undef VARIABLE_RICE_BITS
2579 #endif
2580 #ifndef DONT_ESTIMATE_RICE_BITS
2581 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
2582 #endif
2583
2584 #ifdef DONT_ESTIMATE_RICE_BITS
2585 FLAC__bool set_partitioned_rice_(
2586         const FLAC__uint32 abs_residual[],
2587         const FLAC__int32 residual[],
2588         const unsigned residual_samples,
2589         const unsigned predictor_order,
2590         const unsigned suggested_rice_parameter,
2591         const unsigned rice_parameter_search_dist,
2592         const unsigned partition_order,
2593         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2594         unsigned *bits
2595 )
2596 #else
2597 FLAC__bool set_partitioned_rice_(
2598         const FLAC__uint32 abs_residual[],
2599         const unsigned residual_samples,
2600         const unsigned predictor_order,
2601         const unsigned suggested_rice_parameter,
2602         const unsigned rice_parameter_search_dist,
2603         const unsigned partition_order,
2604         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2605         unsigned *bits
2606 )
2607 #endif
2608 {
2609         unsigned rice_parameter, partition_bits;
2610 #ifndef NO_RICE_SEARCH
2611         unsigned best_partition_bits;
2612         unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2613 #endif
2614         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2615         unsigned *parameters;
2616
2617         FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2618
2619         FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2620         parameters = partitioned_rice_contents->parameters;
2621
2622         if(partition_order == 0) {
2623                 unsigned i;
2624
2625 #ifndef NO_RICE_SEARCH
2626                 if(rice_parameter_search_dist) {
2627                         if(suggested_rice_parameter < rice_parameter_search_dist)
2628                                 min_rice_parameter = 0;
2629                         else
2630                                 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2631                         max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2632                         if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2633 #ifdef DEBUG_VERBOSE
2634                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2635 #endif
2636                                 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2637                         }
2638                 }
2639                 else
2640                         min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2641
2642                 best_partition_bits = 0xffffffff;
2643                 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2644 #endif
2645 #ifdef VARIABLE_RICE_BITS
2646 #ifdef FLAC__SYMMETRIC_RICE
2647                         partition_bits = (2+rice_parameter) * residual_samples;
2648 #else
2649                         const unsigned rice_parameter_estimate = rice_parameter-1;
2650                         partition_bits = (1+rice_parameter) * residual_samples;
2651 #endif
2652 #else
2653                         partition_bits = 0;
2654 #endif
2655                         partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2656                         for(i = 0; i < residual_samples; i++) {
2657 #ifdef VARIABLE_RICE_BITS
2658 #ifdef FLAC__SYMMETRIC_RICE
2659                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2660 #else
2661                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2662 #endif
2663 #else
2664                                 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2665 #endif
2666                         }
2667 #ifndef NO_RICE_SEARCH
2668                         if(partition_bits < best_partition_bits) {
2669                                 best_rice_parameter = rice_parameter;
2670                                 best_partition_bits = partition_bits;
2671                         }
2672                 }
2673 #endif
2674                 parameters[0] = best_rice_parameter;
2675                 bits_ += best_partition_bits;
2676         }
2677         else {
2678                 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2679                 unsigned partition_samples;
2680                 FLAC__uint64 mean, k;
2681                 const unsigned partitions = 1u << partition_order;
2682                 for(partition = residual_sample = 0; partition < partitions; partition++) {
2683                         partition_samples = (residual_samples+predictor_order) >> partition_order;
2684                         if(partition == 0) {
2685                                 if(partition_samples <= predictor_order)
2686                                         return false;
2687                                 else
2688                                         partition_samples -= predictor_order;
2689                         }
2690                         mean = 0;
2691                         save_residual_sample = residual_sample;
2692                         for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2693                                 mean += abs_residual[residual_sample];
2694                         residual_sample = save_residual_sample;
2695 #ifdef FLAC__SYMMETRIC_RICE
2696                         mean += partition_samples >> 1; /* for rounding effect */
2697                         mean /= partition_samples;
2698
2699                         /* calc rice_parameter = floor(log2(mean)) */
2700                         rice_parameter = 0;
2701                         mean>>=1;
2702                         while(mean) {
2703                                 rice_parameter++;
2704                                 mean >>= 1;
2705                         }
2706 #else
2707                         /* calc rice_parameter ala LOCO-I */
2708                         for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2709                                 ;
2710 #endif
2711                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2712 #ifdef DEBUG_VERBOSE
2713                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2714 #endif
2715                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2716                         }
2717
2718 #ifndef NO_RICE_SEARCH
2719                         if(rice_parameter_search_dist) {
2720                                 if(rice_parameter < rice_parameter_search_dist)
2721                                         min_rice_parameter = 0;
2722                                 else
2723                                         min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2724                                 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2725                                 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2726 #ifdef DEBUG_VERBOSE
2727                                         fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2728 #endif
2729                                         max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2730                                 }
2731                         }
2732                         else
2733                                 min_rice_parameter = max_rice_parameter = rice_parameter;
2734
2735                         best_partition_bits = 0xffffffff;
2736                         for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2737 #endif
2738 #ifdef VARIABLE_RICE_BITS
2739 #ifdef FLAC__SYMMETRIC_RICE
2740                                 partition_bits = (2+rice_parameter) * partition_samples;
2741 #else
2742                                 const unsigned rice_parameter_estimate = rice_parameter-1;
2743                                 partition_bits = (1+rice_parameter) * partition_samples;
2744 #endif
2745 #else
2746                                 partition_bits = 0;
2747 #endif
2748                                 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2749                                 save_residual_sample = residual_sample;
2750                                 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2751 #ifdef VARIABLE_RICE_BITS
2752 #ifdef FLAC__SYMMETRIC_RICE
2753                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2754 #else
2755                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2756 #endif
2757 #else
2758                                         partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2759 #endif
2760                                 }
2761 #ifndef NO_RICE_SEARCH
2762                                 if(rice_parameter != max_rice_parameter)
2763                                         residual_sample = save_residual_sample;
2764                                 if(partition_bits < best_partition_bits) {
2765                                         best_rice_parameter = rice_parameter;
2766                                         best_partition_bits = partition_bits;
2767                                 }
2768                         }
2769 #endif
2770                         parameters[partition] = best_rice_parameter;
2771                         bits_ += best_partition_bits;
2772                 }
2773         }
2774
2775         *bits = bits_;
2776         return true;
2777 }
2778
2779 #ifdef DONT_ESTIMATE_RICE_BITS
2780 FLAC__bool set_partitioned_rice_with_precompute_(
2781         const FLAC__int32 residual[],
2782         const FLAC__uint64 abs_residual_partition_sums[],
2783         const unsigned raw_bits_per_partition[],
2784         const unsigned residual_samples,
2785         const unsigned predictor_order,
2786         const unsigned suggested_rice_parameter,
2787         const unsigned rice_parameter_search_dist,
2788         const unsigned partition_order,
2789         const FLAC__bool search_for_escapes,
2790         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2791         unsigned *bits
2792 )
2793 #else
2794 FLAC__bool set_partitioned_rice_with_precompute_(
2795         const FLAC__uint32 abs_residual[],
2796         const FLAC__uint64 abs_residual_partition_sums[],
2797         const unsigned raw_bits_per_partition[],
2798         const unsigned residual_samples,
2799         const unsigned predictor_order,
2800         const unsigned suggested_rice_parameter,
2801         const unsigned rice_parameter_search_dist,
2802         const unsigned partition_order,
2803         const FLAC__bool search_for_escapes,
2804         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2805         unsigned *bits
2806 )
2807 #endif
2808 {
2809         unsigned rice_parameter, partition_bits;
2810 #ifndef NO_RICE_SEARCH
2811         unsigned best_partition_bits;
2812         unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2813 #endif
2814         unsigned flat_bits;
2815         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2816         unsigned *parameters, *raw_bits;
2817
2818         FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2819
2820         FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2821         parameters = partitioned_rice_contents->parameters;
2822         raw_bits = partitioned_rice_contents->raw_bits;
2823
2824         if(partition_order == 0) {
2825                 unsigned i;
2826
2827 #ifndef NO_RICE_SEARCH
2828                 if(rice_parameter_search_dist) {
2829                         if(suggested_rice_parameter < rice_parameter_search_dist)
2830                                 min_rice_parameter = 0;
2831                         else
2832                                 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2833                         max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2834                         if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2835 #ifdef DEBUG_VERBOSE
2836                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2837 #endif
2838                                 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2839                         }
2840                 }
2841                 else
2842                         min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2843
2844                 best_partition_bits = 0xffffffff;
2845                 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2846 #endif
2847 #ifdef VARIABLE_RICE_BITS
2848 #ifdef FLAC__SYMMETRIC_RICE
2849                         partition_bits = (2+rice_parameter) * residual_samples;
2850 #else
2851                         const unsigned rice_parameter_estimate = rice_parameter-1;
2852                         partition_bits = (1+rice_parameter) * residual_samples;
2853 #endif
2854 #else
2855                         partition_bits = 0;
2856 #endif
2857                         partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2858                         for(i = 0; i < residual_samples; i++) {
2859 #ifdef VARIABLE_RICE_BITS
2860 #ifdef FLAC__SYMMETRIC_RICE
2861                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2862 #else
2863                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2864 #endif
2865 #else
2866                                 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2867 #endif
2868                         }
2869 #ifndef NO_RICE_SEARCH
2870                         if(partition_bits < best_partition_bits) {
2871                                 best_rice_parameter = rice_parameter;
2872                                 best_partition_bits = partition_bits;
2873                         }
2874                 }
2875 #endif
2876                 if(search_for_escapes) {
2877                         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;
2878                         if(flat_bits <= best_partition_bits) {
2879                                 raw_bits[0] = raw_bits_per_partition[0];
2880                                 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2881                                 best_partition_bits = flat_bits;
2882                         }
2883                 }
2884                 parameters[0] = best_rice_parameter;
2885                 bits_ += best_partition_bits;
2886         }
2887         else {
2888                 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2889                 unsigned partition_samples;
2890                 FLAC__uint64 mean, k;
2891                 const unsigned partitions = 1u << partition_order;
2892                 for(partition = residual_sample = 0; partition < partitions; partition++) {
2893                         partition_samples = (residual_samples+predictor_order) >> partition_order;
2894                         if(partition == 0) {
2895                                 if(partition_samples <= predictor_order)
2896                                         return false;
2897                                 else
2898                                         partition_samples -= predictor_order;
2899                         }
2900                         mean = abs_residual_partition_sums[partition];
2901 #ifdef FLAC__SYMMETRIC_RICE
2902                         mean += partition_samples >> 1; /* for rounding effect */
2903                         mean /= partition_samples;
2904
2905                         /* calc rice_parameter = floor(log2(mean)) */
2906                         rice_parameter = 0;
2907                         mean>>=1;
2908                         while(mean) {
2909                                 rice_parameter++;
2910                                 mean >>= 1;
2911                         }
2912 #else
2913                         /* calc rice_parameter ala LOCO-I */
2914                         for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2915                                 ;
2916 #endif
2917                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2918 #ifdef DEBUG_VERBOSE
2919                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2920 #endif
2921                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2922                         }
2923
2924 #ifndef NO_RICE_SEARCH
2925                         if(rice_parameter_search_dist) {
2926                                 if(rice_parameter < rice_parameter_search_dist)
2927                                         min_rice_parameter = 0;
2928                                 else
2929                                         min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2930                                 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2931                                 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2932 #ifdef DEBUG_VERBOSE
2933                                         fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2934 #endif
2935                                         max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2936                                 }
2937                         }
2938                         else
2939                                 min_rice_parameter = max_rice_parameter = rice_parameter;
2940
2941                         best_partition_bits = 0xffffffff;
2942                         for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2943 #endif
2944 #ifdef VARIABLE_RICE_BITS
2945 #ifdef FLAC__SYMMETRIC_RICE
2946                                 partition_bits = (2+rice_parameter) * partition_samples;
2947 #else
2948                                 const unsigned rice_parameter_estimate = rice_parameter-1;
2949                                 partition_bits = (1+rice_parameter) * partition_samples;
2950 #endif
2951 #else
2952                                 partition_bits = 0;
2953 #endif
2954                                 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2955                                 save_residual_sample = residual_sample;
2956                                 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2957 #ifdef VARIABLE_RICE_BITS
2958 #ifdef FLAC__SYMMETRIC_RICE
2959                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2960 #else
2961                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2962 #endif
2963 #else
2964                                         partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2965 #endif
2966                                 }
2967 #ifndef NO_RICE_SEARCH
2968                                 if(rice_parameter != max_rice_parameter)
2969                                         residual_sample = save_residual_sample;
2970                                 if(partition_bits < best_partition_bits) {
2971                                         best_rice_parameter = rice_parameter;
2972                                         best_partition_bits = partition_bits;
2973                                 }
2974                         }
2975 #endif
2976                         if(search_for_escapes) {
2977                                 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;
2978                                 if(flat_bits <= best_partition_bits) {
2979                                         raw_bits[partition] = raw_bits_per_partition[partition];
2980                                         best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2981                                         best_partition_bits = flat_bits;
2982                                 }
2983                         }
2984                         parameters[partition] = best_rice_parameter;
2985                         bits_ += best_partition_bits;
2986                 }
2987         }
2988
2989         *bits = bits_;
2990         return true;
2991 }
2992
2993 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
2994 {
2995         unsigned i, shift;
2996         FLAC__int32 x = 0;
2997
2998         for(i = 0; i < samples && !(x&1); i++)
2999                 x |= signal[i];
3000
3001         if(x == 0) {
3002                 shift = 0;
3003         }
3004         else {
3005                 for(shift = 0; !(x&1); shift++)
3006                         x >>= 1;
3007         }
3008
3009         if(shift > 0) {
3010                 for(i = 0; i < samples; i++)
3011                          signal[i] >>= shift;
3012         }
3013
3014         return shift;
3015 }
3016
3017 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3018 {
3019         unsigned channel;
3020
3021         for(channel = 0; channel < channels; channel++)
3022                 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3023
3024         fifo->tail += wide_samples;
3025
3026         FLAC__ASSERT(fifo->tail <= fifo->size);
3027 }
3028
3029 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3030 {
3031         unsigned channel;
3032         unsigned sample, wide_sample;
3033         unsigned tail = fifo->tail;
3034
3035         sample = input_offset * channels;
3036         for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3037                 for(channel = 0; channel < channels; channel++)
3038                         fifo->data[channel][tail] = input[sample++];
3039                 tail++;
3040         }
3041         fifo->tail = tail;
3042
3043         FLAC__ASSERT(fifo->tail <= fifo->size);
3044 }
3045
3046 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3047 {
3048         FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3049         const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3050         (void)decoder;
3051
3052         if(encoder->private_->verify.needs_magic_hack) {
3053                 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3054                 *bytes = FLAC__STREAM_SYNC_LENGTH;
3055                 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3056                 encoder->private_->verify.needs_magic_hack = false;
3057         }
3058         else {
3059                 if(encoded_bytes == 0) {
3060                         /*
3061                          * If we get here, a FIFO underflow has occurred,
3062                          * which means there is a bug somewhere.
3063                          */
3064                         FLAC__ASSERT(0);
3065                         return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3066                 }
3067                 else if(encoded_bytes < *bytes)
3068                         *bytes = encoded_bytes;
3069                 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3070                 encoder->private_->verify.output.data += *bytes;
3071                 encoder->private_->verify.output.bytes -= *bytes;
3072         }
3073
3074         return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3075 }
3076
3077 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3078 {
3079         FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3080         unsigned channel;
3081         const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3082         const unsigned blocksize = frame->header.blocksize;
3083         const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3084
3085         for(channel = 0; channel < channels; channel++) {
3086                 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3087                         unsigned i, sample = 0;
3088                         FLAC__int32 expect = 0, got = 0;
3089
3090                         for(i = 0; i < blocksize; i++) {
3091                                 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3092                                         sample = i;
3093                                         expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3094                                         got = (FLAC__int32)buffer[channel][i];
3095                                         break;
3096                                 }
3097                         }
3098                         FLAC__ASSERT(i < blocksize);
3099                         FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3100                         encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
3101                         encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
3102                         encoder->private_->verify.error_stats.channel = channel;
3103                         encoder->private_->verify.error_stats.sample = sample;
3104                         encoder->private_->verify.error_stats.expected = expect;
3105                         encoder->private_->verify.error_stats.got = got;
3106                         encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3107                         return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3108                 }
3109         }
3110         /* dequeue the frame from the fifo */
3111         for(channel = 0; channel < channels; channel++) {
3112                 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3113         }
3114         encoder->private_->verify.input_fifo.tail -= blocksize;
3115         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3116 }
3117
3118 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3119 {
3120         (void)decoder, (void)metadata, (void)client_data;
3121 }
3122
3123 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3124 {
3125         FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3126         (void)decoder, (void)status;
3127         encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
3128 }