1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 #if defined _MSC_VER || defined __MINGW32__
37 #include <io.h> /* for _setmode() */
38 #include <fcntl.h> /* for _O_BINARY */
40 #if defined __CYGWIN__ || defined __EMX__
41 #include <io.h> /* for setmode(), O_BINARY */
42 #include <fcntl.h> /* for _O_BINARY */
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"
65 #include "private/ogg_helper.h"
66 #include "private/ogg_mapping.h"
68 #include "private/stream_encoder_framing.h"
69 #include "private/window.h"
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.
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.
85 #undef ENABLE_RICE_PARAMETER_SEARCH
89 FLAC__int32 *data[FLAC__MAX_CHANNELS];
90 unsigned size; /* of each data[] in samples */
95 const FLAC__byte *data;
101 ENCODER_IN_MAGIC = 0,
102 ENCODER_IN_METADATA = 1,
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 }
130 /***********************************************************************
132 * Private class method prototypes
134 ***********************************************************************/
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);
143 static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
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);
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,
162 static FLAC__bool add_subframe_(
163 FLAC__StreamEncoder *encoder,
165 unsigned subframe_bps,
166 const FLAC__Subframe *subframe,
167 FLAC__BitWriter *frame
170 static unsigned evaluate_constant_subframe_(
171 FLAC__StreamEncoder *encoder,
172 const FLAC__int32 signal,
174 unsigned subframe_bps,
175 FLAC__Subframe *subframe
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[],
185 unsigned subframe_bps,
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
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[],
206 unsigned subframe_bps,
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
220 static unsigned evaluate_verbatim_subframe_(
221 FLAC__StreamEncoder *encoder,
222 const FLAC__int32 signal[],
224 unsigned subframe_bps,
225 FLAC__Subframe *subframe
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,
240 FLAC__bool do_escape_coding,
241 unsigned rice_parameter_search_dist,
242 FLAC__EntropyCodingMethod *best_ecm
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,
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
264 static FLAC__bool set_partitioned_rice_(
265 #ifdef EXACT_RICE_BITS_CALCULATION
266 const FLAC__int32 residual[],
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,
281 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
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,
289 unsigned wide_samples
292 static void append_to_verify_fifo_interleaved_(
293 verify_input_fifo *fifo,
294 const FLAC__int32 input[],
295 unsigned input_offset,
297 unsigned wide_samples
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);
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);
312 /***********************************************************************
316 ***********************************************************************/
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[] */
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]);
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]);
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[]);
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;
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;
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;
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;
403 * These fields have been moved here from private function local
404 * declarations merely to save stack space during encoding.
406 #ifndef FLAC__INTEGER_ONLY_LIBRARY
407 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
409 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
411 * The data for the verify section
414 FLAC__StreamDecoder *decoder;
415 EncoderStateHint state_hint;
416 FLAC__bool needs_magic_hack;
417 verify_input_fifo input_fifo;
418 verify_output output;
420 FLAC__uint64 absolute_sample;
421 unsigned frame_number;
424 FLAC__int32 expected;
428 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
429 } FLAC__StreamEncoderPrivate;
431 /***********************************************************************
433 * Public static class data
435 ***********************************************************************/
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"
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"
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"
473 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
474 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
475 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
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"
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"
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.
498 * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
499 * not really any reason to change it.
501 static const unsigned OVERREAD_ = 1;
503 /***********************************************************************
505 * Class constructor/destructor
508 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void)
510 FLAC__StreamEncoder *encoder;
513 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
515 encoder = calloc(1, sizeof(FLAC__StreamEncoder));
520 encoder->protected_ = calloc(1, sizeof(FLAC__StreamEncoderProtected));
521 if(encoder->protected_ == 0) {
526 encoder->private_ = calloc(1, sizeof(FLAC__StreamEncoderPrivate));
527 if(encoder->private_ == 0) {
528 free(encoder->protected_);
533 encoder->private_->frame = FLAC__bitwriter_new();
534 if(encoder->private_->frame == 0) {
535 free(encoder->private_);
536 free(encoder->protected_);
541 encoder->private_->file = 0;
543 set_defaults_(encoder);
545 encoder->private_->is_being_deleted = false;
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];
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];
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];
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];
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]);
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]);
572 for(i = 0; i < 2; i++)
573 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
575 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
580 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
584 FLAC__ASSERT(0 != encoder);
585 FLAC__ASSERT(0 != encoder->protected_);
586 FLAC__ASSERT(0 != encoder->private_);
587 FLAC__ASSERT(0 != encoder->private_->frame);
589 encoder->private_->is_being_deleted = true;
591 (void)FLAC__stream_encoder_finish(encoder);
593 if(0 != encoder->private_->verify.decoder)
594 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
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]);
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]);
604 for(i = 0; i < 2; i++)
605 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
607 FLAC__bitwriter_delete(encoder->private_->frame);
608 free(encoder->private_);
609 free(encoder->protected_);
613 /***********************************************************************
615 * Public class methods
617 ***********************************************************************/
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,
631 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
633 FLAC__ASSERT(0 != encoder);
635 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
636 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
640 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
643 if(0 == write_callback || (seek_callback && 0 == tell_callback))
644 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
646 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
647 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
649 if(encoder->protected_->channels != 2) {
650 encoder->protected_->do_mid_side_stereo = false;
651 encoder->protected_->loose_mid_side_stereo = false;
653 else if(!encoder->protected_->do_mid_side_stereo)
654 encoder->protected_->loose_mid_side_stereo = false;
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 */
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;
662 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
663 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
665 if(encoder->protected_->blocksize == 0) {
666 if(encoder->protected_->max_lpc_order == 0)
667 encoder->protected_->blocksize = 1152;
669 encoder->protected_->blocksize = 4096;
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;
675 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
676 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
678 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
679 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
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);
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;
701 encoder->protected_->qlp_coeff_precision = 13;
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;
709 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
711 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
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;
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;
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
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;
732 encoder->protected_->sample_rate <= 48000 &&
734 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
735 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
738 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
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;
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) {
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];
755 encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
756 encoder->protected_->metadata[0] = vc;
762 /* keep track of any SEEKTABLE block */
763 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
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 */
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;
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;
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;
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 */
809 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
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
816 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
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;
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;
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;
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;
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;
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;
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);
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÷ 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);
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;
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 */
878 * get the CPU info and set the function pointers
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;
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;
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;
906 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
908 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
909 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
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;
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;
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 */
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;
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;
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;
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;
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;
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;
961 * Set up the verify stuff if necessary
963 if(encoder->protected_->verify) {
965 * First, set up the fifo which will hold the
966 * original signal to compare against
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;
975 encoder->private_->verify.input_fifo.tail = 0;
978 * Now set up a stream decoder for verification
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;
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;
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;
1001 * These must be done before we write any metadata, because that
1002 * calls the write_callback, which uses these values.
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;
1011 * write the stream header
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;
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;
1025 * write the STREAMINFO metadata block
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;
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;
1053 * Now that the STREAMINFO block is written, we can init this to an
1054 * absurdly-high value...
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;
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).
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
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;
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;
1090 * write the user's metadata blocks
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;
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;
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;
1110 if(encoder->protected_->verify)
1111 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1113 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
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,
1125 return init_stream_internal_(
1127 /*read_callback=*/0,
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,
1147 return init_stream_internal_(
1159 static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1160 FLAC__StreamEncoder *encoder,
1162 FLAC__StreamEncoderProgressCallback progress_callback,
1167 FLAC__StreamEncoderInitStatus init_status;
1169 FLAC__ASSERT(0 != encoder);
1170 FLAC__ASSERT(0 != file);
1172 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1173 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1175 /* double protection */
1177 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1178 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
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
1187 file = get_binary_stdout_(); /* just to be safe */
1189 encoder->private_->file = file;
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;
1196 init_status = init_stream_internal_(
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,
1206 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1207 /* the above function sets the state for us in case of an error */
1212 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1214 FLAC__ASSERT(blocksize != 0);
1215 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1221 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1222 FLAC__StreamEncoder *encoder,
1224 FLAC__StreamEncoderProgressCallback progress_callback,
1228 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1231 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1232 FLAC__StreamEncoder *encoder,
1234 FLAC__StreamEncoderProgressCallback progress_callback,
1238 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1241 static FLAC__StreamEncoderInitStatus init_file_internal_(
1242 FLAC__StreamEncoder *encoder,
1243 const char *filename,
1244 FLAC__StreamEncoderProgressCallback progress_callback,
1251 FLAC__ASSERT(0 != encoder);
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.
1258 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1259 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1261 file = filename? fopen(filename, "w+b") : stdout;
1264 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1265 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1268 return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1271 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1272 FLAC__StreamEncoder *encoder,
1273 const char *filename,
1274 FLAC__StreamEncoderProgressCallback progress_callback,
1278 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1281 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1282 FLAC__StreamEncoder *encoder,
1283 const char *filename,
1284 FLAC__StreamEncoderProgressCallback progress_callback,
1288 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
1291 FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
1293 FLAC__bool error = false;
1295 FLAC__ASSERT(0 != encoder);
1296 FLAC__ASSERT(0 != encoder->private_);
1297 FLAC__ASSERT(0 != encoder->protected_);
1299 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
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))
1311 if(encoder->protected_->do_md5)
1312 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
1314 if(!encoder->private_->is_being_deleted) {
1315 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
1316 if(encoder->private_->seek_callback) {
1318 if(encoder->private_->is_ogg)
1319 update_ogg_metadata_(encoder);
1322 update_metadata_(encoder);
1324 /* check if an error occurred while updating metadata */
1325 if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
1328 if(encoder->private_->metadata_callback)
1329 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1332 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
1334 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1339 if(0 != encoder->private_->file) {
1340 if(encoder->private_->file != stdout)
1341 fclose(encoder->private_->file);
1342 encoder->private_->file = 0;
1346 if(encoder->private_->is_ogg)
1347 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1351 set_defaults_(encoder);
1354 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1359 FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
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)
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);
1376 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
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)
1383 #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1384 encoder->protected_->verify = value;
1389 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
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)
1396 encoder->protected_->streamable_subset = value;
1400 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_md5(FLAC__StreamEncoder *encoder, FLAC__bool value)
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)
1407 encoder->protected_->do_md5 = value;
1411 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
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)
1418 encoder->protected_->channels = value;
1422 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
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)
1429 encoder->protected_->bits_per_sample = value;
1433 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
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)
1440 encoder->protected_->sample_rate = value;
1444 FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
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)
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
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 */
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;
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);
1478 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
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)
1485 encoder->protected_->blocksize = value;
1489 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
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)
1496 encoder->protected_->do_mid_side_stereo = value;
1500 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
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)
1507 encoder->protected_->loose_mid_side_stereo = value;
1511 /*@@@@add to tests*/
1512 FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
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)
1520 #ifdef FLAC__INTEGER_ONLY_LIBRARY
1521 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1523 encoder->protected_->num_apodizations = 0;
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;
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;
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)
1570 specification = s+1;
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;
1583 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
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)
1590 encoder->protected_->max_lpc_order = value;
1594 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
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)
1601 encoder->protected_->qlp_coeff_precision = value;
1605 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
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)
1612 encoder->protected_->do_qlp_coeff_prec_search = value;
1616 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
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)
1624 /*@@@ deprecated: */
1625 encoder->protected_->do_escape_coding = value;
1632 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
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)
1639 encoder->protected_->do_exhaustive_model_search = value;
1643 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
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)
1650 encoder->protected_->min_residual_partition_order = value;
1654 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
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)
1661 encoder->protected_->max_residual_partition_order = value;
1665 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
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)
1673 /*@@@ deprecated: */
1674 encoder->protected_->rice_parameter_search_dist = value;
1681 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
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)
1688 encoder->protected_->total_samples_estimate = value;
1692 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
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)
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;
1710 FLAC__StreamMetadata **m;
1711 if(0 == (m = safe_malloc_mul_2op_(sizeof(m[0]), /*times*/num_blocks)))
1713 memcpy(m, metadata, sizeof(m[0]) * num_blocks);
1714 encoder->protected_->metadata = m;
1715 encoder->protected_->num_metadata_blocks = num_blocks;
1718 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1725 * These three functions are not static, but not publically exposed in
1726 * include/FLAC/ either. They are used by the test suite.
1728 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
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)
1735 encoder->private_->disable_constant_subframes = value;
1739 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
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)
1746 encoder->private_->disable_fixed_subframes = value;
1750 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
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)
1757 encoder->private_->disable_verbatim_subframes = value;
1761 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1763 FLAC__ASSERT(0 != encoder);
1764 FLAC__ASSERT(0 != encoder->private_);
1765 FLAC__ASSERT(0 != encoder->protected_);
1766 return encoder->protected_->state;
1769 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
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);
1777 return FLAC__STREAM_DECODER_UNINITIALIZED;
1780 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
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];
1788 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
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)
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;
1801 *channel = encoder->private_->verify.error_stats.channel;
1803 *sample = encoder->private_->verify.error_stats.sample;
1805 *expected = encoder->private_->verify.error_stats.expected;
1807 *got = encoder->private_->verify.error_stats.got;
1810 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1812 FLAC__ASSERT(0 != encoder);
1813 FLAC__ASSERT(0 != encoder->private_);
1814 FLAC__ASSERT(0 != encoder->protected_);
1815 return encoder->protected_->verify;
1818 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1820 FLAC__ASSERT(0 != encoder);
1821 FLAC__ASSERT(0 != encoder->private_);
1822 FLAC__ASSERT(0 != encoder->protected_);
1823 return encoder->protected_->streamable_subset;
1826 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_md5(const FLAC__StreamEncoder *encoder)
1828 FLAC__ASSERT(0 != encoder);
1829 FLAC__ASSERT(0 != encoder->private_);
1830 FLAC__ASSERT(0 != encoder->protected_);
1831 return encoder->protected_->do_md5;
1834 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1836 FLAC__ASSERT(0 != encoder);
1837 FLAC__ASSERT(0 != encoder->private_);
1838 FLAC__ASSERT(0 != encoder->protected_);
1839 return encoder->protected_->channels;
1842 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1844 FLAC__ASSERT(0 != encoder);
1845 FLAC__ASSERT(0 != encoder->private_);
1846 FLAC__ASSERT(0 != encoder->protected_);
1847 return encoder->protected_->bits_per_sample;
1850 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1852 FLAC__ASSERT(0 != encoder);
1853 FLAC__ASSERT(0 != encoder->private_);
1854 FLAC__ASSERT(0 != encoder->protected_);
1855 return encoder->protected_->sample_rate;
1858 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1860 FLAC__ASSERT(0 != encoder);
1861 FLAC__ASSERT(0 != encoder->private_);
1862 FLAC__ASSERT(0 != encoder->protected_);
1863 return encoder->protected_->blocksize;
1866 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
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;
1874 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
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;
1882 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1884 FLAC__ASSERT(0 != encoder);
1885 FLAC__ASSERT(0 != encoder->private_);
1886 FLAC__ASSERT(0 != encoder->protected_);
1887 return encoder->protected_->max_lpc_order;
1890 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1892 FLAC__ASSERT(0 != encoder);
1893 FLAC__ASSERT(0 != encoder->private_);
1894 FLAC__ASSERT(0 != encoder->protected_);
1895 return encoder->protected_->qlp_coeff_precision;
1898 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
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;
1906 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1908 FLAC__ASSERT(0 != encoder);
1909 FLAC__ASSERT(0 != encoder->private_);
1910 FLAC__ASSERT(0 != encoder->protected_);
1911 return encoder->protected_->do_escape_coding;
1914 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
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;
1922 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
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;
1930 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
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;
1938 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
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;
1946 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1948 FLAC__ASSERT(0 != encoder);
1949 FLAC__ASSERT(0 != encoder->private_);
1950 FLAC__ASSERT(0 != encoder->protected_);
1951 return encoder->protected_->total_samples_estimate;
1954 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1956 unsigned i, j = 0, channel;
1957 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
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);
1965 const unsigned n = flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j);
1967 if(encoder->protected_->verify)
1968 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, n);
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);
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' ! */
1984 encoder->private_->current_sample_number += n;
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))
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];
1999 encoder->private_->current_sample_number = 1;
2001 } while(j < samples);
2006 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
2008 unsigned i, j, k, channel;
2009 FLAC__int32 x, mid, side;
2010 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
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);
2019 * we have several flavors of the same basic loop, optimized for
2020 * different conditions:
2022 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2024 * stereo coding: unroll channel loop
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));
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++];
2034 encoder->private_->integer_signal[1][i] = 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;
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() */
2044 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/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;
2055 } while(j < samples);
2059 * independent channel coding: buffer each channel in inner loop
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));
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++];
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() */
2073 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/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;
2082 } while(j < samples);
2088 /***********************************************************************
2090 * Private class methods
2092 ***********************************************************************/
2094 void set_defaults_(FLAC__StreamEncoder *encoder)
2096 FLAC__ASSERT(0 != encoder);
2098 #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2099 encoder->protected_->verify = true;
2101 encoder->protected_->verify = false;
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;
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;
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;
2133 encoder->private_->is_ogg = false;
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;
2144 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2147 FLAC__stream_encoder_set_compression_level(encoder, 5);
2150 void free_(FLAC__StreamEncoder *encoder)
2152 unsigned i, channel;
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;
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;
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;
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;
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;
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;
2191 if(0 != encoder->private_->windowed_signal_unaligned) {
2192 free(encoder->private_->windowed_signal_unaligned);
2193 encoder->private_->windowed_signal_unaligned = 0;
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;
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;
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;
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;
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;
2228 FLAC__bitwriter_free(encoder->private_->frame);
2231 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
2234 unsigned i, channel;
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);
2240 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
2241 if(new_blocksize <= encoder->private_->input_capacity)
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.
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]);
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]);
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);
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]);
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]);
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);
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);
2305 case FLAC__APODIZATION_BARTLETT_HANN:
2306 FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
2308 case FLAC__APODIZATION_BLACKMAN:
2309 FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
2311 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2312 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
2314 case FLAC__APODIZATION_CONNES:
2315 FLAC__window_connes(encoder->private_->window[i], new_blocksize);
2317 case FLAC__APODIZATION_FLATTOP:
2318 FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
2320 case FLAC__APODIZATION_GAUSS:
2321 FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2323 case FLAC__APODIZATION_HAMMING:
2324 FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
2326 case FLAC__APODIZATION_HANN:
2327 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2329 case FLAC__APODIZATION_KAISER_BESSEL:
2330 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
2332 case FLAC__APODIZATION_NUTTALL:
2333 FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
2335 case FLAC__APODIZATION_RECTANGLE:
2336 FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
2338 case FLAC__APODIZATION_TRIANGLE:
2339 FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
2341 case FLAC__APODIZATION_TUKEY:
2342 FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
2344 case FLAC__APODIZATION_WELCH:
2345 FLAC__window_welch(encoder->private_->window[i], new_blocksize);
2349 /* double protection */
2350 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2358 encoder->private_->input_capacity = new_blocksize;
2360 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2365 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
2367 const FLAC__byte *buffer;
2370 FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
2372 if(!FLAC__bitwriter_get_buffer(encoder->private_->frame, &buffer, &bytes)) {
2373 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
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;
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;
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;
2401 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2402 FLAC__bitwriter_clear(encoder->private_->frame);
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);
2412 FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block)
2414 FLAC__StreamEncoderWriteStatus status;
2415 FLAC__uint64 output_position = 0;
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;
2424 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
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;
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
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;
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) {
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.
2463 encoder->private_->first_seekpoint_to_check++;
2469 if(encoder->private_->is_ogg) {
2470 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2471 &encoder->protected_->ogg_encoder_aspect,
2475 encoder->private_->current_frame_number,
2477 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2479 encoder->private_->client_data
2484 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
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.
2493 encoder->private_->frames_written = flac_max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2496 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
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)
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;
2512 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
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.
2520 * Write MD5 signature
2523 const unsigned md5_offset =
2524 FLAC__STREAM_METADATA_HEADER_LENGTH +
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
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;
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;
2548 * Write total samples
2551 const unsigned total_samples_byte_offset =
2552 FLAC__STREAM_METADATA_HEADER_LENGTH +
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
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;
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;
2581 * Write min/max framesize
2584 const unsigned min_framesize_offset =
2585 FLAC__STREAM_METADATA_HEADER_LENGTH +
2587 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2588 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
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;
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;
2611 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2614 FLAC__format_seektable_sort(encoder->private_->seek_table);
2616 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
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;
2624 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
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;
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)
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
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;
2676 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2677 FLAC__ASSERT(0 != encoder->private_->seek_callback);
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.
2682 if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
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.
2691 ** Write STREAMINFO stats
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 */
2700 * Write MD5 signature
2703 const unsigned md5_offset =
2704 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2705 FLAC__STREAM_METADATA_HEADER_LENGTH +
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
2717 if(md5_offset + 16 > (unsigned)page.body_len) {
2718 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2719 simple_ogg_page__clear(&page);
2722 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2726 * Write total samples
2729 const unsigned total_samples_byte_offset =
2730 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2731 FLAC__STREAM_METADATA_HEADER_LENGTH +
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
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);
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);
2758 * Write min/max framesize
2761 const unsigned min_framesize_offset =
2762 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2763 FLAC__STREAM_METADATA_HEADER_LENGTH +
2765 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2766 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
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);
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);
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 */
2786 simple_ogg_page__clear(&page);
2791 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2795 FLAC__format_seektable_sort(encoder->private_->seek_table);
2797 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
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 */
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);
2811 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
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;
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 */
2842 simple_ogg_page__clear(&page);
2847 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
2850 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2853 * Accumulate raw signal to the MD5 signature
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;
2861 * Process the frame header and subframes into the frame bitbuffer
2863 if(!process_subframes_(encoder, is_fractional_block)) {
2864 /* the above function sets the state for us in case of an error */
2869 * Zero-pad the frame to a byte_boundary
2871 if(!FLAC__bitwriter_zero_pad_to_byte_boundary(encoder->private_->frame)) {
2872 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2877 * CRC-16 the whole thing
2879 FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
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)
2884 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
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 */
2897 * Get ready for the next frame
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;
2906 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
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;
2913 * Calculate the min,max Rice partition orders
2915 if(is_fractional_block) {
2916 max_partition_order = 0;
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);
2922 min_partition_order = flac_min(min_partition_order, max_partition_order);
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;
2936 * Figure out what channel assignments to try
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;
2945 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
2946 do_mid_side = !do_independent;
2950 do_independent = true;
2955 do_independent = true;
2956 do_mid_side = false;
2959 FLAC__ASSERT(do_independent || do_mid_side);
2962 * Check for wasted bits; set effective bps for each subframe
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;
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);
2981 * First do a normal encoding pass of each independent channel
2983 if(do_independent) {
2984 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2988 min_partition_order,
2989 max_partition_order,
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
3005 * Now do mid and side channels if requested
3008 FLAC__ASSERT(encoder->protected_->channels == 2);
3010 for(channel = 0; channel < 2; channel++) {
3014 min_partition_order,
3015 max_partition_order,
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
3031 * Compose the frame bitbuffer
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;
3038 FLAC__ASSERT(encoder->protected_->channels == 2);
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);
3044 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
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);
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];
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;
3070 frame_header.channel_assignment = channel_assignment;
3072 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3073 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
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]];
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]];
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]];
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]];
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];
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];
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];
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];
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))
3122 if(!add_subframe_(encoder, frame_header.blocksize, right_bps, right_subframe, encoder->private_->frame))
3126 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3127 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
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 */
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;
3145 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
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,
3164 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3165 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3167 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
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;
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;
3183 FLAC__ASSERT(frame_header->blocksize > 0);
3185 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3187 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3188 _best_bits = UINT_MAX;
3190 _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
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 */
3197 !encoder->private_->disable_constant_subframes &&
3198 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3199 fixed_residual_bits_per_sample[1] == 0.0
3201 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3204 /* the above means it's possible all samples are the same value; now double-check it: */
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;
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;
3222 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3224 if(encoder->protected_->do_exhaustive_model_search) {
3225 min_fixed_order = 0;
3226 max_fixed_order = FLAC__MAX_FIXED_ORDER;
3229 min_fixed_order = max_fixed_order = guess_fixed_order;
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 */
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 */
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);
3248 rice_parameter = rice_parameter_limit - 1;
3251 evaluate_fixed_subframe_(
3254 residual[!_best_subframe],
3255 encoder->private_->abs_residual_partition_sums,
3256 encoder->private_->raw_bits_per_partition,
3257 frame_header->blocksize,
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]
3269 if(_candidate_bits < _best_bits) {
3270 _best_subframe = !_best_subframe;
3271 _best_bits = _candidate_bits;
3276 #ifndef FLAC__INTEGER_ONLY_LIBRARY
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;
3282 max_lpc_order = encoder->protected_->max_lpc_order;
3283 if(max_lpc_order > 0) {
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) {
3295 const unsigned guess_lpc_order =
3296 FLAC__lpc_compute_best_order(
3299 frame_header->blocksize,
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
3306 min_lpc_order = max_lpc_order = guess_lpc_order;
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);
3320 rice_parameter = rice_parameter_limit - 1;
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);
3330 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3333 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3335 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3337 evaluate_lpc_subframe_(
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,
3347 qlp_coeff_precision,
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]
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;
3369 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
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]);
3379 *best_subframe = _best_subframe;
3380 *best_bits = _best_bits;
3385 FLAC__bool add_subframe_(
3386 FLAC__StreamEncoder *encoder,
3388 unsigned subframe_bps,
3389 const FLAC__Subframe *subframe,
3390 FLAC__BitWriter *frame
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;
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;
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;
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;
3425 #define SPOTCHECK_ESTIMATE 0
3426 #if SPOTCHECK_ESTIMATE
3427 static void spotcheck_subframe_estimate_(
3428 FLAC__StreamEncoder *encoder,
3430 unsigned subframe_bps,
3431 const FLAC__Subframe *subframe,
3436 FLAC__BitWriter *frame = FLAC__bitwriter_new();
3438 fprintf(stderr, "EST: can't allocate frame\n");
3441 if(!FLAC__bitwriter_init(frame)) {
3442 fprintf(stderr, "EST: can't init frame\n");
3445 ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame);
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);
3452 FLAC__bitwriter_delete(frame);
3456 unsigned evaluate_constant_subframe_(
3457 FLAC__StreamEncoder *encoder,
3458 const FLAC__int32 signal,
3460 unsigned subframe_bps,
3461 FLAC__Subframe *subframe
3465 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3466 subframe->data.constant.value = signal;
3468 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + subframe_bps;
3470 #if SPOTCHECK_ESTIMATE
3471 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3473 (void)encoder, (void)blocksize;
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[],
3486 unsigned subframe_bps,
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
3498 unsigned i, residual_bits, estimate;
3499 const unsigned residual_samples = blocksize - order;
3501 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3503 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
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;
3510 find_best_partition_order_(
3513 abs_residual_partition_sums,
3514 raw_bits_per_partition,
3518 rice_parameter_limit,
3519 min_partition_order,
3520 max_partition_order,
3523 rice_parameter_search_dist,
3524 &subframe->data.fixed.entropy_coding_method
3527 subframe->data.fixed.order = order;
3528 for(i = 0; i < order; i++)
3529 subframe->data.fixed.warmup[i] = signal[i];
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;
3533 #if SPOTCHECK_ESTIMATE
3534 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
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[],
3549 unsigned subframe_bps,
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
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;
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));
3574 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
3576 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
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);
3582 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3584 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3586 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
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;
3593 find_best_partition_order_(
3596 abs_residual_partition_sums,
3597 raw_bits_per_partition,
3601 rice_parameter_limit,
3602 min_partition_order,
3603 max_partition_order,
3606 rice_parameter_search_dist,
3607 &subframe->data.lpc.entropy_coding_method
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];
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;
3619 #if SPOTCHECK_ESTIMATE
3620 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3627 unsigned evaluate_verbatim_subframe_(
3628 FLAC__StreamEncoder *encoder,
3629 const FLAC__int32 signal[],
3631 unsigned subframe_bps,
3632 FLAC__Subframe *subframe
3637 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3639 subframe->data.verbatim.data = signal;
3641 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps);
3643 #if SPOTCHECK_ESTIMATE
3644 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
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,
3664 FLAC__bool do_escape_coding,
3665 unsigned rice_parameter_search_dist,
3666 FLAC__EntropyCodingMethod *best_ecm
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;
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);
3677 precompute_partition_info_sums_(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order, bps);
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);
3683 int partition_order;
3686 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3688 set_partitioned_rice_(
3689 #ifdef EXACT_RICE_BITS_CALCULATION
3692 abs_residual_partition_sums+sum,
3693 raw_bits_per_partition+sum,
3697 rice_parameter_limit,
3698 rice_parameter_search_dist,
3699 (unsigned)partition_order,
3701 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3706 FLAC__ASSERT(best_residual_bits != 0);
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;
3718 best_ecm->data.partitioned_rice.order = best_partition_order;
3722 * We are allowed to de-const the pointer based on our special
3723 * knowledge; it is const to the outside world.
3725 FLAC__EntropyCodingMethod_PartitionedRiceContents* prc = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_ecm->data.partitioned_rice.contents;
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)));
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.
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;
3746 return best_residual_bits;
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[],
3754 unsigned predictor_order,
3755 unsigned min_partition_order,
3756 unsigned max_partition_order
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,
3770 const unsigned default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
3771 unsigned partitions = 1u << max_partition_order;
3773 FLAC__ASSERT(default_partition_samples > predictor_order);
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);
3784 /* first do max_partition_order */
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;
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;
3800 else { /* have to pessimistically use 64 bits for accumulator */
3801 FLAC__uint64 abs_residual_partition_sum;
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;
3813 /* now merge partitions for lower orders */
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--) {
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;
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
3839 int partition_order;
3840 unsigned from_partition, to_partition = 0;
3841 const unsigned blocksize = residual_samples + predictor_order;
3843 /* first do max_partition_order */
3844 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
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;
3851 FLAC__ASSERT(default_partition_samples > predictor_order);
3853 for(partition = residual_sample = 0; partition < partitions; partition++) {
3854 partition_samples = default_partition_samples;
3856 partition_samples -= predictor_order;
3858 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3859 r = residual[residual_sample++];
3860 /* OPT: maybe faster: rmax |= r ^ (r>>31) */
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;
3869 to_partition = partitions;
3870 break; /*@@@ yuck, should remove the 'for' loop instead */
3873 /* now merge partitions for lower orders */
3874 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3877 const unsigned partitions = 1u << partition_order;
3878 for(i = 0; i < partitions; i++) {
3879 m = raw_bits_per_partition[from_partition];
3881 raw_bits_per_partition[to_partition] = flac_max(m, raw_bits_per_partition[from_partition]);
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
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 */
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;
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
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 */
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 */
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.
3929 FLAC__bool set_partitioned_rice_(
3930 #ifdef EXACT_RICE_BITS_CALCULATION
3931 const FLAC__int32 residual[],
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,
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;
3953 (void)rice_parameter_search_dist;
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);
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;
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;
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);
3976 max_rice_parameter = rice_parameter_limit - 1;
3980 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3982 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3984 rice_parameter = suggested_rice_parameter;
3986 #ifdef EXACT_RICE_BITS_CALCULATION
3987 partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, residual);
3989 partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, abs_residual_partition_sums[0]);
3991 if(partition_bits < best_partition_bits) {
3992 best_rice_parameter = rice_parameter;
3993 best_partition_bits = partition_bits;
3995 #ifdef ENABLE_RICE_PARAMETER_SEARCH
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;
4008 parameters[0] = best_rice_parameter;
4009 bits_ += best_partition_bits;
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)
4022 partition_samples -= predictor_order;
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
4033 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
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);
4039 rice_parameter = rice_parameter_limit - 1;
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;
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);
4054 max_rice_parameter = rice_parameter_limit - 1;
4058 min_rice_parameter = max_rice_parameter = rice_parameter;
4060 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4062 #ifdef EXACT_RICE_BITS_CALCULATION
4063 partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, residual+residual_sample);
4065 partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, abs_residual_partition_sums[partition]);
4067 if(partition_bits < best_partition_bits) {
4068 best_rice_parameter = rice_parameter;
4069 best_partition_bits = partition_bits;
4071 #ifdef ENABLE_RICE_PARAMETER_SEARCH
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;
4082 raw_bits[partition] = 0;
4084 parameters[partition] = best_rice_parameter;
4085 bits_ += best_partition_bits;
4086 residual_sample += partition_samples;
4094 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
4099 for(i = 0; i < samples && !(x&1); i++)
4106 for(shift = 0; !(x&1); shift++)
4111 for(i = 0; i < samples; i++)
4112 signal[i] >>= shift;
4118 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4122 for(channel = 0; channel < channels; channel++)
4123 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4125 fifo->tail += wide_samples;
4127 FLAC__ASSERT(fifo->tail <= fifo->size);
4130 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4133 unsigned sample, wide_sample;
4134 unsigned tail = fifo->tail;
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++];
4144 FLAC__ASSERT(fifo->tail <= fifo->size);
4147 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4149 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4150 const size_t encoded_bytes = encoder->private_->verify.output.bytes;
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;
4160 if(encoded_bytes == 0) {
4162 * If we get here, a FIFO underflow has occurred,
4163 * which means there is a bug somewhere.
4166 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
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;
4175 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4178 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4180 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4182 const unsigned channels = frame->header.channels;
4183 const unsigned blocksize = frame->header.blocksize;
4184 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
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;
4193 for(i = 0; i < blocksize; i++) {
4194 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4196 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4197 got = (FLAC__int32)buffer[channel][i];
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;
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;
4221 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4223 (void)decoder, (void)metadata, (void)client_data;
4226 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4228 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4229 (void)decoder, (void)status;
4230 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4233 FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4237 *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
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;
4244 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4247 FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4251 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4252 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4254 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4257 FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4263 offset = ftello(encoder->private_->file);
4266 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4269 *absolute_byte_offset = (FLAC__uint64)offset;
4270 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4274 #ifdef FLAC__VALGRIND_TESTING
4275 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4277 size_t ret = fwrite(ptr, size, nmemb, stream);
4283 #define local__fwrite fwrite
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)
4288 (void)client_data, (void)current_frame;
4290 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
4291 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
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
4299 encoder->private_->is_ogg? true :
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
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);
4311 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4314 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4318 * This will forcibly set stdout to binary mode (for OSes that require it)
4320 FILE *get_binary_stdout_(void)
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.
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);