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