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