1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 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.
33 #undef WINDOW_DEBUG_OUTPUT
37 #include <stdlib.h> /* for malloc() */
38 #include <string.h> /* for memcpy() */
39 #include "FLAC/assert.h"
40 #include "FLAC/stream_decoder.h"
41 #include "protected/stream_encoder.h"
42 #include "private/bitbuffer.h"
43 #include "private/bitmath.h"
44 #include "private/crc.h"
45 #include "private/cpu.h"
46 #include "private/fixed.h"
47 #include "private/format.h"
48 #include "private/lpc.h"
49 #include "private/md5.h"
50 #include "private/memory.h"
51 #include "private/stream_encoder_framing.h"
52 #include "private/window.h"
61 #define min(x,y) ((x)<(y)?(x):(y))
66 #define max(x,y) ((x)>(y)?(x):(y))
69 FLAC__int32 *data[FLAC__MAX_CHANNELS];
70 unsigned size; /* of each data[] in samples */
75 const FLAC__byte *data;
82 ENCODER_IN_METADATA = 1,
86 /***********************************************************************
88 * Private class method prototypes
90 ***********************************************************************/
92 static void set_defaults_(FLAC__StreamEncoder *encoder);
93 static void free_(FLAC__StreamEncoder *encoder);
94 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
95 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
96 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
97 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
99 static FLAC__bool process_subframe_(
100 FLAC__StreamEncoder *encoder,
101 unsigned min_partition_order,
102 unsigned max_partition_order,
103 FLAC__bool precompute_partition_sums,
104 const FLAC__FrameHeader *frame_header,
105 unsigned subframe_bps,
106 const FLAC__int32 integer_signal[],
107 #ifndef FLAC__INTEGER_ONLY_LIBRARY
108 const FLAC__real real_signal[],
110 FLAC__Subframe *subframe[2],
111 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
112 FLAC__int32 *residual[2],
113 unsigned *best_subframe,
115 #ifdef WINDOW_DEBUG_OUTPUT
116 ,unsigned subframe_number
120 static FLAC__bool add_subframe_(
121 FLAC__StreamEncoder *encoder,
122 const FLAC__FrameHeader *frame_header,
123 unsigned subframe_bps,
124 const FLAC__Subframe *subframe,
125 FLAC__BitBuffer *frame
126 #ifdef WINDOW_DEBUG_OUTPUT
127 ,unsigned subframe_bits
131 static unsigned evaluate_constant_subframe_(
132 const FLAC__int32 signal,
133 unsigned subframe_bps,
134 FLAC__Subframe *subframe
137 static unsigned evaluate_fixed_subframe_(
138 FLAC__StreamEncoder *encoder,
139 const FLAC__int32 signal[],
140 FLAC__int32 residual[],
141 FLAC__uint32 abs_residual[],
142 FLAC__uint64 abs_residual_partition_sums[],
143 unsigned raw_bits_per_partition[],
145 unsigned subframe_bps,
147 unsigned rice_parameter,
148 unsigned min_partition_order,
149 unsigned max_partition_order,
150 FLAC__bool precompute_partition_sums,
151 FLAC__bool do_escape_coding,
152 unsigned rice_parameter_search_dist,
153 FLAC__Subframe *subframe,
154 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
157 #ifndef FLAC__INTEGER_ONLY_LIBRARY
158 static unsigned evaluate_lpc_subframe_(
159 FLAC__StreamEncoder *encoder,
160 const FLAC__int32 signal[],
161 FLAC__int32 residual[],
162 FLAC__uint32 abs_residual[],
163 FLAC__uint64 abs_residual_partition_sums[],
164 unsigned raw_bits_per_partition[],
165 const FLAC__real lp_coeff[],
167 unsigned subframe_bps,
169 unsigned qlp_coeff_precision,
170 unsigned rice_parameter,
171 unsigned min_partition_order,
172 unsigned max_partition_order,
173 FLAC__bool precompute_partition_sums,
174 FLAC__bool do_escape_coding,
175 unsigned rice_parameter_search_dist,
176 FLAC__Subframe *subframe,
177 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
178 #ifdef WINDOW_DEBUG_OUTPUT
179 ,unsigned frame_number
180 ,unsigned subframe_number
181 ,FLAC__ApodizationSpecification aspec
186 static unsigned evaluate_verbatim_subframe_(
187 const FLAC__int32 signal[],
189 unsigned subframe_bps,
190 FLAC__Subframe *subframe
193 static unsigned find_best_partition_order_(
194 struct FLAC__StreamEncoderPrivate *private_,
195 const FLAC__int32 residual[],
196 FLAC__uint32 abs_residual[],
197 FLAC__uint64 abs_residual_partition_sums[],
198 unsigned raw_bits_per_partition[],
199 unsigned residual_samples,
200 unsigned predictor_order,
201 unsigned rice_parameter,
202 unsigned min_partition_order,
203 unsigned max_partition_order,
204 FLAC__bool precompute_partition_sums,
205 FLAC__bool do_escape_coding,
206 unsigned rice_parameter_search_dist,
207 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
210 static void precompute_partition_info_sums_(
211 const FLAC__uint32 abs_residual[],
212 FLAC__uint64 abs_residual_partition_sums[],
213 unsigned residual_samples,
214 unsigned predictor_order,
215 unsigned min_partition_order,
216 unsigned max_partition_order
219 static void precompute_partition_info_escapes_(
220 const FLAC__int32 residual[],
221 unsigned raw_bits_per_partition[],
222 unsigned residual_samples,
223 unsigned predictor_order,
224 unsigned min_partition_order,
225 unsigned max_partition_order
228 #ifdef DONT_ESTIMATE_RICE_BITS
229 static FLAC__bool set_partitioned_rice_(
230 const FLAC__uint32 abs_residual[],
231 const FLAC__int32 residual[],
232 const unsigned residual_samples,
233 const unsigned predictor_order,
234 const unsigned suggested_rice_parameter,
235 const unsigned rice_parameter_search_dist,
236 const unsigned partition_order,
237 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
241 static FLAC__bool set_partitioned_rice_with_precompute_(
242 const FLAC__int32 residual[],
243 const FLAC__uint64 abs_residual_partition_sums[],
244 const unsigned raw_bits_per_partition[],
245 const unsigned residual_samples,
246 const unsigned predictor_order,
247 const unsigned suggested_rice_parameter,
248 const unsigned rice_parameter_search_dist,
249 const unsigned partition_order,
250 const FLAC__bool search_for_escapes,
251 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
255 static FLAC__bool set_partitioned_rice_(
256 const FLAC__uint32 abs_residual[],
257 const unsigned residual_samples,
258 const unsigned predictor_order,
259 const unsigned suggested_rice_parameter,
260 const unsigned rice_parameter_search_dist,
261 const unsigned partition_order,
262 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
266 static FLAC__bool set_partitioned_rice_with_precompute_(
267 const FLAC__uint32 abs_residual[],
268 const FLAC__uint64 abs_residual_partition_sums[],
269 const unsigned raw_bits_per_partition[],
270 const unsigned residual_samples,
271 const unsigned predictor_order,
272 const unsigned suggested_rice_parameter,
273 const unsigned rice_parameter_search_dist,
274 const unsigned partition_order,
275 const FLAC__bool search_for_escapes,
276 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
281 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
283 /* verify-related routines: */
284 static void append_to_verify_fifo_(
285 verify_input_fifo *fifo,
286 const FLAC__int32 * const input[],
287 unsigned input_offset,
289 unsigned wide_samples
292 static void append_to_verify_fifo_interleaved_(
293 verify_input_fifo *fifo,
294 const FLAC__int32 input[],
295 unsigned input_offset,
297 unsigned wide_samples
300 static FLAC__StreamDecoderReadStatus verify_read_callback_(
301 const FLAC__StreamDecoder *decoder,
307 static FLAC__StreamDecoderWriteStatus verify_write_callback_(
308 const FLAC__StreamDecoder *decoder,
309 const FLAC__Frame *frame,
310 const FLAC__int32 * const buffer[],
314 static void verify_metadata_callback_(
315 const FLAC__StreamDecoder *decoder,
316 const FLAC__StreamMetadata *metadata,
320 static void verify_error_callback_(
321 const FLAC__StreamDecoder *decoder,
322 FLAC__StreamDecoderErrorStatus status,
326 #ifdef WINDOW_DEBUG_OUTPUT
327 static const char * const winstr[] = {
331 "blackman_harris_4term_92db_sidelobe",
346 /***********************************************************************
350 ***********************************************************************/
352 typedef struct FLAC__StreamEncoderPrivate {
353 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
354 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
355 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
356 #ifndef FLAC__INTEGER_ONLY_LIBRARY
357 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
358 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
359 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
360 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
362 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
363 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
364 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
365 FLAC__int32 *residual_workspace_mid_side[2][2];
366 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
367 FLAC__Subframe subframe_workspace_mid_side[2][2];
368 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
369 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
370 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
371 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
372 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
373 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
374 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
375 unsigned best_subframe_mid_side[2];
376 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
377 unsigned best_subframe_bits_mid_side[2];
378 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
379 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
380 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
381 FLAC__BitBuffer *frame; /* the current frame being worked on */
382 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
383 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
384 FLAC__ChannelAssignment last_channel_assignment;
385 FLAC__StreamMetadata metadata;
386 unsigned current_sample_number;
387 unsigned current_frame_number;
388 struct FLAC__MD5Context md5context;
389 FLAC__CPUInfo cpuinfo;
390 #ifndef FLAC__INTEGER_ONLY_LIBRARY
391 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
393 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
395 #ifndef FLAC__INTEGER_ONLY_LIBRARY
396 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
397 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[]);
398 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[]);
399 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[]);
401 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
402 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
403 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
404 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
405 FLAC__bool disable_constant_subframes;
406 FLAC__bool disable_fixed_subframes;
407 FLAC__bool disable_verbatim_subframes;
408 FLAC__StreamEncoderWriteCallback write_callback;
409 FLAC__StreamEncoderMetadataCallback metadata_callback;
411 /* unaligned (original) pointers to allocated data */
412 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
413 FLAC__int32 *integer_signal_mid_side_unaligned[2];
414 #ifndef FLAC__INTEGER_ONLY_LIBRARY
415 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
416 FLAC__real *real_signal_mid_side_unaligned[2];
417 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
418 FLAC__real *windowed_signal_unaligned;
420 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
421 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
422 FLAC__uint32 *abs_residual_unaligned;
423 FLAC__uint64 *abs_residual_partition_sums_unaligned;
424 unsigned *raw_bits_per_partition_unaligned;
426 * These fields have been moved here from private function local
427 * declarations merely to save stack space during encoding.
429 #ifndef FLAC__INTEGER_ONLY_LIBRARY
430 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
432 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
434 * The data for the verify section
437 FLAC__StreamDecoder *decoder;
438 EncoderStateHint state_hint;
439 FLAC__bool needs_magic_hack;
440 verify_input_fifo input_fifo;
441 verify_output output;
443 FLAC__uint64 absolute_sample;
444 unsigned frame_number;
447 FLAC__int32 expected;
451 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
452 } FLAC__StreamEncoderPrivate;
454 /***********************************************************************
456 * Public static class data
458 ***********************************************************************/
460 FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
461 "FLAC__STREAM_ENCODER_OK",
462 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
463 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
464 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
465 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
466 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
467 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
468 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
469 "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
470 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
471 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
472 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
473 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
474 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
475 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
476 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
477 "FLAC__STREAM_ENCODER_INVALID_METADATA",
478 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
479 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
480 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
481 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
482 "FLAC__STREAM_ENCODER_UNINITIALIZED"
485 FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
486 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
487 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
490 /***********************************************************************
492 * Class constructor/destructor
495 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
497 FLAC__StreamEncoder *encoder;
500 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
502 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
507 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
508 if(encoder->protected_ == 0) {
513 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
514 if(encoder->private_ == 0) {
515 free(encoder->protected_);
520 encoder->private_->frame = FLAC__bitbuffer_new();
521 if(encoder->private_->frame == 0) {
522 free(encoder->private_);
523 free(encoder->protected_);
528 set_defaults_(encoder);
530 encoder->private_->is_being_deleted = false;
532 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
533 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
534 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
536 for(i = 0; i < 2; i++) {
537 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
538 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
540 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
541 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
542 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
544 for(i = 0; i < 2; i++) {
545 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
546 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
549 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
550 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
551 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
553 for(i = 0; i < 2; i++) {
554 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
555 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
557 for(i = 0; i < 2; i++)
558 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
560 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
565 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
569 FLAC__ASSERT(0 != encoder);
570 FLAC__ASSERT(0 != encoder->protected_);
571 FLAC__ASSERT(0 != encoder->private_);
572 FLAC__ASSERT(0 != encoder->private_->frame);
574 encoder->private_->is_being_deleted = true;
576 FLAC__stream_encoder_finish(encoder);
578 if(0 != encoder->private_->verify.decoder)
579 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
581 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
582 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
583 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
585 for(i = 0; i < 2; i++) {
586 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
587 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
589 for(i = 0; i < 2; i++)
590 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
592 FLAC__bitbuffer_delete(encoder->private_->frame);
593 free(encoder->private_);
594 free(encoder->protected_);
598 /***********************************************************************
600 * Public class methods
602 ***********************************************************************/
604 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
607 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
609 FLAC__ASSERT(0 != encoder);
611 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
612 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
614 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
616 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
617 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
619 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
620 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
622 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
623 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
625 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
626 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
628 if(encoder->protected_->bits_per_sample >= 32)
629 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
631 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
632 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
634 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
635 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
637 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
638 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
640 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
641 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
643 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
644 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
646 if(encoder->protected_->qlp_coeff_precision == 0) {
647 if(encoder->protected_->bits_per_sample < 16) {
648 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
649 /* @@@ until then we'll make a guess */
650 encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
652 else if(encoder->protected_->bits_per_sample == 16) {
653 if(encoder->protected_->blocksize <= 192)
654 encoder->protected_->qlp_coeff_precision = 7;
655 else if(encoder->protected_->blocksize <= 384)
656 encoder->protected_->qlp_coeff_precision = 8;
657 else if(encoder->protected_->blocksize <= 576)
658 encoder->protected_->qlp_coeff_precision = 9;
659 else if(encoder->protected_->blocksize <= 1152)
660 encoder->protected_->qlp_coeff_precision = 10;
661 else if(encoder->protected_->blocksize <= 2304)
662 encoder->protected_->qlp_coeff_precision = 11;
663 else if(encoder->protected_->blocksize <= 4608)
664 encoder->protected_->qlp_coeff_precision = 12;
666 encoder->protected_->qlp_coeff_precision = 13;
669 if(encoder->protected_->blocksize <= 384)
670 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
671 else if(encoder->protected_->blocksize <= 1152)
672 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
674 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
676 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
678 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
679 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
681 if(encoder->protected_->streamable_subset) {
683 encoder->protected_->blocksize != 192 &&
684 encoder->protected_->blocksize != 576 &&
685 encoder->protected_->blocksize != 1152 &&
686 encoder->protected_->blocksize != 2304 &&
687 encoder->protected_->blocksize != 4608 &&
688 encoder->protected_->blocksize != 256 &&
689 encoder->protected_->blocksize != 512 &&
690 encoder->protected_->blocksize != 1024 &&
691 encoder->protected_->blocksize != 2048 &&
692 encoder->protected_->blocksize != 4096 &&
693 encoder->protected_->blocksize != 8192 &&
694 encoder->protected_->blocksize != 16384
696 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
698 encoder->protected_->sample_rate != 8000 &&
699 encoder->protected_->sample_rate != 16000 &&
700 encoder->protected_->sample_rate != 22050 &&
701 encoder->protected_->sample_rate != 24000 &&
702 encoder->protected_->sample_rate != 32000 &&
703 encoder->protected_->sample_rate != 44100 &&
704 encoder->protected_->sample_rate != 48000 &&
705 encoder->protected_->sample_rate != 96000
707 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
709 encoder->protected_->bits_per_sample != 8 &&
710 encoder->protected_->bits_per_sample != 12 &&
711 encoder->protected_->bits_per_sample != 16 &&
712 encoder->protected_->bits_per_sample != 20 &&
713 encoder->protected_->bits_per_sample != 24
715 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
716 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
717 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
720 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
721 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
722 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
723 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
725 /* validate metadata */
726 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
727 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
728 metadata_has_seektable = false;
729 metadata_has_vorbis_comment = false;
730 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
731 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
732 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
733 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
734 if(metadata_has_seektable) /* only one is allowed */
735 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
736 metadata_has_seektable = true;
737 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
738 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
740 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
741 if(metadata_has_vorbis_comment) /* only one is allowed */
742 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
743 metadata_has_vorbis_comment = true;
745 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
746 if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
747 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
751 encoder->private_->input_capacity = 0;
752 for(i = 0; i < encoder->protected_->channels; i++) {
753 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
754 #ifndef FLAC__INTEGER_ONLY_LIBRARY
755 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
758 for(i = 0; i < 2; i++) {
759 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
760 #ifndef FLAC__INTEGER_ONLY_LIBRARY
761 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
764 #ifndef FLAC__INTEGER_ONLY_LIBRARY
765 for(i = 0; i < encoder->protected_->num_apodizations; i++)
766 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
767 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
769 for(i = 0; i < encoder->protected_->channels; i++) {
770 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
771 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
772 encoder->private_->best_subframe[i] = 0;
774 for(i = 0; i < 2; i++) {
775 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
776 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
777 encoder->private_->best_subframe_mid_side[i] = 0;
779 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
780 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
781 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
782 #ifndef FLAC__INTEGER_ONLY_LIBRARY
783 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
785 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
786 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply÷ by hand */
787 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
788 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
789 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
790 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
791 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);
793 if(encoder->private_->loose_mid_side_stereo_frames == 0)
794 encoder->private_->loose_mid_side_stereo_frames = 1;
795 encoder->private_->loose_mid_side_stereo_frame_count = 0;
796 encoder->private_->current_sample_number = 0;
797 encoder->private_->current_frame_number = 0;
799 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
800 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? */
801 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
804 * get the CPU info and set the function pointers
806 FLAC__cpu_info(&encoder->private_->cpuinfo);
807 /* first default to the non-asm routines */
808 #ifndef FLAC__INTEGER_ONLY_LIBRARY
809 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
811 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
812 #ifndef FLAC__INTEGER_ONLY_LIBRARY
813 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
814 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
815 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
817 /* now override with asm where appropriate */
818 #ifndef FLAC__INTEGER_ONLY_LIBRARY
819 # ifndef FLAC__NO_ASM
820 if(encoder->private_->cpuinfo.use_asm) {
821 # ifdef FLAC__CPU_IA32
822 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
823 # ifdef FLAC__HAS_NASM
825 if(encoder->private_->cpuinfo.data.ia32.sse) {
826 if(encoder->protected_->max_lpc_order < 4)
827 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
828 else if(encoder->protected_->max_lpc_order < 8)
829 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
830 else if(encoder->protected_->max_lpc_order < 12)
831 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
833 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
836 # endif /* FLAC__SSE_OS */
837 if(encoder->private_->cpuinfo.data.ia32._3dnow)
838 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
840 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
841 if(encoder->private_->cpuinfo.data.ia32.mmx) {
842 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
843 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
846 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
847 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
849 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
850 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
851 # endif /* FLAC__HAS_NASM */
852 # endif /* FLAC__CPU_IA32 */
854 # endif /* !FLAC__NO_ASM */
855 #endif /* !FLAC__INTEGER_ONLY_LIBRARY */
856 /* finally override based on wide-ness if necessary */
857 if(encoder->private_->use_wide_by_block) {
858 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
861 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
862 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
864 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
865 /* the above function sets the state for us in case of an error */
866 return encoder->protected_->state;
869 if(!FLAC__bitbuffer_init(encoder->private_->frame))
870 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
873 * Set up the verify stuff if necessary
875 if(encoder->protected_->verify) {
877 * First, set up the fifo which will hold the
878 * original signal to compare against
880 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
881 for(i = 0; i < encoder->protected_->channels; i++) {
882 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
883 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
885 encoder->private_->verify.input_fifo.tail = 0;
888 * Now set up a stream decoder for verification
890 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
891 if(0 == encoder->private_->verify.decoder)
892 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
894 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
895 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
896 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
897 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
898 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
899 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
900 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
902 encoder->private_->verify.error_stats.absolute_sample = 0;
903 encoder->private_->verify.error_stats.frame_number = 0;
904 encoder->private_->verify.error_stats.channel = 0;
905 encoder->private_->verify.error_stats.sample = 0;
906 encoder->private_->verify.error_stats.expected = 0;
907 encoder->private_->verify.error_stats.got = 0;
910 * write the stream header
912 if(encoder->protected_->verify)
913 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
914 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
915 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
916 if(!write_bitbuffer_(encoder, 0)) {
917 /* the above function sets the state for us in case of an error */
918 return encoder->protected_->state;
922 * write the STREAMINFO metadata block
924 if(encoder->protected_->verify)
925 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
926 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
927 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
928 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
929 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
930 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
931 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
932 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
933 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
934 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
935 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
936 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
937 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
938 FLAC__MD5Init(&encoder->private_->md5context);
939 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
940 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
941 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
942 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
943 if(!write_bitbuffer_(encoder, 0)) {
944 /* the above function sets the state for us in case of an error */
945 return encoder->protected_->state;
949 * Now that the STREAMINFO block is written, we can init this to an
950 * absurdly-high value...
952 encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
953 /* ... and clear this to 0 */
954 encoder->private_->metadata.data.stream_info.total_samples = 0;
957 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
958 * if not, we will write an empty one (FLAC__add_metadata_block()
959 * automatically supplies the vendor string).
961 * WATCHOUT: libOggFLAC depends on us to write this block after the
962 * STREAMINFO since that's what the mapping requires. (In the case
963 * that metadata_has_vorbis_comment is true it will have already
964 * insured that the metadata list is properly ordered.)
966 if(!metadata_has_vorbis_comment) {
967 FLAC__StreamMetadata vorbis_comment;
968 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
969 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
970 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
971 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
972 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
973 vorbis_comment.data.vorbis_comment.num_comments = 0;
974 vorbis_comment.data.vorbis_comment.comments = 0;
975 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
976 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
977 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
978 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
979 if(!write_bitbuffer_(encoder, 0)) {
980 /* the above function sets the state for us in case of an error */
981 return encoder->protected_->state;
986 * write the user's metadata blocks
988 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
989 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
990 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
991 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
992 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
993 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
994 if(!write_bitbuffer_(encoder, 0)) {
995 /* the above function sets the state for us in case of an error */
996 return encoder->protected_->state;
1000 if(encoder->protected_->verify)
1001 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1003 return encoder->protected_->state;
1006 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
1008 FLAC__ASSERT(0 != encoder);
1010 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
1013 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
1014 if(encoder->private_->current_sample_number != 0) {
1015 encoder->protected_->blocksize = encoder->private_->current_sample_number;
1016 process_frame_(encoder, true); /* true => is last frame */
1020 FLAC__MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
1022 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
1023 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
1026 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
1027 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
1030 set_defaults_(encoder);
1032 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1035 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
1037 FLAC__ASSERT(0 != encoder);
1038 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1040 #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1041 encoder->protected_->verify = value;
1046 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
1048 FLAC__ASSERT(0 != encoder);
1049 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1051 encoder->protected_->streamable_subset = value;
1055 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1057 FLAC__ASSERT(0 != encoder);
1058 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1060 encoder->protected_->do_mid_side_stereo = value;
1064 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1066 FLAC__ASSERT(0 != encoder);
1067 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1069 encoder->protected_->loose_mid_side_stereo = value;
1073 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
1075 FLAC__ASSERT(0 != encoder);
1076 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1078 encoder->protected_->channels = value;
1082 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
1084 FLAC__ASSERT(0 != encoder);
1085 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1087 encoder->protected_->bits_per_sample = value;
1091 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
1093 FLAC__ASSERT(0 != encoder);
1094 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1096 encoder->protected_->sample_rate = value;
1100 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1102 FLAC__ASSERT(0 != encoder);
1103 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1105 encoder->protected_->blocksize = value;
1109 FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1111 FLAC__ASSERT(0 != encoder);
1112 FLAC__ASSERT(0 != specification);
1113 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1115 #ifdef FLAC__INTEGER_ONLY_LIBRARY
1116 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1118 encoder->protected_->num_apodizations = 0;
1120 const char *s = strchr(specification, ';');
1121 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1122 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1123 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1124 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1125 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1126 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1127 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1128 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1129 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1130 else if(n==6 && 0 == strncmp("connes" , specification, n))
1131 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1132 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1133 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1134 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1135 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1136 if (stddev > 0.0 && stddev <= 0.5) {
1137 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1138 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1141 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1142 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1143 else if(n==4 && 0 == strncmp("hann" , specification, n))
1144 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1145 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1146 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1147 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1148 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1149 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1150 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1151 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1152 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1153 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1154 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1155 if (p >= 0.0 && p <= 1.0) {
1156 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1157 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1160 else if(n==5 && 0 == strncmp("welch" , specification, n))
1161 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1162 if (encoder->protected_->num_apodizations == 32)
1165 specification = s+1;
1169 if(encoder->protected_->num_apodizations == 0) {
1170 encoder->protected_->num_apodizations = 1;
1171 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1172 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1174 #ifdef WINDOW_DEBUG_OUTPUT
1175 {unsigned n;for(n=0;n<encoder->protected_->num_apodizations;n++)fprintf(stderr,"@@@@@@ parsed apodization[%zu]: %s\n",n,winstr[encoder->protected_->apodizations[n].type]);}
1181 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1183 FLAC__ASSERT(0 != encoder);
1184 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1186 encoder->protected_->max_lpc_order = value;
1190 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1192 FLAC__ASSERT(0 != encoder);
1193 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1195 encoder->protected_->qlp_coeff_precision = value;
1199 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1201 FLAC__ASSERT(0 != encoder);
1202 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1204 encoder->protected_->do_qlp_coeff_prec_search = value;
1208 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1210 FLAC__ASSERT(0 != encoder);
1211 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1214 /*@@@ deprecated: */
1215 encoder->protected_->do_escape_coding = value;
1222 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1224 FLAC__ASSERT(0 != encoder);
1225 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1227 encoder->protected_->do_exhaustive_model_search = value;
1231 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1233 FLAC__ASSERT(0 != encoder);
1234 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1236 encoder->protected_->min_residual_partition_order = value;
1240 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1242 FLAC__ASSERT(0 != encoder);
1243 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1245 encoder->protected_->max_residual_partition_order = value;
1249 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1251 FLAC__ASSERT(0 != encoder);
1252 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1255 /*@@@ deprecated: */
1256 encoder->protected_->rice_parameter_search_dist = value;
1263 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1265 FLAC__ASSERT(0 != encoder);
1266 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1268 encoder->protected_->total_samples_estimate = value;
1272 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1274 FLAC__ASSERT(0 != encoder);
1275 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1277 encoder->protected_->metadata = metadata;
1278 encoder->protected_->num_metadata_blocks = num_blocks;
1282 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
1284 FLAC__ASSERT(0 != encoder);
1285 FLAC__ASSERT(0 != value);
1286 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1288 encoder->private_->write_callback = value;
1292 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
1294 FLAC__ASSERT(0 != encoder);
1295 FLAC__ASSERT(0 != value);
1296 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1298 encoder->private_->metadata_callback = value;
1302 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
1304 FLAC__ASSERT(0 != encoder);
1305 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1307 encoder->private_->client_data = value;
1312 * These three functions are not static, but not publically exposed in
1313 * include/FLAC/ either. They are used by the test suite.
1315 FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1317 FLAC__ASSERT(0 != encoder);
1318 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1320 encoder->private_->disable_constant_subframes = value;
1324 FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1326 FLAC__ASSERT(0 != encoder);
1327 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1329 encoder->private_->disable_fixed_subframes = value;
1333 FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1335 FLAC__ASSERT(0 != encoder);
1336 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1338 encoder->private_->disable_verbatim_subframes = value;
1342 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1344 FLAC__ASSERT(0 != encoder);
1345 return encoder->protected_->state;
1348 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1350 FLAC__ASSERT(0 != encoder);
1351 if(encoder->protected_->verify)
1352 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1354 return FLAC__STREAM_DECODER_UNINITIALIZED;
1357 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1359 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1360 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1362 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
1365 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)
1367 FLAC__ASSERT(0 != encoder);
1368 if(0 != absolute_sample)
1369 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1370 if(0 != frame_number)
1371 *frame_number = encoder->private_->verify.error_stats.frame_number;
1373 *channel = encoder->private_->verify.error_stats.channel;
1375 *sample = encoder->private_->verify.error_stats.sample;
1377 *expected = encoder->private_->verify.error_stats.expected;
1379 *got = encoder->private_->verify.error_stats.got;
1382 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1384 FLAC__ASSERT(0 != encoder);
1385 return encoder->protected_->verify;
1388 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1390 FLAC__ASSERT(0 != encoder);
1391 return encoder->protected_->streamable_subset;
1394 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1396 FLAC__ASSERT(0 != encoder);
1397 return encoder->protected_->do_mid_side_stereo;
1400 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1402 FLAC__ASSERT(0 != encoder);
1403 return encoder->protected_->loose_mid_side_stereo;
1406 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1408 FLAC__ASSERT(0 != encoder);
1409 return encoder->protected_->channels;
1412 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1414 FLAC__ASSERT(0 != encoder);
1415 return encoder->protected_->bits_per_sample;
1418 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1420 FLAC__ASSERT(0 != encoder);
1421 return encoder->protected_->sample_rate;
1424 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1426 FLAC__ASSERT(0 != encoder);
1427 return encoder->protected_->blocksize;
1430 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1432 FLAC__ASSERT(0 != encoder);
1433 return encoder->protected_->max_lpc_order;
1436 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1438 FLAC__ASSERT(0 != encoder);
1439 return encoder->protected_->qlp_coeff_precision;
1442 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1444 FLAC__ASSERT(0 != encoder);
1445 return encoder->protected_->do_qlp_coeff_prec_search;
1448 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1450 FLAC__ASSERT(0 != encoder);
1451 return encoder->protected_->do_escape_coding;
1454 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1456 FLAC__ASSERT(0 != encoder);
1457 return encoder->protected_->do_exhaustive_model_search;
1460 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1462 FLAC__ASSERT(0 != encoder);
1463 return encoder->protected_->min_residual_partition_order;
1466 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1468 FLAC__ASSERT(0 != encoder);
1469 return encoder->protected_->max_residual_partition_order;
1472 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1474 FLAC__ASSERT(0 != encoder);
1475 return encoder->protected_->rice_parameter_search_dist;
1478 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1480 FLAC__ASSERT(0 != encoder);
1481 return encoder->protected_->total_samples_estimate;
1484 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1486 unsigned i, j, channel;
1487 FLAC__int32 x, mid, side;
1488 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1490 FLAC__ASSERT(0 != encoder);
1491 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1495 * we have several flavors of the same basic loop, optimized for
1496 * different conditions:
1498 if(encoder->protected_->max_lpc_order > 0) {
1499 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1501 * stereo coding: unroll channel loop
1502 * with LPC: calculate floating point version of signal
1505 if(encoder->protected_->verify)
1506 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1508 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1509 x = mid = side = buffer[0][j];
1510 encoder->private_->integer_signal[0][i] = x;
1511 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1512 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1515 encoder->private_->integer_signal[1][i] = x;
1516 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1517 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1521 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1522 encoder->private_->integer_signal_mid_side[1][i] = side;
1523 encoder->private_->integer_signal_mid_side[0][i] = mid;
1524 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1525 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1526 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1528 encoder->private_->current_sample_number++;
1530 if(i == blocksize) {
1531 if(!process_frame_(encoder, false)) /* false => not last frame */
1534 } while(j < samples);
1538 * independent channel coding: buffer each channel in inner loop
1539 * with LPC: calculate floating point version of signal
1542 if(encoder->protected_->verify)
1543 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1545 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1546 for(channel = 0; channel < channels; channel++) {
1547 x = buffer[channel][j];
1548 encoder->private_->integer_signal[channel][i] = x;
1549 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1550 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1553 encoder->private_->current_sample_number++;
1555 if(i == blocksize) {
1556 if(!process_frame_(encoder, false)) /* false => not last frame */
1559 } while(j < samples);
1563 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1565 * stereo coding: unroll channel loop
1566 * without LPC: no need to calculate floating point version of signal
1569 if(encoder->protected_->verify)
1570 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1572 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1573 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
1575 encoder->private_->integer_signal[1][i] = x;
1578 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1579 encoder->private_->integer_signal_mid_side[1][i] = side;
1580 encoder->private_->integer_signal_mid_side[0][i] = mid;
1581 encoder->private_->current_sample_number++;
1583 if(i == blocksize) {
1584 if(!process_frame_(encoder, false)) /* false => not last frame */
1587 } while(j < samples);
1591 * independent channel coding: buffer each channel in inner loop
1592 * without LPC: no need to calculate floating point version of signal
1595 if(encoder->protected_->verify)
1596 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1598 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1599 for(channel = 0; channel < channels; channel++)
1600 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
1601 encoder->private_->current_sample_number++;
1603 if(i == blocksize) {
1604 if(!process_frame_(encoder, false)) /* false => not last frame */
1607 } while(j < samples);
1614 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1616 unsigned i, j, k, channel;
1617 FLAC__int32 x, mid, side;
1618 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1620 FLAC__ASSERT(0 != encoder);
1621 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1625 * we have several flavors of the same basic loop, optimized for
1626 * different conditions:
1628 if(encoder->protected_->max_lpc_order > 0) {
1629 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1631 * stereo coding: unroll channel loop
1632 * with LPC: calculate floating point version of signal
1635 if(encoder->protected_->verify)
1636 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1638 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1639 x = mid = side = buffer[k++];
1640 encoder->private_->integer_signal[0][i] = x;
1641 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1642 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1645 encoder->private_->integer_signal[1][i] = x;
1646 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1647 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1651 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1652 encoder->private_->integer_signal_mid_side[1][i] = side;
1653 encoder->private_->integer_signal_mid_side[0][i] = mid;
1654 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1655 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1656 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1658 encoder->private_->current_sample_number++;
1660 if(i == blocksize) {
1661 if(!process_frame_(encoder, false)) /* false => not last frame */
1664 } while(j < samples);
1668 * independent channel coding: buffer each channel in inner loop
1669 * with LPC: calculate floating point version of signal
1672 if(encoder->protected_->verify)
1673 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1675 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1676 for(channel = 0; channel < channels; channel++) {
1678 encoder->private_->integer_signal[channel][i] = x;
1679 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1680 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1683 encoder->private_->current_sample_number++;
1685 if(i == blocksize) {
1686 if(!process_frame_(encoder, false)) /* false => not last frame */
1689 } while(j < samples);
1693 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1695 * stereo coding: unroll channel loop
1696 * without LPC: no need to calculate floating point version of signal
1699 if(encoder->protected_->verify)
1700 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1702 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1703 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
1705 encoder->private_->integer_signal[1][i] = x;
1708 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1709 encoder->private_->integer_signal_mid_side[1][i] = side;
1710 encoder->private_->integer_signal_mid_side[0][i] = mid;
1711 encoder->private_->current_sample_number++;
1713 if(i == blocksize) {
1714 if(!process_frame_(encoder, false)) /* false => not last frame */
1717 } while(j < samples);
1721 * independent channel coding: buffer each channel in inner loop
1722 * without LPC: no need to calculate floating point version of signal
1725 if(encoder->protected_->verify)
1726 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1728 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1729 for(channel = 0; channel < channels; channel++)
1730 encoder->private_->integer_signal[channel][i] = buffer[k++];
1731 encoder->private_->current_sample_number++;
1733 if(i == blocksize) {
1734 if(!process_frame_(encoder, false)) /* false => not last frame */
1737 } while(j < samples);
1744 /***********************************************************************
1746 * Private class methods
1748 ***********************************************************************/
1750 void set_defaults_(FLAC__StreamEncoder *encoder)
1752 FLAC__ASSERT(0 != encoder);
1754 #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1755 encoder->protected_->verify = true;
1757 encoder->protected_->verify = false;
1759 encoder->protected_->streamable_subset = true;
1760 encoder->protected_->do_mid_side_stereo = false;
1761 encoder->protected_->loose_mid_side_stereo = false;
1762 encoder->protected_->channels = 2;
1763 encoder->protected_->bits_per_sample = 16;
1764 encoder->protected_->sample_rate = 44100;
1765 encoder->protected_->blocksize = 1152;
1766 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1767 encoder->protected_->num_apodizations = 1;
1768 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1769 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1771 encoder->protected_->max_lpc_order = 0;
1772 encoder->protected_->qlp_coeff_precision = 0;
1773 encoder->protected_->do_qlp_coeff_prec_search = false;
1774 encoder->protected_->do_exhaustive_model_search = false;
1775 encoder->protected_->do_escape_coding = false;
1776 encoder->protected_->min_residual_partition_order = 0;
1777 encoder->protected_->max_residual_partition_order = 0;
1778 encoder->protected_->rice_parameter_search_dist = 0;
1779 encoder->protected_->total_samples_estimate = 0;
1780 encoder->protected_->metadata = 0;
1781 encoder->protected_->num_metadata_blocks = 0;
1783 encoder->private_->disable_constant_subframes = false;
1784 encoder->private_->disable_fixed_subframes = false;
1785 encoder->private_->disable_verbatim_subframes = false;
1786 encoder->private_->write_callback = 0;
1787 encoder->private_->metadata_callback = 0;
1788 encoder->private_->client_data = 0;
1791 void free_(FLAC__StreamEncoder *encoder)
1793 unsigned i, channel;
1795 FLAC__ASSERT(0 != encoder);
1796 for(i = 0; i < encoder->protected_->channels; i++) {
1797 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1798 free(encoder->private_->integer_signal_unaligned[i]);
1799 encoder->private_->integer_signal_unaligned[i] = 0;
1801 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1802 if(0 != encoder->private_->real_signal_unaligned[i]) {
1803 free(encoder->private_->real_signal_unaligned[i]);
1804 encoder->private_->real_signal_unaligned[i] = 0;
1808 for(i = 0; i < 2; i++) {
1809 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1810 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1811 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1813 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1814 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1815 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1816 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1820 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1821 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
1822 if(0 != encoder->private_->window_unaligned[i]) {
1823 free(encoder->private_->window_unaligned[i]);
1824 encoder->private_->window_unaligned[i] = 0;
1827 if(0 != encoder->private_->windowed_signal_unaligned) {
1828 free(encoder->private_->windowed_signal_unaligned);
1829 encoder->private_->windowed_signal_unaligned = 0;
1832 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1833 for(i = 0; i < 2; i++) {
1834 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1835 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1836 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1840 for(channel = 0; channel < 2; channel++) {
1841 for(i = 0; i < 2; i++) {
1842 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1843 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1844 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1848 if(0 != encoder->private_->abs_residual_unaligned) {
1849 free(encoder->private_->abs_residual_unaligned);
1850 encoder->private_->abs_residual_unaligned = 0;
1852 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1853 free(encoder->private_->abs_residual_partition_sums_unaligned);
1854 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1856 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1857 free(encoder->private_->raw_bits_per_partition_unaligned);
1858 encoder->private_->raw_bits_per_partition_unaligned = 0;
1860 if(encoder->protected_->verify) {
1861 for(i = 0; i < encoder->protected_->channels; i++) {
1862 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1863 free(encoder->private_->verify.input_fifo.data[i]);
1864 encoder->private_->verify.input_fifo.data[i] = 0;
1868 FLAC__bitbuffer_free(encoder->private_->frame);
1871 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
1874 unsigned i, channel;
1876 FLAC__ASSERT(new_size > 0);
1877 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1878 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1880 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1881 if(new_size <= encoder->private_->input_capacity)
1886 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1887 * requires that the input arrays (in our case the integer signals)
1888 * have a buffer of up to 3 zeroes in front (at negative indices) for
1889 * alignment purposes; we use 4 to keep the data well-aligned.
1892 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1893 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1894 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1895 if(encoder->protected_->max_lpc_order > 0)
1896 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1898 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1899 encoder->private_->integer_signal[i] += 4;
1901 for(i = 0; ok && i < 2; i++) {
1902 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]);
1903 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1904 if(encoder->protected_->max_lpc_order > 0)
1905 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]);
1907 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1908 encoder->private_->integer_signal_mid_side[i] += 4;
1910 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1911 if(ok && encoder->protected_->max_lpc_order > 0) {
1912 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
1913 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
1914 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
1917 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1918 for(i = 0; ok && i < 2; i++) {
1919 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1922 for(channel = 0; ok && channel < 2; channel++) {
1923 for(i = 0; ok && i < 2; i++) {
1924 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]);
1927 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1928 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 */
1929 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1930 if(encoder->protected_->do_escape_coding)
1931 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1934 encoder->private_->input_capacity = new_size;
1936 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1938 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1939 if(ok && encoder->protected_->max_lpc_order > 0) {
1940 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
1941 switch(encoder->protected_->apodizations[i].type) {
1942 case FLAC__APODIZATION_BARTLETT:
1943 FLAC__window_bartlett(encoder->private_->window[i], new_size);
1945 case FLAC__APODIZATION_BARTLETT_HANN:
1946 FLAC__window_bartlett_hann(encoder->private_->window[i], new_size);
1948 case FLAC__APODIZATION_BLACKMAN:
1949 FLAC__window_blackman(encoder->private_->window[i], new_size);
1951 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
1952 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_size);
1954 case FLAC__APODIZATION_CONNES:
1955 FLAC__window_connes(encoder->private_->window[i], new_size);
1957 case FLAC__APODIZATION_FLATTOP:
1958 FLAC__window_flattop(encoder->private_->window[i], new_size);
1960 case FLAC__APODIZATION_GAUSS:
1961 FLAC__window_gauss(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.gauss.stddev);
1963 case FLAC__APODIZATION_HAMMING:
1964 FLAC__window_hamming(encoder->private_->window[i], new_size);
1966 case FLAC__APODIZATION_HANN:
1967 FLAC__window_hann(encoder->private_->window[i], new_size);
1969 case FLAC__APODIZATION_KAISER_BESSEL:
1970 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_size);
1972 case FLAC__APODIZATION_NUTTALL:
1973 FLAC__window_nuttall(encoder->private_->window[i], new_size);
1975 case FLAC__APODIZATION_RECTANGLE:
1976 FLAC__window_rectangle(encoder->private_->window[i], new_size);
1978 case FLAC__APODIZATION_TRIANGLE:
1979 FLAC__window_triangle(encoder->private_->window[i], new_size);
1981 case FLAC__APODIZATION_TUKEY:
1982 FLAC__window_tukey(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.tukey.p);
1984 case FLAC__APODIZATION_WELCH:
1985 FLAC__window_welch(encoder->private_->window[i], new_size);
1989 /* double protection */
1990 FLAC__window_hann(encoder->private_->window[i], new_size);
2000 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
2002 const FLAC__byte *buffer;
2005 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2007 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2009 if(encoder->protected_->verify) {
2010 encoder->private_->verify.output.data = buffer;
2011 encoder->private_->verify.output.bytes = bytes;
2012 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2013 encoder->private_->verify.needs_magic_hack = true;
2016 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2017 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2018 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2019 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2025 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2026 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2027 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
2031 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2034 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
2035 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
2041 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
2043 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2046 * Accumulate raw signal to the MD5 signature
2048 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)) {
2049 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2054 * Process the frame header and subframes into the frame bitbuffer
2056 if(!process_subframes_(encoder, is_last_frame)) {
2057 /* the above function sets the state for us in case of an error */
2062 * Zero-pad the frame to a byte_boundary
2064 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
2065 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2070 * CRC-16 the whole thing
2072 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2073 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
2078 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
2079 /* the above function sets the state for us in case of an error */
2084 * Get ready for the next frame
2086 encoder->private_->current_sample_number = 0;
2087 encoder->private_->current_frame_number++;
2088 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
2093 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
2095 FLAC__FrameHeader frame_header;
2096 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
2097 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
2100 * Calculate the min,max Rice partition orders
2103 max_partition_order = 0;
2106 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
2107 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
2109 min_partition_order = min(min_partition_order, max_partition_order);
2111 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
2116 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
2117 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2120 frame_header.blocksize = encoder->protected_->blocksize;
2121 frame_header.sample_rate = encoder->protected_->sample_rate;
2122 frame_header.channels = encoder->protected_->channels;
2123 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
2124 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
2125 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
2126 frame_header.number.frame_number = encoder->private_->current_frame_number;
2129 * Figure out what channel assignments to try
2131 if(encoder->protected_->do_mid_side_stereo) {
2132 if(encoder->protected_->loose_mid_side_stereo) {
2133 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
2134 do_independent = true;
2138 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
2139 do_mid_side = !do_independent;
2143 do_independent = true;
2148 do_independent = true;
2149 do_mid_side = false;
2152 FLAC__ASSERT(do_independent || do_mid_side);
2155 * Check for wasted bits; set effective bps for each subframe
2157 if(do_independent) {
2158 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2159 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
2160 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
2161 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
2165 FLAC__ASSERT(encoder->protected_->channels == 2);
2166 for(channel = 0; channel < 2; channel++) {
2167 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
2168 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
2169 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
2174 * First do a normal encoding pass of each independent channel
2176 if(do_independent) {
2177 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2181 min_partition_order,
2182 max_partition_order,
2183 precompute_partition_sums,
2185 encoder->private_->subframe_bps[channel],
2186 encoder->private_->integer_signal[channel],
2187 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2188 encoder->private_->real_signal[channel],
2190 encoder->private_->subframe_workspace_ptr[channel],
2191 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
2192 encoder->private_->residual_workspace[channel],
2193 encoder->private_->best_subframe+channel,
2194 encoder->private_->best_subframe_bits+channel
2195 #ifdef WINDOW_DEBUG_OUTPUT
2205 * Now do mid and side channels if requested
2208 FLAC__ASSERT(encoder->protected_->channels == 2);
2210 for(channel = 0; channel < 2; channel++) {
2214 min_partition_order,
2215 max_partition_order,
2216 precompute_partition_sums,
2218 encoder->private_->subframe_bps_mid_side[channel],
2219 encoder->private_->integer_signal_mid_side[channel],
2220 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2221 encoder->private_->real_signal_mid_side[channel],
2223 encoder->private_->subframe_workspace_ptr_mid_side[channel],
2224 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
2225 encoder->private_->residual_workspace_mid_side[channel],
2226 encoder->private_->best_subframe_mid_side+channel,
2227 encoder->private_->best_subframe_bits_mid_side+channel
2228 #ifdef WINDOW_DEBUG_OUTPUT
2238 * Compose the frame bitbuffer
2241 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
2242 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
2243 FLAC__ChannelAssignment channel_assignment;
2244 #ifdef WINDOW_DEBUG_OUTPUT
2245 unsigned left_bits = 0, right_bits = 0;
2248 FLAC__ASSERT(encoder->protected_->channels == 2);
2250 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
2251 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
2254 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
2256 FLAC__ChannelAssignment ca;
2258 FLAC__ASSERT(do_independent && do_mid_side);
2260 /* We have to figure out which channel assignent results in the smallest frame */
2261 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
2262 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
2263 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
2264 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
2266 for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
2267 if(bits[ca] < min_bits) {
2268 min_bits = bits[ca];
2269 channel_assignment = ca;
2274 frame_header.channel_assignment = channel_assignment;
2276 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
2277 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2281 switch(channel_assignment) {
2282 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2283 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2284 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
2285 #ifdef WINDOW_DEBUG_OUTPUT
2286 left_bits = encoder->private_->best_subframe_bits [0];
2287 right_bits = encoder->private_->best_subframe_bits [1];
2290 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2291 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2292 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2293 #ifdef WINDOW_DEBUG_OUTPUT
2294 left_bits = encoder->private_->best_subframe_bits [0];
2295 right_bits = encoder->private_->best_subframe_bits_mid_side [1];
2298 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2299 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2300 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
2301 #ifdef WINDOW_DEBUG_OUTPUT
2302 left_bits = encoder->private_->best_subframe_bits_mid_side [1];
2303 right_bits = encoder->private_->best_subframe_bits [1];
2306 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2307 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
2308 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2309 #ifdef WINDOW_DEBUG_OUTPUT
2310 left_bits = encoder->private_->best_subframe_bits_mid_side [0];
2311 right_bits = encoder->private_->best_subframe_bits_mid_side [1];
2318 switch(channel_assignment) {
2319 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2320 left_bps = encoder->private_->subframe_bps [0];
2321 right_bps = encoder->private_->subframe_bps [1];
2323 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2324 left_bps = encoder->private_->subframe_bps [0];
2325 right_bps = encoder->private_->subframe_bps_mid_side[1];
2327 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2328 left_bps = encoder->private_->subframe_bps_mid_side[1];
2329 right_bps = encoder->private_->subframe_bps [1];
2331 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2332 left_bps = encoder->private_->subframe_bps_mid_side[0];
2333 right_bps = encoder->private_->subframe_bps_mid_side[1];
2339 /* note that encoder_add_subframe_ sets the state for us in case of an error */
2340 #ifdef WINDOW_DEBUG_OUTPUT
2341 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame, left_bits))
2343 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame, right_bits))
2346 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
2348 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
2353 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
2354 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2358 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2359 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame, encoder->private_->best_subframe_bits[channel])) {
2360 /* the above function sets the state for us in case of an error */
2366 if(encoder->protected_->loose_mid_side_stereo) {
2367 encoder->private_->loose_mid_side_stereo_frame_count++;
2368 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
2369 encoder->private_->loose_mid_side_stereo_frame_count = 0;
2372 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
2377 FLAC__bool process_subframe_(
2378 FLAC__StreamEncoder *encoder,
2379 unsigned min_partition_order,
2380 unsigned max_partition_order,
2381 FLAC__bool precompute_partition_sums,
2382 const FLAC__FrameHeader *frame_header,
2383 unsigned subframe_bps,
2384 const FLAC__int32 integer_signal[],
2385 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2386 const FLAC__real real_signal[],
2388 FLAC__Subframe *subframe[2],
2389 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
2390 FLAC__int32 *residual[2],
2391 unsigned *best_subframe,
2393 #ifdef WINDOW_DEBUG_OUTPUT
2394 ,unsigned subframe_number
2398 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2399 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
2401 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
2403 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2404 FLAC__double lpc_residual_bits_per_sample;
2405 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 */
2406 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
2407 unsigned min_lpc_order, max_lpc_order, lpc_order;
2408 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
2410 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
2411 unsigned rice_parameter;
2412 unsigned _candidate_bits, _best_bits;
2413 unsigned _best_subframe;
2415 /* verbatim subframe is the baseline against which we measure other compressed subframes */
2417 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
2418 _best_bits = UINT_MAX;
2420 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2422 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
2423 unsigned signal_is_constant = false;
2424 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);
2425 /* check for constant subframe */
2427 !encoder->private_->disable_constant_subframes &&
2428 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2429 fixed_residual_bits_per_sample[1] == 0.0
2431 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
2434 /* the above means it's possible all samples are the same value; now double-check it: */
2436 signal_is_constant = true;
2437 for(i = 1; i < frame_header->blocksize; i++) {
2438 if(integer_signal[0] != integer_signal[i]) {
2439 signal_is_constant = false;
2444 if(signal_is_constant) {
2445 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
2446 if(_candidate_bits < _best_bits) {
2447 _best_subframe = !_best_subframe;
2448 _best_bits = _candidate_bits;
2452 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
2454 if(encoder->protected_->do_exhaustive_model_search) {
2455 min_fixed_order = 0;
2456 max_fixed_order = FLAC__MAX_FIXED_ORDER;
2459 min_fixed_order = max_fixed_order = guess_fixed_order;
2461 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
2462 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2463 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
2464 continue; /* don't even try */
2465 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 */
2467 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
2468 continue; /* don't even try */
2469 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 */
2471 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2472 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2473 #ifdef DEBUG_VERBOSE
2474 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2476 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2479 evaluate_fixed_subframe_(
2482 residual[!_best_subframe],
2483 encoder->private_->abs_residual,
2484 encoder->private_->abs_residual_partition_sums,
2485 encoder->private_->raw_bits_per_partition,
2486 frame_header->blocksize,
2490 min_partition_order,
2491 max_partition_order,
2492 precompute_partition_sums,
2493 encoder->protected_->do_escape_coding,
2494 encoder->protected_->rice_parameter_search_dist,
2495 subframe[!_best_subframe],
2496 partitioned_rice_contents[!_best_subframe]
2498 if(_candidate_bits < _best_bits) {
2499 _best_subframe = !_best_subframe;
2500 _best_bits = _candidate_bits;
2505 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2507 if(encoder->protected_->max_lpc_order > 0) {
2508 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
2509 max_lpc_order = frame_header->blocksize-1;
2511 max_lpc_order = encoder->protected_->max_lpc_order;
2512 if(max_lpc_order > 0) {
2514 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
2515 FLAC__lpc_apply_apodization(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
2516 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2517 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2518 if(autoc[0] != 0.0) {
2519 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2520 if(encoder->protected_->do_exhaustive_model_search) {
2524 const unsigned guess_lpc_order =
2525 FLAC__lpc_compute_best_order(
2528 frame_header->blocksize,
2530 encoder->protected_->do_qlp_coeff_prec_search?
2531 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
2532 encoder->protected_->qlp_coeff_precision
2535 min_lpc_order = max_lpc_order = guess_lpc_order;
2537 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2538 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2539 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
2540 continue; /* don't even try */
2541 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2542 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2543 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2544 #ifdef DEBUG_VERBOSE
2545 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2547 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2549 if(encoder->protected_->do_qlp_coeff_prec_search) {
2550 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2551 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
2552 if(subframe_bps <= 17) {
2553 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2554 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
2557 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2560 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2562 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2564 evaluate_lpc_subframe_(
2567 residual[!_best_subframe],
2568 encoder->private_->abs_residual,
2569 encoder->private_->abs_residual_partition_sums,
2570 encoder->private_->raw_bits_per_partition,
2571 encoder->private_->lp_coeff[lpc_order-1],
2572 frame_header->blocksize,
2575 qlp_coeff_precision,
2577 min_partition_order,
2578 max_partition_order,
2579 precompute_partition_sums,
2580 encoder->protected_->do_escape_coding,
2581 encoder->protected_->rice_parameter_search_dist,
2582 subframe[!_best_subframe],
2583 partitioned_rice_contents[!_best_subframe]
2584 #ifdef WINDOW_DEBUG_OUTPUT
2585 ,frame_header->number.frame_number
2587 ,encoder->protected_->apodizations[a]
2590 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2591 if(_candidate_bits < _best_bits) {
2592 _best_subframe = !_best_subframe;
2593 _best_bits = _candidate_bits;
2602 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
2606 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2607 if(_best_bits == UINT_MAX) {
2608 FLAC__ASSERT(_best_subframe == 0);
2609 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2612 *best_subframe = _best_subframe;
2613 *best_bits = _best_bits;
2618 FLAC__bool add_subframe_(
2619 FLAC__StreamEncoder *encoder,
2620 const FLAC__FrameHeader *frame_header,
2621 unsigned subframe_bps,
2622 const FLAC__Subframe *subframe,
2623 FLAC__BitBuffer *frame
2624 #ifdef WINDOW_DEBUG_OUTPUT
2625 ,unsigned subframe_bits
2629 switch(subframe->type) {
2630 case FLAC__SUBFRAME_TYPE_CONSTANT:
2631 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
2632 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2636 case FLAC__SUBFRAME_TYPE_FIXED:
2637 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2638 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2642 case FLAC__SUBFRAME_TYPE_LPC:
2643 #ifdef WINDOW_DEBUG_OUTPUT
2644 fprintf(stderr, "WIN:\tframe=%u\tsubframe=?\torder=%u\twindow=%s\tbits=%u\n", frame_header->number.frame_number, subframe->data.lpc.order, subframe->data.lpc.window_type, subframe_bits);
2646 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2647 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2651 case FLAC__SUBFRAME_TYPE_VERBATIM:
2652 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2653 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2664 unsigned evaluate_constant_subframe_(
2665 const FLAC__int32 signal,
2666 unsigned subframe_bps,
2667 FLAC__Subframe *subframe
2670 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2671 subframe->data.constant.value = signal;
2673 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
2676 unsigned evaluate_fixed_subframe_(
2677 FLAC__StreamEncoder *encoder,
2678 const FLAC__int32 signal[],
2679 FLAC__int32 residual[],
2680 FLAC__uint32 abs_residual[],
2681 FLAC__uint64 abs_residual_partition_sums[],
2682 unsigned raw_bits_per_partition[],
2684 unsigned subframe_bps,
2686 unsigned rice_parameter,
2687 unsigned min_partition_order,
2688 unsigned max_partition_order,
2689 FLAC__bool precompute_partition_sums,
2690 FLAC__bool do_escape_coding,
2691 unsigned rice_parameter_search_dist,
2692 FLAC__Subframe *subframe,
2693 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2696 unsigned i, residual_bits;
2697 const unsigned residual_samples = blocksize - order;
2699 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2701 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2703 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2704 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2705 subframe->data.fixed.residual = residual;
2708 find_best_partition_order_(
2712 abs_residual_partition_sums,
2713 raw_bits_per_partition,
2717 min_partition_order,
2718 max_partition_order,
2719 precompute_partition_sums,
2721 rice_parameter_search_dist,
2722 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2725 subframe->data.fixed.order = order;
2726 for(i = 0; i < order; i++)
2727 subframe->data.fixed.warmup[i] = signal[i];
2729 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
2732 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2733 unsigned evaluate_lpc_subframe_(
2734 FLAC__StreamEncoder *encoder,
2735 const FLAC__int32 signal[],
2736 FLAC__int32 residual[],
2737 FLAC__uint32 abs_residual[],
2738 FLAC__uint64 abs_residual_partition_sums[],
2739 unsigned raw_bits_per_partition[],
2740 const FLAC__real lp_coeff[],
2742 unsigned subframe_bps,
2744 unsigned qlp_coeff_precision,
2745 unsigned rice_parameter,
2746 unsigned min_partition_order,
2747 unsigned max_partition_order,
2748 FLAC__bool precompute_partition_sums,
2749 FLAC__bool do_escape_coding,
2750 unsigned rice_parameter_search_dist,
2751 FLAC__Subframe *subframe,
2752 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2753 #ifdef WINDOW_DEBUG_OUTPUT
2754 ,unsigned frame_number
2755 ,unsigned subframe_number
2756 ,FLAC__ApodizationSpecification aspec
2760 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
2761 unsigned i, residual_bits;
2762 int quantization, ret;
2763 const unsigned residual_samples = blocksize - order;
2765 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2766 if(subframe_bps <= 16) {
2767 FLAC__ASSERT(order > 0);
2768 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2769 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2772 #ifdef WINDOW_DEBUG_OUTPUT
2773 if (aspec.type == FLAC__APODIZATION_GAUSS)
2774 snprintf(subframe->data.lpc.window_type, sizeof subframe->data.lpc.window_type, "%s(%0.5f)", winstr[aspec.type], aspec.parameters.gauss.stddev);
2775 else if (aspec.type == FLAC__APODIZATION_TUKEY)
2776 snprintf(subframe->data.lpc.window_type, sizeof subframe->data.lpc.window_type, "%s(%0.5f)", winstr[aspec.type], aspec.parameters.tukey.p);
2778 strncpy(subframe->data.lpc.window_type, winstr[aspec.type], sizeof subframe->data.lpc.window_type);
2781 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
2783 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2785 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2786 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2787 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2789 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2791 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2793 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2795 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2796 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2797 subframe->data.lpc.residual = residual;
2800 find_best_partition_order_(
2804 abs_residual_partition_sums,
2805 raw_bits_per_partition,
2809 min_partition_order,
2810 max_partition_order,
2811 precompute_partition_sums,
2813 rice_parameter_search_dist,
2814 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2817 subframe->data.lpc.order = order;
2818 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2819 subframe->data.lpc.quantization_level = quantization;
2820 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
2821 for(i = 0; i < order; i++)
2822 subframe->data.lpc.warmup[i] = signal[i];
2824 #ifdef WINDOW_DEBUG_OUTPUT
2825 fprintf(stderr, "SWIN:\tframe=%u\tsubframe=%u\torder=%u\twindow=%s\tbits=%u\n", frame_number, subframe_number, order, subframe->data.lpc.window_type, 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);
2827 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;
2831 unsigned evaluate_verbatim_subframe_(
2832 const FLAC__int32 signal[],
2834 unsigned subframe_bps,
2835 FLAC__Subframe *subframe
2838 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2840 subframe->data.verbatim.data = signal;
2842 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
2845 unsigned find_best_partition_order_(
2846 FLAC__StreamEncoderPrivate *private_,
2847 const FLAC__int32 residual[],
2848 FLAC__uint32 abs_residual[],
2849 FLAC__uint64 abs_residual_partition_sums[],
2850 unsigned raw_bits_per_partition[],
2851 unsigned residual_samples,
2852 unsigned predictor_order,
2853 unsigned rice_parameter,
2854 unsigned min_partition_order,
2855 unsigned max_partition_order,
2856 FLAC__bool precompute_partition_sums,
2857 FLAC__bool do_escape_coding,
2858 unsigned rice_parameter_search_dist,
2859 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2863 unsigned residual_bits, best_residual_bits = 0;
2864 unsigned residual_sample;
2865 unsigned best_parameters_index = 0;
2866 const unsigned blocksize = residual_samples + predictor_order;
2868 /* compute abs(residual) for use later */
2869 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2870 r = residual[residual_sample];
2871 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2874 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2875 min_partition_order = min(min_partition_order, max_partition_order);
2877 if(precompute_partition_sums) {
2878 int partition_order;
2881 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2883 if(do_escape_coding)
2884 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2886 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2887 #ifdef DONT_ESTIMATE_RICE_BITS
2889 set_partitioned_rice_with_precompute_(
2891 abs_residual_partition_sums+sum,
2892 raw_bits_per_partition+sum,
2896 rice_parameter_search_dist,
2897 (unsigned)partition_order,
2899 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2905 set_partitioned_rice_with_precompute_(
2907 abs_residual_partition_sums+sum,
2908 raw_bits_per_partition+sum,
2912 rice_parameter_search_dist,
2913 (unsigned)partition_order,
2915 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2921 FLAC__ASSERT(best_residual_bits != 0);
2924 sum += 1u << partition_order;
2925 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2926 best_residual_bits = residual_bits;
2927 best_parameters_index = !best_parameters_index;
2928 best_partitioned_rice->order = partition_order;
2933 unsigned partition_order;
2934 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2935 #ifdef DONT_ESTIMATE_RICE_BITS
2937 set_partitioned_rice_(
2943 rice_parameter_search_dist,
2945 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2951 set_partitioned_rice_(
2956 rice_parameter_search_dist,
2958 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2964 FLAC__ASSERT(best_residual_bits != 0);
2967 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2968 best_residual_bits = residual_bits;
2969 best_parameters_index = !best_parameters_index;
2970 best_partitioned_rice->order = partition_order;
2976 * We are allowed to de-const the pointer based on our special knowledge;
2977 * it is const to the outside world.
2980 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2981 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2982 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2983 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2986 return best_residual_bits;
2989 void precompute_partition_info_sums_(
2990 const FLAC__uint32 abs_residual[],
2991 FLAC__uint64 abs_residual_partition_sums[],
2992 unsigned residual_samples,
2993 unsigned predictor_order,
2994 unsigned min_partition_order,
2995 unsigned max_partition_order
2998 int partition_order;
2999 unsigned from_partition, to_partition = 0;
3000 const unsigned blocksize = residual_samples + predictor_order;
3002 /* first do max_partition_order */
3003 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3004 FLAC__uint64 abs_residual_partition_sum;
3006 unsigned partition, partition_sample, partition_samples, residual_sample;
3007 const unsigned partitions = 1u << partition_order;
3008 const unsigned default_partition_samples = blocksize >> partition_order;
3010 FLAC__ASSERT(default_partition_samples > predictor_order);
3012 for(partition = residual_sample = 0; partition < partitions; partition++) {
3013 partition_samples = default_partition_samples;
3015 partition_samples -= predictor_order;
3016 abs_residual_partition_sum = 0;
3017 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3018 abs_r = abs_residual[residual_sample];
3019 abs_residual_partition_sum += abs_r;
3022 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
3024 to_partition = partitions;
3028 /* now merge partitions for lower orders */
3029 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3032 const unsigned partitions = 1u << partition_order;
3033 for(i = 0; i < partitions; i++) {
3034 s = abs_residual_partition_sums[from_partition];
3036 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
3043 void precompute_partition_info_escapes_(
3044 const FLAC__int32 residual[],
3045 unsigned raw_bits_per_partition[],
3046 unsigned residual_samples,
3047 unsigned predictor_order,
3048 unsigned min_partition_order,
3049 unsigned max_partition_order
3052 int partition_order;
3053 unsigned from_partition, to_partition = 0;
3054 const unsigned blocksize = residual_samples + predictor_order;
3056 /* first do max_partition_order */
3057 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3058 FLAC__int32 r, residual_partition_min, residual_partition_max;
3059 unsigned silog2_min, silog2_max;
3060 unsigned partition, partition_sample, partition_samples, residual_sample;
3061 const unsigned partitions = 1u << partition_order;
3062 const unsigned default_partition_samples = blocksize >> partition_order;
3064 FLAC__ASSERT(default_partition_samples > predictor_order);
3066 for(partition = residual_sample = 0; partition < partitions; partition++) {
3067 partition_samples = default_partition_samples;
3069 partition_samples -= predictor_order;
3070 residual_partition_min = residual_partition_max = 0;
3071 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3072 r = residual[residual_sample];
3073 if(r < residual_partition_min)
3074 residual_partition_min = r;
3075 else if(r > residual_partition_max)
3076 residual_partition_max = r;
3079 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
3080 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
3081 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
3083 to_partition = partitions;
3087 /* now merge partitions for lower orders */
3088 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3091 const unsigned partitions = 1u << partition_order;
3092 for(i = 0; i < partitions; i++) {
3093 m = raw_bits_per_partition[from_partition];
3095 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
3102 #ifdef VARIABLE_RICE_BITS
3103 #undef VARIABLE_RICE_BITS
3105 #ifndef DONT_ESTIMATE_RICE_BITS
3106 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
3109 #ifdef DONT_ESTIMATE_RICE_BITS
3110 FLAC__bool set_partitioned_rice_(
3111 const FLAC__uint32 abs_residual[],
3112 const FLAC__int32 residual[],
3113 const unsigned residual_samples,
3114 const unsigned predictor_order,
3115 const unsigned suggested_rice_parameter,
3116 const unsigned rice_parameter_search_dist,
3117 const unsigned partition_order,
3118 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3122 FLAC__bool set_partitioned_rice_(
3123 const FLAC__uint32 abs_residual[],
3124 const unsigned residual_samples,
3125 const unsigned predictor_order,
3126 const unsigned suggested_rice_parameter,
3127 const unsigned rice_parameter_search_dist,
3128 const unsigned partition_order,
3129 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3134 unsigned rice_parameter, partition_bits;
3135 #ifndef NO_RICE_SEARCH
3136 unsigned best_partition_bits;
3137 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3139 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
3140 unsigned *parameters;
3142 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
3144 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3145 parameters = partitioned_rice_contents->parameters;
3147 if(partition_order == 0) {
3150 #ifndef NO_RICE_SEARCH
3151 if(rice_parameter_search_dist) {
3152 if(suggested_rice_parameter < rice_parameter_search_dist)
3153 min_rice_parameter = 0;
3155 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3156 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3157 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3158 #ifdef DEBUG_VERBOSE
3159 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3161 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3165 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3167 best_partition_bits = 0xffffffff;
3168 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3170 #ifdef VARIABLE_RICE_BITS
3171 const unsigned rice_parameter_estimate = rice_parameter-1;
3172 partition_bits = (1+rice_parameter) * residual_samples;
3176 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3177 for(i = 0; i < residual_samples; i++) {
3178 #ifdef VARIABLE_RICE_BITS
3179 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
3181 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3184 #ifndef NO_RICE_SEARCH
3185 if(partition_bits < best_partition_bits) {
3186 best_rice_parameter = rice_parameter;
3187 best_partition_bits = partition_bits;
3191 parameters[0] = best_rice_parameter;
3192 bits_ += best_partition_bits;
3195 unsigned partition, residual_sample, save_residual_sample, partition_sample;
3196 unsigned partition_samples;
3197 FLAC__uint64 mean, k;
3198 const unsigned partitions = 1u << partition_order;
3199 for(partition = residual_sample = 0; partition < partitions; partition++) {
3200 partition_samples = (residual_samples+predictor_order) >> partition_order;
3201 if(partition == 0) {
3202 if(partition_samples <= predictor_order)
3205 partition_samples -= predictor_order;
3208 save_residual_sample = residual_sample;
3209 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
3210 mean += abs_residual[residual_sample];
3211 residual_sample = save_residual_sample;
3212 /* we are basically calculating the size in bits of the
3213 * average residual magnitude in the partition:
3214 * rice_parameter = floor(log2(mean/partition_samples))
3215 * 'mean' is not a good name for the variable, it is
3216 * actually the sum of magnitudes of all residual values
3217 * in the partition, so the actual mean is
3218 * mean/partition_samples
3220 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
3222 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3223 #ifdef DEBUG_VERBOSE
3224 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3226 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3229 #ifndef NO_RICE_SEARCH
3230 if(rice_parameter_search_dist) {
3231 if(rice_parameter < rice_parameter_search_dist)
3232 min_rice_parameter = 0;
3234 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3235 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
3236 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3237 #ifdef DEBUG_VERBOSE
3238 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3240 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3244 min_rice_parameter = max_rice_parameter = rice_parameter;
3246 best_partition_bits = 0xffffffff;
3247 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3249 #ifdef VARIABLE_RICE_BITS
3250 const unsigned rice_parameter_estimate = rice_parameter-1;
3251 partition_bits = (1+rice_parameter) * partition_samples;
3255 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3256 save_residual_sample = residual_sample;
3257 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3258 #ifdef VARIABLE_RICE_BITS
3259 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
3261 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3264 #ifndef NO_RICE_SEARCH
3265 if(rice_parameter != max_rice_parameter)
3266 residual_sample = save_residual_sample;
3267 if(partition_bits < best_partition_bits) {
3268 best_rice_parameter = rice_parameter;
3269 best_partition_bits = partition_bits;
3273 parameters[partition] = best_rice_parameter;
3274 bits_ += best_partition_bits;
3282 #ifdef DONT_ESTIMATE_RICE_BITS
3283 FLAC__bool set_partitioned_rice_with_precompute_(
3284 const FLAC__int32 residual[],
3285 const FLAC__uint64 abs_residual_partition_sums[],
3286 const unsigned raw_bits_per_partition[],
3287 const unsigned residual_samples,
3288 const unsigned predictor_order,
3289 const unsigned suggested_rice_parameter,
3290 const unsigned rice_parameter_search_dist,
3291 const unsigned partition_order,
3292 const FLAC__bool search_for_escapes,
3293 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3297 FLAC__bool set_partitioned_rice_with_precompute_(
3298 const FLAC__uint32 abs_residual[],
3299 const FLAC__uint64 abs_residual_partition_sums[],
3300 const unsigned raw_bits_per_partition[],
3301 const unsigned residual_samples,
3302 const unsigned predictor_order,
3303 const unsigned suggested_rice_parameter,
3304 const unsigned rice_parameter_search_dist,
3305 const unsigned partition_order,
3306 const FLAC__bool search_for_escapes,
3307 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3312 unsigned rice_parameter, partition_bits;
3313 #ifndef NO_RICE_SEARCH
3314 unsigned best_partition_bits;
3315 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3318 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
3319 unsigned *parameters, *raw_bits;
3321 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
3323 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3324 parameters = partitioned_rice_contents->parameters;
3325 raw_bits = partitioned_rice_contents->raw_bits;
3327 if(partition_order == 0) {
3330 #ifndef NO_RICE_SEARCH
3331 if(rice_parameter_search_dist) {
3332 if(suggested_rice_parameter < rice_parameter_search_dist)
3333 min_rice_parameter = 0;
3335 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3336 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3337 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3338 #ifdef DEBUG_VERBOSE
3339 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3341 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3345 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3347 best_partition_bits = 0xffffffff;
3348 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3350 #ifdef VARIABLE_RICE_BITS
3351 const unsigned rice_parameter_estimate = rice_parameter-1;
3352 partition_bits = (1+rice_parameter) * residual_samples;
3356 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3357 for(i = 0; i < residual_samples; i++) {
3358 #ifdef VARIABLE_RICE_BITS
3359 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
3361 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
3364 #ifndef NO_RICE_SEARCH
3365 if(partition_bits < best_partition_bits) {
3366 best_rice_parameter = rice_parameter;
3367 best_partition_bits = partition_bits;
3371 if(search_for_escapes) {
3372 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;
3373 if(flat_bits <= best_partition_bits) {
3374 raw_bits[0] = raw_bits_per_partition[0];
3375 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3376 best_partition_bits = flat_bits;
3379 parameters[0] = best_rice_parameter;
3380 bits_ += best_partition_bits;
3383 unsigned partition, residual_sample, save_residual_sample, partition_sample;
3384 unsigned partition_samples;
3385 FLAC__uint64 mean, k;
3386 const unsigned partitions = 1u << partition_order;
3387 for(partition = residual_sample = 0; partition < partitions; partition++) {
3388 partition_samples = (residual_samples+predictor_order) >> partition_order;
3389 if(partition == 0) {
3390 if(partition_samples <= predictor_order)
3393 partition_samples -= predictor_order;
3395 mean = abs_residual_partition_sums[partition];
3396 /* we are basically calculating the size in bits of the
3397 * average residual magnitude in the partition:
3398 * rice_parameter = floor(log2(mean/partition_samples))
3399 * 'mean' is not a good name for the variable, it is
3400 * actually the sum of magnitudes of all residual values
3401 * in the partition, so the actual mean is
3402 * mean/partition_samples
3404 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
3406 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3407 #ifdef DEBUG_VERBOSE
3408 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3410 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3413 #ifndef NO_RICE_SEARCH
3414 if(rice_parameter_search_dist) {
3415 if(rice_parameter < rice_parameter_search_dist)
3416 min_rice_parameter = 0;
3418 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3419 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
3420 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3421 #ifdef DEBUG_VERBOSE
3422 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3424 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3428 min_rice_parameter = max_rice_parameter = rice_parameter;
3430 best_partition_bits = 0xffffffff;
3431 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3433 #ifdef VARIABLE_RICE_BITS
3434 const unsigned rice_parameter_estimate = rice_parameter-1;
3435 partition_bits = (1+rice_parameter) * partition_samples;
3439 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3440 save_residual_sample = residual_sample;
3441 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3442 #ifdef VARIABLE_RICE_BITS
3443 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
3445 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
3448 #ifndef NO_RICE_SEARCH
3449 if(rice_parameter != max_rice_parameter)
3450 residual_sample = save_residual_sample;
3451 if(partition_bits < best_partition_bits) {
3452 best_rice_parameter = rice_parameter;
3453 best_partition_bits = partition_bits;
3457 if(search_for_escapes) {
3458 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;
3459 if(flat_bits <= best_partition_bits) {
3460 raw_bits[partition] = raw_bits_per_partition[partition];
3461 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3462 best_partition_bits = flat_bits;
3465 parameters[partition] = best_rice_parameter;
3466 bits_ += best_partition_bits;
3474 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
3479 for(i = 0; i < samples && !(x&1); i++)
3486 for(shift = 0; !(x&1); shift++)
3491 for(i = 0; i < samples; i++)
3492 signal[i] >>= shift;
3498 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3502 for(channel = 0; channel < channels; channel++)
3503 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3505 fifo->tail += wide_samples;
3507 FLAC__ASSERT(fifo->tail <= fifo->size);
3510 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3513 unsigned sample, wide_sample;
3514 unsigned tail = fifo->tail;
3516 sample = input_offset * channels;
3517 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3518 for(channel = 0; channel < channels; channel++)
3519 fifo->data[channel][tail] = input[sample++];
3524 FLAC__ASSERT(fifo->tail <= fifo->size);
3527 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3529 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3530 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3533 if(encoder->private_->verify.needs_magic_hack) {
3534 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3535 *bytes = FLAC__STREAM_SYNC_LENGTH;
3536 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3537 encoder->private_->verify.needs_magic_hack = false;
3540 if(encoded_bytes == 0) {
3542 * If we get here, a FIFO underflow has occurred,
3543 * which means there is a bug somewhere.
3546 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3548 else if(encoded_bytes < *bytes)
3549 *bytes = encoded_bytes;
3550 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3551 encoder->private_->verify.output.data += *bytes;
3552 encoder->private_->verify.output.bytes -= *bytes;
3555 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3558 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3560 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3562 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3563 const unsigned blocksize = frame->header.blocksize;
3564 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3566 for(channel = 0; channel < channels; channel++) {
3567 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3568 unsigned i, sample = 0;
3569 FLAC__int32 expect = 0, got = 0;
3571 for(i = 0; i < blocksize; i++) {
3572 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3574 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3575 got = (FLAC__int32)buffer[channel][i];
3579 FLAC__ASSERT(i < blocksize);
3580 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3581 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
3582 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
3583 encoder->private_->verify.error_stats.channel = channel;
3584 encoder->private_->verify.error_stats.sample = sample;
3585 encoder->private_->verify.error_stats.expected = expect;
3586 encoder->private_->verify.error_stats.got = got;
3587 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3588 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3591 /* dequeue the frame from the fifo */
3592 for(channel = 0; channel < channels; channel++) {
3593 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3595 encoder->private_->verify.input_fifo.tail -= blocksize;
3596 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3599 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3601 (void)decoder, (void)metadata, (void)client_data;
3604 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3606 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3607 (void)decoder, (void)status;
3608 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;