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/encoder.h"
25 #include "FLAC/seek_table.h"
26 #include "private/bitbuffer.h"
27 #include "private/bitmath.h"
28 #include "private/crc.h"
29 #include "private/cpu.h"
30 #include "private/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 typedef struct FLAC__EncoderPrivate {
47 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
48 int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
49 int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
50 real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
51 real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
52 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
53 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
54 int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
55 int32 *residual_workspace_mid_side[2][2];
56 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
57 FLAC__Subframe subframe_workspace_mid_side[2][2];
58 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
59 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
60 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
61 unsigned best_subframe_mid_side[2];
62 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
63 unsigned best_subframe_bits_mid_side[2];
64 uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
65 uint32 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
66 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
67 FLAC__BitBuffer frame; /* the current frame being worked on */
68 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
69 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
70 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
71 FLAC__ChannelAssignment last_channel_assignment;
72 FLAC__StreamMetaData metadata;
73 unsigned current_sample_number;
74 unsigned current_frame_number;
75 struct MD5Context md5context;
76 FLAC__CPUInfo cpuinfo;
77 unsigned (*local_fixed_compute_best_predictor)(const int32 data[], unsigned data_len, real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
78 void (*local_lpc_compute_autocorrelation)(const real data[], unsigned data_len, unsigned lag, real autoc[]);
79 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[]);
80 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[]);
81 bool use_slow; /* use slow 64-bit versions of some functions */
82 FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
83 void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
85 /* unaligned (original) pointers to allocated data */
86 int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
87 int32 *integer_signal_mid_side_unaligned[2];
88 real *real_signal_unaligned[FLAC__MAX_CHANNELS];
89 real *real_signal_mid_side_unaligned[2];
90 int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
91 int32 *residual_workspace_mid_side_unaligned[2][2];
92 uint32 *abs_residual_unaligned;
93 uint32 *abs_residual_partition_sums_unaligned;
94 unsigned *raw_bits_per_partition_unaligned;
95 } FLAC__EncoderPrivate;
97 static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
98 static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
99 static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
100 static bool encoder_process_subframe_(FLAC__Encoder *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);
101 static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
102 static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
103 static unsigned 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);
104 static unsigned encoder_evaluate_lpc_subframe_(FLAC__Encoder *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);
105 static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
106 static unsigned 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[]);
107 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
108 static unsigned 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);
110 static bool 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);
111 static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples);
113 const char *FLAC__EncoderWriteStatusString[] = {
114 "FLAC__ENCODER_WRITE_OK",
115 "FLAC__ENCODER_WRITE_FATAL_ERROR"
118 const char *FLAC__EncoderStateString[] = {
120 "FLAC__ENCODER_UNINITIALIZED",
121 "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
122 "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
123 "FLAC__ENCODER_INVALID_SAMPLE_RATE",
124 "FLAC__ENCODER_INVALID_BLOCK_SIZE",
125 "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
126 "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
127 "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
128 "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
129 "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
130 "FLAC__ENCODER_NOT_STREAMABLE",
131 "FLAC__ENCODER_FRAMING_ERROR",
132 "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
133 "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
134 "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
138 bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
143 FLAC__ASSERT(new_size > 0);
144 FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
145 FLAC__ASSERT(encoder->guts->current_sample_number == 0);
147 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
148 if(new_size <= encoder->guts->input_capacity)
152 for(i = 0; ok && i < encoder->channels; i++) {
153 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_unaligned[i], &encoder->guts->integer_signal[i]);
154 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_unaligned[i], &encoder->guts->real_signal[i]);
156 for(i = 0; ok && i < 2; i++) {
157 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_mid_side_unaligned[i], &encoder->guts->integer_signal_mid_side[i]);
158 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_mid_side_unaligned[i], &encoder->guts->real_signal_mid_side[i]);
160 for(channel = 0; ok && channel < encoder->channels; channel++) {
161 for(i = 0; ok && i < 2; i++) {
162 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_unaligned[channel][i], &encoder->guts->residual_workspace[channel][i]);
165 for(channel = 0; ok && channel < 2; channel++) {
166 for(i = 0; ok && i < 2; i++) {
167 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_mid_side_unaligned[channel][i], &encoder->guts->residual_workspace_mid_side[channel][i]);
170 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->guts->abs_residual_unaligned, &encoder->guts->abs_residual);
171 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
172 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size * 2, &encoder->guts->abs_residual_partition_sums_unaligned, &encoder->guts->abs_residual_partition_sums);
174 #ifdef FLAC__SEARCH_FOR_ESCAPES
175 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->guts->raw_bits_per_partition_unaligned, &encoder->guts->raw_bits_per_partition);
179 encoder->guts->input_capacity = new_size;
181 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
186 FLAC__Encoder *FLAC__encoder_get_new_instance()
188 FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
190 encoder->state = FLAC__ENCODER_UNINITIALIZED;
196 void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
198 FLAC__ASSERT(encoder != 0);
202 FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data), void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data), void *client_data)
205 FLAC__StreamMetaData padding;
206 FLAC__StreamMetaData seek_table;
208 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
209 FLAC__ASSERT(encoder != 0);
210 FLAC__ASSERT(write_callback != 0);
211 FLAC__ASSERT(metadata_callback != 0);
212 FLAC__ASSERT(encoder->state == FLAC__ENCODER_UNINITIALIZED);
213 FLAC__ASSERT(encoder->guts == 0);
215 encoder->state = FLAC__ENCODER_OK;
217 if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
218 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
220 if(encoder->do_mid_side_stereo && encoder->channels != 2)
221 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
223 if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
224 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
226 if(encoder->bits_per_sample >= 32)
227 encoder->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
229 if(encoder->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
230 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
232 if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
233 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
235 if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
236 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
238 if(encoder->blocksize < encoder->max_lpc_order)
239 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
241 if(encoder->qlp_coeff_precision == 0) {
242 if(encoder->bits_per_sample < 16) {
243 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
244 /* @@@ until then we'll make a guess */
245 encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
247 else if(encoder->bits_per_sample == 16) {
248 if(encoder->blocksize <= 192)
249 encoder->qlp_coeff_precision = 7;
250 else if(encoder->blocksize <= 384)
251 encoder->qlp_coeff_precision = 8;
252 else if(encoder->blocksize <= 576)
253 encoder->qlp_coeff_precision = 9;
254 else if(encoder->blocksize <= 1152)
255 encoder->qlp_coeff_precision = 10;
256 else if(encoder->blocksize <= 2304)
257 encoder->qlp_coeff_precision = 11;
258 else if(encoder->blocksize <= 4608)
259 encoder->qlp_coeff_precision = 12;
261 encoder->qlp_coeff_precision = 13;
264 encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
267 else if(encoder->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->qlp_coeff_precision + encoder->bits_per_sample >= 8*sizeof(uint32) || encoder->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
268 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
270 if(encoder->streamable_subset) {
271 //@@@ add check for blocksize here
272 if(encoder->bits_per_sample != 8 && encoder->bits_per_sample != 12 && encoder->bits_per_sample != 16 && encoder->bits_per_sample != 20 && encoder->bits_per_sample != 24)
273 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
274 if(encoder->sample_rate > 655350)
275 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
278 if(encoder->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
279 encoder->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
280 if(encoder->min_residual_partition_order >= encoder->max_residual_partition_order)
281 encoder->min_residual_partition_order = encoder->max_residual_partition_order;
283 encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
284 if(encoder->guts == 0)
285 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
287 encoder->guts->input_capacity = 0;
288 for(i = 0; i < encoder->channels; i++) {
289 encoder->guts->integer_signal_unaligned[i] = encoder->guts->integer_signal[i] = 0;
290 encoder->guts->real_signal_unaligned[i] = encoder->guts->real_signal[i] = 0;
292 for(i = 0; i < 2; i++) {
293 encoder->guts->integer_signal_mid_side_unaligned[i] = encoder->guts->integer_signal_mid_side[i] = 0;
294 encoder->guts->real_signal_mid_side_unaligned[i] = encoder->guts->real_signal_mid_side[i] = 0;
296 for(i = 0; i < encoder->channels; i++) {
297 encoder->guts->residual_workspace_unaligned[i][0] = encoder->guts->residual_workspace[i][0] = 0;
298 encoder->guts->residual_workspace_unaligned[i][1] = encoder->guts->residual_workspace[i][1] = 0;
299 encoder->guts->best_subframe[i] = 0;
301 for(i = 0; i < 2; i++) {
302 encoder->guts->residual_workspace_mid_side_unaligned[i][0] = encoder->guts->residual_workspace_mid_side[i][0] = 0;
303 encoder->guts->residual_workspace_mid_side_unaligned[i][1] = encoder->guts->residual_workspace_mid_side[i][1] = 0;
304 encoder->guts->best_subframe_mid_side[i] = 0;
306 for(i = 0; i < encoder->channels; i++) {
307 encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
308 encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
310 for(i = 0; i < 2; i++) {
311 encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
312 encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
314 encoder->guts->abs_residual_unaligned = encoder->guts->abs_residual = 0;
315 encoder->guts->abs_residual_partition_sums_unaligned = encoder->guts->abs_residual_partition_sums = 0;
316 encoder->guts->raw_bits_per_partition_unaligned = encoder->guts->raw_bits_per_partition = 0;
317 encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
318 encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
319 if(encoder->guts->loose_mid_side_stereo_frames == 0)
320 encoder->guts->loose_mid_side_stereo_frames = 1;
321 encoder->guts->loose_mid_side_stereo_frame_count = 0;
322 encoder->guts->current_sample_number = 0;
323 encoder->guts->current_frame_number = 0;
326 * get the CPU info and set the function pointers
328 FLAC__cpu_info(&encoder->guts->cpuinfo);
329 /* first default to the non-asm routines */
330 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
331 encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
332 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
333 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
334 /* now override with asm where appropriate */
336 FLAC__ASSERT(encoder->guts->cpuinfo.use_asm);
337 #ifdef FLAC__CPU_IA32
338 FLAC__ASSERT(encoder->guts->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
339 #ifdef FLAC__HAS_NASM
340 if(0 && encoder->guts->cpuinfo.data.ia32.sse) { /* SSE version lacks necessary resolution, plus SSE flag doesn't check for OS support */
341 if(encoder->max_lpc_order < 4)
342 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_4;
343 else if(encoder->max_lpc_order < 8)
344 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_8;
345 else if(encoder->max_lpc_order < 12)
346 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_12;
348 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
351 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
352 if(encoder->guts->cpuinfo.data.ia32.mmx && encoder->guts->cpuinfo.data.ia32.cmov)
353 encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_i386_mmx_cmov;
354 if(encoder->guts->cpuinfo.data.ia32.mmx) {
355 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
356 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386_mmx;
359 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
360 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
366 if(encoder->bits_per_sample + FLAC__bitmath_ilog2(encoder->blocksize)+1 > 30)
367 encoder->guts->use_slow = true;
369 encoder->guts->use_slow = false;
371 if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
372 /* the above function sets the state for us in case of an error */
373 return encoder->state;
375 FLAC__bitbuffer_init(&encoder->guts->frame);
376 encoder->guts->write_callback = write_callback;
377 encoder->guts->metadata_callback = metadata_callback;
378 encoder->guts->client_data = client_data;
381 * write the stream header
383 if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
384 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
386 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
387 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
389 encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
390 encoder->guts->metadata.is_last = (encoder->seek_table == 0 && encoder->padding == 0);
391 encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
392 encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
393 encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
394 encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
395 encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
396 encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
397 encoder->guts->metadata.data.stream_info.channels = encoder->channels;
398 encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
399 encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
400 memset(encoder->guts->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
401 MD5Init(&encoder->guts->md5context);
402 if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
403 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
405 if(0 != encoder->seek_table) {
406 if(!FLAC__seek_table_is_valid(encoder->seek_table))
407 return encoder->state = FLAC__ENCODER_INVALID_SEEK_TABLE;
408 seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
409 seek_table.is_last = (encoder->padding == 0);
410 seek_table.length = encoder->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
411 seek_table.data.seek_table = *encoder->seek_table;
412 if(!FLAC__add_metadata_block(&seek_table, &encoder->guts->frame))
413 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
416 /* add a PADDING block if requested */
417 if(encoder->padding > 0) {
418 padding.type = FLAC__METADATA_TYPE_PADDING;
419 padding.is_last = true;
420 padding.length = encoder->padding;
421 if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
422 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
425 FLAC__ASSERT(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
426 FLAC__ASSERT(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
427 if(encoder->guts->write_callback(encoder, encoder->guts->frame.buffer, encoder->guts->frame.bytes, 0, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK)
428 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
430 /* now that the metadata block is written, we can init this to an absurdly-high value... */
431 encoder->guts->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
432 /* ... and clear this to 0 */
433 encoder->guts->metadata.data.stream_info.total_samples = 0;
435 return encoder->state;
438 void FLAC__encoder_finish(FLAC__Encoder *encoder)
442 FLAC__ASSERT(encoder != 0);
443 if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
445 if(encoder->guts->current_sample_number != 0) {
446 encoder->blocksize = encoder->guts->current_sample_number;
447 encoder_process_frame_(encoder, true); /* true => is last frame */
449 MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
450 encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
451 if(encoder->guts != 0) {
452 for(i = 0; i < encoder->channels; i++) {
453 if(encoder->guts->integer_signal_unaligned[i] != 0) {
454 free(encoder->guts->integer_signal_unaligned[i]);
455 encoder->guts->integer_signal_unaligned[i] = 0;
457 if(encoder->guts->real_signal_unaligned[i] != 0) {
458 free(encoder->guts->real_signal_unaligned[i]);
459 encoder->guts->real_signal_unaligned[i] = 0;
462 for(i = 0; i < 2; i++) {
463 if(encoder->guts->integer_signal_mid_side_unaligned[i] != 0) {
464 free(encoder->guts->integer_signal_mid_side_unaligned[i]);
465 encoder->guts->integer_signal_mid_side_unaligned[i] = 0;
467 if(encoder->guts->real_signal_mid_side_unaligned[i] != 0) {
468 free(encoder->guts->real_signal_mid_side_unaligned[i]);
469 encoder->guts->real_signal_mid_side_unaligned[i] = 0;
472 for(channel = 0; channel < encoder->channels; channel++) {
473 for(i = 0; i < 2; i++) {
474 if(encoder->guts->residual_workspace_unaligned[channel][i] != 0) {
475 free(encoder->guts->residual_workspace_unaligned[channel][i]);
476 encoder->guts->residual_workspace_unaligned[channel][i] = 0;
480 for(channel = 0; channel < 2; channel++) {
481 for(i = 0; i < 2; i++) {
482 if(encoder->guts->residual_workspace_mid_side_unaligned[channel][i] != 0) {
483 free(encoder->guts->residual_workspace_mid_side_unaligned[channel][i]);
484 encoder->guts->residual_workspace_mid_side_unaligned[channel][i] = 0;
488 if(encoder->guts->abs_residual_unaligned != 0) {
489 free(encoder->guts->abs_residual_unaligned);
490 encoder->guts->abs_residual_unaligned = 0;
492 if(encoder->guts->abs_residual_partition_sums_unaligned != 0) {
493 free(encoder->guts->abs_residual_partition_sums_unaligned);
494 encoder->guts->abs_residual_partition_sums_unaligned = 0;
496 if(encoder->guts->raw_bits_per_partition_unaligned != 0) {
497 free(encoder->guts->raw_bits_per_partition_unaligned);
498 encoder->guts->raw_bits_per_partition_unaligned = 0;
500 FLAC__bitbuffer_free(&encoder->guts->frame);
504 encoder->state = FLAC__ENCODER_UNINITIALIZED;
507 bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
509 unsigned i, j, channel;
511 const unsigned channels = encoder->channels, blocksize = encoder->blocksize;
513 FLAC__ASSERT(encoder != 0);
514 FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
517 if(encoder->do_mid_side_stereo && channels == 2) {
519 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
520 x = mid = side = buf[0][j];
521 encoder->guts->integer_signal[0][i] = x;
522 encoder->guts->real_signal[0][i] = (real)x;
524 encoder->guts->integer_signal[1][i] = x;
525 encoder->guts->real_signal[1][i] = (real)x;
528 mid >>= 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
529 encoder->guts->integer_signal_mid_side[1][i] = side;
530 encoder->guts->integer_signal_mid_side[0][i] = mid;
531 encoder->guts->real_signal_mid_side[1][i] = (real)side;
532 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
533 encoder->guts->current_sample_number++;
536 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
539 } while(j < samples);
543 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
544 for(channel = 0; channel < channels; channel++) {
546 encoder->guts->integer_signal[channel][i] = x;
547 encoder->guts->real_signal[channel][i] = (real)x;
549 encoder->guts->current_sample_number++;
552 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
555 } while(j < samples);
561 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
562 bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
564 unsigned i, j, k, channel;
566 const unsigned channels = encoder->channels, blocksize = encoder->blocksize;
568 FLAC__ASSERT(encoder != 0);
569 FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
572 if(encoder->do_mid_side_stereo && channels == 2) {
574 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
575 x = mid = side = buf[k++];
576 encoder->guts->integer_signal[0][i] = x;
577 encoder->guts->real_signal[0][i] = (real)x;
579 encoder->guts->integer_signal[1][i] = x;
580 encoder->guts->real_signal[1][i] = (real)x;
583 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
584 encoder->guts->integer_signal_mid_side[1][i] = side;
585 encoder->guts->integer_signal_mid_side[0][i] = mid;
586 encoder->guts->real_signal_mid_side[1][i] = (real)side;
587 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
588 encoder->guts->current_sample_number++;
591 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
594 } while(j < samples);
598 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
599 for(channel = 0; channel < channels; channel++) {
601 encoder->guts->integer_signal[channel][i] = x;
602 encoder->guts->real_signal[channel][i] = (real)x;
604 encoder->guts->current_sample_number++;
607 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
610 } while(j < samples);
616 bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
618 FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
621 * Accumulate raw signal to the MD5 signature
623 /* 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: */
624 if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
625 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
630 * Process the frame header and subframes into the frame bitbuffer
632 if(!encoder_process_subframes_(encoder, is_last_frame)) {
633 /* the above function sets the state for us in case of an error */
638 * Zero-pad the frame to a byte_boundary
640 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
641 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
646 * CRC-16 the whole thing
648 FLAC__ASSERT(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */
649 FLAC__ASSERT(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
650 FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
655 if(encoder->guts->write_callback(encoder, encoder->guts->frame.buffer, encoder->guts->frame.bytes, encoder->blocksize, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK) {
656 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
661 * Get ready for the next frame
663 encoder->guts->current_sample_number = 0;
664 encoder->guts->current_frame_number++;
665 encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
666 encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
667 encoder->guts->metadata.data.stream_info.max_framesize = max(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.max_framesize);
672 bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
674 FLAC__FrameHeader frame_header;
675 unsigned channel, min_partition_order = encoder->min_residual_partition_order, max_partition_order;
676 bool do_independent, do_mid_side;
679 * Calculate the min,max Rice partition orders
682 max_partition_order = 0;
685 unsigned limit = 0, b = encoder->blocksize;
690 max_partition_order = min(encoder->max_residual_partition_order, limit);
692 min_partition_order = min(min_partition_order, max_partition_order);
697 if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
698 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
701 frame_header.blocksize = encoder->blocksize;
702 frame_header.sample_rate = encoder->sample_rate;
703 frame_header.channels = encoder->channels;
704 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
705 frame_header.bits_per_sample = encoder->bits_per_sample;
706 frame_header.number.frame_number = encoder->guts->current_frame_number;
709 * Figure out what channel assignments to try
711 if(encoder->do_mid_side_stereo) {
712 if(encoder->loose_mid_side_stereo) {
713 if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
714 do_independent = true;
718 do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
719 do_mid_side = !do_independent;
723 do_independent = true;
728 do_independent = true;
732 FLAC__ASSERT(do_independent || do_mid_side);
735 * Check for wasted bits; set effective bps for each subframe
739 for(channel = 0; channel < encoder->channels; channel++) {
740 w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize);
741 encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w;
742 encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w;
747 FLAC__ASSERT(encoder->channels == 2);
748 for(channel = 0; channel < 2; channel++) {
749 w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize);
750 encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w;
751 encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1);
756 * First do a normal encoding pass of each independent channel
759 for(channel = 0; channel < encoder->channels; channel++) {
760 if(!encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->guts->subframe_bps[channel], encoder->guts->integer_signal[channel], encoder->guts->real_signal[channel], encoder->guts->subframe_workspace_ptr[channel], encoder->guts->residual_workspace[channel], encoder->guts->best_subframe+channel, encoder->guts->best_subframe_bits+channel))
766 * Now do mid and side channels if requested
769 FLAC__ASSERT(encoder->channels == 2);
771 for(channel = 0; channel < 2; channel++) {
772 if(!encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->guts->subframe_bps_mid_side[channel], encoder->guts->integer_signal_mid_side[channel], encoder->guts->real_signal_mid_side[channel], encoder->guts->subframe_workspace_ptr_mid_side[channel], encoder->guts->residual_workspace_mid_side[channel], encoder->guts->best_subframe_mid_side+channel, encoder->guts->best_subframe_bits_mid_side+channel))
778 * Compose the frame bitbuffer
781 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
782 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
783 FLAC__ChannelAssignment channel_assignment;
785 FLAC__ASSERT(encoder->channels == 2);
787 if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
788 channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
791 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
793 FLAC__ChannelAssignment ca;
795 FLAC__ASSERT(do_independent && do_mid_side);
797 /* We have to figure out which channel assignent results in the smallest frame */
798 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1];
799 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1];
800 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1];
801 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
803 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
804 if(bits[ca] < min_bits) {
806 channel_assignment = ca;
811 frame_header.channel_assignment = channel_assignment;
813 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
814 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
818 switch(channel_assignment) {
819 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
820 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
821 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
823 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
824 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
825 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
827 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
828 left_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
829 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
831 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
832 left_subframe = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]];
833 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
839 switch(channel_assignment) {
840 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
841 left_bps = encoder->guts->subframe_bps [0];
842 right_bps = encoder->guts->subframe_bps [1];
844 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
845 left_bps = encoder->guts->subframe_bps [0];
846 right_bps = encoder->guts->subframe_bps_mid_side[1];
848 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
849 left_bps = encoder->guts->subframe_bps_mid_side[1];
850 right_bps = encoder->guts->subframe_bps [1];
852 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
853 left_bps = encoder->guts->subframe_bps_mid_side[0];
854 right_bps = encoder->guts->subframe_bps_mid_side[1];
860 /* note that encoder_add_subframe_ sets the state for us in case of an error */
861 if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame))
863 if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame))
867 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
868 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
872 for(channel = 0; channel < encoder->channels; channel++) {
873 if(!encoder_add_subframe_(encoder, &frame_header, encoder->guts->subframe_bps[channel], &encoder->guts->subframe_workspace[channel][encoder->guts->best_subframe[channel]], &encoder->guts->frame)) {
874 /* the above function sets the state for us in case of an error */
880 if(encoder->loose_mid_side_stereo) {
881 encoder->guts->loose_mid_side_stereo_frame_count++;
882 if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
883 encoder->guts->loose_mid_side_stereo_frame_count = 0;
886 encoder->guts->last_channel_assignment = frame_header.channel_assignment;
891 bool encoder_process_subframe_(FLAC__Encoder *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)
893 real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
894 real lpc_residual_bits_per_sample;
895 real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->max_lpc_order might be less; some asm routines need all the space */
896 real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
897 real lpc_error[FLAC__MAX_LPC_ORDER];
898 unsigned min_lpc_order, max_lpc_order, lpc_order;
899 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
900 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
901 unsigned rice_parameter;
902 unsigned _candidate_bits, _best_bits;
903 unsigned _best_subframe;
905 /* verbatim subframe is the baseline against which we measure other compressed subframes */
907 _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
909 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
910 /* check for constant subframe */
911 if(encoder->guts->use_slow)
912 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);
914 guess_fixed_order = encoder->guts->local_fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
915 if(fixed_residual_bits_per_sample[1] == 0.0) {
916 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
917 unsigned i, signal_is_constant = true;
918 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
919 if(integer_signal[0] != integer_signal[i]) {
920 signal_is_constant = false;
924 if(signal_is_constant) {
925 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
926 if(_candidate_bits < _best_bits) {
927 _best_subframe = !_best_subframe;
928 _best_bits = _candidate_bits;
934 if(encoder->do_exhaustive_model_search) {
936 max_fixed_order = FLAC__MAX_FIXED_ORDER;
939 min_fixed_order = max_fixed_order = guess_fixed_order;
941 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
942 if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps)
943 continue; /* don't even try */
944 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 */
945 #ifndef FLAC__SYMMETRIC_RICE
946 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
948 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
949 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
950 _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, encoder->rice_parameter_search_dist, subframe[!_best_subframe]);
951 if(_candidate_bits < _best_bits) {
952 _best_subframe = !_best_subframe;
953 _best_bits = _candidate_bits;
958 if(encoder->max_lpc_order > 0) {
959 if(encoder->max_lpc_order >= frame_header->blocksize)
960 max_lpc_order = frame_header->blocksize-1;
962 max_lpc_order = encoder->max_lpc_order;
963 if(max_lpc_order > 0) {
964 encoder->guts->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
965 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
966 if(autoc[0] != 0.0) {
967 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
968 if(encoder->do_exhaustive_model_search) {
972 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
973 min_lpc_order = max_lpc_order = guess_lpc_order;
975 if(encoder->do_qlp_coeff_prec_search) {
976 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
977 max_qlp_coeff_precision = min(32 - subframe_bps - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1);
980 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
982 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
983 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
984 if(lpc_residual_bits_per_sample >= (real)subframe_bps)
985 continue; /* don't even try */
986 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
987 #ifndef FLAC__SYMMETRIC_RICE
988 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
990 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
991 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
992 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
993 _candidate_bits = encoder_evaluate_lpc_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->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->rice_parameter_search_dist, subframe[!_best_subframe]);
994 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
995 if(_candidate_bits < _best_bits) {
996 _best_subframe = !_best_subframe;
997 _best_bits = _candidate_bits;
1008 *best_subframe = _best_subframe;
1009 *best_bits = _best_bits;
1014 bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
1016 switch(subframe->type) {
1017 case FLAC__SUBFRAME_TYPE_CONSTANT:
1018 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
1019 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1023 case FLAC__SUBFRAME_TYPE_FIXED:
1024 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
1025 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1029 case FLAC__SUBFRAME_TYPE_LPC:
1030 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
1031 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1035 case FLAC__SUBFRAME_TYPE_VERBATIM:
1036 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
1037 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1048 unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
1050 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1051 subframe->data.constant.value = signal;
1053 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
1056 unsigned 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)
1058 unsigned i, residual_bits;
1059 const unsigned residual_samples = blocksize - order;
1061 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1063 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1065 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1066 subframe->data.fixed.residual = residual;
1068 residual_bits = 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);
1070 subframe->data.fixed.order = order;
1071 for(i = 0; i < order; i++)
1072 subframe->data.fixed.warmup[i] = signal[i];
1074 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
1077 unsigned encoder_evaluate_lpc_subframe_(FLAC__Encoder *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)
1079 int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1080 unsigned i, residual_bits;
1081 int quantization, ret;
1082 const unsigned residual_samples = blocksize - order;
1084 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
1086 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1088 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
1089 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1091 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1093 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1095 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1096 subframe->data.lpc.residual = residual;
1098 residual_bits = 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);
1100 subframe->data.lpc.order = order;
1101 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1102 subframe->data.lpc.quantization_level = quantization;
1103 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1104 for(i = 0; i < order; i++)
1105 subframe->data.lpc.warmup[i] = signal[i];
1107 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;
1110 unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
1112 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1114 subframe->data.verbatim.data = signal;
1116 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
1119 unsigned 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[])
1122 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1124 int partition_order;
1126 unsigned partition_order;
1128 unsigned residual_bits, best_residual_bits = 0;
1129 unsigned residual_sample;
1130 unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
1132 /* compute abs(residual) for use later */
1133 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1134 r = residual[residual_sample];
1135 abs_residual[residual_sample] = (uint32)(r<0? -r : r);
1138 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1139 max_partition_order = 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);
1140 min_partition_order = min(min_partition_order, max_partition_order);
1142 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1143 if(!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)) {
1144 FLAC__ASSERT(0); /* encoder_precompute_partition_info_ should keep this from ever happening */
1146 sum += 1u << partition_order;
1147 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1148 best_residual_bits = residual_bits;
1149 *best_partition_order = partition_order;
1150 best_parameters_index = !best_parameters_index;
1154 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1155 if(!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)) {
1156 FLAC__ASSERT(best_residual_bits != 0);
1159 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1160 best_residual_bits = residual_bits;
1161 *best_partition_order = partition_order;
1162 best_parameters_index = !best_parameters_index;
1166 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1167 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1169 return best_residual_bits;
1172 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1173 unsigned 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)
1175 int partition_order;
1176 unsigned from_partition, to_partition = 0;
1177 const unsigned blocksize = residual_samples + predictor_order;
1179 /* first do max_partition_order */
1180 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1181 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1182 uint32 abs_residual_partition_sum;
1184 #ifdef FLAC__SEARCH_FOR_ESCAPES
1185 uint32 abs_residual_partition_max;
1186 unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */
1189 unsigned partition, partition_sample, partition_samples, residual_sample;
1190 const unsigned partitions = 1u << partition_order;
1191 const unsigned default_partition_samples = blocksize >> partition_order;
1193 if(default_partition_samples <= predictor_order) {
1194 FLAC__ASSERT(max_partition_order > 0);
1195 max_partition_order--;
1198 for(partition = residual_sample = 0; partition < partitions; partition++) {
1199 partition_samples = default_partition_samples;
1201 partition_samples -= predictor_order;
1202 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1203 abs_residual_partition_sum = 0;
1205 #ifdef FLAC__SEARCH_FOR_ESCAPES
1206 abs_residual_partition_max = 0;
1208 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1209 abs_r = abs_residual[residual_sample];
1210 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1211 abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */
1213 #ifdef FLAC__SEARCH_FOR_ESCAPES
1214 if(abs_r > abs_residual_partition_max) {
1215 abs_residual_partition_max = abs_r;
1216 abs_residual_partition_max_index = residual_sample;
1221 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1222 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
1224 #ifdef FLAC__SEARCH_FOR_ESCAPES
1225 if(abs_residual_partition_max > 0)
1226 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]);
1228 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0);
1231 to_partition = partitions;
1236 /* now merge for lower orders */
1237 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1238 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1241 #ifdef FLAC__SEARCH_FOR_ESCAPES
1245 const unsigned partitions = 1u << partition_order;
1246 for(i = 0; i < partitions; i++) {
1247 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1248 s = abs_residual_partition_sums[from_partition];
1250 #ifdef FLAC__SEARCH_FOR_ESCAPES
1251 m = raw_bits_per_partition[from_partition];
1254 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1255 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1257 #ifdef FLAC__SEARCH_FOR_ESCAPES
1258 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1265 return max_partition_order;
1269 #ifdef VARIABLE_RICE_BITS
1270 #undef VARIABLE_RICE_BITS
1272 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1274 bool 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)
1276 unsigned rice_parameter, partition_bits;
1277 #ifndef NO_RICE_SEARCH
1278 unsigned best_partition_bits;
1279 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1281 #ifdef FLAC__SEARCH_FOR_ESCAPES
1284 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1286 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1288 if(partition_order == 0) {
1291 #ifndef NO_RICE_SEARCH
1292 if(rice_parameter_search_dist) {
1293 if(suggested_rice_parameter < rice_parameter_search_dist)
1294 min_rice_parameter = 0;
1296 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1297 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1298 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1299 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1302 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1304 best_partition_bits = 0xffffffff;
1305 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1307 #ifdef VARIABLE_RICE_BITS
1308 #ifdef FLAC__SYMMETRIC_RICE
1309 partition_bits = (2+rice_parameter) * residual_samples;
1311 const unsigned rice_parameter_estimate = rice_parameter-1;
1312 partition_bits = (1+rice_parameter) * residual_samples;
1317 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1318 for(i = 0; i < residual_samples; i++) {
1319 #ifdef VARIABLE_RICE_BITS
1320 #ifdef FLAC__SYMMETRIC_RICE
1321 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1323 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1326 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1329 #ifndef NO_RICE_SEARCH
1330 if(partition_bits < best_partition_bits) {
1331 best_rice_parameter = rice_parameter;
1332 best_partition_bits = partition_bits;
1336 #ifdef FLAC__SEARCH_FOR_ESCAPES
1337 flat_bits = raw_bits_per_partition[0] * residual_samples;
1338 if(flat_bits <= best_partition_bits) {
1339 raw_bits[0] = raw_bits_per_partition[0];
1340 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1341 best_partition_bits = flat_bits;
1344 parameters[0] = best_rice_parameter;
1345 bits_ += best_partition_bits;
1348 unsigned partition, residual_sample, save_residual_sample, partition_sample;
1349 unsigned mean, partition_samples;
1350 const unsigned partitions = 1u << partition_order;
1351 for(partition = residual_sample = 0; partition < partitions; partition++) {
1352 partition_samples = (residual_samples+predictor_order) >> partition_order;
1353 if(partition == 0) {
1354 if(partition_samples <= predictor_order)
1357 partition_samples -= predictor_order;
1359 mean = partition_samples >> 1;
1360 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1361 mean += abs_residual_partition_sums[partition];
1363 save_residual_sample = residual_sample;
1364 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
1365 mean += abs_residual[residual_sample];
1366 residual_sample = save_residual_sample;
1368 mean /= partition_samples;
1369 #ifdef FLAC__SYMMETRIC_RICE
1370 /* calc rice_parameter = floor(log2(mean)) */
1378 /* calc rice_parameter = floor(log2(mean)) + 1 */
1385 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1386 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1388 #ifndef NO_RICE_SEARCH
1389 if(rice_parameter_search_dist) {
1390 if(rice_parameter < rice_parameter_search_dist)
1391 min_rice_parameter = 0;
1393 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1394 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
1395 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1396 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1399 min_rice_parameter = max_rice_parameter = rice_parameter;
1401 best_partition_bits = 0xffffffff;
1402 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1404 #ifdef VARIABLE_RICE_BITS
1405 #ifdef FLAC__SYMMETRIC_RICE
1406 partition_bits = (2+rice_parameter) * partition_samples;
1408 const unsigned rice_parameter_estimate = rice_parameter-1;
1409 partition_bits = (1+rice_parameter) * partition_samples;
1414 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1415 save_residual_sample = residual_sample;
1416 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
1417 #ifdef VARIABLE_RICE_BITS
1418 #ifdef FLAC__SYMMETRIC_RICE
1419 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
1421 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
1424 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1427 #ifndef NO_RICE_SEARCH
1428 if(rice_parameter != max_rice_parameter)
1429 residual_sample = save_residual_sample;
1430 if(partition_bits < best_partition_bits) {
1431 best_rice_parameter = rice_parameter;
1432 best_partition_bits = partition_bits;
1436 #ifdef FLAC__SEARCH_FOR_ESCAPES
1437 flat_bits = raw_bits_per_partition[partition] * partition_samples;
1438 if(flat_bits <= best_partition_bits) {
1439 raw_bits[partition] = raw_bits_per_partition[partition];
1440 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1441 best_partition_bits = flat_bits;
1444 parameters[partition] = best_rice_parameter;
1445 bits_ += best_partition_bits;
1453 unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples)
1458 for(i = 0; i < samples && !(x&1); i++)
1465 for(shift = 0; !(x&1); shift++)
1470 for(i = 0; i < samples; i++)
1471 signal[i] >>= shift;