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