add MD5 support
[flac.git] / src / libFLAC / encoder.c
1 /* libFLAC - Free Lossless Audio Coder library
2  * Copyright (C) 2000  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[2];                         /* where the candidate and best subframe residual signals will be stored */
48         unsigned best_residual;                     /* index into the above */
49         FLAC__BitBuffer frame;                      /* the current frame being worked on */
50         FLAC__BitBuffer frame_mid_side;             /* special parallel workspace for the mid-side coded version of the current frame */
51         FLAC__BitBuffer frame_left_side;            /* special parallel workspace for the left-side coded version of the current frame */
52         FLAC__BitBuffer frame_right_side;           /* special parallel workspace for the right-side coded version of the current frame */
53         FLAC__SubframeHeader best_subframe, candidate_subframe;
54         bool current_frame_can_do_mid_side;         /* encoder sets this false when any given sample of a frame's side channel exceeds 16 bits */
55         FLAC__StreamMetaData metadata;
56         unsigned current_sample_number;
57         unsigned current_frame_number;
58         struct MD5Context md5context;
59         FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
60         void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
61         void *client_data;
62 } FLAC__EncoderPrivate;
63
64 static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
65 static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
66 static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame, const FLAC__FrameHeader *frame_header, unsigned channels, const int32 *integer_signal[], const real *real_signal[], FLAC__BitBuffer *bitbuffer);
67 static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__SubframeHeader *subframe);
68 static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__SubframeHeader *subframe);
69 static unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 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__SubframeHeader *subframe);
70 static unsigned encoder_evaluate_verbatim_subframe_(unsigned blocksize, unsigned bits_per_sample, FLAC__SubframeHeader *subframe);
71 static unsigned encoder_find_best_partition_order_(int32 residual[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned max_partition_order, unsigned *best_partition_order, unsigned best_parameters[]);
72 static bool encoder_generate_constant_subframe_(const FLAC__SubframeHeader *header, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer);
73 static bool encoder_generate_fixed_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer);
74 static bool encoder_generate_lpc_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer);
75 static bool encoder_generate_verbatim_subframe_(const FLAC__SubframeHeader *header, const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer);
76 static void encoder_promote_candidate_subframe_(FLAC__Encoder *encoder);
77 static bool encoder_set_partitioned_rice_(const int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameter, const unsigned partition_order, unsigned parameters[], unsigned *bits);
78
79 const char *FLAC__EncoderWriteStatusString[] = {
80         "FLAC__ENCODER_WRITE_OK",
81         "FLAC__ENCODER_WRITE_FATAL_ERROR"
82 };
83
84 const char *FLAC__EncoderStateString[] = {
85         "FLAC__ENCODER_OK",
86         "FLAC__ENCODER_UNINITIALIZED",
87         "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
88         "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
89         "FLAC__ENCODER_INVALID_SAMPLE_RATE",
90         "FLAC__ENCODER_INVALID_BLOCK_SIZE",
91         "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
92         "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
93         "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
94         "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
95         "FLAC__ENCODER_NOT_STREAMABLE",
96         "FLAC__ENCODER_FRAMING_ERROR",
97         "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
98         "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
99         "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
100 };
101
102
103 bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
104 {
105         bool ok;
106         unsigned i;
107         int32 *previous_is, *current_is;
108         real *previous_rs, *current_rs;
109         int32 *residual;
110
111         assert(new_size > 0);
112         assert(encoder->state == FLAC__ENCODER_OK);
113         assert(encoder->guts->current_sample_number == 0);
114
115         /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
116         if(new_size <= encoder->guts->input_capacity)
117                 return true;
118
119         ok = 1;
120         if(ok) {
121                 for(i = 0; ok && i < encoder->channels; i++) {
122                         /* integer version of the signal */
123                         previous_is = encoder->guts->integer_signal[i];
124                         current_is = (int32*)malloc(sizeof(int32) * new_size);
125                         if(0 == current_is) {
126                                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
127                                 ok = 0;
128                         }
129                         else {
130                                 encoder->guts->integer_signal[i] = current_is;
131                                 if(previous_is != 0)
132                                         free(previous_is);
133                         }
134                         /* real version of the signal */
135                         previous_rs = encoder->guts->real_signal[i];
136                         current_rs = (real*)malloc(sizeof(real) * new_size);
137                         if(0 == current_rs) {
138                                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
139                                 ok = 0;
140                         }
141                         else {
142                                 encoder->guts->real_signal[i] = current_rs;
143                                 if(previous_rs != 0)
144                                         free(previous_rs);
145                         }
146                 }
147         }
148         if(ok) {
149                 for(i = 0; ok && i < 2; i++) {
150                         /* integer version of the signal */
151                         previous_is = encoder->guts->integer_signal_mid_side[i];
152                         current_is = (int32*)malloc(sizeof(int32) * new_size);
153                         if(0 == current_is) {
154                                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
155                                 ok = 0;
156                         }
157                         else {
158                                 encoder->guts->integer_signal_mid_side[i] = current_is;
159                                 if(previous_is != 0)
160                                         free(previous_is);
161                         }
162                         /* real version of the signal */
163                         previous_rs = encoder->guts->real_signal_mid_side[i];
164                         current_rs = (real*)malloc(sizeof(real) * new_size);
165                         if(0 == current_rs) {
166                                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
167                                 ok = 0;
168                         }
169                         else {
170                                 encoder->guts->real_signal_mid_side[i] = current_rs;
171                                 if(previous_rs != 0)
172                                         free(previous_rs);
173                         }
174                 }
175         }
176         if(ok) {
177                 for(i = 0; i < 2; i++) {
178                         residual = (int32*)malloc(sizeof(int32) * new_size);
179                         if(0 == residual) {
180                                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
181                                 ok = 0;
182                         }
183                         else {
184                                 if(encoder->guts->residual[i] != 0)
185                                         free(encoder->guts->residual[i]);
186                                 encoder->guts->residual[i] = residual;
187                         }
188                 }
189         }
190         if(ok)
191                 encoder->guts->input_capacity = new_size;
192
193         return ok;
194 }
195
196 FLAC__Encoder *FLAC__encoder_get_new_instance()
197 {
198         FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
199         if(encoder != 0) {
200                 encoder->state = FLAC__ENCODER_UNINITIALIZED;
201                 encoder->guts = 0;
202         }
203         return encoder;
204 }
205
206 void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
207 {
208         assert(encoder != 0);
209         free(encoder);
210 }
211
212 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)
213 {
214         unsigned i;
215
216         assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
217         assert(encoder != 0);
218         assert(write_callback != 0);
219         assert(metadata_callback != 0);
220         assert(encoder->state == FLAC__ENCODER_UNINITIALIZED);
221         assert(encoder->guts == 0);
222
223         encoder->state = FLAC__ENCODER_OK;
224
225         if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
226                 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
227
228         if(encoder->do_mid_side_stereo && encoder->channels != 2)
229                 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
230
231         if(encoder->do_mid_side_stereo && encoder->bits_per_sample > 16)
232                 return encoder->state = FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH;
233
234         if(encoder->bits_per_sample == 0 || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
235                 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
236
237         if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
238                 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
239
240         if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
241                 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
242
243         if(encoder->blocksize < encoder->max_lpc_order)
244                 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
245
246         if(encoder->qlp_coeff_precision == 0) {
247                 if(encoder->bits_per_sample < 16) {
248                         /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
249                         /* @@@ until then we'll make a guess */
250                         encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
251                 }
252                 else if(encoder->bits_per_sample == 16) {
253                         if(encoder->blocksize <= 192)
254                                 encoder->qlp_coeff_precision = 7;
255                         else if(encoder->blocksize <= 384)
256                                 encoder->qlp_coeff_precision = 8;
257                         else if(encoder->blocksize <= 576)
258                                 encoder->qlp_coeff_precision = 9;
259                         else if(encoder->blocksize <= 1152)
260                                 encoder->qlp_coeff_precision = 10;
261                         else if(encoder->blocksize <= 2304)
262                                 encoder->qlp_coeff_precision = 11;
263                         else if(encoder->blocksize <= 4608)
264                                 encoder->qlp_coeff_precision = 12;
265                         else
266                                 encoder->qlp_coeff_precision = 13;
267                 }
268                 else {
269                         encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
270                 }
271         }
272         else if(encoder->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->qlp_coeff_precision + encoder->bits_per_sample >= 8*sizeof(uint32))
273                 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
274
275         if(encoder->streamable_subset) {
276                 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)
277                         return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
278                 if(encoder->sample_rate > 655350)
279                         return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
280         }
281
282         if(encoder->rice_optimization_level >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
283                 encoder->rice_optimization_level = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
284
285         encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
286         if(encoder->guts == 0)
287                 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
288
289         encoder->guts->input_capacity = 0;
290         for(i = 0; i < encoder->channels; i++) {
291                 encoder->guts->integer_signal[i] = 0;
292                 encoder->guts->real_signal[i] = 0;
293         }
294         for(i = 0; i < 2; i++) {
295                 encoder->guts->integer_signal_mid_side[i] = 0;
296                 encoder->guts->real_signal_mid_side[i] = 0;
297         }
298         encoder->guts->residual[0] = 0;
299         encoder->guts->residual[1] = 0;
300         encoder->guts->best_residual = 0;
301         encoder->guts->current_frame_can_do_mid_side = true;
302         encoder->guts->current_sample_number = 0;
303         encoder->guts->current_frame_number = 0;
304
305         if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
306                 /* the above function sets the state for us in case of an error */
307                 return encoder->state;
308         }
309         FLAC__bitbuffer_init(&encoder->guts->frame);
310         encoder->guts->write_callback = write_callback;
311         encoder->guts->metadata_callback = metadata_callback;
312         encoder->guts->client_data = client_data;
313
314         /*
315          * write the stream header
316          */
317         if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
318                 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
319
320         if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
321                 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
322
323         encoder->guts->metadata.type = FLAC__METADATA_TYPE_ENCODING;
324         encoder->guts->metadata.is_last = true;
325         encoder->guts->metadata.length = FLAC__STREAM_METADATA_ENCODING_LENGTH;
326         encoder->guts->metadata.data.encoding.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
327         encoder->guts->metadata.data.encoding.max_blocksize = encoder->blocksize;
328         encoder->guts->metadata.data.encoding.min_framesize = 0; /* we don't know this yet; have to fill it in later */
329         encoder->guts->metadata.data.encoding.max_framesize = 0; /* we don't know this yet; have to fill it in later */
330         encoder->guts->metadata.data.encoding.sample_rate = encoder->sample_rate;
331         encoder->guts->metadata.data.encoding.channels = encoder->channels;
332         encoder->guts->metadata.data.encoding.bits_per_sample = encoder->bits_per_sample;
333         encoder->guts->metadata.data.encoding.total_samples = 0; /* we don't know this yet; have to fill it in later */
334         memset(encoder->guts->metadata.data.encoding.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
335         MD5Init(&encoder->guts->md5context);
336         if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
337                 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
338
339         assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
340         assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
341         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)
342                 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
343
344         /* now that the metadata block is written, we can init this to an absurdly-high value */
345         encoder->guts->metadata.data.encoding.min_framesize = (1u << FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN) - 1;
346
347         return encoder->state;
348 }
349
350 void FLAC__encoder_finish(FLAC__Encoder *encoder)
351 {
352         unsigned i;
353
354         assert(encoder != 0);
355         if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
356                 return;
357         if(encoder->guts->current_sample_number != 0) {
358                 encoder->blocksize = encoder->guts->current_sample_number;
359                 encoder_process_frame_(encoder, true); /* true => is last frame */
360         }
361         MD5Final(encoder->guts->metadata.data.encoding.md5sum, &encoder->guts->md5context);
362         encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
363         if(encoder->guts != 0) {
364                 for(i = 0; i < encoder->channels; i++) {
365                         if(encoder->guts->integer_signal[i] != 0) {
366                                 free(encoder->guts->integer_signal[i]);
367                                 encoder->guts->integer_signal[i] = 0;
368                         }
369                         if(encoder->guts->real_signal[i] != 0) {
370                                 free(encoder->guts->real_signal[i]);
371                                 encoder->guts->real_signal[i] = 0;
372                         }
373                 }
374                 for(i = 0; i < 2; i++) {
375                         if(encoder->guts->integer_signal_mid_side[i] != 0) {
376                                 free(encoder->guts->integer_signal_mid_side[i]);
377                                 encoder->guts->integer_signal_mid_side[i] = 0;
378                         }
379                         if(encoder->guts->real_signal_mid_side[i] != 0) {
380                                 free(encoder->guts->real_signal_mid_side[i]);
381                                 encoder->guts->real_signal_mid_side[i] = 0;
382                         }
383                 }
384                 for(i = 0; i < 2; i++) {
385                         if(encoder->guts->residual[i] != 0) {
386                                 free(encoder->guts->residual[i]);
387                                 encoder->guts->residual[i] = 0;
388                         }
389                 }
390                 FLAC__bitbuffer_free(&encoder->guts->frame);
391                 free(encoder->guts);
392                 encoder->guts = 0;
393         }
394         encoder->state = FLAC__ENCODER_UNINITIALIZED;
395 }
396
397 bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
398 {
399         unsigned i, j, channel;
400         int32 x, mid, side;
401         const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
402         const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
403         const int32 max_side =  ((int64)1 << (encoder->bits_per_sample-1)) - 1;
404
405         assert(encoder != 0);
406         assert(encoder->state == FLAC__ENCODER_OK);
407
408         j = 0;
409         do {
410                 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) {
411                         for(channel = 0; channel < encoder->channels; channel++) {
412                                 x = buf[channel][j];
413                                 encoder->guts->integer_signal[channel][i] = x;
414                                 encoder->guts->real_signal[channel][i] = (real)x;
415                         }
416                         if(ms && encoder->guts->current_frame_can_do_mid_side) {
417                                 side = buf[0][j] - buf[1][j];
418                                 if(side < min_side || side > max_side) {
419                                         encoder->guts->current_frame_can_do_mid_side = false;
420                                 }
421                                 else {
422                                         mid = (buf[0][j] + buf[1][j]) >> 1; /* NOTE: not the same as divide-by-two ! */
423                                         encoder->guts->integer_signal_mid_side[0][i] = mid;
424                                         encoder->guts->integer_signal_mid_side[1][i] = side;
425                                         encoder->guts->real_signal_mid_side[0][i] = (real)mid;
426                                         encoder->guts->real_signal_mid_side[1][i] = (real)side;
427                                 }
428                         }
429                         encoder->guts->current_sample_number++;
430                 }
431                 if(i == encoder->blocksize) {
432                         if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
433                                 return false;
434                 }
435         } while(j < samples);
436
437         return true;
438 }
439
440 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
441 bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
442 {
443         unsigned i, j, k, channel;
444         int32 x, left = 0, mid, side;
445         const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
446         const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
447         const int32 max_side =  ((int64)1 << (encoder->bits_per_sample-1)) - 1;
448
449         assert(encoder != 0);
450         assert(encoder->state == FLAC__ENCODER_OK);
451
452         j = k = 0;
453         do {
454                 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) {
455                         for(channel = 0; channel < encoder->channels; channel++, k++) {
456                                 x = buf[k];
457                                 encoder->guts->integer_signal[channel][i] = x;
458                                 encoder->guts->real_signal[channel][i] = (real)x;
459                                 if(ms && encoder->guts->current_frame_can_do_mid_side) {
460                                         if(channel == 0) {
461                                                 left = x;
462                                         }
463                                         else {
464                                                 side = left - x;
465                                                 if(side < min_side || side > max_side) {
466                                                         encoder->guts->current_frame_can_do_mid_side = false;
467                                                 }
468                                                 else {
469                                                         mid = (left + x) >> 1; /* NOTE: not the same as divide-by-two ! */
470                                                         encoder->guts->integer_signal_mid_side[0][i] = mid;
471                                                         encoder->guts->integer_signal_mid_side[1][i] = side;
472                                                         encoder->guts->real_signal_mid_side[0][i] = (real)mid;
473                                                         encoder->guts->real_signal_mid_side[1][i] = (real)side;
474                                                 }
475                                         }
476                                 }
477                         }
478                         encoder->guts->current_sample_number++;
479                 }
480                 if(i == encoder->blocksize) {
481                         if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
482                                 return false;
483                 }
484         } while(j < samples);
485
486         return true;
487 }
488
489 bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
490 {
491         FLAC__FrameHeader frame_header;
492         FLAC__BitBuffer *smallest_frame;
493
494         assert(encoder->state == FLAC__ENCODER_OK);
495
496         /*
497          * Accumulate raw signal to the MD5 signature
498          */
499         if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
500                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
501                 return false;
502         }
503
504         /*
505          * First do a normal encoding pass
506          */
507         frame_header.blocksize = encoder->blocksize;
508         frame_header.sample_rate = encoder->sample_rate;
509         frame_header.channels = encoder->channels;
510         frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
511         frame_header.bits_per_sample = encoder->bits_per_sample;
512         frame_header.number.frame_number = encoder->guts->current_frame_number;
513
514         if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
515                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
516                 return false;
517         }
518         if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
519                 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
520                 return false;
521         }
522
523         if(!encoder_process_subframes_(encoder, is_last_frame, &frame_header, encoder->channels, encoder->guts->integer_signal, encoder->guts->real_signal, &encoder->guts->frame))
524                 return false;
525
526         smallest_frame = &encoder->guts->frame;
527
528         /*
529          * Now try a mid-side version if necessary; otherwise, just use the previous step's frame
530          */
531         if(encoder->do_mid_side_stereo && encoder->guts->current_frame_can_do_mid_side) {
532                 int32 *integer_signal[2];
533                 real *real_signal[2];
534
535                 assert(encoder->channels == 2);
536
537                 /* mid-side */
538                 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
539                 if(!FLAC__bitbuffer_clear(&encoder->guts->frame_mid_side)) {
540                         encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
541                         return false;
542                 }
543                 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame_mid_side)) {
544                         encoder->state = FLAC__ENCODER_FRAMING_ERROR;
545                         return false;
546                 }
547                 integer_signal[0] = encoder->guts->integer_signal_mid_side[0]; /* mid channel */
548                 integer_signal[1] = encoder->guts->integer_signal_mid_side[1]; /* side channel */
549                 real_signal[0] = encoder->guts->real_signal_mid_side[0]; /* mid channel */
550                 real_signal[1] = encoder->guts->real_signal_mid_side[1]; /* side channel */
551                 if(!encoder_process_subframes_(encoder, is_last_frame, &frame_header, encoder->channels, integer_signal, real_signal, &encoder->guts->frame_mid_side))
552                         return false;
553                 if(encoder->guts->frame_mid_side.total_bits < smallest_frame->total_bits)
554                         smallest_frame = &encoder->guts->frame_mid_side;
555
556                 /* left-side */
557                 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
558                 if(!FLAC__bitbuffer_clear(&encoder->guts->frame_left_side)) {
559                         encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
560                         return false;
561                 }
562                 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame_left_side)) {
563                         encoder->state = FLAC__ENCODER_FRAMING_ERROR;
564                         return false;
565                 }
566                 integer_signal[0] = encoder->guts->integer_signal[0]; /* left channel */
567                 integer_signal[1] = encoder->guts->integer_signal_mid_side[1]; /* side channel */
568                 real_signal[0] = encoder->guts->real_signal[0]; /* left channel */
569                 real_signal[1] = encoder->guts->real_signal_mid_side[1]; /* side channel */
570                 if(!encoder_process_subframes_(encoder, is_last_frame, &frame_header, encoder->channels, integer_signal, real_signal, &encoder->guts->frame_left_side))
571                         return false;
572                 if(encoder->guts->frame_left_side.total_bits < smallest_frame->total_bits)
573                         smallest_frame = &encoder->guts->frame_left_side;
574
575                 /* right-side */
576                 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
577                 if(!FLAC__bitbuffer_clear(&encoder->guts->frame_right_side)) {
578                         encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
579                         return false;
580                 }
581                 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame_right_side)) {
582                         encoder->state = FLAC__ENCODER_FRAMING_ERROR;
583                         return false;
584                 }
585                 integer_signal[0] = encoder->guts->integer_signal_mid_side[1]; /* side channel */
586                 integer_signal[1] = encoder->guts->integer_signal[1]; /* right channel */
587                 real_signal[0] = encoder->guts->real_signal_mid_side[1]; /* side channel */
588                 real_signal[1] = encoder->guts->real_signal[1]; /* right channel */
589                 if(!encoder_process_subframes_(encoder, is_last_frame, &frame_header, encoder->channels, integer_signal, real_signal, &encoder->guts->frame_right_side))
590                         return false;
591                 if(encoder->guts->frame_right_side.total_bits < smallest_frame->total_bits)
592                         smallest_frame = &encoder->guts->frame_right_side;
593         }
594
595         /*
596          * Zero-pad the frame to a byte_boundary
597          */
598         if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(smallest_frame)) {
599                 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
600                 return false;
601         }
602
603         /*
604          * Write it
605          */
606         assert(smallest_frame->bits == 0); /* assert that we're byte-aligned before writing */
607         assert(smallest_frame->total_consumed_bits == 0); /* assert that no reading of the buffer was done */
608         if(encoder->guts->write_callback(encoder, smallest_frame->buffer, smallest_frame->bytes, encoder->blocksize, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK) {
609                 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
610                 return false;
611         }
612
613         /*
614          * Get ready for the next frame
615          */
616         encoder->guts->current_frame_can_do_mid_side = true;
617         encoder->guts->current_sample_number = 0;
618         encoder->guts->current_frame_number++;
619         encoder->guts->metadata.data.encoding.total_samples += (uint64)encoder->blocksize;
620         encoder->guts->metadata.data.encoding.min_framesize = min(smallest_frame->bytes, encoder->guts->metadata.data.encoding.min_framesize);
621         encoder->guts->metadata.data.encoding.max_framesize = max(smallest_frame->bytes, encoder->guts->metadata.data.encoding.max_framesize);
622
623         return true;
624 }
625
626 bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame, const FLAC__FrameHeader *frame_header, unsigned channels, const int32 *integer_signal[], const real *real_signal[], FLAC__BitBuffer *frame)
627 {
628         real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
629         real lpc_residual_bits_per_sample;
630         real autoc[FLAC__MAX_LPC_ORDER+1];
631         real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
632         real lpc_error[FLAC__MAX_LPC_ORDER];
633         unsigned channel;
634         unsigned min_lpc_order, max_lpc_order, lpc_order;
635         unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
636         unsigned max_partition_order;
637         unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
638         unsigned rice_parameter;
639         unsigned candidate_bits, best_bits;
640
641         if(is_last_frame) {
642                 max_partition_order = 0;
643         }
644         else {
645                 unsigned limit = 0, b = encoder->blocksize;
646                 while(!(b & 1)) {
647                         limit++;
648                         b >>= 1;
649                 }
650                 max_partition_order = min(encoder->rice_optimization_level, limit);
651         }
652
653         for(channel = 0; channel < channels; channel++) {
654                 /* verbatim subframe is the baseline against which we measure other compressed subframes */
655                 best_bits = encoder_evaluate_verbatim_subframe_(frame_header->blocksize, frame_header->bits_per_sample, &(encoder->guts->best_subframe));
656
657                 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
658                         /* check for constant subframe */
659                         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);
660                         if(fixed_residual_bits_per_sample[1] == 0.0) {
661                                 /* the above means integer_signal[channel]+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
662                                 unsigned i, signal_is_constant = true;
663                                 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
664                                         if(integer_signal[channel][0] != integer_signal[channel][i]) {
665                                                 signal_is_constant = false;
666                                                 break;
667                                         }
668                                 }
669                                 if(signal_is_constant) {
670                                         candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[channel][0], frame_header->bits_per_sample, &(encoder->guts->candidate_subframe));
671                                         if(candidate_bits < best_bits) {
672                                                 encoder_promote_candidate_subframe_(encoder);
673                                                 best_bits = candidate_bits;
674                                         }
675                                 }
676                         }
677                         else {
678                                 /* encode fixed */
679                                 if(encoder->do_exhaustive_model_search) {
680                                         min_fixed_order = 0;
681                                         max_fixed_order = FLAC__MAX_FIXED_ORDER;
682                                 }
683                                 else {
684                                         min_fixed_order = max_fixed_order = guess_fixed_order;
685                                 }
686                                 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
687                                         if(fixed_residual_bits_per_sample[fixed_order] >= (real)frame_header->bits_per_sample)
688                                                 continue; /* don't even try */
689                                         /* 0.5 is for rounding, another 1.0 is to account for the signed->unsigned conversion during rice coding */
690                                         rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+1.5) : 0;
691                                         if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
692                                                 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
693                                         candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal[channel], encoder->guts->residual[!encoder->guts->best_residual], frame_header->blocksize, frame_header->bits_per_sample, fixed_order, rice_parameter, max_partition_order, &(encoder->guts->candidate_subframe));
694                                         if(candidate_bits < best_bits) {
695                                                 encoder_promote_candidate_subframe_(encoder);
696                                                 best_bits = candidate_bits;
697                                         }
698                                 }
699
700                                 /* encode lpc */
701                                 if(encoder->max_lpc_order > 0) {
702                                         if(encoder->max_lpc_order >= frame_header->blocksize)
703                                                 max_lpc_order = frame_header->blocksize-1;
704                                         else
705                                                 max_lpc_order = encoder->max_lpc_order;
706                                         if(max_lpc_order > 0) {
707                                                 FLAC__lpc_compute_autocorrelation(real_signal[channel], frame_header->blocksize, max_lpc_order+1, autoc);
708                                                 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
709                                                 if(encoder->do_exhaustive_model_search) {
710                                                         min_lpc_order = 1;
711                                                 }
712                                                 else {
713                                                         unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, frame_header->bits_per_sample);
714                                                         min_lpc_order = max_lpc_order = guess_lpc_order;
715                                                 }
716                                                 if(encoder->do_qlp_coeff_prec_search) {
717                                                         min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
718                                                         max_qlp_coeff_precision = 32 - frame_header->bits_per_sample - 1;
719                                                 }
720                                                 else {
721                                                         min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
722                                                 }
723                                                 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
724                                                         lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize);
725                                                         if(lpc_residual_bits_per_sample >= (real)frame_header->bits_per_sample)
726                                                                 continue; /* don't even try */
727                                                         /* 0.5 is for rounding, another 1.0 is to account for the signed->unsigned conversion during rice coding */
728                                                         rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+1.5) : 0;
729                                                         if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
730                                                                 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
731                                                         for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
732                                                                 candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal[channel], encoder->guts->residual[!encoder->guts->best_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));
733                                                                 if(candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
734                                                                         if(candidate_bits < best_bits) {
735                                                                                 encoder_promote_candidate_subframe_(encoder);
736                                                                                 best_bits = candidate_bits;
737                                                                         }
738                                                                 }
739                                                         }
740                                                 }
741                                         }
742                                 }
743                         }
744                 }
745
746                 /* add the best subframe */
747                 switch(encoder->guts->best_subframe.type) {
748                         case FLAC__SUBFRAME_TYPE_CONSTANT:
749                                 if(!encoder_generate_constant_subframe_(&(encoder->guts->best_subframe), frame_header->bits_per_sample, frame)) {
750                                         encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
751                                         return false;
752                                 }
753                                 break;
754                         case FLAC__SUBFRAME_TYPE_FIXED:
755                                 if(!encoder_generate_fixed_subframe_(&(encoder->guts->best_subframe), encoder->guts->residual[encoder->guts->best_residual], frame_header->blocksize, frame_header->bits_per_sample, frame)) {
756                                         encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
757                                         return false;
758                                 }
759                                 break;
760                         case FLAC__SUBFRAME_TYPE_LPC:
761                                 if(!encoder_generate_lpc_subframe_(&(encoder->guts->best_subframe), encoder->guts->residual[encoder->guts->best_residual], frame_header->blocksize, frame_header->bits_per_sample, frame)) {
762                                         encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
763                                         return false;
764                                 }
765                                 break;
766                         case FLAC__SUBFRAME_TYPE_VERBATIM:
767                                 if(!encoder_generate_verbatim_subframe_(&(encoder->guts->best_subframe), integer_signal[channel], frame_header->blocksize, frame_header->bits_per_sample, frame)) {
768                                         encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
769                                         return false;
770                                 }
771                                 break;
772                 }
773         }
774
775         return true;
776 }
777
778 unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__SubframeHeader *subframe)
779 {
780         subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
781         subframe->data.constant.value = signal;
782
783         return FLAC__SUBFRAME_HEADER_TYPE_LEN + bits_per_sample;
784 }
785
786 unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__SubframeHeader *subframe)
787 {
788         unsigned i, residual_bits;
789         const unsigned residual_samples = blocksize - order;
790
791         FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
792
793         subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
794
795         subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
796
797         residual_bits = encoder_find_best_partition_order_(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);
798
799         subframe->data.fixed.order = order;
800         for(i = 0; i < order; i++)
801                 subframe->data.fixed.warmup[i] = signal[i];
802
803         return FLAC__SUBFRAME_HEADER_TYPE_LEN + (order * bits_per_sample) + residual_bits;
804 }
805
806 unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 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__SubframeHeader *subframe)
807 {
808         int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
809         unsigned i, residual_bits;
810         int quantization, ret;
811         const unsigned residual_samples = blocksize - order;
812
813         ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, bits_per_sample, qlp_coeff, &quantization);
814         if(ret != 0)
815                 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
816
817         FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
818
819         subframe->type = FLAC__SUBFRAME_TYPE_LPC;
820
821         subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
822
823         residual_bits = encoder_find_best_partition_order_(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);
824
825         subframe->data.lpc.order = order;
826         subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
827         subframe->data.lpc.quantization_level = quantization;
828         memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
829         for(i = 0; i < order; i++)
830                 subframe->data.lpc.warmup[i] = signal[i];
831
832         return FLAC__SUBFRAME_HEADER_TYPE_LEN + FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + bits_per_sample)) + residual_bits;
833 }
834
835 unsigned encoder_evaluate_verbatim_subframe_(unsigned blocksize, unsigned bits_per_sample, FLAC__SubframeHeader *subframe)
836 {
837         subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
838
839         return FLAC__SUBFRAME_HEADER_TYPE_LEN + (blocksize * bits_per_sample);
840 }
841
842 unsigned encoder_find_best_partition_order_(int32 residual[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned max_partition_order, unsigned *best_partition_order, unsigned best_parameters[])
843 {
844         unsigned residual_bits, best_residual_bits = 0;
845         unsigned partition_order;
846         unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
847
848         for(partition_order = 0; partition_order <= max_partition_order; partition_order++) {
849                 if(!encoder_set_partitioned_rice_(residual, residual_samples, predictor_order, rice_parameter, partition_order, parameters[!best_parameters_index], &residual_bits)) {
850                         assert(best_residual_bits != 0);
851                         break;
852                 }
853                 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
854                         best_residual_bits = residual_bits;
855                         *best_partition_order = partition_order;
856                         best_parameters_index = !best_parameters_index;
857                 }
858         }
859         memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
860
861         return best_residual_bits;
862 }
863
864 bool encoder_generate_constant_subframe_(const FLAC__SubframeHeader *header, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer)
865 {
866         assert(header->type == FLAC__SUBFRAME_TYPE_CONSTANT);
867         return FLAC__subframe_add_constant(bits_per_sample, header, bitbuffer);
868 }
869
870 bool encoder_generate_fixed_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer)
871 {
872         assert(header->type == FLAC__SUBFRAME_TYPE_FIXED);
873         return FLAC__subframe_add_fixed(residual, blocksize - header->data.fixed.order, bits_per_sample, header, bitbuffer);
874 }
875
876 bool encoder_generate_lpc_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer)
877 {
878         assert(header->type == FLAC__SUBFRAME_TYPE_LPC);
879         return FLAC__subframe_add_lpc(residual, blocksize - header->data.lpc.order, bits_per_sample, header, bitbuffer);
880 }
881
882 bool encoder_generate_verbatim_subframe_(const FLAC__SubframeHeader *header, const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer)
883 {
884         assert(header->type == FLAC__SUBFRAME_TYPE_VERBATIM);
885 #ifdef NDEBUG
886         (void)header; /* silence compiler warning about unused parameter */
887 #endif
888         return FLAC__subframe_add_verbatim(signal, blocksize, bits_per_sample, bitbuffer);
889 }
890
891 void encoder_promote_candidate_subframe_(FLAC__Encoder *encoder)
892 {
893         assert(encoder->state == FLAC__ENCODER_OK);
894         encoder->guts->best_subframe = encoder->guts->candidate_subframe;
895         encoder->guts->best_residual = !encoder->guts->best_residual;
896 }
897
898 bool encoder_set_partitioned_rice_(const int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameter, const unsigned partition_order, unsigned parameters[], unsigned *bits)
899 {
900         unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
901
902         if(partition_order == 0) {
903                 unsigned i;
904                 parameters[0] = rice_parameter;
905                 bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
906                 for(i = 0; i < residual_samples; i++)
907                         bits_ += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter);
908         }
909         else {
910                 unsigned i, j, k = 0, k_last = 0;
911                 unsigned mean, parameter, partition_samples;
912                 const unsigned max_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
913                 for(i = 0; i < (1u<<partition_order); i++) {
914                         partition_samples = (residual_samples+predictor_order) >> partition_order;
915                         if(i == 0) {
916                                 if(partition_samples <= predictor_order)
917                                         return false;
918                                 else
919                                         partition_samples -= predictor_order;
920                         }
921                         mean = partition_samples >> 1;
922                         for(j = 0; j < partition_samples; j++, k++)
923                                 mean += ((residual[k] < 0)? (unsigned)(-residual[k]) : (unsigned)residual[k]);
924                         mean /= partition_samples;
925                         /* calc parameter = floor(log2(mean)) + 1 */
926                         parameter = 0;
927                         while(mean) {
928                                 parameter++;
929                                 mean >>= 1;
930                         }
931                         if(parameter > max_parameter)
932                                 parameter = max_parameter;
933                         parameters[i] = parameter;
934                         bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
935                         for(j = k_last; j < k; j++)
936                                 bits_ += FLAC__bitbuffer_rice_bits(residual[j], parameter);
937                         k_last = k;
938                 }
939         }
940
941         *bits = bits_;
942         return true;
943 }