8987380bb9c28324a998df4f033f52be6a9f0229
[flac.git] / src / libFLAC / encoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h> /* for malloc() */
23 #include <string.h> /* for memcpy() */
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"
35
36 #ifdef min
37 #undef min
38 #endif
39 #define min(x,y) ((x)<(y)?(x):(y))
40
41 #ifdef max
42 #undef max
43 #endif
44 #define max(x,y) ((x)>(y)?(x):(y))
45
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         bool current_frame_can_do_mid_side;         /* encoder sets this false when any given sample of a frame's side channel exceeds 16 bits */
69         double loose_mid_side_stereo_frames_exact;  /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
70         unsigned loose_mid_side_stereo_frames;      /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
71         unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
72         FLAC__ChannelAssignment last_channel_assignment;
73         FLAC__StreamMetaData metadata;
74         unsigned current_sample_number;
75         unsigned current_frame_number;
76         struct MD5Context md5context;
77         FLAC__CPUInfo cpuinfo;
78         unsigned (*local_fixed_compute_best_predictor)(const int32 data[], unsigned data_len, real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
79         void (*local_lpc_compute_autocorrelation)(const real data[], unsigned data_len, unsigned lag, real autoc[]);
80         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[]);
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);
84         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;
96
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);
109 #endif
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);
112
113 const char *FLAC__EncoderWriteStatusString[] = {
114         "FLAC__ENCODER_WRITE_OK",
115         "FLAC__ENCODER_WRITE_FATAL_ERROR"
116 };
117
118 const char *FLAC__EncoderStateString[] = {
119         "FLAC__ENCODER_OK",
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"
135 };
136
137
138 bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
139 {
140         bool ok;
141         unsigned i, channel;
142
143         assert(new_size > 0);
144         assert(encoder->state == FLAC__ENCODER_OK);
145         assert(encoder->guts->current_sample_number == 0);
146
147         /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
148         if(new_size <= encoder->guts->input_capacity)
149                 return true;
150
151         ok = true;
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]);
155         }
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]);
159         }
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]);
163                 }
164         }
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]);
168                 }
169         }
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);
173 #endif
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);
176 #endif
177
178         if(ok)
179                 encoder->guts->input_capacity = new_size;
180         else
181                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
182
183         return ok;
184 }
185
186 FLAC__Encoder *FLAC__encoder_get_new_instance()
187 {
188         FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
189         if(encoder != 0) {
190                 encoder->state = FLAC__ENCODER_UNINITIALIZED;
191                 encoder->guts = 0;
192         }
193         return encoder;
194 }
195
196 void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
197 {
198         assert(encoder != 0);
199         free(encoder);
200 }
201
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)
203 {
204         unsigned i;
205         FLAC__StreamMetaData padding;
206         FLAC__StreamMetaData seek_table;
207
208         assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
209         assert(encoder != 0);
210         assert(write_callback != 0);
211         assert(metadata_callback != 0);
212         assert(encoder->state == FLAC__ENCODER_UNINITIALIZED);
213         assert(encoder->guts == 0);
214
215         encoder->state = FLAC__ENCODER_OK;
216
217         if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
218                 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
219
220         if(encoder->do_mid_side_stereo && encoder->channels != 2)
221                 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
222
223         if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
224                 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
225
226         if(encoder->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
227                 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
228
229         if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
230                 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
231
232         if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
233                 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
234
235         if(encoder->blocksize < encoder->max_lpc_order)
236                 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
237
238         if(encoder->qlp_coeff_precision == 0) {
239                 if(encoder->bits_per_sample < 16) {
240                         /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
241                         /* @@@ until then we'll make a guess */
242                         encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
243                 }
244                 else if(encoder->bits_per_sample == 16) {
245                         if(encoder->blocksize <= 192)
246                                 encoder->qlp_coeff_precision = 7;
247                         else if(encoder->blocksize <= 384)
248                                 encoder->qlp_coeff_precision = 8;
249                         else if(encoder->blocksize <= 576)
250                                 encoder->qlp_coeff_precision = 9;
251                         else if(encoder->blocksize <= 1152)
252                                 encoder->qlp_coeff_precision = 10;
253                         else if(encoder->blocksize <= 2304)
254                                 encoder->qlp_coeff_precision = 11;
255                         else if(encoder->blocksize <= 4608)
256                                 encoder->qlp_coeff_precision = 12;
257                         else
258                                 encoder->qlp_coeff_precision = 13;
259                 }
260                 else {
261                         encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
262                 }
263         }
264         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))
265                 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
266
267         if(encoder->streamable_subset) {
268                 //@@@ add check for blocksize here
269                 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)
270                         return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
271                 if(encoder->sample_rate > 655350)
272                         return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
273         }
274
275         if(encoder->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
276                 encoder->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
277         if(encoder->min_residual_partition_order >= encoder->max_residual_partition_order)
278                 encoder->min_residual_partition_order = encoder->max_residual_partition_order;
279
280         encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
281         if(encoder->guts == 0)
282                 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
283
284         encoder->guts->input_capacity = 0;
285         for(i = 0; i < encoder->channels; i++) {
286                 encoder->guts->integer_signal_unaligned[i] = encoder->guts->integer_signal[i] = 0;
287                 encoder->guts->real_signal_unaligned[i] = encoder->guts->real_signal[i] = 0;
288         }
289         for(i = 0; i < 2; i++) {
290                 encoder->guts->integer_signal_mid_side_unaligned[i] = encoder->guts->integer_signal_mid_side[i] = 0;
291                 encoder->guts->real_signal_mid_side_unaligned[i] = encoder->guts->real_signal_mid_side[i] = 0;
292         }
293         for(i = 0; i < encoder->channels; i++) {
294                 encoder->guts->residual_workspace_unaligned[i][0] = encoder->guts->residual_workspace[i][0] = 0;
295                 encoder->guts->residual_workspace_unaligned[i][1] = encoder->guts->residual_workspace[i][1] = 0;
296                 encoder->guts->best_subframe[i] = 0;
297         }
298         for(i = 0; i < 2; i++) {
299                 encoder->guts->residual_workspace_mid_side_unaligned[i][0] = encoder->guts->residual_workspace_mid_side[i][0] = 0;
300                 encoder->guts->residual_workspace_mid_side_unaligned[i][1] = encoder->guts->residual_workspace_mid_side[i][1] = 0;
301                 encoder->guts->best_subframe_mid_side[i] = 0;
302         }
303         for(i = 0; i < encoder->channels; i++) {
304                 encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
305                 encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
306         }
307         for(i = 0; i < 2; i++) {
308                 encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
309                 encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
310         }
311         encoder->guts->abs_residual_unaligned = encoder->guts->abs_residual = 0;
312         encoder->guts->abs_residual_partition_sums_unaligned = encoder->guts->abs_residual_partition_sums = 0;
313         encoder->guts->raw_bits_per_partition_unaligned = encoder->guts->raw_bits_per_partition = 0;
314         encoder->guts->current_frame_can_do_mid_side = true;
315         encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
316         encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
317         if(encoder->guts->loose_mid_side_stereo_frames == 0)
318                 encoder->guts->loose_mid_side_stereo_frames = 1;
319         encoder->guts->loose_mid_side_stereo_frame_count = 0;
320         encoder->guts->current_sample_number = 0;
321         encoder->guts->current_frame_number = 0;
322
323         /*
324          * get the CPU info and set the function pointers
325          */
326         FLAC__cpu_info(&encoder->guts->cpuinfo);
327         /* first default to the non-asm routines */
328         encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
329         encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
330         encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
331         /* now override with asm where appropriate */
332 #ifndef FLAC__NO_ASM
333         assert(encoder->guts->cpuinfo.use_asm);
334 #ifdef FLAC__CPU_IA32
335         assert(encoder->guts->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
336 #ifdef FLAC__HAS_NASM
337 #if 0
338         /* @@@ SSE version not working yet */
339         if(encoder->guts->cpuinfo.data.ia32.sse)
340                 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse;
341         else
342 #endif
343 fprintf(stderr,"@@@ got _asm_i386 of lpc_compute_autocorrelation()\n");
344                 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
345         if(encoder->guts->cpuinfo.data.ia32.mmx && encoder->guts->cpuinfo.data.ia32.cmov)
346 {
347                 encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_i386_mmx_cmov;
348 fprintf(stderr,"@@@ got _asm_i386_mmx_cmov of fixed_compute_best_predictor()\n");}
349 #if 0
350         /* @@@ MMX version needs bps check */
351         if(decoder->guts->cpuinfo.data.ia32.mmx && @@@bps check here@@@)
352                 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386_mmx;
353         else
354 #endif
355 {
356         encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
357 fprintf(stderr,"@@@ got _asm_i386 of lpc_compute_residual_from_qlp_coefficients()\n");}
358 #endif
359 #endif
360 #endif
361
362         if(encoder->bits_per_sample + FLAC__bitmath_ilog2(encoder->blocksize)+1 > 30)
363                 encoder->guts->use_slow = true;
364         else
365                 encoder->guts->use_slow = false;
366
367         if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
368                 /* the above function sets the state for us in case of an error */
369                 return encoder->state;
370         }
371         FLAC__bitbuffer_init(&encoder->guts->frame);
372         encoder->guts->write_callback = write_callback;
373         encoder->guts->metadata_callback = metadata_callback;
374         encoder->guts->client_data = client_data;
375
376         /*
377          * write the stream header
378          */
379         if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
380                 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
381
382         if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
383                 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
384
385         encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
386         encoder->guts->metadata.is_last = (encoder->seek_table == 0 && encoder->padding == 0);
387         encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
388         encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
389         encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
390         encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
391         encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
392         encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
393         encoder->guts->metadata.data.stream_info.channels = encoder->channels;
394         encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
395         encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
396         memset(encoder->guts->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
397         MD5Init(&encoder->guts->md5context);
398         if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
399                 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
400
401         if(0 != encoder->seek_table) {
402                 if(!FLAC__seek_table_is_valid(encoder->seek_table))
403                         return encoder->state = FLAC__ENCODER_INVALID_SEEK_TABLE;
404                 seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
405                 seek_table.is_last = (encoder->padding == 0);
406                 seek_table.length = encoder->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
407                 seek_table.data.seek_table = *encoder->seek_table;
408                 if(!FLAC__add_metadata_block(&seek_table, &encoder->guts->frame))
409                         return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
410         }
411
412         /* add a PADDING block if requested */
413         if(encoder->padding > 0) {
414                 padding.type = FLAC__METADATA_TYPE_PADDING;
415                 padding.is_last = true;
416                 padding.length = encoder->padding;
417                 if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
418                         return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
419         }
420
421         assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
422         assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
423         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)
424                 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
425
426         /* now that the metadata block is written, we can init this to an absurdly-high value... */
427         encoder->guts->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
428         /* ... and clear this to 0 */
429         encoder->guts->metadata.data.stream_info.total_samples = 0;
430
431         return encoder->state;
432 }
433
434 void FLAC__encoder_finish(FLAC__Encoder *encoder)
435 {
436         unsigned i, channel;
437
438         assert(encoder != 0);
439         if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
440                 return;
441         if(encoder->guts->current_sample_number != 0) {
442                 encoder->blocksize = encoder->guts->current_sample_number;
443                 encoder_process_frame_(encoder, true); /* true => is last frame */
444         }
445         MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
446         encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
447         if(encoder->guts != 0) {
448                 for(i = 0; i < encoder->channels; i++) {
449                         if(encoder->guts->integer_signal_unaligned[i] != 0) {
450                                 free(encoder->guts->integer_signal_unaligned[i]);
451                                 encoder->guts->integer_signal_unaligned[i] = 0;
452                         }
453                         if(encoder->guts->real_signal_unaligned[i] != 0) {
454                                 free(encoder->guts->real_signal_unaligned[i]);
455                                 encoder->guts->real_signal_unaligned[i] = 0;
456                         }
457                 }
458                 for(i = 0; i < 2; i++) {
459                         if(encoder->guts->integer_signal_mid_side_unaligned[i] != 0) {
460                                 free(encoder->guts->integer_signal_mid_side_unaligned[i]);
461                                 encoder->guts->integer_signal_mid_side_unaligned[i] = 0;
462                         }
463                         if(encoder->guts->real_signal_mid_side_unaligned[i] != 0) {
464                                 free(encoder->guts->real_signal_mid_side_unaligned[i]);
465                                 encoder->guts->real_signal_mid_side_unaligned[i] = 0;
466                         }
467                 }
468                 for(channel = 0; channel < encoder->channels; channel++) {
469                         for(i = 0; i < 2; i++) {
470                                 if(encoder->guts->residual_workspace_unaligned[channel][i] != 0) {
471                                         free(encoder->guts->residual_workspace_unaligned[channel][i]);
472                                         encoder->guts->residual_workspace_unaligned[channel][i] = 0;
473                                 }
474                         }
475                 }
476                 for(channel = 0; channel < 2; channel++) {
477                         for(i = 0; i < 2; i++) {
478                                 if(encoder->guts->residual_workspace_mid_side_unaligned[channel][i] != 0) {
479                                         free(encoder->guts->residual_workspace_mid_side_unaligned[channel][i]);
480                                         encoder->guts->residual_workspace_mid_side_unaligned[channel][i] = 0;
481                                 }
482                         }
483                 }
484                 if(encoder->guts->abs_residual_unaligned != 0) {
485                         free(encoder->guts->abs_residual_unaligned);
486                         encoder->guts->abs_residual_unaligned = 0;
487                 }
488                 if(encoder->guts->abs_residual_partition_sums_unaligned != 0) {
489                         free(encoder->guts->abs_residual_partition_sums_unaligned);
490                         encoder->guts->abs_residual_partition_sums_unaligned = 0;
491                 }
492                 if(encoder->guts->raw_bits_per_partition_unaligned != 0) {
493                         free(encoder->guts->raw_bits_per_partition_unaligned);
494                         encoder->guts->raw_bits_per_partition_unaligned = 0;
495                 }
496                 FLAC__bitbuffer_free(&encoder->guts->frame);
497                 free(encoder->guts);
498                 encoder->guts = 0;
499         }
500         encoder->state = FLAC__ENCODER_UNINITIALIZED;
501 }
502
503 bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
504 {
505         unsigned i, j, channel;
506         int32 x, mid, side;
507         const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
508         const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
509         const int32 max_side =  ((int64)1 << (encoder->bits_per_sample-1)) - 1;
510
511         assert(encoder != 0);
512         assert(encoder->state == FLAC__ENCODER_OK);
513
514         j = 0;
515         do {
516                 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) {
517                         for(channel = 0; channel < encoder->channels; channel++) {
518                                 x = buf[channel][j];
519                                 encoder->guts->integer_signal[channel][i] = x;
520                                 encoder->guts->real_signal[channel][i] = (real)x;
521                         }
522                         if(ms && encoder->guts->current_frame_can_do_mid_side) {
523                                 side = buf[0][j] - buf[1][j];
524                                 if(side < min_side || side > max_side) {
525                                         encoder->guts->current_frame_can_do_mid_side = false;
526                                 }
527                                 else {
528                                         mid = (buf[0][j] + buf[1][j]) >> 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
529                                         encoder->guts->integer_signal_mid_side[0][i] = mid;
530                                         encoder->guts->integer_signal_mid_side[1][i] = side;
531                                         encoder->guts->real_signal_mid_side[0][i] = (real)mid;
532                                         encoder->guts->real_signal_mid_side[1][i] = (real)side;
533                                 }
534                         }
535                         encoder->guts->current_sample_number++;
536                 }
537                 if(i == encoder->blocksize) {
538                         if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
539                                 return false;
540                 }
541         } while(j < samples);
542
543         return true;
544 }
545
546 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
547 bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
548 {
549         unsigned i, j, k, channel;
550         int32 x, left = 0, mid, side;
551         const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
552         const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
553         const int32 max_side =  ((int64)1 << (encoder->bits_per_sample-1)) - 1;
554
555         assert(encoder != 0);
556         assert(encoder->state == FLAC__ENCODER_OK);
557
558         j = k = 0;
559         do {
560                 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) {
561                         for(channel = 0; channel < encoder->channels; channel++, k++) {
562                                 x = buf[k];
563                                 encoder->guts->integer_signal[channel][i] = x;
564                                 encoder->guts->real_signal[channel][i] = (real)x;
565                                 if(ms && encoder->guts->current_frame_can_do_mid_side) {
566                                         if(channel == 0) {
567                                                 left = x;
568                                         }
569                                         else {
570                                                 side = left - x;
571                                                 if(side < min_side || side > max_side) {
572                                                         encoder->guts->current_frame_can_do_mid_side = false;
573                                                 }
574                                                 else {
575                                                         mid = (left + x) >> 1; /* NOTE: not the same as 'mid = (left + x) / 2' ! */
576                                                         encoder->guts->integer_signal_mid_side[0][i] = mid;
577                                                         encoder->guts->integer_signal_mid_side[1][i] = side;
578                                                         encoder->guts->real_signal_mid_side[0][i] = (real)mid;
579                                                         encoder->guts->real_signal_mid_side[1][i] = (real)side;
580                                                 }
581                                         }
582                                 }
583                         }
584                         encoder->guts->current_sample_number++;
585                 }
586                 if(i == encoder->blocksize) {
587                         if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
588                                 return false;
589                 }
590         } while(j < samples);
591
592         return true;
593 }
594
595 bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
596 {
597         assert(encoder->state == FLAC__ENCODER_OK);
598
599         /*
600          * Accumulate raw signal to the MD5 signature
601          */
602         /* 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: */
603         if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
604                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
605                 return false;
606         }
607
608         /*
609          * Process the frame header and subframes into the frame bitbuffer
610          */
611         if(!encoder_process_subframes_(encoder, is_last_frame)) {
612                 /* the above function sets the state for us in case of an error */
613                 return false;
614         }
615
616         /*
617          * Zero-pad the frame to a byte_boundary
618          */
619         if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
620                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
621                 return false;
622         }
623
624         /*
625          * CRC-16 the whole thing
626          */
627         assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */
628         assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
629         FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
630
631         /*
632          * Write it
633          */
634         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) {
635                 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
636                 return false;
637         }
638
639         /*
640          * Get ready for the next frame
641          */
642         encoder->guts->current_frame_can_do_mid_side = true;
643         encoder->guts->current_sample_number = 0;
644         encoder->guts->current_frame_number++;
645         encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
646         encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
647         encoder->guts->metadata.data.stream_info.max_framesize = max(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.max_framesize);
648
649         return true;
650 }
651
652 bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
653 {
654         FLAC__FrameHeader frame_header;
655         unsigned channel, min_partition_order = encoder->min_residual_partition_order, max_partition_order;
656         bool do_independent, do_mid_side;
657
658         /*
659          * Calculate the min,max Rice partition orders
660          */
661         if(is_last_frame) {
662                 max_partition_order = 0;
663         }
664         else {
665                 unsigned limit = 0, b = encoder->blocksize;
666                 while(!(b & 1)) {
667                         limit++;
668                         b >>= 1;
669                 }
670                 max_partition_order = min(encoder->max_residual_partition_order, limit);
671         }
672         min_partition_order = min(min_partition_order, max_partition_order);
673
674         /*
675          * Setup the frame
676          */
677         if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
678                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
679                 return false;
680         }
681         frame_header.blocksize = encoder->blocksize;
682         frame_header.sample_rate = encoder->sample_rate;
683         frame_header.channels = encoder->channels;
684         frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
685         frame_header.bits_per_sample = encoder->bits_per_sample;
686         frame_header.number.frame_number = encoder->guts->current_frame_number;
687
688         /*
689          * Figure out what channel assignments to try
690          */
691         if(encoder->do_mid_side_stereo) {
692                 if(encoder->loose_mid_side_stereo) {
693                         if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
694                                 do_independent = true;
695                                 do_mid_side = true;
696                         }
697                         else {
698                                 do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
699                                 do_mid_side = !do_independent;
700                         }
701                 }
702                 else {
703                         do_independent = true;
704                         do_mid_side = true;
705                 }
706         }
707         else {
708                 do_independent = true;
709                 do_mid_side = false;
710         }
711         if(do_mid_side && !encoder->guts->current_frame_can_do_mid_side) {
712                 do_independent = true;
713                 do_mid_side = false;
714         }
715
716         assert(do_independent || do_mid_side);
717
718         /*
719          * Check for wasted bits; set effective bps for each subframe
720          */
721         if(do_independent) {
722                 unsigned w;
723                 for(channel = 0; channel < encoder->channels; channel++) {
724                         w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize);
725                         encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w;
726                         encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w;
727                 }
728         }
729         if(do_mid_side) {
730                 unsigned w;
731                 assert(encoder->channels == 2);
732                 for(channel = 0; channel < 2; channel++) {
733                         w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize);
734                         encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w;
735                         encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1);
736                 }
737         }
738
739         /*
740          * First do a normal encoding pass of each independent channel
741          */
742         if(do_independent) {
743                 for(channel = 0; channel < encoder->channels; channel++) {
744                         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))
745                                 return false;
746                 }
747         }
748
749         /*
750          * Now do mid and side channels if requested
751          */
752         if(do_mid_side) {
753                 assert(encoder->channels == 2);
754
755                 for(channel = 0; channel < 2; channel++) {
756                         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))
757                                 return false;
758                 }
759         }
760
761         /*
762          * Compose the frame bitbuffer
763          */
764         if(do_mid_side) {
765                 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
766                 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
767                 FLAC__ChannelAssignment channel_assignment;
768
769                 assert(encoder->channels == 2);
770
771                 if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
772                         channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
773                 }
774                 else {
775                         unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
776                         unsigned min_bits;
777                         FLAC__ChannelAssignment ca;
778
779                         assert(do_independent && do_mid_side);
780
781                         /* We have to figure out which channel assignent results in the smallest frame */
782                         bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits         [0] + encoder->guts->best_subframe_bits         [1];
783                         bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->guts->best_subframe_bits         [0] + encoder->guts->best_subframe_bits_mid_side[1];
784                         bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits         [1] + encoder->guts->best_subframe_bits_mid_side[1];
785                         bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
786
787                         for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
788                                 if(bits[ca] < min_bits) {
789                                         min_bits = bits[ca];
790                                         channel_assignment = ca;
791                                 }
792                         }
793                 }
794
795                 frame_header.channel_assignment = channel_assignment;
796
797                 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
798                         encoder->state = FLAC__ENCODER_FRAMING_ERROR;
799                         return false;
800                 }
801
802                 switch(channel_assignment) {
803                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
804                                 left_subframe  = &encoder->guts->subframe_workspace         [0][encoder->guts->best_subframe         [0]];
805                                 right_subframe = &encoder->guts->subframe_workspace         [1][encoder->guts->best_subframe         [1]];
806                                 break;
807                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
808                                 left_subframe  = &encoder->guts->subframe_workspace         [0][encoder->guts->best_subframe         [0]];
809                                 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
810                                 break;
811                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
812                                 left_subframe  = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
813                                 right_subframe = &encoder->guts->subframe_workspace         [1][encoder->guts->best_subframe         [1]];
814                                 break;
815                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
816                                 left_subframe  = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]];
817                                 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
818                                 break;
819                         default:
820                                 assert(0);
821                 }
822
823                 switch(channel_assignment) {
824                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
825                                 left_bps  = encoder->guts->subframe_bps         [0];
826                                 right_bps = encoder->guts->subframe_bps         [1];
827                                 break;
828                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
829                                 left_bps  = encoder->guts->subframe_bps         [0];
830                                 right_bps = encoder->guts->subframe_bps_mid_side[1];
831                                 break;
832                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
833                                 left_bps  = encoder->guts->subframe_bps_mid_side[1];
834                                 right_bps = encoder->guts->subframe_bps         [1];
835                                 break;
836                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
837                                 left_bps  = encoder->guts->subframe_bps_mid_side[0];
838                                 right_bps = encoder->guts->subframe_bps_mid_side[1];
839                                 break;
840                         default:
841                                 assert(0);
842                 }
843
844                 /* note that encoder_add_subframe_ sets the state for us in case of an error */
845                 if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame))
846                         return false;
847                 if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame))
848                         return false;
849         }
850         else {
851                 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
852                         encoder->state = FLAC__ENCODER_FRAMING_ERROR;
853                         return false;
854                 }
855
856                 for(channel = 0; channel < encoder->channels; channel++) {
857                         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)) {
858                                 /* the above function sets the state for us in case of an error */
859                                 return false;
860                         }
861                 }
862         }
863
864         if(encoder->loose_mid_side_stereo) {
865                 encoder->guts->loose_mid_side_stereo_frame_count++;
866                 if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
867                         encoder->guts->loose_mid_side_stereo_frame_count = 0;
868         }
869
870         encoder->guts->last_channel_assignment = frame_header.channel_assignment;
871
872         return true;
873 }
874
875 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)
876 {
877         real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
878         real lpc_residual_bits_per_sample;
879         real autoc[FLAC__MAX_LPC_ORDER+1];
880         real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
881         real lpc_error[FLAC__MAX_LPC_ORDER];
882         unsigned min_lpc_order, max_lpc_order, lpc_order;
883         unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
884         unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
885         unsigned rice_parameter;
886         unsigned _candidate_bits, _best_bits;
887         unsigned _best_subframe;
888
889         /* verbatim subframe is the baseline against which we measure other compressed subframes */
890         _best_subframe = 0;
891         _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
892
893         if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
894                 /* check for constant subframe */
895                 if(encoder->guts->use_slow)
896                         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);
897                 else
898                         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);
899                 if(fixed_residual_bits_per_sample[1] == 0.0) {
900                         /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
901                         unsigned i, signal_is_constant = true;
902                         for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
903                                 if(integer_signal[0] != integer_signal[i]) {
904                                         signal_is_constant = false;
905                                         break;
906                                 }
907                         }
908                         if(signal_is_constant) {
909                                 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
910                                 if(_candidate_bits < _best_bits) {
911                                         _best_subframe = !_best_subframe;
912                                         _best_bits = _candidate_bits;
913                                 }
914                         }
915                 }
916                 else {
917                         /* encode fixed */
918                         if(encoder->do_exhaustive_model_search) {
919                                 min_fixed_order = 0;
920                                 max_fixed_order = FLAC__MAX_FIXED_ORDER;
921                         }
922                         else {
923                                 min_fixed_order = max_fixed_order = guess_fixed_order;
924                         }
925                         for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
926                                 if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps)
927                                         continue; /* don't even try */
928                                 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 */
929 #ifndef FLAC__SYMMETRIC_RICE
930                                 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
931 #endif
932                                 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
933                                         rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
934                                 _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]);
935                                 if(_candidate_bits < _best_bits) {
936                                         _best_subframe = !_best_subframe;
937                                         _best_bits = _candidate_bits;
938                                 }
939                         }
940
941                         /* encode lpc */
942                         if(encoder->max_lpc_order > 0) {
943                                 if(encoder->max_lpc_order >= frame_header->blocksize)
944                                         max_lpc_order = frame_header->blocksize-1;
945                                 else
946                                         max_lpc_order = encoder->max_lpc_order;
947                                 if(max_lpc_order > 0) {
948                                         encoder->guts->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
949                                         /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
950                                         if(autoc[0] != 0.0) {
951                                                 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
952                                                 if(encoder->do_exhaustive_model_search) {
953                                                         min_lpc_order = 1;
954                                                 }
955                                                 else {
956                                                         unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
957                                                         min_lpc_order = max_lpc_order = guess_lpc_order;
958                                                 }
959                                                 if(encoder->do_qlp_coeff_prec_search) {
960                                                         min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
961                                                         max_qlp_coeff_precision = min(32 - subframe_bps - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1);
962                                                 }
963                                                 else {
964                                                         min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
965                                                 }
966                                                 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
967                                                         lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
968                                                         if(lpc_residual_bits_per_sample >= (real)subframe_bps)
969                                                                 continue; /* don't even try */
970                                                         rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
971 #ifndef FLAC__SYMMETRIC_RICE
972                                                         rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
973 #endif
974                                                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
975                                                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
976                                                         for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
977                                                                 _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]);
978                                                                 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
979                                                                         if(_candidate_bits < _best_bits) {
980                                                                                 _best_subframe = !_best_subframe;
981                                                                                 _best_bits = _candidate_bits;
982                                                                         }
983                                                                 }
984                                                         }
985                                                 }
986                                         }
987                                 }
988                         }
989                 }
990         }
991
992         *best_subframe = _best_subframe;
993         *best_bits = _best_bits;
994
995         return true;
996 }
997
998 bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
999 {
1000         switch(subframe->type) {
1001                 case FLAC__SUBFRAME_TYPE_CONSTANT:
1002                         if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
1003                                 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1004                                 return false;
1005                         }
1006                         break;
1007                 case FLAC__SUBFRAME_TYPE_FIXED:
1008                         if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
1009                                 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1010                                 return false;
1011                         }
1012                         break;
1013                 case FLAC__SUBFRAME_TYPE_LPC:
1014                         if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
1015                                 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1016                                 return false;
1017                         }
1018                         break;
1019                 case FLAC__SUBFRAME_TYPE_VERBATIM:
1020                         if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
1021                                 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1022                                 return false;
1023                         }
1024                         break;
1025                 default:
1026                         assert(0);
1027         }
1028
1029         return true;
1030 }
1031
1032 unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
1033 {
1034         subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1035         subframe->data.constant.value = signal;
1036
1037         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
1038 }
1039
1040 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)
1041 {
1042         unsigned i, residual_bits;
1043         const unsigned residual_samples = blocksize - order;
1044
1045         FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1046
1047         subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1048
1049         subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1050         subframe->data.fixed.residual = residual;
1051
1052         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);
1053
1054         subframe->data.fixed.order = order;
1055         for(i = 0; i < order; i++)
1056                 subframe->data.fixed.warmup[i] = signal[i];
1057
1058         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
1059 }
1060
1061 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)
1062 {
1063         int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1064         unsigned i, residual_bits;
1065         int quantization, ret;
1066         const unsigned residual_samples = blocksize - order;
1067
1068         ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
1069         if(ret != 0)
1070                 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1071
1072         encoder->guts->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1073
1074         subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1075
1076         subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1077         subframe->data.lpc.residual = residual;
1078
1079         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);
1080
1081         subframe->data.lpc.order = order;
1082         subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1083         subframe->data.lpc.quantization_level = quantization;
1084         memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1085         for(i = 0; i < order; i++)
1086                 subframe->data.lpc.warmup[i] = signal[i];
1087
1088         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;
1089 }
1090
1091 unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
1092 {
1093         subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1094
1095         subframe->data.verbatim.data = signal;
1096
1097         return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
1098 }
1099
1100 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[])
1101 {
1102         int32 r;
1103 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1104         unsigned sum;
1105         int partition_order;
1106 #else
1107         unsigned partition_order;
1108 #endif
1109         unsigned residual_bits, best_residual_bits = 0;
1110         unsigned residual_sample;
1111         unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
1112
1113         /* compute abs(residual) for use later */
1114         for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1115                 r = residual[residual_sample];
1116                 abs_residual[residual_sample] = (uint32)(r<0? -r : r);
1117         }
1118
1119 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1120         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);
1121         min_partition_order = min(min_partition_order, max_partition_order);
1122
1123         for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1124                 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)) {
1125                         assert(0); /* encoder_precompute_partition_info_ should keep this from ever happening */
1126                 }
1127                 sum += 1u << partition_order;
1128                 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1129                         best_residual_bits = residual_bits;
1130                         *best_partition_order = partition_order;
1131                         best_parameters_index = !best_parameters_index;
1132                 }
1133         }
1134 #else
1135         for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1136                 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)) {
1137                         assert(best_residual_bits != 0);
1138                         break;
1139                 }
1140                 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1141                         best_residual_bits = residual_bits;
1142                         *best_partition_order = partition_order;
1143                         best_parameters_index = !best_parameters_index;
1144                 }
1145         }
1146 #endif
1147         memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1148         memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1149
1150         return best_residual_bits;
1151 }
1152
1153 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
1154 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)
1155 {
1156         int partition_order;
1157         unsigned from_partition, to_partition = 0;
1158         const unsigned blocksize = residual_samples + predictor_order;
1159
1160         /* first do max_partition_order */
1161         for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1162 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1163                 uint32 abs_residual_partition_sum;
1164 #endif
1165 #ifdef FLAC__SEARCH_FOR_ESCAPES
1166                 uint32 abs_residual_partition_max;
1167                 unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */
1168 #endif
1169                 uint32 abs_r;
1170                 unsigned partition, partition_sample, partition_samples, residual_sample;
1171                 const unsigned partitions = 1u << partition_order;
1172                 const unsigned default_partition_samples = blocksize >> partition_order;
1173
1174                 if(default_partition_samples <= predictor_order) {
1175                         assert(max_partition_order > 0);
1176                         max_partition_order--;
1177                 }
1178                 else {
1179                         for(partition = residual_sample = 0; partition < partitions; partition++) {
1180                                 partition_samples = default_partition_samples;
1181                                 if(partition == 0)
1182                                         partition_samples -= predictor_order;
1183 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1184                                 abs_residual_partition_sum = 0;
1185 #endif
1186 #ifdef FLAC__SEARCH_FOR_ESCAPES
1187                                 abs_residual_partition_max = 0;
1188 #endif
1189                                 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1190                                         abs_r = abs_residual[residual_sample];
1191 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1192                                         abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */
1193 #endif
1194 #ifdef FLAC__SEARCH_FOR_ESCAPES
1195                                         if(abs_r > abs_residual_partition_max) {
1196                                                 abs_residual_partition_max = abs_r;
1197                                                 abs_residual_partition_max_index = residual_sample;
1198                                         }
1199 #endif
1200                                         residual_sample++;
1201                                 }
1202 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1203                                 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
1204 #endif
1205 #ifdef FLAC__SEARCH_FOR_ESCAPES
1206                                 if(abs_residual_partition_max > 0)
1207                                         raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]);
1208                                 else
1209                                         raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0);
1210 #endif
1211                         }
1212                         to_partition = partitions;
1213                         break;
1214                 }
1215         }
1216
1217         /* now merge for lower orders */
1218         for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1219 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1220                 uint32 s;
1221 #endif
1222 #ifdef FLAC__SEARCH_FOR_ESCAPES
1223                 unsigned m;
1224 #endif
1225                 unsigned i;
1226                 const unsigned partitions = 1u << partition_order;
1227                 for(i = 0; i < partitions; i++) {
1228 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1229                         s = abs_residual_partition_sums[from_partition];
1230 #endif
1231 #ifdef FLAC__SEARCH_FOR_ESCAPES
1232                         m = raw_bits_per_partition[from_partition];
1233 #endif
1234                         from_partition++;
1235 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1236                         abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1237 #endif
1238 #ifdef FLAC__SEARCH_FOR_ESCAPES
1239                         raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1240 #endif
1241                         from_partition++;
1242                         to_partition++;
1243                 }
1244         }
1245
1246         return max_partition_order;
1247 }
1248 #endif
1249
1250 #ifdef VARIABLE_RICE_BITS
1251 #undef VARIABLE_RICE_BITS
1252 #endif
1253 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1254
1255 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)
1256 {
1257         unsigned rice_parameter, partition_bits;
1258 #ifndef NO_RICE_SEARCH
1259         unsigned best_partition_bits;
1260         unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1261 #endif
1262 #ifdef FLAC__SEARCH_FOR_ESCAPES
1263         unsigned flat_bits;
1264 #endif
1265         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1266
1267         assert(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1268
1269         if(partition_order == 0) {
1270                 unsigned i;
1271
1272 #ifndef NO_RICE_SEARCH
1273                 if(rice_parameter_search_dist) {
1274                         if(suggested_rice_parameter < rice_parameter_search_dist)
1275                                 min_rice_parameter = 0;
1276                         else
1277                                 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1278                         max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1279                         if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1280                                 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1281                 }
1282                 else
1283                         min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1284
1285                 best_partition_bits = 0xffffffff;
1286                 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1287 #endif
1288 #ifdef VARIABLE_RICE_BITS
1289 #ifdef FLAC__SYMMETRIC_RICE
1290                         partition_bits = (2+rice_parameter) * residual_samples;
1291 #else
1292                         const unsigned rice_parameter_estimate = rice_parameter-1;
1293                         partition_bits = (1+rice_parameter) * residual_samples;
1294 #endif
1295 #else
1296                         partition_bits = 0;
1297 #endif
1298                         partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1299                         for(i = 0; i < residual_samples; i++) {
1300 #ifdef VARIABLE_RICE_BITS
1301 #ifdef FLAC__SYMMETRIC_RICE
1302                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1303 #else
1304                                 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1305 #endif
1306 #else
1307                                 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1308 #endif
1309                         }
1310 #ifndef NO_RICE_SEARCH
1311                         if(partition_bits < best_partition_bits) {
1312                                 best_rice_parameter = rice_parameter;
1313                                 best_partition_bits = partition_bits;
1314                         }
1315                 }
1316 #endif
1317 #ifdef FLAC__SEARCH_FOR_ESCAPES
1318                 flat_bits = raw_bits_per_partition[0] * residual_samples;
1319                 if(flat_bits <= best_partition_bits) {
1320                         raw_bits[0] = raw_bits_per_partition[0];
1321                         best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1322                         best_partition_bits = flat_bits;
1323                 }
1324 #endif
1325                 parameters[0] = best_rice_parameter;
1326                 bits_ += best_partition_bits;
1327         }
1328         else {
1329                 unsigned partition, j, save_j, k;
1330                 unsigned mean, partition_samples;
1331                 const unsigned partitions = 1u << partition_order;
1332                 for(partition = j = 0; partition < partitions; partition++) {
1333                         partition_samples = (residual_samples+predictor_order) >> partition_order;
1334                         if(partition == 0) {
1335                                 if(partition_samples <= predictor_order)
1336                                         return false;
1337                                 else
1338                                         partition_samples -= predictor_order;
1339                         }
1340                         mean = partition_samples >> 1;
1341 #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
1342                         mean += abs_residual_partition_sums[partition];
1343 #else
1344                         save_j = j;
1345                         for(k = 0; k < partition_samples; j++, k++)
1346                                 mean += abs_residual[j];
1347                         j = save_j;
1348 #endif
1349                         mean /= partition_samples;
1350 #ifdef FLAC__SYMMETRIC_RICE
1351                         /* calc rice_parameter = floor(log2(mean)) */
1352                         rice_parameter = 0;
1353                         mean>>=1;
1354                         while(mean) {
1355                                 rice_parameter++;
1356                                 mean >>= 1;
1357                         }
1358 #else
1359                         /* calc rice_parameter = floor(log2(mean)) + 1 */
1360                         rice_parameter = 0;
1361                         while(mean) {
1362                                 rice_parameter++;
1363                                 mean >>= 1;
1364                         }
1365 #endif
1366                         if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1367                                 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1368
1369 #ifndef NO_RICE_SEARCH
1370                         if(rice_parameter_search_dist) {
1371                                 if(rice_parameter < rice_parameter_search_dist)
1372                                         min_rice_parameter = 0;
1373                                 else
1374                                         min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1375                                 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
1376                                 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1377                                         max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1378                         }
1379                         else
1380                                 min_rice_parameter = max_rice_parameter = rice_parameter;
1381
1382                         best_partition_bits = 0xffffffff;
1383                         for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1384 #endif
1385 #ifdef VARIABLE_RICE_BITS
1386 #ifdef FLAC__SYMMETRIC_RICE
1387                                 partition_bits = (2+rice_parameter) * partition_samples;
1388 #else
1389                                 const unsigned rice_parameter_estimate = rice_parameter-1;
1390                                 partition_bits = (1+rice_parameter) * partition_samples;
1391 #endif
1392 #else
1393                                 partition_bits = 0;
1394 #endif
1395                                 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1396                                 save_j = j;
1397                                 for(k = 0; k < partition_samples; j++, k++) {
1398 #ifdef VARIABLE_RICE_BITS
1399 #ifdef FLAC__SYMMETRIC_RICE
1400                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter);
1401 #else
1402                                         partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter_estimate);
1403 #endif
1404 #else
1405                                         partition_bits += FLAC__bitbuffer_rice_bits(residual[j], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
1406 #endif
1407                                 }
1408                                 if(rice_parameter != max_rice_parameter)
1409                                         j = save_j;
1410 #ifndef NO_RICE_SEARCH
1411                                 if(partition_bits < best_partition_bits) {
1412                                         best_rice_parameter = rice_parameter;
1413                                         best_partition_bits = partition_bits;
1414                                 }
1415                         }
1416 #endif
1417 #ifdef FLAC__SEARCH_FOR_ESCAPES
1418                         flat_bits = raw_bits_per_partition[partition] * partition_samples;
1419                         if(flat_bits <= best_partition_bits) {
1420                                 raw_bits[partition] = raw_bits_per_partition[partition];
1421                                 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1422                                 best_partition_bits = flat_bits;
1423                         }
1424 #endif
1425                         parameters[partition] = best_rice_parameter;
1426                         bits_ += best_partition_bits;
1427                 }
1428         }
1429
1430         *bits = bits_;
1431         return true;
1432 }
1433
1434 unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples)
1435 {
1436         unsigned i, shift;
1437         int32 x = 0;
1438
1439         for(i = 0; i < samples && !(x&1); i++)
1440                 x |= signal[i];
1441
1442         if(x == 0) {
1443                 shift = 0;
1444         }
1445         else {
1446                 for(shift = 0; !(x&1); shift++)
1447                         x >>= 1;
1448         }
1449
1450         if(shift > 0) {
1451                 for(i = 0; i < samples; i++)
1452                          signal[i] >>= shift;
1453         }
1454
1455         return shift;
1456 }