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