1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memcpy() */
23 #include "FLAC/assert.h"
24 #include "FLAC/stream_decoder.h"
25 #include "protected/stream_encoder.h"
26 #include "private/bitbuffer.h"
27 #include "private/bitmath.h"
28 #include "private/crc.h"
29 #include "private/cpu.h"
30 #include "private/fixed.h"
31 #include "private/format.h"
32 #include "private/lpc.h"
33 #include "private/md5.h"
34 #include "private/memory.h"
35 #include "private/stream_encoder_framing.h"
40 #define min(x,y) ((x)<(y)?(x):(y))
45 #define max(x,y) ((x)>(y)?(x):(y))
48 FLAC__int32 *data[FLAC__MAX_CHANNELS];
49 unsigned size; /* of each data[] in samples */
54 const FLAC__byte *data;
61 ENCODER_IN_METADATA = 1,
65 /***********************************************************************
67 * Private class method prototypes
69 ***********************************************************************/
71 static void set_defaults_(FLAC__StreamEncoder *encoder);
72 static void free_(FLAC__StreamEncoder *encoder);
73 static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
74 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
75 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
76 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
78 static FLAC__bool process_subframe_(
79 FLAC__StreamEncoder *encoder,
80 unsigned min_partition_order,
81 unsigned max_partition_order,
82 FLAC__bool precompute_partition_sums,
83 FLAC__bool verbatim_only,
84 const FLAC__FrameHeader *frame_header,
85 unsigned subframe_bps,
86 const FLAC__int32 integer_signal[],
87 const FLAC__real real_signal[],
88 FLAC__Subframe *subframe[2],
89 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
90 FLAC__int32 *residual[2],
91 unsigned *best_subframe,
95 static FLAC__bool add_subframe_(
96 FLAC__StreamEncoder *encoder,
97 const FLAC__FrameHeader *frame_header,
98 unsigned subframe_bps,
99 const FLAC__Subframe *subframe,
100 FLAC__BitBuffer *frame
103 static unsigned evaluate_constant_subframe_(
104 const FLAC__int32 signal,
105 unsigned subframe_bps,
106 FLAC__Subframe *subframe
109 static unsigned evaluate_fixed_subframe_(
110 FLAC__StreamEncoder *encoder,
111 const FLAC__int32 signal[],
112 FLAC__int32 residual[],
113 FLAC__uint32 abs_residual[],
114 FLAC__uint64 abs_residual_partition_sums[],
115 unsigned raw_bits_per_partition[],
117 unsigned subframe_bps,
119 unsigned rice_parameter,
120 unsigned min_partition_order,
121 unsigned max_partition_order,
122 FLAC__bool precompute_partition_sums,
123 FLAC__bool do_escape_coding,
124 unsigned rice_parameter_search_dist,
125 FLAC__Subframe *subframe,
126 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
129 static unsigned evaluate_lpc_subframe_(
130 FLAC__StreamEncoder *encoder,
131 const FLAC__int32 signal[],
132 FLAC__int32 residual[],
133 FLAC__uint32 abs_residual[],
134 FLAC__uint64 abs_residual_partition_sums[],
135 unsigned raw_bits_per_partition[],
136 const FLAC__real lp_coeff[],
138 unsigned subframe_bps,
140 unsigned qlp_coeff_precision,
141 unsigned rice_parameter,
142 unsigned min_partition_order,
143 unsigned max_partition_order,
144 FLAC__bool precompute_partition_sums,
145 FLAC__bool do_escape_coding,
146 unsigned rice_parameter_search_dist,
147 FLAC__Subframe *subframe,
148 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
151 static unsigned evaluate_verbatim_subframe_(
152 const FLAC__int32 signal[],
154 unsigned subframe_bps,
155 FLAC__Subframe *subframe
158 static unsigned find_best_partition_order_(
159 struct FLAC__StreamEncoderPrivate *private_,
160 const FLAC__int32 residual[],
161 FLAC__uint32 abs_residual[],
162 FLAC__uint64 abs_residual_partition_sums[],
163 unsigned raw_bits_per_partition[],
164 unsigned residual_samples,
165 unsigned predictor_order,
166 unsigned rice_parameter,
167 unsigned min_partition_order,
168 unsigned max_partition_order,
169 FLAC__bool precompute_partition_sums,
170 FLAC__bool do_escape_coding,
171 unsigned rice_parameter_search_dist,
172 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
175 static void precompute_partition_info_sums_(
176 const FLAC__uint32 abs_residual[],
177 FLAC__uint64 abs_residual_partition_sums[],
178 unsigned residual_samples,
179 unsigned predictor_order,
180 unsigned min_partition_order,
181 unsigned max_partition_order
184 static void precompute_partition_info_escapes_(
185 const FLAC__int32 residual[],
186 unsigned raw_bits_per_partition[],
187 unsigned residual_samples,
188 unsigned predictor_order,
189 unsigned min_partition_order,
190 unsigned max_partition_order
193 #ifdef DONT_ESTIMATE_RICE_BITS
194 static FLAC__bool set_partitioned_rice_(
195 const FLAC__uint32 abs_residual[],
196 const FLAC__int32 residual[],
197 const unsigned residual_samples,
198 const unsigned predictor_order,
199 const unsigned suggested_rice_parameter,
200 const unsigned rice_parameter_search_dist,
201 const unsigned partition_order,
202 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
206 static FLAC__bool set_partitioned_rice_with_precompute_(
207 const FLAC__int32 residual[],
208 const FLAC__uint64 abs_residual_partition_sums[],
209 const unsigned raw_bits_per_partition[],
210 const unsigned residual_samples,
211 const unsigned predictor_order,
212 const unsigned suggested_rice_parameter,
213 const unsigned rice_parameter_search_dist,
214 const unsigned partition_order,
215 const FLAC__bool search_for_escapes,
216 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
220 static FLAC__bool set_partitioned_rice_(
221 const FLAC__uint32 abs_residual[],
222 const unsigned residual_samples,
223 const unsigned predictor_order,
224 const unsigned suggested_rice_parameter,
225 const unsigned rice_parameter_search_dist,
226 const unsigned partition_order,
227 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
231 static FLAC__bool set_partitioned_rice_with_precompute_(
232 const FLAC__uint32 abs_residual[],
233 const FLAC__uint64 abs_residual_partition_sums[],
234 const unsigned raw_bits_per_partition[],
235 const unsigned residual_samples,
236 const unsigned predictor_order,
237 const unsigned suggested_rice_parameter,
238 const unsigned rice_parameter_search_dist,
239 const unsigned partition_order,
240 const FLAC__bool search_for_escapes,
241 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
246 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
248 /* verify-related routines: */
249 static void append_to_verify_fifo_(
250 verify_input_fifo *fifo,
251 const FLAC__int32 * const input[],
252 unsigned input_offset,
254 unsigned wide_samples
257 static void append_to_verify_fifo_interleaved_(
258 verify_input_fifo *fifo,
259 const FLAC__int32 input[],
260 unsigned input_offset,
262 unsigned wide_samples
265 static FLAC__StreamDecoderReadStatus verify_read_callback_(
266 const FLAC__StreamDecoder *decoder,
272 static FLAC__StreamDecoderWriteStatus verify_write_callback_(
273 const FLAC__StreamDecoder *decoder,
274 const FLAC__Frame *frame,
275 const FLAC__int32 * const buffer[],
279 static void verify_metadata_callback_(
280 const FLAC__StreamDecoder *decoder,
281 const FLAC__StreamMetadata *metadata,
285 static void verify_error_callback_(
286 const FLAC__StreamDecoder *decoder,
287 FLAC__StreamDecoderErrorStatus status,
292 /***********************************************************************
296 ***********************************************************************/
298 typedef struct FLAC__StreamEncoderPrivate {
299 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
300 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
301 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
302 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
303 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
304 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
305 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
306 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
307 FLAC__int32 *residual_workspace_mid_side[2][2];
308 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
309 FLAC__Subframe subframe_workspace_mid_side[2][2];
310 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
311 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
312 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
313 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
314 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
315 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
316 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
317 unsigned best_subframe_mid_side[2];
318 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
319 unsigned best_subframe_bits_mid_side[2];
320 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
321 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
322 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
323 FLAC__BitBuffer *frame; /* the current frame being worked on */
324 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
325 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
326 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
327 FLAC__ChannelAssignment last_channel_assignment;
328 FLAC__StreamMetadata metadata;
329 unsigned current_sample_number;
330 unsigned current_frame_number;
331 struct MD5Context md5context;
332 FLAC__CPUInfo cpuinfo;
333 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
334 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
335 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[]);
336 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[]);
337 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[]);
338 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
339 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
340 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
341 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
342 FLAC__StreamEncoderWriteCallback write_callback;
343 FLAC__StreamEncoderMetadataCallback metadata_callback;
345 /* unaligned (original) pointers to allocated data */
346 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
347 FLAC__int32 *integer_signal_mid_side_unaligned[2];
348 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
349 FLAC__real *real_signal_mid_side_unaligned[2];
350 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
351 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
352 FLAC__uint32 *abs_residual_unaligned;
353 FLAC__uint64 *abs_residual_partition_sums_unaligned;
354 unsigned *raw_bits_per_partition_unaligned;
356 * These fields have been moved here from private function local
357 * declarations merely to save stack space during encoding.
359 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
360 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
362 * The data for the verify section
365 FLAC__StreamDecoder *decoder;
366 EncoderStateHint state_hint;
367 FLAC__bool needs_magic_hack;
368 verify_input_fifo input_fifo;
369 verify_output output;
371 FLAC__uint64 absolute_sample;
372 unsigned frame_number;
375 FLAC__int32 expected;
379 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
380 } FLAC__StreamEncoderPrivate;
382 /***********************************************************************
384 * Public static class data
386 ***********************************************************************/
388 const char * const FLAC__StreamEncoderStateString[] = {
389 "FLAC__STREAM_ENCODER_OK",
390 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
391 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
392 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
393 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
394 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
395 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
396 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
397 "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
398 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
399 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
400 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
401 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
402 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
403 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
404 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
405 "FLAC__STREAM_ENCODER_INVALID_METADATA",
406 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
407 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
408 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
409 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
410 "FLAC__STREAM_ENCODER_UNINITIALIZED"
413 const char * const FLAC__StreamEncoderWriteStatusString[] = {
414 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
415 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
418 /***********************************************************************
420 * Class constructor/destructor
423 FLAC__StreamEncoder *FLAC__stream_encoder_new()
425 FLAC__StreamEncoder *encoder;
428 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
430 encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
434 memset(encoder, 0, sizeof(FLAC__StreamEncoder));
436 encoder->protected_ = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
437 if(encoder->protected_ == 0) {
441 memset(encoder->protected_, 0, sizeof(FLAC__StreamEncoderProtected));
443 encoder->private_ = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
444 if(encoder->private_ == 0) {
445 free(encoder->protected_);
449 memset(encoder->private_, 0, sizeof(FLAC__StreamEncoderPrivate));
451 encoder->private_->frame = FLAC__bitbuffer_new();
452 if(encoder->private_->frame == 0) {
453 free(encoder->private_);
454 free(encoder->protected_);
459 set_defaults_(encoder);
461 encoder->private_->is_being_deleted = false;
463 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
464 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
465 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
467 for(i = 0; i < 2; i++) {
468 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
469 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
471 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
472 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
473 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
475 for(i = 0; i < 2; i++) {
476 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
477 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
480 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
481 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
482 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
484 for(i = 0; i < 2; i++) {
485 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
486 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
488 for(i = 0; i < 2; i++)
489 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
491 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
496 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
500 FLAC__ASSERT(0 != encoder);
501 FLAC__ASSERT(0 != encoder->protected_);
502 FLAC__ASSERT(0 != encoder->private_);
503 FLAC__ASSERT(0 != encoder->private_->frame);
505 encoder->private_->is_being_deleted = true;
507 FLAC__stream_encoder_finish(encoder);
509 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
510 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
512 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
513 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
514 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
516 for(i = 0; i < 2; i++) {
517 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
518 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
520 for(i = 0; i < 2; i++)
521 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
523 FLAC__bitbuffer_delete(encoder->private_->frame);
524 free(encoder->private_);
525 free(encoder->protected_);
529 /***********************************************************************
531 * Public class methods
533 ***********************************************************************/
535 FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
538 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
540 FLAC__ASSERT(0 != encoder);
542 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
543 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
545 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
547 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
548 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
550 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
551 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
553 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
554 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
556 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
557 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
559 if(encoder->protected_->bits_per_sample >= 32)
560 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
562 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
563 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
565 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
566 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
568 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
569 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
571 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
572 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
574 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
575 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
577 if(encoder->protected_->qlp_coeff_precision == 0) {
578 if(encoder->protected_->bits_per_sample < 16) {
579 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
580 /* @@@ until then we'll make a guess */
581 encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
583 else if(encoder->protected_->bits_per_sample == 16) {
584 if(encoder->protected_->blocksize <= 192)
585 encoder->protected_->qlp_coeff_precision = 7;
586 else if(encoder->protected_->blocksize <= 384)
587 encoder->protected_->qlp_coeff_precision = 8;
588 else if(encoder->protected_->blocksize <= 576)
589 encoder->protected_->qlp_coeff_precision = 9;
590 else if(encoder->protected_->blocksize <= 1152)
591 encoder->protected_->qlp_coeff_precision = 10;
592 else if(encoder->protected_->blocksize <= 2304)
593 encoder->protected_->qlp_coeff_precision = 11;
594 else if(encoder->protected_->blocksize <= 4608)
595 encoder->protected_->qlp_coeff_precision = 12;
597 encoder->protected_->qlp_coeff_precision = 13;
600 if(encoder->protected_->blocksize <= 384)
601 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
602 else if(encoder->protected_->blocksize <= 1152)
603 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
605 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
607 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
609 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
610 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
612 if(encoder->protected_->streamable_subset) {
614 encoder->protected_->blocksize != 192 &&
615 encoder->protected_->blocksize != 576 &&
616 encoder->protected_->blocksize != 1152 &&
617 encoder->protected_->blocksize != 2304 &&
618 encoder->protected_->blocksize != 4608 &&
619 encoder->protected_->blocksize != 256 &&
620 encoder->protected_->blocksize != 512 &&
621 encoder->protected_->blocksize != 1024 &&
622 encoder->protected_->blocksize != 2048 &&
623 encoder->protected_->blocksize != 4096 &&
624 encoder->protected_->blocksize != 8192 &&
625 encoder->protected_->blocksize != 16384
627 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
629 encoder->protected_->sample_rate != 8000 &&
630 encoder->protected_->sample_rate != 16000 &&
631 encoder->protected_->sample_rate != 22050 &&
632 encoder->protected_->sample_rate != 24000 &&
633 encoder->protected_->sample_rate != 32000 &&
634 encoder->protected_->sample_rate != 44100 &&
635 encoder->protected_->sample_rate != 48000 &&
636 encoder->protected_->sample_rate != 96000
638 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
640 encoder->protected_->bits_per_sample != 8 &&
641 encoder->protected_->bits_per_sample != 12 &&
642 encoder->protected_->bits_per_sample != 16 &&
643 encoder->protected_->bits_per_sample != 20 &&
644 encoder->protected_->bits_per_sample != 24
646 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
647 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
648 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
651 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
652 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
653 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
654 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
656 /* validate metadata */
657 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
658 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
659 metadata_has_seektable = false;
660 metadata_has_vorbis_comment = false;
661 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
662 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
663 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
664 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
665 if(metadata_has_seektable) /* only one is allowed */
666 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
667 metadata_has_seektable = true;
668 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
669 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
671 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
672 if(metadata_has_vorbis_comment) /* only one is allowed */
673 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
674 metadata_has_vorbis_comment = true;
678 encoder->private_->input_capacity = 0;
679 for(i = 0; i < encoder->protected_->channels; i++) {
680 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
681 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
683 for(i = 0; i < 2; i++) {
684 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
685 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
687 for(i = 0; i < encoder->protected_->channels; i++) {
688 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
689 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
690 encoder->private_->best_subframe[i] = 0;
692 for(i = 0; i < 2; i++) {
693 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
694 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
695 encoder->private_->best_subframe_mid_side[i] = 0;
697 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
698 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
699 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
700 encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
701 encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
702 if(encoder->private_->loose_mid_side_stereo_frames == 0)
703 encoder->private_->loose_mid_side_stereo_frames = 1;
704 encoder->private_->loose_mid_side_stereo_frame_count = 0;
705 encoder->private_->current_sample_number = 0;
706 encoder->private_->current_frame_number = 0;
708 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
709 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? */
710 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
713 * get the CPU info and set the function pointers
715 FLAC__cpu_info(&encoder->private_->cpuinfo);
716 /* first default to the non-asm routines */
717 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
718 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
719 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
720 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
721 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
722 /* now override with asm where appropriate */
724 if(encoder->private_->cpuinfo.use_asm) {
725 #ifdef FLAC__CPU_IA32
726 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
727 #ifdef FLAC__HAS_NASM
728 if(0 && encoder->private_->cpuinfo.data.ia32.sse) {
729 if(encoder->protected_->max_lpc_order < 4)
730 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
731 else if(encoder->protected_->max_lpc_order < 8)
732 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
733 else if(encoder->protected_->max_lpc_order < 12)
734 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
736 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
738 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
739 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
741 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
742 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
743 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
744 if(encoder->private_->cpuinfo.data.ia32.mmx) {
745 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
746 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
749 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
750 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
756 /* finally override based on wide-ness if necessary */
757 if(encoder->private_->use_wide_by_block) {
758 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
761 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
762 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
764 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
765 /* the above function sets the state for us in case of an error */
766 return encoder->protected_->state;
769 if(!FLAC__bitbuffer_init(encoder->private_->frame))
770 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
773 * Set up the verify stuff if necessary
775 if(encoder->protected_->verify) {
777 * First, set up the fifo which will hold the
778 * original signal to compare against
780 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
781 for(i = 0; i < encoder->protected_->channels; i++) {
782 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
783 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
785 encoder->private_->verify.input_fifo.tail = 0;
788 * Now set up a stream decoder for verification
790 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
791 if(0 == encoder->private_->verify.decoder)
792 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
794 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
795 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
796 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
797 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
798 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
799 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
800 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
802 encoder->private_->verify.error_stats.absolute_sample = 0;
803 encoder->private_->verify.error_stats.frame_number = 0;
804 encoder->private_->verify.error_stats.channel = 0;
805 encoder->private_->verify.error_stats.sample = 0;
806 encoder->private_->verify.error_stats.expected = 0;
807 encoder->private_->verify.error_stats.got = 0;
810 * write the stream header
812 if(encoder->protected_->verify)
813 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
814 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
815 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
816 if(!write_bitbuffer_(encoder, 0)) {
817 /* the above function sets the state for us in case of an error */
818 return encoder->protected_->state;
822 * write the STREAMINFO metadata block
824 if(encoder->protected_->verify)
825 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
826 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
827 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
828 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
829 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
830 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
831 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
832 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
833 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
834 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
835 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
836 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
837 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
838 MD5Init(&encoder->private_->md5context);
839 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
840 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
843 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
844 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
845 if(!write_bitbuffer_(encoder, 0)) {
846 /* the above function sets the state for us in case of an error */
847 return encoder->protected_->state;
851 * Now that the STREAMINFO block is written, we can init this to an
852 * absurdly-high value...
854 encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
855 /* ... and clear this to 0 */
856 encoder->private_->metadata.data.stream_info.total_samples = 0;
859 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
860 * if not, we will write an empty one (FLAC__add_metadata_block()
861 * automatically supplies the vendor string).
863 if(!metadata_has_vorbis_comment) {
864 FLAC__StreamMetadata vorbis_comment;
865 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
866 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
867 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
868 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
869 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
870 vorbis_comment.data.vorbis_comment.num_comments = 0;
871 vorbis_comment.data.vorbis_comment.comments = 0;
872 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
873 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
876 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
877 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
878 if(!write_bitbuffer_(encoder, 0)) {
879 /* the above function sets the state for us in case of an error */
880 return encoder->protected_->state;
885 * write the user's metadata blocks
887 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
888 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
889 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
890 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
893 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
894 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
895 if(!write_bitbuffer_(encoder, 0)) {
896 /* the above function sets the state for us in case of an error */
897 return encoder->protected_->state;
901 if(encoder->protected_->verify)
902 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
904 return encoder->protected_->state;
907 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
909 FLAC__ASSERT(0 != encoder);
911 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
914 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
915 if(encoder->private_->current_sample_number != 0) {
916 encoder->protected_->blocksize = encoder->private_->current_sample_number;
917 process_frame_(encoder, true); /* true => is last frame */
921 MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
923 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
924 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
927 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
928 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
931 set_defaults_(encoder);
933 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
936 FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
938 FLAC__ASSERT(0 != encoder);
939 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
941 encoder->protected_->verify = value;
945 FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
947 FLAC__ASSERT(0 != encoder);
948 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
950 encoder->protected_->streamable_subset = value;
954 FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
956 FLAC__ASSERT(0 != encoder);
957 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
959 encoder->protected_->do_mid_side_stereo = value;
963 FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
965 FLAC__ASSERT(0 != encoder);
966 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
968 encoder->protected_->loose_mid_side_stereo = value;
972 FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
974 FLAC__ASSERT(0 != encoder);
975 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
977 encoder->protected_->channels = value;
981 FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
983 FLAC__ASSERT(0 != encoder);
984 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
986 encoder->protected_->bits_per_sample = value;
990 FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
992 FLAC__ASSERT(0 != encoder);
993 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
995 encoder->protected_->sample_rate = value;
999 FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1001 FLAC__ASSERT(0 != encoder);
1002 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1004 encoder->protected_->blocksize = value;
1008 FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1010 FLAC__ASSERT(0 != encoder);
1011 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1013 encoder->protected_->max_lpc_order = value;
1017 FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1019 FLAC__ASSERT(0 != encoder);
1020 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1022 encoder->protected_->qlp_coeff_precision = value;
1026 FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1028 FLAC__ASSERT(0 != encoder);
1029 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1031 encoder->protected_->do_qlp_coeff_prec_search = value;
1035 FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1037 FLAC__ASSERT(0 != encoder);
1038 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1041 /*@@@ deprecated: */
1042 encoder->protected_->do_escape_coding = value;
1049 FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1051 FLAC__ASSERT(0 != encoder);
1052 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1054 encoder->protected_->do_exhaustive_model_search = value;
1058 FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1060 FLAC__ASSERT(0 != encoder);
1061 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1063 encoder->protected_->min_residual_partition_order = value;
1067 FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1069 FLAC__ASSERT(0 != encoder);
1070 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1072 encoder->protected_->max_residual_partition_order = value;
1076 FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1078 FLAC__ASSERT(0 != encoder);
1079 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1082 /*@@@ deprecated: */
1083 encoder->protected_->rice_parameter_search_dist = value;
1090 FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1092 FLAC__ASSERT(0 != encoder);
1093 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1095 encoder->protected_->total_samples_estimate = value;
1099 FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1101 FLAC__ASSERT(0 != encoder);
1102 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1104 encoder->protected_->metadata = metadata;
1105 encoder->protected_->num_metadata_blocks = num_blocks;
1109 FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
1111 FLAC__ASSERT(0 != encoder);
1112 FLAC__ASSERT(0 != value);
1113 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1115 encoder->private_->write_callback = value;
1119 FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
1121 FLAC__ASSERT(0 != encoder);
1122 FLAC__ASSERT(0 != value);
1123 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1125 encoder->private_->metadata_callback = value;
1129 FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
1131 FLAC__ASSERT(0 != encoder);
1132 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1134 encoder->private_->client_data = value;
1138 FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1140 FLAC__ASSERT(0 != encoder);
1141 return encoder->protected_->state;
1144 FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1146 FLAC__ASSERT(0 != encoder);
1147 if(encoder->protected_->verify)
1148 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1150 return FLAC__STREAM_DECODER_UNINITIALIZED;
1153 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)
1155 FLAC__ASSERT(0 != encoder);
1156 if(0 != absolute_sample)
1157 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1158 if(0 != frame_number)
1159 *frame_number = encoder->private_->verify.error_stats.frame_number;
1161 *channel = encoder->private_->verify.error_stats.channel;
1163 *sample = encoder->private_->verify.error_stats.sample;
1165 *expected = encoder->private_->verify.error_stats.expected;
1167 *got = encoder->private_->verify.error_stats.got;
1170 FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1172 FLAC__ASSERT(0 != encoder);
1173 return encoder->protected_->verify;
1176 FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1178 FLAC__ASSERT(0 != encoder);
1179 return encoder->protected_->streamable_subset;
1182 FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1184 FLAC__ASSERT(0 != encoder);
1185 return encoder->protected_->do_mid_side_stereo;
1188 FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1190 FLAC__ASSERT(0 != encoder);
1191 return encoder->protected_->loose_mid_side_stereo;
1194 unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1196 FLAC__ASSERT(0 != encoder);
1197 return encoder->protected_->channels;
1200 unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1202 FLAC__ASSERT(0 != encoder);
1203 return encoder->protected_->bits_per_sample;
1206 unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1208 FLAC__ASSERT(0 != encoder);
1209 return encoder->protected_->sample_rate;
1212 unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1214 FLAC__ASSERT(0 != encoder);
1215 return encoder->protected_->blocksize;
1218 unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1220 FLAC__ASSERT(0 != encoder);
1221 return encoder->protected_->max_lpc_order;
1224 unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1226 FLAC__ASSERT(0 != encoder);
1227 return encoder->protected_->qlp_coeff_precision;
1230 FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1232 FLAC__ASSERT(0 != encoder);
1233 return encoder->protected_->do_qlp_coeff_prec_search;
1236 FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1238 FLAC__ASSERT(0 != encoder);
1239 return encoder->protected_->do_escape_coding;
1242 FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1244 FLAC__ASSERT(0 != encoder);
1245 return encoder->protected_->do_exhaustive_model_search;
1248 unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1250 FLAC__ASSERT(0 != encoder);
1251 return encoder->protected_->min_residual_partition_order;
1254 unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1256 FLAC__ASSERT(0 != encoder);
1257 return encoder->protected_->max_residual_partition_order;
1260 unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1262 FLAC__ASSERT(0 != encoder);
1263 return encoder->protected_->rice_parameter_search_dist;
1266 FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1268 FLAC__ASSERT(0 != encoder);
1269 return encoder->protected_->total_samples_estimate;
1272 FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1274 unsigned i, j, channel;
1275 FLAC__int32 x, mid, side;
1276 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1278 FLAC__ASSERT(0 != encoder);
1279 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1282 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1284 if(encoder->protected_->verify)
1285 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1287 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1288 x = mid = side = buffer[0][j];
1289 encoder->private_->integer_signal[0][i] = x;
1290 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1292 encoder->private_->integer_signal[1][i] = x;
1293 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1296 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1297 encoder->private_->integer_signal_mid_side[1][i] = side;
1298 encoder->private_->integer_signal_mid_side[0][i] = mid;
1299 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1300 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1301 encoder->private_->current_sample_number++;
1303 if(i == blocksize) {
1304 if(!process_frame_(encoder, false)) /* false => not last frame */
1307 } while(j < samples);
1311 if(encoder->protected_->verify)
1312 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1314 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1315 for(channel = 0; channel < channels; channel++) {
1316 x = buffer[channel][j];
1317 encoder->private_->integer_signal[channel][i] = x;
1318 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1320 encoder->private_->current_sample_number++;
1322 if(i == blocksize) {
1323 if(!process_frame_(encoder, false)) /* false => not last frame */
1326 } while(j < samples);
1332 FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
1334 unsigned i, j, k, channel;
1335 FLAC__int32 x, mid, side;
1336 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1338 FLAC__ASSERT(0 != encoder);
1339 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1342 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1344 if(encoder->protected_->verify)
1345 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1347 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1348 x = mid = side = buffer[k++];
1349 encoder->private_->integer_signal[0][i] = x;
1350 encoder->private_->real_signal[0][i] = (FLAC__real)x;
1352 encoder->private_->integer_signal[1][i] = x;
1353 encoder->private_->real_signal[1][i] = (FLAC__real)x;
1356 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1357 encoder->private_->integer_signal_mid_side[1][i] = side;
1358 encoder->private_->integer_signal_mid_side[0][i] = mid;
1359 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1360 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1361 encoder->private_->current_sample_number++;
1363 if(i == blocksize) {
1364 if(!process_frame_(encoder, false)) /* false => not last frame */
1367 } while(j < samples);
1371 if(encoder->protected_->verify)
1372 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1374 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1375 for(channel = 0; channel < channels; channel++) {
1377 encoder->private_->integer_signal[channel][i] = x;
1378 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1380 encoder->private_->current_sample_number++;
1382 if(i == blocksize) {
1383 if(!process_frame_(encoder, false)) /* false => not last frame */
1386 } while(j < samples);
1392 /***********************************************************************
1394 * Private class methods
1396 ***********************************************************************/
1398 void set_defaults_(FLAC__StreamEncoder *encoder)
1400 FLAC__ASSERT(0 != encoder);
1402 encoder->protected_->verify = false;
1403 encoder->protected_->streamable_subset = true;
1404 encoder->protected_->do_mid_side_stereo = false;
1405 encoder->protected_->loose_mid_side_stereo = false;
1406 encoder->protected_->channels = 2;
1407 encoder->protected_->bits_per_sample = 16;
1408 encoder->protected_->sample_rate = 44100;
1409 encoder->protected_->blocksize = 1152;
1410 encoder->protected_->max_lpc_order = 0;
1411 encoder->protected_->qlp_coeff_precision = 0;
1412 encoder->protected_->do_qlp_coeff_prec_search = false;
1413 encoder->protected_->do_exhaustive_model_search = false;
1414 encoder->protected_->do_escape_coding = false;
1415 encoder->protected_->min_residual_partition_order = 0;
1416 encoder->protected_->max_residual_partition_order = 0;
1417 encoder->protected_->rice_parameter_search_dist = 0;
1418 encoder->protected_->total_samples_estimate = 0;
1419 encoder->protected_->metadata = 0;
1420 encoder->protected_->num_metadata_blocks = 0;
1422 encoder->private_->write_callback = 0;
1423 encoder->private_->metadata_callback = 0;
1424 encoder->private_->client_data = 0;
1427 void free_(FLAC__StreamEncoder *encoder)
1429 unsigned i, channel;
1431 FLAC__ASSERT(0 != encoder);
1432 for(i = 0; i < encoder->protected_->channels; i++) {
1433 if(0 != encoder->private_->integer_signal_unaligned[i]) {
1434 free(encoder->private_->integer_signal_unaligned[i]);
1435 encoder->private_->integer_signal_unaligned[i] = 0;
1437 if(0 != encoder->private_->real_signal_unaligned[i]) {
1438 free(encoder->private_->real_signal_unaligned[i]);
1439 encoder->private_->real_signal_unaligned[i] = 0;
1442 for(i = 0; i < 2; i++) {
1443 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
1444 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1445 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1447 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
1448 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1449 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1452 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1453 for(i = 0; i < 2; i++) {
1454 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1455 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1456 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1460 for(channel = 0; channel < 2; channel++) {
1461 for(i = 0; i < 2; i++) {
1462 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
1463 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1464 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1468 if(0 != encoder->private_->abs_residual_unaligned) {
1469 free(encoder->private_->abs_residual_unaligned);
1470 encoder->private_->abs_residual_unaligned = 0;
1472 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1473 free(encoder->private_->abs_residual_partition_sums_unaligned);
1474 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1476 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
1477 free(encoder->private_->raw_bits_per_partition_unaligned);
1478 encoder->private_->raw_bits_per_partition_unaligned = 0;
1480 if(encoder->protected_->verify) {
1481 for(i = 0; i < encoder->protected_->channels; i++) {
1482 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1483 free(encoder->private_->verify.input_fifo.data[i]);
1484 encoder->private_->verify.input_fifo.data[i] = 0;
1488 FLAC__bitbuffer_free(encoder->private_->frame);
1491 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
1494 unsigned i, channel;
1496 FLAC__ASSERT(new_size > 0);
1497 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1498 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1500 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1501 if(new_size <= encoder->private_->input_capacity)
1506 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1507 * requires that the input arrays (in our case the integer signals)
1508 * have a buffer of up to 3 zeroes in front (at negative indices) for
1509 * alignment purposes; we use 4 to keep the data well-aligned.
1512 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1513 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1514 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1515 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1516 encoder->private_->integer_signal[i] += 4;
1518 for(i = 0; ok && i < 2; i++) {
1519 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]);
1520 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]);
1521 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1522 encoder->private_->integer_signal_mid_side[i] += 4;
1524 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1525 for(i = 0; ok && i < 2; i++) {
1526 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1529 for(channel = 0; ok && channel < 2; channel++) {
1530 for(i = 0; ok && i < 2; i++) {
1531 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]);
1534 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1535 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 */
1536 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1537 if(encoder->protected_->do_escape_coding)
1538 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
1541 encoder->private_->input_capacity = new_size;
1543 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1548 FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
1550 const FLAC__byte *buffer;
1553 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1555 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1557 if(encoder->protected_->verify) {
1558 encoder->private_->verify.output.data = buffer;
1559 encoder->private_->verify.output.bytes = bytes;
1560 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1561 encoder->private_->verify.needs_magic_hack = true;
1564 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1565 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1566 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1567 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1573 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1574 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1578 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1581 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1582 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1588 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1590 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1593 * Accumulate raw signal to the MD5 signature
1595 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)) {
1596 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1601 * Process the frame header and subframes into the frame bitbuffer
1603 if(!process_subframes_(encoder, is_last_frame)) {
1604 /* the above function sets the state for us in case of an error */
1609 * Zero-pad the frame to a byte_boundary
1611 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1612 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1617 * CRC-16 the whole thing
1619 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1620 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
1625 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1626 /* the above function sets the state for us in case of an error */
1631 * Get ready for the next frame
1633 encoder->private_->current_sample_number = 0;
1634 encoder->private_->current_frame_number++;
1635 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1640 FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1642 FLAC__FrameHeader frame_header;
1643 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1644 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1647 * Calculate the min,max Rice partition orders
1650 max_partition_order = 0;
1653 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1654 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
1656 min_partition_order = min(min_partition_order, max_partition_order);
1658 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1663 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1664 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1667 frame_header.blocksize = encoder->protected_->blocksize;
1668 frame_header.sample_rate = encoder->protected_->sample_rate;
1669 frame_header.channels = encoder->protected_->channels;
1670 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1671 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
1672 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1673 frame_header.number.frame_number = encoder->private_->current_frame_number;
1676 * Figure out what channel assignments to try
1678 if(encoder->protected_->do_mid_side_stereo) {
1679 if(encoder->protected_->loose_mid_side_stereo) {
1680 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
1681 do_independent = true;
1685 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1686 do_mid_side = !do_independent;
1690 do_independent = true;
1695 do_independent = true;
1696 do_mid_side = false;
1699 FLAC__ASSERT(do_independent || do_mid_side);
1702 * Check for wasted bits; set effective bps for each subframe
1704 if(do_independent) {
1705 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1706 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
1707 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1708 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
1712 FLAC__ASSERT(encoder->protected_->channels == 2);
1713 for(channel = 0; channel < 2; channel++) {
1714 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
1715 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1716 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
1721 * First do a normal encoding pass of each independent channel
1723 if(do_independent) {
1724 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1728 min_partition_order,
1729 max_partition_order,
1730 precompute_partition_sums,
1733 encoder->private_->subframe_bps[channel],
1734 encoder->private_->integer_signal[channel],
1735 encoder->private_->real_signal[channel],
1736 encoder->private_->subframe_workspace_ptr[channel],
1737 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1738 encoder->private_->residual_workspace[channel],
1739 encoder->private_->best_subframe+channel,
1740 encoder->private_->best_subframe_bits+channel
1748 * Now do mid and side channels if requested
1751 FLAC__ASSERT(encoder->protected_->channels == 2);
1753 for(channel = 0; channel < 2; channel++) {
1757 min_partition_order,
1758 max_partition_order,
1759 precompute_partition_sums,
1762 encoder->private_->subframe_bps_mid_side[channel],
1763 encoder->private_->integer_signal_mid_side[channel],
1764 encoder->private_->real_signal_mid_side[channel],
1765 encoder->private_->subframe_workspace_ptr_mid_side[channel],
1766 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
1767 encoder->private_->residual_workspace_mid_side[channel],
1768 encoder->private_->best_subframe_mid_side+channel,
1769 encoder->private_->best_subframe_bits_mid_side+channel
1777 * Compose the frame bitbuffer
1780 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1781 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1782 FLAC__ChannelAssignment channel_assignment;
1784 FLAC__ASSERT(encoder->protected_->channels == 2);
1786 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1787 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1790 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1792 FLAC__ChannelAssignment ca;
1794 FLAC__ASSERT(do_independent && do_mid_side);
1796 /* We have to figure out which channel assignent results in the smallest frame */
1797 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
1798 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
1799 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
1800 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
1802 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
1803 if(bits[ca] < min_bits) {
1804 min_bits = bits[ca];
1805 channel_assignment = ca;
1810 frame_header.channel_assignment = channel_assignment;
1812 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1813 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1817 switch(channel_assignment) {
1818 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1819 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1820 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1822 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1823 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1824 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1826 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1827 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1828 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
1830 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1831 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1832 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1838 switch(channel_assignment) {
1839 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1840 left_bps = encoder->private_->subframe_bps [0];
1841 right_bps = encoder->private_->subframe_bps [1];
1843 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1844 left_bps = encoder->private_->subframe_bps [0];
1845 right_bps = encoder->private_->subframe_bps_mid_side[1];
1847 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1848 left_bps = encoder->private_->subframe_bps_mid_side[1];
1849 right_bps = encoder->private_->subframe_bps [1];
1851 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1852 left_bps = encoder->private_->subframe_bps_mid_side[0];
1853 right_bps = encoder->private_->subframe_bps_mid_side[1];
1859 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1860 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
1862 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
1866 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
1867 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1871 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1872 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
1873 /* the above function sets the state for us in case of an error */
1879 if(encoder->protected_->loose_mid_side_stereo) {
1880 encoder->private_->loose_mid_side_stereo_frame_count++;
1881 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1882 encoder->private_->loose_mid_side_stereo_frame_count = 0;
1885 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
1890 FLAC__bool process_subframe_(
1891 FLAC__StreamEncoder *encoder,
1892 unsigned min_partition_order,
1893 unsigned max_partition_order,
1894 FLAC__bool precompute_partition_sums,
1895 FLAC__bool verbatim_only,
1896 const FLAC__FrameHeader *frame_header,
1897 unsigned subframe_bps,
1898 const FLAC__int32 integer_signal[],
1899 const FLAC__real real_signal[],
1900 FLAC__Subframe *subframe[2],
1901 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
1902 FLAC__int32 *residual[2],
1903 unsigned *best_subframe,
1907 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1908 FLAC__real lpc_residual_bits_per_sample;
1909 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 */
1910 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1911 unsigned min_lpc_order, max_lpc_order, lpc_order;
1912 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1913 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1914 unsigned rice_parameter;
1915 unsigned _candidate_bits, _best_bits;
1916 unsigned _best_subframe;
1918 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1920 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1922 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1923 /* check for constant subframe */
1924 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);
1925 if(fixed_residual_bits_per_sample[1] == 0.0) {
1926 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1927 unsigned i, signal_is_constant = true;
1928 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1929 if(integer_signal[0] != integer_signal[i]) {
1930 signal_is_constant = false;
1934 if(signal_is_constant) {
1935 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1936 if(_candidate_bits < _best_bits) {
1937 _best_subframe = !_best_subframe;
1938 _best_bits = _candidate_bits;
1944 if(encoder->protected_->do_exhaustive_model_search) {
1945 min_fixed_order = 0;
1946 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1949 min_fixed_order = max_fixed_order = guess_fixed_order;
1951 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1952 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1953 continue; /* don't even try */
1954 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 */
1955 #ifndef FLAC__SYMMETRIC_RICE
1956 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1958 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1959 #ifdef DEBUG_VERBOSE
1960 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1962 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1965 evaluate_fixed_subframe_(
1968 residual[!_best_subframe],
1969 encoder->private_->abs_residual,
1970 encoder->private_->abs_residual_partition_sums,
1971 encoder->private_->raw_bits_per_partition,
1972 frame_header->blocksize,
1976 min_partition_order,
1977 max_partition_order,
1978 precompute_partition_sums,
1979 encoder->protected_->do_escape_coding,
1980 encoder->protected_->rice_parameter_search_dist,
1981 subframe[!_best_subframe],
1982 partitioned_rice_contents[!_best_subframe]
1984 if(_candidate_bits < _best_bits) {
1985 _best_subframe = !_best_subframe;
1986 _best_bits = _candidate_bits;
1991 if(encoder->protected_->max_lpc_order > 0) {
1992 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
1993 max_lpc_order = frame_header->blocksize-1;
1995 max_lpc_order = encoder->protected_->max_lpc_order;
1996 if(max_lpc_order > 0) {
1997 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
1998 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
1999 if(autoc[0] != 0.0) {
2000 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2001 if(encoder->protected_->do_exhaustive_model_search) {
2005 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
2006 min_lpc_order = max_lpc_order = guess_lpc_order;
2008 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2009 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2010 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
2011 continue; /* don't even try */
2012 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2013 #ifndef FLAC__SYMMETRIC_RICE
2014 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2016 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2017 #ifdef DEBUG_VERBOSE
2018 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2020 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2022 if(encoder->protected_->do_qlp_coeff_prec_search) {
2023 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2024 /* ensure a 32-bit datapath throughout for 16bps or less */
2025 if(subframe_bps <= 16)
2026 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2028 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2031 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2033 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2035 evaluate_lpc_subframe_(
2038 residual[!_best_subframe],
2039 encoder->private_->abs_residual,
2040 encoder->private_->abs_residual_partition_sums,
2041 encoder->private_->raw_bits_per_partition,
2042 encoder->private_->lp_coeff[lpc_order-1],
2043 frame_header->blocksize,
2046 qlp_coeff_precision,
2048 min_partition_order,
2049 max_partition_order,
2050 precompute_partition_sums,
2051 encoder->protected_->do_escape_coding,
2052 encoder->protected_->rice_parameter_search_dist,
2053 subframe[!_best_subframe],
2054 partitioned_rice_contents[!_best_subframe]
2056 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2057 if(_candidate_bits < _best_bits) {
2058 _best_subframe = !_best_subframe;
2059 _best_bits = _candidate_bits;
2070 *best_subframe = _best_subframe;
2071 *best_bits = _best_bits;
2076 FLAC__bool add_subframe_(
2077 FLAC__StreamEncoder *encoder,
2078 const FLAC__FrameHeader *frame_header,
2079 unsigned subframe_bps,
2080 const FLAC__Subframe *subframe,
2081 FLAC__BitBuffer *frame
2084 switch(subframe->type) {
2085 case FLAC__SUBFRAME_TYPE_CONSTANT:
2086 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
2087 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2091 case FLAC__SUBFRAME_TYPE_FIXED:
2092 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2093 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2097 case FLAC__SUBFRAME_TYPE_LPC:
2098 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2099 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2103 case FLAC__SUBFRAME_TYPE_VERBATIM:
2104 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2105 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
2116 unsigned evaluate_constant_subframe_(
2117 const FLAC__int32 signal,
2118 unsigned subframe_bps,
2119 FLAC__Subframe *subframe
2122 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2123 subframe->data.constant.value = signal;
2125 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
2128 unsigned evaluate_fixed_subframe_(
2129 FLAC__StreamEncoder *encoder,
2130 const FLAC__int32 signal[],
2131 FLAC__int32 residual[],
2132 FLAC__uint32 abs_residual[],
2133 FLAC__uint64 abs_residual_partition_sums[],
2134 unsigned raw_bits_per_partition[],
2136 unsigned subframe_bps,
2138 unsigned rice_parameter,
2139 unsigned min_partition_order,
2140 unsigned max_partition_order,
2141 FLAC__bool precompute_partition_sums,
2142 FLAC__bool do_escape_coding,
2143 unsigned rice_parameter_search_dist,
2144 FLAC__Subframe *subframe,
2145 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2148 unsigned i, residual_bits;
2149 const unsigned residual_samples = blocksize - order;
2151 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2153 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2155 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2156 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2157 subframe->data.fixed.residual = residual;
2160 find_best_partition_order_(
2164 abs_residual_partition_sums,
2165 raw_bits_per_partition,
2169 min_partition_order,
2170 max_partition_order,
2171 precompute_partition_sums,
2173 rice_parameter_search_dist,
2174 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2177 subframe->data.fixed.order = order;
2178 for(i = 0; i < order; i++)
2179 subframe->data.fixed.warmup[i] = signal[i];
2181 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
2184 unsigned evaluate_lpc_subframe_(
2185 FLAC__StreamEncoder *encoder,
2186 const FLAC__int32 signal[],
2187 FLAC__int32 residual[],
2188 FLAC__uint32 abs_residual[],
2189 FLAC__uint64 abs_residual_partition_sums[],
2190 unsigned raw_bits_per_partition[],
2191 const FLAC__real lp_coeff[],
2193 unsigned subframe_bps,
2195 unsigned qlp_coeff_precision,
2196 unsigned rice_parameter,
2197 unsigned min_partition_order,
2198 unsigned max_partition_order,
2199 FLAC__bool precompute_partition_sums,
2200 FLAC__bool do_escape_coding,
2201 unsigned rice_parameter_search_dist,
2202 FLAC__Subframe *subframe,
2203 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2206 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
2207 unsigned i, residual_bits;
2208 int quantization, ret;
2209 const unsigned residual_samples = blocksize - order;
2211 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2212 if(subframe_bps <= 16) {
2213 FLAC__ASSERT(order > 0);
2214 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2215 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2218 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
2220 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2222 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2223 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2224 else if(subframe_bps + qlp_coeff_precision + order <= 32)
2225 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2227 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2229 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2231 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
2232 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
2233 subframe->data.lpc.residual = residual;
2236 find_best_partition_order_(
2240 abs_residual_partition_sums,
2241 raw_bits_per_partition,
2245 min_partition_order,
2246 max_partition_order,
2247 precompute_partition_sums,
2249 rice_parameter_search_dist,
2250 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2253 subframe->data.lpc.order = order;
2254 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2255 subframe->data.lpc.quantization_level = quantization;
2256 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
2257 for(i = 0; i < order; i++)
2258 subframe->data.lpc.warmup[i] = signal[i];
2260 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;
2263 unsigned evaluate_verbatim_subframe_(
2264 const FLAC__int32 signal[],
2266 unsigned subframe_bps,
2267 FLAC__Subframe *subframe
2270 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2272 subframe->data.verbatim.data = signal;
2274 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
2277 unsigned find_best_partition_order_(
2278 FLAC__StreamEncoderPrivate *private_,
2279 const FLAC__int32 residual[],
2280 FLAC__uint32 abs_residual[],
2281 FLAC__uint64 abs_residual_partition_sums[],
2282 unsigned raw_bits_per_partition[],
2283 unsigned residual_samples,
2284 unsigned predictor_order,
2285 unsigned rice_parameter,
2286 unsigned min_partition_order,
2287 unsigned max_partition_order,
2288 FLAC__bool precompute_partition_sums,
2289 FLAC__bool do_escape_coding,
2290 unsigned rice_parameter_search_dist,
2291 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2295 unsigned residual_bits, best_residual_bits = 0;
2296 unsigned residual_sample;
2297 unsigned best_parameters_index = 0;
2298 const unsigned blocksize = residual_samples + predictor_order;
2300 /* compute abs(residual) for use later */
2301 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2302 r = residual[residual_sample];
2303 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2306 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2307 min_partition_order = min(min_partition_order, max_partition_order);
2309 if(precompute_partition_sums) {
2310 int partition_order;
2313 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2315 if(do_escape_coding)
2316 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2318 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2319 #ifdef DONT_ESTIMATE_RICE_BITS
2321 set_partitioned_rice_with_precompute_(
2323 abs_residual_partition_sums+sum,
2324 raw_bits_per_partition+sum,
2328 rice_parameter_search_dist,
2329 (unsigned)partition_order,
2331 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2337 set_partitioned_rice_with_precompute_(
2339 abs_residual_partition_sums+sum,
2340 raw_bits_per_partition+sum,
2344 rice_parameter_search_dist,
2345 (unsigned)partition_order,
2347 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2353 FLAC__ASSERT(best_residual_bits != 0);
2356 sum += 1u << partition_order;
2357 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2358 best_residual_bits = residual_bits;
2359 best_parameters_index = !best_parameters_index;
2360 best_partitioned_rice->order = partition_order;
2365 unsigned partition_order;
2366 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2367 #ifdef DONT_ESTIMATE_RICE_BITS
2369 set_partitioned_rice_(
2375 rice_parameter_search_dist,
2377 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2383 set_partitioned_rice_(
2388 rice_parameter_search_dist,
2390 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2396 FLAC__ASSERT(best_residual_bits != 0);
2399 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2400 best_residual_bits = residual_bits;
2401 best_parameters_index = !best_parameters_index;
2402 best_partitioned_rice->order = partition_order;
2408 * We are allowed to de-const the pointer based on our special knowledge;
2409 * it is const to the outside world.
2412 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2413 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2414 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2415 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2418 return best_residual_bits;
2421 void precompute_partition_info_sums_(
2422 const FLAC__uint32 abs_residual[],
2423 FLAC__uint64 abs_residual_partition_sums[],
2424 unsigned residual_samples,
2425 unsigned predictor_order,
2426 unsigned min_partition_order,
2427 unsigned max_partition_order
2430 int partition_order;
2431 unsigned from_partition, to_partition = 0;
2432 const unsigned blocksize = residual_samples + predictor_order;
2434 /* first do max_partition_order */
2435 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2436 FLAC__uint64 abs_residual_partition_sum;
2438 unsigned partition, partition_sample, partition_samples, residual_sample;
2439 const unsigned partitions = 1u << partition_order;
2440 const unsigned default_partition_samples = blocksize >> partition_order;
2442 FLAC__ASSERT(default_partition_samples > predictor_order);
2444 for(partition = residual_sample = 0; partition < partitions; partition++) {
2445 partition_samples = default_partition_samples;
2447 partition_samples -= predictor_order;
2448 abs_residual_partition_sum = 0;
2449 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2450 abs_r = abs_residual[residual_sample];
2451 abs_residual_partition_sum += abs_r;
2454 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2456 to_partition = partitions;
2460 /* now merge partitions for lower orders */
2461 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2464 const unsigned partitions = 1u << partition_order;
2465 for(i = 0; i < partitions; i++) {
2466 s = abs_residual_partition_sums[from_partition];
2468 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
2475 void precompute_partition_info_escapes_(
2476 const FLAC__int32 residual[],
2477 unsigned raw_bits_per_partition[],
2478 unsigned residual_samples,
2479 unsigned predictor_order,
2480 unsigned min_partition_order,
2481 unsigned max_partition_order
2484 int partition_order;
2485 unsigned from_partition, to_partition = 0;
2486 const unsigned blocksize = residual_samples + predictor_order;
2488 /* first do max_partition_order */
2489 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2490 FLAC__int32 r, residual_partition_min, residual_partition_max;
2491 unsigned silog2_min, silog2_max;
2492 unsigned partition, partition_sample, partition_samples, residual_sample;
2493 const unsigned partitions = 1u << partition_order;
2494 const unsigned default_partition_samples = blocksize >> partition_order;
2496 FLAC__ASSERT(default_partition_samples > predictor_order);
2498 for(partition = residual_sample = 0; partition < partitions; partition++) {
2499 partition_samples = default_partition_samples;
2501 partition_samples -= predictor_order;
2502 residual_partition_min = residual_partition_max = 0;
2503 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2504 r = residual[residual_sample];
2505 if(r < residual_partition_min)
2506 residual_partition_min = r;
2507 else if(r > residual_partition_max)
2508 residual_partition_max = r;
2511 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2512 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2513 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
2515 to_partition = partitions;
2519 /* now merge partitions for lower orders */
2520 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2523 const unsigned partitions = 1u << partition_order;
2524 for(i = 0; i < partitions; i++) {
2525 m = raw_bits_per_partition[from_partition];
2527 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2534 #ifdef VARIABLE_RICE_BITS
2535 #undef VARIABLE_RICE_BITS
2537 #ifndef DONT_ESTIMATE_RICE_BITS
2538 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
2541 #ifdef DONT_ESTIMATE_RICE_BITS
2542 FLAC__bool set_partitioned_rice_(
2543 const FLAC__uint32 abs_residual[],
2544 const FLAC__int32 residual[],
2545 const unsigned residual_samples,
2546 const unsigned predictor_order,
2547 const unsigned suggested_rice_parameter,
2548 const unsigned rice_parameter_search_dist,
2549 const unsigned partition_order,
2550 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2554 FLAC__bool set_partitioned_rice_(
2555 const FLAC__uint32 abs_residual[],
2556 const unsigned residual_samples,
2557 const unsigned predictor_order,
2558 const unsigned suggested_rice_parameter,
2559 const unsigned rice_parameter_search_dist,
2560 const unsigned partition_order,
2561 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2566 unsigned rice_parameter, partition_bits;
2567 #ifndef NO_RICE_SEARCH
2568 unsigned best_partition_bits;
2569 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2571 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2572 unsigned *parameters;
2574 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2576 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2577 parameters = partitioned_rice_contents->parameters;
2579 if(partition_order == 0) {
2582 #ifndef NO_RICE_SEARCH
2583 if(rice_parameter_search_dist) {
2584 if(suggested_rice_parameter < rice_parameter_search_dist)
2585 min_rice_parameter = 0;
2587 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2588 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2589 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2590 #ifdef DEBUG_VERBOSE
2591 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2593 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2597 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2599 best_partition_bits = 0xffffffff;
2600 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2602 #ifdef VARIABLE_RICE_BITS
2603 #ifdef FLAC__SYMMETRIC_RICE
2604 partition_bits = (2+rice_parameter) * residual_samples;
2606 const unsigned rice_parameter_estimate = rice_parameter-1;
2607 partition_bits = (1+rice_parameter) * residual_samples;
2612 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2613 for(i = 0; i < residual_samples; i++) {
2614 #ifdef VARIABLE_RICE_BITS
2615 #ifdef FLAC__SYMMETRIC_RICE
2616 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2618 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2621 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2624 #ifndef NO_RICE_SEARCH
2625 if(partition_bits < best_partition_bits) {
2626 best_rice_parameter = rice_parameter;
2627 best_partition_bits = partition_bits;
2631 parameters[0] = best_rice_parameter;
2632 bits_ += best_partition_bits;
2635 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2636 unsigned partition_samples;
2637 FLAC__uint64 mean, k;
2638 const unsigned partitions = 1u << partition_order;
2639 for(partition = residual_sample = 0; partition < partitions; partition++) {
2640 partition_samples = (residual_samples+predictor_order) >> partition_order;
2641 if(partition == 0) {
2642 if(partition_samples <= predictor_order)
2645 partition_samples -= predictor_order;
2648 save_residual_sample = residual_sample;
2649 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2650 mean += abs_residual[residual_sample];
2651 residual_sample = save_residual_sample;
2652 #ifdef FLAC__SYMMETRIC_RICE
2653 mean += partition_samples >> 1; /* for rounding effect */
2654 mean /= partition_samples;
2656 /* calc rice_parameter = floor(log2(mean)) */
2664 /* calc rice_parameter ala LOCO-I */
2665 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2668 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2669 #ifdef DEBUG_VERBOSE
2670 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2672 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2675 #ifndef NO_RICE_SEARCH
2676 if(rice_parameter_search_dist) {
2677 if(rice_parameter < rice_parameter_search_dist)
2678 min_rice_parameter = 0;
2680 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2681 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2682 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2683 #ifdef DEBUG_VERBOSE
2684 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2686 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2690 min_rice_parameter = max_rice_parameter = rice_parameter;
2692 best_partition_bits = 0xffffffff;
2693 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2695 #ifdef VARIABLE_RICE_BITS
2696 #ifdef FLAC__SYMMETRIC_RICE
2697 partition_bits = (2+rice_parameter) * partition_samples;
2699 const unsigned rice_parameter_estimate = rice_parameter-1;
2700 partition_bits = (1+rice_parameter) * partition_samples;
2705 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2706 save_residual_sample = residual_sample;
2707 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2708 #ifdef VARIABLE_RICE_BITS
2709 #ifdef FLAC__SYMMETRIC_RICE
2710 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2712 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2715 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2718 #ifndef NO_RICE_SEARCH
2719 if(rice_parameter != max_rice_parameter)
2720 residual_sample = save_residual_sample;
2721 if(partition_bits < best_partition_bits) {
2722 best_rice_parameter = rice_parameter;
2723 best_partition_bits = partition_bits;
2727 parameters[partition] = best_rice_parameter;
2728 bits_ += best_partition_bits;
2736 #ifdef DONT_ESTIMATE_RICE_BITS
2737 FLAC__bool set_partitioned_rice_with_precompute_(
2738 const FLAC__int32 residual[],
2739 const FLAC__uint64 abs_residual_partition_sums[],
2740 const unsigned raw_bits_per_partition[],
2741 const unsigned residual_samples,
2742 const unsigned predictor_order,
2743 const unsigned suggested_rice_parameter,
2744 const unsigned rice_parameter_search_dist,
2745 const unsigned partition_order,
2746 const FLAC__bool search_for_escapes,
2747 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2751 FLAC__bool set_partitioned_rice_with_precompute_(
2752 const FLAC__uint32 abs_residual[],
2753 const FLAC__uint64 abs_residual_partition_sums[],
2754 const unsigned raw_bits_per_partition[],
2755 const unsigned residual_samples,
2756 const unsigned predictor_order,
2757 const unsigned suggested_rice_parameter,
2758 const unsigned rice_parameter_search_dist,
2759 const unsigned partition_order,
2760 const FLAC__bool search_for_escapes,
2761 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2766 unsigned rice_parameter, partition_bits;
2767 #ifndef NO_RICE_SEARCH
2768 unsigned best_partition_bits;
2769 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2772 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2773 unsigned *parameters, *raw_bits;
2775 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2777 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2778 parameters = partitioned_rice_contents->parameters;
2779 raw_bits = partitioned_rice_contents->raw_bits;
2781 if(partition_order == 0) {
2784 #ifndef NO_RICE_SEARCH
2785 if(rice_parameter_search_dist) {
2786 if(suggested_rice_parameter < rice_parameter_search_dist)
2787 min_rice_parameter = 0;
2789 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2790 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2791 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2792 #ifdef DEBUG_VERBOSE
2793 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2795 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2799 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2801 best_partition_bits = 0xffffffff;
2802 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2804 #ifdef VARIABLE_RICE_BITS
2805 #ifdef FLAC__SYMMETRIC_RICE
2806 partition_bits = (2+rice_parameter) * residual_samples;
2808 const unsigned rice_parameter_estimate = rice_parameter-1;
2809 partition_bits = (1+rice_parameter) * residual_samples;
2814 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2815 for(i = 0; i < residual_samples; i++) {
2816 #ifdef VARIABLE_RICE_BITS
2817 #ifdef FLAC__SYMMETRIC_RICE
2818 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2820 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2823 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2826 #ifndef NO_RICE_SEARCH
2827 if(partition_bits < best_partition_bits) {
2828 best_rice_parameter = rice_parameter;
2829 best_partition_bits = partition_bits;
2833 if(search_for_escapes) {
2834 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;
2835 if(flat_bits <= best_partition_bits) {
2836 raw_bits[0] = raw_bits_per_partition[0];
2837 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2838 best_partition_bits = flat_bits;
2841 parameters[0] = best_rice_parameter;
2842 bits_ += best_partition_bits;
2845 unsigned partition, residual_sample, save_residual_sample, partition_sample;
2846 unsigned partition_samples;
2847 FLAC__uint64 mean, k;
2848 const unsigned partitions = 1u << partition_order;
2849 for(partition = residual_sample = 0; partition < partitions; partition++) {
2850 partition_samples = (residual_samples+predictor_order) >> partition_order;
2851 if(partition == 0) {
2852 if(partition_samples <= predictor_order)
2855 partition_samples -= predictor_order;
2857 mean = abs_residual_partition_sums[partition];
2858 #ifdef FLAC__SYMMETRIC_RICE
2859 mean += partition_samples >> 1; /* for rounding effect */
2860 mean /= partition_samples;
2862 /* calc rice_parameter = floor(log2(mean)) */
2870 /* calc rice_parameter ala LOCO-I */
2871 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2874 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2875 #ifdef DEBUG_VERBOSE
2876 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2878 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2881 #ifndef NO_RICE_SEARCH
2882 if(rice_parameter_search_dist) {
2883 if(rice_parameter < rice_parameter_search_dist)
2884 min_rice_parameter = 0;
2886 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2887 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2888 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2889 #ifdef DEBUG_VERBOSE
2890 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2892 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2896 min_rice_parameter = max_rice_parameter = rice_parameter;
2898 best_partition_bits = 0xffffffff;
2899 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2901 #ifdef VARIABLE_RICE_BITS
2902 #ifdef FLAC__SYMMETRIC_RICE
2903 partition_bits = (2+rice_parameter) * partition_samples;
2905 const unsigned rice_parameter_estimate = rice_parameter-1;
2906 partition_bits = (1+rice_parameter) * partition_samples;
2911 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2912 save_residual_sample = residual_sample;
2913 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2914 #ifdef VARIABLE_RICE_BITS
2915 #ifdef FLAC__SYMMETRIC_RICE
2916 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2918 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2921 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
2924 #ifndef NO_RICE_SEARCH
2925 if(rice_parameter != max_rice_parameter)
2926 residual_sample = save_residual_sample;
2927 if(partition_bits < best_partition_bits) {
2928 best_rice_parameter = rice_parameter;
2929 best_partition_bits = partition_bits;
2933 if(search_for_escapes) {
2934 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;
2935 if(flat_bits <= best_partition_bits) {
2936 raw_bits[partition] = raw_bits_per_partition[partition];
2937 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2938 best_partition_bits = flat_bits;
2941 parameters[partition] = best_rice_parameter;
2942 bits_ += best_partition_bits;
2950 unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
2955 for(i = 0; i < samples && !(x&1); i++)
2962 for(shift = 0; !(x&1); shift++)
2967 for(i = 0; i < samples; i++)
2968 signal[i] >>= shift;
2974 void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
2978 for(channel = 0; channel < channels; channel++)
2979 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
2981 fifo->tail += wide_samples;
2983 FLAC__ASSERT(fifo->tail <= fifo->size);
2986 void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
2989 unsigned sample, wide_sample;
2990 unsigned tail = fifo->tail;
2992 sample = input_offset * channels;
2993 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
2994 for(channel = 0; channel < channels; channel++)
2995 fifo->data[channel][tail] = input[sample++];
3000 FLAC__ASSERT(fifo->tail <= fifo->size);
3003 FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3005 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3006 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3009 if(encoder->private_->verify.needs_magic_hack) {
3010 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3011 *bytes = FLAC__STREAM_SYNC_LENGTH;
3012 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3013 encoder->private_->verify.needs_magic_hack = false;
3016 if(encoded_bytes == 0) {
3018 * If we get here, a FIFO underflow has occurred,
3019 * which means there is a bug somewhere.
3022 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3024 else if(encoded_bytes < *bytes)
3025 *bytes = encoded_bytes;
3026 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3027 encoder->private_->verify.output.data += *bytes;
3028 encoder->private_->verify.output.bytes -= *bytes;
3031 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3034 FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3036 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3038 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3039 const unsigned blocksize = frame->header.blocksize;
3040 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3042 for(channel = 0; channel < channels; channel++) {
3043 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3044 unsigned i, sample = 0;
3045 FLAC__int32 expect = 0, got = 0;
3047 for(i = 0; i < blocksize; i++) {
3048 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3050 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3051 got = (FLAC__int32)buffer[channel][i];
3055 FLAC__ASSERT(i < blocksize);
3056 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3057 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
3058 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
3059 encoder->private_->verify.error_stats.channel = channel;
3060 encoder->private_->verify.error_stats.sample = sample;
3061 encoder->private_->verify.error_stats.expected = expect;
3062 encoder->private_->verify.error_stats.got = got;
3063 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3064 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3067 /* dequeue the frame from the fifo */
3068 for(channel = 0; channel < channels; channel++) {
3069 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3071 encoder->private_->verify.input_fifo.tail -= blocksize;
3072 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3075 void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3077 (void)decoder, (void)metadata, (void)client_data;
3080 void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3082 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3083 (void)decoder, (void)status;
3084 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;