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