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