src/libFLAC/stream_encoder.c : Fix typo.
[platform/upstream/flac.git] / src / libFLAC / stream_encoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009  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 #if HAVE_CONFIG_H
33 #  include <config.h>
34 #endif
35
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h> /* for malloc() */
39 #include <string.h> /* for memcpy() */
40 #include <sys/types.h> /* for off_t */
41 #include "FLAC/assert.h"
42 #include "FLAC/stream_decoder.h"
43 #include "share/alloc.h"
44 #include "share/compat.h"
45 #include "protected/stream_encoder.h"
46 #include "private/bitwriter.h"
47 #include "private/bitmath.h"
48 #include "private/crc.h"
49 #include "private/cpu.h"
50 #include "private/fixed.h"
51 #include "private/format.h"
52 #include "private/lpc.h"
53 #include "private/md5.h"
54 #include "private/memory.h"
55 #include "private/macros.h"
56 #if FLAC__HAS_OGG
57 #include "private/ogg_helper.h"
58 #include "private/ogg_mapping.h"
59 #endif
60 #include "private/stream_encoder_framing.h"
61 #include "private/window.h"
62
63
64 /* Exact Rice codeword length calculation is off by default.  The simple
65  * (and fast) estimation (of how many bits a residual value will be
66  * encoded with) in this encoder is very good, almost always yielding
67  * compression within 0.1% of exact calculation.
68  */
69 #undef EXACT_RICE_BITS_CALCULATION
70 /* Rice parameter searching is off by default.  The simple (and fast)
71  * parameter estimation in this encoder is very good, almost always
72  * yielding compression within 0.1% of the optimal parameters.
73  */
74 #undef ENABLE_RICE_PARAMETER_SEARCH
75
76
77 typedef struct {
78         FLAC__int32 *data[FLAC__MAX_CHANNELS];
79         unsigned size; /* of each data[] in samples */
80         unsigned tail;
81 } verify_input_fifo;
82
83 typedef struct {
84         const FLAC__byte *data;
85         unsigned capacity;
86         unsigned bytes;
87 } verify_output;
88
89 typedef enum {
90         ENCODER_IN_MAGIC = 0,
91         ENCODER_IN_METADATA = 1,
92         ENCODER_IN_AUDIO = 2
93 } EncoderStateHint;
94
95 static struct CompressionLevels {
96         FLAC__bool do_mid_side_stereo;
97         FLAC__bool loose_mid_side_stereo;
98         unsigned max_lpc_order;
99         unsigned qlp_coeff_precision;
100         FLAC__bool do_qlp_coeff_prec_search;
101         FLAC__bool do_escape_coding;
102         FLAC__bool do_exhaustive_model_search;
103         unsigned min_residual_partition_order;
104         unsigned max_residual_partition_order;
105         unsigned rice_parameter_search_dist;
106 } compression_levels_[] = {
107         { false, false,  0, 0, false, false, false, 0, 3, 0 },
108         { true , true ,  0, 0, false, false, false, 0, 3, 0 },
109         { true , false,  0, 0, false, false, false, 0, 3, 0 },
110         { false, false,  6, 0, false, false, false, 0, 4, 0 },
111         { true , true ,  8, 0, false, false, false, 0, 4, 0 },
112         { true , false,  8, 0, false, false, false, 0, 5, 0 },
113         { true , false,  8, 0, false, false, false, 0, 6, 0 },
114         { true , false,  8, 0, false, false, true , 0, 6, 0 },
115         { true , false, 12, 0, false, false, true , 0, 6, 0 }
116 };
117
118
119 /***********************************************************************
120  *
121  * Private class method prototypes
122  *
123  ***********************************************************************/
124
125 static void set_defaults_(FLAC__StreamEncoder *encoder);
126 static void free_(FLAC__StreamEncoder *encoder);
127 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize);
128 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block);
129 static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block);
130 static void update_metadata_(const FLAC__StreamEncoder *encoder);
131 #if FLAC__HAS_OGG
132 static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
133 #endif
134 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block);
135 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);
136
137 static FLAC__bool process_subframe_(
138         FLAC__StreamEncoder *encoder,
139         unsigned min_partition_order,
140         unsigned max_partition_order,
141         const FLAC__FrameHeader *frame_header,
142         unsigned subframe_bps,
143         const FLAC__int32 integer_signal[],
144         FLAC__Subframe *subframe[2],
145         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
146         FLAC__int32 *residual[2],
147         unsigned *best_subframe,
148         unsigned *best_bits
149 );
150
151 static FLAC__bool add_subframe_(
152         FLAC__StreamEncoder *encoder,
153         unsigned blocksize,
154         unsigned subframe_bps,
155         const FLAC__Subframe *subframe,
156         FLAC__BitWriter *frame
157 );
158
159 static unsigned evaluate_constant_subframe_(
160         FLAC__StreamEncoder *encoder,
161         const FLAC__int32 signal,
162         unsigned blocksize,
163         unsigned subframe_bps,
164         FLAC__Subframe *subframe
165 );
166
167 static unsigned evaluate_fixed_subframe_(
168         FLAC__StreamEncoder *encoder,
169         const FLAC__int32 signal[],
170         FLAC__int32 residual[],
171         FLAC__uint64 abs_residual_partition_sums[],
172         unsigned raw_bits_per_partition[],
173         unsigned blocksize,
174         unsigned subframe_bps,
175         unsigned order,
176         unsigned rice_parameter,
177         unsigned rice_parameter_limit,
178         unsigned min_partition_order,
179         unsigned max_partition_order,
180         FLAC__bool do_escape_coding,
181         unsigned rice_parameter_search_dist,
182         FLAC__Subframe *subframe,
183         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
184 );
185
186 #ifndef FLAC__INTEGER_ONLY_LIBRARY
187 static unsigned evaluate_lpc_subframe_(
188         FLAC__StreamEncoder *encoder,
189         const FLAC__int32 signal[],
190         FLAC__int32 residual[],
191         FLAC__uint64 abs_residual_partition_sums[],
192         unsigned raw_bits_per_partition[],
193         const FLAC__real lp_coeff[],
194         unsigned blocksize,
195         unsigned subframe_bps,
196         unsigned order,
197         unsigned qlp_coeff_precision,
198         unsigned rice_parameter,
199         unsigned rice_parameter_limit,
200         unsigned min_partition_order,
201         unsigned max_partition_order,
202         FLAC__bool do_escape_coding,
203         unsigned rice_parameter_search_dist,
204         FLAC__Subframe *subframe,
205         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
206 );
207 #endif
208
209 static unsigned evaluate_verbatim_subframe_(
210         FLAC__StreamEncoder *encoder,
211         const FLAC__int32 signal[],
212         unsigned blocksize,
213         unsigned subframe_bps,
214         FLAC__Subframe *subframe
215 );
216
217 static unsigned find_best_partition_order_(
218         struct FLAC__StreamEncoderPrivate *private_,
219         const FLAC__int32 residual[],
220         FLAC__uint64 abs_residual_partition_sums[],
221         unsigned raw_bits_per_partition[],
222         unsigned residual_samples,
223         unsigned predictor_order,
224         unsigned rice_parameter,
225         unsigned rice_parameter_limit,
226         unsigned min_partition_order,
227         unsigned max_partition_order,
228         unsigned bps,
229         FLAC__bool do_escape_coding,
230         unsigned rice_parameter_search_dist,
231         FLAC__EntropyCodingMethod *best_ecm
232 );
233
234 static void precompute_partition_info_sums_(
235         const FLAC__int32 residual[],
236         FLAC__uint64 abs_residual_partition_sums[],
237         unsigned residual_samples,
238         unsigned predictor_order,
239         unsigned min_partition_order,
240         unsigned max_partition_order,
241         unsigned bps
242 );
243
244 static void precompute_partition_info_escapes_(
245         const FLAC__int32 residual[],
246         unsigned raw_bits_per_partition[],
247         unsigned residual_samples,
248         unsigned predictor_order,
249         unsigned min_partition_order,
250         unsigned max_partition_order
251 );
252
253 static FLAC__bool set_partitioned_rice_(
254 #ifdef EXACT_RICE_BITS_CALCULATION
255         const FLAC__int32 residual[],
256 #endif
257         const FLAC__uint64 abs_residual_partition_sums[],
258         const unsigned raw_bits_per_partition[],
259         const unsigned residual_samples,
260         const unsigned predictor_order,
261         const unsigned suggested_rice_parameter,
262         const unsigned rice_parameter_limit,
263         const unsigned rice_parameter_search_dist,
264         const unsigned partition_order,
265         const FLAC__bool search_for_escapes,
266         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
267         unsigned *bits
268 );
269
270 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
271
272 /* verify-related routines: */
273 static void append_to_verify_fifo_(
274         verify_input_fifo *fifo,
275         const FLAC__int32 * const input[],
276         unsigned input_offset,
277         unsigned channels,
278         unsigned wide_samples
279 );
280
281 static void append_to_verify_fifo_interleaved_(
282         verify_input_fifo *fifo,
283         const FLAC__int32 input[],
284         unsigned input_offset,
285         unsigned channels,
286         unsigned wide_samples
287 );
288
289 static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
290 static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
291 static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
292 static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
293
294 static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
295 static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
296 static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
297 static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data);
298 static FILE *get_binary_stdout_(void);
299
300
301 /***********************************************************************
302  *
303  * Private class data
304  *
305  ***********************************************************************/
306
307 typedef struct FLAC__StreamEncoderPrivate {
308         unsigned input_capacity;                          /* current size (in samples) of the signal and residual buffers */
309         FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS];  /* the integer version of the input signal */
310         FLAC__int32 *integer_signal_mid_side[2];          /* the integer version of the mid-side input signal (stereo only) */
311 #ifndef FLAC__INTEGER_ONLY_LIBRARY
312         FLAC__real *real_signal[FLAC__MAX_CHANNELS];      /* (@@@ currently unused) the floating-point version of the input signal */
313         FLAC__real *real_signal_mid_side[2];              /* (@@@ currently unused) the floating-point version of the mid-side input signal (stereo only) */
314         FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
315         FLAC__real *windowed_signal;                      /* the integer_signal[] * current window[] */
316 #endif
317         unsigned subframe_bps[FLAC__MAX_CHANNELS];        /* the effective bits per sample of the input signal (stream bps - wasted bits) */
318         unsigned subframe_bps_mid_side[2];                /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
319         FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
320         FLAC__int32 *residual_workspace_mid_side[2][2];
321         FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
322         FLAC__Subframe subframe_workspace_mid_side[2][2];
323         FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
324         FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
325         FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
326         FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
327         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
328         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
329         unsigned best_subframe[FLAC__MAX_CHANNELS];       /* index (0 or 1) into 2nd dimension of the above workspaces */
330         unsigned best_subframe_mid_side[2];
331         unsigned best_subframe_bits[FLAC__MAX_CHANNELS];  /* size in bits of the best subframe for each channel */
332         unsigned best_subframe_bits_mid_side[2];
333         FLAC__uint64 *abs_residual_partition_sums;        /* workspace where the sum of abs(candidate residual) for each partition is stored */
334         unsigned *raw_bits_per_partition;                 /* workspace where the sum of silog2(candidate residual) for each partition is stored */
335         FLAC__BitWriter *frame;                           /* the current frame being worked on */
336         unsigned loose_mid_side_stereo_frames;            /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
337         unsigned loose_mid_side_stereo_frame_count;       /* number of frames using the current channel assignment */
338         FLAC__ChannelAssignment last_channel_assignment;
339         FLAC__StreamMetadata streaminfo;                  /* scratchpad for STREAMINFO as it is built */
340         FLAC__StreamMetadata_SeekTable *seek_table;       /* pointer into encoder->protected_->metadata_ where the seek table is */
341         unsigned current_sample_number;
342         unsigned current_frame_number;
343         FLAC__MD5Context md5context;
344         FLAC__CPUInfo cpuinfo;
345 #ifndef FLAC__INTEGER_ONLY_LIBRARY
346         unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
347 #else
348         unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
349 #endif
350 #ifndef FLAC__INTEGER_ONLY_LIBRARY
351         void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
352         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[]);
353         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[]);
354         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[]);
355 #endif
356         FLAC__bool use_wide_by_block;          /* use slow 64-bit versions of some functions because of the block size */
357         FLAC__bool use_wide_by_partition;      /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
358         FLAC__bool use_wide_by_order;          /* use slow 64-bit versions of some functions because of the lpc order */
359         FLAC__bool disable_constant_subframes;
360         FLAC__bool disable_fixed_subframes;
361         FLAC__bool disable_verbatim_subframes;
362 #if FLAC__HAS_OGG
363         FLAC__bool is_ogg;
364 #endif
365         FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
366         FLAC__StreamEncoderSeekCallback seek_callback;
367         FLAC__StreamEncoderTellCallback tell_callback;
368         FLAC__StreamEncoderWriteCallback write_callback;
369         FLAC__StreamEncoderMetadataCallback metadata_callback;
370         FLAC__StreamEncoderProgressCallback progress_callback;
371         void *client_data;
372         unsigned first_seekpoint_to_check;
373         FILE *file;                            /* only used when encoding to a file */
374         FLAC__uint64 bytes_written;
375         FLAC__uint64 samples_written;
376         unsigned frames_written;
377         unsigned total_frames_estimate;
378         /* unaligned (original) pointers to allocated data */
379         FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
380         FLAC__int32 *integer_signal_mid_side_unaligned[2];
381 #ifndef FLAC__INTEGER_ONLY_LIBRARY
382         FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) */
383         FLAC__real *real_signal_mid_side_unaligned[2]; /* (@@@ currently unused) */
384         FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
385         FLAC__real *windowed_signal_unaligned;
386 #endif
387         FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
388         FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
389         FLAC__uint64 *abs_residual_partition_sums_unaligned;
390         unsigned *raw_bits_per_partition_unaligned;
391         /*
392          * These fields have been moved here from private function local
393          * declarations merely to save stack space during encoding.
394          */
395 #ifndef FLAC__INTEGER_ONLY_LIBRARY
396         FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
397 #endif
398         FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
399         /*
400          * The data for the verify section
401          */
402         struct {
403                 FLAC__StreamDecoder *decoder;
404                 EncoderStateHint state_hint;
405                 FLAC__bool needs_magic_hack;
406                 verify_input_fifo input_fifo;
407                 verify_output output;
408                 struct {
409                         FLAC__uint64 absolute_sample;
410                         unsigned frame_number;
411                         unsigned channel;
412                         unsigned sample;
413                         FLAC__int32 expected;
414                         FLAC__int32 got;
415                 } error_stats;
416         } verify;
417         FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
418 } FLAC__StreamEncoderPrivate;
419
420 /***********************************************************************
421  *
422  * Public static class data
423  *
424  ***********************************************************************/
425
426 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
427         "FLAC__STREAM_ENCODER_OK",
428         "FLAC__STREAM_ENCODER_UNINITIALIZED",
429         "FLAC__STREAM_ENCODER_OGG_ERROR",
430         "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
431         "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
432         "FLAC__STREAM_ENCODER_CLIENT_ERROR",
433         "FLAC__STREAM_ENCODER_IO_ERROR",
434         "FLAC__STREAM_ENCODER_FRAMING_ERROR",
435         "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
436 };
437
438 FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
439         "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
440         "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
441         "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
442         "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
443         "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
444         "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
445         "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
446         "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
447         "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
448         "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
449         "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
450         "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
451         "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
452         "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
453 };
454
455 FLAC_API const char * const FLAC__StreamEncoderReadStatusString[] = {
456         "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
457         "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
458         "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
459         "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
460 };
461
462 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
463         "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
464         "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
465 };
466
467 FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
468         "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
469         "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
470         "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
471 };
472
473 FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
474         "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
475         "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
476         "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
477 };
478
479 /* Number of samples that will be overread to watch for end of stream.  By
480  * 'overread', we mean that the FLAC__stream_encoder_process*() calls will
481  * always try to read blocksize+1 samples before encoding a block, so that
482  * even if the stream has a total sample count that is an integral multiple
483  * of the blocksize, we will still notice when we are encoding the last
484  * block.  This is needed, for example, to correctly set the end-of-stream
485  * marker in Ogg FLAC.
486  *
487  * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
488  * not really any reason to change it.
489  */
490 static const unsigned OVERREAD_ = 1;
491
492 /***********************************************************************
493  *
494  * Class constructor/destructor
495  *
496  */
497 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void)
498 {
499         FLAC__StreamEncoder *encoder;
500         unsigned i;
501
502         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
503
504         encoder = calloc(1, sizeof(FLAC__StreamEncoder));
505         if(encoder == 0) {
506                 return 0;
507         }
508
509         encoder->protected_ = calloc(1, sizeof(FLAC__StreamEncoderProtected));
510         if(encoder->protected_ == 0) {
511                 free(encoder);
512                 return 0;
513         }
514
515         encoder->private_ = calloc(1, sizeof(FLAC__StreamEncoderPrivate));
516         if(encoder->private_ == 0) {
517                 free(encoder->protected_);
518                 free(encoder);
519                 return 0;
520         }
521
522         encoder->private_->frame = FLAC__bitwriter_new();
523         if(encoder->private_->frame == 0) {
524                 free(encoder->private_);
525                 free(encoder->protected_);
526                 free(encoder);
527                 return 0;
528         }
529
530         encoder->private_->file = 0;
531
532         set_defaults_(encoder);
533
534         encoder->private_->is_being_deleted = false;
535
536         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
537                 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
538                 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
539         }
540         for(i = 0; i < 2; i++) {
541                 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
542                 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
543         }
544         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
545                 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
546                 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
547         }
548         for(i = 0; i < 2; i++) {
549                 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
550                 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
551         }
552
553         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
554                 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
555                 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
556         }
557         for(i = 0; i < 2; i++) {
558                 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
559                 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
560         }
561         for(i = 0; i < 2; i++)
562                 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
563
564         encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
565
566         return encoder;
567 }
568
569 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
570 {
571         unsigned i;
572
573         if (encoder == NULL)
574                 return ;
575
576         FLAC__ASSERT(0 != encoder->protected_);
577         FLAC__ASSERT(0 != encoder->private_);
578         FLAC__ASSERT(0 != encoder->private_->frame);
579
580         encoder->private_->is_being_deleted = true;
581
582         (void)FLAC__stream_encoder_finish(encoder);
583
584         if(0 != encoder->private_->verify.decoder)
585                 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
586
587         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
588                 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
589                 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
590         }
591         for(i = 0; i < 2; i++) {
592                 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
593                 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
594         }
595         for(i = 0; i < 2; i++)
596                 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
597
598         FLAC__bitwriter_delete(encoder->private_->frame);
599         free(encoder->private_);
600         free(encoder->protected_);
601         free(encoder);
602 }
603
604 /***********************************************************************
605  *
606  * Public class methods
607  *
608  ***********************************************************************/
609
610 static FLAC__StreamEncoderInitStatus init_stream_internal_(
611         FLAC__StreamEncoder *encoder,
612         FLAC__StreamEncoderReadCallback read_callback,
613         FLAC__StreamEncoderWriteCallback write_callback,
614         FLAC__StreamEncoderSeekCallback seek_callback,
615         FLAC__StreamEncoderTellCallback tell_callback,
616         FLAC__StreamEncoderMetadataCallback metadata_callback,
617         void *client_data,
618         FLAC__bool is_ogg
619 )
620 {
621         unsigned i;
622         FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
623
624         FLAC__ASSERT(0 != encoder);
625
626         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
627                 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
628
629 #if !FLAC__HAS_OGG
630         if(is_ogg)
631                 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
632 #endif
633
634         if(0 == write_callback || (seek_callback && 0 == tell_callback))
635                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
636
637         if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
638                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
639
640         if(encoder->protected_->channels != 2) {
641                 encoder->protected_->do_mid_side_stereo = false;
642                 encoder->protected_->loose_mid_side_stereo = false;
643         }
644         else if(!encoder->protected_->do_mid_side_stereo)
645                 encoder->protected_->loose_mid_side_stereo = false;
646
647         if(encoder->protected_->bits_per_sample >= 32)
648                 encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */
649
650         if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
651                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
652
653         if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
654                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
655
656         if(encoder->protected_->blocksize == 0) {
657                 if(encoder->protected_->max_lpc_order == 0)
658                         encoder->protected_->blocksize = 1152;
659                 else
660                         encoder->protected_->blocksize = 4096;
661         }
662
663         if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
664                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
665
666         if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
667                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
668
669         if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
670                 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
671
672         if(encoder->protected_->qlp_coeff_precision == 0) {
673                 if(encoder->protected_->bits_per_sample < 16) {
674                         /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
675                         /* @@@ until then we'll make a guess */
676                         encoder->protected_->qlp_coeff_precision = flac_max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
677                 }
678                 else if(encoder->protected_->bits_per_sample == 16) {
679                         if(encoder->protected_->blocksize <= 192)
680                                 encoder->protected_->qlp_coeff_precision = 7;
681                         else if(encoder->protected_->blocksize <= 384)
682                                 encoder->protected_->qlp_coeff_precision = 8;
683                         else if(encoder->protected_->blocksize <= 576)
684                                 encoder->protected_->qlp_coeff_precision = 9;
685                         else if(encoder->protected_->blocksize <= 1152)
686                                 encoder->protected_->qlp_coeff_precision = 10;
687                         else if(encoder->protected_->blocksize <= 2304)
688                                 encoder->protected_->qlp_coeff_precision = 11;
689                         else if(encoder->protected_->blocksize <= 4608)
690                                 encoder->protected_->qlp_coeff_precision = 12;
691                         else
692                                 encoder->protected_->qlp_coeff_precision = 13;
693                 }
694                 else {
695                         if(encoder->protected_->blocksize <= 384)
696                                 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
697                         else if(encoder->protected_->blocksize <= 1152)
698                                 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
699                         else
700                                 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
701                 }
702                 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
703         }
704         else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
705                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
706
707         if(encoder->protected_->streamable_subset) {
708                 if(!FLAC__format_blocksize_is_subset(encoder->protected_->blocksize, encoder->protected_->sample_rate))
709                         return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
710                 if(!FLAC__format_sample_rate_is_subset(encoder->protected_->sample_rate))
711                         return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
712                 if(
713                         encoder->protected_->bits_per_sample != 8 &&
714                         encoder->protected_->bits_per_sample != 12 &&
715                         encoder->protected_->bits_per_sample != 16 &&
716                         encoder->protected_->bits_per_sample != 20 &&
717                         encoder->protected_->bits_per_sample != 24
718                 )
719                         return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
720                 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
721                         return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
722                 if(
723                         encoder->protected_->sample_rate <= 48000 &&
724                         (
725                                 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
726                                 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
727                         )
728                 ) {
729                         return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
730                 }
731         }
732
733         if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
734                 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
735         if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
736                 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
737
738 #if FLAC__HAS_OGG
739         /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
740         if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
741                 unsigned i;
742                 for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
743                         if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
744                                 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
745                                 for( ; i > 0; i--)
746                                         encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
747                                 encoder->protected_->metadata[0] = vc;
748                                 break;
749                         }
750                 }
751         }
752 #endif
753         /* keep track of any SEEKTABLE block */
754         if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
755                 unsigned i;
756                 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
757                         if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
758                                 encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
759                                 break; /* take only the first one */
760                         }
761                 }
762         }
763
764         /* validate metadata */
765         if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
766                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
767         metadata_has_seektable = false;
768         metadata_has_vorbis_comment = false;
769         metadata_picture_has_type1 = false;
770         metadata_picture_has_type2 = false;
771         for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
772                 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
773                 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
774                         return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
775                 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
776                         if(metadata_has_seektable) /* only one is allowed */
777                                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
778                         metadata_has_seektable = true;
779                         if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
780                                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
781                 }
782                 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
783                         if(metadata_has_vorbis_comment) /* only one is allowed */
784                                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
785                         metadata_has_vorbis_comment = true;
786                 }
787                 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
788                         if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
789                                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
790                 }
791                 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
792                         if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
793                                 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
794                         if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
795                                 if(metadata_picture_has_type1) /* there should only be 1 per stream */
796                                         return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
797                                 metadata_picture_has_type1 = true;
798                                 /* standard icon must be 32x32 pixel PNG */
799                                 if(
800                                         m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
801                                         (
802                                                 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
803                                                 m->data.picture.width != 32 ||
804                                                 m->data.picture.height != 32
805                                         )
806                                 )
807                                         return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
808                         }
809                         else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
810                                 if(metadata_picture_has_type2) /* there should only be 1 per stream */
811                                         return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
812                                 metadata_picture_has_type2 = true;
813                         }
814                 }
815         }
816
817         encoder->private_->input_capacity = 0;
818         for(i = 0; i < encoder->protected_->channels; i++) {
819                 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
820 #ifndef FLAC__INTEGER_ONLY_LIBRARY
821                 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
822 #endif
823         }
824         for(i = 0; i < 2; i++) {
825                 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
826 #ifndef FLAC__INTEGER_ONLY_LIBRARY
827                 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
828 #endif
829         }
830 #ifndef FLAC__INTEGER_ONLY_LIBRARY
831         for(i = 0; i < encoder->protected_->num_apodizations; i++)
832                 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
833         encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
834 #endif
835         for(i = 0; i < encoder->protected_->channels; i++) {
836                 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
837                 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
838                 encoder->private_->best_subframe[i] = 0;
839         }
840         for(i = 0; i < 2; i++) {
841                 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
842                 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
843                 encoder->private_->best_subframe_mid_side[i] = 0;
844         }
845         encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
846         encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
847 #ifndef FLAC__INTEGER_ONLY_LIBRARY
848         encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
849 #else
850         /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
851         /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
852         FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
853         FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
854         FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
855         FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
856         encoder->private_->loose_mid_side_stereo_frames = (unsigned)FLAC__fixedpoint_trunc((((FLAC__uint64)(encoder->protected_->sample_rate) * (FLAC__uint64)(26214)) << 16) / (encoder->protected_->blocksize<<16) + FLAC__FP_ONE_HALF);
857 #endif
858         if(encoder->private_->loose_mid_side_stereo_frames == 0)
859                 encoder->private_->loose_mid_side_stereo_frames = 1;
860         encoder->private_->loose_mid_side_stereo_frame_count = 0;
861         encoder->private_->current_sample_number = 0;
862         encoder->private_->current_frame_number = 0;
863
864         encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
865         encoder->private_->use_wide_by_order = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(flac_max(encoder->protected_->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */
866         encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
867
868         /*
869          * get the CPU info and set the function pointers
870          */
871         FLAC__cpu_info(&encoder->private_->cpuinfo);
872         /* first default to the non-asm routines */
873 #ifndef FLAC__INTEGER_ONLY_LIBRARY
874         encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
875 #endif
876         encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
877 #ifndef FLAC__INTEGER_ONLY_LIBRARY
878         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
879         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
880         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
881 #endif
882         /* now override with asm where appropriate */
883 #ifndef FLAC__INTEGER_ONLY_LIBRARY
884 # ifndef FLAC__NO_ASM
885         if(encoder->private_->cpuinfo.use_asm) {
886 #  ifdef FLAC__CPU_IA32
887                 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
888 #   ifdef FLAC__HAS_NASM
889                 if(encoder->private_->cpuinfo.data.ia32.sse) {
890                         if(encoder->protected_->max_lpc_order < 4)
891                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
892                         else if(encoder->protected_->max_lpc_order < 8)
893                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
894                         else if(encoder->protected_->max_lpc_order < 12)
895                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
896                         else
897                                 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
898                 }
899                 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
900                         encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
901                 else
902                         encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
903                 if(encoder->private_->cpuinfo.data.ia32.mmx) {
904                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
905                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
906                 }
907                 else {
908                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
909                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
910                 }
911                 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
912                         encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
913 #   endif /* FLAC__HAS_NASM */
914 #  endif /* FLAC__CPU_IA32 */
915         }
916 # endif /* !FLAC__NO_ASM */
917 #endif /* !FLAC__INTEGER_ONLY_LIBRARY */
918         /* finally override based on wide-ness if necessary */
919         if(encoder->private_->use_wide_by_block) {
920                 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
921         }
922
923         /* set state to OK; from here on, errors are fatal and we'll override the state then */
924         encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
925
926 #if FLAC__HAS_OGG
927         encoder->private_->is_ogg = is_ogg;
928         if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
929                 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
930                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
931         }
932 #endif
933
934         encoder->private_->read_callback = read_callback;
935         encoder->private_->write_callback = write_callback;
936         encoder->private_->seek_callback = seek_callback;
937         encoder->private_->tell_callback = tell_callback;
938         encoder->private_->metadata_callback = metadata_callback;
939         encoder->private_->client_data = client_data;
940
941         if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
942                 /* the above function sets the state for us in case of an error */
943                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
944         }
945
946         if(!FLAC__bitwriter_init(encoder->private_->frame)) {
947                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
948                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
949         }
950
951         /*
952          * Set up the verify stuff if necessary
953          */
954         if(encoder->protected_->verify) {
955                 /*
956                  * First, set up the fifo which will hold the
957                  * original signal to compare against
958                  */
959                 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
960                 for(i = 0; i < encoder->protected_->channels; i++) {
961                         if(0 == (encoder->private_->verify.input_fifo.data[i] = safe_malloc_mul_2op_p(sizeof(FLAC__int32), /*times*/encoder->private_->verify.input_fifo.size))) {
962                                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
963                                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
964                         }
965                 }
966                 encoder->private_->verify.input_fifo.tail = 0;
967
968                 /*
969                  * Now set up a stream decoder for verification
970                  */
971                 if(0 == encoder->private_->verify.decoder) {
972                         encoder->private_->verify.decoder = FLAC__stream_decoder_new();
973                         if(0 == encoder->private_->verify.decoder) {
974                                 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
975                                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
976                         }
977                 }
978
979                 if(FLAC__stream_decoder_init_stream(encoder->private_->verify.decoder, verify_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, verify_write_callback_, verify_metadata_callback_, verify_error_callback_, /*client_data=*/encoder) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
980                         encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
981                         return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
982                 }
983         }
984         encoder->private_->verify.error_stats.absolute_sample = 0;
985         encoder->private_->verify.error_stats.frame_number = 0;
986         encoder->private_->verify.error_stats.channel = 0;
987         encoder->private_->verify.error_stats.sample = 0;
988         encoder->private_->verify.error_stats.expected = 0;
989         encoder->private_->verify.error_stats.got = 0;
990
991         /*
992          * These must be done before we write any metadata, because that
993          * calls the write_callback, which uses these values.
994          */
995         encoder->private_->first_seekpoint_to_check = 0;
996         encoder->private_->samples_written = 0;
997         encoder->protected_->streaminfo_offset = 0;
998         encoder->protected_->seektable_offset = 0;
999         encoder->protected_->audio_offset = 0;
1000
1001         /*
1002          * write the stream header
1003          */
1004         if(encoder->protected_->verify)
1005                 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
1006         if(!FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
1007                 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1008                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1009         }
1010         if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1011                 /* the above function sets the state for us in case of an error */
1012                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1013         }
1014
1015         /*
1016          * write the STREAMINFO metadata block
1017          */
1018         if(encoder->protected_->verify)
1019                 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
1020         encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1021         encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1022         encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1023         encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1024         encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1025         encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1026         encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1027         encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1028         encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1029         encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1030         encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1031         memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
1032         if(encoder->protected_->do_md5)
1033                 FLAC__MD5Init(&encoder->private_->md5context);
1034         if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1035                 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1036                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1037         }
1038         if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1039                 /* the above function sets the state for us in case of an error */
1040                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1041         }
1042
1043         /*
1044          * Now that the STREAMINFO block is written, we can init this to an
1045          * absurdly-high value...
1046          */
1047         encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
1048         /* ... and clear this to 0 */
1049         encoder->private_->streaminfo.data.stream_info.total_samples = 0;
1050
1051         /*
1052          * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1053          * if not, we will write an empty one (FLAC__add_metadata_block()
1054          * automatically supplies the vendor string).
1055          *
1056          * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1057          * the STREAMINFO.  (In the case that metadata_has_vorbis_comment is
1058          * true it will have already insured that the metadata list is properly
1059          * ordered.)
1060          */
1061         if(!metadata_has_vorbis_comment) {
1062                 FLAC__StreamMetadata vorbis_comment;
1063                 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1064                 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1065                 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1066                 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1067                 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1068                 vorbis_comment.data.vorbis_comment.num_comments = 0;
1069                 vorbis_comment.data.vorbis_comment.comments = 0;
1070                 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1071                         encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1072                         return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1073                 }
1074                 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1075                         /* the above function sets the state for us in case of an error */
1076                         return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1077                 }
1078         }
1079
1080         /*
1081          * write the user's metadata blocks
1082          */
1083         for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1084                 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
1085                 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1086                         encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1087                         return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1088                 }
1089                 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1090                         /* the above function sets the state for us in case of an error */
1091                         return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1092                 }
1093         }
1094
1095         /* now that all the metadata is written, we save the stream offset */
1096         if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &encoder->protected_->audio_offset, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) { /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
1097                 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1098                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1099         }
1100
1101         if(encoder->protected_->verify)
1102                 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1103
1104         return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1105 }
1106
1107 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1108         FLAC__StreamEncoder *encoder,
1109         FLAC__StreamEncoderWriteCallback write_callback,
1110         FLAC__StreamEncoderSeekCallback seek_callback,
1111         FLAC__StreamEncoderTellCallback tell_callback,
1112         FLAC__StreamEncoderMetadataCallback metadata_callback,
1113         void *client_data
1114 )
1115 {
1116         return init_stream_internal_(
1117                 encoder,
1118                 /*read_callback=*/0,
1119                 write_callback,
1120                 seek_callback,
1121                 tell_callback,
1122                 metadata_callback,
1123                 client_data,
1124                 /*is_ogg=*/false
1125         );
1126 }
1127
1128 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1129         FLAC__StreamEncoder *encoder,
1130         FLAC__StreamEncoderReadCallback read_callback,
1131         FLAC__StreamEncoderWriteCallback write_callback,
1132         FLAC__StreamEncoderSeekCallback seek_callback,
1133         FLAC__StreamEncoderTellCallback tell_callback,
1134         FLAC__StreamEncoderMetadataCallback metadata_callback,
1135         void *client_data
1136 )
1137 {
1138         return init_stream_internal_(
1139                 encoder,
1140                 read_callback,
1141                 write_callback,
1142                 seek_callback,
1143                 tell_callback,
1144                 metadata_callback,
1145                 client_data,
1146                 /*is_ogg=*/true
1147         );
1148 }
1149
1150 static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1151         FLAC__StreamEncoder *encoder,
1152         FILE *file,
1153         FLAC__StreamEncoderProgressCallback progress_callback,
1154         void *client_data,
1155         FLAC__bool is_ogg
1156 )
1157 {
1158         FLAC__StreamEncoderInitStatus init_status;
1159
1160         FLAC__ASSERT(0 != encoder);
1161         FLAC__ASSERT(0 != file);
1162
1163         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1164                 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1165
1166         /* double protection */
1167         if(file == 0) {
1168                 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1169                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1170         }
1171
1172         /*
1173          * To make sure that our file does not go unclosed after an error, we
1174          * must assign the FILE pointer before any further error can occur in
1175          * this routine.
1176          */
1177         if(file == stdout)
1178                 file = get_binary_stdout_(); /* just to be safe */
1179
1180         encoder->private_->file = file;
1181
1182         encoder->private_->progress_callback = progress_callback;
1183         encoder->private_->bytes_written = 0;
1184         encoder->private_->samples_written = 0;
1185         encoder->private_->frames_written = 0;
1186
1187         init_status = init_stream_internal_(
1188                 encoder,
1189                 encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0,
1190                 file_write_callback_,
1191                 encoder->private_->file == stdout? 0 : file_seek_callback_,
1192                 encoder->private_->file == stdout? 0 : file_tell_callback_,
1193                 /*metadata_callback=*/0,
1194                 client_data,
1195                 is_ogg
1196         );
1197         if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1198                 /* the above function sets the state for us in case of an error */
1199                 return init_status;
1200         }
1201
1202         {
1203                 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1204
1205                 FLAC__ASSERT(blocksize != 0);
1206                 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1207         }
1208
1209         return init_status;
1210 }
1211
1212 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1213         FLAC__StreamEncoder *encoder,
1214         FILE *file,
1215         FLAC__StreamEncoderProgressCallback progress_callback,
1216         void *client_data
1217 )
1218 {
1219         return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1220 }
1221
1222 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1223         FLAC__StreamEncoder *encoder,
1224         FILE *file,
1225         FLAC__StreamEncoderProgressCallback progress_callback,
1226         void *client_data
1227 )
1228 {
1229         return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1230 }
1231
1232 static FLAC__StreamEncoderInitStatus init_file_internal_(
1233         FLAC__StreamEncoder *encoder,
1234         const char *filename,
1235         FLAC__StreamEncoderProgressCallback progress_callback,
1236         void *client_data,
1237         FLAC__bool is_ogg
1238 )
1239 {
1240         FILE *file;
1241
1242         FLAC__ASSERT(0 != encoder);
1243
1244         /*
1245          * To make sure that our file does not go unclosed after an error, we
1246          * have to do the same entrance checks here that are later performed
1247          * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1248          */
1249         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1250                 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1251
1252         file = filename? fopen(filename, "w+b") : stdout;
1253
1254         if(file == 0) {
1255                 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1256                 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1257         }
1258
1259         return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1260 }
1261
1262 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1263         FLAC__StreamEncoder *encoder,
1264         const char *filename,
1265         FLAC__StreamEncoderProgressCallback progress_callback,
1266         void *client_data
1267 )
1268 {
1269         return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1270 }
1271
1272 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1273         FLAC__StreamEncoder *encoder,
1274         const char *filename,
1275         FLAC__StreamEncoderProgressCallback progress_callback,
1276         void *client_data
1277 )
1278 {
1279         return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
1280 }
1281
1282 FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
1283 {
1284         FLAC__bool error = false;
1285
1286         FLAC__ASSERT(0 != encoder);
1287         FLAC__ASSERT(0 != encoder->private_);
1288         FLAC__ASSERT(0 != encoder->protected_);
1289
1290         if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
1291                 return true;
1292
1293         if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
1294                 if(encoder->private_->current_sample_number != 0) {
1295                         const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
1296                         encoder->protected_->blocksize = encoder->private_->current_sample_number;
1297                         if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
1298                                 error = true;
1299                 }
1300         }
1301
1302         if(encoder->protected_->do_md5)
1303                 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
1304
1305         if(!encoder->private_->is_being_deleted) {
1306                 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
1307                         if(encoder->private_->seek_callback) {
1308 #if FLAC__HAS_OGG
1309                                 if(encoder->private_->is_ogg)
1310                                         update_ogg_metadata_(encoder);
1311                                 else
1312 #endif
1313                                 update_metadata_(encoder);
1314
1315                                 /* check if an error occurred while updating metadata */
1316                                 if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
1317                                         error = true;
1318                         }
1319                         if(encoder->private_->metadata_callback)
1320                                 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1321                 }
1322
1323                 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
1324                         if(!error)
1325                                 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1326                         error = true;
1327                 }
1328         }
1329
1330         if(0 != encoder->private_->file) {
1331                 if(encoder->private_->file != stdout)
1332                         fclose(encoder->private_->file);
1333                 encoder->private_->file = 0;
1334         }
1335
1336 #if FLAC__HAS_OGG
1337         if(encoder->private_->is_ogg)
1338                 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1339 #endif
1340
1341         free_(encoder);
1342         set_defaults_(encoder);
1343
1344         if(!error)
1345                 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1346
1347         return !error;
1348 }
1349
1350 FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
1351 {
1352         FLAC__ASSERT(0 != encoder);
1353         FLAC__ASSERT(0 != encoder->private_);
1354         FLAC__ASSERT(0 != encoder->protected_);
1355         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1356                 return false;
1357 #if FLAC__HAS_OGG
1358         /* can't check encoder->private_->is_ogg since that's not set until init time */
1359         FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1360         return true;
1361 #else
1362         (void)value;
1363         return false;
1364 #endif
1365 }
1366
1367 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
1368 {
1369         FLAC__ASSERT(0 != encoder);
1370         FLAC__ASSERT(0 != encoder->private_);
1371         FLAC__ASSERT(0 != encoder->protected_);
1372         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1373                 return false;
1374 #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1375         encoder->protected_->verify = value;
1376 #endif
1377         return true;
1378 }
1379
1380 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
1381 {
1382         FLAC__ASSERT(0 != encoder);
1383         FLAC__ASSERT(0 != encoder->private_);
1384         FLAC__ASSERT(0 != encoder->protected_);
1385         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1386                 return false;
1387         encoder->protected_->streamable_subset = value;
1388         return true;
1389 }
1390
1391 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_md5(FLAC__StreamEncoder *encoder, FLAC__bool value)
1392 {
1393         FLAC__ASSERT(0 != encoder);
1394         FLAC__ASSERT(0 != encoder->private_);
1395         FLAC__ASSERT(0 != encoder->protected_);
1396         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1397                 return false;
1398         encoder->protected_->do_md5 = value;
1399         return true;
1400 }
1401
1402 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
1403 {
1404         FLAC__ASSERT(0 != encoder);
1405         FLAC__ASSERT(0 != encoder->private_);
1406         FLAC__ASSERT(0 != encoder->protected_);
1407         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1408                 return false;
1409         encoder->protected_->channels = value;
1410         return true;
1411 }
1412
1413 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
1414 {
1415         FLAC__ASSERT(0 != encoder);
1416         FLAC__ASSERT(0 != encoder->private_);
1417         FLAC__ASSERT(0 != encoder->protected_);
1418         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1419                 return false;
1420         encoder->protected_->bits_per_sample = value;
1421         return true;
1422 }
1423
1424 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
1425 {
1426         FLAC__ASSERT(0 != encoder);
1427         FLAC__ASSERT(0 != encoder->private_);
1428         FLAC__ASSERT(0 != encoder->protected_);
1429         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1430                 return false;
1431         encoder->protected_->sample_rate = value;
1432         return true;
1433 }
1434
1435 FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
1436 {
1437         FLAC__bool ok = true;
1438         FLAC__ASSERT(0 != encoder);
1439         FLAC__ASSERT(0 != encoder->private_);
1440         FLAC__ASSERT(0 != encoder->protected_);
1441         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1442                 return false;
1443         if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1444                 value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1445         ok &= FLAC__stream_encoder_set_do_mid_side_stereo          (encoder, compression_levels_[value].do_mid_side_stereo);
1446         ok &= FLAC__stream_encoder_set_loose_mid_side_stereo       (encoder, compression_levels_[value].loose_mid_side_stereo);
1447 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1448 #if 0
1449         /* was: */
1450         ok &= FLAC__stream_encoder_set_apodization                 (encoder, compression_levels_[value].apodization);
1451         /* but it's too hard to specify the string in a locale-specific way */
1452 #else
1453         encoder->protected_->num_apodizations = 1;
1454         encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1455         encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1456 #endif
1457 #endif
1458         ok &= FLAC__stream_encoder_set_max_lpc_order               (encoder, compression_levels_[value].max_lpc_order);
1459         ok &= FLAC__stream_encoder_set_qlp_coeff_precision         (encoder, compression_levels_[value].qlp_coeff_precision);
1460         ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search    (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1461         ok &= FLAC__stream_encoder_set_do_escape_coding            (encoder, compression_levels_[value].do_escape_coding);
1462         ok &= FLAC__stream_encoder_set_do_exhaustive_model_search  (encoder, compression_levels_[value].do_exhaustive_model_search);
1463         ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1464         ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1465         ok &= FLAC__stream_encoder_set_rice_parameter_search_dist  (encoder, compression_levels_[value].rice_parameter_search_dist);
1466         return ok;
1467 }
1468
1469 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1470 {
1471         FLAC__ASSERT(0 != encoder);
1472         FLAC__ASSERT(0 != encoder->private_);
1473         FLAC__ASSERT(0 != encoder->protected_);
1474         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1475                 return false;
1476         encoder->protected_->blocksize = value;
1477         return true;
1478 }
1479
1480 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1481 {
1482         FLAC__ASSERT(0 != encoder);
1483         FLAC__ASSERT(0 != encoder->private_);
1484         FLAC__ASSERT(0 != encoder->protected_);
1485         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1486                 return false;
1487         encoder->protected_->do_mid_side_stereo = value;
1488         return true;
1489 }
1490
1491 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1492 {
1493         FLAC__ASSERT(0 != encoder);
1494         FLAC__ASSERT(0 != encoder->private_);
1495         FLAC__ASSERT(0 != encoder->protected_);
1496         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1497                 return false;
1498         encoder->protected_->loose_mid_side_stereo = value;
1499         return true;
1500 }
1501
1502 /*@@@@add to tests*/
1503 FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1504 {
1505         FLAC__ASSERT(0 != encoder);
1506         FLAC__ASSERT(0 != encoder->private_);
1507         FLAC__ASSERT(0 != encoder->protected_);
1508         FLAC__ASSERT(0 != specification);
1509         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1510                 return false;
1511 #ifdef FLAC__INTEGER_ONLY_LIBRARY
1512         (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1513 #else
1514         encoder->protected_->num_apodizations = 0;
1515         while(1) {
1516                 const char *s = strchr(specification, ';');
1517                 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1518                 if     (n==8  && 0 == strncmp("bartlett"     , specification, n))
1519                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1520                 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1521                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1522                 else if(n==8  && 0 == strncmp("blackman"     , specification, n))
1523                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1524                 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1525                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1526                 else if(n==6  && 0 == strncmp("connes"       , specification, n))
1527                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1528                 else if(n==7  && 0 == strncmp("flattop"      , specification, n))
1529                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1530                 else if(n>7   && 0 == strncmp("gauss("       , specification, 6)) {
1531                         FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1532                         if (stddev > 0.0 && stddev <= 0.5) {
1533                                 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1534                                 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1535                         }
1536                 }
1537                 else if(n==7  && 0 == strncmp("hamming"      , specification, n))
1538                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1539                 else if(n==4  && 0 == strncmp("hann"         , specification, n))
1540                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1541                 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1542                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1543                 else if(n==7  && 0 == strncmp("nuttall"      , specification, n))
1544                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1545                 else if(n==9  && 0 == strncmp("rectangle"    , specification, n))
1546                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1547                 else if(n==8  && 0 == strncmp("triangle"     , specification, n))
1548                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1549                 else if(n>7   && 0 == strncmp("tukey("       , specification, 6)) {
1550                         FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1551                         if (p >= 0.0 && p <= 1.0) {
1552                                 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1553                                 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1554                         }
1555                 }
1556                 else if(n==5  && 0 == strncmp("welch"        , specification, n))
1557                         encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1558                 if (encoder->protected_->num_apodizations == 32)
1559                         break;
1560                 if (s)
1561                         specification = s+1;
1562                 else
1563                         break;
1564         }
1565         if(encoder->protected_->num_apodizations == 0) {
1566                 encoder->protected_->num_apodizations = 1;
1567                 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1568                 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1569         }
1570 #endif
1571         return true;
1572 }
1573
1574 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1575 {
1576         FLAC__ASSERT(0 != encoder);
1577         FLAC__ASSERT(0 != encoder->private_);
1578         FLAC__ASSERT(0 != encoder->protected_);
1579         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1580                 return false;
1581         encoder->protected_->max_lpc_order = value;
1582         return true;
1583 }
1584
1585 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1586 {
1587         FLAC__ASSERT(0 != encoder);
1588         FLAC__ASSERT(0 != encoder->private_);
1589         FLAC__ASSERT(0 != encoder->protected_);
1590         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1591                 return false;
1592         encoder->protected_->qlp_coeff_precision = value;
1593         return true;
1594 }
1595
1596 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1597 {
1598         FLAC__ASSERT(0 != encoder);
1599         FLAC__ASSERT(0 != encoder->private_);
1600         FLAC__ASSERT(0 != encoder->protected_);
1601         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1602                 return false;
1603         encoder->protected_->do_qlp_coeff_prec_search = value;
1604         return true;
1605 }
1606
1607 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1608 {
1609         FLAC__ASSERT(0 != encoder);
1610         FLAC__ASSERT(0 != encoder->private_);
1611         FLAC__ASSERT(0 != encoder->protected_);
1612         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1613                 return false;
1614 #if 0
1615         /*@@@ deprecated: */
1616         encoder->protected_->do_escape_coding = value;
1617 #else
1618         (void)value;
1619 #endif
1620         return true;
1621 }
1622
1623 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1624 {
1625         FLAC__ASSERT(0 != encoder);
1626         FLAC__ASSERT(0 != encoder->private_);
1627         FLAC__ASSERT(0 != encoder->protected_);
1628         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1629                 return false;
1630         encoder->protected_->do_exhaustive_model_search = value;
1631         return true;
1632 }
1633
1634 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1635 {
1636         FLAC__ASSERT(0 != encoder);
1637         FLAC__ASSERT(0 != encoder->private_);
1638         FLAC__ASSERT(0 != encoder->protected_);
1639         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1640                 return false;
1641         encoder->protected_->min_residual_partition_order = value;
1642         return true;
1643 }
1644
1645 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1646 {
1647         FLAC__ASSERT(0 != encoder);
1648         FLAC__ASSERT(0 != encoder->private_);
1649         FLAC__ASSERT(0 != encoder->protected_);
1650         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1651                 return false;
1652         encoder->protected_->max_residual_partition_order = value;
1653         return true;
1654 }
1655
1656 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1657 {
1658         FLAC__ASSERT(0 != encoder);
1659         FLAC__ASSERT(0 != encoder->private_);
1660         FLAC__ASSERT(0 != encoder->protected_);
1661         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1662                 return false;
1663 #if 0
1664         /*@@@ deprecated: */
1665         encoder->protected_->rice_parameter_search_dist = value;
1666 #else
1667         (void)value;
1668 #endif
1669         return true;
1670 }
1671
1672 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1673 {
1674         FLAC__ASSERT(0 != encoder);
1675         FLAC__ASSERT(0 != encoder->private_);
1676         FLAC__ASSERT(0 != encoder->protected_);
1677         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1678                 return false;
1679         encoder->protected_->total_samples_estimate = value;
1680         return true;
1681 }
1682
1683 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1684 {
1685         FLAC__ASSERT(0 != encoder);
1686         FLAC__ASSERT(0 != encoder->private_);
1687         FLAC__ASSERT(0 != encoder->protected_);
1688         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1689                 return false;
1690         if(0 == metadata)
1691                 num_blocks = 0;
1692         if(0 == num_blocks)
1693                 metadata = 0;
1694         /* realloc() does not do exactly what we want so... */
1695         if(encoder->protected_->metadata) {
1696                 free(encoder->protected_->metadata);
1697                 encoder->protected_->metadata = 0;
1698                 encoder->protected_->num_metadata_blocks = 0;
1699         }
1700         if(num_blocks) {
1701                 FLAC__StreamMetadata **m;
1702                 if(0 == (m = safe_malloc_mul_2op_p(sizeof(m[0]), /*times*/num_blocks)))
1703                         return false;
1704                 memcpy(m, metadata, sizeof(m[0]) * num_blocks);
1705                 encoder->protected_->metadata = m;
1706                 encoder->protected_->num_metadata_blocks = num_blocks;
1707         }
1708 #if FLAC__HAS_OGG
1709         if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1710                 return false;
1711 #endif
1712         return true;
1713 }
1714
1715 /*
1716  * These three functions are not static, but not publically exposed in
1717  * include/FLAC/ either.  They are used by the test suite.
1718  */
1719 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1720 {
1721         FLAC__ASSERT(0 != encoder);
1722         FLAC__ASSERT(0 != encoder->private_);
1723         FLAC__ASSERT(0 != encoder->protected_);
1724         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1725                 return false;
1726         encoder->private_->disable_constant_subframes = value;
1727         return true;
1728 }
1729
1730 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1731 {
1732         FLAC__ASSERT(0 != encoder);
1733         FLAC__ASSERT(0 != encoder->private_);
1734         FLAC__ASSERT(0 != encoder->protected_);
1735         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1736                 return false;
1737         encoder->private_->disable_fixed_subframes = value;
1738         return true;
1739 }
1740
1741 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1742 {
1743         FLAC__ASSERT(0 != encoder);
1744         FLAC__ASSERT(0 != encoder->private_);
1745         FLAC__ASSERT(0 != encoder->protected_);
1746         if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1747                 return false;
1748         encoder->private_->disable_verbatim_subframes = value;
1749         return true;
1750 }
1751
1752 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1753 {
1754         FLAC__ASSERT(0 != encoder);
1755         FLAC__ASSERT(0 != encoder->private_);
1756         FLAC__ASSERT(0 != encoder->protected_);
1757         return encoder->protected_->state;
1758 }
1759
1760 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1761 {
1762         FLAC__ASSERT(0 != encoder);
1763         FLAC__ASSERT(0 != encoder->private_);
1764         FLAC__ASSERT(0 != encoder->protected_);
1765         if(encoder->protected_->verify)
1766                 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1767         else
1768                 return FLAC__STREAM_DECODER_UNINITIALIZED;
1769 }
1770
1771 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1772 {
1773         FLAC__ASSERT(0 != encoder);
1774         FLAC__ASSERT(0 != encoder->private_);
1775         FLAC__ASSERT(0 != encoder->protected_);
1776         if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1777                 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1778         else
1779                 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
1780 }
1781
1782 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)
1783 {
1784         FLAC__ASSERT(0 != encoder);
1785         FLAC__ASSERT(0 != encoder->private_);
1786         FLAC__ASSERT(0 != encoder->protected_);
1787         if(0 != absolute_sample)
1788                 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1789         if(0 != frame_number)
1790                 *frame_number = encoder->private_->verify.error_stats.frame_number;
1791         if(0 != channel)
1792                 *channel = encoder->private_->verify.error_stats.channel;
1793         if(0 != sample)
1794                 *sample = encoder->private_->verify.error_stats.sample;
1795         if(0 != expected)
1796                 *expected = encoder->private_->verify.error_stats.expected;
1797         if(0 != got)
1798                 *got = encoder->private_->verify.error_stats.got;
1799 }
1800
1801 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1802 {
1803         FLAC__ASSERT(0 != encoder);
1804         FLAC__ASSERT(0 != encoder->private_);
1805         FLAC__ASSERT(0 != encoder->protected_);
1806         return encoder->protected_->verify;
1807 }
1808
1809 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1810 {
1811         FLAC__ASSERT(0 != encoder);
1812         FLAC__ASSERT(0 != encoder->private_);
1813         FLAC__ASSERT(0 != encoder->protected_);
1814         return encoder->protected_->streamable_subset;
1815 }
1816
1817 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_md5(const FLAC__StreamEncoder *encoder)
1818 {
1819         FLAC__ASSERT(0 != encoder);
1820         FLAC__ASSERT(0 != encoder->private_);
1821         FLAC__ASSERT(0 != encoder->protected_);
1822         return encoder->protected_->do_md5;
1823 }
1824
1825 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1826 {
1827         FLAC__ASSERT(0 != encoder);
1828         FLAC__ASSERT(0 != encoder->private_);
1829         FLAC__ASSERT(0 != encoder->protected_);
1830         return encoder->protected_->channels;
1831 }
1832
1833 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1834 {
1835         FLAC__ASSERT(0 != encoder);
1836         FLAC__ASSERT(0 != encoder->private_);
1837         FLAC__ASSERT(0 != encoder->protected_);
1838         return encoder->protected_->bits_per_sample;
1839 }
1840
1841 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1842 {
1843         FLAC__ASSERT(0 != encoder);
1844         FLAC__ASSERT(0 != encoder->private_);
1845         FLAC__ASSERT(0 != encoder->protected_);
1846         return encoder->protected_->sample_rate;
1847 }
1848
1849 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1850 {
1851         FLAC__ASSERT(0 != encoder);
1852         FLAC__ASSERT(0 != encoder->private_);
1853         FLAC__ASSERT(0 != encoder->protected_);
1854         return encoder->protected_->blocksize;
1855 }
1856
1857 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1858 {
1859         FLAC__ASSERT(0 != encoder);
1860         FLAC__ASSERT(0 != encoder->private_);
1861         FLAC__ASSERT(0 != encoder->protected_);
1862         return encoder->protected_->do_mid_side_stereo;
1863 }
1864
1865 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1866 {
1867         FLAC__ASSERT(0 != encoder);
1868         FLAC__ASSERT(0 != encoder->private_);
1869         FLAC__ASSERT(0 != encoder->protected_);
1870         return encoder->protected_->loose_mid_side_stereo;
1871 }
1872
1873 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1874 {
1875         FLAC__ASSERT(0 != encoder);
1876         FLAC__ASSERT(0 != encoder->private_);
1877         FLAC__ASSERT(0 != encoder->protected_);
1878         return encoder->protected_->max_lpc_order;
1879 }
1880
1881 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1882 {
1883         FLAC__ASSERT(0 != encoder);
1884         FLAC__ASSERT(0 != encoder->private_);
1885         FLAC__ASSERT(0 != encoder->protected_);
1886         return encoder->protected_->qlp_coeff_precision;
1887 }
1888
1889 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1890 {
1891         FLAC__ASSERT(0 != encoder);
1892         FLAC__ASSERT(0 != encoder->private_);
1893         FLAC__ASSERT(0 != encoder->protected_);
1894         return encoder->protected_->do_qlp_coeff_prec_search;
1895 }
1896
1897 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1898 {
1899         FLAC__ASSERT(0 != encoder);
1900         FLAC__ASSERT(0 != encoder->private_);
1901         FLAC__ASSERT(0 != encoder->protected_);
1902         return encoder->protected_->do_escape_coding;
1903 }
1904
1905 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1906 {
1907         FLAC__ASSERT(0 != encoder);
1908         FLAC__ASSERT(0 != encoder->private_);
1909         FLAC__ASSERT(0 != encoder->protected_);
1910         return encoder->protected_->do_exhaustive_model_search;
1911 }
1912
1913 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1914 {
1915         FLAC__ASSERT(0 != encoder);
1916         FLAC__ASSERT(0 != encoder->private_);
1917         FLAC__ASSERT(0 != encoder->protected_);
1918         return encoder->protected_->min_residual_partition_order;
1919 }
1920
1921 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1922 {
1923         FLAC__ASSERT(0 != encoder);
1924         FLAC__ASSERT(0 != encoder->private_);
1925         FLAC__ASSERT(0 != encoder->protected_);
1926         return encoder->protected_->max_residual_partition_order;
1927 }
1928
1929 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1930 {
1931         FLAC__ASSERT(0 != encoder);
1932         FLAC__ASSERT(0 != encoder->private_);
1933         FLAC__ASSERT(0 != encoder->protected_);
1934         return encoder->protected_->rice_parameter_search_dist;
1935 }
1936
1937 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1938 {
1939         FLAC__ASSERT(0 != encoder);
1940         FLAC__ASSERT(0 != encoder->private_);
1941         FLAC__ASSERT(0 != encoder->protected_);
1942         return encoder->protected_->total_samples_estimate;
1943 }
1944
1945 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1946 {
1947         unsigned i, j = 0, channel;
1948         const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1949
1950         FLAC__ASSERT(0 != encoder);
1951         FLAC__ASSERT(0 != encoder->private_);
1952         FLAC__ASSERT(0 != encoder->protected_);
1953         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1954
1955         do {
1956                 const unsigned n = flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j);
1957
1958                 if(encoder->protected_->verify)
1959                         append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, n);
1960
1961                 for(channel = 0; channel < channels; channel++)
1962                         memcpy(&encoder->private_->integer_signal[channel][encoder->private_->current_sample_number], &buffer[channel][j], sizeof(buffer[channel][0]) * n);
1963
1964                 if(encoder->protected_->do_mid_side_stereo) {
1965                         FLAC__ASSERT(channels == 2);
1966                         /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
1967                         for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
1968                                 encoder->private_->integer_signal_mid_side[1][i] = buffer[0][j] - buffer[1][j];
1969                                 encoder->private_->integer_signal_mid_side[0][i] = (buffer[0][j] + buffer[1][j]) >> 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1970                         }
1971                 }
1972                 else
1973                         j += n;
1974
1975                 encoder->private_->current_sample_number += n;
1976
1977                 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
1978                 if(encoder->private_->current_sample_number > blocksize) {
1979                         FLAC__ASSERT(encoder->private_->current_sample_number == blocksize+OVERREAD_);
1980                         FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
1981                         if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
1982                                 return false;
1983                         /* move unprocessed overread samples to beginnings of arrays */
1984                         for(channel = 0; channel < channels; channel++)
1985                                 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
1986                         if(encoder->protected_->do_mid_side_stereo) {
1987                                 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
1988                                 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
1989                         }
1990                         encoder->private_->current_sample_number = 1;
1991                 }
1992         } while(j < samples);
1993
1994         return true;
1995 }
1996
1997 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1998 {
1999         unsigned i, j, k, channel;
2000         FLAC__int32 x, mid, side;
2001         const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
2002
2003         FLAC__ASSERT(0 != encoder);
2004         FLAC__ASSERT(0 != encoder->private_);
2005         FLAC__ASSERT(0 != encoder->protected_);
2006         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2007
2008         j = k = 0;
2009         /*
2010          * we have several flavors of the same basic loop, optimized for
2011          * different conditions:
2012          */
2013         if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2014                 /*
2015                  * stereo coding: unroll channel loop
2016                  */
2017                 do {
2018                         if(encoder->protected_->verify)
2019                                 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
2020
2021                         /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2022                         for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2023                                 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
2024                                 x = buffer[k++];
2025                                 encoder->private_->integer_signal[1][i] = x;
2026                                 mid += x;
2027                                 side -= x;
2028                                 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2029                                 encoder->private_->integer_signal_mid_side[1][i] = side;
2030                                 encoder->private_->integer_signal_mid_side[0][i] = mid;
2031                         }
2032                         encoder->private_->current_sample_number = i;
2033                         /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2034                         if(i > blocksize) {
2035                                 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2036                                         return false;
2037                                 /* move unprocessed overread samples to beginnings of arrays */
2038                                 FLAC__ASSERT(i == blocksize+OVERREAD_);
2039                                 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2040                                 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][blocksize];
2041                                 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][blocksize];
2042                                 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
2043                                 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
2044                                 encoder->private_->current_sample_number = 1;
2045                         }
2046                 } while(j < samples);
2047         }
2048         else {
2049                 /*
2050                  * independent channel coding: buffer each channel in inner loop
2051                  */
2052                 do {
2053                         if(encoder->protected_->verify)
2054                                 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
2055
2056                         /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2057                         for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2058                                 for(channel = 0; channel < channels; channel++)
2059                                         encoder->private_->integer_signal[channel][i] = buffer[k++];
2060                         }
2061                         encoder->private_->current_sample_number = i;
2062                         /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2063                         if(i > blocksize) {
2064                                 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2065                                         return false;
2066                                 /* move unprocessed overread samples to beginnings of arrays */
2067                                 FLAC__ASSERT(i == blocksize+OVERREAD_);
2068                                 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2069                                 for(channel = 0; channel < channels; channel++)
2070                                         encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
2071                                 encoder->private_->current_sample_number = 1;
2072                         }
2073                 } while(j < samples);
2074         }
2075
2076         return true;
2077 }
2078
2079 /***********************************************************************
2080  *
2081  * Private class methods
2082  *
2083  ***********************************************************************/
2084
2085 void set_defaults_(FLAC__StreamEncoder *encoder)
2086 {
2087         FLAC__ASSERT(0 != encoder);
2088
2089 #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2090         encoder->protected_->verify = true;
2091 #else
2092         encoder->protected_->verify = false;
2093 #endif
2094         encoder->protected_->streamable_subset = true;
2095         encoder->protected_->do_md5 = true;
2096         encoder->protected_->do_mid_side_stereo = false;
2097         encoder->protected_->loose_mid_side_stereo = false;
2098         encoder->protected_->channels = 2;
2099         encoder->protected_->bits_per_sample = 16;
2100         encoder->protected_->sample_rate = 44100;
2101         encoder->protected_->blocksize = 0;
2102 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2103         encoder->protected_->num_apodizations = 1;
2104         encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2105         encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
2106 #endif
2107         encoder->protected_->max_lpc_order = 0;
2108         encoder->protected_->qlp_coeff_precision = 0;
2109         encoder->protected_->do_qlp_coeff_prec_search = false;
2110         encoder->protected_->do_exhaustive_model_search = false;
2111         encoder->protected_->do_escape_coding = false;
2112         encoder->protected_->min_residual_partition_order = 0;
2113         encoder->protected_->max_residual_partition_order = 0;
2114         encoder->protected_->rice_parameter_search_dist = 0;
2115         encoder->protected_->total_samples_estimate = 0;
2116         encoder->protected_->metadata = 0;
2117         encoder->protected_->num_metadata_blocks = 0;
2118
2119         encoder->private_->seek_table = 0;
2120         encoder->private_->disable_constant_subframes = false;
2121         encoder->private_->disable_fixed_subframes = false;
2122         encoder->private_->disable_verbatim_subframes = false;
2123 #if FLAC__HAS_OGG
2124         encoder->private_->is_ogg = false;
2125 #endif
2126         encoder->private_->read_callback = 0;
2127         encoder->private_->write_callback = 0;
2128         encoder->private_->seek_callback = 0;
2129         encoder->private_->tell_callback = 0;
2130         encoder->private_->metadata_callback = 0;
2131         encoder->private_->progress_callback = 0;
2132         encoder->private_->client_data = 0;
2133
2134 #if FLAC__HAS_OGG
2135         FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2136 #endif
2137
2138         FLAC__stream_encoder_set_compression_level(encoder, 5);
2139 }
2140
2141 void free_(FLAC__StreamEncoder *encoder)
2142 {
2143         unsigned i, channel;
2144
2145         FLAC__ASSERT(0 != encoder);
2146         if(encoder->protected_->metadata) {
2147                 free(encoder->protected_->metadata);
2148                 encoder->protected_->metadata = 0;
2149                 encoder->protected_->num_metadata_blocks = 0;
2150         }
2151         for(i = 0; i < encoder->protected_->channels; i++) {
2152                 if(0 != encoder->private_->integer_signal_unaligned[i]) {
2153                         free(encoder->private_->integer_signal_unaligned[i]);
2154                         encoder->private_->integer_signal_unaligned[i] = 0;
2155                 }
2156 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2157                 if(0 != encoder->private_->real_signal_unaligned[i]) {
2158                         free(encoder->private_->real_signal_unaligned[i]);
2159                         encoder->private_->real_signal_unaligned[i] = 0;
2160                 }
2161 #endif
2162         }
2163         for(i = 0; i < 2; i++) {
2164                 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
2165                         free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2166                         encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2167                 }
2168 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2169                 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
2170                         free(encoder->private_->real_signal_mid_side_unaligned[i]);
2171                         encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2172                 }
2173 #endif
2174         }
2175 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2176         for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2177                 if(0 != encoder->private_->window_unaligned[i]) {
2178                         free(encoder->private_->window_unaligned[i]);
2179                         encoder->private_->window_unaligned[i] = 0;
2180                 }
2181         }
2182         if(0 != encoder->private_->windowed_signal_unaligned) {
2183                 free(encoder->private_->windowed_signal_unaligned);
2184                 encoder->private_->windowed_signal_unaligned = 0;
2185         }
2186 #endif
2187         for(channel = 0; channel < encoder->protected_->channels; channel++) {
2188                 for(i = 0; i < 2; i++) {
2189                         if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
2190                                 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2191                                 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2192                         }
2193                 }
2194         }
2195         for(channel = 0; channel < 2; channel++) {
2196                 for(i = 0; i < 2; i++) {
2197                         if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
2198                                 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2199                                 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2200                         }
2201                 }
2202         }
2203         if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
2204                 free(encoder->private_->abs_residual_partition_sums_unaligned);
2205                 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2206         }
2207         if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
2208                 free(encoder->private_->raw_bits_per_partition_unaligned);
2209                 encoder->private_->raw_bits_per_partition_unaligned = 0;
2210         }
2211         if(encoder->protected_->verify) {
2212                 for(i = 0; i < encoder->protected_->channels; i++) {
2213                         if(0 != encoder->private_->verify.input_fifo.data[i]) {
2214                                 free(encoder->private_->verify.input_fifo.data[i]);
2215                                 encoder->private_->verify.input_fifo.data[i] = 0;
2216                         }
2217                 }
2218         }
2219         FLAC__bitwriter_free(encoder->private_->frame);
2220 }
2221
2222 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
2223 {
2224         FLAC__bool ok;
2225         unsigned i, channel;
2226
2227         FLAC__ASSERT(new_blocksize > 0);
2228         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2229         FLAC__ASSERT(encoder->private_->current_sample_number == 0);
2230
2231         /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
2232         if(new_blocksize <= encoder->private_->input_capacity)
2233                 return true;
2234
2235         ok = true;
2236
2237         /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2238          * requires that the input arrays (in our case the integer signals)
2239          * have a buffer of up to 3 zeroes in front (at negative indices) for
2240          * alignment purposes; we use 4 in front to keep the data well-aligned.
2241          */
2242
2243         for(i = 0; ok && i < encoder->protected_->channels; i++) {
2244                 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
2245                 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2246                 encoder->private_->integer_signal[i] += 4;
2247 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2248 #if 0 /* @@@ currently unused */
2249                 if(encoder->protected_->max_lpc_order > 0)
2250                         ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
2251 #endif
2252 #endif
2253         }
2254         for(i = 0; ok && i < 2; i++) {
2255                 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
2256                 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2257                 encoder->private_->integer_signal_mid_side[i] += 4;
2258 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2259 #if 0 /* @@@ currently unused */
2260                 if(encoder->protected_->max_lpc_order > 0)
2261                         ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
2262 #endif
2263 #endif
2264         }
2265 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2266         if(ok && encoder->protected_->max_lpc_order > 0) {
2267                 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2268                         ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2269                 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
2270         }
2271 #endif
2272         for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
2273                 for(i = 0; ok && i < 2; i++) {
2274                         ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
2275                 }
2276         }
2277         for(channel = 0; ok && channel < 2; channel++) {
2278                 for(i = 0; ok && i < 2; i++) {
2279                         ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
2280                 }
2281         }
2282         /* the *2 is an approximation to the series 1 + 1/2 + 1/4 + ... that sums tree occupies in a flat array */
2283         /*@@@ new_blocksize*2 is too pessimistic, but to fix, we need smarter logic because a smaller new_blocksize can actually increase the # of partitions; would require moving this out into a separate function, then checking its capacity against the need of the current blocksize&min/max_partition_order (and maybe predictor order) */
2284         ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
2285         if(encoder->protected_->do_escape_coding)
2286                 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
2287
2288         /* now adjust the windows if the blocksize has changed */
2289 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2290         if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
2291                 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2292                         switch(encoder->protected_->apodizations[i].type) {
2293                                 case FLAC__APODIZATION_BARTLETT:
2294                                         FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
2295                                         break;
2296                                 case FLAC__APODIZATION_BARTLETT_HANN:
2297                                         FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
2298                                         break;
2299                                 case FLAC__APODIZATION_BLACKMAN:
2300                                         FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
2301                                         break;
2302                                 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2303                                         FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
2304                                         break;
2305                                 case FLAC__APODIZATION_CONNES:
2306                                         FLAC__window_connes(encoder->private_->window[i], new_blocksize);
2307                                         break;
2308                                 case FLAC__APODIZATION_FLATTOP:
2309                                         FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
2310                                         break;
2311                                 case FLAC__APODIZATION_GAUSS:
2312                                         FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2313                                         break;
2314                                 case FLAC__APODIZATION_HAMMING:
2315                                         FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
2316                                         break;
2317                                 case FLAC__APODIZATION_HANN:
2318                                         FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2319                                         break;
2320                                 case FLAC__APODIZATION_KAISER_BESSEL:
2321                                         FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
2322                                         break;
2323                                 case FLAC__APODIZATION_NUTTALL:
2324                                         FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
2325                                         break;
2326                                 case FLAC__APODIZATION_RECTANGLE:
2327                                         FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
2328                                         break;
2329                                 case FLAC__APODIZATION_TRIANGLE:
2330                                         FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
2331                                         break;
2332                                 case FLAC__APODIZATION_TUKEY:
2333                                         FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
2334                                         break;
2335                                 case FLAC__APODIZATION_WELCH:
2336                                         FLAC__window_welch(encoder->private_->window[i], new_blocksize);
2337                                         break;
2338                                 default:
2339                                         FLAC__ASSERT(0);
2340                                         /* double protection */
2341                                         FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2342                                         break;
2343                         }
2344                 }
2345         }
2346 #endif
2347
2348         if(ok)
2349                 encoder->private_->input_capacity = new_blocksize;
2350         else
2351                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2352
2353         return ok;
2354 }
2355
2356 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
2357 {
2358         const FLAC__byte *buffer;
2359         size_t bytes;
2360
2361         FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
2362
2363         if(!FLAC__bitwriter_get_buffer(encoder->private_->frame, &buffer, &bytes)) {
2364                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2365                 return false;
2366         }
2367
2368         if(encoder->protected_->verify) {
2369                 encoder->private_->verify.output.data = buffer;
2370                 encoder->private_->verify.output.bytes = bytes;
2371                 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2372                         encoder->private_->verify.needs_magic_hack = true;
2373                 }
2374                 else {
2375                         if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2376                                 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2377                                 FLAC__bitwriter_clear(encoder->private_->frame);
2378                                 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2379                                         encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2380                                 return false;
2381                         }
2382                 }
2383         }
2384
2385         if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2386                 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2387                 FLAC__bitwriter_clear(encoder->private_->frame);
2388                 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2389                 return false;
2390         }
2391
2392         FLAC__bitwriter_release_buffer(encoder->private_->frame);
2393         FLAC__bitwriter_clear(encoder->private_->frame);
2394
2395         if(samples > 0) {
2396                 encoder->private_->streaminfo.data.stream_info.min_framesize = flac_min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2397                 encoder->private_->streaminfo.data.stream_info.max_framesize = flac_max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
2398         }
2399
2400         return true;
2401 }
2402
2403 FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block)
2404 {
2405         FLAC__StreamEncoderWriteStatus status;
2406         FLAC__uint64 output_position = 0;
2407
2408         /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2409         if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2410                 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2411                 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2412         }
2413
2414         /*
2415          * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2416          */
2417         if(samples == 0) {
2418                 FLAC__MetadataType type = (buffer[0] & 0x7f);
2419                 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2420                         encoder->protected_->streaminfo_offset = output_position;
2421                 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2422                         encoder->protected_->seektable_offset = output_position;
2423         }
2424
2425         /*
2426          * Mark the current seek point if hit (if audio_offset == 0 that
2427          * means we're still writing metadata and haven't hit the first
2428          * frame yet)
2429          */
2430         if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2431                 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2432                 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2433                 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2434                 FLAC__uint64 test_sample;
2435                 unsigned i;
2436                 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2437                         test_sample = encoder->private_->seek_table->points[i].sample_number;
2438                         if(test_sample > frame_last_sample) {
2439                                 break;
2440                         }
2441                         else if(test_sample >= frame_first_sample) {
2442                                 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2443                                 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2444                                 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2445                                 encoder->private_->first_seekpoint_to_check++;
2446                                 /* DO NOT: "break;" and here's why:
2447                                  * The seektable template may contain more than one target
2448                                  * sample for any given frame; we will keep looping, generating
2449                                  * duplicate seekpoints for them, and we'll clean it up later,
2450                                  * just before writing the seektable back to the metadata.
2451                                  */
2452                         }
2453                         else {
2454                                 encoder->private_->first_seekpoint_to_check++;
2455                         }
2456                 }
2457         }
2458
2459 #if FLAC__HAS_OGG
2460         if(encoder->private_->is_ogg) {
2461                 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2462                         &encoder->protected_->ogg_encoder_aspect,
2463                         buffer,
2464                         bytes,
2465                         samples,
2466                         encoder->private_->current_frame_number,
2467                         is_last_block,
2468                         (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2469                         encoder,
2470                         encoder->private_->client_data
2471                 );
2472         }
2473         else
2474 #endif
2475         status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2476
2477         if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2478                 encoder->private_->bytes_written += bytes;
2479                 encoder->private_->samples_written += samples;
2480                 /* we keep a high watermark on the number of frames written because
2481                  * when the encoder goes back to write metadata, 'current_frame'
2482                  * will drop back to 0.
2483                  */
2484                 encoder->private_->frames_written = flac_max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2485         }
2486         else
2487                 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2488
2489         return status;
2490 }
2491
2492 /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks.  */
2493 void update_metadata_(const FLAC__StreamEncoder *encoder)
2494 {
2495         FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2496         const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2497         const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2498         const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2499         const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2500         const unsigned bps = metadata->data.stream_info.bits_per_sample;
2501         FLAC__StreamEncoderSeekStatus seek_status;
2502
2503         FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2504
2505         /* All this is based on intimate knowledge of the stream header
2506          * layout, but a change to the header format that would break this
2507          * would also break all streams encoded in the previous format.
2508          */
2509
2510         /*
2511          * Write MD5 signature
2512          */
2513         {
2514                 const unsigned md5_offset =
2515                         FLAC__STREAM_METADATA_HEADER_LENGTH +
2516                         (
2517                                 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2518                                 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2519                                 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2520                                 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2521                                 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2522                                 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2523                                 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2524                                 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2525                         ) / 8;
2526
2527                 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2528                         if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2529                                 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2530                         return;
2531                 }
2532                 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2533                         encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2534                         return;
2535                 }
2536         }
2537
2538         /*
2539          * Write total samples
2540          */
2541         {
2542                 const unsigned total_samples_byte_offset =
2543                         FLAC__STREAM_METADATA_HEADER_LENGTH +
2544                         (
2545                                 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2546                                 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2547                                 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2548                                 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2549                                 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2550                                 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2551                                 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2552                                 - 4
2553                         ) / 8;
2554
2555                 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2556                 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2557                 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2558                 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2559                 b[4] = (FLAC__byte)(samples & 0xFF);
2560                 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + total_samples_byte_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2561                         if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2562                                 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2563                         return;
2564                 }
2565                 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2566                         encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2567                         return;
2568                 }
2569         }
2570
2571         /*
2572          * Write min/max framesize
2573          */
2574         {
2575                 const unsigned min_framesize_offset =
2576                         FLAC__STREAM_METADATA_HEADER_LENGTH +
2577                         (
2578                                 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2579                                 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2580                         ) / 8;
2581
2582                 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2583                 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2584                 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2585                 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2586                 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2587                 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2588                 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + min_framesize_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2589                         if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2590                                 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2591                         return;
2592                 }
2593                 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2594                         encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2595                         return;
2596                 }
2597         }
2598
2599         /*
2600          * Write seektable
2601          */
2602         if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2603                 unsigned i;
2604
2605                 FLAC__format_seektable_sort(encoder->private_->seek_table);
2606
2607                 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2608
2609                 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->seektable_offset + FLAC__STREAM_METADATA_HEADER_LENGTH, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2610                         if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2611                                 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2612                         return;
2613                 }
2614
2615                 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2616                         FLAC__uint64 xx;
2617                         unsigned x;
2618                         xx = encoder->private_->seek_table->points[i].sample_number;
2619                         b[7] = (FLAC__byte)xx; xx >>= 8;
2620                         b[6] = (FLAC__byte)xx; xx >>= 8;
2621                         b[5] = (FLAC__byte)xx; xx >>= 8;
2622                         b[4] = (FLAC__byte)xx; xx >>= 8;
2623                         b[3] = (FLAC__byte)xx; xx >>= 8;
2624                         b[2] = (FLAC__byte)xx; xx >>= 8;
2625                         b[1] = (FLAC__byte)xx; xx >>= 8;
2626                         b[0] = (FLAC__byte)xx; xx >>= 8;
2627                         xx = encoder->private_->seek_table->points[i].stream_offset;
2628                         b[15] = (FLAC__byte)xx; xx >>= 8;
2629                         b[14] = (FLAC__byte)xx; xx >>= 8;
2630                         b[13] = (FLAC__byte)xx; xx >>= 8;
2631                         b[12] = (FLAC__byte)xx; xx >>= 8;
2632                         b[11] = (FLAC__byte)xx; xx >>= 8;
2633                         b[10] = (FLAC__byte)xx; xx >>= 8;
2634                         b[9] = (FLAC__byte)xx; xx >>= 8;
2635                         b[8] = (FLAC__byte)xx; xx >>= 8;
2636                         x = encoder->private_->seek_table->points[i].frame_samples;
2637                         b[17] = (FLAC__byte)x; x >>= 8;
2638                         b[16] = (FLAC__byte)x; x >>= 8;
2639                         if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2640                                 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2641                                 return;
2642                         }
2643                 }
2644         }
2645 }
2646
2647 #if FLAC__HAS_OGG
2648 /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks.  */
2649 void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2650 {
2651         /* the # of bytes in the 1st packet that precede the STREAMINFO */
2652         static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
2653                 FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
2654                 FLAC__OGG_MAPPING_MAGIC_LENGTH +
2655                 FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
2656                 FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
2657                 FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
2658                 FLAC__STREAM_SYNC_LENGTH
2659         ;
2660         FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2661         const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2662         const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2663         const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2664         const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2665         ogg_page page;
2666
2667         FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2668         FLAC__ASSERT(0 != encoder->private_->seek_callback);
2669
2670         /* Pre-check that client supports seeking, since we don't want the
2671          * ogg_helper code to ever have to deal with this condition.
2672          */
2673         if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
2674                 return;
2675
2676         /* All this is based on intimate knowledge of the stream header
2677          * layout, but a change to the header format that would break this
2678          * would also break all streams encoded in the previous format.
2679          */
2680
2681         /**
2682          ** Write STREAMINFO stats
2683          **/
2684         simple_ogg_page__init(&page);
2685         if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2686                 simple_ogg_page__clear(&page);
2687                 return; /* state already set */
2688         }
2689
2690         /*
2691          * Write MD5 signature
2692          */
2693         {
2694                 const unsigned md5_offset =
2695                         FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2696                         FLAC__STREAM_METADATA_HEADER_LENGTH +
2697                         (
2698                                 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2699                                 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2700                                 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2701                                 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2702                                 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2703                                 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2704                                 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2705                                 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2706                         ) / 8;
2707
2708                 if(md5_offset + 16 > (unsigned)page.body_len) {
2709                         encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2710                         simple_ogg_page__clear(&page);
2711                         return;
2712                 }
2713                 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2714         }
2715
2716         /*
2717          * Write total samples
2718          */
2719         {
2720                 const unsigned total_samples_byte_offset =
2721                         FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2722                         FLAC__STREAM_METADATA_HEADER_LENGTH +
2723                         (
2724                                 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2725                                 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2726                                 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2727                                 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2728                                 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2729                                 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2730                                 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2731                                 - 4
2732                         ) / 8;
2733
2734                 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2735                         encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2736                         simple_ogg_page__clear(&page);
2737                         return;
2738                 }
2739                 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2740                 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2741                 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2742                 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2743                 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2744                 b[4] = (FLAC__byte)(samples & 0xFF);
2745                 memcpy(page.body + total_samples_byte_offset, b, 5);
2746         }
2747
2748         /*
2749          * Write min/max framesize
2750          */
2751         {
2752                 const unsigned min_framesize_offset =
2753                         FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2754                         FLAC__STREAM_METADATA_HEADER_LENGTH +
2755                         (
2756                                 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2757                                 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2758                         ) / 8;
2759
2760                 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
2761                         encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2762                         simple_ogg_page__clear(&page);
2763                         return;
2764                 }
2765                 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2766                 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2767                 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2768                 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2769                 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2770                 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2771                 memcpy(page.body + min_framesize_offset, b, 6);
2772         }
2773         if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2774                 simple_ogg_page__clear(&page);
2775                 return; /* state already set */
2776         }
2777         simple_ogg_page__clear(&page);
2778
2779         /*
2780          * Write seektable
2781          */
2782         if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2783                 unsigned i;
2784                 FLAC__byte *p;
2785
2786                 FLAC__format_seektable_sort(encoder->private_->seek_table);
2787
2788                 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2789
2790                 simple_ogg_page__init(&page);
2791                 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2792                         simple_ogg_page__clear(&page);
2793                         return; /* state already set */
2794                 }
2795
2796                 if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) {
2797                         encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2798                         simple_ogg_page__clear(&page);
2799                         return;
2800                 }
2801
2802                 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
2803                         FLAC__uint64 xx;
2804                         unsigned x;
2805                         xx = encoder->private_->seek_table->points[i].sample_number;
2806                         b[7] = (FLAC__byte)xx; xx >>= 8;
2807                         b[6] = (FLAC__byte)xx; xx >>= 8;
2808                         b[5] = (FLAC__byte)xx; xx >>= 8;
2809                         b[4] = (FLAC__byte)xx; xx >>= 8;
2810                         b[3] = (FLAC__byte)xx; xx >>= 8;
2811                         b[2] = (FLAC__byte)xx; xx >>= 8;
2812                         b[1] = (FLAC__byte)xx; xx >>= 8;
2813                         b[0] = (FLAC__byte)xx; xx >>= 8;
2814                         xx = encoder->private_->seek_table->points[i].stream_offset;
2815                         b[15] = (FLAC__byte)xx; xx >>= 8;
2816                         b[14] = (FLAC__byte)xx; xx >>= 8;
2817                         b[13] = (FLAC__byte)xx; xx >>= 8;
2818                         b[12] = (FLAC__byte)xx; xx >>= 8;
2819                         b[11] = (FLAC__byte)xx; xx >>= 8;
2820                         b[10] = (FLAC__byte)xx; xx >>= 8;
2821                         b[9] = (FLAC__byte)xx; xx >>= 8;
2822                         b[8] = (FLAC__byte)xx; xx >>= 8;
2823                         x = encoder->private_->seek_table->points[i].frame_samples;
2824                         b[17] = (FLAC__byte)x; x >>= 8;
2825                         b[16] = (FLAC__byte)x; x >>= 8;
2826                         memcpy(p, b, 18);
2827                 }
2828
2829                 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2830                         simple_ogg_page__clear(&page);
2831                         return; /* state already set */
2832                 }
2833                 simple_ogg_page__clear(&page);
2834         }
2835 }
2836 #endif
2837
2838 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
2839 {
2840         FLAC__uint16 crc;
2841         FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2842
2843         /*
2844          * Accumulate raw signal to the MD5 signature
2845          */
2846         if(encoder->protected_->do_md5 && !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)) {
2847                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2848                 return false;
2849         }
2850
2851         /*
2852          * Process the frame header and subframes into the frame bitbuffer
2853          */
2854         if(!process_subframes_(encoder, is_fractional_block)) {
2855                 /* the above function sets the state for us in case of an error */
2856                 return false;
2857         }
2858
2859         /*
2860          * Zero-pad the frame to a byte_boundary
2861          */
2862         if(!FLAC__bitwriter_zero_pad_to_byte_boundary(encoder->private_->frame)) {
2863                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2864                 return false;
2865         }
2866
2867         /*
2868          * CRC-16 the whole thing
2869          */
2870         FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
2871         if(
2872                 !FLAC__bitwriter_get_write_crc16(encoder->private_->frame, &crc) ||
2873                 !FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, crc, FLAC__FRAME_FOOTER_CRC_LEN)
2874         ) {
2875                 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2876                 return false;
2877         }
2878
2879         /*
2880          * Write it
2881          */
2882         if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
2883                 /* the above function sets the state for us in case of an error */
2884                 return false;
2885         }
2886
2887         /*
2888          * Get ready for the next frame
2889          */
2890         encoder->private_->current_sample_number = 0;
2891         encoder->private_->current_frame_number++;
2892         encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
2893
2894         return true;
2895 }
2896
2897 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
2898 {
2899         FLAC__FrameHeader frame_header;
2900         unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
2901         FLAC__bool do_independent, do_mid_side;
2902
2903         /*
2904          * Calculate the min,max Rice partition orders
2905          */
2906         if(is_fractional_block) {
2907                 max_partition_order = 0;
2908         }
2909         else {
2910                 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
2911                 max_partition_order = flac_min(max_partition_order, encoder->protected_->max_residual_partition_order);
2912         }
2913         min_partition_order = flac_min(min_partition_order, max_partition_order);
2914
2915         /*
2916          * Setup the frame
2917          */
2918         frame_header.blocksize = encoder->protected_->blocksize;
2919         frame_header.sample_rate = encoder->protected_->sample_rate;
2920         frame_header.channels = encoder->protected_->channels;
2921         frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
2922         frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
2923         frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
2924         frame_header.number.frame_number = encoder->private_->current_frame_number;
2925
2926         /*
2927          * Figure out what channel assignments to try
2928          */
2929         if(encoder->protected_->do_mid_side_stereo) {
2930                 if(encoder->protected_->loose_mid_side_stereo) {
2931                         if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
2932                                 do_independent = true;
2933                                 do_mid_side = true;
2934                         }
2935                         else {
2936                                 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
2937                                 do_mid_side = !do_independent;
2938                         }
2939                 }
2940                 else {
2941                         do_independent = true;
2942                         do_mid_side = true;
2943                 }
2944         }
2945         else {
2946                 do_independent = true;
2947                 do_mid_side = false;
2948         }
2949
2950         FLAC__ASSERT(do_independent || do_mid_side);
2951
2952         /*
2953          * Check for wasted bits; set effective bps for each subframe
2954          */
2955         if(do_independent) {
2956                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2957                         const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
2958                         encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
2959                         encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
2960                 }
2961         }
2962         if(do_mid_side) {
2963                 FLAC__ASSERT(encoder->protected_->channels == 2);
2964                 for(channel = 0; channel < 2; channel++) {
2965                         const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
2966                         encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
2967                         encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
2968                 }
2969         }
2970
2971         /*
2972          * First do a normal encoding pass of each independent channel
2973          */
2974         if(do_independent) {
2975                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2976                         if(!
2977                                 process_subframe_(
2978                                         encoder,
2979                                         min_partition_order,
2980                                         max_partition_order,
2981                                         &frame_header,
2982                                         encoder->private_->subframe_bps[channel],
2983                                         encoder->private_->integer_signal[channel],
2984                                         encoder->private_->subframe_workspace_ptr[channel],
2985                                         encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
2986                                         encoder->private_->residual_workspace[channel],
2987                                         encoder->private_->best_subframe+channel,
2988                                         encoder->private_->best_subframe_bits+channel
2989                                 )
2990                         )
2991                                 return false;
2992                 }
2993         }
2994
2995         /*
2996          * Now do mid and side channels if requested
2997          */
2998         if(do_mid_side) {
2999                 FLAC__ASSERT(encoder->protected_->channels == 2);
3000
3001                 for(channel = 0; channel < 2; channel++) {
3002                         if(!
3003                                 process_subframe_(
3004                                         encoder,
3005                                         min_partition_order,
3006                                         max_partition_order,
3007                                         &frame_header,
3008                                         encoder->private_->subframe_bps_mid_side[channel],
3009                                         encoder->private_->integer_signal_mid_side[channel],
3010                                         encoder->private_->subframe_workspace_ptr_mid_side[channel],
3011                                         encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3012                                         encoder->private_->residual_workspace_mid_side[channel],
3013                                         encoder->private_->best_subframe_mid_side+channel,
3014                                         encoder->private_->best_subframe_bits_mid_side+channel
3015                                 )
3016                         )
3017                                 return false;
3018                 }
3019         }
3020
3021         /*
3022          * Compose the frame bitbuffer
3023          */
3024         if(do_mid_side) {
3025                 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3026                 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
3027                 FLAC__ChannelAssignment channel_assignment;
3028
3029                 FLAC__ASSERT(encoder->protected_->channels == 2);
3030
3031                 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3032                         channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
3033                 }
3034                 else {
3035                         unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3036                         unsigned min_bits;
3037                         int ca;
3038
3039                         FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT == 0);
3040                         FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE   == 1);
3041                         FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE  == 2);
3042                         FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_MID_SIDE    == 3);
3043                         FLAC__ASSERT(do_independent && do_mid_side);
3044
3045                         /* We have to figure out which channel assignent results in the smallest frame */
3046                         bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits         [0] + encoder->private_->best_subframe_bits         [1];
3047                         bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->private_->best_subframe_bits         [0] + encoder->private_->best_subframe_bits_mid_side[1];
3048                         bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits         [1] + encoder->private_->best_subframe_bits_mid_side[1];
3049                         bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
3050
3051                         channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
3052                         min_bits = bits[channel_assignment];
3053                         for(ca = 1; ca <= 3; ca++) {
3054                                 if(bits[ca] < min_bits) {
3055                                         min_bits = bits[ca];
3056                                         channel_assignment = (FLAC__ChannelAssignment)ca;
3057                                 }
3058                         }
3059                 }
3060
3061                 frame_header.channel_assignment = channel_assignment;
3062
3063                 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3064                         encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3065                         return false;
3066                 }
3067
3068                 switch(channel_assignment) {
3069                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3070                                 left_subframe  = &encoder->private_->subframe_workspace         [0][encoder->private_->best_subframe         [0]];
3071                                 right_subframe = &encoder->private_->subframe_workspace         [1][encoder->private_->best_subframe         [1]];
3072                                 break;
3073                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3074                                 left_subframe  = &encoder->private_->subframe_workspace         [0][encoder->private_->best_subframe         [0]];
3075                                 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3076                                 break;
3077                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3078                                 left_subframe  = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3079                                 right_subframe = &encoder->private_->subframe_workspace         [1][encoder->private_->best_subframe         [1]];
3080                                 break;
3081                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3082                                 left_subframe  = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3083                                 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3084                                 break;
3085                         default:
3086                                 FLAC__ASSERT(0);
3087                 }
3088
3089                 switch(channel_assignment) {
3090                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3091                                 left_bps  = encoder->private_->subframe_bps         [0];
3092                                 right_bps = encoder->private_->subframe_bps         [1];
3093                                 break;
3094                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3095                                 left_bps  = encoder->private_->subframe_bps         [0];
3096                                 right_bps = encoder->private_->subframe_bps_mid_side[1];
3097                                 break;
3098                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3099                                 left_bps  = encoder->private_->subframe_bps_mid_side[1];
3100                                 right_bps = encoder->private_->subframe_bps         [1];
3101                                 break;
3102                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3103                                 left_bps  = encoder->private_->subframe_bps_mid_side[0];
3104                                 right_bps = encoder->private_->subframe_bps_mid_side[1];
3105                                 break;
3106                         default:
3107                                 FLAC__ASSERT(0);
3108                 }
3109
3110                 /* note that encoder_add_subframe_ sets the state for us in case of an error */
3111                 if(!add_subframe_(encoder, frame_header.blocksize, left_bps , left_subframe , encoder->private_->frame))
3112                         return false;
3113                 if(!add_subframe_(encoder, frame_header.blocksize, right_bps, right_subframe, encoder->private_->frame))
3114                         return false;
3115         }
3116         else {
3117                 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3118                         encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3119                         return false;
3120                 }
3121
3122                 for(channel = 0; channel < encoder->protected_->channels; channel++) {
3123                         if(!add_subframe_(encoder, frame_header.blocksize, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
3124                                 /* the above function sets the state for us in case of an error */
3125                                 return false;
3126                         }
3127                 }
3128         }
3129
3130         if(encoder->protected_->loose_mid_side_stereo) {
3131                 encoder->private_->loose_mid_side_stereo_frame_count++;
3132                 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3133                         encoder->private_->loose_mid_side_stereo_frame_count = 0;
3134         }
3135
3136         encoder->private_->last_channel_assignment = frame_header.channel_assignment;
3137
3138         return true;
3139 }
3140
3141 FLAC__bool process_subframe_(
3142         FLAC__StreamEncoder *encoder,
3143         unsigned min_partition_order,
3144         unsigned max_partition_order,
3145         const FLAC__FrameHeader *frame_header,
3146         unsigned subframe_bps,
3147         const FLAC__int32 integer_signal[],
3148         FLAC__Subframe *subframe[2],
3149         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3150         FLAC__int32 *residual[2],
3151         unsigned *best_subframe,
3152         unsigned *best_bits
3153 )
3154 {
3155 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3156         FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3157 #else
3158         FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3159 #endif
3160 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3161         FLAC__double lpc_residual_bits_per_sample;
3162         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 */
3163         FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
3164         unsigned min_lpc_order, max_lpc_order, lpc_order;
3165         unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
3166 #endif
3167         unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
3168         unsigned rice_parameter;
3169         unsigned _candidate_bits, _best_bits;
3170         unsigned _best_subframe;
3171         /* only use RICE2 partitions if stream bps > 16 */
3172         const unsigned rice_parameter_limit = FLAC__stream_encoder_get_bits_per_sample(encoder) > 16? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3173
3174         FLAC__ASSERT(frame_header->blocksize > 0);
3175
3176         /* verbatim subframe is the baseline against which we measure other compressed subframes */
3177         _best_subframe = 0;
3178         if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3179                 _best_bits = UINT_MAX;
3180         else
3181                 _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3182
3183         if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3184                 unsigned signal_is_constant = false;
3185                 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);
3186                 /* check for constant subframe */
3187                 if(
3188                         !encoder->private_->disable_constant_subframes &&
3189 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3190                         fixed_residual_bits_per_sample[1] == 0.0
3191 #else
3192                         fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3193 #endif
3194                 ) {
3195                         /* the above means it's possible all samples are the same value; now double-check it: */
3196                         unsigned i;
3197                         signal_is_constant = true;
3198                         for(i = 1; i < frame_header->blocksize; i++) {
3199                                 if(integer_signal[0] != integer_signal[i]) {
3200                                         signal_is_constant = false;
3201                                         break;
3202                                 }
3203                         }
3204                 }
3205                 if(signal_is_constant) {
3206                         _candidate_bits = evaluate_constant_subframe_(encoder, integer_signal[0], frame_header->blocksize, subframe_bps, subframe[!_best_subframe]);
3207                         if(_candidate_bits < _best_bits) {
3208                                 _best_subframe = !_best_subframe;
3209                                 _best_bits = _candidate_bits;
3210                         }
3211                 }
3212                 else {
3213                         if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3214                                 /* encode fixed */
3215                                 if(encoder->protected_->do_exhaustive_model_search) {
3216                                         min_fixed_order = 0;
3217                                         max_fixed_order = FLAC__MAX_FIXED_ORDER;
3218                                 }
3219                                 else {
3220                                         min_fixed_order = max_fixed_order = guess_fixed_order;
3221                                 }
3222                                 if(max_fixed_order >= frame_header->blocksize)
3223                                         max_fixed_order = frame_header->blocksize - 1;
3224                                 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
3225 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3226                                         if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
3227                                                 continue; /* don't even try */
3228                                         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 */
3229 #else
3230                                         if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3231                                                 continue; /* don't even try */
3232                                         rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > FLAC__FP_ZERO)? (unsigned)FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]+FLAC__FP_ONE_HALF) : 0; /* 0.5 is for rounding */
3233 #endif
3234                                         rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3235                                         if(rice_parameter >= rice_parameter_limit) {
3236 #ifdef DEBUG_VERBOSE
3237                                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, rice_parameter_limit - 1);
3238 #endif
3239                                                 rice_parameter = rice_parameter_limit - 1;
3240                                         }
3241                                         _candidate_bits =
3242                                                 evaluate_fixed_subframe_(
3243                                                         encoder,
3244                                                         integer_signal,
3245                                                         residual[!_best_subframe],
3246                                                         encoder->private_->abs_residual_partition_sums,
3247                                                         encoder->private_->raw_bits_per_partition,
3248                                                         frame_header->blocksize,
3249                                                         subframe_bps,
3250                                                         fixed_order,
3251                                                         rice_parameter,
3252                                                         rice_parameter_limit,
3253                                                         min_partition_order,
3254                                                         max_partition_order,
3255                                                         encoder->protected_->do_escape_coding,
3256                                                         encoder->protected_->rice_parameter_search_dist,
3257                                                         subframe[!_best_subframe],
3258                                                         partitioned_rice_contents[!_best_subframe]
3259                                                 );
3260                                         if(_candidate_bits < _best_bits) {
3261                                                 _best_subframe = !_best_subframe;
3262                                                 _best_bits = _candidate_bits;
3263                                         }
3264                                 }
3265                         }
3266
3267 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3268                         /* encode lpc */
3269                         if(encoder->protected_->max_lpc_order > 0) {
3270                                 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
3271                                         max_lpc_order = frame_header->blocksize-1;
3272                                 else
3273                                         max_lpc_order = encoder->protected_->max_lpc_order;
3274                                 if(max_lpc_order > 0) {
3275                                         unsigned a;
3276                                         for (a = 0; a < encoder->protected_->num_apodizations; a++) {
3277                                                 FLAC__lpc_window_data(integer_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
3278                                                 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3279                                                 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3280                                                 if(autoc[0] != 0.0) {
3281                                                         FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3282                                                         if(encoder->protected_->do_exhaustive_model_search) {
3283                                                                 min_lpc_order = 1;
3284                                                         }
3285                                                         else {
3286                                                                 const unsigned guess_lpc_order =
3287                                                                         FLAC__lpc_compute_best_order(
3288                                                                                 lpc_error,
3289                                                                                 max_lpc_order,
3290                                                                                 frame_header->blocksize,
3291                                                                                 subframe_bps + (
3292                                                                                         encoder->protected_->do_qlp_coeff_prec_search?
3293                                                                                                 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3294                                                                                                 encoder->protected_->qlp_coeff_precision
3295                                                                                 )
3296                                                                         );
3297                                                                 min_lpc_order = max_lpc_order = guess_lpc_order;
3298                                                         }
3299                                                         if(max_lpc_order >= frame_header->blocksize)
3300                                                                 max_lpc_order = frame_header->blocksize - 1;
3301                                                         for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3302                                                                 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3303                                                                 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3304                                                                         continue; /* don't even try */
3305                                                                 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3306                                                                 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3307                                                                 if(rice_parameter >= rice_parameter_limit) {
3308 #ifdef DEBUG_VERBOSE
3309                                                                         fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, rice_parameter_limit - 1);
3310 #endif
3311                                                                         rice_parameter = rice_parameter_limit - 1;
3312                                                                 }
3313                                                                 if(encoder->protected_->do_qlp_coeff_prec_search) {
3314                                                                         min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
3315                                                                         /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3316                                                                         if(subframe_bps <= 17) {
3317                                                                                 max_qlp_coeff_precision = flac_min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
3318                                                                                 max_qlp_coeff_precision = flac_max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3319                                                                         }
3320                                                                         else
3321                                                                                 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3322                                                                 }
3323                                                                 else {
3324                                                                         min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3325                                                                 }
3326                                                                 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3327                                                                         _candidate_bits =
3328                                                                                 evaluate_lpc_subframe_(
3329                                                                                         encoder,
3330                                                                                         integer_signal,
3331                                                                                         residual[!_best_subframe],
3332                                                                                         encoder->private_->abs_residual_partition_sums,
3333                                                                                         encoder->private_->raw_bits_per_partition,
3334                                                                                         encoder->private_->lp_coeff[lpc_order-1],
3335                                                                                         frame_header->blocksize,
3336                                                                                         subframe_bps,
3337                                                                                         lpc_order,
3338                                                                                         qlp_coeff_precision,
3339                                                                                         rice_parameter,
3340                                                                                         rice_parameter_limit,
3341                                                                                         min_partition_order,
3342                                                                                         max_partition_order,
3343                                                                                         encoder->protected_->do_escape_coding,
3344                                                                                         encoder->protected_->rice_parameter_search_dist,
3345                                                                                         subframe[!_best_subframe],
3346                                                                                         partitioned_rice_contents[!_best_subframe]
3347                                                                                 );
3348                                                                         if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3349                                                                                 if(_candidate_bits < _best_bits) {
3350                                                                                         _best_subframe = !_best_subframe;
3351                                                                                         _best_bits = _candidate_bits;
3352                                                                                 }
3353                                                                         }
3354                                                                 }
3355                                                         }
3356                                                 }
3357                                         }
3358                                 }
3359                         }
3360 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
3361                 }
3362         }
3363
3364         /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3365         if(_best_bits == UINT_MAX) {
3366                 FLAC__ASSERT(_best_subframe == 0);
3367                 _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3368         }
3369
3370         *best_subframe = _best_subframe;
3371         *best_bits = _best_bits;
3372
3373         return true;
3374 }
3375
3376 FLAC__bool add_subframe_(
3377         FLAC__StreamEncoder *encoder,
3378         unsigned blocksize,
3379         unsigned subframe_bps,
3380         const FLAC__Subframe *subframe,
3381         FLAC__BitWriter *frame
3382 )
3383 {
3384         switch(subframe->type) {
3385                 case FLAC__SUBFRAME_TYPE_CONSTANT:
3386                         if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
3387                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3388                                 return false;
3389                         }
3390                         break;
3391                 case FLAC__SUBFRAME_TYPE_FIXED:
3392                         if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
3393                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3394                                 return false;
3395                         }
3396                         break;
3397                 case FLAC__SUBFRAME_TYPE_LPC:
3398                         if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
3399                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3400                                 return false;
3401                         }
3402                         break;
3403                 case FLAC__SUBFRAME_TYPE_VERBATIM:
3404                         if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), blocksize, subframe_bps, subframe->wasted_bits, frame)) {
3405                                 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3406                                 return false;
3407                         }
3408                         break;
3409                 default:
3410                         FLAC__ASSERT(0);
3411         }
3412
3413         return true;
3414 }
3415
3416 #define SPOTCHECK_ESTIMATE 0
3417 #if SPOTCHECK_ESTIMATE
3418 static void spotcheck_subframe_estimate_(
3419         FLAC__StreamEncoder *encoder,
3420         unsigned blocksize,
3421         unsigned subframe_bps,
3422         const FLAC__Subframe *subframe,
3423         unsigned estimate
3424 )
3425 {
3426         FLAC__bool ret;
3427         FLAC__BitWriter *frame = FLAC__bitwriter_new();
3428         if(frame == 0) {
3429                 fprintf(stderr, "EST: can't allocate frame\n");
3430                 return;
3431         }
3432         if(!FLAC__bitwriter_init(frame)) {
3433                 fprintf(stderr, "EST: can't init frame\n");
3434                 return;
3435         }
3436         ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame);
3437         FLAC__ASSERT(ret);
3438         {
3439                 const unsigned actual = FLAC__bitwriter_get_input_bits_unconsumed(frame);
3440                 if(estimate != actual)
3441                         fprintf(stderr, "EST: bad, frame#%u sub#%%d type=%8s est=%u, actual=%u, delta=%d\n", encoder->private_->current_frame_number, FLAC__SubframeTypeString[subframe->type], estimate, actual, (int)actual-(int)estimate);
3442         }
3443         FLAC__bitwriter_delete(frame);
3444 }
3445 #endif
3446
3447 unsigned evaluate_constant_subframe_(
3448         FLAC__StreamEncoder *encoder,
3449         const FLAC__int32 signal,
3450         unsigned blocksize,
3451         unsigned subframe_bps,
3452         FLAC__Subframe *subframe
3453 )
3454 {
3455         unsigned estimate;
3456         subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3457         subframe->data.constant.value = signal;
3458
3459         estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + subframe_bps;
3460
3461 #if SPOTCHECK_ESTIMATE
3462         spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3463 #else
3464         (void)encoder, (void)blocksize;
3465 #endif
3466
3467         return estimate;
3468 }
3469
3470 unsigned evaluate_fixed_subframe_(
3471         FLAC__StreamEncoder *encoder,
3472         const FLAC__int32 signal[],
3473         FLAC__int32 residual[],
3474         FLAC__uint64 abs_residual_partition_sums[],
3475         unsigned raw_bits_per_partition[],
3476         unsigned blocksize,
3477         unsigned subframe_bps,
3478         unsigned order,
3479         unsigned rice_parameter,
3480         unsigned rice_parameter_limit,
3481         unsigned min_partition_order,
3482         unsigned max_partition_order,
3483         FLAC__bool do_escape_coding,
3484         unsigned rice_parameter_search_dist,
3485         FLAC__Subframe *subframe,
3486         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3487 )
3488 {
3489         unsigned i, residual_bits, estimate;
3490         const unsigned residual_samples = blocksize - order;
3491
3492         FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3493
3494         subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3495
3496         subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3497         subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3498         subframe->data.fixed.residual = residual;
3499
3500         residual_bits =
3501                 find_best_partition_order_(
3502                         encoder->private_,
3503                         residual,
3504                         abs_residual_partition_sums,
3505                         raw_bits_per_partition,
3506                         residual_samples,
3507                         order,
3508                         rice_parameter,
3509                         rice_parameter_limit,
3510                         min_partition_order,
3511                         max_partition_order,
3512                         subframe_bps,
3513                         do_escape_coding,
3514                         rice_parameter_search_dist,
3515                         &subframe->data.fixed.entropy_coding_method
3516                 );
3517
3518         subframe->data.fixed.order = order;
3519         for(i = 0; i < order; i++)
3520                 subframe->data.fixed.warmup[i] = signal[i];
3521
3522         estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (order * subframe_bps) + residual_bits;
3523
3524 #if SPOTCHECK_ESTIMATE
3525         spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3526 #endif
3527
3528         return estimate;
3529 }
3530
3531 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3532 unsigned evaluate_lpc_subframe_(
3533         FLAC__StreamEncoder *encoder,
3534         const FLAC__int32 signal[],
3535         FLAC__int32 residual[],
3536         FLAC__uint64 abs_residual_partition_sums[],
3537         unsigned raw_bits_per_partition[],
3538         const FLAC__real lp_coeff[],
3539         unsigned blocksize,
3540         unsigned subframe_bps,
3541         unsigned order,
3542         unsigned qlp_coeff_precision,
3543         unsigned rice_parameter,
3544         unsigned rice_parameter_limit,
3545         unsigned min_partition_order,
3546         unsigned max_partition_order,
3547         FLAC__bool do_escape_coding,
3548         unsigned rice_parameter_search_dist,
3549         FLAC__Subframe *subframe,
3550         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3551 )
3552 {
3553         FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
3554         unsigned i, residual_bits, estimate;
3555         int quantization, ret;
3556         const unsigned residual_samples = blocksize - order;
3557
3558         /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3559         if(subframe_bps <= 16) {
3560                 FLAC__ASSERT(order > 0);
3561                 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3562                 qlp_coeff_precision = flac_min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3563         }
3564
3565         ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
3566         if(ret != 0)
3567                 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3568
3569         if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3570                 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3571                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3572                 else
3573                         encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3574         else
3575                 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3576
3577         subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3578
3579         subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3580         subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3581         subframe->data.lpc.residual = residual;
3582
3583         residual_bits =
3584                 find_best_partition_order_(
3585                         encoder->private_,
3586                         residual,
3587                         abs_residual_partition_sums,
3588                         raw_bits_per_partition,
3589                         residual_samples,
3590                         order,
3591                         rice_parameter,
3592                         rice_parameter_limit,
3593                         min_partition_order,
3594                         max_partition_order,
3595                         subframe_bps,
3596                         do_escape_coding,
3597                         rice_parameter_search_dist,
3598                         &subframe->data.lpc.entropy_coding_method
3599                 );
3600
3601         subframe->data.lpc.order = order;
3602         subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3603         subframe->data.lpc.quantization_level = quantization;
3604         memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
3605         for(i = 0; i < order; i++)
3606                 subframe->data.lpc.warmup[i] = signal[i];
3607
3608         estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
3609
3610 #if SPOTCHECK_ESTIMATE
3611         spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3612 #endif
3613
3614         return estimate;
3615 }
3616 #endif
3617
3618 unsigned evaluate_verbatim_subframe_(
3619         FLAC__StreamEncoder *encoder,
3620         const FLAC__int32 signal[],
3621         unsigned blocksize,
3622         unsigned subframe_bps,
3623         FLAC__Subframe *subframe
3624 )
3625 {
3626         unsigned estimate;
3627
3628         subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3629
3630         subframe->data.verbatim.data = signal;
3631
3632         estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps);
3633
3634 #if SPOTCHECK_ESTIMATE
3635         spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3636 #else
3637         (void)encoder;
3638 #endif
3639
3640         return estimate;
3641 }
3642
3643 unsigned find_best_partition_order_(
3644         FLAC__StreamEncoderPrivate *private_,
3645         const FLAC__int32 residual[],
3646         FLAC__uint64 abs_residual_partition_sums[],
3647         unsigned raw_bits_per_partition[],
3648         unsigned residual_samples,
3649         unsigned predictor_order,
3650         unsigned rice_parameter,
3651         unsigned rice_parameter_limit,
3652         unsigned min_partition_order,
3653         unsigned max_partition_order,
3654         unsigned bps,
3655         FLAC__bool do_escape_coding,
3656         unsigned rice_parameter_search_dist,
3657         FLAC__EntropyCodingMethod *best_ecm
3658 )
3659 {
3660         unsigned residual_bits, best_residual_bits = 0;
3661         unsigned best_parameters_index = 0;
3662         unsigned best_partition_order = 0;
3663         const unsigned blocksize = residual_samples + predictor_order;
3664
3665         max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
3666         min_partition_order = flac_min(min_partition_order, max_partition_order);
3667
3668         precompute_partition_info_sums_(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order, bps);
3669
3670         if(do_escape_coding)
3671                 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
3672
3673         {
3674                 int partition_order;
3675                 unsigned sum;
3676
3677                 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3678                         if(!
3679                                 set_partitioned_rice_(
3680 #ifdef EXACT_RICE_BITS_CALCULATION
3681                                         residual,
3682 #endif
3683                                         abs_residual_partition_sums+sum,
3684                                         raw_bits_per_partition+sum,
3685                                         residual_samples,
3686                                         predictor_order,
3687                                         rice_parameter,
3688                                         rice_parameter_limit,
3689                                         rice_parameter_search_dist,
3690                                         (unsigned)partition_order,
3691                                         do_escape_coding,
3692                                         &private_->partitioned_rice_contents_extra[!best_parameters_index],
3693                                         &residual_bits
3694                                 )
3695                         )
3696                         {
3697                                 FLAC__ASSERT(best_residual_bits != 0);
3698                                 break;
3699                         }
3700                         sum += 1u << partition_order;
3701                         if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3702                                 best_residual_bits = residual_bits;
3703                                 best_parameters_index = !best_parameters_index;
3704                                 best_partition_order = partition_order;
3705                         }
3706                 }
3707         }
3708
3709         best_ecm->data.partitioned_rice.order = best_partition_order;
3710
3711         {
3712                 /*
3713                  * We are allowed to de-const the pointer based on our special
3714                  * knowledge; it is const to the outside world.
3715                  */
3716                 FLAC__EntropyCodingMethod_PartitionedRiceContents* prc = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_ecm->data.partitioned_rice.contents;
3717                 unsigned partition;
3718
3719                 /* save best parameters and raw_bits */
3720                 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(prc, flac_max(6u, best_partition_order));
3721                 memcpy(prc->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partition_order)));
3722                 if(do_escape_coding)
3723                         memcpy(prc->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partition_order)));
3724                 /*
3725                  * Now need to check if the type should be changed to
3726                  * FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2 based on the
3727                  * size of the rice parameters.
3728                  */
3729                 for(partition = 0; partition < (1u<<best_partition_order); partition++) {
3730                         if(prc->parameters[partition] >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3731                                 best_ecm->type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2;
3732                                 break;
3733                         }
3734                 }
3735         }
3736
3737         return best_residual_bits;
3738 }
3739
3740 #if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
3741 extern void precompute_partition_info_sums_32bit_asm_ia32_(
3742         const FLAC__int32 residual[],
3743         FLAC__uint64 abs_residual_partition_sums[],
3744         unsigned blocksize,
3745         unsigned predictor_order,
3746         unsigned min_partition_order,
3747         unsigned max_partition_order
3748 );
3749 #endif
3750
3751 void precompute_partition_info_sums_(
3752         const FLAC__int32 residual[],
3753         FLAC__uint64 abs_residual_partition_sums[],
3754         unsigned residual_samples,
3755         unsigned predictor_order,
3756         unsigned min_partition_order,
3757         unsigned max_partition_order,
3758         unsigned bps
3759 )
3760 {
3761         const unsigned default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
3762         unsigned partitions = 1u << max_partition_order;
3763
3764         FLAC__ASSERT(default_partition_samples > predictor_order);
3765
3766 #if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
3767         /* slightly pessimistic but still catches all common cases */
3768         /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */
3769         if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
3770                 precompute_partition_info_sums_32bit_asm_ia32_(residual, abs_residual_partition_sums, residual_samples + predictor_order, predictor_order, min_partition_order, max_partition_order);
3771                 return;
3772         }
3773 #endif
3774
3775         /* first do max_partition_order */
3776         {
3777                 unsigned partition, residual_sample, end = (unsigned)(-(int)predictor_order);
3778                 /* slightly pessimistic but still catches all common cases */
3779                 /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */
3780                 if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
3781                         FLAC__uint32 abs_residual_partition_sum;
3782
3783                         for(partition = residual_sample = 0; partition < partitions; partition++) {
3784                                 end += default_partition_samples;
3785                                 abs_residual_partition_sum = 0;
3786                                 for( ; residual_sample < end; residual_sample++)
3787                                         abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
3788                                 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
3789                         }
3790                 }
3791                 else { /* have to pessimistically use 64 bits for accumulator */
3792                         FLAC__uint64 abs_residual_partition_sum;
3793
3794                         for(partition = residual_sample = 0; partition < partitions; partition++) {
3795                                 end += default_partition_samples;
3796                                 abs_residual_partition_sum = 0;
3797                                 for( ; residual_sample < end; residual_sample++)
3798                                         abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
3799                                 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
3800                         }
3801                 }
3802         }
3803
3804         /* now merge partitions for lower orders */
3805         {
3806                 unsigned from_partition = 0, to_partition = partitions;
3807                 int partition_order;
3808                 for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) {
3809                         unsigned i;
3810                         partitions >>= 1;
3811                         for(i = 0; i < partitions; i++) {
3812                                 abs_residual_partition_sums[to_partition++] =
3813                                         abs_residual_partition_sums[from_partition  ] +
3814                                         abs_residual_partition_sums[from_partition+1];
3815                                 from_partition += 2;
3816                         }
3817                 }
3818         }
3819 }
3820
3821 void precompute_partition_info_escapes_(
3822         const FLAC__int32 residual[],
3823         unsigned raw_bits_per_partition[],
3824         unsigned residual_samples,
3825         unsigned predictor_order,
3826         unsigned min_partition_order,
3827         unsigned max_partition_order
3828 )
3829 {
3830         int partition_order;
3831         unsigned from_partition, to_partition = 0;
3832         const unsigned blocksize = residual_samples + predictor_order;
3833
3834         /* first do max_partition_order */
3835         for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3836                 FLAC__int32 r;
3837                 FLAC__uint32 rmax;
3838                 unsigned partition, partition_sample, partition_samples, residual_sample;
3839                 const unsigned partitions = 1u << partition_order;
3840                 const unsigned default_partition_samples = blocksize >> partition_order;
3841
3842                 FLAC__ASSERT(default_partition_samples > predictor_order);
3843
3844                 for(partition = residual_sample = 0; partition < partitions; partition++) {
3845                         partition_samples = default_partition_samples;
3846                         if(partition == 0)
3847                                 partition_samples -= predictor_order;
3848                         rmax = 0;
3849                         for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3850                                 r = residual[residual_sample++];
3851                                 /* OPT: maybe faster: rmax |= r ^ (r>>31) */
3852                                 if(r < 0)
3853                                         rmax |= ~r;
3854                                 else
3855                                         rmax |= r;
3856                         }
3857                         /* now we know all residual values are in the range [-rmax-1,rmax] */
3858                         raw_bits_per_partition[partition] = rmax? FLAC__bitmath_ilog2(rmax) + 2 : 1;
3859                 }
3860                 to_partition = partitions;
3861                 break; /*@@@ yuck, should remove the 'for' loop instead */
3862         }
3863
3864         /* now merge partitions for lower orders */
3865         for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3866                 unsigned m;
3867                 unsigned i;
3868                 const unsigned partitions = 1u << partition_order;
3869                 for(i = 0; i < partitions; i++) {
3870                         m = raw_bits_per_partition[from_partition];
3871                         from_partition++;
3872                         raw_bits_per_partition[to_partition] = flac_max(m, raw_bits_per_partition[from_partition]);
3873                         from_partition++;
3874                         to_partition++;
3875                 }
3876         }
3877 }
3878
3879 #ifdef EXACT_RICE_BITS_CALCULATION
3880 static inline unsigned count_rice_bits_in_partition_(
3881         const unsigned rice_parameter,
3882         const unsigned partition_samples,
3883         const FLAC__int32 *residual
3884 )
3885 {
3886         unsigned i, partition_bits =
3887                 FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
3888                 (1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */
3889         ;
3890         for(i = 0; i < partition_samples; i++)
3891                 partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter );
3892         return partition_bits;
3893 }
3894 #else
3895 static inline unsigned count_rice_bits_in_partition_(
3896         const unsigned rice_parameter,
3897         const unsigned partition_samples,
3898         const FLAC__uint64 abs_residual_partition_sum
3899 )
3900 {
3901         return
3902                 FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
3903                 (1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */
3904                 (
3905                         rice_parameter?
3906                                 (unsigned)(abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */
3907                                 : (unsigned)(abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */
3908                 )
3909                 - (partition_samples >> 1)
3910                 /* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum.
3911                  * The actual number of bits used is closer to the sum(for all i in the partition) of  abs(residual[i])>>(rice_parameter-1)
3912                  * By using the abs_residual_partition sum, we also add in bits in the LSBs that would normally be shifted out.
3913                  * So the subtraction term tries to guess how many extra bits were contributed.
3914                  * If the LSBs are randomly distributed, this should average to 0.5 extra bits per sample.
3915                  */
3916         ;
3917 }
3918 #endif
3919
3920 FLAC__bool set_partitioned_rice_(
3921 #ifdef EXACT_RICE_BITS_CALCULATION
3922         const FLAC__int32 residual[],
3923 #endif
3924         const FLAC__uint64 abs_residual_partition_sums[],
3925         const unsigned raw_bits_per_partition[],
3926         const unsigned residual_samples,
3927         const unsigned predictor_order,
3928         const unsigned suggested_rice_parameter,
3929         const unsigned rice_parameter_limit,
3930         const unsigned rice_parameter_search_dist,
3931         const unsigned partition_order,
3932         const FLAC__bool search_for_escapes,
3933         FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3934         unsigned *bits
3935 )
3936 {
3937         unsigned rice_parameter, partition_bits;
3938         unsigned best_partition_bits, best_rice_parameter = 0;
3939         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
3940         unsigned *parameters, *raw_bits;
3941 #ifdef ENABLE_RICE_PARAMETER_SEARCH
3942         unsigned min_rice_parameter, max_rice_parameter;
3943 #else
3944         (void)rice_parameter_search_dist;
3945 #endif
3946
3947         FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
3948         FLAC__ASSERT(rice_parameter_limit <= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
3949
3950         FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order));
3951         parameters = partitioned_rice_contents->parameters;
3952         raw_bits = partitioned_rice_contents->raw_bits;
3953
3954         if(partition_order == 0) {
3955                 best_partition_bits = (unsigned)(-1);
3956 #ifdef ENABLE_RICE_PARAMETER_SEARCH
3957                 if(rice_parameter_search_dist) {
3958                         if(suggested_rice_parameter < rice_parameter_search_dist)
3959                                 min_rice_parameter = 0;
3960                         else
3961                                 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3962                         max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3963                         if(max_rice_parameter >= rice_parameter_limit) {
3964 #ifdef DEBUG_VERBOSE
3965                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, rice_parameter_limit - 1);
3966 #endif
3967                                 max_rice_parameter = rice_parameter_limit - 1;
3968                         }
3969                 }
3970                 else
3971                         min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3972
3973                 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3974 #else
3975                         rice_parameter = suggested_rice_parameter;
3976 #endif
3977 #ifdef EXACT_RICE_BITS_CALCULATION
3978                         partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, residual);
3979 #else
3980                         partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, abs_residual_partition_sums[0]);
3981 #endif
3982                         if(partition_bits < best_partition_bits) {
3983                                 best_rice_parameter = rice_parameter;
3984                                 best_partition_bits = partition_bits;
3985                         }
3986 #ifdef ENABLE_RICE_PARAMETER_SEARCH
3987                 }
3988 #endif
3989                 if(search_for_escapes) {
3990                         partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
3991                         if(partition_bits <= best_partition_bits) {
3992                                 raw_bits[0] = raw_bits_per_partition[0];
3993                                 best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
3994                                 best_partition_bits = partition_bits;
3995                         }
3996                         else
3997                                 raw_bits[0] = 0;
3998                 }
3999                 parameters[0] = best_rice_parameter;
4000                 bits_ += best_partition_bits;
4001         }
4002         else {
4003                 unsigned partition, residual_sample;
4004                 unsigned partition_samples;
4005                 FLAC__uint64 mean, k;
4006                 const unsigned partitions = 1u << partition_order;
4007                 for(partition = residual_sample = 0; partition < partitions; partition++) {
4008                         partition_samples = (residual_samples+predictor_order) >> partition_order;
4009                         if(partition == 0) {
4010                                 if(partition_samples <= predictor_order)
4011                                         return false;
4012                                 else
4013                                         partition_samples -= predictor_order;
4014                         }
4015                         mean = abs_residual_partition_sums[partition];
4016                         /* we are basically calculating the size in bits of the
4017                          * average residual magnitude in the partition:
4018                          *   rice_parameter = floor(log2(mean/partition_samples))
4019                          * 'mean' is not a good name for the variable, it is
4020                          * actually the sum of magnitudes of all residual values
4021                          * in the partition, so the actual mean is
4022                          * mean/partition_samples
4023                          */
4024                         for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
4025                                 ;
4026                         if(rice_parameter >= rice_parameter_limit) {
4027 #ifdef DEBUG_VERBOSE
4028                                 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, rice_parameter_limit - 1);
4029 #endif
4030                                 rice_parameter = rice_parameter_limit - 1;
4031                         }
4032
4033                         best_partition_bits = (unsigned)(-1);
4034 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4035                         if(rice_parameter_search_dist) {
4036                                 if(rice_parameter < rice_parameter_search_dist)
4037                                         min_rice_parameter = 0;
4038                                 else
4039                                         min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4040                                 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4041                                 if(max_rice_parameter >= rice_parameter_limit) {
4042 #ifdef DEBUG_VERBOSE
4043                                         fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, rice_parameter_limit - 1);
4044 #endif
4045                                         max_rice_parameter = rice_parameter_limit - 1;
4046                                 }
4047                         }
4048                         else
4049                                 min_rice_parameter = max_rice_parameter = rice_parameter;
4050
4051                         for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4052 #endif
4053 #ifdef EXACT_RICE_BITS_CALCULATION
4054                                 partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, residual+residual_sample);
4055 #else
4056                                 partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, abs_residual_partition_sums[partition]);
4057 #endif
4058                                 if(partition_bits < best_partition_bits) {
4059                                         best_rice_parameter = rice_parameter;
4060                                         best_partition_bits = partition_bits;
4061                                 }
4062 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4063                         }
4064 #endif
4065                         if(search_for_escapes) {
4066                                 partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
4067                                 if(partition_bits <= best_partition_bits) {
4068                                         raw_bits[partition] = raw_bits_per_partition[partition];
4069                                         best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
4070                                         best_partition_bits = partition_bits;
4071                                 }
4072                                 else
4073                                         raw_bits[partition] = 0;
4074                         }
4075                         parameters[partition] = best_rice_parameter;
4076                         bits_ += best_partition_bits;
4077                         residual_sample += partition_samples;
4078                 }
4079         }
4080
4081         *bits = bits_;
4082         return true;
4083 }
4084
4085 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
4086 {
4087         unsigned i, shift;
4088         FLAC__int32 x = 0;
4089
4090         for(i = 0; i < samples && !(x&1); i++)
4091                 x |= signal[i];
4092
4093         if(x == 0) {
4094                 shift = 0;
4095         }
4096         else {
4097                 for(shift = 0; !(x&1); shift++)
4098                         x >>= 1;
4099         }
4100
4101         if(shift > 0) {
4102                 for(i = 0; i < samples; i++)
4103                          signal[i] >>= shift;
4104         }
4105
4106         return shift;
4107 }
4108
4109 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4110 {
4111         unsigned channel;
4112
4113         for(channel = 0; channel < channels; channel++)
4114                 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4115
4116         fifo->tail += wide_samples;
4117
4118         FLAC__ASSERT(fifo->tail <= fifo->size);
4119 }
4120
4121 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4122 {
4123         unsigned channel;
4124         unsigned sample, wide_sample;
4125         unsigned tail = fifo->tail;
4126
4127         sample = input_offset * channels;
4128         for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4129                 for(channel = 0; channel < channels; channel++)
4130                         fifo->data[channel][tail] = input[sample++];
4131                 tail++;
4132         }
4133         fifo->tail = tail;
4134
4135         FLAC__ASSERT(fifo->tail <= fifo->size);
4136 }
4137
4138 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4139 {
4140         FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4141         const size_t encoded_bytes = encoder->private_->verify.output.bytes;
4142         (void)decoder;
4143
4144         if(encoder->private_->verify.needs_magic_hack) {
4145                 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4146                 *bytes = FLAC__STREAM_SYNC_LENGTH;
4147                 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4148                 encoder->private_->verify.needs_magic_hack = false;
4149         }
4150         else {
4151                 if(encoded_bytes == 0) {
4152                         /*
4153                          * If we get here, a FIFO underflow has occurred,
4154                          * which means there is a bug somewhere.
4155                          */
4156                         FLAC__ASSERT(0);
4157                         return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4158                 }
4159                 else if(encoded_bytes < *bytes)
4160                         *bytes = encoded_bytes;
4161                 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4162                 encoder->private_->verify.output.data += *bytes;
4163                 encoder->private_->verify.output.bytes -= *bytes;
4164         }
4165
4166         return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4167 }
4168
4169 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4170 {
4171         FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4172         unsigned channel;
4173         const unsigned channels = frame->header.channels;
4174         const unsigned blocksize = frame->header.blocksize;
4175         const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4176
4177         (void)decoder;
4178
4179         for(channel = 0; channel < channels; channel++) {
4180                 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4181                         unsigned i, sample = 0;
4182                         FLAC__int32 expect = 0, got = 0;
4183
4184                         for(i = 0; i < blocksize; i++) {
4185                                 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4186                                         sample = i;
4187                                         expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4188                                         got = (FLAC__int32)buffer[channel][i];
4189                                         break;
4190                                 }
4191                         }
4192                         FLAC__ASSERT(i < blocksize);
4193                         FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4194                         encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
4195                         encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
4196                         encoder->private_->verify.error_stats.channel = channel;
4197                         encoder->private_->verify.error_stats.sample = sample;
4198                         encoder->private_->verify.error_stats.expected = expect;
4199                         encoder->private_->verify.error_stats.got = got;
4200                         encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4201                         return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4202                 }
4203         }
4204         /* dequeue the frame from the fifo */
4205         encoder->private_->verify.input_fifo.tail -= blocksize;
4206         FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
4207         for(channel = 0; channel < channels; channel++)
4208                 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail * sizeof(encoder->private_->verify.input_fifo.data[0][0]));
4209         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4210 }
4211
4212 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4213 {
4214         (void)decoder, (void)metadata, (void)client_data;
4215 }
4216
4217 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4218 {
4219         FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4220         (void)decoder, (void)status;
4221         encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4222 }
4223
4224 FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4225 {
4226         (void)client_data;
4227
4228         *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
4229         if (*bytes == 0) {
4230                 if (feof(encoder->private_->file))
4231                         return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4232                 else if (ferror(encoder->private_->file))
4233                         return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4234         }
4235         return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4236 }
4237
4238 FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4239 {
4240         (void)client_data;
4241
4242         if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4243                 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4244         else
4245                 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4246 }
4247
4248 FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4249 {
4250         off_t offset;
4251
4252         (void)client_data;
4253
4254         offset = ftello(encoder->private_->file);
4255
4256         if(offset < 0) {
4257                 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4258         }
4259         else {
4260                 *absolute_byte_offset = (FLAC__uint64)offset;
4261                 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4262         }
4263 }
4264
4265 #ifdef FLAC__VALGRIND_TESTING
4266 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4267 {
4268         size_t ret = fwrite(ptr, size, nmemb, stream);
4269         if(!ferror(stream))
4270                 fflush(stream);
4271         return ret;
4272 }
4273 #else
4274 #define local__fwrite fwrite
4275 #endif
4276
4277 FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
4278 {
4279         (void)client_data, (void)current_frame;
4280
4281         if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
4282                 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4283 #if FLAC__HAS_OGG
4284                         /* We would like to be able to use 'samples > 0' in the
4285                          * clause here but currently because of the nature of our
4286                          * Ogg writing implementation, 'samples' is always 0 (see
4287                          * ogg_encoder_aspect.c).  The downside is extra progress
4288                          * callbacks.
4289                          */
4290                         encoder->private_->is_ogg? true :
4291 #endif
4292                         samples > 0
4293                 );
4294                 if(call_it) {
4295                         /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4296                          * because at this point in the callback chain, the stats
4297                          * have not been updated.  Only after we return and control
4298                          * gets back to write_frame_() are the stats updated
4299                          */
4300                         encoder->private_->progress_callback(encoder, encoder->private_->bytes_written+bytes, encoder->private_->samples_written+samples, encoder->private_->frames_written+(samples?1:0), encoder->private_->total_frames_estimate, encoder->private_->client_data);
4301                 }
4302                 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4303         }
4304         else
4305                 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4306 }
4307
4308 /*
4309  * This will forcibly set stdout to binary mode (for OSes that require it)
4310  */
4311 FILE *get_binary_stdout_(void)
4312 {
4313         /* if something breaks here it is probably due to the presence or
4314          * absence of an underscore before the identifiers 'setmode',
4315          * 'fileno', and/or 'O_BINARY'; check your system header files.
4316          */
4317 #if defined _MSC_VER || defined __MINGW32__
4318         _setmode(_fileno(stdout), _O_BINARY);
4319 #elif defined __CYGWIN__
4320         /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4321         setmode(_fileno(stdout), _O_BINARY);
4322 #elif defined __EMX__
4323         setmode(fileno(stdout), O_BINARY);
4324 #endif
4325
4326         return stdout;
4327 }