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 bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
53 static bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, bool is_last_frame);
54 static bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, bool is_last_frame);
55 static bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
56 static 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 int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
58 static unsigned stream_encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
59 static unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
60 static unsigned stream_encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
61 static unsigned stream_encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 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, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]);
62 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
63 static unsigned stream_encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
65 static bool stream_encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 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, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
66 static unsigned stream_encoder_get_wasted_bits_(int32 signal[], unsigned samples);
68 /***********************************************************************
72 ***********************************************************************/
74 typedef struct FLAC__StreamEncoderPrivate {
75 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
76 int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
77 int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
78 real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
79 real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
80 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
81 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
82 int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
83 int32 *residual_workspace_mid_side[2][2];
84 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
85 FLAC__Subframe subframe_workspace_mid_side[2][2];
86 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
87 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
88 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
89 unsigned best_subframe_mid_side[2];
90 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
91 unsigned best_subframe_bits_mid_side[2];
92 uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
93 uint32 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
94 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
95 FLAC__BitBuffer frame; /* the current frame being worked on */
96 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
97 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
98 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
99 FLAC__ChannelAssignment last_channel_assignment;
100 FLAC__StreamMetaData metadata;
101 unsigned current_sample_number;
102 unsigned current_frame_number;
103 struct MD5Context md5context;
104 FLAC__CPUInfo cpuinfo;
105 unsigned (*local_fixed_compute_best_predictor)(const int32 data[], unsigned data_len, real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
106 void (*local_lpc_compute_autocorrelation)(const real data[], unsigned data_len, unsigned lag, real autoc[]);
107 void (*local_lpc_compute_residual_from_qlp_coefficients)(const int32 data[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 residual[]);
108 void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const int32 data[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 residual[]);
109 bool use_slow; /* use slow 64-bit versions of some functions */
110 FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
111 void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
113 /* unaligned (original) pointers to allocated data */
114 int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
115 int32 *integer_signal_mid_side_unaligned[2];
116 real *real_signal_unaligned[FLAC__MAX_CHANNELS];
117 real *real_signal_mid_side_unaligned[2];
118 int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
119 int32 *residual_workspace_mid_side_unaligned[2][2];
120 uint32 *abs_residual_unaligned;
121 uint32 *abs_residual_partition_sums_unaligned;
122 unsigned *raw_bits_per_partition_unaligned;
123 } FLAC__StreamEncoderPrivate;
125 /***********************************************************************
127 * Public static class data
129 ***********************************************************************/
131 const char *FLAC__StreamEncoderStateString[] = {
132 "FLAC__STREAM_ENCODER_OK",
133 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
134 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
135 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
136 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
137 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
138 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
139 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
140 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
141 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
142 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
143 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
144 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
145 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
146 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
147 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
148 "FLAC__STREAM_ENCODER_UNINITIALIZED"
151 const char *FLAC__StreamEncoderWriteStatusString[] = {
152 "FLAC__STREAM_ENCODER_WRITE_OK",
153 "FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR"
156 /***********************************************************************
158 * Class constructor/destructor
160 ***********************************************************************/
161 FLAC__StreamEncoder *FLAC__stream_encoder_new()
163 FLAC__StreamEncoder *encoder;
165 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
167 encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
171 encoder->protected = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
172 if(encoder->protected == 0) {
176 encoder->private = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
177 if(encoder->private == 0) {
178 free(encoder->protected);
183 encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
188 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
190 FLAC__ASSERT(encoder != 0);
191 FLAC__ASSERT(encoder->protected != 0);
192 FLAC__ASSERT(encoder->private != 0);
194 free(encoder->private);
195 free(encoder->protected);
199 /***********************************************************************
201 * Public class methods
203 ***********************************************************************/
205 FLAC__StreamEncoderState FLAC__stream_encoder_init(
206 FLAC__StreamEncoder *encoder,
207 bool streamable_subset,
208 bool do_mid_side_stereo,
209 bool loose_mid_side_stereo,
211 unsigned bits_per_sample,
212 unsigned sample_rate,
214 unsigned max_lpc_order,
215 unsigned qlp_coeff_precision,
216 bool do_qlp_coeff_prec_search,
217 bool do_exhaustive_model_search,
218 unsigned min_residual_partition_order,
219 unsigned max_residual_partition_order,
220 unsigned rice_parameter_search_dist,
221 uint64 total_samples_estimate,
222 const FLAC__StreamMetaData_SeekTable *seek_table,
224 bool last_metadata_is_last,
225 FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data),
226 void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data),
230 FLAC__StreamMetaData padding_block;
231 FLAC__StreamMetaData seek_table_block;
233 FLAC__ASSERT(encoder != 0);
234 FLAC__ASSERT(write_callback != 0);
235 FLAC__ASSERT(metadata_callback != 0);
237 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
238 return encoder->protected->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
240 encoder->protected->state = FLAC__STREAM_ENCODER_OK;
242 encoder->protected->streamable_subset = streamable_subset;
243 encoder->protected->do_mid_side_stereo = do_mid_side_stereo;
244 encoder->protected->loose_mid_side_stereo = loose_mid_side_stereo;
245 encoder->protected->channels = channels;
246 encoder->protected->bits_per_sample = bits_per_sample;
247 encoder->protected->sample_rate = sample_rate;
248 encoder->protected->blocksize = blocksize;
249 encoder->protected->max_lpc_order = max_lpc_order;
250 encoder->protected->qlp_coeff_precision = qlp_coeff_precision;
251 encoder->protected->do_qlp_coeff_prec_search = do_qlp_coeff_prec_search;
252 encoder->protected->do_exhaustive_model_search = do_exhaustive_model_search;
253 encoder->protected->min_residual_partition_order = min_residual_partition_order;
254 encoder->protected->max_residual_partition_order = max_residual_partition_order;
255 encoder->protected->rice_parameter_search_dist = rice_parameter_search_dist;
256 encoder->protected->total_samples_estimate = total_samples_estimate;
257 encoder->protected->seek_table = seek_table;
259 if(encoder->protected->channels == 0 || encoder->protected->channels > FLAC__MAX_CHANNELS)
260 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
262 if(encoder->protected->do_mid_side_stereo && encoder->protected->channels != 2)
263 return encoder->protected->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
265 if(encoder->protected->loose_mid_side_stereo && !encoder->protected->do_mid_side_stereo)
266 return encoder->protected->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
268 if(encoder->protected->bits_per_sample >= 32)
269 encoder->protected->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
271 if(encoder->protected->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
272 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
274 if(encoder->protected->sample_rate == 0 || encoder->protected->sample_rate > FLAC__MAX_SAMPLE_RATE)
275 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
277 if(encoder->protected->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected->blocksize > FLAC__MAX_BLOCK_SIZE)
278 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
280 if(encoder->protected->blocksize < encoder->protected->max_lpc_order)
281 return encoder->protected->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
283 if(encoder->protected->qlp_coeff_precision == 0) {
284 if(encoder->protected->bits_per_sample < 16) {
285 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
286 /* @@@ until then we'll make a guess */
287 encoder->protected->qlp_coeff_precision = max(5, 2 + encoder->protected->bits_per_sample / 2);
289 else if(encoder->protected->bits_per_sample == 16) {
290 if(encoder->protected->blocksize <= 192)
291 encoder->protected->qlp_coeff_precision = 7;
292 else if(encoder->protected->blocksize <= 384)
293 encoder->protected->qlp_coeff_precision = 8;
294 else if(encoder->protected->blocksize <= 576)
295 encoder->protected->qlp_coeff_precision = 9;
296 else if(encoder->protected->blocksize <= 1152)
297 encoder->protected->qlp_coeff_precision = 10;
298 else if(encoder->protected->blocksize <= 2304)
299 encoder->protected->qlp_coeff_precision = 11;
300 else if(encoder->protected->blocksize <= 4608)
301 encoder->protected->qlp_coeff_precision = 12;
303 encoder->protected->qlp_coeff_precision = 13;
306 encoder->protected->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->protected->bits_per_sample - 1);
309 else if(encoder->protected->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected->qlp_coeff_precision + encoder->protected->bits_per_sample >= 8*sizeof(uint32) || encoder->protected->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
310 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
312 if(encoder->protected->streamable_subset) {
313 //@@@ add check for blocksize here
314 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)
315 return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
316 if(encoder->protected->sample_rate > 655350)
317 return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
320 if(encoder->protected->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
321 encoder->protected->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
322 if(encoder->protected->min_residual_partition_order >= encoder->protected->max_residual_partition_order)
323 encoder->protected->min_residual_partition_order = encoder->protected->max_residual_partition_order;
325 encoder->private->input_capacity = 0;
326 for(i = 0; i < encoder->protected->channels; i++) {
327 encoder->private->integer_signal_unaligned[i] = encoder->private->integer_signal[i] = 0;
328 encoder->private->real_signal_unaligned[i] = encoder->private->real_signal[i] = 0;
330 for(i = 0; i < 2; i++) {
331 encoder->private->integer_signal_mid_side_unaligned[i] = encoder->private->integer_signal_mid_side[i] = 0;
332 encoder->private->real_signal_mid_side_unaligned[i] = encoder->private->real_signal_mid_side[i] = 0;
334 for(i = 0; i < encoder->protected->channels; i++) {
335 encoder->private->residual_workspace_unaligned[i][0] = encoder->private->residual_workspace[i][0] = 0;
336 encoder->private->residual_workspace_unaligned[i][1] = encoder->private->residual_workspace[i][1] = 0;
337 encoder->private->best_subframe[i] = 0;
339 for(i = 0; i < 2; i++) {
340 encoder->private->residual_workspace_mid_side_unaligned[i][0] = encoder->private->residual_workspace_mid_side[i][0] = 0;
341 encoder->private->residual_workspace_mid_side_unaligned[i][1] = encoder->private->residual_workspace_mid_side[i][1] = 0;
342 encoder->private->best_subframe_mid_side[i] = 0;
344 for(i = 0; i < encoder->protected->channels; i++) {
345 encoder->private->subframe_workspace_ptr[i][0] = &encoder->private->subframe_workspace[i][0];
346 encoder->private->subframe_workspace_ptr[i][1] = &encoder->private->subframe_workspace[i][1];
348 for(i = 0; i < 2; i++) {
349 encoder->private->subframe_workspace_ptr_mid_side[i][0] = &encoder->private->subframe_workspace_mid_side[i][0];
350 encoder->private->subframe_workspace_ptr_mid_side[i][1] = &encoder->private->subframe_workspace_mid_side[i][1];
352 encoder->private->abs_residual_unaligned = encoder->private->abs_residual = 0;
353 encoder->private->abs_residual_partition_sums_unaligned = encoder->private->abs_residual_partition_sums = 0;
354 encoder->private->raw_bits_per_partition_unaligned = encoder->private->raw_bits_per_partition = 0;
355 encoder->private->loose_mid_side_stereo_frames_exact = (double)encoder->protected->sample_rate * 0.4 / (double)encoder->protected->blocksize;
356 encoder->private->loose_mid_side_stereo_frames = (unsigned)(encoder->private->loose_mid_side_stereo_frames_exact + 0.5);
357 if(encoder->private->loose_mid_side_stereo_frames == 0)
358 encoder->private->loose_mid_side_stereo_frames = 1;
359 encoder->private->loose_mid_side_stereo_frame_count = 0;
360 encoder->private->current_sample_number = 0;
361 encoder->private->current_frame_number = 0;
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;
389 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
390 if(encoder->private->cpuinfo.data.ia32.mmx && encoder->private->cpuinfo.data.ia32.cmov)
391 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
392 if(encoder->private->cpuinfo.data.ia32.mmx) {
393 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
394 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
397 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
398 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
404 if(encoder->protected->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected->blocksize)+1 > 30)
405 encoder->private->use_slow = true;
407 encoder->private->use_slow = false;
409 if(!stream_encoder_resize_buffers_(encoder, encoder->protected->blocksize)) {
410 /* the above function sets the state for us in case of an error */
411 return encoder->protected->state;
413 FLAC__bitbuffer_init(&encoder->private->frame);
414 encoder->private->write_callback = write_callback;
415 encoder->private->metadata_callback = metadata_callback;
416 encoder->private->client_data = client_data;
419 * write the stream header
422 if(!FLAC__bitbuffer_clear(&encoder->private->frame))
423 return encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
424 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
425 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
427 encoder->private->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
428 encoder->private->metadata.is_last = (encoder->protected->seek_table == 0 && padding == 0 && last_metadata_is_last);
429 encoder->private->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
430 encoder->private->metadata.data.stream_info.min_blocksize = encoder->protected->blocksize; /* this encoder uses the same blocksize for the whole stream */
431 encoder->private->metadata.data.stream_info.max_blocksize = encoder->protected->blocksize;
432 encoder->private->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
433 encoder->private->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
434 encoder->private->metadata.data.stream_info.sample_rate = encoder->protected->sample_rate;
435 encoder->private->metadata.data.stream_info.channels = encoder->protected->channels;
436 encoder->private->metadata.data.stream_info.bits_per_sample = encoder->protected->bits_per_sample;
437 encoder->private->metadata.data.stream_info.total_samples = encoder->protected->total_samples_estimate; /* we will replace this later with the real total */
438 memset(encoder->private->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
439 MD5Init(&encoder->private->md5context);
440 if(!FLAC__add_metadata_block(&encoder->private->metadata, &encoder->private->frame))
441 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
443 if(0 != encoder->protected->seek_table) {
444 if(!FLAC__seek_table_is_valid(encoder->protected->seek_table))
445 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE;
446 seek_table_block.type = FLAC__METADATA_TYPE_SEEKTABLE;
447 seek_table_block.is_last = (padding == 0 && last_metadata_is_last);
448 seek_table_block.length = encoder->protected->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
449 seek_table_block.data.seek_table = *encoder->protected->seek_table;
450 if(!FLAC__add_metadata_block(&seek_table_block, &encoder->private->frame))
451 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
454 /* add a PADDING block if requested */
456 padding_block.type = FLAC__METADATA_TYPE_PADDING;
457 padding_block.is_last = last_metadata_is_last;
458 padding_block.length = padding;
459 if(!FLAC__add_metadata_block(&padding_block, &encoder->private->frame))
460 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
463 FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned before writing */
464 FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
465 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)
466 return encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
468 /* now that the metadata block is written, we can init this to an absurdly-high value... */
469 encoder->private->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
470 /* ... and clear this to 0 */
471 encoder->private->metadata.data.stream_info.total_samples = 0;
473 return encoder->protected->state;
476 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
480 FLAC__ASSERT(encoder != 0);
481 if(encoder->protected->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
483 if(encoder->private->current_sample_number != 0) {
484 encoder->protected->blocksize = encoder->private->current_sample_number;
485 stream_encoder_process_frame_(encoder, true); /* true => is last frame */
487 MD5Final(encoder->private->metadata.data.stream_info.md5sum, &encoder->private->md5context);
488 encoder->private->metadata_callback(encoder, &encoder->private->metadata, encoder->private->client_data);
489 for(i = 0; i < encoder->protected->channels; i++) {
490 if(encoder->private->integer_signal_unaligned[i] != 0) {
491 free(encoder->private->integer_signal_unaligned[i]);
492 encoder->private->integer_signal_unaligned[i] = 0;
494 if(encoder->private->real_signal_unaligned[i] != 0) {
495 free(encoder->private->real_signal_unaligned[i]);
496 encoder->private->real_signal_unaligned[i] = 0;
499 for(i = 0; i < 2; i++) {
500 if(encoder->private->integer_signal_mid_side_unaligned[i] != 0) {
501 free(encoder->private->integer_signal_mid_side_unaligned[i]);
502 encoder->private->integer_signal_mid_side_unaligned[i] = 0;
504 if(encoder->private->real_signal_mid_side_unaligned[i] != 0) {
505 free(encoder->private->real_signal_mid_side_unaligned[i]);
506 encoder->private->real_signal_mid_side_unaligned[i] = 0;
509 for(channel = 0; channel < encoder->protected->channels; channel++) {
510 for(i = 0; i < 2; i++) {
511 if(encoder->private->residual_workspace_unaligned[channel][i] != 0) {
512 free(encoder->private->residual_workspace_unaligned[channel][i]);
513 encoder->private->residual_workspace_unaligned[channel][i] = 0;
517 for(channel = 0; channel < 2; channel++) {
518 for(i = 0; i < 2; i++) {
519 if(encoder->private->residual_workspace_mid_side_unaligned[channel][i] != 0) {
520 free(encoder->private->residual_workspace_mid_side_unaligned[channel][i]);
521 encoder->private->residual_workspace_mid_side_unaligned[channel][i] = 0;
525 if(encoder->private->abs_residual_unaligned != 0) {
526 free(encoder->private->abs_residual_unaligned);
527 encoder->private->abs_residual_unaligned = 0;
529 if(encoder->private->abs_residual_partition_sums_unaligned != 0) {
530 free(encoder->private->abs_residual_partition_sums_unaligned);
531 encoder->private->abs_residual_partition_sums_unaligned = 0;
533 if(encoder->private->raw_bits_per_partition_unaligned != 0) {
534 free(encoder->private->raw_bits_per_partition_unaligned);
535 encoder->private->raw_bits_per_partition_unaligned = 0;
537 FLAC__bitbuffer_free(&encoder->private->frame);
539 encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
542 FLAC__StreamEncoderState FLAC__stream_encoder_state(const FLAC__StreamEncoder *encoder)
544 return encoder->protected->state;
547 bool FLAC__stream_encoder_streamable_subset(const FLAC__StreamEncoder *encoder)
549 return encoder->protected->streamable_subset;
552 bool FLAC__stream_encoder_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
554 return encoder->protected->do_mid_side_stereo;
557 bool FLAC__stream_encoder_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
559 return encoder->protected->loose_mid_side_stereo;
562 unsigned FLAC__stream_encoder_channels(const FLAC__StreamEncoder *encoder)
564 return encoder->protected->channels;
567 unsigned FLAC__stream_encoder_bits_per_sample(const FLAC__StreamEncoder *encoder)
569 return encoder->protected->bits_per_sample;
572 unsigned FLAC__stream_encoder_sample_rate(const FLAC__StreamEncoder *encoder)
574 return encoder->protected->sample_rate;
577 unsigned FLAC__stream_encoder_blocksize(const FLAC__StreamEncoder *encoder)
579 return encoder->protected->blocksize;
582 unsigned FLAC__stream_encoder_max_lpc_order(const FLAC__StreamEncoder *encoder)
584 return encoder->protected->max_lpc_order;
587 unsigned FLAC__stream_encoder_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
589 return encoder->protected->qlp_coeff_precision;
592 bool FLAC__stream_encoder_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
594 return encoder->protected->do_qlp_coeff_prec_search;
597 bool FLAC__stream_encoder_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
599 return encoder->protected->do_exhaustive_model_search;
602 unsigned FLAC__stream_encoder_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
604 return encoder->protected->min_residual_partition_order;
607 unsigned FLAC__stream_encoder_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
609 return encoder->protected->max_residual_partition_order;
612 unsigned FLAC__stream_encoder_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
614 return encoder->protected->rice_parameter_search_dist;
617 bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const int32 *buf[], unsigned samples)
619 unsigned i, j, channel;
621 const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
623 FLAC__ASSERT(encoder != 0);
624 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
627 if(encoder->protected->do_mid_side_stereo && channels == 2) {
629 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
630 x = mid = side = buf[0][j];
631 encoder->private->integer_signal[0][i] = x;
632 encoder->private->real_signal[0][i] = (real)x;
634 encoder->private->integer_signal[1][i] = x;
635 encoder->private->real_signal[1][i] = (real)x;
638 mid >>= 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
639 encoder->private->integer_signal_mid_side[1][i] = side;
640 encoder->private->integer_signal_mid_side[0][i] = mid;
641 encoder->private->real_signal_mid_side[1][i] = (real)side;
642 encoder->private->real_signal_mid_side[0][i] = (real)mid;
643 encoder->private->current_sample_number++;
646 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
649 } while(j < samples);
653 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
654 for(channel = 0; channel < channels; channel++) {
656 encoder->private->integer_signal[channel][i] = x;
657 encoder->private->real_signal[channel][i] = (real)x;
659 encoder->private->current_sample_number++;
662 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
665 } while(j < samples);
671 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
672 bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const int32 buf[], unsigned samples)
674 unsigned i, j, k, channel;
676 const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
678 FLAC__ASSERT(encoder != 0);
679 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
682 if(encoder->protected->do_mid_side_stereo && channels == 2) {
684 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
685 x = mid = side = buf[k++];
686 encoder->private->integer_signal[0][i] = x;
687 encoder->private->real_signal[0][i] = (real)x;
689 encoder->private->integer_signal[1][i] = x;
690 encoder->private->real_signal[1][i] = (real)x;
693 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
694 encoder->private->integer_signal_mid_side[1][i] = side;
695 encoder->private->integer_signal_mid_side[0][i] = mid;
696 encoder->private->real_signal_mid_side[1][i] = (real)side;
697 encoder->private->real_signal_mid_side[0][i] = (real)mid;
698 encoder->private->current_sample_number++;
701 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
704 } while(j < samples);
708 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
709 for(channel = 0; channel < channels; channel++) {
711 encoder->private->integer_signal[channel][i] = x;
712 encoder->private->real_signal[channel][i] = (real)x;
714 encoder->private->current_sample_number++;
717 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
720 } while(j < samples);
726 bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
731 FLAC__ASSERT(new_size > 0);
732 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
733 FLAC__ASSERT(encoder->private->current_sample_number == 0);
735 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
736 if(new_size <= encoder->private->input_capacity)
740 for(i = 0; ok && i < encoder->protected->channels; i++) {
741 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->integer_signal_unaligned[i], &encoder->private->integer_signal[i]);
742 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private->real_signal_unaligned[i], &encoder->private->real_signal[i]);
744 for(i = 0; ok && i < 2; i++) {
745 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->integer_signal_mid_side_unaligned[i], &encoder->private->integer_signal_mid_side[i]);
746 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]);
748 for(channel = 0; ok && channel < encoder->protected->channels; channel++) {
749 for(i = 0; ok && i < 2; i++) {
750 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->residual_workspace_unaligned[channel][i], &encoder->private->residual_workspace[channel][i]);
753 for(channel = 0; ok && channel < 2; channel++) {
754 for(i = 0; ok && i < 2; i++) {
755 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]);
758 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private->abs_residual_unaligned, &encoder->private->abs_residual);
759 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
760 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size * 2, &encoder->private->abs_residual_partition_sums_unaligned, &encoder->private->abs_residual_partition_sums);
762 #ifdef FLAC__SEARCH_FOR_ESCAPES
763 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private->raw_bits_per_partition_unaligned, &encoder->private->raw_bits_per_partition);
767 encoder->private->input_capacity = new_size;
769 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
774 /***********************************************************************
776 * Private class methods
778 ***********************************************************************/
780 bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, bool is_last_frame)
782 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
785 * Accumulate raw signal to the MD5 signature
787 /* 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: */
788 if(!FLAC__MD5Accumulate(&encoder->private->md5context, encoder->private->integer_signal, encoder->protected->channels, encoder->protected->blocksize, (encoder->protected->bits_per_sample+7) / 8)) {
789 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
794 * Process the frame header and subframes into the frame bitbuffer
796 if(!stream_encoder_process_subframes_(encoder, is_last_frame)) {
797 /* the above function sets the state for us in case of an error */
802 * Zero-pad the frame to a byte_boundary
804 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->private->frame)) {
805 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
810 * CRC-16 the whole thing
812 FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned */
813 FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
814 FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__crc16(encoder->private->frame.buffer, encoder->private->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
819 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) {
820 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
825 * Get ready for the next frame
827 encoder->private->current_sample_number = 0;
828 encoder->private->current_frame_number++;
829 encoder->private->metadata.data.stream_info.total_samples += (uint64)encoder->protected->blocksize;
830 encoder->private->metadata.data.stream_info.min_framesize = min(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.min_framesize);
831 encoder->private->metadata.data.stream_info.max_framesize = max(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.max_framesize);
836 bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, bool is_last_frame)
838 FLAC__FrameHeader frame_header;
839 unsigned channel, min_partition_order = encoder->protected->min_residual_partition_order, max_partition_order;
840 bool do_independent, do_mid_side;
843 * Calculate the min,max Rice partition orders
846 max_partition_order = 0;
849 unsigned limit = 0, b = encoder->protected->blocksize;
854 max_partition_order = min(encoder->protected->max_residual_partition_order, limit);
856 min_partition_order = min(min_partition_order, max_partition_order);
861 if(!FLAC__bitbuffer_clear(&encoder->private->frame)) {
862 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
865 frame_header.blocksize = encoder->protected->blocksize;
866 frame_header.sample_rate = encoder->protected->sample_rate;
867 frame_header.channels = encoder->protected->channels;
868 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
869 frame_header.bits_per_sample = encoder->protected->bits_per_sample;
870 frame_header.number.frame_number = encoder->private->current_frame_number;
873 * Figure out what channel assignments to try
875 if(encoder->protected->do_mid_side_stereo) {
876 if(encoder->protected->loose_mid_side_stereo) {
877 if(encoder->private->loose_mid_side_stereo_frame_count == 0) {
878 do_independent = true;
882 do_independent = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
883 do_mid_side = !do_independent;
887 do_independent = true;
892 do_independent = true;
896 FLAC__ASSERT(do_independent || do_mid_side);
899 * Check for wasted bits; set effective bps for each subframe
903 for(channel = 0; channel < encoder->protected->channels; channel++) {
904 w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal[channel], encoder->protected->blocksize);
905 encoder->private->subframe_workspace[channel][0].wasted_bits = encoder->private->subframe_workspace[channel][1].wasted_bits = w;
906 encoder->private->subframe_bps[channel] = encoder->protected->bits_per_sample - w;
911 FLAC__ASSERT(encoder->protected->channels == 2);
912 for(channel = 0; channel < 2; channel++) {
913 w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal_mid_side[channel], encoder->protected->blocksize);
914 encoder->private->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private->subframe_workspace_mid_side[channel][1].wasted_bits = w;
915 encoder->private->subframe_bps_mid_side[channel] = encoder->protected->bits_per_sample - w + (channel==0? 0:1);
920 * First do a normal encoding pass of each independent channel
923 for(channel = 0; channel < encoder->protected->channels; channel++) {
924 if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, 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))
930 * Now do mid and side channels if requested
933 FLAC__ASSERT(encoder->protected->channels == 2);
935 for(channel = 0; channel < 2; channel++) {
936 if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, 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))
942 * Compose the frame bitbuffer
945 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
946 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
947 FLAC__ChannelAssignment channel_assignment;
949 FLAC__ASSERT(encoder->protected->channels == 2);
951 if(encoder->protected->loose_mid_side_stereo && encoder->private->loose_mid_side_stereo_frame_count > 0) {
952 channel_assignment = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
955 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
957 FLAC__ChannelAssignment ca;
959 FLAC__ASSERT(do_independent && do_mid_side);
961 /* We have to figure out which channel assignent results in the smallest frame */
962 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits [1];
963 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits_mid_side[1];
964 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private->best_subframe_bits [1] + encoder->private->best_subframe_bits_mid_side[1];
965 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private->best_subframe_bits_mid_side[0] + encoder->private->best_subframe_bits_mid_side[1];
967 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
968 if(bits[ca] < min_bits) {
970 channel_assignment = ca;
975 frame_header.channel_assignment = channel_assignment;
977 if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
978 encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
982 switch(channel_assignment) {
983 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
984 left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]];
985 right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]];
987 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
988 left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]];
989 right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
991 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
992 left_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
993 right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]];
995 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
996 left_subframe = &encoder->private->subframe_workspace_mid_side[0][encoder->private->best_subframe_mid_side[0]];
997 right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
1003 switch(channel_assignment) {
1004 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1005 left_bps = encoder->private->subframe_bps [0];
1006 right_bps = encoder->private->subframe_bps [1];
1008 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1009 left_bps = encoder->private->subframe_bps [0];
1010 right_bps = encoder->private->subframe_bps_mid_side[1];
1012 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1013 left_bps = encoder->private->subframe_bps_mid_side[1];
1014 right_bps = encoder->private->subframe_bps [1];
1016 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1017 left_bps = encoder->private->subframe_bps_mid_side[0];
1018 right_bps = encoder->private->subframe_bps_mid_side[1];
1024 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1025 if(!stream_encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->private->frame))
1027 if(!stream_encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->private->frame))
1031 if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
1032 encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1036 for(channel = 0; channel < encoder->protected->channels; channel++) {
1037 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)) {
1038 /* the above function sets the state for us in case of an error */
1044 if(encoder->protected->loose_mid_side_stereo) {
1045 encoder->private->loose_mid_side_stereo_frame_count++;
1046 if(encoder->private->loose_mid_side_stereo_frame_count >= encoder->private->loose_mid_side_stereo_frames)
1047 encoder->private->loose_mid_side_stereo_frame_count = 0;
1050 encoder->private->last_channel_assignment = frame_header.channel_assignment;
1055 bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
1057 real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1058 real lpc_residual_bits_per_sample;
1059 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 */
1060 real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
1061 real lpc_error[FLAC__MAX_LPC_ORDER];
1062 unsigned min_lpc_order, max_lpc_order, lpc_order;
1063 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1064 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1065 unsigned rice_parameter;
1066 unsigned _candidate_bits, _best_bits;
1067 unsigned _best_subframe;
1069 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1071 _best_bits = stream_encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1073 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1074 /* check for constant subframe */
1075 if(encoder->private->use_slow)
1076 guess_fixed_order = FLAC__fixed_compute_best_predictor_slow(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
1078 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);
1079 if(fixed_residual_bits_per_sample[1] == 0.0) {
1080 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1081 unsigned i, signal_is_constant = true;
1082 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1083 if(integer_signal[0] != integer_signal[i]) {
1084 signal_is_constant = false;
1088 if(signal_is_constant) {
1089 _candidate_bits = stream_encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1090 if(_candidate_bits < _best_bits) {
1091 _best_subframe = !_best_subframe;
1092 _best_bits = _candidate_bits;
1098 if(encoder->protected->do_exhaustive_model_search) {
1099 min_fixed_order = 0;
1100 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1103 min_fixed_order = max_fixed_order = guess_fixed_order;
1105 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1106 if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps)
1107 continue; /* don't even try */
1108 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 */
1109 #ifndef FLAC__SYMMETRIC_RICE
1110 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1112 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1113 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1114 _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, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]);
1115 if(_candidate_bits < _best_bits) {
1116 _best_subframe = !_best_subframe;
1117 _best_bits = _candidate_bits;
1122 if(encoder->protected->max_lpc_order > 0) {
1123 if(encoder->protected->max_lpc_order >= frame_header->blocksize)
1124 max_lpc_order = frame_header->blocksize-1;
1126 max_lpc_order = encoder->protected->max_lpc_order;
1127 if(max_lpc_order > 0) {
1128 encoder->private->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
1129 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
1130 if(autoc[0] != 0.0) {
1131 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
1132 if(encoder->protected->do_exhaustive_model_search) {
1136 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
1137 min_lpc_order = max_lpc_order = guess_lpc_order;
1139 if(encoder->protected->do_qlp_coeff_prec_search) {
1140 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
1141 max_qlp_coeff_precision = min(32 - subframe_bps - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1);
1144 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected->qlp_coeff_precision;
1146 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
1147 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
1148 if(lpc_residual_bits_per_sample >= (real)subframe_bps)
1149 continue; /* don't even try */
1150 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
1151 #ifndef FLAC__SYMMETRIC_RICE
1152 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1154 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1155 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1156 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
1157 _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, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]);
1158 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1159 if(_candidate_bits < _best_bits) {
1160 _best_subframe = !_best_subframe;
1161 _best_bits = _candidate_bits;
1172 *best_subframe = _best_subframe;
1173 *best_bits = _best_bits;
1178 bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
1180 switch(subframe->type) {
1181 case FLAC__SUBFRAME_TYPE_CONSTANT:
1182 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
1183 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1187 case FLAC__SUBFRAME_TYPE_FIXED:
1188 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
1189 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1193 case FLAC__SUBFRAME_TYPE_LPC:
1194 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
1195 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1199 case FLAC__SUBFRAME_TYPE_VERBATIM:
1200 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
1201 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1212 unsigned stream_encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
1214 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1215 subframe->data.constant.value = signal;
1217 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
1220 unsigned stream_encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
1222 unsigned i, residual_bits;
1223 const unsigned residual_samples = blocksize - order;
1225 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1227 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1229 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1230 subframe->data.fixed.residual = residual;
1232 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, 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);
1234 subframe->data.fixed.order = order;
1235 for(i = 0; i < order; i++)
1236 subframe->data.fixed.warmup[i] = signal[i];
1238 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
1241 unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
1243 int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1244 unsigned i, residual_bits;
1245 int quantization, ret;
1246 const unsigned residual_samples = blocksize - order;
1248 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
1250 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1252 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
1253 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1255 encoder->private->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1257 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1259 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1260 subframe->data.lpc.residual = residual;
1262 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, 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);
1264 subframe->data.lpc.order = order;
1265 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1266 subframe->data.lpc.quantization_level = quantization;
1267 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1268 for(i = 0; i < order; i++)
1269 subframe->data.lpc.warmup[i] = signal[i];
1271 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;
1274 unsigned stream_encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
1276 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1278 subframe->data.verbatim.data = signal;
1280 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
1283 unsigned stream_encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 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, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[])
1286 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1288 int partition_order;
1290 unsigned partition_order;
1292 unsigned residual_bits, best_residual_bits = 0;
1293 unsigned residual_sample;
1294 unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
1296 /* compute abs(residual) for use later */
1297 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1298 r = residual[residual_sample];
1299 abs_residual[residual_sample] = (uint32)(r<0? -r : r);
1302 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1303 max_partition_order = stream_encoder_precompute_partition_info_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
1304 min_partition_order = min(min_partition_order, max_partition_order);
1306 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1307 if(!stream_encoder_set_partitioned_rice_(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, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) {
1308 FLAC__ASSERT(0); /* stream_encoder_precompute_partition_info_ should keep this from ever happening */
1310 sum += 1u << partition_order;
1311 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1312 best_residual_bits = residual_bits;
1313 *best_partition_order = partition_order;
1314 best_parameters_index = !best_parameters_index;
1318 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1319 if(!stream_encoder_set_partitioned_rice_(abs_residual, 0, 0, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) {
1320 FLAC__ASSERT(best_residual_bits != 0);
1323 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1324 best_residual_bits = residual_bits;
1325 *best_partition_order = partition_order;
1326 best_parameters_index = !best_parameters_index;
1330 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1331 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1333 return best_residual_bits;
1336 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1337 unsigned stream_encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
1339 int partition_order;
1340 unsigned from_partition, to_partition = 0;
1341 const unsigned blocksize = residual_samples + predictor_order;
1343 /* first do max_partition_order */
1344 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1345 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1346 uint32 abs_residual_partition_sum;
1348 #ifdef FLAC__SEARCH_FOR_ESCAPES
1349 uint32 abs_residual_partition_max;
1350 unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */
1353 unsigned partition, partition_sample, partition_samples, residual_sample;
1354 const unsigned partitions = 1u << partition_order;
1355 const unsigned default_partition_samples = blocksize >> partition_order;
1357 if(default_partition_samples <= predictor_order) {
1358 FLAC__ASSERT(max_partition_order > 0);
1359 max_partition_order--;
1362 for(partition = residual_sample = 0; partition < partitions; partition++) {
1363 partition_samples = default_partition_samples;
1365 partition_samples -= predictor_order;
1366 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1367 abs_residual_partition_sum = 0;
1369 #ifdef FLAC__SEARCH_FOR_ESCAPES
1370 abs_residual_partition_max = 0;
1372 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1373 abs_r = abs_residual[residual_sample];
1374 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1375 abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */
1377 #ifdef FLAC__SEARCH_FOR_ESCAPES
1378 if(abs_r > abs_residual_partition_max) {
1379 abs_residual_partition_max = abs_r;
1380 abs_residual_partition_max_index = residual_sample;
1385 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1386 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
1388 #ifdef FLAC__SEARCH_FOR_ESCAPES
1389 if(abs_residual_partition_max > 0)
1390 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]);
1392 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0);
1395 to_partition = partitions;
1400 /* now merge for lower orders */
1401 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1402 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1405 #ifdef FLAC__SEARCH_FOR_ESCAPES
1409 const unsigned partitions = 1u << partition_order;
1410 for(i = 0; i < partitions; i++) {
1411 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1412 s = abs_residual_partition_sums[from_partition];
1414 #ifdef FLAC__SEARCH_FOR_ESCAPES
1415 m = raw_bits_per_partition[from_partition];
1418 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1419 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1421 #ifdef FLAC__SEARCH_FOR_ESCAPES
1422 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1429 return max_partition_order;
1433 #ifdef VARIABLE_RICE_BITS
1434 #undef VARIABLE_RICE_BITS
1436 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1438 bool stream_encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 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, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
1440 unsigned rice_parameter, partition_bits;
1441 #ifndef NO_RICE_SEARCH
1442 unsigned best_partition_bits;
1443 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1445 #ifdef FLAC__SEARCH_FOR_ESCAPES
1448 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1450 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1452 if(partition_order == 0) {
1455 #ifndef NO_RICE_SEARCH
1456 if(rice_parameter_search_dist) {
1457 if(suggested_rice_parameter < rice_parameter_search_dist)
1458 min_rice_parameter = 0;
1460 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1461 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1462 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1463 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1466 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1468 best_partition_bits = 0xffffffff;
1469 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1471 #ifdef VARIABLE_RICE_BITS
1472 #ifdef FLAC__SYMMETRIC_RICE
1473 partition_bits = (2+rice_parameter) * residual_samples;
1475 const unsigned rice_parameter_estimate = rice_parameter-1;
1476 partition_bits = (1+rice_parameter) * residual_samples;
1481 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1482 for(i = 0; i < residual_samples; i++) {
1483 #ifdef VARIABLE_RICE_BITS
1484 #ifdef FLAC__SYMMETRIC_RICE
1485 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1487 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1490 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1493 #ifndef NO_RICE_SEARCH
1494 if(partition_bits < best_partition_bits) {
1495 best_rice_parameter = rice_parameter;
1496 best_partition_bits = partition_bits;
1500 #ifdef FLAC__SEARCH_FOR_ESCAPES
1501 flat_bits = raw_bits_per_partition[0] * residual_samples;
1502 if(flat_bits <= best_partition_bits) {
1503 raw_bits[0] = raw_bits_per_partition[0];
1504 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1505 best_partition_bits = flat_bits;
1508 parameters[0] = best_rice_parameter;
1509 bits_ += best_partition_bits;
1512 unsigned partition, residual_sample, save_residual_sample, partition_sample;
1513 unsigned mean, partition_samples;
1514 const unsigned partitions = 1u << partition_order;
1515 for(partition = residual_sample = 0; partition < partitions; partition++) {
1516 partition_samples = (residual_samples+predictor_order) >> partition_order;
1517 if(partition == 0) {
1518 if(partition_samples <= predictor_order)
1521 partition_samples -= predictor_order;
1523 mean = partition_samples >> 1;
1524 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1525 mean += abs_residual_partition_sums[partition];
1527 save_residual_sample = residual_sample;
1528 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
1529 mean += abs_residual[residual_sample];
1530 residual_sample = save_residual_sample;
1532 mean /= partition_samples;
1533 #ifdef FLAC__SYMMETRIC_RICE
1534 /* calc rice_parameter = floor(log2(mean)) */
1542 /* calc rice_parameter = floor(log2(mean)) + 1 */
1549 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1550 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1552 #ifndef NO_RICE_SEARCH
1553 if(rice_parameter_search_dist) {
1554 if(rice_parameter < rice_parameter_search_dist)
1555 min_rice_parameter = 0;
1557 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1558 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
1559 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1560 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1563 min_rice_parameter = max_rice_parameter = rice_parameter;
1565 best_partition_bits = 0xffffffff;
1566 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1568 #ifdef VARIABLE_RICE_BITS
1569 #ifdef FLAC__SYMMETRIC_RICE
1570 partition_bits = (2+rice_parameter) * partition_samples;
1572 const unsigned rice_parameter_estimate = rice_parameter-1;
1573 partition_bits = (1+rice_parameter) * partition_samples;
1578 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1579 save_residual_sample = residual_sample;
1580 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
1581 #ifdef VARIABLE_RICE_BITS
1582 #ifdef FLAC__SYMMETRIC_RICE
1583 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
1585 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
1588 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1591 #ifndef NO_RICE_SEARCH
1592 if(rice_parameter != max_rice_parameter)
1593 residual_sample = save_residual_sample;
1594 if(partition_bits < best_partition_bits) {
1595 best_rice_parameter = rice_parameter;
1596 best_partition_bits = partition_bits;
1600 #ifdef FLAC__SEARCH_FOR_ESCAPES
1601 flat_bits = raw_bits_per_partition[partition] * partition_samples;
1602 if(flat_bits <= best_partition_bits) {
1603 raw_bits[partition] = raw_bits_per_partition[partition];
1604 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1605 best_partition_bits = flat_bits;
1608 parameters[partition] = best_rice_parameter;
1609 bits_ += best_partition_bits;
1617 unsigned stream_encoder_get_wasted_bits_(int32 signal[], unsigned samples)
1622 for(i = 0; i < samples && !(x&1); i++)
1629 for(shift = 0; !(x&1); shift++)
1634 for(i = 0; i < samples; i++)
1635 signal[i] >>= shift;