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