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 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__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 FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint32 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, 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__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 FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__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 FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__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_(FLAC__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 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
77 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
78 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
79 FLAC__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 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
83 FLAC__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 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
93 FLAC__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 FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
106 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
107 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[]);
108 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[]);
109 FLAC__bool use_slow; /* use slow 64-bit versions of some functions */
110 FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const FLAC__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 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
115 FLAC__int32 *integer_signal_mid_side_unaligned[2];
116 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
117 FLAC__real *real_signal_mid_side_unaligned[2];
118 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
119 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
120 FLAC__uint32 *abs_residual_unaligned;
121 FLAC__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_CALLBACK",
134 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
135 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
136 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
137 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
138 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
139 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
140 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
141 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
142 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
143 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
144 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
145 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
146 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
147 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
148 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
149 "FLAC__STREAM_ENCODER_UNINITIALIZED"
152 const char *FLAC__StreamEncoderWriteStatusString[] = {
153 "FLAC__STREAM_ENCODER_WRITE_OK",
154 "FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR"
157 /***********************************************************************
159 * Class constructor/destructor
161 ***********************************************************************/
162 FLAC__StreamEncoder *FLAC__stream_encoder_new()
164 FLAC__StreamEncoder *encoder;
166 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
168 encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
172 encoder->protected = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
173 if(encoder->protected == 0) {
177 encoder->private = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
178 if(encoder->private == 0) {
179 free(encoder->protected);
184 encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
186 encoder->protected->streamable_subset = true;
187 encoder->protected->do_mid_side_stereo = false;
188 encoder->protected->loose_mid_side_stereo = false;
189 encoder->protected->channels = 2;
190 encoder->protected->bits_per_sample = 16;
191 encoder->protected->sample_rate = 44100;
192 encoder->protected->blocksize = 1152;
193 encoder->protected->max_lpc_order = 0;
194 encoder->protected->qlp_coeff_precision = 0;
195 encoder->protected->do_qlp_coeff_prec_search = false;
196 encoder->protected->do_exhaustive_model_search = false;
197 encoder->protected->min_residual_partition_order = 0;
198 encoder->protected->max_residual_partition_order = 0;
199 encoder->protected->rice_parameter_search_dist = 0;
200 encoder->protected->total_samples_estimate = 0;
201 encoder->protected->seek_table = 0;
202 encoder->protected->padding = 0;
203 encoder->protected->last_metadata_is_last = true;
205 encoder->private->write_callback = 0;
206 encoder->private->metadata_callback = 0;
207 encoder->private->client_data = 0;
212 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
214 FLAC__ASSERT(encoder != 0);
215 FLAC__ASSERT(encoder->protected != 0);
216 FLAC__ASSERT(encoder->private != 0);
218 free(encoder->private);
219 free(encoder->protected);
223 /***********************************************************************
225 * Public class methods
227 ***********************************************************************/
229 FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
232 FLAC__StreamMetaData padding_block;
233 FLAC__StreamMetaData seek_table_block;
235 FLAC__ASSERT(encoder != 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 if(0 == encoder->private->write_callback || 0 == encoder->private->metadata_callback)
243 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
245 if(encoder->protected->channels == 0 || encoder->protected->channels > FLAC__MAX_CHANNELS)
246 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
248 if(encoder->protected->do_mid_side_stereo && encoder->protected->channels != 2)
249 return encoder->protected->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
251 if(encoder->protected->loose_mid_side_stereo && !encoder->protected->do_mid_side_stereo)
252 return encoder->protected->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
254 if(encoder->protected->bits_per_sample >= 32)
255 encoder->protected->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
257 if(encoder->protected->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
258 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
260 if(encoder->protected->sample_rate == 0 || encoder->protected->sample_rate > FLAC__MAX_SAMPLE_RATE)
261 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
263 if(encoder->protected->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected->blocksize > FLAC__MAX_BLOCK_SIZE)
264 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
266 if(encoder->protected->blocksize < encoder->protected->max_lpc_order)
267 return encoder->protected->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
269 if(encoder->protected->qlp_coeff_precision == 0) {
270 if(encoder->protected->bits_per_sample < 16) {
271 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
272 /* @@@ until then we'll make a guess */
273 encoder->protected->qlp_coeff_precision = max(5, 2 + encoder->protected->bits_per_sample / 2);
275 else if(encoder->protected->bits_per_sample == 16) {
276 if(encoder->protected->blocksize <= 192)
277 encoder->protected->qlp_coeff_precision = 7;
278 else if(encoder->protected->blocksize <= 384)
279 encoder->protected->qlp_coeff_precision = 8;
280 else if(encoder->protected->blocksize <= 576)
281 encoder->protected->qlp_coeff_precision = 9;
282 else if(encoder->protected->blocksize <= 1152)
283 encoder->protected->qlp_coeff_precision = 10;
284 else if(encoder->protected->blocksize <= 2304)
285 encoder->protected->qlp_coeff_precision = 11;
286 else if(encoder->protected->blocksize <= 4608)
287 encoder->protected->qlp_coeff_precision = 12;
289 encoder->protected->qlp_coeff_precision = 13;
292 encoder->protected->qlp_coeff_precision = min(13, 8*sizeof(FLAC__int32) - encoder->protected->bits_per_sample - 1);
295 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))
296 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
298 if(encoder->protected->streamable_subset) {
299 //@@@ add check for blocksize here
300 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)
301 return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
302 if(encoder->protected->sample_rate > 655350)
303 return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
306 if(encoder->protected->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
307 encoder->protected->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
308 if(encoder->protected->min_residual_partition_order >= encoder->protected->max_residual_partition_order)
309 encoder->protected->min_residual_partition_order = encoder->protected->max_residual_partition_order;
311 encoder->private->input_capacity = 0;
312 for(i = 0; i < encoder->protected->channels; i++) {
313 encoder->private->integer_signal_unaligned[i] = encoder->private->integer_signal[i] = 0;
314 encoder->private->real_signal_unaligned[i] = encoder->private->real_signal[i] = 0;
316 for(i = 0; i < 2; i++) {
317 encoder->private->integer_signal_mid_side_unaligned[i] = encoder->private->integer_signal_mid_side[i] = 0;
318 encoder->private->real_signal_mid_side_unaligned[i] = encoder->private->real_signal_mid_side[i] = 0;
320 for(i = 0; i < encoder->protected->channels; i++) {
321 encoder->private->residual_workspace_unaligned[i][0] = encoder->private->residual_workspace[i][0] = 0;
322 encoder->private->residual_workspace_unaligned[i][1] = encoder->private->residual_workspace[i][1] = 0;
323 encoder->private->best_subframe[i] = 0;
325 for(i = 0; i < 2; i++) {
326 encoder->private->residual_workspace_mid_side_unaligned[i][0] = encoder->private->residual_workspace_mid_side[i][0] = 0;
327 encoder->private->residual_workspace_mid_side_unaligned[i][1] = encoder->private->residual_workspace_mid_side[i][1] = 0;
328 encoder->private->best_subframe_mid_side[i] = 0;
330 for(i = 0; i < encoder->protected->channels; i++) {
331 encoder->private->subframe_workspace_ptr[i][0] = &encoder->private->subframe_workspace[i][0];
332 encoder->private->subframe_workspace_ptr[i][1] = &encoder->private->subframe_workspace[i][1];
334 for(i = 0; i < 2; i++) {
335 encoder->private->subframe_workspace_ptr_mid_side[i][0] = &encoder->private->subframe_workspace_mid_side[i][0];
336 encoder->private->subframe_workspace_ptr_mid_side[i][1] = &encoder->private->subframe_workspace_mid_side[i][1];
338 encoder->private->abs_residual_unaligned = encoder->private->abs_residual = 0;
339 encoder->private->abs_residual_partition_sums_unaligned = encoder->private->abs_residual_partition_sums = 0;
340 encoder->private->raw_bits_per_partition_unaligned = encoder->private->raw_bits_per_partition = 0;
341 encoder->private->loose_mid_side_stereo_frames_exact = (double)encoder->protected->sample_rate * 0.4 / (double)encoder->protected->blocksize;
342 encoder->private->loose_mid_side_stereo_frames = (unsigned)(encoder->private->loose_mid_side_stereo_frames_exact + 0.5);
343 if(encoder->private->loose_mid_side_stereo_frames == 0)
344 encoder->private->loose_mid_side_stereo_frames = 1;
345 encoder->private->loose_mid_side_stereo_frame_count = 0;
346 encoder->private->current_sample_number = 0;
347 encoder->private->current_frame_number = 0;
350 * get the CPU info and set the function pointers
352 FLAC__cpu_info(&encoder->private->cpuinfo);
353 /* first default to the non-asm routines */
354 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
355 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
356 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
357 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
358 /* now override with asm where appropriate */
360 FLAC__ASSERT(encoder->private->cpuinfo.use_asm);
361 #ifdef FLAC__CPU_IA32
362 FLAC__ASSERT(encoder->private->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
363 #ifdef FLAC__HAS_NASM
364 if(0 && encoder->private->cpuinfo.data.ia32.sse) { /* SSE version lacks necessary resolution, plus SSE flag doesn't check for OS support */
365 if(encoder->protected->max_lpc_order < 4)
366 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
367 else if(encoder->protected->max_lpc_order < 8)
368 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
369 else if(encoder->protected->max_lpc_order < 12)
370 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
372 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
375 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
376 if(encoder->private->cpuinfo.data.ia32.mmx && encoder->private->cpuinfo.data.ia32.cmov)
377 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
378 if(encoder->private->cpuinfo.data.ia32.mmx) {
379 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
380 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
383 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
384 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
390 if(encoder->protected->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected->blocksize)+1 > 30)
391 encoder->private->use_slow = true;
393 encoder->private->use_slow = false;
395 if(!stream_encoder_resize_buffers_(encoder, encoder->protected->blocksize)) {
396 /* the above function sets the state for us in case of an error */
397 return encoder->protected->state;
399 FLAC__bitbuffer_init(&encoder->private->frame);
402 * write the stream header
405 if(!FLAC__bitbuffer_clear(&encoder->private->frame))
406 return encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
407 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
408 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
410 encoder->private->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
411 encoder->private->metadata.is_last = (encoder->protected->seek_table == 0 && encoder->protected->padding == 0 && encoder->protected->last_metadata_is_last);
412 encoder->private->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
413 encoder->private->metadata.data.stream_info.min_blocksize = encoder->protected->blocksize; /* this encoder uses the same blocksize for the whole stream */
414 encoder->private->metadata.data.stream_info.max_blocksize = encoder->protected->blocksize;
415 encoder->private->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
416 encoder->private->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
417 encoder->private->metadata.data.stream_info.sample_rate = encoder->protected->sample_rate;
418 encoder->private->metadata.data.stream_info.channels = encoder->protected->channels;
419 encoder->private->metadata.data.stream_info.bits_per_sample = encoder->protected->bits_per_sample;
420 encoder->private->metadata.data.stream_info.total_samples = encoder->protected->total_samples_estimate; /* we will replace this later with the real total */
421 memset(encoder->private->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
422 MD5Init(&encoder->private->md5context);
423 if(!FLAC__add_metadata_block(&encoder->private->metadata, &encoder->private->frame))
424 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
426 if(0 != encoder->protected->seek_table) {
427 if(!FLAC__seek_table_is_valid(encoder->protected->seek_table))
428 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE;
429 seek_table_block.type = FLAC__METADATA_TYPE_SEEKTABLE;
430 seek_table_block.is_last = (encoder->protected->padding == 0 && encoder->protected->last_metadata_is_last);
431 seek_table_block.length = encoder->protected->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
432 seek_table_block.data.seek_table = *encoder->protected->seek_table;
433 if(!FLAC__add_metadata_block(&seek_table_block, &encoder->private->frame))
434 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
437 /* add a PADDING block if requested */
438 if(encoder->protected->padding > 0) {
439 padding_block.type = FLAC__METADATA_TYPE_PADDING;
440 padding_block.is_last = encoder->protected->last_metadata_is_last;
441 padding_block.length = encoder->protected->padding;
442 if(!FLAC__add_metadata_block(&padding_block, &encoder->private->frame))
443 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
446 FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned before writing */
447 FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
448 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)
449 return encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
451 /* now that the metadata block is written, we can init this to an absurdly-high value... */
452 encoder->private->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
453 /* ... and clear this to 0 */
454 encoder->private->metadata.data.stream_info.total_samples = 0;
456 return encoder->protected->state;
459 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
463 FLAC__ASSERT(encoder != 0);
464 if(encoder->protected->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
466 if(encoder->private->current_sample_number != 0) {
467 encoder->protected->blocksize = encoder->private->current_sample_number;
468 stream_encoder_process_frame_(encoder, true); /* true => is last frame */
470 MD5Final(encoder->private->metadata.data.stream_info.md5sum, &encoder->private->md5context);
471 encoder->private->metadata_callback(encoder, &encoder->private->metadata, encoder->private->client_data);
472 for(i = 0; i < encoder->protected->channels; i++) {
473 if(encoder->private->integer_signal_unaligned[i] != 0) {
474 free(encoder->private->integer_signal_unaligned[i]);
475 encoder->private->integer_signal_unaligned[i] = 0;
477 if(encoder->private->real_signal_unaligned[i] != 0) {
478 free(encoder->private->real_signal_unaligned[i]);
479 encoder->private->real_signal_unaligned[i] = 0;
482 for(i = 0; i < 2; i++) {
483 if(encoder->private->integer_signal_mid_side_unaligned[i] != 0) {
484 free(encoder->private->integer_signal_mid_side_unaligned[i]);
485 encoder->private->integer_signal_mid_side_unaligned[i] = 0;
487 if(encoder->private->real_signal_mid_side_unaligned[i] != 0) {
488 free(encoder->private->real_signal_mid_side_unaligned[i]);
489 encoder->private->real_signal_mid_side_unaligned[i] = 0;
492 for(channel = 0; channel < encoder->protected->channels; channel++) {
493 for(i = 0; i < 2; i++) {
494 if(encoder->private->residual_workspace_unaligned[channel][i] != 0) {
495 free(encoder->private->residual_workspace_unaligned[channel][i]);
496 encoder->private->residual_workspace_unaligned[channel][i] = 0;
500 for(channel = 0; channel < 2; channel++) {
501 for(i = 0; i < 2; i++) {
502 if(encoder->private->residual_workspace_mid_side_unaligned[channel][i] != 0) {
503 free(encoder->private->residual_workspace_mid_side_unaligned[channel][i]);
504 encoder->private->residual_workspace_mid_side_unaligned[channel][i] = 0;
508 if(encoder->private->abs_residual_unaligned != 0) {
509 free(encoder->private->abs_residual_unaligned);
510 encoder->private->abs_residual_unaligned = 0;
512 if(encoder->private->abs_residual_partition_sums_unaligned != 0) {
513 free(encoder->private->abs_residual_partition_sums_unaligned);
514 encoder->private->abs_residual_partition_sums_unaligned = 0;
516 if(encoder->private->raw_bits_per_partition_unaligned != 0) {
517 free(encoder->private->raw_bits_per_partition_unaligned);
518 encoder->private->raw_bits_per_partition_unaligned = 0;
520 FLAC__bitbuffer_free(&encoder->private->frame);
522 encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
525 FLAC__bool FLAC__stream_encoder_set_streamable_subset(const FLAC__StreamEncoder *encoder, FLAC__bool value)
527 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
529 encoder->protected->streamable_subset = value;
533 FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(const FLAC__StreamEncoder *encoder, FLAC__bool value)
535 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
537 encoder->protected->do_mid_side_stereo = value;
541 FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder, FLAC__bool value)
543 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
545 encoder->protected->loose_mid_side_stereo = value;
549 FLAC__bool FLAC__stream_encoder_set_channels(const FLAC__StreamEncoder *encoder, unsigned value)
551 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
553 encoder->protected->channels = value;
557 FLAC__bool FLAC__stream_encoder_set_bits_per_sample(const FLAC__StreamEncoder *encoder, unsigned value)
559 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
561 encoder->protected->bits_per_sample = value;
565 FLAC__bool FLAC__stream_encoder_set_sample_rate(const FLAC__StreamEncoder *encoder, unsigned value)
567 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
569 encoder->protected->sample_rate = value;
573 FLAC__bool FLAC__stream_encoder_set_blocksize(const FLAC__StreamEncoder *encoder, unsigned value)
575 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
577 encoder->protected->blocksize = value;
581 FLAC__bool FLAC__stream_encoder_set_max_lpc_order(const FLAC__StreamEncoder *encoder, unsigned value)
583 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
585 encoder->protected->max_lpc_order = value;
589 FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(const FLAC__StreamEncoder *encoder, unsigned value)
591 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
593 encoder->protected->qlp_coeff_precision = value;
597 FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder, FLAC__bool value)
599 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
601 encoder->protected->do_qlp_coeff_prec_search = value;
605 FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder, FLAC__bool value)
607 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
609 encoder->protected->do_exhaustive_model_search = value;
613 FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value)
615 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
617 encoder->protected->min_residual_partition_order = value;
621 FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value)
623 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
625 encoder->protected->max_residual_partition_order = value;
629 FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder, unsigned value)
631 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
633 encoder->protected->rice_parameter_search_dist = value;
637 FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(const FLAC__StreamEncoder *encoder, FLAC__uint64 value)
639 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
641 encoder->protected->total_samples_estimate = value;
645 FLAC__bool FLAC__stream_encoder_set_seek_table(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData_SeekTable *value)
647 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
649 encoder->protected->seek_table = value;
653 FLAC__bool FLAC__stream_encoder_set_padding(const FLAC__StreamEncoder *encoder, unsigned value)
655 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
657 encoder->protected->padding = value;
661 FLAC__bool FLAC__stream_encoder_set_last_metadata_is_last(const FLAC__StreamEncoder *encoder, FLAC__bool value)
663 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
665 encoder->protected->last_metadata_is_last = value;
669 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))
671 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
673 encoder->private->write_callback = value;
677 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))
679 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
681 encoder->private->metadata_callback = value;
685 FLAC__bool FLAC__stream_encoder_set_client_data(const FLAC__StreamEncoder *encoder, void *value)
687 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
689 encoder->private->client_data = value;
693 FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
695 return encoder->protected->state;
698 FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
700 return encoder->protected->streamable_subset;
703 FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
705 return encoder->protected->do_mid_side_stereo;
708 FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
710 return encoder->protected->loose_mid_side_stereo;
713 unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
715 return encoder->protected->channels;
718 unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
720 return encoder->protected->bits_per_sample;
723 unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
725 return encoder->protected->sample_rate;
728 unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
730 return encoder->protected->blocksize;
733 unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
735 return encoder->protected->max_lpc_order;
738 unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
740 return encoder->protected->qlp_coeff_precision;
743 FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
745 return encoder->protected->do_qlp_coeff_prec_search;
748 FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
750 return encoder->protected->do_exhaustive_model_search;
753 unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
755 return encoder->protected->min_residual_partition_order;
758 unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
760 return encoder->protected->max_residual_partition_order;
763 unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
765 return encoder->protected->rice_parameter_search_dist;
768 FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 *buf[], unsigned samples)
770 unsigned i, j, channel;
771 FLAC__int32 x, mid, side;
772 const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
774 FLAC__ASSERT(encoder != 0);
775 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
778 if(encoder->protected->do_mid_side_stereo && channels == 2) {
780 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
781 x = mid = side = buf[0][j];
782 encoder->private->integer_signal[0][i] = x;
783 encoder->private->real_signal[0][i] = (FLAC__real)x;
785 encoder->private->integer_signal[1][i] = x;
786 encoder->private->real_signal[1][i] = (FLAC__real)x;
789 mid >>= 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
790 encoder->private->integer_signal_mid_side[1][i] = side;
791 encoder->private->integer_signal_mid_side[0][i] = mid;
792 encoder->private->real_signal_mid_side[1][i] = (FLAC__real)side;
793 encoder->private->real_signal_mid_side[0][i] = (FLAC__real)mid;
794 encoder->private->current_sample_number++;
797 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
800 } while(j < samples);
804 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
805 for(channel = 0; channel < channels; channel++) {
807 encoder->private->integer_signal[channel][i] = x;
808 encoder->private->real_signal[channel][i] = (FLAC__real)x;
810 encoder->private->current_sample_number++;
813 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
816 } while(j < samples);
822 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
823 FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buf[], unsigned samples)
825 unsigned i, j, k, channel;
826 FLAC__int32 x, mid, side;
827 const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
829 FLAC__ASSERT(encoder != 0);
830 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
833 if(encoder->protected->do_mid_side_stereo && channels == 2) {
835 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
836 x = mid = side = buf[k++];
837 encoder->private->integer_signal[0][i] = x;
838 encoder->private->real_signal[0][i] = (FLAC__real)x;
840 encoder->private->integer_signal[1][i] = x;
841 encoder->private->real_signal[1][i] = (FLAC__real)x;
844 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
845 encoder->private->integer_signal_mid_side[1][i] = side;
846 encoder->private->integer_signal_mid_side[0][i] = mid;
847 encoder->private->real_signal_mid_side[1][i] = (FLAC__real)side;
848 encoder->private->real_signal_mid_side[0][i] = (FLAC__real)mid;
849 encoder->private->current_sample_number++;
852 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
855 } while(j < samples);
859 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
860 for(channel = 0; channel < channels; channel++) {
862 encoder->private->integer_signal[channel][i] = x;
863 encoder->private->real_signal[channel][i] = (FLAC__real)x;
865 encoder->private->current_sample_number++;
868 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
871 } while(j < samples);
877 FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
882 FLAC__ASSERT(new_size > 0);
883 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
884 FLAC__ASSERT(encoder->private->current_sample_number == 0);
886 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
887 if(new_size <= encoder->private->input_capacity)
891 for(i = 0; ok && i < encoder->protected->channels; i++) {
892 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->integer_signal_unaligned[i], &encoder->private->integer_signal[i]);
893 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private->real_signal_unaligned[i], &encoder->private->real_signal[i]);
895 for(i = 0; ok && i < 2; i++) {
896 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]);
897 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]);
899 for(channel = 0; ok && channel < encoder->protected->channels; channel++) {
900 for(i = 0; ok && i < 2; i++) {
901 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->residual_workspace_unaligned[channel][i], &encoder->private->residual_workspace[channel][i]);
904 for(channel = 0; ok && channel < 2; channel++) {
905 for(i = 0; ok && i < 2; i++) {
906 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]);
909 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private->abs_residual_unaligned, &encoder->private->abs_residual);
910 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
911 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size * 2, &encoder->private->abs_residual_partition_sums_unaligned, &encoder->private->abs_residual_partition_sums);
913 #ifdef FLAC__SEARCH_FOR_ESCAPES
914 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private->raw_bits_per_partition_unaligned, &encoder->private->raw_bits_per_partition);
918 encoder->private->input_capacity = new_size;
920 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
925 /***********************************************************************
927 * Private class methods
929 ***********************************************************************/
931 FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
933 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
936 * Accumulate raw signal to the MD5 signature
938 /* 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: */
939 if(!FLAC__MD5Accumulate(&encoder->private->md5context, encoder->private->integer_signal, encoder->protected->channels, encoder->protected->blocksize, (encoder->protected->bits_per_sample+7) / 8)) {
940 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
945 * Process the frame header and subframes into the frame bitbuffer
947 if(!stream_encoder_process_subframes_(encoder, is_last_frame)) {
948 /* the above function sets the state for us in case of an error */
953 * Zero-pad the frame to a byte_boundary
955 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->private->frame)) {
956 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
961 * CRC-16 the whole thing
963 FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned */
964 FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
965 FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__crc16(encoder->private->frame.buffer, encoder->private->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
970 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) {
971 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
976 * Get ready for the next frame
978 encoder->private->current_sample_number = 0;
979 encoder->private->current_frame_number++;
980 encoder->private->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected->blocksize;
981 encoder->private->metadata.data.stream_info.min_framesize = min(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.min_framesize);
982 encoder->private->metadata.data.stream_info.max_framesize = max(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.max_framesize);
987 FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
989 FLAC__FrameHeader frame_header;
990 unsigned channel, min_partition_order = encoder->protected->min_residual_partition_order, max_partition_order;
991 FLAC__bool do_independent, do_mid_side;
994 * Calculate the min,max Rice partition orders
997 max_partition_order = 0;
1000 unsigned limit = 0, b = encoder->protected->blocksize;
1005 max_partition_order = min(encoder->protected->max_residual_partition_order, limit);
1007 min_partition_order = min(min_partition_order, max_partition_order);
1012 if(!FLAC__bitbuffer_clear(&encoder->private->frame)) {
1013 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1016 frame_header.blocksize = encoder->protected->blocksize;
1017 frame_header.sample_rate = encoder->protected->sample_rate;
1018 frame_header.channels = encoder->protected->channels;
1019 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
1020 frame_header.bits_per_sample = encoder->protected->bits_per_sample;
1021 frame_header.number.frame_number = encoder->private->current_frame_number;
1024 * Figure out what channel assignments to try
1026 if(encoder->protected->do_mid_side_stereo) {
1027 if(encoder->protected->loose_mid_side_stereo) {
1028 if(encoder->private->loose_mid_side_stereo_frame_count == 0) {
1029 do_independent = true;
1033 do_independent = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
1034 do_mid_side = !do_independent;
1038 do_independent = true;
1043 do_independent = true;
1044 do_mid_side = false;
1047 FLAC__ASSERT(do_independent || do_mid_side);
1050 * Check for wasted bits; set effective bps for each subframe
1052 if(do_independent) {
1054 for(channel = 0; channel < encoder->protected->channels; channel++) {
1055 w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal[channel], encoder->protected->blocksize);
1056 encoder->private->subframe_workspace[channel][0].wasted_bits = encoder->private->subframe_workspace[channel][1].wasted_bits = w;
1057 encoder->private->subframe_bps[channel] = encoder->protected->bits_per_sample - w;
1062 FLAC__ASSERT(encoder->protected->channels == 2);
1063 for(channel = 0; channel < 2; channel++) {
1064 w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal_mid_side[channel], encoder->protected->blocksize);
1065 encoder->private->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1066 encoder->private->subframe_bps_mid_side[channel] = encoder->protected->bits_per_sample - w + (channel==0? 0:1);
1071 * First do a normal encoding pass of each independent channel
1073 if(do_independent) {
1074 for(channel = 0; channel < encoder->protected->channels; channel++) {
1075 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))
1081 * Now do mid and side channels if requested
1084 FLAC__ASSERT(encoder->protected->channels == 2);
1086 for(channel = 0; channel < 2; channel++) {
1087 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))
1093 * Compose the frame bitbuffer
1096 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1097 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
1098 FLAC__ChannelAssignment channel_assignment;
1100 FLAC__ASSERT(encoder->protected->channels == 2);
1102 if(encoder->protected->loose_mid_side_stereo && encoder->private->loose_mid_side_stereo_frame_count > 0) {
1103 channel_assignment = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
1106 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1108 FLAC__ChannelAssignment ca;
1110 FLAC__ASSERT(do_independent && do_mid_side);
1112 /* We have to figure out which channel assignent results in the smallest frame */
1113 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits [1];
1114 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits_mid_side[1];
1115 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private->best_subframe_bits [1] + encoder->private->best_subframe_bits_mid_side[1];
1116 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private->best_subframe_bits_mid_side[0] + encoder->private->best_subframe_bits_mid_side[1];
1118 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
1119 if(bits[ca] < min_bits) {
1120 min_bits = bits[ca];
1121 channel_assignment = ca;
1126 frame_header.channel_assignment = channel_assignment;
1128 if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
1129 encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1133 switch(channel_assignment) {
1134 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1135 left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]];
1136 right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]];
1138 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1139 left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]];
1140 right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
1142 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1143 left_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
1144 right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]];
1146 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1147 left_subframe = &encoder->private->subframe_workspace_mid_side[0][encoder->private->best_subframe_mid_side[0]];
1148 right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
1154 switch(channel_assignment) {
1155 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1156 left_bps = encoder->private->subframe_bps [0];
1157 right_bps = encoder->private->subframe_bps [1];
1159 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1160 left_bps = encoder->private->subframe_bps [0];
1161 right_bps = encoder->private->subframe_bps_mid_side[1];
1163 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1164 left_bps = encoder->private->subframe_bps_mid_side[1];
1165 right_bps = encoder->private->subframe_bps [1];
1167 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1168 left_bps = encoder->private->subframe_bps_mid_side[0];
1169 right_bps = encoder->private->subframe_bps_mid_side[1];
1175 /* note that encoder_add_subframe_ sets the state for us in case of an error */
1176 if(!stream_encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->private->frame))
1178 if(!stream_encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->private->frame))
1182 if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
1183 encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1187 for(channel = 0; channel < encoder->protected->channels; channel++) {
1188 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)) {
1189 /* the above function sets the state for us in case of an error */
1195 if(encoder->protected->loose_mid_side_stereo) {
1196 encoder->private->loose_mid_side_stereo_frame_count++;
1197 if(encoder->private->loose_mid_side_stereo_frame_count >= encoder->private->loose_mid_side_stereo_frames)
1198 encoder->private->loose_mid_side_stereo_frame_count = 0;
1201 encoder->private->last_channel_assignment = frame_header.channel_assignment;
1206 FLAC__bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, 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)
1208 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1209 FLAC__real lpc_residual_bits_per_sample;
1210 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 */
1211 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
1212 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
1213 unsigned min_lpc_order, max_lpc_order, lpc_order;
1214 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1215 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1216 unsigned rice_parameter;
1217 unsigned _candidate_bits, _best_bits;
1218 unsigned _best_subframe;
1220 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1222 _best_bits = stream_encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
1224 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1225 /* check for constant subframe */
1226 if(encoder->private->use_slow)
1227 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);
1229 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);
1230 if(fixed_residual_bits_per_sample[1] == 0.0) {
1231 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1232 unsigned i, signal_is_constant = true;
1233 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1234 if(integer_signal[0] != integer_signal[i]) {
1235 signal_is_constant = false;
1239 if(signal_is_constant) {
1240 _candidate_bits = stream_encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1241 if(_candidate_bits < _best_bits) {
1242 _best_subframe = !_best_subframe;
1243 _best_bits = _candidate_bits;
1249 if(encoder->protected->do_exhaustive_model_search) {
1250 min_fixed_order = 0;
1251 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1254 min_fixed_order = max_fixed_order = guess_fixed_order;
1256 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1257 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1258 continue; /* don't even try */
1259 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 */
1260 #ifndef FLAC__SYMMETRIC_RICE
1261 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1263 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1264 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1265 _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]);
1266 if(_candidate_bits < _best_bits) {
1267 _best_subframe = !_best_subframe;
1268 _best_bits = _candidate_bits;
1273 if(encoder->protected->max_lpc_order > 0) {
1274 if(encoder->protected->max_lpc_order >= frame_header->blocksize)
1275 max_lpc_order = frame_header->blocksize-1;
1277 max_lpc_order = encoder->protected->max_lpc_order;
1278 if(max_lpc_order > 0) {
1279 encoder->private->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
1280 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
1281 if(autoc[0] != 0.0) {
1282 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
1283 if(encoder->protected->do_exhaustive_model_search) {
1287 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
1288 min_lpc_order = max_lpc_order = guess_lpc_order;
1290 if(encoder->protected->do_qlp_coeff_prec_search) {
1291 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
1292 max_qlp_coeff_precision = min(32 - subframe_bps - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1);
1295 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected->qlp_coeff_precision;
1297 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
1298 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
1299 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
1300 continue; /* don't even try */
1301 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
1302 #ifndef FLAC__SYMMETRIC_RICE
1303 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1305 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1306 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1307 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
1308 _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]);
1309 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1310 if(_candidate_bits < _best_bits) {
1311 _best_subframe = !_best_subframe;
1312 _best_bits = _candidate_bits;
1323 *best_subframe = _best_subframe;
1324 *best_bits = _best_bits;
1329 FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
1331 switch(subframe->type) {
1332 case FLAC__SUBFRAME_TYPE_CONSTANT:
1333 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
1334 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1338 case FLAC__SUBFRAME_TYPE_FIXED:
1339 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
1340 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1344 case FLAC__SUBFRAME_TYPE_LPC:
1345 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
1346 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1350 case FLAC__SUBFRAME_TYPE_VERBATIM:
1351 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
1352 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
1363 unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
1365 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1366 subframe->data.constant.value = signal;
1368 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
1371 unsigned stream_encoder_evaluate_fixed_subframe_(const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__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)
1373 unsigned i, residual_bits;
1374 const unsigned residual_samples = blocksize - order;
1376 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1378 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1380 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1381 subframe->data.fixed.residual = residual;
1383 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);
1385 subframe->data.fixed.order = order;
1386 for(i = 0; i < order; i++)
1387 subframe->data.fixed.warmup[i] = signal[i];
1389 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
1392 unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint32 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
1394 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1395 unsigned i, residual_bits;
1396 int quantization, ret;
1397 const unsigned residual_samples = blocksize - order;
1399 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
1401 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1403 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
1404 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1406 encoder->private->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1408 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1410 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1411 subframe->data.lpc.residual = residual;
1413 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);
1415 subframe->data.lpc.order = order;
1416 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1417 subframe->data.lpc.quantization_level = quantization;
1418 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
1419 for(i = 0; i < order; i++)
1420 subframe->data.lpc.warmup[i] = signal[i];
1422 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;
1425 unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
1427 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1429 subframe->data.verbatim.data = signal;
1431 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
1434 unsigned stream_encoder_find_best_partition_order_(const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__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[])
1437 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1439 int partition_order;
1441 unsigned partition_order;
1443 unsigned residual_bits, best_residual_bits = 0;
1444 unsigned residual_sample;
1445 unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
1447 /* compute abs(residual) for use later */
1448 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1449 r = residual[residual_sample];
1450 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
1453 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1454 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);
1455 min_partition_order = min(min_partition_order, max_partition_order);
1457 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1458 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)) {
1459 FLAC__ASSERT(0); /* stream_encoder_precompute_partition_info_ should keep this from ever happening */
1461 sum += 1u << partition_order;
1462 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1463 best_residual_bits = residual_bits;
1464 *best_partition_order = partition_order;
1465 best_parameters_index = !best_parameters_index;
1469 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1470 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)) {
1471 FLAC__ASSERT(best_residual_bits != 0);
1474 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1475 best_residual_bits = residual_bits;
1476 *best_partition_order = partition_order;
1477 best_parameters_index = !best_parameters_index;
1481 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1482 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1484 return best_residual_bits;
1487 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1488 unsigned stream_encoder_precompute_partition_info_(const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
1490 int partition_order;
1491 unsigned from_partition, to_partition = 0;
1492 const unsigned blocksize = residual_samples + predictor_order;
1494 /* first do max_partition_order */
1495 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1496 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1497 FLAC__uint32 abs_residual_partition_sum;
1499 #ifdef FLAC__SEARCH_FOR_ESCAPES
1500 FLAC__uint32 abs_residual_partition_max;
1501 unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */
1504 unsigned partition, partition_sample, partition_samples, residual_sample;
1505 const unsigned partitions = 1u << partition_order;
1506 const unsigned default_partition_samples = blocksize >> partition_order;
1508 if(default_partition_samples <= predictor_order) {
1509 FLAC__ASSERT(max_partition_order > 0);
1510 max_partition_order--;
1513 for(partition = residual_sample = 0; partition < partitions; partition++) {
1514 partition_samples = default_partition_samples;
1516 partition_samples -= predictor_order;
1517 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1518 abs_residual_partition_sum = 0;
1520 #ifdef FLAC__SEARCH_FOR_ESCAPES
1521 abs_residual_partition_max = 0;
1523 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1524 abs_r = abs_residual[residual_sample];
1525 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1526 abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */
1528 #ifdef FLAC__SEARCH_FOR_ESCAPES
1529 if(abs_r > abs_residual_partition_max) {
1530 abs_residual_partition_max = abs_r;
1531 abs_residual_partition_max_index = residual_sample;
1536 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1537 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
1539 #ifdef FLAC__SEARCH_FOR_ESCAPES
1540 if(abs_residual_partition_max > 0)
1541 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]);
1543 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0);
1546 to_partition = partitions;
1551 /* now merge for lower orders */
1552 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1553 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1556 #ifdef FLAC__SEARCH_FOR_ESCAPES
1560 const unsigned partitions = 1u << partition_order;
1561 for(i = 0; i < partitions; i++) {
1562 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1563 s = abs_residual_partition_sums[from_partition];
1565 #ifdef FLAC__SEARCH_FOR_ESCAPES
1566 m = raw_bits_per_partition[from_partition];
1569 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1570 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1572 #ifdef FLAC__SEARCH_FOR_ESCAPES
1573 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1580 return max_partition_order;
1584 #ifdef VARIABLE_RICE_BITS
1585 #undef VARIABLE_RICE_BITS
1587 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1589 FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__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)
1591 unsigned rice_parameter, partition_bits;
1592 #ifndef NO_RICE_SEARCH
1593 unsigned best_partition_bits;
1594 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1596 #ifdef FLAC__SEARCH_FOR_ESCAPES
1599 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1601 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1603 if(partition_order == 0) {
1606 #ifndef NO_RICE_SEARCH
1607 if(rice_parameter_search_dist) {
1608 if(suggested_rice_parameter < rice_parameter_search_dist)
1609 min_rice_parameter = 0;
1611 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1612 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1613 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1614 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1617 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1619 best_partition_bits = 0xffffffff;
1620 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1622 #ifdef VARIABLE_RICE_BITS
1623 #ifdef FLAC__SYMMETRIC_RICE
1624 partition_bits = (2+rice_parameter) * residual_samples;
1626 const unsigned rice_parameter_estimate = rice_parameter-1;
1627 partition_bits = (1+rice_parameter) * residual_samples;
1632 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1633 for(i = 0; i < residual_samples; i++) {
1634 #ifdef VARIABLE_RICE_BITS
1635 #ifdef FLAC__SYMMETRIC_RICE
1636 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1638 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1641 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1644 #ifndef NO_RICE_SEARCH
1645 if(partition_bits < best_partition_bits) {
1646 best_rice_parameter = rice_parameter;
1647 best_partition_bits = partition_bits;
1651 #ifdef FLAC__SEARCH_FOR_ESCAPES
1652 flat_bits = raw_bits_per_partition[0] * residual_samples;
1653 if(flat_bits <= best_partition_bits) {
1654 raw_bits[0] = raw_bits_per_partition[0];
1655 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1656 best_partition_bits = flat_bits;
1659 parameters[0] = best_rice_parameter;
1660 bits_ += best_partition_bits;
1663 unsigned partition, residual_sample, save_residual_sample, partition_sample;
1664 unsigned mean, partition_samples;
1665 const unsigned partitions = 1u << partition_order;
1666 for(partition = residual_sample = 0; partition < partitions; partition++) {
1667 partition_samples = (residual_samples+predictor_order) >> partition_order;
1668 if(partition == 0) {
1669 if(partition_samples <= predictor_order)
1672 partition_samples -= predictor_order;
1674 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1675 mean = abs_residual_partition_sums[partition];
1678 save_residual_sample = residual_sample;
1679 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
1680 mean += abs_residual[residual_sample];
1681 residual_sample = save_residual_sample;
1683 #ifdef FLAC__SYMMETRIC_RICE
1684 mean += partition_samples >> 1; /* for rounding effect */
1685 mean /= partition_samples;
1687 /* calc rice_parameter = floor(log2(mean)) */
1695 /* calc rice_parameter ala LOCO-I */
1696 for(rice_parameter = 0; (partition_samples<<rice_parameter) < mean; rice_parameter++)
1699 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1700 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1702 #ifndef NO_RICE_SEARCH
1703 if(rice_parameter_search_dist) {
1704 if(rice_parameter < rice_parameter_search_dist)
1705 min_rice_parameter = 0;
1707 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1708 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
1709 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1710 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1713 min_rice_parameter = max_rice_parameter = rice_parameter;
1715 best_partition_bits = 0xffffffff;
1716 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1718 #ifdef VARIABLE_RICE_BITS
1719 #ifdef FLAC__SYMMETRIC_RICE
1720 partition_bits = (2+rice_parameter) * partition_samples;
1722 const unsigned rice_parameter_estimate = rice_parameter-1;
1723 partition_bits = (1+rice_parameter) * partition_samples;
1728 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1729 save_residual_sample = residual_sample;
1730 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
1731 #ifdef VARIABLE_RICE_BITS
1732 #ifdef FLAC__SYMMETRIC_RICE
1733 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
1735 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
1738 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1741 #ifndef NO_RICE_SEARCH
1742 if(rice_parameter != max_rice_parameter)
1743 residual_sample = save_residual_sample;
1744 if(partition_bits < best_partition_bits) {
1745 best_rice_parameter = rice_parameter;
1746 best_partition_bits = partition_bits;
1750 #ifdef FLAC__SEARCH_FOR_ESCAPES
1751 flat_bits = raw_bits_per_partition[partition] * partition_samples;
1752 if(flat_bits <= best_partition_bits) {
1753 raw_bits[partition] = raw_bits_per_partition[partition];
1754 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1755 best_partition_bits = flat_bits;
1758 parameters[partition] = best_rice_parameter;
1759 bits_ += best_partition_bits;
1767 unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
1772 for(i = 0; i < samples && !(x&1); i++)
1779 for(shift = 0; !(x&1); shift++)
1784 for(i = 0; i < samples; i++)
1785 signal[i] >>= shift;