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