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.
38 #include <stdlib.h> /* for malloc() */
39 #include <string.h> /* for memcpy() */
40 #include <sys/types.h> /* for off_t */
41 #include "FLAC/assert.h"
42 #include "FLAC/stream_decoder.h"
43 #include "share/alloc.h"
44 #include "share/compat.h"
45 #include "protected/stream_encoder.h"
46 #include "private/bitwriter.h"
47 #include "private/bitmath.h"
48 #include "private/crc.h"
49 #include "private/cpu.h"
50 #include "private/fixed.h"
51 #include "private/format.h"
52 #include "private/lpc.h"
53 #include "private/md5.h"
54 #include "private/memory.h"
55 #include "private/macros.h"
57 #include "private/ogg_helper.h"
58 #include "private/ogg_mapping.h"
60 #include "private/stream_encoder_framing.h"
61 #include "private/window.h"
64 /* Exact Rice codeword length calculation is off by default. The simple
65 * (and fast) estimation (of how many bits a residual value will be
66 * encoded with) in this encoder is very good, almost always yielding
67 * compression within 0.1% of exact calculation.
69 #undef EXACT_RICE_BITS_CALCULATION
70 /* Rice parameter searching is off by default. The simple (and fast)
71 * parameter estimation in this encoder is very good, almost always
72 * yielding compression within 0.1% of the optimal parameters.
74 #undef ENABLE_RICE_PARAMETER_SEARCH
78 FLAC__int32 *data[FLAC__MAX_CHANNELS];
79 unsigned size; /* of each data[] in samples */
84 const FLAC__byte *data;
91 ENCODER_IN_METADATA = 1,
95 static struct CompressionLevels {
96 FLAC__bool do_mid_side_stereo;
97 FLAC__bool loose_mid_side_stereo;
98 unsigned max_lpc_order;
99 unsigned qlp_coeff_precision;
100 FLAC__bool do_qlp_coeff_prec_search;
101 FLAC__bool do_escape_coding;
102 FLAC__bool do_exhaustive_model_search;
103 unsigned min_residual_partition_order;
104 unsigned max_residual_partition_order;
105 unsigned rice_parameter_search_dist;
106 } compression_levels_[] = {
107 { false, false, 0, 0, false, false, false, 0, 3, 0 },
108 { true , true , 0, 0, false, false, false, 0, 3, 0 },
109 { true , false, 0, 0, false, false, false, 0, 3, 0 },
110 { false, false, 6, 0, false, false, false, 0, 4, 0 },
111 { true , true , 8, 0, false, false, false, 0, 4, 0 },
112 { true , false, 8, 0, false, false, false, 0, 5, 0 },
113 { true , false, 8, 0, false, false, false, 0, 6, 0 },
114 { true , false, 8, 0, false, false, true , 0, 6, 0 },
115 { true , false, 12, 0, false, false, true , 0, 6, 0 }
119 /***********************************************************************
121 * Private class method prototypes
123 ***********************************************************************/
125 static void set_defaults_(FLAC__StreamEncoder *encoder);
126 static void free_(FLAC__StreamEncoder *encoder);
127 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize);
128 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block);
129 static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block);
130 static void update_metadata_(const FLAC__StreamEncoder *encoder);
132 static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
134 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block);
135 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);
137 static FLAC__bool process_subframe_(
138 FLAC__StreamEncoder *encoder,
139 unsigned min_partition_order,
140 unsigned max_partition_order,
141 const FLAC__FrameHeader *frame_header,
142 unsigned subframe_bps,
143 const FLAC__int32 integer_signal[],
144 FLAC__Subframe *subframe[2],
145 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
146 FLAC__int32 *residual[2],
147 unsigned *best_subframe,
151 static FLAC__bool add_subframe_(
152 FLAC__StreamEncoder *encoder,
154 unsigned subframe_bps,
155 const FLAC__Subframe *subframe,
156 FLAC__BitWriter *frame
159 static unsigned evaluate_constant_subframe_(
160 FLAC__StreamEncoder *encoder,
161 const FLAC__int32 signal,
163 unsigned subframe_bps,
164 FLAC__Subframe *subframe
167 static unsigned evaluate_fixed_subframe_(
168 FLAC__StreamEncoder *encoder,
169 const FLAC__int32 signal[],
170 FLAC__int32 residual[],
171 FLAC__uint64 abs_residual_partition_sums[],
172 unsigned raw_bits_per_partition[],
174 unsigned subframe_bps,
176 unsigned rice_parameter,
177 unsigned rice_parameter_limit,
178 unsigned min_partition_order,
179 unsigned max_partition_order,
180 FLAC__bool do_escape_coding,
181 unsigned rice_parameter_search_dist,
182 FLAC__Subframe *subframe,
183 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
186 #ifndef FLAC__INTEGER_ONLY_LIBRARY
187 static unsigned evaluate_lpc_subframe_(
188 FLAC__StreamEncoder *encoder,
189 const FLAC__int32 signal[],
190 FLAC__int32 residual[],
191 FLAC__uint64 abs_residual_partition_sums[],
192 unsigned raw_bits_per_partition[],
193 const FLAC__real lp_coeff[],
195 unsigned subframe_bps,
197 unsigned qlp_coeff_precision,
198 unsigned rice_parameter,
199 unsigned rice_parameter_limit,
200 unsigned min_partition_order,
201 unsigned max_partition_order,
202 FLAC__bool do_escape_coding,
203 unsigned rice_parameter_search_dist,
204 FLAC__Subframe *subframe,
205 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
209 static unsigned evaluate_verbatim_subframe_(
210 FLAC__StreamEncoder *encoder,
211 const FLAC__int32 signal[],
213 unsigned subframe_bps,
214 FLAC__Subframe *subframe
217 static unsigned find_best_partition_order_(
218 struct FLAC__StreamEncoderPrivate *private_,
219 const FLAC__int32 residual[],
220 FLAC__uint64 abs_residual_partition_sums[],
221 unsigned raw_bits_per_partition[],
222 unsigned residual_samples,
223 unsigned predictor_order,
224 unsigned rice_parameter,
225 unsigned rice_parameter_limit,
226 unsigned min_partition_order,
227 unsigned max_partition_order,
229 FLAC__bool do_escape_coding,
230 unsigned rice_parameter_search_dist,
231 FLAC__EntropyCodingMethod *best_ecm
234 static void precompute_partition_info_sums_(
235 const FLAC__int32 residual[],
236 FLAC__uint64 abs_residual_partition_sums[],
237 unsigned residual_samples,
238 unsigned predictor_order,
239 unsigned min_partition_order,
240 unsigned max_partition_order,
244 static void precompute_partition_info_escapes_(
245 const FLAC__int32 residual[],
246 unsigned raw_bits_per_partition[],
247 unsigned residual_samples,
248 unsigned predictor_order,
249 unsigned min_partition_order,
250 unsigned max_partition_order
253 static FLAC__bool set_partitioned_rice_(
254 #ifdef EXACT_RICE_BITS_CALCULATION
255 const FLAC__int32 residual[],
257 const FLAC__uint64 abs_residual_partition_sums[],
258 const unsigned raw_bits_per_partition[],
259 const unsigned residual_samples,
260 const unsigned predictor_order,
261 const unsigned suggested_rice_parameter,
262 const unsigned rice_parameter_limit,
263 const unsigned rice_parameter_search_dist,
264 const unsigned partition_order,
265 const FLAC__bool search_for_escapes,
266 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
270 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
272 /* verify-related routines: */
273 static void append_to_verify_fifo_(
274 verify_input_fifo *fifo,
275 const FLAC__int32 * const input[],
276 unsigned input_offset,
278 unsigned wide_samples
281 static void append_to_verify_fifo_interleaved_(
282 verify_input_fifo *fifo,
283 const FLAC__int32 input[],
284 unsigned input_offset,
286 unsigned wide_samples
289 static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
290 static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
291 static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
292 static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
294 static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
295 static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
296 static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
297 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);
298 static FILE *get_binary_stdout_(void);
301 /***********************************************************************
305 ***********************************************************************/
307 typedef struct FLAC__StreamEncoderPrivate {
308 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
309 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
310 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
311 #ifndef FLAC__INTEGER_ONLY_LIBRARY
312 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) the floating-point version of the input signal */
313 FLAC__real *real_signal_mid_side[2]; /* (@@@ currently unused) the floating-point version of the mid-side input signal (stereo only) */
314 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
315 FLAC__real *windowed_signal; /* the integer_signal[] * current window[] */
317 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
318 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
319 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
320 FLAC__int32 *residual_workspace_mid_side[2][2];
321 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
322 FLAC__Subframe subframe_workspace_mid_side[2][2];
323 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
324 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
325 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
326 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
327 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
328 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
329 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index (0 or 1) into 2nd dimension of the above workspaces */
330 unsigned best_subframe_mid_side[2];
331 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
332 unsigned best_subframe_bits_mid_side[2];
333 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
334 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
335 FLAC__BitWriter *frame; /* the current frame being worked on */
336 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
337 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
338 FLAC__ChannelAssignment last_channel_assignment;
339 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
340 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
341 unsigned current_sample_number;
342 unsigned current_frame_number;
343 FLAC__MD5Context md5context;
344 FLAC__CPUInfo cpuinfo;
345 #ifndef FLAC__INTEGER_ONLY_LIBRARY
346 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
348 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
350 #ifndef FLAC__INTEGER_ONLY_LIBRARY
351 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
352 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[]);
353 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[]);
354 void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
356 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
357 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
358 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
359 FLAC__bool disable_constant_subframes;
360 FLAC__bool disable_fixed_subframes;
361 FLAC__bool disable_verbatim_subframes;
365 FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
366 FLAC__StreamEncoderSeekCallback seek_callback;
367 FLAC__StreamEncoderTellCallback tell_callback;
368 FLAC__StreamEncoderWriteCallback write_callback;
369 FLAC__StreamEncoderMetadataCallback metadata_callback;
370 FLAC__StreamEncoderProgressCallback progress_callback;
372 unsigned first_seekpoint_to_check;
373 FILE *file; /* only used when encoding to a file */
374 FLAC__uint64 bytes_written;
375 FLAC__uint64 samples_written;
376 unsigned frames_written;
377 unsigned total_frames_estimate;
378 /* unaligned (original) pointers to allocated data */
379 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
380 FLAC__int32 *integer_signal_mid_side_unaligned[2];
381 #ifndef FLAC__INTEGER_ONLY_LIBRARY
382 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) */
383 FLAC__real *real_signal_mid_side_unaligned[2]; /* (@@@ currently unused) */
384 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
385 FLAC__real *windowed_signal_unaligned;
387 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
388 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
389 FLAC__uint64 *abs_residual_partition_sums_unaligned;
390 unsigned *raw_bits_per_partition_unaligned;
392 * These fields have been moved here from private function local
393 * declarations merely to save stack space during encoding.
395 #ifndef FLAC__INTEGER_ONLY_LIBRARY
396 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
398 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
400 * The data for the verify section
403 FLAC__StreamDecoder *decoder;
404 EncoderStateHint state_hint;
405 FLAC__bool needs_magic_hack;
406 verify_input_fifo input_fifo;
407 verify_output output;
409 FLAC__uint64 absolute_sample;
410 unsigned frame_number;
413 FLAC__int32 expected;
417 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
418 } FLAC__StreamEncoderPrivate;
420 /***********************************************************************
422 * Public static class data
424 ***********************************************************************/
426 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
427 "FLAC__STREAM_ENCODER_OK",
428 "FLAC__STREAM_ENCODER_UNINITIALIZED",
429 "FLAC__STREAM_ENCODER_OGG_ERROR",
430 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
431 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
432 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
433 "FLAC__STREAM_ENCODER_IO_ERROR",
434 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
435 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
438 FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
439 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
440 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
441 "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
442 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
443 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
444 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
445 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
446 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
447 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
448 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
449 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
450 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
451 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
452 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
455 FLAC_API const char * const FLAC__StreamEncoderReadStatusString[] = {
456 "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
457 "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
458 "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
459 "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
462 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
463 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
464 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
467 FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
468 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
469 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
470 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
473 FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
474 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
475 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
476 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
479 /* Number of samples that will be overread to watch for end of stream. By
480 * 'overread', we mean that the FLAC__stream_encoder_process*() calls will
481 * always try to read blocksize+1 samples before encoding a block, so that
482 * even if the stream has a total sample count that is an integral multiple
483 * of the blocksize, we will still notice when we are encoding the last
484 * block. This is needed, for example, to correctly set the end-of-stream
485 * marker in Ogg FLAC.
487 * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
488 * not really any reason to change it.
490 static const unsigned OVERREAD_ = 1;
492 /***********************************************************************
494 * Class constructor/destructor
497 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void)
499 FLAC__StreamEncoder *encoder;
502 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
504 encoder = calloc(1, sizeof(FLAC__StreamEncoder));
509 encoder->protected_ = calloc(1, sizeof(FLAC__StreamEncoderProtected));
510 if(encoder->protected_ == 0) {
515 encoder->private_ = calloc(1, sizeof(FLAC__StreamEncoderPrivate));
516 if(encoder->private_ == 0) {
517 free(encoder->protected_);
522 encoder->private_->frame = FLAC__bitwriter_new();
523 if(encoder->private_->frame == 0) {
524 free(encoder->private_);
525 free(encoder->protected_);
530 encoder->private_->file = 0;
532 set_defaults_(encoder);
534 encoder->private_->is_being_deleted = false;
536 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
537 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
538 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
540 for(i = 0; i < 2; i++) {
541 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
542 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
544 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
545 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
546 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
548 for(i = 0; i < 2; i++) {
549 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
550 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
553 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
554 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
555 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
557 for(i = 0; i < 2; i++) {
558 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
559 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
561 for(i = 0; i < 2; i++)
562 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
564 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
569 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
576 FLAC__ASSERT(0 != encoder->protected_);
577 FLAC__ASSERT(0 != encoder->private_);
578 FLAC__ASSERT(0 != encoder->private_->frame);
580 encoder->private_->is_being_deleted = true;
582 (void)FLAC__stream_encoder_finish(encoder);
584 if(0 != encoder->private_->verify.decoder)
585 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
587 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
588 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
589 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
591 for(i = 0; i < 2; i++) {
592 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
593 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
595 for(i = 0; i < 2; i++)
596 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
598 FLAC__bitwriter_delete(encoder->private_->frame);
599 free(encoder->private_);
600 free(encoder->protected_);
604 /***********************************************************************
606 * Public class methods
608 ***********************************************************************/
610 static FLAC__StreamEncoderInitStatus init_stream_internal_(
611 FLAC__StreamEncoder *encoder,
612 FLAC__StreamEncoderReadCallback read_callback,
613 FLAC__StreamEncoderWriteCallback write_callback,
614 FLAC__StreamEncoderSeekCallback seek_callback,
615 FLAC__StreamEncoderTellCallback tell_callback,
616 FLAC__StreamEncoderMetadataCallback metadata_callback,
622 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
624 FLAC__ASSERT(0 != encoder);
626 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
627 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
631 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
634 if(0 == write_callback || (seek_callback && 0 == tell_callback))
635 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
637 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
638 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
640 if(encoder->protected_->channels != 2) {
641 encoder->protected_->do_mid_side_stereo = false;
642 encoder->protected_->loose_mid_side_stereo = false;
644 else if(!encoder->protected_->do_mid_side_stereo)
645 encoder->protected_->loose_mid_side_stereo = false;
647 if(encoder->protected_->bits_per_sample >= 32)
648 encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */
650 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
651 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
653 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
654 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
656 if(encoder->protected_->blocksize == 0) {
657 if(encoder->protected_->max_lpc_order == 0)
658 encoder->protected_->blocksize = 1152;
660 encoder->protected_->blocksize = 4096;
663 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
664 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
666 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
667 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
669 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
670 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
672 if(encoder->protected_->qlp_coeff_precision == 0) {
673 if(encoder->protected_->bits_per_sample < 16) {
674 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
675 /* @@@ until then we'll make a guess */
676 encoder->protected_->qlp_coeff_precision = flac_max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
678 else if(encoder->protected_->bits_per_sample == 16) {
679 if(encoder->protected_->blocksize <= 192)
680 encoder->protected_->qlp_coeff_precision = 7;
681 else if(encoder->protected_->blocksize <= 384)
682 encoder->protected_->qlp_coeff_precision = 8;
683 else if(encoder->protected_->blocksize <= 576)
684 encoder->protected_->qlp_coeff_precision = 9;
685 else if(encoder->protected_->blocksize <= 1152)
686 encoder->protected_->qlp_coeff_precision = 10;
687 else if(encoder->protected_->blocksize <= 2304)
688 encoder->protected_->qlp_coeff_precision = 11;
689 else if(encoder->protected_->blocksize <= 4608)
690 encoder->protected_->qlp_coeff_precision = 12;
692 encoder->protected_->qlp_coeff_precision = 13;
695 if(encoder->protected_->blocksize <= 384)
696 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
697 else if(encoder->protected_->blocksize <= 1152)
698 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
700 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
702 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
704 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
705 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
707 if(encoder->protected_->streamable_subset) {
708 if(!FLAC__format_blocksize_is_subset(encoder->protected_->blocksize, encoder->protected_->sample_rate))
709 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
710 if(!FLAC__format_sample_rate_is_subset(encoder->protected_->sample_rate))
711 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
713 encoder->protected_->bits_per_sample != 8 &&
714 encoder->protected_->bits_per_sample != 12 &&
715 encoder->protected_->bits_per_sample != 16 &&
716 encoder->protected_->bits_per_sample != 20 &&
717 encoder->protected_->bits_per_sample != 24
719 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
720 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
721 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
723 encoder->protected_->sample_rate <= 48000 &&
725 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
726 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
729 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
733 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
734 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
735 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
736 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
739 /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
740 if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
742 for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
743 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
744 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
746 encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
747 encoder->protected_->metadata[0] = vc;
753 /* keep track of any SEEKTABLE block */
754 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
756 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
757 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
758 encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
759 break; /* take only the first one */
764 /* validate metadata */
765 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
766 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
767 metadata_has_seektable = false;
768 metadata_has_vorbis_comment = false;
769 metadata_picture_has_type1 = false;
770 metadata_picture_has_type2 = false;
771 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
772 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
773 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
774 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
775 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
776 if(metadata_has_seektable) /* only one is allowed */
777 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
778 metadata_has_seektable = true;
779 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
780 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
782 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
783 if(metadata_has_vorbis_comment) /* only one is allowed */
784 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
785 metadata_has_vorbis_comment = true;
787 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
788 if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
789 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
791 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
792 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
793 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
794 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
795 if(metadata_picture_has_type1) /* there should only be 1 per stream */
796 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
797 metadata_picture_has_type1 = true;
798 /* standard icon must be 32x32 pixel PNG */
800 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
802 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
803 m->data.picture.width != 32 ||
804 m->data.picture.height != 32
807 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
809 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
810 if(metadata_picture_has_type2) /* there should only be 1 per stream */
811 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
812 metadata_picture_has_type2 = true;
817 encoder->private_->input_capacity = 0;
818 for(i = 0; i < encoder->protected_->channels; i++) {
819 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
820 #ifndef FLAC__INTEGER_ONLY_LIBRARY
821 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
824 for(i = 0; i < 2; i++) {
825 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
826 #ifndef FLAC__INTEGER_ONLY_LIBRARY
827 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
830 #ifndef FLAC__INTEGER_ONLY_LIBRARY
831 for(i = 0; i < encoder->protected_->num_apodizations; i++)
832 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
833 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
835 for(i = 0; i < encoder->protected_->channels; i++) {
836 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
837 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
838 encoder->private_->best_subframe[i] = 0;
840 for(i = 0; i < 2; i++) {
841 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
842 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
843 encoder->private_->best_subframe_mid_side[i] = 0;
845 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
846 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
847 #ifndef FLAC__INTEGER_ONLY_LIBRARY
848 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
850 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
851 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply÷ by hand */
852 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
853 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
854 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
855 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
856 encoder->private_->loose_mid_side_stereo_frames = (unsigned)FLAC__fixedpoint_trunc((((FLAC__uint64)(encoder->protected_->sample_rate) * (FLAC__uint64)(26214)) << 16) / (encoder->protected_->blocksize<<16) + FLAC__FP_ONE_HALF);
858 if(encoder->private_->loose_mid_side_stereo_frames == 0)
859 encoder->private_->loose_mid_side_stereo_frames = 1;
860 encoder->private_->loose_mid_side_stereo_frame_count = 0;
861 encoder->private_->current_sample_number = 0;
862 encoder->private_->current_frame_number = 0;
864 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
865 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? */
866 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
869 * get the CPU info and set the function pointers
871 FLAC__cpu_info(&encoder->private_->cpuinfo);
872 /* first default to the non-asm routines */
873 #ifndef FLAC__INTEGER_ONLY_LIBRARY
874 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
876 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
877 #ifndef FLAC__INTEGER_ONLY_LIBRARY
878 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
879 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
880 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
882 /* now override with asm where appropriate */
883 #ifndef FLAC__INTEGER_ONLY_LIBRARY
884 # ifndef FLAC__NO_ASM
885 if(encoder->private_->cpuinfo.use_asm) {
886 # ifdef FLAC__CPU_IA32
887 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
888 # ifdef FLAC__HAS_NASM
889 if(encoder->private_->cpuinfo.data.ia32.sse) {
890 if(encoder->protected_->max_lpc_order < 4)
891 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
892 else if(encoder->protected_->max_lpc_order < 8)
893 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
894 else if(encoder->protected_->max_lpc_order < 12)
895 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
897 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
899 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
900 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
902 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
903 if(encoder->private_->cpuinfo.data.ia32.mmx) {
904 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
905 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
908 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
909 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
911 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
912 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
913 # endif /* FLAC__HAS_NASM */
914 # endif /* FLAC__CPU_IA32 */
916 # endif /* !FLAC__NO_ASM */
917 #endif /* !FLAC__INTEGER_ONLY_LIBRARY */
918 /* finally override based on wide-ness if necessary */
919 if(encoder->private_->use_wide_by_block) {
920 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
923 /* set state to OK; from here on, errors are fatal and we'll override the state then */
924 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
927 encoder->private_->is_ogg = is_ogg;
928 if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
929 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
930 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
934 encoder->private_->read_callback = read_callback;
935 encoder->private_->write_callback = write_callback;
936 encoder->private_->seek_callback = seek_callback;
937 encoder->private_->tell_callback = tell_callback;
938 encoder->private_->metadata_callback = metadata_callback;
939 encoder->private_->client_data = client_data;
941 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
942 /* the above function sets the state for us in case of an error */
943 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
946 if(!FLAC__bitwriter_init(encoder->private_->frame)) {
947 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
948 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
952 * Set up the verify stuff if necessary
954 if(encoder->protected_->verify) {
956 * First, set up the fifo which will hold the
957 * original signal to compare against
959 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
960 for(i = 0; i < encoder->protected_->channels; i++) {
961 if(0 == (encoder->private_->verify.input_fifo.data[i] = safe_malloc_mul_2op_p(sizeof(FLAC__int32), /*times*/encoder->private_->verify.input_fifo.size))) {
962 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
963 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
966 encoder->private_->verify.input_fifo.tail = 0;
969 * Now set up a stream decoder for verification
971 if(0 == encoder->private_->verify.decoder) {
972 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
973 if(0 == encoder->private_->verify.decoder) {
974 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
975 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
979 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) {
980 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
981 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
984 encoder->private_->verify.error_stats.absolute_sample = 0;
985 encoder->private_->verify.error_stats.frame_number = 0;
986 encoder->private_->verify.error_stats.channel = 0;
987 encoder->private_->verify.error_stats.sample = 0;
988 encoder->private_->verify.error_stats.expected = 0;
989 encoder->private_->verify.error_stats.got = 0;
992 * These must be done before we write any metadata, because that
993 * calls the write_callback, which uses these values.
995 encoder->private_->first_seekpoint_to_check = 0;
996 encoder->private_->samples_written = 0;
997 encoder->protected_->streaminfo_offset = 0;
998 encoder->protected_->seektable_offset = 0;
999 encoder->protected_->audio_offset = 0;
1002 * write the stream header
1004 if(encoder->protected_->verify)
1005 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
1006 if(!FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
1007 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1008 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1010 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1011 /* the above function sets the state for us in case of an error */
1012 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1016 * write the STREAMINFO metadata block
1018 if(encoder->protected_->verify)
1019 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
1020 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1021 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1022 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1023 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1024 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1025 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1026 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1027 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1028 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1029 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1030 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1031 memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
1032 if(encoder->protected_->do_md5)
1033 FLAC__MD5Init(&encoder->private_->md5context);
1034 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1035 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1036 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1038 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1039 /* the above function sets the state for us in case of an error */
1040 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1044 * Now that the STREAMINFO block is written, we can init this to an
1045 * absurdly-high value...
1047 encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
1048 /* ... and clear this to 0 */
1049 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
1052 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1053 * if not, we will write an empty one (FLAC__add_metadata_block()
1054 * automatically supplies the vendor string).
1056 * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1057 * the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1058 * true it will have already insured that the metadata list is properly
1061 if(!metadata_has_vorbis_comment) {
1062 FLAC__StreamMetadata vorbis_comment;
1063 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1064 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1065 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1066 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1067 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1068 vorbis_comment.data.vorbis_comment.num_comments = 0;
1069 vorbis_comment.data.vorbis_comment.comments = 0;
1070 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1071 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1072 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1074 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1075 /* the above function sets the state for us in case of an error */
1076 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1081 * write the user's metadata blocks
1083 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1084 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
1085 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1086 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1087 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1089 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
1090 /* the above function sets the state for us in case of an error */
1091 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1095 /* now that all the metadata is written, we save the stream offset */
1096 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 */
1097 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1098 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1101 if(encoder->protected_->verify)
1102 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1104 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1107 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1108 FLAC__StreamEncoder *encoder,
1109 FLAC__StreamEncoderWriteCallback write_callback,
1110 FLAC__StreamEncoderSeekCallback seek_callback,
1111 FLAC__StreamEncoderTellCallback tell_callback,
1112 FLAC__StreamEncoderMetadataCallback metadata_callback,
1116 return init_stream_internal_(
1118 /*read_callback=*/0,
1128 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1129 FLAC__StreamEncoder *encoder,
1130 FLAC__StreamEncoderReadCallback read_callback,
1131 FLAC__StreamEncoderWriteCallback write_callback,
1132 FLAC__StreamEncoderSeekCallback seek_callback,
1133 FLAC__StreamEncoderTellCallback tell_callback,
1134 FLAC__StreamEncoderMetadataCallback metadata_callback,
1138 return init_stream_internal_(
1150 static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1151 FLAC__StreamEncoder *encoder,
1153 FLAC__StreamEncoderProgressCallback progress_callback,
1158 FLAC__StreamEncoderInitStatus init_status;
1160 FLAC__ASSERT(0 != encoder);
1161 FLAC__ASSERT(0 != file);
1163 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1164 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1166 /* double protection */
1168 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1169 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1173 * To make sure that our file does not go unclosed after an error, we
1174 * must assign the FILE pointer before any further error can occur in
1178 file = get_binary_stdout_(); /* just to be safe */
1180 encoder->private_->file = file;
1182 encoder->private_->progress_callback = progress_callback;
1183 encoder->private_->bytes_written = 0;
1184 encoder->private_->samples_written = 0;
1185 encoder->private_->frames_written = 0;
1187 init_status = init_stream_internal_(
1189 encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0,
1190 file_write_callback_,
1191 encoder->private_->file == stdout? 0 : file_seek_callback_,
1192 encoder->private_->file == stdout? 0 : file_tell_callback_,
1193 /*metadata_callback=*/0,
1197 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1198 /* the above function sets the state for us in case of an error */
1203 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1205 FLAC__ASSERT(blocksize != 0);
1206 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1212 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1213 FLAC__StreamEncoder *encoder,
1215 FLAC__StreamEncoderProgressCallback progress_callback,
1219 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1222 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1223 FLAC__StreamEncoder *encoder,
1225 FLAC__StreamEncoderProgressCallback progress_callback,
1229 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1232 static FLAC__StreamEncoderInitStatus init_file_internal_(
1233 FLAC__StreamEncoder *encoder,
1234 const char *filename,
1235 FLAC__StreamEncoderProgressCallback progress_callback,
1242 FLAC__ASSERT(0 != encoder);
1245 * To make sure that our file does not go unclosed after an error, we
1246 * have to do the same entrance checks here that are later performed
1247 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1249 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1250 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1252 file = filename? fopen(filename, "w+b") : stdout;
1255 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1256 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1259 return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1262 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1263 FLAC__StreamEncoder *encoder,
1264 const char *filename,
1265 FLAC__StreamEncoderProgressCallback progress_callback,
1269 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1272 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1273 FLAC__StreamEncoder *encoder,
1274 const char *filename,
1275 FLAC__StreamEncoderProgressCallback progress_callback,
1279 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
1282 FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
1284 FLAC__bool error = false;
1286 FLAC__ASSERT(0 != encoder);
1287 FLAC__ASSERT(0 != encoder->private_);
1288 FLAC__ASSERT(0 != encoder->protected_);
1290 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
1293 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
1294 if(encoder->private_->current_sample_number != 0) {
1295 const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
1296 encoder->protected_->blocksize = encoder->private_->current_sample_number;
1297 if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
1302 if(encoder->protected_->do_md5)
1303 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
1305 if(!encoder->private_->is_being_deleted) {
1306 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
1307 if(encoder->private_->seek_callback) {
1309 if(encoder->private_->is_ogg)
1310 update_ogg_metadata_(encoder);
1313 update_metadata_(encoder);
1315 /* check if an error occurred while updating metadata */
1316 if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
1319 if(encoder->private_->metadata_callback)
1320 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1323 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
1325 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1330 if(0 != encoder->private_->file) {
1331 if(encoder->private_->file != stdout)
1332 fclose(encoder->private_->file);
1333 encoder->private_->file = 0;
1337 if(encoder->private_->is_ogg)
1338 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1342 set_defaults_(encoder);
1345 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1350 FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
1352 FLAC__ASSERT(0 != encoder);
1353 FLAC__ASSERT(0 != encoder->private_);
1354 FLAC__ASSERT(0 != encoder->protected_);
1355 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1358 /* can't check encoder->private_->is_ogg since that's not set until init time */
1359 FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1367 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
1369 FLAC__ASSERT(0 != encoder);
1370 FLAC__ASSERT(0 != encoder->private_);
1371 FLAC__ASSERT(0 != encoder->protected_);
1372 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1374 #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1375 encoder->protected_->verify = value;
1380 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
1382 FLAC__ASSERT(0 != encoder);
1383 FLAC__ASSERT(0 != encoder->private_);
1384 FLAC__ASSERT(0 != encoder->protected_);
1385 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1387 encoder->protected_->streamable_subset = value;
1391 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_md5(FLAC__StreamEncoder *encoder, FLAC__bool value)
1393 FLAC__ASSERT(0 != encoder);
1394 FLAC__ASSERT(0 != encoder->private_);
1395 FLAC__ASSERT(0 != encoder->protected_);
1396 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1398 encoder->protected_->do_md5 = value;
1402 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
1404 FLAC__ASSERT(0 != encoder);
1405 FLAC__ASSERT(0 != encoder->private_);
1406 FLAC__ASSERT(0 != encoder->protected_);
1407 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1409 encoder->protected_->channels = value;
1413 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
1415 FLAC__ASSERT(0 != encoder);
1416 FLAC__ASSERT(0 != encoder->private_);
1417 FLAC__ASSERT(0 != encoder->protected_);
1418 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1420 encoder->protected_->bits_per_sample = value;
1424 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
1426 FLAC__ASSERT(0 != encoder);
1427 FLAC__ASSERT(0 != encoder->private_);
1428 FLAC__ASSERT(0 != encoder->protected_);
1429 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1431 encoder->protected_->sample_rate = value;
1435 FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
1437 FLAC__bool ok = true;
1438 FLAC__ASSERT(0 != encoder);
1439 FLAC__ASSERT(0 != encoder->private_);
1440 FLAC__ASSERT(0 != encoder->protected_);
1441 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1443 if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1444 value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1445 ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
1446 ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
1447 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1450 ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
1451 /* but it's too hard to specify the string in a locale-specific way */
1453 encoder->protected_->num_apodizations = 1;
1454 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1455 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1458 ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1459 ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1460 ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1461 ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1462 ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1463 ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1464 ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1465 ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1469 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1471 FLAC__ASSERT(0 != encoder);
1472 FLAC__ASSERT(0 != encoder->private_);
1473 FLAC__ASSERT(0 != encoder->protected_);
1474 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1476 encoder->protected_->blocksize = value;
1480 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1482 FLAC__ASSERT(0 != encoder);
1483 FLAC__ASSERT(0 != encoder->private_);
1484 FLAC__ASSERT(0 != encoder->protected_);
1485 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1487 encoder->protected_->do_mid_side_stereo = value;
1491 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1493 FLAC__ASSERT(0 != encoder);
1494 FLAC__ASSERT(0 != encoder->private_);
1495 FLAC__ASSERT(0 != encoder->protected_);
1496 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1498 encoder->protected_->loose_mid_side_stereo = value;
1502 /*@@@@add to tests*/
1503 FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1505 FLAC__ASSERT(0 != encoder);
1506 FLAC__ASSERT(0 != encoder->private_);
1507 FLAC__ASSERT(0 != encoder->protected_);
1508 FLAC__ASSERT(0 != specification);
1509 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1511 #ifdef FLAC__INTEGER_ONLY_LIBRARY
1512 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1514 encoder->protected_->num_apodizations = 0;
1516 const char *s = strchr(specification, ';');
1517 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1518 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1519 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1520 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1521 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1522 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1523 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1524 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1525 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1526 else if(n==6 && 0 == strncmp("connes" , specification, n))
1527 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1528 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1529 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1530 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1531 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1532 if (stddev > 0.0 && stddev <= 0.5) {
1533 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1534 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1537 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1538 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1539 else if(n==4 && 0 == strncmp("hann" , specification, n))
1540 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1541 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1542 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1543 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1544 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1545 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1546 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1547 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1548 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1549 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1550 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1551 if (p >= 0.0 && p <= 1.0) {
1552 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1553 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1556 else if(n==5 && 0 == strncmp("welch" , specification, n))
1557 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1558 if (encoder->protected_->num_apodizations == 32)
1561 specification = s+1;
1565 if(encoder->protected_->num_apodizations == 0) {
1566 encoder->protected_->num_apodizations = 1;
1567 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1568 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1574 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1576 FLAC__ASSERT(0 != encoder);
1577 FLAC__ASSERT(0 != encoder->private_);
1578 FLAC__ASSERT(0 != encoder->protected_);
1579 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1581 encoder->protected_->max_lpc_order = value;
1585 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1587 FLAC__ASSERT(0 != encoder);
1588 FLAC__ASSERT(0 != encoder->private_);
1589 FLAC__ASSERT(0 != encoder->protected_);
1590 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1592 encoder->protected_->qlp_coeff_precision = value;
1596 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1598 FLAC__ASSERT(0 != encoder);
1599 FLAC__ASSERT(0 != encoder->private_);
1600 FLAC__ASSERT(0 != encoder->protected_);
1601 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1603 encoder->protected_->do_qlp_coeff_prec_search = value;
1607 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1609 FLAC__ASSERT(0 != encoder);
1610 FLAC__ASSERT(0 != encoder->private_);
1611 FLAC__ASSERT(0 != encoder->protected_);
1612 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1615 /*@@@ deprecated: */
1616 encoder->protected_->do_escape_coding = value;
1623 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1625 FLAC__ASSERT(0 != encoder);
1626 FLAC__ASSERT(0 != encoder->private_);
1627 FLAC__ASSERT(0 != encoder->protected_);
1628 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1630 encoder->protected_->do_exhaustive_model_search = value;
1634 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1636 FLAC__ASSERT(0 != encoder);
1637 FLAC__ASSERT(0 != encoder->private_);
1638 FLAC__ASSERT(0 != encoder->protected_);
1639 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1641 encoder->protected_->min_residual_partition_order = value;
1645 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1647 FLAC__ASSERT(0 != encoder);
1648 FLAC__ASSERT(0 != encoder->private_);
1649 FLAC__ASSERT(0 != encoder->protected_);
1650 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1652 encoder->protected_->max_residual_partition_order = value;
1656 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1658 FLAC__ASSERT(0 != encoder);
1659 FLAC__ASSERT(0 != encoder->private_);
1660 FLAC__ASSERT(0 != encoder->protected_);
1661 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1664 /*@@@ deprecated: */
1665 encoder->protected_->rice_parameter_search_dist = value;
1672 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1674 FLAC__ASSERT(0 != encoder);
1675 FLAC__ASSERT(0 != encoder->private_);
1676 FLAC__ASSERT(0 != encoder->protected_);
1677 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1679 encoder->protected_->total_samples_estimate = value;
1683 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1685 FLAC__ASSERT(0 != encoder);
1686 FLAC__ASSERT(0 != encoder->private_);
1687 FLAC__ASSERT(0 != encoder->protected_);
1688 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1694 /* realloc() does not do exactly what we want so... */
1695 if(encoder->protected_->metadata) {
1696 free(encoder->protected_->metadata);
1697 encoder->protected_->metadata = 0;
1698 encoder->protected_->num_metadata_blocks = 0;
1701 FLAC__StreamMetadata **m;
1702 if(0 == (m = safe_malloc_mul_2op_p(sizeof(m[0]), /*times*/num_blocks)))
1704 memcpy(m, metadata, sizeof(m[0]) * num_blocks);
1705 encoder->protected_->metadata = m;
1706 encoder->protected_->num_metadata_blocks = num_blocks;
1709 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1716 * These three functions are not static, but not publically exposed in
1717 * include/FLAC/ either. They are used by the test suite.
1719 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1721 FLAC__ASSERT(0 != encoder);
1722 FLAC__ASSERT(0 != encoder->private_);
1723 FLAC__ASSERT(0 != encoder->protected_);
1724 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1726 encoder->private_->disable_constant_subframes = value;
1730 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1732 FLAC__ASSERT(0 != encoder);
1733 FLAC__ASSERT(0 != encoder->private_);
1734 FLAC__ASSERT(0 != encoder->protected_);
1735 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1737 encoder->private_->disable_fixed_subframes = value;
1741 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1743 FLAC__ASSERT(0 != encoder);
1744 FLAC__ASSERT(0 != encoder->private_);
1745 FLAC__ASSERT(0 != encoder->protected_);
1746 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1748 encoder->private_->disable_verbatim_subframes = value;
1752 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1754 FLAC__ASSERT(0 != encoder);
1755 FLAC__ASSERT(0 != encoder->private_);
1756 FLAC__ASSERT(0 != encoder->protected_);
1757 return encoder->protected_->state;
1760 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1762 FLAC__ASSERT(0 != encoder);
1763 FLAC__ASSERT(0 != encoder->private_);
1764 FLAC__ASSERT(0 != encoder->protected_);
1765 if(encoder->protected_->verify)
1766 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1768 return FLAC__STREAM_DECODER_UNINITIALIZED;
1771 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1773 FLAC__ASSERT(0 != encoder);
1774 FLAC__ASSERT(0 != encoder->private_);
1775 FLAC__ASSERT(0 != encoder->protected_);
1776 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1777 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1779 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
1782 FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
1784 FLAC__ASSERT(0 != encoder);
1785 FLAC__ASSERT(0 != encoder->private_);
1786 FLAC__ASSERT(0 != encoder->protected_);
1787 if(0 != absolute_sample)
1788 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1789 if(0 != frame_number)
1790 *frame_number = encoder->private_->verify.error_stats.frame_number;
1792 *channel = encoder->private_->verify.error_stats.channel;
1794 *sample = encoder->private_->verify.error_stats.sample;
1796 *expected = encoder->private_->verify.error_stats.expected;
1798 *got = encoder->private_->verify.error_stats.got;
1801 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1803 FLAC__ASSERT(0 != encoder);
1804 FLAC__ASSERT(0 != encoder->private_);
1805 FLAC__ASSERT(0 != encoder->protected_);
1806 return encoder->protected_->verify;
1809 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1811 FLAC__ASSERT(0 != encoder);
1812 FLAC__ASSERT(0 != encoder->private_);
1813 FLAC__ASSERT(0 != encoder->protected_);
1814 return encoder->protected_->streamable_subset;
1817 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_md5(const FLAC__StreamEncoder *encoder)
1819 FLAC__ASSERT(0 != encoder);
1820 FLAC__ASSERT(0 != encoder->private_);
1821 FLAC__ASSERT(0 != encoder->protected_);
1822 return encoder->protected_->do_md5;
1825 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1827 FLAC__ASSERT(0 != encoder);
1828 FLAC__ASSERT(0 != encoder->private_);
1829 FLAC__ASSERT(0 != encoder->protected_);
1830 return encoder->protected_->channels;
1833 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1835 FLAC__ASSERT(0 != encoder);
1836 FLAC__ASSERT(0 != encoder->private_);
1837 FLAC__ASSERT(0 != encoder->protected_);
1838 return encoder->protected_->bits_per_sample;
1841 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1843 FLAC__ASSERT(0 != encoder);
1844 FLAC__ASSERT(0 != encoder->private_);
1845 FLAC__ASSERT(0 != encoder->protected_);
1846 return encoder->protected_->sample_rate;
1849 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1851 FLAC__ASSERT(0 != encoder);
1852 FLAC__ASSERT(0 != encoder->private_);
1853 FLAC__ASSERT(0 != encoder->protected_);
1854 return encoder->protected_->blocksize;
1857 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1859 FLAC__ASSERT(0 != encoder);
1860 FLAC__ASSERT(0 != encoder->private_);
1861 FLAC__ASSERT(0 != encoder->protected_);
1862 return encoder->protected_->do_mid_side_stereo;
1865 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1867 FLAC__ASSERT(0 != encoder);
1868 FLAC__ASSERT(0 != encoder->private_);
1869 FLAC__ASSERT(0 != encoder->protected_);
1870 return encoder->protected_->loose_mid_side_stereo;
1873 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1875 FLAC__ASSERT(0 != encoder);
1876 FLAC__ASSERT(0 != encoder->private_);
1877 FLAC__ASSERT(0 != encoder->protected_);
1878 return encoder->protected_->max_lpc_order;
1881 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1883 FLAC__ASSERT(0 != encoder);
1884 FLAC__ASSERT(0 != encoder->private_);
1885 FLAC__ASSERT(0 != encoder->protected_);
1886 return encoder->protected_->qlp_coeff_precision;
1889 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1891 FLAC__ASSERT(0 != encoder);
1892 FLAC__ASSERT(0 != encoder->private_);
1893 FLAC__ASSERT(0 != encoder->protected_);
1894 return encoder->protected_->do_qlp_coeff_prec_search;
1897 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1899 FLAC__ASSERT(0 != encoder);
1900 FLAC__ASSERT(0 != encoder->private_);
1901 FLAC__ASSERT(0 != encoder->protected_);
1902 return encoder->protected_->do_escape_coding;
1905 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1907 FLAC__ASSERT(0 != encoder);
1908 FLAC__ASSERT(0 != encoder->private_);
1909 FLAC__ASSERT(0 != encoder->protected_);
1910 return encoder->protected_->do_exhaustive_model_search;
1913 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1915 FLAC__ASSERT(0 != encoder);
1916 FLAC__ASSERT(0 != encoder->private_);
1917 FLAC__ASSERT(0 != encoder->protected_);
1918 return encoder->protected_->min_residual_partition_order;
1921 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1923 FLAC__ASSERT(0 != encoder);
1924 FLAC__ASSERT(0 != encoder->private_);
1925 FLAC__ASSERT(0 != encoder->protected_);
1926 return encoder->protected_->max_residual_partition_order;
1929 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1931 FLAC__ASSERT(0 != encoder);
1932 FLAC__ASSERT(0 != encoder->private_);
1933 FLAC__ASSERT(0 != encoder->protected_);
1934 return encoder->protected_->rice_parameter_search_dist;
1937 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1939 FLAC__ASSERT(0 != encoder);
1940 FLAC__ASSERT(0 != encoder->private_);
1941 FLAC__ASSERT(0 != encoder->protected_);
1942 return encoder->protected_->total_samples_estimate;
1945 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1947 unsigned i, j = 0, channel;
1948 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1950 FLAC__ASSERT(0 != encoder);
1951 FLAC__ASSERT(0 != encoder->private_);
1952 FLAC__ASSERT(0 != encoder->protected_);
1953 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1956 const unsigned n = flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j);
1958 if(encoder->protected_->verify)
1959 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, n);
1961 for(channel = 0; channel < channels; channel++)
1962 memcpy(&encoder->private_->integer_signal[channel][encoder->private_->current_sample_number], &buffer[channel][j], sizeof(buffer[channel][0]) * n);
1964 if(encoder->protected_->do_mid_side_stereo) {
1965 FLAC__ASSERT(channels == 2);
1966 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
1967 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
1968 encoder->private_->integer_signal_mid_side[1][i] = buffer[0][j] - buffer[1][j];
1969 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' ! */
1975 encoder->private_->current_sample_number += n;
1977 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
1978 if(encoder->private_->current_sample_number > blocksize) {
1979 FLAC__ASSERT(encoder->private_->current_sample_number == blocksize+OVERREAD_);
1980 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
1981 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
1983 /* move unprocessed overread samples to beginnings of arrays */
1984 for(channel = 0; channel < channels; channel++)
1985 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
1986 if(encoder->protected_->do_mid_side_stereo) {
1987 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
1988 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
1990 encoder->private_->current_sample_number = 1;
1992 } while(j < samples);
1997 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1999 unsigned i, j, k, channel;
2000 FLAC__int32 x, mid, side;
2001 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
2003 FLAC__ASSERT(0 != encoder);
2004 FLAC__ASSERT(0 != encoder->private_);
2005 FLAC__ASSERT(0 != encoder->protected_);
2006 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2010 * we have several flavors of the same basic loop, optimized for
2011 * different conditions:
2013 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2015 * stereo coding: unroll channel loop
2018 if(encoder->protected_->verify)
2019 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
2021 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2022 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2023 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
2025 encoder->private_->integer_signal[1][i] = x;
2028 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2029 encoder->private_->integer_signal_mid_side[1][i] = side;
2030 encoder->private_->integer_signal_mid_side[0][i] = mid;
2032 encoder->private_->current_sample_number = i;
2033 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2035 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2037 /* move unprocessed overread samples to beginnings of arrays */
2038 FLAC__ASSERT(i == blocksize+OVERREAD_);
2039 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2040 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][blocksize];
2041 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][blocksize];
2042 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
2043 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
2044 encoder->private_->current_sample_number = 1;
2046 } while(j < samples);
2050 * independent channel coding: buffer each channel in inner loop
2053 if(encoder->protected_->verify)
2054 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
2056 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2057 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2058 for(channel = 0; channel < channels; channel++)
2059 encoder->private_->integer_signal[channel][i] = buffer[k++];
2061 encoder->private_->current_sample_number = i;
2062 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2064 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2066 /* move unprocessed overread samples to beginnings of arrays */
2067 FLAC__ASSERT(i == blocksize+OVERREAD_);
2068 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2069 for(channel = 0; channel < channels; channel++)
2070 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
2071 encoder->private_->current_sample_number = 1;
2073 } while(j < samples);
2079 /***********************************************************************
2081 * Private class methods
2083 ***********************************************************************/
2085 void set_defaults_(FLAC__StreamEncoder *encoder)
2087 FLAC__ASSERT(0 != encoder);
2089 #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2090 encoder->protected_->verify = true;
2092 encoder->protected_->verify = false;
2094 encoder->protected_->streamable_subset = true;
2095 encoder->protected_->do_md5 = true;
2096 encoder->protected_->do_mid_side_stereo = false;
2097 encoder->protected_->loose_mid_side_stereo = false;
2098 encoder->protected_->channels = 2;
2099 encoder->protected_->bits_per_sample = 16;
2100 encoder->protected_->sample_rate = 44100;
2101 encoder->protected_->blocksize = 0;
2102 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2103 encoder->protected_->num_apodizations = 1;
2104 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2105 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
2107 encoder->protected_->max_lpc_order = 0;
2108 encoder->protected_->qlp_coeff_precision = 0;
2109 encoder->protected_->do_qlp_coeff_prec_search = false;
2110 encoder->protected_->do_exhaustive_model_search = false;
2111 encoder->protected_->do_escape_coding = false;
2112 encoder->protected_->min_residual_partition_order = 0;
2113 encoder->protected_->max_residual_partition_order = 0;
2114 encoder->protected_->rice_parameter_search_dist = 0;
2115 encoder->protected_->total_samples_estimate = 0;
2116 encoder->protected_->metadata = 0;
2117 encoder->protected_->num_metadata_blocks = 0;
2119 encoder->private_->seek_table = 0;
2120 encoder->private_->disable_constant_subframes = false;
2121 encoder->private_->disable_fixed_subframes = false;
2122 encoder->private_->disable_verbatim_subframes = false;
2124 encoder->private_->is_ogg = false;
2126 encoder->private_->read_callback = 0;
2127 encoder->private_->write_callback = 0;
2128 encoder->private_->seek_callback = 0;
2129 encoder->private_->tell_callback = 0;
2130 encoder->private_->metadata_callback = 0;
2131 encoder->private_->progress_callback = 0;
2132 encoder->private_->client_data = 0;
2135 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2138 FLAC__stream_encoder_set_compression_level(encoder, 5);
2141 void free_(FLAC__StreamEncoder *encoder)
2143 unsigned i, channel;
2145 FLAC__ASSERT(0 != encoder);
2146 if(encoder->protected_->metadata) {
2147 free(encoder->protected_->metadata);
2148 encoder->protected_->metadata = 0;
2149 encoder->protected_->num_metadata_blocks = 0;
2151 for(i = 0; i < encoder->protected_->channels; i++) {
2152 if(0 != encoder->private_->integer_signal_unaligned[i]) {
2153 free(encoder->private_->integer_signal_unaligned[i]);
2154 encoder->private_->integer_signal_unaligned[i] = 0;
2156 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2157 if(0 != encoder->private_->real_signal_unaligned[i]) {
2158 free(encoder->private_->real_signal_unaligned[i]);
2159 encoder->private_->real_signal_unaligned[i] = 0;
2163 for(i = 0; i < 2; i++) {
2164 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
2165 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2166 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2168 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2169 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
2170 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2171 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2175 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2176 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2177 if(0 != encoder->private_->window_unaligned[i]) {
2178 free(encoder->private_->window_unaligned[i]);
2179 encoder->private_->window_unaligned[i] = 0;
2182 if(0 != encoder->private_->windowed_signal_unaligned) {
2183 free(encoder->private_->windowed_signal_unaligned);
2184 encoder->private_->windowed_signal_unaligned = 0;
2187 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2188 for(i = 0; i < 2; i++) {
2189 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
2190 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2191 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2195 for(channel = 0; channel < 2; channel++) {
2196 for(i = 0; i < 2; i++) {
2197 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
2198 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2199 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2203 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
2204 free(encoder->private_->abs_residual_partition_sums_unaligned);
2205 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2207 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
2208 free(encoder->private_->raw_bits_per_partition_unaligned);
2209 encoder->private_->raw_bits_per_partition_unaligned = 0;
2211 if(encoder->protected_->verify) {
2212 for(i = 0; i < encoder->protected_->channels; i++) {
2213 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2214 free(encoder->private_->verify.input_fifo.data[i]);
2215 encoder->private_->verify.input_fifo.data[i] = 0;
2219 FLAC__bitwriter_free(encoder->private_->frame);
2222 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
2225 unsigned i, channel;
2227 FLAC__ASSERT(new_blocksize > 0);
2228 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2229 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
2231 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
2232 if(new_blocksize <= encoder->private_->input_capacity)
2237 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2238 * requires that the input arrays (in our case the integer signals)
2239 * have a buffer of up to 3 zeroes in front (at negative indices) for
2240 * alignment purposes; we use 4 in front to keep the data well-aligned.
2243 for(i = 0; ok && i < encoder->protected_->channels; i++) {
2244 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
2245 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2246 encoder->private_->integer_signal[i] += 4;
2247 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2248 #if 0 /* @@@ currently unused */
2249 if(encoder->protected_->max_lpc_order > 0)
2250 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
2254 for(i = 0; ok && i < 2; i++) {
2255 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]);
2256 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2257 encoder->private_->integer_signal_mid_side[i] += 4;
2258 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2259 #if 0 /* @@@ currently unused */
2260 if(encoder->protected_->max_lpc_order > 0)
2261 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]);
2265 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2266 if(ok && encoder->protected_->max_lpc_order > 0) {
2267 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2268 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2269 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
2272 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
2273 for(i = 0; ok && i < 2; i++) {
2274 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
2277 for(channel = 0; ok && channel < 2; channel++) {
2278 for(i = 0; ok && i < 2; i++) {
2279 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]);
2282 /* the *2 is an approximation to the series 1 + 1/2 + 1/4 + ... that sums tree occupies in a flat array */
2283 /*@@@ 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) */
2284 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
2285 if(encoder->protected_->do_escape_coding)
2286 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
2288 /* now adjust the windows if the blocksize has changed */
2289 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2290 if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
2291 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2292 switch(encoder->protected_->apodizations[i].type) {
2293 case FLAC__APODIZATION_BARTLETT:
2294 FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
2296 case FLAC__APODIZATION_BARTLETT_HANN:
2297 FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
2299 case FLAC__APODIZATION_BLACKMAN:
2300 FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
2302 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2303 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
2305 case FLAC__APODIZATION_CONNES:
2306 FLAC__window_connes(encoder->private_->window[i], new_blocksize);
2308 case FLAC__APODIZATION_FLATTOP:
2309 FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
2311 case FLAC__APODIZATION_GAUSS:
2312 FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2314 case FLAC__APODIZATION_HAMMING:
2315 FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
2317 case FLAC__APODIZATION_HANN:
2318 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2320 case FLAC__APODIZATION_KAISER_BESSEL:
2321 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
2323 case FLAC__APODIZATION_NUTTALL:
2324 FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
2326 case FLAC__APODIZATION_RECTANGLE:
2327 FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
2329 case FLAC__APODIZATION_TRIANGLE:
2330 FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
2332 case FLAC__APODIZATION_TUKEY:
2333 FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
2335 case FLAC__APODIZATION_WELCH:
2336 FLAC__window_welch(encoder->private_->window[i], new_blocksize);
2340 /* double protection */
2341 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2349 encoder->private_->input_capacity = new_blocksize;
2351 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2356 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
2358 const FLAC__byte *buffer;
2361 FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
2363 if(!FLAC__bitwriter_get_buffer(encoder->private_->frame, &buffer, &bytes)) {
2364 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2368 if(encoder->protected_->verify) {
2369 encoder->private_->verify.output.data = buffer;
2370 encoder->private_->verify.output.bytes = bytes;
2371 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2372 encoder->private_->verify.needs_magic_hack = true;
2375 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2376 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2377 FLAC__bitwriter_clear(encoder->private_->frame);
2378 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2379 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2385 if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2386 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2387 FLAC__bitwriter_clear(encoder->private_->frame);
2388 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2392 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2393 FLAC__bitwriter_clear(encoder->private_->frame);
2396 encoder->private_->streaminfo.data.stream_info.min_framesize = flac_min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2397 encoder->private_->streaminfo.data.stream_info.max_framesize = flac_max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
2403 FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block)
2405 FLAC__StreamEncoderWriteStatus status;
2406 FLAC__uint64 output_position = 0;
2408 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2409 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2410 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2411 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2415 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2418 FLAC__MetadataType type = (buffer[0] & 0x7f);
2419 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2420 encoder->protected_->streaminfo_offset = output_position;
2421 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2422 encoder->protected_->seektable_offset = output_position;
2426 * Mark the current seek point if hit (if audio_offset == 0 that
2427 * means we're still writing metadata and haven't hit the first
2430 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2431 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2432 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2433 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2434 FLAC__uint64 test_sample;
2436 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2437 test_sample = encoder->private_->seek_table->points[i].sample_number;
2438 if(test_sample > frame_last_sample) {
2441 else if(test_sample >= frame_first_sample) {
2442 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2443 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2444 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2445 encoder->private_->first_seekpoint_to_check++;
2446 /* DO NOT: "break;" and here's why:
2447 * The seektable template may contain more than one target
2448 * sample for any given frame; we will keep looping, generating
2449 * duplicate seekpoints for them, and we'll clean it up later,
2450 * just before writing the seektable back to the metadata.
2454 encoder->private_->first_seekpoint_to_check++;
2460 if(encoder->private_->is_ogg) {
2461 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2462 &encoder->protected_->ogg_encoder_aspect,
2466 encoder->private_->current_frame_number,
2468 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2470 encoder->private_->client_data
2475 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2477 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2478 encoder->private_->bytes_written += bytes;
2479 encoder->private_->samples_written += samples;
2480 /* we keep a high watermark on the number of frames written because
2481 * when the encoder goes back to write metadata, 'current_frame'
2482 * will drop back to 0.
2484 encoder->private_->frames_written = flac_max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2487 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2492 /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2493 void update_metadata_(const FLAC__StreamEncoder *encoder)
2495 FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2496 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2497 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2498 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2499 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2500 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2501 FLAC__StreamEncoderSeekStatus seek_status;
2503 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2505 /* All this is based on intimate knowledge of the stream header
2506 * layout, but a change to the header format that would break this
2507 * would also break all streams encoded in the previous format.
2511 * Write MD5 signature
2514 const unsigned md5_offset =
2515 FLAC__STREAM_METADATA_HEADER_LENGTH +
2517 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2518 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2519 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2520 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2521 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2522 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2523 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2524 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2527 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2528 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2529 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2532 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2533 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2539 * Write total samples
2542 const unsigned total_samples_byte_offset =
2543 FLAC__STREAM_METADATA_HEADER_LENGTH +
2545 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2546 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2547 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2548 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2549 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2550 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2551 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2555 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2556 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2557 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2558 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2559 b[4] = (FLAC__byte)(samples & 0xFF);
2560 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) {
2561 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2562 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2565 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2566 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2572 * Write min/max framesize
2575 const unsigned min_framesize_offset =
2576 FLAC__STREAM_METADATA_HEADER_LENGTH +
2578 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2579 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2582 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2583 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2584 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2585 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2586 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2587 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2588 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) {
2589 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2590 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2593 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2594 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2602 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2605 FLAC__format_seektable_sort(encoder->private_->seek_table);
2607 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2609 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) {
2610 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2611 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2615 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2618 xx = encoder->private_->seek_table->points[i].sample_number;
2619 b[7] = (FLAC__byte)xx; xx >>= 8;
2620 b[6] = (FLAC__byte)xx; xx >>= 8;
2621 b[5] = (FLAC__byte)xx; xx >>= 8;
2622 b[4] = (FLAC__byte)xx; xx >>= 8;
2623 b[3] = (FLAC__byte)xx; xx >>= 8;
2624 b[2] = (FLAC__byte)xx; xx >>= 8;
2625 b[1] = (FLAC__byte)xx; xx >>= 8;
2626 b[0] = (FLAC__byte)xx; xx >>= 8;
2627 xx = encoder->private_->seek_table->points[i].stream_offset;
2628 b[15] = (FLAC__byte)xx; xx >>= 8;
2629 b[14] = (FLAC__byte)xx; xx >>= 8;
2630 b[13] = (FLAC__byte)xx; xx >>= 8;
2631 b[12] = (FLAC__byte)xx; xx >>= 8;
2632 b[11] = (FLAC__byte)xx; xx >>= 8;
2633 b[10] = (FLAC__byte)xx; xx >>= 8;
2634 b[9] = (FLAC__byte)xx; xx >>= 8;
2635 b[8] = (FLAC__byte)xx; xx >>= 8;
2636 x = encoder->private_->seek_table->points[i].frame_samples;
2637 b[17] = (FLAC__byte)x; x >>= 8;
2638 b[16] = (FLAC__byte)x; x >>= 8;
2639 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2640 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2648 /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2649 void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2651 /* the # of bytes in the 1st packet that precede the STREAMINFO */
2652 static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
2653 FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
2654 FLAC__OGG_MAPPING_MAGIC_LENGTH +
2655 FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
2656 FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
2657 FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
2658 FLAC__STREAM_SYNC_LENGTH
2660 FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2661 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2662 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2663 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2664 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2667 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2668 FLAC__ASSERT(0 != encoder->private_->seek_callback);
2670 /* Pre-check that client supports seeking, since we don't want the
2671 * ogg_helper code to ever have to deal with this condition.
2673 if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
2676 /* All this is based on intimate knowledge of the stream header
2677 * layout, but a change to the header format that would break this
2678 * would also break all streams encoded in the previous format.
2682 ** Write STREAMINFO stats
2684 simple_ogg_page__init(&page);
2685 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2686 simple_ogg_page__clear(&page);
2687 return; /* state already set */
2691 * Write MD5 signature
2694 const unsigned md5_offset =
2695 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2696 FLAC__STREAM_METADATA_HEADER_LENGTH +
2698 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2699 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2700 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2701 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2702 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2703 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2704 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2705 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2708 if(md5_offset + 16 > (unsigned)page.body_len) {
2709 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2710 simple_ogg_page__clear(&page);
2713 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2717 * Write total samples
2720 const unsigned total_samples_byte_offset =
2721 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2722 FLAC__STREAM_METADATA_HEADER_LENGTH +
2724 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2725 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2726 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2727 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2728 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2729 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2730 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2734 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2735 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2736 simple_ogg_page__clear(&page);
2739 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2740 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2741 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2742 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2743 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2744 b[4] = (FLAC__byte)(samples & 0xFF);
2745 memcpy(page.body + total_samples_byte_offset, b, 5);
2749 * Write min/max framesize
2752 const unsigned min_framesize_offset =
2753 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2754 FLAC__STREAM_METADATA_HEADER_LENGTH +
2756 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2757 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2760 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
2761 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2762 simple_ogg_page__clear(&page);
2765 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2766 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2767 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2768 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2769 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2770 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2771 memcpy(page.body + min_framesize_offset, b, 6);
2773 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2774 simple_ogg_page__clear(&page);
2775 return; /* state already set */
2777 simple_ogg_page__clear(&page);
2782 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2786 FLAC__format_seektable_sort(encoder->private_->seek_table);
2788 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2790 simple_ogg_page__init(&page);
2791 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2792 simple_ogg_page__clear(&page);
2793 return; /* state already set */
2796 if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) {
2797 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2798 simple_ogg_page__clear(&page);
2802 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
2805 xx = encoder->private_->seek_table->points[i].sample_number;
2806 b[7] = (FLAC__byte)xx; xx >>= 8;
2807 b[6] = (FLAC__byte)xx; xx >>= 8;
2808 b[5] = (FLAC__byte)xx; xx >>= 8;
2809 b[4] = (FLAC__byte)xx; xx >>= 8;
2810 b[3] = (FLAC__byte)xx; xx >>= 8;
2811 b[2] = (FLAC__byte)xx; xx >>= 8;
2812 b[1] = (FLAC__byte)xx; xx >>= 8;
2813 b[0] = (FLAC__byte)xx; xx >>= 8;
2814 xx = encoder->private_->seek_table->points[i].stream_offset;
2815 b[15] = (FLAC__byte)xx; xx >>= 8;
2816 b[14] = (FLAC__byte)xx; xx >>= 8;
2817 b[13] = (FLAC__byte)xx; xx >>= 8;
2818 b[12] = (FLAC__byte)xx; xx >>= 8;
2819 b[11] = (FLAC__byte)xx; xx >>= 8;
2820 b[10] = (FLAC__byte)xx; xx >>= 8;
2821 b[9] = (FLAC__byte)xx; xx >>= 8;
2822 b[8] = (FLAC__byte)xx; xx >>= 8;
2823 x = encoder->private_->seek_table->points[i].frame_samples;
2824 b[17] = (FLAC__byte)x; x >>= 8;
2825 b[16] = (FLAC__byte)x; x >>= 8;
2829 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2830 simple_ogg_page__clear(&page);
2831 return; /* state already set */
2833 simple_ogg_page__clear(&page);
2838 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
2841 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2844 * Accumulate raw signal to the MD5 signature
2846 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)) {
2847 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2852 * Process the frame header and subframes into the frame bitbuffer
2854 if(!process_subframes_(encoder, is_fractional_block)) {
2855 /* the above function sets the state for us in case of an error */
2860 * Zero-pad the frame to a byte_boundary
2862 if(!FLAC__bitwriter_zero_pad_to_byte_boundary(encoder->private_->frame)) {
2863 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2868 * CRC-16 the whole thing
2870 FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
2872 !FLAC__bitwriter_get_write_crc16(encoder->private_->frame, &crc) ||
2873 !FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, crc, FLAC__FRAME_FOOTER_CRC_LEN)
2875 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2882 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
2883 /* the above function sets the state for us in case of an error */
2888 * Get ready for the next frame
2890 encoder->private_->current_sample_number = 0;
2891 encoder->private_->current_frame_number++;
2892 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
2897 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
2899 FLAC__FrameHeader frame_header;
2900 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
2901 FLAC__bool do_independent, do_mid_side;
2904 * Calculate the min,max Rice partition orders
2906 if(is_fractional_block) {
2907 max_partition_order = 0;
2910 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
2911 max_partition_order = flac_min(max_partition_order, encoder->protected_->max_residual_partition_order);
2913 min_partition_order = flac_min(min_partition_order, max_partition_order);
2918 frame_header.blocksize = encoder->protected_->blocksize;
2919 frame_header.sample_rate = encoder->protected_->sample_rate;
2920 frame_header.channels = encoder->protected_->channels;
2921 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
2922 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
2923 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
2924 frame_header.number.frame_number = encoder->private_->current_frame_number;
2927 * Figure out what channel assignments to try
2929 if(encoder->protected_->do_mid_side_stereo) {
2930 if(encoder->protected_->loose_mid_side_stereo) {
2931 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
2932 do_independent = true;
2936 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
2937 do_mid_side = !do_independent;
2941 do_independent = true;
2946 do_independent = true;
2947 do_mid_side = false;
2950 FLAC__ASSERT(do_independent || do_mid_side);
2953 * Check for wasted bits; set effective bps for each subframe
2955 if(do_independent) {
2956 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2957 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
2958 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
2959 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
2963 FLAC__ASSERT(encoder->protected_->channels == 2);
2964 for(channel = 0; channel < 2; channel++) {
2965 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
2966 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
2967 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
2972 * First do a normal encoding pass of each independent channel
2974 if(do_independent) {
2975 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2979 min_partition_order,
2980 max_partition_order,
2982 encoder->private_->subframe_bps[channel],
2983 encoder->private_->integer_signal[channel],
2984 encoder->private_->subframe_workspace_ptr[channel],
2985 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
2986 encoder->private_->residual_workspace[channel],
2987 encoder->private_->best_subframe+channel,
2988 encoder->private_->best_subframe_bits+channel
2996 * Now do mid and side channels if requested
2999 FLAC__ASSERT(encoder->protected_->channels == 2);
3001 for(channel = 0; channel < 2; channel++) {
3005 min_partition_order,
3006 max_partition_order,
3008 encoder->private_->subframe_bps_mid_side[channel],
3009 encoder->private_->integer_signal_mid_side[channel],
3010 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3011 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3012 encoder->private_->residual_workspace_mid_side[channel],
3013 encoder->private_->best_subframe_mid_side+channel,
3014 encoder->private_->best_subframe_bits_mid_side+channel
3022 * Compose the frame bitbuffer
3025 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3026 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
3027 FLAC__ChannelAssignment channel_assignment;
3029 FLAC__ASSERT(encoder->protected_->channels == 2);
3031 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3032 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
3035 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3039 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT == 0);
3040 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE == 1);
3041 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE == 2);
3042 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_MID_SIDE == 3);
3043 FLAC__ASSERT(do_independent && do_mid_side);
3045 /* We have to figure out which channel assignent results in the smallest frame */
3046 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3047 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3048 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3049 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
3051 channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
3052 min_bits = bits[channel_assignment];
3053 for(ca = 1; ca <= 3; ca++) {
3054 if(bits[ca] < min_bits) {
3055 min_bits = bits[ca];
3056 channel_assignment = (FLAC__ChannelAssignment)ca;
3061 frame_header.channel_assignment = channel_assignment;
3063 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3064 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3068 switch(channel_assignment) {
3069 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3070 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3071 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
3073 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3074 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3075 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3077 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3078 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3079 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
3081 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3082 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3083 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3089 switch(channel_assignment) {
3090 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3091 left_bps = encoder->private_->subframe_bps [0];
3092 right_bps = encoder->private_->subframe_bps [1];
3094 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3095 left_bps = encoder->private_->subframe_bps [0];
3096 right_bps = encoder->private_->subframe_bps_mid_side[1];
3098 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3099 left_bps = encoder->private_->subframe_bps_mid_side[1];
3100 right_bps = encoder->private_->subframe_bps [1];
3102 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3103 left_bps = encoder->private_->subframe_bps_mid_side[0];
3104 right_bps = encoder->private_->subframe_bps_mid_side[1];
3110 /* note that encoder_add_subframe_ sets the state for us in case of an error */
3111 if(!add_subframe_(encoder, frame_header.blocksize, left_bps , left_subframe , encoder->private_->frame))
3113 if(!add_subframe_(encoder, frame_header.blocksize, right_bps, right_subframe, encoder->private_->frame))
3117 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
3118 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3122 for(channel = 0; channel < encoder->protected_->channels; channel++) {
3123 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)) {
3124 /* the above function sets the state for us in case of an error */
3130 if(encoder->protected_->loose_mid_side_stereo) {
3131 encoder->private_->loose_mid_side_stereo_frame_count++;
3132 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3133 encoder->private_->loose_mid_side_stereo_frame_count = 0;
3136 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
3141 FLAC__bool process_subframe_(
3142 FLAC__StreamEncoder *encoder,
3143 unsigned min_partition_order,
3144 unsigned max_partition_order,
3145 const FLAC__FrameHeader *frame_header,
3146 unsigned subframe_bps,
3147 const FLAC__int32 integer_signal[],
3148 FLAC__Subframe *subframe[2],
3149 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3150 FLAC__int32 *residual[2],
3151 unsigned *best_subframe,
3155 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3156 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3158 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3160 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3161 FLAC__double lpc_residual_bits_per_sample;
3162 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 */
3163 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
3164 unsigned min_lpc_order, max_lpc_order, lpc_order;
3165 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
3167 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
3168 unsigned rice_parameter;
3169 unsigned _candidate_bits, _best_bits;
3170 unsigned _best_subframe;
3171 /* only use RICE2 partitions if stream bps > 16 */
3172 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;
3174 FLAC__ASSERT(frame_header->blocksize > 0);
3176 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3178 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3179 _best_bits = UINT_MAX;
3181 _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3183 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3184 unsigned signal_is_constant = false;
3185 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);
3186 /* check for constant subframe */
3188 !encoder->private_->disable_constant_subframes &&
3189 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3190 fixed_residual_bits_per_sample[1] == 0.0
3192 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3195 /* the above means it's possible all samples are the same value; now double-check it: */
3197 signal_is_constant = true;
3198 for(i = 1; i < frame_header->blocksize; i++) {
3199 if(integer_signal[0] != integer_signal[i]) {
3200 signal_is_constant = false;
3205 if(signal_is_constant) {
3206 _candidate_bits = evaluate_constant_subframe_(encoder, integer_signal[0], frame_header->blocksize, subframe_bps, subframe[!_best_subframe]);
3207 if(_candidate_bits < _best_bits) {
3208 _best_subframe = !_best_subframe;
3209 _best_bits = _candidate_bits;
3213 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3215 if(encoder->protected_->do_exhaustive_model_search) {
3216 min_fixed_order = 0;
3217 max_fixed_order = FLAC__MAX_FIXED_ORDER;
3220 min_fixed_order = max_fixed_order = guess_fixed_order;
3222 if(max_fixed_order >= frame_header->blocksize)
3223 max_fixed_order = frame_header->blocksize - 1;
3224 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
3225 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3226 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
3227 continue; /* don't even try */
3228 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 */
3230 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3231 continue; /* don't even try */
3232 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 */
3234 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3235 if(rice_parameter >= rice_parameter_limit) {
3236 #ifdef DEBUG_VERBOSE
3237 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, rice_parameter_limit - 1);
3239 rice_parameter = rice_parameter_limit - 1;
3242 evaluate_fixed_subframe_(
3245 residual[!_best_subframe],
3246 encoder->private_->abs_residual_partition_sums,
3247 encoder->private_->raw_bits_per_partition,
3248 frame_header->blocksize,
3252 rice_parameter_limit,
3253 min_partition_order,
3254 max_partition_order,
3255 encoder->protected_->do_escape_coding,
3256 encoder->protected_->rice_parameter_search_dist,
3257 subframe[!_best_subframe],
3258 partitioned_rice_contents[!_best_subframe]
3260 if(_candidate_bits < _best_bits) {
3261 _best_subframe = !_best_subframe;
3262 _best_bits = _candidate_bits;
3267 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3269 if(encoder->protected_->max_lpc_order > 0) {
3270 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
3271 max_lpc_order = frame_header->blocksize-1;
3273 max_lpc_order = encoder->protected_->max_lpc_order;
3274 if(max_lpc_order > 0) {
3276 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
3277 FLAC__lpc_window_data(integer_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
3278 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3279 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3280 if(autoc[0] != 0.0) {
3281 FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3282 if(encoder->protected_->do_exhaustive_model_search) {
3286 const unsigned guess_lpc_order =
3287 FLAC__lpc_compute_best_order(
3290 frame_header->blocksize,
3292 encoder->protected_->do_qlp_coeff_prec_search?
3293 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3294 encoder->protected_->qlp_coeff_precision
3297 min_lpc_order = max_lpc_order = guess_lpc_order;
3299 if(max_lpc_order >= frame_header->blocksize)
3300 max_lpc_order = frame_header->blocksize - 1;
3301 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3302 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3303 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3304 continue; /* don't even try */
3305 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3306 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3307 if(rice_parameter >= rice_parameter_limit) {
3308 #ifdef DEBUG_VERBOSE
3309 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, rice_parameter_limit - 1);
3311 rice_parameter = rice_parameter_limit - 1;
3313 if(encoder->protected_->do_qlp_coeff_prec_search) {
3314 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
3315 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3316 if(subframe_bps <= 17) {
3317 max_qlp_coeff_precision = flac_min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
3318 max_qlp_coeff_precision = flac_max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3321 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3324 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3326 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3328 evaluate_lpc_subframe_(
3331 residual[!_best_subframe],
3332 encoder->private_->abs_residual_partition_sums,
3333 encoder->private_->raw_bits_per_partition,
3334 encoder->private_->lp_coeff[lpc_order-1],
3335 frame_header->blocksize,
3338 qlp_coeff_precision,
3340 rice_parameter_limit,
3341 min_partition_order,
3342 max_partition_order,
3343 encoder->protected_->do_escape_coding,
3344 encoder->protected_->rice_parameter_search_dist,
3345 subframe[!_best_subframe],
3346 partitioned_rice_contents[!_best_subframe]
3348 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3349 if(_candidate_bits < _best_bits) {
3350 _best_subframe = !_best_subframe;
3351 _best_bits = _candidate_bits;
3360 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
3364 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3365 if(_best_bits == UINT_MAX) {
3366 FLAC__ASSERT(_best_subframe == 0);
3367 _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3370 *best_subframe = _best_subframe;
3371 *best_bits = _best_bits;
3376 FLAC__bool add_subframe_(
3377 FLAC__StreamEncoder *encoder,
3379 unsigned subframe_bps,
3380 const FLAC__Subframe *subframe,
3381 FLAC__BitWriter *frame
3384 switch(subframe->type) {
3385 case FLAC__SUBFRAME_TYPE_CONSTANT:
3386 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
3387 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3391 case FLAC__SUBFRAME_TYPE_FIXED:
3392 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
3393 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3397 case FLAC__SUBFRAME_TYPE_LPC:
3398 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
3399 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3403 case FLAC__SUBFRAME_TYPE_VERBATIM:
3404 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), blocksize, subframe_bps, subframe->wasted_bits, frame)) {
3405 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
3416 #define SPOTCHECK_ESTIMATE 0
3417 #if SPOTCHECK_ESTIMATE
3418 static void spotcheck_subframe_estimate_(
3419 FLAC__StreamEncoder *encoder,
3421 unsigned subframe_bps,
3422 const FLAC__Subframe *subframe,
3427 FLAC__BitWriter *frame = FLAC__bitwriter_new();
3429 fprintf(stderr, "EST: can't allocate frame\n");
3432 if(!FLAC__bitwriter_init(frame)) {
3433 fprintf(stderr, "EST: can't init frame\n");
3436 ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame);
3439 const unsigned actual = FLAC__bitwriter_get_input_bits_unconsumed(frame);
3440 if(estimate != actual)
3441 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);
3443 FLAC__bitwriter_delete(frame);
3447 unsigned evaluate_constant_subframe_(
3448 FLAC__StreamEncoder *encoder,
3449 const FLAC__int32 signal,
3451 unsigned subframe_bps,
3452 FLAC__Subframe *subframe
3456 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3457 subframe->data.constant.value = signal;
3459 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + subframe_bps;
3461 #if SPOTCHECK_ESTIMATE
3462 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3464 (void)encoder, (void)blocksize;
3470 unsigned evaluate_fixed_subframe_(
3471 FLAC__StreamEncoder *encoder,
3472 const FLAC__int32 signal[],
3473 FLAC__int32 residual[],
3474 FLAC__uint64 abs_residual_partition_sums[],
3475 unsigned raw_bits_per_partition[],
3477 unsigned subframe_bps,
3479 unsigned rice_parameter,
3480 unsigned rice_parameter_limit,
3481 unsigned min_partition_order,
3482 unsigned max_partition_order,
3483 FLAC__bool do_escape_coding,
3484 unsigned rice_parameter_search_dist,
3485 FLAC__Subframe *subframe,
3486 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3489 unsigned i, residual_bits, estimate;
3490 const unsigned residual_samples = blocksize - order;
3492 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3494 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3496 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3497 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3498 subframe->data.fixed.residual = residual;
3501 find_best_partition_order_(
3504 abs_residual_partition_sums,
3505 raw_bits_per_partition,
3509 rice_parameter_limit,
3510 min_partition_order,
3511 max_partition_order,
3514 rice_parameter_search_dist,
3515 &subframe->data.fixed.entropy_coding_method
3518 subframe->data.fixed.order = order;
3519 for(i = 0; i < order; i++)
3520 subframe->data.fixed.warmup[i] = signal[i];
3522 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (order * subframe_bps) + residual_bits;
3524 #if SPOTCHECK_ESTIMATE
3525 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3531 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3532 unsigned evaluate_lpc_subframe_(
3533 FLAC__StreamEncoder *encoder,
3534 const FLAC__int32 signal[],
3535 FLAC__int32 residual[],
3536 FLAC__uint64 abs_residual_partition_sums[],
3537 unsigned raw_bits_per_partition[],
3538 const FLAC__real lp_coeff[],
3540 unsigned subframe_bps,
3542 unsigned qlp_coeff_precision,
3543 unsigned rice_parameter,
3544 unsigned rice_parameter_limit,
3545 unsigned min_partition_order,
3546 unsigned max_partition_order,
3547 FLAC__bool do_escape_coding,
3548 unsigned rice_parameter_search_dist,
3549 FLAC__Subframe *subframe,
3550 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3553 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
3554 unsigned i, residual_bits, estimate;
3555 int quantization, ret;
3556 const unsigned residual_samples = blocksize - order;
3558 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3559 if(subframe_bps <= 16) {
3560 FLAC__ASSERT(order > 0);
3561 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3562 qlp_coeff_precision = flac_min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3565 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
3567 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3569 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3570 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3571 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3573 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3575 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3577 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3579 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
3580 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
3581 subframe->data.lpc.residual = residual;
3584 find_best_partition_order_(
3587 abs_residual_partition_sums,
3588 raw_bits_per_partition,
3592 rice_parameter_limit,
3593 min_partition_order,
3594 max_partition_order,
3597 rice_parameter_search_dist,
3598 &subframe->data.lpc.entropy_coding_method
3601 subframe->data.lpc.order = order;
3602 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3603 subframe->data.lpc.quantization_level = quantization;
3604 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
3605 for(i = 0; i < order; i++)
3606 subframe->data.lpc.warmup[i] = signal[i];
3608 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;
3610 #if SPOTCHECK_ESTIMATE
3611 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3618 unsigned evaluate_verbatim_subframe_(
3619 FLAC__StreamEncoder *encoder,
3620 const FLAC__int32 signal[],
3622 unsigned subframe_bps,
3623 FLAC__Subframe *subframe
3628 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3630 subframe->data.verbatim.data = signal;
3632 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps);
3634 #if SPOTCHECK_ESTIMATE
3635 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3643 unsigned find_best_partition_order_(
3644 FLAC__StreamEncoderPrivate *private_,
3645 const FLAC__int32 residual[],
3646 FLAC__uint64 abs_residual_partition_sums[],
3647 unsigned raw_bits_per_partition[],
3648 unsigned residual_samples,
3649 unsigned predictor_order,
3650 unsigned rice_parameter,
3651 unsigned rice_parameter_limit,
3652 unsigned min_partition_order,
3653 unsigned max_partition_order,
3655 FLAC__bool do_escape_coding,
3656 unsigned rice_parameter_search_dist,
3657 FLAC__EntropyCodingMethod *best_ecm
3660 unsigned residual_bits, best_residual_bits = 0;
3661 unsigned best_parameters_index = 0;
3662 unsigned best_partition_order = 0;
3663 const unsigned blocksize = residual_samples + predictor_order;
3665 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
3666 min_partition_order = flac_min(min_partition_order, max_partition_order);
3668 precompute_partition_info_sums_(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order, bps);
3670 if(do_escape_coding)
3671 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
3674 int partition_order;
3677 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3679 set_partitioned_rice_(
3680 #ifdef EXACT_RICE_BITS_CALCULATION
3683 abs_residual_partition_sums+sum,
3684 raw_bits_per_partition+sum,
3688 rice_parameter_limit,
3689 rice_parameter_search_dist,
3690 (unsigned)partition_order,
3692 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3697 FLAC__ASSERT(best_residual_bits != 0);
3700 sum += 1u << partition_order;
3701 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3702 best_residual_bits = residual_bits;
3703 best_parameters_index = !best_parameters_index;
3704 best_partition_order = partition_order;
3709 best_ecm->data.partitioned_rice.order = best_partition_order;
3713 * We are allowed to de-const the pointer based on our special
3714 * knowledge; it is const to the outside world.
3716 FLAC__EntropyCodingMethod_PartitionedRiceContents* prc = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_ecm->data.partitioned_rice.contents;
3719 /* save best parameters and raw_bits */
3720 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(prc, flac_max(6u, best_partition_order));
3721 memcpy(prc->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partition_order)));
3722 if(do_escape_coding)
3723 memcpy(prc->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partition_order)));
3725 * Now need to check if the type should be changed to
3726 * FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2 based on the
3727 * size of the rice parameters.
3729 for(partition = 0; partition < (1u<<best_partition_order); partition++) {
3730 if(prc->parameters[partition] >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3731 best_ecm->type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2;
3737 return best_residual_bits;
3740 #if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
3741 extern void precompute_partition_info_sums_32bit_asm_ia32_(
3742 const FLAC__int32 residual[],
3743 FLAC__uint64 abs_residual_partition_sums[],
3745 unsigned predictor_order,
3746 unsigned min_partition_order,
3747 unsigned max_partition_order
3751 void precompute_partition_info_sums_(
3752 const FLAC__int32 residual[],
3753 FLAC__uint64 abs_residual_partition_sums[],
3754 unsigned residual_samples,
3755 unsigned predictor_order,
3756 unsigned min_partition_order,
3757 unsigned max_partition_order,
3761 const unsigned default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
3762 unsigned partitions = 1u << max_partition_order;
3764 FLAC__ASSERT(default_partition_samples > predictor_order);
3766 #if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
3767 /* slightly pessimistic but still catches all common cases */
3768 /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */
3769 if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
3770 precompute_partition_info_sums_32bit_asm_ia32_(residual, abs_residual_partition_sums, residual_samples + predictor_order, predictor_order, min_partition_order, max_partition_order);
3775 /* first do max_partition_order */
3777 unsigned partition, residual_sample, end = (unsigned)(-(int)predictor_order);
3778 /* slightly pessimistic but still catches all common cases */
3779 /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */
3780 if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
3781 FLAC__uint32 abs_residual_partition_sum;
3783 for(partition = residual_sample = 0; partition < partitions; partition++) {
3784 end += default_partition_samples;
3785 abs_residual_partition_sum = 0;
3786 for( ; residual_sample < end; residual_sample++)
3787 abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
3788 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
3791 else { /* have to pessimistically use 64 bits for accumulator */
3792 FLAC__uint64 abs_residual_partition_sum;
3794 for(partition = residual_sample = 0; partition < partitions; partition++) {
3795 end += default_partition_samples;
3796 abs_residual_partition_sum = 0;
3797 for( ; residual_sample < end; residual_sample++)
3798 abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
3799 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
3804 /* now merge partitions for lower orders */
3806 unsigned from_partition = 0, to_partition = partitions;
3807 int partition_order;
3808 for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) {
3811 for(i = 0; i < partitions; i++) {
3812 abs_residual_partition_sums[to_partition++] =
3813 abs_residual_partition_sums[from_partition ] +
3814 abs_residual_partition_sums[from_partition+1];
3815 from_partition += 2;
3821 void precompute_partition_info_escapes_(
3822 const FLAC__int32 residual[],
3823 unsigned raw_bits_per_partition[],
3824 unsigned residual_samples,
3825 unsigned predictor_order,
3826 unsigned min_partition_order,
3827 unsigned max_partition_order
3830 int partition_order;
3831 unsigned from_partition, to_partition = 0;
3832 const unsigned blocksize = residual_samples + predictor_order;
3834 /* first do max_partition_order */
3835 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3838 unsigned partition, partition_sample, partition_samples, residual_sample;
3839 const unsigned partitions = 1u << partition_order;
3840 const unsigned default_partition_samples = blocksize >> partition_order;
3842 FLAC__ASSERT(default_partition_samples > predictor_order);
3844 for(partition = residual_sample = 0; partition < partitions; partition++) {
3845 partition_samples = default_partition_samples;
3847 partition_samples -= predictor_order;
3849 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3850 r = residual[residual_sample++];
3851 /* OPT: maybe faster: rmax |= r ^ (r>>31) */
3857 /* now we know all residual values are in the range [-rmax-1,rmax] */
3858 raw_bits_per_partition[partition] = rmax? FLAC__bitmath_ilog2(rmax) + 2 : 1;
3860 to_partition = partitions;
3861 break; /*@@@ yuck, should remove the 'for' loop instead */
3864 /* now merge partitions for lower orders */
3865 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3868 const unsigned partitions = 1u << partition_order;
3869 for(i = 0; i < partitions; i++) {
3870 m = raw_bits_per_partition[from_partition];
3872 raw_bits_per_partition[to_partition] = flac_max(m, raw_bits_per_partition[from_partition]);
3879 #ifdef EXACT_RICE_BITS_CALCULATION
3880 static inline unsigned count_rice_bits_in_partition_(
3881 const unsigned rice_parameter,
3882 const unsigned partition_samples,
3883 const FLAC__int32 *residual
3886 unsigned i, partition_bits =
3887 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 */
3888 (1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */
3890 for(i = 0; i < partition_samples; i++)
3891 partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter );
3892 return partition_bits;
3895 static inline unsigned count_rice_bits_in_partition_(
3896 const unsigned rice_parameter,
3897 const unsigned partition_samples,
3898 const FLAC__uint64 abs_residual_partition_sum
3902 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 */
3903 (1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */
3906 (unsigned)(abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */
3907 : (unsigned)(abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */
3909 - (partition_samples >> 1)
3910 /* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum.
3911 * The actual number of bits used is closer to the sum(for all i in the partition) of abs(residual[i])>>(rice_parameter-1)
3912 * By using the abs_residual_partition sum, we also add in bits in the LSBs that would normally be shifted out.
3913 * So the subtraction term tries to guess how many extra bits were contributed.
3914 * If the LSBs are randomly distributed, this should average to 0.5 extra bits per sample.
3920 FLAC__bool set_partitioned_rice_(
3921 #ifdef EXACT_RICE_BITS_CALCULATION
3922 const FLAC__int32 residual[],
3924 const FLAC__uint64 abs_residual_partition_sums[],
3925 const unsigned raw_bits_per_partition[],
3926 const unsigned residual_samples,
3927 const unsigned predictor_order,
3928 const unsigned suggested_rice_parameter,
3929 const unsigned rice_parameter_limit,
3930 const unsigned rice_parameter_search_dist,
3931 const unsigned partition_order,
3932 const FLAC__bool search_for_escapes,
3933 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3937 unsigned rice_parameter, partition_bits;
3938 unsigned best_partition_bits, best_rice_parameter = 0;
3939 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
3940 unsigned *parameters, *raw_bits;
3941 #ifdef ENABLE_RICE_PARAMETER_SEARCH
3942 unsigned min_rice_parameter, max_rice_parameter;
3944 (void)rice_parameter_search_dist;
3947 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
3948 FLAC__ASSERT(rice_parameter_limit <= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
3950 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order));
3951 parameters = partitioned_rice_contents->parameters;
3952 raw_bits = partitioned_rice_contents->raw_bits;
3954 if(partition_order == 0) {
3955 best_partition_bits = (unsigned)(-1);
3956 #ifdef ENABLE_RICE_PARAMETER_SEARCH
3957 if(rice_parameter_search_dist) {
3958 if(suggested_rice_parameter < rice_parameter_search_dist)
3959 min_rice_parameter = 0;
3961 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3962 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3963 if(max_rice_parameter >= rice_parameter_limit) {
3964 #ifdef DEBUG_VERBOSE
3965 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, rice_parameter_limit - 1);
3967 max_rice_parameter = rice_parameter_limit - 1;
3971 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3973 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3975 rice_parameter = suggested_rice_parameter;
3977 #ifdef EXACT_RICE_BITS_CALCULATION
3978 partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, residual);
3980 partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, abs_residual_partition_sums[0]);
3982 if(partition_bits < best_partition_bits) {
3983 best_rice_parameter = rice_parameter;
3984 best_partition_bits = partition_bits;
3986 #ifdef ENABLE_RICE_PARAMETER_SEARCH
3989 if(search_for_escapes) {
3990 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;
3991 if(partition_bits <= best_partition_bits) {
3992 raw_bits[0] = raw_bits_per_partition[0];
3993 best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
3994 best_partition_bits = partition_bits;
3999 parameters[0] = best_rice_parameter;
4000 bits_ += best_partition_bits;
4003 unsigned partition, residual_sample;
4004 unsigned partition_samples;
4005 FLAC__uint64 mean, k;
4006 const unsigned partitions = 1u << partition_order;
4007 for(partition = residual_sample = 0; partition < partitions; partition++) {
4008 partition_samples = (residual_samples+predictor_order) >> partition_order;
4009 if(partition == 0) {
4010 if(partition_samples <= predictor_order)
4013 partition_samples -= predictor_order;
4015 mean = abs_residual_partition_sums[partition];
4016 /* we are basically calculating the size in bits of the
4017 * average residual magnitude in the partition:
4018 * rice_parameter = floor(log2(mean/partition_samples))
4019 * 'mean' is not a good name for the variable, it is
4020 * actually the sum of magnitudes of all residual values
4021 * in the partition, so the actual mean is
4022 * mean/partition_samples
4024 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
4026 if(rice_parameter >= rice_parameter_limit) {
4027 #ifdef DEBUG_VERBOSE
4028 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, rice_parameter_limit - 1);
4030 rice_parameter = rice_parameter_limit - 1;
4033 best_partition_bits = (unsigned)(-1);
4034 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4035 if(rice_parameter_search_dist) {
4036 if(rice_parameter < rice_parameter_search_dist)
4037 min_rice_parameter = 0;
4039 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4040 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4041 if(max_rice_parameter >= rice_parameter_limit) {
4042 #ifdef DEBUG_VERBOSE
4043 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, rice_parameter_limit - 1);
4045 max_rice_parameter = rice_parameter_limit - 1;
4049 min_rice_parameter = max_rice_parameter = rice_parameter;
4051 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4053 #ifdef EXACT_RICE_BITS_CALCULATION
4054 partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, residual+residual_sample);
4056 partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, abs_residual_partition_sums[partition]);
4058 if(partition_bits < best_partition_bits) {
4059 best_rice_parameter = rice_parameter;
4060 best_partition_bits = partition_bits;
4062 #ifdef ENABLE_RICE_PARAMETER_SEARCH
4065 if(search_for_escapes) {
4066 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;
4067 if(partition_bits <= best_partition_bits) {
4068 raw_bits[partition] = raw_bits_per_partition[partition];
4069 best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
4070 best_partition_bits = partition_bits;
4073 raw_bits[partition] = 0;
4075 parameters[partition] = best_rice_parameter;
4076 bits_ += best_partition_bits;
4077 residual_sample += partition_samples;
4085 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
4090 for(i = 0; i < samples && !(x&1); i++)
4097 for(shift = 0; !(x&1); shift++)
4102 for(i = 0; i < samples; i++)
4103 signal[i] >>= shift;
4109 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4113 for(channel = 0; channel < channels; channel++)
4114 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4116 fifo->tail += wide_samples;
4118 FLAC__ASSERT(fifo->tail <= fifo->size);
4121 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4124 unsigned sample, wide_sample;
4125 unsigned tail = fifo->tail;
4127 sample = input_offset * channels;
4128 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4129 for(channel = 0; channel < channels; channel++)
4130 fifo->data[channel][tail] = input[sample++];
4135 FLAC__ASSERT(fifo->tail <= fifo->size);
4138 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4140 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4141 const size_t encoded_bytes = encoder->private_->verify.output.bytes;
4144 if(encoder->private_->verify.needs_magic_hack) {
4145 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4146 *bytes = FLAC__STREAM_SYNC_LENGTH;
4147 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4148 encoder->private_->verify.needs_magic_hack = false;
4151 if(encoded_bytes == 0) {
4153 * If we get here, a FIFO underflow has occurred,
4154 * which means there is a bug somewhere.
4157 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4159 else if(encoded_bytes < *bytes)
4160 *bytes = encoded_bytes;
4161 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4162 encoder->private_->verify.output.data += *bytes;
4163 encoder->private_->verify.output.bytes -= *bytes;
4166 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4169 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4171 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4173 const unsigned channels = frame->header.channels;
4174 const unsigned blocksize = frame->header.blocksize;
4175 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4179 for(channel = 0; channel < channels; channel++) {
4180 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4181 unsigned i, sample = 0;
4182 FLAC__int32 expect = 0, got = 0;
4184 for(i = 0; i < blocksize; i++) {
4185 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4187 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4188 got = (FLAC__int32)buffer[channel][i];
4192 FLAC__ASSERT(i < blocksize);
4193 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4194 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
4195 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
4196 encoder->private_->verify.error_stats.channel = channel;
4197 encoder->private_->verify.error_stats.sample = sample;
4198 encoder->private_->verify.error_stats.expected = expect;
4199 encoder->private_->verify.error_stats.got = got;
4200 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4201 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4204 /* dequeue the frame from the fifo */
4205 encoder->private_->verify.input_fifo.tail -= blocksize;
4206 FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
4207 for(channel = 0; channel < channels; channel++)
4208 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]));
4209 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4212 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4214 (void)decoder, (void)metadata, (void)client_data;
4217 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4219 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4220 (void)decoder, (void)status;
4221 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4224 FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4228 *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
4230 if (feof(encoder->private_->file))
4231 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4232 else if (ferror(encoder->private_->file))
4233 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4235 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4238 FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4242 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4243 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4245 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4248 FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4254 offset = ftello(encoder->private_->file);
4257 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4260 *absolute_byte_offset = (FLAC__uint64)offset;
4261 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4265 #ifdef FLAC__VALGRIND_TESTING
4266 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4268 size_t ret = fwrite(ptr, size, nmemb, stream);
4274 #define local__fwrite fwrite
4277 FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
4279 (void)client_data, (void)current_frame;
4281 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
4282 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4284 /* We would like to be able to use 'samples > 0' in the
4285 * clause here but currently because of the nature of our
4286 * Ogg writing implementation, 'samples' is always 0 (see
4287 * ogg_encoder_aspect.c). The downside is extra progress
4290 encoder->private_->is_ogg? true :
4295 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4296 * because at this point in the callback chain, the stats
4297 * have not been updated. Only after we return and control
4298 * gets back to write_frame_() are the stats updated
4300 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);
4302 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4305 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4309 * This will forcibly set stdout to binary mode (for OSes that require it)
4311 FILE *get_binary_stdout_(void)
4313 /* if something breaks here it is probably due to the presence or
4314 * absence of an underscore before the identifiers 'setmode',
4315 * 'fileno', and/or 'O_BINARY'; check your system header files.
4317 #if defined _MSC_VER || defined __MINGW32__
4318 _setmode(_fileno(stdout), _O_BINARY);
4319 #elif defined __CYGWIN__
4320 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4321 setmode(_fileno(stdout), _O_BINARY);
4322 #elif defined __EMX__
4323 setmode(fileno(stdout), O_BINARY);