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