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