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