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