speed up mid-side coding
[flac.git] / src / libFLAC / encoder.c
1 /* libFLAC - Free Lossless Audio Coder 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 "private/bitbuffer.h"
26 #include "private/encoder_framing.h"
27 #include "private/fixed.h"
28 #include "private/lpc.h"
29 #include "private/md5.h"
30
31 #ifdef min
32 #undef min
33 #endif
34 #define min(x,y) ((x)<(y)?(x):(y))
35
36 #ifdef max
37 #undef max
38 #endif
39 #define max(x,y) ((x)>(y)?(x):(y))
40
41 typedef struct FLAC__EncoderPrivate {
42         unsigned input_capacity;                    /* current size (in samples) of the signal and residual buffers */
43         int32 *integer_signal[FLAC__MAX_CHANNELS];  /* the integer version of the input signal */
44         int32 *integer_signal_mid_side[2];          /* the integer version of the mid-side input signal (stereo only) */
45         real *real_signal[FLAC__MAX_CHANNELS];      /* the floating-point version of the input signal */
46         real *real_signal_mid_side[2];              /* the floating-point version of the mid-side input signal (stereo only) */
47         int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
48         int32 *residual_workspace_mid_side[2][2];
49         FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
50         FLAC__Subframe subframe_workspace_mid_side[2][2];
51         FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
52         FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
53         unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
54         unsigned best_subframe_mid_side[2];
55         unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
56         unsigned best_subframe_bits_mid_side[2];
57         uint32 *abs_residual;                       /* workspace where the abs(candidate residual) is stored */
58         FLAC__BitBuffer frame;                      /* the current frame being worked on */
59         bool current_frame_can_do_mid_side;         /* encoder sets this false when any given sample of a frame's side channel exceeds 16 bits */
60         FLAC__StreamMetaData metadata;
61         unsigned current_sample_number;
62         unsigned current_frame_number;
63         struct MD5Context md5context;
64         FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
65         void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
66         void *client_data;
67 } FLAC__EncoderPrivate;
68
69 static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
70 static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
71 static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
72 static bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
73 static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
74 static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__Subframe *subframe);
75 static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe);
76 static unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], const real lp_coeff[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe);
77 static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__Subframe *subframe);
78 static unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned max_partition_order, unsigned *best_partition_order, unsigned best_parameters[]);
79 #if 0
80 @@@
81 static void encoder_promote_candidate_subframe_(FLAC__Subframe *best_subframe, FLAC__Subframe *candidata_subframe, unsigned *best_residual);
82 #endif
83 static bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameter, const unsigned partition_order, unsigned parameters[], unsigned *bits);
84
85 const char *FLAC__EncoderWriteStatusString[] = {
86         "FLAC__ENCODER_WRITE_OK",
87         "FLAC__ENCODER_WRITE_FATAL_ERROR"
88 };
89
90 const char *FLAC__EncoderStateString[] = {
91         "FLAC__ENCODER_OK",
92         "FLAC__ENCODER_UNINITIALIZED",
93         "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
94         "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
95         "FLAC__ENCODER_INVALID_SAMPLE_RATE",
96         "FLAC__ENCODER_INVALID_BLOCK_SIZE",
97         "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
98         "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
99         "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
100         "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
101         "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
102         "FLAC__ENCODER_NOT_STREAMABLE",
103         "FLAC__ENCODER_FRAMING_ERROR",
104         "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
105         "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
106         "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
107 };
108
109
110 bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
111 {
112         bool ok;
113         unsigned i, channel;
114         int32 *previous_is, *current_is;
115         real *previous_rs, *current_rs;
116         int32 *residual;
117         uint32 *abs_residual;
118
119         assert(new_size > 0);
120         assert(encoder->state == FLAC__ENCODER_OK);
121         assert(encoder->guts->current_sample_number == 0);
122
123         /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
124         if(new_size <= encoder->guts->input_capacity)
125                 return true;
126
127         ok = 1;
128         if(ok) {
129                 for(i = 0; ok && i < encoder->channels; i++) {
130                         /* integer version of the signal */
131                         previous_is = encoder->guts->integer_signal[i];
132                         current_is = (int32*)malloc(sizeof(int32) * new_size);
133                         if(0 == current_is) {
134                                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
135                                 ok = 0;
136                         }
137                         else {
138                                 encoder->guts->integer_signal[i] = current_is;
139                                 if(previous_is != 0)
140                                         free(previous_is);
141                         }
142                         /* real version of the signal */
143                         previous_rs = encoder->guts->real_signal[i];
144                         current_rs = (real*)malloc(sizeof(real) * new_size);
145                         if(0 == current_rs) {
146                                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
147                                 ok = 0;
148                         }
149                         else {
150                                 encoder->guts->real_signal[i] = current_rs;
151                                 if(previous_rs != 0)
152                                         free(previous_rs);
153                         }
154                 }
155         }
156         if(ok) {
157                 for(i = 0; ok && i < 2; i++) {
158                         /* integer version of the signal */
159                         previous_is = encoder->guts->integer_signal_mid_side[i];
160                         current_is = (int32*)malloc(sizeof(int32) * new_size);
161                         if(0 == current_is) {
162                                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
163                                 ok = 0;
164                         }
165                         else {
166                                 encoder->guts->integer_signal_mid_side[i] = current_is;
167                                 if(previous_is != 0)
168                                         free(previous_is);
169                         }
170                         /* real version of the signal */
171                         previous_rs = encoder->guts->real_signal_mid_side[i];
172                         current_rs = (real*)malloc(sizeof(real) * new_size);
173                         if(0 == current_rs) {
174                                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
175                                 ok = 0;
176                         }
177                         else {
178                                 encoder->guts->real_signal_mid_side[i] = current_rs;
179                                 if(previous_rs != 0)
180                                         free(previous_rs);
181                         }
182                 }
183         }
184         if(ok) {
185                 for(channel = 0; channel < encoder->channels; channel++) {
186                         for(i = 0; i < 2; i++) {
187                                 residual = (int32*)malloc(sizeof(int32) * new_size);
188                                 if(0 == residual) {
189                                         encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
190                                         ok = 0;
191                                 }
192                                 else {
193                                         if(encoder->guts->residual_workspace[channel][i] != 0)
194                                                 free(encoder->guts->residual_workspace[channel][i]);
195                                         encoder->guts->residual_workspace[channel][i] = residual;
196                                 }
197                         }
198                 }
199                 for(channel = 0; channel < 2; channel++) {
200                         for(i = 0; i < 2; i++) {
201                                 residual = (int32*)malloc(sizeof(int32) * new_size);
202                                 if(0 == residual) {
203                                         encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
204                                         ok = 0;
205                                 }
206                                 else {
207                                         if(encoder->guts->residual_workspace_mid_side[channel][i] != 0)
208                                                 free(encoder->guts->residual_workspace_mid_side[channel][i]);
209                                         encoder->guts->residual_workspace_mid_side[channel][i] = residual;
210                                 }
211                         }
212                 }
213                 abs_residual = (uint32*)malloc(sizeof(uint32) * new_size);
214                 if(0 == residual) {
215                         encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
216                         ok = 0;
217                 }
218                 else {
219                         if(encoder->guts->abs_residual != 0)
220                                 free(encoder->guts->abs_residual);
221                         encoder->guts->abs_residual = abs_residual;
222                 }
223         }
224         if(ok)
225                 encoder->guts->input_capacity = new_size;
226
227         return ok;
228 }
229
230 FLAC__Encoder *FLAC__encoder_get_new_instance()
231 {
232         FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
233         if(encoder != 0) {
234                 encoder->state = FLAC__ENCODER_UNINITIALIZED;
235                 encoder->guts = 0;
236         }
237         return encoder;
238 }
239
240 void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
241 {
242         assert(encoder != 0);
243         free(encoder);
244 }
245
246 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)
247 {
248         unsigned i;
249
250         assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
251         assert(encoder != 0);
252         assert(write_callback != 0);
253         assert(metadata_callback != 0);
254         assert(encoder->state == FLAC__ENCODER_UNINITIALIZED);
255         assert(encoder->guts == 0);
256
257         encoder->state = FLAC__ENCODER_OK;
258
259         if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
260                 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
261
262         if(encoder->do_mid_side_stereo && encoder->channels != 2)
263                 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
264
265         if(encoder->do_mid_side_stereo && encoder->bits_per_sample > 16)
266                 return encoder->state = FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH;
267
268         if(encoder->force_mid_side_stereo && !encoder->do_mid_side_stereo)
269                 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
270
271         if(encoder->bits_per_sample == 0 || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
272                 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
273
274         if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
275                 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
276
277         if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
278                 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
279
280         if(encoder->blocksize < encoder->max_lpc_order)
281                 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
282
283         if(encoder->qlp_coeff_precision == 0) {
284                 if(encoder->bits_per_sample < 16) {
285                         /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
286                         /* @@@ until then we'll make a guess */
287                         encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
288                 }
289                 else if(encoder->bits_per_sample == 16) {
290                         if(encoder->blocksize <= 192)
291                                 encoder->qlp_coeff_precision = 7;
292                         else if(encoder->blocksize <= 384)
293                                 encoder->qlp_coeff_precision = 8;
294                         else if(encoder->blocksize <= 576)
295                                 encoder->qlp_coeff_precision = 9;
296                         else if(encoder->blocksize <= 1152)
297                                 encoder->qlp_coeff_precision = 10;
298                         else if(encoder->blocksize <= 2304)
299                                 encoder->qlp_coeff_precision = 11;
300                         else if(encoder->blocksize <= 4608)
301                                 encoder->qlp_coeff_precision = 12;
302                         else
303                                 encoder->qlp_coeff_precision = 13;
304                 }
305                 else {
306                         encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
307                 }
308         }
309         else if(encoder->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->qlp_coeff_precision + encoder->bits_per_sample >= 8*sizeof(uint32))
310                 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
311
312         if(encoder->streamable_subset) {
313                 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)
314                         return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
315                 if(encoder->sample_rate > 655350)
316                         return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
317         }
318
319         if(encoder->rice_optimization_level >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
320                 encoder->rice_optimization_level = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
321
322         encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
323         if(encoder->guts == 0)
324                 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
325
326         encoder->guts->input_capacity = 0;
327         for(i = 0; i < encoder->channels; i++) {
328                 encoder->guts->integer_signal[i] = 0;
329                 encoder->guts->real_signal[i] = 0;
330         }
331         for(i = 0; i < 2; i++) {
332                 encoder->guts->integer_signal_mid_side[i] = 0;
333                 encoder->guts->real_signal_mid_side[i] = 0;
334         }
335         for(i = 0; i < encoder->channels; i++) {
336                 encoder->guts->residual_workspace[i][0] = encoder->guts->residual_workspace[i][1] = 0;
337                 encoder->guts->best_subframe[i] = 0;
338         }
339         for(i = 0; i < 2; i++) {
340                 encoder->guts->residual_workspace_mid_side[i][0] = encoder->guts->residual_workspace_mid_side[i][1] = 0;
341                 encoder->guts->best_subframe_mid_side[i] = 0;
342         }
343         for(i = 0; i < encoder->channels; i++) {
344                 encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
345                 encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
346         }
347         for(i = 0; i < 2; i++) {
348                 encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
349                 encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
350         }
351         encoder->guts->abs_residual = 0;
352         encoder->guts->current_frame_can_do_mid_side = true;
353         encoder->guts->current_sample_number = 0;
354         encoder->guts->current_frame_number = 0;
355
356         if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
357                 /* the above function sets the state for us in case of an error */
358                 return encoder->state;
359         }
360         FLAC__bitbuffer_init(&encoder->guts->frame);
361         encoder->guts->write_callback = write_callback;
362         encoder->guts->metadata_callback = metadata_callback;
363         encoder->guts->client_data = client_data;
364
365         /*
366          * write the stream header
367          */
368         if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
369                 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
370
371         if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
372                 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
373
374         encoder->guts->metadata.type = FLAC__METADATA_TYPE_ENCODING;
375         encoder->guts->metadata.is_last = true;
376         encoder->guts->metadata.length = FLAC__STREAM_METADATA_ENCODING_LENGTH;
377         encoder->guts->metadata.data.encoding.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
378         encoder->guts->metadata.data.encoding.max_blocksize = encoder->blocksize;
379         encoder->guts->metadata.data.encoding.min_framesize = 0; /* we don't know this yet; have to fill it in later */
380         encoder->guts->metadata.data.encoding.max_framesize = 0; /* we don't know this yet; have to fill it in later */
381         encoder->guts->metadata.data.encoding.sample_rate = encoder->sample_rate;
382         encoder->guts->metadata.data.encoding.channels = encoder->channels;
383         encoder->guts->metadata.data.encoding.bits_per_sample = encoder->bits_per_sample;
384         encoder->guts->metadata.data.encoding.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
385         memset(encoder->guts->metadata.data.encoding.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
386         MD5Init(&encoder->guts->md5context);
387         if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
388                 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
389
390         assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
391         assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
392         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)
393                 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
394
395         /* now that the metadata block is written, we can init this to an absurdly-high value... */
396         encoder->guts->metadata.data.encoding.min_framesize = (1u << FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN) - 1;
397         /* ... and clear this to 0 */
398         encoder->guts->metadata.data.encoding.total_samples = 0;
399
400         return encoder->state;
401 }
402
403 void FLAC__encoder_finish(FLAC__Encoder *encoder)
404 {
405         unsigned i, channel;
406
407         assert(encoder != 0);
408         if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
409                 return;
410         if(encoder->guts->current_sample_number != 0) {
411                 encoder->blocksize = encoder->guts->current_sample_number;
412                 encoder_process_frame_(encoder, true); /* true => is last frame */
413         }
414         MD5Final(encoder->guts->metadata.data.encoding.md5sum, &encoder->guts->md5context);
415         encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
416         if(encoder->guts != 0) {
417                 for(i = 0; i < encoder->channels; i++) {
418                         if(encoder->guts->integer_signal[i] != 0) {
419                                 free(encoder->guts->integer_signal[i]);
420                                 encoder->guts->integer_signal[i] = 0;
421                         }
422                         if(encoder->guts->real_signal[i] != 0) {
423                                 free(encoder->guts->real_signal[i]);
424                                 encoder->guts->real_signal[i] = 0;
425                         }
426                 }
427                 for(i = 0; i < 2; i++) {
428                         if(encoder->guts->integer_signal_mid_side[i] != 0) {
429                                 free(encoder->guts->integer_signal_mid_side[i]);
430                                 encoder->guts->integer_signal_mid_side[i] = 0;
431                         }
432                         if(encoder->guts->real_signal_mid_side[i] != 0) {
433                                 free(encoder->guts->real_signal_mid_side[i]);
434                                 encoder->guts->real_signal_mid_side[i] = 0;
435                         }
436                 }
437                 for(channel = 0; channel < encoder->channels; channel++) {
438                         for(i = 0; i < 2; i++) {
439                                 if(encoder->guts->residual_workspace[channel][i] != 0) {
440                                         free(encoder->guts->residual_workspace[channel][i]);
441                                         encoder->guts->residual_workspace[channel][i] = 0;
442                                 }
443                         }
444                 }
445                 for(channel = 0; channel < 2; channel++) {
446                         for(i = 0; i < 2; i++) {
447                                 if(encoder->guts->residual_workspace_mid_side[channel][i] != 0) {
448                                         free(encoder->guts->residual_workspace_mid_side[channel][i]);
449                                         encoder->guts->residual_workspace_mid_side[channel][i] = 0;
450                                 }
451                         }
452                 }
453                 if(encoder->guts->abs_residual != 0) {
454                         free(encoder->guts->abs_residual);
455                         encoder->guts->abs_residual = 0;
456                 }
457                 FLAC__bitbuffer_free(&encoder->guts->frame);
458                 free(encoder->guts);
459                 encoder->guts = 0;
460         }
461         encoder->state = FLAC__ENCODER_UNINITIALIZED;
462 }
463
464 bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
465 {
466         unsigned i, j, channel;
467         int32 x, mid, side;
468         const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
469         const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
470         const int32 max_side =  ((int64)1 << (encoder->bits_per_sample-1)) - 1;
471
472         assert(encoder != 0);
473         assert(encoder->state == FLAC__ENCODER_OK);
474
475         j = 0;
476         do {
477                 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) {
478                         for(channel = 0; channel < encoder->channels; channel++) {
479                                 x = buf[channel][j];
480                                 encoder->guts->integer_signal[channel][i] = x;
481                                 encoder->guts->real_signal[channel][i] = (real)x;
482                         }
483                         if(ms && encoder->guts->current_frame_can_do_mid_side) {
484                                 side = buf[0][j] - buf[1][j];
485                                 if(side < min_side || side > max_side) {
486                                         encoder->guts->current_frame_can_do_mid_side = false;
487                                 }
488                                 else {
489                                         mid = (buf[0][j] + buf[1][j]) >> 1; /* NOTE: not the same as divide-by-two ! */
490                                         encoder->guts->integer_signal_mid_side[0][i] = mid;
491                                         encoder->guts->integer_signal_mid_side[1][i] = side;
492                                         encoder->guts->real_signal_mid_side[0][i] = (real)mid;
493                                         encoder->guts->real_signal_mid_side[1][i] = (real)side;
494                                 }
495                         }
496                         encoder->guts->current_sample_number++;
497                 }
498                 if(i == encoder->blocksize) {
499                         if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
500                                 return false;
501                 }
502         } while(j < samples);
503
504         return true;
505 }
506
507 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
508 bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
509 {
510         unsigned i, j, k, channel;
511         int32 x, left = 0, mid, side;
512         const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
513         const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
514         const int32 max_side =  ((int64)1 << (encoder->bits_per_sample-1)) - 1;
515
516         assert(encoder != 0);
517         assert(encoder->state == FLAC__ENCODER_OK);
518
519         j = k = 0;
520         do {
521                 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) {
522                         for(channel = 0; channel < encoder->channels; channel++, k++) {
523                                 x = buf[k];
524                                 encoder->guts->integer_signal[channel][i] = x;
525                                 encoder->guts->real_signal[channel][i] = (real)x;
526                                 if(ms && encoder->guts->current_frame_can_do_mid_side) {
527                                         if(channel == 0) {
528                                                 left = x;
529                                         }
530                                         else {
531                                                 side = left - x;
532                                                 if(side < min_side || side > max_side) {
533                                                         encoder->guts->current_frame_can_do_mid_side = false;
534                                                 }
535                                                 else {
536                                                         mid = (left + x) >> 1; /* NOTE: not the same as divide-by-two ! */
537                                                         encoder->guts->integer_signal_mid_side[0][i] = mid;
538                                                         encoder->guts->integer_signal_mid_side[1][i] = side;
539                                                         encoder->guts->real_signal_mid_side[0][i] = (real)mid;
540                                                         encoder->guts->real_signal_mid_side[1][i] = (real)side;
541                                                 }
542                                         }
543                                 }
544                         }
545                         encoder->guts->current_sample_number++;
546                 }
547                 if(i == encoder->blocksize) {
548                         if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
549                                 return false;
550                 }
551         } while(j < samples);
552
553         return true;
554 }
555
556 bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
557 {
558         assert(encoder->state == FLAC__ENCODER_OK);
559
560         /*
561          * Accumulate raw signal to the MD5 signature
562          */
563         if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
564                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
565                 return false;
566         }
567
568         /*
569          * Process the frame header and subframes into the frame bitbuffer
570          */
571         if(!encoder_process_subframes_(encoder, is_last_frame)) {
572                 /* the above function sets the state for us in case of an error */
573                 return false;
574         }
575
576         /*
577          * Zero-pad the frame to a byte_boundary
578          */
579         if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
580                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
581                 return false;
582         }
583
584         /*
585          * Write it
586          */
587         assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
588         assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
589         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) {
590                 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
591                 return false;
592         }
593
594         /*
595          * Get ready for the next frame
596          */
597         encoder->guts->current_frame_can_do_mid_side = true;
598         encoder->guts->current_sample_number = 0;
599         encoder->guts->current_frame_number++;
600         encoder->guts->metadata.data.encoding.total_samples += (uint64)encoder->blocksize;
601         encoder->guts->metadata.data.encoding.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.encoding.min_framesize);
602         encoder->guts->metadata.data.encoding.max_framesize = max(encoder->guts->frame.bytes, encoder->guts->metadata.data.encoding.max_framesize);
603
604         return true;
605 }
606
607 bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
608 {
609         FLAC__FrameHeader frame_header;
610         unsigned channel, max_partition_order;
611
612         /*
613          * Calculate the max Rice partition order
614          */
615         if(is_last_frame) {
616                 max_partition_order = 0;
617         }
618         else {
619                 unsigned limit = 0, b = encoder->blocksize;
620                 while(!(b & 1)) {
621                         limit++;
622                         b >>= 1;
623                 }
624                 max_partition_order = min(encoder->rice_optimization_level, limit);
625         }
626
627         /*
628          * Setup the frame
629          */
630         if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
631                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
632                 return false;
633         }
634         frame_header.blocksize = encoder->blocksize;
635         frame_header.sample_rate = encoder->sample_rate;
636         frame_header.channels = encoder->channels;
637         frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
638         frame_header.bits_per_sample = encoder->bits_per_sample;
639         frame_header.number.frame_number = encoder->guts->current_frame_number;
640
641         /*
642          * First do a normal encoding pass of each independent channel
643          */
644         for(channel = 0; channel < encoder->channels; channel++) {
645                 if(!encoder_process_subframe_(encoder, max_partition_order, encoder->force_mid_side_stereo, &frame_header, 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))
646                         return false;
647         }
648
649         /*
650          * Now do mid and side channels if requested
651          */
652         if(encoder->do_mid_side_stereo && encoder->guts->current_frame_can_do_mid_side) {
653                 assert(encoder->channels == 2);
654
655                 for(channel = 0; channel < 2; channel++) {
656                         if(!encoder_process_subframe_(encoder, max_partition_order, false, &frame_header, 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))
657                                 return false;
658                 }
659         }
660
661         /*
662          * Compose the frame bitbuffer
663          */
664         if(encoder->do_mid_side_stereo && encoder->guts->current_frame_can_do_mid_side) {
665                 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
666                 unsigned min_bits;
667                 FLAC__ChannelAssignment ca, min_assignment;
668                 assert(encoder->channels == 2);
669
670                 /* We have to figure out which channel assignent results in the smallest frame */
671                 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits         [0] + encoder->guts->best_subframe_bits         [1];
672                 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->guts->best_subframe_bits         [0] + encoder->guts->best_subframe_bits_mid_side[1];
673                 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits         [1] + encoder->guts->best_subframe_bits_mid_side[1];
674                 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
675
676                 for(min_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
677                         if(bits[ca] < min_bits) {
678                                 min_bits = bits[ca];
679                                 min_assignment = ca;
680                         }
681                 }
682
683                 frame_header.channel_assignment = min_assignment;
684
685                 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
686                         encoder->state = FLAC__ENCODER_FRAMING_ERROR;
687                         return false;
688                 }
689
690                 switch(min_assignment) {
691                         /* note that encoder_add_subframe_ sets the state for us in case of an error */
692                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
693                                 if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace         [0][encoder->guts->best_subframe         [0]], &encoder->guts->frame))
694                                         return false;
695                                 if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace         [1][encoder->guts->best_subframe         [1]], &encoder->guts->frame))
696                                         return false;
697                                 break;
698                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
699                                 if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace         [0][encoder->guts->best_subframe         [0]], &encoder->guts->frame))
700                                         return false;
701                                 if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
702                                         return false;
703                                 break;
704                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
705                                 if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
706                                         return false;
707                                 if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace         [1][encoder->guts->best_subframe         [1]], &encoder->guts->frame))
708                                         return false;
709                                 break;
710                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
711                                 if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]], &encoder->guts->frame))
712                                         return false;
713                                 if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
714                                         return false;
715                                 break;
716                         default:
717                                 assert(0);
718                 }
719         }
720         else {
721                 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
722                         encoder->state = FLAC__ENCODER_FRAMING_ERROR;
723                         return false;
724                 }
725
726                 for(channel = 0; channel < encoder->channels; channel++) {
727                         if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace[channel][encoder->guts->best_subframe[channel]], &encoder->guts->frame)) {
728                                 /* the above function sets the state for us in case of an error */
729                                 return false;
730                         }
731                 }
732         }
733
734         return true;
735 }
736
737 bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
738 {
739         real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
740         real lpc_residual_bits_per_sample;
741         real autoc[FLAC__MAX_LPC_ORDER+1];
742         real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
743         real lpc_error[FLAC__MAX_LPC_ORDER];
744         unsigned min_lpc_order, max_lpc_order, lpc_order;
745         unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
746         unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
747         unsigned rice_parameter;
748         unsigned _candidate_bits, _best_bits;
749         unsigned _best_subframe;
750
751         /* verbatim subframe is the baseline against which we measure other compressed subframes */
752         _best_subframe = 0;
753         _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, frame_header->bits_per_sample, subframe[_best_subframe]);
754
755         if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
756                 /* check for constant subframe */
757                 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);
758                 if(fixed_residual_bits_per_sample[1] == 0.0) {
759                         /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
760                         unsigned i, signal_is_constant = true;
761                         for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
762                                 if(integer_signal[0] != integer_signal[i]) {
763                                         signal_is_constant = false;
764                                         break;
765                                 }
766                         }
767                         if(signal_is_constant) {
768                                 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], frame_header->bits_per_sample, subframe[!_best_subframe]);
769                                 if(_candidate_bits < _best_bits) {
770                                         _best_subframe = !_best_subframe;
771                                         _best_bits = _candidate_bits;
772                                 }
773                         }
774                 }
775                 else {
776                         /* encode fixed */
777                         if(encoder->do_exhaustive_model_search) {
778                                 min_fixed_order = 0;
779                                 max_fixed_order = FLAC__MAX_FIXED_ORDER;
780                         }
781                         else {
782                                 min_fixed_order = max_fixed_order = guess_fixed_order;
783                         }
784                         for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
785                                 if(fixed_residual_bits_per_sample[fixed_order] >= (real)frame_header->bits_per_sample)
786                                         continue; /* don't even try */
787                                 /* 0.5 is for rounding, another 1.0 is to account for the signed->unsigned conversion during rice coding */
788                                 rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+1.5) : 0;
789                                 if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
790                                         rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
791                                 _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, frame_header->blocksize, frame_header->bits_per_sample, fixed_order, rice_parameter, max_partition_order, subframe[!_best_subframe]);
792                                 if(_candidate_bits < _best_bits) {
793                                         _best_subframe = !_best_subframe;
794                                         _best_bits = _candidate_bits;
795                                 }
796                         }
797
798                         /* encode lpc */
799                         if(encoder->max_lpc_order > 0) {
800                                 if(encoder->max_lpc_order >= frame_header->blocksize)
801                                         max_lpc_order = frame_header->blocksize-1;
802                                 else
803                                         max_lpc_order = encoder->max_lpc_order;
804                                 if(max_lpc_order > 0) {
805                                         FLAC__lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
806                                         FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
807                                         if(encoder->do_exhaustive_model_search) {
808                                                 min_lpc_order = 1;
809                                         }
810                                         else {
811                                                 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, frame_header->bits_per_sample);
812                                                 min_lpc_order = max_lpc_order = guess_lpc_order;
813                                         }
814                                         if(encoder->do_qlp_coeff_prec_search) {
815                                                 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
816                                                 max_qlp_coeff_precision = 32 - frame_header->bits_per_sample - 1;
817                                         }
818                                         else {
819                                                 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
820                                         }
821                                         for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
822                                                 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize);
823                                                 if(lpc_residual_bits_per_sample >= (real)frame_header->bits_per_sample)
824                                                         continue; /* don't even try */
825                                                 /* 0.5 is for rounding, another 1.0 is to account for the signed->unsigned conversion during rice coding */
826                                                 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+1.5) : 0;
827                                                 if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
828                                                         rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
829                                                 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
830                                                         _candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, lp_coeff[lpc_order-1], frame_header->blocksize, frame_header->bits_per_sample, lpc_order, qlp_coeff_precision, rice_parameter, max_partition_order, subframe[!_best_subframe]);
831                                                         if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
832                                                                 if(_candidate_bits < _best_bits) {
833                                                                         _best_subframe = !_best_subframe;
834                                                                         _best_bits = _candidate_bits;
835                                                                 }
836                                                         }
837                                                 }
838                                         }
839                                 }
840                         }
841                 }
842         }
843
844         *best_subframe = _best_subframe;
845         *best_bits = _best_bits;
846
847         return true;
848 }
849
850 bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
851 {
852         switch(subframe->type) {
853                 case FLAC__SUBFRAME_TYPE_CONSTANT:
854                         if(!FLAC__subframe_add_constant(&(subframe->data.constant), frame_header->bits_per_sample, frame)) {
855                                 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
856                                 return false;
857                         }
858                         break;
859                 case FLAC__SUBFRAME_TYPE_FIXED:
860                         if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, frame_header->bits_per_sample, frame)) {
861                                 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
862                                 return false;
863                         }
864                         break;
865                 case FLAC__SUBFRAME_TYPE_LPC:
866                         if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, frame_header->bits_per_sample, frame)) {
867                                 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
868                                 return false;
869                         }
870                         break;
871                 case FLAC__SUBFRAME_TYPE_VERBATIM:
872                         if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, frame_header->bits_per_sample, frame)) {
873                                 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
874                                 return false;
875                         }
876                         break;
877                 default:
878                         assert(0);
879         }
880
881         return true;
882 }
883
884 unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__Subframe *subframe)
885 {
886         subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
887         subframe->data.constant.value = signal;
888
889         return FLAC__SUBFRAME_TYPE_LEN + bits_per_sample;
890 }
891
892 unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe)
893 {
894         unsigned i, residual_bits;
895         const unsigned residual_samples = blocksize - order;
896
897         FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
898
899         subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
900
901         subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
902         subframe->data.fixed.residual = residual;
903
904         residual_bits = encoder_find_best_partition_order_(residual, abs_residual, residual_samples, order, rice_parameter, max_partition_order, &subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters);
905
906         subframe->data.fixed.order = order;
907         for(i = 0; i < order; i++)
908                 subframe->data.fixed.warmup[i] = signal[i];
909
910         return FLAC__SUBFRAME_TYPE_LEN + (order * bits_per_sample) + residual_bits;
911 }
912
913 unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], const real lp_coeff[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe)
914 {
915         int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
916         unsigned i, residual_bits;
917         int quantization, ret;
918         const unsigned residual_samples = blocksize - order;
919
920         ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, bits_per_sample, qlp_coeff, &quantization);
921         if(ret != 0)
922                 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
923
924         FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
925
926         subframe->type = FLAC__SUBFRAME_TYPE_LPC;
927
928         subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
929         subframe->data.lpc.residual = residual;
930
931         residual_bits = encoder_find_best_partition_order_(residual, abs_residual, residual_samples, order, rice_parameter, max_partition_order, &subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.parameters);
932
933         subframe->data.lpc.order = order;
934         subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
935         subframe->data.lpc.quantization_level = quantization;
936         memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
937         for(i = 0; i < order; i++)
938                 subframe->data.lpc.warmup[i] = signal[i];
939
940         return FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + bits_per_sample)) + residual_bits;
941 }
942
943 unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__Subframe *subframe)
944 {
945         subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
946
947         subframe->data.verbatim.data = signal;
948
949         return FLAC__SUBFRAME_TYPE_LEN + (blocksize * bits_per_sample);
950 }
951
952 unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned max_partition_order, unsigned *best_partition_order, unsigned best_parameters[])
953 {
954         unsigned residual_bits, best_residual_bits = 0;
955         unsigned i, partition_order;
956         unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
957         int32 r;
958
959         /* compute the abs(residual) for use later */
960         for(i = 0; i < residual_samples; i++) {
961                 r = residual[i];
962                 abs_residual[i] = (uint32)(r<0? -r : r);
963         }
964
965         for(partition_order = 0; partition_order <= max_partition_order; partition_order++) {
966                 if(!encoder_set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, partition_order, parameters[!best_parameters_index], &residual_bits)) {
967                         assert(best_residual_bits != 0);
968                         break;
969                 }
970                 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
971                         best_residual_bits = residual_bits;
972                         *best_partition_order = partition_order;
973                         best_parameters_index = !best_parameters_index;
974                 }
975         }
976         memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
977
978         return best_residual_bits;
979 }
980
981 #if 0
982 @@@
983 void encoder_promote_candidate_subframe_(FLAC__Encoder *encoder)
984 {
985         assert(encoder->state == FLAC__ENCODER_OK);
986         encoder->guts->best_subframe = encoder->guts->candidate_subframe;
987         encoder->guts->best_residual = !encoder->guts->best_residual;
988 }
989 #endif
990
991 #ifdef ESTIMATE_RICE_BITS
992 #undef ESTIMATE_RICE_BITS
993 #endif
994 #define ESTIMATE_RICE_BITS(value, parameter) ((value) >> (parameter))
995
996 bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameter, const unsigned partition_order, unsigned parameters[], unsigned *bits)
997 {
998         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
999
1000         if(partition_order == 0) {
1001                 unsigned i;
1002 #ifdef ESTIMATE_RICE_BITS
1003                 const unsigned rice_parameter_estimate = rice_parameter-1;
1004                 bits_ += (1+rice_parameter) * residual_samples;
1005 #endif
1006                 parameters[0] = rice_parameter;
1007                 bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1008                 for(i = 0; i < residual_samples; i++)
1009 #ifdef ESTIMATE_RICE_BITS
1010                         bits_ += ESTIMATE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1011 #else
1012                         bits_ += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter);
1013 #endif
1014         }
1015         else {
1016                 unsigned i, j, k = 0, k_last = 0;
1017                 unsigned mean, parameter, partition_samples;
1018                 const unsigned max_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
1019                 for(i = 0; i < (1u<<partition_order); i++) {
1020                         partition_samples = (residual_samples+predictor_order) >> partition_order;
1021                         if(i == 0) {
1022                                 if(partition_samples <= predictor_order)
1023                                         return false;
1024                                 else
1025                                         partition_samples -= predictor_order;
1026                         }
1027                         mean = partition_samples >> 1;
1028                         for(j = 0; j < partition_samples; j++, k++)
1029                                 mean += abs_residual[k];
1030                         mean /= partition_samples;
1031                         /* calc parameter = floor(log2(mean)) + 1 */
1032                         parameter = 0;
1033                         while(mean) {
1034                                 parameter++;
1035                                 mean >>= 1;
1036                         }
1037                         if(parameter > max_parameter)
1038                                 parameter = max_parameter;
1039                         parameters[i] = parameter;
1040                         bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1041 #ifdef ESTIMATE_RICE_BITS
1042                         bits_ += (1+parameter) * partition_samples;
1043                         --parameter;
1044 #endif
1045                         for(j = k_last; j < k; j++)
1046 #ifdef ESTIMATE_RICE_BITS
1047                                 bits_ += ESTIMATE_RICE_BITS(abs_residual[j], parameter);
1048 #else
1049                                 bits_ += FLAC__bitbuffer_rice_bits(residual[j], parameter);
1050 #endif
1051                         k_last = k;
1052                 }
1053         }
1054
1055         *bits = bits_;
1056         return true;
1057 }
1058
1059 #if 0
1060 @@@
1061 bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned channels, const int32 *integer_signal[], const real *real_signal[], FLAC__BitBuffer *frame)
1062 {
1063         real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1064         real lpc_residual_bits_per_sample;
1065         real autoc[FLAC__MAX_LPC_ORDER+1];
1066         real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
1067         real lpc_error[FLAC__MAX_LPC_ORDER];
1068         unsigned channel;
1069         unsigned min_lpc_order, max_lpc_order, lpc_order;
1070         unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1071         unsigned max_partition_order;
1072         unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1073         unsigned rice_parameter;
1074         unsigned candidate_bits, best_bits;
1075
1076         if(is_last_frame) {
1077                 max_partition_order = 0;
1078         }
1079         else {
1080                 unsigned limit = 0, b = encoder->blocksize;
1081                 while(!(b & 1)) {
1082                         limit++;
1083                         b >>= 1;
1084                 }
1085                 max_partition_order = min(encoder->rice_optimization_level, limit);
1086         }
1087
1088         for(channel = 0; channel < channels; channel++) {
1089                 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1090                 best_bits = encoder_evaluate_verbatim_subframe_(frame_header->blocksize, frame_header->bits_per_sample, &(encoder->guts->best_subframe));
1091
1092                 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1093                         /* check for constant subframe */
1094                         guess_fixed_order = FLAC__fixed_compute_best_predictor(integer_signal[channel]+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
1095                         if(fixed_residual_bits_per_sample[1] == 0.0) {
1096                                 /* the above means integer_signal[channel]+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1097                                 unsigned i, signal_is_constant = true;
1098                                 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1099                                         if(integer_signal[channel][0] != integer_signal[channel][i]) {
1100                                                 signal_is_constant = false;
1101                                                 break;
1102                                         }
1103                                 }
1104                                 if(signal_is_constant) {
1105                                         candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[channel][0], frame_header->bits_per_sample, &(encoder->guts->candidate_subframe));
1106                                         if(candidate_bits < best_bits) {
1107                                                 encoder_promote_candidate_subframe_(encoder);
1108                                                 best_bits = candidate_bits;
1109                                         }
1110                                 }
1111                         }
1112                         else {
1113                                 /* encode fixed */
1114                                 if(encoder->do_exhaustive_model_search) {
1115                                         min_fixed_order = 0;
1116                                         max_fixed_order = FLAC__MAX_FIXED_ORDER;
1117                                 }
1118                                 else {
1119                                         min_fixed_order = max_fixed_order = guess_fixed_order;
1120                                 }
1121                                 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1122                                         if(fixed_residual_bits_per_sample[fixed_order] >= (real)frame_header->bits_per_sample)
1123                                                 continue; /* don't even try */
1124                                         /* 0.5 is for rounding, another 1.0 is to account for the signed->unsigned conversion during rice coding */
1125                                         rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+1.5) : 0;
1126                                         if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
1127                                                 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
1128                                         candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal[channel], encoder->guts->residual[!encoder->guts->best_residual], encoder->guts->abs_residual, frame_header->blocksize, frame_header->bits_per_sample, fixed_order, rice_parameter, max_partition_order, &(encoder->guts->candidate_subframe));
1129                                         if(candidate_bits < best_bits) {
1130                                                 encoder_promote_candidate_subframe_(encoder);
1131                                                 best_bits = candidate_bits;
1132                                         }
1133                                 }
1134
1135                                 /* encode lpc */
1136                                 if(encoder->max_lpc_order > 0) {
1137                                         if(encoder->max_lpc_order >= frame_header->blocksize)
1138                                                 max_lpc_order = frame_header->blocksize-1;
1139                                         else
1140                                                 max_lpc_order = encoder->max_lpc_order;
1141                                         if(max_lpc_order > 0) {
1142                                                 FLAC__lpc_compute_autocorrelation(real_signal[channel], frame_header->blocksize, max_lpc_order+1, autoc);
1143                                                 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
1144                                                 if(encoder->do_exhaustive_model_search) {
1145                                                         min_lpc_order = 1;
1146                                                 }
1147                                                 else {
1148                                                         unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, frame_header->bits_per_sample);
1149                                                         min_lpc_order = max_lpc_order = guess_lpc_order;
1150                                                 }
1151                                                 if(encoder->do_qlp_coeff_prec_search) {
1152                                                         min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
1153                                                         max_qlp_coeff_precision = 32 - frame_header->bits_per_sample - 1;
1154                                                 }
1155                                                 else {
1156                                                         min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
1157                                                 }
1158                                                 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
1159                                                         lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize);
1160                                                         if(lpc_residual_bits_per_sample >= (real)frame_header->bits_per_sample)
1161                                                                 continue; /* don't even try */
1162                                                         /* 0.5 is for rounding, another 1.0 is to account for the signed->unsigned conversion during rice coding */
1163                                                         rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+1.5) : 0;
1164                                                         if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
1165                                                                 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
1166                                                         for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
1167                                                                 candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal[channel], encoder->guts->residual[!encoder->guts->best_residual], encoder->guts->abs_residual, lp_coeff[lpc_order-1], frame_header->blocksize, frame_header->bits_per_sample, lpc_order, qlp_coeff_precision, rice_parameter, max_partition_order, &(encoder->guts->candidate_subframe));
1168                                                                 if(candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1169                                                                         if(candidate_bits < best_bits) {
1170                                                                                 encoder_promote_candidate_subframe_(encoder);
1171                                                                                 best_bits = candidate_bits;
1172                                                                         }
1173                                                                 }
1174                                                         }
1175                                                 }
1176                                         }
1177                                 }
1178                         }
1179                 }
1180
1181                 /* add the best subframe */
1182                 switch(encoder->guts->best_subframe.type) {
1183                         case FLAC__SUBFRAME_TYPE_CONSTANT:
1184                                 if(!FLAC__subframe_add_constant(&(encoder->guts->best_subframe.data.constant), frame_header->bits_per_sample, frame)) {
1185                                         encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1186                                         return false;
1187                                 }
1188                                 break;
1189                         case FLAC__SUBFRAME_TYPE_FIXED:
1190                                 encoder->guts->best_subframe.data.fixed.residual = encoder->guts->residual[encoder->guts->best_residual];
1191                                 if(!FLAC__subframe_add_fixed(&(encoder->guts->best_subframe.data.fixed), frame_header->blocksize - encoder->guts->best_subframe.data.fixed.order, frame_header->bits_per_sample, frame)) {
1192                                         encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1193                                         return false;
1194                                 }
1195                                 break;
1196                         case FLAC__SUBFRAME_TYPE_LPC:
1197                                 encoder->guts->best_subframe.data.lpc.residual = encoder->guts->residual[encoder->guts->best_residual];
1198                                 if(!FLAC__subframe_add_lpc(&(encoder->guts->best_subframe.data.lpc), frame_header->blocksize - encoder->guts->best_subframe.data.lpc.order, frame_header->bits_per_sample, frame)) {
1199                                         encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1200                                         return false;
1201                                 }
1202                                 break;
1203                         case FLAC__SUBFRAME_TYPE_VERBATIM:
1204                                 encoder->guts->best_subframe.data.verbatim.data = integer_signal[channel];
1205                                 if(!FLAC__subframe_add_verbatim(&(encoder->guts->best_subframe.data.verbatim), frame_header->blocksize, frame_header->bits_per_sample, frame)) {
1206                                         encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1207                                         return false;
1208                                 }
1209                                 break;
1210                 }
1211         }
1212
1213         return true;
1214 }
1215 #endif