1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001 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/seek_table.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/stream_encoder_framing.h"
31 #include "private/fixed.h"
32 #include "private/lpc.h"
33 #include "private/md5.h"
34 #include "private/memory.h"
39 #define min(x,y) ((x)<(y)?(x):(y))
44 #define max(x,y) ((x)>(y)?(x):(y))
46 /***********************************************************************
48 * Private class method prototypes
50 ***********************************************************************/
52 static FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
53 static FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
54 static FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
55 static FLAC__bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
56 static FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
57 static unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
58 static unsigned stream_encoder_evaluate_fixed_subframe_(const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
59 static unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
60 static unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
61 static unsigned stream_encoder_find_best_partition_order_(const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]);
62 static void stream_encoder_precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
63 static void stream_encoder_precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
64 #ifdef DONT_ESTIMATE_RICE_BITS
65 static FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits);
66 static FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
68 static FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits);
69 static FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
71 static unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
73 /***********************************************************************
77 ***********************************************************************/
79 typedef struct FLAC__StreamEncoderPrivate {
80 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
81 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
82 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
83 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
84 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
85 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
86 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
87 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
88 FLAC__int32 *residual_workspace_mid_side[2][2];
89 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
90 FLAC__Subframe subframe_workspace_mid_side[2][2];
91 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
92 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
93 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
94 unsigned best_subframe_mid_side[2];
95 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
96 unsigned best_subframe_bits_mid_side[2];
97 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
98 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
99 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
100 FLAC__BitBuffer frame; /* the current frame being worked on */
101 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
102 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
103 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
104 FLAC__ChannelAssignment last_channel_assignment;
105 FLAC__StreamMetaData metadata;
106 unsigned current_sample_number;
107 unsigned current_frame_number;
108 struct MD5Context md5context;
109 FLAC__CPUInfo cpuinfo;
110 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
111 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
112 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[]);
113 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[]);
114 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
115 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
116 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
117 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
118 FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
119 void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
121 /* unaligned (original) pointers to allocated data */
122 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
123 FLAC__int32 *integer_signal_mid_side_unaligned[2];
124 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
125 FLAC__real *real_signal_mid_side_unaligned[2];
126 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
127 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
128 FLAC__uint32 *abs_residual_unaligned;
129 FLAC__uint64 *abs_residual_partition_sums_unaligned;
130 unsigned *raw_bits_per_partition_unaligned;
131 } FLAC__StreamEncoderPrivate;
133 /***********************************************************************
135 * Public static class data
137 ***********************************************************************/
139 const char *FLAC__StreamEncoderStateString[] = {
140 "FLAC__STREAM_ENCODER_OK",
141 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
142 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
143 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
144 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
145 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
146 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
147 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
148 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
149 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
150 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
151 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
152 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
153 "FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE",
154 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
155 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
156 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
157 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
158 "FLAC__STREAM_ENCODER_UNINITIALIZED"
161 const char *FLAC__StreamEncoderWriteStatusString[] = {
162 "FLAC__STREAM_ENCODER_WRITE_OK",
163 "FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR"
166 /***********************************************************************
168 * Class constructor/destructor
170 ***********************************************************************/
171 FLAC__StreamEncoder *FLAC__stream_encoder_new()
173 FLAC__StreamEncoder *encoder;
175 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
177 encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
181 encoder->protected = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
182 if(encoder->protected == 0) {
186 encoder->private = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
187 if(encoder->private == 0) {
188 free(encoder->protected);
193 encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
195 encoder->protected->streamable_subset = true;
196 encoder->protected->do_mid_side_stereo = false;
197 encoder->protected->loose_mid_side_stereo = false;
198 encoder->protected->channels = 2;
199 encoder->protected->bits_per_sample = 16;
200 encoder->protected->sample_rate = 44100;
201 encoder->protected->blocksize = 1152;
202 encoder->protected->max_lpc_order = 0;
203 encoder->protected->qlp_coeff_precision = 0;
204 encoder->protected->do_qlp_coeff_prec_search = false;
205 encoder->protected->do_exhaustive_model_search = false;
206 encoder->protected->do_escape_coding = false;
207 encoder->protected->min_residual_partition_order = 0;
208 encoder->protected->max_residual_partition_order = 0;
209 encoder->protected->rice_parameter_search_dist = 0;
210 encoder->protected->total_samples_estimate = 0;
211 encoder->protected->seek_table = 0;
212 encoder->protected->padding = 0;
213 encoder->protected->last_metadata_is_last = true;
215 encoder->private->write_callback = 0;
216 encoder->private->metadata_callback = 0;
217 encoder->private->client_data = 0;
222 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
224 FLAC__ASSERT(encoder != 0);
225 FLAC__ASSERT(encoder->protected != 0);
226 FLAC__ASSERT(encoder->private != 0);
228 free(encoder->private);
229 free(encoder->protected);
233 /***********************************************************************
235 * Public class methods
237 ***********************************************************************/
239 FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
242 FLAC__StreamMetaData padding_block;
243 FLAC__StreamMetaData seek_table_block;
245 FLAC__ASSERT(encoder != 0);
247 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
248 return encoder->protected->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
250 encoder->protected->state = FLAC__STREAM_ENCODER_OK;
252 if(0 == encoder->private->write_callback || 0 == encoder->private->metadata_callback)
253 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
255 if(encoder->protected->channels == 0 || encoder->protected->channels > FLAC__MAX_CHANNELS)
256 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
258 if(encoder->protected->do_mid_side_stereo && encoder->protected->channels != 2)
259 return encoder->protected->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
261 if(encoder->protected->loose_mid_side_stereo && !encoder->protected->do_mid_side_stereo)
262 return encoder->protected->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
264 if(encoder->protected->bits_per_sample >= 32)
265 encoder->protected->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
267 if(encoder->protected->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
268 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
270 if(encoder->protected->sample_rate == 0 || encoder->protected->sample_rate > FLAC__MAX_SAMPLE_RATE)
271 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
273 if(encoder->protected->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected->blocksize > FLAC__MAX_BLOCK_SIZE)
274 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
276 if(encoder->protected->blocksize < encoder->protected->max_lpc_order)
277 return encoder->protected->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
279 if(encoder->protected->qlp_coeff_precision == 0) {
280 if(encoder->protected->bits_per_sample < 16) {
281 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
282 /* @@@ until then we'll make a guess */
283 encoder->protected->qlp_coeff_precision = max(5, 2 + encoder->protected->bits_per_sample / 2);
285 else if(encoder->protected->bits_per_sample == 16) {
286 if(encoder->protected->blocksize <= 192)
287 encoder->protected->qlp_coeff_precision = 7;
288 else if(encoder->protected->blocksize <= 384)
289 encoder->protected->qlp_coeff_precision = 8;
290 else if(encoder->protected->blocksize <= 576)
291 encoder->protected->qlp_coeff_precision = 9;
292 else if(encoder->protected->blocksize <= 1152)
293 encoder->protected->qlp_coeff_precision = 10;
294 else if(encoder->protected->blocksize <= 2304)
295 encoder->protected->qlp_coeff_precision = 11;
296 else if(encoder->protected->blocksize <= 4608)
297 encoder->protected->qlp_coeff_precision = 12;
299 encoder->protected->qlp_coeff_precision = 13;
302 encoder->protected->qlp_coeff_precision = min(13, 8*sizeof(FLAC__int32) - encoder->protected->bits_per_sample - 1 - 2); /* @@@ -2 to keep things 32-bit safe */
305 else if(encoder->protected->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected->qlp_coeff_precision + encoder->protected->bits_per_sample >= 8*sizeof(FLAC__uint32) || encoder->protected->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
306 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
308 if(encoder->protected->streamable_subset) {
309 /*@@@ add check for blocksize here */
310 if(encoder->protected->bits_per_sample != 8 && encoder->protected->bits_per_sample != 12 && encoder->protected->bits_per_sample != 16 && encoder->protected->bits_per_sample != 20 && encoder->protected->bits_per_sample != 24)
311 return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
312 if(encoder->protected->sample_rate > 655350)
313 return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
316 if(encoder->protected->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
317 encoder->protected->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
318 if(encoder->protected->min_residual_partition_order >= encoder->protected->max_residual_partition_order)
319 encoder->protected->min_residual_partition_order = encoder->protected->max_residual_partition_order;
321 encoder->private->input_capacity = 0;
322 for(i = 0; i < encoder->protected->channels; i++) {
323 encoder->private->integer_signal_unaligned[i] = encoder->private->integer_signal[i] = 0;
324 encoder->private->real_signal_unaligned[i] = encoder->private->real_signal[i] = 0;
326 for(i = 0; i < 2; i++) {
327 encoder->private->integer_signal_mid_side_unaligned[i] = encoder->private->integer_signal_mid_side[i] = 0;
328 encoder->private->real_signal_mid_side_unaligned[i] = encoder->private->real_signal_mid_side[i] = 0;
330 for(i = 0; i < encoder->protected->channels; i++) {
331 encoder->private->residual_workspace_unaligned[i][0] = encoder->private->residual_workspace[i][0] = 0;
332 encoder->private->residual_workspace_unaligned[i][1] = encoder->private->residual_workspace[i][1] = 0;
333 encoder->private->best_subframe[i] = 0;
335 for(i = 0; i < 2; i++) {
336 encoder->private->residual_workspace_mid_side_unaligned[i][0] = encoder->private->residual_workspace_mid_side[i][0] = 0;
337 encoder->private->residual_workspace_mid_side_unaligned[i][1] = encoder->private->residual_workspace_mid_side[i][1] = 0;
338 encoder->private->best_subframe_mid_side[i] = 0;
340 for(i = 0; i < encoder->protected->channels; i++) {
341 encoder->private->subframe_workspace_ptr[i][0] = &encoder->private->subframe_workspace[i][0];
342 encoder->private->subframe_workspace_ptr[i][1] = &encoder->private->subframe_workspace[i][1];
344 for(i = 0; i < 2; i++) {
345 encoder->private->subframe_workspace_ptr_mid_side[i][0] = &encoder->private->subframe_workspace_mid_side[i][0];
346 encoder->private->subframe_workspace_ptr_mid_side[i][1] = &encoder->private->subframe_workspace_mid_side[i][1];
348 encoder->private->abs_residual_unaligned = encoder->private->abs_residual = 0;
349 encoder->private->abs_residual_partition_sums_unaligned = encoder->private->abs_residual_partition_sums = 0;
350 encoder->private->raw_bits_per_partition_unaligned = encoder->private->raw_bits_per_partition = 0;
351 encoder->private->loose_mid_side_stereo_frames_exact = (double)encoder->protected->sample_rate * 0.4 / (double)encoder->protected->blocksize;
352 encoder->private->loose_mid_side_stereo_frames = (unsigned)(encoder->private->loose_mid_side_stereo_frames_exact + 0.5);
353 if(encoder->private->loose_mid_side_stereo_frames == 0)
354 encoder->private->loose_mid_side_stereo_frames = 1;
355 encoder->private->loose_mid_side_stereo_frame_count = 0;
356 encoder->private->current_sample_number = 0;
357 encoder->private->current_frame_number = 0;
359 encoder->private->use_wide_by_block = (encoder->protected->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected->blocksize)+1 > 30);
360 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? */
361 encoder->private->use_wide_by_partition = (false); /*@@@ need to set this */
364 * get the CPU info and set the function pointers
366 FLAC__cpu_info(&encoder->private->cpuinfo);
367 /* first default to the non-asm routines */
368 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
369 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
370 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
371 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
372 /* now override with asm where appropriate */
374 FLAC__ASSERT(encoder->private->cpuinfo.use_asm);
375 #ifdef FLAC__CPU_IA32
376 FLAC__ASSERT(encoder->private->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
377 #ifdef FLAC__HAS_NASM
378 if(0 && encoder->private->cpuinfo.data.ia32.sse) { /*@@@ SSE version lacks necessary resolution, plus SSE flag doesn't check for OS support */
379 if(encoder->protected->max_lpc_order < 4)
380 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
381 else if(encoder->protected->max_lpc_order < 8)
382 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
383 else if(encoder->protected->max_lpc_order < 12)
384 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
386 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
388 else if(0 && encoder->private->cpuinfo.data.ia32._3dnow) /*@@@ turn back on in first beta after 1.0 */
389 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
391 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
392 if(encoder->private->cpuinfo.data.ia32.mmx && encoder->private->cpuinfo.data.ia32.cmov)
393 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
394 if(encoder->private->cpuinfo.data.ia32.mmx) {
395 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
396 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
399 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
400 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
405 /* finally override based on wide-ness if necessary */
406 if(encoder->private->use_wide_by_block) {
407 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
410 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
411 encoder->private->precompute_partition_sums = (encoder->protected->max_residual_partition_order > encoder->protected->min_residual_partition_order) || encoder->protected->do_escape_coding;
413 if(!stream_encoder_resize_buffers_(encoder, encoder->protected->blocksize)) {
414 /* the above function sets the state for us in case of an error */
415 return encoder->protected->state;
417 FLAC__bitbuffer_init(&encoder->private->frame);
420 * write the stream header
423 if(!FLAC__bitbuffer_clear(&encoder->private->frame))
424 return encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
425 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
426 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
428 encoder->private->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
429 encoder->private->metadata.is_last = (encoder->protected->seek_table == 0 && encoder->protected->padding == 0 && encoder->protected->last_metadata_is_last);
430 encoder->private->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
431 encoder->private->metadata.data.stream_info.min_blocksize = encoder->protected->blocksize; /* this encoder uses the same blocksize for the whole stream */
432 encoder->private->metadata.data.stream_info.max_blocksize = encoder->protected->blocksize;
433 encoder->private->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
434 encoder->private->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
435 encoder->private->metadata.data.stream_info.sample_rate = encoder->protected->sample_rate;
436 encoder->private->metadata.data.stream_info.channels = encoder->protected->channels;
437 encoder->private->metadata.data.stream_info.bits_per_sample = encoder->protected->bits_per_sample;
438 encoder->private->metadata.data.stream_info.total_samples = encoder->protected->total_samples_estimate; /* we will replace this later with the real total */
439 memset(encoder->private->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
440 MD5Init(&encoder->private->md5context);
441 if(!FLAC__add_metadata_block(&encoder->private->metadata, &encoder->private->frame))
442 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
444 if(0 != encoder->protected->seek_table) {
445 if(!FLAC__seek_table_is_valid(encoder->protected->seek_table))
446 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE;
447 seek_table_block.type = FLAC__METADATA_TYPE_SEEKTABLE;
448 seek_table_block.is_last = (encoder->protected->padding == 0 && encoder->protected->last_metadata_is_last);
449 seek_table_block.length = encoder->protected->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
450 seek_table_block.data.seek_table = *encoder->protected->seek_table;
451 if(!FLAC__add_metadata_block(&seek_table_block, &encoder->private->frame))
452 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
455 /* add a PADDING block if requested */
456 if(encoder->protected->padding > 0) {
457 padding_block.type = FLAC__METADATA_TYPE_PADDING;
458 padding_block.is_last = encoder->protected->last_metadata_is_last;
459 padding_block.length = encoder->protected->padding;
460 if(!FLAC__add_metadata_block(&padding_block, &encoder->private->frame))
461 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
464 FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned before writing */
465 FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
466 if(encoder->private->write_callback(encoder, encoder->private->frame.buffer, encoder->private->frame.bytes, 0, encoder->private->current_frame_number, encoder->private->client_data) != FLAC__STREAM_ENCODER_WRITE_OK)
467 return encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
469 /* now that the metadata block is written, we can init this to an absurdly-high value... */
470 encoder->private->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
471 /* ... and clear this to 0 */
472 encoder->private->metadata.data.stream_info.total_samples = 0;
474 return encoder->protected->state;
477 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
481 FLAC__ASSERT(encoder != 0);
482 if(encoder->protected->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
484 if(encoder->private->current_sample_number != 0) {
485 encoder->protected->blocksize = encoder->private->current_sample_number;
486 stream_encoder_process_frame_(encoder, true); /* true => is last frame */
488 MD5Final(encoder->private->metadata.data.stream_info.md5sum, &encoder->private->md5context);
489 encoder->private->metadata_callback(encoder, &encoder->private->metadata, encoder->private->client_data);
490 for(i = 0; i < encoder->protected->channels; i++) {
491 if(encoder->private->integer_signal_unaligned[i] != 0) {
492 free(encoder->private->integer_signal_unaligned[i]);
493 encoder->private->integer_signal_unaligned[i] = 0;
495 if(encoder->private->real_signal_unaligned[i] != 0) {
496 free(encoder->private->real_signal_unaligned[i]);
497 encoder->private->real_signal_unaligned[i] = 0;
500 for(i = 0; i < 2; i++) {
501 if(encoder->private->integer_signal_mid_side_unaligned[i] != 0) {
502 free(encoder->private->integer_signal_mid_side_unaligned[i]);
503 encoder->private->integer_signal_mid_side_unaligned[i] = 0;
505 if(encoder->private->real_signal_mid_side_unaligned[i] != 0) {
506 free(encoder->private->real_signal_mid_side_unaligned[i]);
507 encoder->private->real_signal_mid_side_unaligned[i] = 0;
510 for(channel = 0; channel < encoder->protected->channels; channel++) {
511 for(i = 0; i < 2; i++) {
512 if(encoder->private->residual_workspace_unaligned[channel][i] != 0) {
513 free(encoder->private->residual_workspace_unaligned[channel][i]);
514 encoder->private->residual_workspace_unaligned[channel][i] = 0;
518 for(channel = 0; channel < 2; channel++) {
519 for(i = 0; i < 2; i++) {
520 if(encoder->private->residual_workspace_mid_side_unaligned[channel][i] != 0) {
521 free(encoder->private->residual_workspace_mid_side_unaligned[channel][i]);
522 encoder->private->residual_workspace_mid_side_unaligned[channel][i] = 0;
526 if(encoder->private->abs_residual_unaligned != 0) {
527 free(encoder->private->abs_residual_unaligned);
528 encoder->private->abs_residual_unaligned = 0;
530 if(encoder->private->abs_residual_partition_sums_unaligned != 0) {
531 free(encoder->private->abs_residual_partition_sums_unaligned);
532 encoder->private->abs_residual_partition_sums_unaligned = 0;
534 if(encoder->private->raw_bits_per_partition_unaligned != 0) {
535 free(encoder->private->raw_bits_per_partition_unaligned);
536 encoder->private->raw_bits_per_partition_unaligned = 0;
538 FLAC__bitbuffer_free(&encoder->private->frame);
540 encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
543 FLAC__bool FLAC__stream_encoder_set_streamable_subset(const FLAC__StreamEncoder *encoder, FLAC__bool value)
545 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
547 encoder->protected->streamable_subset = value;
551 FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(const FLAC__StreamEncoder *encoder, FLAC__bool value)
553 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
555 encoder->protected->do_mid_side_stereo = value;
559 FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder, FLAC__bool value)
561 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
563 encoder->protected->loose_mid_side_stereo = value;
567 FLAC__bool FLAC__stream_encoder_set_channels(const FLAC__StreamEncoder *encoder, unsigned value)
569 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
571 encoder->protected->channels = value;
575 FLAC__bool FLAC__stream_encoder_set_bits_per_sample(const FLAC__StreamEncoder *encoder, unsigned value)
577 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
579 encoder->protected->bits_per_sample = value;
583 FLAC__bool FLAC__stream_encoder_set_sample_rate(const FLAC__StreamEncoder *encoder, unsigned value)
585 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
587 encoder->protected->sample_rate = value;
591 FLAC__bool FLAC__stream_encoder_set_blocksize(const FLAC__StreamEncoder *encoder, unsigned value)
593 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
595 encoder->protected->blocksize = value;
599 FLAC__bool FLAC__stream_encoder_set_max_lpc_order(const FLAC__StreamEncoder *encoder, unsigned value)
601 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
603 encoder->protected->max_lpc_order = value;
607 FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(const FLAC__StreamEncoder *encoder, unsigned value)
609 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
611 encoder->protected->qlp_coeff_precision = value;
615 FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder, FLAC__bool value)
617 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
619 encoder->protected->do_qlp_coeff_prec_search = value;
623 FLAC__bool FLAC__stream_encoder_set_do_escape_coding(const FLAC__StreamEncoder *encoder, FLAC__bool value)
625 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
627 encoder->protected->do_escape_coding = value;
631 FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder, FLAC__bool value)
633 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
635 encoder->protected->do_exhaustive_model_search = value;
639 FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value)
641 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
643 encoder->protected->min_residual_partition_order = value;
647 FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value)
649 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
651 encoder->protected->max_residual_partition_order = value;
655 FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder, unsigned value)
657 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
659 encoder->protected->rice_parameter_search_dist = value;
663 FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(const FLAC__StreamEncoder *encoder, FLAC__uint64 value)
665 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
667 encoder->protected->total_samples_estimate = value;
671 FLAC__bool FLAC__stream_encoder_set_seek_table(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData_SeekTable *value)
673 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
675 encoder->protected->seek_table = value;
679 FLAC__bool FLAC__stream_encoder_set_padding(const FLAC__StreamEncoder *encoder, unsigned value)
681 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
683 encoder->protected->padding = value;
687 FLAC__bool FLAC__stream_encoder_set_last_metadata_is_last(const FLAC__StreamEncoder *encoder, FLAC__bool value)
689 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
691 encoder->protected->last_metadata_is_last = value;
695 FLAC__bool FLAC__stream_encoder_set_write_callback(const FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteStatus (*value)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data))
697 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
699 encoder->private->write_callback = value;
703 FLAC__bool FLAC__stream_encoder_set_metadata_callback(const FLAC__StreamEncoder *encoder, void (*value)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data))
705 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
707 encoder->private->metadata_callback = value;
711 FLAC__bool FLAC__stream_encoder_set_client_data(const FLAC__StreamEncoder *encoder, void *value)
713 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
715 encoder->private->client_data = value;
719 FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
721 return encoder->protected->state;
724 FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
726 return encoder->protected->streamable_subset;
729 FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
731 return encoder->protected->do_mid_side_stereo;
734 FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
736 return encoder->protected->loose_mid_side_stereo;
739 unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
741 return encoder->protected->channels;
744 unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
746 return encoder->protected->bits_per_sample;
749 unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
751 return encoder->protected->sample_rate;
754 unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
756 return encoder->protected->blocksize;
759 unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
761 return encoder->protected->max_lpc_order;
764 unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
766 return encoder->protected->qlp_coeff_precision;
769 FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
771 return encoder->protected->do_qlp_coeff_prec_search;
774 FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
776 return encoder->protected->do_escape_coding;
779 FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
781 return encoder->protected->do_exhaustive_model_search;
784 unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
786 return encoder->protected->min_residual_partition_order;
789 unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
791 return encoder->protected->max_residual_partition_order;
794 unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
796 return encoder->protected->rice_parameter_search_dist;
799 FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 *buf[], unsigned samples)
801 unsigned i, j, channel;
802 FLAC__int32 x, mid, side;
803 const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
805 FLAC__ASSERT(encoder != 0);
806 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
809 if(encoder->protected->do_mid_side_stereo && channels == 2) {
811 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
812 x = mid = side = buf[0][j];
813 encoder->private->integer_signal[0][i] = x;
814 encoder->private->real_signal[0][i] = (FLAC__real)x;
816 encoder->private->integer_signal[1][i] = x;
817 encoder->private->real_signal[1][i] = (FLAC__real)x;
820 mid >>= 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
821 encoder->private->integer_signal_mid_side[1][i] = side;
822 encoder->private->integer_signal_mid_side[0][i] = mid;
823 encoder->private->real_signal_mid_side[1][i] = (FLAC__real)side;
824 encoder->private->real_signal_mid_side[0][i] = (FLAC__real)mid;
825 encoder->private->current_sample_number++;
828 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
831 } while(j < samples);
835 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
836 for(channel = 0; channel < channels; channel++) {
838 encoder->private->integer_signal[channel][i] = x;
839 encoder->private->real_signal[channel][i] = (FLAC__real)x;
841 encoder->private->current_sample_number++;
844 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
847 } while(j < samples);
853 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
854 FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buf[], unsigned samples)
856 unsigned i, j, k, channel;
857 FLAC__int32 x, mid, side;
858 const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
860 FLAC__ASSERT(encoder != 0);
861 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
864 if(encoder->protected->do_mid_side_stereo && channels == 2) {
866 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
867 x = mid = side = buf[k++];
868 encoder->private->integer_signal[0][i] = x;
869 encoder->private->real_signal[0][i] = (FLAC__real)x;
871 encoder->private->integer_signal[1][i] = x;
872 encoder->private->real_signal[1][i] = (FLAC__real)x;
875 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
876 encoder->private->integer_signal_mid_side[1][i] = side;
877 encoder->private->integer_signal_mid_side[0][i] = mid;
878 encoder->private->real_signal_mid_side[1][i] = (FLAC__real)side;
879 encoder->private->real_signal_mid_side[0][i] = (FLAC__real)mid;
880 encoder->private->current_sample_number++;
883 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
886 } while(j < samples);
890 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
891 for(channel = 0; channel < channels; channel++) {
893 encoder->private->integer_signal[channel][i] = x;
894 encoder->private->real_signal[channel][i] = (FLAC__real)x;
896 encoder->private->current_sample_number++;
899 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
902 } while(j < samples);
908 FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
913 FLAC__ASSERT(new_size > 0);
914 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
915 FLAC__ASSERT(encoder->private->current_sample_number == 0);
917 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
918 if(new_size <= encoder->private->input_capacity)
923 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx() requires that the input arrays (in our case the integer signals) have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
925 for(i = 0; ok && i < encoder->protected->channels; i++) {
926 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private->integer_signal_unaligned[i], &encoder->private->integer_signal[i]);
927 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private->real_signal_unaligned[i], &encoder->private->real_signal[i]);
928 memset(encoder->private->integer_signal[i], 0, sizeof(FLAC__int32)*4);
929 encoder->private->integer_signal[i] += 4;
931 for(i = 0; ok && i < 2; i++) {
932 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]);
933 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]);
934 memset(encoder->private->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
935 encoder->private->integer_signal_mid_side[i] += 4;
937 for(channel = 0; ok && channel < encoder->protected->channels; channel++) {
938 for(i = 0; ok && i < 2; i++) {
939 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->residual_workspace_unaligned[channel][i], &encoder->private->residual_workspace[channel][i]);
942 for(channel = 0; ok && channel < 2; channel++) {
943 for(i = 0; ok && i < 2; i++) {
944 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]);
947 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private->abs_residual_unaligned, &encoder->private->abs_residual);
948 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 */
949 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private->abs_residual_partition_sums_unaligned, &encoder->private->abs_residual_partition_sums);
950 if(encoder->protected->do_escape_coding)
951 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private->raw_bits_per_partition_unaligned, &encoder->private->raw_bits_per_partition);
954 encoder->private->input_capacity = new_size;
956 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
961 /***********************************************************************
963 * Private class methods
965 ***********************************************************************/
967 FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
969 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
972 * Accumulate raw signal to the MD5 signature
974 /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */
975 if(!FLAC__MD5Accumulate(&encoder->private->md5context, encoder->private->integer_signal, encoder->protected->channels, encoder->protected->blocksize, (encoder->protected->bits_per_sample+7) / 8)) {
976 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
981 * Process the frame header and subframes into the frame bitbuffer
983 if(!stream_encoder_process_subframes_(encoder, is_last_frame)) {
984 /* the above function sets the state for us in case of an error */
989 * Zero-pad the frame to a byte_boundary
991 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->private->frame)) {
992 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
997 * CRC-16 the whole thing
999 FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned */
1000 FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
1001 FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__crc16(encoder->private->frame.buffer, encoder->private->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
1006 if(encoder->private->write_callback(encoder, encoder->private->frame.buffer, encoder->private->frame.bytes, encoder->protected->blocksize, encoder->private->current_frame_number, encoder->private->client_data) != FLAC__STREAM_ENCODER_WRITE_OK) {
1007 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
1012 * Get ready for the next frame
1014 encoder->private->current_sample_number = 0;
1015 encoder->private->current_frame_number++;
1016 encoder->private->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected->blocksize;
1017 encoder->private->metadata.data.stream_info.min_framesize = min(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.min_framesize);
1018 encoder->private->metadata.data.stream_info.max_framesize = max(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.max_framesize);
1023 FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1025 FLAC__FrameHeader frame_header;
1026 unsigned channel, min_partition_order = encoder->protected->min_residual_partition_order, max_partition_order;
1027 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
1030 * Calculate the min,max Rice partition orders
1033 max_partition_order = 0;
1036 unsigned limit = 0, b = encoder->protected->blocksize;
1041 max_partition_order = min(encoder->protected->max_residual_partition_order, limit);
1043 min_partition_order = min(min_partition_order, max_partition_order);
1045 precompute_partition_sums = encoder->private->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected->do_escape_coding);
1050 if(!FLAC__bitbuffer_clear(&encoder->private->frame)) {
1051 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1054 frame_header.blocksize = encoder->protected->blocksize;
1055 frame_header.sample_rate = encoder->protected->sample_rate;
1056 frame_header.channels = encoder->protected->channels;
1057 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1058 frame_header.bits_per_sample = encoder->protected->bits_per_sample;
1059 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
1060 frame_header.number.frame_number = encoder->private->current_frame_number;
1063 * Figure out what channel assignments to try
1065 if(encoder->protected->do_mid_side_stereo) {
1066 if(encoder->protected->loose_mid_side_stereo) {
1067 if(encoder->private->loose_mid_side_stereo_frame_count == 0) {
1068 do_independent = true;
1072 do_independent = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1073 do_mid_side = !do_independent;
1077 do_independent = true;
1082 do_independent = true;
1083 do_mid_side = false;
1086 FLAC__ASSERT(do_independent || do_mid_side);
1089 * Check for wasted bits; set effective bps for each subframe
1091 if(do_independent) {
1093 for(channel = 0; channel < encoder->protected->channels; channel++) {
1094 w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal[channel], encoder->protected->blocksize);
1095 encoder->private->subframe_workspace[channel][0].wasted_bits = encoder->private->subframe_workspace[channel][1].wasted_bits = w;
1096 encoder->private->subframe_bps[channel] = encoder->protected->bits_per_sample - w;
1101 FLAC__ASSERT(encoder->protected->channels == 2);
1102 for(channel = 0; channel < 2; channel++) {
1103 w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal_mid_side[channel], encoder->protected->blocksize);
1104 encoder->private->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1105 encoder->private->subframe_bps_mid_side[channel] = encoder->protected->bits_per_sample - w + (channel==0? 0:1);
1110 * First do a normal encoding pass of each independent channel
1112 if(do_independent) {
1113 for(channel = 0; channel < encoder->protected->channels; channel++) {
1114 if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private->subframe_bps[channel], encoder->private->integer_signal[channel], encoder->private->real_signal[channel], encoder->private->subframe_workspace_ptr[channel], encoder->private->residual_workspace[channel], encoder->private->best_subframe+channel, encoder->private->best_subframe_bits+channel))
1120 * Now do mid and side channels if requested
1123 FLAC__ASSERT(encoder->protected->channels == 2);
1125 for(channel = 0; channel < 2; channel++) {
1126 if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private->subframe_bps_mid_side[channel], encoder->private->integer_signal_mid_side[channel], encoder->private->real_signal_mid_side[channel], encoder->private->subframe_workspace_ptr_mid_side[channel], encoder->private->residual_workspace_mid_side[channel], encoder->private->best_subframe_mid_side+channel, encoder->private->best_subframe_bits_mid_side+channel))
1132 * Compose the frame bitbuffer
1135 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1136 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1137 FLAC__ChannelAssignment channel_assignment;
1139 FLAC__ASSERT(encoder->protected->channels == 2);
1141 if(encoder->protected->loose_mid_side_stereo && encoder->private->loose_mid_side_stereo_frame_count > 0) {
1142 channel_assignment = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1145 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1147 FLAC__ChannelAssignment ca;
1149 FLAC__ASSERT(do_independent && do_mid_side);
1151 /* We have to figure out which channel assignent results in the smallest frame */
1152 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits [1];
1153 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits_mid_side[1];
1154 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private->best_subframe_bits [1] + encoder->private->best_subframe_bits_mid_side[1];
1155 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private->best_subframe_bits_mid_side[0] + encoder->private->best_subframe_bits_mid_side[1];
1157 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
1158 if(bits[ca] < min_bits) {
1159 min_bits = bits[ca];
1160 channel_assignment = ca;
1165 frame_header.channel_assignment = channel_assignment;
1167 if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
1168 encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1172 switch(channel_assignment) {
1173 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1174 left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]];
1175 right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]];
1177 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1178 left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]];
1179 right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
1181 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1182 left_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
1183 right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]];
1185 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1186 left_subframe = &encoder->private->subframe_workspace_mid_side[0][encoder->private->best_subframe_mid_side[0]];
1187 right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
1193 switch(channel_assignment) {
1194 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1195 left_bps = encoder->private->subframe_bps [0];
1196 right_bps = encoder->private->subframe_bps [1];
1198 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1199 left_bps = encoder->private->subframe_bps [0];
1200 right_bps = encoder->private->subframe_bps_mid_side[1];
1202 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1203 left_bps = encoder->private->subframe_bps_mid_side[1];
1204 right_bps = encoder->private->subframe_bps [1];
1206 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1207 left_bps = encoder->private->subframe_bps_mid_side[0];
1208 right_bps = encoder->private->subframe_bps_mid_side[1];
1214 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1215 if(!stream_encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->private->frame))
1217 if(!stream_encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->private->frame))
1221 if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
1222 encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1226 for(channel = 0; channel < encoder->protected->channels; channel++) {
1227 if(!stream_encoder_add_subframe_(encoder, &frame_header, encoder->private->subframe_bps[channel], &encoder->private->subframe_workspace[channel][encoder->private->best_subframe[channel]], &encoder->private->frame)) {
1228 /* the above function sets the state for us in case of an error */
1234 if(encoder->protected->loose_mid_side_stereo) {
1235 encoder->private->loose_mid_side_stereo_frame_count++;
1236 if(encoder->private->loose_mid_side_stereo_frame_count >= encoder->private->loose_mid_side_stereo_frames)
1237 encoder->private->loose_mid_side_stereo_frame_count = 0;
1240 encoder->private->last_channel_assignment = frame_header.channel_assignment;
1245 FLAC__bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
1247 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1248 FLAC__real lpc_residual_bits_per_sample;
1249 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 */
1250 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
1251 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1252 unsigned min_lpc_order, max_lpc_order, lpc_order;
1253 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1254 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1255 unsigned rice_parameter;
1256 unsigned _candidate_bits, _best_bits;
1257 unsigned _best_subframe;
1259 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1261 _best_bits = stream_encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1263 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1264 /* check for constant subframe */
1265 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);
1266 if(fixed_residual_bits_per_sample[1] == 0.0) {
1267 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1268 unsigned i, signal_is_constant = true;
1269 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1270 if(integer_signal[0] != integer_signal[i]) {
1271 signal_is_constant = false;
1275 if(signal_is_constant) {
1276 _candidate_bits = stream_encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1277 if(_candidate_bits < _best_bits) {
1278 _best_subframe = !_best_subframe;
1279 _best_bits = _candidate_bits;
1285 if(encoder->protected->do_exhaustive_model_search) {
1286 min_fixed_order = 0;
1287 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1290 min_fixed_order = max_fixed_order = guess_fixed_order;
1292 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1293 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1294 continue; /* don't even try */
1295 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 */
1296 #ifndef FLAC__SYMMETRIC_RICE
1297 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1299 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1301 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1303 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1305 _candidate_bits = stream_encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->private->abs_residual, encoder->private->abs_residual_partition_sums, encoder->private->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected->do_escape_coding, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]);
1306 if(_candidate_bits < _best_bits) {
1307 _best_subframe = !_best_subframe;
1308 _best_bits = _candidate_bits;
1313 if(encoder->protected->max_lpc_order > 0) {
1314 if(encoder->protected->max_lpc_order >= frame_header->blocksize)
1315 max_lpc_order = frame_header->blocksize-1;
1317 max_lpc_order = encoder->protected->max_lpc_order;
1318 if(max_lpc_order > 0) {
1319 encoder->private->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
1320 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
1321 if(autoc[0] != 0.0) {
1322 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
1323 if(encoder->protected->do_exhaustive_model_search) {
1327 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
1328 min_lpc_order = max_lpc_order = guess_lpc_order;
1330 if(encoder->protected->do_qlp_coeff_prec_search) {
1331 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
1332 max_qlp_coeff_precision = min(8*sizeof(FLAC__int32) - subframe_bps - 1 - 2, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1); /* -2 to keep things 32-bit safe */
1335 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected->qlp_coeff_precision;
1337 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
1338 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
1339 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
1340 continue; /* don't even try */
1341 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
1342 #ifndef FLAC__SYMMETRIC_RICE
1343 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1345 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1347 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1349 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1351 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
1352 _candidate_bits = stream_encoder_evaluate_lpc_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private->abs_residual, encoder->private->abs_residual_partition_sums, encoder->private->raw_bits_per_partition, lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected->do_escape_coding, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]);
1353 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1354 if(_candidate_bits < _best_bits) {
1355 _best_subframe = !_best_subframe;
1356 _best_bits = _candidate_bits;
1367 *best_subframe = _best_subframe;
1368 *best_bits = _best_bits;
1373 FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
1375 switch(subframe->type) {
1376 case FLAC__SUBFRAME_TYPE_CONSTANT:
1377 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
1378 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1382 case FLAC__SUBFRAME_TYPE_FIXED:
1383 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
1384 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1388 case FLAC__SUBFRAME_TYPE_LPC:
1389 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
1390 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1394 case FLAC__SUBFRAME_TYPE_VERBATIM:
1395 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
1396 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1407 unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
1409 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1410 subframe->data.constant.value = signal;
1412 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
1415 unsigned stream_encoder_evaluate_fixed_subframe_(const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
1417 unsigned i, residual_bits;
1418 const unsigned residual_samples = blocksize - order;
1420 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1422 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1424 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1425 subframe->data.fixed.residual = residual;
1427 residual_bits = stream_encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, &subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.raw_bits);
1429 subframe->data.fixed.order = order;
1430 for(i = 0; i < order; i++)
1431 subframe->data.fixed.warmup[i] = signal[i];
1433 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
1436 unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
1438 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1439 unsigned i, residual_bits;
1440 int quantization, ret;
1441 const unsigned residual_samples = blocksize - order;
1443 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
1445 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1447 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
1448 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1450 encoder->private->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1452 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1454 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1455 subframe->data.lpc.residual = residual;
1457 residual_bits = stream_encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, &subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.raw_bits);
1459 subframe->data.lpc.order = order;
1460 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1461 subframe->data.lpc.quantization_level = quantization;
1462 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
1463 for(i = 0; i < order; i++)
1464 subframe->data.lpc.warmup[i] = signal[i];
1466 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;
1469 unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
1471 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1473 subframe->data.verbatim.data = signal;
1475 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
1478 unsigned stream_encoder_find_best_partition_order_(const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[])
1481 unsigned residual_bits, best_residual_bits = 0;
1482 unsigned residual_sample;
1483 unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
1484 const unsigned blocksize = residual_samples + predictor_order;
1486 /* compute abs(residual) for use later */
1487 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1488 r = residual[residual_sample];
1489 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
1492 while(max_partition_order > 0 && blocksize >> max_partition_order <= predictor_order)
1493 max_partition_order--;
1494 FLAC__ASSERT(blocksize >> max_partition_order > predictor_order);
1495 min_partition_order = min(min_partition_order, max_partition_order);
1497 if(precompute_partition_sums) {
1498 int partition_order;
1501 stream_encoder_precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
1503 if(do_escape_coding)
1504 stream_encoder_precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
1506 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1507 #ifdef DONT_ESTIMATE_RICE_BITS
1508 if(!stream_encoder_set_partitioned_rice_with_precompute_(residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits))
1510 if(!stream_encoder_set_partitioned_rice_with_precompute_(abs_residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits))
1513 FLAC__ASSERT(best_residual_bits != 0);
1516 sum += 1u << partition_order;
1517 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1518 best_residual_bits = residual_bits;
1519 *best_partition_order = partition_order;
1520 best_parameters_index = !best_parameters_index;
1525 unsigned partition_order;
1526 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1527 #ifdef DONT_ESTIMATE_RICE_BITS
1528 if(!stream_encoder_set_partitioned_rice_(abs_residual, residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], &residual_bits))
1530 if(!stream_encoder_set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], &residual_bits))
1533 FLAC__ASSERT(best_residual_bits != 0);
1536 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1537 best_residual_bits = residual_bits;
1538 *best_partition_order = partition_order;
1539 best_parameters_index = !best_parameters_index;
1544 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1545 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1547 return best_residual_bits;
1550 void stream_encoder_precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
1552 int partition_order;
1553 unsigned from_partition, to_partition = 0;
1554 const unsigned blocksize = residual_samples + predictor_order;
1556 /* first do max_partition_order */
1557 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1558 FLAC__uint64 abs_residual_partition_sum;
1560 unsigned partition, partition_sample, partition_samples, residual_sample;
1561 const unsigned partitions = 1u << partition_order;
1562 const unsigned default_partition_samples = blocksize >> partition_order;
1564 FLAC__ASSERT(default_partition_samples > predictor_order);
1566 for(partition = residual_sample = 0; partition < partitions; partition++) {
1567 partition_samples = default_partition_samples;
1569 partition_samples -= predictor_order;
1570 abs_residual_partition_sum = 0;
1571 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1572 abs_r = abs_residual[residual_sample];
1573 abs_residual_partition_sum += abs_r;
1576 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
1578 to_partition = partitions;
1582 /* now merge partitions for lower orders */
1583 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1586 const unsigned partitions = 1u << partition_order;
1587 for(i = 0; i < partitions; i++) {
1588 s = abs_residual_partition_sums[from_partition];
1590 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1597 void stream_encoder_precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
1599 int partition_order;
1600 unsigned from_partition, to_partition = 0;
1601 const unsigned blocksize = residual_samples + predictor_order;
1603 /* first do max_partition_order */
1604 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1605 FLAC__int32 r, residual_partition_min, residual_partition_max;
1606 unsigned silog2_min, silog2_max;
1607 unsigned partition, partition_sample, partition_samples, residual_sample;
1608 const unsigned partitions = 1u << partition_order;
1609 const unsigned default_partition_samples = blocksize >> partition_order;
1611 FLAC__ASSERT(default_partition_samples > predictor_order);
1613 for(partition = residual_sample = 0; partition < partitions; partition++) {
1614 partition_samples = default_partition_samples;
1616 partition_samples -= predictor_order;
1617 residual_partition_min = residual_partition_max = 0;
1618 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1619 r = residual[residual_sample];
1620 if(r < residual_partition_min)
1621 residual_partition_min = r;
1622 else if(r > residual_partition_max)
1623 residual_partition_max = r;
1626 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
1627 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
1628 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
1630 to_partition = partitions;
1634 /* now merge partitions for lower orders */
1635 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1638 const unsigned partitions = 1u << partition_order;
1639 for(i = 0; i < partitions; i++) {
1640 m = raw_bits_per_partition[from_partition];
1642 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1649 #ifdef VARIABLE_RICE_BITS
1650 #undef VARIABLE_RICE_BITS
1652 #ifndef DONT_ESTIMATE_RICE_BITS
1653 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1656 #ifdef DONT_ESTIMATE_RICE_BITS
1657 FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits)
1659 FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits)
1662 unsigned rice_parameter, partition_bits;
1663 #ifndef NO_RICE_SEARCH
1664 unsigned best_partition_bits;
1665 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1667 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1669 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1671 if(partition_order == 0) {
1674 #ifndef NO_RICE_SEARCH
1675 if(rice_parameter_search_dist) {
1676 if(suggested_rice_parameter < rice_parameter_search_dist)
1677 min_rice_parameter = 0;
1679 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1680 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1681 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1683 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1685 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1689 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1691 best_partition_bits = 0xffffffff;
1692 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1694 #ifdef VARIABLE_RICE_BITS
1695 #ifdef FLAC__SYMMETRIC_RICE
1696 partition_bits = (2+rice_parameter) * residual_samples;
1698 const unsigned rice_parameter_estimate = rice_parameter-1;
1699 partition_bits = (1+rice_parameter) * residual_samples;
1704 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1705 for(i = 0; i < residual_samples; i++) {
1706 #ifdef VARIABLE_RICE_BITS
1707 #ifdef FLAC__SYMMETRIC_RICE
1708 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1710 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1713 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
1716 #ifndef NO_RICE_SEARCH
1717 if(partition_bits < best_partition_bits) {
1718 best_rice_parameter = rice_parameter;
1719 best_partition_bits = partition_bits;
1723 parameters[0] = best_rice_parameter;
1724 bits_ += best_partition_bits;
1727 unsigned partition, residual_sample, save_residual_sample, partition_sample;
1728 unsigned partition_samples;
1729 FLAC__uint64 mean, k;
1730 const unsigned partitions = 1u << partition_order;
1731 for(partition = residual_sample = 0; partition < partitions; partition++) {
1732 partition_samples = (residual_samples+predictor_order) >> partition_order;
1733 if(partition == 0) {
1734 if(partition_samples <= predictor_order)
1737 partition_samples -= predictor_order;
1740 save_residual_sample = residual_sample;
1741 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
1742 mean += abs_residual[residual_sample];
1743 residual_sample = save_residual_sample;
1744 #ifdef FLAC__SYMMETRIC_RICE
1745 mean += partition_samples >> 1; /* for rounding effect */
1746 mean /= partition_samples;
1748 /* calc rice_parameter = floor(log2(mean)) */
1756 /* calc rice_parameter ala LOCO-I */
1757 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
1760 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1762 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1764 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1767 #ifndef NO_RICE_SEARCH
1768 if(rice_parameter_search_dist) {
1769 if(rice_parameter < rice_parameter_search_dist)
1770 min_rice_parameter = 0;
1772 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1773 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
1774 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1776 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1778 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1782 min_rice_parameter = max_rice_parameter = rice_parameter;
1784 best_partition_bits = 0xffffffff;
1785 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1787 #ifdef VARIABLE_RICE_BITS
1788 #ifdef FLAC__SYMMETRIC_RICE
1789 partition_bits = (2+rice_parameter) * partition_samples;
1791 const unsigned rice_parameter_estimate = rice_parameter-1;
1792 partition_bits = (1+rice_parameter) * partition_samples;
1797 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1798 save_residual_sample = residual_sample;
1799 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
1800 #ifdef VARIABLE_RICE_BITS
1801 #ifdef FLAC__SYMMETRIC_RICE
1802 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
1804 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
1807 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
1810 #ifndef NO_RICE_SEARCH
1811 if(rice_parameter != max_rice_parameter)
1812 residual_sample = save_residual_sample;
1813 if(partition_bits < best_partition_bits) {
1814 best_rice_parameter = rice_parameter;
1815 best_partition_bits = partition_bits;
1819 parameters[partition] = best_rice_parameter;
1820 bits_ += best_partition_bits;
1828 #ifdef DONT_ESTIMATE_RICE_BITS
1829 FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
1831 FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
1834 unsigned rice_parameter, partition_bits;
1835 #ifndef NO_RICE_SEARCH
1836 unsigned best_partition_bits;
1837 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1840 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1842 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1844 if(partition_order == 0) {
1847 #ifndef NO_RICE_SEARCH
1848 if(rice_parameter_search_dist) {
1849 if(suggested_rice_parameter < rice_parameter_search_dist)
1850 min_rice_parameter = 0;
1852 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1853 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1854 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1856 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1858 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1862 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1864 best_partition_bits = 0xffffffff;
1865 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1867 #ifdef VARIABLE_RICE_BITS
1868 #ifdef FLAC__SYMMETRIC_RICE
1869 partition_bits = (2+rice_parameter) * residual_samples;
1871 const unsigned rice_parameter_estimate = rice_parameter-1;
1872 partition_bits = (1+rice_parameter) * residual_samples;
1877 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1878 for(i = 0; i < residual_samples; i++) {
1879 #ifdef VARIABLE_RICE_BITS
1880 #ifdef FLAC__SYMMETRIC_RICE
1881 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1883 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1886 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1889 #ifndef NO_RICE_SEARCH
1890 if(partition_bits < best_partition_bits) {
1891 best_rice_parameter = rice_parameter;
1892 best_partition_bits = partition_bits;
1896 if(search_for_escapes) {
1897 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;
1898 if(flat_bits <= best_partition_bits) {
1899 raw_bits[0] = raw_bits_per_partition[0];
1900 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1901 best_partition_bits = flat_bits;
1904 parameters[0] = best_rice_parameter;
1905 bits_ += best_partition_bits;
1908 unsigned partition, residual_sample, save_residual_sample, partition_sample;
1909 unsigned partition_samples;
1910 FLAC__uint64 mean, k;
1911 const unsigned partitions = 1u << partition_order;
1912 for(partition = residual_sample = 0; partition < partitions; partition++) {
1913 partition_samples = (residual_samples+predictor_order) >> partition_order;
1914 if(partition == 0) {
1915 if(partition_samples <= predictor_order)
1918 partition_samples -= predictor_order;
1920 mean = abs_residual_partition_sums[partition];
1921 #ifdef FLAC__SYMMETRIC_RICE
1922 mean += partition_samples >> 1; /* for rounding effect */
1923 mean /= partition_samples;
1925 /* calc rice_parameter = floor(log2(mean)) */
1933 /* calc rice_parameter ala LOCO-I */
1934 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
1937 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1939 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1941 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1944 #ifndef NO_RICE_SEARCH
1945 if(rice_parameter_search_dist) {
1946 if(rice_parameter < rice_parameter_search_dist)
1947 min_rice_parameter = 0;
1949 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1950 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
1951 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1953 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1955 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1959 min_rice_parameter = max_rice_parameter = rice_parameter;
1961 best_partition_bits = 0xffffffff;
1962 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1964 #ifdef VARIABLE_RICE_BITS
1965 #ifdef FLAC__SYMMETRIC_RICE
1966 partition_bits = (2+rice_parameter) * partition_samples;
1968 const unsigned rice_parameter_estimate = rice_parameter-1;
1969 partition_bits = (1+rice_parameter) * partition_samples;
1974 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1975 save_residual_sample = residual_sample;
1976 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
1977 #ifdef VARIABLE_RICE_BITS
1978 #ifdef FLAC__SYMMETRIC_RICE
1979 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
1981 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
1984 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1987 #ifndef NO_RICE_SEARCH
1988 if(rice_parameter != max_rice_parameter)
1989 residual_sample = save_residual_sample;
1990 if(partition_bits < best_partition_bits) {
1991 best_rice_parameter = rice_parameter;
1992 best_partition_bits = partition_bits;
1996 if(search_for_escapes) {
1997 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;
1998 if(flat_bits <= best_partition_bits) {
1999 raw_bits[partition] = raw_bits_per_partition[partition];
2000 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2001 best_partition_bits = flat_bits;
2004 parameters[partition] = best_rice_parameter;
2005 bits_ += best_partition_bits;
2013 unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
2018 for(i = 0; i < samples && !(x&1); i++)
2025 for(shift = 0; !(x&1); shift++)
2030 for(i = 0; i < samples; i++)
2031 signal[i] >>= shift;