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