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