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