1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005 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.
34 #include <stdlib.h> /* for malloc() */
35 #include <string.h> /* for memcpy() */
36 #include "FLAC/assert.h"
37 #include "FLAC/stream_decoder.h"
38 #include "protected/stream_encoder.h"
39 #include "private/bitbuffer.h"
40 #include "private/bitmath.h"
41 #include "private/crc.h"
42 #include "private/cpu.h"
43 #include "private/fixed.h"
44 #include "private/format.h"
45 #include "private/lpc.h"
46 #include "private/md5.h"
47 #include "private/memory.h"
48 #include "private/stream_encoder_framing.h"
57 #define min(x,y) ((x)<(y)?(x):(y))
62 #define max(x,y) ((x)>(y)?(x):(y))
65 FLAC__int32 *data[FLAC__MAX_CHANNELS];
66 unsigned size; /* of each data[] in samples */
71 const FLAC__byte *data;
78 ENCODER_IN_METADATA = 1,
82 /***********************************************************************
84 * Private class method prototypes
86 ***********************************************************************/
88 static void set_defaults_(FLAC__StreamEncoder *encoder);
89 static void free_(FLAC__StreamEncoder *encoder);
90 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
91 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
92 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
93 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
95 static FLAC__bool process_subframe_(
96 FLAC__StreamEncoder *encoder,
97 unsigned min_partition_order,
98 unsigned max_partition_order,
99 FLAC__bool precompute_partition_sums,
100 const FLAC__FrameHeader *frame_header,
101 unsigned subframe_bps,
102 const FLAC__int32 integer_signal[],
103 #ifndef FLAC__INTEGER_ONLY_LIBRARY
104 const FLAC__real real_signal[],
106 FLAC__Subframe *subframe[2],
107 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
108 FLAC__int32 *residual[2],
109 unsigned *best_subframe,
113 static FLAC__bool add_subframe_(
114 FLAC__StreamEncoder *encoder,
115 const FLAC__FrameHeader *frame_header,
116 unsigned subframe_bps,
117 const FLAC__Subframe *subframe,
118 FLAC__BitBuffer *frame
121 static unsigned evaluate_constant_subframe_(
122 const FLAC__int32 signal,
123 unsigned subframe_bps,
124 FLAC__Subframe *subframe
127 static unsigned evaluate_fixed_subframe_(
128 FLAC__StreamEncoder *encoder,
129 const FLAC__int32 signal[],
130 FLAC__int32 residual[],
131 FLAC__uint32 abs_residual[],
132 FLAC__uint64 abs_residual_partition_sums[],
133 unsigned raw_bits_per_partition[],
135 unsigned subframe_bps,
137 unsigned rice_parameter,
138 unsigned min_partition_order,
139 unsigned max_partition_order,
140 FLAC__bool precompute_partition_sums,
141 FLAC__bool do_escape_coding,
142 unsigned rice_parameter_search_dist,
143 FLAC__Subframe *subframe,
144 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
147 #ifndef FLAC__INTEGER_ONLY_LIBRARY
148 static unsigned evaluate_lpc_subframe_(
149 FLAC__StreamEncoder *encoder,
150 const FLAC__int32 signal[],
151 FLAC__int32 residual[],
152 FLAC__uint32 abs_residual[],
153 FLAC__uint64 abs_residual_partition_sums[],
154 unsigned raw_bits_per_partition[],
155 const FLAC__real lp_coeff[],
157 unsigned subframe_bps,
159 unsigned qlp_coeff_precision,
160 unsigned rice_parameter,
161 unsigned min_partition_order,
162 unsigned max_partition_order,
163 FLAC__bool precompute_partition_sums,
164 FLAC__bool do_escape_coding,
165 unsigned rice_parameter_search_dist,
166 FLAC__Subframe *subframe,
167 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
171 static unsigned evaluate_verbatim_subframe_(
172 const FLAC__int32 signal[],
174 unsigned subframe_bps,
175 FLAC__Subframe *subframe
178 static unsigned find_best_partition_order_(
179 struct FLAC__StreamEncoderPrivate *private_,
180 const FLAC__int32 residual[],
181 FLAC__uint32 abs_residual[],
182 FLAC__uint64 abs_residual_partition_sums[],
183 unsigned raw_bits_per_partition[],
184 unsigned residual_samples,
185 unsigned predictor_order,
186 unsigned rice_parameter,
187 unsigned min_partition_order,
188 unsigned max_partition_order,
189 FLAC__bool precompute_partition_sums,
190 FLAC__bool do_escape_coding,
191 unsigned rice_parameter_search_dist,
192 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
195 static void precompute_partition_info_sums_(
196 const FLAC__uint32 abs_residual[],
197 FLAC__uint64 abs_residual_partition_sums[],
198 unsigned residual_samples,
199 unsigned predictor_order,
200 unsigned min_partition_order,
201 unsigned max_partition_order
204 static void precompute_partition_info_escapes_(
205 const FLAC__int32 residual[],
206 unsigned raw_bits_per_partition[],
207 unsigned residual_samples,
208 unsigned predictor_order,
209 unsigned min_partition_order,
210 unsigned max_partition_order
213 #ifdef DONT_ESTIMATE_RICE_BITS
214 static FLAC__bool set_partitioned_rice_(
215 const FLAC__uint32 abs_residual[],
216 const FLAC__int32 residual[],
217 const unsigned residual_samples,
218 const unsigned predictor_order,
219 const unsigned suggested_rice_parameter,
220 const unsigned rice_parameter_search_dist,
221 const unsigned partition_order,
222 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
226 static FLAC__bool set_partitioned_rice_with_precompute_(
227 const FLAC__int32 residual[],
228 const FLAC__uint64 abs_residual_partition_sums[],
229 const unsigned raw_bits_per_partition[],
230 const unsigned residual_samples,
231 const unsigned predictor_order,
232 const unsigned suggested_rice_parameter,
233 const unsigned rice_parameter_search_dist,
234 const unsigned partition_order,
235 const FLAC__bool search_for_escapes,
236 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
240 static FLAC__bool set_partitioned_rice_(
241 const FLAC__uint32 abs_residual[],
242 const unsigned residual_samples,
243 const unsigned predictor_order,
244 const unsigned suggested_rice_parameter,
245 const unsigned rice_parameter_search_dist,
246 const unsigned partition_order,
247 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
251 static FLAC__bool set_partitioned_rice_with_precompute_(
252 const FLAC__uint32 abs_residual[],
253 const FLAC__uint64 abs_residual_partition_sums[],
254 const unsigned raw_bits_per_partition[],
255 const unsigned residual_samples,
256 const unsigned predictor_order,
257 const unsigned suggested_rice_parameter,
258 const unsigned rice_parameter_search_dist,
259 const unsigned partition_order,
260 const FLAC__bool search_for_escapes,
261 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
266 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
268 /* verify-related routines: */
269 static void append_to_verify_fifo_(
270 verify_input_fifo *fifo,
271 const FLAC__int32 * const input[],
272 unsigned input_offset,
274 unsigned wide_samples
277 static void append_to_verify_fifo_interleaved_(
278 verify_input_fifo *fifo,
279 const FLAC__int32 input[],
280 unsigned input_offset,
282 unsigned wide_samples
285 static FLAC__StreamDecoderReadStatus verify_read_callback_(
286 const FLAC__StreamDecoder *decoder,
292 static FLAC__StreamDecoderWriteStatus verify_write_callback_(
293 const FLAC__StreamDecoder *decoder,
294 const FLAC__Frame *frame,
295 const FLAC__int32 * const buffer[],
299 static void verify_metadata_callback_(
300 const FLAC__StreamDecoder *decoder,
301 const FLAC__StreamMetadata *metadata,
305 static void verify_error_callback_(
306 const FLAC__StreamDecoder *decoder,
307 FLAC__StreamDecoderErrorStatus status,
312 /***********************************************************************
316 ***********************************************************************/
318 typedef struct FLAC__StreamEncoderPrivate {
319 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
320 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
321 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
322 #ifndef FLAC__INTEGER_ONLY_LIBRARY
323 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
324 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
326 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
327 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
328 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
329 FLAC__int32 *residual_workspace_mid_side[2][2];
330 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
331 FLAC__Subframe subframe_workspace_mid_side[2][2];
332 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
333 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
334 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
335 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
336 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
337 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
338 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
339 unsigned best_subframe_mid_side[2];
340 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
341 unsigned best_subframe_bits_mid_side[2];
342 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
343 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
344 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
345 FLAC__BitBuffer *frame; /* the current frame being worked on */
346 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
347 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
348 FLAC__ChannelAssignment last_channel_assignment;
349 FLAC__StreamMetadata metadata;
350 unsigned current_sample_number;
351 unsigned current_frame_number;
352 struct FLAC__MD5Context md5context;
353 FLAC__CPUInfo cpuinfo;
354 #ifndef FLAC__INTEGER_ONLY_LIBRARY
355 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
357 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
359 #ifndef FLAC__INTEGER_ONLY_LIBRARY
360 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
361 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[]);
362 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[]);
363 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[]);
365 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
366 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
367 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
368 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
369 FLAC__bool disable_constant_subframes;
370 FLAC__bool disable_fixed_subframes;
371 FLAC__bool disable_verbatim_subframes;
372 FLAC__StreamEncoderWriteCallback write_callback;
373 FLAC__StreamEncoderMetadataCallback metadata_callback;
375 /* unaligned (original) pointers to allocated data */
376 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
377 FLAC__int32 *integer_signal_mid_side_unaligned[2];
378 #ifndef FLAC__INTEGER_ONLY_LIBRARY
379 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
380 FLAC__real *real_signal_mid_side_unaligned[2];
382 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
383 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
384 FLAC__uint32 *abs_residual_unaligned;
385 FLAC__uint64 *abs_residual_partition_sums_unaligned;
386 unsigned *raw_bits_per_partition_unaligned;
388 * These fields have been moved here from private function local
389 * declarations merely to save stack space during encoding.
391 #ifndef FLAC__INTEGER_ONLY_LIBRARY
392 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
394 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
396 * The data for the verify section
399 FLAC__StreamDecoder *decoder;
400 EncoderStateHint state_hint;
401 FLAC__bool needs_magic_hack;
402 verify_input_fifo input_fifo;
403 verify_output output;
405 FLAC__uint64 absolute_sample;
406 unsigned frame_number;
409 FLAC__int32 expected;
413 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
414 } FLAC__StreamEncoderPrivate;
416 /***********************************************************************
418 * Public static class data
420 ***********************************************************************/
422 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
423 "FLAC__STREAM_ENCODER_OK",
424 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
425 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
426 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
427 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
428 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
429 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
430 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
431 "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
432 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
433 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
434 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
435 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
436 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
437 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
438 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
439 "FLAC__STREAM_ENCODER_INVALID_METADATA",
440 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
441 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
442 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
443 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
444 "FLAC__STREAM_ENCODER_UNINITIALIZED"
447 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
448 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
449 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
452 /***********************************************************************
454 * Class constructor/destructor
457 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
459 FLAC__StreamEncoder *encoder;
462 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
464 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
469 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
470 if(encoder->protected_ == 0) {
475 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
476 if(encoder->private_ == 0) {
477 free(encoder->protected_);
482 encoder->private_->frame = FLAC__bitbuffer_new();
483 if(encoder->private_->frame == 0) {
484 free(encoder->private_);
485 free(encoder->protected_);
490 set_defaults_(encoder);
492 encoder->private_->is_being_deleted = false;
494 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
495 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
496 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
498 for(i = 0; i < 2; i++) {
499 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
500 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
502 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
503 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
504 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
506 for(i = 0; i < 2; i++) {
507 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
508 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
511 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
512 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
513 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
515 for(i = 0; i < 2; i++) {
516 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
517 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
519 for(i = 0; i < 2; i++)
520 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
522 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
527 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
531 FLAC__ASSERT(0 != encoder);
532 FLAC__ASSERT(0 != encoder->protected_);
533 FLAC__ASSERT(0 != encoder->private_);
534 FLAC__ASSERT(0 != encoder->private_->frame);
536 encoder->private_->is_being_deleted = true;
538 FLAC__stream_encoder_finish(encoder);
540 if(0 != encoder->private_->verify.decoder)
541 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
543 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
544 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
545 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
547 for(i = 0; i < 2; i++) {
548 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
549 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
551 for(i = 0; i < 2; i++)
552 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
554 FLAC__bitbuffer_delete(encoder->private_->frame);
555 free(encoder->private_);
556 free(encoder->protected_);
560 /***********************************************************************
562 * Public class methods
564 ***********************************************************************/
566 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
569 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
571 FLAC__ASSERT(0 != encoder);
573 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
574 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
576 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
578 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
579 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
581 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
582 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
584 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
585 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
587 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
588 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
590 if(encoder->protected_->bits_per_sample >= 32)
591 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
593 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
594 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
596 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
597 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
599 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
600 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
602 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
603 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
605 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
606 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
608 if(encoder->protected_->qlp_coeff_precision == 0) {
609 if(encoder->protected_->bits_per_sample < 16) {
610 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
611 /* @@@ until then we'll make a guess */
612 encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
614 else if(encoder->protected_->bits_per_sample == 16) {
615 if(encoder->protected_->blocksize <= 192)
616 encoder->protected_->qlp_coeff_precision = 7;
617 else if(encoder->protected_->blocksize <= 384)
618 encoder->protected_->qlp_coeff_precision = 8;
619 else if(encoder->protected_->blocksize <= 576)
620 encoder->protected_->qlp_coeff_precision = 9;
621 else if(encoder->protected_->blocksize <= 1152)
622 encoder->protected_->qlp_coeff_precision = 10;
623 else if(encoder->protected_->blocksize <= 2304)
624 encoder->protected_->qlp_coeff_precision = 11;
625 else if(encoder->protected_->blocksize <= 4608)
626 encoder->protected_->qlp_coeff_precision = 12;
628 encoder->protected_->qlp_coeff_precision = 13;
631 if(encoder->protected_->blocksize <= 384)
632 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
633 else if(encoder->protected_->blocksize <= 1152)
634 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
636 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
638 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
640 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
641 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
643 if(encoder->protected_->streamable_subset) {
645 encoder->protected_->blocksize != 192 &&
646 encoder->protected_->blocksize != 576 &&
647 encoder->protected_->blocksize != 1152 &&
648 encoder->protected_->blocksize != 2304 &&
649 encoder->protected_->blocksize != 4608 &&
650 encoder->protected_->blocksize != 256 &&
651 encoder->protected_->blocksize != 512 &&
652 encoder->protected_->blocksize != 1024 &&
653 encoder->protected_->blocksize != 2048 &&
654 encoder->protected_->blocksize != 4096 &&
655 encoder->protected_->blocksize != 8192 &&
656 encoder->protected_->blocksize != 16384
658 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
660 encoder->protected_->sample_rate != 8000 &&
661 encoder->protected_->sample_rate != 16000 &&
662 encoder->protected_->sample_rate != 22050 &&
663 encoder->protected_->sample_rate != 24000 &&
664 encoder->protected_->sample_rate != 32000 &&
665 encoder->protected_->sample_rate != 44100 &&
666 encoder->protected_->sample_rate != 48000 &&
667 encoder->protected_->sample_rate != 96000
669 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
671 encoder->protected_->bits_per_sample != 8 &&
672 encoder->protected_->bits_per_sample != 12 &&
673 encoder->protected_->bits_per_sample != 16 &&
674 encoder->protected_->bits_per_sample != 20 &&
675 encoder->protected_->bits_per_sample != 24
677 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
678 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
679 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
682 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
683 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
684 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
685 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
687 /* validate metadata */
688 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
689 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
690 metadata_has_seektable = false;
691 metadata_has_vorbis_comment = false;
692 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
693 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
694 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
695 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
696 if(metadata_has_seektable) /* only one is allowed */
697 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
698 metadata_has_seektable = true;
699 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
700 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
702 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
703 if(metadata_has_vorbis_comment) /* only one is allowed */
704 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
705 metadata_has_vorbis_comment = true;
707 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
708 if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
709 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
713 encoder->private_->input_capacity = 0;
714 for(i = 0; i < encoder->protected_->channels; i++) {
715 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
716 #ifndef FLAC__INTEGER_ONLY_LIBRARY
717 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
720 for(i = 0; i < 2; i++) {
721 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
722 #ifndef FLAC__INTEGER_ONLY_LIBRARY
723 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
726 for(i = 0; i < encoder->protected_->channels; i++) {
727 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
728 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
729 encoder->private_->best_subframe[i] = 0;
731 for(i = 0; i < 2; i++) {
732 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
733 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
734 encoder->private_->best_subframe_mid_side[i] = 0;
736 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
737 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
738 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
739 #ifndef FLAC__INTEGER_ONLY_LIBRARY
740 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
742 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
743 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply÷ by hand */
744 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
745 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
746 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
747 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
748 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);
750 if(encoder->private_->loose_mid_side_stereo_frames == 0)
751 encoder->private_->loose_mid_side_stereo_frames = 1;
752 encoder->private_->loose_mid_side_stereo_frame_count = 0;
753 encoder->private_->current_sample_number = 0;
754 encoder->private_->current_frame_number = 0;
756 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
757 encoder->private_->use_wide_by_order = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(max(encoder->protected_->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */
758 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
761 * get the CPU info and set the function pointers
763 FLAC__cpu_info(&encoder->private_->cpuinfo);
764 /* first default to the non-asm routines */
765 #ifndef FLAC__INTEGER_ONLY_LIBRARY
766 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
768 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
769 #ifndef FLAC__INTEGER_ONLY_LIBRARY
770 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
771 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
772 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
774 /* now override with asm where appropriate */
775 #ifndef FLAC__INTEGER_ONLY_LIBRARY
776 # ifndef FLAC__NO_ASM
777 if(encoder->private_->cpuinfo.use_asm) {
778 # ifdef FLAC__CPU_IA32
779 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
780 # ifdef FLAC__HAS_NASM
782 if(encoder->private_->cpuinfo.data.ia32.sse) {
783 if(encoder->protected_->max_lpc_order < 4)
784 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
785 else if(encoder->protected_->max_lpc_order < 8)
786 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
787 else if(encoder->protected_->max_lpc_order < 12)
788 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
790 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
793 # endif /* FLAC__SSE_OS */
794 if(encoder->private_->cpuinfo.data.ia32._3dnow)
795 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
797 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
798 if(encoder->private_->cpuinfo.data.ia32.mmx) {
799 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
800 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
803 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
804 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
806 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
807 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
808 # endif /* FLAC__HAS_NASM */
809 # endif /* FLAC__CPU_IA32 */
811 # endif /* !FLAC__NO_ASM */
812 #endif /* !FLAC__INTEGER_ONLY_LIBRARY */
813 /* finally override based on wide-ness if necessary */
814 if(encoder->private_->use_wide_by_block) {
815 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
818 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
819 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
821 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
822 /* the above function sets the state for us in case of an error */
823 return encoder->protected_->state;
826 if(!FLAC__bitbuffer_init(encoder->private_->frame))
827 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
830 * Set up the verify stuff if necessary
832 if(encoder->protected_->verify) {
834 * First, set up the fifo which will hold the
835 * original signal to compare against
837 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
838 for(i = 0; i < encoder->protected_->channels; i++) {
839 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
840 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
842 encoder->private_->verify.input_fifo.tail = 0;
845 * Now set up a stream decoder for verification
847 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
848 if(0 == encoder->private_->verify.decoder)
849 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
851 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
852 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
853 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
854 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
855 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
856 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
857 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
859 encoder->private_->verify.error_stats.absolute_sample = 0;
860 encoder->private_->verify.error_stats.frame_number = 0;
861 encoder->private_->verify.error_stats.channel = 0;
862 encoder->private_->verify.error_stats.sample = 0;
863 encoder->private_->verify.error_stats.expected = 0;
864 encoder->private_->verify.error_stats.got = 0;
867 * write the stream header
869 if(encoder->protected_->verify)
870 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
871 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
872 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
873 if(!write_bitbuffer_(encoder, 0)) {
874 /* the above function sets the state for us in case of an error */
875 return encoder->protected_->state;
879 * write the STREAMINFO metadata block
881 if(encoder->protected_->verify)
882 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
883 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
884 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
885 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
886 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
887 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
888 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
889 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
890 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
891 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
892 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
893 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
894 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
895 FLAC__MD5Init(&encoder->private_->md5context);
896 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
897 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
898 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
899 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
900 if(!write_bitbuffer_(encoder, 0)) {
901 /* the above function sets the state for us in case of an error */
902 return encoder->protected_->state;
906 * Now that the STREAMINFO block is written, we can init this to an
907 * absurdly-high value...
909 encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
910 /* ... and clear this to 0 */
911 encoder->private_->metadata.data.stream_info.total_samples = 0;
914 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
915 * if not, we will write an empty one (FLAC__add_metadata_block()
916 * automatically supplies the vendor string).
918 * WATCHOUT: libOggFLAC depends on us to write this block after the
919 * STREAMINFO since that's what the mapping requires. (In the case
920 * that metadata_has_vorbis_comment is true it will have already
921 * insured that the metadata list is properly ordered.)
923 if(!metadata_has_vorbis_comment) {
924 FLAC__StreamMetadata vorbis_comment;
925 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
926 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
927 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
928 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
929 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
930 vorbis_comment.data.vorbis_comment.num_comments = 0;
931 vorbis_comment.data.vorbis_comment.comments = 0;
932 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
933 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
934 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
935 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
936 if(!write_bitbuffer_(encoder, 0)) {
937 /* the above function sets the state for us in case of an error */
938 return encoder->protected_->state;
943 * write the user's metadata blocks
945 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
946 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
947 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
948 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
949 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
950 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
951 if(!write_bitbuffer_(encoder, 0)) {
952 /* the above function sets the state for us in case of an error */
953 return encoder->protected_->state;
957 if(encoder->protected_->verify)
958 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
960 return encoder->protected_->state;
963 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
965 FLAC__ASSERT(0 != encoder);
967 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
970 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
971 if(encoder->private_->current_sample_number != 0) {
972 encoder->protected_->blocksize = encoder->private_->current_sample_number;
973 process_frame_(encoder, true); /* true => is last frame */
977 FLAC__MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
979 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
980 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
983 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
984 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
987 set_defaults_(encoder);
989 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
992 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
994 FLAC__ASSERT(0 != encoder);
995 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
997 #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
998 encoder->protected_->verify = value;
1003 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
1005 FLAC__ASSERT(0 != encoder);
1006 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1008 encoder->protected_->streamable_subset = value;
1012 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1014 FLAC__ASSERT(0 != encoder);
1015 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1017 encoder->protected_->do_mid_side_stereo = value;
1021 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1023 FLAC__ASSERT(0 != encoder);
1024 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1026 encoder->protected_->loose_mid_side_stereo = value;
1030 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
1032 FLAC__ASSERT(0 != encoder);
1033 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1035 encoder->protected_->channels = value;
1039 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
1041 FLAC__ASSERT(0 != encoder);
1042 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1044 encoder->protected_->bits_per_sample = value;
1048 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
1050 FLAC__ASSERT(0 != encoder);
1051 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1053 encoder->protected_->sample_rate = value;
1057 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1059 FLAC__ASSERT(0 != encoder);
1060 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1062 encoder->protected_->blocksize = value;
1066 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1068 FLAC__ASSERT(0 != encoder);
1069 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1071 encoder->protected_->max_lpc_order = value;
1075 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1077 FLAC__ASSERT(0 != encoder);
1078 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1080 encoder->protected_->qlp_coeff_precision = value;
1084 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1086 FLAC__ASSERT(0 != encoder);
1087 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1089 encoder->protected_->do_qlp_coeff_prec_search = value;
1093 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1095 FLAC__ASSERT(0 != encoder);
1096 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1099 /*@@@ deprecated: */
1100 encoder->protected_->do_escape_coding = value;
1107 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1109 FLAC__ASSERT(0 != encoder);
1110 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1112 encoder->protected_->do_exhaustive_model_search = value;
1116 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1118 FLAC__ASSERT(0 != encoder);
1119 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1121 encoder->protected_->min_residual_partition_order = value;
1125 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1127 FLAC__ASSERT(0 != encoder);
1128 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1130 encoder->protected_->max_residual_partition_order = value;
1134 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1136 FLAC__ASSERT(0 != encoder);
1137 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1140 /*@@@ deprecated: */
1141 encoder->protected_->rice_parameter_search_dist = value;
1148 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1150 FLAC__ASSERT(0 != encoder);
1151 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1153 encoder->protected_->total_samples_estimate = value;
1157 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1159 FLAC__ASSERT(0 != encoder);
1160 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1162 encoder->protected_->metadata = metadata;
1163 encoder->protected_->num_metadata_blocks = num_blocks;
1167 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
1169 FLAC__ASSERT(0 != encoder);
1170 FLAC__ASSERT(0 != value);
1171 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1173 encoder->private_->write_callback = value;
1177 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
1179 FLAC__ASSERT(0 != encoder);
1180 FLAC__ASSERT(0 != value);
1181 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1183 encoder->private_->metadata_callback = value;
1187 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
1189 FLAC__ASSERT(0 != encoder);
1190 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1192 encoder->private_->client_data = value;
1197 * These three functions are not static, but not publically exposed in
1198 * include/FLAC/ either. They are used by the test suite.
1200 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1202 FLAC__ASSERT(0 != encoder);
1203 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1205 encoder->private_->disable_constant_subframes = value;
1209 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1211 FLAC__ASSERT(0 != encoder);
1212 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1214 encoder->private_->disable_fixed_subframes = value;
1218 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1220 FLAC__ASSERT(0 != encoder);
1221 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1223 encoder->private_->disable_verbatim_subframes = value;
1227 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1229 FLAC__ASSERT(0 != encoder);
1230 return encoder->protected_->state;
1233 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1235 FLAC__ASSERT(0 != encoder);
1236 if(encoder->protected_->verify)
1237 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1239 return FLAC__STREAM_DECODER_UNINITIALIZED;
1242 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1244 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1245 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1247 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
1250 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)
1252 FLAC__ASSERT(0 != encoder);
1253 if(0 != absolute_sample)
1254 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1255 if(0 != frame_number)
1256 *frame_number = encoder->private_->verify.error_stats.frame_number;
1258 *channel = encoder->private_->verify.error_stats.channel;
1260 *sample = encoder->private_->verify.error_stats.sample;
1262 *expected = encoder->private_->verify.error_stats.expected;
1264 *got = encoder->private_->verify.error_stats.got;
1267 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1269 FLAC__ASSERT(0 != encoder);
1270 return encoder->protected_->verify;
1273 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1275 FLAC__ASSERT(0 != encoder);
1276 return encoder->protected_->streamable_subset;
1279 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1281 FLAC__ASSERT(0 != encoder);
1282 return encoder->protected_->do_mid_side_stereo;
1285 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1287 FLAC__ASSERT(0 != encoder);
1288 return encoder->protected_->loose_mid_side_stereo;
1291 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1293 FLAC__ASSERT(0 != encoder);
1294 return encoder->protected_->channels;
1297 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1299 FLAC__ASSERT(0 != encoder);
1300 return encoder->protected_->bits_per_sample;
1303 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1305 FLAC__ASSERT(0 != encoder);
1306 return encoder->protected_->sample_rate;
1309 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1311 FLAC__ASSERT(0 != encoder);
1312 return encoder->protected_->blocksize;
1315 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1317 FLAC__ASSERT(0 != encoder);
1318 return encoder->protected_->max_lpc_order;
1321 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1323 FLAC__ASSERT(0 != encoder);
1324 return encoder->protected_->qlp_coeff_precision;
1327 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1329 FLAC__ASSERT(0 != encoder);
1330 return encoder->protected_->do_qlp_coeff_prec_search;
1333 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1335 FLAC__ASSERT(0 != encoder);
1336 return encoder->protected_->do_escape_coding;
1339 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1341 FLAC__ASSERT(0 != encoder);
1342 return encoder->protected_->do_exhaustive_model_search;
1345 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1347 FLAC__ASSERT(0 != encoder);
1348 return encoder->protected_->min_residual_partition_order;
1351 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1353 FLAC__ASSERT(0 != encoder);
1354 return encoder->protected_->max_residual_partition_order;
1357 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1359 FLAC__ASSERT(0 != encoder);
1360 return encoder->protected_->rice_parameter_search_dist;
1363 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1365 FLAC__ASSERT(0 != encoder);
1366 return encoder->protected_->total_samples_estimate;
1369 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1371 unsigned i, j, channel;
1372 FLAC__int32 x, mid, side;
1373 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1375 FLAC__ASSERT(0 != encoder);
1376 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1380 * we have several flavors of the same basic loop, optimized for
1381 * different conditions:
1383 if(encoder->protected_->max_lpc_order > 0) {
1384 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1386 * stereo coding: unroll channel loop
1387 * with LPC: calculate floating point version of signal
1390 if(encoder->protected_->verify)
1391 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1393 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1394 x = mid = side = buffer[0][j];
1395 encoder->private_->integer_signal[0][i] = x;
1396 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1397 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1400 encoder->private_->integer_signal[1][i] = x;
1401 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1402 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1406 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1407 encoder->private_->integer_signal_mid_side[1][i] = side;
1408 encoder->private_->integer_signal_mid_side[0][i] = mid;
1409 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1410 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1411 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1413 encoder->private_->current_sample_number++;
1415 if(i == blocksize) {
1416 if(!process_frame_(encoder, false)) /* false => not last frame */
1419 } while(j < samples);
1423 * independent channel coding: buffer each channel in inner loop
1424 * with LPC: calculate floating point version of signal
1427 if(encoder->protected_->verify)
1428 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1430 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1431 for(channel = 0; channel < channels; channel++) {
1432 x = buffer[channel][j];
1433 encoder->private_->integer_signal[channel][i] = x;
1434 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1435 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1438 encoder->private_->current_sample_number++;
1440 if(i == blocksize) {
1441 if(!process_frame_(encoder, false)) /* false => not last frame */
1444 } while(j < samples);
1448 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1450 * stereo coding: unroll channel loop
1451 * without LPC: no need to calculate floating point version of signal
1454 if(encoder->protected_->verify)
1455 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1457 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1458 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
1460 encoder->private_->integer_signal[1][i] = x;
1463 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1464 encoder->private_->integer_signal_mid_side[1][i] = side;
1465 encoder->private_->integer_signal_mid_side[0][i] = mid;
1466 encoder->private_->current_sample_number++;
1468 if(i == blocksize) {
1469 if(!process_frame_(encoder, false)) /* false => not last frame */
1472 } while(j < samples);
1476 * independent channel coding: buffer each channel in inner loop
1477 * without LPC: no need to calculate floating point version of signal
1480 if(encoder->protected_->verify)
1481 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1483 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1484 for(channel = 0; channel < channels; channel++)
1485 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
1486 encoder->private_->current_sample_number++;
1488 if(i == blocksize) {
1489 if(!process_frame_(encoder, false)) /* false => not last frame */
1492 } while(j < samples);
1499 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1501 unsigned i, j, k, channel;
1502 FLAC__int32 x, mid, side;
1503 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1505 FLAC__ASSERT(0 != encoder);
1506 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1510 * we have several flavors of the same basic loop, optimized for
1511 * different conditions:
1513 if(encoder->protected_->max_lpc_order > 0) {
1514 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1516 * stereo coding: unroll channel loop
1517 * with LPC: calculate floating point version of signal
1520 if(encoder->protected_->verify)
1521 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1523 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1524 x = mid = side = buffer[k++];
1525 encoder->private_->integer_signal[0][i] = x;
1526 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1527 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1530 encoder->private_->integer_signal[1][i] = x;
1531 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1532 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1536 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1537 encoder->private_->integer_signal_mid_side[1][i] = side;
1538 encoder->private_->integer_signal_mid_side[0][i] = mid;
1539 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1540 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1541 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1543 encoder->private_->current_sample_number++;
1545 if(i == blocksize) {
1546 if(!process_frame_(encoder, false)) /* false => not last frame */
1549 } while(j < samples);
1553 * independent channel coding: buffer each channel in inner loop
1554 * with LPC: calculate floating point version of signal
1557 if(encoder->protected_->verify)
1558 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1560 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1561 for(channel = 0; channel < channels; channel++) {
1563 encoder->private_->integer_signal[channel][i] = x;
1564 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1565 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1568 encoder->private_->current_sample_number++;
1570 if(i == blocksize) {
1571 if(!process_frame_(encoder, false)) /* false => not last frame */
1574 } while(j < samples);
1578 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1580 * stereo coding: unroll channel loop
1581 * without LPC: no need to calculate floating point version of signal
1584 if(encoder->protected_->verify)
1585 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1587 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1588 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
1590 encoder->private_->integer_signal[1][i] = x;
1593 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1594 encoder->private_->integer_signal_mid_side[1][i] = side;
1595 encoder->private_->integer_signal_mid_side[0][i] = mid;
1596 encoder->private_->current_sample_number++;
1598 if(i == blocksize) {
1599 if(!process_frame_(encoder, false)) /* false => not last frame */
1602 } while(j < samples);
1606 * independent channel coding: buffer each channel in inner loop
1607 * without LPC: no need to calculate floating point version of signal
1610 if(encoder->protected_->verify)
1611 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1613 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1614 for(channel = 0; channel < channels; channel++)
1615 encoder->private_->integer_signal[channel][i] = buffer[k++];
1616 encoder->private_->current_sample_number++;
1618 if(i == blocksize) {
1619 if(!process_frame_(encoder, false)) /* false => not last frame */
1622 } while(j < samples);
1629 /***********************************************************************
1631 * Private class methods
1633 ***********************************************************************/
1635 void set_defaults_(FLAC__StreamEncoder *encoder)
1637 FLAC__ASSERT(0 != encoder);
1639 #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1640 encoder->protected_->verify = true;
1642 encoder->protected_->verify = false;
1644 encoder->protected_->streamable_subset = true;
1645 encoder->protected_->do_mid_side_stereo = false;
1646 encoder->protected_->loose_mid_side_stereo = false;
1647 encoder->protected_->channels = 2;
1648 encoder->protected_->bits_per_sample = 16;
1649 encoder->protected_->sample_rate = 44100;
1650 encoder->protected_->blocksize = 1152;
1651 encoder->protected_->max_lpc_order = 0;
1652 encoder->protected_->qlp_coeff_precision = 0;
1653 encoder->protected_->do_qlp_coeff_prec_search = false;
1654 encoder->protected_->do_exhaustive_model_search = false;
1655 encoder->protected_->do_escape_coding = false;
1656 encoder->protected_->min_residual_partition_order = 0;
1657 encoder->protected_->max_residual_partition_order = 0;
1658 encoder->protected_->rice_parameter_search_dist = 0;
1659 encoder->protected_->total_samples_estimate = 0;
1660 encoder->protected_->metadata = 0;
1661 encoder->protected_->num_metadata_blocks = 0;
1663 encoder->private_->disable_constant_subframes = false;
1664 encoder->private_->disable_fixed_subframes = false;
1665 encoder->private_->disable_verbatim_subframes = false;
1666 encoder->private_->write_callback = 0;
1667 encoder->private_->metadata_callback = 0;
1668 encoder->private_->client_data = 0;
1671 void free_(FLAC__StreamEncoder *encoder)
1673 unsigned i, channel;
1675 FLAC__ASSERT(0 != encoder);
1676 for(i = 0; i < encoder->protected_->channels; i++) {
1677 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1678 free(encoder->private_->integer_signal_unaligned[i]);
1679 encoder->private_->integer_signal_unaligned[i] = 0;
1681 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1682 if(0 != encoder->private_->real_signal_unaligned[i]) {
1683 free(encoder->private_->real_signal_unaligned[i]);
1684 encoder->private_->real_signal_unaligned[i] = 0;
1688 for(i = 0; i < 2; i++) {
1689 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1690 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1691 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1693 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1694 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1695 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1696 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1700 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1701 for(i = 0; i < 2; i++) {
1702 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1703 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1704 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1708 for(channel = 0; channel < 2; channel++) {
1709 for(i = 0; i < 2; i++) {
1710 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1711 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1712 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1716 if(0 != encoder->private_->abs_residual_unaligned) {
1717 free(encoder->private_->abs_residual_unaligned);
1718 encoder->private_->abs_residual_unaligned = 0;
1720 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1721 free(encoder->private_->abs_residual_partition_sums_unaligned);
1722 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1724 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1725 free(encoder->private_->raw_bits_per_partition_unaligned);
1726 encoder->private_->raw_bits_per_partition_unaligned = 0;
1728 if(encoder->protected_->verify) {
1729 for(i = 0; i < encoder->protected_->channels; i++) {
1730 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1731 free(encoder->private_->verify.input_fifo.data[i]);
1732 encoder->private_->verify.input_fifo.data[i] = 0;
1736 FLAC__bitbuffer_free(encoder->private_->frame);
1739 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
1742 unsigned i, channel;
1744 FLAC__ASSERT(new_size > 0);
1745 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1746 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1748 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1749 if(new_size <= encoder->private_->input_capacity)
1754 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1755 * requires that the input arrays (in our case the integer signals)
1756 * have a buffer of up to 3 zeroes in front (at negative indices) for
1757 * alignment purposes; we use 4 to keep the data well-aligned.
1760 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1761 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1762 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1763 if(encoder->protected_->max_lpc_order > 0)
1764 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1766 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1767 encoder->private_->integer_signal[i] += 4;
1769 for(i = 0; ok && i < 2; i++) {
1770 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
1771 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1772 if(encoder->protected_->max_lpc_order > 0)
1773 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
1775 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1776 encoder->private_->integer_signal_mid_side[i] += 4;
1778 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1779 for(i = 0; ok && i < 2; i++) {
1780 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1783 for(channel = 0; ok && channel < 2; channel++) {
1784 for(i = 0; ok && i < 2; i++) {
1785 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
1788 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1789 if(encoder->private_->precompute_partition_sums || encoder->protected_->do_escape_coding) /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
1790 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1791 if(encoder->protected_->do_escape_coding)
1792 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1795 encoder->private_->input_capacity = new_size;
1797 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1802 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
1804 const FLAC__byte *buffer;
1807 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1809 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1811 if(encoder->protected_->verify) {
1812 encoder->private_->verify.output.data = buffer;
1813 encoder->private_->verify.output.bytes = bytes;
1814 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1815 encoder->private_->verify.needs_magic_hack = true;
1818 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1819 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1820 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1821 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1827 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1828 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1829 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1833 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1836 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1837 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1843 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1845 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1848 * Accumulate raw signal to the MD5 signature
1850 if(!FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) {
1851 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1856 * Process the frame header and subframes into the frame bitbuffer
1858 if(!process_subframes_(encoder, is_last_frame)) {
1859 /* the above function sets the state for us in case of an error */
1864 * Zero-pad the frame to a byte_boundary
1866 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1867 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1872 * CRC-16 the whole thing
1874 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1875 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1880 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1881 /* the above function sets the state for us in case of an error */
1886 * Get ready for the next frame
1888 encoder->private_->current_sample_number = 0;
1889 encoder->private_->current_frame_number++;
1890 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1895 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1897 FLAC__FrameHeader frame_header;
1898 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1899 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1902 * Calculate the min,max Rice partition orders
1905 max_partition_order = 0;
1908 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1909 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
1911 min_partition_order = min(min_partition_order, max_partition_order);
1913 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1918 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1919 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1922 frame_header.blocksize = encoder->protected_->blocksize;
1923 frame_header.sample_rate = encoder->protected_->sample_rate;
1924 frame_header.channels = encoder->protected_->channels;
1925 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1926 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1927 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1928 frame_header.number.frame_number = encoder->private_->current_frame_number;
1931 * Figure out what channel assignments to try
1933 if(encoder->protected_->do_mid_side_stereo) {
1934 if(encoder->protected_->loose_mid_side_stereo) {
1935 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1936 do_independent = true;
1940 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1941 do_mid_side = !do_independent;
1945 do_independent = true;
1950 do_independent = true;
1951 do_mid_side = false;
1954 FLAC__ASSERT(do_independent || do_mid_side);
1957 * Check for wasted bits; set effective bps for each subframe
1959 if(do_independent) {
1960 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1961 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1962 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1963 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1967 FLAC__ASSERT(encoder->protected_->channels == 2);
1968 for(channel = 0; channel < 2; channel++) {
1969 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1970 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1971 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1976 * First do a normal encoding pass of each independent channel
1978 if(do_independent) {
1979 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1983 min_partition_order,
1984 max_partition_order,
1985 precompute_partition_sums,
1987 encoder->private_->subframe_bps[channel],
1988 encoder->private_->integer_signal[channel],
1989 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1990 encoder->private_->real_signal[channel],
1992 encoder->private_->subframe_workspace_ptr[channel],
1993 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1994 encoder->private_->residual_workspace[channel],
1995 encoder->private_->best_subframe+channel,
1996 encoder->private_->best_subframe_bits+channel
2004 * Now do mid and side channels if requested
2007 FLAC__ASSERT(encoder->protected_->channels == 2);
2009 for(channel = 0; channel < 2; channel++) {
2013 min_partition_order,
2014 max_partition_order,
2015 precompute_partition_sums,
2017 encoder->private_->subframe_bps_mid_side[channel],
2018 encoder->private_->integer_signal_mid_side[channel],
2019 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2020 encoder->private_->real_signal_mid_side[channel],
2022 encoder->private_->subframe_workspace_ptr_mid_side[channel],
2023 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
2024 encoder->private_->residual_workspace_mid_side[channel],
2025 encoder->private_->best_subframe_mid_side+channel,
2026 encoder->private_->best_subframe_bits_mid_side+channel
2034 * Compose the frame bitbuffer
2037 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
2038 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
2039 FLAC__ChannelAssignment channel_assignment;
2041 FLAC__ASSERT(encoder->protected_->channels == 2);
2043 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
2044 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
2047 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
2049 FLAC__ChannelAssignment ca;
2051 FLAC__ASSERT(do_independent && do_mid_side);
2053 /* We have to figure out which channel assignent results in the smallest frame */
2054 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
2055 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
2056 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
2057 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
2059 for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
2060 if(bits[ca] < min_bits) {
2061 min_bits = bits[ca];
2062 channel_assignment = ca;
2067 frame_header.channel_assignment = channel_assignment;
2069 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
2070 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2074 switch(channel_assignment) {
2075 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2076 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2077 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
2079 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2080 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2081 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2083 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2084 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2085 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
2087 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2088 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
2089 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2095 switch(channel_assignment) {
2096 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2097 left_bps = encoder->private_->subframe_bps [0];
2098 right_bps = encoder->private_->subframe_bps [1];
2100 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2101 left_bps = encoder->private_->subframe_bps [0];
2102 right_bps = encoder->private_->subframe_bps_mid_side[1];
2104 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2105 left_bps = encoder->private_->subframe_bps_mid_side[1];
2106 right_bps = encoder->private_->subframe_bps [1];
2108 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2109 left_bps = encoder->private_->subframe_bps_mid_side[0];
2110 right_bps = encoder->private_->subframe_bps_mid_side[1];
2116 /* note that encoder_add_subframe_ sets the state for us in case of an error */
2117 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
2119 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
2123 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
2124 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2128 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2129 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
2130 /* the above function sets the state for us in case of an error */
2136 if(encoder->protected_->loose_mid_side_stereo) {
2137 encoder->private_->loose_mid_side_stereo_frame_count++;
2138 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
2139 encoder->private_->loose_mid_side_stereo_frame_count = 0;
2142 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
2147 FLAC__bool process_subframe_(
2148 FLAC__StreamEncoder *encoder,
2149 unsigned min_partition_order,
2150 unsigned max_partition_order,
2151 FLAC__bool precompute_partition_sums,
2152 const FLAC__FrameHeader *frame_header,
2153 unsigned subframe_bps,
2154 const FLAC__int32 integer_signal[],
2155 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2156 const FLAC__real real_signal[],
2158 FLAC__Subframe *subframe[2],
2159 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
2160 FLAC__int32 *residual[2],
2161 unsigned *best_subframe,
2165 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2166 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
2168 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
2170 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2171 FLAC__double lpc_residual_bits_per_sample;
2172 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 */
2173 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
2174 unsigned min_lpc_order, max_lpc_order, lpc_order;
2175 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
2177 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
2178 unsigned rice_parameter;
2179 unsigned _candidate_bits, _best_bits;
2180 unsigned _best_subframe;
2182 /* verbatim subframe is the baseline against which we measure other compressed subframes */
2184 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
2185 _best_bits = UINT_MAX;
2187 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2189 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
2190 unsigned signal_is_constant = false;
2191 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);
2192 /* check for constant subframe */
2194 !encoder->private_->disable_constant_subframes &&
2195 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2196 fixed_residual_bits_per_sample[1] == 0.0
2198 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
2201 /* the above means it's possible all samples are the same value; now double-check it: */
2203 signal_is_constant = true;
2204 for(i = 1; i < frame_header->blocksize; i++) {
2205 if(integer_signal[0] != integer_signal[i]) {
2206 signal_is_constant = false;
2211 if(signal_is_constant) {
2212 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
2213 if(_candidate_bits < _best_bits) {
2214 _best_subframe = !_best_subframe;
2215 _best_bits = _candidate_bits;
2219 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
2221 if(encoder->protected_->do_exhaustive_model_search) {
2222 min_fixed_order = 0;
2223 max_fixed_order = FLAC__MAX_FIXED_ORDER;
2226 min_fixed_order = max_fixed_order = guess_fixed_order;
2228 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
2229 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2230 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
2231 continue; /* don't even try */
2232 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 */
2234 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
2235 continue; /* don't even try */
2236 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 */
2238 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2239 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2240 #ifdef DEBUG_VERBOSE
2241 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2243 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2246 evaluate_fixed_subframe_(
2249 residual[!_best_subframe],
2250 encoder->private_->abs_residual,
2251 encoder->private_->abs_residual_partition_sums,
2252 encoder->private_->raw_bits_per_partition,
2253 frame_header->blocksize,
2257 min_partition_order,
2258 max_partition_order,
2259 precompute_partition_sums,
2260 encoder->protected_->do_escape_coding,
2261 encoder->protected_->rice_parameter_search_dist,
2262 subframe[!_best_subframe],
2263 partitioned_rice_contents[!_best_subframe]
2265 if(_candidate_bits < _best_bits) {
2266 _best_subframe = !_best_subframe;
2267 _best_bits = _candidate_bits;
2272 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2274 if(encoder->protected_->max_lpc_order > 0) {
2275 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
2276 max_lpc_order = frame_header->blocksize-1;
2278 max_lpc_order = encoder->protected_->max_lpc_order;
2279 if(max_lpc_order > 0) {
2280 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2281 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2282 if(autoc[0] != 0.0) {
2283 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2284 if(encoder->protected_->do_exhaustive_model_search) {
2288 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
2289 min_lpc_order = max_lpc_order = guess_lpc_order;
2291 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2292 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2293 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
2294 continue; /* don't even try */
2295 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2296 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2297 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2298 #ifdef DEBUG_VERBOSE
2299 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2301 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2303 if(encoder->protected_->do_qlp_coeff_prec_search) {
2304 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2305 /* ensure a 32-bit datapath throughout for 16bps or less */
2306 if(subframe_bps <= 16)
2307 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2309 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2312 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2314 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2316 evaluate_lpc_subframe_(
2319 residual[!_best_subframe],
2320 encoder->private_->abs_residual,
2321 encoder->private_->abs_residual_partition_sums,
2322 encoder->private_->raw_bits_per_partition,
2323 encoder->private_->lp_coeff[lpc_order-1],
2324 frame_header->blocksize,
2327 qlp_coeff_precision,
2329 min_partition_order,
2330 max_partition_order,
2331 precompute_partition_sums,
2332 encoder->protected_->do_escape_coding,
2333 encoder->protected_->rice_parameter_search_dist,
2334 subframe[!_best_subframe],
2335 partitioned_rice_contents[!_best_subframe]
2337 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2338 if(_candidate_bits < _best_bits) {
2339 _best_subframe = !_best_subframe;
2340 _best_bits = _candidate_bits;
2348 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
2352 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2353 if(_best_bits == UINT_MAX) {
2354 FLAC__ASSERT(_best_subframe == 0);
2355 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2358 *best_subframe = _best_subframe;
2359 *best_bits = _best_bits;
2364 FLAC__bool add_subframe_(
2365 FLAC__StreamEncoder *encoder,
2366 const FLAC__FrameHeader *frame_header,
2367 unsigned subframe_bps,
2368 const FLAC__Subframe *subframe,
2369 FLAC__BitBuffer *frame
2372 switch(subframe->type) {
2373 case FLAC__SUBFRAME_TYPE_CONSTANT:
2374 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
2375 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2379 case FLAC__SUBFRAME_TYPE_FIXED:
2380 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2381 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2385 case FLAC__SUBFRAME_TYPE_LPC:
2386 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2387 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2391 case FLAC__SUBFRAME_TYPE_VERBATIM:
2392 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2393 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2404 unsigned evaluate_constant_subframe_(
2405 const FLAC__int32 signal,
2406 unsigned subframe_bps,
2407 FLAC__Subframe *subframe
2410 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2411 subframe->data.constant.value = signal;
2413 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
2416 unsigned evaluate_fixed_subframe_(
2417 FLAC__StreamEncoder *encoder,
2418 const FLAC__int32 signal[],
2419 FLAC__int32 residual[],
2420 FLAC__uint32 abs_residual[],
2421 FLAC__uint64 abs_residual_partition_sums[],
2422 unsigned raw_bits_per_partition[],
2424 unsigned subframe_bps,
2426 unsigned rice_parameter,
2427 unsigned min_partition_order,
2428 unsigned max_partition_order,
2429 FLAC__bool precompute_partition_sums,
2430 FLAC__bool do_escape_coding,
2431 unsigned rice_parameter_search_dist,
2432 FLAC__Subframe *subframe,
2433 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2436 unsigned i, residual_bits;
2437 const unsigned residual_samples = blocksize - order;
2439 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2441 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2443 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2444 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2445 subframe->data.fixed.residual = residual;
2448 find_best_partition_order_(
2452 abs_residual_partition_sums,
2453 raw_bits_per_partition,
2457 min_partition_order,
2458 max_partition_order,
2459 precompute_partition_sums,
2461 rice_parameter_search_dist,
2462 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2465 subframe->data.fixed.order = order;
2466 for(i = 0; i < order; i++)
2467 subframe->data.fixed.warmup[i] = signal[i];
2469 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
2472 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2473 unsigned evaluate_lpc_subframe_(
2474 FLAC__StreamEncoder *encoder,
2475 const FLAC__int32 signal[],
2476 FLAC__int32 residual[],
2477 FLAC__uint32 abs_residual[],
2478 FLAC__uint64 abs_residual_partition_sums[],
2479 unsigned raw_bits_per_partition[],
2480 const FLAC__real lp_coeff[],
2482 unsigned subframe_bps,
2484 unsigned qlp_coeff_precision,
2485 unsigned rice_parameter,
2486 unsigned min_partition_order,
2487 unsigned max_partition_order,
2488 FLAC__bool precompute_partition_sums,
2489 FLAC__bool do_escape_coding,
2490 unsigned rice_parameter_search_dist,
2491 FLAC__Subframe *subframe,
2492 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2495 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
2496 unsigned i, residual_bits;
2497 int quantization, ret;
2498 const unsigned residual_samples = blocksize - order;
2500 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2501 if(subframe_bps <= 16) {
2502 FLAC__ASSERT(order > 0);
2503 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2504 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2507 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
2509 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2511 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2512 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2513 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2515 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2517 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2519 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2521 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2522 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2523 subframe->data.lpc.residual = residual;
2526 find_best_partition_order_(
2530 abs_residual_partition_sums,
2531 raw_bits_per_partition,
2535 min_partition_order,
2536 max_partition_order,
2537 precompute_partition_sums,
2539 rice_parameter_search_dist,
2540 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2543 subframe->data.lpc.order = order;
2544 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2545 subframe->data.lpc.quantization_level = quantization;
2546 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
2547 for(i = 0; i < order; i++)
2548 subframe->data.lpc.warmup[i] = signal[i];
2550 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
2554 unsigned evaluate_verbatim_subframe_(
2555 const FLAC__int32 signal[],
2557 unsigned subframe_bps,
2558 FLAC__Subframe *subframe
2561 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2563 subframe->data.verbatim.data = signal;
2565 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
2568 unsigned find_best_partition_order_(
2569 FLAC__StreamEncoderPrivate *private_,
2570 const FLAC__int32 residual[],
2571 FLAC__uint32 abs_residual[],
2572 FLAC__uint64 abs_residual_partition_sums[],
2573 unsigned raw_bits_per_partition[],
2574 unsigned residual_samples,
2575 unsigned predictor_order,
2576 unsigned rice_parameter,
2577 unsigned min_partition_order,
2578 unsigned max_partition_order,
2579 FLAC__bool precompute_partition_sums,
2580 FLAC__bool do_escape_coding,
2581 unsigned rice_parameter_search_dist,
2582 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2586 unsigned residual_bits, best_residual_bits = 0;
2587 unsigned residual_sample;
2588 unsigned best_parameters_index = 0;
2589 const unsigned blocksize = residual_samples + predictor_order;
2591 /* compute abs(residual) for use later */
2592 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2593 r = residual[residual_sample];
2594 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2597 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2598 min_partition_order = min(min_partition_order, max_partition_order);
2600 if(precompute_partition_sums) {
2601 int partition_order;
2604 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2606 if(do_escape_coding)
2607 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2609 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2610 #ifdef DONT_ESTIMATE_RICE_BITS
2612 set_partitioned_rice_with_precompute_(
2614 abs_residual_partition_sums+sum,
2615 raw_bits_per_partition+sum,
2619 rice_parameter_search_dist,
2620 (unsigned)partition_order,
2622 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2628 set_partitioned_rice_with_precompute_(
2630 abs_residual_partition_sums+sum,
2631 raw_bits_per_partition+sum,
2635 rice_parameter_search_dist,
2636 (unsigned)partition_order,
2638 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2644 FLAC__ASSERT(best_residual_bits != 0);
2647 sum += 1u << partition_order;
2648 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2649 best_residual_bits = residual_bits;
2650 best_parameters_index = !best_parameters_index;
2651 best_partitioned_rice->order = partition_order;
2656 unsigned partition_order;
2657 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2658 #ifdef DONT_ESTIMATE_RICE_BITS
2660 set_partitioned_rice_(
2666 rice_parameter_search_dist,
2668 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2674 set_partitioned_rice_(
2679 rice_parameter_search_dist,
2681 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2687 FLAC__ASSERT(best_residual_bits != 0);
2690 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2691 best_residual_bits = residual_bits;
2692 best_parameters_index = !best_parameters_index;
2693 best_partitioned_rice->order = partition_order;
2699 * We are allowed to de-const the pointer based on our special knowledge;
2700 * it is const to the outside world.
2703 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2704 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2705 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2706 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2709 return best_residual_bits;
2712 void precompute_partition_info_sums_(
2713 const FLAC__uint32 abs_residual[],
2714 FLAC__uint64 abs_residual_partition_sums[],
2715 unsigned residual_samples,
2716 unsigned predictor_order,
2717 unsigned min_partition_order,
2718 unsigned max_partition_order
2721 int partition_order;
2722 unsigned from_partition, to_partition = 0;
2723 const unsigned blocksize = residual_samples + predictor_order;
2725 /* first do max_partition_order */
2726 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2727 FLAC__uint64 abs_residual_partition_sum;
2729 unsigned partition, partition_sample, partition_samples, residual_sample;
2730 const unsigned partitions = 1u << partition_order;
2731 const unsigned default_partition_samples = blocksize >> partition_order;
2733 FLAC__ASSERT(default_partition_samples > predictor_order);
2735 for(partition = residual_sample = 0; partition < partitions; partition++) {
2736 partition_samples = default_partition_samples;
2738 partition_samples -= predictor_order;
2739 abs_residual_partition_sum = 0;
2740 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2741 abs_r = abs_residual[residual_sample];
2742 abs_residual_partition_sum += abs_r;
2745 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2747 to_partition = partitions;
2751 /* now merge partitions for lower orders */
2752 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2755 const unsigned partitions = 1u << partition_order;
2756 for(i = 0; i < partitions; i++) {
2757 s = abs_residual_partition_sums[from_partition];
2759 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
2766 void precompute_partition_info_escapes_(
2767 const FLAC__int32 residual[],
2768 unsigned raw_bits_per_partition[],
2769 unsigned residual_samples,
2770 unsigned predictor_order,
2771 unsigned min_partition_order,
2772 unsigned max_partition_order
2775 int partition_order;
2776 unsigned from_partition, to_partition = 0;
2777 const unsigned blocksize = residual_samples + predictor_order;
2779 /* first do max_partition_order */
2780 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2781 FLAC__int32 r, residual_partition_min, residual_partition_max;
2782 unsigned silog2_min, silog2_max;
2783 unsigned partition, partition_sample, partition_samples, residual_sample;
2784 const unsigned partitions = 1u << partition_order;
2785 const unsigned default_partition_samples = blocksize >> partition_order;
2787 FLAC__ASSERT(default_partition_samples > predictor_order);
2789 for(partition = residual_sample = 0; partition < partitions; partition++) {
2790 partition_samples = default_partition_samples;
2792 partition_samples -= predictor_order;
2793 residual_partition_min = residual_partition_max = 0;
2794 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2795 r = residual[residual_sample];
2796 if(r < residual_partition_min)
2797 residual_partition_min = r;
2798 else if(r > residual_partition_max)
2799 residual_partition_max = r;
2802 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2803 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2804 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
2806 to_partition = partitions;
2810 /* now merge partitions for lower orders */
2811 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2814 const unsigned partitions = 1u << partition_order;
2815 for(i = 0; i < partitions; i++) {
2816 m = raw_bits_per_partition[from_partition];
2818 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2825 #ifdef VARIABLE_RICE_BITS
2826 #undef VARIABLE_RICE_BITS
2828 #ifndef DONT_ESTIMATE_RICE_BITS
2829 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
2832 #ifdef DONT_ESTIMATE_RICE_BITS
2833 FLAC__bool set_partitioned_rice_(
2834 const FLAC__uint32 abs_residual[],
2835 const FLAC__int32 residual[],
2836 const unsigned residual_samples,
2837 const unsigned predictor_order,
2838 const unsigned suggested_rice_parameter,
2839 const unsigned rice_parameter_search_dist,
2840 const unsigned partition_order,
2841 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2845 FLAC__bool set_partitioned_rice_(
2846 const FLAC__uint32 abs_residual[],
2847 const unsigned residual_samples,
2848 const unsigned predictor_order,
2849 const unsigned suggested_rice_parameter,
2850 const unsigned rice_parameter_search_dist,
2851 const unsigned partition_order,
2852 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2857 unsigned rice_parameter, partition_bits;
2858 #ifndef NO_RICE_SEARCH
2859 unsigned best_partition_bits;
2860 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2862 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2863 unsigned *parameters;
2865 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2867 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2868 parameters = partitioned_rice_contents->parameters;
2870 if(partition_order == 0) {
2873 #ifndef NO_RICE_SEARCH
2874 if(rice_parameter_search_dist) {
2875 if(suggested_rice_parameter < rice_parameter_search_dist)
2876 min_rice_parameter = 0;
2878 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2879 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2880 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2881 #ifdef DEBUG_VERBOSE
2882 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2884 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2888 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2890 best_partition_bits = 0xffffffff;
2891 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2893 #ifdef VARIABLE_RICE_BITS
2894 const unsigned rice_parameter_estimate = rice_parameter-1;
2895 partition_bits = (1+rice_parameter) * residual_samples;
2899 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2900 for(i = 0; i < residual_samples; i++) {
2901 #ifdef VARIABLE_RICE_BITS
2902 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2904 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2907 #ifndef NO_RICE_SEARCH
2908 if(partition_bits < best_partition_bits) {
2909 best_rice_parameter = rice_parameter;
2910 best_partition_bits = partition_bits;
2914 parameters[0] = best_rice_parameter;
2915 bits_ += best_partition_bits;
2918 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2919 unsigned partition_samples;
2920 FLAC__uint64 mean, k;
2921 const unsigned partitions = 1u << partition_order;
2922 for(partition = residual_sample = 0; partition < partitions; partition++) {
2923 partition_samples = (residual_samples+predictor_order) >> partition_order;
2924 if(partition == 0) {
2925 if(partition_samples <= predictor_order)
2928 partition_samples -= predictor_order;
2931 save_residual_sample = residual_sample;
2932 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2933 mean += abs_residual[residual_sample];
2934 residual_sample = save_residual_sample;
2935 /* we are basically calculating the size in bits of the
2936 * average residual magnitude in the partition:
2937 * rice_parameter = floor(log2(mean/partition_samples))
2938 * 'mean' is not a good name for the variable, it is
2939 * actually the sum of magnitudes of all residual values
2940 * in the partition, so the actual mean is
2941 * mean/partition_samples
2943 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2945 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2946 #ifdef DEBUG_VERBOSE
2947 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2949 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2952 #ifndef NO_RICE_SEARCH
2953 if(rice_parameter_search_dist) {
2954 if(rice_parameter < rice_parameter_search_dist)
2955 min_rice_parameter = 0;
2957 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2958 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2959 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2960 #ifdef DEBUG_VERBOSE
2961 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2963 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2967 min_rice_parameter = max_rice_parameter = rice_parameter;
2969 best_partition_bits = 0xffffffff;
2970 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2972 #ifdef VARIABLE_RICE_BITS
2973 const unsigned rice_parameter_estimate = rice_parameter-1;
2974 partition_bits = (1+rice_parameter) * partition_samples;
2978 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2979 save_residual_sample = residual_sample;
2980 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2981 #ifdef VARIABLE_RICE_BITS
2982 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2984 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2987 #ifndef NO_RICE_SEARCH
2988 if(rice_parameter != max_rice_parameter)
2989 residual_sample = save_residual_sample;
2990 if(partition_bits < best_partition_bits) {
2991 best_rice_parameter = rice_parameter;
2992 best_partition_bits = partition_bits;
2996 parameters[partition] = best_rice_parameter;
2997 bits_ += best_partition_bits;
3005 #ifdef DONT_ESTIMATE_RICE_BITS
3006 FLAC__bool set_partitioned_rice_with_precompute_(
3007 const FLAC__int32 residual[],
3008 const FLAC__uint64 abs_residual_partition_sums[],
3009 const unsigned raw_bits_per_partition[],
3010 const unsigned residual_samples,
3011 const unsigned predictor_order,
3012 const unsigned suggested_rice_parameter,
3013 const unsigned rice_parameter_search_dist,
3014 const unsigned partition_order,
3015 const FLAC__bool search_for_escapes,
3016 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3020 FLAC__bool set_partitioned_rice_with_precompute_(
3021 const FLAC__uint32 abs_residual[],
3022 const FLAC__uint64 abs_residual_partition_sums[],
3023 const unsigned raw_bits_per_partition[],
3024 const unsigned residual_samples,
3025 const unsigned predictor_order,
3026 const unsigned suggested_rice_parameter,
3027 const unsigned rice_parameter_search_dist,
3028 const unsigned partition_order,
3029 const FLAC__bool search_for_escapes,
3030 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3035 unsigned rice_parameter, partition_bits;
3036 #ifndef NO_RICE_SEARCH
3037 unsigned best_partition_bits;
3038 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3041 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
3042 unsigned *parameters, *raw_bits;
3044 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
3046 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3047 parameters = partitioned_rice_contents->parameters;
3048 raw_bits = partitioned_rice_contents->raw_bits;
3050 if(partition_order == 0) {
3053 #ifndef NO_RICE_SEARCH
3054 if(rice_parameter_search_dist) {
3055 if(suggested_rice_parameter < rice_parameter_search_dist)
3056 min_rice_parameter = 0;
3058 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3059 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3060 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3061 #ifdef DEBUG_VERBOSE
3062 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3064 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3068 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3070 best_partition_bits = 0xffffffff;
3071 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3073 #ifdef VARIABLE_RICE_BITS
3074 const unsigned rice_parameter_estimate = rice_parameter-1;
3075 partition_bits = (1+rice_parameter) * residual_samples;
3079 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3080 for(i = 0; i < residual_samples; i++) {
3081 #ifdef VARIABLE_RICE_BITS
3082 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
3084 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
3087 #ifndef NO_RICE_SEARCH
3088 if(partition_bits < best_partition_bits) {
3089 best_rice_parameter = rice_parameter;
3090 best_partition_bits = partition_bits;
3094 if(search_for_escapes) {
3095 flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
3096 if(flat_bits <= best_partition_bits) {
3097 raw_bits[0] = raw_bits_per_partition[0];
3098 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3099 best_partition_bits = flat_bits;
3102 parameters[0] = best_rice_parameter;
3103 bits_ += best_partition_bits;
3106 unsigned partition, residual_sample, save_residual_sample, partition_sample;
3107 unsigned partition_samples;
3108 FLAC__uint64 mean, k;
3109 const unsigned partitions = 1u << partition_order;
3110 for(partition = residual_sample = 0; partition < partitions; partition++) {
3111 partition_samples = (residual_samples+predictor_order) >> partition_order;
3112 if(partition == 0) {
3113 if(partition_samples <= predictor_order)
3116 partition_samples -= predictor_order;
3118 mean = abs_residual_partition_sums[partition];
3119 /* we are basically calculating the size in bits of the
3120 * average residual magnitude in the partition:
3121 * rice_parameter = floor(log2(mean/partition_samples))
3122 * 'mean' is not a good name for the variable, it is
3123 * actually the sum of magnitudes of all residual values
3124 * in the partition, so the actual mean is
3125 * mean/partition_samples
3127 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
3129 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3130 #ifdef DEBUG_VERBOSE
3131 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3133 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3136 #ifndef NO_RICE_SEARCH
3137 if(rice_parameter_search_dist) {
3138 if(rice_parameter < rice_parameter_search_dist)
3139 min_rice_parameter = 0;
3141 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3142 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
3143 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3144 #ifdef DEBUG_VERBOSE
3145 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3147 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3151 min_rice_parameter = max_rice_parameter = rice_parameter;
3153 best_partition_bits = 0xffffffff;
3154 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3156 #ifdef VARIABLE_RICE_BITS
3157 const unsigned rice_parameter_estimate = rice_parameter-1;
3158 partition_bits = (1+rice_parameter) * partition_samples;
3162 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3163 save_residual_sample = residual_sample;
3164 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3165 #ifdef VARIABLE_RICE_BITS
3166 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
3168 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
3171 #ifndef NO_RICE_SEARCH
3172 if(rice_parameter != max_rice_parameter)
3173 residual_sample = save_residual_sample;
3174 if(partition_bits < best_partition_bits) {
3175 best_rice_parameter = rice_parameter;
3176 best_partition_bits = partition_bits;
3180 if(search_for_escapes) {
3181 flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
3182 if(flat_bits <= best_partition_bits) {
3183 raw_bits[partition] = raw_bits_per_partition[partition];
3184 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3185 best_partition_bits = flat_bits;
3188 parameters[partition] = best_rice_parameter;
3189 bits_ += best_partition_bits;
3197 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
3202 for(i = 0; i < samples && !(x&1); i++)
3209 for(shift = 0; !(x&1); shift++)
3214 for(i = 0; i < samples; i++)
3215 signal[i] >>= shift;
3221 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3225 for(channel = 0; channel < channels; channel++)
3226 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3228 fifo->tail += wide_samples;
3230 FLAC__ASSERT(fifo->tail <= fifo->size);
3233 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3236 unsigned sample, wide_sample;
3237 unsigned tail = fifo->tail;
3239 sample = input_offset * channels;
3240 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3241 for(channel = 0; channel < channels; channel++)
3242 fifo->data[channel][tail] = input[sample++];
3247 FLAC__ASSERT(fifo->tail <= fifo->size);
3250 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3252 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3253 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3256 if(encoder->private_->verify.needs_magic_hack) {
3257 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3258 *bytes = FLAC__STREAM_SYNC_LENGTH;
3259 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3260 encoder->private_->verify.needs_magic_hack = false;
3263 if(encoded_bytes == 0) {
3265 * If we get here, a FIFO underflow has occurred,
3266 * which means there is a bug somewhere.
3269 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3271 else if(encoded_bytes < *bytes)
3272 *bytes = encoded_bytes;
3273 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3274 encoder->private_->verify.output.data += *bytes;
3275 encoder->private_->verify.output.bytes -= *bytes;
3278 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3281 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3283 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3285 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3286 const unsigned blocksize = frame->header.blocksize;
3287 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3289 for(channel = 0; channel < channels; channel++) {
3290 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3291 unsigned i, sample = 0;
3292 FLAC__int32 expect = 0, got = 0;
3294 for(i = 0; i < blocksize; i++) {
3295 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3297 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3298 got = (FLAC__int32)buffer[channel][i];
3302 FLAC__ASSERT(i < blocksize);
3303 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3304 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
3305 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
3306 encoder->private_->verify.error_stats.channel = channel;
3307 encoder->private_->verify.error_stats.sample = sample;
3308 encoder->private_->verify.error_stats.expected = expect;
3309 encoder->private_->verify.error_stats.got = got;
3310 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3311 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3314 /* dequeue the frame from the fifo */
3315 for(channel = 0; channel < channels; channel++) {
3316 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3318 encoder->private_->verify.input_fifo.tail -= blocksize;
3319 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3322 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3324 (void)decoder, (void)metadata, (void)client_data;
3327 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3329 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3330 (void)decoder, (void)status;
3331 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;